LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.5 % 9900 9260
Test Date: 2026-06-20 15:32:29 Functions: 99.6 % 254 253
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Perform type resolution on the various structures.
       2              :    Copyright (C) 2001-2026 Free Software Foundation, Inc.
       3              :    Contributed by Andy Vaught
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "options.h"
      25              : #include "bitmap.h"
      26              : #include "gfortran.h"
      27              : #include "arith.h"  /* For gfc_compare_expr().  */
      28              : #include "dependency.h"
      29              : #include "data.h"
      30              : #include "target-memory.h" /* for gfc_simplify_transfer */
      31              : #include "constructor.h"
      32              : 
      33              : /* Types used in equivalence statements.  */
      34              : 
      35              : enum seq_type
      36              : {
      37              :   SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
      38              : };
      39              : 
      40              : /* Stack to keep track of the nesting of blocks as we move through the
      41              :    code.  See resolve_branch() and gfc_resolve_code().  */
      42              : 
      43              : typedef struct code_stack
      44              : {
      45              :   struct gfc_code *head, *current;
      46              :   struct code_stack *prev;
      47              : 
      48              :   /* This bitmap keeps track of the targets valid for a branch from
      49              :      inside this block except for END {IF|SELECT}s of enclosing
      50              :      blocks.  */
      51              :   bitmap reachable_labels;
      52              : }
      53              : code_stack;
      54              : 
      55              : static code_stack *cs_base = NULL;
      56              : 
      57              : struct check_default_none_data
      58              : {
      59              :   gfc_code *code;
      60              :   hash_set<gfc_symbol *> *sym_hash;
      61              :   gfc_namespace *ns;
      62              :   bool default_none;
      63              : };
      64              : 
      65              : /* Nonzero if we're inside a FORALL or DO CONCURRENT block.  */
      66              : 
      67              : static int forall_flag;
      68              : int gfc_do_concurrent_flag;
      69              : 
      70              : /* True when we are resolving an expression that is an actual argument to
      71              :    a procedure.  */
      72              : static bool actual_arg = false;
      73              : /* True when we are resolving an expression that is the first actual argument
      74              :    to a procedure.  */
      75              : static bool first_actual_arg = false;
      76              : 
      77              : 
      78              : /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block.  */
      79              : 
      80              : static int omp_workshare_flag;
      81              : 
      82              : 
      83              : /* True if we are resolving a specification expression.  */
      84              : static bool specification_expr = false;
      85              : /* The dummy whose character length or array bounds are currently being
      86              :    resolved as a specification expression.  */
      87              : static gfc_symbol *specification_expr_symbol = NULL;
      88              : 
      89              : /* The id of the last entry seen.  */
      90              : static int current_entry_id;
      91              : 
      92              : /* We use bitmaps to determine if a branch target is valid.  */
      93              : static bitmap_obstack labels_obstack;
      94              : 
      95              : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function.  */
      96              : static bool inquiry_argument = false;
      97              : 
      98              : static bool
      99          464 : entry_dummy_seen_p (gfc_symbol *sym)
     100              : {
     101          464 :   gfc_entry_list *entry;
     102          464 :   gfc_formal_arglist *formal;
     103              : 
     104          464 :   gcc_checking_assert (sym->attr.dummy && sym->ns == gfc_current_ns);
     105              : 
     106          464 :   for (entry = gfc_current_ns->entries;
     107          471 :        entry && entry->id <= current_entry_id;
     108            7 :        entry = entry->next)
     109          765 :     for (formal = entry->sym->formal; formal; formal = formal->next)
     110          758 :       if (formal->sym && sym->name == formal->sym->name)
     111              :         return true;
     112              : 
     113              :   return false;
     114              : }
     115              : 
     116              : 
     117              : /* Is the symbol host associated?  */
     118              : static bool
     119        52953 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
     120              : {
     121        57815 :   for (ns = ns->parent; ns; ns = ns->parent)
     122              :     {
     123         5120 :       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      1553698 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     136              : {
     137      1553698 :   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         5636 : check_proc_interface (gfc_symbol *ifc, locus *where)
     158              : {
     159              :   /* Several checks for F08:C1216.  */
     160         5636 :   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         5634 :   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         5630 :   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         5626 :   if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
     187         5626 :       || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
     188           17 :     ifc->attr.intrinsic = 1;
     189         5626 :   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         5623 :   if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
     196              :     {
     197            7 :       gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
     198            7 :       return false;
     199              :     }
     200              :   return true;
     201              : }
     202              : 
     203              : 
     204              : static void resolve_symbol (gfc_symbol *sym);
     205              : 
     206              : 
     207              : /* Resolve the interface for a PROCEDURE declaration or procedure pointer.  */
     208              : 
     209              : static bool
     210         2116 : resolve_procedure_interface (gfc_symbol *sym)
     211              : {
     212         2116 :   gfc_symbol *ifc = sym->ts.interface;
     213              : 
     214         2116 :   if (!ifc)
     215              :     return true;
     216              : 
     217         1956 :   if (ifc == sym)
     218              :     {
     219            2 :       gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
     220              :                  sym->name, &sym->declared_at);
     221            2 :       return false;
     222              :     }
     223         1954 :   if (!check_proc_interface (ifc, &sym->declared_at))
     224              :     return false;
     225              : 
     226         1945 :   if (ifc->attr.if_source || ifc->attr.intrinsic)
     227              :     {
     228              :       /* Resolve interface and copy attributes.  */
     229         1666 :       resolve_symbol (ifc);
     230         1666 :       if (ifc->attr.intrinsic)
     231           14 :         gfc_resolve_intrinsic (ifc, &ifc->declared_at);
     232              : 
     233         1666 :       if (ifc->result)
     234              :         {
     235          779 :           sym->ts = ifc->result->ts;
     236          779 :           sym->attr.allocatable = ifc->result->attr.allocatable;
     237          779 :           sym->attr.pointer = ifc->result->attr.pointer;
     238          779 :           sym->attr.dimension = ifc->result->attr.dimension;
     239          779 :           sym->attr.class_ok = ifc->result->attr.class_ok;
     240          779 :           sym->as = gfc_copy_array_spec (ifc->result->as);
     241          779 :           sym->result = sym;
     242              :         }
     243              :       else
     244              :         {
     245          887 :           sym->ts = ifc->ts;
     246          887 :           sym->attr.allocatable = ifc->attr.allocatable;
     247          887 :           sym->attr.pointer = ifc->attr.pointer;
     248          887 :           sym->attr.dimension = ifc->attr.dimension;
     249          887 :           sym->attr.class_ok = ifc->attr.class_ok;
     250          887 :           sym->as = gfc_copy_array_spec (ifc->as);
     251              :         }
     252         1666 :       sym->ts.interface = ifc;
     253         1666 :       sym->attr.function = ifc->attr.function;
     254         1666 :       sym->attr.subroutine = ifc->attr.subroutine;
     255              : 
     256         1666 :       sym->attr.pure = ifc->attr.pure;
     257         1666 :       sym->attr.elemental = ifc->attr.elemental;
     258         1666 :       sym->attr.contiguous = ifc->attr.contiguous;
     259         1666 :       sym->attr.recursive = ifc->attr.recursive;
     260         1666 :       sym->attr.always_explicit = ifc->attr.always_explicit;
     261         1666 :       sym->attr.ext_attr |= ifc->attr.ext_attr;
     262         1666 :       sym->attr.is_bind_c = ifc->attr.is_bind_c;
     263              :       /* Copy char length.  */
     264         1666 :       if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
     265              :         {
     266           45 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
     267           45 :           if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
     268           53 :               && !gfc_resolve_expr (sym->ts.u.cl->length))
     269              :             return false;
     270              :         }
     271              :     }
     272              : 
     273              :   return true;
     274              : }
     275              : 
     276              : 
     277              : /* Resolve types of formal argument lists.  These have to be done early so that
     278              :    the formal argument lists of module procedures can be copied to the
     279              :    containing module before the individual procedures are resolved
     280              :    individually.  We also resolve argument lists of procedures in interface
     281              :    blocks because they are self-contained scoping units.
     282              : 
     283              :    Since a dummy argument cannot be a non-dummy procedure, the only
     284              :    resort left for untyped names are the IMPLICIT types.  */
     285              : 
     286              : void
     287       527104 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     288              : {
     289       527104 :   gfc_formal_arglist *f;
     290       527104 :   gfc_symbol *sym;
     291       527104 :   bool saved_specification_expr;
     292       527104 :   int i;
     293              : 
     294       527104 :   if (proc->result != NULL)
     295       327854 :     sym = proc->result;
     296              :   else
     297              :     sym = proc;
     298              : 
     299       527104 :   if (gfc_elemental (proc)
     300       364783 :       || sym->attr.pointer || sym->attr.allocatable
     301       879637 :       || (sym->as && sym->as->rank != 0))
     302              :     {
     303       176901 :       proc->attr.always_explicit = 1;
     304       176901 :       sym->attr.always_explicit = 1;
     305              :     }
     306              : 
     307       527104 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     308       527104 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     309              : 
     310      1365506 :   for (f = proc->formal; f; f = f->next)
     311              :     {
     312       838404 :       gfc_array_spec *as;
     313       838404 :       gfc_symbol *saved_specification_expr_symbol;
     314              : 
     315       838404 :       sym = f->sym;
     316              : 
     317       838404 :       if (sym == NULL)
     318              :         {
     319              :           /* Alternate return placeholder.  */
     320          171 :           if (gfc_elemental (proc))
     321            1 :             gfc_error ("Alternate return specifier in elemental subroutine "
     322              :                        "%qs at %L is not allowed", proc->name,
     323              :                        &proc->declared_at);
     324          171 :           if (proc->attr.function)
     325            1 :             gfc_error ("Alternate return specifier in function "
     326              :                        "%qs at %L is not allowed", proc->name,
     327              :                        &proc->declared_at);
     328          171 :           continue;
     329              :         }
     330              : 
     331          599 :       if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
     332       838832 :                && !resolve_procedure_interface (sym))
     333              :         break;
     334              : 
     335       838233 :       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       838231 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     344          891 :         gfc_resolve_formal_arglist (sym);
     345              : 
     346       838231 :       if (sym->attr.subroutine || sym->attr.external)
     347              :         {
     348          901 :           if (sym->attr.flavor == FL_UNKNOWN)
     349            9 :             gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
     350              :         }
     351              :       else
     352              :         {
     353       837330 :           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       838231 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     359       852251 :            ? CLASS_DATA (sym)->as : sym->as;
     360              : 
     361       838231 :       saved_specification_expr = specification_expr;
     362       838231 :       saved_specification_expr_symbol = specification_expr_symbol;
     363       838231 :       specification_expr = true;
     364       838231 :       specification_expr_symbol = sym;
     365       838231 :       gfc_resolve_array_spec (as, 0);
     366       838231 :       specification_expr = saved_specification_expr;
     367       838231 :       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       838231 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     373        12324 :           && ((sym->ts.type != BT_CLASS
     374        11204 :                && !(sym->attr.pointer || sym->attr.allocatable))
     375         5372 :               || (sym->ts.type == BT_CLASS
     376         1120 :                   && !(CLASS_DATA (sym)->attr.class_pointer
     377          920 :                        || CLASS_DATA (sym)->attr.allocatable)))
     378         7457 :           && sym->attr.flavor != FL_PROCEDURE)
     379              :         {
     380         7456 :           as->type = AS_ASSUMED_SHAPE;
     381        17273 :           for (i = 0; i < as->rank; i++)
     382         9817 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     383              :         }
     384              : 
     385       131798 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     386       118032 :           || (as && as->type == AS_ASSUMED_RANK)
     387       786948 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     388       776785 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     389        11835 :               && (CLASS_DATA (sym)->attr.class_pointer
     390        11352 :                   || CLASS_DATA (sym)->attr.allocatable
     391        10454 :                   || CLASS_DATA (sym)->attr.target))
     392       775404 :           || sym->attr.optional)
     393              :         {
     394        78103 :           proc->attr.always_explicit = 1;
     395        78103 :           if (proc->result)
     396        36381 :             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       838231 :       if (sym->attr.flavor == FL_UNKNOWN)
     403        50947 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     404              : 
     405       838231 :       if (gfc_pure (proc))
     406              :         {
     407       327591 :           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       327562 :           else if (!sym->attr.pointer)
     418              :             {
     419       327548 :               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       327548 :               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       327590 :           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       838229 :       if (proc->attr.implicit_pure)
     458              :         {
     459        25022 :           if (sym->attr.flavor == FL_PROCEDURE)
     460              :             {
     461          331 :               if (!gfc_pure (sym))
     462          299 :                 proc->attr.implicit_pure = 0;
     463              :             }
     464        24691 :           else if (!sym->attr.pointer)
     465              :             {
     466        23910 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     467         2741 :                   && !sym->value)
     468         2741 :                 proc->attr.implicit_pure = 0;
     469              : 
     470        23910 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     471         4197 :                   && !sym->value)
     472         4197 :                 proc->attr.implicit_pure = 0;
     473              :             }
     474              :         }
     475              : 
     476       838229 :       if (gfc_elemental (proc))
     477              :         {
     478              :           /* F08:C1289.  */
     479       301958 :           if (sym->attr.codimension
     480       301957 :               || (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       301955 :           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       301953 :           if (sym->attr.allocatable
     497       301952 :               || (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       301951 :           if (sym->attr.pointer
     507       301950 :               || (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       301949 :           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       301947 :           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       838216 :       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       527104 :   if (sym)
     562       527012 :     sym->formal_resolved = 1;
     563       527104 :   gfc_current_ns = orig_current_ns;
     564       527104 : }
     565              : 
     566              : 
     567              : /* Work function called when searching for symbols that have argument lists
     568              :    associated with them.  */
     569              : 
     570              : static void
     571      1864974 : find_arglists (gfc_symbol *sym)
     572              : {
     573      1864974 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     574       333782 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     575              :     return;
     576              : 
     577       331457 :   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       347654 : resolve_formal_arglists (gfc_namespace *ns)
     586              : {
     587            0 :   if (ns == NULL)
     588              :     return;
     589              : 
     590       347654 :   gfc_traverse_ns (ns, find_arglists);
     591              : }
     592              : 
     593              : 
     594              : static void
     595        37390 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     596              : {
     597        37390 :   bool t;
     598              : 
     599        37390 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     600        37390 :       && sym->ns->parent
     601         1445 :       && sym->ns->parent->proc_name
     602         1445 :       && 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        37390 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     610        11006 :       || sym->attr.entry_master)
     611        26573 :     return;
     612              : 
     613        10817 :   if (!sym->result)
     614              :     return;
     615              : 
     616              :   /* Try to find out of what the return type is.  */
     617        10817 :   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        10817 :   if (sym->result->ts.type == BT_CHARACTER)
     642              :     {
     643         1203 :       gfc_charlen *cl = sym->result->ts.u.cl;
     644         1203 :       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       384537 : resolve_entries (gfc_namespace *ns)
     727              : {
     728       384537 :   gfc_namespace *old_ns;
     729       384537 :   gfc_code *c;
     730       384537 :   gfc_symbol *proc;
     731       384537 :   gfc_entry_list *el;
     732              :   /* Provide sufficient space to hold "master.%d.%s".  */
     733       384537 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     734       384537 :   static int master_count = 0;
     735              : 
     736       384537 :   if (ns->proc_name == NULL)
     737       383834 :     return;
     738              : 
     739              :   /* No need to do anything if this procedure doesn't have alternate entry
     740              :      points.  */
     741       384488 :   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       349631 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     996              : {
     997       349631 :   gfc_symbol *csym = common_block->head;
     998       349631 :   gfc_gsymbol *gsym;
     999              : 
    1000       355682 :   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       349631 : }
    1071              : 
    1072              : /* Resolve common blocks.  */
    1073              : static void
    1074       348184 : resolve_common_blocks (gfc_symtree *common_root)
    1075              : {
    1076       348184 :   gfc_symbol *sym = NULL;
    1077       348184 :   gfc_gsymbol * gsym;
    1078              : 
    1079       348184 :   if (common_root == NULL)
    1080       348062 :     return;
    1081              : 
    1082         1977 :   if (common_root->left)
    1083          251 :     resolve_common_blocks (common_root->left);
    1084         1977 :   if (common_root->right)
    1085          279 :     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       347654 : resolve_contained_functions (gfc_namespace *ns)
    1206              : {
    1207       347654 :   gfc_namespace *child;
    1208       347654 :   gfc_entry_list *el;
    1209              : 
    1210       347654 :   resolve_formal_arglists (ns);
    1211              : 
    1212       384537 :   for (child = ns->contained; child; child = child->sibling)
    1213              :     {
    1214              :       /* Resolve alternate entry points first.  */
    1215        36883 :       resolve_entries (child);
    1216              : 
    1217              :       /* Then check function return types.  */
    1218        36883 :       resolve_contained_fntype (child->proc_name, child);
    1219        37390 :       for (el = child->entries; el; el = el->next)
    1220          507 :         resolve_contained_fntype (el->sym, child);
    1221              :     }
    1222       347654 : }
    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        63368 : resolve_structure_cons (gfc_expr *expr, int init)
    1319              : {
    1320        63368 :   gfc_constructor *cons;
    1321        63368 :   gfc_component *comp;
    1322        63368 :   bool t;
    1323        63368 :   symbol_attribute a;
    1324              : 
    1325        63368 :   t = true;
    1326              : 
    1327        63368 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1328              :     {
    1329        60476 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1330        60326 :         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        60476 :       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        63368 :   if (expr->ref)
    1358          160 :     comp = expr->ref->u.c.sym->components;
    1359        63208 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1360              :             || expr->ts.type == BT_UNION)
    1361        63206 :            && expr->ts.u.derived)
    1362        63206 :     comp = expr->ts.u.derived->components;
    1363              :   else
    1364              :     return false;
    1365              : 
    1366        63366 :   cons = gfc_constructor_first (expr->value.constructor);
    1367              : 
    1368       210915 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1369              :     {
    1370       147551 :       int rank;
    1371              : 
    1372       147551 :       if (!cons->expr)
    1373         9764 :         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       137787 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1379           15 :         continue;
    1380              : 
    1381       137772 :       if (!gfc_resolve_expr (cons->expr))
    1382              :         {
    1383            0 :           t = false;
    1384            0 :           continue;
    1385              :         }
    1386              : 
    1387       137772 :       rank = comp->as ? comp->as->rank : 0;
    1388       137772 :       if (comp->ts.type == BT_CLASS
    1389         1771 :           && !comp->ts.u.derived->attr.unlimited_polymorphic
    1390         1770 :           && CLASS_DATA (comp)->as)
    1391          525 :         rank = CLASS_DATA (comp)->as->rank;
    1392              : 
    1393       137772 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1394          228 :           gfc_find_vtab (&cons->expr->ts);
    1395              : 
    1396       137772 :       if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
    1397          495 :           && (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       240816 :       if (!comp->attr.proc_pointer &&
    1409       103044 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1410              :         {
    1411        12555 :           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         9202 :               cons->expr->ts = comp->ts;
    1417              :             }
    1418         3353 :           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         3351 :           else if (!UNLIMITED_POLY (comp))
    1428              :             {
    1429         3288 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1430         3288 :               if (t)
    1431       137772 :                 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       137772 :       if (cons->expr->ts.type == BT_CHARACTER
    1440         3890 :           && comp->ts.type == BT_CHARACTER
    1441         3864 :           && 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       137772 :       if (cons->expr->expr_type == EXPR_NULL
    1494        41313 :           && !(comp->attr.pointer || comp->attr.allocatable
    1495        20559 :                || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
    1496         1118 :                || (comp->ts.type == BT_CLASS
    1497         1116 :                    && (CLASS_DATA (comp)->attr.class_pointer
    1498          899 :                        || 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       137772 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1508              :         {
    1509              :           /* Check procedure pointer interface.  */
    1510        15610 :           gfc_symbol *s2 = NULL;
    1511        15610 :           gfc_component *c2;
    1512        15610 :           const char *name;
    1513        15610 :           char err[200];
    1514              : 
    1515        15610 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1516        15610 :           if (c2)
    1517              :             {
    1518           12 :               s2 = c2->ts.interface;
    1519           12 :               name = c2->name;
    1520              :             }
    1521        15598 :           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        15598 :           else if (cons->expr->expr_type != EXPR_NULL)
    1527              :             {
    1528        15182 :               s2 = cons->expr->symtree->n.sym;
    1529        15182 :               name = cons->expr->symtree->n.sym->name;
    1530              :             }
    1531              : 
    1532        15194 :           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       137770 :       if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
    1544         2263 :           && comp->as && !comp->attr.allocatable && !comp->attr.pointer
    1545         1526 :           && !comp->attr.pdt_array)
    1546              :         {
    1547         1279 :           mpz_t len;
    1548         1279 :           mpz_init (len);
    1549         2651 :           for (int n = 0; n < rank; n++)
    1550              :             {
    1551         1377 :               if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
    1552         1372 :                   || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
    1553              :                 {
    1554            5 :                   gfc_error ("Bad array spec of component %qs referenced in "
    1555              :                              "structure constructor at %L",
    1556            5 :                              comp->name, &cons->expr->where);
    1557            5 :                   t = false;
    1558            5 :                   break;
    1559         1372 :                 };
    1560         1372 :               if (cons->expr->shape == NULL)
    1561           12 :                 continue;
    1562         1360 :               mpz_set_ui (len, 1);
    1563         1360 :               mpz_add (len, len, comp->as->upper[n]->value.integer);
    1564         1360 :               mpz_sub (len, len, comp->as->lower[n]->value.integer);
    1565         1360 :               if (mpz_cmp (cons->expr->shape[n], len) != 0)
    1566              :                 {
    1567            9 :                   gfc_error ("The shape of component %qs in the structure "
    1568              :                              "constructor at %L differs from the shape of the "
    1569              :                              "declared component for dimension %d (%ld/%ld)",
    1570              :                              comp->name, &cons->expr->where, n+1,
    1571              :                              mpz_get_si (cons->expr->shape[n]),
    1572              :                              mpz_get_si (len));
    1573            9 :                   t = false;
    1574              :                 }
    1575              :             }
    1576         1279 :           mpz_clear (len);
    1577              :         }
    1578              : 
    1579       137770 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1580        22155 :           || cons->expr->expr_type == EXPR_NULL)
    1581       127577 :         continue;
    1582              : 
    1583        10193 :       a = gfc_expr_attr (cons->expr);
    1584              : 
    1585        10193 :       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        10193 :       if (init)
    1594              :         {
    1595              :           /* F08:C461. Additional checks for pointer initialization.  */
    1596        10125 :           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        10125 :           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        10193 :       if (comp->attr.pointer && (a.pointer || a.target)
    1613        20385 :           && 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        10193 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1622        10193 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1623        10157 :                         || 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        10193 :       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       746143 : was_declared (gfc_symbol *sym)
    1647              : {
    1648       746143 :   symbol_attribute a;
    1649              : 
    1650       746143 :   a = sym->attr;
    1651              : 
    1652       746143 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1653              :     return 1;
    1654              : 
    1655       632182 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1656       623392 :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1657       623390 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1658       623336 :       || a.asynchronous || a.codimension || a.subroutine)
    1659        95208 :     return 1;
    1660              : 
    1661              :   return 0;
    1662              : }
    1663              : 
    1664              : 
    1665              : /* Determine if a symbol is generic or not.  */
    1666              : 
    1667              : static int
    1668       414333 : generic_sym (gfc_symbol *sym)
    1669              : {
    1670       414333 :   gfc_symbol *s;
    1671              : 
    1672       414333 :   if (sym->attr.generic ||
    1673       385065 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1674        30331 :     return 1;
    1675              : 
    1676       384002 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1677              :     return 0;
    1678              : 
    1679        77427 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1680              : 
    1681        77427 :   if (s != NULL)
    1682              :     {
    1683          135 :       if (s == sym)
    1684              :         return 0;
    1685              :       else
    1686          134 :         return generic_sym (s);
    1687              :     }
    1688              : 
    1689              :   return 0;
    1690              : }
    1691              : 
    1692              : 
    1693              : /* Determine if a symbol is specific or not.  */
    1694              : 
    1695              : static int
    1696       383914 : specific_sym (gfc_symbol *sym)
    1697              : {
    1698       383914 :   gfc_symbol *s;
    1699              : 
    1700       383914 :   if (sym->attr.if_source == IFSRC_IFBODY
    1701       372550 :       || sym->attr.proc == PROC_MODULE
    1702              :       || sym->attr.proc == PROC_INTERNAL
    1703              :       || sym->attr.proc == PROC_ST_FUNCTION
    1704       295950 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1705       679133 :       || sym->attr.external)
    1706        91096 :     return 1;
    1707              : 
    1708       292818 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1709              :     return 0;
    1710              : 
    1711        77325 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1712              : 
    1713        77325 :   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       414050 : procedure_kind (gfc_symbol *sym)
    1724              : {
    1725       414050 :   if (generic_sym (sym))
    1726              :     return PTYPE_GENERIC;
    1727              : 
    1728       383865 :   if (specific_sym (sym))
    1729        91096 :     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      1430971 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1741              : {
    1742      1430971 :   if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
    1743              :       return false;
    1744              : 
    1745              :   /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
    1746              :      What should it be?  */
    1747         3800 :   if (e->ref
    1748         3798 :       && e->ref->u.ar.as
    1749         3797 :       && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
    1750         3302 :       && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    1751         3302 :       && (e->ref->u.ar.type == AR_FULL))
    1752              :     {
    1753           25 :       gfc_error ("The upper bound in the last dimension must "
    1754              :                  "appear in the reference to the assumed size "
    1755              :                  "array %qs at %L", sym->name, &e->where);
    1756           25 :       return true;
    1757              :     }
    1758              :   return false;
    1759              : }
    1760              : 
    1761              : 
    1762              : /* Look for bad assumed size array references in argument expressions
    1763              :   of elemental and array valued intrinsic procedures.  Since this is
    1764              :   called from procedure resolution functions, it only recurses at
    1765              :   operators.  */
    1766              : 
    1767              : static bool
    1768       230251 : resolve_assumed_size_actual (gfc_expr *e)
    1769              : {
    1770       230251 :   if (e == NULL)
    1771              :    return false;
    1772              : 
    1773       229684 :   switch (e->expr_type)
    1774              :     {
    1775       110691 :     case EXPR_VARIABLE:
    1776       110691 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1777              :         return true;
    1778              :       break;
    1779              : 
    1780        48777 :     case EXPR_OP:
    1781        48777 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1782        48777 :           || 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       152295 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1834              : {
    1835       152295 :   gfc_symbol* proc_sym;
    1836       152295 :   gfc_symbol* context_proc;
    1837       152295 :   gfc_namespace* real_context;
    1838              : 
    1839       152295 :   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       152294 :   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       152294 :   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         1959 :   for (real_context = context; ; real_context = real_context->parent)
    1857              :     {
    1858              :       /* We should find something, eventually!  */
    1859       129287 :       gcc_assert (real_context);
    1860              : 
    1861       129287 :       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       129287 :       if (!context_proc)
    1871              :         return false;
    1872              : 
    1873       129023 :       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       127064 :   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       127049 :   if (context_proc->attr.contained)
    1884              :     {
    1885        21306 :       gfc_symbol* parent_proc;
    1886              : 
    1887        21306 :       gcc_assert (context->parent);
    1888        21306 :       parent_proc = (context->parent->entries ? context->parent->entries->sym
    1889              :                                               : context->parent->proc_name);
    1890              : 
    1891        21306 :       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        46872 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1904              : {
    1905        46872 :   gfc_intrinsic_sym* isym = NULL;
    1906        46872 :   const char* symstd;
    1907              : 
    1908        46872 :   if (sym->resolve_symbol_called >= 2)
    1909              :     return true;
    1910              : 
    1911        36939 :   sym->resolve_symbol_called = 2;
    1912              : 
    1913              :   /* Already resolved.  */
    1914        36939 :   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        28891 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1923              :     {
    1924        12620 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1925        12620 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1926        12620 :     }
    1927        16271 :   else if (sym->intmod_sym_id)
    1928              :     {
    1929        12551 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1930        12551 :       isym = gfc_intrinsic_function_by_id (id);
    1931              :     }
    1932         3720 :   else if (!sym->attr.subroutine)
    1933         3633 :     isym = gfc_find_function (sym->name);
    1934              : 
    1935        28804 :   if (isym && !sym->attr.subroutine)
    1936              :     {
    1937        16139 :       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        20414 :       if (!sym->attr.function &&
    1944         4275 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1945              :         return false;
    1946              : 
    1947        16139 :       sym->ts = isym->ts;
    1948              :     }
    1949        12752 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1950              :     {
    1951        12749 :       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        12789 :       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        28886 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1970              : 
    1971        28886 :   sym->attr.pure = isym->pure;
    1972        28886 :   sym->attr.elemental = isym->elemental;
    1973              : 
    1974              :   /* Check it is actually available in the standard settings.  */
    1975        28886 :   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      1333480 : resolve_procedure_expression (gfc_expr* expr)
    1994              : {
    1995      1333480 :   gfc_symbol* sym;
    1996              : 
    1997      1333480 :   if (expr->expr_type != EXPR_VARIABLE)
    1998              :     return true;
    1999      1333463 :   gcc_assert (expr->symtree);
    2000              : 
    2001      1333463 :   sym = expr->symtree->n.sym;
    2002              : 
    2003      1333463 :   if (sym->attr.intrinsic)
    2004         1346 :     gfc_resolve_intrinsic (sym, &expr->where);
    2005              : 
    2006      1333463 :   if (sym->attr.flavor != FL_PROCEDURE
    2007        31947 :       || (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        17334 :   if (is_illegal_recursion (sym, gfc_current_ns))
    2013              :     {
    2014           10 :       if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
    2015            0 :         gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
    2016              :                      " possibly calling itself recursively in procedure %qs. "
    2017              :                      " Declare it RECURSIVE or use %<-frecursive%>",
    2018            0 :                      sym->name, sym->module, gfc_current_ns->proc_name->name);
    2019              :       else
    2020           10 :         gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    2021              :                      " itself recursively.  Declare it RECURSIVE or use"
    2022              :                      " %<-frecursive%>", sym->name, &expr->where);
    2023              :     }
    2024              : 
    2025              :   return true;
    2026              : }
    2027              : 
    2028              : 
    2029              : /* Check that name is not a derived type.  */
    2030              : 
    2031              : static bool
    2032         3416 : is_dt_name (const char *name)
    2033              : {
    2034         3416 :   gfc_symbol *dt_list, *dt_first;
    2035              : 
    2036         3416 :   dt_list = dt_first = gfc_derived_types;
    2037         5870 :   for (; dt_list; dt_list = dt_list->dt_next)
    2038              :     {
    2039         3577 :       if (strcmp(dt_list->name, name) == 0)
    2040              :         return true;
    2041         3574 :       if (dt_first == dt_list->dt_next)
    2042              :         break;
    2043              :     }
    2044              :   return false;
    2045              : }
    2046              : 
    2047              : 
    2048              : /* Resolve an actual argument list.  Most of the time, this is just
    2049              :    resolving the expressions in the list.
    2050              :    The exception is that we sometimes have to decide whether arguments
    2051              :    that look like procedure arguments are really simple variable
    2052              :    references.  */
    2053              : 
    2054              : static bool
    2055       428246 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2056              :                         bool no_formal_args)
    2057              : {
    2058       428246 :   gfc_symbol *sym = NULL;
    2059       428246 :   gfc_symtree *parent_st;
    2060       428246 :   gfc_expr *e;
    2061       428246 :   gfc_component *comp;
    2062       428246 :   int save_need_full_assumed_size;
    2063       428246 :   bool return_value = false;
    2064       428246 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2065              : 
    2066       428246 :   actual_arg = true;
    2067       428246 :   first_actual_arg = true;
    2068              : 
    2069      1099096 :   for (; arg; arg = arg->next)
    2070              :     {
    2071       670951 :       e = arg->expr;
    2072       670951 :       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       668515 :       if (e->expr_type == EXPR_VARIABLE
    2089       295028 :             && e->symtree->n.sym->attr.generic
    2090            8 :             && no_formal_args
    2091       668520 :             && count_specific_procs (e) != 1)
    2092            2 :         goto cleanup;
    2093              : 
    2094       668513 :       if (e->ts.type != BT_PROCEDURE)
    2095              :         {
    2096       595774 :           save_need_full_assumed_size = need_full_assumed_size;
    2097       595774 :           if (e->expr_type != EXPR_VARIABLE)
    2098       373487 :             need_full_assumed_size = 0;
    2099       595774 :           if (!gfc_resolve_expr (e))
    2100           60 :             goto cleanup;
    2101       595714 :           need_full_assumed_size = save_need_full_assumed_size;
    2102       595714 :           goto argument_list;
    2103              :         }
    2104              : 
    2105              :       /* See if the expression node should really be a variable reference.  */
    2106              : 
    2107        72739 :       sym = e->symtree->n.sym;
    2108              : 
    2109        72739 :       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        72736 :       if (sym->attr.flavor == FL_PROCEDURE
    2117        69323 :           || sym->attr.intrinsic
    2118        69323 :           || sym->attr.external)
    2119              :         {
    2120         3413 :           int actual_ok;
    2121              : 
    2122              :           /* If a procedure is not already determined to be something else
    2123              :              check if it is intrinsic.  */
    2124         3413 :           if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
    2125         1254 :             sym->attr.intrinsic = 1;
    2126              : 
    2127         3413 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    2128              :             {
    2129            2 :               gfc_error ("Statement function %qs at %L is not allowed as an "
    2130              :                          "actual argument", sym->name, &e->where);
    2131              :             }
    2132              : 
    2133         6826 :           actual_ok = gfc_intrinsic_actual_ok (sym->name,
    2134         3413 :                                                sym->attr.subroutine);
    2135         3413 :           if (sym->attr.intrinsic && actual_ok == 0)
    2136              :             {
    2137            0 :               gfc_error ("Intrinsic %qs at %L is not allowed as an "
    2138              :                          "actual argument", sym->name, &e->where);
    2139              :             }
    2140              : 
    2141         3413 :           if (sym->attr.contained && !sym->attr.use_assoc
    2142          438 :               && sym->ns->proc_name->attr.flavor != FL_MODULE)
    2143              :             {
    2144          250 :               if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
    2145              :                                    " used as actual argument at %L",
    2146              :                                    sym->name, &e->where))
    2147            3 :                 goto cleanup;
    2148              :             }
    2149              : 
    2150         3410 :           if (sym->attr.elemental && !sym->attr.intrinsic)
    2151              :             {
    2152            2 :               gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
    2153              :                          "allowed as an actual argument at %L", sym->name,
    2154              :                          &e->where);
    2155              :             }
    2156              : 
    2157              :           /* Check if a generic interface has a specific procedure
    2158              :             with the same name before emitting an error.  */
    2159         3410 :           if (sym->attr.generic && count_specific_procs (e) != 1)
    2160            0 :             goto cleanup;
    2161              : 
    2162              :           /* Just in case a specific was found for the expression.  */
    2163         3410 :           sym = e->symtree->n.sym;
    2164              : 
    2165              :           /* If the symbol is the function that names the current (or
    2166              :              parent) scope, then we really have a variable reference.  */
    2167              : 
    2168         3410 :           if (gfc_is_function_return_value (sym, sym->ns))
    2169            0 :             goto got_variable;
    2170              : 
    2171              :           /* If all else fails, see if we have a specific intrinsic.  */
    2172         3410 :           if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
    2173              :             {
    2174            0 :               gfc_intrinsic_sym *isym;
    2175              : 
    2176            0 :               isym = gfc_find_function (sym->name);
    2177            0 :               if (isym == NULL || !isym->specific)
    2178              :                 {
    2179            0 :                   gfc_error ("Unable to find a specific INTRINSIC procedure "
    2180              :                              "for the reference %qs at %L", sym->name,
    2181              :                              &e->where);
    2182            0 :                   goto cleanup;
    2183              :                 }
    2184            0 :               sym->ts = isym->ts;
    2185            0 :               sym->attr.intrinsic = 1;
    2186            0 :               sym->attr.function = 1;
    2187              :             }
    2188              : 
    2189         3410 :           if (!gfc_resolve_expr (e))
    2190            0 :             goto cleanup;
    2191         3410 :           goto argument_list;
    2192              :         }
    2193              : 
    2194              :       /* See if the name is a module procedure in a parent unit.  */
    2195              : 
    2196        69323 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2197        69230 :         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        69323 :       e->expr_type = EXPR_VARIABLE;
    2222        69323 :       e->ts = sym->ts;
    2223        69323 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2224        35974 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2225         3876 :               && CLASS_DATA (sym)->as))
    2226              :         {
    2227        38973 :           gfc_array_spec *as
    2228        36161 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2229        36161 :           e->rank = as->rank;
    2230        36161 :           e->corank = as->corank;
    2231        36161 :           e->ref = gfc_get_ref ();
    2232        36161 :           e->ref->type = REF_ARRAY;
    2233        36161 :           e->ref->u.ar.type = AR_FULL;
    2234        36161 :           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        69323 :       if (e->expr_type == EXPR_VARIABLE
    2241        69323 :           && 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        69323 :       save_need_full_assumed_size = need_full_assumed_size;
    2250        69323 :       if (e->expr_type != EXPR_VARIABLE)
    2251            0 :         need_full_assumed_size = 0;
    2252        69323 :       if (!gfc_resolve_expr (e))
    2253           22 :         goto cleanup;
    2254        69301 :       need_full_assumed_size = save_need_full_assumed_size;
    2255              : 
    2256       668425 :     argument_list:
    2257              :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2258              :          nothing to do for %REF.  */
    2259       668425 :       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       668419 :       comp = gfc_get_proc_ptr_comp(e);
    2306       668419 :       if (e->expr_type == EXPR_VARIABLE
    2307       293650 :           && 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       293650 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2316       668864 :           && 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       668416 :       if (e->expr_type == EXPR_VARIABLE
    2324       293647 :           && e->ts.type == BT_PROCEDURE
    2325         3410 :           && no_formal_args
    2326         1505 :           && sym->attr.flavor == FL_PROCEDURE
    2327         1505 :           && sym->attr.if_source == IFSRC_UNKNOWN
    2328          142 :           && !sym->attr.external
    2329            2 :           && !sym->attr.intrinsic
    2330            2 :           && !sym->attr.artificial
    2331            2 :           && !sym->ts.interface)
    2332              :         {
    2333              :           /* Emit a warning for -std=legacy and an error otherwise. */
    2334            2 :           if (gfc_option.warn_std == 0)
    2335            0 :             gfc_warning (0, "Procedure %qs at %L used as actual argument but "
    2336              :                          "does neither have an explicit interface nor the "
    2337              :                          "EXTERNAL attribute", sym->name, &e->where);
    2338              :           else
    2339              :             {
    2340            2 :               gfc_error ("Procedure %qs at %L used as actual argument but "
    2341              :                          "does neither have an explicit interface nor the "
    2342              :                          "EXTERNAL attribute", sym->name, &e->where);
    2343            2 :               goto cleanup;
    2344              :             }
    2345              :         }
    2346              : 
    2347       668414 :       first_actual_arg = false;
    2348              :     }
    2349              : 
    2350              :   return_value = true;
    2351              : 
    2352       428246 : cleanup:
    2353       428246 :   actual_arg = actual_arg_sav;
    2354       428246 :   first_actual_arg = first_actual_arg_sav;
    2355              : 
    2356       428246 :   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       326091 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2366              : {
    2367       326091 :   gfc_actual_arglist *arg0;
    2368       326091 :   gfc_actual_arglist *arg;
    2369       326091 :   gfc_symbol *esym = NULL;
    2370       326091 :   gfc_intrinsic_sym *isym = NULL;
    2371       326091 :   gfc_expr *e = NULL;
    2372       326091 :   gfc_intrinsic_arg *iformal = NULL;
    2373       326091 :   gfc_formal_arglist *eformal = NULL;
    2374       326091 :   bool formal_optional = false;
    2375       326091 :   bool set_by_optional = false;
    2376       326091 :   int i;
    2377       326091 :   int rank = 0;
    2378              : 
    2379              :   /* Is this an elemental procedure?  */
    2380       326091 :   if (expr && expr->value.function.actual != NULL)
    2381              :     {
    2382       236340 :       if (expr->value.function.esym != NULL
    2383        43968 :           && expr->value.function.esym->attr.elemental)
    2384              :         {
    2385              :           arg0 = expr->value.function.actual;
    2386              :           esym = expr->value.function.esym;
    2387              :         }
    2388       220032 :       else if (expr->value.function.isym != NULL
    2389       191318 :                && 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        89751 :   else if (c && c->ext.actual != NULL)
    2398              :     {
    2399        71034 :       arg0 = c->ext.actual;
    2400              : 
    2401        71034 :       if (c->resolved_sym)
    2402              :         esym = c->resolved_sym;
    2403              :       else
    2404          323 :         esym = c->symtree->n.sym;
    2405        71034 :       gcc_assert (esym);
    2406              : 
    2407        71034 :       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       174169 :   for (arg = arg0; arg; arg = arg->next)
    2415              :     {
    2416       112887 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2417              :         {
    2418        10716 :           rank = arg->expr->rank;
    2419        10716 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2420         5484 :               && arg->expr->symtree->n.sym->attr.optional)
    2421        10716 :             set_by_optional = true;
    2422              : 
    2423              :           /* Function specific; set the result rank and shape.  */
    2424        10716 :           if (expr)
    2425              :             {
    2426         8314 :               expr->rank = rank;
    2427         8314 :               expr->corank = arg->expr->corank;
    2428         8314 :               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        71998 :   formal_optional = false;
    2444        71998 :   if (isym)
    2445        49483 :     iformal = isym->formal;
    2446              :   else
    2447        22515 :     eformal = esym->formal;
    2448              : 
    2449       190401 :   for (arg = arg0; arg; arg = arg->next)
    2450              :     {
    2451       118403 :       if (eformal)
    2452              :         {
    2453        40405 :           if (eformal->sym && eformal->sym->attr.optional)
    2454        40405 :             formal_optional = true;
    2455        40405 :           eformal = eformal->next;
    2456              :         }
    2457        77998 :       else if (isym && iformal)
    2458              :         {
    2459        67748 :           if (iformal->optional)
    2460        13454 :             formal_optional = true;
    2461        67748 :           iformal = iformal->next;
    2462              :         }
    2463        10250 :       else if (isym)
    2464        10242 :         formal_optional = true;
    2465              : 
    2466       118403 :       if (pedantic && arg->expr != NULL
    2467        67651 :           && arg->expr->expr_type == EXPR_VARIABLE
    2468        31920 :           && arg->expr->symtree->n.sym->attr.optional
    2469          572 :           && formal_optional
    2470          479 :           && arg->expr->rank
    2471          153 :           && (set_by_optional || arg->expr->rank != rank)
    2472           42 :           && !(isym && isym->id == GFC_ISYM_CONVERSION))
    2473              :         {
    2474          114 :           bool t = false;
    2475              :           gfc_actual_arglist *a;
    2476              : 
    2477              :           /* Scan the argument list for a non-optional argument with the
    2478              :              same rank as arg.  */
    2479          114 :           for (a = arg0; a; a = a->next)
    2480           87 :             if (a != arg
    2481           45 :                 && a->expr->rank == arg->expr->rank
    2482           39 :                 && (a->expr->expr_type != EXPR_VARIABLE
    2483           37 :                     || (a->expr->expr_type == EXPR_VARIABLE
    2484           37 :                         && !a->expr->symtree->n.sym->attr.optional)))
    2485              :               {
    2486              :                 t = true;
    2487              :                 break;
    2488              :               }
    2489              : 
    2490           42 :           if (!t)
    2491           27 :             gfc_warning (OPT_Wpedantic,
    2492              :                          "%qs at %L is an array and OPTIONAL; If it is not "
    2493              :                          "present, then it cannot be the actual argument of "
    2494              :                          "an ELEMENTAL procedure unless there is a non-optional"
    2495              :                          " argument with the same rank "
    2496              :                          "(Fortran 2018, 15.5.2.12)",
    2497              :                          arg->expr->symtree->n.sym->name, &arg->expr->where);
    2498              :         }
    2499              :     }
    2500              : 
    2501       190390 :   for (arg = arg0; arg; arg = arg->next)
    2502              :     {
    2503       118401 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2504       104797 :         continue;
    2505              : 
    2506              :       /* Being elemental, the last upper bound of an assumed size array
    2507              :          argument must be present.  */
    2508        13604 :       if (resolve_assumed_size_actual (arg->expr))
    2509              :         return false;
    2510              : 
    2511              :       /* Elemental procedure's array actual arguments must conform.  */
    2512        13601 :       if (e != NULL)
    2513              :         {
    2514         2888 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2515              :             return false;
    2516              :         }
    2517              :       else
    2518        10713 :         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        71989 :   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        14945 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2559              : {
    2560        14945 :   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        14945 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2574              : {
    2575        14945 :   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        15757 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2602              : {
    2603        15757 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2604              : 
    2605        58896 :   for ( ; arg; arg = arg->next)
    2606              :     {
    2607        27790 :       if (!arg->sym)
    2608          157 :         continue;
    2609              : 
    2610        27633 :       if (arg->sym->attr.allocatable)  /* (2a)  */
    2611              :         {
    2612            0 :           strncpy (errmsg, _("allocatable argument"), err_len);
    2613            0 :           return true;
    2614              :         }
    2615        27633 :       else if (arg->sym->attr.asynchronous)
    2616              :         {
    2617            0 :           strncpy (errmsg, _("asynchronous argument"), err_len);
    2618            0 :           return true;
    2619              :         }
    2620        27633 :       else if (arg->sym->attr.optional)
    2621              :         {
    2622           75 :           strncpy (errmsg, _("optional argument"), err_len);
    2623           75 :           return true;
    2624              :         }
    2625        27558 :       else if (arg->sym->attr.pointer)
    2626              :         {
    2627           12 :           strncpy (errmsg, _("pointer argument"), err_len);
    2628           12 :           return true;
    2629              :         }
    2630        27546 :       else if (arg->sym->attr.target)
    2631              :         {
    2632           72 :           strncpy (errmsg, _("target argument"), err_len);
    2633           72 :           return true;
    2634              :         }
    2635        27474 :       else if (arg->sym->attr.value)
    2636              :         {
    2637           12 :           strncpy (errmsg, _("value argument"), err_len);
    2638           12 :           return true;
    2639              :         }
    2640        27462 :       else if (arg->sym->attr.volatile_)
    2641              :         {
    2642            1 :           strncpy (errmsg, _("volatile argument"), err_len);
    2643            1 :           return true;
    2644              :         }
    2645        27461 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE)  /* (2b)  */
    2646              :         {
    2647           69 :           strncpy (errmsg, _("assumed-shape argument"), err_len);
    2648           69 :           return true;
    2649              :         }
    2650        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        15349 :   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        15206 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2708              :     {
    2709            7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2710            7 :       return true;
    2711              :     }
    2712        15199 :   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        29472 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2724              : {
    2725        29472 :   gfc_gsymbol * gsym;
    2726        29472 :   gfc_namespace *ns;
    2727        29472 :   enum gfc_symbol_type type;
    2728        29472 :   char reason[200];
    2729              : 
    2730        29472 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2731              : 
    2732        29472 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2733        29472 :                           sym->binding_label != NULL);
    2734              : 
    2735        29472 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2736            9 :     gfc_global_used (gsym, where);
    2737              : 
    2738        29472 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2739         9301 :        || sym->attr.if_source == IFSRC_IFBODY)
    2740        25059 :       && gsym->type != GSYM_UNKNOWN
    2741        22885 :       && !gsym->binding_label
    2742        20587 :       && gsym->ns
    2743        14945 :       && gsym->ns->proc_name
    2744        14945 :       && not_in_recursive (sym, gsym->ns)
    2745        44417 :       && not_entry_self_reference (sym, gsym->ns))
    2746              :     {
    2747        14945 :       gfc_symbol *def_sym;
    2748        14945 :       def_sym = gsym->ns->proc_name;
    2749              : 
    2750        14945 :       if (gsym->ns->resolved != -1)
    2751              :         {
    2752              : 
    2753              :           /* Resolve the gsymbol namespace if needed.  */
    2754        14923 :           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        14923 :           ns = gfc_global_ns_list;
    2776        25315 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2777              :             {
    2778        16918 :               if (ns->sibling == gsym->ns)
    2779              :                 {
    2780         6526 :                   ns->sibling = gsym->ns->sibling;
    2781         6526 :                   gsym->ns->sibling = gfc_global_ns_list;
    2782         6526 :                   gfc_global_ns_list = gsym->ns;
    2783         6526 :                   break;
    2784              :                 }
    2785              :             }
    2786              : 
    2787              :           /* This can happen if a binding name has been specified.  */
    2788        14923 :           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        14945 :       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        14945 :       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        14939 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2817        14939 :           && 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        14931 :       bool bad_result_characteristics;
    2825        14931 :       if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
    2826              :                                    reason, sizeof(reason), NULL, NULL,
    2827              :                                    &bad_result_characteristics))
    2828              :         {
    2829              :           /* Turn errors into warnings with -std=gnu and -std=legacy,
    2830              :              unless a function returns a wrong type, which can lead
    2831              :              to all kinds of ICEs and wrong code.  */
    2832              : 
    2833           14 :           if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
    2834            2 :               && !bad_result_characteristics)
    2835            2 :             gfc_errors_to_warnings (true);
    2836              : 
    2837           14 :           gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
    2838              :                      sym->name, &sym->declared_at, reason);
    2839           14 :           sym->error = 1;
    2840           14 :           gfc_errors_to_warnings (false);
    2841           14 :           goto done;
    2842              :         }
    2843              :     }
    2844              : 
    2845        29472 : done:
    2846              : 
    2847        29472 :   if (gsym->type == GSYM_UNKNOWN)
    2848              :     {
    2849         3988 :       gsym->type = type;
    2850         3988 :       gsym->where = *where;
    2851              :     }
    2852              : 
    2853        29472 :   gsym->used = 1;
    2854        29472 : }
    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        27520 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2864              : {
    2865        27520 :   gfc_symbol *s;
    2866              : 
    2867        27520 :   if (sym->attr.generic)
    2868              :     {
    2869        26415 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2870        26415 :       if (s != NULL)
    2871              :         {
    2872        19790 :           expr->value.function.name = s->name;
    2873        19790 :           expr->value.function.esym = s;
    2874              : 
    2875        19790 :           if (s->ts.type != BT_UNKNOWN)
    2876        19773 :             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        19790 :           if (s->as != NULL)
    2881              :             {
    2882           55 :               expr->rank = s->as->rank;
    2883           55 :               expr->corank = s->as->corank;
    2884              :             }
    2885        19735 :           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        19790 :           gfc_set_sym_referenced (expr->value.function.esym);
    2892              : 
    2893        19790 :           return MATCH_YES;
    2894              :         }
    2895              : 
    2896              :       /* TODO: Need to search for elemental references in generic
    2897              :          interface.  */
    2898              :     }
    2899              : 
    2900         7730 :   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        27376 : resolve_generic_f (gfc_expr *expr)
    2909              : {
    2910        27376 :   gfc_symbol *sym;
    2911        27376 :   match m;
    2912        27376 :   gfc_interface *intr = NULL;
    2913              : 
    2914        27376 :   sym = expr->symtree->n.sym;
    2915              : 
    2916        27520 :   for (;;)
    2917              :     {
    2918        27520 :       m = resolve_generic_f0 (expr, sym);
    2919        27520 :       if (m == MATCH_YES)
    2920              :         return true;
    2921         6670 :       else if (m == MATCH_ERROR)
    2922              :         return false;
    2923              : 
    2924         6670 : generic:
    2925         6673 :       if (!intr)
    2926         6641 :         for (intr = sym->generic; intr; intr = intr->next)
    2927         6557 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2928              :             break;
    2929              : 
    2930         6673 :       if (sym->ns->parent == NULL)
    2931              :         break;
    2932          298 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2933              : 
    2934          298 :       if (sym == NULL)
    2935              :         break;
    2936          147 :       if (!generic_sym (sym))
    2937            3 :         goto generic;
    2938              :     }
    2939              : 
    2940              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    2941              :      that possesses a matching interface.  14.1.2.4  */
    2942         6526 :   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         6521 :   if (intr)
    2955              :     {
    2956         6486 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2957              :                                                  NULL, false))
    2958              :         return false;
    2959         6459 :       if (!gfc_use_derived (expr->ts.u.derived))
    2960              :         return false;
    2961         6459 :       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        28219 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2981              : {
    2982        28219 :   match m;
    2983              : 
    2984        28219 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2985              :     {
    2986         8144 :       if (sym->attr.dummy)
    2987              :         {
    2988          282 :           sym->attr.proc = PROC_DUMMY;
    2989          282 :           goto found;
    2990              :         }
    2991              : 
    2992         7862 :       sym->attr.proc = PROC_EXTERNAL;
    2993         7862 :       goto found;
    2994              :     }
    2995              : 
    2996        20075 :   if (sym->attr.proc == PROC_MODULE
    2997              :       || sym->attr.proc == PROC_ST_FUNCTION
    2998              :       || sym->attr.proc == PROC_INTERNAL)
    2999        19337 :     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        27481 : found:
    3016        27481 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3017              : 
    3018        27481 :   if (sym->result)
    3019        27481 :     expr->ts = sym->result->ts;
    3020              :   else
    3021            0 :     expr->ts = sym->ts;
    3022        27481 :   expr->value.function.name = sym->name;
    3023        27481 :   expr->value.function.esym = sym;
    3024              :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    3025              :      error(s).  */
    3026        27481 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    3027              :     return MATCH_ERROR;
    3028        27480 :   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        27158 :   else if (sym->as != NULL)
    3034              :     {
    3035         2335 :       expr->rank = sym->as->rank;
    3036         2335 :       expr->corank = sym->as->corank;
    3037              :     }
    3038              : 
    3039              :   return MATCH_YES;
    3040              : }
    3041              : 
    3042              : 
    3043              : static bool
    3044        28212 : resolve_specific_f (gfc_expr *expr)
    3045              : {
    3046        28212 :   gfc_symbol *sym;
    3047        28212 :   match m;
    3048              : 
    3049        28212 :   sym = expr->symtree->n.sym;
    3050              : 
    3051        28219 :   for (;;)
    3052              :     {
    3053        28219 :       m = resolve_specific_f0 (sym, expr);
    3054        28219 :       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       276889 : resolve_unknown_f (gfc_expr *expr)
    3116              : {
    3117       276889 :   gfc_symbol *sym;
    3118       276889 :   gfc_typespec *ts;
    3119              : 
    3120       276889 :   sym = expr->symtree->n.sym;
    3121              : 
    3122       276889 :   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       276600 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3132              :     {
    3133       274343 :       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       853865 : is_external_proc (gfc_symbol *sym)
    3199              : {
    3200       852150 :   if (!sym->attr.dummy && !sym->attr.contained
    3201       743684 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3202       162018 :         && sym->attr.proc != PROC_ST_FUNCTION
    3203       161423 :         && !sym->attr.proc_pointer
    3204       160217 :         && !sym->attr.use_assoc
    3205       912840 :         && 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       256777 : gfc_pure_function (gfc_expr *e, const char **name)
    3220              : {
    3221       256777 :   bool pure;
    3222       256777 :   gfc_component *comp;
    3223              : 
    3224       256777 :   *name = NULL;
    3225              : 
    3226       256777 :   if (e->symtree != NULL
    3227       256421 :         && e->symtree->n.sym != NULL
    3228       256421 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3229          305 :     return pure_stmt_function (e, e->symtree->n.sym);
    3230              : 
    3231       256472 :   comp = gfc_get_proc_ptr_comp (e);
    3232       256472 :   if (comp)
    3233              :     {
    3234          465 :       pure = gfc_pure (comp->ts.interface);
    3235          465 :       *name = comp->name;
    3236              :     }
    3237       256007 :   else if (e->value.function.esym)
    3238              :     {
    3239        52848 :       pure = gfc_pure (e->value.function.esym);
    3240        52848 :       *name = e->value.function.esym->name;
    3241              :     }
    3242       203159 :   else if (e->value.function.isym)
    3243              :     {
    3244       404180 :       pure = e->value.function.isym->pure
    3245       202090 :              || e->value.function.isym->elemental;
    3246       202090 :       *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        38147 : gfc_implicit_pure_function (gfc_expr *e)
    3270              : {
    3271        38147 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3272        38147 :   if (comp)
    3273          449 :     return gfc_implicit_pure (comp->ts.interface);
    3274        37698 :   else if (e->value.function.esym)
    3275        32293 :     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       244856 : static bool check_pure_function (gfc_expr *e)
    3309              : {
    3310       244856 :   const char *name = NULL;
    3311       244856 :   code_stack *stack;
    3312       244856 :   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       566539 :   for (stack = cs_base; stack; stack = stack->prev)
    3320              :     {
    3321       321685 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3322              :         {
    3323         7392 :           saw_block = true;
    3324         7392 :           continue;
    3325              :         }
    3326              : 
    3327         5234 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3328              :         {
    3329           10 :           bool is_pure;
    3330       321683 :           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       244854 :   if (!gfc_pure_function (e, &name) && name)
    3346              :     {
    3347        36866 :       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        36862 :       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        36860 :       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        36855 :       if (!gfc_implicit_pure_function (e))
    3368        30367 :         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       132898 : 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       132898 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3383       250507 :   for (; sibling; sibling = sibling->sibling)
    3384              :     {
    3385       124649 :       if (sibling->proc_name == sym)
    3386              :         {
    3387         7040 :           gfc_resolve (sibling);
    3388         7040 :           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       132898 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3395        68116 :       && gfc_current_ns->proc_name)
    3396        68072 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3397       132898 : }
    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       345710 : resolve_function (gfc_expr *expr)
    3405              : {
    3406       345710 :   gfc_actual_arglist *arg;
    3407       345710 :   gfc_symbol *sym;
    3408       345710 :   bool t;
    3409       345710 :   int temp;
    3410       345710 :   procedure_type p = PROC_INTRINSIC;
    3411       345710 :   bool no_formal_args;
    3412              : 
    3413       345710 :   sym = NULL;
    3414       345710 :   if (expr->symtree)
    3415       345354 :     sym = expr->symtree->n.sym;
    3416              : 
    3417              :   /* If this is a procedure pointer component, it has already been resolved.  */
    3418       345710 :   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       345300 :   if (sym && sym->attr.intrinsic
    3424         8660 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3425         8660 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3426              :     return true;
    3427              : 
    3428       345300 :   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       344943 :   if (sym && sym->attr.intrinsic
    3436       353959 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3437              :     return false;
    3438              : 
    3439       345299 :   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       344939 :   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       344938 :   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       345293 :   need_full_assumed_size++;
    3470              : 
    3471       345293 :   if (expr->symtree && expr->symtree->n.sym)
    3472       344937 :     p = expr->symtree->n.sym->attr.proc;
    3473              : 
    3474       345293 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3475         1105 :     inquiry_argument = true;
    3476       344937 :   no_formal_args = sym && is_external_proc (sym)
    3477       359119 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3478              : 
    3479       345293 :   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       345226 :   inquiry_argument = false;
    3487              : 
    3488              :   /* Resume assumed_size checking.  */
    3489       345226 :   need_full_assumed_size--;
    3490              : 
    3491              :   /* If the procedure is external, check for usage.  */
    3492       345226 :   if (sym && is_external_proc (sym))
    3493        13806 :     resolve_global_procedure (sym, &expr->where, 0);
    3494              : 
    3495       345226 :   if (sym && sym->ts.type == BT_CHARACTER
    3496         3346 :       && sym->ts.u.cl
    3497         3252 :       && sym->ts.u.cl->length == NULL
    3498          677 :       && !sym->attr.dummy
    3499          670 :       && !sym->ts.deferred
    3500            2 :       && expr->value.function.esym == NULL
    3501            2 :       && !sym->attr.contained)
    3502              :     {
    3503              :       /* Internal procedures are taken care of in resolve_contained_fntype.  */
    3504            1 :       gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
    3505              :                  "be used at %L since it is not a dummy argument",
    3506              :                  sym->name, &expr->where);
    3507            1 :       return false;
    3508              :     }
    3509              : 
    3510              :   /* Add and check formal interface when -fc-prototypes-external is in
    3511              :      force, see comment in resolve_call().  */
    3512              : 
    3513       345225 :   if (warn_external_argument_mismatch && sym && sym->attr.dummy
    3514           18 :       && sym->attr.external)
    3515              :     {
    3516           18 :       if (sym->formal)
    3517              :         {
    3518            6 :           bool conflict;
    3519            6 :           conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
    3520              :                                                  sym->formal, 0, 0, 0, NULL);
    3521            6 :           if (conflict)
    3522              :             {
    3523            6 :               sym->ext_dummy_arglist_mismatch = 1;
    3524            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    3525              :                            "Different argument lists in external dummy "
    3526              :                            "function %s at %L and %L", sym->name,
    3527              :                            &expr->where, &sym->other_loc);
    3528              :             }
    3529              :         }
    3530           12 :       else if (!sym->formal_resolved)
    3531              :         {
    3532            6 :           gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
    3533            6 :           sym->other_loc = expr->where;
    3534              :         }
    3535              :     }
    3536              :   /* See if function is already resolved.  */
    3537              : 
    3538       345225 :   if (expr->value.function.name != NULL
    3539       333275 :       || expr->value.function.isym != NULL)
    3540              :     {
    3541        12748 :       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       332477 :       switch (procedure_kind (sym))
    3550              :         {
    3551        27376 :         case PTYPE_GENERIC:
    3552        27376 :           t = resolve_generic_f (expr);
    3553        27376 :           break;
    3554              : 
    3555        28212 :         case PTYPE_SPECIFIC:
    3556        28212 :           t = resolve_specific_f (expr);
    3557        28212 :           break;
    3558              : 
    3559       276889 :         case PTYPE_UNKNOWN:
    3560       276889 :           t = resolve_unknown_f (expr);
    3561       276889 :           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       345225 :   if (expr->expr_type != EXPR_FUNCTION)
    3572              :     return t;
    3573              : 
    3574              :   /* Walk the argument list looking for invalid BOZ.  */
    3575       741397 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3576       496983 :     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       244414 :   temp = need_full_assumed_size;
    3585       244414 :   need_full_assumed_size = 0;
    3586              : 
    3587       244414 :   if (!resolve_elemental_actual (expr, NULL))
    3588              :     return false;
    3589              : 
    3590       244411 :   if (omp_workshare_flag
    3591           32 :       && expr->value.function.esym
    3592       244416 :       && ! 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       244407 :   else if (expr->value.function.actual != NULL
    3602       236337 :            && expr->value.function.isym != NULL
    3603       191317 :            && 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       538492 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3617              :         {
    3618       373136 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3619        45833 :               && arg == expr->value.function.actual
    3620        16881 :               && arg->next != NULL && arg->next->expr)
    3621              :             {
    3622         8315 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3623              :                 break;
    3624              : 
    3625         8091 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3626              :                 break;
    3627              : 
    3628         8091 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3629         8091 :                         < arg->expr->rank)
    3630              :                 break;
    3631              :             }
    3632              : 
    3633       370733 :           if (arg->expr != NULL
    3634       247181 :               && arg->expr->rank > 0
    3635       489826 :               && resolve_assumed_size_actual (arg->expr))
    3636              :             return false;
    3637              :         }
    3638              :     }
    3639              : #undef GENERIC_ID
    3640              : 
    3641       244408 :   need_full_assumed_size = temp;
    3642              : 
    3643       244408 :   if (!check_pure_function(expr))
    3644           12 :     t = false;
    3645              : 
    3646              :   /* Functions without the RECURSIVE attribution are not allowed to
    3647              :    * call themselves.  */
    3648       244408 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3649              :     {
    3650        51589 :       gfc_symbol *esym;
    3651        51589 :       esym = expr->value.function.esym;
    3652              : 
    3653        51589 :       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       244408 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3672         3450 :       && expr->value.function.esym->attr.use_assoc)
    3673              :     {
    3674         1256 :       gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
    3675              :     }
    3676              : 
    3677              :   /* Make sure that the expression has a typespec that works.  */
    3678       244408 :   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       244408 :   if (expr->ts.type == BT_DERIVED
    3690         9553 :       && !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       244408 :   if (!expr->ref && !expr->value.function.isym)
    3705              :     {
    3706        52970 :       if (expr->value.function.esym)
    3707        51901 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3708              :       else
    3709         1069 :         update_current_proc_array_outer_dependency (sym);
    3710              :     }
    3711       191438 :   else if (expr->ref)
    3712              :     /* typebound procedure: Assume the worst.  */
    3713            0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3714              : 
    3715       244408 :   if (expr->value.function.esym
    3716        51901 :       && 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       244408 :   if (expr->expr_type == EXPR_FUNCTION
    3724       244408 :       && expr->symtree
    3725       244052 :       && expr->symtree->n.sym->attr.dummy
    3726          570 :       && expr->symtree->n.sym->ns->has_implicit_none_export
    3727       244409 :       && !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        77389 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3742              : {
    3743        77389 :   code_stack *stack;
    3744        77389 :   bool saw_block = false;
    3745              : 
    3746        77389 :   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       159593 :   for (stack = cs_base; stack; stack = stack->prev)
    3755              :     {
    3756        87849 :       if (stack->current->op == EXEC_BLOCK)
    3757              :         {
    3758         1916 :           saw_block = true;
    3759         1916 :           continue;
    3760              :         }
    3761              : 
    3762        85933 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3763              :         {
    3764              : 
    3765            2 :           bool is_pure = true;
    3766        87849 :           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        71744 :   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        71744 :   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        71738 :   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        71734 :   gfc_unset_implicit_pure (NULL);
    3796        71734 :   return true;
    3797              : }
    3798              : 
    3799              : 
    3800              : static match
    3801         2811 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
    3802              : {
    3803         2811 :   gfc_symbol *s;
    3804              : 
    3805         2811 :   if (sym->attr.generic)
    3806              :     {
    3807         2810 :       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
    3808         2810 :       if (s != NULL)
    3809              :         {
    3810         2801 :           c->resolved_sym = s;
    3811         2801 :           if (!pure_subroutine (s, s->name, &c->loc))
    3812              :             return MATCH_ERROR;
    3813         2801 :           return MATCH_YES;
    3814              :         }
    3815              : 
    3816              :       /* TODO: Need to search for elemental references in generic interface.  */
    3817              :     }
    3818              : 
    3819           10 :   if (sym->attr.intrinsic)
    3820            1 :     return gfc_intrinsic_sub_interface (c, 0);
    3821              : 
    3822              :   return MATCH_NO;
    3823              : }
    3824              : 
    3825              : 
    3826              : static bool
    3827         2809 : resolve_generic_s (gfc_code *c)
    3828              : {
    3829         2809 :   gfc_symbol *sym;
    3830         2809 :   match m;
    3831              : 
    3832         2809 :   sym = c->symtree->n.sym;
    3833              : 
    3834         2811 :   for (;;)
    3835              :     {
    3836         2811 :       m = resolve_generic_s0 (c, sym);
    3837         2811 :       if (m == MATCH_YES)
    3838              :         return true;
    3839            9 :       else if (m == MATCH_ERROR)
    3840              :         return false;
    3841              : 
    3842            9 : generic:
    3843            9 :       if (sym->ns->parent == NULL)
    3844              :         break;
    3845            3 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3846              : 
    3847            3 :       if (sym == NULL)
    3848              :         break;
    3849            2 :       if (!generic_sym (sym))
    3850            0 :         goto generic;
    3851              :     }
    3852              : 
    3853              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    3854              :      that possesses a matching interface.  14.1.2.4  */
    3855            7 :   sym = c->symtree->n.sym;
    3856              : 
    3857            7 :   if (!gfc_is_intrinsic (sym, 1, c->loc))
    3858              :     {
    3859            4 :       gfc_error ("There is no specific subroutine for the generic %qs at %L",
    3860              :                  sym->name, &c->loc);
    3861            4 :       return false;
    3862              :     }
    3863              : 
    3864            3 :   m = gfc_intrinsic_sub_interface (c, 0);
    3865            3 :   if (m == MATCH_YES)
    3866              :     return true;
    3867            1 :   if (m == MATCH_NO)
    3868            1 :     gfc_error ("Generic subroutine %qs at %L is not consistent with an "
    3869              :                "intrinsic subroutine interface", sym->name, &c->loc);
    3870              : 
    3871              :   return false;
    3872              : }
    3873              : 
    3874              : 
    3875              : /* Resolve a subroutine call known to be specific.  */
    3876              : 
    3877              : static match
    3878        62884 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3879              : {
    3880        62884 :   match m;
    3881              : 
    3882        62884 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    3883              :     {
    3884         5707 :       if (sym->attr.dummy)
    3885              :         {
    3886          257 :           sym->attr.proc = PROC_DUMMY;
    3887          257 :           goto found;
    3888              :         }
    3889              : 
    3890         5450 :       sym->attr.proc = PROC_EXTERNAL;
    3891         5450 :       goto found;
    3892              :     }
    3893              : 
    3894        57177 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3895        57177 :     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        62884 : found:
    3912        62884 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3913              : 
    3914        62884 :   c->resolved_sym = sym;
    3915        62884 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3916              :     return MATCH_ERROR;
    3917              : 
    3918              :   return MATCH_YES;
    3919              : }
    3920              : 
    3921              : 
    3922              : static bool
    3923        62884 : resolve_specific_s (gfc_code *c)
    3924              : {
    3925        62884 :   gfc_symbol *sym;
    3926        62884 :   match m;
    3927              : 
    3928        62884 :   sym = c->symtree->n.sym;
    3929              : 
    3930        62884 :   for (;;)
    3931              :     {
    3932        62884 :       m = resolve_specific_s0 (c, sym);
    3933        62884 :       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        15880 : resolve_unknown_s (gfc_code *c)
    3959              : {
    3960        15880 :   gfc_symbol *sym;
    3961              : 
    3962        15880 :   sym = c->symtree->n.sym;
    3963              : 
    3964        15880 :   if (sym->attr.dummy)
    3965              :     {
    3966           26 :       sym->attr.proc = PROC_DUMMY;
    3967           26 :       goto found;
    3968              :     }
    3969              : 
    3970              :   /* See if we have an intrinsic function reference.  */
    3971              : 
    3972        15854 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3973              :     {
    3974         4299 :       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
    3975              :         return true;
    3976          319 :       return false;
    3977              :     }
    3978              : 
    3979              :   /* The reference is to an external name.  */
    3980              : 
    3981        11555 : found:
    3982        11581 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3983              : 
    3984        11581 :   c->resolved_sym = sym;
    3985              : 
    3986        11581 :   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         7300 : check_import_status (gfc_expr *e)
    4030              : {
    4031         7300 :   gfc_symtree *st;
    4032         7300 :   gfc_ref *ref;
    4033         7300 :   gfc_symbol *sym, *der;
    4034         7300 :   gfc_namespace *ns = gfc_current_ns;
    4035              : 
    4036         7300 :   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        81718 : resolve_call (gfc_code *c)
    4188              : {
    4189        81718 :   bool t;
    4190        81718 :   procedure_type ptype = PROC_INTRINSIC;
    4191        81718 :   gfc_symbol *csym, *sym;
    4192        81718 :   bool no_formal_args;
    4193              : 
    4194        81718 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    4195              : 
    4196        81718 :   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        81714 :   if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
    4204              :     {
    4205        17338 :       gfc_symtree *st;
    4206        17338 :       gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
    4207        17338 :       sym = st ? st->n.sym : NULL;
    4208        17338 :       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        81714 :   if (!c->expr1 && csym)
    4224              :     {
    4225        79973 :       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        79972 :       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        81713 :           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        81713 :   need_full_assumed_size++;
    4251              : 
    4252        81713 :   if (csym)
    4253        81713 :     ptype = csym->attr.proc;
    4254              : 
    4255        81713 :   no_formal_args = csym && is_external_proc (csym)
    4256        15672 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    4257        81713 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    4258              :     return false;
    4259              : 
    4260              :   /* Resume assumed_size checking.  */
    4261        81679 :   need_full_assumed_size--;
    4262              : 
    4263              :   /* If 'implicit none (external)' and the symbol is a dummy argument,
    4264              :      check for an 'external' attribute.  */
    4265        81679 :   if (csym->ns->has_implicit_none_export
    4266         4481 :       && 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        81678 :   if (csym && is_external_proc (csym))
    4275        15666 :     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        81678 :   if (warn_external_argument_mismatch && csym && csym->attr.dummy
    4291           14 :       && csym->attr.external)
    4292              :     {
    4293           14 :       if (csym->formal)
    4294              :         {
    4295            6 :           bool conflict;
    4296            6 :           conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
    4297              :                                                  0, 0, 0, NULL);
    4298            6 :           if (conflict)
    4299              :             {
    4300            6 :               csym->ext_dummy_arglist_mismatch = 1;
    4301            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    4302              :                            "Different argument lists in external dummy "
    4303              :                            "subroutine %s at %L and %L", csym->name,
    4304              :                            &c->loc, &csym->other_loc);
    4305              :             }
    4306              :         }
    4307            8 :       else if (!csym->formal_resolved)
    4308              :         {
    4309            7 :           gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
    4310            7 :           csym->other_loc = c->loc;
    4311              :         }
    4312              :     }
    4313              : 
    4314        81678 :   t = true;
    4315        81678 :   if (c->resolved_sym == NULL)
    4316              :     {
    4317        81573 :       c->resolved_isym = NULL;
    4318        81573 :       switch (procedure_kind (csym))
    4319              :         {
    4320         2809 :         case PTYPE_GENERIC:
    4321         2809 :           t = resolve_generic_s (c);
    4322         2809 :           break;
    4323              : 
    4324        62884 :         case PTYPE_SPECIFIC:
    4325        62884 :           t = resolve_specific_s (c);
    4326        62884 :           break;
    4327              : 
    4328        15880 :         case PTYPE_UNKNOWN:
    4329        15880 :           t = resolve_unknown_s (c);
    4330        15880 :           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        81677 :   if (!resolve_elemental_actual (NULL, c))
    4339              :     return false;
    4340              : 
    4341              :   /* Deal with complicated dependencies that the scalarizer cannot handle.  */
    4342        81669 :   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        81669 :   if (!c->expr1)
    4347        79928 :     update_current_proc_array_outer_dependency (csym);
    4348              :   else
    4349              :     /* Typebound procedure: Assume the worst.  */
    4350         1741 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4351              : 
    4352        81669 :   if (c->resolved_sym
    4353        81346 :       && 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        81669 :   csym = c->resolved_sym ? c->resolved_sym : csym;
    4359        81669 :   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        32647 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4375              : {
    4376        32647 :   bool t;
    4377        32647 :   int i;
    4378              : 
    4379        32647 :   t = true;
    4380              : 
    4381        32647 :   if (op1->shape != NULL && op2->shape != NULL)
    4382              :     {
    4383        43134 :       for (i = 0; i < op1->rank; i++)
    4384              :         {
    4385        23004 :           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        32647 :   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          111 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
    4485              :                                   char **&candidates,
    4486              :                                   size_t &candidates_len)
    4487              : {
    4488          113 :   gfc_symtree *p;
    4489              : 
    4490          113 :   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          113 :   if (uop->n.uop->op != NULL)
    4498            2 :     vec_push (candidates, candidates_len, uop->name);
    4499              : 
    4500          113 :   p = uop->left;
    4501          113 :   if (p)
    4502           36 :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4503              : 
    4504          113 :   p = uop->right;
    4505          113 :   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           75 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
    4513              : {
    4514           75 :   char **candidates = NULL;
    4515           75 :   size_t candidates_len = 0;
    4516           75 :   lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
    4517           75 :   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       193406 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4527              :                           void *data)
    4528              : {
    4529       193406 :   gfc_expr *f = *e;
    4530       193406 :   const char *name;
    4531       193406 :   static gfc_expr *last = NULL;
    4532       193406 :   bool *found = (bool *) data;
    4533              : 
    4534       193406 :   if (f->expr_type == EXPR_FUNCTION)
    4535              :     {
    4536        11892 :       *found = 1;
    4537        11892 :       if (f != last && !gfc_pure_function (f, &name)
    4538        13179 :           && !gfc_implicit_pure_function (f))
    4539              :         {
    4540         1148 :           if (name)
    4541         1148 :             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        11892 :       last = f;
    4550              :     }
    4551              : 
    4552       193406 :   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       534407 : resolve_operator (gfc_expr *e)
    4607              : {
    4608       534407 :   gfc_expr *op1, *op2;
    4609              :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4610       534407 :   bool t = true;
    4611              : 
    4612              :   /* Reduce stacked parentheses to single pair  */
    4613       534407 :   while (e->expr_type == EXPR_OP
    4614       534565 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4615        23565 :          && e->value.op.op1->expr_type == EXPR_OP
    4616       551433 :          && 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       534407 :   switch (e->value.op.op)
    4625              :     {
    4626       482279 :     default:
    4627       482279 :       if (!gfc_resolve_expr (e->value.op.op2))
    4628       534407 :         t = false;
    4629              : 
    4630              :     /* Fall through.  */
    4631              : 
    4632       534407 :     case INTRINSIC_NOT:
    4633       534407 :     case INTRINSIC_UPLUS:
    4634       534407 :     case INTRINSIC_UMINUS:
    4635       534407 :     case INTRINSIC_PARENTHESES:
    4636       534407 :       if (!gfc_resolve_expr (e->value.op.op1))
    4637              :         return false;
    4638       534246 :       if (e->value.op.op1
    4639       534237 :           && 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       534246 :       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       534244 :       break;
    4654              :     }
    4655              : 
    4656              :   /* Typecheck the new node.  */
    4657              : 
    4658       534244 :   op1 = e->value.op.op1;
    4659       534244 :   op2 = e->value.op.op2;
    4660       534244 :   if (op1 == NULL && op2 == NULL)
    4661              :     return false;
    4662              :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4663       534235 :   if (t == false)
    4664              :     return false;
    4665              : 
    4666              :   /* op1 and op2 cannot both be BOZ.  */
    4667       534169 :   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       534169 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4677       534167 :       || (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       534166 :   switch (e->value.op.op)
    4685              :     {
    4686         8178 :     case INTRINSIC_UPLUS:
    4687         8178 :     case INTRINSIC_UMINUS:
    4688         8178 :       if (op1->ts.type == BT_INTEGER
    4689              :           || op1->ts.type == BT_REAL
    4690              :           || op1->ts.type == BT_COMPLEX
    4691              :           || op1->ts.type == BT_UNSIGNED)
    4692              :         {
    4693         8109 :           e->ts = op1->ts;
    4694         8109 :           break;
    4695              :         }
    4696              : 
    4697           69 :       CHECK_INTERFACES
    4698           43 :       gfc_error ("Operand of unary numeric operator %qs at %L is %s",
    4699              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
    4700           43 :       return false;
    4701              : 
    4702       156059 :     case INTRINSIC_POWER:
    4703       156059 :     case INTRINSIC_PLUS:
    4704       156059 :     case INTRINSIC_MINUS:
    4705       156059 :     case INTRINSIC_TIMES:
    4706       156059 :     case INTRINSIC_DIVIDE:
    4707              : 
    4708              :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4709              :              conversion.  */
    4710       156059 :       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       156056 :       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       155602 :           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       155566 :           gfc_type_convert_binary (e, 1);
    4733       155566 :           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         2280 :     case INTRINSIC_CONCAT:
    4754         2280 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4755         2255 :           && op1->ts.kind == op2->ts.kind)
    4756              :         {
    4757         2246 :           e->ts.type = BT_CHARACTER;
    4758         2246 :           e->ts.kind = op1->ts.kind;
    4759         2246 :           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        69718 :     case INTRINSIC_AND:
    4768        69718 :     case INTRINSIC_OR:
    4769        69718 :     case INTRINSIC_EQV:
    4770        69718 :     case INTRINSIC_NEQV:
    4771        69718 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4772              :         {
    4773        69167 :           e->ts.type = BT_LOGICAL;
    4774        69167 :           e->ts.kind = gfc_kind_max (op1, op2);
    4775        69167 :           if (op1->ts.kind < e->ts.kind)
    4776          140 :             gfc_convert_type (op1, &e->ts, 2);
    4777        69027 :           else if (op2->ts.kind < e->ts.kind)
    4778          117 :             gfc_convert_type (op2, &e->ts, 2);
    4779              : 
    4780        69167 :           if (flag_frontend_optimize &&
    4781        58102 :             (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        52097 :               bool op2_f = false;
    4786        52097 :               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        20527 :     case INTRINSIC_NOT:
    4812              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4813        20527 :       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        20508 :       if (op1->ts.type == BT_LOGICAL)
    4822              :         {
    4823        20502 :           e->ts.type = BT_LOGICAL;
    4824        20502 :           e->ts.kind = op1->ts.kind;
    4825        20502 :           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        21403 :     case INTRINSIC_GT:
    4834        21403 :     case INTRINSIC_GT_OS:
    4835        21403 :     case INTRINSIC_GE:
    4836        21403 :     case INTRINSIC_GE_OS:
    4837        21403 :     case INTRINSIC_LT:
    4838        21403 :     case INTRINSIC_LT_OS:
    4839        21403 :     case INTRINSIC_LE:
    4840        21403 :     case INTRINSIC_LE_OS:
    4841        21403 :       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       253718 :     case INTRINSIC_EQ:
    4851       253718 :     case INTRINSIC_EQ_OS:
    4852       253718 :     case INTRINSIC_NE:
    4853       253718 :     case INTRINSIC_NE_OS:
    4854              : 
    4855       253718 :       if (flag_dec
    4856         1038 :           && is_character_based (op1->ts.type)
    4857       254053 :           && is_character_based (op2->ts.type))
    4858              :         {
    4859          204 :           convert_hollerith_to_character (op1);
    4860          204 :           convert_hollerith_to_character (op2);
    4861              :         }
    4862              : 
    4863       253718 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4864        38493 :           && op1->ts.kind == op2->ts.kind)
    4865              :         {
    4866        38456 :           e->ts.type = BT_LOGICAL;
    4867        38456 :           e->ts.kind = gfc_default_logical_kind;
    4868        38456 :           break;
    4869              :         }
    4870              : 
    4871              :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4872       215262 :       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       215262 :       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       215262 :       if (flag_dec
    4901       215262 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4902          120 :         convert_to_numeric (op1, op2);
    4903              : 
    4904       215262 :       if (flag_dec
    4905       215262 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4906          120 :         convert_to_numeric (op2, op1);
    4907              : 
    4908       215262 :       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       214133 :           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       214063 :           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       214062 :           gfc_type_convert_binary (e, 1);
    4931              : 
    4932       214062 :           e->ts.type = BT_LOGICAL;
    4933       214062 :           e->ts.kind = gfc_default_logical_kind;
    4934              : 
    4935       214062 :           if (warn_compare_reals)
    4936              :             {
    4937           70 :               gfc_intrinsic_op op = e->value.op.op;
    4938              : 
    4939              :               /* Type conversion has made sure that the types of op1 and op2
    4940              :                  agree, so it is only necessary to check the first one.   */
    4941           70 :               if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
    4942           13 :                   && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
    4943            6 :                       || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
    4944              :                 {
    4945           13 :                   const char *msg;
    4946              : 
    4947           13 :                   if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
    4948              :                     msg = G_("Equality comparison for %s at %L");
    4949              :                   else
    4950            6 :                     msg = G_("Inequality comparison for %s at %L");
    4951              : 
    4952           13 :                   gfc_warning (OPT_Wcompare_reals, msg,
    4953              :                                gfc_typename (op1), &op1->where);
    4954              :                 }
    4955              :             }
    4956              : 
    4957              :           break;
    4958              :         }
    4959              : 
    4960         1129 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4961              :         {
    4962            2 :           CHECK_INTERFACES
    4963            4 :           gfc_error ("Logicals at %L must be compared with %s instead of %s",
    4964              :                      &e->where,
    4965            2 :                      (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
    4966              :                       ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
    4967            2 :         }
    4968              :       else
    4969              :         {
    4970         1127 :           CHECK_INTERFACES
    4971          113 :           gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
    4972              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4973              :                      gfc_typename (op2));
    4974              :         }
    4975              : 
    4976              :       return false;
    4977              : 
    4978          300 :     case INTRINSIC_USER:
    4979          300 :       if (e->value.op.uop->op == NULL)
    4980              :         {
    4981           75 :           const char *name = e->value.op.uop->name;
    4982           75 :           const char *guessed;
    4983           75 :           guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
    4984           75 :           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        23368 :     case INTRINSIC_PARENTHESES:
    5009        23368 :       e->ts = op1->ts;
    5010        23368 :       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       531476 :   switch (e->value.op.op)
    5021              :     {
    5022       479497 :     case INTRINSIC_PLUS:
    5023       479497 :     case INTRINSIC_MINUS:
    5024       479497 :     case INTRINSIC_TIMES:
    5025       479497 :     case INTRINSIC_DIVIDE:
    5026       479497 :     case INTRINSIC_POWER:
    5027       479497 :     case INTRINSIC_CONCAT:
    5028       479497 :     case INTRINSIC_AND:
    5029       479497 :     case INTRINSIC_OR:
    5030       479497 :     case INTRINSIC_EQV:
    5031       479497 :     case INTRINSIC_NEQV:
    5032       479497 :     case INTRINSIC_EQ:
    5033       479497 :     case INTRINSIC_EQ_OS:
    5034       479497 :     case INTRINSIC_NE:
    5035       479497 :     case INTRINSIC_NE_OS:
    5036       479497 :     case INTRINSIC_GT:
    5037       479497 :     case INTRINSIC_GT_OS:
    5038       479497 :     case INTRINSIC_GE:
    5039       479497 :     case INTRINSIC_GE_OS:
    5040       479497 :     case INTRINSIC_LT:
    5041       479497 :     case INTRINSIC_LT_OS:
    5042       479497 :     case INTRINSIC_LE:
    5043       479497 :     case INTRINSIC_LE_OS:
    5044              : 
    5045       479497 :       if (op1->rank == 0 && op2->rank == 0)
    5046       426995 :         e->rank = 0;
    5047              : 
    5048       479497 :       if (op1->rank == 0 && op2->rank != 0)
    5049              :         {
    5050         2589 :           e->rank = op2->rank;
    5051              : 
    5052         2589 :           if (e->shape == NULL)
    5053         2559 :             e->shape = gfc_copy_shape (op2->shape, op2->rank);
    5054              :         }
    5055              : 
    5056       479497 :       if (op1->rank != 0 && op2->rank == 0)
    5057              :         {
    5058        17205 :           e->rank = op1->rank;
    5059              : 
    5060        17205 :           if (e->shape == NULL)
    5061        17181 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5062              :         }
    5063              : 
    5064       479497 :       if (op1->rank != 0 && op2->rank != 0)
    5065              :         {
    5066        32708 :           if (op1->rank == op2->rank)
    5067              :             {
    5068        32708 :               e->rank = op1->rank;
    5069        32708 :               if (e->shape == NULL)
    5070              :                 {
    5071        32647 :                   t = compare_shapes (op1, op2);
    5072        32647 :                   if (!t)
    5073            3 :                     e->shape = NULL;
    5074              :                   else
    5075        32644 :                     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        51979 :     case INTRINSIC_PARENTHESES:
    5093        51979 :     case INTRINSIC_NOT:
    5094        51979 :     case INTRINSIC_UPLUS:
    5095        51979 :     case INTRINSIC_UMINUS:
    5096              :       /* Simply copy arrayness attribute */
    5097        51979 :       e->rank = op1->rank;
    5098        51979 :       e->corank = op1->corank;
    5099              : 
    5100        51979 :       if (e->shape == NULL)
    5101        51972 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5102              : 
    5103              :       break;
    5104              : 
    5105              :     default:
    5106              :       break;
    5107              :     }
    5108              : 
    5109       532018 : simplify_op:
    5110              : 
    5111              :   /* Attempt to simplify the expression.  */
    5112            3 :   if (t)
    5113              :     {
    5114       532015 :       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       532015 :       if (!gfc_is_constant_expr (e))
    5119       486111 :         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       468810 : compare_bound (gfc_expr *a, gfc_expr *b)
    5204              : {
    5205       468810 :   int i;
    5206              : 
    5207       468810 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    5208       308284 :       || 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       212896 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    5215              :     return CMP_UNKNOWN;
    5216              : 
    5217       212892 :   i = mpz_cmp (a->value.integer, b->value.integer);
    5218              : 
    5219       212892 :   if (i < 0)
    5220              :     return CMP_LT;
    5221        99889 :   if (i > 0)
    5222        39689 :     return CMP_GT;
    5223              :   return CMP_EQ;
    5224              : }
    5225              : 
    5226              : 
    5227              : /* Compare an integer expression with an integer.  */
    5228              : 
    5229              : static compare_result
    5230        75231 : compare_bound_int (gfc_expr *a, int b)
    5231              : {
    5232        75231 :   int i;
    5233              : 
    5234        75231 :   if (a == NULL
    5235        32361 :       || a->expr_type != EXPR_CONSTANT
    5236        29413 :       || a->ts.type != BT_INTEGER)
    5237              :     return CMP_UNKNOWN;
    5238              : 
    5239        29413 :   i = mpz_cmp_si (a->value.integer, b);
    5240              : 
    5241        29413 :   if (i < 0)
    5242              :     return CMP_LT;
    5243        24939 :   if (i > 0)
    5244        21440 :     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        69996 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    5253              : {
    5254        69996 :   int i;
    5255              : 
    5256        69996 :   if (a == NULL
    5257        57163 :       || a->expr_type != EXPR_CONSTANT
    5258        55040 :       || a->ts.type != BT_INTEGER)
    5259              :     return CMP_UNKNOWN;
    5260              : 
    5261        55037 :   i = mpz_cmp (a->value.integer, b);
    5262              : 
    5263        55037 :   if (i < 0)
    5264              :     return CMP_LT;
    5265        25070 :   if (i > 0)
    5266        10710 :     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        52303 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    5277              :                                 gfc_expr *stride, mpz_t last)
    5278              : {
    5279        52303 :   mpz_t rem;
    5280              : 
    5281        52303 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    5282        37139 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    5283        32429 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    5284              :     return 0;
    5285              : 
    5286        32110 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    5287        32109 :       || (stride != NULL && stride->ts.type != BT_INTEGER))
    5288              :     return 0;
    5289              : 
    5290         6647 :   if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
    5291              :     {
    5292        25588 :       if (compare_bound (start, end) == CMP_GT)
    5293              :         return 0;
    5294        24199 :       mpz_set (last, end->value.integer);
    5295        24199 :       return 1;
    5296              :     }
    5297              : 
    5298         6521 :   if (compare_bound_int (stride, 0) == CMP_GT)
    5299              :     {
    5300              :       /* Stride is positive */
    5301         5156 :       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         6501 :   mpz_init (rem);
    5312         6501 :   mpz_sub (rem, end->value.integer, start->value.integer);
    5313         6501 :   mpz_tdiv_r (rem, rem, stride->value.integer);
    5314         6501 :   mpz_sub (last, end->value.integer, rem);
    5315         6501 :   mpz_clear (rem);
    5316              : 
    5317         6501 :   return 1;
    5318              : }
    5319              : 
    5320              : 
    5321              : /* Compare a single dimension of an array reference to the array
    5322              :    specification.  */
    5323              : 
    5324              : static bool
    5325       217247 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5326              : {
    5327       217247 :   mpz_t last_value;
    5328              : 
    5329       217247 :   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       216841 :   switch (ar->dimen_type[i])
    5344              :     {
    5345              :     case DIMEN_VECTOR:
    5346              :     case DIMEN_THIS_IMAGE:
    5347              :       break;
    5348              : 
    5349       156244 :     case DIMEN_STAR:
    5350       156244 :     case DIMEN_ELEMENT:
    5351       156244 :       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       156242 :       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        52348 :     case DIMEN_RANGE:
    5385        52348 :       {
    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        52348 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5390        52348 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5391              : 
    5392              :         /* Check for zero stride, which is not allowed.  */
    5393        52348 :         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        52347 :         if (comp_start_end == CMP_EQ
    5405        51585 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5406        48796 :                 && comp_start_end == CMP_LT)
    5407        22946 :             || (comp_stride_zero == CMP_LT
    5408        22946 :                 && comp_start_end == CMP_GT))
    5409              :           {
    5410        30746 :             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        30719 :             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        52303 :         mpz_init (last_value);
    5431        52303 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5432              :                                             last_value))
    5433              :           {
    5434        30700 :             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        30697 :             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        52293 :         mpz_clear (last_value);
    5454              : 
    5455              : #undef AR_START
    5456              : #undef AR_END
    5457              :       }
    5458        52293 :       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       427767 : compare_spec_to_ref (gfc_array_ref *ar)
    5472              : {
    5473       427767 :   gfc_array_spec *as;
    5474       427767 :   int i;
    5475              : 
    5476       427767 :   as = ar->as;
    5477       427767 :   i = as->rank - 1;
    5478              :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5479       427767 :   if (as->type == AS_ASSUMED_SIZE
    5480         5804 :       && (/*ar->type == AR_FULL
    5481         5804 :           ||*/ (ar->type == AR_SECTION
    5482          517 :               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
    5483              :     {
    5484            5 :       gfc_error ("Rightmost upper bound of assumed size array section "
    5485              :                  "not specified at %L", &ar->where);
    5486            5 :       return false;
    5487              :     }
    5488              : 
    5489       427762 :   if (ar->type == AR_FULL)
    5490              :     return true;
    5491              : 
    5492       165062 :   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       165034 :   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       372466 :   for (i = 0; i < as->rank; i++)
    5508       207433 :     if (!check_dimension (i, ar, as))
    5509              :       return false;
    5510              : 
    5511              :   /* Local access has no coarray spec.  */
    5512       165033 :   if (ar->codimen != 0)
    5513        18870 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5514              :       {
    5515         9816 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5516         6831 :             && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
    5517              :           {
    5518            2 :             gfc_error ("Coindex of codimension %d must be a scalar at %L",
    5519            2 :                        i + 1 - as->rank, &ar->where);
    5520            2 :             return false;
    5521              :           }
    5522         9814 :         if (!check_dimension (i, ar, as))
    5523              :           return false;
    5524              :       }
    5525              : 
    5526              :   return true;
    5527              : }
    5528              : 
    5529              : 
    5530              : /* Resolve one part of an array index.  */
    5531              : 
    5532              : static bool
    5533       736759 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5534              :                      int force_index_integer_kind)
    5535              : {
    5536       736759 :   gfc_typespec ts;
    5537              : 
    5538       736759 :   if (index == NULL)
    5539              :     return true;
    5540              : 
    5541       218838 :   if (!gfc_resolve_expr (index))
    5542              :     return false;
    5543              : 
    5544       218827 :   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       218825 :   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       218821 :   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       218821 :   if ((index->ts.kind != gfc_index_integer_kind
    5563       213876 :        && force_index_integer_kind)
    5564       187344 :       || (index->ts.type != BT_INTEGER
    5565              :           && index->ts.type != BT_UNKNOWN))
    5566              :     {
    5567        31813 :       gfc_clear_ts (&ts);
    5568        31813 :       ts.type = BT_INTEGER;
    5569        31813 :       ts.kind = gfc_index_integer_kind;
    5570              : 
    5571        31813 :       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       491431 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5581              : {
    5582       491431 :   return gfc_resolve_index_1 (index, check_scalar, 1);
    5583              : }
    5584              : 
    5585              : /* Resolve a dim argument to an intrinsic function.  */
    5586              : 
    5587              : bool
    5588        23915 : gfc_resolve_dim_arg (gfc_expr *dim)
    5589              : {
    5590        23915 :   if (dim == NULL)
    5591              :     return true;
    5592              : 
    5593        23915 :   if (!gfc_resolve_expr (dim))
    5594              :     return false;
    5595              : 
    5596        23915 :   if (dim->rank != 0)
    5597              :     {
    5598            0 :       gfc_error ("Argument dim at %L must be scalar", &dim->where);
    5599            0 :       return false;
    5600              : 
    5601              :     }
    5602              : 
    5603        23915 :   if (dim->ts.type != BT_INTEGER)
    5604              :     {
    5605            0 :       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
    5606            0 :       return false;
    5607              :     }
    5608              : 
    5609        23915 :   if (dim->ts.kind != gfc_index_integer_kind)
    5610              :     {
    5611        15306 :       gfc_typespec ts;
    5612              : 
    5613        15306 :       gfc_clear_ts (&ts);
    5614        15306 :       ts.type = BT_INTEGER;
    5615        15306 :       ts.kind = gfc_index_integer_kind;
    5616              : 
    5617        15306 :       gfc_convert_type_warn (dim, &ts, 2, 0);
    5618              :     }
    5619              : 
    5620              :   return true;
    5621              : }
    5622              : 
    5623              : /* Given an expression that contains array references, update those array
    5624              :    references to point to the right array specifications.  While this is
    5625              :    filled in during matching, this information is difficult to save and load
    5626              :    in a module, so we take care of it here.
    5627              : 
    5628              :    The idea here is that the original array reference comes from the
    5629              :    base symbol.  We traverse the list of reference structures, setting
    5630              :    the stored reference to references.  Component references can
    5631              :    provide an additional array specification.  */
    5632              : static void
    5633              : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
    5634              : 
    5635              : static bool
    5636          914 : find_array_spec (gfc_expr *e)
    5637              : {
    5638          914 :   gfc_array_spec *as;
    5639          914 :   gfc_component *c;
    5640          914 :   gfc_ref *ref;
    5641          914 :   bool class_as = false;
    5642              : 
    5643          914 :   if (e->symtree->n.sym->assoc)
    5644              :     {
    5645          217 :       if (e->symtree->n.sym->assoc->target)
    5646          217 :         gfc_resolve_expr (e->symtree->n.sym->assoc->target);
    5647          217 :       resolve_assoc_var (e->symtree->n.sym, false);
    5648              :     }
    5649              : 
    5650          914 :   if (e->symtree->n.sym->ts.type == BT_CLASS)
    5651              :     {
    5652          124 :       as = CLASS_DATA (e->symtree->n.sym)->as;
    5653          124 :       class_as = true;
    5654              :     }
    5655              :   else
    5656          790 :     as = e->symtree->n.sym->as;
    5657              : 
    5658         2085 :   for (ref = e->ref; ref; ref = ref->next)
    5659         1178 :     switch (ref->type)
    5660              :       {
    5661          916 :       case REF_ARRAY:
    5662          916 :         if (as == NULL)
    5663              :           {
    5664            7 :             locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
    5665           14 :                          ? ref->u.ar.where : e->where);
    5666            7 :             gfc_error ("Invalid array reference of a non-array entity at %L",
    5667              :                        &loc);
    5668            7 :             return false;
    5669              :           }
    5670              : 
    5671          909 :         ref->u.ar.as = as;
    5672          909 :         if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
    5673              :         as = NULL;
    5674              :         break;
    5675              : 
    5676          238 :       case REF_COMPONENT:
    5677          238 :         c = ref->u.c.component;
    5678          238 :         if (c->attr.dimension)
    5679              :           {
    5680          107 :             if (as != NULL && !(class_as && as == c->as))
    5681            0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5682          107 :             as = c->as;
    5683              :           }
    5684              : 
    5685              :         break;
    5686              : 
    5687              :       case REF_SUBSTRING:
    5688              :       case REF_INQUIRY:
    5689              :         break;
    5690              :       }
    5691              : 
    5692          907 :   if (as != NULL)
    5693            0 :     gfc_internal_error ("find_array_spec(): unused as(2)");
    5694              : 
    5695              :   return true;
    5696              : }
    5697              : 
    5698              : 
    5699              : /* Resolve an array reference.  */
    5700              : 
    5701              : static bool
    5702       428481 : resolve_array_ref (gfc_array_ref *ar)
    5703              : {
    5704       428481 :   int i, check_scalar;
    5705       428481 :   gfc_expr *e;
    5706              : 
    5707       673792 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5708              :     {
    5709       245328 :       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       245328 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5715              :         return false;
    5716       245313 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5717              :         return false;
    5718       245311 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5719              :         return false;
    5720              : 
    5721       245311 :       e = ar->start[i];
    5722              : 
    5723       245311 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5724       146362 :         switch (e->rank)
    5725              :           {
    5726       145270 :           case 0:
    5727       145270 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5728       145270 :             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       245311 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5748        72059 :           && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
    5749         8384 :           && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
    5750         8237 :           && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
    5751              :         {
    5752         8236 :           mpz_t size, end;
    5753              : 
    5754         8236 :           if (gfc_ref_dimen_size (ar, i, &size, &end))
    5755              :             {
    5756         6531 :               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         2568 :               else if (ar->end[i]->ts.type == BT_INTEGER
    5764         2568 :                        && ar->end[i]->expr_type == EXPR_CONSTANT)
    5765              :                 {
    5766         2568 :                   mpz_set (ar->end[i]->value.integer, end);
    5767              :                 }
    5768              :               else
    5769            0 :                 gcc_unreachable ();
    5770              : 
    5771         6531 :               mpz_clear (size);
    5772         6531 :               mpz_clear (end);
    5773              :             }
    5774              :         }
    5775              :     }
    5776              : 
    5777       428464 :   if (ar->type == AR_FULL)
    5778              :     {
    5779       266146 :       if (ar->as->rank == 0)
    5780         3412 :         ar->type = AR_ELEMENT;
    5781              : 
    5782              :       /* Make sure array is the same as array(:,:), this way
    5783              :          we don't need to special case all the time.  */
    5784       266146 :       ar->dimen = ar->as->rank;
    5785       634216 :       for (i = 0; i < ar->dimen; i++)
    5786              :         {
    5787       368070 :           ar->dimen_type[i] = DIMEN_RANGE;
    5788              : 
    5789       368070 :           gcc_assert (ar->start[i] == NULL);
    5790       368070 :           gcc_assert (ar->end[i] == NULL);
    5791       368070 :           gcc_assert (ar->stride[i] == NULL);
    5792              :         }
    5793              :     }
    5794              : 
    5795              :   /* If the reference type is unknown, figure out what kind it is.  */
    5796              : 
    5797       428464 :   if (ar->type == AR_UNKNOWN)
    5798              :     {
    5799       149229 :       ar->type = AR_ELEMENT;
    5800       288798 :       for (i = 0; i < ar->dimen; i++)
    5801       177831 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5802       177831 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5803              :           {
    5804        38262 :             ar->type = AR_SECTION;
    5805        38262 :             break;
    5806              :           }
    5807              :     }
    5808              : 
    5809       428464 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5810              :     return false;
    5811              : 
    5812       428428 :   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       428428 :   if (ar->codimen)
    5821              :     {
    5822        13631 :       if (ar->team_type == TEAM_NUMBER)
    5823              :         {
    5824           60 :           if (!gfc_resolve_expr (ar->team))
    5825              :             return false;
    5826              : 
    5827           60 :           if (ar->team->rank != 0)
    5828              :             {
    5829            0 :               gfc_error ("TEAM_NUMBER argument at %L must be scalar",
    5830              :                          &ar->team->where);
    5831            0 :               return false;
    5832              :             }
    5833              : 
    5834           60 :           if (ar->team->ts.type != BT_INTEGER)
    5835              :             {
    5836            6 :               gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
    5837              :                          "type, found %s",
    5838            6 :                          &ar->team->where,
    5839              :                          gfc_basic_typename (ar->team->ts.type));
    5840            6 :               return false;
    5841              :             }
    5842              :         }
    5843        13571 :       else if (ar->team_type == TEAM_TEAM)
    5844              :         {
    5845           42 :           if (!gfc_resolve_expr (ar->team))
    5846              :             return false;
    5847              : 
    5848           42 :           if (ar->team->rank != 0)
    5849              :             {
    5850            3 :               gfc_error ("TEAM argument at %L must be scalar",
    5851              :                          &ar->team->where);
    5852            3 :               return false;
    5853              :             }
    5854              : 
    5855           39 :           if (ar->team->ts.type != BT_DERIVED
    5856           36 :               || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
    5857           36 :               || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
    5858              :             {
    5859            3 :               gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
    5860              :                          "the intrinsic module ISO_FORTRAN_ENV, found %s",
    5861            3 :                          &ar->team->where,
    5862              :                          gfc_basic_typename (ar->team->ts.type));
    5863            3 :               return false;
    5864              :             }
    5865              :         }
    5866        13619 :       if (ar->stat)
    5867              :         {
    5868           62 :           if (!gfc_resolve_expr (ar->stat))
    5869              :             return false;
    5870              : 
    5871           62 :           if (ar->stat->rank != 0)
    5872              :             {
    5873            3 :               gfc_error ("STAT argument at %L must be scalar",
    5874              :                          &ar->stat->where);
    5875            3 :               return false;
    5876              :             }
    5877              : 
    5878           59 :           if (ar->stat->ts.type != BT_INTEGER)
    5879              :             {
    5880            3 :               gfc_error ("STAT argument at %L must be of INTEGER "
    5881              :                          "type, found %s",
    5882            3 :                          &ar->stat->where,
    5883              :                          gfc_basic_typename (ar->stat->ts.type));
    5884            3 :               return false;
    5885              :             }
    5886              : 
    5887           56 :           if (ar->stat->expr_type != EXPR_VARIABLE)
    5888              :             {
    5889            0 :               gfc_error ("STAT's expression at %L must be a variable",
    5890              :                          &ar->stat->where);
    5891            0 :               return false;
    5892              :             }
    5893              :         }
    5894              :     }
    5895              :   return true;
    5896              : }
    5897              : 
    5898              : 
    5899              : bool
    5900         8800 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
    5901              : {
    5902         8800 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    5903              : 
    5904         8800 :   if (ref->u.ss.start != NULL)
    5905              :     {
    5906         8800 :       if (!gfc_resolve_expr (ref->u.ss.start))
    5907              :         return false;
    5908              : 
    5909         8800 :       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         8799 :       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         8799 :       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
    5924         8799 :           && (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         8798 :   if (ref->u.ss.end != NULL)
    5934              :     {
    5935         8604 :       if (!gfc_resolve_expr (ref->u.ss.end))
    5936              :         return false;
    5937              : 
    5938         8604 :       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         8603 :       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         8603 :       if (ref->u.ss.length != NULL
    5953         8267 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
    5954         8615 :           && (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         8599 :       if (compare_bound_mpz_t (ref->u.ss.end,
    5963         8599 :                                gfc_integer_kinds[k].huge) == CMP_GT
    5964         8599 :           && (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         8595 :       if (ref->u.ss.length != NULL
    5975         8259 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
    5976         9511 :           && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
    5977          230 :         *equal_length = true;
    5978              :     }
    5979              : 
    5980              :   return true;
    5981              : }
    5982              : 
    5983              : 
    5984              : /* This function supplies missing substring charlens.  */
    5985              : 
    5986              : void
    5987         4564 : gfc_resolve_substring_charlen (gfc_expr *e)
    5988              : {
    5989         4564 :   gfc_ref *char_ref;
    5990         4564 :   gfc_expr *start, *end;
    5991         4564 :   gfc_typespec *ts = NULL;
    5992         4564 :   mpz_t diff;
    5993              : 
    5994         8889 :   for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
    5995              :     {
    5996         7042 :       if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
    5997              :         break;
    5998         4325 :       if (char_ref->type == REF_COMPONENT)
    5999          328 :         ts = &char_ref->u.c.component->ts;
    6000              :     }
    6001              : 
    6002         4564 :   if (!char_ref || char_ref->type == REF_INQUIRY)
    6003         1909 :     return;
    6004              : 
    6005         2717 :   gcc_assert (char_ref->next == NULL);
    6006              : 
    6007         2717 :   if (e->ts.u.cl)
    6008              :     {
    6009          120 :       if (e->ts.u.cl->length)
    6010          108 :         gfc_free_expr (e->ts.u.cl->length);
    6011           12 :       else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
    6012              :         return;
    6013              :     }
    6014              : 
    6015         2705 :   if (!e->ts.u.cl)
    6016         2597 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6017              : 
    6018         2705 :   if (char_ref->u.ss.start)
    6019         2705 :     start = gfc_copy_expr (char_ref->u.ss.start);
    6020              :   else
    6021            0 :     start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    6022              : 
    6023         2705 :   if (char_ref->u.ss.end)
    6024         2655 :     end = gfc_copy_expr (char_ref->u.ss.end);
    6025           50 :   else if (e->expr_type == EXPR_VARIABLE)
    6026              :     {
    6027           50 :       if (!ts)
    6028           32 :         ts = &e->symtree->n.sym->ts;
    6029           50 :       end = gfc_copy_expr (ts->u.cl->length);
    6030              :     }
    6031              :   else
    6032              :     end = NULL;
    6033              : 
    6034         2705 :   if (!start || !end)
    6035              :     {
    6036           50 :       gfc_free_expr (start);
    6037           50 :       gfc_free_expr (end);
    6038           50 :       return;
    6039              :     }
    6040              : 
    6041              :   /* Length = (end - start + 1).
    6042              :      Check first whether it has a constant length.  */
    6043         2655 :   if (gfc_dep_difference (end, start, &diff))
    6044              :     {
    6045         2539 :       gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
    6046              :                                              &e->where);
    6047              : 
    6048         2539 :       mpz_add_ui (len->value.integer, diff, 1);
    6049         2539 :       mpz_clear (diff);
    6050         2539 :       e->ts.u.cl->length = len;
    6051              :       /* The check for length < 0 is handled below */
    6052              :     }
    6053              :   else
    6054              :     {
    6055          116 :       e->ts.u.cl->length = gfc_subtract (end, start);
    6056          116 :       e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
    6057              :                                     gfc_get_int_expr (gfc_charlen_int_kind,
    6058              :                                                       NULL, 1));
    6059              :     }
    6060              : 
    6061              :   /* F2008, 6.4.1:  Both the starting point and the ending point shall
    6062              :      be within the range 1, 2, ..., n unless the starting point exceeds
    6063              :      the ending point, in which case the substring has length zero.  */
    6064              : 
    6065         2655 :   if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
    6066           15 :     mpz_set_si (e->ts.u.cl->length->value.integer, 0);
    6067              : 
    6068         2655 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    6069         2655 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    6070              : 
    6071              :   /* Make sure that the length is simplified.  */
    6072         2655 :   gfc_simplify_expr (e->ts.u.cl->length, 1);
    6073         2655 :   gfc_resolve_expr (e->ts.u.cl->length);
    6074              : }
    6075              : 
    6076              : 
    6077              : /* Convert an array reference to an array element so that PDT KIND and LEN
    6078              :    or inquiry references are always scalar.  */
    6079              : 
    6080              : static void
    6081           21 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
    6082              : {
    6083           21 :   gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
    6084           21 :   int dim;
    6085              : 
    6086           21 :   array_ref->u.ar.type = AR_ELEMENT;
    6087           21 :   expr->rank = 0;
    6088              :   /* Suppress the runtime bounds check.  */
    6089           21 :   expr->no_bounds_check = 1;
    6090           42 :   for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
    6091              :     {
    6092           21 :       array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
    6093           21 :       if (array_ref->u.ar.start[dim])
    6094            0 :         gfc_free_expr (array_ref->u.ar.start[dim]);
    6095              : 
    6096           21 :       if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
    6097            9 :         array_ref->u.ar.start[dim]
    6098            9 :                         = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
    6099              :       else
    6100           12 :         array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
    6101              : 
    6102           21 :       if (array_ref->u.ar.end[dim])
    6103            0 :         gfc_free_expr (array_ref->u.ar.end[dim]);
    6104           21 :       if (array_ref->u.ar.stride[dim])
    6105            0 :         gfc_free_expr (array_ref->u.ar.stride[dim]);
    6106              :     }
    6107           21 :   gfc_free_expr (unity);
    6108           21 : }
    6109              : 
    6110              : 
    6111              : /* Resolve subtype references.  */
    6112              : 
    6113              : bool
    6114       545611 : gfc_resolve_ref (gfc_expr *expr)
    6115              : {
    6116       545611 :   int current_part_dimension, n_components, seen_part_dimension;
    6117       545611 :   gfc_ref *ref, **prev, *array_ref;
    6118       545611 :   bool equal_length;
    6119       545611 :   gfc_symbol *last_pdt = NULL;
    6120              : 
    6121      1071950 :   for (ref = expr->ref; ref; ref = ref->next)
    6122       527253 :     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
    6123              :       {
    6124          914 :         if (!find_array_spec (expr))
    6125              :           return false;
    6126              :         break;
    6127              :       }
    6128              : 
    6129      1600037 :   for (prev = &expr->ref; *prev != NULL;
    6130       527319 :        prev = *prev == NULL ? prev : &(*prev)->next)
    6131       527398 :     switch ((*prev)->type)
    6132              :       {
    6133       428481 :       case REF_ARRAY:
    6134       428481 :         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         8519 :       case REF_SUBSTRING:
    6143         8519 :         equal_length = false;
    6144         8519 :         if (!gfc_resolve_substring (*prev, &equal_length))
    6145              :             return false;
    6146              : 
    6147         8511 :         if (expr->expr_type != EXPR_SUBSTRING && equal_length)
    6148              :           {
    6149              :             /* Remove the reference and move the charlen, if any.  */
    6150          205 :             ref = *prev;
    6151          205 :             *prev = ref->next;
    6152          205 :             ref->next = NULL;
    6153          205 :             expr->ts.u.cl = ref->u.ss.length;
    6154          205 :             ref->u.ss.length = NULL;
    6155          205 :             gfc_free_ref_list (ref);
    6156              :           }
    6157              :         break;
    6158              :       }
    6159              : 
    6160              :   /* Check constraints on part references.  */
    6161              : 
    6162       545525 :   current_part_dimension = 0;
    6163       545525 :   seen_part_dimension = 0;
    6164       545525 :   n_components = 0;
    6165       545525 :   array_ref = NULL;
    6166              : 
    6167       545525 :   if (expr->expr_type == EXPR_VARIABLE && IS_PDT (expr))
    6168          540 :     last_pdt = expr->symtree->n.sym->ts.u.derived;
    6169              : 
    6170      1072614 :   for (ref = expr->ref; ref; ref = ref->next)
    6171              :     {
    6172       527100 :       switch (ref->type)
    6173              :         {
    6174       428403 :         case REF_ARRAY:
    6175       428403 :           array_ref = ref;
    6176       428403 :           switch (ref->u.ar.type)
    6177              :             {
    6178       262732 :             case AR_FULL:
    6179              :               /* Coarray scalar.  */
    6180       262732 :               if (ref->u.ar.as->rank == 0)
    6181              :                 {
    6182              :                   current_part_dimension = 0;
    6183              :                   break;
    6184              :                 }
    6185              :               /* Fall through.  */
    6186       304064 :             case AR_SECTION:
    6187       304064 :               current_part_dimension = 1;
    6188       304064 :               break;
    6189              : 
    6190       124339 :             case AR_ELEMENT:
    6191       124339 :               array_ref = NULL;
    6192       124339 :               current_part_dimension = 0;
    6193       124339 :               break;
    6194              : 
    6195            0 :             case AR_UNKNOWN:
    6196            0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    6197              :             }
    6198              : 
    6199              :           break;
    6200              : 
    6201        89570 :         case REF_COMPONENT:
    6202        89570 :           if (current_part_dimension || seen_part_dimension)
    6203              :             {
    6204              :               /* F03:C614.  */
    6205         6851 :               if (ref->u.c.component->attr.pointer
    6206         6848 :                   || ref->u.c.component->attr.proc_pointer
    6207         6847 :                   || (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         6847 :               else if (ref->u.c.component->attr.allocatable
    6216         6841 :                         || (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        89559 :           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        89559 :           if (ref->u.c.component->ts.type == BT_DERIVED)
    6244              :             {
    6245        20816 :               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        20816 :               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        89559 :           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        89559 :           n_components++;
    6268        89559 :           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       527089 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    6284       513796 :            || ref->next == NULL)
    6285              :           && current_part_dimension
    6286       461937 :           && 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       527089 :       if (ref->type == REF_COMPONENT)
    6294              :         {
    6295        89559 :           if (current_part_dimension)
    6296         6653 :             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      2610800 : expression_shape (gfc_expr *e)
    6312              : {
    6313      2610800 :   mpz_t array[GFC_MAX_DIMENSIONS];
    6314      2610800 :   int i;
    6315              : 
    6316      2610800 :   if (e->rank <= 0 || e->shape != NULL)
    6317      2433454 :     return;
    6318              : 
    6319       708260 :   for (i = 0; i < e->rank; i++)
    6320       478391 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    6321       177346 :       goto fail;
    6322              : 
    6323       229869 :   e->shape = gfc_get_shape (e->rank);
    6324              : 
    6325       229869 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    6326              : 
    6327       229869 :   return;
    6328              : 
    6329       177346 : fail:
    6330       179017 :   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      2610800 : gfc_expression_rank (gfc_expr *e)
    6340              : {
    6341      2610800 :   gfc_ref *ref, *last_arr_ref = nullptr;
    6342      2610800 :   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      2610800 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    6347              : 
    6348      2610800 :   if (e->ref == NULL)
    6349              :     {
    6350      1925156 :       if (e->expr_type == EXPR_ARRAY)
    6351        72408 :         goto done;
    6352              :       /* Constructors can have a rank different from one via RESHAPE().  */
    6353              : 
    6354      1852748 :       if (e->symtree != NULL)
    6355              :         {
    6356              :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    6357      1852736 :           gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
    6358        13875 :                                 && e->symtree->n.sym->ts.u.derived
    6359        13870 :                                 && CLASS_DATA (e->symtree->n.sym))
    6360      1852736 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    6361              :                                  : e->symtree->n.sym->as;
    6362      1852736 :           if (as)
    6363              :             {
    6364          620 :               e->rank = as->rank;
    6365          620 :               e->corank = as->corank;
    6366          620 :               goto done;
    6367              :             }
    6368              :         }
    6369      1852128 :       e->rank = 0;
    6370      1852128 :       e->corank = 0;
    6371      1852128 :       goto done;
    6372              :     }
    6373              : 
    6374              :   rank = 0;
    6375              :   corank = 0;
    6376              : 
    6377      1084662 :   for (ref = e->ref; ref; ref = ref->next)
    6378              :     {
    6379       793958 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
    6380          554 :           && ref->u.c.component->attr.function && !ref->next)
    6381              :         {
    6382          358 :           rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
    6383          358 :           corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
    6384              :         }
    6385              : 
    6386       793958 :       if (ref->type != REF_ARRAY)
    6387       158902 :         continue;
    6388              : 
    6389       635056 :       last_arr_ref = ref;
    6390       635056 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6391              :         {
    6392       348910 :           rank = ref->u.ar.as->rank;
    6393       348910 :           break;
    6394              :         }
    6395              : 
    6396       286146 :       if (ref->u.ar.type == AR_SECTION)
    6397              :         {
    6398              :           /* Figure out the rank of the section.  */
    6399        46030 :           if (rank != 0)
    6400            0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6401              : 
    6402       114672 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6403        68642 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6404        68642 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6405        59822 :               rank++;
    6406              : 
    6407              :           break;
    6408              :         }
    6409              :     }
    6410       685644 :   if (last_arr_ref && last_arr_ref->u.ar.as
    6411       614921 :       && last_arr_ref->u.ar.as->rank != -1)
    6412              :     {
    6413        19272 :       for (i = last_arr_ref->u.ar.as->rank;
    6414       625964 :            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        20162 :           if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6418        19595 :               || (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        19272 :           else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6425        19272 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6426        19174 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6427        16683 :             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       685644 :   e->rank = rank;
    6434       685644 :   e->corank = corank;
    6435              : 
    6436      2610800 : done:
    6437      2610800 :   expression_shape (e);
    6438      2610800 : }
    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     12242448 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6446              : {
    6447     12242448 :   if (op1->expr_type == EXPR_VARIABLE)
    6448       738034 :     gfc_expression_rank (op1);
    6449     12242448 :   if (op2->expr_type == EXPR_VARIABLE)
    6450       447280 :     gfc_expression_rank (op2);
    6451              : 
    6452        77346 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6453     12319468 :          && (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      1334366 : resolve_variable (gfc_expr *e)
    6461              : {
    6462      1334366 :   gfc_symbol *sym;
    6463      1334366 :   bool t;
    6464              : 
    6465      1334366 :   t = true;
    6466              : 
    6467      1334366 :   if (e->symtree == NULL)
    6468              :     return false;
    6469      1333921 :   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      1333921 :   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      1333738 :   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      1333167 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6505        37490 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6506        37485 :              && CLASS_DATA (sym)->as
    6507        14638 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6508      1332215 :             || (sym->ts.type != BT_CLASS && sym->as
    6509       364626 :                 && sym->as->type == AS_ASSUMED_RANK))
    6510         8021 :            && !sym->attr.select_rank_temporary
    6511         8021 :            && !(sym->assoc && sym->assoc->ar))
    6512              :     {
    6513         8021 :       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         7877 :       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      1333755 :   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      1333754 :   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      1333747 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6555        37490 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6556        37485 :         && CLASS_DATA (sym)->as
    6557        14638 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6558      1332795 :        || (sym->ts.type != BT_CLASS && sym->as
    6559       365162 :            && sym->as->type == AS_ASSUMED_RANK))
    6560         8161 :       && !(sym->assoc && sym->assoc->ar)
    6561         8161 :       && e->ref
    6562         8161 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6563         8157 :            && 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      1333743 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6575              :     {
    6576          410 :       gfc_fixup_inferred_type_refs (e);
    6577              :       /* KIND inquiry ref returns the kind of the target.  */
    6578          410 :       if (e->expr_type == EXPR_CONSTANT)
    6579              :         return true;
    6580              :     }
    6581      1333333 :   else if (IS_INFERRED_TYPE (e)
    6582          489 :            && sym->ts.type != BT_UNKNOWN
    6583          489 :            && (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          192 :     e->ts = sym->ts;
    6588      1333141 :   else if (sym->attr.select_type_temporary
    6589         8978 :            && 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      1333731 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6597          605 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6598          605 :       && sym->assoc->target->ts.u.derived
    6599          605 :       && CLASS_DATA (sym->assoc->target)
    6600          605 :       && CLASS_DATA (sym->assoc->target)->as)
    6601              :     {
    6602              :       gfc_ref *ref = e->ref;
    6603          701 :       while (ref)
    6604              :         {
    6605          542 :           switch (ref->type)
    6606              :             {
    6607          237 :             case REF_COMPONENT:
    6608          237 :               ref->u.c.sym = sym->ts.u.derived;
    6609              :               /* Stop the loop.  */
    6610          237 :               ref = NULL;
    6611          237 :               break;
    6612          305 :             default:
    6613          305 :               ref = ref->next;
    6614          305 :               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      1333731 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6623              :     {
    6624        11848 :       if (sym->ts.type == BT_CLASS)
    6625          245 :         gfc_fix_class_refs (e);
    6626        11848 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6627         2180 :           && 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        11845 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6638         6990 :                && (!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          141 :           gfc_ref *ref = gfc_get_ref ();
    6644          141 :           ref->type = REF_ARRAY;
    6645          141 :           ref->u.ar.type = AR_FULL;
    6646          141 :           if (sym->as)
    6647              :             {
    6648          140 :               ref->u.ar.as = sym->as;
    6649          140 :               ref->u.ar.dimen = sym->as->rank;
    6650              :             }
    6651          141 :           ref->next = e->ref;
    6652          141 :           e->ref = ref;
    6653              :         }
    6654              :     }
    6655              : 
    6656      1333728 :   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      1333728 :   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      1333728 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6673         1023 :       && CLASS_DATA (sym)
    6674         1023 :       && (CLASS_DATA (sym)->attr.dimension
    6675          449 :           || CLASS_DATA (sym)->attr.codimension)
    6676          580 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6677              :     {
    6678          555 :       gfc_ref *ref, *newref;
    6679              : 
    6680          555 :       newref = gfc_get_ref ();
    6681          555 :       newref->type = REF_ARRAY;
    6682          555 :       newref->u.ar.type = AR_FULL;
    6683          555 :       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          555 :       ref = e->ref;
    6691          555 :       if (!ref)
    6692           18 :         e->ref = newref;
    6693          537 :       else if (ref->type == REF_COMPONENT
    6694          232 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6695              :         {
    6696          232 :           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          220 :             gfc_free_ref_list (newref);
    6704              :         }
    6705          305 :       else if (ref->type == REF_ARRAY)
    6706              :         /* Array ref present already.  */
    6707          305 :         gfc_free_ref_list (newref);
    6708              :       else
    6709              :         {
    6710            0 :           newref->next = ref;
    6711            0 :           e->ref = newref;
    6712              :         }
    6713              :     }
    6714      1333173 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6715              :     {
    6716          498 :       gfc_ref *ref;
    6717          922 :       for (ref = e->ref; ref; ref = ref->next)
    6718          454 :         if (ref->type == REF_SUBSTRING)
    6719              :           break;
    6720          498 :       if (ref == NULL)
    6721          468 :         e->ts = sym->ts;
    6722              :     }
    6723              : 
    6724      1333728 :   if (e->ref && !gfc_resolve_ref (e))
    6725              :     return false;
    6726              : 
    6727      1333635 :   if (sym->attr.flavor == FL_PROCEDURE
    6728        31965 :       && (!sym->attr.function
    6729        18739 :           || (sym->attr.function && sym->result
    6730        18291 :               && sym->result->attr.proc_pointer
    6731          726 :               && !sym->result->attr.function)))
    6732              :     {
    6733        13226 :       e->ts.type = BT_PROCEDURE;
    6734        13226 :       goto resolve_procedure;
    6735              :     }
    6736              : 
    6737      1320409 :   if (sym->ts.type != BT_UNKNOWN)
    6738      1319646 :     gfc_variable_attr (e, &e->ts);
    6739          763 :   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          753 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6748              :         return false;
    6749          624 :       e->ts = sym->ts;
    6750              :     }
    6751              : 
    6752      1320280 :   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      1320261 :   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      1320261 :   if (sym->attr.flavor == FL_VARIABLE
    6822      1281996 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6823         6182 :           || !sym->ns->code->ext.block.assoc)
    6824      1279898 :       && gfc_current_ns->parent
    6825       610848 :       && (gfc_current_ns->parent == sym->ns
    6826       572175 :           || (gfc_current_ns->parent->parent
    6827        12221 :               && gfc_current_ns->parent->parent == sym->ns)))
    6828        45304 :     sym->attr.host_assoc = 1;
    6829              : 
    6830      1320261 :   if (gfc_current_ns->proc_name
    6831      1316096 :       && sym->attr.dimension
    6832       358618 :       && (sym->ns != gfc_current_ns
    6833       334744 :           || sym->attr.use_assoc
    6834       330757 :           || sym->attr.in_common))
    6835        32649 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6836              : 
    6837      1333487 : resolve_procedure:
    6838      1333487 :   if (t && !resolve_procedure_expression (e))
    6839              :     t = false;
    6840              : 
    6841              :   /* F2008, C617 and C1229.  */
    6842      1332447 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6843      1432412 :       && 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      1333487 :   if (t)
    6884      1333477 :     gfc_expression_rank (e);
    6885              : 
    6886      1333487 :   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      1333487 :   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          920 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    6912              : {
    6913          920 :   gfc_ref *ref, *new_ref;
    6914          920 :   gfc_symbol *sym, *derived;
    6915          920 :   gfc_expr *target;
    6916          920 :   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          920 :   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          602 :   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          602 :   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          865 :   for (ref = e->ref; ref; ref = ref->next)
    7018          829 :     if (ref->type == REF_COMPONENT)
    7019              :       {
    7020          566 :         if (ref->u.c.component->name[0] != '_')
    7021          370 :           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 inference 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          602 :   target = e->symtree->n.sym->assoc->target;
    7031          602 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    7032          190 :       && e != target && !target->rank)
    7033              :     {
    7034              :       /* First case: array ref after the scalar class or derived
    7035              :          associate_name.  */
    7036          190 :       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          183 :       else if (e->ref && e->ref->type == REF_COMPONENT
    7059          183 :                && 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          602 :   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      1680076 : check_host_association (gfc_expr *e)
    7087              : {
    7088      1680076 :   gfc_symbol *sym, *old_sym;
    7089      1680076 :   gfc_symtree *st;
    7090      1680076 :   int n;
    7091      1680076 :   gfc_ref *ref;
    7092      1680076 :   gfc_actual_arglist *arg, *tail = NULL;
    7093      1680076 :   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      1680076 :   if (e->symtree == NULL
    7099      1679275 :         || e->symtree->n.sym == NULL
    7100      1679275 :         || e->user_operator)
    7101              :     return retval;
    7102              : 
    7103      1677498 :   old_sym = e->symtree->n.sym;
    7104              : 
    7105      1677498 :   if (gfc_current_ns->parent
    7106       736950 :         && old_sym->ns != gfc_current_ns)
    7107              :     {
    7108              :       /* Use the 'USE' name so that renamed module symbols are
    7109              :          correctly handled.  */
    7110        92215 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    7111              : 
    7112        92215 :       if (sym && old_sym != sym
    7113          702 :               && 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        92132 :       else if (sym && old_sym != sym
    7191          619 :                && !e->ref
    7192          347 :                && 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      1677481 :   return e->expr_type == EXPR_FUNCTION;
    7211              : }
    7212              : 
    7213              : 
    7214              : static void
    7215         1454 : gfc_resolve_character_operator (gfc_expr *e)
    7216              : {
    7217         1454 :   gfc_expr *op1 = e->value.op.op1;
    7218         1454 :   gfc_expr *op2 = e->value.op.op2;
    7219         1454 :   gfc_expr *e1 = NULL;
    7220         1454 :   gfc_expr *e2 = NULL;
    7221              : 
    7222         1454 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    7223              : 
    7224         1454 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    7225          767 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    7226          687 :   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         1454 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    7231          755 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    7232          699 :   else if (op2->expr_type == EXPR_CONSTANT)
    7233          468 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7234          468 :                            op2->value.character.length);
    7235              : 
    7236         1454 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7237              : 
    7238         1454 :   if (!e1 || !e2)
    7239              :     {
    7240          547 :       gfc_free_expr (e1);
    7241          547 :       gfc_free_expr (e2);
    7242              : 
    7243          547 :       return;
    7244              :     }
    7245              : 
    7246          907 :   e->ts.u.cl->length = gfc_add (e1, e2);
    7247          907 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    7248          907 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    7249          907 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    7250          907 :   gfc_resolve_expr (e->ts.u.cl->length);
    7251              : 
    7252          907 :   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       184085 : 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       184085 :   switch (e->expr_type)
    7266              :     {
    7267         1454 :     case EXPR_OP:
    7268         1454 :       gfc_resolve_character_operator (e);
    7269              :       /* FALLTHRU */
    7270              : 
    7271         1521 :     case EXPR_ARRAY:
    7272         1521 :       if (e->expr_type == EXPR_ARRAY)
    7273           67 :         gfc_resolve_character_array_constructor (e);
    7274              :       /* FALLTHRU */
    7275              : 
    7276         1978 :     case EXPR_SUBSTRING:
    7277         1978 :       if (!e->ts.u.cl && e->ref)
    7278          453 :         gfc_resolve_substring_charlen (e);
    7279              :       /* FALLTHRU */
    7280              : 
    7281       184085 :     default:
    7282       184085 :       if (!e->ts.u.cl)
    7283       182111 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7284              : 
    7285       184085 :       break;
    7286              :     }
    7287       184085 : }
    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         3038 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    7295              :                      const char *name)
    7296              : {
    7297         3062 :   gcc_assert (argpos > 0);
    7298              : 
    7299         3062 :   if (argpos == 1)
    7300              :     {
    7301         2913 :       gfc_actual_arglist* result;
    7302              : 
    7303         2913 :       result = gfc_get_actual_arglist ();
    7304         2913 :       result->expr = po;
    7305         2913 :       result->next = lst;
    7306         2913 :       if (name)
    7307          514 :         result->name = name;
    7308              : 
    7309         2913 :       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         7395 : extract_compcall_passed_object (gfc_expr* e)
    7324              : {
    7325         7395 :   gfc_expr* po;
    7326              : 
    7327         7395 :   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         7395 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7335              : 
    7336         7395 :   if (e->value.compcall.base_object)
    7337         1656 :     po = gfc_copy_expr (e->value.compcall.base_object);
    7338              :   else
    7339              :     {
    7340         5739 :       po = gfc_get_expr ();
    7341         5739 :       po->expr_type = EXPR_VARIABLE;
    7342         5739 :       po->symtree = e->symtree;
    7343         5739 :       po->ref = gfc_copy_ref (e->ref);
    7344         5739 :       po->where = e->where;
    7345              :     }
    7346              : 
    7347         7395 :   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         3402 : update_compcall_arglist (gfc_expr* e)
    7359              : {
    7360         3402 :   gfc_expr* po;
    7361         3402 :   gfc_typebound_proc* tbp;
    7362              : 
    7363         3402 :   tbp = e->value.compcall.tbp;
    7364              : 
    7365         3402 :   if (tbp->error)
    7366              :     return false;
    7367              : 
    7368         3401 :   po = extract_compcall_passed_object (e);
    7369         3401 :   if (!po)
    7370              :     return false;
    7371              : 
    7372         3401 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    7373              :     {
    7374         1152 :       gfc_free_expr (po);
    7375         1152 :       return true;
    7376              :     }
    7377              : 
    7378         2249 :   if (tbp->pass_arg_num <= 0)
    7379              :     return false;
    7380              : 
    7381         2248 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7382              :                                                   tbp->pass_arg_num,
    7383              :                                                   tbp->pass_arg);
    7384              : 
    7385         2248 :   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         3413 : check_typebound_baseobject (gfc_expr* e)
    7471              : {
    7472         3413 :   gfc_expr* base;
    7473         3413 :   bool return_value = false;
    7474              : 
    7475         3413 :   base = extract_compcall_passed_object (e);
    7476         3413 :   if (!base)
    7477              :     return false;
    7478              : 
    7479         3410 :   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         3409 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7486            1 :     return false;
    7487              : 
    7488              :   /* F08:C611.  */
    7489         3408 :   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         3405 :   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         3409 : cleanup:
    7508         3409 :   gfc_free_expr (base);
    7509         3409 :   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         3402 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7519              :                           gfc_actual_arglist** actual)
    7520              : {
    7521         3402 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7522         3402 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7523              : 
    7524              :   /* Update the actual arglist for PASS.  */
    7525         3402 :   if (!update_compcall_arglist (e))
    7526              :     return false;
    7527              : 
    7528         3400 :   *actual = e->value.compcall.actual;
    7529         3400 :   *target = e->value.compcall.tbp->u.specific;
    7530              : 
    7531         3400 :   gfc_free_ref_list (e->ref);
    7532         3400 :   e->ref = NULL;
    7533         3400 :   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         3400 :   if (e->value.compcall.name
    7538         3400 :       && !e->value.compcall.tbp->non_overridable
    7539         3382 :       && e->value.compcall.base_object
    7540          828 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7541              :     {
    7542          535 :       gfc_symtree *st;
    7543          535 :       gfc_symbol *derived;
    7544              : 
    7545              :       /* Use the derived type of the base_object.  */
    7546          535 :       derived = e->value.compcall.base_object->ts.u.derived;
    7547          535 :       st = NULL;
    7548              : 
    7549              :       /* If necessary, go through the inheritance chain.  */
    7550         1613 :       while (!st && derived)
    7551              :         {
    7552              :           /* Look for the typebound procedure 'name'.  */
    7553          543 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7554          535 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7555              :                                    e->value.compcall.name);
    7556          543 :           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          535 :       if (st && st->n.tb && st->n.tb->u.specific)
    7562          535 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7563          535 :                            derived->ns, 1, &st);
    7564          535 :       if (st)
    7565          535 :         *target = st;
    7566              :     }
    7567              : 
    7568         3400 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7569         3400 :       && !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         3321 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7584              :                         gfc_expr *e, bool check_types)
    7585              : {
    7586         3321 :   gfc_symbol *declared;
    7587         3321 :   gfc_ref *ref;
    7588              : 
    7589         3321 :   declared = NULL;
    7590         3321 :   if (class_ref)
    7591         2888 :     *class_ref = NULL;
    7592         3321 :   if (new_ref)
    7593         2595 :     *new_ref = gfc_copy_ref (e->ref);
    7594              : 
    7595         4116 :   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         3321 :   if (declared == NULL)
    7611         2993 :     declared = e->symtree->n.sym->ts.u.derived;
    7612              : 
    7613         3321 :   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         3404 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7623              : {
    7624         3404 :   gfc_typebound_proc* genproc;
    7625         3404 :   const char* genname;
    7626         3404 :   gfc_symtree *st;
    7627         3404 :   gfc_symbol *derived;
    7628              : 
    7629         3404 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7630         3404 :   genname = e->value.compcall.name;
    7631         3404 :   genproc = e->value.compcall.tbp;
    7632              : 
    7633         3404 :   if (!genproc->is_generic)
    7634              :     return true;
    7635              : 
    7636              :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7637          445 :   for (; genproc; genproc = genproc->overridden)
    7638              :     {
    7639          443 :       gfc_tbp_generic* g;
    7640              : 
    7641          443 :       gcc_assert (genproc->is_generic);
    7642          677 :       for (g = genproc->u.generic; g; g = g->next)
    7643              :         {
    7644          667 :           gfc_symbol* target;
    7645          667 :           gfc_actual_arglist* args;
    7646          667 :           bool matches;
    7647              : 
    7648          667 :           gcc_assert (g->specific);
    7649              : 
    7650          667 :           if (g->specific->error)
    7651            0 :             continue;
    7652              : 
    7653          667 :           target = g->specific->u.specific->n.sym;
    7654              : 
    7655              :           /* Get the right arglist by handling PASS/NOPASS.  */
    7656          667 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7657          667 :           if (!g->specific->nopass)
    7658              :             {
    7659          581 :               gfc_expr* po;
    7660          581 :               po = extract_compcall_passed_object (e);
    7661          581 :               if (!po)
    7662              :                 {
    7663            0 :                   gfc_free_actual_arglist (args);
    7664            0 :                   return false;
    7665              :                 }
    7666              : 
    7667          581 :               gcc_assert (g->specific->pass_arg_num > 0);
    7668          581 :               gcc_assert (!g->specific->error);
    7669          581 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7670              :                                           g->specific->pass_arg);
    7671              :             }
    7672          667 :           resolve_actual_arglist (args, target->attr.proc,
    7673          667 :                                   is_external_proc (target)
    7674          667 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7675              : 
    7676              :           /* Check if this arglist matches the formal.  */
    7677          667 :           matches = gfc_arglist_matches_symbol (&args, target);
    7678              : 
    7679              :           /* Clean up and break out of the loop if we've found it.  */
    7680          667 :           gfc_free_actual_arglist (args);
    7681          667 :           if (matches)
    7682              :             {
    7683          433 :               e->value.compcall.tbp = g->specific;
    7684          433 :               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          433 :               if (name)
    7688          161 :                 *name = genname;
    7689          433 :               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          433 : success:
    7700              :   /* Make sure that we have the right specific instance for the name.  */
    7701          433 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7702              : 
    7703          433 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7704          433 :   if (st)
    7705          433 :     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         1756 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7715              : {
    7716         1756 :   gfc_actual_arglist* newactual;
    7717         1756 :   gfc_symtree* target;
    7718              : 
    7719              :   /* Check that's really a SUBROUTINE.  */
    7720         1756 :   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         1751 :   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         1744 :   if (name)
    7741          480 :     *name = c->expr1->value.compcall.name;
    7742              : 
    7743         1744 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7744              :     return false;
    7745              : 
    7746              :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7747         1743 :   if (overridable)
    7748          371 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7749              : 
    7750              :   /* Transform into an ordinary EXEC_CALL for now.  */
    7751              : 
    7752         1743 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7753              :     return false;
    7754              : 
    7755         1741 :   c->ext.actual = newactual;
    7756         1741 :   c->symtree = target;
    7757         1741 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7758              : 
    7759         1741 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    7760              : 
    7761         1741 :   gfc_free_expr (c->expr1);
    7762         1741 :   c->expr1 = gfc_get_expr ();
    7763         1741 :   c->expr1->expr_type = EXPR_FUNCTION;
    7764         1741 :   c->expr1->symtree = target;
    7765         1741 :   c->expr1->where = c->loc;
    7766              : 
    7767         1741 :   return resolve_call (c);
    7768              : }
    7769              : 
    7770              : 
    7771              : /* Resolve a component-call expression.  */
    7772              : static bool
    7773         1669 : resolve_compcall (gfc_expr* e, const char **name)
    7774              : {
    7775         1669 :   gfc_actual_arglist* newactual;
    7776         1669 :   gfc_symtree* target;
    7777              : 
    7778              :   /* Check that's really a FUNCTION.  */
    7779         1669 :   if (!e->value.compcall.tbp->function)
    7780              :     {
    7781            7 :       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            7 :       return false;
    7785              :     }
    7786              : 
    7787              : 
    7788              :   /* These must not be assign-calls!  */
    7789         1662 :   gcc_assert (!e->value.compcall.assign);
    7790              : 
    7791         1662 :   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         1660 :   if (name)
    7797          864 :     *name = e->value.compcall.name;
    7798              : 
    7799         1660 :   if (!resolve_typebound_generic_call (e, name))
    7800              :     return false;
    7801         1659 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7802              : 
    7803              :   /* Take the rank from the function's symbol.  */
    7804         1659 :   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         1659 :   if (!resolve_typebound_static (e, &target, &newactual))
    7814              :     return false;
    7815              : 
    7816         1659 :   e->value.function.actual = newactual;
    7817         1659 :   e->value.function.name = NULL;
    7818         1659 :   e->value.function.esym = target->n.sym;
    7819         1659 :   e->value.function.isym = NULL;
    7820         1659 :   e->symtree = target;
    7821         1659 :   e->ts = target->n.sym->ts;
    7822         1659 :   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         1659 :   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         1669 : resolve_typebound_function (gfc_expr* e)
    7839              : {
    7840         1669 :   gfc_symbol *declared;
    7841         1669 :   gfc_component *c;
    7842         1669 :   gfc_ref *new_ref;
    7843         1669 :   gfc_ref *class_ref;
    7844         1669 :   gfc_symtree *st;
    7845         1669 :   const char *name;
    7846         1669 :   gfc_typespec ts;
    7847         1669 :   gfc_expr *expr;
    7848         1669 :   bool overridable;
    7849              : 
    7850         1669 :   st = e->symtree;
    7851              : 
    7852              :   /* Deal with typebound operators for CLASS objects.  */
    7853         1669 :   expr = e->value.compcall.base_object;
    7854         1669 :   overridable = !e->value.compcall.tbp->non_overridable;
    7855         1669 :   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         1485 :   if (st == NULL)
    7900          195 :     return resolve_compcall (e, NULL);
    7901              : 
    7902         1290 :   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         1290 :   if (!expr && overridable
    7909         1282 :       && e->value.compcall.tbp->is_generic
    7910          198 :       && e->value.compcall.tbp->u.generic->specific
    7911          197 :       && 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         1288 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    7923              : 
    7924         1288 :   if (!resolve_fl_derived (declared))
    7925              :     return false;
    7926              : 
    7927              :   /* Weed out cases of the ultimate component being a derived type.  */
    7928         1288 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7929         1194 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7930              :     {
    7931          608 :       gfc_free_ref_list (new_ref);
    7932          608 :       return resolve_compcall (e, NULL);
    7933              :     }
    7934              : 
    7935          680 :   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          680 :   if (!resolve_compcall (e, &name))
    7940              :     {
    7941            3 :       gfc_free_ref_list (new_ref);
    7942            3 :       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         1756 : resolve_typebound_subroutine (gfc_code *code)
    7977              : {
    7978         1756 :   gfc_symbol *declared;
    7979         1756 :   gfc_component *c;
    7980         1756 :   gfc_ref *new_ref;
    7981         1756 :   gfc_ref *class_ref;
    7982         1756 :   gfc_symtree *st;
    7983         1756 :   const char *name;
    7984         1756 :   gfc_typespec ts;
    7985         1756 :   gfc_expr *expr;
    7986         1756 :   bool overridable;
    7987              : 
    7988         1756 :   st = code->expr1->symtree;
    7989              : 
    7990              :   /* Deal with typebound operators for CLASS objects.  */
    7991         1756 :   expr = code->expr1->value.compcall.base_object;
    7992         1756 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    7993         1756 :   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         1647 :   if (st == NULL)
    8049          340 :     return resolve_typebound_call (code, NULL, NULL);
    8050              : 
    8051         1307 :   if (!gfc_resolve_ref (code->expr1))
    8052              :     return false;
    8053              : 
    8054              :   /* Get the CLASS declared type.  */
    8055         1307 :   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         1307 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8059         1242 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8060              :     {
    8061          931 :       gfc_free_ref_list (new_ref);
    8062          931 :       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        12074 : gfc_is_expandable_expr (gfc_expr *e)
    8182              : {
    8183        12074 :   gfc_constructor *con;
    8184              : 
    8185        12074 :   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        12074 :       con = gfc_constructor_first (e->value.constructor);
    8191        31949 :       for (; con; con = gfc_constructor_next (con))
    8192              :         {
    8193        13991 :           if (con->expr->expr_type == EXPR_VARIABLE
    8194         5411 :               && con->expr->symtree
    8195         5411 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    8196         5329 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    8197              :             return true;
    8198         8580 :           if (con->expr->expr_type == EXPR_ARRAY
    8199         8580 :               && 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         4895 : fixup_unique_dummy (gfc_expr *e)
    8215              : {
    8216         4895 :   gfc_symtree *st = NULL;
    8217         4895 :   gfc_symbol *s = NULL;
    8218              : 
    8219         4895 :   if (e->symtree->n.sym->ns->proc_name
    8220         4865 :       && e->symtree->n.sym->ns->proc_name->formal)
    8221         4865 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    8222              : 
    8223         4865 :   if (s != NULL)
    8224         4865 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    8225              : 
    8226         4895 :   if (st != NULL
    8227           14 :       && st->n.sym != NULL
    8228           14 :       && st->n.sym->attr.dummy)
    8229           14 :     e->symtree = st;
    8230         4895 : }
    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      7199196 : gfc_resolve_expr (gfc_expr *e)
    8239              : {
    8240      7199196 :   bool t;
    8241      7199196 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    8242              : 
    8243      7199196 :   if (e == NULL || e->do_not_resolve_again)
    8244              :     return true;
    8245              : 
    8246              :   /* inquiry_argument only applies to variables.  */
    8247      5268638 :   inquiry_save = inquiry_argument;
    8248      5268638 :   actual_arg_save = actual_arg;
    8249      5268638 :   first_actual_arg_save = first_actual_arg;
    8250              : 
    8251      5268638 :   if (e->expr_type != EXPR_VARIABLE)
    8252              :     {
    8253      3934236 :       inquiry_argument = false;
    8254      3934236 :       actual_arg = false;
    8255      3934236 :       first_actual_arg = false;
    8256              :     }
    8257      1334402 :   else if (e->symtree != NULL
    8258      1333957 :            && *e->symtree->name == '@'
    8259         5625 :            && 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         4895 :       fixup_unique_dummy (e);
    8264              :     }
    8265              : 
    8266      5268638 :   switch (e->expr_type)
    8267              :     {
    8268       534407 :     case EXPR_OP:
    8269       534407 :       t = resolve_operator (e);
    8270       534407 :       break;
    8271              : 
    8272          162 :     case EXPR_CONDITIONAL:
    8273          162 :       t = resolve_conditional (e);
    8274          162 :       break;
    8275              : 
    8276      1680076 :     case EXPR_FUNCTION:
    8277      1680076 :     case EXPR_VARIABLE:
    8278              : 
    8279      1680076 :       if (check_host_association (e))
    8280       345710 :         t = resolve_function (e);
    8281              :       else
    8282      1334366 :         t = resolve_variable (e);
    8283              : 
    8284      1680076 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
    8285         7332 :           && e->ref->type != REF_SUBSTRING)
    8286         2162 :         gfc_resolve_substring_charlen (e);
    8287              : 
    8288              :       break;
    8289              : 
    8290         1669 :     case EXPR_COMPCALL:
    8291         1669 :       t = resolve_typebound_function (e);
    8292         1669 :       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        72637 :     case EXPR_ARRAY:
    8308        72637 :       t = false;
    8309        72637 :       if (!gfc_resolve_ref (e))
    8310              :         break;
    8311              : 
    8312        72637 :       t = gfc_resolve_array_constructor (e);
    8313              :       /* Also try to expand a constructor.  */
    8314        72637 :       if (t)
    8315              :         {
    8316        72535 :           gfc_expression_rank (e);
    8317        72535 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    8318        67897 :             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        72535 :       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        10762 :           gfc_expand_constructor (e, false);
    8329        10762 :           t = gfc_resolve_character_array_constructor (e);
    8330              :         }
    8331              : 
    8332              :       break;
    8333              : 
    8334        16633 :     case EXPR_STRUCTURE:
    8335        16633 :       t = gfc_resolve_ref (e);
    8336        16633 :       if (!t)
    8337              :         break;
    8338              : 
    8339        16633 :       t = resolve_structure_cons (e, 0);
    8340        16633 :       if (!t)
    8341              :         break;
    8342              : 
    8343        16621 :       t = gfc_simplify_expr (e, 0);
    8344        16621 :       break;
    8345              : 
    8346            0 :     default:
    8347            0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    8348              :     }
    8349              : 
    8350      5268638 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    8351       184085 :     fixup_charlen (e);
    8352              : 
    8353      5268638 :   inquiry_argument = inquiry_save;
    8354      5268638 :   actual_arg = actual_arg_save;
    8355      5268638 :   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      5268638 :   if (t && e->expr_type == EXPR_VARIABLE
    8360      1331520 :       && 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      5266107 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
    8365         7300 :     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       153513 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    8376              :                            const char *name_msgid)
    8377              : {
    8378       153513 :   if (!gfc_resolve_expr (expr))
    8379              :     return false;
    8380              : 
    8381       153508 :   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       153508 :   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        38387 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8419              : {
    8420        38387 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8421              :     return false;
    8422              : 
    8423        38383 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8424        38383 :                                  _("iterator variable")))
    8425              :     return false;
    8426              : 
    8427        38377 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8428              :                                   "Start expression in DO loop"))
    8429              :     return false;
    8430              : 
    8431        38376 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8432              :                                   "End expression in DO loop"))
    8433              :     return false;
    8434              : 
    8435        38373 :   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        38372 :   if (iter->start->ts.kind != iter->var->ts.kind
    8441        38092 :       || iter->start->ts.type != iter->var->ts.type)
    8442          315 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8443              : 
    8444        38372 :   if (iter->end->ts.kind != iter->var->ts.kind
    8445        38119 :       || iter->end->ts.type != iter->var->ts.type)
    8446          278 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8447              : 
    8448        38372 :   if (iter->step->ts.kind != iter->var->ts.kind
    8449        38128 :       || iter->step->ts.type != iter->var->ts.type)
    8450          280 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8451              : 
    8452        38372 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8453              :     {
    8454        37249 :       if ((iter->step->ts.type == BT_INTEGER
    8455        37166 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8456        74413 :           || (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        38369 :   if (iter->start->expr_type == EXPR_CONSTANT
    8466        35236 :       && iter->end->expr_type == EXPR_CONSTANT
    8467        27569 :       && iter->step->expr_type == EXPR_CONSTANT)
    8468              :     {
    8469        27302 :       int sgn, cmp;
    8470        27302 :       if (iter->start->ts.type == BT_INTEGER)
    8471              :         {
    8472        27248 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8473        27248 :           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        27302 :       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        38369 :   if (iter->end->expr_type == EXPR_CONSTANT
    8487        27937 :       && iter->end->ts.type == BT_INTEGER
    8488        27883 :       && iter->step->expr_type == EXPR_CONSTANT
    8489        27573 :       && iter->step->ts.type == BT_INTEGER
    8490        27573 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8491        27202 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8492              :     {
    8493        26416 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8494        26416 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8495              : 
    8496        26416 :       if (is_step_positive
    8497        26045 :           && 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        38369 :   gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8510              :                           VALUE_USED);
    8511        38369 :   gfc_value_used_expr (iter->start, VALUE_USED);
    8512        38369 :   gfc_value_used_expr (iter->end, VALUE_USED);
    8513        38369 :   gfc_value_used_expr (iter->step, VALUE_USED);
    8514              : 
    8515        38369 :   return true;
    8516              : }
    8517              : 
    8518              : 
    8519              : /* Traversal function for find_forall_index.  f == 2 signals that
    8520              :    that variable itself is not to be checked - only the references.  */
    8521              : 
    8522              : static bool
    8523        42682 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8524              : {
    8525        42682 :   if (expr->expr_type != EXPR_VARIABLE)
    8526              :     return false;
    8527              : 
    8528              :   /* A scalar assignment  */
    8529        18195 :   if (!expr->ref || *f == 1)
    8530              :     {
    8531        12133 :       if (expr->symtree->n.sym == sym)
    8532              :         return true;
    8533              :       else
    8534              :         return false;
    8535              :     }
    8536              : 
    8537         6062 :   if (*f == 2)
    8538         1731 :     *f = 1;
    8539              :   return false;
    8540              : }
    8541              : 
    8542              : 
    8543              : /* Check whether the FORALL index appears in the expression or not.
    8544              :    Returns true if SYM is found in EXPR.  */
    8545              : 
    8546              : bool
    8547        27060 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8548              : {
    8549        27060 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8550              :     return true;
    8551              :   else
    8552              :     return false;
    8553              : }
    8554              : 
    8555              : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8556              :    This constraint specifies rules for variables in locality-specs.  */
    8557              : 
    8558              : static int
    8559          765 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8560              : {
    8561          765 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8562              : 
    8563          765 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8564              :     {
    8565           22 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8566           22 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8567           24 :            list; list = list->next)
    8568              :         {
    8569            5 :           if (list->expr->symtree->n.sym == sym)
    8570              :             {
    8571            3 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8572              :                          "must not appear in LOCAL locality-spec at %L",
    8573              :                          sym->name, &(*expr)->where, &list->expr->where);
    8574            3 :               *walk_subtrees = 0;
    8575            3 :               return 1;
    8576              :             }
    8577              :         }
    8578              :     }
    8579              : 
    8580          762 :     *walk_subtrees = 1;
    8581          762 :     return 0;
    8582              : }
    8583              : 
    8584              : static int
    8585         4058 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8586              : {
    8587         4058 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8588              : 
    8589         4058 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8590              :     {
    8591         1866 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8592              : 
    8593         1866 :       if (d->sym_hash->contains (sym))
    8594         1275 :         sym->mark = 1;
    8595              : 
    8596          591 :       else if (d->default_none)
    8597              :         {
    8598            8 :           gfc_namespace *ns2 = d->ns;
    8599           13 :           while (ns2)
    8600              :             {
    8601            8 :               if (ns2 == sym->ns)
    8602              :                 break;
    8603            5 :               ns2 = ns2->parent;
    8604              :             }
    8605              : 
    8606              :           /* A DO CONCURRENT iterator cannot appear in a locality spec.
    8607              :              Use d->code (the DO CONCURRENT node) rather than sym->ns->code,
    8608              :              which may be a different code type (e.g. EXEC_ASSOCIATE) whose
    8609              :              ext union would be read incorrectly.  */
    8610            8 :           for (gfc_forall_iterator *iter = d->code->ext.concur.forall_iterator;
    8611           17 :                iter; iter = iter->next)
    8612              :             {
    8613           10 :               if (!iter->var || !iter->var->symtree)
    8614            0 :                 continue;
    8615           10 :               const char *iter_name = iter->var->symtree->name;
    8616              :               /* Shadow iterators (from inline type-spec: integer :: i = ...)
    8617              :                  store the iterator with a leading underscore internally; the
    8618              :                  user-visible name does not have the underscore.  */
    8619           10 :               if (iter->shadow)
    8620            0 :                 iter_name++;
    8621           10 :               if (strcmp (sym->name, iter_name) == 0)
    8622            1 :                 return 0;
    8623              :             }
    8624              : 
    8625              :           /* A named constant is not a variable, so skip test.  */
    8626            7 :           if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
    8627              :             {
    8628            2 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8629              :                         "of DO CONCURRENT at %L but required due to "
    8630              :                         "DEFAULT (NONE)",
    8631              :                         sym->name, &(*e)->where, &d->code->loc);
    8632            2 :               d->sym_hash->add (sym);
    8633              :             }
    8634              :         }
    8635              :     }
    8636              :   return 0;
    8637              : }
    8638              : 
    8639              : static void
    8640          224 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8641              : {
    8642          224 :   struct check_default_none_data data;
    8643          224 :   data.code = code;
    8644          224 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8645          224 :   data.ns = ns;
    8646          224 :   data.default_none = code->ext.concur.default_none;
    8647              : 
    8648         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8649              :     {
    8650          896 :       const char *name;
    8651          896 :       switch (locality)
    8652              :         {
    8653              :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8654          224 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8655          224 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8656          224 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8657              :           default: gcc_unreachable ();
    8658              :         }
    8659              : 
    8660         1287 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8661          391 :            list = list->next)
    8662              :         {
    8663          391 :           gfc_expr *expr = list->expr;
    8664              : 
    8665          391 :           if (locality == LOCALITY_REDUCE
    8666           72 :               && (expr->expr_type == EXPR_FUNCTION
    8667           48 :                   || expr->expr_type == EXPR_OP))
    8668           35 :             continue;
    8669              : 
    8670          367 :           if (!gfc_resolve_expr (expr))
    8671            3 :             continue;
    8672              : 
    8673          364 :           if (expr->expr_type != EXPR_VARIABLE
    8674          364 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8675          364 :               || (expr->ref
    8676          151 :                   && (expr->ref->type != REF_ARRAY
    8677          151 :                       || expr->ref->u.ar.type != AR_FULL
    8678          147 :                       || expr->ref->next)))
    8679              :             {
    8680            4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8681              :                          name, &expr->where);
    8682            4 :                 continue;
    8683              :             }
    8684              : 
    8685          360 :           gfc_symbol *sym = expr->symtree->n.sym;
    8686              : 
    8687          360 :           if (data.sym_hash->contains (sym))
    8688              :             {
    8689            4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8690              :                          "locality-spec", sym->name, &expr->where);
    8691            4 :               continue;
    8692              :             }
    8693              : 
    8694          356 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8695          716 :                iter; iter = iter->next)
    8696              :             {
    8697          360 :               if (iter->var->symtree->n.sym == sym)
    8698              :                 {
    8699            1 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8700              :                              "locality-spec", sym->name, &expr->where);
    8701            1 :                   continue;
    8702              :                 }
    8703              : 
    8704          359 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8705              :             }
    8706              : 
    8707          356 :           if (locality == LOCALITY_LOCAL
    8708          356 :               || locality == LOCALITY_LOCAL_INIT
    8709          356 :               || locality == LOCALITY_REDUCE)
    8710              :             {
    8711          198 :               if (sym->attr.optional)
    8712            3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    8713              :                            "locality-spec at %L",
    8714              :                            sym->name, name, &expr->where);
    8715              : 
    8716          198 :               if (sym->attr.dimension
    8717           66 :                   && sym->as
    8718           66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    8719            0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    8720              :                            "locality-spec at %L",
    8721              :                            sym->name, name, &expr->where);
    8722              : 
    8723          198 :               gfc_check_vardef_context (expr, false, false, false, name);
    8724              :             }
    8725              : 
    8726          198 :           if (locality == LOCALITY_LOCAL
    8727              :               || locality == LOCALITY_LOCAL_INIT)
    8728              :             {
    8729          181 :               symbol_attribute attr = gfc_expr_attr (expr);
    8730              : 
    8731          181 :               if (attr.allocatable)
    8732            2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    8733              :                            "locality-spec at %L",
    8734              :                            sym->name, name, &expr->where);
    8735              : 
    8736          179 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    8737            2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    8738              :                            " for %qs in %s locality-spec at %L",
    8739              :                            sym->name, name, &expr->where);
    8740              : 
    8741          177 :               else if (attr.codimension)
    8742            0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    8743              :                            "at %L",
    8744              :                            sym->name, name, &expr->where);
    8745              : 
    8746          177 :               else if (expr->ts.type == BT_DERIVED
    8747          177 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    8748            0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    8749              :                            "locality-spec at %L",
    8750              :                            sym->name, name, &expr->where);
    8751              : 
    8752          177 :               else if (gfc_has_ultimate_allocatable (expr))
    8753            4 :                 gfc_error ("Type with ultimate allocatable component not "
    8754              :                            "permitted for %qs in %s locality-spec at %L",
    8755              :                            sym->name, name, &expr->where);
    8756              :             }
    8757              : 
    8758          175 :           else if (locality == LOCALITY_REDUCE)
    8759              :             {
    8760           17 :               if (sym->attr.asynchronous)
    8761            1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    8762              :                            "REDUCE locality-spec at %L",
    8763              :                            sym->name, &expr->where);
    8764           17 :               if (sym->attr.volatile_)
    8765            1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    8766              :                            "locality-spec at %L", sym->name, &expr->where);
    8767              :             }
    8768              : 
    8769          356 :           data.sym_hash->add (sym);
    8770              :         }
    8771              : 
    8772          896 :       if (locality == LOCALITY_LOCAL)
    8773              :         {
    8774          224 :           gcc_assert (locality == 0);
    8775              : 
    8776          224 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8777          467 :                iter; iter = iter->next)
    8778              :             {
    8779          243 :               gfc_expr_walker (&iter->start,
    8780              :                                do_concur_locality_specs_f2023,
    8781              :                                &data);
    8782              : 
    8783          243 :               gfc_expr_walker (&iter->end,
    8784              :                                do_concur_locality_specs_f2023,
    8785              :                                &data);
    8786              : 
    8787          243 :               gfc_expr_walker (&iter->stride,
    8788              :                                do_concur_locality_specs_f2023,
    8789              :                                &data);
    8790              :             }
    8791              : 
    8792          224 :           if (code->expr1)
    8793            7 :             gfc_expr_walker (&code->expr1,
    8794              :                              do_concur_locality_specs_f2023,
    8795              :                              &data);
    8796              :         }
    8797              :     }
    8798              : 
    8799          224 :   gfc_expr *reduce_op = NULL;
    8800              : 
    8801          224 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    8802          272 :        list; list = list->next)
    8803              :     {
    8804           48 :       gfc_expr *expr = list->expr;
    8805              : 
    8806           48 :       if (expr->expr_type != EXPR_VARIABLE)
    8807              :         {
    8808           24 :           reduce_op = expr;
    8809           24 :           continue;
    8810              :         }
    8811              : 
    8812           24 :       if (reduce_op->expr_type == EXPR_OP)
    8813              :         {
    8814           17 :           switch (reduce_op->value.op.op)
    8815              :             {
    8816           17 :               case INTRINSIC_PLUS:
    8817           17 :               case INTRINSIC_TIMES:
    8818           17 :                 if (!gfc_numeric_ts (&expr->ts))
    8819            3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    8820            3 :                              "got %s", expr->symtree->n.sym->name,
    8821              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8822              :                 break;
    8823            0 :               case INTRINSIC_AND:
    8824            0 :               case INTRINSIC_OR:
    8825            0 :               case INTRINSIC_EQV:
    8826            0 :               case INTRINSIC_NEQV:
    8827            0 :                 if (expr->ts.type != BT_LOGICAL)
    8828            0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    8829            0 :                              "got %qs", expr->symtree->n.sym->name,
    8830              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8831              :                 break;
    8832            0 :               default:
    8833            0 :                 gcc_unreachable ();
    8834              :             }
    8835              :         }
    8836              : 
    8837            7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    8838              :         {
    8839            7 :           switch (reduce_op->value.function.isym->id)
    8840              :             {
    8841            6 :               case GFC_ISYM_MIN:
    8842            6 :               case GFC_ISYM_MAX:
    8843            6 :                 if (expr->ts.type != BT_INTEGER
    8844              :                     && expr->ts.type != BT_REAL
    8845              :                     && expr->ts.type != BT_CHARACTER)
    8846            2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    8847              :                              "in REDUCE with MIN/MAX at %L, got %s",
    8848            2 :                              expr->symtree->n.sym->name, &expr->where,
    8849              :                              gfc_basic_typename (expr->ts.type));
    8850              :                 break;
    8851            1 :               case GFC_ISYM_IAND:
    8852            1 :               case GFC_ISYM_IOR:
    8853            1 :               case GFC_ISYM_IEOR:
    8854            1 :                 if (expr->ts.type != BT_INTEGER)
    8855            1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    8856              :                              "IAND/IOR/IEOR at %L, got %s",
    8857            1 :                              expr->symtree->n.sym->name, &expr->where,
    8858              :                              gfc_basic_typename (expr->ts.type));
    8859              :                 break;
    8860            0 :               default:
    8861            0 :                 gcc_unreachable ();
    8862              :             }
    8863              :         }
    8864              : 
    8865              :       else
    8866            0 :         gcc_unreachable ();
    8867              :     }
    8868              : 
    8869         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8870              :     {
    8871         1287 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8872          391 :            list = list->next)
    8873              :         {
    8874          391 :           if (list->expr->expr_type == EXPR_VARIABLE)
    8875          367 :             list->expr->symtree->n.sym->mark = 0;
    8876              :         }
    8877              :     }
    8878              : 
    8879          224 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    8880              :                    check_default_none_expr, &data);
    8881              : 
    8882         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8883              :     {
    8884          896 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    8885         1287 :       while (*plist)
    8886              :         {
    8887          391 :           gfc_expr *expr = (*plist)->expr;
    8888          391 :           if (expr->expr_type == EXPR_VARIABLE)
    8889              :             {
    8890          367 :               gfc_symbol *sym = expr->symtree->n.sym;
    8891          367 :               if (sym->mark == 0)
    8892              :                 {
    8893           70 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    8894              :                                "locality-spec at %L is not used",
    8895              :                                sym->name, &expr->where);
    8896           70 :                   gfc_expr_list *tmp = *plist;
    8897           70 :                   *plist = (*plist)->next;
    8898           70 :                   gfc_free_expr (tmp->expr);
    8899           70 :                   free (tmp);
    8900           70 :                   continue;
    8901           70 :                 }
    8902              :             }
    8903          321 :           plist = &((*plist)->next);
    8904              :         }
    8905              :     }
    8906              : 
    8907          448 :   delete data.sym_hash;
    8908          224 : }
    8909              : 
    8910              : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    8911              :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    8912              :    INTEGERs, and if stride is a constant it must be nonzero.
    8913              :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    8914              :    not contain a reference to any index-name in the
    8915              :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    8916              : 
    8917              : static void
    8918         2217 : resolve_forall_iterators (gfc_forall_iterator *it)
    8919              : {
    8920         2217 :   gfc_forall_iterator *iter, *iter2;
    8921              : 
    8922         6352 :   for (iter = it; iter; iter = iter->next)
    8923              :     {
    8924         4135 :       if (gfc_resolve_expr (iter->var)
    8925         4135 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    8926            0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    8927              :                    &iter->var->where);
    8928              : 
    8929         4135 :       if (gfc_resolve_expr (iter->start)
    8930         4135 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    8931            0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    8932              :                    &iter->start->where);
    8933         4135 :       if (iter->var->ts.kind != iter->start->ts.kind)
    8934            1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    8935              : 
    8936         4135 :       if (gfc_resolve_expr (iter->end)
    8937         4135 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    8938            0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    8939              :                    &iter->end->where);
    8940         4135 :       if (iter->var->ts.kind != iter->end->ts.kind)
    8941            2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    8942              : 
    8943         4135 :       if (gfc_resolve_expr (iter->stride))
    8944              :         {
    8945         4135 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    8946            0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    8947              :                        &iter->stride->where, "INTEGER");
    8948              : 
    8949         4135 :           if (iter->stride->expr_type == EXPR_CONSTANT
    8950         4131 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    8951            1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    8952              :                        &iter->stride->where);
    8953              :         }
    8954         4135 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    8955            1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    8956              : 
    8957         4135 :       gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8958              :                               VALUE_USED);
    8959         4135 :       gfc_value_used_expr (iter->start, VALUE_USED);
    8960         4135 :       gfc_value_used_expr (iter->end, VALUE_USED);
    8961         4135 :       gfc_value_used_expr (iter->stride, VALUE_USED);
    8962              :     }
    8963              : 
    8964         6352 :   for (iter = it; iter; iter = iter->next)
    8965        11114 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    8966              :       {
    8967         6979 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    8968         6977 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    8969        13954 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    8970            6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    8971            6 :                      "specification at %L", iter->var->symtree->name,
    8972            6 :                      &iter2->start->where);
    8973              :       }
    8974         2217 : }
    8975              : 
    8976              : 
    8977              : /* Given a pointer to a symbol that is a derived type, see if it's
    8978              :    inaccessible, i.e. if it's defined in another module and the components are
    8979              :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    8980              :    inaccessible components are found, nonzero otherwise.  */
    8981              : 
    8982              : static bool
    8983         1352 : derived_inaccessible (gfc_symbol *sym)
    8984              : {
    8985         1352 :   gfc_component *c;
    8986              : 
    8987         1352 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    8988              :     return 1;
    8989              : 
    8990         4001 :   for (c = sym->components; c; c = c->next)
    8991              :     {
    8992              :         /* Prevent an infinite loop through this function.  */
    8993         2662 :         if (c->ts.type == BT_DERIVED
    8994          289 :             && (c->attr.pointer || c->attr.allocatable)
    8995           72 :             && sym == c->ts.u.derived)
    8996           72 :           continue;
    8997              : 
    8998         2590 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    8999              :           return 1;
    9000              :     }
    9001              : 
    9002              :   return 0;
    9003              : }
    9004              : 
    9005              : 
    9006              : /* Resolve the argument of a deallocate expression.  The expression must be
    9007              :    a pointer or a full array.  */
    9008              : 
    9009              : static bool
    9010         8375 : resolve_deallocate_expr (gfc_expr *e)
    9011              : {
    9012         8375 :   symbol_attribute attr;
    9013         8375 :   int allocatable, pointer;
    9014         8375 :   gfc_ref *ref;
    9015         8375 :   gfc_symbol *sym;
    9016         8375 :   gfc_component *c;
    9017         8375 :   bool unlimited;
    9018              : 
    9019         8375 :   if (!gfc_resolve_expr (e))
    9020              :     return false;
    9021              : 
    9022         8375 :   if (e->expr_type != EXPR_VARIABLE)
    9023            0 :     goto bad;
    9024              : 
    9025         8375 :   sym = e->symtree->n.sym;
    9026         8375 :   unlimited = UNLIMITED_POLY(sym);
    9027              : 
    9028         8375 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    9029              :     {
    9030         1574 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    9031         1574 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    9032              :     }
    9033              :   else
    9034              :     {
    9035         6801 :       allocatable = sym->attr.allocatable;
    9036         6801 :       pointer = sym->attr.pointer;
    9037              :     }
    9038        16821 :   for (ref = e->ref; ref; ref = ref->next)
    9039              :     {
    9040         8446 :       switch (ref->type)
    9041              :         {
    9042         6308 :         case REF_ARRAY:
    9043         6308 :           if (ref->u.ar.type != AR_FULL
    9044         6516 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    9045          208 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    9046              :             allocatable = 0;
    9047              :           break;
    9048              : 
    9049         2138 :         case REF_COMPONENT:
    9050         2138 :           c = ref->u.c.component;
    9051         2138 :           if (c->ts.type == BT_CLASS)
    9052              :             {
    9053          297 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    9054          297 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    9055              :             }
    9056              :           else
    9057              :             {
    9058         1841 :               allocatable = c->attr.allocatable;
    9059         1841 :               pointer = c->attr.pointer;
    9060              :             }
    9061              :           break;
    9062              : 
    9063              :         case REF_SUBSTRING:
    9064              :         case REF_INQUIRY:
    9065          513 :           allocatable = 0;
    9066              :           break;
    9067              :         }
    9068              :     }
    9069              : 
    9070         8375 :   attr = gfc_expr_attr (e);
    9071              : 
    9072         8375 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    9073              :     {
    9074            3 :     bad:
    9075            3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9076              :                  &e->where);
    9077            3 :       return false;
    9078              :     }
    9079              : 
    9080              :   /* F2008, C644.  */
    9081         8372 :   if (gfc_is_coindexed (e))
    9082              :     {
    9083            1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    9084            1 :       return false;
    9085              :     }
    9086              : 
    9087         8371 :   if (pointer
    9088        10745 :       && !gfc_check_vardef_context (e, true, true, false,
    9089         2374 :                                     _("DEALLOCATE object")))
    9090              :     return false;
    9091         8369 :   if (!gfc_check_vardef_context (e, false, true, false,
    9092         8369 :                                  _("DEALLOCATE object")))
    9093              :     return false;
    9094              : 
    9095              :   return true;
    9096              : }
    9097              : 
    9098              : 
    9099              : /* Returns true if the expression e contains a reference to the symbol sym.  */
    9100              : static bool
    9101        47383 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    9102              : {
    9103        47383 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    9104         2081 :     return true;
    9105              : 
    9106              :   return false;
    9107              : }
    9108              : 
    9109              : bool
    9110        20080 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    9111              : {
    9112        20080 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    9113              : }
    9114              : 
    9115              : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    9116              :    of character expressions.  */
    9117              : static bool
    9118        20479 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    9119              : {
    9120            0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    9121              : }
    9122              : 
    9123              : 
    9124              : /* Given the expression node e for an allocatable/pointer of derived type to be
    9125              :    allocated, get the expression node to be initialized afterwards (needed for
    9126              :    derived types with default initializers, and derived types with allocatable
    9127              :    components that need nullification.)  */
    9128              : 
    9129              : gfc_expr *
    9130         5799 : gfc_expr_to_initialize (gfc_expr *e)
    9131              : {
    9132         5799 :   gfc_expr *result;
    9133         5799 :   gfc_ref *ref;
    9134         5799 :   int i;
    9135              : 
    9136         5799 :   result = gfc_copy_expr (e);
    9137              : 
    9138              :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    9139        11468 :   for (ref = result->ref; ref; ref = ref->next)
    9140         9038 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    9141              :       {
    9142         3369 :         if (ref->u.ar.dimen == 0
    9143           77 :             && ref->u.ar.as && ref->u.ar.as->corank)
    9144              :           return result;
    9145              : 
    9146         3292 :         ref->u.ar.type = AR_FULL;
    9147              : 
    9148         7436 :         for (i = 0; i < ref->u.ar.dimen; i++)
    9149         4144 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    9150              : 
    9151              :         break;
    9152              :       }
    9153              : 
    9154         5722 :   gfc_free_shape (&result->shape, result->rank);
    9155              : 
    9156              :   /* Recalculate rank, shape, etc.  */
    9157         5722 :   gfc_resolve_expr (result);
    9158         5722 :   return result;
    9159              : }
    9160              : 
    9161              : 
    9162              : /* If the last ref of an expression is an array ref, return a copy of the
    9163              :    expression with that one removed.  Otherwise, a copy of the original
    9164              :    expression.  This is used for allocate-expressions and pointer assignment
    9165              :    LHS, where there may be an array specification that needs to be stripped
    9166              :    off when using gfc_check_vardef_context.  */
    9167              : 
    9168              : static gfc_expr*
    9169        27801 : remove_last_array_ref (gfc_expr* e)
    9170              : {
    9171        27801 :   gfc_expr* e2;
    9172        27801 :   gfc_ref** r;
    9173              : 
    9174        27801 :   e2 = gfc_copy_expr (e);
    9175        35984 :   for (r = &e2->ref; *r; r = &(*r)->next)
    9176        24626 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    9177              :       {
    9178        16443 :         gfc_free_ref_list (*r);
    9179        16443 :         *r = NULL;
    9180        16443 :         break;
    9181              :       }
    9182              : 
    9183        27801 :   return e2;
    9184              : }
    9185              : 
    9186              : 
    9187              : /* Used in resolve_allocate_expr to check that a allocation-object and
    9188              :    a source-expr are conformable.  This does not catch all possible
    9189              :    cases; in particular a runtime checking is needed.  */
    9190              : 
    9191              : static bool
    9192         1910 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    9193              : {
    9194         1910 :   gfc_ref *tail;
    9195         1910 :   bool scalar;
    9196              : 
    9197         2642 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    9198              : 
    9199              :   /* If MOLD= is present and is not scalar, and the allocate-object has an
    9200              :      explicit-shape-spec, the ranks need not agree.  This may be unintended,
    9201              :      so let's emit a warning if -Wsurprising is given.  */
    9202         1910 :   scalar = !tail || tail->type == REF_COMPONENT;
    9203         1910 :   if (e1->mold && e1->rank > 0
    9204          166 :       && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
    9205              :     {
    9206           27 :       if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
    9207           15 :         gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
    9208              :                      "but MOLD= expression at %L has rank %d",
    9209            6 :                      &e2->where, scalar ? 0 : tail->u.ar.as->rank,
    9210              :                      &e1->where, e1->rank);
    9211           30 :       return true;
    9212              :     }
    9213              : 
    9214              :   /* First compare rank.  */
    9215         1880 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    9216            2 :       || (!tail && e1->rank != e2->rank))
    9217              :     {
    9218            7 :       gfc_error ("Source-expr at %L must be scalar or have the "
    9219              :                  "same rank as the allocate-object at %L",
    9220              :                  &e1->where, &e2->where);
    9221            7 :       return false;
    9222              :     }
    9223              : 
    9224         1873 :   if (e1->shape)
    9225              :     {
    9226         1373 :       int i;
    9227         1373 :       mpz_t s;
    9228              : 
    9229         1373 :       mpz_init (s);
    9230              : 
    9231         3165 :       for (i = 0; i < e1->rank; i++)
    9232              :         {
    9233         1379 :           if (tail->u.ar.start[i] == NULL)
    9234              :             break;
    9235              : 
    9236          419 :           if (tail->u.ar.end[i])
    9237              :             {
    9238           54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    9239           54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    9240           54 :               mpz_add_ui (s, s, 1);
    9241              :             }
    9242              :           else
    9243              :             {
    9244          365 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    9245              :             }
    9246              : 
    9247          419 :           if (mpz_cmp (e1->shape[i], s) != 0)
    9248              :             {
    9249            0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    9250              :                          "have the same shape", &e1->where, &e2->where);
    9251            0 :               mpz_clear (s);
    9252            0 :               return false;
    9253              :             }
    9254              :         }
    9255              : 
    9256         1373 :       mpz_clear (s);
    9257              :     }
    9258              : 
    9259              :   return true;
    9260              : }
    9261              : 
    9262              : 
    9263              : /* Resolve the expression in an ALLOCATE statement, doing the additional
    9264              :    checks to see whether the expression is OK or not.  The expression must
    9265              :    have a trailing array reference that gives the size of the array.  */
    9266              : 
    9267              : static bool
    9268        17421 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    9269              : {
    9270        17421 :   int i, pointer, allocatable, dimension, is_abstract;
    9271        17421 :   int codimension;
    9272        17421 :   bool coindexed;
    9273        17421 :   bool unlimited;
    9274        17421 :   symbol_attribute attr;
    9275        17421 :   gfc_ref *ref, *ref2;
    9276        17421 :   gfc_expr *e2;
    9277        17421 :   gfc_array_ref *ar;
    9278        17421 :   gfc_symbol *sym = NULL;
    9279        17421 :   gfc_alloc *a;
    9280        17421 :   gfc_component *c;
    9281        17421 :   bool t;
    9282              : 
    9283              :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    9284              :      checking of coarrays.  */
    9285        22273 :   for (ref = e->ref; ref; ref = ref->next)
    9286        18097 :     if (ref->next == NULL)
    9287              :       break;
    9288              : 
    9289        17421 :   if (ref && ref->type == REF_ARRAY)
    9290        12050 :     ref->u.ar.in_allocate = true;
    9291              : 
    9292        17421 :   if (!gfc_resolve_expr (e))
    9293            1 :     goto failure;
    9294              : 
    9295              :   /* Make sure the expression is allocatable or a pointer.  If it is
    9296              :      pointer, the next-to-last reference must be a pointer.  */
    9297              : 
    9298        17420 :   ref2 = NULL;
    9299        17420 :   if (e->symtree)
    9300        17420 :     sym = e->symtree->n.sym;
    9301              : 
    9302              :   /* Check whether ultimate component is abstract and CLASS.  */
    9303        34840 :   is_abstract = 0;
    9304              : 
    9305              :   /* Is the allocate-object unlimited polymorphic?  */
    9306        17420 :   unlimited = UNLIMITED_POLY(e);
    9307              : 
    9308        17420 :   if (e->expr_type != EXPR_VARIABLE)
    9309              :     {
    9310            0 :       allocatable = 0;
    9311            0 :       attr = gfc_expr_attr (e);
    9312            0 :       pointer = attr.pointer;
    9313            0 :       dimension = attr.dimension;
    9314            0 :       codimension = attr.codimension;
    9315              :     }
    9316              :   else
    9317              :     {
    9318        17420 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    9319              :         {
    9320         3426 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    9321         3426 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    9322         3426 :           dimension = CLASS_DATA (sym)->attr.dimension;
    9323         3426 :           codimension = CLASS_DATA (sym)->attr.codimension;
    9324         3426 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    9325              :         }
    9326              :       else
    9327              :         {
    9328        13994 :           allocatable = sym->attr.allocatable;
    9329        13994 :           pointer = sym->attr.pointer;
    9330        13994 :           dimension = sym->attr.dimension;
    9331        13994 :           codimension = sym->attr.codimension;
    9332              :         }
    9333              : 
    9334        17420 :       coindexed = false;
    9335              : 
    9336        35511 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    9337              :         {
    9338        18093 :           switch (ref->type)
    9339              :             {
    9340        13532 :               case REF_ARRAY:
    9341        13532 :                 if (ref->u.ar.codimen > 0)
    9342              :                   {
    9343          760 :                     int n;
    9344         1061 :                     for (n = ref->u.ar.dimen;
    9345         1061 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    9346          801 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    9347              :                         {
    9348              :                           coindexed = true;
    9349              :                           break;
    9350              :                         }
    9351              :                    }
    9352              : 
    9353        13532 :                 if (ref->next != NULL)
    9354         1484 :                   pointer = 0;
    9355              :                 break;
    9356              : 
    9357         4561 :               case REF_COMPONENT:
    9358              :                 /* F2008, C644.  */
    9359         4561 :                 if (coindexed)
    9360              :                   {
    9361            2 :                     gfc_error ("Coindexed allocatable object at %L",
    9362              :                                &e->where);
    9363            2 :                     goto failure;
    9364              :                   }
    9365              : 
    9366         4559 :                 c = ref->u.c.component;
    9367         4559 :                 if (c->ts.type == BT_CLASS)
    9368              :                   {
    9369          988 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    9370          988 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    9371          988 :                     dimension = CLASS_DATA (c)->attr.dimension;
    9372          988 :                     codimension = CLASS_DATA (c)->attr.codimension;
    9373          988 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    9374              :                   }
    9375              :                 else
    9376              :                   {
    9377         3571 :                     allocatable = c->attr.allocatable;
    9378         3571 :                     pointer = c->attr.pointer;
    9379         3571 :                     dimension = c->attr.dimension;
    9380         3571 :                     codimension = c->attr.codimension;
    9381         3571 :                     is_abstract = c->attr.abstract;
    9382              :                   }
    9383              :                 break;
    9384              : 
    9385            0 :               case REF_SUBSTRING:
    9386            0 :               case REF_INQUIRY:
    9387            0 :                 allocatable = 0;
    9388            0 :                 pointer = 0;
    9389            0 :                 break;
    9390              :             }
    9391              :         }
    9392              :     }
    9393              : 
    9394              :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    9395              :      pointer or an allocatable variable.  */
    9396        17418 :   if (allocatable == 0 && pointer == 0)
    9397              :     {
    9398            4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9399              :                  &e->where);
    9400            4 :       goto failure;
    9401              :     }
    9402              : 
    9403              :   /* Some checks for the SOURCE tag.  */
    9404        17414 :   if (code->expr3)
    9405              :     {
    9406              :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    9407              :          rank as allocate-object".  This would require the MOLD argument to
    9408              :          NULL() as source-expr for subsequent checking.  However, even the
    9409              :          resulting disassociated pointer or unallocated array has no shape that
    9410              :          could be used for SOURCE= or MOLD=.  */
    9411         3851 :       if (code->expr3->expr_type == EXPR_NULL)
    9412              :         {
    9413            4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    9414              :                      &code->expr3->where);
    9415            4 :           goto failure;
    9416              :         }
    9417              : 
    9418              :       /* Check F03:C631.  */
    9419         3847 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    9420              :         {
    9421           10 :           gfc_error ("Type of entity at %L is type incompatible with "
    9422           10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    9423           10 :           goto failure;
    9424              :         }
    9425              : 
    9426              :       /* Check F03:C632 and restriction following Note 6.18.  */
    9427         3837 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    9428            7 :         goto failure;
    9429              : 
    9430              :       /* Check F03:C633.  */
    9431         3830 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    9432              :         {
    9433            1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    9434              :                      "shall have the same kind type parameter",
    9435              :                      &e->where, &code->expr3->where);
    9436            1 :           goto failure;
    9437              :         }
    9438              : 
    9439              :       /* Check F2008, C642.  */
    9440         3829 :       if (code->expr3->ts.type == BT_DERIVED
    9441         3829 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    9442         1192 :               || (code->expr3->ts.u.derived->from_intmod
    9443              :                      == INTMOD_ISO_FORTRAN_ENV
    9444            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9445              :                      == ISOFORTRAN_LOCK_TYPE)))
    9446              :         {
    9447            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9448              :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    9449              :                       "allocate-object at %L is a coarray",
    9450            0 :                       &code->expr3->where, &e->where);
    9451            0 :           goto failure;
    9452              :         }
    9453              : 
    9454              :       /* Check F2008:C639: "Corresponding kind type parameters of
    9455              :          allocate-object and source-expr shall have the same values."  */
    9456         3829 :       if (e->ts.type == BT_CHARACTER
    9457          816 :           && !e->ts.deferred
    9458          162 :           && e->ts.u.cl->length
    9459          162 :           && code->expr3->ts.type == BT_CHARACTER
    9460         3991 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    9461              :                                      "SOURCE= or MOLD= specifier"))
    9462           17 :             goto failure;
    9463              : 
    9464              :       /* Check TS18508, C702/C703.  */
    9465         3812 :       if (code->expr3->ts.type == BT_DERIVED
    9466         5004 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    9467         1192 :               || (code->expr3->ts.u.derived->from_intmod
    9468              :                      == INTMOD_ISO_FORTRAN_ENV
    9469            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9470              :                      == ISOFORTRAN_EVENT_TYPE)))
    9471              :         {
    9472            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9473              :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9474              :                       "allocate-object at %L is a coarray",
    9475            0 :                       &code->expr3->where, &e->where);
    9476            0 :           goto failure;
    9477              :         }
    9478              :     }
    9479              : 
    9480              :   /* Check F08:C629.  */
    9481        17375 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9482          153 :       && !code->expr3)
    9483              :     {
    9484            2 :       gcc_assert (e->ts.type == BT_CLASS);
    9485            2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9486              :                  "type-spec or source-expr", sym->name, &e->where);
    9487            2 :       goto failure;
    9488              :     }
    9489              : 
    9490              :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9491              :      if and only if each allocate-object is a dummy argument for which the
    9492              :      corresponding type parameter is assumed.  */
    9493        17373 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9494          513 :       && code->ext.alloc.ts.u.cl->length != NULL
    9495          498 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9496           23 :       && e->ts.u.cl->length == NULL
    9497            2 :       && e->symtree->n.sym->attr.dummy)
    9498              :     {
    9499            2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9500              :                  "shall be an asterisk as allocate object %qs at %L is a "
    9501              :                  "dummy argument with assumed type parameter",
    9502              :                  sym->name, &e->where);
    9503            2 :       goto failure;
    9504              :     }
    9505              : 
    9506              :   /* Check F08:C632.  */
    9507        17371 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9508           60 :       && !UNLIMITED_POLY (e))
    9509              :     {
    9510           36 :       int cmp;
    9511              : 
    9512           36 :       if (!e->ts.u.cl->length)
    9513           15 :         goto failure;
    9514              : 
    9515           42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9516           21 :                                   code->ext.alloc.ts.u.cl->length);
    9517           21 :       if (cmp == 1 || cmp == -1 || cmp == -3)
    9518              :         {
    9519            2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9520              :                      "character-length parameter as in the declaration",
    9521              :                      sym->name, &e->where);
    9522            2 :           goto failure;
    9523              :         }
    9524              :     }
    9525              : 
    9526              :   /* In the variable definition context checks, gfc_expr_attr is used
    9527              :      on the expression.  This is fooled by the array specification
    9528              :      present in e, thus we have to eliminate that one temporarily.  */
    9529        17354 :   e2 = remove_last_array_ref (e);
    9530        17354 :   t = true;
    9531        17354 :   if (t && pointer)
    9532         3909 :     t = gfc_check_vardef_context (e2, true, true, false,
    9533         3909 :                                   _("ALLOCATE object"));
    9534         3909 :   if (t)
    9535        17346 :     t = gfc_check_vardef_context (e2, false, true, false,
    9536        17346 :                                   _("ALLOCATE object"));
    9537        17354 :   gfc_free_expr (e2);
    9538        17354 :   if (!t)
    9539           11 :     goto failure;
    9540              : 
    9541        17343 :   code->ext.alloc.expr3_not_explicit = 0;
    9542        17343 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9543         1617 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9544              :     {
    9545              :       /* For class arrays, the initialization with SOURCE is done
    9546              :          using _copy and trans_call. It is convenient to exploit that
    9547              :          when the allocated type is different from the declared type but
    9548              :          no SOURCE exists by setting expr3.  */
    9549          305 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9550          305 :       code->ext.alloc.expr3_not_explicit = 1;
    9551              :     }
    9552        17038 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9553         2634 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9554            6 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9555              :     {
    9556              :       /* We have to zero initialize the integer variable.  */
    9557            2 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9558            2 :       code->ext.alloc.expr3_not_explicit = 1;
    9559              :     }
    9560              : 
    9561        17343 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9562              :     {
    9563              :       /* Make sure the vtab symbol is present when
    9564              :          the module variables are generated.  */
    9565         2990 :       gfc_typespec ts = e->ts;
    9566         2990 :       if (code->expr3)
    9567         1331 :         ts = code->expr3->ts;
    9568         1659 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9569          726 :         ts = code->ext.alloc.ts;
    9570              : 
    9571              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9572              :          statement is necessary.  */
    9573         2990 :       gfc_find_derived_vtab (ts.u.derived);
    9574         2990 :     }
    9575        14353 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9576              :     {
    9577              :       /* Again, make sure the vtab symbol is present when
    9578              :          the module variables are generated.  */
    9579          434 :       gfc_typespec *ts = NULL;
    9580          434 :       if (code->expr3)
    9581          347 :         ts = &code->expr3->ts;
    9582              :       else
    9583           87 :         ts = &code->ext.alloc.ts;
    9584              : 
    9585          434 :       gcc_assert (ts);
    9586              : 
    9587              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9588              :          statement is necessary.  */
    9589          434 :       gfc_find_vtab (ts);
    9590              :     }
    9591              : 
    9592        17343 :   if (dimension == 0 && codimension == 0)
    9593         5324 :     goto success;
    9594              : 
    9595              :   /* Make sure the last reference node is an array specification.  */
    9596              : 
    9597        12019 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9598        10786 :       || (dimension && ref2->u.ar.dimen == 0))
    9599              :     {
    9600              :       /* F08:C633.  */
    9601         1233 :       if (code->expr3)
    9602              :         {
    9603         1232 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9604              :                                "in ALLOCATE statement at %L", &e->where))
    9605            0 :             goto failure;
    9606         1232 :           if (code->expr3->rank != 0)
    9607         1231 :             *array_alloc_wo_spec = true;
    9608              :           else
    9609              :             {
    9610            1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9611              :                          "expression required in ALLOCATE statement at %L",
    9612              :                          &e->where);
    9613            1 :               goto failure;
    9614              :             }
    9615              :         }
    9616              :       else
    9617              :         {
    9618            1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9619              :                      "at %L", &e->where);
    9620            1 :           goto failure;
    9621              :         }
    9622              :     }
    9623              : 
    9624              :   /* Make sure that the array section reference makes sense in the
    9625              :      context of an ALLOCATE specification.  */
    9626              : 
    9627        12017 :   ar = &ref2->u.ar;
    9628              : 
    9629        12017 :   if (codimension)
    9630         1179 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9631              :       {
    9632          692 :         switch (ar->dimen_type[i])
    9633              :           {
    9634            2 :           case DIMEN_THIS_IMAGE:
    9635            2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9636              :                        "at %L", &e->where);
    9637            2 :             goto failure;
    9638              : 
    9639           98 :           case  DIMEN_RANGE:
    9640              :             /* F2018:R937:
    9641              :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9642              :              */
    9643           98 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9644              :               {
    9645            8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9646              :                            "at %L", &e->where);
    9647            8 :                 goto failure;
    9648              :               }
    9649           90 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9650              :               {
    9651            2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9652            2 :                            &ar->start[i]->where);
    9653            2 :                 goto failure;
    9654              :               }
    9655              :             break;
    9656              : 
    9657          105 :           case DIMEN_ELEMENT:
    9658          105 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9659              :               {
    9660           97 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9661           97 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9662              :                   {
    9663            1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9664              :                                "of 1 at %L", &ar->start[i]->where);
    9665            1 :                     goto failure;
    9666              :                   }
    9667              :               }
    9668              :             break;
    9669              : 
    9670              :           case DIMEN_STAR:
    9671              :             break;
    9672              : 
    9673            0 :           default:
    9674            0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9675              :                        &e->where);
    9676            0 :             goto failure;
    9677              : 
    9678              :           }
    9679              :       }
    9680        29398 :   for (i = 0; i < ar->dimen; i++)
    9681              :     {
    9682        17398 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9683        14659 :         goto check_symbols;
    9684              : 
    9685         2739 :       switch (ar->dimen_type[i])
    9686              :         {
    9687              :         case DIMEN_ELEMENT:
    9688              :           break;
    9689              : 
    9690         2473 :         case DIMEN_RANGE:
    9691         2473 :           if (ar->start[i] != NULL
    9692         2473 :               && ar->end[i] != NULL
    9693         2472 :               && ar->stride[i] == NULL)
    9694              :             break;
    9695              : 
    9696              :           /* Fall through.  */
    9697              : 
    9698            1 :         case DIMEN_UNKNOWN:
    9699            1 :         case DIMEN_VECTOR:
    9700            1 :         case DIMEN_STAR:
    9701            1 :         case DIMEN_THIS_IMAGE:
    9702            1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9703              :                      &e->where);
    9704            1 :           goto failure;
    9705              :         }
    9706              : 
    9707         2472 : check_symbols:
    9708        45231 :       for (a = code->ext.alloc.list; a; a = a->next)
    9709              :         {
    9710        27837 :           sym = a->expr->symtree->n.sym;
    9711              : 
    9712              :           /* TODO - check derived type components.  */
    9713        27837 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
    9714         9400 :             continue;
    9715              : 
    9716        18437 :           if ((ar->start[i] != NULL
    9717        17756 :                && gfc_find_var_in_expr (sym, ar->start[i]))
    9718        36190 :               || (ar->end[i] != NULL
    9719         2723 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
    9720              :             {
    9721            3 :               gfc_error ("%qs must not appear in the array specification at "
    9722              :                          "%L in the same ALLOCATE statement where it is "
    9723              :                          "itself allocated", sym->name, &ar->where);
    9724            3 :               goto failure;
    9725              :             }
    9726              :         }
    9727              :     }
    9728              : 
    9729        12191 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
    9730              :     {
    9731          868 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
    9732          677 :           || ar->dimen_type[i] == DIMEN_RANGE)
    9733              :         {
    9734          191 :           if (i == (ar->dimen + ar->codimen - 1))
    9735              :             {
    9736            0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
    9737              :                          "statement at %L", &e->where);
    9738            0 :               goto failure;
    9739              :             }
    9740          191 :           continue;
    9741              :         }
    9742              : 
    9743          486 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
    9744          486 :           && ar->stride[i] == NULL)
    9745              :         break;
    9746              : 
    9747            0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
    9748              :                  &e->where);
    9749            0 :       goto failure;
    9750              :     }
    9751              : 
    9752        12000 : success:
    9753        17324 :   gfc_used_in_allocate_expr (e, &e->where);
    9754              : 
    9755        17324 :   if (code->expr3)
    9756         4007 :     gfc_value_set_at (e->symtree->n.sym, &code->expr3->where, VALUE_VARDEF);
    9757              : 
    9758              :   return true;
    9759              : 
    9760              : failure:
    9761              :   return false;
    9762              : }
    9763              : 
    9764              : 
    9765              : static void
    9766        20480 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
    9767              : {
    9768        20480 :   gfc_expr *stat, *errmsg, *pe, *qe;
    9769        20480 :   gfc_alloc *a, *p, *q;
    9770              : 
    9771        20480 :   stat = code->expr1;
    9772        20480 :   errmsg = code->expr2;
    9773              : 
    9774              :   /* Check the stat variable.  */
    9775        20480 :   if (stat)
    9776              :     {
    9777          661 :       if (!gfc_check_vardef_context (stat, false, false, false,
    9778          661 :                                      _("STAT variable")))
    9779            8 :           goto done_stat;
    9780              : 
    9781          653 :       if (stat->ts.type != BT_INTEGER
    9782          644 :           || stat->rank > 0)
    9783           11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
    9784              :                    "variable", &stat->where);
    9785              : 
    9786          653 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
    9787            0 :         goto done_stat;
    9788              : 
    9789              :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
    9790              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9791              :        */
    9792         1354 :       for (p = code->ext.alloc.list; p; p = p->next)
    9793          708 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
    9794              :           {
    9795            9 :             gfc_ref *ref1, *ref2;
    9796            9 :             bool found = true;
    9797              : 
    9798           16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
    9799            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9800              :               {
    9801            9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9802            5 :                   continue;
    9803            4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9804              :                   {
    9805              :                     found = false;
    9806              :                     break;
    9807              :                   }
    9808              :               }
    9809              : 
    9810            9 :             if (found)
    9811              :               {
    9812            7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
    9813              :                            "the same %s statement", &stat->where, fcn, fcn);
    9814            7 :                 break;
    9815              :               }
    9816              :           }
    9817              :     }
    9818              : 
    9819        19819 : done_stat:
    9820              : 
    9821              :   /* Check the errmsg variable.  */
    9822        20480 :   if (errmsg)
    9823              :     {
    9824          150 :       if (!stat)
    9825            2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
    9826              :                      &errmsg->where);
    9827              : 
    9828          150 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
    9829          150 :                                      _("ERRMSG variable")))
    9830            6 :           goto done_errmsg;
    9831              : 
    9832              :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
    9833              :          F18:R930  errmsg-variable       is scalar-default-char-variable
    9834              :          F18:R906  default-char-variable is variable
    9835              :          F18:C906  default-char-variable shall be default character.  */
    9836          144 :       if (errmsg->ts.type != BT_CHARACTER
    9837          142 :           || errmsg->rank > 0
    9838          141 :           || errmsg->ts.kind != gfc_default_character_kind)
    9839            4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
    9840              :                    "variable", &errmsg->where);
    9841              : 
    9842          144 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
    9843            0 :         goto done_errmsg;
    9844              : 
    9845              :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
    9846              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9847              :        */
    9848          286 :       for (p = code->ext.alloc.list; p; p = p->next)
    9849          147 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
    9850              :           {
    9851            9 :             gfc_ref *ref1, *ref2;
    9852            9 :             bool found = true;
    9853              : 
    9854           16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
    9855            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9856              :               {
    9857           11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9858            4 :                   continue;
    9859            7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9860              :                   {
    9861              :                     found = false;
    9862              :                     break;
    9863              :                   }
    9864              :               }
    9865              : 
    9866            9 :             if (found)
    9867              :               {
    9868            5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
    9869              :                            "the same %s statement", &errmsg->where, fcn, fcn);
    9870            5 :                 break;
    9871              :               }
    9872              :           }
    9873              :     }
    9874              : 
    9875        20330 : done_errmsg:
    9876              : 
    9877              :   /* Check that an allocate-object appears only once in the statement.  */
    9878              : 
    9879        46276 :   for (p = code->ext.alloc.list; p; p = p->next)
    9880              :     {
    9881        25796 :       pe = p->expr;
    9882        35090 :       for (q = p->next; q; q = q->next)
    9883              :         {
    9884         9294 :           qe = q->expr;
    9885         9294 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
    9886              :             {
    9887              :               /* This is a potential collision.  */
    9888         2094 :               gfc_ref *pr = pe->ref;
    9889         2094 :               gfc_ref *qr = qe->ref;
    9890              : 
    9891              :               /* Follow the references  until
    9892              :                  a) They start to differ, in which case there is no error;
    9893              :                  you can deallocate a%b and a%c in a single statement
    9894              :                  b) Both of them stop, which is an error
    9895              :                  c) One of them stops, which is also an error.  */
    9896         4518 :               while (1)
    9897              :                 {
    9898         3306 :                   if (pr == NULL && qr == NULL)
    9899              :                     {
    9900            7 :                       gfc_error ("Allocate-object at %L also appears at %L",
    9901              :                                  &pe->where, &qe->where);
    9902            7 :                       break;
    9903              :                     }
    9904         3299 :                   else if (pr != NULL && qr == NULL)
    9905              :                     {
    9906            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9907              :                                  " object at %L", &pe->where, &qe->where);
    9908            2 :                       break;
    9909              :                     }
    9910         3297 :                   else if (pr == NULL && qr != NULL)
    9911              :                     {
    9912            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9913              :                                  " object at %L", &qe->where, &pe->where);
    9914            2 :                       break;
    9915              :                     }
    9916              :                   /* Here, pr != NULL && qr != NULL  */
    9917         3295 :                   gcc_assert(pr->type == qr->type);
    9918         3295 :                   if (pr->type == REF_ARRAY)
    9919              :                     {
    9920              :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
    9921              :                          which are legal.  */
    9922         1065 :                       gcc_assert (qr->type == REF_ARRAY);
    9923              : 
    9924         1065 :                       if (pr->next && qr->next)
    9925              :                         {
    9926              :                           int i;
    9927              :                           gfc_array_ref *par = &(pr->u.ar);
    9928              :                           gfc_array_ref *qar = &(qr->u.ar);
    9929              : 
    9930         1840 :                           for (i=0; i<par->dimen; i++)
    9931              :                             {
    9932          954 :                               if ((par->start[i] != NULL
    9933            0 :                                    || qar->start[i] != NULL)
    9934         1908 :                                   && gfc_dep_compare_expr (par->start[i],
    9935          954 :                                                            qar->start[i]) != 0)
    9936          168 :                                 goto break_label;
    9937              :                             }
    9938              :                         }
    9939              :                     }
    9940              :                   else
    9941              :                     {
    9942         2230 :                       if (pr->u.c.component->name != qr->u.c.component->name)
    9943              :                         break;
    9944              :                     }
    9945              : 
    9946         1212 :                   pr = pr->next;
    9947         1212 :                   qr = qr->next;
    9948         1212 :                 }
    9949         9294 :             break_label:
    9950              :               ;
    9951              :             }
    9952              :         }
    9953              :     }
    9954              : 
    9955        20480 :   if (strcmp (fcn, "ALLOCATE") == 0)
    9956              :     {
    9957        14381 :       bool arr_alloc_wo_spec = false;
    9958              : 
    9959              :       /* Resolving the expr3 in the loop over all objects to allocate would
    9960              :          execute loop invariant code for each loop item.  Therefore do it just
    9961              :          once here.  */
    9962        14381 :       if (code->expr3 && code->expr3->mold
    9963          351 :           && code->expr3->ts.type == BT_DERIVED
    9964           24 :           && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
    9965              :         {
    9966              :           /* Default initialization via MOLD (non-polymorphic).  */
    9967           22 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
    9968           22 :           if (rhs != NULL)
    9969              :             {
    9970            9 :               gfc_resolve_expr (rhs);
    9971            9 :               gfc_free_expr (code->expr3);
    9972            9 :               code->expr3 = rhs;
    9973              :             }
    9974              :         }
    9975        31802 :       for (a = code->ext.alloc.list; a; a = a->next)
    9976        17421 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
    9977              : 
    9978        14381 :       if (arr_alloc_wo_spec && code->expr3)
    9979              :         {
    9980              :           /* Mark the allocate to have to take the array specification
    9981              :              from the expr3.  */
    9982         1225 :           code->ext.alloc.arr_spec_from_expr3 = 1;
    9983              :         }
    9984              :     }
    9985              :   else
    9986              :     {
    9987        14474 :       for (a = code->ext.alloc.list; a; a = a->next)
    9988         8375 :         resolve_deallocate_expr (a->expr);
    9989              :     }
    9990        20480 : }
    9991              : 
    9992              : 
    9993              : /************ SELECT CASE resolution subroutines ************/
    9994              : 
    9995              : /* Callback function for our mergesort variant.  Determines interval
    9996              :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
    9997              :    op1 > op2.  Assumes we're not dealing with the default case.
    9998              :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
    9999              :    There are nine situations to check.  */
   10000              : 
   10001              : static int
   10002         1578 : compare_cases (const gfc_case *op1, const gfc_case *op2)
   10003              : {
   10004         1578 :   int retval;
   10005              : 
   10006         1578 :   if (op1->low == NULL) /* op1 = (:L)  */
   10007              :     {
   10008              :       /* op2 = (:N), so overlap.  */
   10009           52 :       retval = 0;
   10010              :       /* op2 = (M:) or (M:N),  L < M  */
   10011           52 :       if (op2->low != NULL
   10012           52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10013              :         retval = -1;
   10014              :     }
   10015         1526 :   else if (op1->high == NULL) /* op1 = (K:)  */
   10016              :     {
   10017              :       /* op2 = (M:), so overlap.  */
   10018           10 :       retval = 0;
   10019              :       /* op2 = (:N) or (M:N), K > N  */
   10020           10 :       if (op2->high != NULL
   10021           10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10022              :         retval = 1;
   10023              :     }
   10024              :   else /* op1 = (K:L)  */
   10025              :     {
   10026         1516 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
   10027           18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10028           18 :                  ? 1 : 0;
   10029         1498 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
   10030           14 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10031           10 :                  ? -1 : 0;
   10032              :       else                      /* op2 = (M:N)  */
   10033              :         {
   10034         1488 :           retval =  0;
   10035              :           /* L < M  */
   10036         1488 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10037              :             retval =  -1;
   10038              :           /* K > N  */
   10039          412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10040          438 :             retval =  1;
   10041              :         }
   10042              :     }
   10043              : 
   10044         1578 :   return retval;
   10045              : }
   10046              : 
   10047              : 
   10048              : /* Merge-sort a double linked case list, detecting overlap in the
   10049              :    process.  LIST is the head of the double linked case list before it
   10050              :    is sorted.  Returns the head of the sorted list if we don't see any
   10051              :    overlap, or NULL otherwise.  */
   10052              : 
   10053              : static gfc_case *
   10054          646 : check_case_overlap (gfc_case *list)
   10055              : {
   10056          646 :   gfc_case *p, *q, *e, *tail;
   10057          646 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
   10058              : 
   10059              :   /* If the passed list was empty, return immediately.  */
   10060          646 :   if (!list)
   10061              :     return NULL;
   10062              : 
   10063              :   overlap_seen = 0;
   10064              :   insize = 1;
   10065              : 
   10066              :   /* Loop unconditionally.  The only exit from this loop is a return
   10067              :      statement, when we've finished sorting the case list.  */
   10068         1350 :   for (;;)
   10069              :     {
   10070          998 :       p = list;
   10071          998 :       list = NULL;
   10072          998 :       tail = NULL;
   10073              : 
   10074              :       /* Count the number of merges we do in this pass.  */
   10075          998 :       nmerges = 0;
   10076              : 
   10077              :       /* Loop while there exists a merge to be done.  */
   10078         2523 :       while (p)
   10079              :         {
   10080         1525 :           int i;
   10081              : 
   10082              :           /* Count this merge.  */
   10083         1525 :           nmerges++;
   10084              : 
   10085              :           /* Cut the list in two pieces by stepping INSIZE places
   10086              :              forward in the list, starting from P.  */
   10087         1525 :           psize = 0;
   10088         1525 :           q = p;
   10089         3208 :           for (i = 0; i < insize; i++)
   10090              :             {
   10091         2243 :               psize++;
   10092         2243 :               q = q->right;
   10093         2243 :               if (!q)
   10094              :                 break;
   10095              :             }
   10096              :           qsize = insize;
   10097              : 
   10098              :           /* Now we have two lists.  Merge them!  */
   10099         5013 :           while (psize > 0 || (qsize > 0 && q != NULL))
   10100              :             {
   10101              :               /* See from which the next case to merge comes from.  */
   10102          807 :               if (psize == 0)
   10103              :                 {
   10104              :                   /* P is empty so the next case must come from Q.  */
   10105          807 :                   e = q;
   10106          807 :                   q = q->right;
   10107          807 :                   qsize--;
   10108              :                 }
   10109         2681 :               else if (qsize == 0 || q == NULL)
   10110              :                 {
   10111              :                   /* Q is empty.  */
   10112         1103 :                   e = p;
   10113         1103 :                   p = p->right;
   10114         1103 :                   psize--;
   10115              :                 }
   10116              :               else
   10117              :                 {
   10118         1578 :                   cmp = compare_cases (p, q);
   10119         1578 :                   if (cmp < 0)
   10120              :                     {
   10121              :                       /* The whole case range for P is less than the
   10122              :                          one for Q.  */
   10123         1136 :                       e = p;
   10124         1136 :                       p = p->right;
   10125         1136 :                       psize--;
   10126              :                     }
   10127          442 :                   else if (cmp > 0)
   10128              :                     {
   10129              :                       /* The whole case range for Q is greater than
   10130              :                          the case range for P.  */
   10131          438 :                       e = q;
   10132          438 :                       q = q->right;
   10133          438 :                       qsize--;
   10134              :                     }
   10135              :                   else
   10136              :                     {
   10137              :                       /* The cases overlap, or they are the same
   10138              :                          element in the list.  Either way, we must
   10139              :                          issue an error and get the next case from P.  */
   10140              :                       /* FIXME: Sort P and Q by line number.  */
   10141            4 :                       gfc_error ("CASE label at %L overlaps with CASE "
   10142              :                                  "label at %L", &p->where, &q->where);
   10143            4 :                       overlap_seen = 1;
   10144            4 :                       e = p;
   10145            4 :                       p = p->right;
   10146            4 :                       psize--;
   10147              :                     }
   10148              :                 }
   10149              : 
   10150              :                 /* Add the next element to the merged list.  */
   10151         3488 :               if (tail)
   10152         2490 :                 tail->right = e;
   10153              :               else
   10154              :                 list = e;
   10155         3488 :               e->left = tail;
   10156         3488 :               tail = e;
   10157              :             }
   10158              : 
   10159              :           /* P has now stepped INSIZE places along, and so has Q.  So
   10160              :              they're the same.  */
   10161              :           p = q;
   10162              :         }
   10163          998 :       tail->right = NULL;
   10164              : 
   10165              :       /* If we have done only one merge or none at all, we've
   10166              :          finished sorting the cases.  */
   10167          998 :       if (nmerges <= 1)
   10168              :         {
   10169          646 :           if (!overlap_seen)
   10170              :             return list;
   10171              :           else
   10172              :             return NULL;
   10173              :         }
   10174              : 
   10175              :       /* Otherwise repeat, merging lists twice the size.  */
   10176          352 :       insize *= 2;
   10177          352 :     }
   10178              : }
   10179              : 
   10180              : 
   10181              : /* Check to see if an expression is suitable for use in a CASE statement.
   10182              :    Makes sure that all case expressions are scalar constants of the same
   10183              :    type.  Return false if anything is wrong.  */
   10184              : 
   10185              : static bool
   10186         3307 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
   10187              : {
   10188         3307 :   if (e == NULL) return true;
   10189              : 
   10190         3214 :   if (e->ts.type != case_expr->ts.type)
   10191              :     {
   10192            4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
   10193              :                  &e->where, gfc_basic_typename (case_expr->ts.type));
   10194            4 :       return false;
   10195              :     }
   10196              : 
   10197              :   /* C805 (R808) For a given case-construct, each case-value shall be of
   10198              :      the same type as case-expr.  For character type, length differences
   10199              :      are allowed, but the kind type parameters shall be the same.  */
   10200              : 
   10201         3210 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
   10202              :     {
   10203            4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
   10204              :                  &e->where, case_expr->ts.kind);
   10205            4 :       return false;
   10206              :     }
   10207              : 
   10208              :   /* Convert the case value kind to that of case expression kind,
   10209              :      if needed */
   10210              : 
   10211         3206 :   if (e->ts.kind != case_expr->ts.kind)
   10212           14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
   10213              : 
   10214         3206 :   if (e->rank != 0)
   10215              :     {
   10216            0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
   10217              :                  &e->where);
   10218            0 :       return false;
   10219              :     }
   10220              : 
   10221              :   return true;
   10222              : }
   10223              : 
   10224              : 
   10225              : /* Given a completely parsed select statement, we:
   10226              : 
   10227              :      - Validate all expressions and code within the SELECT.
   10228              :      - Make sure that the selection expression is not of the wrong type.
   10229              :      - Make sure that no case ranges overlap.
   10230              :      - Eliminate unreachable cases and unreachable code resulting from
   10231              :        removing case labels.
   10232              : 
   10233              :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
   10234              :    they are a hassle for code generation, and to prevent that, we just
   10235              :    cut them out here.  This is not necessary for overlapping cases
   10236              :    because they are illegal and we never even try to generate code.
   10237              : 
   10238              :    We have the additional caveat that a SELECT construct could have
   10239              :    been a computed GOTO in the source code. Fortunately we can fairly
   10240              :    easily work around that here: The case_expr for a "real" SELECT CASE
   10241              :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
   10242              :    we have to do is make sure that the case_expr is a scalar integer
   10243              :    expression.  */
   10244              : 
   10245              : static void
   10246          687 : resolve_select (gfc_code *code, bool select_type)
   10247              : {
   10248          687 :   gfc_code *body;
   10249          687 :   gfc_expr *case_expr;
   10250          687 :   gfc_case *cp, *default_case, *tail, *head;
   10251          687 :   int seen_unreachable;
   10252          687 :   int seen_logical;
   10253          687 :   int ncases;
   10254          687 :   bt type;
   10255          687 :   bool t;
   10256              : 
   10257          687 :   if (code->expr1 == NULL)
   10258              :     {
   10259              :       /* This was actually a computed GOTO statement.  */
   10260            5 :       case_expr = code->expr2;
   10261            5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
   10262            3 :         gfc_error ("Selection expression in computed GOTO statement "
   10263              :                    "at %L must be a scalar integer expression",
   10264              :                    &case_expr->where);
   10265              : 
   10266              :       /* Further checking is not necessary because this SELECT was built
   10267              :          by the compiler, so it should always be OK.  Just move the
   10268              :          case_expr from expr2 to expr so that we can handle computed
   10269              :          GOTOs as normal SELECTs from here on.  */
   10270            5 :       code->expr1 = code->expr2;
   10271            5 :       code->expr2 = NULL;
   10272            5 :       return;
   10273              :     }
   10274              : 
   10275          682 :   case_expr = code->expr1;
   10276          682 :   type = case_expr->ts.type;
   10277              : 
   10278              :   /* F08:C830.  */
   10279          682 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
   10280            6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
   10281              : 
   10282              :     {
   10283            0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
   10284              :                  &case_expr->where, gfc_typename (case_expr));
   10285              : 
   10286              :       /* Punt. Going on here just produce more garbage error messages.  */
   10287            0 :       return;
   10288              :     }
   10289              : 
   10290              :   /* F08:R842.  */
   10291          682 :   if (!select_type && case_expr->rank != 0)
   10292              :     {
   10293            1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
   10294              :                  "expression", &case_expr->where);
   10295              : 
   10296              :       /* Punt.  */
   10297            1 :       return;
   10298              :     }
   10299              : 
   10300              :   /* Raise a warning if an INTEGER case value exceeds the range of
   10301              :      the case-expr. Later, all expressions will be promoted to the
   10302              :      largest kind of all case-labels.  */
   10303              : 
   10304          681 :   if (type == BT_INTEGER)
   10305         1927 :     for (body = code->block; body; body = body->block)
   10306         2852 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10307              :         {
   10308         1462 :           if (cp->low
   10309         1462 :               && gfc_check_integer_range (cp->low->value.integer,
   10310              :                                           case_expr->ts.kind) != ARITH_OK)
   10311            6 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10312            6 :                          "not in the range of %s", &cp->low->where,
   10313              :                          gfc_typename (case_expr));
   10314              : 
   10315         1462 :           if (cp->high
   10316         1178 :               && cp->low != cp->high
   10317         1570 :               && gfc_check_integer_range (cp->high->value.integer,
   10318              :                                           case_expr->ts.kind) != ARITH_OK)
   10319            0 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10320            0 :                          "not in the range of %s", &cp->high->where,
   10321              :                          gfc_typename (case_expr));
   10322              :         }
   10323              : 
   10324              :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
   10325              :      of the SELECT CASE expression and its CASE values.  Walk the lists
   10326              :      of case values, and if we find a mismatch, promote case_expr to
   10327              :      the appropriate kind.  */
   10328              : 
   10329          681 :   if (type == BT_LOGICAL || type == BT_INTEGER)
   10330              :     {
   10331         2113 :       for (body = code->block; body; body = body->block)
   10332              :         {
   10333              :           /* Walk the case label list.  */
   10334         3113 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10335              :             {
   10336              :               /* Intercept the DEFAULT case.  It does not have a kind.  */
   10337         1597 :               if (cp->low == NULL && cp->high == NULL)
   10338          292 :                 continue;
   10339              : 
   10340              :               /* Unreachable case ranges are discarded, so ignore.  */
   10341         1260 :               if (cp->low != NULL && cp->high != NULL
   10342         1212 :                   && cp->low != cp->high
   10343         1370 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10344           33 :                 continue;
   10345              : 
   10346         1272 :               if (cp->low != NULL
   10347         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
   10348           17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
   10349              : 
   10350         1272 :               if (cp->high != NULL
   10351         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
   10352            4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
   10353              :             }
   10354              :          }
   10355              :     }
   10356              : 
   10357              :   /* Assume there is no DEFAULT case.  */
   10358          681 :   default_case = NULL;
   10359          681 :   head = tail = NULL;
   10360          681 :   ncases = 0;
   10361          681 :   seen_logical = 0;
   10362              : 
   10363         2502 :   for (body = code->block; body; body = body->block)
   10364              :     {
   10365              :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
   10366         1821 :       t = true;
   10367         1821 :       seen_unreachable = 0;
   10368              : 
   10369              :       /* Walk the case label list, making sure that all case labels
   10370              :          are legal.  */
   10371         3829 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10372              :         {
   10373              :           /* Count the number of cases in the whole construct.  */
   10374         2019 :           ncases++;
   10375              : 
   10376              :           /* Intercept the DEFAULT case.  */
   10377         2019 :           if (cp->low == NULL && cp->high == NULL)
   10378              :             {
   10379          362 :               if (default_case != NULL)
   10380              :                 {
   10381            0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10382              :                              "by a second DEFAULT CASE at %L",
   10383              :                              &default_case->where, &cp->where);
   10384            0 :                   t = false;
   10385            0 :                   break;
   10386              :                 }
   10387              :               else
   10388              :                 {
   10389          362 :                   default_case = cp;
   10390          362 :                   continue;
   10391              :                 }
   10392              :             }
   10393              : 
   10394              :           /* Deal with single value cases and case ranges.  Errors are
   10395              :              issued from the validation function.  */
   10396         1657 :           if (!validate_case_label_expr (cp->low, case_expr)
   10397         1657 :               || !validate_case_label_expr (cp->high, case_expr))
   10398              :             {
   10399              :               t = false;
   10400              :               break;
   10401              :             }
   10402              : 
   10403         1649 :           if (type == BT_LOGICAL
   10404           78 :               && ((cp->low == NULL || cp->high == NULL)
   10405           76 :                   || cp->low != cp->high))
   10406              :             {
   10407            2 :               gfc_error ("Logical range in CASE statement at %L is not "
   10408              :                          "allowed",
   10409            1 :                          cp->low ? &cp->low->where : &cp->high->where);
   10410            2 :               t = false;
   10411            2 :               break;
   10412              :             }
   10413              : 
   10414           76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
   10415              :             {
   10416           76 :               int value;
   10417           76 :               value = cp->low->value.logical == 0 ? 2 : 1;
   10418           76 :               if (value & seen_logical)
   10419              :                 {
   10420            1 :                   gfc_error ("Constant logical value in CASE statement "
   10421              :                              "is repeated at %L",
   10422              :                              &cp->low->where);
   10423            1 :                   t = false;
   10424            1 :                   break;
   10425              :                 }
   10426           75 :               seen_logical |= value;
   10427              :             }
   10428              : 
   10429         1602 :           if (cp->low != NULL && cp->high != NULL
   10430         1555 :               && cp->low != cp->high
   10431         1758 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10432              :             {
   10433           35 :               if (warn_surprising)
   10434            1 :                 gfc_warning (OPT_Wsurprising,
   10435              :                              "Range specification at %L can never be matched",
   10436              :                              &cp->where);
   10437              : 
   10438           35 :               cp->unreachable = 1;
   10439           35 :               seen_unreachable = 1;
   10440              :             }
   10441              :           else
   10442              :             {
   10443              :               /* If the case range can be matched, it can also overlap with
   10444              :                  other cases.  To make sure it does not, we put it in a
   10445              :                  double linked list here.  We sort that with a merge sort
   10446              :                  later on to detect any overlapping cases.  */
   10447         1611 :               if (!head)
   10448              :                 {
   10449          646 :                   head = tail = cp;
   10450          646 :                   head->right = head->left = NULL;
   10451              :                 }
   10452              :               else
   10453              :                 {
   10454          965 :                   tail->right = cp;
   10455          965 :                   tail->right->left = tail;
   10456          965 :                   tail = tail->right;
   10457          965 :                   tail->right = NULL;
   10458              :                 }
   10459              :             }
   10460              :         }
   10461              : 
   10462              :       /* It there was a failure in the previous case label, give up
   10463              :          for this case label list.  Continue with the next block.  */
   10464         1821 :       if (!t)
   10465           11 :         continue;
   10466              : 
   10467              :       /* See if any case labels that are unreachable have been seen.
   10468              :          If so, we eliminate them.  This is a bit of a kludge because
   10469              :          the case lists for a single case statement (label) is a
   10470              :          single forward linked lists.  */
   10471         1810 :       if (seen_unreachable)
   10472              :       {
   10473              :         /* Advance until the first case in the list is reachable.  */
   10474           69 :         while (body->ext.block.case_list != NULL
   10475           69 :                && body->ext.block.case_list->unreachable)
   10476              :           {
   10477           34 :             gfc_case *n = body->ext.block.case_list;
   10478           34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10479           34 :             n->next = NULL;
   10480           34 :             gfc_free_case_list (n);
   10481              :           }
   10482              : 
   10483              :         /* Strip all other unreachable cases.  */
   10484           35 :         if (body->ext.block.case_list)
   10485              :           {
   10486            2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10487              :               {
   10488            1 :                 if (cp->next->unreachable)
   10489              :                   {
   10490            1 :                     gfc_case *n = cp->next;
   10491            1 :                     cp->next = cp->next->next;
   10492            1 :                     n->next = NULL;
   10493            1 :                     gfc_free_case_list (n);
   10494              :                   }
   10495              :               }
   10496              :           }
   10497              :       }
   10498              :     }
   10499              : 
   10500              :   /* See if there were overlapping cases.  If the check returns NULL,
   10501              :      there was overlap.  In that case we don't do anything.  If head
   10502              :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10503              :      then used during code generation for SELECT CASE constructs with
   10504              :      a case expression of a CHARACTER type.  */
   10505          681 :   if (head)
   10506              :     {
   10507          646 :       head = check_case_overlap (head);
   10508              : 
   10509              :       /* Prepend the default_case if it is there.  */
   10510          646 :       if (head != NULL && default_case)
   10511              :         {
   10512          345 :           default_case->left = NULL;
   10513          345 :           default_case->right = head;
   10514          345 :           head->left = default_case;
   10515              :         }
   10516              :     }
   10517              : 
   10518              :   /* Eliminate dead blocks that may be the result if we've seen
   10519              :      unreachable case labels for a block.  */
   10520         2468 :   for (body = code; body && body->block; body = body->block)
   10521              :     {
   10522         1787 :       if (body->block->ext.block.case_list == NULL)
   10523              :         {
   10524              :           /* Cut the unreachable block from the code chain.  */
   10525           34 :           gfc_code *c = body->block;
   10526           34 :           body->block = c->block;
   10527              : 
   10528              :           /* Kill the dead block, but not the blocks below it.  */
   10529           34 :           c->block = NULL;
   10530           34 :           gfc_free_statements (c);
   10531              :         }
   10532              :     }
   10533              : 
   10534              :   /* More than two cases is legal but insane for logical selects.
   10535              :      Issue a warning for it.  */
   10536          681 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10537            0 :     gfc_warning (OPT_Wsurprising,
   10538              :                  "Logical SELECT CASE block at %L has more that two cases",
   10539              :                  &code->loc);
   10540              : }
   10541              : 
   10542              : 
   10543              : /* Check if a derived type is extensible.  */
   10544              : 
   10545              : bool
   10546        24214 : gfc_type_is_extensible (gfc_symbol *sym)
   10547              : {
   10548        24214 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10549        24198 :            || (sym->attr.is_class
   10550         2208 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10551              : }
   10552              : 
   10553              : 
   10554              : static void
   10555              : resolve_types (gfc_namespace *ns);
   10556              : 
   10557              : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10558              :    correct as well as possibly the array-spec.  */
   10559              : 
   10560              : static void
   10561        12931 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10562              : {
   10563        12931 :   gfc_expr* target;
   10564        12931 :   bool parentheses = false;
   10565              : 
   10566        12931 :   gcc_assert (sym->assoc);
   10567        12931 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10568              : 
   10569        12931 :   if (sym->assoc->target
   10570         7749 :       && sym->assoc->target->expr_type == EXPR_FUNCTION
   10571          598 :       && sym->assoc->target->symtree
   10572          598 :       && sym->assoc->target->symtree->n.sym
   10573          598 :       && sym->assoc->target->symtree->n.sym->attr.generic)
   10574              :     {
   10575           33 :       if (gfc_resolve_expr (sym->assoc->target))
   10576           33 :         sym->ts = sym->assoc->target->ts;
   10577              :       else
   10578              :         {
   10579            0 :           gfc_error ("%s could not be resolved to a specific function at %L",
   10580            0 :                      sym->assoc->target->symtree->n.sym->name,
   10581            0 :                      &sym->assoc->target->where);
   10582            0 :           return;
   10583              :         }
   10584              :     }
   10585              : 
   10586              :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10587              :      case, return.  Resolution will be called later manually again when
   10588              :      this is done.  */
   10589        12931 :   target = sym->assoc->target;
   10590        12931 :   if (!target)
   10591              :     return;
   10592         7749 :   gcc_assert (!sym->assoc->dangling);
   10593              : 
   10594         7749 :   if (target->expr_type == EXPR_OP
   10595          267 :       && target->value.op.op == INTRINSIC_PARENTHESES
   10596           42 :       && target->value.op.op1->expr_type == EXPR_VARIABLE)
   10597              :     {
   10598           23 :       sym->assoc->target = gfc_copy_expr (target->value.op.op1);
   10599           23 :       gfc_free_expr (target);
   10600           23 :       target = sym->assoc->target;
   10601           23 :       parentheses = true;
   10602              :     }
   10603              : 
   10604         7749 :   if (resolve_target && !gfc_resolve_expr (target))
   10605              :     return;
   10606              : 
   10607         7744 :   if (sym->assoc->ar)
   10608              :     {
   10609              :       int dim;
   10610              :       gfc_array_ref *ar = sym->assoc->ar;
   10611           68 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10612              :         {
   10613           39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10614           39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10615           78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10616           39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10617            0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10618              :                        "remapping of associate name %s at %L",
   10619              :                        sym->name, &sym->declared_at);
   10620              :         }
   10621              :     }
   10622              : 
   10623              :   /* For variable targets, we get some attributes from the target.  */
   10624         7744 :   if (target->expr_type == EXPR_VARIABLE)
   10625              :     {
   10626         6695 :       gfc_symbol *tsym, *dsym;
   10627              : 
   10628         6695 :       gcc_assert (target->symtree);
   10629         6695 :       tsym = target->symtree->n.sym;
   10630              : 
   10631         6695 :       if (gfc_expr_attr (target).proc_pointer)
   10632              :         {
   10633            0 :           gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10634              :                      tsym->name, &target->where);
   10635            0 :           return;
   10636              :         }
   10637              : 
   10638           74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10639            2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10640         6696 :           && dsym->attr.flavor == FL_DERIVED)
   10641              :         {
   10642            1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10643              :                      tsym->name, &target->where);
   10644            1 :           return;
   10645              :         }
   10646              : 
   10647         6694 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10648              :         {
   10649           73 :           bool is_error = true;
   10650           73 :           if (tsym->attr.function && tsym->result == tsym)
   10651          141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10652          137 :               if (tsym == ns->proc_name)
   10653              :                 {
   10654              :                   is_error = false;
   10655              :                   break;
   10656              :                 }
   10657           64 :           if (is_error)
   10658              :             {
   10659           13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10660              :                          tsym->name, &target->where);
   10661           13 :               return;
   10662              :             }
   10663              :         }
   10664              : 
   10665         6681 :       sym->attr.asynchronous = tsym->attr.asynchronous;
   10666         6681 :       sym->attr.volatile_ = tsym->attr.volatile_;
   10667              : 
   10668        13362 :       sym->attr.target = tsym->attr.target
   10669         6681 :                          || gfc_expr_attr (target).pointer;
   10670         6681 :       if (is_subref_array (target))
   10671          402 :         sym->attr.subref_array_pointer = 1;
   10672              :     }
   10673         1049 :   else if (target->ts.type == BT_PROCEDURE)
   10674              :     {
   10675            0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   10676              :                  &target->where);
   10677            0 :       return;
   10678              :     }
   10679              : 
   10680         7730 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   10681              :     {
   10682              :       /* By now, the type of the target has been fixed up.  */
   10683          314 :       symbol_attribute attr;
   10684              : 
   10685          314 :       if (sym->ts.type == BT_DERIVED
   10686          181 :           && target->ts.type == BT_CLASS
   10687           31 :           && !UNLIMITED_POLY (target))
   10688              :         {
   10689              :           /* Inferred to be derived type but the target has type class.  */
   10690           31 :           sym->ts = CLASS_DATA (target)->ts;
   10691           31 :           if (!sym->as)
   10692           31 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   10693           31 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10694           31 :           sym->attr.dimension = target->rank ? 1 : 0;
   10695           31 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10696              :                             target->corank);
   10697           31 :           sym->as = NULL;
   10698              :         }
   10699          283 :       else if (target->ts.type == BT_DERIVED
   10700          150 :                && target->symtree && target->symtree->n.sym
   10701          126 :                && target->symtree->n.sym->ts.type == BT_CLASS
   10702            0 :                && IS_INFERRED_TYPE (target)
   10703            0 :                && target->ref && target->ref->next
   10704            0 :                && target->ref->next->type == REF_ARRAY
   10705            0 :                && !target->ref->next->next)
   10706              :         {
   10707              :           /* A inferred type selector whose symbol has been determined to be
   10708              :              a class array but which only has an array reference. Change the
   10709              :              associate name and the selector to class type.  */
   10710            0 :           sym->ts = target->ts;
   10711            0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10712            0 :           sym->attr.dimension = target->rank ? 1 : 0;
   10713            0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10714              :                             target->corank);
   10715            0 :           sym->as = NULL;
   10716            0 :           target->ts = sym->ts;
   10717              :         }
   10718          283 :       else if ((target->ts.type == BT_DERIVED)
   10719          133 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   10720           61 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   10721              :         /* Confirmed to be either a derived type or misidentified to be a
   10722              :            scalar class object, when the selector is a class array.  */
   10723          156 :         sym->ts = target->ts;
   10724          127 :       else if (sym->assoc->inferred_type
   10725          120 :                && (sym->ts.type == BT_COMPLEX
   10726           78 :                    || sym->ts.type == BT_CHARACTER)
   10727           66 :                && target->ts.type == sym->ts.type
   10728           66 :                && sym->ts.kind != target->ts.kind)
   10729              :         /* The inferred type was set from a %re, %im or %len inquiry on
   10730              :            the associate name with the default kind, before the target's
   10731              :            actual type was known.  Now that the target has been resolved,
   10732              :            update the kind to match.  */
   10733            6 :         sym->ts = target->ts;
   10734              :     }
   10735              : 
   10736              : 
   10737         7730 :   if (target->expr_type == EXPR_NULL)
   10738              :     {
   10739            1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   10740            1 :       return;
   10741              :     }
   10742         7729 :   else if (target->ts.type == BT_UNKNOWN)
   10743              :     {
   10744            2 :       gfc_error ("Selector at %L has no type", &target->where);
   10745            2 :       return;
   10746              :     }
   10747              : 
   10748              :   /* Get type if this was not already set.  Note that it can be
   10749              :      some other type than the target in case this is a SELECT TYPE
   10750              :      selector!  So we must not update when the type is already there.  */
   10751         7727 :   if (sym->ts.type == BT_UNKNOWN)
   10752          259 :     sym->ts = target->ts;
   10753              : 
   10754         7727 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   10755              : 
   10756              :   /* See if this is a valid association-to-variable.  */
   10757        15454 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   10758         6681 :                            && !parentheses
   10759         6660 :                            && !gfc_has_vector_subscript (target))
   10760         7775 :                           || gfc_is_ptr_fcn (target));
   10761              : 
   10762              :   /* Finally resolve if this is an array or not.  */
   10763         7727 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   10764          237 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   10765              :     {
   10766          142 :       gfc_expression_rank (target);
   10767          142 :       if (target->ts.type == BT_DERIVED
   10768           95 :           && !sym->as
   10769           95 :           && target->symtree->n.sym->as)
   10770              :         {
   10771            0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   10772            0 :           sym->attr.dimension = 1;
   10773              :         }
   10774          142 :       else if (target->ts.type == BT_CLASS
   10775           47 :                && CLASS_DATA (target)->as)
   10776              :         {
   10777            0 :           target->rank = CLASS_DATA (target)->as->rank;
   10778            0 :           target->corank = CLASS_DATA (target)->as->corank;
   10779            0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   10780              :             {
   10781            0 :               sym->ts = target->ts;
   10782            0 :               sym->attr.dimension = 0;
   10783              :             }
   10784              :         }
   10785              :     }
   10786              : 
   10787              : 
   10788         7727 :   if (sym->attr.dimension && target->rank == 0)
   10789              :     {
   10790              :       /* primary.cc makes the assumption that a reference to an associate
   10791              :          name followed by a left parenthesis is an array reference.  */
   10792           17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   10793              :         {
   10794           12 :           gfc_expression_rank (sym->assoc->target);
   10795           12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   10796           12 :           if (!sym->attr.dimension && sym->as)
   10797            0 :             sym->as = NULL;
   10798              :         }
   10799              : 
   10800           17 :       if (sym->attr.dimension && target->rank == 0)
   10801              :         {
   10802            5 :           if (sym->ts.type != BT_CHARACTER)
   10803            5 :             gfc_error ("Associate-name %qs at %L is used as array",
   10804              :                        sym->name, &sym->declared_at);
   10805            5 :           sym->attr.dimension = 0;
   10806            5 :           return;
   10807              :         }
   10808              :     }
   10809              : 
   10810              :   /* We cannot deal with class selectors that need temporaries.  */
   10811         7722 :   if (target->ts.type == BT_CLASS
   10812         7722 :         && gfc_ref_needs_temporary_p (target->ref))
   10813              :     {
   10814            1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   10815              :                  "yet implemented", &target->where);
   10816            1 :       return;
   10817              :     }
   10818              : 
   10819         7721 :   if (target->ts.type == BT_CLASS)
   10820         2824 :     gfc_fix_class_refs (target);
   10821              : 
   10822         7721 :   if ((target->rank > 0 || target->corank > 0)
   10823         2748 :       && !sym->attr.select_rank_temporary)
   10824              :     {
   10825         2748 :       gfc_array_spec *as;
   10826              :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   10827              :          it is corrected now.  */
   10828         2748 :       if (sym->ts.type != BT_CLASS
   10829         2163 :           && (!sym->as || sym->as->corank != target->corank))
   10830              :         {
   10831          135 :           if (!sym->as)
   10832          128 :             sym->as = gfc_get_array_spec ();
   10833          135 :           as = sym->as;
   10834          135 :           as->rank = target->rank;
   10835          135 :           as->type = AS_DEFERRED;
   10836          135 :           as->corank = target->corank;
   10837          135 :           sym->attr.dimension = 1;
   10838          135 :           if (as->corank != 0)
   10839            7 :             sym->attr.codimension = 1;
   10840              :         }
   10841         2613 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   10842          584 :                && (!CLASS_DATA (sym)->as
   10843          584 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   10844              :         {
   10845            0 :           if (!CLASS_DATA (sym)->as)
   10846            0 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   10847            0 :           as = CLASS_DATA (sym)->as;
   10848            0 :           as->rank = target->rank;
   10849            0 :           as->type = AS_DEFERRED;
   10850            0 :           as->corank = target->corank;
   10851            0 :           CLASS_DATA (sym)->attr.dimension = 1;
   10852            0 :           if (as->corank != 0)
   10853            0 :             CLASS_DATA (sym)->attr.codimension = 1;
   10854              :         }
   10855              :     }
   10856         4973 :   else if (!sym->attr.select_rank_temporary)
   10857              :     {
   10858              :       /* target's rank is 0, but the type of the sym is still array valued,
   10859              :          which has to be corrected.  */
   10860         3584 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   10861          724 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   10862              :         {
   10863           24 :           gfc_array_spec *as;
   10864           24 :           symbol_attribute attr;
   10865              :           /* The associated variable's type is still the array type
   10866              :              correct this now.  */
   10867           24 :           gfc_typespec *ts = &target->ts;
   10868           24 :           gfc_ref *ref;
   10869              :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   10870              :            */
   10871           24 :           bool internal_ref = true;
   10872              : 
   10873           72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   10874              :             {
   10875           48 :               switch (ref->type)
   10876              :                 {
   10877           24 :                 case REF_COMPONENT:
   10878           24 :                   ts = &ref->u.c.component->ts;
   10879           24 :                   internal_ref
   10880           24 :                     = target->ref == ref && ref->next
   10881           48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   10882              :                   break;
   10883           24 :                 case REF_ARRAY:
   10884           24 :                   if (ts->type == BT_CLASS)
   10885            0 :                     ts = &ts->u.derived->components->ts;
   10886           24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   10887            0 :                     for (int i = ref->u.ar.dimen;
   10888              :                          internal_ref
   10889            0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   10890              :                          ++i)
   10891            0 :                       internal_ref
   10892            0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   10893              :                   break;
   10894              :                 default:
   10895              :                   break;
   10896              :                 }
   10897              :             }
   10898              :           /* Only rewrite the type of this symbol, when the refs are not the
   10899              :              internal ones for class and co-array this-image.  */
   10900           24 :           if (!internal_ref)
   10901              :             {
   10902              :               /* Create a scalar instance of the current class type.  Because
   10903              :                  the rank of a class array goes into its name, the type has to
   10904              :                  be rebuilt.  The alternative of (re-)setting just the
   10905              :                  attributes and as in the current type, destroys the type also
   10906              :                  in other places.  */
   10907            0 :               as = NULL;
   10908            0 :               sym->ts = *ts;
   10909            0 :               sym->ts.type = BT_CLASS;
   10910            0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10911            0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   10912            0 :               sym->as = NULL;
   10913              :             }
   10914              :         }
   10915              :     }
   10916              : 
   10917              :   /* Mark this as an associate variable.  */
   10918         7721 :   sym->attr.associate_var = 1;
   10919              : 
   10920              :   /* Fix up the type-spec for CHARACTER types.  */
   10921         7721 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   10922              :     {
   10923          527 :       gfc_ref *ref;
   10924          812 :       for (ref = target->ref; ref; ref = ref->next)
   10925          311 :         if (ref->type == REF_SUBSTRING
   10926           74 :             && (ref->u.ss.start == NULL
   10927           74 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   10928           74 :                 || ref->u.ss.end == NULL
   10929           54 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   10930              :           break;
   10931              : 
   10932          527 :       if (!sym->ts.u.cl)
   10933          182 :         sym->ts.u.cl = target->ts.u.cl;
   10934              : 
   10935          527 :       if (sym->ts.deferred
   10936          195 :           && sym->ts.u.cl == target->ts.u.cl)
   10937              :         {
   10938          116 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10939          116 :           sym->ts.deferred = 1;
   10940              :         }
   10941              : 
   10942          527 :       if (!sym->ts.u.cl->length
   10943          333 :           && !sym->ts.deferred
   10944          138 :           && target->expr_type == EXPR_CONSTANT)
   10945              :         {
   10946           30 :           sym->ts.u.cl->length =
   10947           30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   10948           30 :                                   target->value.character.length);
   10949              :         }
   10950          497 :       else if (((!sym->ts.u.cl->length
   10951          194 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   10952          309 :                 && target->expr_type != EXPR_VARIABLE)
   10953          368 :                || ref)
   10954              :         {
   10955          155 :           if (!sym->ts.deferred)
   10956              :             {
   10957           45 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10958           45 :               sym->ts.deferred = 1;
   10959              :             }
   10960              : 
   10961              :           /* This is reset in trans-stmt.cc after the assignment
   10962              :              of the target expression to the associate name.  */
   10963          155 :           if (ref && sym->as)
   10964           26 :             sym->attr.pointer = 1;
   10965              :           else
   10966          129 :             sym->attr.allocatable = 1;
   10967              :         }
   10968              :     }
   10969              : 
   10970         7721 :   if (sym->ts.type == BT_CLASS
   10971         1454 :       && IS_INFERRED_TYPE (target)
   10972           13 :       && target->ts.type == BT_DERIVED
   10973            0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   10974            0 :       && target->ref && target->ref->next && !target->ref->next->next
   10975            0 :       && target->ref->next->type == REF_ARRAY)
   10976            0 :     target->ts = target->symtree->n.sym->ts;
   10977              : 
   10978              :   /* If the target is a good class object, so is the associate variable.  */
   10979         7721 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   10980          725 :     sym->attr.class_ok = 1;
   10981              : 
   10982              :   /* If the target is a contiguous pointer, so is the associate variable.  */
   10983         7721 :   if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
   10984            3 :     sym->attr.contiguous = 1;
   10985              : }
   10986              : 
   10987              : 
   10988              : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   10989              :    array reference, where necessary.  The symbols are artificial and so
   10990              :    the dimension attribute and arrayspec can also be set.  In addition,
   10991              :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   10992              :    This is corrected here as well.*/
   10993              : 
   10994              : static void
   10995         1701 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   10996              :                  gfc_ref *ref)
   10997              : {
   10998         1701 :   gfc_ref *nref = (*expr1)->ref;
   10999         1701 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   11000         1701 :   gfc_symbol *sym2;
   11001         1701 :   gfc_expr *selector = gfc_copy_expr (expr2);
   11002              : 
   11003         1701 :   (*expr1)->rank = rank;
   11004         1701 :   (*expr1)->corank = corank;
   11005         1701 :   if (selector)
   11006              :     {
   11007          318 :       gfc_resolve_expr (selector);
   11008          318 :       if (selector->expr_type == EXPR_OP
   11009            2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   11010            2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   11011          316 :       else if (selector->expr_type == EXPR_VARIABLE
   11012            7 :                || selector->expr_type == EXPR_FUNCTION)
   11013          316 :         sym2 = selector->symtree->n.sym;
   11014              :       else
   11015            0 :         gcc_unreachable ();
   11016              :     }
   11017              :   else
   11018              :     sym2 = NULL;
   11019              : 
   11020         1701 :   if (sym1->ts.type == BT_CLASS)
   11021              :     {
   11022         1701 :       if ((*expr1)->ts.type != BT_CLASS)
   11023           13 :         (*expr1)->ts = sym1->ts;
   11024              : 
   11025         1701 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   11026         1701 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   11027         1701 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   11028            1 :         CLASS_DATA (sym1)->as
   11029            1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   11030              :     }
   11031              :   else
   11032              :     {
   11033            0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   11034            0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   11035            0 :       if (sym1->as == NULL && sym2)
   11036            0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   11037              :     }
   11038              : 
   11039         3078 :   for (; nref; nref = nref->next)
   11040         2760 :     if (nref->next == NULL)
   11041              :       break;
   11042              : 
   11043         1701 :   if (ref && nref && nref->type != REF_ARRAY)
   11044            6 :     nref->next = gfc_copy_ref (ref);
   11045         1695 :   else if (ref && !nref)
   11046          309 :     (*expr1)->ref = gfc_copy_ref (ref);
   11047         1386 :   else if (ref && nref->u.ar.codimen != corank)
   11048              :     {
   11049          976 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   11050          915 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   11051           61 :       nref->u.ar.codimen = corank;
   11052              :     }
   11053         1701 : }
   11054              : 
   11055              : 
   11056              : static gfc_expr *
   11057         6796 : build_loc_call (gfc_expr *sym_expr)
   11058              : {
   11059         6796 :   gfc_expr *loc_call;
   11060         6796 :   loc_call = gfc_get_expr ();
   11061         6796 :   loc_call->expr_type = EXPR_FUNCTION;
   11062         6796 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   11063         6796 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   11064         6796 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   11065         6796 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   11066         6796 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   11067         6796 :   loc_call->ts.type = BT_INTEGER;
   11068         6796 :   loc_call->ts.kind = gfc_index_integer_kind;
   11069         6796 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   11070         6796 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   11071         6796 :   loc_call->value.function.actual->expr = sym_expr;
   11072         6796 :   loc_call->where = sym_expr->where;
   11073         6796 :   return loc_call;
   11074              : }
   11075              : 
   11076              : /* Resolve a SELECT TYPE statement.  */
   11077              : 
   11078              : static void
   11079         3051 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   11080              : {
   11081         3051 :   gfc_symbol *selector_type;
   11082         3051 :   gfc_code *body, *new_st, *if_st, *tail;
   11083         3051 :   gfc_code *class_is = NULL, *default_case = NULL;
   11084         3051 :   gfc_case *c;
   11085         3051 :   gfc_symtree *st;
   11086         3051 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   11087         3051 :   gfc_namespace *ns;
   11088         3051 :   int error = 0;
   11089         3051 :   int rank = 0, corank = 0;
   11090         3051 :   gfc_ref* ref = NULL;
   11091         3051 :   gfc_expr *selector_expr = NULL;
   11092         3051 :   gfc_code *old_code = code;
   11093              : 
   11094         3051 :   ns = code->ext.block.ns;
   11095         3051 :   if (code->expr2)
   11096              :     {
   11097              :       /* Set this, or coarray checks in resolve will fail.  */
   11098          658 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   11099              :     }
   11100         3051 :   gfc_resolve (ns);
   11101              : 
   11102              :   /* Check for F03:C813.  */
   11103         3051 :   if (code->expr1->ts.type != BT_CLASS
   11104           36 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   11105              :     {
   11106           13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   11107              :                  "at %L", &code->loc);
   11108           42 :       return;
   11109              :     }
   11110              : 
   11111              :   /* Prevent segfault, when class type is not initialized due to previous
   11112              :      error.  */
   11113         3038 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   11114         3036 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   11115              :     return;
   11116              : 
   11117         3031 :   if (code->expr2)
   11118              :     {
   11119          649 :       gfc_ref *ref2 = NULL;
   11120         1502 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   11121          853 :          if (ref->type == REF_COMPONENT
   11122          435 :              && ref->u.c.component->ts.type == BT_CLASS)
   11123          853 :            ref2 = ref;
   11124              : 
   11125          649 :       if (ref2)
   11126              :         {
   11127          341 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11128            1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   11129          341 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   11130              :         }
   11131              :       else
   11132              :         {
   11133          308 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11134           28 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   11135              :           /* Sometimes the selector expression is given the typespec of the
   11136              :              '_data' field, which is logical enough but inappropriate here. */
   11137          308 :           if (code->expr2->ts.type == BT_DERIVED
   11138           73 :               && code->expr2->symtree
   11139           73 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   11140           73 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   11141          308 :           selector_type = CLASS_DATA (code->expr2)
   11142              :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   11143              :         }
   11144              : 
   11145          649 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   11146              :         {
   11147          304 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   11148          304 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   11149          304 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   11150              :         }
   11151              : 
   11152              :       /* F2008: C803 The selector expression must not be coindexed.  */
   11153          649 :       if (gfc_is_coindexed (code->expr2))
   11154              :         {
   11155            4 :           gfc_error ("Selector at %L must not be coindexed",
   11156            4 :                      &code->expr2->where);
   11157            4 :           return;
   11158              :         }
   11159              : 
   11160              :     }
   11161              :   else
   11162              :     {
   11163         2382 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   11164              : 
   11165         2382 :       if (gfc_is_coindexed (code->expr1))
   11166              :         {
   11167            0 :           gfc_error ("Selector at %L must not be coindexed",
   11168            0 :                      &code->expr1->where);
   11169            0 :           return;
   11170              :         }
   11171              :     }
   11172              : 
   11173              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11174         8441 :   for (body = code->block; body; body = body->block)
   11175              :     {
   11176         5415 :       c = body->ext.block.case_list;
   11177              : 
   11178         5415 :       if (!error)
   11179              :         {
   11180              :           /* Check for repeated cases.  */
   11181         8398 :           for (tail = code->block; tail; tail = tail->block)
   11182              :             {
   11183         8398 :               gfc_case *d = tail->ext.block.case_list;
   11184         8398 :               if (tail == body)
   11185              :                 break;
   11186              : 
   11187         2992 :               if (c->ts.type == d->ts.type
   11188          516 :                   && ((c->ts.type == BT_DERIVED
   11189          418 :                        && c->ts.u.derived && d->ts.u.derived
   11190          418 :                        && !strcmp (c->ts.u.derived->name,
   11191              :                                    d->ts.u.derived->name))
   11192          515 :                       || c->ts.type == BT_UNKNOWN
   11193          515 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11194           55 :                           && c->ts.kind == d->ts.kind)))
   11195              :                 {
   11196            1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   11197              :                              &c->where, &d->where);
   11198            1 :                   return;
   11199              :                 }
   11200              :             }
   11201              :         }
   11202              : 
   11203              :       /* Check F03:C815.  */
   11204         3424 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11205         2340 :           && selector_type
   11206         2340 :           && !selector_type->attr.unlimited_polymorphic
   11207         7431 :           && !gfc_type_is_extensible (c->ts.u.derived))
   11208              :         {
   11209            1 :           gfc_error ("Derived type %qs at %L must be extensible",
   11210            1 :                      c->ts.u.derived->name, &c->where);
   11211            1 :           error++;
   11212            1 :           continue;
   11213              :         }
   11214              : 
   11215              :       /* Check F03:C816.  */
   11216         5419 :       if (c->ts.type != BT_UNKNOWN
   11217         3785 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   11218         7433 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   11219         2016 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   11220              :         {
   11221            6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11222            2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   11223            2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   11224              :           else
   11225            4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   11226              :                        gfc_basic_typename (c->ts.type), &c->where);
   11227            6 :           error++;
   11228            6 :           continue;
   11229              :         }
   11230              : 
   11231              :       /* Check F03:C814.  */
   11232         5407 :       if (c->ts.type == BT_CHARACTER
   11233          736 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   11234              :         {
   11235            0 :           gfc_error ("The type-spec at %L shall specify that each length "
   11236              :                      "type parameter is assumed", &c->where);
   11237            0 :           error++;
   11238            0 :           continue;
   11239              :         }
   11240              : 
   11241              :       /* Intercept the DEFAULT case.  */
   11242         5407 :       if (c->ts.type == BT_UNKNOWN)
   11243              :         {
   11244              :           /* Check F03:C818.  */
   11245         1628 :           if (default_case)
   11246              :             {
   11247            1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   11248              :                          "by a second DEFAULT CASE at %L",
   11249            1 :                          &default_case->ext.block.case_list->where, &c->where);
   11250            1 :               error++;
   11251            1 :               continue;
   11252              :             }
   11253              : 
   11254              :           default_case = body;
   11255              :         }
   11256              :     }
   11257              : 
   11258         3026 :   if (error > 0)
   11259              :     return;
   11260              : 
   11261              :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   11262              :      target if present.  If there are any EXIT statements referring to the
   11263              :      SELECT TYPE construct, this is no problem because the gfc_code
   11264              :      reference stays the same and EXIT is equally possible from the BLOCK
   11265              :      it is changed to.  */
   11266         3023 :   code->op = EXEC_BLOCK;
   11267         3023 :   if (code->expr2)
   11268              :     {
   11269          645 :       gfc_association_list* assoc;
   11270              : 
   11271          645 :       assoc = gfc_get_association_list ();
   11272          645 :       assoc->st = code->expr1->symtree;
   11273          645 :       assoc->target = gfc_copy_expr (code->expr2);
   11274          645 :       assoc->target->where = code->expr2->where;
   11275              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11276              : 
   11277          645 :       code->ext.block.assoc = assoc;
   11278          645 :       code->expr1->symtree->n.sym->assoc = assoc;
   11279              : 
   11280          645 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11281              :     }
   11282              :   else
   11283         2378 :     code->ext.block.assoc = NULL;
   11284              : 
   11285              :   /* Ensure that the selector rank and arrayspec are available to
   11286              :      correct expressions in which they might be missing.  */
   11287         3023 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   11288              :     {
   11289          318 :       rank = code->expr2->rank;
   11290          318 :       corank = code->expr2->corank;
   11291          596 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   11292          587 :         if (ref->next == NULL)
   11293              :           break;
   11294          318 :       if (ref && ref->type == REF_ARRAY)
   11295          309 :         ref = gfc_copy_ref (ref);
   11296              : 
   11297              :       /* Fixup expr1 if necessary.  */
   11298          318 :       if (rank || corank)
   11299          318 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   11300              :     }
   11301         2705 :   else if (code->expr1->rank || code->expr1->corank)
   11302              :     {
   11303          886 :       rank = code->expr1->rank;
   11304          886 :       corank = code->expr1->corank;
   11305          886 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   11306          886 :         if (ref->next == NULL)
   11307              :           break;
   11308          886 :       if (ref && ref->type == REF_ARRAY)
   11309          886 :         ref = gfc_copy_ref (ref);
   11310              :     }
   11311              : 
   11312         3023 :   gfc_expr *orig_expr1 = code->expr1;
   11313              : 
   11314              :   /* Add EXEC_SELECT to switch on type.  */
   11315         3023 :   new_st = gfc_get_code (code->op);
   11316         3023 :   new_st->expr1 = code->expr1;
   11317         3023 :   new_st->expr2 = code->expr2;
   11318         3023 :   new_st->block = code->block;
   11319         3023 :   code->expr1 = code->expr2 =  NULL;
   11320         3023 :   code->block = NULL;
   11321         3023 :   if (!ns->code)
   11322         3023 :     ns->code = new_st;
   11323              :   else
   11324            0 :     ns->code->next = new_st;
   11325         3023 :   code = new_st;
   11326         3023 :   code->op = EXEC_SELECT_TYPE;
   11327              : 
   11328              :   /* Use the intrinsic LOC function to generate an integer expression
   11329              :      for the vtable of the selector.  Note that the rank of the selector
   11330              :      expression has to be set to zero.  */
   11331         3023 :   gfc_add_vptr_component (code->expr1);
   11332         3023 :   code->expr1->rank = 0;
   11333         3023 :   code->expr1->corank = 0;
   11334         3023 :   code->expr1 = build_loc_call (code->expr1);
   11335         3023 :   selector_expr = code->expr1->value.function.actual->expr;
   11336              : 
   11337              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11338         8422 :   for (body = code->block; body; body = body->block)
   11339              :     {
   11340         5399 :       gfc_symbol *vtab;
   11341         5399 :       c = body->ext.block.case_list;
   11342              : 
   11343              :       /* Generate an index integer expression for address of the
   11344              :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   11345              :          is stored in c->high and is used to resolve intrinsic cases.  */
   11346         5399 :       if (c->ts.type != BT_UNKNOWN)
   11347              :         {
   11348         3773 :           gfc_expr *e;
   11349         3773 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11350              :             {
   11351         2331 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   11352         2331 :               gcc_assert (vtab);
   11353         2331 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   11354         2331 :                                           c->ts.u.derived->hash_value);
   11355              :             }
   11356              :           else
   11357              :             {
   11358         1442 :               vtab = gfc_find_vtab (&c->ts);
   11359         1442 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   11360         1442 :               e = CLASS_DATA (vtab)->initializer;
   11361         1442 :               c->high = gfc_copy_expr (e);
   11362         1442 :               if (c->high->ts.kind != gfc_integer_4_kind)
   11363              :                 {
   11364            1 :                   gfc_typespec ts;
   11365            1 :                   ts.kind = gfc_integer_4_kind;
   11366            1 :                   ts.type = BT_INTEGER;
   11367            1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   11368              :                 }
   11369              :             }
   11370              : 
   11371         3773 :           e = gfc_lval_expr_from_sym (vtab);
   11372         3773 :           c->low = build_loc_call (e);
   11373              :         }
   11374              :       else
   11375         1626 :         continue;
   11376              : 
   11377              :       /* Associate temporary to selector.  This should only be done
   11378              :          when this case is actually true, so build a new ASSOCIATE
   11379              :          that does precisely this here (instead of using the
   11380              :          'global' one).  */
   11381              : 
   11382              :       /* First check the derived type import status.  */
   11383         3773 :       if (gfc_current_ns->import_state != IMPORT_NOT_SET
   11384            6 :           && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
   11385              :         {
   11386           12 :           st = gfc_find_symtree (gfc_current_ns->sym_root,
   11387            6 :                                  c->ts.u.derived->name);
   11388            6 :           if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
   11389              :                                         gfc_current_ns))
   11390            6 :             error++;
   11391              :         }
   11392              : 
   11393         3773 :       const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
   11394         3773 :       if (c->ts.type == BT_CLASS)
   11395          348 :         snprintf (name, sizeof (name), "__tmp_class_%s_%s",
   11396          348 :                   c->ts.u.derived->name, var_name);
   11397         3425 :       else if (c->ts.type == BT_DERIVED)
   11398         1983 :         snprintf (name, sizeof (name), "__tmp_type_%s_%s",
   11399         1983 :                   c->ts.u.derived->name, var_name);
   11400         1442 :       else if (c->ts.type == BT_CHARACTER)
   11401              :         {
   11402          736 :           HOST_WIDE_INT charlen = 0;
   11403          736 :           if (c->ts.u.cl && c->ts.u.cl->length
   11404            0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11405            0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11406          736 :           snprintf (name, sizeof (name),
   11407              :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
   11408              :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
   11409              :                     var_name);
   11410              :         }
   11411              :       else
   11412          706 :         snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
   11413              :                   gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
   11414              : 
   11415         3773 :       st = gfc_find_symtree (ns->sym_root, name);
   11416         3773 :       gcc_assert (st->n.sym->assoc);
   11417         3773 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11418         3773 :       st->n.sym->assoc->target->where = selector_expr->where;
   11419         3773 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   11420              :         {
   11421         3425 :           gfc_add_data_component (st->n.sym->assoc->target);
   11422              :           /* Fixup the target expression if necessary.  */
   11423         3425 :           if (rank || corank)
   11424         1383 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   11425              :                              ref);
   11426              :         }
   11427              : 
   11428         3773 :       new_st = gfc_get_code (EXEC_BLOCK);
   11429         3773 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11430         3773 :       new_st->ext.block.ns->code = body->next;
   11431         3773 :       body->next = new_st;
   11432              : 
   11433              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11434              :          there is a CASE label overlap and this is already used.  Just ignore,
   11435              :          the error is diagnosed elsewhere.  */
   11436         3773 :       if (st->n.sym->assoc->dangling)
   11437              :         {
   11438         3772 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11439         3772 :           st->n.sym->assoc->dangling = 0;
   11440              :         }
   11441              : 
   11442         3773 :       resolve_assoc_var (st->n.sym, false);
   11443              :     }
   11444              : 
   11445              :   /* Take out CLASS IS cases for separate treatment.  */
   11446              :   body = code;
   11447         8422 :   while (body && body->block)
   11448              :     {
   11449         5399 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   11450              :         {
   11451              :           /* Add to class_is list.  */
   11452          348 :           if (class_is == NULL)
   11453              :             {
   11454          317 :               class_is = body->block;
   11455          317 :               tail = class_is;
   11456              :             }
   11457              :           else
   11458              :             {
   11459           43 :               for (tail = class_is; tail->block; tail = tail->block) ;
   11460           31 :               tail->block = body->block;
   11461           31 :               tail = tail->block;
   11462              :             }
   11463              :           /* Remove from EXEC_SELECT list.  */
   11464          348 :           body->block = body->block->block;
   11465          348 :           tail->block = NULL;
   11466              :         }
   11467              :       else
   11468              :         body = body->block;
   11469              :     }
   11470              : 
   11471         3023 :   if (class_is)
   11472              :     {
   11473          317 :       gfc_symbol *vtab;
   11474              : 
   11475          317 :       if (!default_case)
   11476              :         {
   11477              :           /* Add a default case to hold the CLASS IS cases.  */
   11478          315 :           for (tail = code; tail->block; tail = tail->block) ;
   11479          207 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   11480          207 :           tail = tail->block;
   11481          207 :           tail->ext.block.case_list = gfc_get_case ();
   11482          207 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   11483          207 :           tail->next = NULL;
   11484          207 :           default_case = tail;
   11485              :         }
   11486              : 
   11487              :       /* More than one CLASS IS block?  */
   11488          317 :       if (class_is->block)
   11489              :         {
   11490           37 :           gfc_code **c1,*c2;
   11491           37 :           bool swapped;
   11492              :           /* Sort CLASS IS blocks by extension level.  */
   11493           36 :           do
   11494              :             {
   11495           37 :               swapped = false;
   11496           97 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   11497              :                 {
   11498           61 :                   c2 = (*c1)->block;
   11499              :                   /* F03:C817 (check for doubles).  */
   11500           61 :                   if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
   11501           61 :                       == c2->ext.block.case_list->ts.u.derived->hash_value)
   11502              :                     {
   11503            1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   11504              :                                  "statement at %L",
   11505              :                                  &c2->ext.block.case_list->where);
   11506            1 :                       return;
   11507              :                     }
   11508           60 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   11509           60 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   11510              :                     {
   11511              :                       /* Swap.  */
   11512           24 :                       (*c1)->block = c2->block;
   11513           24 :                       c2->block = *c1;
   11514           24 :                       *c1 = c2;
   11515           24 :                       swapped = true;
   11516              :                     }
   11517              :                 }
   11518              :             }
   11519              :           while (swapped);
   11520              :         }
   11521              : 
   11522              :       /* Generate IF chain.  */
   11523          316 :       if_st = gfc_get_code (EXEC_IF);
   11524          316 :       new_st = if_st;
   11525          662 :       for (body = class_is; body; body = body->block)
   11526              :         {
   11527          346 :           new_st->block = gfc_get_code (EXEC_IF);
   11528          346 :           new_st = new_st->block;
   11529              :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11530          346 :           new_st->expr1 = gfc_get_expr ();
   11531          346 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11532          346 :           new_st->expr1->ts.type = BT_LOGICAL;
   11533          346 :           new_st->expr1->ts.kind = 4;
   11534          346 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11535          346 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11536          346 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11537              :           /* Set up arguments.  */
   11538          346 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11539          346 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11540          346 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11541          346 :           new_st->expr1->where = code->loc;
   11542          346 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11543          346 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11544          346 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11545          346 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11546          346 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11547          346 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11548              :           /* Set up types in formal arg list.  */
   11549          346 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11550          346 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11551          346 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11552          346 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11553              : 
   11554          346 :           new_st->next = body->next;
   11555              :         }
   11556          316 :         if (default_case->next)
   11557              :           {
   11558          110 :             new_st->block = gfc_get_code (EXEC_IF);
   11559          110 :             new_st = new_st->block;
   11560          110 :             new_st->next = default_case->next;
   11561              :           }
   11562              : 
   11563              :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11564          316 :         default_case->next = if_st;
   11565              :     }
   11566              : 
   11567              :   /* Resolve the internal code.  This cannot be done earlier because
   11568              :      it requires that the sym->assoc of selectors is set already.  */
   11569         3022 :   gfc_current_ns = ns;
   11570         3022 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11571         3022 :   gfc_current_ns = old_ns;
   11572              : 
   11573         3022 :   free (ref);
   11574              : }
   11575              : 
   11576              : 
   11577              : /* Resolve a SELECT RANK statement.  */
   11578              : 
   11579              : static void
   11580         1024 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11581              : {
   11582         1024 :   gfc_namespace *ns;
   11583         1024 :   gfc_code *body, *new_st, *tail;
   11584         1024 :   gfc_case *c;
   11585         1024 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11586         1024 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11587         1024 :   gfc_symtree *st;
   11588         1024 :   gfc_expr *selector_expr = NULL;
   11589         1024 :   int case_value;
   11590         1024 :   HOST_WIDE_INT charlen = 0;
   11591              : 
   11592         1024 :   ns = code->ext.block.ns;
   11593         1024 :   gfc_resolve (ns);
   11594              : 
   11595         1024 :   code->op = EXEC_BLOCK;
   11596         1024 :   if (code->expr2)
   11597              :     {
   11598           42 :       gfc_association_list* assoc;
   11599              : 
   11600           42 :       assoc = gfc_get_association_list ();
   11601           42 :       assoc->st = code->expr1->symtree;
   11602           42 :       assoc->target = gfc_copy_expr (code->expr2);
   11603           42 :       assoc->target->where = code->expr2->where;
   11604              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11605              : 
   11606           42 :       code->ext.block.assoc = assoc;
   11607           42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11608              : 
   11609           42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11610              :     }
   11611              :   else
   11612          982 :     code->ext.block.assoc = NULL;
   11613              : 
   11614              :   /* Loop over RANK cases. Note that returning on the errors causes a
   11615              :      cascade of further errors because the case blocks do not compile
   11616              :      correctly.  */
   11617         3332 :   for (body = code->block; body; body = body->block)
   11618              :     {
   11619         2308 :       c = body->ext.block.case_list;
   11620         2308 :       if (c->low)
   11621         1389 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11622              :       else
   11623              :         case_value = -2;
   11624              : 
   11625              :       /* Check for repeated cases.  */
   11626         5842 :       for (tail = code->block; tail; tail = tail->block)
   11627              :         {
   11628         5842 :           gfc_case *d = tail->ext.block.case_list;
   11629         5842 :           int case_value2;
   11630              : 
   11631         5842 :           if (tail == body)
   11632              :             break;
   11633              : 
   11634              :           /* Check F2018: C1153.  */
   11635         3534 :           if (!c->low && !d->low)
   11636            1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11637              :                        &c->where, &d->where);
   11638              : 
   11639         3534 :           if (!c->low || !d->low)
   11640         1253 :             continue;
   11641              : 
   11642              :           /* Check F2018: C1153.  */
   11643         2281 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11644         2281 :           if ((case_value == case_value2) && case_value == -1)
   11645            1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   11646              :                        &c->where, &d->where);
   11647         2280 :           else if (case_value == case_value2)
   11648            1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   11649              :                        case_value, &c->where, &d->where);
   11650              :         }
   11651              : 
   11652         2308 :       if (!c->low)
   11653          919 :         continue;
   11654              : 
   11655              :       /* Check F2018: C1155.  */
   11656         1389 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   11657         1387 :                                || gfc_expr_attr (code->expr1).pointer))
   11658            3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   11659            3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   11660              :     }
   11661              : 
   11662              :   /* Add EXEC_SELECT to switch on rank.  */
   11663         1024 :   new_st = gfc_get_code (code->op);
   11664         1024 :   new_st->expr1 = code->expr1;
   11665         1024 :   new_st->expr2 = code->expr2;
   11666         1024 :   new_st->block = code->block;
   11667         1024 :   code->expr1 = code->expr2 =  NULL;
   11668         1024 :   code->block = NULL;
   11669         1024 :   if (!ns->code)
   11670         1024 :     ns->code = new_st;
   11671              :   else
   11672            0 :     ns->code->next = new_st;
   11673         1024 :   code = new_st;
   11674         1024 :   code->op = EXEC_SELECT_RANK;
   11675              : 
   11676         1024 :   selector_expr = code->expr1;
   11677              : 
   11678              :   /* Loop over SELECT RANK cases.  */
   11679         3332 :   for (body = code->block; body; body = body->block)
   11680              :     {
   11681         2308 :       c = body->ext.block.case_list;
   11682         2308 :       int case_value;
   11683              : 
   11684              :       /* Pass on the default case.  */
   11685         2308 :       if (c->low == NULL)
   11686          919 :         continue;
   11687              : 
   11688              :       /* Associate temporary to selector.  This should only be done
   11689              :          when this case is actually true, so build a new ASSOCIATE
   11690              :          that does precisely this here (instead of using the
   11691              :          'global' one).  */
   11692         1389 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   11693          265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11694          186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11695              : 
   11696         1389 :       if (c->ts.type == BT_CLASS)
   11697          145 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   11698         1244 :       else if (c->ts.type == BT_DERIVED)
   11699          110 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   11700         1134 :       else if (c->ts.type != BT_CHARACTER)
   11701          575 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   11702              :       else
   11703          559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   11704              :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   11705              : 
   11706         1389 :       case_value = (int) mpz_get_si (c->low->value.integer);
   11707         1389 :       if (case_value >= 0)
   11708         1356 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   11709              :       else
   11710           33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   11711              : 
   11712         1389 :       st = gfc_find_symtree (ns->sym_root, name);
   11713         1389 :       gcc_assert (st->n.sym->assoc);
   11714              : 
   11715         1389 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11716         1389 :       st->n.sym->assoc->target->where = selector_expr->where;
   11717              : 
   11718         1389 :       new_st = gfc_get_code (EXEC_BLOCK);
   11719         1389 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11720         1389 :       new_st->ext.block.ns->code = body->next;
   11721         1389 :       body->next = new_st;
   11722              : 
   11723              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11724              :          there is a CASE label overlap and this is already used.  Just ignore,
   11725              :          the error is diagnosed elsewhere.  */
   11726         1389 :       if (st->n.sym->assoc->dangling)
   11727              :         {
   11728         1387 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11729         1387 :           st->n.sym->assoc->dangling = 0;
   11730              :         }
   11731              : 
   11732         1389 :       resolve_assoc_var (st->n.sym, false);
   11733              :     }
   11734              : 
   11735         1024 :   gfc_current_ns = ns;
   11736         1024 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11737         1024 :   gfc_current_ns = old_ns;
   11738         1024 : }
   11739              : 
   11740              : 
   11741              : /* Resolve a transfer statement. This is making sure that:
   11742              :    -- a derived type being transferred has only non-pointer components
   11743              :    -- a derived type being transferred doesn't have private components, unless
   11744              :       it's being transferred from the module where the type was defined
   11745              :    -- we're not trying to transfer a whole assumed size array.  */
   11746              : 
   11747              : static void
   11748        47323 : resolve_transfer (gfc_code *code)
   11749              : {
   11750        47323 :   gfc_symbol *sym, *derived;
   11751        47323 :   gfc_ref *ref;
   11752        47323 :   gfc_expr *exp;
   11753        47323 :   bool write = false;
   11754        47323 :   bool formatted = false;
   11755        47323 :   gfc_dt *dt = code->ext.dt;
   11756        47323 :   gfc_symbol *dtio_sub = NULL;
   11757              : 
   11758        47323 :   exp = code->expr1;
   11759              : 
   11760        94652 :   while (exp != NULL && exp->expr_type == EXPR_OP
   11761        48256 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   11762            6 :     exp = exp->value.op.op1;
   11763              : 
   11764        47323 :   if (exp && exp->expr_type == EXPR_NULL
   11765            2 :       && code->ext.dt)
   11766              :     {
   11767            2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   11768              :                  &exp->where);
   11769            2 :       return;
   11770              :     }
   11771              : 
   11772              :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   11773              :                       && exp->expr_type != EXPR_FUNCTION
   11774              :                       && exp->expr_type != EXPR_ARRAY
   11775              :                       && exp->expr_type != EXPR_STRUCTURE))
   11776              :     return;
   11777              : 
   11778        26195 :   if (dt && dt->dt_io_kind->value.iokind == M_READ)
   11779              :     {
   11780              :       /* If we are reading, the variable will be changed.  Note that
   11781              :          code->ext.dt may be NULL if the TRANSFER is related to an INQUIRE
   11782              :          statement -- but in this case, we are not reading, either.  */
   11783         7470 :       if (!gfc_check_vardef_context (exp, false, false, false,
   11784         7470 :                                      _("item in READ")))
   11785              :         return;
   11786              : 
   11787         7466 :       gfc_expr_set_at (exp, &exp->where, VALUE_READ);
   11788              :     }
   11789              : 
   11790        26191 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   11791        26191 :                         || exp->expr_type == EXPR_FUNCTION
   11792        21800 :                         || exp->expr_type == EXPR_ARRAY
   11793        47991 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   11794              : 
   11795              :   /* Go to actual component transferred.  */
   11796        33972 :   for (ref = exp->ref; ref; ref = ref->next)
   11797         7781 :     if (ref->type == REF_COMPONENT)
   11798         2210 :       ts = &ref->u.c.component->ts;
   11799              : 
   11800        26191 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   11801        26043 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   11802              :     {
   11803          720 :       derived = ts->u.derived;
   11804              : 
   11805              :       /* Determine when to use the formatted DTIO procedure.  */
   11806          720 :       if (dt && (dt->format_expr || dt->format_label))
   11807          645 :         formatted = true;
   11808              : 
   11809          720 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   11810          720 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   11811          720 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   11812              : 
   11813          720 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   11814              :         {
   11815          450 :           dt->udtio = exp;
   11816          450 :           sym = exp->symtree->n.sym->ns->proc_name;
   11817              :           /* Check to see if this is a nested DTIO call, with the
   11818              :              dummy as the io-list object.  */
   11819          450 :           if (sym && sym == dtio_sub && sym->formal
   11820           30 :               && sym->formal->sym == exp->symtree->n.sym
   11821           30 :               && exp->ref == NULL)
   11822              :             {
   11823            0 :               if (!sym->attr.recursive)
   11824              :                 {
   11825            0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   11826              :                              sym->name, &sym->declared_at);
   11827            0 :                   return;
   11828              :                 }
   11829              :             }
   11830              :         }
   11831              :     }
   11832              : 
   11833        26191 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   11834              :     {
   11835            3 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   11836              :                 "it is processed by a defined input/output procedure",
   11837              :                 &code->loc);
   11838            3 :       return;
   11839              :     }
   11840              : 
   11841        26188 :   if (ts->type == BT_DERIVED)
   11842              :     {
   11843              :       /* Check that transferred derived type doesn't contain POINTER
   11844              :          components unless it is processed by a defined input/output
   11845              :          procedure".  */
   11846          688 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   11847              :         {
   11848            2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   11849              :                      "components unless it is processed by a defined "
   11850              :                      "input/output procedure", &code->loc);
   11851            2 :           return;
   11852              :         }
   11853              : 
   11854              :       /* F08:C935.  */
   11855          686 :       if (ts->u.derived->attr.proc_pointer_comp)
   11856              :         {
   11857            2 :           gfc_error ("Data transfer element at %L cannot have "
   11858              :                      "procedure pointer components", &code->loc);
   11859            2 :           return;
   11860              :         }
   11861              : 
   11862          684 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   11863              :         {
   11864            6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   11865              :                      "components unless it is processed by a defined "
   11866              :                      "input/output procedure", &code->loc);
   11867            6 :           return;
   11868              :         }
   11869              : 
   11870              :       /* C_PTR and C_FUNPTR have private components which means they cannot
   11871              :          be printed.  However, if -std=gnu and not -pedantic, allow
   11872              :          the component to be printed to help debugging.  */
   11873          678 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   11874              :         {
   11875            4 :           gfc_error ("Data transfer element at %L "
   11876              :                      "cannot have PRIVATE components", &code->loc);
   11877            4 :             return;
   11878              :         }
   11879          674 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   11880              :         {
   11881            4 :           gfc_error ("Data transfer element at %L cannot have "
   11882              :                      "PRIVATE components unless it is processed by "
   11883              :                      "a defined input/output procedure", &code->loc);
   11884            4 :           return;
   11885              :         }
   11886              :     }
   11887              : 
   11888        26170 :   if (exp->expr_type == EXPR_STRUCTURE)
   11889              :     return;
   11890              : 
   11891        26125 :   if (exp->expr_type == EXPR_ARRAY)
   11892              :     return;
   11893              : 
   11894        25749 :   sym = exp->symtree->n.sym;
   11895              : 
   11896        25749 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   11897           81 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   11898              :     {
   11899            1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   11900              :                  "an assumed-size array", &code->loc);
   11901            1 :       return;
   11902              :     }
   11903              : 
   11904        25748 :   if (dt && (dt->dt_io_kind->value.iokind == M_WRITE
   11905        25600 :              || dt->dt_io_kind->value.iokind == M_PRINT))
   11906        18135 :     gfc_value_used_expr (exp, VALUE_USED);
   11907              : 
   11908              : }
   11909              : 
   11910              : 
   11911              : /*********** Toplevel code resolution subroutines ***********/
   11912              : 
   11913              : /* Find the set of labels that are reachable from this block.  We also
   11914              :    record the last statement in each block.  */
   11915              : 
   11916              : static void
   11917       682643 : find_reachable_labels (gfc_code *block)
   11918              : {
   11919       682643 :   gfc_code *c;
   11920              : 
   11921       682643 :   if (!block)
   11922              :     return;
   11923              : 
   11924       427497 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   11925              : 
   11926              :   /* Collect labels in this block.  We don't keep those corresponding
   11927              :      to END {IF|SELECT}, these are checked in resolve_branch by going
   11928              :      up through the code_stack.  */
   11929      1567887 :   for (c = block; c; c = c->next)
   11930              :     {
   11931      1140390 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   11932         3661 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   11933              :     }
   11934              : 
   11935              :   /* Merge with labels from parent block.  */
   11936       427497 :   if (cs_base->prev)
   11937              :     {
   11938       351009 :       gcc_assert (cs_base->prev->reachable_labels);
   11939       351009 :       bitmap_ior_into (cs_base->reachable_labels,
   11940              :                        cs_base->prev->reachable_labels);
   11941              :     }
   11942              : }
   11943              : 
   11944              : static void
   11945          197 : resolve_lock_unlock_event (gfc_code *code)
   11946              : {
   11947          197 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   11948          197 :       && (code->expr1->ts.type != BT_DERIVED
   11949          137 :           || code->expr1->expr_type != EXPR_VARIABLE
   11950          137 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11951          136 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   11952          136 :           || code->expr1->rank != 0
   11953          181 :           || (!gfc_is_coarray (code->expr1) &&
   11954           46 :               !gfc_is_coindexed (code->expr1))))
   11955            4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   11956            4 :                &code->expr1->where);
   11957          193 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   11958           58 :            && (code->expr1->ts.type != BT_DERIVED
   11959           58 :                || code->expr1->expr_type != EXPR_VARIABLE
   11960           58 :                || code->expr1->ts.u.derived->from_intmod
   11961              :                   != INTMOD_ISO_FORTRAN_ENV
   11962           58 :                || code->expr1->ts.u.derived->intmod_sym_id
   11963              :                   != ISOFORTRAN_EVENT_TYPE
   11964           58 :                || code->expr1->rank != 0))
   11965            0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   11966              :                &code->expr1->where);
   11967           34 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   11968          209 :            && !gfc_is_coindexed (code->expr1))
   11969            0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   11970            0 :                &code->expr1->where);
   11971          193 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   11972            0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   11973            0 :                "coindexed", &code->expr1->where);
   11974              : 
   11975              :   /* Check STAT.  */
   11976          197 :   if (code->expr2
   11977           54 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   11978           54 :           || code->expr2->expr_type != EXPR_VARIABLE))
   11979            0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11980              :                &code->expr2->where);
   11981              : 
   11982          197 :   if (code->expr2
   11983          251 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   11984           54 :                                     _("STAT variable")))
   11985              :     return;
   11986              : 
   11987              :   /* Check ERRMSG.  */
   11988          197 :   if (code->expr3
   11989            2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   11990            2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   11991            0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   11992              :                &code->expr3->where);
   11993              : 
   11994          197 :   if (code->expr3
   11995          199 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   11996            2 :                                     _("ERRMSG variable")))
   11997              :     return;
   11998              : 
   11999              :   /* Check for LOCK the ACQUIRED_LOCK.  */
   12000          197 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12001           22 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   12002           22 :           || code->expr4->expr_type != EXPR_VARIABLE))
   12003            0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   12004              :                "variable", &code->expr4->where);
   12005              : 
   12006          173 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12007          219 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   12008           22 :                                     _("ACQUIRED_LOCK variable")))
   12009              :     return;
   12010              : 
   12011              :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   12012          197 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   12013              :     {
   12014           36 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   12015           36 :           || code->expr4->rank != 0)
   12016            0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   12017            0 :                    "expression", &code->expr4->where);
   12018              :     }
   12019              : }
   12020              : 
   12021              : static void
   12022          246 : resolve_team_argument (gfc_expr *team)
   12023              : {
   12024          246 :   gfc_resolve_expr (team);
   12025          246 :   if (team->rank != 0 || team->ts.type != BT_DERIVED
   12026          239 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   12027          239 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   12028              :     {
   12029            7 :       gfc_error ("TEAM argument at %L must be a scalar expression "
   12030              :                  "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
   12031              :                  &team->where);
   12032              :     }
   12033          246 : }
   12034              : 
   12035              : static void
   12036         1358 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
   12037              :                                 gfc_expr *e)
   12038              : {
   12039         1358 :   gfc_resolve_expr (e);
   12040         1358 :   if (e
   12041          139 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
   12042          124 :           || e->expr_type != EXPR_VARIABLE))
   12043           15 :     gfc_error ("%s argument at %L must be a scalar %s variable of at least "
   12044              :                "kind %d", name, &e->where, gfc_basic_typename (exp_type),
   12045              :                exp_kind);
   12046         1358 : }
   12047              : 
   12048              : void
   12049          679 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
   12050              : {
   12051          679 :   resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
   12052          679 :   resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
   12053              :                                   gfc_default_character_kind,
   12054              :                                   sync_stat->errmsg);
   12055          679 : }
   12056              : 
   12057              : static void
   12058          260 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
   12059              :                          gfc_expr *e)
   12060              : {
   12061          260 :   gfc_resolve_expr (e);
   12062          260 :   if (e
   12063          161 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
   12064            3 :     gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
   12065              :                name, &e->where, gfc_basic_typename (exp_type), exp_kind);
   12066          260 : }
   12067              : 
   12068              : static void
   12069          130 : resolve_form_team (gfc_code *code)
   12070              : {
   12071          130 :   resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
   12072              :                            code->expr1);
   12073          130 :   resolve_team_argument (code->expr2);
   12074          130 :   resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
   12075              :                            code->expr3);
   12076          130 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12077          130 : }
   12078              : 
   12079              : static void resolve_block_construct (gfc_code *);
   12080              : 
   12081              : static void
   12082           73 : resolve_change_team (gfc_code *code)
   12083              : {
   12084           73 :   resolve_team_argument (code->expr1);
   12085           73 :   gfc_resolve_sync_stat (&code->ext.block.sync_stat);
   12086          146 :   resolve_block_construct (code);
   12087              :   /* Map the coarray bounds as selected.  */
   12088           76 :   for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
   12089            3 :     if (a->ar)
   12090              :       {
   12091            3 :         gfc_array_spec *src = a->ar->as, *dst;
   12092            3 :         if (a->st->n.sym->ts.type == BT_CLASS)
   12093            0 :           dst = CLASS_DATA (a->st->n.sym)->as;
   12094              :         else
   12095            3 :           dst = a->st->n.sym->as;
   12096            3 :         dst->corank = src->corank;
   12097            3 :         dst->cotype = src->cotype;
   12098            6 :         for (int i = 0; i < src->corank; ++i)
   12099              :           {
   12100            3 :             dst->lower[dst->rank + i] = src->lower[i];
   12101            3 :             dst->upper[dst->rank + i] = src->upper[i];
   12102            3 :             src->lower[i] = src->upper[i] = nullptr;
   12103              :           }
   12104            3 :         gfc_free_array_spec (src);
   12105            3 :         free (a->ar);
   12106            3 :         a->ar = nullptr;
   12107            3 :         dst->resolved = false;
   12108            3 :         gfc_resolve_array_spec (dst, 0);
   12109              :       }
   12110           73 : }
   12111              : 
   12112              : static void
   12113           43 : resolve_sync_team (gfc_code *code)
   12114              : {
   12115           43 :   resolve_team_argument (code->expr1);
   12116           43 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12117           43 : }
   12118              : 
   12119              : static void
   12120           71 : resolve_end_team (gfc_code *code)
   12121              : {
   12122           71 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12123           71 : }
   12124              : 
   12125              : static void
   12126           54 : resolve_critical (gfc_code *code)
   12127              : {
   12128           54 :   gfc_symtree *symtree;
   12129           54 :   gfc_symbol *lock_type;
   12130           54 :   char name[GFC_MAX_SYMBOL_LEN];
   12131           54 :   static int serial = 0;
   12132              : 
   12133           54 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12134              : 
   12135           54 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   12136           30 :     return;
   12137              : 
   12138           24 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   12139              :                               GFC_PREFIX ("lock_type"));
   12140           24 :   if (symtree)
   12141           12 :     lock_type = symtree->n.sym;
   12142              :   else
   12143              :     {
   12144           12 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   12145              :                             false) != 0)
   12146            0 :         gcc_unreachable ();
   12147           12 :       lock_type = symtree->n.sym;
   12148           12 :       lock_type->attr.flavor = FL_DERIVED;
   12149           12 :       lock_type->attr.zero_comp = 1;
   12150           12 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   12151           12 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   12152              :     }
   12153              : 
   12154           24 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   12155           24 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   12156            0 :     gcc_unreachable ();
   12157              : 
   12158           24 :   code->resolved_sym = symtree->n.sym;
   12159           24 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   12160           24 :   symtree->n.sym->attr.referenced = 1;
   12161           24 :   symtree->n.sym->attr.artificial = 1;
   12162           24 :   symtree->n.sym->attr.codimension = 1;
   12163           24 :   symtree->n.sym->ts.type = BT_DERIVED;
   12164           24 :   symtree->n.sym->ts.u.derived = lock_type;
   12165           24 :   symtree->n.sym->as = gfc_get_array_spec ();
   12166           24 :   symtree->n.sym->as->corank = 1;
   12167           24 :   symtree->n.sym->as->type = AS_EXPLICIT;
   12168           24 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   12169           24 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   12170              :                                                    NULL, 1);
   12171           24 :   gfc_commit_symbols();
   12172              : }
   12173              : 
   12174              : 
   12175              : static void
   12176         1317 : resolve_sync (gfc_code *code)
   12177              : {
   12178              :   /* Check imageset. The * case matches expr1 == NULL.  */
   12179         1317 :   if (code->expr1)
   12180              :     {
   12181           71 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   12182            1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   12183              :                    "INTEGER expression", &code->expr1->where);
   12184           71 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   12185           27 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   12186            1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   12187              :                    &code->expr1->where);
   12188           70 :       else if (code->expr1->expr_type == EXPR_ARRAY
   12189           70 :                && gfc_simplify_expr (code->expr1, 0))
   12190              :         {
   12191           20 :            gfc_constructor *cons;
   12192           20 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   12193           60 :            for (; cons; cons = gfc_constructor_next (cons))
   12194           20 :              if (cons->expr->expr_type == EXPR_CONSTANT
   12195           20 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   12196            0 :                gfc_error ("Imageset argument at %L must between 1 and "
   12197              :                           "num_images()", &cons->expr->where);
   12198              :         }
   12199              :     }
   12200              : 
   12201              :   /* Check STAT.  */
   12202         1317 :   gfc_resolve_expr (code->expr2);
   12203         1317 :   if (code->expr2)
   12204              :     {
   12205          108 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   12206            1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12207              :                    &code->expr2->where);
   12208              :       else
   12209          107 :         gfc_check_vardef_context (code->expr2, false, false, false,
   12210          107 :                                   _("STAT variable"));
   12211              :     }
   12212              : 
   12213              :   /* Check ERRMSG.  */
   12214         1317 :   gfc_resolve_expr (code->expr3);
   12215         1317 :   if (code->expr3)
   12216              :     {
   12217           90 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   12218            4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12219              :                    &code->expr3->where);
   12220              :       else
   12221           86 :         gfc_check_vardef_context (code->expr3, false, false, false,
   12222           86 :                                   _("ERRMSG variable"));
   12223              :     }
   12224         1317 : }
   12225              : 
   12226              : 
   12227              : /* Given a branch to a label, see if the branch is conforming.
   12228              :    The code node describes where the branch is located.  */
   12229              : 
   12230              : static void
   12231       110980 : resolve_branch (gfc_st_label *label, gfc_code *code)
   12232              : {
   12233       110980 :   code_stack *stack;
   12234              : 
   12235       110980 :   if (label == NULL)
   12236              :     return;
   12237              : 
   12238              :   /* Step one: is this a valid branching target?  */
   12239              : 
   12240         2460 :   if (label->defined == ST_LABEL_UNKNOWN)
   12241              :     {
   12242            4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   12243              :                  &code->loc);
   12244            4 :       return;
   12245              :     }
   12246              : 
   12247         2456 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   12248              :     {
   12249            4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   12250              :                  "for the branch statement at %L", &label->where, &code->loc);
   12251            4 :       return;
   12252              :     }
   12253              : 
   12254              :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   12255              : 
   12256         2452 :   if (code->here == label)
   12257              :     {
   12258            0 :       gfc_warning (0, "Branch at %L may result in an infinite loop",
   12259              :                    &code->loc);
   12260            0 :       return;
   12261              :     }
   12262              : 
   12263              :   /* Step three:  See if the label is in the same block as the
   12264              :      branching statement.  The hard work has been done by setting up
   12265              :      the bitmap reachable_labels.  */
   12266              : 
   12267         2452 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   12268              :     {
   12269              :       /* Check now whether there is a CRITICAL construct; if so, check
   12270              :          whether the label is still visible outside of the CRITICAL block,
   12271              :          which is invalid.  */
   12272         6267 :       for (stack = cs_base; stack; stack = stack->prev)
   12273              :         {
   12274         3883 :           if (stack->current->op == EXEC_CRITICAL
   12275         3883 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   12276            2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   12277              :                       "label at %L", &code->loc, &label->where);
   12278         3881 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   12279         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12280            0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   12281              :                       "for label at %L", &code->loc, &label->where);
   12282         3881 :           else if (stack->current->op == EXEC_CHANGE_TEAM
   12283         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12284            1 :             gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
   12285              :                       "for label at %L", &code->loc, &label->where);
   12286              :         }
   12287              : 
   12288              :       return;
   12289              :     }
   12290              : 
   12291              :   /* Step four:  If we haven't found the label in the bitmap, it may
   12292              :     still be the label of the END of the enclosing block, in which
   12293              :     case we find it by going up the code_stack.  */
   12294              : 
   12295          167 :   for (stack = cs_base; stack; stack = stack->prev)
   12296              :     {
   12297          131 :       if (stack->current->next && stack->current->next->here == label)
   12298              :         break;
   12299          101 :       if (stack->current->op == EXEC_CRITICAL)
   12300              :         {
   12301              :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   12302              :              construct as END CRITICAL is still part of it.  */
   12303            2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   12304              :                       " at %L", &code->loc, &label->where);
   12305            2 :           return;
   12306              :         }
   12307           99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   12308              :         {
   12309            0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   12310              :                      "label at %L", &code->loc, &label->where);
   12311            0 :           return;
   12312              :         }
   12313              :     }
   12314              : 
   12315           66 :   if (stack)
   12316              :     {
   12317           30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   12318              :       return;
   12319              :     }
   12320              : 
   12321              :   /* The label is not in an enclosing block, so illegal.  This was
   12322              :      allowed in Fortran 66, so we allow it as extension.  No
   12323              :      further checks are necessary in this case.  */
   12324           36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   12325              :                   "as the GOTO statement at %L", &label->where,
   12326              :                   &code->loc);
   12327           36 :   return;
   12328              : }
   12329              : 
   12330              : 
   12331              : /* Check whether EXPR1 has the same shape as EXPR2.  */
   12332              : 
   12333              : static bool
   12334         1467 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   12335              : {
   12336         1467 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   12337         1467 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   12338         1467 :   bool result = false;
   12339         1467 :   int i;
   12340              : 
   12341              :   /* Compare the rank.  */
   12342         1467 :   if (expr1->rank != expr2->rank)
   12343              :     return result;
   12344              : 
   12345              :   /* Compare the size of each dimension.  */
   12346         2811 :   for (i=0; i<expr1->rank; i++)
   12347              :     {
   12348         1495 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   12349          151 :         goto ignore;
   12350              : 
   12351         1344 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   12352            0 :         goto ignore;
   12353              : 
   12354         1344 :       if (mpz_cmp (shape[i], shape2[i]))
   12355            0 :         goto over;
   12356              :     }
   12357              : 
   12358              :   /* When either of the two expression is an assumed size array, we
   12359              :      ignore the comparison of dimension sizes.  */
   12360         1316 : ignore:
   12361              :   result = true;
   12362              : 
   12363         1467 : over:
   12364         1467 :   gfc_clear_shape (shape, i);
   12365         1467 :   gfc_clear_shape (shape2, i);
   12366         1467 :   return result;
   12367              : }
   12368              : 
   12369              : 
   12370              : /* Check whether a WHERE assignment target or a WHERE mask expression
   12371              :    has the same shape as the outermost WHERE mask expression.  */
   12372              : 
   12373              : static void
   12374          509 : resolve_where (gfc_code *code, gfc_expr *mask)
   12375              : {
   12376          509 :   gfc_code *cblock;
   12377          509 :   gfc_code *cnext;
   12378          509 :   gfc_expr *e = NULL;
   12379              : 
   12380          509 :   cblock = code->block;
   12381              : 
   12382              :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   12383              :      In case of nested WHERE, only the outermost one is stored.  */
   12384          509 :   if (mask == NULL) /* outermost WHERE */
   12385          453 :     e = cblock->expr1;
   12386              :   else /* inner WHERE */
   12387          509 :     e = mask;
   12388              : 
   12389         1387 :   while (cblock)
   12390              :     {
   12391          878 :       if (cblock->expr1)
   12392              :         {
   12393              :           /* Check if the mask-expr has a consistent shape with the
   12394              :              outermost WHERE mask-expr.  */
   12395          714 :           if (!resolve_where_shape (cblock->expr1, e))
   12396            0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   12397            0 :                        &cblock->expr1->where);
   12398              :          }
   12399              : 
   12400              :       /* the assignment statement of a WHERE statement, or the first
   12401              :          statement in where-body-construct of a WHERE construct */
   12402          878 :       cnext = cblock->next;
   12403         1733 :       while (cnext)
   12404              :         {
   12405          855 :           switch (cnext->op)
   12406              :             {
   12407              :             /* WHERE assignment statement */
   12408          753 :             case EXEC_ASSIGN:
   12409              : 
   12410              :               /* Check shape consistent for WHERE assignment target.  */
   12411          753 :               if (e && !resolve_where_shape (cnext->expr1, e))
   12412            0 :                gfc_error ("WHERE assignment target at %L has "
   12413            0 :                           "inconsistent shape", &cnext->expr1->where);
   12414              : 
   12415          753 :               if (cnext->op == EXEC_ASSIGN
   12416          753 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12417            0 :                 cnext->expr1->must_finalize = 1;
   12418              : 
   12419              :               break;
   12420              : 
   12421              : 
   12422           46 :             case EXEC_ASSIGN_CALL:
   12423           46 :               resolve_call (cnext);
   12424           46 :               if (!cnext->resolved_sym->attr.elemental)
   12425            2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12426            2 :                           &cnext->ext.actual->expr->where);
   12427              :               break;
   12428              : 
   12429              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12430           56 :             case EXEC_WHERE:
   12431           56 :               resolve_where (cnext, e);
   12432           56 :               break;
   12433              : 
   12434            0 :             default:
   12435            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12436              :                          &cnext->loc);
   12437              :             }
   12438              :          /* the next statement within the same where-body-construct */
   12439          855 :          cnext = cnext->next;
   12440              :        }
   12441              :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12442          878 :     cblock = cblock->block;
   12443              :   }
   12444          509 : }
   12445              : 
   12446              : 
   12447              : /* Resolve assignment in FORALL construct.
   12448              :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   12449              :    FORALL index variables.  */
   12450              : 
   12451              : static void
   12452         2376 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   12453              : {
   12454         2376 :   int n;
   12455         2376 :   gfc_symbol *forall_index;
   12456              : 
   12457         6774 :   for (n = 0; n < nvar; n++)
   12458              :     {
   12459         4398 :       forall_index = var_expr[n]->symtree->n.sym;
   12460              : 
   12461              :       /* Check whether the assignment target is one of the FORALL index
   12462              :          variable.  */
   12463         4398 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   12464         4398 :           && (code->expr1->symtree->n.sym == forall_index))
   12465            0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   12466              :                    &code->expr1->where);
   12467              :       else
   12468              :         {
   12469              :           /* If one of the FORALL index variables doesn't appear in the
   12470              :              assignment variable, then there could be a many-to-one
   12471              :              assignment.  Emit a warning rather than an error because the
   12472              :              mask could be resolving this problem.
   12473              :              DO NOT emit this warning for DO CONCURRENT - reduction-like
   12474              :              many-to-one assignments are semantically valid (formalized with
   12475              :              the REDUCE locality-spec in Fortran 2023).  */
   12476         4398 :           if (!find_forall_index (code->expr1, forall_index, 0)
   12477         4398 :               && !gfc_do_concurrent_flag)
   12478            0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   12479              :                          "left side of the assignment at %L and so might "
   12480              :                          "cause multiple assignment to this object",
   12481            0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   12482              :         }
   12483              :     }
   12484         2376 : }
   12485              : 
   12486              : 
   12487              : /* Resolve WHERE statement in FORALL construct.  */
   12488              : 
   12489              : static void
   12490           47 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   12491              :                                   gfc_expr **var_expr)
   12492              : {
   12493           47 :   gfc_code *cblock;
   12494           47 :   gfc_code *cnext;
   12495              : 
   12496           47 :   cblock = code->block;
   12497          113 :   while (cblock)
   12498              :     {
   12499              :       /* the assignment statement of a WHERE statement, or the first
   12500              :          statement in where-body-construct of a WHERE construct */
   12501           66 :       cnext = cblock->next;
   12502          132 :       while (cnext)
   12503              :         {
   12504           66 :           switch (cnext->op)
   12505              :             {
   12506              :             /* WHERE assignment statement */
   12507           66 :             case EXEC_ASSIGN:
   12508           66 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   12509              : 
   12510           66 :               if (cnext->op == EXEC_ASSIGN
   12511           66 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12512            0 :                 cnext->expr1->must_finalize = 1;
   12513              : 
   12514              :               break;
   12515              : 
   12516              :             /* WHERE operator assignment statement */
   12517            0 :             case EXEC_ASSIGN_CALL:
   12518            0 :               resolve_call (cnext);
   12519            0 :               if (!cnext->resolved_sym->attr.elemental)
   12520            0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12521            0 :                           &cnext->ext.actual->expr->where);
   12522              :               break;
   12523              : 
   12524              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12525            0 :             case EXEC_WHERE:
   12526            0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   12527            0 :               break;
   12528              : 
   12529            0 :             default:
   12530            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12531              :                          &cnext->loc);
   12532              :             }
   12533              :           /* the next statement within the same where-body-construct */
   12534           66 :           cnext = cnext->next;
   12535              :         }
   12536              :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12537           66 :       cblock = cblock->block;
   12538              :     }
   12539           47 : }
   12540              : 
   12541              : 
   12542              : /* Traverse the FORALL body to check whether the following errors exist:
   12543              :    1. For assignment, check if a many-to-one assignment happens.
   12544              :    2. For WHERE statement, check the WHERE body to see if there is any
   12545              :       many-to-one assignment.  */
   12546              : 
   12547              : static void
   12548         2217 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   12549              : {
   12550         2217 :   gfc_code *c;
   12551              : 
   12552         2217 :   c = code->block->next;
   12553         4856 :   while (c)
   12554              :     {
   12555         2639 :       switch (c->op)
   12556              :         {
   12557         2310 :         case EXEC_ASSIGN:
   12558         2310 :         case EXEC_POINTER_ASSIGN:
   12559         2310 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   12560              : 
   12561         2310 :           if (c->op == EXEC_ASSIGN
   12562         2310 :               && gfc_may_be_finalized (c->expr1->ts))
   12563            0 :             c->expr1->must_finalize = 1;
   12564              : 
   12565              :           break;
   12566              : 
   12567            0 :         case EXEC_ASSIGN_CALL:
   12568            0 :           resolve_call (c);
   12569            0 :           break;
   12570              : 
   12571              :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   12572              :            there is no need to handle it here.  */
   12573              :         case EXEC_FORALL:
   12574              :           break;
   12575           47 :         case EXEC_WHERE:
   12576           47 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   12577           47 :           break;
   12578              :         default:
   12579              :           break;
   12580              :         }
   12581              :       /* The next statement in the FORALL body.  */
   12582         2639 :       c = c->next;
   12583              :     }
   12584         2217 : }
   12585              : 
   12586              : 
   12587              : /* Counts the number of iterators needed inside a forall construct, including
   12588              :    nested forall constructs. This is used to allocate the needed memory
   12589              :    in gfc_resolve_forall.  */
   12590              : 
   12591              : static int gfc_count_forall_iterators (gfc_code *code);
   12592              : 
   12593              : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
   12594              :    next-chain, descending into block arms such as IF/ELSE branches.  */
   12595              : 
   12596              : static int
   12597         2415 : gfc_max_forall_iterators_in_chain (gfc_code *code)
   12598              : {
   12599         2415 :   int max_iters = 0;
   12600              : 
   12601         5281 :   for (gfc_code *c = code; c; c = c->next)
   12602              :     {
   12603         2866 :       int sub_iters = 0;
   12604              : 
   12605         2866 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   12606           94 :         sub_iters = gfc_count_forall_iterators (c);
   12607         2772 :       else if (c->op == EXEC_BLOCK)
   12608              :         {
   12609              :           /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
   12610              :              not in the generic c->block arm list used by IF/SELECT.  */
   12611           34 :           if (c->ext.block.ns && c->ext.block.ns->code)
   12612           34 :             sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
   12613              :         }
   12614         2738 :       else if (c->block)
   12615          307 :         for (gfc_code *b = c->block; b; b = b->block)
   12616              :           {
   12617          164 :             int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
   12618          164 :             if (arm_iters > sub_iters)
   12619              :               sub_iters = arm_iters;
   12620              :           }
   12621              : 
   12622         2866 :       if (sub_iters > max_iters)
   12623              :         max_iters = sub_iters;
   12624              :     }
   12625              : 
   12626         2415 :   return max_iters;
   12627              : }
   12628              : 
   12629              : 
   12630              : static int
   12631         2217 : gfc_count_forall_iterators (gfc_code *code)
   12632              : {
   12633         2217 :   int current_iters = 0;
   12634         2217 :   gfc_forall_iterator *fa;
   12635              : 
   12636         2217 :   gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
   12637              : 
   12638         6352 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12639         4135 :     current_iters++;
   12640              : 
   12641         2217 :   return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
   12642              : }
   12643              : 
   12644              : 
   12645              : /* Given a FORALL construct.
   12646              :    1) Resolve the FORALL iterator.
   12647              :    2) Check for shadow index-name(s) and update code block.
   12648              :    3) call gfc_resolve_forall_body to resolve the FORALL body.  */
   12649              : 
   12650              : /* Custom recursive expression walker that replaces symbols.
   12651              :    Visits all expressions including array subscripts.  Also called from
   12652              :    replace_in_code_recursive to handle ASSOCIATE selector expressions.  */
   12653              : 
   12654              : static void
   12655          192 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym, gfc_symtree *new_st)
   12656              : {
   12657          228 :   if (!expr)
   12658              :     return;
   12659              : 
   12660              :   /* Check if this is a variable reference to replace */
   12661          144 :   if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
   12662              :     {
   12663           30 :       expr->symtree = new_st;
   12664           30 :       expr->ts = new_st->n.sym->ts;
   12665              :     }
   12666              : 
   12667              :   /* Walk through reference chain (array subscripts, substrings, etc.) */
   12668          150 :   for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
   12669              :     {
   12670            6 :       if (ref->type == REF_ARRAY)
   12671              :         {
   12672              :           gfc_array_ref *ar = &ref->u.ar;
   12673           12 :           for (int i = 0; i < ar->dimen; i++)
   12674              :             {
   12675            6 :               replace_in_expr_recursive (ar->start[i], old_sym, new_st);
   12676            6 :               replace_in_expr_recursive (ar->end[i], old_sym, new_st);
   12677            6 :               replace_in_expr_recursive (ar->stride[i], old_sym, new_st);
   12678              :             }
   12679              :         }
   12680            0 :       else if (ref->type == REF_SUBSTRING)
   12681              :         {
   12682            0 :           replace_in_expr_recursive (ref->u.ss.start, old_sym, new_st);
   12683            0 :           replace_in_expr_recursive (ref->u.ss.end, old_sym, new_st);
   12684              :         }
   12685              :     }
   12686              : 
   12687              :   /* Walk through sub-expressions based on expression type */
   12688          144 :   switch (expr->expr_type)
   12689              :     {
   12690           36 :     case EXPR_OP:
   12691           36 :       replace_in_expr_recursive (expr->value.op.op1, old_sym, new_st);
   12692           36 :       replace_in_expr_recursive (expr->value.op.op2, old_sym, new_st);
   12693           36 :       break;
   12694              : 
   12695            6 :     case EXPR_FUNCTION:
   12696           18 :       for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
   12697           12 :         replace_in_expr_recursive (a->expr, old_sym, new_st);
   12698              :       break;
   12699              : 
   12700            0 :     case EXPR_ARRAY:
   12701            0 :     case EXPR_STRUCTURE:
   12702            0 :       for (gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   12703            0 :            c; c = gfc_constructor_next (c))
   12704              :         {
   12705            0 :           replace_in_expr_recursive (c->expr, old_sym, new_st);
   12706            0 :           if (c->iterator)
   12707              :             {
   12708            0 :               replace_in_expr_recursive (c->iterator->start, old_sym, new_st);
   12709            0 :               replace_in_expr_recursive (c->iterator->end, old_sym, new_st);
   12710            0 :               replace_in_expr_recursive (c->iterator->step, old_sym, new_st);
   12711              :             }
   12712              :         }
   12713              :       break;
   12714              : 
   12715              :     default:
   12716              :       break;
   12717              :     }
   12718              : }
   12719              : 
   12720              : 
   12721              : /* Walk code tree and replace all variable references */
   12722              : 
   12723              : static void
   12724           30 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
   12725              : {
   12726           30 :   if (!code)
   12727              :     return;
   12728              : 
   12729           60 :   for (gfc_code *c = code; c; c = c->next)
   12730              :     {
   12731              :       /* Replace in expressions associated with this code node */
   12732           30 :       replace_in_expr_recursive (c->expr1, old_sym, new_st);
   12733           30 :       replace_in_expr_recursive (c->expr2, old_sym, new_st);
   12734           30 :       replace_in_expr_recursive (c->expr3, old_sym, new_st);
   12735           30 :       replace_in_expr_recursive (c->expr4, old_sym, new_st);
   12736              : 
   12737              :       /* Handle special code types with additional expressions */
   12738           30 :       switch (c->op)
   12739              :         {
   12740            0 :         case EXEC_DO:
   12741            0 :           if (c->ext.iterator)
   12742              :             {
   12743            0 :               replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
   12744            0 :               replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
   12745            0 :               replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
   12746              :             }
   12747              :           break;
   12748              : 
   12749            0 :         case EXEC_CALL:
   12750            0 :         case EXEC_ASSIGN_CALL:
   12751            0 :           for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
   12752            0 :             replace_in_expr_recursive (a->expr, old_sym, new_st);
   12753              :           break;
   12754              : 
   12755            0 :         case EXEC_SELECT:
   12756            0 :           for (gfc_code *b = c->block; b; b = b->block)
   12757              :             {
   12758            0 :               for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
   12759              :                 {
   12760            0 :                   replace_in_expr_recursive (cp->low, old_sym, new_st);
   12761            0 :                   replace_in_expr_recursive (cp->high, old_sym, new_st);
   12762              :                 }
   12763            0 :               replace_in_code_recursive (b->next, old_sym, new_st);
   12764              :             }
   12765              :           break;
   12766              : 
   12767            0 :         case EXEC_FORALL:
   12768            0 :         case EXEC_DO_CONCURRENT:
   12769            0 :           for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
   12770              :             {
   12771            0 :               replace_in_expr_recursive (fa->start, old_sym, new_st);
   12772            0 :               replace_in_expr_recursive (fa->end, old_sym, new_st);
   12773            0 :               replace_in_expr_recursive (fa->stride, old_sym, new_st);
   12774              :             }
   12775              :           /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
   12776              :              they'll be handled separately */
   12777              :           break;
   12778              : 
   12779            6 :         case EXEC_BLOCK:
   12780              :           /* Replace in ASSOCIATE selector expressions and the body.
   12781              :              The body of an EXEC_BLOCK lives in c->ext.block.ns->code, not
   12782              :              c->block->next, so without this case both selectors and body
   12783              :              are silently skipped, leaving shadow iterator references unreplaced
   12784              :              and producing wrong values at runtime.  */
   12785            6 :           for (gfc_association_list *alist = c->ext.block.assoc;
   12786           12 :                alist; alist = alist->next)
   12787            6 :             replace_in_expr_recursive (alist->target, old_sym, new_st);
   12788            6 :           if (c->ext.block.ns)
   12789            6 :             replace_in_code_recursive (c->ext.block.ns->code, old_sym, new_st);
   12790              :           break;
   12791              : 
   12792              :         default:
   12793              :           break;
   12794              :         }
   12795              : 
   12796              :       /* Recurse into blocks */
   12797           30 :       if (c->block)
   12798            0 :         replace_in_code_recursive (c->block->next, old_sym, new_st);
   12799              :     }
   12800              : }
   12801              : 
   12802              : 
   12803              : /* Replace all references to outer_sym with shadow_st in the given code.  */
   12804              : 
   12805              : static void
   12806           24 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
   12807              :                               gfc_symtree *shadow_st)
   12808              : {
   12809              :   /* Use custom recursive walker to ensure we visit ALL expressions */
   12810            0 :   replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
   12811           24 : }
   12812              : 
   12813              : 
   12814              : static void
   12815         2217 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   12816              : {
   12817         2217 :   static gfc_expr **var_expr;
   12818         2217 :   static int total_var = 0;
   12819         2217 :   static int nvar = 0;
   12820         2217 :   int i, old_nvar, tmp;
   12821         2217 :   gfc_forall_iterator *fa;
   12822         2217 :   bool shadow = false;
   12823              : 
   12824         2217 :   old_nvar = nvar;
   12825              : 
   12826              :   /* Only warn about obsolescent FORALL, not DO CONCURRENT */
   12827         2217 :   if (code->op == EXEC_FORALL
   12828         2217 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   12829              :     return;
   12830              : 
   12831              :   /* Start to resolve a FORALL construct   */
   12832              :   /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
   12833              :      forall_save==0 means we're not nested in a FORALL in the current scope,
   12834              :      but nvar==0 ensures we're not nested in a parent scope either (prevents
   12835              :      double allocation when FORALL is nested inside DO CONCURRENT).  */
   12836         2217 :   if (forall_save == 0 && nvar == 0)
   12837              :     {
   12838              :       /* Count the total number of FORALL indices in the nested FORALL
   12839              :          construct in order to allocate the VAR_EXPR with proper size.  */
   12840         2123 :       total_var = gfc_count_forall_iterators (code);
   12841              : 
   12842              :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   12843         2123 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   12844              :     }
   12845              : 
   12846              :   /* The information about FORALL iterator, including FORALL indices start,
   12847              :      end and stride.  An outer FORALL indice cannot appear in start, end or
   12848              :      stride.  Check for a shadow index-name.  */
   12849         6352 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12850              :     {
   12851              :       /* Fortran 2008: C738 (R753).  */
   12852         4135 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   12853              :         {
   12854            2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   12855              :                      "of type integer", &fa->var->where);
   12856            2 :           continue;
   12857              :         }
   12858              : 
   12859              :       /* Check if any outer FORALL index name is the same as the current
   12860              :          one.  Skip this check if the iterator is a shadow variable (from
   12861              :          DO CONCURRENT type spec) which may not have a symtree yet.  */
   12862         7144 :       for (i = 0; i < nvar; i++)
   12863              :         {
   12864         3011 :           if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
   12865         3011 :               && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   12866            0 :             gfc_error ("An outer FORALL construct already has an index "
   12867              :                         "with this name %L", &fa->var->where);
   12868              :         }
   12869              : 
   12870         4133 :       if (fa->shadow)
   12871           24 :         shadow = true;
   12872              : 
   12873              :       /* Record the current FORALL index.  */
   12874         4133 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   12875              : 
   12876         4133 :       nvar++;
   12877              : 
   12878              :       /* No memory leak.  */
   12879         4133 :       gcc_assert (nvar <= total_var);
   12880              :     }
   12881              : 
   12882              :   /* Need to walk the code and replace references to the index-name with
   12883              :      references to the shadow index-name. This must be done BEFORE resolving
   12884              :      the body so that resolution uses the correct shadow variables.  */
   12885         2217 :   if (shadow)
   12886              :     {
   12887              :       /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables.  */
   12888           54 :       for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12889              :         {
   12890           30 :           if (fa->shadow)
   12891              :             {
   12892           24 :               gfc_symtree *shadow_st;
   12893           24 :               const char *shadow_name_str;
   12894           24 :               char *outer_name;
   12895              : 
   12896              :               /* fa->var now points to the shadow variable "_name".  */
   12897           24 :               shadow_name_str = fa->var->symtree->name;
   12898           24 :               shadow_st = fa->var->symtree;
   12899              : 
   12900           24 :               if (shadow_name_str[0] != '_')
   12901            0 :                 gfc_internal_error ("Expected shadow variable name to start with _");
   12902              : 
   12903           24 :               outer_name = (char *) alloca (strlen (shadow_name_str));
   12904           24 :               strcpy (outer_name, shadow_name_str + 1);
   12905              : 
   12906              :               /* Find the ITERATOR symbol in the current namespace.
   12907              :                  This is the local DO CONCURRENT variable that body expressions reference.  */
   12908           24 :               gfc_symtree *iter_st = gfc_find_symtree (ns->sym_root, outer_name);
   12909              : 
   12910           24 :               if (!iter_st)
   12911              :                 /* No iterator variable found - this shouldn't happen */
   12912            0 :                 continue;
   12913              : 
   12914           24 :               gfc_symbol *iter_sym = iter_st->n.sym;
   12915              : 
   12916              :               /* Walk the FORALL/DO CONCURRENT body and replace all references.  */
   12917           24 :               if (code->block && code->block->next)
   12918           24 :                 gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
   12919              :             }
   12920              :         }
   12921              :     }
   12922              : 
   12923              :   /* Resolve the FORALL body.  */
   12924         2217 :   gfc_resolve_forall_body (code, nvar, var_expr);
   12925              : 
   12926              :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   12927         2217 :   gfc_resolve_blocks (code->block, ns);
   12928              : 
   12929         2217 :   tmp = nvar;
   12930         2217 :   nvar = old_nvar;
   12931              :   /* Free only the VAR_EXPRs allocated in this frame.  */
   12932         6350 :   for (i = nvar; i < tmp; i++)
   12933         4133 :      gfc_free_expr (var_expr[i]);
   12934              : 
   12935         2217 :   if (nvar == 0)
   12936              :     {
   12937              :       /* We are in the outermost FORALL construct.  */
   12938         2123 :       gcc_assert (forall_save == 0);
   12939              : 
   12940              :       /* VAR_EXPR is not needed any more.  */
   12941         2123 :       free (var_expr);
   12942         2123 :       total_var = 0;
   12943              :     }
   12944              : }
   12945              : 
   12946              : 
   12947              : /* Resolve a BLOCK construct statement.  */
   12948              : 
   12949              : static void
   12950         8131 : resolve_block_construct (gfc_code* code)
   12951              : {
   12952         8131 :   gfc_namespace *ns = code->ext.block.ns;
   12953              : 
   12954              :   /* For an ASSOCIATE block, the associations (and their targets) will be
   12955              :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   12956              :      namespace.  */
   12957         8131 :   gfc_resolve (ns);
   12958            0 : }
   12959              : 
   12960              : 
   12961              : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   12962              :    DO code nodes.  */
   12963              : 
   12964              : void
   12965       333478 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   12966              : {
   12967       333478 :   bool t;
   12968              : 
   12969       678450 :   for (; b; b = b->block)
   12970              :     {
   12971       344972 :       t = gfc_resolve_expr (b->expr1);
   12972       344972 :       if (!gfc_resolve_expr (b->expr2))
   12973            0 :         t = false;
   12974              : 
   12975       344972 :       switch (b->op)
   12976              :         {
   12977       238083 :         case EXEC_IF:
   12978       238083 :           if (t && b->expr1 != NULL
   12979       233762 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   12980            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   12981              :                        &b->expr1->where);
   12982              :           break;
   12983              : 
   12984          764 :         case EXEC_WHERE:
   12985          764 :           if (t
   12986          764 :               && b->expr1 != NULL
   12987          631 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   12988            0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   12989              :                        &b->expr1->where);
   12990              :           break;
   12991              : 
   12992           76 :         case EXEC_GOTO:
   12993           76 :           resolve_branch (b->label1, b);
   12994           76 :           break;
   12995              : 
   12996            0 :         case EXEC_BLOCK:
   12997            0 :           resolve_block_construct (b);
   12998            0 :           break;
   12999              : 
   13000              :         case EXEC_SELECT:
   13001              :         case EXEC_SELECT_TYPE:
   13002              :         case EXEC_SELECT_RANK:
   13003              :         case EXEC_FORALL:
   13004              :         case EXEC_DO:
   13005              :         case EXEC_DO_WHILE:
   13006              :         case EXEC_DO_CONCURRENT:
   13007              :         case EXEC_CRITICAL:
   13008              :         case EXEC_READ:
   13009              :         case EXEC_WRITE:
   13010              :         case EXEC_IOLENGTH:
   13011              :         case EXEC_WAIT:
   13012              :           break;
   13013              : 
   13014         2697 :         case EXEC_OMP_ATOMIC:
   13015         2697 :         case EXEC_OACC_ATOMIC:
   13016         2697 :           {
   13017              :             /* Verify this before calling gfc_resolve_code, which might
   13018              :                change it.  */
   13019         2697 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   13020              :                         || (b->next && b->next->op == EXEC_ASSIGN));
   13021              :           }
   13022              :           break;
   13023              : 
   13024              :         case EXEC_OACC_PARALLEL_LOOP:
   13025              :         case EXEC_OACC_PARALLEL:
   13026              :         case EXEC_OACC_KERNELS_LOOP:
   13027              :         case EXEC_OACC_KERNELS:
   13028              :         case EXEC_OACC_SERIAL_LOOP:
   13029              :         case EXEC_OACC_SERIAL:
   13030              :         case EXEC_OACC_DATA:
   13031              :         case EXEC_OACC_HOST_DATA:
   13032              :         case EXEC_OACC_LOOP:
   13033              :         case EXEC_OACC_UPDATE:
   13034              :         case EXEC_OACC_WAIT:
   13035              :         case EXEC_OACC_CACHE:
   13036              :         case EXEC_OACC_ENTER_DATA:
   13037              :         case EXEC_OACC_EXIT_DATA:
   13038              :         case EXEC_OACC_ROUTINE:
   13039              :         case EXEC_OMP_ALLOCATE:
   13040              :         case EXEC_OMP_ALLOCATORS:
   13041              :         case EXEC_OMP_ASSUME:
   13042              :         case EXEC_OMP_CRITICAL:
   13043              :         case EXEC_OMP_DISPATCH:
   13044              :         case EXEC_OMP_DISTRIBUTE:
   13045              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   13046              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   13047              :         case EXEC_OMP_DISTRIBUTE_SIMD:
   13048              :         case EXEC_OMP_DO:
   13049              :         case EXEC_OMP_DO_SIMD:
   13050              :         case EXEC_OMP_ERROR:
   13051              :         case EXEC_OMP_LOOP:
   13052              :         case EXEC_OMP_MASKED:
   13053              :         case EXEC_OMP_MASKED_TASKLOOP:
   13054              :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13055              :         case EXEC_OMP_MASTER:
   13056              :         case EXEC_OMP_MASTER_TASKLOOP:
   13057              :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13058              :         case EXEC_OMP_ORDERED:
   13059              :         case EXEC_OMP_PARALLEL:
   13060              :         case EXEC_OMP_PARALLEL_DO:
   13061              :         case EXEC_OMP_PARALLEL_DO_SIMD:
   13062              :         case EXEC_OMP_PARALLEL_LOOP:
   13063              :         case EXEC_OMP_PARALLEL_MASKED:
   13064              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13065              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13066              :         case EXEC_OMP_PARALLEL_MASTER:
   13067              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13068              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13069              :         case EXEC_OMP_PARALLEL_SECTIONS:
   13070              :         case EXEC_OMP_PARALLEL_WORKSHARE:
   13071              :         case EXEC_OMP_SECTIONS:
   13072              :         case EXEC_OMP_SIMD:
   13073              :         case EXEC_OMP_SCOPE:
   13074              :         case EXEC_OMP_SINGLE:
   13075              :         case EXEC_OMP_TARGET:
   13076              :         case EXEC_OMP_TARGET_DATA:
   13077              :         case EXEC_OMP_TARGET_ENTER_DATA:
   13078              :         case EXEC_OMP_TARGET_EXIT_DATA:
   13079              :         case EXEC_OMP_TARGET_PARALLEL:
   13080              :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13081              :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13082              :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13083              :         case EXEC_OMP_TARGET_SIMD:
   13084              :         case EXEC_OMP_TARGET_TEAMS:
   13085              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13086              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13087              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13088              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13089              :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13090              :         case EXEC_OMP_TARGET_UPDATE:
   13091              :         case EXEC_OMP_TASK:
   13092              :         case EXEC_OMP_TASKGROUP:
   13093              :         case EXEC_OMP_TASKLOOP:
   13094              :         case EXEC_OMP_TASKLOOP_SIMD:
   13095              :         case EXEC_OMP_TASKWAIT:
   13096              :         case EXEC_OMP_TASKYIELD:
   13097              :         case EXEC_OMP_TEAMS:
   13098              :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13099              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13100              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13101              :         case EXEC_OMP_TEAMS_LOOP:
   13102              :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13103              :         case EXEC_OMP_TILE:
   13104              :         case EXEC_OMP_UNROLL:
   13105              :         case EXEC_OMP_WORKSHARE:
   13106              :           break;
   13107              : 
   13108            0 :         default:
   13109            0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   13110              :         }
   13111       344972 :       gfc_value_used_expr (b->expr1, VALUE_USED);
   13112       344972 :       gfc_value_used_expr (b->expr2, VALUE_USED);
   13113       344972 :       gfc_resolve_code (b->next, ns);
   13114              :     }
   13115       333478 : }
   13116              : 
   13117              : bool
   13118            0 : caf_possible_reallocate (gfc_expr *e)
   13119              : {
   13120            0 :   symbol_attribute caf_attr;
   13121            0 :   gfc_ref *last_arr_ref = nullptr;
   13122              : 
   13123            0 :   caf_attr = gfc_caf_attr (e);
   13124            0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   13125              :     return false;
   13126              : 
   13127              :   /* Only full array refs can indicate a needed reallocation.  */
   13128            0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   13129            0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   13130            0 :       last_arr_ref = ref;
   13131              : 
   13132            0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   13133              : }
   13134              : 
   13135              : /* Does everything to resolve an ordinary assignment.  Returns true
   13136              :    if this is an interface assignment.  */
   13137              : static bool
   13138       286825 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   13139              : {
   13140       286825 :   bool rval = false;
   13141       286825 :   gfc_expr *lhs;
   13142       286825 :   gfc_expr *rhs;
   13143       286825 :   int n;
   13144       286825 :   gfc_ref *ref;
   13145       286825 :   symbol_attribute attr;
   13146              : 
   13147       286825 :   if (gfc_extend_assign (code, ns))
   13148              :     {
   13149          918 :       gfc_expr** rhsptr;
   13150              : 
   13151          918 :       if (code->op == EXEC_ASSIGN_CALL)
   13152              :         {
   13153          469 :           lhs = code->ext.actual->expr;
   13154          469 :           rhsptr = &code->ext.actual->next->expr;
   13155              :         }
   13156              :       else
   13157              :         {
   13158          449 :           gfc_actual_arglist* args;
   13159          449 :           gfc_typebound_proc* tbp;
   13160              : 
   13161          449 :           gcc_assert (code->op == EXEC_COMPCALL);
   13162              : 
   13163          449 :           args = code->expr1->value.compcall.actual;
   13164          449 :           lhs = args->expr;
   13165          449 :           rhsptr = &args->next->expr;
   13166              : 
   13167          449 :           tbp = code->expr1->value.compcall.tbp;
   13168          449 :           gcc_assert (!tbp->is_generic);
   13169              :         }
   13170              : 
   13171              :       /* Make a temporary rhs when there is a default initializer
   13172              :          and rhs is the same symbol as the lhs.  */
   13173          918 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   13174          507 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   13175          436 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   13176         1206 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   13177           60 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   13178              : 
   13179          918 :       return true;
   13180              :     }
   13181              : 
   13182       285907 :   lhs = code->expr1;
   13183       285907 :   rhs = code->expr2;
   13184              : 
   13185       285907 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   13186       265663 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   13187        22874 :       && !lhs->symtree->n.sym->attr.proc_pointer
   13188       308781 :       && gfc_expr_attr (lhs).proc_pointer)
   13189              :     {
   13190            1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   13191              :                  "pointer component",
   13192              :                  &lhs->where);
   13193            1 :       return false;
   13194              :     }
   13195              : 
   13196       336961 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   13197       250264 :       && rhs->ts.type == BT_CHARACTER
   13198       286299 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   13199              :     {
   13200              :       /* Use of -fdec-char-conversions allows assignment of character data
   13201              :          to non-character variables.  This not permitted for nonconstant
   13202              :          strings.  */
   13203           29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   13204              :                  gfc_typename (lhs), &rhs->where);
   13205           29 :       return false;
   13206              :     }
   13207              : 
   13208       285877 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   13209              :     {
   13210            0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   13211              :                    gfc_typename (lhs), &rhs->where);
   13212            0 :       return false;
   13213              :     }
   13214              : 
   13215              :   /* Handle the case of a BOZ literal on the RHS.  */
   13216       285877 :   if (rhs->ts.type == BT_BOZ)
   13217              :     {
   13218            3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   13219              :                            "statement value nor an actual argument of "
   13220              :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   13221              :                            &rhs->where))
   13222              :         return false;
   13223              : 
   13224            1 :       switch (lhs->ts.type)
   13225              :         {
   13226            0 :         case BT_INTEGER:
   13227            0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   13228              :             return false;
   13229              :           break;
   13230            1 :         case BT_REAL:
   13231            1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   13232              :             return false;
   13233              :           break;
   13234            0 :         default:
   13235            0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   13236            0 :           return false;
   13237              :         }
   13238              :     }
   13239              : 
   13240       285875 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   13241              :     {
   13242           67 :       HOST_WIDE_INT llen = 0, rlen = 0;
   13243           67 :       if (lhs->ts.u.cl != NULL
   13244           67 :             && lhs->ts.u.cl->length != NULL
   13245           56 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13246           56 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   13247              : 
   13248           67 :       if (rhs->expr_type == EXPR_CONSTANT)
   13249           29 :         rlen = rhs->value.character.length;
   13250              : 
   13251           38 :       else if (rhs->ts.u.cl != NULL
   13252           38 :                  && rhs->ts.u.cl->length != NULL
   13253           35 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13254           35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   13255              : 
   13256           67 :       if (rlen && llen && rlen > llen)
   13257           28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   13258              :                          "CHARACTER expression will be truncated "
   13259              :                          "in assignment (%wd/%wd) at %L",
   13260              :                          llen, rlen, &code->loc);
   13261              :     }
   13262              : 
   13263              :   /* Ensure that a vector index expression for the lvalue is evaluated
   13264              :      to a temporary if the lvalue symbol is referenced in it.  */
   13265       285875 :   if (lhs->rank)
   13266              :     {
   13267       113141 :       for (ref = lhs->ref; ref; ref= ref->next)
   13268        60464 :         if (ref->type == REF_ARRAY)
   13269              :           {
   13270       133218 :             for (n = 0; n < ref->u.ar.dimen; n++)
   13271        78733 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   13272        78963 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   13273          230 :                                            ref->u.ar.start[n]))
   13274           14 :                 ref->u.ar.start[n]
   13275           14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   13276              :           }
   13277              :     }
   13278              : 
   13279       285875 :   if (gfc_pure (NULL))
   13280              :     {
   13281         3430 :       if (lhs->ts.type == BT_DERIVED
   13282          136 :             && lhs->expr_type == EXPR_VARIABLE
   13283          136 :             && lhs->ts.u.derived->attr.pointer_comp
   13284            4 :             && rhs->expr_type == EXPR_VARIABLE
   13285         3433 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13286            2 :                 || gfc_is_coindexed (rhs)))
   13287              :         {
   13288              :           /* F2008, C1283.  */
   13289            2 :           if (gfc_is_coindexed (rhs))
   13290            1 :             gfc_error ("Coindexed expression at %L is assigned to "
   13291              :                         "a derived type variable with a POINTER "
   13292              :                         "component in a PURE procedure",
   13293              :                         &rhs->where);
   13294              :           else
   13295              :           /* F2008, C1283 (4).  */
   13296            1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   13297              :                         "shall not be used as the expr at %L of an intrinsic "
   13298              :                         "assignment statement in which the variable is of a "
   13299              :                         "derived type if the derived type has a pointer "
   13300              :                         "component at any level of component selection.",
   13301              :                         &rhs->where);
   13302            2 :           return rval;
   13303              :         }
   13304              : 
   13305              :       /* Fortran 2008, C1283.  */
   13306         3428 :       if (gfc_is_coindexed (lhs))
   13307              :         {
   13308            1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   13309              :                      "procedure", &rhs->where);
   13310            1 :           return rval;
   13311              :         }
   13312              :     }
   13313              : 
   13314       285872 :   if (gfc_implicit_pure (NULL))
   13315              :     {
   13316         7321 :       if (lhs->expr_type == EXPR_VARIABLE
   13317         7321 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   13318         5208 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   13319          253 :         gfc_unset_implicit_pure (NULL);
   13320              : 
   13321         7321 :       if (lhs->ts.type == BT_DERIVED
   13322          352 :             && lhs->expr_type == EXPR_VARIABLE
   13323          352 :             && lhs->ts.u.derived->attr.pointer_comp
   13324            7 :             && rhs->expr_type == EXPR_VARIABLE
   13325         7328 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13326            7 :                 || gfc_is_coindexed (rhs)))
   13327            0 :         gfc_unset_implicit_pure (NULL);
   13328              : 
   13329              :       /* Fortran 2008, C1283.  */
   13330         7321 :       if (gfc_is_coindexed (lhs))
   13331            0 :         gfc_unset_implicit_pure (NULL);
   13332              :     }
   13333              : 
   13334              :   /* F2008, 7.2.1.2.  */
   13335       285872 :   attr = gfc_expr_attr (lhs);
   13336       285872 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   13337              :     {
   13338          987 :       if (attr.codimension)
   13339              :         {
   13340            1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   13341              :                      "permitted", &lhs->where);
   13342            1 :           return false;
   13343              :         }
   13344          986 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   13345              :                            "polymorphic variable at %L", &lhs->where))
   13346              :         return false;
   13347          985 :       if (!flag_realloc_lhs)
   13348              :         {
   13349            1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   13350              :                      "requires %<-frealloc-lhs%>", &lhs->where);
   13351            1 :           return false;
   13352              :         }
   13353              :     }
   13354       284885 :   else if (lhs->ts.type == BT_CLASS)
   13355              :     {
   13356            9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   13357              :                  "assignment at %L - check that there is a matching specific "
   13358              :                  "subroutine for %<=%> operator", &lhs->where);
   13359            9 :       return false;
   13360              :     }
   13361              : 
   13362       285860 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   13363              : 
   13364              :   /* F2008, Section 7.2.1.2.  */
   13365       285860 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   13366              :     {
   13367            1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   13368              :                  "component in assignment at %L", &lhs->where);
   13369            1 :       return false;
   13370              :     }
   13371              : 
   13372              :   /* Assign the 'data' of a class object to a derived type.  */
   13373       285859 :   if (lhs->ts.type == BT_DERIVED
   13374         7275 :       && rhs->ts.type == BT_CLASS
   13375          168 :       && (rhs->expr_type != EXPR_ARRAY
   13376          162 :           && rhs->expr_type != EXPR_OP))
   13377          156 :     gfc_add_data_component (rhs);
   13378              : 
   13379              :   /* Make sure there is a vtable and, in particular, a _copy for the
   13380              :      rhs type.  */
   13381       285859 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   13382          615 :     gfc_find_vtab (&rhs->ts);
   13383              : 
   13384       285859 :   gfc_check_assign (lhs, rhs, 1);
   13385              : 
   13386       285859 :   return false;
   13387              : }
   13388              : 
   13389              : 
   13390              : /* Add a component reference onto an expression.  */
   13391              : 
   13392              : static void
   13393          665 : add_comp_ref (gfc_expr *e, gfc_component *c)
   13394              : {
   13395          665 :   gfc_ref **ref;
   13396          665 :   ref = &(e->ref);
   13397          889 :   while (*ref)
   13398          224 :     ref = &((*ref)->next);
   13399          665 :   *ref = gfc_get_ref ();
   13400          665 :   (*ref)->type = REF_COMPONENT;
   13401          665 :   (*ref)->u.c.sym = e->ts.u.derived;
   13402          665 :   (*ref)->u.c.component = c;
   13403          665 :   e->ts = c->ts;
   13404              : 
   13405              :   /* Add a full array ref, as necessary.  */
   13406          665 :   if (c->as)
   13407              :     {
   13408           84 :       gfc_add_full_array_ref (e, c->as);
   13409           84 :       e->rank = c->as->rank;
   13410           84 :       e->corank = c->as->corank;
   13411              :     }
   13412          665 : }
   13413              : 
   13414              : 
   13415              : /* Build an assignment.  Keep the argument 'op' for future use, so that
   13416              :    pointer assignments can be made.  */
   13417              : 
   13418              : static gfc_code *
   13419          988 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   13420              :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   13421              : {
   13422          988 :   gfc_code *this_code;
   13423              : 
   13424          988 :   this_code = gfc_get_code (op);
   13425          988 :   this_code->next = NULL;
   13426          988 :   this_code->expr1 = gfc_copy_expr (expr1);
   13427          988 :   this_code->expr2 = gfc_copy_expr (expr2);
   13428          988 :   this_code->loc = loc;
   13429          988 :   if (comp1 && comp2)
   13430              :     {
   13431          288 :       add_comp_ref (this_code->expr1, comp1);
   13432          288 :       add_comp_ref (this_code->expr2, comp2);
   13433              :     }
   13434              : 
   13435          988 :   return this_code;
   13436              : }
   13437              : 
   13438              : 
   13439              : /* Makes a temporary variable expression based on the characteristics of
   13440              :    a given variable expression.  If allocatable is set, the temporary is
   13441              :    unconditionally allocatable*/
   13442              : 
   13443              : static gfc_expr*
   13444          482 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13445              :                     bool allocatable = false)
   13446              : {
   13447          482 :   static int serial = 0;
   13448          482 :   char name[GFC_MAX_SYMBOL_LEN];
   13449          482 :   gfc_symtree *tmp;
   13450          482 :   gfc_array_spec *as;
   13451          482 :   gfc_array_ref *aref;
   13452          482 :   gfc_ref *ref;
   13453              : 
   13454          482 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   13455          482 :   gfc_get_sym_tree (name, ns, &tmp, false);
   13456          482 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   13457              : 
   13458          482 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   13459            0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   13460              :                                                     NULL,
   13461            0 :                                                     e->value.character.length);
   13462              : 
   13463          482 :   as = NULL;
   13464          482 :   ref = NULL;
   13465          482 :   aref = NULL;
   13466              : 
   13467              :   /* Obtain the arrayspec for the temporary.  */
   13468          482 :    if (e->rank && e->expr_type != EXPR_ARRAY
   13469              :        && e->expr_type != EXPR_FUNCTION
   13470              :        && e->expr_type != EXPR_OP)
   13471              :     {
   13472           52 :       aref = gfc_find_array_ref (e);
   13473           52 :       if (e->expr_type == EXPR_VARIABLE
   13474           52 :           && e->symtree->n.sym->as == aref->as)
   13475              :         as = aref->as;
   13476              :       else
   13477              :         {
   13478            0 :           for (ref = e->ref; ref; ref = ref->next)
   13479            0 :             if (ref->type == REF_COMPONENT
   13480            0 :                 && ref->u.c.component->as == aref->as)
   13481              :               {
   13482              :                 as = aref->as;
   13483              :                 break;
   13484              :               }
   13485              :         }
   13486              :     }
   13487              : 
   13488              :   /* Add the attributes and the arrayspec to the temporary.  */
   13489          482 :   tmp->n.sym->attr = gfc_expr_attr (e);
   13490          482 :   tmp->n.sym->attr.function = 0;
   13491          482 :   tmp->n.sym->attr.proc_pointer = 0;
   13492          482 :   tmp->n.sym->attr.result = 0;
   13493          482 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   13494          482 :   tmp->n.sym->attr.dummy = 0;
   13495          482 :   tmp->n.sym->attr.use_assoc = 0;
   13496          482 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   13497              : 
   13498              : 
   13499          482 :   if (as && !allocatable)
   13500              :     {
   13501           52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   13502           52 :       if (!ref)
   13503           52 :         ref = e->ref;
   13504           52 :       if (as->type == AS_DEFERRED)
   13505           46 :         tmp->n.sym->attr.allocatable = 1;
   13506              :     }
   13507          430 :   else if ((e->rank || e->corank)
   13508          130 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   13509           24 :                || e->expr_type == EXPR_OP || allocatable))
   13510              :     {
   13511          130 :       tmp->n.sym->as = gfc_get_array_spec ();
   13512          130 :       tmp->n.sym->as->type = AS_DEFERRED;
   13513          130 :       tmp->n.sym->as->rank = e->rank;
   13514          130 :       tmp->n.sym->as->corank = e->corank;
   13515          130 :       tmp->n.sym->attr.allocatable = 1;
   13516          130 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   13517          260 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   13518              :     }
   13519              :   else
   13520          300 :     tmp->n.sym->attr.dimension = 0;
   13521              : 
   13522          482 :   gfc_set_sym_referenced (tmp->n.sym);
   13523          482 :   gfc_commit_symbol (tmp->n.sym);
   13524          482 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   13525              : 
   13526              :   /* Should the lhs be a section, use its array ref for the
   13527              :      temporary expression.  */
   13528          482 :   if (aref && aref->type != AR_FULL && !allocatable)
   13529              :     {
   13530            6 :       gfc_free_ref_list (e->ref);
   13531            6 :       e->ref = gfc_copy_ref (ref);
   13532              :     }
   13533          482 :   return e;
   13534              : }
   13535              : 
   13536              : 
   13537              : /* Helper function to take an argument in a subroutine call with a dependency
   13538              :    on another argument, copy it to an allocatable temporary and use the
   13539              :    temporary in the call expression. The new code is embedded in a block to
   13540              :    ensure local, automatic deallocation.  */
   13541              : 
   13542              : static void
   13543           36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
   13544              :                              gfc_expr **rhsptr)
   13545              : {
   13546           36 :   gfc_namespace *block_ns;
   13547           36 :   gfc_expr *tmp_var;
   13548              : 
   13549              :   /* Wrap the new code in a block so that the temporary is deallocated.  */
   13550           36 :   block_ns = gfc_build_block_ns (ns);
   13551              : 
   13552              :   /* As it stands, the block_ns does not not stand up to resolution because the
   13553              :      the assignment would be converted to a call and, in any case, the modified
   13554              :      call fails in gfc_check_conformance.  */
   13555           36 :   block_ns->resolved = 1;
   13556              : 
   13557              :   /* Assign the original expression to the temporary.  */
   13558           36 :   tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
   13559           72 :   block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
   13560           36 :                                      NULL, NULL, (*rhsptr)->where);
   13561              : 
   13562              :   /* Transfer the call to the block and terminate block code.  */
   13563           36 :   *rhsptr = gfc_copy_expr (tmp_var);
   13564           36 :   block_ns->code->next = gfc_get_code (EXEC_NOP);
   13565           36 :   *(block_ns->code->next) = *code;
   13566           36 :   block_ns->code->next->next = NULL;
   13567              : 
   13568              :   /* Convert the original code to execute the block.  */
   13569           36 :   code->op = EXEC_BLOCK;
   13570           36 :   code->ext.block.ns = block_ns;
   13571           36 :   code->ext.block.assoc = NULL;
   13572           36 :   code->expr1 = code->expr2 = NULL;
   13573           36 : }
   13574              : 
   13575              : 
   13576              : /* Add one line of code to the code chain, making sure that 'head' and
   13577              :    'tail' are appropriately updated.  */
   13578              : 
   13579              : static void
   13580          656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   13581              : {
   13582          656 :   gcc_assert (this_code);
   13583          656 :   if (*head == NULL)
   13584          308 :     *head = *tail = *this_code;
   13585              :   else
   13586          348 :     *tail = gfc_append_code (*tail, *this_code);
   13587          656 :   *this_code = NULL;
   13588          656 : }
   13589              : 
   13590              : 
   13591              : /* Generate a final call from a variable expression  */
   13592              : 
   13593              : static void
   13594           81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   13595              : {
   13596           81 :   gfc_code *this_code;
   13597           81 :   gfc_expr *final_expr = NULL;
   13598           81 :   gfc_expr *size_expr;
   13599           81 :   gfc_expr *fini_coarray;
   13600              : 
   13601           81 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   13602           81 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   13603           75 :     return;
   13604              : 
   13605              :   /* Now generate the finalizer call.  */
   13606            6 :   this_code = gfc_get_code (EXEC_CALL);
   13607            6 :   this_code->symtree = final_expr->symtree;
   13608            6 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   13609              : 
   13610              :   //* Expression to be finalized  */
   13611            6 :   this_code->ext.actual = gfc_get_actual_arglist ();
   13612            6 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   13613              : 
   13614              :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   13615            6 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   13616            6 :   size_expr = gfc_get_expr ();
   13617            6 :   size_expr->where = gfc_current_locus;
   13618            6 :   size_expr->expr_type = EXPR_OP;
   13619            6 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   13620            6 :   size_expr->value.op.op1
   13621           12 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   13622              :                                     "storage_size", gfc_current_locus, 2,
   13623            6 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   13624              :                                     gfc_get_int_expr (gfc_index_integer_kind,
   13625              :                                                       NULL, 0));
   13626            6 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   13627              :                                               gfc_character_storage_size);
   13628            6 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   13629            6 :   size_expr->ts = size_expr->value.op.op1->ts;
   13630            6 :   this_code->ext.actual->next->expr = size_expr;
   13631              : 
   13632              :   /* fini_coarray  */
   13633            6 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   13634            6 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   13635              :                                         &tmp_expr->where);
   13636            6 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   13637            6 :   this_code->ext.actual->next->next->expr = fini_coarray;
   13638              : 
   13639            6 :   add_code_to_chain (&this_code, head, tail);
   13640              : 
   13641              : }
   13642              : 
   13643              : /* Counts the potential number of part array references that would
   13644              :    result from resolution of typebound defined assignments.  */
   13645              : 
   13646              : 
   13647              : static int
   13648          243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   13649              : {
   13650          243 :   gfc_component *c;
   13651          243 :   int c_depth = 0, t_depth;
   13652              : 
   13653          584 :   for (c= derived->components; c; c = c->next)
   13654              :     {
   13655          341 :       if ((!gfc_bt_struct (c->ts.type)
   13656          261 :             || c->attr.pointer
   13657          261 :             || c->attr.allocatable
   13658          260 :             || c->attr.proc_pointer_comp
   13659          260 :             || c->attr.class_pointer
   13660          260 :             || c->attr.proc_pointer)
   13661           81 :           && !c->attr.defined_assign_comp)
   13662           81 :         continue;
   13663              : 
   13664          260 :       if (c->as && c_depth == 0)
   13665          260 :         c_depth = 1;
   13666              : 
   13667          260 :       if (c->ts.u.derived->attr.defined_assign_comp)
   13668          110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   13669              :                                               c->as ? 1 : 0);
   13670              :       else
   13671              :         t_depth = 0;
   13672              : 
   13673          260 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   13674              :     }
   13675          243 :   return depth + c_depth;
   13676              : }
   13677              : 
   13678              : 
   13679              : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   13680              :    "An intrinsic assignment where the variable is of derived type is performed
   13681              :     as if each component of the variable were assigned from the corresponding
   13682              :     component of expr using pointer assignment (10.2.2) for each pointer
   13683              :     component, defined assignment for each nonpointer nonallocatable component
   13684              :     of a type that has a type-bound defined assignment consistent with the
   13685              :     component, intrinsic assignment for each other nonpointer nonallocatable
   13686              :     component, and intrinsic assignment for each allocated coarray component.
   13687              :     For unallocated coarray components, the corresponding component of the
   13688              :     variable shall be unallocated. For a noncoarray allocatable component the
   13689              :     following sequence of operations is applied.
   13690              :         (1) If the component of the variable is allocated, it is deallocated.
   13691              :         (2) If the component of the value of expr is allocated, the
   13692              :             corresponding component of the variable is allocated with the same
   13693              :             dynamic type and type parameters as the component of the value of
   13694              :             expr. If it is an array, it is allocated with the same bounds. The
   13695              :             value of the component of the value of expr is then assigned to the
   13696              :             corresponding component of the variable using defined assignment if
   13697              :             the declared type of the component has a type-bound defined
   13698              :             assignment consistent with the component, and intrinsic assignment
   13699              :             for the dynamic type of that component otherwise."
   13700              : 
   13701              :    The pointer assignments are taken care of by the intrinsic assignment of the
   13702              :    structure itself.  This function recursively adds defined assignments where
   13703              :    required.  The recursion is accomplished by calling gfc_resolve_code.
   13704              : 
   13705              :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   13706              :    and the component of 'var' is finalizable, we need a temporary for the
   13707              :    lhs.  In pseudo-code for an assignment var = expr:
   13708              : 
   13709              :    ! Confine finalization of temporaries, as far as possible.
   13710              :      Enclose the code for the assignment in a block
   13711              :    ! Only call function 'expr' once.
   13712              :       #if ('expr is not a constant or an variable)
   13713              :         temp_expr = expr
   13714              :         expr = temp_x
   13715              :    ! Do the intrinsic assignment
   13716              :       #if typeof ('var') has a typebound final subroutine
   13717              :         finalize (var)
   13718              :       var = expr
   13719              :    ! Now do the component assignments
   13720              :       #do over derived type components [%cmp]
   13721              :         #if (cmp is a pointer of any kind)
   13722              :           continue
   13723              :         build the assignment
   13724              :         resolve the code
   13725              :         #if the code is a typebound assignment
   13726              :            #if (arg1 is INOUT or finalizable OUT && !t1)
   13727              :              t1 = var
   13728              :              arg1 = t1
   13729              :              deal with allocatation or not of var and this component
   13730              :         #elseif the code is an assignment by itself
   13731              :            #if this component does not need finalization
   13732              :              delete code and continue
   13733              :         #else
   13734              :            remove the leading assignment
   13735              :         #endif
   13736              :         commit the code
   13737              :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   13738              :            var%cmp = t1%cmp
   13739              :       #enddo
   13740              :       put all code chunks involving t1 to the top of the generated code
   13741              :       insert the generated block in place of the original code
   13742              : */
   13743              : 
   13744              : static bool
   13745          381 : is_finalizable_type (gfc_typespec ts)
   13746              : {
   13747          381 :   gfc_component *c;
   13748              : 
   13749          381 :   if (ts.type != BT_DERIVED)
   13750              :     return false;
   13751              : 
   13752              :   /* (1) Check for FINAL subroutines.  */
   13753          381 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   13754              :     return true;
   13755              : 
   13756              :   /* (2) Check for components of finalizable type.  */
   13757          809 :   for (c = ts.u.derived->components; c; c = c->next)
   13758          470 :     if (c->ts.type == BT_DERIVED
   13759          243 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   13760          242 :         && c->ts.u.derived->f2k_derived
   13761          242 :         && c->ts.u.derived->f2k_derived->finalizers)
   13762              :       return true;
   13763              : 
   13764              :   return false;
   13765              : }
   13766              : 
   13767              : /* The temporary assignments have to be put on top of the additional
   13768              :    code to avoid the result being changed by the intrinsic assignment.
   13769              :    */
   13770              : static int component_assignment_level = 0;
   13771              : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   13772              : static bool finalizable_comp;
   13773              : 
   13774              : static void
   13775          188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   13776              : {
   13777          188 :   gfc_component *comp1, *comp2;
   13778          188 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   13779          188 :   gfc_code *tmp_code = NULL;
   13780          188 :   gfc_expr *t1 = NULL;
   13781          188 :   gfc_expr *tmp_expr = NULL;
   13782          188 :   int error_count, depth;
   13783          188 :   bool finalizable_lhs;
   13784              : 
   13785          188 :   gfc_get_errors (NULL, &error_count);
   13786              : 
   13787              :   /* Filter out continuing processing after an error.  */
   13788          188 :   if (error_count
   13789          188 :       || (*code)->expr1->ts.type != BT_DERIVED
   13790          188 :       || (*code)->expr2->ts.type != BT_DERIVED)
   13791          140 :     return;
   13792              : 
   13793              :   /* TODO: Handle more than one part array reference in assignments.  */
   13794          188 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   13795          188 :                                       (*code)->expr1->rank ? 1 : 0);
   13796          188 :   if (depth > 1)
   13797              :     {
   13798            6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   13799              :                    "done because multiple part array references would "
   13800              :                    "occur in intermediate expressions.", &(*code)->loc);
   13801            6 :       return;
   13802              :     }
   13803              : 
   13804          182 :   if (!component_assignment_level)
   13805          134 :     finalizable_comp = true;
   13806              : 
   13807              :   /* Build a block so that function result temporaries are finalized
   13808              :      locally on exiting the rather than enclosing scope.  */
   13809          182 :   if (!component_assignment_level)
   13810              :     {
   13811          134 :       ns = gfc_build_block_ns (ns);
   13812          134 :       tmp_code = gfc_get_code (EXEC_NOP);
   13813          134 :       *tmp_code = **code;
   13814          134 :       tmp_code->next = NULL;
   13815          134 :       (*code)->op = EXEC_BLOCK;
   13816          134 :       (*code)->ext.block.ns = ns;
   13817          134 :       (*code)->ext.block.assoc = NULL;
   13818          134 :       (*code)->expr1 = (*code)->expr2 = NULL;
   13819          134 :       ns->code = tmp_code;
   13820          134 :       code = &ns->code;
   13821              :     }
   13822              : 
   13823          182 :   component_assignment_level++;
   13824              : 
   13825          182 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   13826              : 
   13827              :   /* Create a temporary so that functions get called only once.  */
   13828          182 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   13829          182 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   13830              :     {
   13831              :       /* Assign the rhs to the temporary.  */
   13832           81 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   13833           81 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   13834              :         {
   13835              :           /* Use allocate on assignment for the sake of simplicity. The
   13836              :              temporary must not take on the optional attribute. Assume
   13837              :              that the assignment is guarded by a PRESENT condition if the
   13838              :              lhs is optional.  */
   13839           25 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   13840           25 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   13841           25 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   13842              :         }
   13843          162 :       this_code = build_assignment (EXEC_ASSIGN,
   13844              :                                     tmp_expr, (*code)->expr2,
   13845           81 :                                     NULL, NULL, (*code)->loc);
   13846           81 :       this_code->expr2->must_finalize = 1;
   13847              :       /* Add the code and substitute the rhs expression.  */
   13848           81 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   13849           81 :       gfc_free_expr ((*code)->expr2);
   13850           81 :       (*code)->expr2 = tmp_expr;
   13851              :     }
   13852              : 
   13853              :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   13854              :      of the temporaries generated here, since the intrinsic assignment
   13855              :      to the final result already does this.  */
   13856          182 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   13857              :     {
   13858          182 :       if (finalizable_lhs)
   13859           18 :         (*code)->expr1->must_finalize = 1;
   13860          182 :       this_code = build_assignment (EXEC_ASSIGN,
   13861              :                                     (*code)->expr1, (*code)->expr2,
   13862              :                                     NULL, NULL, (*code)->loc);
   13863          182 :       add_code_to_chain (&this_code, &head, &tail);
   13864              :     }
   13865              : 
   13866          182 :   comp1 = (*code)->expr1->ts.u.derived->components;
   13867          182 :   comp2 = (*code)->expr2->ts.u.derived->components;
   13868              : 
   13869          449 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   13870              :     {
   13871          267 :       bool inout = false;
   13872          267 :       bool finalizable_out = false;
   13873              : 
   13874              :       /* The intrinsic assignment does the right thing for pointers
   13875              :          of all kinds and allocatable components.  */
   13876          267 :       if (!gfc_bt_struct (comp1->ts.type)
   13877          200 :           || comp1->attr.pointer
   13878          200 :           || comp1->attr.allocatable
   13879          199 :           || comp1->attr.proc_pointer_comp
   13880          199 :           || comp1->attr.class_pointer
   13881          199 :           || comp1->attr.proc_pointer)
   13882           68 :         continue;
   13883              : 
   13884          398 :       finalizable_comp = is_finalizable_type (comp1->ts)
   13885          199 :                          && !finalizable_lhs;
   13886              : 
   13887              :       /* Make an assignment for this component.  */
   13888          398 :       this_code = build_assignment (EXEC_ASSIGN,
   13889              :                                     (*code)->expr1, (*code)->expr2,
   13890          199 :                                     comp1, comp2, (*code)->loc);
   13891              : 
   13892              :       /* Convert the assignment if there is a defined assignment for
   13893              :          this type.  Otherwise, using the call from gfc_resolve_code,
   13894              :          recurse into its components.  */
   13895          199 :       gfc_resolve_code (this_code, ns);
   13896              : 
   13897          199 :       if (this_code->op == EXEC_ASSIGN_CALL)
   13898              :         {
   13899          144 :           gfc_formal_arglist *dummy_args;
   13900          144 :           gfc_symbol *rsym;
   13901              :           /* Check that there is a typebound defined assignment.  If not,
   13902              :              then this must be a module defined assignment.  We cannot
   13903              :              use the defined_assign_comp attribute here because it must
   13904              :              be this derived type that has the defined assignment and not
   13905              :              a parent type.  */
   13906          144 :           if (!(comp1->ts.u.derived->f2k_derived
   13907              :                 && comp1->ts.u.derived->f2k_derived
   13908          144 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   13909              :             {
   13910            1 :               gfc_free_statements (this_code);
   13911            1 :               this_code = NULL;
   13912            1 :               continue;
   13913              :             }
   13914              : 
   13915              :           /* If the first argument of the subroutine has intent INOUT
   13916              :              a temporary must be generated and used instead.  */
   13917          143 :           rsym = this_code->resolved_sym;
   13918          143 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   13919          268 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   13920           18 :                             && dummy_args
   13921          161 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   13922          286 :           inout = dummy_args
   13923          268 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   13924           72 :           if ((inout || finalizable_out)
   13925           89 :               && !comp1->attr.allocatable)
   13926              :             {
   13927           89 :               gfc_code *temp_code;
   13928           89 :               inout = true;
   13929              : 
   13930              :               /* Build the temporary required for the assignment and put
   13931              :                  it at the head of the generated code.  */
   13932           89 :               if (!t1)
   13933              :                 {
   13934           89 :                   gfc_namespace *tmp_ns = ns;
   13935           89 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   13936           18 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   13937           89 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   13938           89 :                   t1->symtree->n.sym->attr.artificial = 1;
   13939          178 :                   temp_code = build_assignment (EXEC_ASSIGN,
   13940              :                                                 t1, (*code)->expr1,
   13941           89 :                                 NULL, NULL, (*code)->loc);
   13942              : 
   13943              :                   /* For allocatable LHS, check whether it is allocated.  Note
   13944              :                      that allocatable components with defined assignment are
   13945              :                      not yet support.  See PR 57696.  */
   13946           89 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   13947              :                     {
   13948           24 :                       gfc_code *block;
   13949           24 :                       gfc_expr *e =
   13950           24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13951           24 :                       block = gfc_get_code (EXEC_IF);
   13952           24 :                       block->block = gfc_get_code (EXEC_IF);
   13953           24 :                       block->block->expr1
   13954           48 :                           = gfc_build_intrinsic_call (ns,
   13955              :                                     GFC_ISYM_ALLOCATED, "allocated",
   13956           24 :                                     (*code)->loc, 1, e);
   13957           24 :                       block->block->next = temp_code;
   13958           24 :                       temp_code = block;
   13959              :                     }
   13960           89 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   13961              :                 }
   13962              : 
   13963              :               /* Replace the first actual arg with the component of the
   13964              :                  temporary.  */
   13965           89 :               gfc_free_expr (this_code->ext.actual->expr);
   13966           89 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   13967           89 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   13968              : 
   13969              :               /* If the LHS variable is allocatable and wasn't allocated and
   13970              :                  the temporary is allocatable, pointer assign the address of
   13971              :                  the freshly allocated LHS to the temporary.  */
   13972           89 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   13973           89 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   13974              :                 {
   13975           18 :                   gfc_code *block;
   13976           18 :                   gfc_expr *cond;
   13977              : 
   13978           18 :                   cond = gfc_get_expr ();
   13979           18 :                   cond->ts.type = BT_LOGICAL;
   13980           18 :                   cond->ts.kind = gfc_default_logical_kind;
   13981           18 :                   cond->expr_type = EXPR_OP;
   13982           18 :                   cond->where = (*code)->loc;
   13983           18 :                   cond->value.op.op = INTRINSIC_NOT;
   13984           18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   13985              :                                           GFC_ISYM_ALLOCATED, "allocated",
   13986           18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   13987           18 :                   block = gfc_get_code (EXEC_IF);
   13988           18 :                   block->block = gfc_get_code (EXEC_IF);
   13989           18 :                   block->block->expr1 = cond;
   13990           36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13991              :                                         t1, (*code)->expr1,
   13992           18 :                                         NULL, NULL, (*code)->loc);
   13993           18 :                   add_code_to_chain (&block, &head, &tail);
   13994              :                 }
   13995              :             }
   13996              :         }
   13997           55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   13998              :         {
   13999              :           /* Don't add intrinsic assignments since they are already
   14000              :              effected by the intrinsic assignment of the structure, unless
   14001              :              finalization is required.  */
   14002            7 :           if (finalizable_comp)
   14003            0 :             this_code->expr1->must_finalize = 1;
   14004              :           else
   14005              :             {
   14006            7 :               gfc_free_statements (this_code);
   14007            7 :               this_code = NULL;
   14008            7 :               continue;
   14009              :             }
   14010              :         }
   14011              :       else
   14012              :         {
   14013              :           /* Resolution has expanded an assignment of a derived type with
   14014              :              defined assigned components.  Remove the redundant, leading
   14015              :              assignment.  */
   14016           48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   14017           48 :           gfc_code *tmp = this_code;
   14018           48 :           this_code = this_code->next;
   14019           48 :           tmp->next = NULL;
   14020           48 :           gfc_free_statements (tmp);
   14021              :         }
   14022              : 
   14023          191 :       add_code_to_chain (&this_code, &head, &tail);
   14024              : 
   14025          191 :       if (t1 && (inout || finalizable_out))
   14026              :         {
   14027              :           /* Transfer the value to the final result.  */
   14028          178 :           this_code = build_assignment (EXEC_ASSIGN,
   14029              :                                         (*code)->expr1, t1,
   14030           89 :                                         comp1, comp2, (*code)->loc);
   14031           89 :           this_code->expr1->must_finalize = 0;
   14032           89 :           add_code_to_chain (&this_code, &head, &tail);
   14033              :         }
   14034              :     }
   14035              : 
   14036              :   /* Put the temporary assignments at the top of the generated code.  */
   14037          182 :   if (tmp_head && component_assignment_level == 1)
   14038              :     {
   14039          126 :       gfc_append_code (tmp_head, head);
   14040          126 :       head = tmp_head;
   14041          126 :       tmp_head = tmp_tail = NULL;
   14042              :     }
   14043              : 
   14044              :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   14045              :      not accidentally deallocated. Hence, nullify t1.  */
   14046           89 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   14047          271 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   14048              :     {
   14049           18 :       gfc_code *block;
   14050           18 :       gfc_expr *cond;
   14051           18 :       gfc_expr *e;
   14052              : 
   14053           18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14054           18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   14055           18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   14056           18 :       block = gfc_get_code (EXEC_IF);
   14057           18 :       block->block = gfc_get_code (EXEC_IF);
   14058           18 :       block->block->expr1 = cond;
   14059           18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14060              :                                         t1, gfc_get_null_expr (&(*code)->loc),
   14061           18 :                                         NULL, NULL, (*code)->loc);
   14062           18 :       gfc_append_code (tail, block);
   14063           18 :       tail = block;
   14064              :     }
   14065              : 
   14066          182 :   component_assignment_level--;
   14067              : 
   14068              :   /* Make an explicit final call for the function result.  */
   14069          182 :   if (tmp_expr)
   14070           81 :     generate_final_call (tmp_expr, &head, &tail);
   14071              : 
   14072          182 :   if (tmp_code)
   14073              :     {
   14074          134 :       ns->code = head;
   14075          134 :       return;
   14076              :     }
   14077              : 
   14078              :   /* Now attach the remaining code chain to the input code.  Step on
   14079              :      to the end of the new code since resolution is complete.  */
   14080           48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   14081           48 :   tail->next = (*code)->next;
   14082              :   /* Overwrite 'code' because this would place the intrinsic assignment
   14083              :      before the temporary for the lhs is created.  */
   14084           48 :   gfc_free_expr ((*code)->expr1);
   14085           48 :   gfc_free_expr ((*code)->expr2);
   14086           48 :   **code = *head;
   14087           48 :   if (head != tail)
   14088           48 :     free (head);
   14089           48 :   *code = tail;
   14090              : }
   14091              : 
   14092              : 
   14093              : /* F2008: Pointer function assignments are of the form:
   14094              :         ptr_fcn (args) = expr
   14095              :    This function breaks these assignments into two statements:
   14096              :         temporary_pointer => ptr_fcn(args)
   14097              :         temporary_pointer = expr  */
   14098              : 
   14099              : static bool
   14100       287069 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   14101              : {
   14102       287069 :   gfc_expr *tmp_ptr_expr;
   14103       287069 :   gfc_code *this_code;
   14104       287069 :   gfc_component *comp;
   14105       287069 :   gfc_symbol *s;
   14106              : 
   14107       287069 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   14108              :     return false;
   14109              : 
   14110              :   /* Even if standard does not support this feature, continue to build
   14111              :      the two statements to avoid upsetting frontend_passes.c.  */
   14112          205 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   14113              :                   "%L", &(*code)->loc);
   14114              : 
   14115          205 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   14116              : 
   14117          205 :   if (comp)
   14118            6 :     s = comp->ts.interface;
   14119              :   else
   14120          199 :     s = (*code)->expr1->symtree->n.sym;
   14121              : 
   14122          205 :   if (s == NULL || !s->result->attr.pointer)
   14123              :     {
   14124            5 :       gfc_error ("The function result on the lhs of the assignment at "
   14125              :                  "%L must have the pointer attribute.",
   14126            5 :                  &(*code)->expr1->where);
   14127            5 :       (*code)->op = EXEC_NOP;
   14128            5 :       return false;
   14129              :     }
   14130              : 
   14131          200 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   14132              : 
   14133              :   /* get_temp_from_expression is set up for ordinary assignments. To that
   14134              :      end, where array bounds are not known, arrays are made allocatable.
   14135              :      Change the temporary to a pointer here.  */
   14136          200 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   14137          200 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   14138          200 :   tmp_ptr_expr->where = (*code)->loc;
   14139              : 
   14140              :   /* A new charlen is required to ensure that the variable string length
   14141              :      is different to that of the original lhs for deferred results.  */
   14142          200 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   14143              :     {
   14144           60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   14145           60 :       tmp_ptr_expr->ts.deferred = 1;
   14146           60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   14147           60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   14148           60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   14149              :     }
   14150              : 
   14151          400 :   this_code = build_assignment (EXEC_ASSIGN,
   14152              :                                 tmp_ptr_expr, (*code)->expr2,
   14153          200 :                                 NULL, NULL, (*code)->loc);
   14154          200 :   this_code->next = (*code)->next;
   14155          200 :   (*code)->next = this_code;
   14156          200 :   (*code)->op = EXEC_POINTER_ASSIGN;
   14157          200 :   (*code)->expr2 = (*code)->expr1;
   14158          200 :   (*code)->expr1 = tmp_ptr_expr;
   14159              : 
   14160          200 :   return true;
   14161              : }
   14162              : 
   14163              : 
   14164              : /* Deferred character length assignments from an operator expression
   14165              :    require a temporary because the character length of the lhs can
   14166              :    change in the course of the assignment.  */
   14167              : 
   14168              : static bool
   14169       285907 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   14170              : {
   14171       285907 :   gfc_expr *tmp_expr;
   14172       285907 :   gfc_code *this_code;
   14173              : 
   14174       285907 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   14175        27369 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   14176          836 :          && (*code)->expr2->ts.type == BT_CHARACTER
   14177          835 :          && (*code)->expr2->expr_type == EXPR_OP))
   14178              :     return false;
   14179              : 
   14180           34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   14181              :     return false;
   14182              : 
   14183           28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   14184              :     return false;
   14185              : 
   14186           22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14187           22 :   tmp_expr->where = (*code)->loc;
   14188              : 
   14189              :   /* A new charlen is required to ensure that the variable string
   14190              :      length is different to that of the original lhs.  */
   14191           22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   14192           22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   14193           22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   14194           22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   14195              : 
   14196           22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   14197              : 
   14198           22 :   this_code = build_assignment (EXEC_ASSIGN,
   14199           22 :                                 (*code)->expr1,
   14200              :                                 gfc_copy_expr (tmp_expr),
   14201              :                                 NULL, NULL, (*code)->loc);
   14202              : 
   14203           22 :   (*code)->expr1 = tmp_expr;
   14204              : 
   14205           22 :   this_code->next = (*code)->next;
   14206           22 :   (*code)->next = this_code;
   14207              : 
   14208           22 :   return true;
   14209              : }
   14210              : 
   14211              : static void mark_lhs_assignments_set (gfc_code *code);
   14212              : 
   14213              : /* Given a block of code, recursively resolve everything pointed to by this
   14214              :    code block.  */
   14215              : 
   14216              : void
   14217       682643 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   14218              : {
   14219       682643 :   int omp_workshare_save;
   14220       682643 :   int forall_save, do_concurrent_save;
   14221       682643 :   code_stack frame;
   14222       682643 :   bool t;
   14223       682643 :   gfc_code *orig_code = code;
   14224              : 
   14225       682643 :   frame.prev = cs_base;
   14226       682643 :   frame.head = code;
   14227       682643 :   cs_base = &frame;
   14228              : 
   14229       682643 :   find_reachable_labels (code);
   14230              : 
   14231      1823307 :   for (; code; code = code->next)
   14232              :     {
   14233      1140665 :       frame.current = code;
   14234      1140665 :       forall_save = forall_flag;
   14235      1140665 :       do_concurrent_save = gfc_do_concurrent_flag;
   14236              : 
   14237      1140665 :       if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
   14238              :         {
   14239         2217 :           if (code->op == EXEC_FORALL)
   14240         1993 :             forall_flag = 1;
   14241          224 :           else if (code->op == EXEC_DO_CONCURRENT)
   14242          224 :             gfc_do_concurrent_flag = 1;
   14243         2217 :           gfc_resolve_forall (code, ns, forall_save);
   14244         2217 :           if (code->op == EXEC_FORALL)
   14245         1993 :             forall_flag = 2;
   14246          224 :           else if (code->op == EXEC_DO_CONCURRENT)
   14247          224 :             gfc_do_concurrent_flag = 2;
   14248              :         }
   14249      1138448 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   14250          138 :         for (gfc_omp_variant *variant
   14251              :                = code->ext.omp_variants;
   14252          448 :              variant; variant = variant->next)
   14253          310 :           gfc_resolve_code (variant->code, ns);
   14254      1138310 :       else if (code->block)
   14255              :         {
   14256       331264 :           omp_workshare_save = -1;
   14257       331264 :           switch (code->op)
   14258              :             {
   14259        10119 :             case EXEC_OACC_PARALLEL_LOOP:
   14260        10119 :             case EXEC_OACC_PARALLEL:
   14261        10119 :             case EXEC_OACC_KERNELS_LOOP:
   14262        10119 :             case EXEC_OACC_KERNELS:
   14263        10119 :             case EXEC_OACC_SERIAL_LOOP:
   14264        10119 :             case EXEC_OACC_SERIAL:
   14265        10119 :             case EXEC_OACC_DATA:
   14266        10119 :             case EXEC_OACC_HOST_DATA:
   14267        10119 :             case EXEC_OACC_LOOP:
   14268        10119 :               gfc_resolve_oacc_blocks (code, ns);
   14269        10119 :               break;
   14270           54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   14271           54 :               omp_workshare_save = omp_workshare_flag;
   14272           54 :               omp_workshare_flag = 1;
   14273           54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14274           54 :               break;
   14275         5992 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14276         5992 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14277         5992 :             case EXEC_OMP_MASKED_TASKLOOP:
   14278         5992 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14279         5992 :             case EXEC_OMP_MASTER_TASKLOOP:
   14280         5992 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14281         5992 :             case EXEC_OMP_PARALLEL:
   14282         5992 :             case EXEC_OMP_PARALLEL_DO:
   14283         5992 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   14284         5992 :             case EXEC_OMP_PARALLEL_LOOP:
   14285         5992 :             case EXEC_OMP_PARALLEL_MASKED:
   14286         5992 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14287         5992 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14288         5992 :             case EXEC_OMP_PARALLEL_MASTER:
   14289         5992 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14290         5992 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14291         5992 :             case EXEC_OMP_PARALLEL_SECTIONS:
   14292         5992 :             case EXEC_OMP_TARGET_PARALLEL:
   14293         5992 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   14294         5992 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14295         5992 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14296         5992 :             case EXEC_OMP_TARGET_TEAMS:
   14297         5992 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14298         5992 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14299         5992 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14300         5992 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14301         5992 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   14302         5992 :             case EXEC_OMP_TASK:
   14303         5992 :             case EXEC_OMP_TASKLOOP:
   14304         5992 :             case EXEC_OMP_TASKLOOP_SIMD:
   14305         5992 :             case EXEC_OMP_TEAMS:
   14306         5992 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   14307         5992 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14308         5992 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14309         5992 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14310         5992 :             case EXEC_OMP_TEAMS_LOOP:
   14311         5992 :               omp_workshare_save = omp_workshare_flag;
   14312         5992 :               omp_workshare_flag = 0;
   14313         5992 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14314         5992 :               break;
   14315         3063 :             case EXEC_OMP_DISTRIBUTE:
   14316         3063 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   14317         3063 :             case EXEC_OMP_DO:
   14318         3063 :             case EXEC_OMP_DO_SIMD:
   14319         3063 :             case EXEC_OMP_LOOP:
   14320         3063 :             case EXEC_OMP_SIMD:
   14321         3063 :             case EXEC_OMP_TARGET_SIMD:
   14322         3063 :             case EXEC_OMP_TILE:
   14323         3063 :             case EXEC_OMP_UNROLL:
   14324         3063 :               gfc_resolve_omp_do_blocks (code, ns);
   14325         3063 :               break;
   14326              :             case EXEC_SELECT_TYPE:
   14327              :             case EXEC_SELECT_RANK:
   14328              :               /* Blocks are handled in resolve_select_type/rank because we
   14329              :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   14330              :               break;
   14331              :             case EXEC_DO_CONCURRENT:
   14332              :               gfc_do_concurrent_flag = 1;
   14333              :               gfc_resolve_blocks (code->block, ns);
   14334              :               gfc_do_concurrent_flag = 2;
   14335              :               break;
   14336           39 :             case EXEC_OMP_WORKSHARE:
   14337           39 :               omp_workshare_save = omp_workshare_flag;
   14338           39 :               omp_workshare_flag = 1;
   14339              :               /* FALL THROUGH */
   14340       307987 :             default:
   14341       307987 :               gfc_resolve_blocks (code->block, ns);
   14342       307987 :               break;
   14343              :             }
   14344              : 
   14345       327215 :           if (omp_workshare_save != -1)
   14346         6085 :             omp_workshare_flag = omp_workshare_save;
   14347              :         }
   14348       807046 : start:
   14349      1140870 :       t = true;
   14350      1140870 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   14351      1139439 :           t = gfc_resolve_expr (code->expr1);
   14352              : 
   14353      1140870 :       forall_flag = forall_save;
   14354      1140870 :       gfc_do_concurrent_flag = do_concurrent_save;
   14355              : 
   14356      1140870 :       if (!gfc_resolve_expr (code->expr2))
   14357          637 :         t = false;
   14358              : 
   14359      1140870 :       if (code->op == EXEC_ALLOCATE
   14360      1140870 :           && !gfc_resolve_expr (code->expr3))
   14361              :         t = false;
   14362              : 
   14363      1140870 :       switch (code->op)
   14364              :         {
   14365              :         case EXEC_NOP:
   14366              :         case EXEC_END_BLOCK:
   14367              :         case EXEC_END_NESTED_BLOCK:
   14368              :         case EXEC_CYCLE:
   14369              :         case EXEC_PAUSE:
   14370              :           break;
   14371              : 
   14372       218426 :         case EXEC_STOP:
   14373       218426 :         case EXEC_ERROR_STOP:
   14374       218426 :           if (code->expr2 != NULL
   14375           37 :               && (code->expr2->ts.type != BT_LOGICAL
   14376           37 :                   || code->expr2->rank != 0))
   14377            0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   14378              :                        &code->expr2->where);
   14379              :           break;
   14380              : 
   14381              :         case EXEC_EXIT:
   14382              :         case EXEC_CONTINUE:
   14383              :         case EXEC_DT_END:
   14384              :         case EXEC_ASSIGN_CALL:
   14385              :           break;
   14386              : 
   14387           54 :         case EXEC_CRITICAL:
   14388           54 :           resolve_critical (code);
   14389           54 :           break;
   14390              : 
   14391         1317 :         case EXEC_SYNC_ALL:
   14392         1317 :         case EXEC_SYNC_IMAGES:
   14393         1317 :         case EXEC_SYNC_MEMORY:
   14394         1317 :           resolve_sync (code);
   14395         1317 :           break;
   14396              : 
   14397          197 :         case EXEC_LOCK:
   14398          197 :         case EXEC_UNLOCK:
   14399          197 :         case EXEC_EVENT_POST:
   14400          197 :         case EXEC_EVENT_WAIT:
   14401          197 :           resolve_lock_unlock_event (code);
   14402          197 :           break;
   14403              : 
   14404              :         case EXEC_FAIL_IMAGE:
   14405              :           break;
   14406              : 
   14407          130 :         case EXEC_FORM_TEAM:
   14408          130 :           resolve_form_team (code);
   14409          130 :           break;
   14410              : 
   14411           73 :         case EXEC_CHANGE_TEAM:
   14412           73 :           resolve_change_team (code);
   14413           73 :           break;
   14414              : 
   14415           71 :         case EXEC_END_TEAM:
   14416           71 :           resolve_end_team (code);
   14417           71 :           break;
   14418              : 
   14419           43 :         case EXEC_SYNC_TEAM:
   14420           43 :           resolve_sync_team (code);
   14421           43 :           break;
   14422              : 
   14423         1491 :         case EXEC_ENTRY:
   14424              :           /* Keep track of which entry we are up to.  */
   14425         1491 :           current_entry_id = code->ext.entry->id;
   14426         1491 :           break;
   14427              : 
   14428          453 :         case EXEC_WHERE:
   14429          453 :           resolve_where (code, NULL);
   14430          453 :           break;
   14431              : 
   14432         1250 :         case EXEC_GOTO:
   14433         1250 :           if (code->expr1 != NULL)
   14434              :             {
   14435           78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   14436           76 :                   || code->expr1->ts.type != BT_INTEGER
   14437           76 :                   || (code->expr1->ref
   14438            1 :                       && code->expr1->ref->type == REF_ARRAY)
   14439           75 :                   || code->expr1->symtree == NULL
   14440           75 :                   || (code->expr1->symtree->n.sym
   14441           75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   14442           75 :                           == FL_PARAMETER)))
   14443            4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   14444              :                            "scalar INTEGER variable", &code->expr1->where);
   14445           74 :               else if (code->expr1->symtree->n.sym
   14446           74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   14447            1 :                 gfc_error ("Variable %qs has not been assigned a target "
   14448              :                            "label at %L", code->expr1->symtree->n.sym->name,
   14449              :                            &code->expr1->where);
   14450              :             }
   14451              :           else
   14452         1172 :             resolve_branch (code->label1, code);
   14453              :           break;
   14454              : 
   14455         3224 :         case EXEC_RETURN:
   14456         3224 :           if (code->expr1 != NULL
   14457           53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   14458            1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   14459              :                        "INTEGER return specifier", &code->expr1->where);
   14460              :           break;
   14461              : 
   14462              :         case EXEC_INIT_ASSIGN:
   14463              :         case EXEC_END_PROCEDURE:
   14464              :           break;
   14465              : 
   14466       288244 :         case EXEC_ASSIGN:
   14467       288244 :           if (!t)
   14468              :             break;
   14469              : 
   14470       287569 :           if (flag_coarray == GFC_FCOARRAY_LIB
   14471       287569 :               && gfc_is_coindexed (code->expr1))
   14472              :             {
   14473              :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   14474              :                  coindexed variable.  */
   14475          500 :               code->op = EXEC_CALL;
   14476          500 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   14477              :                                 true);
   14478          500 :               code->resolved_sym = code->symtree->n.sym;
   14479          500 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   14480          500 :               code->resolved_sym->attr.intrinsic = 1;
   14481          500 :               code->resolved_sym->attr.subroutine = 1;
   14482          500 :               code->resolved_isym
   14483          500 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   14484          500 :               gfc_commit_symbol (code->resolved_sym);
   14485          500 :               code->ext.actual = gfc_get_actual_arglist ();
   14486          500 :               code->ext.actual->expr = code->expr1;
   14487          500 :               code->ext.actual->next = gfc_get_actual_arglist ();
   14488          500 :               if (code->expr2->expr_type != EXPR_VARIABLE
   14489          500 :                   && code->expr2->expr_type != EXPR_CONSTANT)
   14490              :                 {
   14491              :                   /* Convert assignments of expr1[...] = expr2 into
   14492              :                         tvar = expr2
   14493              :                         expr1[...] = tvar
   14494              :                      when expr2 is not trivial.  */
   14495           54 :                   gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
   14496           54 :                   gfc_code next_code = *code;
   14497           54 :                   gfc_code *rhs_code
   14498          108 :                     = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
   14499           54 :                                         NULL, code->expr2->where);
   14500           54 :                   *code = *rhs_code;
   14501           54 :                   code->next = rhs_code;
   14502           54 :                   *rhs_code = next_code;
   14503              : 
   14504           54 :                   rhs_code->ext.actual->next->expr = tvar;
   14505           54 :                   rhs_code->expr1 = NULL;
   14506           54 :                   rhs_code->expr2 = NULL;
   14507              :                 }
   14508              :               else
   14509              :                 {
   14510          446 :                   code->ext.actual->next->expr = code->expr2;
   14511              : 
   14512          446 :                   code->expr1 = NULL;
   14513          446 :                   code->expr2 = NULL;
   14514              :                 }
   14515              :               break;
   14516              :             }
   14517              : 
   14518       287069 :           if (code->expr1->ts.type == BT_CLASS)
   14519         1114 :             gfc_find_vtab (&code->expr2->ts);
   14520              : 
   14521              :           /* If this is a pointer function in an lvalue variable context,
   14522              :              the new code will have to be resolved afresh. This is also the
   14523              :              case with an error, where the code is transformed into NOP to
   14524              :              prevent ICEs downstream.  */
   14525       287069 :           if (resolve_ptr_fcn_assign (&code, ns)
   14526       287069 :               || code->op == EXEC_NOP)
   14527          205 :             goto start;
   14528              : 
   14529       286864 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   14530       286864 :                                          _("assignment")))
   14531              :             break;
   14532              : 
   14533       286825 :           if (resolve_ordinary_assign (code, ns))
   14534              :             {
   14535          918 :               if (omp_workshare_flag)
   14536              :                 {
   14537            1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   14538            1 :                              "at %L", &code->loc);
   14539            1 :                   break;
   14540              :                 }
   14541          917 :               if (code->op == EXEC_COMPCALL)
   14542          449 :                 goto compcall;
   14543              :               else
   14544          468 :                 goto call;
   14545              :             }
   14546              : 
   14547              :           /* Check for dependencies in deferred character length array
   14548              :              assignments and generate a temporary, if necessary.  */
   14549       285907 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   14550              :             break;
   14551              : 
   14552              :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   14553       285885 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   14554         7278 :               && code->expr1->ts.u.derived
   14555         7278 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   14556          188 :             generate_component_assignments (&code, ns);
   14557       285697 :           else if (code->op == EXEC_ASSIGN)
   14558              :             {
   14559       285697 :               if (gfc_may_be_finalized (code->expr1->ts))
   14560         1289 :                 code->expr1->must_finalize = 1;
   14561       285697 :               if (code->expr2->expr_type == EXPR_ARRAY
   14562       285697 :                   && gfc_may_be_finalized (code->expr2->ts))
   14563           73 :                 code->expr2->must_finalize = 1;
   14564              :             }
   14565              : 
   14566              :           break;
   14567              : 
   14568          126 :         case EXEC_LABEL_ASSIGN:
   14569          126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   14570            0 :             gfc_error ("Label %d referenced at %L is never defined",
   14571              :                        code->label1->value, &code->label1->where);
   14572          126 :           if (t
   14573          126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   14574          126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   14575          126 :                   || code->expr1->symtree->n.sym->ts.kind
   14576          126 :                      != gfc_default_integer_kind
   14577          126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   14578          125 :                   || code->expr1->symtree->n.sym->as != NULL))
   14579            2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   14580              :                        "default INTEGER variable", &code->expr1->where);
   14581              :           break;
   14582              : 
   14583        10452 :         case EXEC_POINTER_ASSIGN:
   14584        10452 :           {
   14585        10452 :             gfc_expr* e;
   14586              : 
   14587        10452 :             if (!t)
   14588              :               break;
   14589              : 
   14590              :             /* This is both a variable definition and pointer assignment
   14591              :                context, so check both of them.  For rank remapping, a final
   14592              :                array ref may be present on the LHS and fool gfc_expr_attr
   14593              :                used in gfc_check_vardef_context.  Remove it.  */
   14594        10447 :             e = remove_last_array_ref (code->expr1);
   14595        20894 :             t = gfc_check_vardef_context (e, true, false, false,
   14596        10447 :                                           _("pointer assignment"));
   14597        10447 :             if (t)
   14598        10418 :               t = gfc_check_vardef_context (e, false, false, false,
   14599        10418 :                                             _("pointer assignment"));
   14600        10447 :             gfc_free_expr (e);
   14601              : 
   14602        10447 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   14603              : 
   14604        10305 :             if (!t)
   14605              :               break;
   14606              : 
   14607              :             /* Assigning a class object always is a regular assign.  */
   14608        10305 :             if (code->expr2->ts.type == BT_CLASS
   14609          582 :                 && code->expr1->ts.type == BT_CLASS
   14610          491 :                 && CLASS_DATA (code->expr2)
   14611          490 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   14612        10942 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   14613           55 :                      && code->expr2->expr_type == EXPR_VARIABLE
   14614           43 :                      && code->expr2->symtree->n.sym->attr.flavor
   14615           43 :                         == FL_PROCEDURE))
   14616          340 :               code->op = EXEC_ASSIGN;
   14617              :             break;
   14618              :           }
   14619              : 
   14620           72 :         case EXEC_ARITHMETIC_IF:
   14621           72 :           {
   14622           72 :             gfc_expr *e = code->expr1;
   14623              : 
   14624           72 :             gfc_resolve_expr (e);
   14625           72 :             if (e->expr_type == EXPR_NULL)
   14626            1 :               gfc_error ("Invalid NULL at %L", &e->where);
   14627              : 
   14628           72 :             if (t && (e->rank > 0
   14629           68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   14630            5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   14631              :                          "REAL or INTEGER expression", &e->where);
   14632              : 
   14633           72 :             resolve_branch (code->label1, code);
   14634           72 :             resolve_branch (code->label2, code);
   14635           72 :             resolve_branch (code->label3, code);
   14636              :           }
   14637           72 :           break;
   14638              : 
   14639       231884 :         case EXEC_IF:
   14640       231884 :           if (t && code->expr1 != NULL
   14641            0 :               && (code->expr1->ts.type != BT_LOGICAL
   14642            0 :                   || code->expr1->rank != 0))
   14643            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   14644              :                        &code->expr1->where);
   14645              :           break;
   14646              : 
   14647        79931 :         case EXEC_CALL:
   14648        79931 :         call:
   14649        79931 :           resolve_call (code);
   14650        79931 :           break;
   14651              : 
   14652         1756 :         case EXEC_COMPCALL:
   14653         1756 :         compcall:
   14654         1756 :           resolve_typebound_subroutine (code);
   14655         1756 :           break;
   14656              : 
   14657          124 :         case EXEC_CALL_PPC:
   14658          124 :           resolve_ppc_call (code);
   14659          124 :           break;
   14660              : 
   14661          687 :         case EXEC_SELECT:
   14662              :           /* Select is complicated. Also, a SELECT construct could be
   14663              :              a transformed computed GOTO.  */
   14664          687 :           resolve_select (code, false);
   14665          687 :           break;
   14666              : 
   14667         3051 :         case EXEC_SELECT_TYPE:
   14668         3051 :           resolve_select_type (code, ns);
   14669         3051 :           break;
   14670              : 
   14671         1024 :         case EXEC_SELECT_RANK:
   14672         1024 :           resolve_select_rank (code, ns);
   14673         1024 :           break;
   14674              : 
   14675         8058 :         case EXEC_BLOCK:
   14676         8058 :           resolve_block_construct (code);
   14677         8058 :           break;
   14678              : 
   14679        32999 :         case EXEC_DO:
   14680        32999 :           if (code->ext.iterator != NULL)
   14681              :             {
   14682        32999 :               gfc_iterator *iter = code->ext.iterator;
   14683        32999 :               if (gfc_resolve_iterator (iter, true, false))
   14684        32985 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   14685              :                                          true);
   14686              :             }
   14687              :           break;
   14688              : 
   14689          531 :         case EXEC_DO_WHILE:
   14690          531 :           if (code->expr1 == NULL)
   14691            0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   14692              :                                 "DO WHILE");
   14693          531 :           if (t
   14694          531 :               && (code->expr1->rank != 0
   14695          531 :                   || code->expr1->ts.type != BT_LOGICAL))
   14696            0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   14697              :                        "a scalar LOGICAL expression", &code->expr1->where);
   14698              :           break;
   14699              : 
   14700        14383 :         case EXEC_ALLOCATE:
   14701        14383 :           if (t)
   14702        14381 :             resolve_allocate_deallocate (code, "ALLOCATE");
   14703              : 
   14704              :           break;
   14705              : 
   14706         6099 :         case EXEC_DEALLOCATE:
   14707         6099 :           if (t)
   14708         6099 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   14709              : 
   14710              :           break;
   14711              : 
   14712         3907 :         case EXEC_OPEN:
   14713         3907 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   14714              :             break;
   14715              : 
   14716         3680 :           resolve_branch (code->ext.open->err, code);
   14717         3680 :           break;
   14718              : 
   14719         3094 :         case EXEC_CLOSE:
   14720         3094 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   14721              :             break;
   14722              : 
   14723         3060 :           resolve_branch (code->ext.close->err, code);
   14724         3060 :           break;
   14725              : 
   14726         2809 :         case EXEC_BACKSPACE:
   14727         2809 :         case EXEC_ENDFILE:
   14728         2809 :         case EXEC_REWIND:
   14729         2809 :         case EXEC_FLUSH:
   14730         2809 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   14731              :             break;
   14732              : 
   14733         2743 :           resolve_branch (code->ext.filepos->err, code);
   14734         2743 :           break;
   14735              : 
   14736          838 :         case EXEC_INQUIRE:
   14737          838 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14738              :               break;
   14739              : 
   14740          790 :           resolve_branch (code->ext.inquire->err, code);
   14741          790 :           break;
   14742              : 
   14743           92 :         case EXEC_IOLENGTH:
   14744           92 :           gcc_assert (code->ext.inquire != NULL);
   14745           92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14746              :             break;
   14747              : 
   14748           90 :           resolve_branch (code->ext.inquire->err, code);
   14749           90 :           break;
   14750              : 
   14751           89 :         case EXEC_WAIT:
   14752           89 :           if (!gfc_resolve_wait (code->ext.wait))
   14753              :             break;
   14754              : 
   14755           74 :           resolve_branch (code->ext.wait->err, code);
   14756           74 :           resolve_branch (code->ext.wait->end, code);
   14757           74 :           resolve_branch (code->ext.wait->eor, code);
   14758           74 :           break;
   14759              : 
   14760        33285 :         case EXEC_READ:
   14761        33285 :         case EXEC_WRITE:
   14762        33285 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   14763              :             break;
   14764              : 
   14765        32977 :           resolve_branch (code->ext.dt->err, code);
   14766        32977 :           resolve_branch (code->ext.dt->end, code);
   14767        32977 :           resolve_branch (code->ext.dt->eor, code);
   14768        32977 :           break;
   14769              : 
   14770        47323 :         case EXEC_TRANSFER:
   14771        47323 :           resolve_transfer (code);
   14772        47323 :           break;
   14773              : 
   14774         2217 :         case EXEC_DO_CONCURRENT:
   14775         2217 :         case EXEC_FORALL:
   14776         2217 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   14777              : 
   14778         2217 :           if (code->expr1 != NULL
   14779          732 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   14780            2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   14781              :                        "expression", &code->expr1->where);
   14782              : 
   14783         2217 :     if (code->op == EXEC_DO_CONCURRENT)
   14784          224 :       resolve_locality_spec (code, ns);
   14785              :           break;
   14786              : 
   14787        13168 :         case EXEC_OACC_PARALLEL_LOOP:
   14788        13168 :         case EXEC_OACC_PARALLEL:
   14789        13168 :         case EXEC_OACC_KERNELS_LOOP:
   14790        13168 :         case EXEC_OACC_KERNELS:
   14791        13168 :         case EXEC_OACC_SERIAL_LOOP:
   14792        13168 :         case EXEC_OACC_SERIAL:
   14793        13168 :         case EXEC_OACC_DATA:
   14794        13168 :         case EXEC_OACC_HOST_DATA:
   14795        13168 :         case EXEC_OACC_LOOP:
   14796        13168 :         case EXEC_OACC_UPDATE:
   14797        13168 :         case EXEC_OACC_WAIT:
   14798        13168 :         case EXEC_OACC_CACHE:
   14799        13168 :         case EXEC_OACC_ENTER_DATA:
   14800        13168 :         case EXEC_OACC_EXIT_DATA:
   14801        13168 :         case EXEC_OACC_ATOMIC:
   14802        13168 :         case EXEC_OACC_DECLARE:
   14803        13168 :           gfc_resolve_oacc_directive (code, ns);
   14804        13168 :           break;
   14805              : 
   14806        17266 :         case EXEC_OMP_ALLOCATE:
   14807        17266 :         case EXEC_OMP_ALLOCATORS:
   14808        17266 :         case EXEC_OMP_ASSUME:
   14809        17266 :         case EXEC_OMP_ATOMIC:
   14810        17266 :         case EXEC_OMP_BARRIER:
   14811        17266 :         case EXEC_OMP_CANCEL:
   14812        17266 :         case EXEC_OMP_CANCELLATION_POINT:
   14813        17266 :         case EXEC_OMP_CRITICAL:
   14814        17266 :         case EXEC_OMP_FLUSH:
   14815        17266 :         case EXEC_OMP_DEPOBJ:
   14816        17266 :         case EXEC_OMP_DISPATCH:
   14817        17266 :         case EXEC_OMP_DISTRIBUTE:
   14818        17266 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14819        17266 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14820        17266 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   14821        17266 :         case EXEC_OMP_DO:
   14822        17266 :         case EXEC_OMP_DO_SIMD:
   14823        17266 :         case EXEC_OMP_ERROR:
   14824        17266 :         case EXEC_OMP_INTEROP:
   14825        17266 :         case EXEC_OMP_LOOP:
   14826        17266 :         case EXEC_OMP_MASTER:
   14827        17266 :         case EXEC_OMP_MASTER_TASKLOOP:
   14828        17266 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14829        17266 :         case EXEC_OMP_MASKED:
   14830        17266 :         case EXEC_OMP_MASKED_TASKLOOP:
   14831        17266 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14832        17266 :         case EXEC_OMP_METADIRECTIVE:
   14833        17266 :         case EXEC_OMP_ORDERED:
   14834        17266 :         case EXEC_OMP_SCAN:
   14835        17266 :         case EXEC_OMP_SCOPE:
   14836        17266 :         case EXEC_OMP_SECTIONS:
   14837        17266 :         case EXEC_OMP_SIMD:
   14838        17266 :         case EXEC_OMP_SINGLE:
   14839        17266 :         case EXEC_OMP_TARGET:
   14840        17266 :         case EXEC_OMP_TARGET_DATA:
   14841        17266 :         case EXEC_OMP_TARGET_ENTER_DATA:
   14842        17266 :         case EXEC_OMP_TARGET_EXIT_DATA:
   14843        17266 :         case EXEC_OMP_TARGET_PARALLEL:
   14844        17266 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   14845        17266 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14846        17266 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14847        17266 :         case EXEC_OMP_TARGET_SIMD:
   14848        17266 :         case EXEC_OMP_TARGET_TEAMS:
   14849        17266 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14850        17266 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14851        17266 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14852        17266 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14853        17266 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   14854        17266 :         case EXEC_OMP_TARGET_UPDATE:
   14855        17266 :         case EXEC_OMP_TASK:
   14856        17266 :         case EXEC_OMP_TASKGROUP:
   14857        17266 :         case EXEC_OMP_TASKLOOP:
   14858        17266 :         case EXEC_OMP_TASKLOOP_SIMD:
   14859        17266 :         case EXEC_OMP_TASKWAIT:
   14860        17266 :         case EXEC_OMP_TASKYIELD:
   14861        17266 :         case EXEC_OMP_TEAMS:
   14862        17266 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   14863        17266 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14864        17266 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14865        17266 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14866        17266 :         case EXEC_OMP_TEAMS_LOOP:
   14867        17266 :         case EXEC_OMP_TILE:
   14868        17266 :         case EXEC_OMP_UNROLL:
   14869        17266 :         case EXEC_OMP_WORKSHARE:
   14870        17266 :           gfc_resolve_omp_directive (code, ns);
   14871        17266 :           break;
   14872              : 
   14873         3903 :         case EXEC_OMP_PARALLEL:
   14874         3903 :         case EXEC_OMP_PARALLEL_DO:
   14875         3903 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   14876         3903 :         case EXEC_OMP_PARALLEL_LOOP:
   14877         3903 :         case EXEC_OMP_PARALLEL_MASKED:
   14878         3903 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14879         3903 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14880         3903 :         case EXEC_OMP_PARALLEL_MASTER:
   14881         3903 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14882         3903 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14883         3903 :         case EXEC_OMP_PARALLEL_SECTIONS:
   14884         3903 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   14885         3903 :           omp_workshare_save = omp_workshare_flag;
   14886         3903 :           omp_workshare_flag = 0;
   14887         3903 :           gfc_resolve_omp_directive (code, ns);
   14888         3903 :           omp_workshare_flag = omp_workshare_save;
   14889         3903 :           break;
   14890              : 
   14891            0 :         default:
   14892            0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   14893              :         }
   14894      1140664 :       gfc_value_used_expr (code->expr2, VALUE_USED);
   14895      1140664 :       gfc_value_used_expr (code->expr3, VALUE_USED);
   14896      1140664 :       gfc_value_used_expr (code->expr4, VALUE_USED);
   14897              :     }
   14898              : 
   14899       682642 :   mark_lhs_assignments_set (orig_code);
   14900              : 
   14901       682642 :   cs_base = frame.prev;
   14902       682642 : }
   14903              : 
   14904              : 
   14905              : /* Resolve initial values and make sure they are compatible with
   14906              :    the variable.  */
   14907              : 
   14908              : static void
   14909      1894819 : resolve_values (gfc_symbol *sym)
   14910              : {
   14911      1894819 :   bool t;
   14912              : 
   14913      1894819 :   if (sym->value == NULL)
   14914              :     return;
   14915              : 
   14916       441552 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   14917           14 :     gfc_warning (OPT_Wdeprecated_declarations,
   14918              :                  "Using parameter %qs declared at %L is deprecated",
   14919              :                  sym->name, &sym->declared_at);
   14920              : 
   14921       441552 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   14922        40276 :     t= resolve_structure_cons (sym->value, 1);
   14923              :   else
   14924       401276 :     t = gfc_resolve_expr (sym->value);
   14925              : 
   14926       441552 :   if (!t)
   14927              :     return;
   14928              : 
   14929       441550 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   14930              : }
   14931              : 
   14932              : 
   14933              : /* Verify any BIND(C) derived types in the namespace so we can report errors
   14934              :    for them once, rather than for each variable declared of that type.  */
   14935              : 
   14936              : static void
   14937      1865118 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   14938              : {
   14939      1865118 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   14940        84121 :       && derived_sym->attr.is_bind_c == 1)
   14941        27283 :     verify_bind_c_derived_type (derived_sym);
   14942              : 
   14943      1865118 :   return;
   14944              : }
   14945              : 
   14946              : 
   14947              : /* Check the interfaces of DTIO procedures associated with derived
   14948              :    type 'sym'.  These procedures can either have typebound bindings or
   14949              :    can appear in DTIO generic interfaces.  */
   14950              : 
   14951              : static void
   14952      1895789 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   14953              : {
   14954      1895789 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   14955              :     return;
   14956              : 
   14957        93550 :   gfc_check_dtio_interfaces (sym);
   14958              : 
   14959        93550 :   return;
   14960              : }
   14961              : 
   14962              : /* Auxiliary function, checks if an argument decays to a pointer.  */
   14963              : 
   14964              : static bool
   14965        67062 : decays_to_pointer (gfc_symbol *sym)
   14966              : {
   14967        67062 :   if (!sym->as)
   14968              :     return true;
   14969              : 
   14970        19557 :   if (sym->as->type == AS_ASSUMED_SHAPE)
   14971              :     return false;
   14972              : 
   14973        15806 :   if (sym->as->type == AS_ASSUMED_RANK)
   14974              :     return false;
   14975              : 
   14976        10708 :   if (sym->as->type == AS_DEFERRED && sym->attr.dummy)
   14977          964 :     return false;
   14978              : 
   14979              :   return true;
   14980              : }
   14981              : 
   14982              : /* Helper function, returns true if the types conform according to the C
   14983              :    standard, when they are not equal on the Fortran side.  If we decide to
   14984              :    include or exclude any types from this, this is the place to change.  */
   14985              : 
   14986              : static bool
   14987          390 : c_types_conform (gfc_typespec *ts1, gfc_typespec *ts2)
   14988              : {
   14989          390 :   if (ts1->type == BT_ASSUMED || ts2->type == BT_ASSUMED)
   14990              :     return true;
   14991              : 
   14992          384 :   if (ts1->kind == ts2->kind
   14993              :       && (ts1->type == BT_CHARACTER || ts1->type == BT_INTEGER
   14994              :           || ts1->type == BT_UNSIGNED)
   14995              :       && (ts2->type == BT_CHARACTER || ts2->type == BT_INTEGER
   14996              :           || ts2->type == BT_UNSIGNED))
   14997          384 :     return true;
   14998              : 
   14999              :   return false;
   15000              : 
   15001              : }
   15002              : 
   15003              : /* Check argument lists of BIND(C) procedures against each other, return
   15004              :    false if they do not. */
   15005              : 
   15006              : static bool
   15007        11625 : compare_c_binding_arglists (gfc_symbol *osym, gfc_symbol *nsym)
   15008              : {
   15009        11625 :   gfc_formal_arglist *oarg, *narg;
   15010        11625 :   bool ret = true;
   15011        11625 :   locus *oloc, *nloc;
   15012              : 
   15013        11625 :   oarg = osym->formal;
   15014        11625 :   narg = nsym->formal;
   15015        11625 :   oloc = &osym->declared_at;
   15016        11625 :   nloc = &nsym->declared_at;
   15017        45166 :   for ( ; oarg && narg ; oarg = oarg->next, narg = narg->next)
   15018              :     {
   15019        33541 :       oloc = &oarg->sym->declared_at;
   15020        33541 :       nloc = &narg->sym->declared_at;
   15021              : 
   15022        33541 :       if (!gfc_compare_types (&oarg->sym->ts, &narg->sym->ts)
   15023        33541 :           && (pedantic || !c_types_conform (&oarg->sym->ts, &narg->sym->ts)))
   15024              :         {
   15025           24 :           gfc_error ("Type mismatch in argument %qs at %L (%s/%s) "
   15026            8 :                      "originally declared at %L", narg->sym->name,
   15027            8 :                      nloc, gfc_typename (&narg->sym->ts),
   15028            8 :                      gfc_typename (&oarg->sym->ts), oloc);
   15029            8 :                      ret = false;
   15030            8 :                      continue;
   15031              :         }
   15032        33533 :       if (oarg->sym->attr.value != narg->sym->attr.value)
   15033              :         {
   15034            1 :           gfc_error ("VALUE attribute mismatch in argument %qs at %L "
   15035              :                      "originally declared at %L",narg->sym->name,
   15036              :                      nloc, oloc);
   15037            1 :           ret = false;
   15038            1 :           continue;
   15039              :         }
   15040              : 
   15041              :       /* According to the Fortran standard, ranks have to match for arguments.
   15042              :          In this case, this makes little sense because both decay to C
   15043              :          pointers.  Only issue an error if -pedantic or if the argument does
   15044              :          not decay to a pointer.  Same thing for CFI_desc arrays, which include
   15045              :          assumed rank.  */
   15046              : 
   15047        33532 :       int orank = gfc_symbol_rank (oarg->sym);
   15048        33532 :       int nrank = gfc_symbol_rank (narg->sym);
   15049        33532 :       if (orank != nrank && pedantic)
   15050              :         {
   15051            1 :           gfc_error ("Rank mismatch in argument %qs (%d/%d) at %L originally "
   15052            1 :                      "declared at %L", narg->sym->name, nrank, orank,  nloc,
   15053              :                      oloc);
   15054            1 :           ret = false;
   15055            1 :           continue;
   15056              :         }
   15057              : 
   15058              :       /* Confusion between CFI_desc and "normal" arrays.  */
   15059              : 
   15060        33531 :       if (decays_to_pointer (oarg->sym) != decays_to_pointer (narg->sym))
   15061              :         {
   15062            1 :           gfc_error ("Array specification mismatch in argument %qs at %L "
   15063              :                      "originally declared at %L", narg->sym->name,
   15064              :                      nloc, oloc);
   15065            1 :           ret = false;
   15066            1 :           continue;
   15067              :         }
   15068              :     }
   15069              : 
   15070        11625 :   if (oarg && !narg)
   15071              :     {
   15072            0 :       gfc_error ("Not enough arguments for procedure %qs with binding label "
   15073              :                  "%qs after %L, originally declared at %L", nsym->name,
   15074            0 :                  nsym->binding_label, nloc, &oarg->sym->declared_at);
   15075            0 :       ret = false;
   15076              :     }
   15077              : 
   15078        11625 :   if (!oarg && narg)
   15079              :     {
   15080            2 :       gfc_error ("Too many arguments for procedure %qs with binding label "
   15081              :                  "%qs at %L, originally declared at %L", nsym->name,
   15082            2 :                  nsym->binding_label, &narg->sym->declared_at, oloc);
   15083            2 :       ret = false;
   15084              :     }
   15085              : 
   15086        11625 :   return ret;
   15087              : }
   15088              : 
   15089              : 
   15090              : /* Verify that any binding labels used in a given namespace do not collide
   15091              :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   15092              :    for the same procedure are permitted.  Abstract interfaces and dummy
   15093              :    arguments are not checked.  */
   15094              : 
   15095              : static void
   15096      1895789 : gfc_verify_binding_labels (gfc_symbol *sym)
   15097              : {
   15098      1895789 :   gfc_gsymbol *gsym;
   15099      1895789 :   const char *module;
   15100              : 
   15101      1895789 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   15102        63239 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   15103        35015 :       || sym->attr.abstract || sym->attr.dummy)
   15104              :     return;
   15105              : 
   15106              :   /* Avoid double error reporting.  */
   15107        34879 :   if (sym->error)
   15108              :     return;
   15109              : 
   15110              :   /* TODO: Check the names of reserved external C identifiers here, see
   15111              :      PR 125251.  */
   15112              : 
   15113              :   /* According to the Fortran standard, global identifiers are case
   15114              :      insensitive, which also holds for C identifiers.  This was probably done
   15115              :      for systems which had case-insensitive linkers.  Such systems could not
   15116              :      accommodate the C standards referenced, so this restriction makes little
   15117              :      sense for modern systems. Therefore, check case-sensitive labels unless
   15118              :      -pedantic is in force.  */
   15119              : 
   15120        34879 :   if (pedantic)
   15121         4650 :     gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   15122              :   else
   15123        30229 :     gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
   15124              : 
   15125        34879 :   if (sym->module)
   15126              :     module = sym->module;
   15127        12273 :   else if (sym->ns && sym->ns->proc_name
   15128        12273 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   15129         4548 :     module = sym->ns->proc_name->name;
   15130         7725 :   else if (sym->ns && sym->ns->parent
   15131          358 :            && sym->ns && sym->ns->parent->proc_name
   15132          358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15133          272 :     module = sym->ns->parent->proc_name->name;
   15134              :   else
   15135              :     module = NULL;
   15136              : 
   15137        34879 :   if (gsym)
   15138              :     {
   15139        11669 :       if (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)
   15140              :         {
   15141        11628 :           gfc_symbol *global_sym;
   15142        11628 :           gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &global_sym);
   15143              : 
   15144              :           /* For when the symtree does not match the symbol name, which can happen
   15145              :              in modules with PRIVATE.  */
   15146              : 
   15147        11628 :           if (global_sym == NULL)
   15148            1 :             gfc_find_symbol_by_name (gsym->sym_name, gsym->ns, &global_sym);
   15149              : 
   15150        11628 :           gcc_assert (global_sym);
   15151              : 
   15152              :           /* If subroutines and functions are conflated, there is little point
   15153              :              in continuing checks.  */
   15154        11628 :           if ((sym->attr.function && gsym->type == GSYM_SUBROUTINE)
   15155        11628 :               || (sym->attr.subroutine && gsym->type == GSYM_FUNCTION))
   15156              :             {
   15157            1 :               gfc_global_used (gsym, &sym->declared_at);
   15158            1 :               sym->binding_label = NULL;
   15159            1 :               sym->error = 1;
   15160           13 :               return;
   15161              :             }
   15162              : 
   15163         6001 :           if (gsym->type == GSYM_FUNCTION && sym->attr.function
   15164        17628 :               && !gfc_compare_types (&sym->ts, &global_sym->ts))
   15165              :             {
   15166            2 :               gfc_error ("Return type mismatch of function %qs with binding "
   15167              :                          "label %qs at %L (%s/%s), originally declared at %L",
   15168              :                          sym->name, sym->binding_label,
   15169              :                          &sym->declared_at,
   15170              :                          gfc_typename (&sym->ts),
   15171            2 :                          gfc_typename (&global_sym->ts),
   15172              :                          &gsym->where);
   15173            2 :               sym->binding_label = NULL;
   15174            2 :               sym->error = 1;
   15175            2 :               return;
   15176              :             }
   15177        11625 :           if (!compare_c_binding_arglists (global_sym, sym))
   15178              :             {
   15179           10 :               sym->binding_label = NULL;
   15180           10 :               sym->error = 1;
   15181           10 :               return;
   15182              :             }
   15183              :         }
   15184              :     }
   15185              : 
   15186        11615 :   if (!gsym
   15187        11656 :       || (!gsym->defined
   15188         8709 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   15189              :     {
   15190        23210 :       if (!gsym)
   15191        23210 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   15192        31919 :       gsym->where = sym->declared_at;
   15193        31919 :       gsym->sym_name = sym->name;
   15194        31919 :       gsym->binding_label = sym->binding_label;
   15195        31919 :       gsym->ns = sym->ns;
   15196        31919 :       gsym->mod_name = module;
   15197        31919 :       if (sym->attr.function)
   15198        20159 :         gsym->type = GSYM_FUNCTION;
   15199        11760 :       else if (sym->attr.subroutine)
   15200        11621 :         gsym->type = GSYM_SUBROUTINE;
   15201              :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   15202        31919 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   15203        31919 :       return;
   15204              :     }
   15205              : 
   15206         2947 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   15207              :     {
   15208            1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   15209              :                  "identifier as entity at %L", sym->name,
   15210              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15211              :       /* Clear the binding label to prevent checking multiple times.  */
   15212            1 :       sym->binding_label = NULL;
   15213            1 :       return;
   15214              :     }
   15215              : 
   15216         2946 :   if (sym->attr.flavor == FL_VARIABLE && module
   15217           37 :       && (strcmp (module, gsym->mod_name) != 0
   15218           35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   15219              :     {
   15220              :       /* This can only happen if the variable is defined in a module - if it
   15221              :          isn't the same module, reject it.  */
   15222            3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   15223              :                  "uses the same global identifier as entity at %L from module %qs",
   15224              :                  sym->name, module, sym->binding_label,
   15225              :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   15226            3 :       sym->binding_label = NULL;
   15227            3 :       return;
   15228              :     }
   15229              : 
   15230         2943 :   if ((sym->attr.function || sym->attr.subroutine)
   15231         2907 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   15232         2905 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   15233         2522 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   15234         2091 :       && (module != gsym->mod_name
   15235         2087 :           || strcmp (gsym->sym_name, sym->name) != 0
   15236         2087 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   15237              :     {
   15238              :       /* Print an error if the procedure is defined multiple times; we have to
   15239              :          exclude references to the same procedure via module association or
   15240              :          multiple checks for the same procedure.  */
   15241            4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   15242              :                  "global identifier as entity at %L", sym->name,
   15243              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15244            4 :       sym->binding_label = NULL;
   15245            4 :       return;
   15246              :     }
   15247              : }
   15248              : 
   15249              : 
   15250              : /* Resolve an index expression.  */
   15251              : 
   15252              : static bool
   15253       267226 : resolve_index_expr (gfc_expr *e)
   15254              : {
   15255       267226 :   if (!gfc_resolve_expr (e))
   15256              :     return false;
   15257              : 
   15258       267216 :   if (!gfc_simplify_expr (e, 0))
   15259              :     return false;
   15260              : 
   15261       267214 :   if (!gfc_specification_expr (e))
   15262              :     return false;
   15263              : 
   15264              :   return true;
   15265              : }
   15266              : 
   15267              : 
   15268              : /* Resolve a charlen structure.  */
   15269              : 
   15270              : static bool
   15271       104794 : resolve_charlen (gfc_charlen *cl)
   15272              : {
   15273       104794 :   int k;
   15274       104794 :   bool saved_specification_expr;
   15275              : 
   15276       104794 :   if (cl->resolved)
   15277              :     return true;
   15278              : 
   15279        96282 :   cl->resolved = 1;
   15280        96282 :   saved_specification_expr = specification_expr;
   15281        96282 :   specification_expr = true;
   15282              : 
   15283        96282 :   if (cl->length_from_typespec)
   15284              :     {
   15285         2136 :       if (!gfc_resolve_expr (cl->length))
   15286              :         {
   15287            1 :           specification_expr = saved_specification_expr;
   15288            1 :           return false;
   15289              :         }
   15290              : 
   15291         2135 :       if (!gfc_simplify_expr (cl->length, 0))
   15292              :         {
   15293            0 :           specification_expr = saved_specification_expr;
   15294            0 :           return false;
   15295              :         }
   15296              : 
   15297              :       /* cl->length has been resolved.  It should have an integer type.  */
   15298         2135 :       if (cl->length
   15299         2134 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   15300              :         {
   15301            4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   15302              :                      &cl->length->where);
   15303            4 :           return false;
   15304              :         }
   15305              :     }
   15306              :   else
   15307              :     {
   15308        94146 :       if (!resolve_index_expr (cl->length))
   15309              :         {
   15310           19 :           specification_expr = saved_specification_expr;
   15311           19 :           return false;
   15312              :         }
   15313              :     }
   15314              : 
   15315              :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   15316              :      a negative value, the length of character entities declared is zero.  */
   15317        96258 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15318        57555 :       && mpz_sgn (cl->length->value.integer) < 0)
   15319            0 :     gfc_replace_expr (cl->length,
   15320              :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   15321              : 
   15322              :   /* Check that the character length is not too large.  */
   15323        96258 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   15324        96258 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15325        57555 :       && cl->length->ts.type == BT_INTEGER
   15326        57555 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   15327              :     {
   15328            4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   15329            4 :       specification_expr = saved_specification_expr;
   15330            4 :       return false;
   15331              :     }
   15332              : 
   15333        96254 :   specification_expr = saved_specification_expr;
   15334        96254 :   return true;
   15335              : }
   15336              : 
   15337              : 
   15338              : /* Test for non-constant shape arrays.  */
   15339              : 
   15340              : static bool
   15341       118331 : is_non_constant_shape_array (gfc_symbol *sym)
   15342              : {
   15343       118331 :   gfc_expr *e;
   15344       118331 :   int i;
   15345       118331 :   bool not_constant;
   15346              : 
   15347       118331 :   not_constant = false;
   15348       118331 :   if (sym->as != NULL)
   15349              :     {
   15350              :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   15351              :          has not been simplified; parameter array references.  Do the
   15352              :          simplification now.  */
   15353       155832 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   15354              :         {
   15355        89957 :           if (i == GFC_MAX_DIMENSIONS)
   15356              :             break;
   15357              : 
   15358        89955 :           e = sym->as->lower[i];
   15359        89955 :           if (e && (!resolve_index_expr(e)
   15360        87137 :                     || !gfc_is_constant_expr (e)))
   15361              :             not_constant = true;
   15362        89955 :           e = sym->as->upper[i];
   15363        89955 :           if (e && (!resolve_index_expr(e)
   15364        85915 :                     || !gfc_is_constant_expr (e)))
   15365              :             not_constant = true;
   15366              :         }
   15367              :     }
   15368       118331 :   return not_constant;
   15369              : }
   15370              : 
   15371              : /* Given a symbol and an initialization expression, add code to initialize
   15372              :    the symbol to the function entry.  */
   15373              : static void
   15374         2138 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   15375              : {
   15376         2138 :   gfc_expr *lval;
   15377         2138 :   gfc_code *init_st;
   15378         2138 :   gfc_namespace *ns = sym->ns;
   15379              : 
   15380         2138 :   if (sym->attr.function && sym->result == sym && IS_PDT (sym))
   15381              :     {
   15382           46 :       gfc_free_expr (init);
   15383           46 :       return;
   15384              :     }
   15385              : 
   15386              :   /* Search for the function namespace if this is a contained
   15387              :      function without an explicit result.  */
   15388         2092 :   if (sym->attr.function && sym == sym->result
   15389          299 :       && sym->name != sym->ns->proc_name->name)
   15390              :     {
   15391          298 :       ns = ns->contained;
   15392         1376 :       for (;ns; ns = ns->sibling)
   15393         1315 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   15394              :           break;
   15395              :     }
   15396              : 
   15397         2092 :   if (ns == NULL)
   15398              :     {
   15399           61 :       gfc_free_expr (init);
   15400           61 :       return;
   15401              :     }
   15402              : 
   15403              :   /* Build an l-value expression for the result.  */
   15404         2031 :   lval = gfc_lval_expr_from_sym (sym);
   15405              : 
   15406              :   /* Add the code at scope entry.  */
   15407         2031 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   15408         2031 :   init_st->next = ns->code;
   15409         2031 :   ns->code = init_st;
   15410              : 
   15411              :   /* Assign the default initializer to the l-value.  */
   15412         2031 :   init_st->loc = sym->declared_at;
   15413         2031 :   init_st->expr1 = lval;
   15414         2031 :   init_st->expr2 = init;
   15415              : }
   15416              : 
   15417              : 
   15418              : /* Whether or not we can generate a default initializer for a symbol.  */
   15419              : 
   15420              : static bool
   15421        30473 : can_generate_init (gfc_symbol *sym)
   15422              : {
   15423        30473 :   symbol_attribute *a;
   15424        30473 :   if (!sym)
   15425              :     return false;
   15426        30473 :   a = &sym->attr;
   15427              : 
   15428              :   /* These symbols should never have a default initialization.  */
   15429        50144 :   return !(
   15430        30473 :        a->allocatable
   15431        30473 :     || a->external
   15432        29304 :     || a->pointer
   15433        29304 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   15434         5757 :         && (CLASS_DATA (sym)->attr.class_pointer
   15435         3781 :             || CLASS_DATA (sym)->attr.proc_pointer))
   15436        27328 :     || a->in_equivalence
   15437        27207 :     || a->in_common
   15438        27160 :     || a->data
   15439        26982 :     || sym->module
   15440        23153 :     || a->cray_pointee
   15441        23091 :     || a->cray_pointer
   15442        23091 :     || sym->assoc
   15443        20346 :     || (!a->referenced && !a->result)
   15444        19671 :     || (a->dummy && (a->intent != INTENT_OUT
   15445         1081 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   15446        19671 :     || (a->function && sym != sym->result)
   15447              :   );
   15448              : }
   15449              : 
   15450              : 
   15451              : /* Assign the default initializer to a derived type variable or result.  */
   15452              : 
   15453              : static void
   15454        11668 : apply_default_init (gfc_symbol *sym)
   15455              : {
   15456        11668 :   gfc_expr *init = NULL;
   15457              : 
   15458        11668 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15459              :     return;
   15460              : 
   15461        11422 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   15462        10569 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15463              : 
   15464        11422 :   if (init == NULL && sym->ts.type != BT_CLASS)
   15465              :     return;
   15466              : 
   15467         1756 :   build_init_assign (sym, init);
   15468         1756 :   sym->attr.referenced = 1;
   15469              : }
   15470              : 
   15471              : 
   15472              : /* Build an initializer for a local. Returns null if the symbol should not have
   15473              :    a default initialization.  */
   15474              : 
   15475              : static gfc_expr *
   15476       205933 : build_default_init_expr (gfc_symbol *sym)
   15477              : {
   15478              :   /* These symbols should never have a default initialization.  */
   15479       205933 :   if (sym->attr.allocatable
   15480       192119 :       || sym->attr.external
   15481       192119 :       || sym->attr.dummy
   15482       126242 :       || sym->attr.pointer
   15483       118068 :       || sym->attr.in_equivalence
   15484       115692 :       || sym->attr.in_common
   15485       112591 :       || sym->attr.data
   15486       110293 :       || sym->module
   15487       107751 :       || sym->attr.cray_pointee
   15488       107450 :       || sym->attr.cray_pointer
   15489       107148 :       || sym->assoc)
   15490              :     return NULL;
   15491              : 
   15492              :   /* Get the appropriate init expression.  */
   15493       102329 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   15494              : }
   15495              : 
   15496              : /* Add an initialization expression to a local variable.  */
   15497              : static void
   15498       205933 : apply_default_init_local (gfc_symbol *sym)
   15499              : {
   15500       205933 :   gfc_expr *init = NULL;
   15501              : 
   15502              :   /* The symbol should be a variable or a function return value.  */
   15503       205933 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15504       205933 :       || (sym->attr.function && sym->result != sym))
   15505              :     return;
   15506              : 
   15507              :   /* Try to build the initializer expression.  If we can't initialize
   15508              :      this symbol, then init will be NULL.  */
   15509       205933 :   init = build_default_init_expr (sym);
   15510       205933 :   if (init == NULL)
   15511              :     return;
   15512              : 
   15513              :   /* For saved variables, we don't want to add an initializer at function
   15514              :      entry, so we just add a static initializer. Note that automatic variables
   15515              :      are stack allocated even with -fno-automatic; we have also to exclude
   15516              :      result variable, which are also nonstatic.  */
   15517          419 :   if (!sym->attr.automatic
   15518          419 :       && (sym->attr.save || sym->ns->save_all
   15519          377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   15520           27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   15521           14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   15522              :     {
   15523              :       /* Don't clobber an existing initializer!  */
   15524           37 :       gcc_assert (sym->value == NULL);
   15525           37 :       sym->value = init;
   15526           37 :       return;
   15527              :     }
   15528              : 
   15529          382 :   build_init_assign (sym, init);
   15530              : }
   15531              : 
   15532              : 
   15533              : /* Resolution of common features of flavors variable and procedure.  */
   15534              : 
   15535              : static bool
   15536       978215 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   15537              : {
   15538       978215 :   gfc_array_spec *as;
   15539              : 
   15540       978215 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15541        19608 :       && sym->ts.u.derived && CLASS_DATA (sym))
   15542        19602 :     as = CLASS_DATA (sym)->as;
   15543              :   else
   15544       958613 :     as = sym->as;
   15545              : 
   15546              :   /* Constraints on deferred shape variable.  */
   15547       978215 :   if (as == NULL || as->type != AS_DEFERRED)
   15548              :     {
   15549       953705 :       bool pointer, allocatable, dimension;
   15550              : 
   15551       953705 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15552        16379 :           && sym->ts.u.derived && CLASS_DATA (sym))
   15553              :         {
   15554        16373 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   15555        16373 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   15556        16373 :           dimension = CLASS_DATA (sym)->attr.dimension;
   15557              :         }
   15558              :       else
   15559              :         {
   15560       937332 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   15561       937332 :           allocatable = sym->attr.allocatable;
   15562       937332 :           dimension = sym->attr.dimension;
   15563              :         }
   15564              : 
   15565       953705 :       if (allocatable)
   15566              :         {
   15567         8097 :           if (dimension
   15568         8097 :               && as
   15569          524 :               && as->type != AS_ASSUMED_RANK
   15570            5 :               && !sym->attr.select_rank_temporary)
   15571              :             {
   15572            3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   15573              :                          "shape or assumed rank", sym->name, &sym->declared_at);
   15574            3 :               return false;
   15575              :             }
   15576         8094 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   15577              :                                     "%qs at %L may not be ALLOCATABLE",
   15578              :                                     sym->name, &sym->declared_at))
   15579              :             return false;
   15580              :         }
   15581              : 
   15582       953701 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   15583              :         {
   15584            4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   15585              :                      "assumed rank", sym->name, &sym->declared_at);
   15586            4 :           sym->error = 1;
   15587            4 :           return false;
   15588              :         }
   15589              :     }
   15590              :   else
   15591              :     {
   15592        24510 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   15593         4695 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   15594              :         {
   15595            3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   15596              :                      sym->name, &sym->declared_at);
   15597            3 :           return false;
   15598              :          }
   15599              :     }
   15600              : 
   15601              :   /* Constraints on polymorphic variables.  */
   15602       978204 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   15603              :     {
   15604              :       /* F03:C502.  */
   15605        18941 :       if (sym->attr.class_ok
   15606        18885 :           && sym->ts.u.derived
   15607        18880 :           && !sym->attr.select_type_temporary
   15608        17758 :           && !UNLIMITED_POLY (sym)
   15609        15247 :           && CLASS_DATA (sym)
   15610        15246 :           && CLASS_DATA (sym)->ts.u.derived
   15611        34186 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   15612              :         {
   15613            5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   15614            5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   15615              :                      &sym->declared_at);
   15616            5 :           return false;
   15617              :         }
   15618              : 
   15619              :       /* F03:C509.  */
   15620              :       /* Assume that use associated symbols were checked in the module ns.
   15621              :          Class-variables that are associate-names are also something special
   15622              :          and excepted from the test.  */
   15623        18936 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   15624           54 :           && !sym->attr.select_type_temporary
   15625           54 :           && !sym->attr.select_rank_temporary)
   15626              :         {
   15627           54 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   15628              :                      "or pointer", sym->name, &sym->declared_at);
   15629           54 :           return false;
   15630              :         }
   15631              :     }
   15632              : 
   15633              :   return true;
   15634              : }
   15635              : 
   15636              : 
   15637              : /* Additional checks for symbols with flavor variable and derived
   15638              :    type.  To be called from resolve_fl_variable.  */
   15639              : 
   15640              : static bool
   15641        82936 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   15642              : {
   15643        82936 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   15644              : 
   15645              :   /* Check to see if a derived type is blocked from being host
   15646              :      associated by the presence of another class I symbol in the same
   15647              :      namespace.  14.6.1.3 of the standard and the discussion on
   15648              :      comp.lang.fortran.  */
   15649        82936 :   if (sym->ts.u.derived
   15650        82931 :       && sym->ns != sym->ts.u.derived->ns
   15651        47671 :       && !sym->ts.u.derived->attr.use_assoc
   15652        17724 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   15653              :     {
   15654        16735 :       gfc_symbol *s;
   15655        16735 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   15656        16735 :       if (s && s->attr.generic)
   15657            2 :         s = gfc_find_dt_in_generic (s);
   15658        16735 :       if (s && !gfc_fl_struct (s->attr.flavor))
   15659              :         {
   15660            2 :           gfc_error ("The type %qs cannot be host associated at %L "
   15661              :                      "because it is blocked by an incompatible object "
   15662              :                      "of the same name declared at %L",
   15663            2 :                      sym->ts.u.derived->name, &sym->declared_at,
   15664              :                      &s->declared_at);
   15665            2 :           return false;
   15666              :         }
   15667              :     }
   15668              : 
   15669              :   /* 4th constraint in section 11.3: "If an object of a type for which
   15670              :      component-initialization is specified (R429) appears in the
   15671              :      specification-part of a module and does not have the ALLOCATABLE
   15672              :      or POINTER attribute, the object shall have the SAVE attribute."
   15673              : 
   15674              :      The check for initializers is performed with
   15675              :      gfc_has_default_initializer because gfc_default_initializer generates
   15676              :      a hidden default for allocatable components.  */
   15677        82257 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   15678        18673 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   15679          417 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   15680           21 :       && !sym->attr.pointer && !sym->attr.allocatable
   15681           21 :       && gfc_has_default_initializer (sym->ts.u.derived)
   15682        82943 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   15683              :                           "%qs at %L, needed due to the default "
   15684              :                           "initialization", sym->name, &sym->declared_at))
   15685              :     return false;
   15686              : 
   15687              :   /* Assign default initializer.  */
   15688        82932 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   15689        76650 :       && (!no_init_flag
   15690        59797 :           || (sym->attr.intent == INTENT_OUT
   15691         3225 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   15692        19904 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15693              : 
   15694              :   return true;
   15695              : }
   15696              : 
   15697              : 
   15698              : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   15699              :    except in the declaration of an entity or component that has the POINTER
   15700              :    or ALLOCATABLE attribute.  */
   15701              : 
   15702              : static bool
   15703      1546776 : deferred_requirements (gfc_symbol *sym)
   15704              : {
   15705      1546776 :   if (sym->ts.deferred
   15706         8040 :       && !(sym->attr.pointer
   15707         2421 :            || sym->attr.allocatable
   15708           92 :            || sym->attr.associate_var
   15709            7 :            || sym->attr.omp_udr_artificial_var))
   15710              :     {
   15711              :       /* If a function has a result variable, only check the variable.  */
   15712            7 :       if (sym->result && sym->name != sym->result->name)
   15713              :         return true;
   15714              : 
   15715            6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   15716              :                  "requires either the POINTER or ALLOCATABLE attribute",
   15717              :                  sym->name, &sym->declared_at);
   15718            6 :       return false;
   15719              :     }
   15720              :   return true;
   15721              : }
   15722              : 
   15723              : 
   15724              : /* Resolve symbols with flavor variable.  */
   15725              : 
   15726              : static bool
   15727       656113 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   15728              : {
   15729       656113 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   15730              :                                  "SAVE attribute");
   15731              : 
   15732       656113 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   15733              :     return false;
   15734              : 
   15735              :   /* Set this flag to check that variables are parameters of all entries.
   15736              :      This check is effected by the call to gfc_resolve_expr through
   15737              :      is_non_constant_shape_array.  */
   15738       656053 :   bool saved_specification_expr = specification_expr;
   15739       656053 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   15740       656053 :   specification_expr = true;
   15741       656053 :   specification_expr_symbol = sym;
   15742              : 
   15743       656053 :   if (sym->ns->proc_name
   15744       655958 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15745       650917 :           || sym->ns->proc_name->attr.is_main_program)
   15746        83173 :       && !sym->attr.use_assoc
   15747        79991 :       && !sym->attr.allocatable
   15748        74189 :       && !sym->attr.pointer
   15749       726586 :       && is_non_constant_shape_array (sym))
   15750              :     {
   15751              :       /* F08:C541. The shape of an array defined in a main program or module
   15752              :        * needs to be constant.  */
   15753            3 :       gfc_error ("The module or main program array %qs at %L must "
   15754              :                  "have constant shape", sym->name, &sym->declared_at);
   15755            3 :       specification_expr = saved_specification_expr;
   15756            3 :       specification_expr_symbol = saved_specification_expr_symbol;
   15757            3 :       return false;
   15758              :     }
   15759              : 
   15760              :   /* Constraints on deferred type parameter.  */
   15761       656050 :   if (!deferred_requirements (sym))
   15762              :     return false;
   15763              : 
   15764       656046 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   15765              :     {
   15766              :       /* Make sure that character string variables with assumed length are
   15767              :          dummy arguments.  */
   15768        36193 :       gfc_expr *e = NULL;
   15769              : 
   15770        36193 :       if (sym->ts.u.cl)
   15771        36193 :         e = sym->ts.u.cl->length;
   15772              :       else
   15773              :         return false;
   15774              : 
   15775        36193 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   15776         2638 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   15777            2 :           && !sym->attr.omp_udr_artificial_var)
   15778              :         {
   15779            2 :           gfc_error ("Entity with assumed character length at %L must be a "
   15780              :                      "dummy argument or a PARAMETER", &sym->declared_at);
   15781            2 :           specification_expr = saved_specification_expr;
   15782            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15783            2 :           return false;
   15784              :         }
   15785              : 
   15786        20973 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   15787              :         {
   15788            1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15789            1 :           specification_expr = saved_specification_expr;
   15790            1 :           specification_expr_symbol = saved_specification_expr_symbol;
   15791            1 :           return false;
   15792              :         }
   15793              : 
   15794        36190 :       if (!gfc_is_constant_expr (e)
   15795        36190 :           && !(e->expr_type == EXPR_VARIABLE
   15796         1388 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   15797              :         {
   15798         2184 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   15799         1680 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15800         1679 :                   || sym->ns->proc_name->attr.is_main_program))
   15801              :             {
   15802            3 :               gfc_error ("%qs at %L must have constant character length "
   15803              :                         "in this context", sym->name, &sym->declared_at);
   15804            3 :               specification_expr = saved_specification_expr;
   15805            3 :               specification_expr_symbol = saved_specification_expr_symbol;
   15806            3 :               return false;
   15807              :             }
   15808         2181 :           if (sym->attr.in_common)
   15809              :             {
   15810            1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   15811              :                          "character length", sym->name, &sym->declared_at);
   15812            1 :               specification_expr = saved_specification_expr;
   15813            1 :               specification_expr_symbol = saved_specification_expr_symbol;
   15814            1 :               return false;
   15815              :             }
   15816              :         }
   15817              :     }
   15818              : 
   15819       656039 :   if (sym->value == NULL && sym->attr.referenced
   15820       207855 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   15821       205933 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   15822              : 
   15823              :   /* Determine if the symbol may not have an initializer.  */
   15824       656039 :   int no_init_flag = 0, automatic_flag = 0;
   15825       656039 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   15826       171765 :       || sym->attr.intrinsic || sym->attr.result)
   15827              :     no_init_flag = 1;
   15828       139340 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   15829       174129 :            && is_non_constant_shape_array (sym))
   15830              :     {
   15831         1351 :       no_init_flag = automatic_flag = 1;
   15832              : 
   15833              :       /* Also, they must not have the SAVE attribute.
   15834              :          SAVE_IMPLICIT is checked below.  */
   15835         1351 :       if (sym->as && sym->attr.codimension)
   15836              :         {
   15837            7 :           int corank = sym->as->corank;
   15838            7 :           sym->as->corank = 0;
   15839            7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   15840            7 :           sym->as->corank = corank;
   15841              :         }
   15842         1351 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   15843              :         {
   15844            2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15845            2 :           specification_expr = saved_specification_expr;
   15846            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15847            2 :           return false;
   15848              :         }
   15849              :     }
   15850              : 
   15851              :   /* Ensure that any initializer is simplified.  */
   15852       656037 :   if (sym->value)
   15853         8185 :     gfc_simplify_expr (sym->value, 1);
   15854              : 
   15855              :   /* Reject illegal initializers.  */
   15856       656037 :   if (!sym->mark && sym->value)
   15857              :     {
   15858         8185 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   15859           67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   15860            1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   15861              :                    sym->name, &sym->declared_at);
   15862         8184 :       else if (sym->attr.external)
   15863            0 :         gfc_error ("External %qs at %L cannot have an initializer",
   15864              :                    sym->name, &sym->declared_at);
   15865         8184 :       else if (sym->attr.dummy)
   15866            3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   15867              :                    sym->name, &sym->declared_at);
   15868         8181 :       else if (sym->attr.intrinsic)
   15869            0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   15870              :                    sym->name, &sym->declared_at);
   15871         8181 :       else if (sym->attr.result)
   15872            1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   15873              :                    sym->name, &sym->declared_at);
   15874         8180 :       else if (automatic_flag)
   15875            5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   15876              :                    sym->name, &sym->declared_at);
   15877              :       else
   15878         8175 :         goto no_init_error;
   15879           10 :       specification_expr = saved_specification_expr;
   15880           10 :       specification_expr_symbol = saved_specification_expr_symbol;
   15881           10 :       return false;
   15882              :     }
   15883              : 
   15884       647852 : no_init_error:
   15885       656027 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   15886              :     {
   15887        82936 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   15888        82936 :       specification_expr = saved_specification_expr;
   15889        82936 :       specification_expr_symbol = saved_specification_expr_symbol;
   15890        82936 :       return res;
   15891              :     }
   15892              : 
   15893       573091 :   specification_expr = saved_specification_expr;
   15894       573091 :   specification_expr_symbol = saved_specification_expr_symbol;
   15895       573091 :   return true;
   15896              : }
   15897              : 
   15898              : 
   15899              : /* Compare the dummy characteristics of a module procedure interface
   15900              :    declaration with the corresponding declaration in a submodule.  */
   15901              : static gfc_formal_arglist *new_formal;
   15902              : static char errmsg[200];
   15903              : 
   15904              : static void
   15905         1351 : compare_fsyms (gfc_symbol *sym)
   15906              : {
   15907         1351 :   gfc_symbol *fsym;
   15908              : 
   15909         1351 :   if (sym == NULL || new_formal == NULL)
   15910              :     return;
   15911              : 
   15912         1351 :   fsym = new_formal->sym;
   15913              : 
   15914         1351 :   if (sym == fsym)
   15915              :     return;
   15916              : 
   15917         1327 :   if (strcmp (sym->name, fsym->name) == 0)
   15918              :     {
   15919          522 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   15920            2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   15921              :     }
   15922              : }
   15923              : 
   15924              : 
   15925              : /* Resolve a procedure.  */
   15926              : 
   15927              : static bool
   15928       483797 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   15929              : {
   15930       483797 :   gfc_formal_arglist *arg;
   15931       483797 :   bool allocatable_or_pointer = false;
   15932              : 
   15933       483797 :   if (sym->attr.function
   15934       483797 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   15935              :     return false;
   15936              : 
   15937              :   /* Constraints on deferred type parameter.  */
   15938       483787 :   if (!deferred_requirements (sym))
   15939              :     return false;
   15940              : 
   15941       483786 :   if (sym->ts.type == BT_CHARACTER)
   15942              :     {
   15943        11886 :       gfc_charlen *cl = sym->ts.u.cl;
   15944              : 
   15945         7672 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   15946        13191 :              && !resolve_charlen (cl))
   15947              :         return false;
   15948              : 
   15949        11885 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   15950        10581 :           && sym->attr.proc == PROC_ST_FUNCTION)
   15951              :         {
   15952            0 :           gfc_error ("Character-valued statement function %qs at %L must "
   15953              :                      "have constant length", sym->name, &sym->declared_at);
   15954            0 :           return false;
   15955              :         }
   15956              :     }
   15957              : 
   15958              :   /* Ensure that derived type for are not of a private type.  Internal
   15959              :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   15960              :      externally accessible and can access all the objects accessible in
   15961              :      the host.  */
   15962       113087 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   15963       113087 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15964       571091 :       && gfc_check_symbol_access (sym))
   15965              :     {
   15966       450767 :       gfc_interface *iface;
   15967              : 
   15968       958557 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   15969              :         {
   15970       507791 :           if (arg->sym
   15971       507651 :               && arg->sym->ts.type == BT_DERIVED
   15972        42878 :               && arg->sym->ts.u.derived
   15973        42878 :               && !arg->sym->ts.u.derived->attr.use_assoc
   15974         4223 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   15975       507800 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   15976              :                                   "and cannot be a dummy argument"
   15977              :                                   " of %qs, which is PUBLIC at %L",
   15978            9 :                                   arg->sym->name, sym->name,
   15979              :                                   &sym->declared_at))
   15980              :             {
   15981              :               /* Stop this message from recurring.  */
   15982            1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   15983            1 :               return false;
   15984              :             }
   15985              :         }
   15986              : 
   15987              :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   15988              :          PRIVATE to the containing module.  */
   15989       640211 :       for (iface = sym->generic; iface; iface = iface->next)
   15990              :         {
   15991       442541 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   15992              :             {
   15993       253096 :               if (arg->sym
   15994       253064 :                   && arg->sym->ts.type == BT_DERIVED
   15995         8021 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   15996          232 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   15997       253100 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   15998              :                                       "PUBLIC interface %qs at %L "
   15999              :                                       "takes dummy arguments of %qs which "
   16000              :                                       "is PRIVATE", iface->sym->name,
   16001            4 :                                       sym->name, &iface->sym->declared_at,
   16002            4 :                                       gfc_typename(&arg->sym->ts)))
   16003              :                 {
   16004              :                   /* Stop this message from recurring.  */
   16005            1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16006            1 :                   return false;
   16007              :                 }
   16008              :              }
   16009              :         }
   16010              :     }
   16011              : 
   16012       483783 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   16013           86 :       && !sym->attr.proc_pointer)
   16014              :     {
   16015            2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   16016              :                  sym->name, &sym->declared_at);
   16017              : 
   16018              :       /* Make sure no second error is issued for this.  */
   16019            2 :       sym->value->error = 1;
   16020            2 :       return false;
   16021              :     }
   16022              : 
   16023              :   /* An external symbol may not have an initializer because it is taken to be
   16024              :      a procedure. Exception: Procedure Pointers.  */
   16025       483781 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   16026              :     {
   16027            0 :       gfc_error ("External object %qs at %L may not have an initializer",
   16028              :                  sym->name, &sym->declared_at);
   16029            0 :       return false;
   16030              :     }
   16031              : 
   16032              :   /* An elemental function is required to return a scalar 12.7.1  */
   16033       483781 :   if (sym->attr.elemental && sym->attr.function
   16034        86370 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   16035            2 :                       && CLASS_DATA (sym)->as)))
   16036              :     {
   16037            3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   16038              :                  "result", sym->name, &sym->declared_at);
   16039              :       /* Reset so that the error only occurs once.  */
   16040            3 :       sym->attr.elemental = 0;
   16041            3 :       return false;
   16042              :     }
   16043              : 
   16044       483778 :   if (sym->attr.proc == PROC_ST_FUNCTION
   16045          223 :       && (sym->attr.allocatable || sym->attr.pointer))
   16046              :     {
   16047            2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   16048              :                  "allocatable attribute", sym->name, &sym->declared_at);
   16049            2 :       return false;
   16050              :     }
   16051              : 
   16052              :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   16053              :      char-len-param shall not be array-valued, pointer-valued, recursive
   16054              :      or pure.  ....snip... A character value of * may only be used in the
   16055              :      following ways: (i) Dummy arg of procedure - dummy associates with
   16056              :      actual length; (ii) To declare a named constant; or (iii) External
   16057              :      function - but length must be declared in calling scoping unit.  */
   16058       483776 :   if (sym->attr.function
   16059       322083 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   16060         6811 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   16061              :     {
   16062          180 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   16063          178 :           || (sym->attr.recursive) || (sym->attr.pure))
   16064              :         {
   16065            4 :           if (sym->as && sym->as->rank)
   16066            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16067              :                        "array-valued", sym->name, &sym->declared_at);
   16068              : 
   16069            4 :           if (sym->attr.pointer)
   16070            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16071              :                        "pointer-valued", sym->name, &sym->declared_at);
   16072              : 
   16073            4 :           if (sym->attr.pure)
   16074            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16075              :                        "pure", sym->name, &sym->declared_at);
   16076              : 
   16077            4 :           if (sym->attr.recursive)
   16078            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16079              :                        "recursive", sym->name, &sym->declared_at);
   16080              : 
   16081            4 :           return false;
   16082              :         }
   16083              : 
   16084              :       /* Appendix B.2 of the standard.  Contained functions give an
   16085              :          error anyway.  Deferred character length is an F2003 feature.
   16086              :          Don't warn on intrinsic conversion functions, which start
   16087              :          with two underscores.  */
   16088          176 :       if (!sym->attr.contained && !sym->ts.deferred
   16089          172 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   16090          172 :         gfc_notify_std (GFC_STD_F95_OBS,
   16091              :                         "CHARACTER(*) function %qs at %L",
   16092              :                         sym->name, &sym->declared_at);
   16093              :     }
   16094              : 
   16095              :   /* F2008, C1218.  */
   16096       483772 :   if (sym->attr.elemental)
   16097              :     {
   16098        89648 :       if (sym->attr.proc_pointer)
   16099              :         {
   16100            7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   16101              :                                                : sym->name);
   16102            7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   16103              :                      name, &sym->declared_at);
   16104            7 :           return false;
   16105              :         }
   16106        89641 :       if (sym->attr.dummy)
   16107              :         {
   16108            3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   16109              :                      sym->name, &sym->declared_at);
   16110            3 :           return false;
   16111              :         }
   16112              :     }
   16113              : 
   16114              :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   16115              :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   16116              :      pointer is tested and caught elsewhere.  */
   16117       483762 :   if (sym->result)
   16118       270020 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   16119       270020 :                              && CLASS_DATA (sym->result) ?
   16120         1669 :                              (CLASS_DATA (sym->result)->attr.allocatable
   16121         1669 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   16122       268351 :                              (sym->result->attr.allocatable
   16123       268351 :                               || sym->result->attr.pointer);
   16124              : 
   16125       483762 :   if (sym->attr.elemental && sym->result
   16126        85995 :       && allocatable_or_pointer)
   16127              :     {
   16128            4 :       gfc_error ("Function result variable %qs at %L of elemental "
   16129              :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   16130              :                  "attribute", sym->result->name,
   16131              :                  &sym->result->declared_at, sym->name);
   16132            4 :       return false;
   16133              :     }
   16134              : 
   16135              :   /* F2018:C1585: "The function result of a pure function shall not be both
   16136              :      polymorphic and allocatable, or have a polymorphic allocatable ultimate
   16137              :      component."  */
   16138       483758 :   if (sym->attr.pure && sym->result && sym->ts.u.derived)
   16139              :     {
   16140         2520 :       if (sym->ts.type == BT_CLASS
   16141            5 :           && sym->attr.class_ok
   16142            4 :           && CLASS_DATA (sym->result)
   16143            4 :           && CLASS_DATA (sym->result)->attr.allocatable)
   16144              :         {
   16145            4 :           gfc_error ("Result variable %qs of pure function at %L is "
   16146              :                      "polymorphic allocatable",
   16147              :                      sym->result->name, &sym->result->declared_at);
   16148            4 :           return false;
   16149              :         }
   16150              : 
   16151         2516 :       if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
   16152              :         {
   16153              :           gfc_component *c = sym->ts.u.derived->components;
   16154         4613 :           for (; c; c = c->next)
   16155         2406 :             if (c->ts.type == BT_CLASS
   16156            2 :                 && CLASS_DATA (c)
   16157            2 :                 && CLASS_DATA (c)->attr.allocatable)
   16158              :               {
   16159            2 :                 gfc_error ("Result variable %qs of pure function at %L has "
   16160              :                            "polymorphic allocatable component %qs",
   16161              :                            sym->result->name, &sym->result->declared_at,
   16162              :                            c->name);
   16163            2 :                 return false;
   16164              :               }
   16165              :         }
   16166              :     }
   16167              : 
   16168       483752 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   16169              :     {
   16170         6808 :       gfc_formal_arglist *curr_arg;
   16171         6808 :       int has_non_interop_arg = 0;
   16172              : 
   16173         6808 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   16174         6808 :                               sym->common_block))
   16175              :         {
   16176              :           /* Clear these to prevent looking at them again if there was an
   16177              :              error.  */
   16178            2 :           sym->attr.is_bind_c = 0;
   16179            2 :           sym->attr.is_c_interop = 0;
   16180            2 :           sym->ts.is_c_interop = 0;
   16181              :         }
   16182              :       else
   16183              :         {
   16184              :           /* So far, no errors have been found.  */
   16185         6806 :           sym->attr.is_c_interop = 1;
   16186         6806 :           sym->ts.is_c_interop = 1;
   16187              :         }
   16188              : 
   16189         6808 :       curr_arg = gfc_sym_get_dummy_args (sym);
   16190        30368 :       while (curr_arg != NULL)
   16191              :         {
   16192              :           /* Skip implicitly typed dummy args here.  */
   16193        16752 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   16194        16695 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   16195              :               /* If something is found to fail, record the fact so we
   16196              :                  can mark the symbol for the procedure as not being
   16197              :                  BIND(C) to try and prevent multiple errors being
   16198              :                  reported.  */
   16199        16752 :               has_non_interop_arg = 1;
   16200              : 
   16201        16752 :           curr_arg = curr_arg->next;
   16202              :         }
   16203              : 
   16204              :       /* See if any of the arguments were not interoperable and if so, clear
   16205              :          the procedure symbol to prevent duplicate error messages.  */
   16206         6808 :       if (has_non_interop_arg != 0)
   16207              :         {
   16208          128 :           sym->attr.is_c_interop = 0;
   16209          128 :           sym->ts.is_c_interop = 0;
   16210          128 :           sym->attr.is_bind_c = 0;
   16211              :         }
   16212              :     }
   16213              : 
   16214       483752 :   if (!sym->attr.proc_pointer)
   16215              :     {
   16216       482646 :       if (sym->attr.save == SAVE_EXPLICIT)
   16217              :         {
   16218            5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   16219              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16220            5 :           return false;
   16221              :         }
   16222       482641 :       if (sym->attr.intent)
   16223              :         {
   16224            1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   16225              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16226            1 :           return false;
   16227              :         }
   16228       482640 :       if (sym->attr.subroutine && sym->attr.result)
   16229              :         {
   16230            2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   16231            2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   16232            2 :           return false;
   16233              :         }
   16234       482638 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   16235       136880 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   16236       136877 :               || sym->attr.contained))
   16237              :         {
   16238            3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   16239              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16240            3 :           return false;
   16241              :         }
   16242       482635 :       if (strcmp ("ppr@", sym->name) == 0)
   16243              :         {
   16244            0 :           gfc_error ("Procedure pointer result %qs at %L "
   16245              :                      "is missing the pointer attribute",
   16246            0 :                      sym->ns->proc_name->name, &sym->declared_at);
   16247            0 :           return false;
   16248              :         }
   16249              :     }
   16250              : 
   16251              :   /* Assume that a procedure whose body is not known has references
   16252              :      to external arrays.  */
   16253       483741 :   if (sym->attr.if_source != IFSRC_DECL)
   16254       330413 :     sym->attr.array_outer_dependency = 1;
   16255              : 
   16256              :   /* Compare the characteristics of a module procedure with the
   16257              :      interface declaration. Ideally this would be done with
   16258              :      gfc_compare_interfaces but, at present, the formal interface
   16259              :      cannot be copied to the ts.interface.  */
   16260       483741 :   if (sym->attr.module_procedure
   16261         1612 :       && sym->attr.if_source == IFSRC_DECL)
   16262              :     {
   16263          657 :       gfc_symbol *iface;
   16264          657 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   16265          657 :       char *module_name;
   16266          657 :       char *submodule_name;
   16267          657 :       strcpy (name, sym->ns->proc_name->name);
   16268          657 :       module_name = strtok (name, ".");
   16269          657 :       submodule_name = strtok (NULL, ".");
   16270              : 
   16271          657 :       iface = sym->tlink;
   16272          657 :       sym->tlink = NULL;
   16273              : 
   16274              :       /* Make sure that the result uses the correct charlen for deferred
   16275              :          length results.  */
   16276          657 :       if (iface && sym->result
   16277          192 :           && iface->ts.type == BT_CHARACTER
   16278           19 :           && iface->ts.deferred)
   16279            6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   16280              : 
   16281            6 :       if (iface == NULL)
   16282          195 :         goto check_formal;
   16283              : 
   16284              :       /* Check the procedure characteristics.  */
   16285          462 :       if (sym->attr.elemental != iface->attr.elemental)
   16286              :         {
   16287            1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   16288              :                      "PROCEDURE at %L and its interface in %s",
   16289              :                      &sym->declared_at, module_name);
   16290           10 :           return false;
   16291              :         }
   16292              : 
   16293          461 :       if (sym->attr.pure != iface->attr.pure)
   16294              :         {
   16295            2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   16296              :                      "PROCEDURE at %L and its interface in %s",
   16297              :                      &sym->declared_at, module_name);
   16298            2 :           return false;
   16299              :         }
   16300              : 
   16301          459 :       if (sym->attr.recursive != iface->attr.recursive)
   16302              :         {
   16303            2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   16304              :                      "PROCEDURE at %L and its interface in %s",
   16305              :                      &sym->declared_at, module_name);
   16306            2 :           return false;
   16307              :         }
   16308              : 
   16309              :       /* Check the result characteristics.  */
   16310          457 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   16311              :         {
   16312            5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   16313              :                      "in MODULE %qs and the declaration at %L in "
   16314              :                      "(SUB)MODULE %qs",
   16315              :                      errmsg, module_name, &sym->declared_at,
   16316              :                      submodule_name ? submodule_name : module_name);
   16317            5 :           return false;
   16318              :         }
   16319              : 
   16320          452 : check_formal:
   16321              :       /* Check the characteristics of the formal arguments.  */
   16322          647 :       if (sym->formal && sym->formal_ns)
   16323              :         {
   16324         1256 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   16325              :             {
   16326          720 :               new_formal = arg;
   16327          720 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   16328              :             }
   16329              :         }
   16330              :     }
   16331              : 
   16332              :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   16333              :      BIND(C) attribute.  */
   16334       483731 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   16335              :     {
   16336            1 :       gfc_error ("Interface of %qs at %L must be explicit",
   16337              :                  sym->name, &sym->declared_at);
   16338            1 :       return false;
   16339              :     }
   16340              : 
   16341              :   return true;
   16342              : }
   16343              : 
   16344              : 
   16345              : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   16346              :    been defined and we now know their defined arguments, check that they fulfill
   16347              :    the requirements of the standard for procedures used as finalizers.  */
   16348              : 
   16349              : static bool
   16350       113122 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   16351              : {
   16352       113122 :   gfc_finalizer *list, *pdt_finalizers = NULL;
   16353       113122 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   16354       113122 :   bool result = true;
   16355       113122 :   bool seen_scalar = false;
   16356       113122 :   gfc_symbol *vtab;
   16357       113122 :   gfc_component *c;
   16358       113122 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   16359              : 
   16360       113122 :   if (parent)
   16361        15755 :     gfc_resolve_finalizers (parent, finalizable);
   16362              : 
   16363              :   /* Ensure that derived-type components have a their finalizers resolved.  */
   16364       113122 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   16365       355987 :   for (c = derived->components; c; c = c->next)
   16366       242865 :     if (c->ts.type == BT_DERIVED
   16367        68277 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   16368              :       {
   16369         8376 :         bool has_final2 = false;
   16370         8376 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   16371            0 :           return false;  /* Error.  */
   16372         8376 :         has_final = has_final || has_final2;
   16373              :       }
   16374              :   /* Return early if not finalizable.  */
   16375       113122 :   if (!has_final)
   16376              :     {
   16377       110527 :       if (finalizable)
   16378         8266 :         *finalizable = false;
   16379       110527 :       return true;
   16380              :     }
   16381              : 
   16382              :   /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
   16383              :      the template. If the finalizers field has the same value, it needs to be
   16384              :      supplied with finalizers of the same pdt_type.  */
   16385         2595 :   if (derived->attr.pdt_type
   16386           30 :       && derived->template_sym
   16387           12 :       && derived->template_sym->f2k_derived
   16388           12 :       && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
   16389         2607 :       && derived->f2k_derived->finalizers == pdt_finalizers)
   16390              :     {
   16391           12 :       gfc_finalizer *tmp = NULL;
   16392           12 :       derived->f2k_derived->finalizers = NULL;
   16393           12 :       prev_link = &derived->f2k_derived->finalizers;
   16394           48 :       for (list = pdt_finalizers; list; list = list->next)
   16395              :         {
   16396           36 :           gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
   16397           36 :           if (args->sym
   16398           36 :               && args->sym->ts.type == BT_DERIVED
   16399           36 :               && args->sym->ts.u.derived
   16400           36 :               && !strcmp (args->sym->ts.u.derived->name, derived->name))
   16401              :             {
   16402           18 :               tmp = gfc_get_finalizer ();
   16403           18 :               *tmp = *list;
   16404           18 :               tmp->next = NULL;
   16405           18 :               if (*prev_link)
   16406              :                 {
   16407            6 :                   (*prev_link)->next = tmp;
   16408            6 :                   prev_link = &tmp;
   16409              :                 }
   16410              :               else
   16411           12 :                 *prev_link = tmp;
   16412           18 :               list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16413              :             }
   16414              :         }
   16415              :     }
   16416              : 
   16417              :   /* Walk over the list of finalizer-procedures, check them, and if any one
   16418              :      does not fit in with the standard's definition, print an error and remove
   16419              :      it from the list.  */
   16420         2595 :   prev_link = &derived->f2k_derived->finalizers;
   16421         5326 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   16422              :     {
   16423         2731 :       gfc_formal_arglist *dummy_args;
   16424         2731 :       gfc_symbol* arg;
   16425         2731 :       gfc_finalizer* i;
   16426         2731 :       int my_rank;
   16427              : 
   16428              :       /* Skip this finalizer if we already resolved it.  */
   16429         2731 :       if (list->proc_tree)
   16430              :         {
   16431         2192 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   16432          584 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   16433         1608 :             seen_scalar = true;
   16434         2192 :           prev_link = &(list->next);
   16435         2192 :           continue;
   16436              :         }
   16437              : 
   16438              :       /* Check this exists and is a SUBROUTINE.  */
   16439          539 :       if (!list->proc_sym->attr.subroutine)
   16440              :         {
   16441            3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   16442              :                      list->proc_sym->name, &list->where);
   16443            3 :           goto error;
   16444              :         }
   16445              : 
   16446              :       /* We should have exactly one argument.  */
   16447          536 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   16448          536 :       if (!dummy_args || dummy_args->next)
   16449              :         {
   16450            2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   16451              :                      &list->where);
   16452            2 :           goto error;
   16453              :         }
   16454          534 :       arg = dummy_args->sym;
   16455              : 
   16456          534 :       if (!arg)
   16457              :         {
   16458            1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16459            1 :                      &list->proc_sym->declared_at, derived->name);
   16460            1 :           goto error;
   16461              :         }
   16462              : 
   16463          533 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   16464            6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   16465              :         {
   16466            0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   16467              :                      "be the only finalizer with the same kind/type "
   16468              :                      "(F2018: C790)", &list->where);
   16469            0 :           goto error;
   16470              :         }
   16471              : 
   16472              :       /* This argument must be of our type.  */
   16473          533 :       if (!derived->attr.pdt_template
   16474          533 :           && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
   16475              :         {
   16476            2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16477              :                      &arg->declared_at, derived->name);
   16478            2 :           goto error;
   16479              :         }
   16480              : 
   16481              :       /* It must neither be a pointer nor allocatable nor optional.  */
   16482          531 :       if (arg->attr.pointer)
   16483              :         {
   16484            1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   16485              :                      &arg->declared_at);
   16486            1 :           goto error;
   16487              :         }
   16488          530 :       if (arg->attr.allocatable)
   16489              :         {
   16490            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16491              :                      " ALLOCATABLE", &arg->declared_at);
   16492            1 :           goto error;
   16493              :         }
   16494          529 :       if (arg->attr.optional)
   16495              :         {
   16496            1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   16497              :                      &arg->declared_at);
   16498            1 :           goto error;
   16499              :         }
   16500              : 
   16501              :       /* It must not be INTENT(OUT).  */
   16502          528 :       if (arg->attr.intent == INTENT_OUT)
   16503              :         {
   16504            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16505              :                      " INTENT(OUT)", &arg->declared_at);
   16506            1 :           goto error;
   16507              :         }
   16508              : 
   16509              :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   16510          527 :       if (warn_surprising && arg->as && arg->as->rank != 0
   16511            3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   16512            2 :         gfc_warning (OPT_Wsurprising,
   16513              :                      "Non-scalar FINAL procedure at %L should have assumed"
   16514              :                      " shape argument", &arg->declared_at);
   16515              : 
   16516              :       /* Check that it does not match in kind and rank with a FINAL procedure
   16517              :          defined earlier.  To really loop over the *earlier* declarations,
   16518              :          we need to walk the tail of the list as new ones were pushed at the
   16519              :          front.  */
   16520              :       /* TODO: Handle kind parameters once they are implemented.  */
   16521          527 :       my_rank = (arg->as ? arg->as->rank : 0);
   16522          622 :       for (i = list->next; i; i = i->next)
   16523              :         {
   16524           97 :           gfc_formal_arglist *dummy_args;
   16525              : 
   16526              :           /* Argument list might be empty; that is an error signalled earlier,
   16527              :              but we nevertheless continued resolving.  */
   16528           97 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   16529           97 :           if (dummy_args && !derived->attr.pdt_template)
   16530              :             {
   16531           95 :               gfc_symbol* i_arg = dummy_args->sym;
   16532           95 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   16533           95 :               if (i_rank == my_rank)
   16534              :                 {
   16535            2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   16536              :                              " rank (%d) as %qs",
   16537            2 :                              list->proc_sym->name, &list->where, my_rank,
   16538            2 :                              i->proc_sym->name);
   16539            2 :                   goto error;
   16540              :                 }
   16541              :             }
   16542              :         }
   16543              : 
   16544              :         /* Is this the/a scalar finalizer procedure?  */
   16545          525 :         if (my_rank == 0)
   16546          399 :           seen_scalar = true;
   16547              : 
   16548              :         /* Find the symtree for this procedure.  */
   16549          525 :         gcc_assert (!list->proc_tree);
   16550          525 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16551              : 
   16552          525 :         prev_link = &list->next;
   16553          525 :         continue;
   16554              : 
   16555              :         /* Remove wrong nodes immediately from the list so we don't risk any
   16556              :            troubles in the future when they might fail later expectations.  */
   16557           14 : error:
   16558           14 :         i = list;
   16559           14 :         *prev_link = list->next;
   16560           14 :         gfc_free_finalizer (i);
   16561           14 :         result = false;
   16562          525 :     }
   16563              : 
   16564         2595 :   if (result == false)
   16565              :     return false;
   16566              : 
   16567              :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   16568              :      were nodes in the list, must have been for arrays.  It is surely a good
   16569              :      idea to have a scalar version there if there's something to finalize.  */
   16570         2591 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   16571            1 :     gfc_warning (OPT_Wsurprising,
   16572              :                  "Only array FINAL procedures declared for derived type %qs"
   16573              :                  " defined at %L, suggest also scalar one unless an assumed"
   16574              :                  " rank finalizer has been declared",
   16575              :                  derived->name, &derived->declared_at);
   16576              : 
   16577         2591 :   if (!derived->attr.pdt_template)
   16578              :     {
   16579         2567 :       vtab = gfc_find_derived_vtab (derived);
   16580         2567 :       c = vtab->ts.u.derived->components->next->next->next->next->next;
   16581         2567 :       if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
   16582         2567 :         gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   16583              :     }
   16584              : 
   16585         2591 :   if (finalizable)
   16586          664 :     *finalizable = true;
   16587              : 
   16588              :   return true;
   16589              : }
   16590              : 
   16591              : 
   16592              : static gfc_symbol * containing_dt;
   16593              : 
   16594              : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
   16595              :    arguments whose declared types are PDT instances only transmit the PASS arg
   16596              :    if they match the enclosing derived type.  */
   16597              : 
   16598              : static bool
   16599         1496 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
   16600              : {
   16601         1496 :   gfc_formal_arglist *dummy_args;
   16602         1496 :   if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
   16603              :     {
   16604          532 :       dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
   16605         1190 :       while (dummy_args && strcmp (pass, dummy_args->sym->name))
   16606          126 :         dummy_args = dummy_args->next;
   16607          532 :       gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
   16608          532 :       if (dummy_args->sym->ts.type == BT_CLASS
   16609          532 :           && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
   16610              :                      containing_dt->name))
   16611              :         return true;
   16612              :     }
   16613              :   return false;
   16614              : }
   16615              : 
   16616              : 
   16617              : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   16618              : 
   16619              : static bool
   16620          750 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   16621              :                              const char* generic_name, locus where)
   16622              : {
   16623          750 :   gfc_symbol *sym1, *sym2;
   16624          750 :   const char *pass1, *pass2;
   16625          750 :   gfc_formal_arglist *dummy_args;
   16626              : 
   16627          750 :   gcc_assert (t1->specific && t2->specific);
   16628          750 :   gcc_assert (!t1->specific->is_generic);
   16629          750 :   gcc_assert (!t2->specific->is_generic);
   16630          750 :   gcc_assert (t1->is_operator == t2->is_operator);
   16631              : 
   16632          750 :   sym1 = t1->specific->u.specific->n.sym;
   16633          750 :   sym2 = t2->specific->u.specific->n.sym;
   16634              : 
   16635          750 :   if (sym1 == sym2)
   16636              :     return true;
   16637              : 
   16638              :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   16639          750 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   16640          748 :       || sym1->attr.function != sym2->attr.function)
   16641              :     {
   16642            2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   16643              :                  " GENERIC %qs at %L",
   16644              :                  sym1->name, sym2->name, generic_name, &where);
   16645            2 :       return false;
   16646              :     }
   16647              : 
   16648              :   /* Determine PASS arguments.  */
   16649          748 :   if (t1->specific->nopass)
   16650              :     pass1 = NULL;
   16651          697 :   else if (t1->specific->pass_arg)
   16652              :     pass1 = t1->specific->pass_arg;
   16653              :   else
   16654              :     {
   16655          438 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   16656          438 :       if (dummy_args)
   16657          437 :         pass1 = dummy_args->sym->name;
   16658              :       else
   16659              :         pass1 = NULL;
   16660              :     }
   16661          748 :   if (t2->specific->nopass)
   16662              :     pass2 = NULL;
   16663          696 :   else if (t2->specific->pass_arg)
   16664              :     pass2 = t2->specific->pass_arg;
   16665              :   else
   16666              :     {
   16667          559 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   16668          559 :       if (dummy_args)
   16669          558 :         pass2 = dummy_args->sym->name;
   16670              :       else
   16671              :         pass2 = NULL;
   16672              :     }
   16673              : 
   16674              :   /* Care must be taken with pdt types and templates because the declared type
   16675              :      of the argument that is not 'no_pass' need not be the same as the
   16676              :      containing derived type.  If this is the case, subject the argument to
   16677              :      the full interface check, even though it cannot be used in the type
   16678              :      bound context.  */
   16679          748 :   pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
   16680          748 :   pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
   16681              : 
   16682          748 :   if (containing_dt != NULL && containing_dt->attr.pdt_template)
   16683          748 :     pass1 = pass2 = NULL;
   16684              : 
   16685              :   /* Compare the interfaces.  */
   16686          748 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   16687              :                               NULL, 0, pass1, pass2))
   16688              :     {
   16689            8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   16690              :                  sym1->name, sym2->name, generic_name, &where);
   16691            8 :       return false;
   16692              :     }
   16693              : 
   16694              :   return true;
   16695              : }
   16696              : 
   16697              : 
   16698              : /* Worker function for resolving a generic procedure binding; this is used to
   16699              :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   16700              : 
   16701              :    The difference between those cases is finding possible inherited bindings
   16702              :    that are overridden, as one has to look for them in tb_sym_root,
   16703              :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   16704              :    the super-type and set p->overridden correctly.  */
   16705              : 
   16706              : static bool
   16707         2409 : resolve_tb_generic_targets (gfc_symbol* super_type,
   16708              :                             gfc_typebound_proc* p, const char* name)
   16709              : {
   16710         2409 :   gfc_tbp_generic* target;
   16711         2409 :   gfc_symtree* first_target;
   16712         2409 :   gfc_symtree* inherited;
   16713              : 
   16714         2409 :   gcc_assert (p && p->is_generic);
   16715              : 
   16716              :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   16717         2409 :   gcc_assert (p->u.generic);
   16718         5422 :   for (target = p->u.generic; target; target = target->next)
   16719         3030 :     if (!target->specific)
   16720              :       {
   16721         2615 :         gfc_typebound_proc* overridden_tbp;
   16722         2615 :         gfc_tbp_generic* g;
   16723         2615 :         const char* target_name;
   16724              : 
   16725         2615 :         target_name = target->specific_st->name;
   16726              : 
   16727              :         /* Defined for this type directly.  */
   16728         2615 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   16729              :           {
   16730         2606 :             target->specific = target->specific_st->n.tb;
   16731         2606 :             goto specific_found;
   16732              :           }
   16733              : 
   16734              :         /* Look for an inherited specific binding.  */
   16735            9 :         if (super_type)
   16736              :           {
   16737            5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   16738              :                                                  true, NULL);
   16739              : 
   16740            5 :             if (inherited)
   16741              :               {
   16742            5 :                 gcc_assert (inherited->n.tb);
   16743            5 :                 target->specific = inherited->n.tb;
   16744            5 :                 goto specific_found;
   16745              :               }
   16746              :           }
   16747              : 
   16748            4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   16749              :                    " at %L", target_name, name, &p->where);
   16750            4 :         return false;
   16751              : 
   16752              :         /* Once we've found the specific binding, check it is not ambiguous with
   16753              :            other specifics already found or inherited for the same GENERIC.  */
   16754         2611 : specific_found:
   16755         2611 :         gcc_assert (target->specific);
   16756              : 
   16757              :         /* This must really be a specific binding!  */
   16758         2611 :         if (target->specific->is_generic)
   16759              :           {
   16760            3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   16761              :                        " %qs is GENERIC, too", name, &p->where, target_name);
   16762            3 :             return false;
   16763              :           }
   16764              : 
   16765              :         /* Check those already resolved on this type directly.  */
   16766         6666 :         for (g = p->u.generic; g; g = g->next)
   16767         1464 :           if (g != target && g->specific
   16768         4797 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   16769              :             return false;
   16770              : 
   16771              :         /* Check for ambiguity with inherited specific targets.  */
   16772         2617 :         for (overridden_tbp = p->overridden; overridden_tbp;
   16773           16 :              overridden_tbp = overridden_tbp->overridden)
   16774           19 :           if (overridden_tbp->is_generic)
   16775              :             {
   16776           33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   16777              :                 {
   16778           18 :                   gcc_assert (g->specific);
   16779           18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   16780              :                     return false;
   16781              :                 }
   16782              :             }
   16783              :       }
   16784              : 
   16785              :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   16786         2392 :   if (p->overridden && !p->overridden->is_generic)
   16787              :     {
   16788            1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   16789              :                  " the same name", name, &p->where);
   16790            1 :       return false;
   16791              :     }
   16792              : 
   16793              :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   16794              :      all must have the same attributes here.  */
   16795         2391 :   first_target = p->u.generic->specific->u.specific;
   16796         2391 :   gcc_assert (first_target);
   16797         2391 :   p->subroutine = first_target->n.sym->attr.subroutine;
   16798         2391 :   p->function = first_target->n.sym->attr.function;
   16799              : 
   16800         2391 :   return true;
   16801              : }
   16802              : 
   16803              : 
   16804              : /* Resolve a GENERIC procedure binding for a derived type.  */
   16805              : 
   16806              : static bool
   16807         1249 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   16808              : {
   16809         1249 :   gfc_symbol* super_type;
   16810              : 
   16811              :   /* Find the overridden binding if any.  */
   16812         1249 :   st->n.tb->overridden = NULL;
   16813         1249 :   super_type = gfc_get_derived_super_type (derived);
   16814         1249 :   if (super_type)
   16815              :     {
   16816           40 :       gfc_symtree* overridden;
   16817           40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   16818              :                                             true, NULL);
   16819              : 
   16820           40 :       if (overridden && overridden->n.tb)
   16821           21 :         st->n.tb->overridden = overridden->n.tb;
   16822              :     }
   16823              : 
   16824              :   /* Resolve using worker function.  */
   16825         1249 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   16826              : }
   16827              : 
   16828              : 
   16829              : /* Retrieve the target-procedure of an operator binding and do some checks in
   16830              :    common for intrinsic and user-defined type-bound operators.  */
   16831              : 
   16832              : static gfc_symbol*
   16833         1232 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   16834              : {
   16835         1232 :   gfc_symbol* target_proc;
   16836              : 
   16837         1232 :   gcc_assert (target->specific && !target->specific->is_generic);
   16838         1232 :   target_proc = target->specific->u.specific->n.sym;
   16839         1232 :   gcc_assert (target_proc);
   16840              : 
   16841              :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   16842         1232 :   if (target->specific->nopass)
   16843              :     {
   16844            2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   16845            2 :       return NULL;
   16846              :     }
   16847              : 
   16848              :   return target_proc;
   16849              : }
   16850              : 
   16851              : 
   16852              : /* Resolve a type-bound intrinsic operator.  */
   16853              : 
   16854              : static bool
   16855         1047 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   16856              :                                 gfc_typebound_proc* p)
   16857              : {
   16858         1047 :   gfc_symbol* super_type;
   16859         1047 :   gfc_tbp_generic* target;
   16860              : 
   16861              :   /* If there's already an error here, do nothing (but don't fail again).  */
   16862         1047 :   if (p->error)
   16863              :     return true;
   16864              : 
   16865              :   /* Operators should always be GENERIC bindings.  */
   16866         1047 :   gcc_assert (p->is_generic);
   16867              : 
   16868              :   /* Look for an overridden binding.  */
   16869         1047 :   super_type = gfc_get_derived_super_type (derived);
   16870         1047 :   if (super_type && super_type->f2k_derived)
   16871            1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   16872              :                                                      op, true, NULL);
   16873              :   else
   16874         1046 :     p->overridden = NULL;
   16875              : 
   16876              :   /* Resolve general GENERIC properties using worker function.  */
   16877         1047 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   16878            1 :     goto error;
   16879              : 
   16880              :   /* Check the targets to be procedures of correct interface.  */
   16881         2139 :   for (target = p->u.generic; target; target = target->next)
   16882              :     {
   16883         1118 :       gfc_symbol* target_proc;
   16884              : 
   16885         1118 :       target_proc = get_checked_tb_operator_target (target, p->where);
   16886         1118 :       if (!target_proc)
   16887            1 :         goto error;
   16888              : 
   16889         1117 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   16890            3 :         goto error;
   16891              : 
   16892              :       /* Add target to non-typebound operator list.  */
   16893         1114 :       if (!target->specific->deferred && !derived->attr.use_assoc
   16894          391 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   16895              :         {
   16896          389 :           gfc_interface *head, *intr;
   16897              : 
   16898              :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   16899              :              mechanism for handling module procedures winds up resolving
   16900              :              operator interfaces twice and would otherwise cause an error.
   16901              :              Likewise, new instances of PDTs can cause the operator inter-
   16902              :              faces to be resolved multiple times.  */
   16903          461 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   16904           91 :             if (intr->sym == target_proc
   16905           21 :                 && (target_proc->attr.used_in_submodule
   16906            4 :                     || derived->attr.pdt_type
   16907            2 :                     || derived->attr.pdt_template))
   16908              :               return true;
   16909              : 
   16910          370 :           if (!gfc_check_new_interface (derived->ns->op[op],
   16911              :                                         target_proc, p->where))
   16912              :             return false;
   16913          368 :           head = derived->ns->op[op];
   16914          368 :           intr = gfc_get_interface ();
   16915          368 :           intr->sym = target_proc;
   16916          368 :           intr->where = p->where;
   16917          368 :           intr->next = head;
   16918          368 :           derived->ns->op[op] = intr;
   16919              :         }
   16920              :     }
   16921              : 
   16922              :   return true;
   16923              : 
   16924            5 : error:
   16925            5 :   p->error = 1;
   16926            5 :   return false;
   16927              : }
   16928              : 
   16929              : 
   16930              : /* Resolve a type-bound user operator (tree-walker callback).  */
   16931              : 
   16932              : static gfc_symbol* resolve_bindings_derived;
   16933              : static bool resolve_bindings_result;
   16934              : 
   16935              : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   16936              : 
   16937              : static void
   16938          113 : resolve_typebound_user_op (gfc_symtree* stree)
   16939              : {
   16940          113 :   gfc_symbol* super_type;
   16941          113 :   gfc_tbp_generic* target;
   16942              : 
   16943          113 :   gcc_assert (stree && stree->n.tb);
   16944              : 
   16945          113 :   if (stree->n.tb->error)
   16946              :     return;
   16947              : 
   16948              :   /* Operators should always be GENERIC bindings.  */
   16949          113 :   gcc_assert (stree->n.tb->is_generic);
   16950              : 
   16951              :   /* Find overridden procedure, if any.  */
   16952          113 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   16953          113 :   if (super_type && super_type->f2k_derived)
   16954              :     {
   16955           18 :       gfc_symtree* overridden;
   16956           18 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   16957              :                                                stree->name, true, NULL);
   16958              : 
   16959           18 :       if (overridden && overridden->n.tb)
   16960            0 :         stree->n.tb->overridden = overridden->n.tb;
   16961              :     }
   16962              :   else
   16963           95 :     stree->n.tb->overridden = NULL;
   16964              : 
   16965              :   /* Resolve basically using worker function.  */
   16966          113 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   16967            0 :     goto error;
   16968              : 
   16969              :   /* Check the targets to be functions of correct interface.  */
   16970          224 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   16971              :     {
   16972          114 :       gfc_symbol* target_proc;
   16973              : 
   16974          114 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   16975          114 :       if (!target_proc)
   16976            1 :         goto error;
   16977              : 
   16978          113 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   16979            2 :         goto error;
   16980              :     }
   16981              : 
   16982              :   return;
   16983              : 
   16984            3 : error:
   16985            3 :   resolve_bindings_result = false;
   16986            3 :   stree->n.tb->error = 1;
   16987              : }
   16988              : 
   16989              : 
   16990              : /* Resolve the type-bound procedures for a derived type.  */
   16991              : 
   16992              : static void
   16993        10183 : resolve_typebound_procedure (gfc_symtree* stree)
   16994              : {
   16995        10183 :   gfc_symbol* proc;
   16996        10183 :   locus where;
   16997        10183 :   gfc_symbol* me_arg;
   16998        10183 :   gfc_symbol* super_type;
   16999        10183 :   gfc_component* comp;
   17000              : 
   17001        10183 :   gcc_assert (stree);
   17002              : 
   17003              :   /* Undefined specific symbol from GENERIC target definition.  */
   17004        10183 :   if (!stree->n.tb)
   17005        10101 :     return;
   17006              : 
   17007        10177 :   if (stree->n.tb->error)
   17008              :     return;
   17009              : 
   17010              :   /* If this is a GENERIC binding, use that routine.  */
   17011        10161 :   if (stree->n.tb->is_generic)
   17012              :     {
   17013         1249 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   17014           17 :         goto error;
   17015              :       return;
   17016              :     }
   17017              : 
   17018              :   /* Get the target-procedure to check it.  */
   17019         8912 :   gcc_assert (!stree->n.tb->is_generic);
   17020         8912 :   gcc_assert (stree->n.tb->u.specific);
   17021         8912 :   proc = stree->n.tb->u.specific->n.sym;
   17022         8912 :   where = stree->n.tb->where;
   17023              : 
   17024              :   /* Default access should already be resolved from the parser.  */
   17025         8912 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   17026              : 
   17027         8912 :   if (stree->n.tb->deferred)
   17028              :     {
   17029          676 :       if (!check_proc_interface (proc, &where))
   17030            5 :         goto error;
   17031              :     }
   17032              :   else
   17033              :     {
   17034              :       /* If proc has not been resolved at this point, proc->name may
   17035              :          actually be a USE associated entity. See PR fortran/89647. */
   17036         8236 :       if (!proc->resolve_symbol_called
   17037         5698 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   17038              :         {
   17039           11 :           gfc_symbol *tmp;
   17040           11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   17041           11 :           if (tmp && tmp->attr.use_assoc)
   17042              :             {
   17043            1 :               proc->module = tmp->module;
   17044            1 :               proc->attr.proc = tmp->attr.proc;
   17045            1 :               proc->attr.function = tmp->attr.function;
   17046            1 :               proc->attr.subroutine = tmp->attr.subroutine;
   17047            1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   17048            1 :               proc->ts = tmp->ts;
   17049            1 :               proc->result = tmp->result;
   17050              :             }
   17051              :         }
   17052              : 
   17053              :       /* Check for F08:C465.  */
   17054         8236 :       if ((!proc->attr.subroutine && !proc->attr.function)
   17055         8226 :           || (proc->attr.proc != PROC_MODULE
   17056           70 :               && proc->attr.if_source != IFSRC_IFBODY
   17057            7 :               && !proc->attr.module_procedure)
   17058         8225 :           || proc->attr.abstract)
   17059              :         {
   17060           12 :           gfc_error ("%qs must be a module procedure or an external "
   17061              :                      "procedure with an explicit interface at %L",
   17062              :                      proc->name, &where);
   17063           12 :           goto error;
   17064              :         }
   17065              :     }
   17066              : 
   17067         8895 :   stree->n.tb->subroutine = proc->attr.subroutine;
   17068         8895 :   stree->n.tb->function = proc->attr.function;
   17069              : 
   17070              :   /* Find the super-type of the current derived type.  We could do this once and
   17071              :      store in a global if speed is needed, but as long as not I believe this is
   17072              :      more readable and clearer.  */
   17073         8895 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   17074              : 
   17075              :   /* If PASS, resolve and check arguments if not already resolved / loaded
   17076              :      from a .mod file.  */
   17077         8895 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   17078              :     {
   17079         2838 :       gfc_formal_arglist *dummy_args;
   17080              : 
   17081         2838 :       dummy_args = gfc_sym_get_dummy_args (proc);
   17082         2838 :       if (stree->n.tb->pass_arg)
   17083              :         {
   17084          468 :           gfc_formal_arglist *i;
   17085              : 
   17086              :           /* If an explicit passing argument name is given, walk the arg-list
   17087              :              and look for it.  */
   17088              : 
   17089          468 :           me_arg = NULL;
   17090          468 :           stree->n.tb->pass_arg_num = 1;
   17091          601 :           for (i = dummy_args; i; i = i->next)
   17092              :             {
   17093          599 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   17094              :                 {
   17095              :                   me_arg = i->sym;
   17096              :                   break;
   17097              :                 }
   17098          133 :               ++stree->n.tb->pass_arg_num;
   17099              :             }
   17100              : 
   17101          468 :           if (!me_arg)
   17102              :             {
   17103            2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   17104              :                          " argument %qs",
   17105              :                          proc->name, stree->n.tb->pass_arg, &where,
   17106              :                          stree->n.tb->pass_arg);
   17107            2 :               goto error;
   17108              :             }
   17109              :         }
   17110              :       else
   17111              :         {
   17112              :           /* Otherwise, take the first one; there should in fact be at least
   17113              :              one.  */
   17114         2370 :           stree->n.tb->pass_arg_num = 1;
   17115         2370 :           if (!dummy_args)
   17116              :             {
   17117            2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   17118              :                          " least one argument", proc->name, &where);
   17119            2 :               goto error;
   17120              :             }
   17121         2368 :           me_arg = dummy_args->sym;
   17122              :         }
   17123              : 
   17124              :       /* Now check that the argument-type matches and the passed-object
   17125              :          dummy argument is generally fine.  */
   17126              : 
   17127         2368 :       gcc_assert (me_arg);
   17128              : 
   17129         2834 :       if (me_arg->ts.type != BT_CLASS)
   17130              :         {
   17131            5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17132              :                      " at %L", proc->name, &where);
   17133            5 :           goto error;
   17134              :         }
   17135              : 
   17136              :       /* The derived type is not a PDT template or type.  Resolve as usual.  */
   17137         2829 :       if (!resolve_bindings_derived->attr.pdt_template
   17138         2820 :           && !(containing_dt && containing_dt->attr.pdt_type
   17139           60 :                && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
   17140         2800 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   17141              :         {
   17142            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17143              :                      "the derived-type %qs", me_arg->name, proc->name,
   17144              :                      me_arg->name, &where, resolve_bindings_derived->name);
   17145            0 :           goto error;
   17146              :         }
   17147              : 
   17148         2829 :       if (resolve_bindings_derived->attr.pdt_template
   17149         2838 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   17150            9 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   17151              :         {
   17152            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17153              :                      "the parametric derived-type %qs", me_arg->name,
   17154              :                      proc->name, me_arg->name, &where,
   17155              :                      resolve_bindings_derived->name);
   17156            0 :           goto error;
   17157              :         }
   17158              : 
   17159         2829 :       if (((resolve_bindings_derived->attr.pdt_template
   17160            9 :             && gfc_pdt_is_instance_of (resolve_bindings_derived,
   17161            9 :                                        CLASS_DATA (me_arg)->ts.u.derived))
   17162         2820 :            || resolve_bindings_derived->attr.pdt_type)
   17163           69 :           && (me_arg->param_list != NULL)
   17164         2898 :           && (gfc_spec_list_type (me_arg->param_list,
   17165           69 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   17166              :                                   != SPEC_ASSUMED))
   17167              :         {
   17168              : 
   17169              :           /* Add a check to verify if there are any LEN parameters in the
   17170              :              first place.  If there are LEN parameters, throw this error.
   17171              :              If there are only KIND parameters, then don't trigger
   17172              :              this error.  */
   17173            6 :           gfc_component *c;
   17174            6 :           bool seen_len_param = false;
   17175            6 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   17176              : 
   17177            6 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   17178              :             {
   17179            6 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   17180              :                                      me_arg_param->name, true, true, NULL);
   17181              : 
   17182            6 :               gcc_assert (c != NULL);
   17183              : 
   17184            6 :               if (c->attr.pdt_kind)
   17185            0 :                 continue;
   17186              : 
   17187              :               /* Getting here implies that there is a pdt_len parameter
   17188              :                  in the list.  */
   17189              :               seen_len_param = true;
   17190              :               break;
   17191              :             }
   17192              : 
   17193            6 :             if (seen_len_param)
   17194              :               {
   17195            6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   17196              :                            "argument %qs of %qs at %L must be ASSUMED.",
   17197              :                            me_arg->name, proc->name, &where);
   17198            6 :                 goto error;
   17199              :               }
   17200              :         }
   17201              : 
   17202         2823 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   17203         2823 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   17204              :         {
   17205            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   17206              :                      " scalar", proc->name, &where);
   17207            1 :           goto error;
   17208              :         }
   17209         2822 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17210              :         {
   17211            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17212              :                      " be ALLOCATABLE", proc->name, &where);
   17213            2 :           goto error;
   17214              :         }
   17215         2820 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17216              :         {
   17217            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17218              :                      " be POINTER", proc->name, &where);
   17219            2 :           goto error;
   17220              :         }
   17221              :     }
   17222              : 
   17223              :   /* If we are extending some type, check that we don't override a procedure
   17224              :      flagged NON_OVERRIDABLE.  */
   17225         8875 :   stree->n.tb->overridden = NULL;
   17226         8875 :   if (super_type)
   17227              :     {
   17228         1513 :       gfc_symtree* overridden;
   17229         1513 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   17230              :                                             stree->name, true, NULL);
   17231              : 
   17232         1513 :       if (overridden)
   17233              :         {
   17234         1218 :           if (overridden->n.tb)
   17235         1218 :             stree->n.tb->overridden = overridden->n.tb;
   17236              : 
   17237         1218 :           if (!gfc_check_typebound_override (stree, overridden))
   17238           26 :             goto error;
   17239              :         }
   17240              :     }
   17241              : 
   17242              :   /* See if there's a name collision with a component directly in this type.  */
   17243        21237 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   17244        12389 :     if (!strcmp (comp->name, stree->name))
   17245              :       {
   17246            1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   17247              :                    " %qs",
   17248              :                    stree->name, &where, resolve_bindings_derived->name);
   17249            1 :         goto error;
   17250              :       }
   17251              : 
   17252              :   /* Try to find a name collision with an inherited component.  */
   17253         8848 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   17254              :                                         NULL))
   17255              :     {
   17256            1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   17257              :                  " component of %qs",
   17258              :                  stree->name, &where, resolve_bindings_derived->name);
   17259            1 :       goto error;
   17260              :     }
   17261              : 
   17262         8847 :   stree->n.tb->error = 0;
   17263         8847 :   return;
   17264              : 
   17265           82 : error:
   17266           82 :   resolve_bindings_result = false;
   17267           82 :   stree->n.tb->error = 1;
   17268              : }
   17269              : 
   17270              : 
   17271              : static bool
   17272        86900 : resolve_typebound_procedures (gfc_symbol* derived)
   17273              : {
   17274        86900 :   int op;
   17275        86900 :   gfc_symbol* super_type;
   17276              : 
   17277              :   /* Resolve the super-type first so that inherited bindings (including
   17278              :      user operators) are fully resolved before we look them up via
   17279              :      gfc_find_typebound_user_op.  This must happen even when 'derived'
   17280              :      has no direct type-bound bindings of its own.  */
   17281        86900 :   super_type = gfc_get_derived_super_type (derived);
   17282        86900 :   if (super_type)
   17283        13405 :     resolve_symbol (super_type);
   17284              : 
   17285        86900 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   17286              :     return true;
   17287              : 
   17288         4870 :   resolve_bindings_derived = derived;
   17289         4870 :   resolve_bindings_result = true;
   17290              : 
   17291         4870 :   containing_dt = derived;  /* Needed for checks of PDTs.  */
   17292         4870 :   if (derived->f2k_derived->tb_sym_root)
   17293         4870 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   17294              :                           &resolve_typebound_procedure);
   17295              : 
   17296         4870 :   if (derived->f2k_derived->tb_uop_root)
   17297           91 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   17298              :                           &resolve_typebound_user_op);
   17299         4870 :   containing_dt = NULL;
   17300              : 
   17301       141230 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   17302              :     {
   17303       136360 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   17304       136360 :       if (p && !resolve_typebound_intrinsic_op (derived,
   17305              :                                                 (gfc_intrinsic_op)op, p))
   17306            7 :         resolve_bindings_result = false;
   17307              :     }
   17308              : 
   17309         4870 :   return resolve_bindings_result;
   17310              : }
   17311              : 
   17312              : 
   17313              : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   17314              :    to give all identical derived types the same backend_decl.  */
   17315              : static void
   17316       178293 : add_dt_to_dt_list (gfc_symbol *derived)
   17317              : {
   17318       178293 :   if (!derived->dt_next)
   17319              :     {
   17320        82956 :       if (gfc_derived_types)
   17321              :         {
   17322        67917 :           derived->dt_next = gfc_derived_types->dt_next;
   17323        67917 :           gfc_derived_types->dt_next = derived;
   17324              :         }
   17325              :       else
   17326              :         {
   17327        15039 :           derived->dt_next = derived;
   17328              :         }
   17329        82956 :       gfc_derived_types = derived;
   17330              :     }
   17331       178293 : }
   17332              : 
   17333              : 
   17334              : /* Ensure that a derived-type is really not abstract, meaning that every
   17335              :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   17336              : 
   17337              : static bool
   17338         7086 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   17339              : {
   17340         7086 :   if (!st)
   17341              :     return true;
   17342              : 
   17343         2772 :   if (!ensure_not_abstract_walker (sub, st->left))
   17344              :     return false;
   17345         2772 :   if (!ensure_not_abstract_walker (sub, st->right))
   17346              :     return false;
   17347              : 
   17348         2771 :   if (st->n.tb && st->n.tb->deferred)
   17349              :     {
   17350         2019 :       gfc_symtree* overriding;
   17351         2019 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   17352         2019 :       if (!overriding)
   17353              :         return false;
   17354         2018 :       gcc_assert (overriding->n.tb);
   17355         2018 :       if (overriding->n.tb->deferred)
   17356              :         {
   17357            5 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   17358              :                      " %qs is DEFERRED and not overridden",
   17359              :                      sub->name, &sub->declared_at, st->name);
   17360            5 :           return false;
   17361              :         }
   17362              :     }
   17363              : 
   17364              :   return true;
   17365              : }
   17366              : 
   17367              : static bool
   17368         1394 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   17369              : {
   17370              :   /* The algorithm used here is to recursively travel up the ancestry of sub
   17371              :      and for each ancestor-type, check all bindings.  If any of them is
   17372              :      DEFERRED, look it up starting from sub and see if the found (overriding)
   17373              :      binding is not DEFERRED.
   17374              :      This is not the most efficient way to do this, but it should be ok and is
   17375              :      clearer than something sophisticated.  */
   17376              : 
   17377         1543 :   gcc_assert (ancestor && !sub->attr.abstract);
   17378              : 
   17379         1543 :   if (!ancestor->attr.abstract)
   17380              :     return true;
   17381              : 
   17382              :   /* Walk bindings of this ancestor.  */
   17383         1542 :   if (ancestor->f2k_derived)
   17384              :     {
   17385         1542 :       bool t;
   17386         1542 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   17387         1542 :       if (!t)
   17388              :         return false;
   17389              :     }
   17390              : 
   17391              :   /* Find next ancestor type and recurse on it.  */
   17392         1536 :   ancestor = gfc_get_derived_super_type (ancestor);
   17393         1536 :   if (ancestor)
   17394              :     return ensure_not_abstract (sub, ancestor);
   17395              : 
   17396              :   return true;
   17397              : }
   17398              : 
   17399              : 
   17400              : /* This check for typebound defined assignments is done recursively
   17401              :    since the order in which derived types are resolved is not always in
   17402              :    order of the declarations.  */
   17403              : 
   17404              : static void
   17405       182864 : check_defined_assignments (gfc_symbol *derived)
   17406              : {
   17407       182864 :   gfc_component *c;
   17408              : 
   17409       613096 :   for (c = derived->components; c; c = c->next)
   17410              :     {
   17411       432009 :       if (!gfc_bt_struct (c->ts.type)
   17412       104480 :           || c->attr.pointer
   17413        20698 :           || c->attr.proc_pointer_comp
   17414        20698 :           || c->attr.class_pointer
   17415        20692 :           || c->attr.proc_pointer)
   17416       411851 :         continue;
   17417              : 
   17418        20158 :       if (c->ts.u.derived->attr.defined_assign_comp
   17419        19923 :           || (c->ts.u.derived->f2k_derived
   17420        19341 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   17421              :         {
   17422         1753 :           derived->attr.defined_assign_comp = 1;
   17423         1753 :           return;
   17424              :         }
   17425              : 
   17426        18405 :       if (c->attr.allocatable)
   17427         6691 :         continue;
   17428              : 
   17429        11714 :       check_defined_assignments (c->ts.u.derived);
   17430        11714 :       if (c->ts.u.derived->attr.defined_assign_comp)
   17431              :         {
   17432           24 :           derived->attr.defined_assign_comp = 1;
   17433           24 :           return;
   17434              :         }
   17435              :     }
   17436              : }
   17437              : 
   17438              : 
   17439              : /* Resolve a single component of a derived type or structure.  */
   17440              : 
   17441              : static bool
   17442       412003 : resolve_component (gfc_component *c, gfc_symbol *sym)
   17443              : {
   17444       412003 :   gfc_symbol *super_type;
   17445       412003 :   symbol_attribute *attr;
   17446              : 
   17447       412003 :   if (c->attr.artificial)
   17448              :     return true;
   17449              : 
   17450              :   /* Do not allow vtype components to be resolved in nameless namespaces
   17451              :      such as block data because the procedure pointers will cause ICEs
   17452              :      and vtables are not needed in these contexts.  */
   17453       281220 :   if (sym->attr.vtype && sym->attr.use_assoc
   17454        49040 :       && sym->ns->proc_name == NULL)
   17455              :     return true;
   17456              : 
   17457              :   /* F2008, C442.  */
   17458       281211 :   if ((!sym->attr.is_class || c != sym->components)
   17459       281211 :       && c->attr.codimension
   17460          208 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   17461              :     {
   17462            4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   17463              :                  "deferred shape", c->name, &c->loc);
   17464            4 :       return false;
   17465              :     }
   17466              : 
   17467              :   /* F2008, C443.  */
   17468       281207 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   17469           85 :       && c->ts.u.derived->ts.is_iso_c)
   17470              :     {
   17471            1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17472              :                  "shall not be a coarray", c->name, &c->loc);
   17473            1 :       return false;
   17474              :     }
   17475              : 
   17476              :   /* F2008, C444.  */
   17477       281206 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   17478           28 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   17479           26 :           || c->attr.allocatable))
   17480              :     {
   17481            3 :       gfc_error ("Component %qs at %L with coarray component "
   17482              :                  "shall be a nonpointer, nonallocatable scalar",
   17483              :                  c->name, &c->loc);
   17484            3 :       return false;
   17485              :     }
   17486              : 
   17487              :   /* F2008, C448.  */
   17488       281203 :   if (c->ts.type == BT_CLASS)
   17489              :     {
   17490         6938 :       if (c->attr.class_ok && CLASS_DATA (c))
   17491              :         {
   17492         6930 :           attr = &(CLASS_DATA (c)->attr);
   17493              : 
   17494              :           /* Fix up contiguous attribute.  */
   17495         6930 :           if (c->attr.contiguous)
   17496           11 :             attr->contiguous = 1;
   17497              :         }
   17498              :       else
   17499              :         attr = NULL;
   17500              :     }
   17501              :   else
   17502       274265 :     attr = &c->attr;
   17503              : 
   17504       281206 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   17505              :     {
   17506            5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   17507              :                  "is not an array pointer", c->name, &c->loc);
   17508            5 :       return false;
   17509              :     }
   17510              : 
   17511              :   /* F2003, 15.2.1 - length has to be one.  */
   17512        41090 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   17513       281217 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   17514           19 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   17515           19 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   17516              :     {
   17517            1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   17518              :                  c->name, &c->loc);
   17519            1 :       return false;
   17520              :     }
   17521              : 
   17522        52306 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
   17523          307 :       && !sym->attr.pdt_type && !sym->attr.pdt_template
   17524       281205 :       && !(gfc_get_derived_super_type (sym)
   17525            0 :            && (gfc_get_derived_super_type (sym)->attr.pdt_type
   17526            0 :                ||  gfc_get_derived_super_type (sym)->attr.pdt_template)))
   17527              :     {
   17528            8 :       gfc_actual_arglist *type_spec_list;
   17529            8 :       if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
   17530              :                                 &type_spec_list)
   17531              :           != MATCH_YES)
   17532            0 :         return false;
   17533            8 :       gfc_free_actual_arglist (c->param_list);
   17534            8 :       c->param_list = type_spec_list;
   17535            8 :       if (!sym->attr.pdt_type)
   17536            8 :         sym->attr.pdt_comp = 1;
   17537              :     }
   17538       281189 :   else if (IS_PDT (c) && !sym->attr.pdt_type)
   17539           54 :     sym->attr.pdt_comp = 1;
   17540              : 
   17541       281197 :   if (c->attr.proc_pointer && c->ts.interface)
   17542              :     {
   17543        14886 :       gfc_symbol *ifc = c->ts.interface;
   17544              : 
   17545        14886 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   17546              :         {
   17547            6 :           c->tb->error = 1;
   17548            6 :           return false;
   17549              :         }
   17550              : 
   17551        14880 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   17552              :         {
   17553              :           /* Resolve interface and copy attributes.  */
   17554        14831 :           if (ifc->formal && !ifc->formal_ns)
   17555         2605 :             resolve_symbol (ifc);
   17556        14831 :           if (ifc->attr.intrinsic)
   17557            0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   17558              : 
   17559        14831 :           if (ifc->result)
   17560              :             {
   17561         7739 :               c->ts = ifc->result->ts;
   17562         7739 :               c->attr.allocatable = ifc->result->attr.allocatable;
   17563         7739 :               c->attr.pointer = ifc->result->attr.pointer;
   17564         7739 :               c->attr.dimension = ifc->result->attr.dimension;
   17565         7739 :               c->as = gfc_copy_array_spec (ifc->result->as);
   17566         7739 :               c->attr.class_ok = ifc->result->attr.class_ok;
   17567              :             }
   17568              :           else
   17569              :             {
   17570         7092 :               c->ts = ifc->ts;
   17571         7092 :               c->attr.allocatable = ifc->attr.allocatable;
   17572         7092 :               c->attr.pointer = ifc->attr.pointer;
   17573         7092 :               c->attr.dimension = ifc->attr.dimension;
   17574         7092 :               c->as = gfc_copy_array_spec (ifc->as);
   17575         7092 :               c->attr.class_ok = ifc->attr.class_ok;
   17576              :             }
   17577        14831 :           c->ts.interface = ifc;
   17578        14831 :           c->attr.function = ifc->attr.function;
   17579        14831 :           c->attr.subroutine = ifc->attr.subroutine;
   17580              : 
   17581        14831 :           c->attr.pure = ifc->attr.pure;
   17582        14831 :           c->attr.elemental = ifc->attr.elemental;
   17583        14831 :           c->attr.recursive = ifc->attr.recursive;
   17584        14831 :           c->attr.always_explicit = ifc->attr.always_explicit;
   17585        14831 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   17586              :           /* Copy char length.  */
   17587        14831 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   17588              :             {
   17589          491 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   17590          454 :               if (cl->length && !cl->resolved
   17591          601 :                   && !gfc_resolve_expr (cl->length))
   17592              :                 {
   17593            0 :                   c->tb->error = 1;
   17594            0 :                   return false;
   17595              :                 }
   17596          491 :               c->ts.u.cl = cl;
   17597              :             }
   17598              :         }
   17599              :     }
   17600       266311 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   17601              :     {
   17602              :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   17603              :          interface must be a subroutine.  */
   17604          116 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   17605              :     }
   17606              : 
   17607              :   /* Procedure pointer components: Check PASS arg.  */
   17608       281191 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   17609          560 :       && !sym->attr.vtype)
   17610              :     {
   17611           95 :       gfc_symbol* me_arg;
   17612              : 
   17613           95 :       if (c->tb->pass_arg)
   17614              :         {
   17615           20 :           gfc_formal_arglist* i;
   17616              : 
   17617              :           /* If an explicit passing argument name is given, walk the arg-list
   17618              :             and look for it.  */
   17619              : 
   17620           20 :           me_arg = NULL;
   17621           20 :           c->tb->pass_arg_num = 1;
   17622           34 :           for (i = c->ts.interface->formal; i; i = i->next)
   17623              :             {
   17624           33 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   17625              :                 {
   17626              :                   me_arg = i->sym;
   17627              :                   break;
   17628              :                 }
   17629           14 :               c->tb->pass_arg_num++;
   17630              :             }
   17631              : 
   17632           20 :           if (!me_arg)
   17633              :             {
   17634            1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   17635              :                          "at %L has no argument %qs", c->name,
   17636              :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   17637            1 :               c->tb->error = 1;
   17638            1 :               return false;
   17639              :             }
   17640              :         }
   17641              :       else
   17642              :         {
   17643              :           /* Otherwise, take the first one; there should in fact be at least
   17644              :             one.  */
   17645           75 :           c->tb->pass_arg_num = 1;
   17646           75 :           if (!c->ts.interface->formal)
   17647              :             {
   17648            3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   17649              :                          "must have at least one argument",
   17650              :                          c->name, &c->loc);
   17651            3 :               c->tb->error = 1;
   17652            3 :               return false;
   17653              :             }
   17654           72 :           me_arg = c->ts.interface->formal->sym;
   17655              :         }
   17656              : 
   17657              :       /* Now check that the argument-type matches.  */
   17658           72 :       gcc_assert (me_arg);
   17659           91 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   17660           90 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   17661           90 :           || (me_arg->ts.type == BT_CLASS
   17662           82 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   17663              :         {
   17664            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   17665              :                      " the derived type %qs", me_arg->name, c->name,
   17666              :                      me_arg->name, &c->loc, sym->name);
   17667            1 :           c->tb->error = 1;
   17668            1 :           return false;
   17669              :         }
   17670              : 
   17671              :       /* Check for F03:C453.  */
   17672           90 :       if (CLASS_DATA (me_arg)->attr.dimension)
   17673              :         {
   17674            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17675              :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   17676              :                      &c->loc);
   17677            1 :           c->tb->error = 1;
   17678            1 :           return false;
   17679              :         }
   17680              : 
   17681           89 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17682              :         {
   17683            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17684              :                      "may not have the POINTER attribute", me_arg->name,
   17685              :                      c->name, me_arg->name, &c->loc);
   17686            1 :           c->tb->error = 1;
   17687            1 :           return false;
   17688              :         }
   17689              : 
   17690           88 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17691              :         {
   17692            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17693              :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   17694              :                      me_arg->name, &c->loc);
   17695            1 :           c->tb->error = 1;
   17696            1 :           return false;
   17697              :         }
   17698              : 
   17699           87 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   17700              :         {
   17701            2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17702              :                      " at %L", c->name, &c->loc);
   17703            2 :           return false;
   17704              :         }
   17705              : 
   17706              :     }
   17707              : 
   17708              :   /* Check type-spec if this is not the parent-type component.  */
   17709       281181 :   if (((sym->attr.is_class
   17710        12514 :         && (!sym->components->ts.u.derived->attr.extension
   17711         2400 :             || c != CLASS_DATA (sym->components)))
   17712       270018 :        || (!sym->attr.is_class
   17713       268667 :            && (!sym->attr.extension || c != sym->components)))
   17714       273033 :       && !sym->attr.vtype
   17715       444907 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   17716              :     return false;
   17717              : 
   17718       281180 :   super_type = gfc_get_derived_super_type (sym);
   17719              : 
   17720              :   /* If this type is an extension, set the accessibility of the parent
   17721              :      component.  */
   17722       281180 :   if (super_type
   17723        25709 :       && ((sym->attr.is_class
   17724        12514 :            && c == CLASS_DATA (sym->components))
   17725        16963 :           || (!sym->attr.is_class && c == sym->components))
   17726        15543 :       && strcmp (super_type->name, c->name) == 0)
   17727         6635 :     c->attr.access = super_type->attr.access;
   17728              : 
   17729              :   /* If this type is an extension, see if this component has the same name
   17730              :      as an inherited type-bound procedure.  */
   17731        25709 :   if (super_type && !sym->attr.is_class
   17732        13195 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   17733              :     {
   17734            1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   17735              :                  " inherited type-bound procedure",
   17736              :                  c->name, sym->name, &c->loc);
   17737            1 :       return false;
   17738              :     }
   17739              : 
   17740       281179 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   17741         9429 :       && !c->ts.deferred)
   17742              :     {
   17743         7200 :       if (sym->attr.pdt_template || c->attr.pdt_string)
   17744          258 :         gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
   17745              : 
   17746         7200 :       if (c->ts.u.cl->length == NULL
   17747         7194 :           || !resolve_charlen(c->ts.u.cl)
   17748        14393 :           || !gfc_is_constant_expr (c->ts.u.cl->length))
   17749              :         {
   17750            9 :           gfc_error ("Character length of component %qs needs to "
   17751              :                      "be a constant specification expression at %L",
   17752              :                      c->name,
   17753            9 :                      c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   17754            9 :           return false;
   17755              :         }
   17756              : 
   17757         7191 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   17758              :         {
   17759            2 :          if (!c->ts.u.cl->length->error)
   17760              :            {
   17761            1 :              gfc_error ("Character length expression of component %qs at %L "
   17762              :                         "must be of INTEGER type, found %s",
   17763            1 :                         c->name, &c->ts.u.cl->length->where,
   17764              :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   17765            1 :              c->ts.u.cl->length->error = 1;
   17766              :            }
   17767            2 :          return false;
   17768              :        }
   17769              :     }
   17770              : 
   17771       281168 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   17772         2265 :       && !c->attr.pointer && !c->attr.allocatable)
   17773              :     {
   17774            1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   17775              :                  "length must be a POINTER or ALLOCATABLE",
   17776              :                  c->name, sym->name, &c->loc);
   17777            1 :       return false;
   17778              :     }
   17779              : 
   17780              :   /* Add the hidden deferred length field.  */
   17781       281167 :   if (c->ts.type == BT_CHARACTER
   17782         9929 :       && (c->ts.deferred || c->attr.pdt_string)
   17783         2439 :       && !c->attr.function
   17784         2403 :       && !sym->attr.is_class)
   17785              :     {
   17786         2256 :       char name[GFC_MAX_SYMBOL_LEN+9];
   17787         2256 :       gfc_component *strlen;
   17788         2256 :       sprintf (name, "_%s_length", c->name);
   17789         2256 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   17790         2256 :       if (strlen == NULL)
   17791              :         {
   17792          478 :           if (!gfc_add_component (sym, name, &strlen))
   17793            0 :             return false;
   17794          478 :           strlen->ts.type = BT_INTEGER;
   17795          478 :           strlen->ts.kind = gfc_charlen_int_kind;
   17796          478 :           strlen->attr.access = ACCESS_PRIVATE;
   17797          478 :           strlen->attr.artificial = 1;
   17798              :         }
   17799              :     }
   17800              : 
   17801       281167 :   if (c->ts.type == BT_DERIVED
   17802        52516 :       && sym->component_access != ACCESS_PRIVATE
   17803        51496 :       && gfc_check_symbol_access (sym)
   17804       100956 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   17805        50419 :       && !c->ts.u.derived->attr.use_assoc
   17806        27070 :       && !gfc_check_symbol_access (c->ts.u.derived)
   17807       281364 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   17808              :                           "PRIVATE type and cannot be a component of "
   17809              :                           "%qs, which is PUBLIC at %L", c->name,
   17810              :                           sym->name, &sym->declared_at))
   17811              :     return false;
   17812              : 
   17813       281166 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   17814              :     {
   17815            2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   17816              :                  "type %s", c->name, &c->loc, sym->name);
   17817            2 :       return false;
   17818              :     }
   17819              : 
   17820       281164 :   if (sym->attr.sequence)
   17821              :     {
   17822         2506 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   17823              :         {
   17824            0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   17825              :                      "not have the SEQUENCE attribute",
   17826              :                      c->ts.u.derived->name, &sym->declared_at);
   17827            0 :           return false;
   17828              :         }
   17829              :     }
   17830              : 
   17831       281164 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   17832            0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   17833       281164 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   17834         7272 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   17835            0 :     CLASS_DATA (c)->ts.u.derived
   17836            0 :                 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   17837              : 
   17838              :   /* If an allocatable component derived type is of the same type as
   17839              :      the enclosing derived type, we need a vtable generating so that
   17840              :      the __deallocate procedure is created.  */
   17841       281164 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   17842        59798 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   17843          399 :     gfc_find_vtab (&c->ts);
   17844              : 
   17845              :   /* Ensure that all the derived type components are put on the
   17846              :      derived type list; even in formal namespaces, where derived type
   17847              :      pointer components might not have been declared.  */
   17848       281164 :   if (c->ts.type == BT_DERIVED
   17849        52515 :       && c->ts.u.derived
   17850        52515 :       && c->ts.u.derived->components
   17851        49241 :       && c->attr.pointer
   17852        33726 :       && sym != c->ts.u.derived)
   17853         4283 :     add_dt_to_dt_list (c->ts.u.derived);
   17854              : 
   17855       281164 :   if (c->as && c->as->type != AS_DEFERRED
   17856         6380 :       && (c->attr.pointer || c->attr.allocatable))
   17857              :     return false;
   17858              : 
   17859       281150 :   if (!gfc_resolve_array_spec (c->as,
   17860       281150 :                                !(c->attr.pointer || c->attr.proc_pointer
   17861       228710 :                                  || c->attr.allocatable)))
   17862              :     return false;
   17863              : 
   17864       106377 :   if (c->initializer && !sym->attr.vtype
   17865        32521 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   17866       310563 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   17867              :     return false;
   17868              : 
   17869              :   return true;
   17870              : }
   17871              : 
   17872              : 
   17873              : /* Be nice about the locus for a structure expression - show the locus of the
   17874              :    first non-null sub-expression if we can.  */
   17875              : 
   17876              : static locus *
   17877            4 : cons_where (gfc_expr *struct_expr)
   17878              : {
   17879            4 :   gfc_constructor *cons;
   17880              : 
   17881            4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   17882              : 
   17883            4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   17884           12 :   for (; cons; cons = gfc_constructor_next (cons))
   17885              :     {
   17886            8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   17887            4 :         return &cons->expr->where;
   17888              :     }
   17889              : 
   17890            0 :   return &struct_expr->where;
   17891              : }
   17892              : 
   17893              : /* Resolve the components of a structure type. Much less work than derived
   17894              :    types.  */
   17895              : 
   17896              : static bool
   17897          913 : resolve_fl_struct (gfc_symbol *sym)
   17898              : {
   17899          913 :   gfc_component *c;
   17900          913 :   gfc_expr *init = NULL;
   17901          913 :   bool success;
   17902              : 
   17903              :   /* Make sure UNIONs do not have overlapping initializers.  */
   17904          913 :   if (sym->attr.flavor == FL_UNION)
   17905              :     {
   17906          498 :       for (c = sym->components; c; c = c->next)
   17907              :         {
   17908          331 :           if (init && c->initializer)
   17909              :             {
   17910            2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   17911              :                          cons_where (init), cons_where (c->initializer));
   17912            2 :               gfc_free_expr (c->initializer);
   17913            2 :               c->initializer = NULL;
   17914              :             }
   17915          291 :           if (init == NULL)
   17916          291 :             init = c->initializer;
   17917              :         }
   17918              :     }
   17919              : 
   17920          913 :   success = true;
   17921         2830 :   for (c = sym->components; c; c = c->next)
   17922         1917 :     if (!resolve_component (c, sym))
   17923            0 :       success = false;
   17924              : 
   17925          913 :   if (!success)
   17926              :     return false;
   17927              : 
   17928          913 :   if (sym->components)
   17929          862 :     add_dt_to_dt_list (sym);
   17930              : 
   17931              :   return true;
   17932              : }
   17933              : 
   17934              : /* Figure if the derived type is using itself directly in one of its components
   17935              :    or through referencing other derived types.  The information is required to
   17936              :    generate the __deallocate and __final type bound procedures to ensure
   17937              :    freeing larger hierarchies of derived types with allocatable objects.  */
   17938              : 
   17939              : static void
   17940       138918 : resolve_cyclic_derived_type (gfc_symbol *derived)
   17941              : {
   17942       138918 :   hash_set<gfc_symbol *> seen, to_examin;
   17943       138918 :   gfc_component *c;
   17944       138918 :   seen.add (derived);
   17945       138918 :   to_examin.add (derived);
   17946       465837 :   while (!to_examin.is_empty ())
   17947              :     {
   17948       190193 :       gfc_symbol *cand = *to_examin.begin ();
   17949       190193 :       to_examin.remove (cand);
   17950       512533 :       for (c = cand->components; c; c = c->next)
   17951       324532 :         if (c->ts.type == BT_DERIVED)
   17952              :           {
   17953        71707 :             if (c->ts.u.derived == derived)
   17954              :               {
   17955         1168 :                 derived->attr.recursive = 1;
   17956         2192 :                 return;
   17957              :               }
   17958        70539 :             else if (!seen.contains (c->ts.u.derived))
   17959              :               {
   17960        46691 :                 seen.add (c->ts.u.derived);
   17961        46691 :                 to_examin.add (c->ts.u.derived);
   17962              :               }
   17963              :           }
   17964       252825 :         else if (c->ts.type == BT_CLASS)
   17965              :           {
   17966         9588 :             if (!c->attr.class_ok)
   17967            7 :               continue;
   17968         9581 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   17969              :               {
   17970         1024 :                 derived->attr.recursive = 1;
   17971         1024 :                 return;
   17972              :               }
   17973         8557 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   17974              :               {
   17975         4796 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   17976         4796 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   17977              :               }
   17978              :           }
   17979              :     }
   17980       138918 : }
   17981              : 
   17982              : /* Resolve the components of a derived type. This does not have to wait until
   17983              :    resolution stage, but can be done as soon as the dt declaration has been
   17984              :    parsed.  */
   17985              : 
   17986              : static bool
   17987       171246 : resolve_fl_derived0 (gfc_symbol *sym)
   17988              : {
   17989       171246 :   gfc_symbol* super_type;
   17990       171246 :   gfc_component *c;
   17991       171246 :   gfc_formal_arglist *f;
   17992       171246 :   bool success;
   17993              : 
   17994       171246 :   if (sym->attr.unlimited_polymorphic)
   17995              :     return true;
   17996              : 
   17997       171246 :   super_type = gfc_get_derived_super_type (sym);
   17998              : 
   17999              :   /* F2008, C432.  */
   18000       171246 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   18001              :     {
   18002            2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   18003              :                  "parent type %qs shall also have one", sym->name,
   18004              :                  &sym->declared_at, super_type->name);
   18005            2 :       return false;
   18006              :     }
   18007              : 
   18008              :   /* Ensure the extended type gets resolved before we do.  */
   18009        17571 :   if (super_type && !resolve_fl_derived0 (super_type))
   18010              :     return false;
   18011              : 
   18012              :   /* An ABSTRACT type must be extensible.  */
   18013       171238 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   18014              :     {
   18015            2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   18016              :                  sym->name, &sym->declared_at);
   18017            2 :       return false;
   18018              :     }
   18019              : 
   18020              :   /* Resolving components below, may create vtabs for which the cyclic type
   18021              :      information needs to be present.  */
   18022       171236 :   if (!sym->attr.vtype)
   18023       138918 :     resolve_cyclic_derived_type (sym);
   18024              : 
   18025       171236 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   18026              :                            : sym->components;
   18027              : 
   18028              :   success = true;
   18029       581322 :   for ( ; c != NULL; c = c->next)
   18030       410086 :     if (!resolve_component (c, sym))
   18031           96 :       success = false;
   18032              : 
   18033       171236 :   if (!success)
   18034              :     return false;
   18035              : 
   18036              :   /* Now add the caf token field, where needed.  */
   18037       171150 :   if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
   18038         1000 :       && !sym->attr.vtype)
   18039              :     {
   18040         2238 :       for (c = sym->components; c; c = c->next)
   18041         1441 :         if (!c->attr.dimension && !c->attr.codimension
   18042          795 :             && (c->attr.allocatable || c->attr.pointer))
   18043              :           {
   18044          146 :             char name[GFC_MAX_SYMBOL_LEN+9];
   18045          146 :             gfc_component *token;
   18046          146 :             sprintf (name, "_caf_%s", c->name);
   18047          146 :             token = gfc_find_component (sym, name, true, true, NULL);
   18048          146 :             if (token == NULL)
   18049              :               {
   18050           82 :                 if (!gfc_add_component (sym, name, &token))
   18051            0 :                   return false;
   18052           82 :                 token->ts.type = BT_VOID;
   18053           82 :                 token->ts.kind = gfc_default_integer_kind;
   18054           82 :                 token->attr.access = ACCESS_PRIVATE;
   18055           82 :                 token->attr.artificial = 1;
   18056           82 :                 token->attr.caf_token = 1;
   18057              :               }
   18058          146 :             c->caf_token = token;
   18059              :           }
   18060              :     }
   18061              : 
   18062       171150 :   check_defined_assignments (sym);
   18063              : 
   18064       171150 :   if (!sym->attr.defined_assign_comp && super_type)
   18065        16564 :     sym->attr.defined_assign_comp
   18066        16564 :                         = super_type->attr.defined_assign_comp;
   18067              : 
   18068              :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   18069              :      all DEFERRED bindings are overridden.  */
   18070        17564 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   18071         1397 :       && !sym->attr.is_class
   18072         3147 :       && !ensure_not_abstract (sym, super_type))
   18073              :     return false;
   18074              : 
   18075              :   /* Check that there is a component for every PDT parameter.  */
   18076       171144 :   if (sym->attr.pdt_template)
   18077              :     {
   18078         2370 :       for (f = sym->formal; f; f = f->next)
   18079              :         {
   18080         1378 :           if (!f->sym)
   18081            1 :             continue;
   18082         1377 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   18083         1377 :           if (c == NULL)
   18084              :             {
   18085            9 :               gfc_error ("Parameterized type %qs does not have a component "
   18086              :                          "corresponding to parameter %qs at %L", sym->name,
   18087            9 :                          f->sym->name, &sym->declared_at);
   18088            9 :               break;
   18089              :             }
   18090              :         }
   18091              :     }
   18092              : 
   18093              :   /* Add derived type to the derived type list.  */
   18094       171144 :   add_dt_to_dt_list (sym);
   18095              : 
   18096       171144 :   return true;
   18097              : }
   18098              : 
   18099              : /* The following procedure does the full resolution of a derived type,
   18100              :    including resolution of all type-bound procedures (if present). In contrast
   18101              :    to 'resolve_fl_derived0' this can only be done after the module has been
   18102              :    parsed completely.  */
   18103              : 
   18104              : static bool
   18105        89008 : resolve_fl_derived (gfc_symbol *sym)
   18106              : {
   18107        89008 :   gfc_symbol *gen_dt = NULL;
   18108              : 
   18109        89008 :   if (sym->attr.unlimited_polymorphic)
   18110              :     return true;
   18111              : 
   18112        89008 :   if (!sym->attr.is_class)
   18113        76231 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   18114        57139 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   18115         2297 :       && (!gen_dt->generic->sym->attr.use_assoc
   18116         2154 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   18117        89184 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   18118              :                           "%qs at %L being the same name as derived "
   18119              :                           "type at %L", sym->name,
   18120              :                           gen_dt->generic->sym == sym
   18121           11 :                           ? gen_dt->generic->next->sym->name
   18122              :                           : gen_dt->generic->sym->name,
   18123              :                           gen_dt->generic->sym == sym
   18124           11 :                           ? &gen_dt->generic->next->sym->declared_at
   18125              :                           : &gen_dt->generic->sym->declared_at,
   18126              :                           &sym->declared_at))
   18127              :     return false;
   18128              : 
   18129        89004 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   18130              :     {
   18131           13 :       gfc_error ("Derived type %qs at %L has not been declared",
   18132              :                   sym->name, &sym->declared_at);
   18133           13 :       return false;
   18134              :     }
   18135              : 
   18136              :   /* Resolve the finalizer procedures.  */
   18137        88991 :   if (!gfc_resolve_finalizers (sym, NULL))
   18138              :     return false;
   18139              : 
   18140        88988 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   18141              :     {
   18142              :       /* Fix up incomplete CLASS symbols.  */
   18143        12777 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   18144        12777 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18145              : 
   18146        12777 :       if (data->ts.u.derived->attr.pdt_template)
   18147              :         {
   18148            6 :           match m;
   18149            6 :           m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
   18150              :                                     &data->param_list);
   18151            6 :           if (m != MATCH_YES
   18152            6 :               || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
   18153              :             {
   18154            0 :               gfc_error ("Failed to build PDT class component at %L",
   18155              :                          &sym->declared_at);
   18156            0 :               return false;
   18157              :             }
   18158            6 :           data = gfc_find_component (sym, "_data", true, true, NULL);
   18159            6 :           vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18160              :         }
   18161              : 
   18162              :       /* Nothing more to do for unlimited polymorphic entities.  */
   18163        12777 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   18164              :         {
   18165         2004 :           add_dt_to_dt_list (sym);
   18166         2004 :           return true;
   18167              :         }
   18168        10773 :       else if (vptr->ts.u.derived == NULL)
   18169              :         {
   18170         6365 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   18171         6365 :           gcc_assert (vtab);
   18172         6365 :           vptr->ts.u.derived = vtab->ts.u.derived;
   18173         6365 :           if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
   18174              :             return false;
   18175              :         }
   18176              :     }
   18177              : 
   18178        86984 :   if (!resolve_fl_derived0 (sym))
   18179              :     return false;
   18180              : 
   18181              :   /* Resolve the type-bound procedures.  */
   18182        86900 :   if (!resolve_typebound_procedures (sym))
   18183              :     return false;
   18184              : 
   18185              :   /* Generate module vtables subject to their accessibility and their not
   18186              :      being vtables or pdt templates. If this is not done class declarations
   18187              :      in external procedures wind up with their own version and so SELECT TYPE
   18188              :      fails because the vptrs do not have the same address.  */
   18189        86859 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   18190        86798 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   18191        65018 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   18192        21934 :       && sym->attr.access != ACCESS_PRIVATE
   18193        21901 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   18194              :     {
   18195        19694 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   18196        19694 :       gfc_set_sym_referenced (vtab);
   18197              :     }
   18198              : 
   18199              :   return true;
   18200              : }
   18201              : 
   18202              : 
   18203              : static bool
   18204          855 : resolve_fl_namelist (gfc_symbol *sym)
   18205              : {
   18206          855 :   gfc_namelist *nl;
   18207          855 :   gfc_symbol *nlsym;
   18208              : 
   18209         3024 :   for (nl = sym->namelist; nl; nl = nl->next)
   18210              :     {
   18211              :       /* Check again, the check in match only works if NAMELIST comes
   18212              :          after the decl.  */
   18213         2174 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   18214              :         {
   18215            1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   18216              :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   18217            1 :           return false;
   18218              :         }
   18219              : 
   18220          672 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   18221         2181 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18222              :                               "with assumed shape in namelist %qs at %L",
   18223              :                               nl->sym->name, sym->name, &sym->declared_at))
   18224              :         return false;
   18225              : 
   18226         2172 :       if (is_non_constant_shape_array (nl->sym)
   18227         2222 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18228              :                               "with nonconstant shape in namelist %qs at %L",
   18229           50 :                               nl->sym->name, sym->name, &sym->declared_at))
   18230              :         return false;
   18231              : 
   18232         2171 :       if (nl->sym->ts.type == BT_CHARACTER
   18233          593 :           && (nl->sym->ts.u.cl->length == NULL
   18234          554 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   18235         2253 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   18236              :                               "nonconstant character length in "
   18237           82 :                               "namelist %qs at %L", nl->sym->name,
   18238              :                               sym->name, &sym->declared_at))
   18239              :         return false;
   18240              : 
   18241              :     }
   18242              : 
   18243              :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   18244          850 :   if (gfc_check_symbol_access (sym))
   18245              :     {
   18246         3005 :       for (nl = sym->namelist; nl; nl = nl->next)
   18247              :         {
   18248         2168 :           if (!nl->sym->attr.use_assoc
   18249         4040 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   18250         4166 :               && !gfc_check_symbol_access (nl->sym))
   18251              :             {
   18252            2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   18253              :                          "cannot be member of PUBLIC namelist %qs at %L",
   18254            2 :                          nl->sym->name, sym->name, &sym->declared_at);
   18255            2 :               return false;
   18256              :             }
   18257              : 
   18258         2166 :           if (nl->sym->ts.type == BT_DERIVED
   18259          466 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   18260          464 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   18261              :            {
   18262            5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   18263              :                                   "namelist %qs at %L with ALLOCATABLE "
   18264              :                                   "or POINTER components", nl->sym->name,
   18265              :                                   sym->name, &sym->declared_at))
   18266              :                return false;
   18267              :              return true;
   18268              :            }
   18269              : 
   18270              :           /* Types with private components that came here by USE-association.  */
   18271         2161 :           if (nl->sym->ts.type == BT_DERIVED
   18272         2161 :               && derived_inaccessible (nl->sym->ts.u.derived))
   18273              :             {
   18274            6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   18275              :                          "components and cannot be member of namelist %qs at %L",
   18276              :                          nl->sym->name, sym->name, &sym->declared_at);
   18277            6 :               return false;
   18278              :             }
   18279              : 
   18280              :           /* Types with private components that are defined in the same module.  */
   18281         2155 :           if (nl->sym->ts.type == BT_DERIVED
   18282          910 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   18283         2433 :               && nl->sym->ts.u.derived->attr.private_comp)
   18284              :             {
   18285            0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   18286              :                          "cannot be a member of PUBLIC namelist %qs at %L",
   18287              :                          nl->sym->name, sym->name, &sym->declared_at);
   18288            0 :               return false;
   18289              :             }
   18290              :         }
   18291              :     }
   18292              : 
   18293              : 
   18294              :   /* 14.1.2 A module or internal procedure represent local entities
   18295              :      of the same type as a namelist member and so are not allowed.  */
   18296         2989 :   for (nl = sym->namelist; nl; nl = nl->next)
   18297              :     {
   18298         2155 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   18299         1596 :         continue;
   18300              : 
   18301          559 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   18302            7 :         if ((nl->sym == sym->ns->proc_name)
   18303            1 :                ||
   18304            1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   18305            6 :           continue;
   18306              : 
   18307          553 :       nlsym = NULL;
   18308          553 :       if (nl->sym->name)
   18309          553 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   18310          553 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   18311              :         {
   18312            3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   18313              :                      "attribute in %qs at %L", nlsym->name,
   18314              :                      &sym->declared_at);
   18315            3 :           return false;
   18316              :         }
   18317              :     }
   18318              : 
   18319              :   return true;
   18320              : }
   18321              : 
   18322              : 
   18323              : static bool
   18324       406956 : resolve_fl_parameter (gfc_symbol *sym)
   18325              : {
   18326              :   /* A parameter array's shape needs to be constant.  */
   18327       406956 :   if (sym->as != NULL
   18328       406956 :       && (sym->as->type == AS_DEFERRED
   18329         6256 :           || is_non_constant_shape_array (sym)))
   18330              :     {
   18331           17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   18332              :                  "or of deferred shape", sym->name, &sym->declared_at);
   18333           17 :       return false;
   18334              :     }
   18335              : 
   18336              :   /* Constraints on deferred type parameter.  */
   18337       406939 :   if (!deferred_requirements (sym))
   18338              :     return false;
   18339              : 
   18340              :   /* Make sure a parameter that has been implicitly typed still
   18341              :      matches the implicit type, since PARAMETER statements can precede
   18342              :      IMPLICIT statements.  */
   18343       406938 :   if (sym->attr.implicit_type
   18344       407651 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   18345          713 :                                                              sym->ns)))
   18346              :     {
   18347            0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   18348              :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   18349            0 :       return false;
   18350              :     }
   18351              : 
   18352              :   /* Make sure the types of derived parameters are consistent.  This
   18353              :      type checking is deferred until resolution because the type may
   18354              :      refer to a derived type from the host.  */
   18355       406938 :   if (sym->ts.type == BT_DERIVED
   18356       406938 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   18357              :     {
   18358            0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   18359            0 :                  &sym->value->where);
   18360            0 :       return false;
   18361              :     }
   18362              : 
   18363              :   /* F03:C509,C514.  */
   18364       406938 :   if (sym->ts.type == BT_CLASS)
   18365              :     {
   18366            0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   18367              :                  sym->name, &sym->declared_at);
   18368            0 :       return false;
   18369              :     }
   18370              : 
   18371              :   /* Some programmers can have a typo when using an implied-do loop to
   18372              :      initialize an array constant.  For example,
   18373              :        INTEGER I,J
   18374              :        INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]     ! OK
   18375              :        INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK, J undefined
   18376              :      This check catches the typo.  */
   18377       406938 :   if (sym->attr.dimension
   18378         6249 :       && sym->value && sym->value->expr_type == EXPR_ARRAY
   18379       413183 :       && !gfc_is_constant_expr (sym->value))
   18380              :     {
   18381              :       /* PR fortran/117070 argues a nonconstant proc pointer can appear in
   18382              :          the array constructor of a parameter.  This seems inconsistent with
   18383              :          the concept of a parameter. TODO: Needs an interpretation.  */
   18384           20 :       if (sym->value->ts.type == BT_DERIVED
   18385           18 :           && sym->value->ts.u.derived
   18386           18 :           && sym->value->ts.u.derived->attr.proc_pointer_comp)
   18387              :         return true;
   18388            2 :       gfc_error ("Expecting constant expression near %L", &sym->value->where);
   18389            2 :       return false;
   18390              :     }
   18391              : 
   18392              :   return true;
   18393              : }
   18394              : 
   18395              : 
   18396              : /* Called by resolve_symbol to check PDTs.  */
   18397              : 
   18398              : static void
   18399         1384 : resolve_pdt (gfc_symbol* sym)
   18400              : {
   18401         1384 :   gfc_symbol *derived = NULL;
   18402         1384 :   gfc_actual_arglist *param;
   18403         1384 :   gfc_component *c;
   18404         1384 :   bool const_len_exprs = true;
   18405         1384 :   bool assumed_len_exprs = false;
   18406         1384 :   symbol_attribute *attr;
   18407              : 
   18408         1384 :   if (sym->ts.type == BT_DERIVED)
   18409              :     {
   18410         1157 :       derived = sym->ts.u.derived;
   18411         1157 :       attr = &(sym->attr);
   18412              :     }
   18413          227 :   else if (sym->ts.type == BT_CLASS)
   18414              :     {
   18415          227 :       derived = CLASS_DATA (sym)->ts.u.derived;
   18416          227 :       attr = &(CLASS_DATA (sym)->attr);
   18417              :     }
   18418              :   else
   18419            0 :     gcc_unreachable ();
   18420              : 
   18421         1384 :   gcc_assert (derived->attr.pdt_type);
   18422              : 
   18423         3291 :   for (param = sym->param_list; param; param = param->next)
   18424              :     {
   18425         1907 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   18426         1907 :       gcc_assert (c);
   18427         1907 :       if (c->attr.pdt_kind)
   18428         1024 :         continue;
   18429              : 
   18430          614 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   18431          967 :           && c->attr.pdt_len)
   18432              :         const_len_exprs = false;
   18433          799 :       else if (param->spec_type == SPEC_ASSUMED)
   18434          291 :         assumed_len_exprs = true;
   18435              : 
   18436          883 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   18437           18 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   18438           16 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   18439            3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   18440              :                    "parameter %qs and requires either the POINTER "
   18441              :                    "or ALLOCATABLE attribute",
   18442              :                    sym->name, &sym->declared_at,
   18443              :                    param->name);
   18444              : 
   18445              :     }
   18446              : 
   18447         1384 :   if (!const_len_exprs
   18448           84 :       && (sym->ns->proc_name->attr.is_main_program
   18449           83 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   18450           82 :           || sym->attr.save != SAVE_NONE))
   18451            2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   18452              :                "SAVE attribute or be a variable declared in the "
   18453              :                "main program, a module or a submodule(F08/C513)",
   18454              :                sym->name, &sym->declared_at);
   18455              : 
   18456         1384 :   if (assumed_len_exprs && !(sym->attr.dummy
   18457            1 :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   18458            1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   18459              :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   18460              :                sym->name, &sym->declared_at);
   18461         1384 : }
   18462              : 
   18463              : 
   18464              : /* Resolve the symbol's array spec.  */
   18465              : 
   18466              : static bool
   18467      1736087 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   18468              : {
   18469      1736087 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   18470      1736087 :   gfc_current_ns = gfc_get_spec_ns (sym);
   18471              : 
   18472      1736087 :   bool saved_specification_expr = specification_expr;
   18473      1736087 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   18474      1736087 :   specification_expr = true;
   18475      1736087 :   specification_expr_symbol = sym;
   18476              : 
   18477      1736087 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   18478              : 
   18479      1736087 :   specification_expr = saved_specification_expr;
   18480      1736087 :   specification_expr_symbol = saved_specification_expr_symbol;
   18481      1736087 :   gfc_current_ns = orig_current_ns;
   18482              : 
   18483      1736087 :   return result;
   18484              : }
   18485              : 
   18486              : 
   18487              : /* Do anything necessary to resolve a symbol.  Right now, we just
   18488              :    assume that an otherwise unknown symbol is a variable.  This sort
   18489              :    of thing commonly happens for symbols in module.  */
   18490              : 
   18491              : static void
   18492      1892635 : resolve_symbol (gfc_symbol *sym)
   18493              : {
   18494      1892635 :   int check_constant, mp_flag;
   18495      1892635 :   gfc_symtree *symtree;
   18496      1892635 :   gfc_symtree *this_symtree;
   18497      1892635 :   gfc_namespace *ns;
   18498      1892635 :   gfc_component *c;
   18499      1892635 :   symbol_attribute class_attr;
   18500      1892635 :   gfc_array_spec *as;
   18501              : 
   18502      1892635 :   if (sym->resolve_symbol_called >= 1)
   18503       187954 :     return;
   18504      1805066 :   sym->resolve_symbol_called = 1;
   18505              : 
   18506              :   /* No symbol will ever have union type; only components can be unions.
   18507              :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   18508              :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   18509      1805066 :   gcc_assert (sym->ts.type != BT_UNION);
   18510              : 
   18511              :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   18512              :      yet unsupported for -fcoarray=lib.  */
   18513      1805066 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   18514          112 :       && sym->ts.u.derived && CLASS_DATA (sym)
   18515          112 :       && CLASS_DATA (sym)->attr.codimension
   18516           94 :       && CLASS_DATA (sym)->ts.u.derived
   18517           93 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   18518           90 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   18519              :     {
   18520            6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   18521              :                  "type coarrays at %L are unsupported", &sym->declared_at);
   18522            6 :       return;
   18523              :     }
   18524              : 
   18525      1805060 :   if (sym->attr.artificial)
   18526              :     return;
   18527              : 
   18528      1707380 :   if (sym->attr.unlimited_polymorphic)
   18529              :     return;
   18530              : 
   18531      1705922 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   18532              :     {
   18533            4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   18534              :                  "the OpenMP DEPEND clause", &sym->declared_at);
   18535            4 :       return;
   18536              :     }
   18537              : 
   18538      1705918 :   if (sym->attr.flavor == FL_UNKNOWN
   18539      1684606 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   18540       447568 :           && !sym->attr.generic && !sym->attr.external
   18541       181151 :           && sym->attr.if_source == IFSRC_UNKNOWN
   18542        81536 :           && sym->ts.type == BT_UNKNOWN))
   18543              :     {
   18544              :       /* A symbol in a common block might not have been resolved yet properly.
   18545              :          Do not try to find an interface with the same name.  */
   18546        94339 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   18547        21308 :           && !sym->attr.generic && !sym->attr.external
   18548        21257 :           && sym->attr.in_common)
   18549         2594 :         goto skip_interfaces;
   18550              : 
   18551              :     /* If we find that a flavorless symbol is an interface in one of the
   18552              :        parent namespaces, find its symtree in this namespace, free the
   18553              :        symbol and set the symtree to point to the interface symbol.  */
   18554       131094 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   18555              :         {
   18556        40058 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   18557        40058 :           if (symtree && (symtree->n.sym->generic ||
   18558          766 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   18559          676 :                            && sym->ns->construct_entities)))
   18560              :             {
   18561          717 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   18562              :                                                sym->name);
   18563          717 :               if (this_symtree->n.sym == sym)
   18564              :                 {
   18565          709 :                   symtree->n.sym->refs++;
   18566          709 :                   gfc_release_symbol (sym);
   18567          709 :                   this_symtree->n.sym = symtree->n.sym;
   18568          709 :                   return;
   18569              :                 }
   18570              :             }
   18571              :         }
   18572              : 
   18573        91036 : skip_interfaces:
   18574              :       /* Otherwise give it a flavor according to such attributes as
   18575              :          it has.  */
   18576        93630 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   18577        21127 :           && sym->attr.intrinsic == 0)
   18578        21123 :         sym->attr.flavor = FL_VARIABLE;
   18579        72507 :       else if (sym->attr.flavor == FL_UNKNOWN)
   18580              :         {
   18581           55 :           sym->attr.flavor = FL_PROCEDURE;
   18582           55 :           if (sym->attr.dimension)
   18583            0 :             sym->attr.function = 1;
   18584              :         }
   18585              :     }
   18586              : 
   18587      1705209 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   18588         2346 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   18589              : 
   18590         1517 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   18591      1706726 :       && !resolve_procedure_interface (sym))
   18592              :     return;
   18593              : 
   18594      1705198 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   18595          130 :       && (sym->attr.procedure || sym->attr.external))
   18596              :     {
   18597            0 :       if (sym->attr.external)
   18598            0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   18599              :                    "at %L", &sym->declared_at);
   18600              :       else
   18601            0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   18602              :                    "at %L", &sym->declared_at);
   18603              : 
   18604            0 :       return;
   18605              :     }
   18606              : 
   18607              :   /* Ensure that variables of derived or class type having a finalizer are
   18608              :      marked used even when the variable is not used anything else in the scope.
   18609              :      This fixes PR118730.  */
   18610       656230 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   18611       449673 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   18612      1754971 :       && gfc_may_be_finalized (sym->ts))
   18613         8618 :     gfc_set_sym_referenced (sym);
   18614              : 
   18615      1705198 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   18616              :     return;
   18617              : 
   18618      1704422 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   18619      1705185 :            && !resolve_fl_struct (sym))
   18620              :     return;
   18621              : 
   18622              :   /* Symbols that are module procedures with results (functions) have
   18623              :      the types and array specification copied for type checking in
   18624              :      procedures that call them, as well as for saving to a module
   18625              :      file.  These symbols can't stand the scrutiny that their results
   18626              :      can.  */
   18627      1705053 :   mp_flag = (sym->result != NULL && sym->result != sym);
   18628              : 
   18629              :   /* Make sure that the intrinsic is consistent with its internal
   18630              :      representation. This needs to be done before assigning a default
   18631              :      type to avoid spurious warnings.  */
   18632      1670531 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   18633      1741868 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   18634              :     return;
   18635              : 
   18636              :   /* Resolve associate names.  */
   18637      1705017 :   if (sym->assoc)
   18638         6865 :     resolve_assoc_var (sym, true);
   18639              : 
   18640              :   /* Assign default type to symbols that need one and don't have one.  */
   18641      1705017 :   if (sym->ts.type == BT_UNKNOWN)
   18642              :     {
   18643       408425 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   18644              :         {
   18645        11761 :           gfc_set_default_type (sym, 1, NULL);
   18646              :         }
   18647              : 
   18648       264928 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   18649        62224 :           && !sym->attr.function && !sym->attr.subroutine
   18650       410071 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   18651          595 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   18652              : 
   18653       408425 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18654              :         {
   18655              :           /* The specific case of an external procedure should emit an error
   18656              :              in the case that there is no implicit type.  */
   18657       103235 :           if (!mp_flag)
   18658              :             {
   18659        97124 :               if (!sym->attr.mixed_entry_master)
   18660        97016 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   18661              :             }
   18662              :           else
   18663              :             {
   18664              :               /* Result may be in another namespace.  */
   18665         6111 :               resolve_symbol (sym->result);
   18666              : 
   18667         6111 :               if (!sym->result->attr.proc_pointer)
   18668              :                 {
   18669         5932 :                   sym->ts = sym->result->ts;
   18670         5932 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   18671         5932 :                   sym->attr.dimension = sym->result->attr.dimension;
   18672         5932 :                   sym->attr.codimension = sym->result->attr.codimension;
   18673         5932 :                   sym->attr.pointer = sym->result->attr.pointer;
   18674         5932 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   18675         5932 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   18676              :                 }
   18677              :             }
   18678              :         }
   18679              :     }
   18680      1296592 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18681        31403 :     resolve_symbol_array_spec (sym->result, false);
   18682              : 
   18683              :   /* For a CLASS-valued function with a result variable, affirm that it has
   18684              :      been resolved also when looking at the symbol 'sym'.  */
   18685       439828 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   18686          720 :     sym->attr.class_ok = sym->result->attr.class_ok;
   18687              : 
   18688      1705017 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   18689        19611 :       && CLASS_DATA (sym))
   18690              :     {
   18691        19610 :       as = CLASS_DATA (sym)->as;
   18692        19610 :       class_attr = CLASS_DATA (sym)->attr;
   18693        19610 :       class_attr.pointer = class_attr.class_pointer;
   18694              :     }
   18695              :   else
   18696              :     {
   18697      1685407 :       class_attr = sym->attr;
   18698      1685407 :       as = sym->as;
   18699              :     }
   18700              : 
   18701              :   /* F2008, C530.  */
   18702      1705017 :   if (sym->attr.contiguous
   18703         7730 :       && !sym->attr.associate_var
   18704         7729 :       && (!class_attr.dimension
   18705         7726 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   18706          140 :               && !class_attr.pointer)))
   18707              :     {
   18708            7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   18709              :                  "array pointer or an assumed-shape or assumed-rank array",
   18710              :                  sym->name, &sym->declared_at);
   18711            7 :       return;
   18712              :     }
   18713              : 
   18714              :   /* Assumed size arrays and assumed shape arrays must be dummy
   18715              :      arguments.  Array-spec's of implied-shape should have been resolved to
   18716              :      AS_EXPLICIT already.  */
   18717              : 
   18718      1697424 :   if (as)
   18719              :     {
   18720              :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   18721              :          specification expression.  */
   18722       147846 :       if (as->type == AS_IMPLIED_SHAPE)
   18723              :         {
   18724              :           int i;
   18725            1 :           for (i=0; i<as->rank; i++)
   18726              :             {
   18727            1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   18728              :                 {
   18729            1 :                   gfc_error ("Bad specification for assumed size array at %L",
   18730              :                              &as->lower[i]->where);
   18731            1 :                   return;
   18732              :                 }
   18733              :             }
   18734            0 :           gcc_unreachable();
   18735              :         }
   18736              : 
   18737       147845 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   18738       114754 :            || as->type == AS_ASSUMED_SHAPE)
   18739        44839 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   18740            8 :           && !sym->attr.associate_var)
   18741              :         {
   18742            7 :           if (as->type == AS_ASSUMED_SIZE)
   18743            7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   18744              :                        &sym->declared_at);
   18745              :           else
   18746            0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   18747              :                        &sym->declared_at);
   18748            7 :           return;
   18749              :         }
   18750              :       /* TS 29113, C535a.  */
   18751       147838 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   18752           60 :           && !sym->attr.select_type_temporary
   18753           60 :           && !(cs_base && cs_base->current
   18754           45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   18755            3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   18756            0 :                         && cs_base->current->op == EXEC_BLOCK))))
   18757              :         {
   18758           18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   18759              :                      &sym->declared_at);
   18760           18 :           return;
   18761              :         }
   18762       147820 :       if (as->type == AS_ASSUMED_RANK
   18763        26269 :           && (sym->attr.codimension || sym->attr.value))
   18764              :         {
   18765            2 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   18766              :                      "CODIMENSION attribute", &sym->declared_at);
   18767            2 :           return;
   18768              :         }
   18769              :     }
   18770              : 
   18771              :   /* Make sure symbols with known intent or optional are really dummy
   18772              :      variable.  Because of ENTRY statement, this has to be deferred
   18773              :      until resolution time.  */
   18774              : 
   18775      1704982 :   if (!sym->attr.dummy
   18776      1231764 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   18777              :     {
   18778            2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   18779            2 :       return;
   18780              :     }
   18781              : 
   18782      1704980 :   if (sym->attr.value && !sym->attr.dummy)
   18783              :     {
   18784            2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   18785              :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   18786            2 :       return;
   18787              :     }
   18788              : 
   18789      1704978 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   18790              :     {
   18791          616 :       gfc_charlen *cl = sym->ts.u.cl;
   18792          616 :       if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   18793              :         {
   18794            2 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   18795              :                      "attribute must have constant length",
   18796              :                      sym->name, &sym->declared_at);
   18797            2 :           return;
   18798              :         }
   18799              : 
   18800          614 :       if (sym->ts.is_c_interop
   18801          381 :           && mpz_cmp_si (cl->length->value.integer, 1) != 0)
   18802              :         {
   18803            1 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   18804              :                      "with VALUE attribute must have length one",
   18805              :                      sym->name, &sym->declared_at);
   18806            1 :           return;
   18807              :         }
   18808              :     }
   18809              : 
   18810      1704975 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18811       124250 :       && sym->ts.u.derived->attr.generic)
   18812              :     {
   18813           20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   18814           20 :       if (!sym->ts.u.derived)
   18815              :         {
   18816            0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   18817              :                      "which has not been defined", sym->name,
   18818              :                      &sym->declared_at, sym->ts.u.derived->name);
   18819            0 :           sym->ts.type = BT_UNKNOWN;
   18820            0 :           return;
   18821              :         }
   18822              :     }
   18823              : 
   18824              :     /* Use the same constraints as TYPE(*), except for the type check
   18825              :        and that only scalars and assumed-size arrays are permitted.  */
   18826      1704975 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   18827              :       {
   18828        12960 :         if (!sym->attr.dummy)
   18829              :           {
   18830            1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18831              :                        "a dummy argument", sym->name, &sym->declared_at);
   18832            1 :             return;
   18833              :           }
   18834              : 
   18835        12959 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   18836            8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   18837            0 :             && sym->ts.type != BT_COMPLEX)
   18838              :           {
   18839            0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18840              :                        "of type TYPE(*) or of an numeric intrinsic type",
   18841              :                        sym->name, &sym->declared_at);
   18842            0 :             return;
   18843              :           }
   18844              : 
   18845        12959 :       if (sym->attr.allocatable || sym->attr.codimension
   18846        12957 :           || sym->attr.pointer || sym->attr.value)
   18847              :         {
   18848            4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18849              :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   18850              :                      "attribute", sym->name, &sym->declared_at);
   18851            4 :           return;
   18852              :         }
   18853              : 
   18854        12955 :       if (sym->attr.intent == INTENT_OUT)
   18855              :         {
   18856            0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18857              :                      "have the INTENT(OUT) attribute",
   18858              :                      sym->name, &sym->declared_at);
   18859            0 :           return;
   18860              :         }
   18861        12955 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   18862              :         {
   18863            1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   18864              :                      "either be a scalar or an assumed-size array",
   18865              :                      sym->name, &sym->declared_at);
   18866            1 :           return;
   18867              :         }
   18868              : 
   18869              :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   18870              :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   18871              :          packing.  */
   18872        12954 :       sym->ts.type = BT_ASSUMED;
   18873        12954 :       sym->as = gfc_get_array_spec ();
   18874        12954 :       sym->as->type = AS_ASSUMED_SIZE;
   18875        12954 :       sym->as->rank = 1;
   18876        12954 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   18877              :     }
   18878      1692015 :   else if (sym->ts.type == BT_ASSUMED)
   18879              :     {
   18880              :       /* TS 29113, C407a.  */
   18881        11006 :       if (!sym->attr.dummy)
   18882              :         {
   18883            7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   18884              :                      "for dummy variables", sym->name, &sym->declared_at);
   18885            7 :           return;
   18886              :         }
   18887        10999 :       if (sym->attr.allocatable || sym->attr.codimension
   18888        10995 :           || sym->attr.pointer || sym->attr.value)
   18889              :         {
   18890            8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18891              :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   18892              :                      sym->name, &sym->declared_at);
   18893            8 :           return;
   18894              :         }
   18895        10991 :       if (sym->attr.intent == INTENT_OUT)
   18896              :         {
   18897            2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18898              :                      "INTENT(OUT) attribute",
   18899              :                      sym->name, &sym->declared_at);
   18900            2 :           return;
   18901              :         }
   18902        10989 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   18903              :         {
   18904            3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   18905              :                      "explicit-shape array", sym->name, &sym->declared_at);
   18906            3 :           return;
   18907              :         }
   18908              :     }
   18909              : 
   18910              :   /* If the symbol is marked as bind(c), that it is declared at module level
   18911              :      scope and verify its type and kind.  Do not do the latter for symbols
   18912              :      that are implicitly typed because that is handled in
   18913              :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   18914              :      separately.  Also, anything that is use associated is not handled here
   18915              :      but instead is handled in the module it is declared in.  Finally, derived
   18916              :      type definitions are allowed to be BIND(C) since that only implies that
   18917              :      they're interoperable, and they are checked fully for interoperability
   18918              :      when a variable is declared of that type.  */
   18919      1704949 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   18920         7383 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   18921          567 :       && sym->attr.flavor != FL_DERIVED)
   18922              :     {
   18923          167 :       bool t = true;
   18924              : 
   18925              :       /* First, make sure the variable is declared at the
   18926              :          module-level scope (J3/04-007, Section 15.3).  */
   18927          167 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   18928            7 :           && !sym->attr.in_common)
   18929              :         {
   18930            6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   18931              :                      "is neither a COMMON block nor declared at the "
   18932              :                      "module level scope", sym->name, &(sym->declared_at));
   18933            6 :           t = false;
   18934              :         }
   18935          161 :       else if (sym->ts.type == BT_CHARACTER
   18936          161 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   18937            1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   18938            1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   18939              :         {
   18940            1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   18941            1 :                      sym->name, &sym->declared_at);
   18942            1 :           t = false;
   18943              :         }
   18944          160 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   18945              :         {
   18946            1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   18947              :         }
   18948          159 :       else if (sym->attr.implicit_type == 0)
   18949              :         {
   18950              :           /* If type() declaration, we need to verify that the components
   18951              :              of the given type are all C interoperable, etc.  */
   18952          157 :           if (sym->ts.type == BT_DERIVED &&
   18953           24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   18954              :             {
   18955              :               /* Make sure the user marked the derived type as BIND(C).  If
   18956              :                  not, call the verify routine.  This could print an error
   18957              :                  for the derived type more than once if multiple variables
   18958              :                  of that type are declared.  */
   18959           14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   18960            1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   18961          157 :               t = false;
   18962              :             }
   18963              : 
   18964              :           /* Verify the variable itself as C interoperable if it
   18965              :              is BIND(C).  It is not possible for this to succeed if
   18966              :              the verify_bind_c_derived_type failed, so don't have to handle
   18967              :              any error returned by verify_bind_c_derived_type.  */
   18968          157 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   18969          157 :                                  sym->common_block);
   18970              :         }
   18971              : 
   18972          165 :       if (!t)
   18973              :         {
   18974              :           /* clear the is_bind_c flag to prevent reporting errors more than
   18975              :              once if something failed.  */
   18976           10 :           sym->attr.is_bind_c = 0;
   18977           10 :           return;
   18978              :         }
   18979              :     }
   18980              : 
   18981              :   /* If a derived type symbol has reached this point, without its
   18982              :      type being declared, we have an error.  Notice that most
   18983              :      conditions that produce undefined derived types have already
   18984              :      been dealt with.  However, the likes of:
   18985              :      implicit type(t) (t) ..... call foo (t) will get us here if
   18986              :      the type is not declared in the scope of the implicit
   18987              :      statement. Change the type to BT_UNKNOWN, both because it is so
   18988              :      and to prevent an ICE.  */
   18989      1704939 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18990       124248 :       && sym->ts.u.derived->components == NULL
   18991         1139 :       && !sym->ts.u.derived->attr.zero_comp)
   18992              :     {
   18993            3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   18994              :                  "which has not been defined", sym->name,
   18995              :                   &sym->declared_at, sym->ts.u.derived->name);
   18996            3 :       sym->ts.type = BT_UNKNOWN;
   18997            3 :       return;
   18998              :     }
   18999              : 
   19000              :   /* Make sure that the derived type has been resolved and that the
   19001              :      derived type is visible in the symbol's namespace, if it is a
   19002              :      module function and is not PRIVATE.  */
   19003      1704936 :   if (sym->ts.type == BT_DERIVED
   19004       131381 :         && sym->ts.u.derived->attr.use_assoc
   19005       113752 :         && sym->ns->proc_name
   19006       113744 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   19007      1710867 :         && !resolve_fl_derived (sym->ts.u.derived))
   19008              :     return;
   19009              : 
   19010              :   /* Unless the derived-type declaration is use associated, Fortran 95
   19011              :      does not allow public entries of private derived types.
   19012              :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   19013              :      161 in 95-006r3.  */
   19014      1704936 :   if (sym->ts.type == BT_DERIVED
   19015       131381 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   19016         8057 :       && !sym->ts.u.derived->attr.use_assoc
   19017         2126 :       && gfc_check_symbol_access (sym)
   19018         1913 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   19019      1704950 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   19020              :                           "derived type %qs",
   19021           14 :                           (sym->attr.flavor == FL_PARAMETER)
   19022              :                           ? "parameter" : "variable",
   19023              :                           sym->name, &sym->declared_at,
   19024           14 :                           sym->ts.u.derived->name))
   19025              :     return;
   19026              : 
   19027              :   /* F2008, C1302.  */
   19028      1704929 :   if (sym->ts.type == BT_DERIVED
   19029       131374 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19030          154 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   19031       131343 :           || sym->ts.u.derived->attr.lock_comp)
   19032           44 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19033              :     {
   19034            4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   19035              :                  "type LOCK_TYPE must be a coarray", sym->name,
   19036              :                  &sym->declared_at);
   19037            4 :       return;
   19038              :     }
   19039              : 
   19040              :   /* TS18508, C702/C703.  */
   19041      1704925 :   if (sym->ts.type == BT_DERIVED
   19042       131370 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19043          153 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   19044       131353 :           || sym->ts.u.derived->attr.event_comp)
   19045           17 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19046              :     {
   19047            1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   19048              :                  "type EVENT_TYPE must be a coarray", sym->name,
   19049              :                  &sym->declared_at);
   19050            1 :       return;
   19051              :     }
   19052              : 
   19053              :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   19054              :      default initialization is defined (5.1.2.4.4).  */
   19055      1704924 :   if (sym->ts.type == BT_DERIVED
   19056       131369 :       && sym->attr.dummy
   19057        45019 :       && sym->attr.intent == INTENT_OUT
   19058         2356 :       && sym->as
   19059          381 :       && sym->as->type == AS_ASSUMED_SIZE)
   19060              :     {
   19061            1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   19062              :         {
   19063            1 :           if (c->initializer)
   19064              :             {
   19065            1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   19066              :                          "ASSUMED SIZE and so cannot have a default initializer",
   19067              :                          sym->name, &sym->declared_at);
   19068            1 :               return;
   19069              :             }
   19070              :         }
   19071              :     }
   19072              : 
   19073              :   /* F2008, C542.  */
   19074      1704923 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19075        45018 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   19076              :     {
   19077            0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   19078              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19079            0 :       return;
   19080              :     }
   19081              : 
   19082              :   /* TS18508.  */
   19083      1704923 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19084        45018 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   19085              :     {
   19086            0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   19087              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19088            0 :       return;
   19089              :     }
   19090              : 
   19091              :   /* F2008, C525.  */
   19092      1704923 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19093      1704823 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19094        19614 :              && sym->ts.u.derived && CLASS_DATA (sym)
   19095        19608 :              && CLASS_DATA (sym)->attr.coarray_comp))
   19096      1704823 :        || class_attr.codimension)
   19097         1796 :       && (sym->attr.result || sym->result == sym))
   19098              :     {
   19099            8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   19100              :                  "a coarray component", sym->name, &sym->declared_at);
   19101            8 :       return;
   19102              :     }
   19103              : 
   19104              :   /* F2008, C524.  */
   19105      1704915 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   19106          420 :       && sym->ts.u.derived->ts.is_iso_c)
   19107              :     {
   19108            3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   19109              :                  "shall not be a coarray", sym->name, &sym->declared_at);
   19110            3 :       return;
   19111              :     }
   19112              : 
   19113              :   /* F2008, C525.  */
   19114      1704912 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19115      1704815 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19116        19613 :             && sym->ts.u.derived && CLASS_DATA (sym)
   19117        19607 :             && CLASS_DATA (sym)->attr.coarray_comp))
   19118           97 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   19119           93 :           || class_attr.allocatable))
   19120              :     {
   19121            4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   19122              :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   19123              :                  sym->name, &sym->declared_at);
   19124            4 :       return;
   19125              :     }
   19126              : 
   19127              :   /* F2008, C526.  The function-result case was handled above.  */
   19128      1704908 :   if (class_attr.codimension
   19129         1688 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   19130          350 :            || sym->attr.select_type_temporary
   19131          274 :            || sym->attr.associate_var
   19132          256 :            || (sym->ns->save_all && !sym->attr.automatic)
   19133          256 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   19134          256 :            || sym->ns->proc_name->attr.is_main_program
   19135            5 :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   19136              :     {
   19137            4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   19138              :                  "nor a dummy argument", sym->name, &sym->declared_at);
   19139            4 :       return;
   19140              :     }
   19141              :   /* F2008, C528.  */
   19142      1704904 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   19143         1608 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   19144              :     {
   19145            6 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   19146              :                  "deferred shape without allocatable", sym->name,
   19147              :                  &sym->declared_at);
   19148            6 :       return;
   19149              :     }
   19150      1704898 :   else if (class_attr.codimension && class_attr.allocatable && as
   19151          614 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   19152              :     {
   19153            9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   19154              :                  "deferred shape", sym->name, &sym->declared_at);
   19155            9 :       return;
   19156              :     }
   19157              : 
   19158              :   /* F2008, C541.  */
   19159      1704889 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19160      1704796 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19161        19608 :             && sym->ts.u.derived && CLASS_DATA (sym)
   19162        19602 :             && CLASS_DATA (sym)->attr.coarray_comp))
   19163      1704796 :        || (class_attr.codimension && class_attr.allocatable))
   19164          698 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   19165              :     {
   19166            3 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   19167              :                  "allocatable coarray or have coarray components",
   19168              :                  sym->name, &sym->declared_at);
   19169            3 :       return;
   19170              :     }
   19171              : 
   19172      1704886 :   if (class_attr.codimension && sym->attr.dummy
   19173          469 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   19174              :     {
   19175            2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   19176              :                  "procedure %qs", sym->name, &sym->declared_at,
   19177              :                  sym->ns->proc_name->name);
   19178            2 :       return;
   19179              :     }
   19180              : 
   19181      1704884 :   if (sym->ts.type == BT_LOGICAL
   19182       112340 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   19183       112337 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   19184        31003 :               && sym->ns->proc_name->attr.is_bind_c)))
   19185              :     {
   19186              :       int i;
   19187          200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   19188          200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   19189              :           break;
   19190           16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   19191          181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   19192              :                               "%L with non-C_Bool kind in BIND(C) procedure "
   19193              :                               "%qs", sym->name, &sym->declared_at,
   19194           13 :                               sym->ns->proc_name->name))
   19195              :         return;
   19196          167 :       else if (!gfc_logical_kinds[i].c_bool
   19197          182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   19198              :                                    "%qs at %L with non-C_Bool kind in "
   19199              :                                    "BIND(C) procedure %qs", sym->name,
   19200              :                                    &sym->declared_at,
   19201           15 :                                    sym->attr.function ? sym->name
   19202           13 :                                    : sym->ns->proc_name->name))
   19203              :         return;
   19204              :     }
   19205              : 
   19206      1704881 :   switch (sym->attr.flavor)
   19207              :     {
   19208       656113 :     case FL_VARIABLE:
   19209       656113 :       if (!resolve_fl_variable (sym, mp_flag))
   19210              :         return;
   19211              :       break;
   19212              : 
   19213       483798 :     case FL_PROCEDURE:
   19214       483798 :       if (sym->formal && !sym->formal_ns)
   19215              :         {
   19216              :           /* Check that none of the arguments are a namelist.  */
   19217              :           gfc_formal_arglist *formal = sym->formal;
   19218              : 
   19219       105820 :           for (; formal; formal = formal->next)
   19220        71789 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   19221              :               {
   19222            1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   19223              :                            "subroutine or function at %L",
   19224              :                            formal->sym->name, &sym->declared_at);
   19225            1 :                 return;
   19226              :               }
   19227              :         }
   19228              : 
   19229       483797 :       if (!resolve_fl_procedure (sym, mp_flag))
   19230              :         return;
   19231              :       break;
   19232              : 
   19233          855 :     case FL_NAMELIST:
   19234          855 :       if (!resolve_fl_namelist (sym))
   19235              :         return;
   19236              :       break;
   19237              : 
   19238       406956 :     case FL_PARAMETER:
   19239       406956 :       if (!resolve_fl_parameter (sym))
   19240              :         return;
   19241              :       break;
   19242              : 
   19243              :     default:
   19244              :       break;
   19245              :     }
   19246              : 
   19247              :   /* Resolve array specifier. Check as well some constraints
   19248              :      on COMMON blocks.  */
   19249              : 
   19250      1704684 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   19251              : 
   19252      1704684 :   resolve_symbol_array_spec (sym, check_constant);
   19253              : 
   19254              :   /* Resolve formal namespaces.  */
   19255      1704684 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   19256       266923 :       && !sym->attr.contained && !sym->attr.intrinsic)
   19257       237587 :     gfc_resolve (sym->formal_ns);
   19258              : 
   19259              :   /* Make sure the formal namespace is present.  */
   19260      1704684 :   if (sym->formal && !sym->formal_ns)
   19261              :     {
   19262              :       gfc_formal_arglist *formal = sym->formal;
   19263        34482 :       while (formal && !formal->sym)
   19264           11 :         formal = formal->next;
   19265              : 
   19266        34471 :       if (formal)
   19267              :         {
   19268        34460 :           sym->formal_ns = formal->sym->ns;
   19269        34460 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   19270        26142 :             sym->formal_ns->refs++;
   19271              :         }
   19272              :     }
   19273              : 
   19274              :   /* Check threadprivate restrictions.  */
   19275      1704684 :   if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
   19276          384 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   19277           33 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19278           32 :       && sym->module == NULL
   19279           17 :       && (sym->ns->proc_name == NULL
   19280           17 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19281            4 :               && !sym->ns->proc_name->attr.is_main_program)))
   19282              :     {
   19283            2 :       if (sym->attr.threadprivate)
   19284            1 :         gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   19285              :       else
   19286            1 :         gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
   19287              :                    "attribute", sym->name, &sym->declared_at);
   19288              :     }
   19289              : 
   19290      1704684 :   if (sym->attr.omp_groupprivate && sym->value)
   19291            2 :     gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
   19292              :                "initializer", sym->name, &sym->declared_at);
   19293              : 
   19294              :   /* Check omp declare target restrictions.  */
   19295      1704684 :   if ((sym->attr.omp_declare_target
   19296      1703267 :        || sym->attr.omp_declare_target_link
   19297      1703219 :        || sym->attr.omp_declare_target_local)
   19298         1505 :       && !sym->attr.omp_groupprivate  /* already warned.  */
   19299         1458 :       && sym->attr.flavor == FL_VARIABLE
   19300          616 :       && !sym->attr.save
   19301          199 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19302          199 :       && (!sym->attr.in_common
   19303          186 :           && sym->module == NULL
   19304           96 :           && (sym->ns->proc_name == NULL
   19305           96 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19306            6 :                   && !sym->ns->proc_name->attr.is_main_program))))
   19307            4 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   19308              :                sym->name, &sym->declared_at);
   19309              : 
   19310              :   /* If we have come this far we can apply default-initializers, as
   19311              :      described in 14.7.5, to those variables that have not already
   19312              :      been assigned one.  */
   19313      1704684 :   if (sym->ts.type == BT_DERIVED
   19314       131339 :       && !sym->value
   19315       106163 :       && !sym->attr.allocatable
   19316       103165 :       && !sym->attr.alloc_comp)
   19317              :     {
   19318       103107 :       symbol_attribute *a = &sym->attr;
   19319              : 
   19320       103107 :       if ((!a->save && !a->dummy && !a->pointer
   19321        56760 :            && !a->in_common && !a->use_assoc
   19322        10560 :            && a->referenced
   19323         8287 :            && !((a->function || a->result)
   19324         1692 :                 && (!a->dimension
   19325          160 :                     || sym->ts.u.derived->attr.alloc_comp
   19326           95 :                     || sym->ts.u.derived->attr.pointer_comp))
   19327         6676 :            && !(a->function && sym != sym->result))
   19328        96451 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   19329         1528 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   19330         8085 :         apply_default_init (sym);
   19331        95022 :       else if (a->function && !a->pointer && !a->allocatable
   19332        20783 :                && !a->use_assoc && !a->used_in_submodule && sym->result)
   19333              :         /* Default initialization for function results.  */
   19334         2730 :         apply_default_init (sym->result);
   19335        92292 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   19336        11817 :                && (sym->ts.u.derived->attr.alloc_comp
   19337        11270 :                    || sym->ts.u.derived->attr.pointer_comp))
   19338              :         /* Mark the result symbol to be referenced, when it has allocatable
   19339              :            components.  */
   19340          606 :         sym->result->attr.referenced = 1;
   19341              :     }
   19342              : 
   19343      1704684 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   19344        19109 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   19345         1226 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   19346         1151 :       && !CLASS_DATA (sym)->attr.class_pointer
   19347         1125 :       && !CLASS_DATA (sym)->attr.allocatable)
   19348          853 :     apply_default_init (sym);
   19349              : 
   19350              :   /* If this symbol has a type-spec, check it.  */
   19351      1704684 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   19352       641725 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   19353      1385003 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   19354              :       return;
   19355              : 
   19356      1704681 :   if (sym->param_list)
   19357         1384 :     resolve_pdt (sym);
   19358              : }
   19359              : 
   19360              : 
   19361         4015 : void gfc_resolve_symbol (gfc_symbol *sym)
   19362              : {
   19363         4015 :   resolve_symbol (sym);
   19364         4015 :   return;
   19365              : }
   19366              : 
   19367              : 
   19368              : /************* Resolve DATA statements *************/
   19369              : 
   19370              : static struct
   19371              : {
   19372              :   gfc_data_value *vnode;
   19373              :   mpz_t left;
   19374              : }
   19375              : values;
   19376              : 
   19377              : 
   19378              : /* Advance the values structure to point to the next value in the data list.  */
   19379              : 
   19380              : static bool
   19381        10892 : next_data_value (void)
   19382              : {
   19383        16660 :   while (mpz_cmp_ui (values.left, 0) == 0)
   19384              :     {
   19385              : 
   19386         8198 :       if (values.vnode->next == NULL)
   19387              :         return false;
   19388              : 
   19389         5768 :       values.vnode = values.vnode->next;
   19390         5768 :       mpz_set (values.left, values.vnode->repeat);
   19391              :     }
   19392              : 
   19393              :   return true;
   19394              : }
   19395              : 
   19396              : 
   19397              : static bool
   19398         3557 : check_data_variable (gfc_data_variable *var, locus *where)
   19399              : {
   19400         3557 :   gfc_expr *e;
   19401         3557 :   mpz_t size;
   19402         3557 :   mpz_t offset;
   19403         3557 :   bool t;
   19404         3557 :   ar_type mark = AR_UNKNOWN;
   19405         3557 :   int i;
   19406         3557 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   19407         3557 :   int vector_offset[GFC_MAX_DIMENSIONS];
   19408         3557 :   gfc_ref *ref;
   19409         3557 :   gfc_array_ref *ar;
   19410         3557 :   gfc_symbol *sym;
   19411         3557 :   int has_pointer;
   19412              : 
   19413         3557 :   if (!gfc_resolve_expr (var->expr))
   19414              :     return false;
   19415              : 
   19416         3557 :   ar = NULL;
   19417         3557 :   e = var->expr;
   19418              : 
   19419         3557 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   19420            0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   19421            0 :     e = e->value.function.actual->expr;
   19422              : 
   19423         3557 :   if (e->expr_type != EXPR_VARIABLE)
   19424              :     {
   19425            0 :       gfc_error ("Expecting definable entity near %L", where);
   19426            0 :       return false;
   19427              :     }
   19428              : 
   19429         3557 :   sym = e->symtree->n.sym;
   19430              : 
   19431         3557 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   19432              :     {
   19433            2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   19434              :                  sym->name, &sym->declared_at);
   19435            2 :       return false;
   19436              :     }
   19437              : 
   19438         3555 :   if (e->ref == NULL && sym->as)
   19439              :     {
   19440            1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   19441              :                  " declaration", sym->name, where);
   19442            1 :       return false;
   19443              :     }
   19444              : 
   19445         3554 :   if (gfc_is_coindexed (e))
   19446              :     {
   19447            7 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   19448              :                  where);
   19449            7 :       return false;
   19450              :     }
   19451              : 
   19452         3547 :   has_pointer = sym->attr.pointer;
   19453              : 
   19454         5988 :   for (ref = e->ref; ref; ref = ref->next)
   19455              :     {
   19456         2445 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   19457              :         has_pointer = 1;
   19458              : 
   19459         2419 :       if (has_pointer)
   19460              :         {
   19461           29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   19462              :             {
   19463            1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   19464              :                          "be a full array", sym->name, where);
   19465            1 :               return false;
   19466              :             }
   19467              : 
   19468           28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   19469              :             {
   19470            1 :               gfc_error ("DATA object near %L has the pointer attribute "
   19471              :                          "and the corresponding DATA value is not a valid "
   19472              :                          "initial-data-target", where);
   19473            1 :               return false;
   19474              :             }
   19475              :         }
   19476              : 
   19477         2443 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   19478              :         {
   19479            1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   19480              :                      "attribute", ref->u.c.component->name, &e->where);
   19481            1 :           return false;
   19482              :         }
   19483              : 
   19484              :       /* Reject substrings of strings of non-constant length.  */
   19485         2442 :       if (ref->type == REF_SUBSTRING
   19486           73 :           && ref->u.ss.length
   19487           73 :           && ref->u.ss.length->length
   19488         2515 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   19489            1 :         goto bad_charlen;
   19490              :     }
   19491              : 
   19492              :   /* Reject strings with deferred length or non-constant length.  */
   19493         3543 :   if (e->ts.type == BT_CHARACTER
   19494         3543 :       && (e->ts.deferred
   19495          374 :           || (e->ts.u.cl->length
   19496          323 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   19497            5 :     goto bad_charlen;
   19498              : 
   19499         3538 :   mpz_init_set_si (offset, 0);
   19500              : 
   19501         3538 :   if (e->rank == 0 || has_pointer)
   19502              :     {
   19503         2691 :       mpz_init_set_ui (size, 1);
   19504         2691 :       ref = NULL;
   19505              :     }
   19506              :   else
   19507              :     {
   19508          847 :       ref = e->ref;
   19509              : 
   19510              :       /* Find the array section reference.  */
   19511         1030 :       for (ref = e->ref; ref; ref = ref->next)
   19512              :         {
   19513         1030 :           if (ref->type != REF_ARRAY)
   19514           92 :             continue;
   19515          938 :           if (ref->u.ar.type == AR_ELEMENT)
   19516           91 :             continue;
   19517              :           break;
   19518              :         }
   19519          847 :       gcc_assert (ref);
   19520              : 
   19521              :       /* Set marks according to the reference pattern.  */
   19522          847 :       switch (ref->u.ar.type)
   19523              :         {
   19524              :         case AR_FULL:
   19525              :           mark = AR_FULL;
   19526              :           break;
   19527              : 
   19528          151 :         case AR_SECTION:
   19529          151 :           ar = &ref->u.ar;
   19530              :           /* Get the start position of array section.  */
   19531          151 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   19532          151 :           mark = AR_SECTION;
   19533          151 :           break;
   19534              : 
   19535            0 :         default:
   19536            0 :           gcc_unreachable ();
   19537              :         }
   19538              : 
   19539          847 :       if (!gfc_array_size (e, &size))
   19540              :         {
   19541            1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   19542              :                      where);
   19543            1 :           mpz_clear (offset);
   19544            1 :           return false;
   19545              :         }
   19546              :     }
   19547              : 
   19548         3537 :   t = true;
   19549              : 
   19550        11937 :   while (mpz_cmp_ui (size, 0) > 0)
   19551              :     {
   19552         8463 :       if (!next_data_value ())
   19553              :         {
   19554            1 :           gfc_error ("DATA statement at %L has more variables than values",
   19555              :                      where);
   19556            1 :           t = false;
   19557            1 :           break;
   19558              :         }
   19559              : 
   19560         8462 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   19561         8462 :       if (!t)
   19562              :         break;
   19563              : 
   19564              :       /* If we have more than one element left in the repeat count,
   19565              :          and we have more than one element left in the target variable,
   19566              :          then create a range assignment.  */
   19567              :       /* FIXME: Only done for full arrays for now, since array sections
   19568              :          seem tricky.  */
   19569         8443 :       if (mark == AR_FULL && ref && ref->next == NULL
   19570         5364 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   19571              :         {
   19572          137 :           mpz_t range;
   19573              : 
   19574          137 :           if (mpz_cmp (size, values.left) >= 0)
   19575              :             {
   19576          126 :               mpz_init_set (range, values.left);
   19577          126 :               mpz_sub (size, size, values.left);
   19578          126 :               mpz_set_ui (values.left, 0);
   19579              :             }
   19580              :           else
   19581              :             {
   19582           11 :               mpz_init_set (range, size);
   19583           11 :               mpz_sub (values.left, values.left, size);
   19584           11 :               mpz_set_ui (size, 0);
   19585              :             }
   19586              : 
   19587          137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19588              :                                      offset, &range);
   19589              : 
   19590          137 :           mpz_add (offset, offset, range);
   19591          137 :           mpz_clear (range);
   19592              : 
   19593          137 :           if (!t)
   19594              :             break;
   19595          129 :         }
   19596              : 
   19597              :       /* Assign initial value to symbol.  */
   19598              :       else
   19599              :         {
   19600         8306 :           mpz_sub_ui (values.left, values.left, 1);
   19601         8306 :           mpz_sub_ui (size, size, 1);
   19602              : 
   19603         8306 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19604              :                                      offset, NULL);
   19605         8306 :           if (!t)
   19606              :             break;
   19607              : 
   19608         8271 :           if (mark == AR_FULL)
   19609         5259 :             mpz_add_ui (offset, offset, 1);
   19610              : 
   19611              :           /* Modify the array section indexes and recalculate the offset
   19612              :              for next element.  */
   19613         3012 :           else if (mark == AR_SECTION)
   19614          366 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   19615              :         }
   19616              :     }
   19617              : 
   19618         3537 :   if (mark == AR_SECTION)
   19619              :     {
   19620          344 :       for (i = 0; i < ar->dimen; i++)
   19621          194 :         mpz_clear (section_index[i]);
   19622              :     }
   19623              : 
   19624         3537 :   mpz_clear (size);
   19625         3537 :   mpz_clear (offset);
   19626              : 
   19627         3537 :   return t;
   19628              : 
   19629            6 : bad_charlen:
   19630            6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   19631              :              &e->where);
   19632            6 :   return false;
   19633              : }
   19634              : 
   19635              : 
   19636              : static bool traverse_data_var (gfc_data_variable *, locus *);
   19637              : 
   19638              : /* Iterate over a list of elements in a DATA statement.  */
   19639              : 
   19640              : static bool
   19641          237 : traverse_data_list (gfc_data_variable *var, locus *where)
   19642              : {
   19643          237 :   mpz_t trip;
   19644          237 :   iterator_stack frame;
   19645          237 :   gfc_expr *e, *start, *end, *step;
   19646          237 :   bool retval = true;
   19647              : 
   19648          237 :   mpz_init (frame.value);
   19649          237 :   mpz_init (trip);
   19650              : 
   19651          237 :   start = gfc_copy_expr (var->iter.start);
   19652          237 :   end = gfc_copy_expr (var->iter.end);
   19653          237 :   step = gfc_copy_expr (var->iter.step);
   19654              : 
   19655          237 :   if (!gfc_simplify_expr (start, 1)
   19656          237 :       || start->expr_type != EXPR_CONSTANT)
   19657              :     {
   19658            0 :       gfc_error ("start of implied-do loop at %L could not be "
   19659              :                  "simplified to a constant value", &start->where);
   19660            0 :       retval = false;
   19661            0 :       goto cleanup;
   19662              :     }
   19663          237 :   if (!gfc_simplify_expr (end, 1)
   19664          237 :       || end->expr_type != EXPR_CONSTANT)
   19665              :     {
   19666            0 :       gfc_error ("end of implied-do loop at %L could not be "
   19667              :                  "simplified to a constant value", &end->where);
   19668            0 :       retval = false;
   19669            0 :       goto cleanup;
   19670              :     }
   19671          237 :   if (!gfc_simplify_expr (step, 1)
   19672          237 :       || step->expr_type != EXPR_CONSTANT)
   19673              :     {
   19674            0 :       gfc_error ("step of implied-do loop at %L could not be "
   19675              :                  "simplified to a constant value", &step->where);
   19676            0 :       retval = false;
   19677            0 :       goto cleanup;
   19678              :     }
   19679          237 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   19680              :     {
   19681            1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   19682              :                  &step->where);
   19683            1 :       retval = false;
   19684            1 :       goto cleanup;
   19685              :     }
   19686              : 
   19687          236 :   mpz_set (trip, end->value.integer);
   19688          236 :   mpz_sub (trip, trip, start->value.integer);
   19689          236 :   mpz_add (trip, trip, step->value.integer);
   19690              : 
   19691          236 :   mpz_div (trip, trip, step->value.integer);
   19692              : 
   19693          236 :   mpz_set (frame.value, start->value.integer);
   19694              : 
   19695          236 :   frame.prev = iter_stack;
   19696          236 :   frame.variable = var->iter.var->symtree;
   19697          236 :   iter_stack = &frame;
   19698              : 
   19699         1127 :   while (mpz_cmp_ui (trip, 0) > 0)
   19700              :     {
   19701          905 :       if (!traverse_data_var (var->list, where))
   19702              :         {
   19703           14 :           retval = false;
   19704           14 :           goto cleanup;
   19705              :         }
   19706              : 
   19707          891 :       e = gfc_copy_expr (var->expr);
   19708          891 :       if (!gfc_simplify_expr (e, 1))
   19709              :         {
   19710            0 :           gfc_free_expr (e);
   19711            0 :           retval = false;
   19712            0 :           goto cleanup;
   19713              :         }
   19714              : 
   19715          891 :       mpz_add (frame.value, frame.value, step->value.integer);
   19716              : 
   19717          891 :       mpz_sub_ui (trip, trip, 1);
   19718              :     }
   19719              : 
   19720          222 : cleanup:
   19721          237 :   mpz_clear (frame.value);
   19722          237 :   mpz_clear (trip);
   19723              : 
   19724          237 :   gfc_free_expr (start);
   19725          237 :   gfc_free_expr (end);
   19726          237 :   gfc_free_expr (step);
   19727              : 
   19728          237 :   iter_stack = frame.prev;
   19729          237 :   return retval;
   19730              : }
   19731              : 
   19732              : 
   19733              : /* Type resolve variables in the variable list of a DATA statement.  */
   19734              : 
   19735              : static bool
   19736         3418 : traverse_data_var (gfc_data_variable *var, locus *where)
   19737              : {
   19738         3418 :   bool t;
   19739              : 
   19740         7114 :   for (; var; var = var->next)
   19741              :     {
   19742         3794 :       if (var->expr == NULL)
   19743          237 :         t = traverse_data_list (var, where);
   19744              :       else
   19745         3557 :         t = check_data_variable (var, where);
   19746              : 
   19747         3794 :       if (!t)
   19748              :         return false;
   19749              :     }
   19750              : 
   19751              :   return true;
   19752              : }
   19753              : 
   19754              : 
   19755              : /* Resolve the expressions and iterators associated with a data statement.
   19756              :    This is separate from the assignment checking because data lists should
   19757              :    only be resolved once.  */
   19758              : 
   19759              : static bool
   19760         2668 : resolve_data_variables (gfc_data_variable *d)
   19761              : {
   19762         5707 :   for (; d; d = d->next)
   19763              :     {
   19764         3044 :       if (d->list == NULL)
   19765              :         {
   19766         2891 :           if (!gfc_resolve_expr (d->expr))
   19767              :             return false;
   19768              :         }
   19769              :       else
   19770              :         {
   19771          153 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   19772              :             return false;
   19773              : 
   19774          150 :           if (!resolve_data_variables (d->list))
   19775              :             return false;
   19776              :         }
   19777              :     }
   19778              : 
   19779              :   return true;
   19780              : }
   19781              : 
   19782              : 
   19783              : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   19784              :    the value list into static variables, and then recursively traversing the
   19785              :    variables list, expanding iterators and such.  */
   19786              : 
   19787              : static void
   19788         2518 : resolve_data (gfc_data *d)
   19789              : {
   19790              : 
   19791         2518 :   if (!resolve_data_variables (d->var))
   19792              :     return;
   19793              : 
   19794         2513 :   values.vnode = d->value;
   19795         2513 :   if (d->value == NULL)
   19796            0 :     mpz_set_ui (values.left, 0);
   19797              :   else
   19798         2513 :     mpz_set (values.left, d->value->repeat);
   19799              : 
   19800         2513 :   if (!traverse_data_var (d->var, &d->where))
   19801              :     return;
   19802              : 
   19803              :   /* At this point, we better not have any values left.  */
   19804              : 
   19805         2429 :   if (next_data_value ())
   19806            0 :     gfc_error ("DATA statement at %L has more values than variables",
   19807              :                &d->where);
   19808              : }
   19809              : 
   19810              : 
   19811              : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   19812              :    accessed by host or use association, is a dummy argument to a pure function,
   19813              :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   19814              :    is storage associated with any such variable, shall not be used in the
   19815              :    following contexts: (clients of this function).  */
   19816              : 
   19817              : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   19818              :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   19819              :    problem.  */
   19820              : bool
   19821        55993 : gfc_impure_variable (gfc_symbol *sym)
   19822              : {
   19823        55993 :   gfc_symbol *proc;
   19824        55993 :   gfc_namespace *ns;
   19825              : 
   19826        55993 :   if (sym->attr.use_assoc || sym->attr.in_common)
   19827              :     return 1;
   19828              : 
   19829              :   /* The namespace of a module procedure interface holds the arguments and
   19830              :      symbols, and so the symbol namespace can be different to that of the
   19831              :      procedure.  */
   19832        55376 :   if (sym->ns != gfc_current_ns
   19833         5938 :       && gfc_current_ns->proc_name->abr_modproc_decl
   19834           48 :       && sym->ns->proc_name->attr.function
   19835           12 :       && sym->attr.result
   19836           12 :       && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
   19837              :     return 0;
   19838              : 
   19839              :   /* Check if the symbol's ns is inside the pure procedure.  */
   19840        60059 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   19841              :     {
   19842        59775 :       if (ns == sym->ns)
   19843              :         break;
   19844         6244 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE
   19845         5182 :           && !(sym->attr.function || sym->attr.result))
   19846              :         return 1;
   19847              :     }
   19848              : 
   19849        53815 :   proc = sym->ns->proc_name;
   19850        53815 :   if (sym->attr.dummy
   19851         5961 :       && !sym->attr.value
   19852         5839 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   19853         5636 :           || proc->attr.function))
   19854          697 :     return 1;
   19855              : 
   19856              :   /* TODO: Sort out what can be storage associated, if anything, and include
   19857              :      it here.  In principle equivalences should be scanned but it does not
   19858              :      seem to be possible to storage associate an impure variable this way.  */
   19859              :   return 0;
   19860              : }
   19861              : 
   19862              : 
   19863              : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   19864              :    current namespace is inside a pure procedure.  */
   19865              : 
   19866              : bool
   19867      2332430 : gfc_pure (gfc_symbol *sym)
   19868              : {
   19869      2332430 :   symbol_attribute attr;
   19870      2332430 :   gfc_namespace *ns;
   19871              : 
   19872      2332430 :   if (sym == NULL)
   19873              :     {
   19874              :       /* Check if the current namespace or one of its parents
   19875              :         belongs to a pure procedure.  */
   19876      3192469 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19877              :         {
   19878      1886290 :           sym = ns->proc_name;
   19879      1886290 :           if (sym == NULL)
   19880              :             return 0;
   19881      1885149 :           attr = sym->attr;
   19882      1885149 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   19883              :             return 1;
   19884              :         }
   19885              :       return 0;
   19886              :     }
   19887              : 
   19888      1017833 :   attr = sym->attr;
   19889              : 
   19890      1017833 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   19891              : }
   19892              : 
   19893              : 
   19894              : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   19895              :    checks if the current namespace is implicitly pure.  Note that this
   19896              :    function returns false for a PURE procedure.  */
   19897              : 
   19898              : bool
   19899       727138 : gfc_implicit_pure (gfc_symbol *sym)
   19900              : {
   19901       727138 :   gfc_namespace *ns;
   19902              : 
   19903       727138 :   if (sym == NULL)
   19904              :     {
   19905              :       /* Check if the current procedure is implicit_pure.  Walk up
   19906              :          the procedure list until we find a procedure.  */
   19907      1002034 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19908              :         {
   19909       715226 :           sym = ns->proc_name;
   19910       715226 :           if (sym == NULL)
   19911              :             return 0;
   19912              : 
   19913       715153 :           if (sym->attr.flavor == FL_PROCEDURE)
   19914              :             break;
   19915              :         }
   19916              :     }
   19917              : 
   19918       440254 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   19919       754462 :     && !sym->attr.pure;
   19920              : }
   19921              : 
   19922              : 
   19923              : void
   19924       425946 : gfc_unset_implicit_pure (gfc_symbol *sym)
   19925              : {
   19926       425946 :   gfc_namespace *ns;
   19927              : 
   19928       425946 :   if (sym == NULL)
   19929              :     {
   19930              :       /* Check if the current procedure is implicit_pure.  Walk up
   19931              :          the procedure list until we find a procedure.  */
   19932       695869 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19933              :         {
   19934       430435 :           sym = ns->proc_name;
   19935       430435 :           if (sym == NULL)
   19936              :             return;
   19937              : 
   19938       429602 :           if (sym->attr.flavor == FL_PROCEDURE)
   19939              :             break;
   19940              :         }
   19941              :     }
   19942              : 
   19943       425113 :   if (sym->attr.flavor == FL_PROCEDURE)
   19944       151350 :     sym->attr.implicit_pure = 0;
   19945              :   else
   19946       273763 :     sym->attr.pure = 0;
   19947              : }
   19948              : 
   19949              : 
   19950              : /* Test whether the current procedure is elemental or not.  */
   19951              : 
   19952              : bool
   19953      1365509 : gfc_elemental (gfc_symbol *sym)
   19954              : {
   19955      1365509 :   symbol_attribute attr;
   19956              : 
   19957      1365509 :   if (sym == NULL)
   19958            0 :     sym = gfc_current_ns->proc_name;
   19959            0 :   if (sym == NULL)
   19960              :     return 0;
   19961      1365509 :   attr = sym->attr;
   19962              : 
   19963      1365509 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   19964              : }
   19965              : 
   19966              : 
   19967              : /* Warn about unused labels.  */
   19968              : 
   19969              : static void
   19970         4674 : warn_unused_fortran_label (gfc_st_label *label)
   19971              : {
   19972         4700 :   if (label == NULL)
   19973              :     return;
   19974              : 
   19975           27 :   warn_unused_fortran_label (label->left);
   19976              : 
   19977           27 :   if (label->defined == ST_LABEL_UNKNOWN)
   19978              :     return;
   19979              : 
   19980           26 :   switch (label->referenced)
   19981              :     {
   19982            2 :     case ST_LABEL_UNKNOWN:
   19983            2 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   19984              :                    label->value, &label->where);
   19985            2 :       break;
   19986              : 
   19987            1 :     case ST_LABEL_BAD_TARGET:
   19988            1 :       gfc_warning (OPT_Wunused_label,
   19989              :                    "Label %d at %L defined but cannot be used",
   19990              :                    label->value, &label->where);
   19991            1 :       break;
   19992              : 
   19993              :     default:
   19994              :       break;
   19995              :     }
   19996              : 
   19997           26 :   warn_unused_fortran_label (label->right);
   19998              : }
   19999              : 
   20000              : 
   20001              : /* Returns the sequence type of a symbol or sequence.  */
   20002              : 
   20003              : static seq_type
   20004         1076 : sequence_type (gfc_typespec ts)
   20005              : {
   20006         1076 :   seq_type result;
   20007         1076 :   gfc_component *c;
   20008              : 
   20009         1076 :   switch (ts.type)
   20010              :   {
   20011           49 :     case BT_DERIVED:
   20012              : 
   20013           49 :       if (ts.u.derived->components == NULL)
   20014              :         return SEQ_NONDEFAULT;
   20015              : 
   20016           49 :       result = sequence_type (ts.u.derived->components->ts);
   20017          103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   20018           67 :         if (sequence_type (c->ts) != result)
   20019              :           return SEQ_MIXED;
   20020              : 
   20021              :       return result;
   20022              : 
   20023          129 :     case BT_CHARACTER:
   20024          129 :       if (ts.kind != gfc_default_character_kind)
   20025            0 :           return SEQ_NONDEFAULT;
   20026              : 
   20027              :       return SEQ_CHARACTER;
   20028              : 
   20029          240 :     case BT_INTEGER:
   20030          240 :       if (ts.kind != gfc_default_integer_kind)
   20031           25 :           return SEQ_NONDEFAULT;
   20032              : 
   20033              :       return SEQ_NUMERIC;
   20034              : 
   20035          559 :     case BT_REAL:
   20036          559 :       if (!(ts.kind == gfc_default_real_kind
   20037          269 :             || ts.kind == gfc_default_double_kind))
   20038            0 :           return SEQ_NONDEFAULT;
   20039              : 
   20040              :       return SEQ_NUMERIC;
   20041              : 
   20042           81 :     case BT_COMPLEX:
   20043           81 :       if (ts.kind != gfc_default_complex_kind)
   20044           48 :           return SEQ_NONDEFAULT;
   20045              : 
   20046              :       return SEQ_NUMERIC;
   20047              : 
   20048           17 :     case BT_LOGICAL:
   20049           17 :       if (ts.kind != gfc_default_logical_kind)
   20050            0 :           return SEQ_NONDEFAULT;
   20051              : 
   20052              :       return SEQ_NUMERIC;
   20053              : 
   20054              :     default:
   20055              :       return SEQ_NONDEFAULT;
   20056              :   }
   20057              : }
   20058              : 
   20059              : 
   20060              : /* Resolve derived type EQUIVALENCE object.  */
   20061              : 
   20062              : static bool
   20063           80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   20064              : {
   20065           80 :   gfc_component *c = derived->components;
   20066              : 
   20067           80 :   if (!derived)
   20068              :     return true;
   20069              : 
   20070              :   /* Shall not be an object of nonsequence derived type.  */
   20071           80 :   if (!derived->attr.sequence)
   20072              :     {
   20073            0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   20074              :                  "attribute to be an EQUIVALENCE object", sym->name,
   20075              :                  &e->where);
   20076            0 :       return false;
   20077              :     }
   20078              : 
   20079              :   /* Shall not have allocatable components.  */
   20080           80 :   if (derived->attr.alloc_comp)
   20081              :     {
   20082            1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   20083              :                  "components to be an EQUIVALENCE object",sym->name,
   20084              :                  &e->where);
   20085            1 :       return false;
   20086              :     }
   20087              : 
   20088           79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   20089              :     {
   20090            1 :       gfc_error ("Derived type variable %qs at %L with default "
   20091              :                  "initialization cannot be in EQUIVALENCE with a variable "
   20092              :                  "in COMMON", sym->name, &e->where);
   20093            1 :       return false;
   20094              :     }
   20095              : 
   20096          245 :   for (; c ; c = c->next)
   20097              :     {
   20098          167 :       if (gfc_bt_struct (c->ts.type)
   20099          167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   20100              :         return false;
   20101              : 
   20102              :       /* Shall not be an object of sequence derived type containing a pointer
   20103              :          in the structure.  */
   20104          167 :       if (c->attr.pointer)
   20105              :         {
   20106            0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   20107              :                      "component(s) cannot be an EQUIVALENCE object",
   20108              :                      sym->name, &e->where);
   20109            0 :           return false;
   20110              :         }
   20111              :     }
   20112              :   return true;
   20113              : }
   20114              : 
   20115              : 
   20116              : /* Resolve equivalence object.
   20117              :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   20118              :    an allocatable array, an object of nonsequence derived type, an object of
   20119              :    sequence derived type containing a pointer at any level of component
   20120              :    selection, an automatic object, a function name, an entry name, a result
   20121              :    name, a named constant, a structure component, or a subobject of any of
   20122              :    the preceding objects.  A substring shall not have length zero.  A
   20123              :    derived type shall not have components with default initialization nor
   20124              :    shall two objects of an equivalence group be initialized.
   20125              :    Either all or none of the objects shall have an protected attribute.
   20126              :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   20127              :    are implemented here.  */
   20128              : 
   20129              : static void
   20130         1565 : resolve_equivalence (gfc_equiv *eq)
   20131              : {
   20132         1565 :   gfc_symbol *sym;
   20133         1565 :   gfc_symbol *first_sym;
   20134         1565 :   gfc_expr *e;
   20135         1565 :   gfc_ref *r;
   20136         1565 :   locus *last_where = NULL;
   20137         1565 :   seq_type eq_type, last_eq_type;
   20138         1565 :   gfc_typespec *last_ts;
   20139         1565 :   int object, cnt_protected;
   20140         1565 :   const char *msg;
   20141              : 
   20142         1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   20143              : 
   20144         1565 :   first_sym = eq->expr->symtree->n.sym;
   20145              : 
   20146         1565 :   cnt_protected = 0;
   20147              : 
   20148         4727 :   for (object = 1; eq; eq = eq->eq, object++)
   20149              :     {
   20150         3171 :       e = eq->expr;
   20151              : 
   20152         3171 :       e->ts = e->symtree->n.sym->ts;
   20153              :       /* match_varspec might not know yet if it is seeing
   20154              :          array reference or substring reference, as it doesn't
   20155              :          know the types.  */
   20156         3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   20157              :         {
   20158         2152 :           gfc_ref *ref = e->ref;
   20159         2152 :           sym = e->symtree->n.sym;
   20160              : 
   20161         2152 :           if (sym->attr.dimension)
   20162              :             {
   20163         1855 :               ref->u.ar.as = sym->as;
   20164         1855 :               ref = ref->next;
   20165              :             }
   20166              : 
   20167              :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   20168         2152 :           if (e->ts.type == BT_CHARACTER
   20169          592 :               && ref
   20170          371 :               && ref->type == REF_ARRAY
   20171          371 :               && ref->u.ar.dimen == 1
   20172          371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   20173          371 :               && ref->u.ar.stride[0] == NULL)
   20174              :             {
   20175          370 :               gfc_expr *start = ref->u.ar.start[0];
   20176          370 :               gfc_expr *end = ref->u.ar.end[0];
   20177          370 :               void *mem = NULL;
   20178              : 
   20179              :               /* Optimize away the (:) reference.  */
   20180          370 :               if (start == NULL && end == NULL)
   20181              :                 {
   20182            9 :                   if (e->ref == ref)
   20183            0 :                     e->ref = ref->next;
   20184              :                   else
   20185            9 :                     e->ref->next = ref->next;
   20186              :                   mem = ref;
   20187              :                 }
   20188              :               else
   20189              :                 {
   20190          361 :                   ref->type = REF_SUBSTRING;
   20191          361 :                   if (start == NULL)
   20192            9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   20193              :                                               NULL, 1);
   20194          361 :                   ref->u.ss.start = start;
   20195          361 :                   if (end == NULL && e->ts.u.cl)
   20196           27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   20197          361 :                   ref->u.ss.end = end;
   20198          361 :                   ref->u.ss.length = e->ts.u.cl;
   20199          361 :                   e->ts.u.cl = NULL;
   20200              :                 }
   20201          370 :               ref = ref->next;
   20202          370 :               free (mem);
   20203              :             }
   20204              : 
   20205              :           /* Any further ref is an error.  */
   20206         1930 :           if (ref)
   20207              :             {
   20208            1 :               gcc_assert (ref->type == REF_ARRAY);
   20209            1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   20210              :                          &ref->u.ar.where);
   20211            1 :               continue;
   20212              :             }
   20213              :         }
   20214              : 
   20215         3170 :       if (!gfc_resolve_expr (e))
   20216            2 :         continue;
   20217              : 
   20218         3168 :       sym = e->symtree->n.sym;
   20219              : 
   20220         3168 :       if (sym->attr.is_protected)
   20221            2 :         cnt_protected++;
   20222         3168 :       if (cnt_protected > 0 && cnt_protected != object)
   20223              :         {
   20224            2 :               gfc_error ("Either all or none of the objects in the "
   20225              :                          "EQUIVALENCE set at %L shall have the "
   20226              :                          "PROTECTED attribute",
   20227              :                          &e->where);
   20228            2 :               break;
   20229              :         }
   20230              : 
   20231              :       /* Shall not equivalence common block variables in a PURE procedure.  */
   20232         3166 :       if (sym->ns->proc_name
   20233         3150 :           && sym->ns->proc_name->attr.pure
   20234            7 :           && sym->attr.in_common)
   20235              :         {
   20236              :           /* Need to check for symbols that may have entered the pure
   20237              :              procedure via a USE statement.  */
   20238            7 :           bool saw_sym = false;
   20239            7 :           if (sym->ns->use_stmts)
   20240              :             {
   20241            6 :               gfc_use_rename *r;
   20242           10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   20243            4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   20244              :             }
   20245              :           else
   20246              :             saw_sym = true;
   20247              : 
   20248            6 :           if (saw_sym)
   20249            3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   20250              :                        "EQUIVALENCE object in the pure procedure %qs",
   20251              :                        sym->name, &e->where, sym->ns->proc_name->name);
   20252              :           break;
   20253              :         }
   20254              : 
   20255              :       /* Shall not be a named constant.  */
   20256         3159 :       if (e->expr_type == EXPR_CONSTANT)
   20257              :         {
   20258            0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   20259              :                      "object", sym->name, &e->where);
   20260            0 :           continue;
   20261              :         }
   20262              : 
   20263         3161 :       if (e->ts.type == BT_DERIVED
   20264         3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   20265            2 :         continue;
   20266              : 
   20267              :       /* Check that the types correspond correctly:
   20268              :          Note 5.28:
   20269              :          A numeric sequence structure may be equivalenced to another sequence
   20270              :          structure, an object of default integer type, default real type, double
   20271              :          precision real type, default logical type such that components of the
   20272              :          structure ultimately only become associated to objects of the same
   20273              :          kind. A character sequence structure may be equivalenced to an object
   20274              :          of default character kind or another character sequence structure.
   20275              :          Other objects may be equivalenced only to objects of the same type and
   20276              :          kind parameters.  */
   20277              : 
   20278              :       /* Identical types are unconditionally OK.  */
   20279         3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   20280         2677 :         goto identical_types;
   20281              : 
   20282          480 :       last_eq_type = sequence_type (*last_ts);
   20283          480 :       eq_type = sequence_type (sym->ts);
   20284              : 
   20285              :       /* Since the pair of objects is not of the same type, mixed or
   20286              :          non-default sequences can be rejected.  */
   20287              : 
   20288          480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   20289              :                "statement at %L with different type objects");
   20290          481 :       if ((object ==2
   20291          480 :            && last_eq_type == SEQ_MIXED
   20292            7 :            && last_where
   20293            7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20294          486 :           || (eq_type == SEQ_MIXED
   20295            6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20296            1 :         continue;
   20297              : 
   20298          479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   20299              :                "statement at %L with objects of different type");
   20300          483 :       if ((object ==2
   20301          479 :            && last_eq_type == SEQ_NONDEFAULT
   20302           50 :            && last_where
   20303           49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20304          525 :           || (eq_type == SEQ_NONDEFAULT
   20305           24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20306            4 :         continue;
   20307              : 
   20308          475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   20309              :                "EQUIVALENCE statement at %L");
   20310          479 :       if (last_eq_type == SEQ_CHARACTER
   20311          475 :           && eq_type != SEQ_CHARACTER
   20312          475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20313            4 :                 continue;
   20314              : 
   20315          471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   20316              :                "EQUIVALENCE statement at %L");
   20317          473 :       if (last_eq_type == SEQ_NUMERIC
   20318          471 :           && eq_type != SEQ_NUMERIC
   20319          471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20320            2 :                 continue;
   20321              : 
   20322         3146 : identical_types:
   20323              : 
   20324         3146 :       last_ts =&sym->ts;
   20325         3146 :       last_where = &e->where;
   20326              : 
   20327         3146 :       if (!e->ref)
   20328         1003 :         continue;
   20329              : 
   20330              :       /* Shall not be an automatic array.  */
   20331         2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   20332              :         {
   20333            3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   20334              :                      "an EQUIVALENCE object", sym->name, &e->where);
   20335            3 :           continue;
   20336              :         }
   20337              : 
   20338         2140 :       r = e->ref;
   20339         4326 :       while (r)
   20340              :         {
   20341              :           /* Shall not be a structure component.  */
   20342         2187 :           if (r->type == REF_COMPONENT)
   20343              :             {
   20344            0 :               gfc_error ("Structure component %qs at %L cannot be an "
   20345              :                          "EQUIVALENCE object",
   20346            0 :                          r->u.c.component->name, &e->where);
   20347            0 :               break;
   20348              :             }
   20349              : 
   20350              :           /* A substring shall not have length zero.  */
   20351         2187 :           if (r->type == REF_SUBSTRING)
   20352              :             {
   20353          341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   20354              :                 {
   20355            1 :                   gfc_error ("Substring at %L has length zero",
   20356              :                              &r->u.ss.start->where);
   20357            1 :                   break;
   20358              :                 }
   20359              :             }
   20360         2186 :           r = r->next;
   20361              :         }
   20362              :     }
   20363         1565 : }
   20364              : 
   20365              : 
   20366              : /* Function called by resolve_fntype to flag other symbols used in the
   20367              :    length type parameter specification of function results.  */
   20368              : 
   20369              : static bool
   20370         4223 : flag_fn_result_spec (gfc_expr *expr,
   20371              :                      gfc_symbol *sym,
   20372              :                      int *f ATTRIBUTE_UNUSED)
   20373              : {
   20374         4223 :   gfc_namespace *ns;
   20375         4223 :   gfc_symbol *s;
   20376              : 
   20377         4223 :   if (expr->expr_type == EXPR_VARIABLE)
   20378              :     {
   20379         1378 :       s = expr->symtree->n.sym;
   20380         2159 :       for (ns = s->ns; ns; ns = ns->parent)
   20381         2159 :         if (!ns->parent)
   20382              :           break;
   20383              : 
   20384         1378 :       if (sym == s)
   20385              :         {
   20386            1 :           gfc_error ("Self reference in character length expression "
   20387              :                      "for %qs at %L", sym->name, &expr->where);
   20388            1 :           return true;
   20389              :         }
   20390              : 
   20391         1377 :       if (!s->fn_result_spec
   20392         1377 :           && s->attr.flavor == FL_PARAMETER)
   20393              :         {
   20394              :           /* Function contained in a module.... */
   20395           63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   20396              :             {
   20397           32 :               gfc_symtree *st;
   20398           32 :               s->fn_result_spec = 1;
   20399              :               /* Make sure that this symbol is translated as a module
   20400              :                  variable.  */
   20401           32 :               st = gfc_get_unique_symtree (ns);
   20402           32 :               st->n.sym = s;
   20403           32 :               s->refs++;
   20404           32 :             }
   20405              :           /* ... which is use associated and called.  */
   20406           31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   20407            0 :                         ||
   20408              :                   /* External function matched with an interface.  */
   20409            0 :                   (s->ns->proc_name
   20410            0 :                    && ((s->ns == ns
   20411            0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   20412            0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20413            0 :                    && s->ns->proc_name->attr.function))
   20414           31 :             s->fn_result_spec = 1;
   20415              :         }
   20416              :     }
   20417              :   return false;
   20418              : }
   20419              : 
   20420              : 
   20421              : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   20422              : 
   20423              : static void
   20424       347654 : resolve_fntype (gfc_namespace *ns)
   20425              : {
   20426       347654 :   gfc_entry_list *el;
   20427       347654 :   gfc_symbol *sym;
   20428              : 
   20429       347654 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   20430              :     return;
   20431              : 
   20432              :   /* If there are any entries, ns->proc_name is the entry master
   20433              :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   20434       180977 :   if (ns->entries)
   20435          596 :     sym = ns->entries->sym;
   20436              :   else
   20437              :     sym = ns->proc_name;
   20438       180977 :   if (sym->result == sym
   20439       145632 :       && sym->ts.type == BT_UNKNOWN
   20440            6 :       && !gfc_set_default_type (sym, 0, NULL)
   20441       180981 :       && !sym->attr.untyped)
   20442              :     {
   20443            3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   20444              :                  sym->name, &sym->declared_at);
   20445            3 :       sym->attr.untyped = 1;
   20446              :     }
   20447              : 
   20448        13789 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   20449         1856 :       && !sym->attr.contained
   20450          299 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   20451       180977 :       && gfc_check_symbol_access (sym))
   20452              :     {
   20453            0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   20454              :                       "%L of PRIVATE type %qs", sym->name,
   20455            0 :                       &sym->declared_at, sym->ts.u.derived->name);
   20456              :     }
   20457              : 
   20458       180977 :     if (ns->entries)
   20459         1253 :     for (el = ns->entries->next; el; el = el->next)
   20460              :       {
   20461          657 :         if (el->sym->result == el->sym
   20462          445 :             && el->sym->ts.type == BT_UNKNOWN
   20463            2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   20464          659 :             && !el->sym->attr.untyped)
   20465              :           {
   20466            2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   20467              :                        el->sym->name, &el->sym->declared_at);
   20468            2 :             el->sym->attr.untyped = 1;
   20469              :           }
   20470              :       }
   20471              : 
   20472       180977 :   if (sym->ts.type == BT_CHARACTER
   20473         7024 :       && sym->ts.u.cl->length
   20474         1875 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   20475         1870 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   20476              : }
   20477              : 
   20478              : 
   20479              : /* 12.3.2.1.1 Defined operators.  */
   20480              : 
   20481              : static bool
   20482          506 : check_uop_procedure (gfc_symbol *sym, locus where)
   20483              : {
   20484          506 :   gfc_formal_arglist *formal;
   20485              : 
   20486          506 :   if (!sym->attr.function)
   20487              :     {
   20488            4 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   20489              :                  sym->name, &where);
   20490            4 :       return false;
   20491              :     }
   20492              : 
   20493          502 :   if (sym->ts.type == BT_CHARACTER
   20494           15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   20495            2 :       && !(sym->result && ((sym->result->ts.u.cl
   20496            2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   20497              :     {
   20498            2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   20499              :                  "character length", sym->name, &where);
   20500            2 :       return false;
   20501              :     }
   20502              : 
   20503          500 :   formal = gfc_sym_get_dummy_args (sym);
   20504          500 :   if (!formal || !formal->sym)
   20505              :     {
   20506            1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   20507              :                  "one argument", sym->name, &where);
   20508            1 :       return false;
   20509              :     }
   20510              : 
   20511          499 :   if (formal->sym->attr.intent != INTENT_IN)
   20512              :     {
   20513            0 :       gfc_error ("First argument of operator interface at %L must be "
   20514              :                  "INTENT(IN)", &where);
   20515            0 :       return false;
   20516              :     }
   20517              : 
   20518          499 :   if (formal->sym->attr.optional)
   20519              :     {
   20520            0 :       gfc_error ("First argument of operator interface at %L cannot be "
   20521              :                  "optional", &where);
   20522            0 :       return false;
   20523              :     }
   20524              : 
   20525          499 :   formal = formal->next;
   20526          499 :   if (!formal || !formal->sym)
   20527              :     return true;
   20528              : 
   20529          295 :   if (formal->sym->attr.intent != INTENT_IN)
   20530              :     {
   20531            0 :       gfc_error ("Second argument of operator interface at %L must be "
   20532              :                  "INTENT(IN)", &where);
   20533            0 :       return false;
   20534              :     }
   20535              : 
   20536          295 :   if (formal->sym->attr.optional)
   20537              :     {
   20538            1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   20539              :                  "optional", &where);
   20540            1 :       return false;
   20541              :     }
   20542              : 
   20543          294 :   if (formal->next)
   20544              :     {
   20545            2 :       gfc_error ("Operator interface at %L must have, at most, two "
   20546              :                  "arguments", &where);
   20547            2 :       return false;
   20548              :     }
   20549              : 
   20550              :   return true;
   20551              : }
   20552              : 
   20553              : static void
   20554       348450 : gfc_resolve_uops (gfc_symtree *symtree)
   20555              : {
   20556       348450 :   gfc_interface *itr;
   20557              : 
   20558       348450 :   if (symtree == NULL)
   20559              :     return;
   20560              : 
   20561          398 :   gfc_resolve_uops (symtree->left);
   20562          398 :   gfc_resolve_uops (symtree->right);
   20563              : 
   20564          791 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   20565          393 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   20566              : }
   20567              : 
   20568              : /* Mark all lhs in assignment statement as used.  It is better to put this into
   20569              :    its own function rather than into the different switch cases in
   20570              :    gfc_resolve_code.  */
   20571              : 
   20572              : static void
   20573       682642 : mark_lhs_assignments_set (gfc_code *code)
   20574              : {
   20575              : 
   20576      1823378 :   for (; code; code = code->next)
   20577              :     {
   20578      1140736 :       gfc_expr *lvalue = code->expr1, *rvalue = code->expr2;
   20579              : 
   20580      1140736 :       if (lvalue == NULL || lvalue->symtree == NULL || rvalue == NULL)
   20581       842258 :         continue;
   20582              : 
   20583       298478 :       switch (code->op)
   20584              :         {
   20585       286902 :         case EXEC_ASSIGN:
   20586       286902 :           if (gfc_is_reallocatable_lhs (lvalue) && lvalue->rank == rvalue->rank)
   20587         8402 :             gfc_lvalue_allocated_at (lvalue->symtree->n.sym, &lvalue->where);
   20588              : 
   20589       297013 :           gcc_fallthrough();
   20590       297013 :         case EXEC_POINTER_ASSIGN:
   20591       297013 :           gfc_expr_set_at (lvalue, &rvalue->where, VALUE_VARDEF);
   20592              :         default:
   20593              :           break;
   20594              :         }
   20595              :     }
   20596       682642 : }
   20597              : 
   20598              : /* Examine all of the expressions associated with a program unit,
   20599              :    assign types to all intermediate expressions, make sure that all
   20600              :    assignments are to compatible types and figure out which names
   20601              :    refer to which functions or subroutines.  It doesn't check code
   20602              :    block, which is handled by gfc_resolve_code.  */
   20603              : 
   20604              : static void
   20605       350161 : resolve_types (gfc_namespace *ns)
   20606              : {
   20607       350161 :   gfc_namespace *n;
   20608       350161 :   gfc_charlen *cl;
   20609       350161 :   gfc_data *d;
   20610       350161 :   gfc_equiv *eq;
   20611       350161 :   gfc_namespace* old_ns = gfc_current_ns;
   20612       350161 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   20613              : 
   20614       350161 :   if (ns->types_resolved)
   20615              :     return;
   20616              : 
   20617              :   /* Check that all IMPLICIT types are ok.  */
   20618       347655 :   if (!ns->seen_implicit_none)
   20619              :     {
   20620              :       unsigned letter;
   20621      8743276 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   20622      8419451 :         if (ns->set_flag[letter]
   20623      8419451 :             && !resolve_typespec_used (&ns->default_type[letter],
   20624              :                                        &ns->implicit_loc[letter], NULL))
   20625              :           return;
   20626              :     }
   20627              : 
   20628       347654 :   gfc_current_ns = ns;
   20629              : 
   20630       347654 :   resolve_entries (ns);
   20631              : 
   20632       347654 :   resolve_common_vars (&ns->blank_common, false);
   20633       347654 :   resolve_common_blocks (ns->common_root);
   20634              : 
   20635       347654 :   resolve_contained_functions (ns);
   20636              : 
   20637       347654 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   20638       297227 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20639       194755 :     gfc_resolve_formal_arglist (ns->proc_name);
   20640              : 
   20641       347654 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   20642              : 
   20643       443949 :   for (cl = ns->cl_list; cl; cl = cl->next)
   20644        96295 :     resolve_charlen (cl);
   20645              : 
   20646       347654 :   gfc_traverse_ns (ns, resolve_symbol);
   20647              : 
   20648       347654 :   resolve_fntype (ns);
   20649              : 
   20650       396111 :   for (n = ns->contained; n; n = n->sibling)
   20651              :     {
   20652              :       /* Exclude final wrappers with the test for the artificial attribute.  */
   20653        48457 :       if (gfc_pure (ns->proc_name)
   20654            5 :           && !gfc_pure (n->proc_name)
   20655        48457 :           && !n->proc_name->attr.artificial)
   20656            0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   20657              :                    "also be PURE", n->proc_name->name,
   20658              :                    &n->proc_name->declared_at);
   20659              : 
   20660        48457 :       resolve_types (n);
   20661              :     }
   20662              : 
   20663       347654 :   forall_flag = 0;
   20664       347654 :   gfc_do_concurrent_flag = 0;
   20665       347654 :   gfc_check_interfaces (ns);
   20666              : 
   20667       347654 :   gfc_traverse_ns (ns, resolve_values);
   20668              : 
   20669       347654 :   if (ns->save_all || (!flag_automatic && !recursive))
   20670          315 :     gfc_save_all (ns);
   20671              : 
   20672       347654 :   iter_stack = NULL;
   20673       350172 :   for (d = ns->data; d; d = d->next)
   20674         2518 :     resolve_data (d);
   20675              : 
   20676       347654 :   iter_stack = NULL;
   20677       347654 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   20678              : 
   20679       347654 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   20680              : 
   20681       349219 :   for (eq = ns->equiv; eq; eq = eq->next)
   20682         1565 :     resolve_equivalence (eq);
   20683              : 
   20684              :   /* Warn about unused labels.  */
   20685       347654 :   if (warn_unused_label)
   20686         4647 :     warn_unused_fortran_label (ns->st_labels);
   20687              : 
   20688       347654 :   gfc_resolve_uops (ns->uop_root);
   20689              : 
   20690       347654 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   20691              : 
   20692       347654 :   gfc_resolve_omp_declare (ns);
   20693              : 
   20694       347654 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   20695              : 
   20696       347654 :   gfc_resolve_omp_udms (ns->omp_udm_root);
   20697              : 
   20698       347654 :   ns->types_resolved = 1;
   20699              : 
   20700       347654 :   gfc_current_ns = old_ns;
   20701              : }
   20702              : 
   20703              : 
   20704              : /* Call gfc_resolve_code recursively.  */
   20705              : 
   20706              : static void
   20707       350217 : resolve_codes (gfc_namespace *ns)
   20708              : {
   20709       350217 :   gfc_namespace *n;
   20710       350217 :   bitmap_obstack old_obstack;
   20711              : 
   20712       350217 :   if (ns->resolved == 1)
   20713        14166 :     return;
   20714              : 
   20715       384564 :   for (n = ns->contained; n; n = n->sibling)
   20716        48513 :     resolve_codes (n);
   20717              : 
   20718       336051 :   gfc_current_ns = ns;
   20719              : 
   20720              :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   20721       336051 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   20722       323853 :     cs_base = NULL;
   20723              : 
   20724              :   /* Set to an out of range value.  */
   20725       336051 :   current_entry_id = -1;
   20726              : 
   20727       336051 :   old_obstack = labels_obstack;
   20728       336051 :   bitmap_obstack_initialize (&labels_obstack);
   20729              : 
   20730       336051 :   gfc_resolve_oacc_declare (ns);
   20731       336051 :   gfc_resolve_oacc_routines (ns);
   20732       336051 :   gfc_resolve_omp_local_vars (ns);
   20733       336051 :   if (ns->omp_allocate)
   20734           62 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   20735       336051 :   gfc_resolve_code (ns->code, ns);
   20736              : 
   20737       336050 :   bitmap_obstack_release (&labels_obstack);
   20738       336050 :   labels_obstack = old_obstack;
   20739              : }
   20740              : 
   20741              : /* Return true if the value of a variable can be considered used, either
   20742              :    through the value_used flag or because it is a suitable dummy argument.  */
   20743              : 
   20744              : static bool
   20745          467 : var_value_is_used (gfc_symbol *sym)
   20746              : {
   20747          467 :   if (sym->attr.value_used != VALUE_UNUSED)
   20748              :     return true;
   20749              : 
   20750          107 :   if (!sym->attr.dummy)
   20751              :     return false;
   20752              : 
   20753           91 :   if (sym->attr.value)
   20754              :     return false;
   20755              : 
   20756           91 :   switch (sym->attr.intent)
   20757              :     {
   20758              :     case INTENT_UNKNOWN:
   20759              :     case INTENT_INOUT:
   20760              :     case INTENT_OUT:
   20761              :       return true;
   20762              : 
   20763              :     case INTENT_IN:
   20764              :     default:
   20765              :       return false;
   20766              :     }
   20767              : }
   20768              : 
   20769              : /* Similar, see if the variable could have gotten its value from somewhere.  */
   20770              : 
   20771              : static bool
   20772         2275 : var_value_is_set (gfc_symbol *sym)
   20773              : {
   20774         2275 :   if (sym->attr.value_set != VALUE_UNSET)
   20775              :     return true;
   20776              : 
   20777         1562 :   if (sym->value)
   20778              :     return true;
   20779              : 
   20780         1547 :   if (sym->ts.type == BT_DERIVED
   20781         1547 :       && gfc_has_default_initializer (sym->ts.u.derived))
   20782              :     return true;
   20783              : 
   20784         1547 :   if (!sym->attr.dummy)
   20785              :     return false;
   20786              : 
   20787         1503 :   if (sym->attr.value)
   20788              :     return true;
   20789              : 
   20790         1462 :   if (sym->attr.intent == INTENT_OUT)
   20791              :     return false;
   20792              : 
   20793              :   return true;
   20794              : }
   20795              : 
   20796              : /* Callback function to catch set but never used variables.  */
   20797              : 
   20798              : static void
   20799        33754 : find_unused_vs_set (gfc_symbol *sym)
   20800              : {
   20801        33754 :   symbol_attribute *attr = &sym->attr;
   20802              : 
   20803        33754 :   if (attr->flavor != FL_VARIABLE)
   20804              :     return;
   20805              : 
   20806              :   /* Do not warn about anything too far out of the ordinary.  This might be
   20807              :      tightened later.  */
   20808         8405 :   if (attr->in_common || attr->in_equivalence || attr->artificial
   20809         7995 :       || attr->cray_pointer || attr->cray_pointee || attr->associate_var
   20810         7994 :       || attr->target || attr->fe_temp || attr->omp_declare_target
   20811         7987 :       || attr->omp_declare_target_link || attr->omp_declare_target_local
   20812         7978 :       || attr->omp_declare_target_indirect || attr->oacc_declare_create
   20813         7978 :       || attr->oacc_declare_copyin || attr->oacc_declare_deviceptr
   20814         7978 :       || attr->oacc_declare_device_resident || attr->oacc_declare_link
   20815         7978 :       || attr->result || attr->warning_emitted || attr->use_assoc
   20816         5477 :       || attr->volatile_ || attr->asynchronous || !attr->referenced)
   20817              :     return;
   20818              : 
   20819           39 :   if (warn_unused_intent_out && attr->value_set == VALUE_INTENT_OUT
   20820         2322 :       && !var_value_is_used (sym))
   20821              :     {
   20822            1 :       gfc_warning (OPT_Wunused_intent_out, "Variable %qs passed to "
   20823              :                    "INTENT(OUT) argument at %L but value never used",
   20824              :                    sym->name, &sym->other_loc);
   20825            1 :       attr->warning_emitted = 1;
   20826            1 :       return;
   20827              :     }
   20828              : 
   20829         2319 :   if (warn_unused_read && attr->value_set == VALUE_READ && !var_value_is_used (sym))
   20830              :     {
   20831            1 :       gfc_warning (OPT_Wunused_read, "Variable %qs read at %L but never "
   20832              :                    "used", sym->name, &sym->other_loc);
   20833            1 :       attr->warning_emitted = 1;
   20834            1 :       return;
   20835              :     }
   20836              : 
   20837              :   /* There is no allocation in sight, but the variable is used anyway.  This
   20838              :      might be hidden behind PRESENT, but issue a warning nonetheless.  If
   20839              :      people complain, we might want to make this to an extra option to be
   20840              :      included with -Wextra.  */
   20841              : 
   20842         2275 :   if (warn_undefined_vars && attr->allocatable && !attr->allocated
   20843         2326 :       && var_value_is_used (sym))
   20844              :     {
   20845            2 :       if (attr->dummy && attr->intent == INTENT_OUT)
   20846              :         {
   20847            0 :           gfc_warning (OPT_Wundefined_vars, "Unallocated INTENT(OUT) variable "
   20848              :                        "%qs referenced at %L", sym->name, &sym->other_loc);
   20849            0 :           attr->warning_emitted = 1;
   20850            0 :           return;
   20851              :         }
   20852              : 
   20853            2 :       if (!attr->dummy)
   20854              :         {
   20855            0 :           gfc_warning (OPT_Wundefined_vars, "Unallocated variable %qs "
   20856              :                        "referenced at %L", sym->name, &sym->other_loc);
   20857            0 :           attr->warning_emitted = 1;
   20858            0 :           return;
   20859              :         }
   20860              :     }
   20861              : 
   20862         2318 :   if (warn_undefined_vars && !var_value_is_set (sym))
   20863              :     {
   20864              :       /* Warn about variables which have been allocated and used, but never
   20865              :          set.  */
   20866           46 :       if (attr->allocated && sym->attr.value_used > VALUE_MAYBE_USED)
   20867              :         {
   20868            3 :           switch (sym->attr.value_used)
   20869              :             {
   20870            1 :             case VALUE_INTENT_IN:
   20871            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   20872              :                            "undefined to INTENT(IN) argument at %L", sym->name,
   20873              :                            &sym->other_loc);
   20874            1 :               break;
   20875              : 
   20876            1 :             case VALUE_VALUE_ARG:
   20877            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   20878              :                            "undefined to VALUE argument at %L", sym->name,
   20879              :                            &sym->other_loc);
   20880            1 :               break;
   20881            1 :             case VALUE_USED:
   20882            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated undefined variable "
   20883              :                            "%qs used at %L", sym->name, &sym->other_loc);
   20884            1 :               break;
   20885            0 :             default:
   20886            0 :               gfc_internal_error ("Wrong value_set");
   20887            3 :               break;
   20888              :             }
   20889            3 :           attr->warning_emitted = 1;
   20890            3 :           return;
   20891              :         }
   20892              : 
   20893              :       /* Similar, when undefined variables are passed to INTENT(IN), VALUE
   20894              :          arguments or are used in general.  */
   20895              : 
   20896           43 :       if (attr->value_used == VALUE_INTENT_IN)
   20897              :         {
   20898            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   20899              :                        "to INTENT(IN) argument at %L", sym->name, &sym->other_loc);
   20900            1 :           attr->warning_emitted = 1;
   20901            1 :           return;
   20902              :         }
   20903           42 :       else if (attr->value_used == VALUE_VALUE_ARG)
   20904              :         {
   20905            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   20906              :                        "to VALUE argument at %L", sym->name, &sym->other_loc);
   20907            1 :           attr->warning_emitted = 1;
   20908            1 :           return;
   20909              :         }
   20910           41 :       else if (attr->value_used == VALUE_USED)
   20911              :         {
   20912            4 :           if (attr->dummy && attr->intent == INTENT_OUT)
   20913            1 :             gfc_warning (OPT_Wundefined_vars, "Undefined INTENT(OUT) variable %qs "
   20914              :                          "used at %L", sym->name, &sym->other_loc);
   20915              :           else
   20916            3 :             gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs used at "
   20917              :                          "%L", sym->name, &sym->other_loc);
   20918              : 
   20919            4 :           attr->warning_emitted = 1;
   20920            4 :           return;
   20921              :         }
   20922              : 
   20923              :       /* PR 28004 - warn about INTENT(OUT) variables that are never set.  If
   20924              :          the variable or a component are allocatable, do not warn since this is
   20925              :          a frequent shortcut for deallocation.  */
   20926              : 
   20927           37 :       if (sym->attr.dummy && sym->attr.intent == INTENT_OUT
   20928            1 :           && !(attr->allocatable || attr->alloc_comp))
   20929              :         {
   20930            0 :           gfc_warning (OPT_Wundefined_vars, "INTENT(OUT) variable %qs  "
   20931              :                        "declared at %L is not assigned a value", sym->name,
   20932              :                        &sym->declared_at);
   20933            0 :           attr->warning_emitted = 1;
   20934            0 :           return;
   20935              :         }
   20936              :     }
   20937              : 
   20938              :   /* Warn for unused but defined variables.  */
   20939              : 
   20940         2309 :   if (warn_unused_but_set_variable)
   20941              :     {
   20942         2201 :       if (attr->value_set == VALUE_VARDEF && !var_value_is_used (sym))
   20943              :         {
   20944            7 :           gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs defined at "
   20945              :                        "%L but never used", sym->name, &sym->other_loc);
   20946            7 :           attr->warning_emitted = 1;
   20947            7 :           return;
   20948              :         }
   20949         2194 :       if (attr->allocatable && attr->allocated && !var_value_is_used (sym))
   20950              :         {
   20951            1 :           gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs "
   20952              :                        "allocated at %L but never used", sym->name,
   20953              :                        &sym->extra_loc);
   20954            1 :           attr->warning_emitted = 1;
   20955            1 :           return;
   20956              :         }
   20957              :     }
   20958              : }
   20959              : 
   20960              : /* Run warn_unused_vs_set over a namespace recursively.  */
   20961              : 
   20962              : static void
   20963         4684 : warn_unused_vs_set (gfc_namespace *ns)
   20964              : {
   20965         4684 :   gfc_traverse_ns (ns, find_unused_vs_set);
   20966              : 
   20967         5204 :   for (gfc_namespace *n = ns->contained; n; n = n->sibling)
   20968          520 :     warn_unused_vs_set (n);
   20969         4684 : }
   20970              : 
   20971              : /* This function is called after a complete program unit has been compiled.
   20972              :    Its purpose is to examine all of the expressions associated with a program
   20973              :    unit, assign types to all intermediate expressions, make sure that all
   20974              :    assignments are to compatible types and figure out which names refer to
   20975              :    which functions or subroutines.  */
   20976              : 
   20977              : void
   20978       306466 : gfc_resolve (gfc_namespace *ns)
   20979              : {
   20980       306466 :   gfc_namespace *old_ns;
   20981       306466 :   code_stack *old_cs_base;
   20982       306466 :   struct gfc_omp_saved_state old_omp_state;
   20983              : 
   20984       306466 :   if (ns->resolved)
   20985         4762 :     return;
   20986              : 
   20987       301704 :   ns->resolved = -1;
   20988       301704 :   old_ns = gfc_current_ns;
   20989       301704 :   old_cs_base = cs_base;
   20990              : 
   20991              :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   20992              :      body, we should clear any state associated to it, so that say NS's
   20993              :      DO loops are not interpreted as OpenMP loops.  */
   20994       301704 :   if (!ns->construct_entities)
   20995       289506 :     gfc_omp_save_and_clear_state (&old_omp_state);
   20996              : 
   20997       301704 :   resolve_types (ns);
   20998       301704 :   component_assignment_level = 0;
   20999       301704 :   resolve_codes (ns);
   21000              : 
   21001       301703 :   if (warn_unused_but_set_variable || warn_unused_intent_out
   21002       297611 :       || warn_unused_read || warn_undefined_vars)
   21003         4164 :     warn_unused_vs_set (ns);
   21004              : 
   21005       301703 :   if (ns->omp_assumes)
   21006           13 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   21007              : 
   21008       301703 :   gfc_current_ns = old_ns;
   21009       301703 :   cs_base = old_cs_base;
   21010       301703 :   ns->resolved = 1;
   21011              : 
   21012       301703 :   gfc_run_passes (ns);
   21013              : 
   21014       301703 :   if (!ns->construct_entities)
   21015       289505 :     gfc_omp_restore_state (&old_omp_state);
   21016              : }
        

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.