LCOV - code coverage report
Current view: top level - gcc/fortran - symbol.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.2 % 2595 2393
Test Date: 2026-11-14 16:39:50 Functions: 96.3 % 188 181
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Maintain binary trees of symbols.
       2              :    Copyright (C) 2000-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              : 
      22              : #include "config.h"
      23              : #include "system.h"
      24              : #include "coretypes.h"
      25              : #include "options.h"
      26              : #include "gfortran.h"
      27              : #include "diagnostic-core.h"
      28              : #include "parse.h"
      29              : #include "match.h"
      30              : #include "constructor.h"
      31              : 
      32              : 
      33              : /* Strings for all symbol attributes.  We use these for dumping the
      34              :    parse tree, in error messages, and also when reading and writing
      35              :    modules.  */
      36              : 
      37              : const mstring flavors[] =
      38              : {
      39              :   minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
      40              :   minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
      41              :   minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
      42              :   minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
      43              :   minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
      44              :   minit ("UNION", FL_UNION), minit ("STRUCTURE", FL_STRUCT),
      45              :   minit (NULL, -1)
      46              : };
      47              : 
      48              : const mstring procedures[] =
      49              : {
      50              :     minit ("UNKNOWN-PROC", PROC_UNKNOWN),
      51              :     minit ("MODULE-PROC", PROC_MODULE),
      52              :     minit ("INTERNAL-PROC", PROC_INTERNAL),
      53              :     minit ("DUMMY-PROC", PROC_DUMMY),
      54              :     minit ("INTRINSIC-PROC", PROC_INTRINSIC),
      55              :     minit ("EXTERNAL-PROC", PROC_EXTERNAL),
      56              :     minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
      57              :     minit (NULL, -1)
      58              : };
      59              : 
      60              : const mstring intents[] =
      61              : {
      62              :     minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
      63              :     minit ("IN", INTENT_IN),
      64              :     minit ("OUT", INTENT_OUT),
      65              :     minit ("INOUT", INTENT_INOUT),
      66              :     minit (NULL, -1)
      67              : };
      68              : 
      69              : const mstring access_types[] =
      70              : {
      71              :     minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
      72              :     minit ("PUBLIC", ACCESS_PUBLIC),
      73              :     minit ("PRIVATE", ACCESS_PRIVATE),
      74              :     minit (NULL, -1)
      75              : };
      76              : 
      77              : const mstring ifsrc_types[] =
      78              : {
      79              :     minit ("UNKNOWN", IFSRC_UNKNOWN),
      80              :     minit ("DECL", IFSRC_DECL),
      81              :     minit ("BODY", IFSRC_IFBODY)
      82              : };
      83              : 
      84              : const mstring save_status[] =
      85              : {
      86              :     minit ("UNKNOWN", SAVE_NONE),
      87              :     minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
      88              :     minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
      89              : };
      90              : 
      91              : /* Set the mstrings for DTIO procedure names.  */
      92              : const mstring dtio_procs[] =
      93              : {
      94              :     minit ("_dtio_formatted_read", DTIO_RF),
      95              :     minit ("_dtio_formatted_write", DTIO_WF),
      96              :     minit ("_dtio_unformatted_read", DTIO_RUF),
      97              :     minit ("_dtio_unformatted_write", DTIO_WUF),
      98              : };
      99              : 
     100              : /* This is to make sure the backend generates setup code in the correct
     101              :    order.  */
     102              : static int next_decl_order = 1;
     103              : 
     104              : gfc_namespace *gfc_current_ns;
     105              : gfc_namespace *gfc_global_ns_list;
     106              : 
     107              : gfc_gsymbol *gfc_gsym_root = NULL;
     108              : 
     109              : gfc_symbol *gfc_derived_types;
     110              : 
     111              : static gfc_undo_change_set default_undo_chgset_var = { vNULL, vNULL, vNULL, NULL };
     112              : static gfc_undo_change_set *latest_undo_chgset = &default_undo_chgset_var;
     113              : 
     114              : 
     115              : /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
     116              : 
     117              : /* The following static variable indicates whether a particular element has
     118              :    been explicitly set or not.  */
     119              : 
     120              : static int new_flag[GFC_LETTERS];
     121              : 
     122              : 
     123              : /* Handle a correctly parsed IMPLICIT NONE.  */
     124              : 
     125              : void
     126        24776 : gfc_set_implicit_none (bool type, bool external, locus *loc)
     127              : {
     128        24776 :   int i;
     129              : 
     130        24776 :   if (external)
     131         1109 :     gfc_current_ns->has_implicit_none_export = 1;
     132              : 
     133        24776 :   if (type)
     134              :     {
     135        24763 :       gfc_current_ns->seen_implicit_none = 1;
     136       668550 :       for (i = 0; i < GFC_LETTERS; i++)
     137              :         {
     138       643789 :           if (gfc_current_ns->set_flag[i])
     139              :             {
     140            2 :               gfc_error_now ("IMPLICIT NONE (type) statement at %L following an "
     141              :                              "IMPLICIT statement", loc);
     142            2 :               return;
     143              :             }
     144       643787 :           gfc_clear_ts (&gfc_current_ns->default_type[i]);
     145       643787 :           gfc_current_ns->set_flag[i] = 1;
     146              :         }
     147              :     }
     148              : }
     149              : 
     150              : 
     151              : /* Reset the implicit range flags.  */
     152              : 
     153              : void
     154        25386 : gfc_clear_new_implicit (void)
     155              : {
     156        25386 :   int i;
     157              : 
     158       685422 :   for (i = 0; i < GFC_LETTERS; i++)
     159       660036 :     new_flag[i] = 0;
     160        25386 : }
     161              : 
     162              : 
     163              : /* Prepare for a new implicit range.  Sets flags in new_flag[].  */
     164              : 
     165              : bool
     166          654 : gfc_add_new_implicit_range (int c1, int c2)
     167              : {
     168          654 :   int i;
     169              : 
     170          654 :   c1 -= 'a';
     171          654 :   c2 -= 'a';
     172              : 
     173         5723 :   for (i = c1; i <= c2; i++)
     174              :     {
     175         5069 :       if (new_flag[i])
     176              :         {
     177            0 :           gfc_error ("Letter %qc already set in IMPLICIT statement at %C",
     178              :                      i + 'A');
     179            0 :           return false;
     180              :         }
     181              : 
     182         5069 :       new_flag[i] = 1;
     183              :     }
     184              : 
     185              :   return true;
     186              : }
     187              : 
     188              : 
     189              : /* Add a matched implicit range for gfc_set_implicit().  Check if merging
     190              :    the new implicit types back into the existing types will work.  */
     191              : 
     192              : bool
     193          446 : gfc_merge_new_implicit (gfc_typespec *ts)
     194              : {
     195          446 :   int i;
     196              : 
     197          446 :   if (gfc_current_ns->seen_implicit_none)
     198              :     {
     199            0 :       gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
     200            0 :       return false;
     201              :     }
     202              : 
     203        11996 :   for (i = 0; i < GFC_LETTERS; i++)
     204              :     {
     205        11552 :       if (new_flag[i])
     206              :         {
     207         5031 :           if (gfc_current_ns->set_flag[i])
     208              :             {
     209            2 :               gfc_error ("Letter %qc already has an IMPLICIT type at %C",
     210              :                          i + 'A');
     211            2 :               return false;
     212              :             }
     213              : 
     214         5029 :           gfc_current_ns->default_type[i] = *ts;
     215         5029 :           gfc_current_ns->implicit_loc[i] = gfc_current_locus;
     216         5029 :           gfc_current_ns->set_flag[i] = 1;
     217              :         }
     218              :     }
     219              : 
     220              :   /* The charlen belongs to ns->default_type; remove it.  */
     221          444 :   if (ts->type == BT_CHARACTER && ts->u.cl)
     222          103 :     gfc_remove_saved_charlen (ts->u.cl);
     223              : 
     224              :   return true;
     225              : }
     226              : 
     227              : 
     228              : /* Given a symbol, return a pointer to the typespec for its default type.  */
     229              : 
     230              : gfc_typespec *
     231      3083050 : gfc_get_default_type (const char *name, gfc_namespace *ns)
     232              : {
     233      3083050 :   char letter;
     234              : 
     235      3083050 :   letter = name[0];
     236              : 
     237      3083050 :   if (flag_allow_leading_underscore && letter == '_')
     238            0 :     gfc_fatal_error ("Option %<-fallow-leading-underscore%> is for use only by "
     239              :                      "gfortran developers, and should not be used for "
     240              :                      "implicitly typed variables");
     241              : 
     242      3083050 :   if (letter < 'a' || letter > 'z')
     243            0 :     gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name);
     244              : 
     245      3083050 :   if (ns == NULL)
     246       291310 :     ns = gfc_current_ns;
     247              : 
     248      3083050 :   return &ns->default_type[letter - 'a'];
     249              : }
     250              : 
     251              : 
     252              : /* Recursively append candidate SYM to CANDIDATES.  Store the number of
     253              :    candidates in CANDIDATES_LEN.  */
     254              : 
     255              : static void
     256          962 : lookup_symbol_fuzzy_find_candidates (gfc_symtree *sym,
     257              :                                      char **&candidates,
     258              :                                      size_t &candidates_len)
     259              : {
     260         1749 :   gfc_symtree *p;
     261              : 
     262         1749 :   if (sym == NULL)
     263              :     return;
     264              : 
     265         1749 :   if (sym->n.sym->ts.type != BT_UNKNOWN && sym->n.sym->ts.type != BT_PROCEDURE)
     266         1166 :     vec_push (candidates, candidates_len, sym->name);
     267         1749 :   p = sym->left;
     268         1749 :   if (p)
     269          829 :     lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
     270              : 
     271         1749 :   p = sym->right;
     272         1749 :   if (p)
     273              :     lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
     274              : }
     275              : 
     276              : 
     277              : /* Lookup symbol SYM_NAME fuzzily, taking names in SYMBOL into account.  */
     278              : 
     279              : static const char*
     280          133 : lookup_symbol_fuzzy (const char *sym_name, gfc_symbol *symbol)
     281              : {
     282          133 :   char **candidates = NULL;
     283          133 :   size_t candidates_len = 0;
     284          133 :   lookup_symbol_fuzzy_find_candidates (symbol->ns->sym_root, candidates,
     285              :                                        candidates_len);
     286          133 :   return gfc_closest_fuzzy_match (sym_name, candidates);
     287              : }
     288              : 
     289              : 
     290              : /* Given a pointer to a symbol, set its type according to the first
     291              :    letter of its name.  Fails if the letter in question has no default
     292              :    type.  */
     293              : 
     294              : bool
     295       121074 : gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
     296              : {
     297       121074 :   gfc_typespec *ts;
     298       121074 :   gfc_expr *e;
     299              : 
     300              :   /* Check to see if a function selector of unknown type can be resolved.  */
     301       121074 :   if (sym->assoc
     302           19 :       && (e = sym->assoc->target)
     303       121093 :       && e->expr_type == EXPR_FUNCTION)
     304              :     {
     305            6 :       if (e->ts.type == BT_UNKNOWN)
     306            5 :         gfc_resolve_expr (e);
     307            6 :       sym->ts = e->ts;
     308            6 :       if (sym->ts.type != BT_UNKNOWN)
     309              :         return true;
     310              :     }
     311              : 
     312       121069 :   if (sym->ts.type != BT_UNKNOWN)
     313            0 :     gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
     314              : 
     315       121069 :   ts = gfc_get_default_type (sym->name, ns);
     316              : 
     317       121069 :   if (ts->type == BT_UNKNOWN)
     318              :     {
     319        63509 :       if (error_flag && !sym->attr.untyped && !gfc_query_suppress_errors ())
     320              :         {
     321          133 :           const char *guessed = lookup_symbol_fuzzy (sym->name, sym);
     322          133 :           if (guessed)
     323           23 :             gfc_error ("Symbol %qs at %L has no IMPLICIT type"
     324              :                        "; did you mean %qs?",
     325              :                        sym->name, &sym->declared_at, guessed);
     326              :           else
     327          110 :             gfc_error ("Symbol %qs at %L has no IMPLICIT type",
     328              :                        sym->name, &sym->declared_at);
     329          133 :           sym->attr.untyped = 1; /* Ensure we only give an error once.  */
     330              :         }
     331              : 
     332              :       return false;
     333              :     }
     334              : 
     335        57560 :   sym->ts = *ts;
     336        57560 :   sym->attr.implicit_type = 1;
     337              : 
     338        57560 :   if (ts->type == BT_CHARACTER && ts->u.cl)
     339          457 :     sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
     340        57103 :   else if (ts->type == BT_CLASS
     341        57103 :            && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
     342              :     return false;
     343              : 
     344        57560 :   if (sym->attr.is_bind_c == 1 && warn_c_binding_type)
     345              :     {
     346              :       /* BIND(C) variables should not be implicitly declared.  */
     347            1 :       gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared BIND(C) "
     348              :                        "variable %qs at %L may not be C interoperable",
     349              :                        sym->name, &sym->declared_at);
     350            1 :       sym->ts.f90_type = sym->ts.type;
     351              :     }
     352              : 
     353        57560 :   if (sym->attr.dummy != 0)
     354              :     {
     355         4385 :       if (sym->ns->proc_name != NULL
     356         4384 :           && (sym->ns->proc_name->attr.subroutine != 0
     357          401 :               || sym->ns->proc_name->attr.function != 0)
     358         4384 :           && sym->ns->proc_name->attr.is_bind_c != 0
     359           57 :           && warn_c_binding_type)
     360              :         {
     361              :           /* Dummy args to a BIND(C) routine may not be interoperable if
     362              :              they are implicitly typed.  */
     363            1 :           gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared variable "
     364              :                            "%qs at %L may not be C interoperable but it is a "
     365              :                            "dummy argument to the BIND(C) procedure %qs at %L",
     366              :                            sym->name, &(sym->declared_at),
     367              :                            sym->ns->proc_name->name,
     368              :                            &(sym->ns->proc_name->declared_at));
     369            1 :           sym->ts.f90_type = sym->ts.type;
     370              :         }
     371              :     }
     372              : 
     373              :   return true;
     374              : }
     375              : 
     376              : 
     377              : /* This function is called from parse.cc(parse_progunit) to check the
     378              :    type of the function is not implicitly typed in the host namespace
     379              :    and to implicitly type the function result, if necessary.  */
     380              : 
     381              : void
     382        13423 : gfc_check_function_type (gfc_namespace *ns)
     383              : {
     384        13423 :   gfc_symbol *proc = ns->proc_name;
     385              : 
     386        13423 :   if (!proc->attr.contained || proc->result->attr.implicit_type)
     387              :     return;
     388              : 
     389        10534 :   if (proc->result->ts.type == BT_UNKNOWN && proc->result->ts.interface == NULL)
     390              :     {
     391          109 :       if (gfc_set_default_type (proc->result, 0, gfc_current_ns))
     392              :         {
     393           89 :           if (proc->result != proc)
     394              :             {
     395           23 :               proc->ts = proc->result->ts;
     396           23 :               proc->as = gfc_copy_array_spec (proc->result->as);
     397           23 :               proc->attr.dimension = proc->result->attr.dimension;
     398           23 :               proc->attr.pointer = proc->result->attr.pointer;
     399           23 :               proc->attr.allocatable = proc->result->attr.allocatable;
     400              :             }
     401              :         }
     402           20 :       else if (!proc->result->attr.proc_pointer)
     403              :         {
     404            2 :           gfc_error ("Function result %qs at %L has no IMPLICIT type",
     405              :                      proc->result->name, &proc->result->declared_at);
     406            2 :           proc->result->attr.untyped = 1;
     407              :         }
     408              :     }
     409              : }
     410              : 
     411              : 
     412              : /******************** Symbol attribute stuff *********************/
     413              : 
     414              : /* Older standards produced conflicts for some attributes that are allowed
     415              :    in newer standards.  Check for the conflict and issue an error depending
     416              :    on the standard in play.  */
     417              : 
     418              : static bool
     419        17798 : conflict_std (int standard, const char *a1, const char *a2, const char *name,
     420              :               locus *where)
     421              : {
     422        17798 :   if (name == NULL)
     423              :     {
     424        10726 :       return gfc_notify_std (standard, "%s attribute conflicts "
     425              :                              "with %s attribute at %L", a1, a2,
     426        10726 :                              where);
     427              :     }
     428              :   else
     429              :     {
     430         7072 :       return gfc_notify_std (standard, "%s attribute conflicts "
     431              :                              "with %s attribute in %qs at %L",
     432         7072 :                              a1, a2, name, where);
     433              :     }
     434              : }
     435              : 
     436              : /* This is a generic conflict-checker.  We do this to avoid having a
     437              :    single conflict in two places.  */
     438              : 
     439              : #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
     440              : #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
     441              : #define conf_std(a, b, std) if (attr->a && attr->b \
     442              :                                 && !conflict_std (std, a, b, name, where)) \
     443              :                                 return false;
     444              : 
     445              : bool
     446      7288752 : gfc_check_conflict (symbol_attribute *attr, const char *name, locus *where)
     447              : {
     448      7288752 :   static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
     449              :     *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
     450              :     *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
     451              :     *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
     452              :     *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
     453              :     *privat = "PRIVATE", *recursive = "RECURSIVE",
     454              :     *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
     455              :     *publik = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
     456              :     *function = "FUNCTION", *subroutine = "SUBROUTINE",
     457              :     *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
     458              :     *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
     459              :     *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
     460              :     *volatile_ = "VOLATILE", *is_protected = "PROTECTED",
     461              :     *is_bind_c = "BIND(C)", *procedure = "PROCEDURE",
     462              :     *proc_pointer = "PROCEDURE POINTER", *abstract = "ABSTRACT",
     463              :     *asynchronous = "ASYNCHRONOUS", *codimension = "CODIMENSION",
     464              :     *contiguous = "CONTIGUOUS", *generic = "GENERIC", *automatic = "AUTOMATIC",
     465              :     *pdt_len = "LEN", *pdt_kind = "KIND";
     466      7288752 :   static const char *threadprivate = "THREADPRIVATE";
     467      7288752 :   static const char *omp_groupprivate = "OpenMP GROUPPRIVATE";
     468      7288752 :   static const char *omp_declare_target = "OMP DECLARE TARGET";
     469      7288752 :   static const char *omp_declare_target_link = "OMP DECLARE TARGET LINK";
     470      7288752 :   static const char *omp_declare_target_local = "OMP DECLARE TARGET LOCAL";
     471      7288752 :   static const char *oacc_declare_copyin = "OACC DECLARE COPYIN";
     472      7288752 :   static const char *oacc_declare_create = "OACC DECLARE CREATE";
     473      7288752 :   static const char *oacc_declare_deviceptr = "OACC DECLARE DEVICEPTR";
     474      7288752 :   static const char *oacc_declare_device_resident =
     475              :                                                 "OACC DECLARE DEVICE_RESIDENT";
     476              : 
     477      7288752 :   const char *a1, *a2;
     478              : 
     479      7288752 :   if (attr->artificial)
     480              :     return true;
     481              : 
     482      7288732 :   if (where == NULL)
     483      4777327 :     where = &gfc_current_locus;
     484              : 
     485      7288732 :   if (attr->pointer && attr->intent != INTENT_UNKNOWN)
     486         4488 :     conf_std (pointer, intent, GFC_STD_F2003);
     487              : 
     488      7288731 :   conf_std (in_namelist, allocatable, GFC_STD_F2003);
     489      7288731 :   conf_std (in_namelist, pointer, GFC_STD_F2003);
     490              : 
     491              :   /* Check for attributes not allowed in a BLOCK DATA.  */
     492      7288730 :   if (gfc_current_state () == COMP_BLOCK_DATA)
     493              :     {
     494         3743 :       a1 = NULL;
     495              : 
     496         3743 :       if (attr->in_namelist)
     497            1 :         a1 = in_namelist;
     498         3743 :       if (attr->allocatable)
     499            0 :         a1 = allocatable;
     500         3743 :       if (attr->external)
     501            0 :         a1 = external;
     502         3743 :       if (attr->optional)
     503            0 :         a1 = optional;
     504         3743 :       if (attr->access == ACCESS_PRIVATE)
     505            0 :         a1 = privat;
     506         3743 :       if (attr->access == ACCESS_PUBLIC)
     507            0 :         a1 = publik;
     508         3743 :       if (attr->intent != INTENT_UNKNOWN)
     509            0 :         a1 = intent;
     510              : 
     511         3743 :       if (a1 != NULL)
     512              :         {
     513            1 :           gfc_error
     514            1 :             ("%s attribute not allowed in BLOCK DATA program unit at %L",
     515              :              a1, where);
     516            1 :           return false;
     517              :         }
     518              :     }
     519              : 
     520      7288729 :   if (attr->save == SAVE_EXPLICIT)
     521              :     {
     522         6757 :       conf (dummy, save);
     523         6755 :       conf (in_common, save);
     524         6741 :       conf (result, save);
     525         6738 :       conf (automatic, save);
     526              : 
     527         6736 :       switch (attr->flavor)
     528              :         {
     529            2 :           case FL_PROGRAM:
     530            2 :           case FL_BLOCK_DATA:
     531            2 :           case FL_MODULE:
     532            2 :           case FL_LABEL:
     533            2 :           case_fl_struct:
     534            2 :           case FL_PARAMETER:
     535            2 :             a1 = gfc_code2string (flavors, attr->flavor);
     536            2 :             a2 = save;
     537            2 :             goto conflict;
     538            2 :           case FL_NAMELIST:
     539            2 :             gfc_error ("Namelist group name at %L cannot have the "
     540              :                        "SAVE attribute", where);
     541            2 :             return false;
     542              :           case FL_PROCEDURE:
     543              :             /* Conflicts between SAVE and PROCEDURE will be checked at
     544              :                resolution stage, see "resolve_fl_procedure".  */
     545              :           case FL_VARIABLE:
     546              :           default:
     547              :             break;
     548              :         }
     549              :     }
     550              : 
     551              :   /* The copying of procedure dummy arguments for module procedures in
     552              :      a submodule occur whilst the current state is COMP_CONTAINS. It
     553              :      is necessary, therefore, to let this through.  */
     554      7288704 :   if (name && attr->dummy
     555       268620 :       && (attr->function || attr->subroutine)
     556         1678 :       && gfc_current_state () == COMP_CONTAINS
     557           21 :       && !(gfc_new_block && gfc_new_block->abr_modproc_decl))
     558            3 :     gfc_error_now ("internal procedure %qs at %L conflicts with "
     559              :                    "DUMMY argument", name, where);
     560              : 
     561      7288704 :   conf (dummy, entry);
     562      7288702 :   conf (dummy, intrinsic);
     563      7288701 :   conf (dummy, threadprivate);
     564      7288701 :   conf (dummy, omp_groupprivate);
     565      7288701 :   conf (dummy, omp_declare_target);
     566      7288701 :   conf (dummy, omp_declare_target_link);
     567      7288701 :   conf (dummy, omp_declare_target_local);
     568      7288701 :   conf (pointer, target);
     569      7288701 :   conf (pointer, intrinsic);
     570      7288701 :   conf (pointer, elemental);
     571      7288699 :   conf (pointer, codimension);
     572      7288665 :   conf (allocatable, elemental);
     573      7288664 :   conf (threadprivate, omp_groupprivate);
     574              : 
     575      7288656 :   conf (in_common, automatic);
     576      7288650 :   conf (result, automatic);
     577      7288648 :   conf (use_assoc, automatic);
     578      7288648 :   conf (dummy, automatic);
     579              : 
     580      7288646 :   conf (target, external);
     581      7288646 :   conf (target, intrinsic);
     582              : 
     583      7288646 :   if (!attr->if_source)
     584      7178653 :     conf (external, dimension);   /* See Fortran 95's R504.  */
     585              : 
     586      7288646 :   conf (external, intrinsic);
     587      7288644 :   conf (entry, intrinsic);
     588      7288643 :   conf (abstract, intrinsic);
     589              : 
     590      7288640 :   if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
     591        92322 :     conf (external, subroutine);
     592              : 
     593      7288638 :   if (attr->proc_pointer && !gfc_notify_std (GFC_STD_F2003,
     594              :                                              "Procedure pointer at %C"))
     595              :     return false;
     596              : 
     597      7288632 :   conf (allocatable, pointer);
     598      7288632 :   conf_std (allocatable, dummy, GFC_STD_F2003);
     599      7288632 :   conf_std (allocatable, function, GFC_STD_F2003);
     600      7288632 :   conf_std (allocatable, result, GFC_STD_F2003);
     601      7288632 :   conf_std (elemental, recursive, GFC_STD_F2018);
     602              : 
     603      7288632 :   conf (in_common, dummy);
     604      7288632 :   conf (in_common, allocatable);
     605      7288632 :   conf (in_common, codimension);
     606      7288632 :   conf (in_common, result);
     607              : 
     608      7288632 :   conf (in_equivalence, use_assoc);
     609      7288631 :   conf (in_equivalence, codimension);
     610      7288631 :   conf (in_equivalence, dummy);
     611      7288630 :   conf (in_equivalence, target);
     612      7288629 :   conf (in_equivalence, pointer);
     613      7288628 :   conf (in_equivalence, function);
     614      7288628 :   conf (in_equivalence, result);
     615      7288628 :   conf (in_equivalence, entry);
     616      7288628 :   conf (in_equivalence, allocatable);
     617      7288625 :   conf (in_equivalence, threadprivate);
     618      7288625 :   conf (in_equivalence, omp_groupprivate);
     619      7288625 :   conf (in_equivalence, omp_declare_target);
     620      7288625 :   conf (in_equivalence, omp_declare_target_link);
     621      7288625 :   conf (in_equivalence, omp_declare_target_local);
     622      7288625 :   conf (in_equivalence, oacc_declare_create);
     623      7288625 :   conf (in_equivalence, oacc_declare_copyin);
     624      7288625 :   conf (in_equivalence, oacc_declare_deviceptr);
     625      7288625 :   conf (in_equivalence, oacc_declare_device_resident);
     626      7288625 :   conf (in_equivalence, is_bind_c);
     627              : 
     628      7288624 :   conf (dummy, result);
     629      7288624 :   conf (entry, result);
     630      7288623 :   conf (generic, result);
     631      7288620 :   conf (generic, omp_declare_target);
     632      7288620 :   conf (generic, omp_declare_target_local);
     633      7288620 :   conf (generic, omp_declare_target_link);
     634              : 
     635      7288620 :   conf (function, subroutine);
     636              : 
     637      7288560 :   if (!function && !subroutine)
     638            0 :     conf (is_bind_c, dummy);
     639              : 
     640      7288560 :   conf (is_bind_c, cray_pointer);
     641      7288560 :   conf (is_bind_c, cray_pointee);
     642      7288560 :   conf (is_bind_c, codimension);
     643      7288559 :   conf (is_bind_c, allocatable);
     644      7288558 :   conf (is_bind_c, elemental);
     645              : 
     646              :   /* Need to also get volatile attr, according to 5.1 of F2003 draft.
     647              :      Parameter conflict caught below.  Also, value cannot be specified
     648              :      for a dummy procedure.  */
     649              : 
     650              :   /* Cray pointer/pointee conflicts.  */
     651      7288556 :   conf (cray_pointer, cray_pointee);
     652      7288555 :   conf (cray_pointer, dimension);
     653      7288554 :   conf (cray_pointer, codimension);
     654      7288554 :   conf (cray_pointer, contiguous);
     655      7288554 :   conf (cray_pointer, pointer);
     656      7288553 :   conf (cray_pointer, target);
     657      7288552 :   conf (cray_pointer, allocatable);
     658      7288552 :   conf (cray_pointer, external);
     659      7288552 :   conf (cray_pointer, intrinsic);
     660      7288552 :   conf (cray_pointer, in_namelist);
     661      7288552 :   conf (cray_pointer, function);
     662      7288552 :   conf (cray_pointer, subroutine);
     663      7288552 :   conf (cray_pointer, entry);
     664              : 
     665      7288552 :   conf (cray_pointee, allocatable);
     666      7288552 :   conf (cray_pointee, contiguous);
     667      7288552 :   conf (cray_pointee, codimension);
     668      7288552 :   conf (cray_pointee, intent);
     669      7288552 :   conf (cray_pointee, optional);
     670      7288552 :   conf (cray_pointee, dummy);
     671      7288551 :   conf (cray_pointee, target);
     672      7288550 :   conf (cray_pointee, intrinsic);
     673      7288550 :   conf (cray_pointee, pointer);
     674      7288549 :   conf (cray_pointee, entry);
     675      7288549 :   conf (cray_pointee, in_common);
     676      7288546 :   conf (cray_pointee, in_equivalence);
     677      7288544 :   conf (cray_pointee, threadprivate);
     678      7288543 :   conf (cray_pointee, omp_groupprivate);
     679      7288543 :   conf (cray_pointee, omp_declare_target);
     680      7288543 :   conf (cray_pointee, omp_declare_target_link);
     681      7288543 :   conf (cray_pointee, omp_declare_target_local);
     682      7288543 :   conf (cray_pointee, oacc_declare_create);
     683      7288543 :   conf (cray_pointee, oacc_declare_copyin);
     684      7288543 :   conf (cray_pointee, oacc_declare_deviceptr);
     685      7288543 :   conf (cray_pointee, oacc_declare_device_resident);
     686              : 
     687      7288543 :   conf (data, dummy);
     688      7288540 :   conf (data, function);
     689      7288539 :   conf (data, result);
     690      7288538 :   conf (data, allocatable);
     691              : 
     692      7288537 :   conf (value, pointer)
     693      7288536 :   conf (value, allocatable)
     694      7288536 :   conf (value, subroutine)
     695      7288536 :   conf (value, function)
     696      7288535 :   conf (value, volatile_)
     697      7288535 :   conf (value, codimension)
     698      7288535 :   conf (value, external)
     699              : 
     700      7288534 :   conf (codimension, result)
     701              : 
     702      7288531 :   if (attr->value
     703        45711 :       && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
     704              :     {
     705            4 :       a1 = value;
     706            4 :       a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
     707            4 :       goto conflict;
     708              :     }
     709              : 
     710      7288527 :   conf (is_protected, intrinsic)
     711      7288527 :   conf (is_protected, in_common)
     712              : 
     713      7288523 :   conf (asynchronous, intrinsic)
     714      7288523 :   conf (asynchronous, external)
     715              : 
     716      7288523 :   conf (volatile_, intrinsic)
     717      7288522 :   conf (volatile_, external)
     718              : 
     719      7288521 :   if (attr->volatile_ && attr->intent == INTENT_IN)
     720              :     {
     721            1 :       a1 = volatile_;
     722            1 :       a2 = intent_in;
     723            1 :       goto conflict;
     724              :     }
     725              : 
     726      7288520 :   conf (procedure, allocatable)
     727      7288518 :   conf (procedure, dimension)
     728      7288518 :   conf (procedure, codimension)
     729      7288518 :   conf (procedure, intrinsic)
     730      7288518 :   conf (procedure, target)
     731      7288518 :   conf (procedure, value)
     732      7288518 :   conf (procedure, volatile_)
     733      7288518 :   conf (procedure, asynchronous)
     734      7288518 :   conf (procedure, entry)
     735              : 
     736      7288517 :   conf (proc_pointer, abstract)
     737      7288515 :   conf (proc_pointer, omp_declare_target)
     738      7288515 :   conf (proc_pointer, omp_declare_target_local)
     739      7288515 :   conf (proc_pointer, omp_declare_target_link)
     740              : 
     741      7288515 :   conf (entry, omp_declare_target)
     742      7288515 :   conf (entry, omp_declare_target_local)
     743      7288515 :   conf (entry, omp_declare_target_link)
     744      7288515 :   conf (entry, oacc_declare_create)
     745      7288515 :   conf (entry, oacc_declare_copyin)
     746      7288515 :   conf (entry, oacc_declare_deviceptr)
     747      7288515 :   conf (entry, oacc_declare_device_resident)
     748              : 
     749      7288515 :   conf (pdt_kind, allocatable)
     750      7288514 :   conf (pdt_kind, pointer)
     751      7288513 :   conf (pdt_kind, dimension)
     752      7288512 :   conf (pdt_kind, codimension)
     753              : 
     754      7288512 :   conf (pdt_len, allocatable)
     755      7288511 :   conf (pdt_len, pointer)
     756      7288510 :   conf (pdt_len, dimension)
     757      7288509 :   conf (pdt_len, codimension)
     758      7288509 :   conf (pdt_len, pdt_kind)
     759              : 
     760      7288507 :   if (attr->access == ACCESS_PRIVATE)
     761              :     {
     762         2140 :       a1 = privat;
     763         2140 :       conf2 (pdt_kind);
     764         2139 :       conf2 (pdt_len);
     765              :     }
     766              : 
     767      7288505 :   a1 = gfc_code2string (flavors, attr->flavor);
     768              : 
     769      7288505 :   if (attr->in_namelist
     770         4621 :       && attr->flavor != FL_VARIABLE
     771         2021 :       && attr->flavor != FL_PROCEDURE
     772         2012 :       && attr->flavor != FL_UNKNOWN)
     773              :     {
     774            0 :       a2 = in_namelist;
     775            0 :       goto conflict;
     776              :     }
     777              : 
     778      7288505 :   switch (attr->flavor)
     779              :     {
     780       179788 :     case FL_PROGRAM:
     781       179788 :     case FL_BLOCK_DATA:
     782       179788 :     case FL_MODULE:
     783       179788 :     case FL_LABEL:
     784       179788 :       conf2 (codimension);
     785       179788 :       conf2 (dimension);
     786       179787 :       conf2 (dummy);
     787       179787 :       conf2 (volatile_);
     788       179785 :       conf2 (asynchronous);
     789       179784 :       conf2 (contiguous);
     790       179784 :       conf2 (pointer);
     791       179784 :       conf2 (is_protected);
     792       179783 :       conf2 (target);
     793       179783 :       conf2 (external);
     794       179782 :       conf2 (intrinsic);
     795       179782 :       conf2 (allocatable);
     796       179782 :       conf2 (result);
     797       179782 :       conf2 (in_namelist);
     798       179782 :       conf2 (optional);
     799       179782 :       conf2 (function);
     800       179782 :       conf2 (subroutine);
     801       179781 :       conf2 (threadprivate);
     802       179781 :       conf2 (omp_groupprivate);
     803       179781 :       conf2 (omp_declare_target);
     804       179781 :       conf2 (omp_declare_target_link);
     805       179781 :       conf2 (omp_declare_target_local);
     806       179781 :       conf2 (oacc_declare_create);
     807       179781 :       conf2 (oacc_declare_copyin);
     808       179781 :       conf2 (oacc_declare_deviceptr);
     809       179781 :       conf2 (oacc_declare_device_resident);
     810              : 
     811       179781 :       if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
     812              :         {
     813            2 :           a2 = attr->access == ACCESS_PUBLIC ? publik : privat;
     814            2 :           gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
     815              :             name, where);
     816            2 :           return false;
     817              :         }
     818              : 
     819       179779 :       if (attr->is_bind_c)
     820              :         {
     821            2 :           gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
     822            2 :           return false;
     823              :         }
     824              : 
     825              :       break;
     826              : 
     827              :     case FL_VARIABLE:
     828              :       break;
     829              : 
     830          835 :     case FL_NAMELIST:
     831          835 :       conf2 (result);
     832              :       break;
     833              : 
     834      4542810 :     case FL_PROCEDURE:
     835              :       /* Conflicts with INTENT, SAVE and RESULT will be checked
     836              :          at resolution stage, see "resolve_fl_procedure".  */
     837              : 
     838      4542810 :       if (attr->subroutine)
     839              :         {
     840       116802 :           a1 = subroutine;
     841       116802 :           conf2 (target);
     842       116802 :           conf2 (allocatable);
     843       116802 :           conf2 (volatile_);
     844       116801 :           conf2 (asynchronous);
     845       116800 :           conf2 (in_namelist);
     846       116800 :           conf2 (codimension);
     847       116800 :           conf2 (dimension);
     848       116799 :           conf2 (function);
     849       116799 :           if (!attr->proc_pointer)
     850              :             {
     851       116612 :               conf2 (threadprivate);
     852       116612 :               conf2 (omp_groupprivate);
     853              :             }
     854              :         }
     855              : 
     856              :       /* Procedure pointers in COMMON blocks are allowed in F03,
     857              :        * but forbidden per F08:C5100.  */
     858      4542807 :       if (!attr->proc_pointer || (gfc_option.allow_std & GFC_STD_F2008))
     859      4542637 :         conf2 (in_common);
     860              : 
     861      4542803 :       conf2 (omp_declare_target_local);
     862      4542801 :       conf2 (omp_declare_target_link);
     863              : 
     864      4542797 :       switch (attr->proc)
     865              :         {
     866       874917 :         case PROC_ST_FUNCTION:
     867       874917 :           conf2 (dummy);
     868       874916 :           conf2 (target);
     869              :           break;
     870              : 
     871        55260 :         case PROC_MODULE:
     872        55260 :           conf2 (dummy);
     873              :           break;
     874              : 
     875            0 :         case PROC_DUMMY:
     876            0 :           conf2 (result);
     877            0 :           conf2 (threadprivate);
     878            0 :           conf2 (omp_groupprivate);
     879              :           break;
     880              : 
     881              :         default:
     882              :           break;
     883              :         }
     884              : 
     885              :       break;
     886              : 
     887        39903 :     case_fl_struct:
     888        39903 :       conf2 (dummy);
     889        39903 :       conf2 (pointer);
     890        39903 :       conf2 (target);
     891        39903 :       conf2 (external);
     892        39903 :       conf2 (intrinsic);
     893        39903 :       conf2 (allocatable);
     894        39903 :       conf2 (optional);
     895        39903 :       conf2 (entry);
     896        39903 :       conf2 (function);
     897        39903 :       conf2 (subroutine);
     898        39903 :       conf2 (threadprivate);
     899        39903 :       conf2 (omp_groupprivate);
     900        39903 :       conf2 (result);
     901        39903 :       conf2 (omp_declare_target);
     902        39903 :       conf2 (omp_declare_target_local);
     903        39903 :       conf2 (omp_declare_target_link);
     904        39903 :       conf2 (oacc_declare_create);
     905        39903 :       conf2 (oacc_declare_copyin);
     906        39903 :       conf2 (oacc_declare_deviceptr);
     907        39903 :       conf2 (oacc_declare_device_resident);
     908              : 
     909        39903 :       if (attr->intent != INTENT_UNKNOWN)
     910              :         {
     911            0 :           a2 = intent;
     912            0 :           goto conflict;
     913              :         }
     914              :       break;
     915              : 
     916        41063 :     case FL_PARAMETER:
     917        41063 :       conf2 (external);
     918        41063 :       conf2 (intrinsic);
     919        41063 :       conf2 (optional);
     920        41063 :       conf2 (allocatable);
     921        41063 :       conf2 (function);
     922        41063 :       conf2 (subroutine);
     923        41063 :       conf2 (entry);
     924        41063 :       conf2 (contiguous);
     925        41063 :       conf2 (pointer);
     926        41063 :       conf2 (is_protected);
     927        41063 :       conf2 (target);
     928        41063 :       conf2 (dummy);
     929        41063 :       conf2 (in_common);
     930        41063 :       conf2 (value);
     931        41062 :       conf2 (volatile_);
     932        41061 :       conf2 (asynchronous);
     933        41061 :       conf2 (threadprivate);
     934        41061 :       conf2 (omp_groupprivate);
     935        41061 :       conf2 (value);
     936        41061 :       conf2 (codimension);
     937        41060 :       conf2 (result);
     938        41059 :       if (!attr->is_iso_c)
     939        41029 :         conf2 (is_bind_c);
     940              :       break;
     941              : 
     942              :     default:
     943              :       break;
     944              :     }
     945              : 
     946              :   return true;
     947              : 
     948          245 : conflict:
     949          245 :   if (name == NULL)
     950           57 :     gfc_error ("%s attribute conflicts with %s attribute at %L",
     951              :                a1, a2, where);
     952              :   else
     953          188 :     gfc_error ("%s attribute conflicts with %s attribute in %qs at %L",
     954              :                a1, a2, name, where);
     955              : 
     956              :   return false;
     957              : }
     958              : 
     959              : #undef conf
     960              : #undef conf2
     961              : #undef conf_std
     962              : 
     963              : 
     964              : /* Mark a symbol as referenced.  */
     965              : 
     966              : void
     967      8588020 : gfc_set_sym_referenced (gfc_symbol *sym)
     968              : {
     969      8588020 :   if (sym->attr.referenced)
     970              :     return;
     971              : 
     972      4346240 :   sym->attr.referenced = 1;
     973              : 
     974              :   /* Remember the declaration order.  */
     975      4346240 :   sym->decl_order = next_decl_order++;
     976              : }
     977              : 
     978              : 
     979              : /* Common subroutine called by attribute changing subroutines in order
     980              :    to prevent them from changing a symbol that has been
     981              :    use-associated.  Returns zero if it is OK to change the symbol,
     982              :    nonzero if not.  */
     983              : 
     984              : static int
     985      2443712 : check_used (symbol_attribute *attr, const char *name, locus *where)
     986              : {
     987              : 
     988      2443712 :   if (attr->use_assoc == 0)
     989              :     return 0;
     990              : 
     991           58 :   if (where == NULL)
     992           32 :     where = &gfc_current_locus;
     993              : 
     994           58 :   if (name == NULL)
     995            3 :     gfc_error ("Cannot change attributes of USE-associated symbol at %L",
     996              :                where);
     997              :   else
     998           55 :     gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
     999              :                name, where);
    1000              : 
    1001              :   return 1;
    1002              : }
    1003              : 
    1004              : 
    1005              : /* Generate an error because of a duplicate attribute.  */
    1006              : 
    1007              : static void
    1008           27 : duplicate_attr (const char *attr, locus *where)
    1009              : {
    1010              : 
    1011            0 :   if (where == NULL)
    1012            7 :     where = &gfc_current_locus;
    1013              : 
    1014            0 :   gfc_error ("Duplicate %s attribute specified at %L", attr, where);
    1015            0 : }
    1016              : 
    1017              : 
    1018              : bool
    1019         3016 : gfc_add_ext_attribute (symbol_attribute *attr, ext_attr_id_t ext_attr,
    1020              :                        locus *where ATTRIBUTE_UNUSED)
    1021              : {
    1022         3016 :   attr->ext_attr |= 1 << ext_attr;
    1023         3016 :   return true;
    1024              : }
    1025              : 
    1026              : 
    1027              : /* Called from decl.cc (attr_decl1) to check attributes, when declared
    1028              :    separately.  */
    1029              : 
    1030              : bool
    1031        10379 : gfc_add_attribute (symbol_attribute *attr, locus *where)
    1032              : {
    1033        10379 :   if (check_used (attr, NULL, where))
    1034              :     return false;
    1035              : 
    1036        10379 :   return gfc_check_conflict (attr, NULL, where);
    1037              : }
    1038              : 
    1039              : 
    1040              : bool
    1041        38424 : gfc_add_allocatable (symbol_attribute *attr, locus *where)
    1042              : {
    1043              : 
    1044        38424 :   if (check_used (attr, NULL, where))
    1045              :     return false;
    1046              : 
    1047        38424 :   if (attr->allocatable && ! gfc_submodule_procedure(attr))
    1048              :     {
    1049            1 :       duplicate_attr ("ALLOCATABLE", where);
    1050            1 :       return false;
    1051              :     }
    1052              : 
    1053          587 :   if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
    1054        38518 :       && !gfc_find_state (COMP_INTERFACE))
    1055              :     {
    1056            1 :       gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
    1057              :                  where);
    1058            1 :       return false;
    1059              :     }
    1060              : 
    1061        38422 :   attr->allocatable = 1;
    1062        38422 :   return gfc_check_conflict (attr, NULL, where);
    1063              : }
    1064              : 
    1065              : 
    1066              : bool
    1067           77 : gfc_add_automatic (symbol_attribute *attr, const char *name, locus *where)
    1068              : {
    1069           77 :   if (check_used (attr, name, where))
    1070              :     return false;
    1071              : 
    1072           77 :   if (attr->automatic && !gfc_notify_std (GFC_STD_LEGACY,
    1073              :         "Duplicate AUTOMATIC attribute specified at %L", where))
    1074              :     return false;
    1075              : 
    1076           77 :   attr->automatic = 1;
    1077           77 :   return gfc_check_conflict (attr, name, where);
    1078              : }
    1079              : 
    1080              : 
    1081              : bool
    1082         1847 : gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
    1083              : {
    1084              : 
    1085         1847 :   if (check_used (attr, name, where))
    1086              :     return false;
    1087              : 
    1088         1847 :   if (attr->codimension)
    1089              :     {
    1090            2 :       duplicate_attr ("CODIMENSION", where);
    1091            2 :       return false;
    1092              :     }
    1093              : 
    1094            6 :   if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
    1095         1846 :       && !gfc_find_state (COMP_INTERFACE))
    1096              :     {
    1097            0 :       gfc_error ("CODIMENSION specified for %qs outside its INTERFACE body "
    1098              :                  "at %L", name, where);
    1099            0 :       return false;
    1100              :     }
    1101              : 
    1102         1845 :   attr->codimension = 1;
    1103         1845 :   return gfc_check_conflict (attr, name, where);
    1104              : }
    1105              : 
    1106              : 
    1107              : bool
    1108       103787 : gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
    1109              : {
    1110              : 
    1111       103787 :   if (check_used (attr, name, where))
    1112              :     return false;
    1113              : 
    1114       103787 :   if (attr->dimension && ! gfc_submodule_procedure(attr))
    1115              :     {
    1116            2 :       duplicate_attr ("DIMENSION", where);
    1117            2 :       return false;
    1118              :     }
    1119              : 
    1120         1247 :   if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
    1121       104024 :       && !gfc_find_state (COMP_INTERFACE))
    1122              :     {
    1123            1 :       gfc_error ("DIMENSION specified for %qs outside its INTERFACE body "
    1124              :                  "at %L", name, where);
    1125            1 :       return false;
    1126              :     }
    1127              : 
    1128       103784 :   attr->dimension = 1;
    1129       103784 :   return gfc_check_conflict (attr, name, where);
    1130              : }
    1131              : 
    1132              : 
    1133              : bool
    1134         4468 : gfc_add_contiguous (symbol_attribute *attr, const char *name, locus *where)
    1135              : {
    1136              : 
    1137         4468 :   if (check_used (attr, name, where))
    1138              :     return false;
    1139              : 
    1140         4468 :   if (attr->contiguous)
    1141              :     {
    1142            2 :       duplicate_attr ("CONTIGUOUS", where);
    1143            2 :       return false;
    1144              :     }
    1145              : 
    1146         4466 :   attr->contiguous = 1;
    1147         4466 :   return gfc_check_conflict (attr, name, where);
    1148              : }
    1149              : 
    1150              : 
    1151              : bool
    1152        21260 : gfc_add_external (symbol_attribute *attr, locus *where)
    1153              : {
    1154              : 
    1155        21260 :   if (check_used (attr, NULL, where))
    1156              :     return false;
    1157              : 
    1158        21257 :   if (attr->external)
    1159              :     {
    1160            4 :       duplicate_attr ("EXTERNAL", where);
    1161            4 :       return false;
    1162              :     }
    1163              : 
    1164        21253 :   if (attr->pointer && attr->if_source != IFSRC_IFBODY)
    1165              :     {
    1166          864 :       attr->pointer = 0;
    1167          864 :       attr->proc_pointer = 1;
    1168              :     }
    1169              : 
    1170        21253 :   attr->external = 1;
    1171              : 
    1172        21253 :   return gfc_check_conflict (attr, NULL, where);
    1173              : }
    1174              : 
    1175              : 
    1176              : bool
    1177         1726 : gfc_add_intrinsic (symbol_attribute *attr, locus *where)
    1178              : {
    1179              : 
    1180         1726 :   if (check_used (attr, NULL, where))
    1181              :     return false;
    1182              : 
    1183         1726 :   if (attr->intrinsic)
    1184              :     {
    1185            0 :       duplicate_attr ("INTRINSIC", where);
    1186            0 :       return false;
    1187              :     }
    1188              : 
    1189         1726 :   attr->intrinsic = 1;
    1190              : 
    1191         1726 :   return gfc_check_conflict (attr, NULL, where);
    1192              : }
    1193              : 
    1194              : 
    1195              : bool
    1196        12255 : gfc_add_optional (symbol_attribute *attr, locus *where)
    1197              : {
    1198              : 
    1199        12255 :   if (check_used (attr, NULL, where))
    1200              :     return false;
    1201              : 
    1202        12255 :   if (attr->optional)
    1203              :     {
    1204            1 :       duplicate_attr ("OPTIONAL", where);
    1205            1 :       return false;
    1206              :     }
    1207              : 
    1208        12254 :   attr->optional = 1;
    1209        12254 :   return gfc_check_conflict (attr, NULL, where);
    1210              : }
    1211              : 
    1212              : bool
    1213          396 : gfc_add_kind (symbol_attribute *attr, locus *where)
    1214              : {
    1215          396 :   if (attr->pdt_kind)
    1216              :     {
    1217            0 :       duplicate_attr ("KIND", where);
    1218            0 :       return false;
    1219              :     }
    1220              : 
    1221          396 :   attr->pdt_kind = 1;
    1222          396 :   return gfc_check_conflict (attr, NULL, where);
    1223              : }
    1224              : 
    1225              : bool
    1226          485 : gfc_add_len (symbol_attribute *attr, locus *where)
    1227              : {
    1228          485 :   if (attr->pdt_len)
    1229              :     {
    1230            0 :       duplicate_attr ("LEN", where);
    1231            0 :       return false;
    1232              :     }
    1233              : 
    1234          485 :   attr->pdt_len = 1;
    1235          485 :   return gfc_check_conflict (attr, NULL, where);
    1236              : }
    1237              : 
    1238              : 
    1239              : bool
    1240        27430 : gfc_add_pointer (symbol_attribute *attr, locus *where)
    1241              : {
    1242              : 
    1243        27430 :   if (check_used (attr, NULL, where))
    1244              :     return false;
    1245              : 
    1246            3 :   if (attr->pointer && !(attr->if_source == IFSRC_IFBODY
    1247            1 :       && !gfc_find_state (COMP_INTERFACE))
    1248        27431 :       && ! gfc_submodule_procedure(attr))
    1249              :     {
    1250            1 :       duplicate_attr ("POINTER", where);
    1251            1 :       return false;
    1252              :     }
    1253              : 
    1254        27421 :   if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
    1255        54829 :       || (attr->if_source == IFSRC_IFBODY
    1256          502 :       && !gfc_find_state (COMP_INTERFACE)))
    1257           36 :     attr->proc_pointer = 1;
    1258              :   else
    1259        27393 :     attr->pointer = 1;
    1260              : 
    1261        27429 :   return gfc_check_conflict (attr, NULL, where);
    1262              : }
    1263              : 
    1264              : 
    1265              : bool
    1266          690 : gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
    1267              : {
    1268              : 
    1269          690 :   if (check_used (attr, NULL, where))
    1270              :     return false;
    1271              : 
    1272          690 :   attr->cray_pointer = 1;
    1273          690 :   return gfc_check_conflict (attr, NULL, where);
    1274              : }
    1275              : 
    1276              : 
    1277              : bool
    1278          674 : gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
    1279              : {
    1280              : 
    1281          674 :   if (check_used (attr, NULL, where))
    1282              :     return false;
    1283              : 
    1284          674 :   if (attr->cray_pointee)
    1285              :     {
    1286            1 :       gfc_error ("Cray Pointee at %L appears in multiple pointer()"
    1287              :                  " statements", where);
    1288            1 :       return false;
    1289              :     }
    1290              : 
    1291          673 :   attr->cray_pointee = 1;
    1292          673 :   return gfc_check_conflict (attr, NULL, where);
    1293              : }
    1294              : 
    1295              : 
    1296              : bool
    1297          114 : gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
    1298              : {
    1299          114 :   if (check_used (attr, name, where))
    1300              :     return false;
    1301              : 
    1302          114 :   if (attr->is_protected)
    1303              :     {
    1304            0 :         if (!gfc_notify_std (GFC_STD_LEGACY,
    1305              :                              "Duplicate PROTECTED attribute specified at %L",
    1306              :                              where))
    1307              :           return false;
    1308              :     }
    1309              : 
    1310          114 :   attr->is_protected = 1;
    1311          114 :   return gfc_check_conflict (attr, name, where);
    1312              : }
    1313              : 
    1314              : 
    1315              : bool
    1316         9451 : gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
    1317              : {
    1318              : 
    1319         9451 :   if (check_used (attr, name, where))
    1320              :     return false;
    1321              : 
    1322         9451 :   attr->result = 1;
    1323         9451 :   return gfc_check_conflict (attr, name, where);
    1324              : }
    1325              : 
    1326              : 
    1327              : bool
    1328        10840 : gfc_add_save (symbol_attribute *attr, save_state s, const char *name,
    1329              :               locus *where)
    1330              : {
    1331              : 
    1332        10840 :   if (check_used (attr, name, where))
    1333              :     return false;
    1334              : 
    1335        10840 :   if (s == SAVE_EXPLICIT && gfc_pure (NULL))
    1336              :     {
    1337            2 :       gfc_error ("SAVE attribute at %L cannot be specified in a PURE "
    1338              :                  "procedure", where);
    1339            2 :       return false;
    1340              :     }
    1341              : 
    1342        10838 :   if (s == SAVE_EXPLICIT)
    1343         3828 :     gfc_unset_implicit_pure (NULL);
    1344              : 
    1345         3828 :   if (s == SAVE_EXPLICIT && attr->save == SAVE_EXPLICIT
    1346           56 :       && (flag_automatic || pedantic))
    1347              :     {
    1348           21 :       if (!where)
    1349              :         {
    1350            1 :           gfc_error ("Duplicate SAVE attribute specified near %C");
    1351            1 :           return false;
    1352              :         }
    1353              : 
    1354           20 :       if (!gfc_notify_std (GFC_STD_LEGACY, "Duplicate SAVE attribute "
    1355              :                            "specified at %L", where))
    1356              :         return false;
    1357              :     }
    1358              : 
    1359        10835 :   attr->save = s;
    1360        10835 :   return gfc_check_conflict (attr, name, where);
    1361              : }
    1362              : 
    1363              : 
    1364              : bool
    1365        25788 : gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
    1366              : {
    1367              : 
    1368        25788 :   if (check_used (attr, name, where))
    1369              :     return false;
    1370              : 
    1371        25788 :   if (attr->value)
    1372              :     {
    1373            0 :         if (!gfc_notify_std (GFC_STD_LEGACY,
    1374              :                              "Duplicate VALUE attribute specified at %L",
    1375              :                              where))
    1376              :           return false;
    1377              :     }
    1378              : 
    1379        25788 :   attr->value = 1;
    1380        25788 :   return gfc_check_conflict (attr, name, where);
    1381              : }
    1382              : 
    1383              : 
    1384              : bool
    1385         1261 : gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
    1386              : {
    1387              :   /* No check_used needed as 11.2.1 of the F2003 standard allows
    1388              :      that the local identifier made accessible by a use statement can be
    1389              :      given a VOLATILE attribute - unless it is a coarray (F2008, C560).  */
    1390              : 
    1391         1261 :   if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
    1392            1 :     if (!gfc_notify_std (GFC_STD_LEGACY,
    1393              :                          "Duplicate VOLATILE attribute specified at %L",
    1394              :                          where))
    1395              :       return false;
    1396              : 
    1397              :   /* F2008:  C1282 A designator of a variable with the VOLATILE attribute
    1398              :      shall not appear in a pure subprogram.
    1399              : 
    1400              :      F2018: C1588 A local variable of a pure subprogram, or of a BLOCK
    1401              :      construct within a pure subprogram, shall not have the SAVE or
    1402              :      VOLATILE attribute.  */
    1403         1261 :   if (gfc_pure (NULL))
    1404              :     {
    1405            2 :       gfc_error ("VOLATILE attribute at %L cannot be specified in a "
    1406              :                  "PURE procedure", where);
    1407            2 :       return false;
    1408              :     }
    1409              : 
    1410              : 
    1411         1259 :   attr->volatile_ = 1;
    1412         1259 :   attr->volatile_ns = gfc_current_ns;
    1413         1259 :   return gfc_check_conflict (attr, name, where);
    1414              : }
    1415              : 
    1416              : 
    1417              : bool
    1418           71 : gfc_add_asynchronous (symbol_attribute *attr, const char *name, locus *where)
    1419              : {
    1420              :   /* No check_used needed as 11.2.1 of the F2003 standard allows
    1421              :      that the local identifier made accessible by a use statement can be
    1422              :      given a ASYNCHRONOUS attribute.  */
    1423              : 
    1424           71 :   if (attr->asynchronous && attr->asynchronous_ns == gfc_current_ns)
    1425            0 :     if (!gfc_notify_std (GFC_STD_LEGACY,
    1426              :                          "Duplicate ASYNCHRONOUS attribute specified at %L",
    1427              :                          where))
    1428              :       return false;
    1429              : 
    1430           71 :   attr->asynchronous = 1;
    1431           71 :   attr->asynchronous_ns = gfc_current_ns;
    1432           71 :   return gfc_check_conflict (attr, name, where);
    1433              : }
    1434              : 
    1435              : 
    1436              : bool
    1437           63 : gfc_add_omp_groupprivate (symbol_attribute *attr, const char *name,
    1438              :                           locus *where)
    1439              : {
    1440              : 
    1441           63 :   if (check_used (attr, name, where))
    1442              :     return false;
    1443              : 
    1444           63 :   if (attr->omp_groupprivate)
    1445              :     {
    1446            6 :       duplicate_attr ("OpenMP GROUPPRIVATE", where);
    1447            6 :       return false;
    1448              :     }
    1449              : 
    1450           57 :   attr->omp_groupprivate = true;
    1451           57 :   return gfc_check_conflict (attr, name, where);
    1452              : }
    1453              : 
    1454              : 
    1455              : bool
    1456          290 : gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
    1457              : {
    1458              : 
    1459          290 :   if (check_used (attr, name, where))
    1460              :     return false;
    1461              : 
    1462          290 :   if (attr->threadprivate)
    1463              :     {
    1464            0 :       duplicate_attr ("THREADPRIVATE", where);
    1465            0 :       return false;
    1466              :     }
    1467              : 
    1468          290 :   attr->threadprivate = 1;
    1469          290 :   return gfc_check_conflict (attr, name, where);
    1470              : }
    1471              : 
    1472              : 
    1473              : bool
    1474         1129 : gfc_add_omp_declare_target (symbol_attribute *attr, const char *name,
    1475              :                             locus *where)
    1476              : {
    1477              : 
    1478         1129 :   if (check_used (attr, name, where))
    1479              :     return false;
    1480              : 
    1481         1106 :   if (attr->omp_declare_target)
    1482              :     return true;
    1483              : 
    1484         1055 :   attr->omp_declare_target = 1;
    1485         1055 :   return gfc_check_conflict (attr, name, where);
    1486              : }
    1487              : 
    1488              : 
    1489              : bool
    1490           62 : gfc_add_omp_declare_target_link (symbol_attribute *attr, const char *name,
    1491              :                                  locus *where)
    1492              : {
    1493              : 
    1494           62 :   if (check_used (attr, name, where))
    1495              :     return false;
    1496              : 
    1497           60 :   if (attr->omp_declare_target_link)
    1498              :     return true;
    1499              : 
    1500           43 :   attr->omp_declare_target_link = 1;
    1501           43 :   return gfc_check_conflict (attr, name, where);
    1502              : }
    1503              : 
    1504              : 
    1505              : bool
    1506           69 : gfc_add_omp_declare_target_local (symbol_attribute *attr, const char *name,
    1507              :                                   locus *where)
    1508              : {
    1509              : 
    1510           69 :   if (check_used (attr, name, where))
    1511              :     return false;
    1512              : 
    1513           69 :   if (attr->omp_declare_target_local)
    1514              :     return true;
    1515              : 
    1516           59 :   attr->omp_declare_target_local = 1;
    1517           59 :   return gfc_check_conflict (attr, name, where);
    1518              : }
    1519              : 
    1520              : 
    1521              : bool
    1522            0 : gfc_add_oacc_declare_create (symbol_attribute *attr, const char *name,
    1523              :                              locus *where)
    1524              : {
    1525            0 :   if (check_used (attr, name, where))
    1526              :     return false;
    1527              : 
    1528            0 :   if (attr->oacc_declare_create)
    1529              :     return true;
    1530              : 
    1531            0 :   attr->oacc_declare_create = 1;
    1532            0 :   return gfc_check_conflict (attr, name, where);
    1533              : }
    1534              : 
    1535              : 
    1536              : bool
    1537            0 : gfc_add_oacc_declare_copyin (symbol_attribute *attr, const char *name,
    1538              :                              locus *where)
    1539              : {
    1540            0 :   if (check_used (attr, name, where))
    1541              :     return false;
    1542              : 
    1543            0 :   if (attr->oacc_declare_copyin)
    1544              :     return true;
    1545              : 
    1546            0 :   attr->oacc_declare_copyin = 1;
    1547            0 :   return gfc_check_conflict (attr, name, where);
    1548              : }
    1549              : 
    1550              : 
    1551              : bool
    1552            0 : gfc_add_oacc_declare_deviceptr (symbol_attribute *attr, const char *name,
    1553              :                                 locus *where)
    1554              : {
    1555            0 :   if (check_used (attr, name, where))
    1556              :     return false;
    1557              : 
    1558            0 :   if (attr->oacc_declare_deviceptr)
    1559              :     return true;
    1560              : 
    1561            0 :   attr->oacc_declare_deviceptr = 1;
    1562            0 :   return gfc_check_conflict (attr, name, where);
    1563              : }
    1564              : 
    1565              : 
    1566              : bool
    1567            0 : gfc_add_oacc_declare_device_resident (symbol_attribute *attr, const char *name,
    1568              :                                       locus *where)
    1569              : {
    1570            0 :   if (check_used (attr, name, where))
    1571              :     return false;
    1572              : 
    1573            0 :   if (attr->oacc_declare_device_resident)
    1574              :     return true;
    1575              : 
    1576            0 :   attr->oacc_declare_device_resident = 1;
    1577            0 :   return gfc_check_conflict (attr, name, where);
    1578              : }
    1579              : 
    1580              : 
    1581              : bool
    1582        13101 : gfc_add_target (symbol_attribute *attr, locus *where)
    1583              : {
    1584              : 
    1585        13101 :   if (check_used (attr, NULL, where))
    1586              :     return false;
    1587              : 
    1588        13101 :   if (attr->target)
    1589              :     {
    1590            1 :       duplicate_attr ("TARGET", where);
    1591            1 :       return false;
    1592              :     }
    1593              : 
    1594        13100 :   attr->target = 1;
    1595        13100 :   return gfc_check_conflict (attr, NULL, where);
    1596              : }
    1597              : 
    1598              : 
    1599              : bool
    1600       104300 : gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
    1601              : {
    1602              : 
    1603       104300 :   if (check_used (attr, name, where))
    1604              :     return false;
    1605              : 
    1606              :   /* Duplicate dummy arguments are allowed due to ENTRY statements.  */
    1607       104300 :   attr->dummy = 1;
    1608       104300 :   return gfc_check_conflict (attr, name, where);
    1609              : }
    1610              : 
    1611              : 
    1612              : bool
    1613        11643 : gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
    1614              : {
    1615              : 
    1616        11643 :   if (check_used (attr, name, where))
    1617              :     return false;
    1618              : 
    1619              :   /* Duplicate attribute already checked for.  */
    1620        11643 :   attr->in_common = 1;
    1621        11643 :   return gfc_check_conflict (attr, name, where);
    1622              : }
    1623              : 
    1624              : 
    1625              : bool
    1626         2949 : gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
    1627              : {
    1628              : 
    1629              :   /* Duplicate attribute already checked for.  */
    1630         2949 :   attr->in_equivalence = 1;
    1631         2949 :   if (!gfc_check_conflict (attr, name, where))
    1632              :     return false;
    1633              : 
    1634         2940 :   if (attr->flavor == FL_VARIABLE)
    1635              :     return true;
    1636              : 
    1637          109 :   return gfc_add_flavor (attr, FL_VARIABLE, name, where);
    1638              : }
    1639              : 
    1640              : 
    1641              : bool
    1642         2968 : gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
    1643              : {
    1644              : 
    1645         2968 :   if (check_used (attr, name, where))
    1646              :     return false;
    1647              : 
    1648         2967 :   attr->data = 1;
    1649         2967 :   return gfc_check_conflict (attr, name, where);
    1650              : }
    1651              : 
    1652              : 
    1653              : bool
    1654         2100 : gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
    1655              : {
    1656              : 
    1657         2100 :   attr->in_namelist = 1;
    1658         2100 :   return gfc_check_conflict (attr, name, where);
    1659              : }
    1660              : 
    1661              : 
    1662              : bool
    1663          953 : gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
    1664              : {
    1665              : 
    1666          953 :   if (check_used (attr, name, where))
    1667              :     return false;
    1668              : 
    1669          953 :   attr->sequence = 1;
    1670          953 :   return gfc_check_conflict (attr, name, where);
    1671              : }
    1672              : 
    1673              : 
    1674              : bool
    1675         8866 : gfc_add_elemental (symbol_attribute *attr, locus *where)
    1676              : {
    1677              : 
    1678         8866 :   if (check_used (attr, NULL, where))
    1679              :     return false;
    1680              : 
    1681         8866 :   if (attr->elemental)
    1682              :     {
    1683            2 :       duplicate_attr ("ELEMENTAL", where);
    1684            2 :       return false;
    1685              :     }
    1686              : 
    1687         8864 :   attr->elemental = 1;
    1688         8864 :   return gfc_check_conflict (attr, NULL, where);
    1689              : }
    1690              : 
    1691              : 
    1692              : bool
    1693        11860 : gfc_add_pure (symbol_attribute *attr, locus *where)
    1694              : {
    1695              : 
    1696        11860 :   if (check_used (attr, NULL, where))
    1697              :     return false;
    1698              : 
    1699        11860 :   if (attr->pure)
    1700              :     {
    1701            2 :       duplicate_attr ("PURE", where);
    1702            2 :       return false;
    1703              :     }
    1704              : 
    1705        11858 :   attr->pure = 1;
    1706        11858 :   return gfc_check_conflict (attr, NULL, where);
    1707              : }
    1708              : 
    1709              : 
    1710              : bool
    1711          769 : gfc_add_recursive (symbol_attribute *attr, locus *where)
    1712              : {
    1713              : 
    1714          769 :   if (check_used (attr, NULL, where))
    1715              :     return false;
    1716              : 
    1717          769 :   if (attr->recursive)
    1718              :     {
    1719            2 :       duplicate_attr ("RECURSIVE", where);
    1720            2 :       return false;
    1721              :     }
    1722              : 
    1723          767 :   attr->recursive = 1;
    1724          767 :   return gfc_check_conflict (attr, NULL, where);
    1725              : }
    1726              : 
    1727              : 
    1728              : bool
    1729          795 : gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
    1730              : {
    1731              : 
    1732          795 :   if (check_used (attr, name, where))
    1733              :     return false;
    1734              : 
    1735          795 :   if (attr->entry)
    1736              :     {
    1737            0 :       duplicate_attr ("ENTRY", where);
    1738            0 :       return false;
    1739              :     }
    1740              : 
    1741          795 :   attr->entry = 1;
    1742          795 :   return gfc_check_conflict (attr, name, where);
    1743              : }
    1744              : 
    1745              : 
    1746              : bool
    1747      1078867 : gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
    1748              : {
    1749              : 
    1750      1078867 :   if (attr->flavor != FL_PROCEDURE
    1751      1078867 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1752              :     return false;
    1753              : 
    1754      1078867 :   attr->function = 1;
    1755      1078867 :   return gfc_check_conflict (attr, name, where);
    1756              : }
    1757              : 
    1758              : 
    1759              : bool
    1760        88209 : gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
    1761              : {
    1762              : 
    1763        88209 :   if (attr->flavor != FL_PROCEDURE
    1764        88209 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1765              :     return false;
    1766              : 
    1767        88206 :   attr->subroutine = 1;
    1768              : 
    1769              :   /* If we are looking at a BLOCK DATA statement and we encounter a
    1770              :      name with a leading underscore (which must be
    1771              :      compiler-generated), do not check. See PR 84394.  */
    1772              : 
    1773        88206 :   if (name && *name != '_' && gfc_current_state () != COMP_BLOCK_DATA)
    1774        86248 :     return gfc_check_conflict (attr, name, where);
    1775              :   else
    1776              :     return true;
    1777              : }
    1778              : 
    1779              : 
    1780              : bool
    1781        28123 : gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
    1782              : {
    1783              : 
    1784        28123 :   if (attr->flavor != FL_PROCEDURE
    1785        28123 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1786              :     return false;
    1787              : 
    1788        28121 :   attr->generic = 1;
    1789        28121 :   return gfc_check_conflict (attr, name, where);
    1790              : }
    1791              : 
    1792              : 
    1793              : bool
    1794         1722 : gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
    1795              : {
    1796              : 
    1797         1722 :   if (check_used (attr, NULL, where))
    1798              :     return false;
    1799              : 
    1800         1722 :   if (attr->flavor != FL_PROCEDURE
    1801         1722 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1802              :     return false;
    1803              : 
    1804         1722 :   if (attr->procedure)
    1805              :     {
    1806            0 :       duplicate_attr ("PROCEDURE", where);
    1807            0 :       return false;
    1808              :     }
    1809              : 
    1810         1722 :   attr->procedure = 1;
    1811              : 
    1812         1722 :   return gfc_check_conflict (attr, NULL, where);
    1813              : }
    1814              : 
    1815              : 
    1816              : bool
    1817          872 : gfc_add_abstract (symbol_attribute* attr, locus* where)
    1818              : {
    1819          872 :   if (attr->abstract)
    1820              :     {
    1821            1 :       duplicate_attr ("ABSTRACT", where);
    1822            1 :       return false;
    1823              :     }
    1824              : 
    1825          871 :   attr->abstract = 1;
    1826              : 
    1827          871 :   return gfc_check_conflict (attr, NULL, where);
    1828              : }
    1829              : 
    1830              : 
    1831              : /* Flavors are special because some flavors are not what Fortran
    1832              :    considers attributes and can be reaffirmed multiple times.  */
    1833              : 
    1834              : bool
    1835      4028723 : gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
    1836              :                 locus *where)
    1837              : {
    1838              : 
    1839      4028723 :   if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
    1840      4028723 :        || f == FL_PARAMETER || f == FL_LABEL || gfc_fl_struct(f)
    1841       259106 :        || f == FL_NAMELIST) && check_used (attr, name, where))
    1842              :     return false;
    1843              : 
    1844      4028723 :   if (attr->flavor == f && f == FL_VARIABLE)
    1845              :     return true;
    1846              : 
    1847              :   /* Copying a procedure dummy argument for a module procedure in a
    1848              :      submodule results in the flavor being copied and would result in
    1849              :      an error without this.  */
    1850      4028721 :   if (attr->flavor == f && f == FL_PROCEDURE
    1851          638 :       && gfc_new_block && gfc_new_block->abr_modproc_decl)
    1852              :     return true;
    1853              : 
    1854      4028703 :   if (attr->flavor != FL_UNKNOWN)
    1855              :     {
    1856          684 :       if (where == NULL)
    1857          572 :         where = &gfc_current_locus;
    1858              : 
    1859          684 :       if (name)
    1860          352 :         gfc_error ("%s attribute of %qs conflicts with %s attribute at %L",
    1861          176 :                    gfc_code2string (flavors, attr->flavor), name,
    1862              :                    gfc_code2string (flavors, f), where);
    1863              :       else
    1864         1016 :         gfc_error ("%s attribute conflicts with %s attribute at %L",
    1865          508 :                    gfc_code2string (flavors, attr->flavor),
    1866              :                    gfc_code2string (flavors, f), where);
    1867              : 
    1868              :       return false;
    1869              :     }
    1870              : 
    1871      4028019 :   attr->flavor = f;
    1872              : 
    1873      4028019 :   return gfc_check_conflict (attr, name, where);
    1874              : }
    1875              : 
    1876              : 
    1877              : bool
    1878      1532065 : gfc_add_procedure (symbol_attribute *attr, procedure_type t,
    1879              :                    const char *name, locus *where)
    1880              : {
    1881              : 
    1882      1532065 :   if (check_used (attr, name, where))
    1883              :     return false;
    1884              : 
    1885      1532036 :   if (attr->flavor != FL_PROCEDURE
    1886      1532036 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1887              :     return false;
    1888              : 
    1889      1531986 :   if (where == NULL)
    1890      1511723 :     where = &gfc_current_locus;
    1891              : 
    1892      1531986 :   if (attr->proc != PROC_UNKNOWN && !attr->module_procedure
    1893          284 :       && attr->access == ACCESS_UNKNOWN)
    1894              :     {
    1895          282 :       gfc_error ("%s procedure at %L is already declared as %s procedure",
    1896              :                  gfc_code2string (procedures, t), where,
    1897          282 :                  gfc_code2string (procedures, attr->proc));
    1898            1 :       if (attr->proc == PROC_ST_FUNCTION && t == PROC_INTERNAL
    1899          283 :           && !gfc_notification_std (GFC_STD_F2008))
    1900              :         {
    1901            1 :           inform (gfc_get_location (where),
    1902              :                   "F2008: A pointer function assignment is ambiguous if it is "
    1903              :                   "the first executable statement after the specification "
    1904              :                   "block.  Please add any other kind of executable "
    1905              :                   "statement before it");
    1906              :         }
    1907              : 
    1908              :       return false;
    1909              :     }
    1910              : 
    1911      1531704 :   attr->proc = t;
    1912              : 
    1913              :   /* Statement functions are always scalar and functions.  */
    1914      1531704 :   if (t == PROC_ST_FUNCTION
    1915      1531704 :       && ((!attr->function && !gfc_add_function (attr, name, where))
    1916       437476 :           || attr->dimension))
    1917              :     return false;
    1918              : 
    1919      1531636 :   return gfc_check_conflict (attr, name, where);
    1920              : }
    1921              : 
    1922              : 
    1923              : bool
    1924        63027 : gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
    1925              : {
    1926              : 
    1927        63027 :   if (check_used (attr, NULL, where))
    1928              :     return false;
    1929              : 
    1930        63027 :   if (attr->intent == INTENT_UNKNOWN)
    1931              :     {
    1932        63027 :       attr->intent = intent;
    1933        63027 :       return gfc_check_conflict (attr, NULL, where);
    1934              :     }
    1935              : 
    1936            0 :   if (where == NULL)
    1937            0 :     where = &gfc_current_locus;
    1938              : 
    1939            0 :   gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
    1940            0 :              gfc_intent_string (attr->intent),
    1941              :              gfc_intent_string (intent), where);
    1942              : 
    1943            0 :   return false;
    1944              : }
    1945              : 
    1946              : 
    1947              : /* No checks for use-association in public and private statements.  */
    1948              : 
    1949              : bool
    1950         5896 : gfc_add_access (symbol_attribute *attr, gfc_access access,
    1951              :                 const char *name, locus *where)
    1952              : {
    1953              : 
    1954         5896 :   if (attr->access == ACCESS_UNKNOWN
    1955            5 :         || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
    1956              :     {
    1957         5892 :       attr->access = access;
    1958         5892 :       return gfc_check_conflict (attr, name, where);
    1959              :     }
    1960              : 
    1961            4 :   if (where == NULL)
    1962            3 :     where = &gfc_current_locus;
    1963            4 :   gfc_error ("ACCESS specification at %L was already specified", where);
    1964              : 
    1965            4 :   return false;
    1966              : }
    1967              : 
    1968              : 
    1969              : /* Set the is_bind_c field for the given symbol_attribute.  */
    1970              : 
    1971              : bool
    1972         8243 : gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
    1973              :                    int is_proc_lang_bind_spec)
    1974              : {
    1975              : 
    1976         8243 :   if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
    1977            5 :     gfc_error_now ("BIND(C) attribute at %L can only be used for "
    1978              :                    "variables or common blocks", where);
    1979         8238 :   else if (attr->is_bind_c)
    1980            1 :     gfc_error_now ("Duplicate BIND attribute specified at %L", where);
    1981              :   else
    1982         8237 :     attr->is_bind_c = 1;
    1983              : 
    1984         8243 :   if (where == NULL)
    1985           90 :     where = &gfc_current_locus;
    1986              : 
    1987         8243 :   if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) at %L", where))
    1988              :     return false;
    1989              : 
    1990         8243 :   return gfc_check_conflict (attr, name, where);
    1991              : }
    1992              : 
    1993              : 
    1994              : /* Set the extension field for the given symbol_attribute.  */
    1995              : 
    1996              : bool
    1997         1663 : gfc_add_extension (symbol_attribute *attr, locus *where)
    1998              : {
    1999         1663 :   if (where == NULL)
    2000            0 :     where = &gfc_current_locus;
    2001              : 
    2002         1663 :   if (attr->extension)
    2003            0 :     gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
    2004              :   else
    2005         1663 :     attr->extension = 1;
    2006              : 
    2007         1663 :   if (!gfc_notify_std (GFC_STD_F2003, "EXTENDS at %L", where))
    2008              :     return false;
    2009              : 
    2010              :   return true;
    2011              : }
    2012              : 
    2013              : 
    2014              : bool
    2015       161714 : gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
    2016              :                             gfc_formal_arglist * formal, locus *where)
    2017              : {
    2018       161714 :   if (check_used (&sym->attr, sym->name, where))
    2019              :     return false;
    2020              : 
    2021              :   /* Skip the following checks in the case of a module_procedures in a
    2022              :      submodule since they will manifestly fail.  */
    2023       161714 :   if (sym->attr.module_procedure == 1
    2024         1626 :       && source == IFSRC_DECL)
    2025         1072 :     goto finish;
    2026              : 
    2027       160642 :   if (where == NULL)
    2028       160642 :     where = &gfc_current_locus;
    2029              : 
    2030       160642 :   if (sym->attr.if_source != IFSRC_UNKNOWN
    2031        15268 :       && sym->attr.if_source != IFSRC_DECL)
    2032              :     {
    2033            0 :       gfc_error ("Symbol %qs at %L already has an explicit interface",
    2034              :                  sym->name, where);
    2035            0 :       return false;
    2036              :     }
    2037              : 
    2038       160642 :   if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
    2039              :     {
    2040            2 :       gfc_error ("%qs at %L has attributes specified outside its INTERFACE "
    2041              :                  "body", sym->name, where);
    2042            2 :       return false;
    2043              :     }
    2044              : 
    2045       160640 : finish:
    2046       161712 :   sym->formal = formal;
    2047       161712 :   sym->attr.if_source = source;
    2048              : 
    2049       161712 :   return true;
    2050              : }
    2051              : 
    2052              : 
    2053              : /* Add a type to a symbol.  */
    2054              : 
    2055              : bool
    2056       283999 : gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
    2057              : {
    2058       283999 :   sym_flavor flavor;
    2059       283999 :   bt type;
    2060              : 
    2061       283999 :   if (where == NULL)
    2062         5942 :     where = &gfc_current_locus;
    2063              : 
    2064       283999 :   if (sym->result)
    2065         9107 :     type = sym->result->ts.type;
    2066              :   else
    2067       274892 :     type = sym->ts.type;
    2068              : 
    2069       283999 :   if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
    2070         4775 :     type = sym->ns->proc_name->ts.type;
    2071              : 
    2072       283999 :   if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
    2073          111 :       && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
    2074           92 :            && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
    2075           55 :       && !sym->attr.module_procedure)
    2076              :     {
    2077           27 :       if (sym->attr.use_assoc)
    2078            2 :         gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
    2079              :                    "use-associated at %L", sym->name, where, sym->module,
    2080              :                    &sym->declared_at);
    2081           25 :       else if (sym->attr.function && sym->attr.result)
    2082            1 :         gfc_error ("Symbol %qs at %L already has basic type of %s",
    2083            1 :                    sym->ns->proc_name->name, where, gfc_basic_typename (type));
    2084              :       else
    2085           24 :         gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
    2086              :                    where, gfc_basic_typename (type));
    2087              :       return false;
    2088              :     }
    2089              : 
    2090       283972 :   if (sym->attr.procedure && sym->ts.interface)
    2091              :     {
    2092            1 :       gfc_error ("Procedure %qs at %L may not have basic type of %s",
    2093              :                  sym->name, where, gfc_basic_typename (ts->type));
    2094            1 :       return false;
    2095              :     }
    2096              : 
    2097       283971 :   flavor = sym->attr.flavor;
    2098              : 
    2099       283971 :   if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
    2100       283971 :       || flavor == FL_LABEL
    2101       283969 :       || (flavor == FL_PROCEDURE && sym->attr.subroutine)
    2102       283967 :       || flavor == FL_DERIVED || flavor == FL_NAMELIST)
    2103              :     {
    2104            4 :       gfc_error ("Symbol %qs at %L cannot have a type",
    2105            4 :                  sym->ns->proc_name ? sym->ns->proc_name->name : sym->name,
    2106              :                  where);
    2107            4 :       return false;
    2108              :     }
    2109              : 
    2110       283967 :   sym->ts = *ts;
    2111       283967 :   return true;
    2112              : }
    2113              : 
    2114              : 
    2115              : /* Clears all attributes.  */
    2116              : 
    2117              : void
    2118      8220901 : gfc_clear_attr (symbol_attribute *attr)
    2119              : {
    2120      8220901 :   memset (attr, 0, sizeof (symbol_attribute));
    2121      8220901 : }
    2122              : 
    2123              : 
    2124              : /* Check for missing attributes in the new symbol.  Currently does
    2125              :    nothing, but it's not clear that it is unnecessary yet.  */
    2126              : 
    2127              : bool
    2128       406328 : gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
    2129              :                   locus *where ATTRIBUTE_UNUSED)
    2130              : {
    2131              : 
    2132       406328 :   return true;
    2133              : }
    2134              : 
    2135              : 
    2136              : /* Copy an attribute to a symbol attribute, bit by bit.  Some
    2137              :    attributes have a lot of side-effects but cannot be present given
    2138              :    where we are called from, so we ignore some bits.  */
    2139              : 
    2140              : bool
    2141       282083 : gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
    2142              : {
    2143       282083 :   int is_proc_lang_bind_spec;
    2144              : 
    2145              :   /* In line with the other attributes, we only add bits but do not remove
    2146              :      them; cf. also PR 41034.  */
    2147       282083 :   dest->ext_attr |= src->ext_attr;
    2148              : 
    2149       282083 :   if (src->allocatable && !gfc_add_allocatable (dest, where))
    2150            4 :     goto fail;
    2151              : 
    2152       282079 :   if (src->automatic && !gfc_add_automatic (dest, NULL, where))
    2153            2 :     goto fail;
    2154       282077 :   if (src->dimension && !gfc_add_dimension (dest, NULL, where))
    2155            0 :     goto fail;
    2156       282077 :   if (src->codimension && !gfc_add_codimension (dest, NULL, where))
    2157            0 :     goto fail;
    2158       282077 :   if (src->contiguous && !gfc_add_contiguous (dest, NULL, where))
    2159            2 :     goto fail;
    2160       282075 :   if (src->optional && !gfc_add_optional (dest, where))
    2161            1 :     goto fail;
    2162       282074 :   if (src->pointer && !gfc_add_pointer (dest, where))
    2163            8 :     goto fail;
    2164       282066 :   if (src->is_protected && !gfc_add_protected (dest, NULL, where))
    2165            0 :     goto fail;
    2166       282066 :   if (src->save && !gfc_add_save (dest, src->save, NULL, where))
    2167            4 :     goto fail;
    2168       282062 :   if (src->value && !gfc_add_value (dest, NULL, where))
    2169            0 :     goto fail;
    2170       282062 :   if (src->volatile_ && !gfc_add_volatile (dest, NULL, where))
    2171            0 :     goto fail;
    2172       282062 :   if (src->asynchronous && !gfc_add_asynchronous (dest, NULL, where))
    2173            0 :     goto fail;
    2174       282062 :   if (src->omp_groupprivate
    2175       282062 :       && !gfc_add_omp_groupprivate (dest, NULL, where))
    2176            0 :     goto fail;
    2177       282062 :   if (src->threadprivate
    2178       282062 :       && !gfc_add_threadprivate (dest, NULL, where))
    2179            0 :     goto fail;
    2180       282062 :   if (src->omp_declare_target
    2181       282062 :       && !gfc_add_omp_declare_target (dest, NULL, where))
    2182            0 :     goto fail;
    2183       282062 :   if (src->omp_declare_target_link
    2184       282062 :       && !gfc_add_omp_declare_target_link (dest, NULL, where))
    2185            0 :     goto fail;
    2186       282062 :   if (src->omp_declare_target_local
    2187       282062 :       && !gfc_add_omp_declare_target_local (dest, NULL, where))
    2188            0 :     goto fail;
    2189       282062 :   if (src->oacc_declare_create
    2190       282062 :       && !gfc_add_oacc_declare_create (dest, NULL, where))
    2191            0 :     goto fail;
    2192       282062 :   if (src->oacc_declare_copyin
    2193       282062 :       && !gfc_add_oacc_declare_copyin (dest, NULL, where))
    2194            0 :     goto fail;
    2195       282062 :   if (src->oacc_declare_deviceptr
    2196       282062 :       && !gfc_add_oacc_declare_deviceptr (dest, NULL, where))
    2197            0 :     goto fail;
    2198       282062 :   if (src->oacc_declare_device_resident
    2199       282062 :       && !gfc_add_oacc_declare_device_resident (dest, NULL, where))
    2200            0 :     goto fail;
    2201       282062 :   if (src->target && !gfc_add_target (dest, where))
    2202            2 :     goto fail;
    2203       282060 :   if (src->dummy && !gfc_add_dummy (dest, NULL, where))
    2204            0 :     goto fail;
    2205       282060 :   if (src->result && !gfc_add_result (dest, NULL, where))
    2206            0 :     goto fail;
    2207       282060 :   if (src->entry)
    2208            0 :     dest->entry = 1;
    2209              : 
    2210       282060 :   if (src->in_namelist && !gfc_add_in_namelist (dest, NULL, where))
    2211            0 :     goto fail;
    2212              : 
    2213       282060 :   if (src->in_common && !gfc_add_in_common (dest, NULL, where))
    2214            0 :     goto fail;
    2215              : 
    2216       282060 :   if (src->generic && !gfc_add_generic (dest, NULL, where))
    2217            0 :     goto fail;
    2218       282060 :   if (src->function && !gfc_add_function (dest, NULL, where))
    2219            0 :     goto fail;
    2220       282060 :   if (src->subroutine && !gfc_add_subroutine (dest, NULL, where))
    2221            0 :     goto fail;
    2222              : 
    2223       282060 :   if (src->sequence && !gfc_add_sequence (dest, NULL, where))
    2224            0 :     goto fail;
    2225       282060 :   if (src->elemental && !gfc_add_elemental (dest, where))
    2226            0 :     goto fail;
    2227       282060 :   if (src->pure && !gfc_add_pure (dest, where))
    2228            0 :     goto fail;
    2229       282060 :   if (src->recursive && !gfc_add_recursive (dest, where))
    2230            0 :     goto fail;
    2231       282060 :   if (src->always_explicit)
    2232          141 :     dest->always_explicit = 1;
    2233              : 
    2234       282060 :   if (src->flavor != FL_UNKNOWN
    2235       282060 :       && !gfc_add_flavor (dest, src->flavor, NULL, where))
    2236          510 :     goto fail;
    2237              : 
    2238       281550 :   if (src->intent != INTENT_UNKNOWN
    2239       281550 :       && !gfc_add_intent (dest, src->intent, where))
    2240            0 :     goto fail;
    2241              : 
    2242       281550 :   if (src->access != ACCESS_UNKNOWN
    2243       281550 :       && !gfc_add_access (dest, src->access, NULL, where))
    2244            1 :     goto fail;
    2245              : 
    2246       281549 :   if (!gfc_missing_attr (dest, where))
    2247            0 :     goto fail;
    2248              : 
    2249       281549 :   if (src->cray_pointer && !gfc_add_cray_pointer (dest, where))
    2250            0 :     goto fail;
    2251       281549 :   if (src->cray_pointee && !gfc_add_cray_pointee (dest, where))
    2252            0 :     goto fail;
    2253              : 
    2254       281549 :   is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
    2255       281549 :   if (src->is_bind_c
    2256       281549 :       && !gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec))
    2257              :     return false;
    2258              : 
    2259       281548 :   if (src->is_c_interop)
    2260            0 :     dest->is_c_interop = 1;
    2261       281548 :   if (src->is_iso_c)
    2262            0 :     dest->is_iso_c = 1;
    2263              : 
    2264       281548 :   if (src->external && !gfc_add_external (dest, where))
    2265            5 :     goto fail;
    2266       281543 :   if (src->intrinsic && !gfc_add_intrinsic (dest, where))
    2267            4 :     goto fail;
    2268       281539 :   if (src->proc_pointer)
    2269          463 :     dest->proc_pointer = 1;
    2270              : 
    2271              :   return true;
    2272              : 
    2273       282083 : fail:
    2274              :   return false;
    2275              : }
    2276              : 
    2277              : 
    2278              : /* A function to generate a dummy argument symbol using that from the
    2279              :    interface declaration. Can be used for the result symbol as well if
    2280              :    the flag is set.  */
    2281              : 
    2282              : int
    2283          418 : gfc_copy_dummy_sym (gfc_symbol **dsym, gfc_symbol *sym, int result)
    2284              : {
    2285          418 :   int rc;
    2286              : 
    2287          418 :   rc = gfc_get_symbol (sym->name, NULL, dsym);
    2288          418 :   if (rc)
    2289              :     return rc;
    2290              : 
    2291          418 :   if (!gfc_add_type (*dsym, &(sym->ts), &gfc_current_locus))
    2292              :     return 1;
    2293              : 
    2294          418 :   if (sym->attr.external
    2295           11 :       && (sym->attr.codimension || sym->attr.dimension))
    2296            1 :     (*dsym)->attr.if_source = IFSRC_DECL;
    2297              : 
    2298          418 :   if (!gfc_copy_attr (&(*dsym)->attr, &(sym->attr),
    2299              :       &gfc_current_locus))
    2300              :     return 1;
    2301              : 
    2302          418 :   if ((*dsym)->attr.dimension)
    2303           64 :     (*dsym)->as = gfc_copy_array_spec (sym->as);
    2304              : 
    2305          418 :   (*dsym)->attr.class_ok = sym->attr.class_ok;
    2306              : 
    2307          418 :   if ((*dsym) != NULL && !result
    2308          353 :       && (!gfc_add_dummy(&(*dsym)->attr, (*dsym)->name, NULL)
    2309          353 :           || !gfc_missing_attr (&(*dsym)->attr, NULL)))
    2310              :     return 1;
    2311          418 :   else if ((*dsym) != NULL && result
    2312          483 :       && (!gfc_add_result(&(*dsym)->attr, (*dsym)->name, NULL)
    2313           65 :           || !gfc_missing_attr (&(*dsym)->attr, NULL)))
    2314            0 :     return 1;
    2315              : 
    2316              :   return 0;
    2317              : }
    2318              : 
    2319              : 
    2320              : /************** Component name management ************/
    2321              : 
    2322              : /* Component names of a derived type form their own little namespaces
    2323              :    that are separate from all other spaces.  The space is composed of
    2324              :    a singly linked list of gfc_component structures whose head is
    2325              :    located in the parent symbol.  */
    2326              : 
    2327              : 
    2328              : /* Add a component name to a symbol.  The call fails if the name is
    2329              :    already present.  On success, the component pointer is modified to
    2330              :    point to the additional component structure.  */
    2331              : 
    2332              : bool
    2333       143509 : gfc_add_component (gfc_symbol *sym, const char *name,
    2334              :                    gfc_component **component)
    2335              : {
    2336       143509 :   gfc_component *p, *tail;
    2337              : 
    2338              :   /* Check for existing components with the same name, but not for union
    2339              :      components or containers. Unions and maps are anonymous so they have
    2340              :      unique internal names which will never conflict.
    2341              :      Don't use gfc_find_component here because it calls gfc_use_derived,
    2342              :      but the derived type may not be fully defined yet. */
    2343       143509 :   tail = NULL;
    2344              : 
    2345       464308 :   for (p = sym->components; p; p = p->next)
    2346              :     {
    2347       320803 :       if (strcmp (p->name, name) == 0)
    2348              :         {
    2349            4 :           gfc_error ("Component %qs at %C already declared at %L",
    2350              :                      name, &p->loc);
    2351            4 :           return false;
    2352              :         }
    2353              : 
    2354       320799 :       tail = p;
    2355              :     }
    2356              : 
    2357       143505 :   if (sym->attr.extension
    2358       143505 :         && gfc_find_component (sym->components->ts.u.derived,
    2359              :                                name, true, true, NULL))
    2360              :     {
    2361            2 :       gfc_error ("Component %qs at %C already in the parent type "
    2362            2 :                  "at %L", name, &sym->components->ts.u.derived->declared_at);
    2363            2 :       return false;
    2364              :     }
    2365              : 
    2366              :   /* Allocate a new component.  */
    2367       143503 :   p = gfc_get_component ();
    2368              : 
    2369       143503 :   if (tail == NULL)
    2370        44400 :     sym->components = p;
    2371              :   else
    2372        99103 :     tail->next = p;
    2373              : 
    2374       143503 :   p->name = gfc_get_string ("%s", name);
    2375       143503 :   p->loc = gfc_current_locus;
    2376       143503 :   p->ts.type = BT_UNKNOWN;
    2377              : 
    2378       143503 :   *component = p;
    2379       143503 :   return true;
    2380              : }
    2381              : 
    2382              : 
    2383              : /* Recursive function to switch derived types of all symbol in a
    2384              :    namespace.  */
    2385              : 
    2386              : static void
    2387            0 : switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
    2388              : {
    2389            0 :   gfc_symbol *sym;
    2390              : 
    2391            0 :   if (st == NULL)
    2392            0 :     return;
    2393              : 
    2394            0 :   sym = st->n.sym;
    2395            0 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived == from)
    2396            0 :     sym->ts.u.derived = to;
    2397              : 
    2398            0 :   switch_types (st->left, from, to);
    2399            0 :   switch_types (st->right, from, to);
    2400              : }
    2401              : 
    2402              : 
    2403              : /* This subroutine is called when a derived type is used in order to
    2404              :    make the final determination about which version to use.  The
    2405              :    standard requires that a type be defined before it is 'used', but
    2406              :    such types can appear in IMPLICIT statements before the actual
    2407              :    definition.  'Using' in this context means declaring a variable to
    2408              :    be that type or using the type constructor.
    2409              : 
    2410              :    If a type is used and the components haven't been defined, then we
    2411              :    have to have a derived type in a parent unit.  We find the node in
    2412              :    the other namespace and point the symtree node in this namespace to
    2413              :    that node.  Further reference to this name point to the correct
    2414              :    node.  If we can't find the node in a parent namespace, then we have
    2415              :    an error.
    2416              : 
    2417              :    This subroutine takes a pointer to a symbol node and returns a
    2418              :    pointer to the translated node or NULL for an error.  Usually there
    2419              :    is no translation and we return the node we were passed.  */
    2420              : 
    2421              : gfc_symbol *
    2422       416381 : gfc_use_derived (gfc_symbol *sym)
    2423              : {
    2424       416381 :   gfc_symbol *s;
    2425       416381 :   gfc_typespec *t;
    2426       416381 :   gfc_symtree *st;
    2427       416381 :   int i;
    2428              : 
    2429       416381 :   if (!sym)
    2430              :     return NULL;
    2431              : 
    2432       416377 :   if (sym->attr.unlimited_polymorphic)
    2433              :     return sym;
    2434              : 
    2435       414543 :   if (sym->attr.generic)
    2436            0 :     sym = gfc_find_dt_in_generic (sym);
    2437              : 
    2438       414543 :   if (sym->components != NULL || sym->attr.zero_comp)
    2439              :     return sym;               /* Already defined.  */
    2440              : 
    2441           24 :   if (sym->ns->parent == NULL)
    2442            9 :     goto bad;
    2443              : 
    2444           15 :   if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
    2445              :     {
    2446            0 :       gfc_error ("Symbol %qs at %C is ambiguous", sym->name);
    2447            0 :       return NULL;
    2448              :     }
    2449              : 
    2450           15 :   if (s == NULL || !gfc_fl_struct (s->attr.flavor))
    2451           15 :     goto bad;
    2452              : 
    2453              :   /* Get rid of symbol sym, translating all references to s.  */
    2454            0 :   for (i = 0; i < GFC_LETTERS; i++)
    2455              :     {
    2456            0 :       t = &sym->ns->default_type[i];
    2457            0 :       if (t->u.derived == sym)
    2458            0 :         t->u.derived = s;
    2459              :     }
    2460              : 
    2461            0 :   st = gfc_find_symtree (sym->ns->sym_root, sym->name);
    2462            0 :   st->n.sym = s;
    2463              : 
    2464            0 :   s->refs++;
    2465              : 
    2466              :   /* Unlink from list of modified symbols.  */
    2467            0 :   gfc_commit_symbol (sym);
    2468              : 
    2469            0 :   switch_types (sym->ns->sym_root, sym, s);
    2470              : 
    2471              :   /* TODO: Also have to replace sym -> s in other lists like
    2472              :      namelists, common lists and interface lists.  */
    2473            0 :   gfc_free_symbol (sym);
    2474              : 
    2475            0 :   return s;
    2476              : 
    2477           24 : bad:
    2478           24 :   gfc_error ("Derived type %qs at %C is being used before it is defined",
    2479              :              sym->name);
    2480           24 :   return NULL;
    2481              : }
    2482              : 
    2483              : 
    2484              : /* Find all derived types in the uppermost namespace that have a component
    2485              :    a component called name and stash them in the assoc field of an
    2486              :    associate name variable.
    2487              :    This is used to infer the derived type of an associate name, whose selector
    2488              :    is a sibling derived type function that has not yet been parsed. Either
    2489              :    the derived type is use associated in both contained and sibling procedures
    2490              :    or it appears in the uppermost namespace.  */
    2491              : 
    2492              : static int cts = 0;
    2493              : static void
    2494        15120 : find_derived_types (gfc_symbol *sym, gfc_symtree *st, const char *name,
    2495              :                     bool contained, bool stash)
    2496              : {
    2497              :   /* Visit in order so that the result does not depend on the tree shape.  */
    2498        29017 :   if (st->left)
    2499        12680 :     find_derived_types (sym, st->left, name, contained, stash);
    2500              : 
    2501        29017 :   if (st->n.sym && st->n.sym->attr.flavor == FL_DERIVED
    2502         7558 :       && !st->n.sym->attr.is_class
    2503         5922 :       && ((contained && st->n.sym->attr.use_assoc) || !contained)
    2504         5906 :       && !st->n.sym->attr.vtype
    2505        32621 :       && (gfc_find_component (st->n.sym, name, true, true, NULL)
    2506         2050 :           || (st->n.sym->f2k_derived
    2507         2050 :               && gfc_find_typebound_proc (st->n.sym, NULL, name, true,
    2508              :                                          NULL))))
    2509              :     {
    2510              :       /* Do the stashing, if required.  */
    2511         1610 :       cts++;
    2512         1610 :       if (stash)
    2513              :         {
    2514         1538 :           if (sym->assoc->derived_types)
    2515          771 :             st->n.sym->dt_next = sym->assoc->derived_types;
    2516         1538 :           sym->assoc->derived_types = st->n.sym;
    2517              :         }
    2518              :     }
    2519              : 
    2520        29017 :   if (st->right)
    2521              :     find_derived_types (sym, st->right, name, contained, stash);
    2522        15120 : }
    2523              : 
    2524              : int
    2525         1478 : gfc_find_derived_types (gfc_symbol *sym, gfc_namespace *ns,
    2526              :                         const char *name, bool stash)
    2527              : {
    2528         1478 :   gfc_namespace *encompassing = NULL;
    2529         1478 :   if (sym->assoc == NULL)
    2530              :     return 0;
    2531              : 
    2532         1476 :   cts = 0;
    2533         4032 :   while (ns->parent)
    2534              :     {
    2535         2556 :       if (!ns->parent->parent && ns->proc_name
    2536         1476 :           && (ns->proc_name->attr.function || ns->proc_name->attr.subroutine))
    2537         2556 :         encompassing = ns;
    2538         2556 :       ns = ns->parent;
    2539              :     }
    2540              : 
    2541              :   /* Search the top level namespace first.  */
    2542         1476 :   find_derived_types (sym, ns->sym_root, name, false, stash);
    2543              : 
    2544              :   /* Then the encompassing namespace.  */
    2545         1476 :   if (encompassing && encompassing != ns)
    2546          964 :     find_derived_types (sym, encompassing->sym_root, name, true, stash);
    2547              : 
    2548         1476 :   return cts;
    2549              : }
    2550              : 
    2551              : /* Find the component with the given name in the union type symbol.
    2552              :    If ref is not NULL it will be set to the chain of components through which
    2553              :    the component can actually be accessed. This is necessary for unions because
    2554              :    intermediate structures may be maps, nested structures, or other unions,
    2555              :    all of which may (or must) be 'anonymous' to user code.  */
    2556              : 
    2557              : static gfc_component *
    2558         2192 : find_union_component (gfc_symbol *un, const char *name,
    2559              :                       bool noaccess, gfc_ref **ref)
    2560              : {
    2561         2192 :   gfc_component *m, *check;
    2562         2192 :   gfc_ref *sref, *tmp;
    2563              : 
    2564         3983 :   for (m = un->components; m; m = m->next)
    2565              :     {
    2566         3483 :       check = gfc_find_component (m->ts.u.derived, name, noaccess, true, &tmp);
    2567         3483 :       if (check == NULL)
    2568         1791 :         continue;
    2569              : 
    2570              :       /* Found component somewhere in m; chain the refs together.  */
    2571         1692 :       if (ref)
    2572              :         {
    2573              :           /* Map ref. */
    2574         1692 :           sref = gfc_get_ref ();
    2575         1692 :           sref->type = REF_COMPONENT;
    2576         1692 :           sref->u.c.component = m;
    2577         1692 :           sref->u.c.sym = m->ts.u.derived;
    2578         1692 :           sref->next = tmp;
    2579              : 
    2580         1692 :           *ref = sref;
    2581              :         }
    2582              :       /* Other checks (such as access) were done in the recursive calls.  */
    2583              :       return check;
    2584              :     }
    2585              :   return NULL;
    2586              : }
    2587              : 
    2588              : 
    2589              : /* Recursively append candidate COMPONENT structures to CANDIDATES.  Store
    2590              :    the number of total candidates in CANDIDATES_LEN.  */
    2591              : 
    2592              : static void
    2593           34 : lookup_component_fuzzy_find_candidates (gfc_component *component,
    2594              :                                         char **&candidates,
    2595              :                                         size_t &candidates_len)
    2596              : {
    2597           81 :   for (gfc_component *p = component; p; p = p->next)
    2598           47 :     vec_push (candidates, candidates_len, p->name);
    2599           34 : }
    2600              : 
    2601              : 
    2602              : /* Lookup component MEMBER fuzzily, taking names in COMPONENT into account.  */
    2603              : 
    2604              : static const char*
    2605           34 : lookup_component_fuzzy (const char *member, gfc_component *component)
    2606              : {
    2607           34 :   char **candidates = NULL;
    2608           34 :   size_t candidates_len = 0;
    2609           34 :   lookup_component_fuzzy_find_candidates (component, candidates,
    2610              :                                           candidates_len);
    2611           34 :   return gfc_closest_fuzzy_match (member, candidates);
    2612              : }
    2613              : 
    2614              : 
    2615              : /* Given a derived type node and a component name, try to locate the
    2616              :    component structure.  Returns the NULL pointer if the component is
    2617              :    not found or the components are private.  If noaccess is set, no access
    2618              :    checks are done.  If silent is set, an error will not be generated if
    2619              :    the component cannot be found or accessed.
    2620              : 
    2621              :    If ref is not NULL, *ref is set to represent the chain of components
    2622              :    required to get to the ultimate component.
    2623              : 
    2624              :    If the component is simply a direct subcomponent, or is inherited from a
    2625              :    parent derived type in the given derived type, this is a single ref with its
    2626              :    component set to the returned component.
    2627              : 
    2628              :    Otherwise, *ref is constructed as a chain of subcomponents. This occurs
    2629              :    when the component is found through an implicit chain of nested union and
    2630              :    map components. Unions and maps are "anonymous" substructures in FORTRAN
    2631              :    which cannot be explicitly referenced, but the reference chain must be
    2632              :    considered as in C for backend translation to correctly compute layouts.
    2633              :    (For example, x.a may refer to x->(UNION)->(MAP)->(UNION)->(MAP)->a).  */
    2634              : 
    2635              : gfc_component *
    2636       386108 : gfc_find_component (gfc_symbol *sym, const char *name,
    2637              :                     bool noaccess, bool silent, gfc_ref **ref)
    2638              : {
    2639       386108 :   gfc_component *p, *check;
    2640       386108 :   gfc_ref *sref = NULL, *tmp = NULL;
    2641              : 
    2642       386108 :   if (name == NULL || sym == NULL)
    2643              :     return NULL;
    2644              : 
    2645       381113 :   if (sym->attr.flavor == FL_DERIVED)
    2646       372350 :     sym = gfc_use_derived (sym);
    2647              :   else
    2648         8763 :     gcc_assert (gfc_fl_struct (sym->attr.flavor));
    2649              : 
    2650       372350 :   if (sym == NULL)
    2651              :     return NULL;
    2652              : 
    2653              :   /* Handle UNIONs specially - mutually recursive with gfc_find_component. */
    2654       381111 :   if (sym->attr.flavor == FL_UNION)
    2655          500 :     return find_union_component (sym, name, noaccess, ref);
    2656              : 
    2657       380611 :   if (ref) *ref = NULL;
    2658       826947 :   for (p = sym->components; p; p = p->next)
    2659              :     {
    2660              :       /* Nest search into union's maps. */
    2661       785273 :       if (p->ts.type == BT_UNION)
    2662              :         {
    2663         1692 :           check = find_union_component (p->ts.u.derived, name, noaccess, &tmp);
    2664         1692 :           if (check != NULL)
    2665              :             {
    2666              :               /* Union ref. */
    2667         1692 :               if (ref)
    2668              :                 {
    2669         1252 :                   sref = gfc_get_ref ();
    2670         1252 :                   sref->type = REF_COMPONENT;
    2671         1252 :                   sref->u.c.component = p;
    2672         1252 :                   sref->u.c.sym = p->ts.u.derived;
    2673         1252 :                   sref->next = tmp;
    2674         1252 :                   *ref = sref;
    2675              :                 }
    2676              :               return check;
    2677              :             }
    2678              :         }
    2679       783581 :       else if (strcmp (p->name, name) == 0)
    2680              :         break;
    2681              : 
    2682       446336 :       continue;
    2683              :     }
    2684              : 
    2685       378919 :   if (p && sym->attr.use_assoc && !noaccess)
    2686              :     {
    2687        61502 :       bool is_parent_comp = sym->attr.extension && (p == sym->components);
    2688        61502 :       if (p->attr.access == ACCESS_PRIVATE ||
    2689              :           (p->attr.access != ACCESS_PUBLIC
    2690        60602 :            && sym->component_access == ACCESS_PRIVATE
    2691            8 :            && !is_parent_comp))
    2692              :         {
    2693           16 :           if (!silent)
    2694           15 :             gfc_error ("Component %qs at %C is a PRIVATE component of %qs",
    2695              :                        name, sym->name);
    2696              :           return NULL;
    2697              :         }
    2698              :     }
    2699              : 
    2700              :   if (p == NULL
    2701        41674 :         && sym->attr.extension
    2702        28329 :         && sym->components->ts.type == BT_DERIVED)
    2703              :     {
    2704        28329 :       p = gfc_find_component (sym->components->ts.u.derived, name,
    2705              :                               noaccess, silent, ref);
    2706              :       /* Do not overwrite the error.  */
    2707        28329 :       if (p == NULL)
    2708              :         return p;
    2709              :     }
    2710              : 
    2711       378021 :   if (p == NULL && !silent)
    2712              :     {
    2713           34 :       const char *guessed = lookup_component_fuzzy (name, sym->components);
    2714           34 :       if (guessed)
    2715           10 :         gfc_error ("%qs at %C is not a member of the %qs structure"
    2716              :                    "; did you mean %qs?",
    2717              :                    name, sym->name, guessed);
    2718              :       else
    2719           24 :         gfc_error ("%qs at %C is not a member of the %qs structure",
    2720              :                    name, sym->name);
    2721              :     }
    2722              : 
    2723              :   /* Component was found; build the ultimate component reference. */
    2724       378021 :   if (p != NULL && ref)
    2725              :     {
    2726       290376 :       tmp = gfc_get_ref ();
    2727       290376 :       tmp->type = REF_COMPONENT;
    2728       290376 :       tmp->u.c.component = p;
    2729       290376 :       tmp->u.c.sym = sym;
    2730              :       /* Link the final component ref to the end of the chain of subrefs. */
    2731       290376 :       if (sref)
    2732              :         {
    2733              :           *ref = sref;
    2734              :           for (; sref->next; sref = sref->next)
    2735              :             ;
    2736              :           sref->next = tmp;
    2737              :         }
    2738              :       else
    2739       290376 :         *ref = tmp;
    2740              :     }
    2741              : 
    2742              :   return p;
    2743       446336 : }
    2744              : 
    2745              : 
    2746              : /* Given a symbol, free all of the component structures and everything
    2747              :    they point to.  */
    2748              : 
    2749              : void
    2750       300261 : gfc_free_component (gfc_component *p)
    2751              : {
    2752       300261 :   gfc_free_array_spec (p->as);
    2753       300261 :   gfc_free_expr (p->initializer);
    2754       300261 :   if (p->kind_expr)
    2755          684 :     gfc_free_expr (p->kind_expr);
    2756       300261 :   if (p->param_list)
    2757          330 :     gfc_free_actual_arglist (p->param_list);
    2758       300261 :   free (p->tb);
    2759       300261 :   p->tb = NULL;
    2760       300261 :   free (p);
    2761       300261 : }
    2762              : 
    2763              : 
    2764              : static void
    2765      6497175 : free_components (gfc_component *p)
    2766              : {
    2767      6497175 :   gfc_component *q;
    2768              : 
    2769      6797433 :   for (; p; p = q)
    2770              :     {
    2771       300258 :       q = p->next;
    2772       300258 :       gfc_free_component (p);
    2773              :     }
    2774            0 : }
    2775              : 
    2776              : 
    2777              : /******************** Statement label management ********************/
    2778              : 
    2779              : /* Comparison function for statement labels, used for managing the
    2780              :    binary tree.  */
    2781              : 
    2782              : static int
    2783         7649 : compare_st_labels (void *a1, void *b1)
    2784              : {
    2785         7649 :   gfc_st_label *a = (gfc_st_label *) a1;
    2786         7649 :   gfc_st_label *b = (gfc_st_label *) b1;
    2787              : 
    2788         7649 :   if (a->omp_region == b->omp_region)
    2789         7586 :     return b->value - a->value;
    2790              :   else
    2791           63 :     return b->omp_region - a->omp_region;
    2792              : }
    2793              : 
    2794              : 
    2795              : /* Free a single gfc_st_label structure, making sure the tree is not
    2796              :    messed up.  This function is called only when some parse error
    2797              :    occurs.  */
    2798              : 
    2799              : void
    2800            3 : gfc_free_st_label (gfc_st_label *label)
    2801              : {
    2802              : 
    2803            3 :   if (label == NULL)
    2804              :     return;
    2805              : 
    2806            3 :   gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
    2807              : 
    2808            3 :   if (label->format != NULL)
    2809            0 :     gfc_free_expr (label->format);
    2810              : 
    2811            3 :   free (label);
    2812              : }
    2813              : 
    2814              : 
    2815              : /* Free a whole tree of gfc_st_label structures.  */
    2816              : 
    2817              : static void
    2818       568559 : free_st_labels (gfc_st_label *label)
    2819              : {
    2820              : 
    2821       568559 :   if (label == NULL)
    2822              :     return;
    2823              : 
    2824         4783 :   free_st_labels (label->left);
    2825         4783 :   free_st_labels (label->right);
    2826              : 
    2827         4783 :   if (label->format != NULL)
    2828         1026 :     gfc_free_expr (label->format);
    2829         4783 :   free (label);
    2830              : }
    2831              : 
    2832              : 
    2833              : /* Given a label number, search for and return a pointer to the label
    2834              :    structure, creating it if it does not exist.  */
    2835              : 
    2836              : gfc_st_label *
    2837        13739 : gfc_get_st_label (int labelno)
    2838              : {
    2839        13739 :   gfc_st_label *lp;
    2840        13739 :   gfc_namespace *ns;
    2841        13739 :   int omp_region = gfc_omp_metadirective_region_stack.last ();
    2842              : 
    2843        13739 :   if (gfc_current_state () == COMP_DERIVED)
    2844            3 :     ns = gfc_current_block ()->f2k_derived;
    2845              :   else
    2846              :     {
    2847              :       /* Find the namespace of the scoping unit:
    2848              :          If we're in a BLOCK construct, jump to the parent namespace.  */
    2849        13736 :       ns = gfc_current_ns;
    2850        13771 :       while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
    2851           35 :         ns = ns->parent;
    2852              :     }
    2853              : 
    2854              :   /* First see if the label is already in this namespace.  */
    2855        13739 :   gcc_checking_assert (gfc_omp_metadirective_region_stack.length () > 0);
    2856        18601 :   for (int omp_region_idx = gfc_omp_metadirective_region_stack.length () - 1;
    2857        18601 :        omp_region_idx >= 0; omp_region_idx--)
    2858              :     {
    2859        13815 :       int omp_region2 = gfc_omp_metadirective_region_stack[omp_region_idx];
    2860        13815 :       lp = ns->st_labels;
    2861        31569 :       while (lp)
    2862              :         {
    2863        26707 :           if (lp->omp_region == omp_region2)
    2864              :             {
    2865        26449 :               if (lp->value == labelno)
    2866              :                 return lp;
    2867        17496 :               if (lp->value < labelno)
    2868        12649 :                 lp = lp->left;
    2869              :               else
    2870         4847 :                 lp = lp->right;
    2871              :             }
    2872          258 :           else if (lp->omp_region < omp_region2)
    2873          177 :             lp = lp->left;
    2874              :           else
    2875           81 :             lp = lp->right;
    2876              :         }
    2877              :     }
    2878              : 
    2879         4786 :   lp = XCNEW (gfc_st_label);
    2880              : 
    2881         4786 :   lp->value = labelno;
    2882         4786 :   lp->defined = ST_LABEL_UNKNOWN;
    2883         4786 :   lp->referenced = ST_LABEL_UNKNOWN;
    2884         4786 :   lp->ns = ns;
    2885         4786 :   lp->omp_region = omp_region;
    2886              : 
    2887         4786 :   gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
    2888              : 
    2889         4786 :   return lp;
    2890              : }
    2891              : 
    2892              : /* Rebind a statement label to a new OpenMP region. If a label with the same
    2893              :    value already exists in the new region, update it and return it. Otherwise,
    2894              :    move the label to the new region.  */
    2895              : 
    2896              : gfc_st_label *
    2897           44 : gfc_rebind_label (gfc_st_label *label, int new_omp_region)
    2898              : {
    2899           44 :   gfc_st_label *lp = label->ns->st_labels;
    2900           44 :   int labelno = label->value;
    2901              : 
    2902          106 :   while (lp)
    2903              :     {
    2904           97 :       if (lp->omp_region == new_omp_region)
    2905              :         {
    2906           38 :           if (lp->value == labelno)
    2907              :             {
    2908           35 :               if (lp == label)
    2909              :                 return label;
    2910            0 :               if (lp->defined == ST_LABEL_UNKNOWN
    2911            0 :                   && label->defined != ST_LABEL_UNKNOWN)
    2912            0 :                 lp->defined = label->defined;
    2913            0 :               if (lp->referenced == ST_LABEL_UNKNOWN
    2914            0 :                   && label->referenced != ST_LABEL_UNKNOWN)
    2915            0 :                 lp->referenced = label->referenced;
    2916            0 :               if (lp->format == NULL && label->format != NULL)
    2917            0 :                 lp->format = label->format;
    2918            0 :               gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
    2919            0 :               return lp;
    2920              :             }
    2921            3 :           if (lp->value < labelno)
    2922            2 :             lp = lp->left;
    2923              :           else
    2924            1 :             lp = lp->right;
    2925              :         }
    2926           59 :       else if (lp->omp_region < new_omp_region)
    2927           29 :         lp = lp->left;
    2928              :       else
    2929           30 :         lp = lp->right;
    2930              :     }
    2931              : 
    2932            9 :   gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
    2933            9 :   label->left = nullptr;
    2934            9 :   label->right = nullptr;
    2935            9 :   label->omp_region = new_omp_region;
    2936            9 :   gfc_insert_bbt (&label->ns->st_labels, label, compare_st_labels);
    2937            9 :   return label;
    2938              : }
    2939              : 
    2940              : /* Called when a statement with a statement label is about to be
    2941              :    accepted.  We add the label to the list of the current namespace,
    2942              :    making sure it hasn't been defined previously and referenced
    2943              :    correctly.  */
    2944              : 
    2945              : void
    2946         4752 : gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
    2947              : {
    2948         4752 :   int labelno;
    2949              : 
    2950         4752 :   labelno = lp->value;
    2951              : 
    2952         4752 :   if (lp->defined != ST_LABEL_UNKNOWN && !gfc_in_omp_metadirective_body)
    2953            2 :     gfc_error ("Duplicate statement label %d at %L and %L", labelno,
    2954              :                &lp->where, label_locus);
    2955              :   else
    2956              :     {
    2957         4750 :       lp->where = *label_locus;
    2958              : 
    2959         4750 :       switch (type)
    2960              :         {
    2961         1029 :         case ST_LABEL_FORMAT:
    2962         1029 :           if (lp->referenced == ST_LABEL_TARGET
    2963         1029 :               || lp->referenced == ST_LABEL_DO_TARGET)
    2964            0 :             gfc_error ("Label %d at %C already referenced as branch target",
    2965              :                        labelno);
    2966              :           else
    2967         1029 :             lp->defined = ST_LABEL_FORMAT;
    2968              : 
    2969              :           break;
    2970              : 
    2971         3714 :         case ST_LABEL_TARGET:
    2972         3714 :         case ST_LABEL_DO_TARGET:
    2973         3714 :           if (lp->referenced == ST_LABEL_FORMAT)
    2974            2 :             gfc_error ("Label %d at %C already referenced as a format label",
    2975              :                        labelno);
    2976              :           else
    2977         3712 :             lp->defined = type;
    2978              : 
    2979         1721 :           if (lp->referenced == ST_LABEL_DO_TARGET && type != ST_LABEL_DO_TARGET
    2980         3846 :               && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
    2981              :                                   "DO termination statement which is not END DO"
    2982              :                                   " or CONTINUE with label %d at %C", labelno))
    2983              :             return;
    2984              :           break;
    2985              : 
    2986            7 :         default:
    2987            7 :           lp->defined = ST_LABEL_BAD_TARGET;
    2988            7 :           lp->referenced = ST_LABEL_BAD_TARGET;
    2989              :         }
    2990              :     }
    2991              : }
    2992              : 
    2993              : 
    2994              : /* Reference a label.  Given a label and its type, see if that
    2995              :    reference is consistent with what is known about that label,
    2996              :    updating the unknown state.  Returns false if something goes
    2997              :    wrong.  */
    2998              : 
    2999              : bool
    3000        18373 : gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
    3001              : {
    3002        18373 :   gfc_sl_type label_type;
    3003        18373 :   int labelno;
    3004        18373 :   bool rc;
    3005              : 
    3006        18373 :   if (lp == NULL)
    3007              :     return true;
    3008              : 
    3009         7714 :   labelno = lp->value;
    3010              : 
    3011         7714 :   if (lp->defined != ST_LABEL_UNKNOWN)
    3012              :     label_type = lp->defined;
    3013              :   else
    3014              :     {
    3015         6054 :       label_type = lp->referenced;
    3016         6054 :       lp->where = gfc_current_locus;
    3017              :     }
    3018              : 
    3019         7714 :   if (label_type == ST_LABEL_FORMAT
    3020         1145 :       && (type == ST_LABEL_TARGET || type == ST_LABEL_DO_TARGET))
    3021              :     {
    3022            0 :       gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
    3023            0 :       rc = false;
    3024            0 :       goto done;
    3025              :     }
    3026              : 
    3027         7714 :   if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_DO_TARGET
    3028         7714 :        || label_type == ST_LABEL_BAD_TARGET)
    3029         2441 :       && type == ST_LABEL_FORMAT)
    3030              :     {
    3031            5 :       gfc_error ("Label %d at %C previously used as branch target", labelno);
    3032            5 :       rc = false;
    3033            5 :       goto done;
    3034              :     }
    3035              : 
    3036          623 :   if (lp->referenced == ST_LABEL_DO_TARGET && type == ST_LABEL_DO_TARGET
    3037          544 :       && !gfc_in_omp_metadirective_body
    3038         8251 :       && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
    3039              :                           "Shared DO termination label %d at %C", labelno))
    3040              :     return false;
    3041              : 
    3042         7709 :   if (type == ST_LABEL_DO_TARGET
    3043         7709 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "Labeled DO statement "
    3044              :                           "at %L", &gfc_current_locus))
    3045              :     return false;
    3046              : 
    3047         7709 :   if (lp->referenced != ST_LABEL_DO_TARGET)
    3048         7086 :     lp->referenced = type;
    3049              :   rc = true;
    3050              : 
    3051        18373 : done:
    3052              :   return rc;
    3053              : }
    3054              : 
    3055              : 
    3056              : /************** Symbol table management subroutines ****************/
    3057              : 
    3058              : /* Basic details: Fortran 95 requires a potentially unlimited number
    3059              :    of distinct namespaces when compiling a program unit.  This case
    3060              :    occurs during a compilation of internal subprograms because all of
    3061              :    the internal subprograms must be read before we can start
    3062              :    generating code for the host.
    3063              : 
    3064              :    Given the tricky nature of the Fortran grammar, we must be able to
    3065              :    undo changes made to a symbol table if the current interpretation
    3066              :    of a statement is found to be incorrect.  Whenever a symbol is
    3067              :    looked up, we make a copy of it and link to it.  All of these
    3068              :    symbols are kept in a vector so that we can commit or
    3069              :    undo the changes at a later time.
    3070              : 
    3071              :    A symtree may point to a symbol node outside of its namespace.  In
    3072              :    this case, that symbol has been used as a host associated variable
    3073              :    at some previous time.  */
    3074              : 
    3075              : /* Allocate a new namespace structure.  Copies the implicit types from
    3076              :    PARENT if PARENT_TYPES is set.  */
    3077              : 
    3078              : gfc_namespace *
    3079       588882 : gfc_get_namespace (gfc_namespace *parent, int parent_types)
    3080              : {
    3081       588882 :   gfc_namespace *ns;
    3082       588882 :   gfc_typespec *ts;
    3083       588882 :   int in;
    3084       588882 :   int i;
    3085              : 
    3086       588882 :   ns = XCNEW (gfc_namespace);
    3087       588882 :   ns->sym_root = NULL;
    3088       588882 :   ns->uop_root = NULL;
    3089       588882 :   ns->tb_sym_root = NULL;
    3090       588882 :   ns->finalizers = NULL;
    3091       588882 :   ns->default_access = ACCESS_UNKNOWN;
    3092       588882 :   ns->parent = parent;
    3093              : 
    3094     17077578 :   for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
    3095              :     {
    3096     16488696 :       ns->operator_access[in] = ACCESS_UNKNOWN;
    3097     16488696 :       ns->tb_op[in] = NULL;
    3098              :     }
    3099              : 
    3100              :   /* Initialize default implicit types.  */
    3101     15899814 :   for (i = 'a'; i <= 'z'; i++)
    3102              :     {
    3103     15310932 :       ns->set_flag[i - 'a'] = 0;
    3104     15310932 :       ts = &ns->default_type[i - 'a'];
    3105              : 
    3106     15310932 :       if (parent_types && ns->parent != NULL)
    3107              :         {
    3108              :           /* Copy parent settings.  */
    3109      1850238 :           *ts = ns->parent->default_type[i - 'a'];
    3110      1850238 :           continue;
    3111              :         }
    3112              : 
    3113     13460694 :       if (flag_implicit_none != 0)
    3114              :         {
    3115       111722 :           gfc_clear_ts (ts);
    3116       111722 :           continue;
    3117              :         }
    3118              : 
    3119     13348972 :       if ('i' <= i && i <= 'n')
    3120              :         {
    3121      3080532 :           ts->type = BT_INTEGER;
    3122      3080532 :           ts->kind = gfc_default_integer_kind;
    3123              :         }
    3124              :       else
    3125              :         {
    3126     10268440 :           ts->type = BT_REAL;
    3127     10268440 :           ts->kind = gfc_default_real_kind;
    3128              :         }
    3129              :     }
    3130              : 
    3131       588882 :   ns->refs = 1;
    3132              : 
    3133       588882 :   return ns;
    3134              : }
    3135              : 
    3136              : 
    3137              : /* Comparison function for symtree nodes.  */
    3138              : 
    3139              : static int
    3140     36327911 : compare_symtree (void *_st1, void *_st2)
    3141              : {
    3142     36327911 :   gfc_symtree *st1, *st2;
    3143              : 
    3144     36327911 :   st1 = (gfc_symtree *) _st1;
    3145     36327911 :   st2 = (gfc_symtree *) _st2;
    3146              : 
    3147     36327911 :   return strcmp (st1->name, st2->name);
    3148              : }
    3149              : 
    3150              : 
    3151              : /* Allocate a new symtree node and associate it with the new symbol.  */
    3152              : 
    3153              : gfc_symtree *
    3154      6689661 : gfc_new_symtree (gfc_symtree **root, const char *name)
    3155              : {
    3156      6689661 :   gfc_symtree *st;
    3157              : 
    3158      6689661 :   st = XCNEW (gfc_symtree);
    3159      6689661 :   st->name = gfc_get_string ("%s", name);
    3160              : 
    3161      6689661 :   gfc_insert_bbt (root, st, compare_symtree);
    3162      6689661 :   return st;
    3163              : }
    3164              : 
    3165              : 
    3166              : /* Delete a symbol from the tree.  Does not free the symbol itself!  */
    3167              : 
    3168              : void
    3169      4351139 : gfc_delete_symtree (gfc_symtree **root, const char *name)
    3170              : {
    3171      4351139 :   gfc_symtree st, *st0;
    3172      4351139 :   const char *p;
    3173              : 
    3174              :   /* Submodules are marked as mod.submod.  When freeing a submodule
    3175              :      symbol, the symtree only has "submod", so adjust that here.  */
    3176              : 
    3177      4351139 :   p = strrchr(name, '.');
    3178      4351139 :   if (p)
    3179            0 :     p++;
    3180              :   else
    3181              :     p = name;
    3182              : 
    3183      4351139 :   st.name = gfc_get_string ("%s", p);
    3184      4351139 :   st0 = (gfc_symtree *) gfc_delete_bbt (root, &st, compare_symtree);
    3185              : 
    3186      4351139 :   free (st0);
    3187      4351139 : }
    3188              : 
    3189              : 
    3190              : /* Given a root symtree node and a name, try to find the symbol within
    3191              :    the namespace.  Returns NULL if the symbol is not found.  */
    3192              : 
    3193              : gfc_symtree *
    3194     32079479 : gfc_find_symtree (gfc_symtree *st, const char *name)
    3195              : {
    3196     32079479 :   int c;
    3197              : 
    3198    139153386 :   while (st != NULL)
    3199              :     {
    3200    119773403 :       c = strcmp (name, st->name);
    3201    119773403 :       if (c == 0)
    3202              :         return st;
    3203              : 
    3204    107073907 :       st = (c < 0) ? st->left : st->right;
    3205              :     }
    3206              : 
    3207              :   return NULL;
    3208              : }
    3209              : 
    3210              : 
    3211              : /* Return a symtree node with a name that is guaranteed to be unique
    3212              :    within the namespace and corresponds to an illegal fortran name.  */
    3213              : 
    3214              : gfc_symtree *
    3215       689199 : gfc_get_unique_symtree (gfc_namespace *ns)
    3216              : {
    3217       689199 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    3218       689199 :   static int serial = 0;
    3219              : 
    3220       689199 :   sprintf (name, "@%d", serial++);
    3221       689199 :   if (ns)
    3222       689042 :     return gfc_new_symtree (&ns->sym_root, name);
    3223              :   else
    3224              :     {
    3225              :       /* Some uses need a symtree that is cleaned up locally.  */
    3226          157 :       gfc_symtree *st = XCNEW (gfc_symtree);
    3227          157 :       st->name = gfc_get_string ("%s", name);
    3228          157 :       return st;
    3229              :     }
    3230              : }
    3231              : 
    3232              : 
    3233              : /* Given a name find a user operator node, creating it if it doesn't
    3234              :    exist.  These are much simpler than symbols because they can't be
    3235              :    ambiguous with one another.  */
    3236              : 
    3237              : gfc_user_op *
    3238         1004 : gfc_get_uop (const char *name)
    3239              : {
    3240         1004 :   gfc_user_op *uop;
    3241         1004 :   gfc_symtree *st;
    3242         1004 :   gfc_namespace *ns = gfc_current_ns;
    3243              : 
    3244         1004 :   if (ns->omp_udr_ns)
    3245           35 :     ns = ns->parent;
    3246         1004 :   st = gfc_find_symtree (ns->uop_root, name);
    3247         1004 :   if (st != NULL)
    3248          601 :     return st->n.uop;
    3249              : 
    3250          403 :   st = gfc_new_symtree (&ns->uop_root, name);
    3251              : 
    3252          403 :   uop = st->n.uop = XCNEW (gfc_user_op);
    3253          403 :   uop->name = gfc_get_string ("%s", name);
    3254          403 :   uop->access = ACCESS_UNKNOWN;
    3255          403 :   uop->ns = ns;
    3256              : 
    3257          403 :   return uop;
    3258              : }
    3259              : 
    3260              : 
    3261              : /* Given a name find the user operator node.  Returns NULL if it does
    3262              :    not exist.  */
    3263              : 
    3264              : gfc_user_op *
    3265         6932 : gfc_find_uop (const char *name, gfc_namespace *ns)
    3266              : {
    3267         6932 :   gfc_symtree *st;
    3268              : 
    3269         6932 :   if (ns == NULL)
    3270           18 :     ns = gfc_current_ns;
    3271              : 
    3272         6932 :   st = gfc_find_symtree (ns->uop_root, name);
    3273         6932 :   return (st == NULL) ? NULL : st->n.uop;
    3274              : }
    3275              : 
    3276              : 
    3277              : /* Update a symbol's common_block field, and take care of the associated
    3278              :    memory management.  */
    3279              : 
    3280              : static void
    3281      7981639 : set_symbol_common_block (gfc_symbol *sym, gfc_common_head *common_block)
    3282              : {
    3283      7981639 :   if (sym->common_block == common_block)
    3284              :     return;
    3285              : 
    3286         5857 :   if (sym->common_block && sym->common_block->name[0] != '\0')
    3287              :     {
    3288         5571 :       sym->common_block->refs--;
    3289         5571 :       if (sym->common_block->refs == 0)
    3290         1804 :         free (sym->common_block);
    3291              :     }
    3292         5857 :   sym->common_block = common_block;
    3293              : }
    3294              : 
    3295              : 
    3296              : /* Remove a gfc_symbol structure and everything it points to.  */
    3297              : 
    3298              : void
    3299      6653676 : gfc_free_symbol (gfc_symbol *&sym)
    3300              : {
    3301              : 
    3302      6653676 :   if (sym == NULL)
    3303              :     return;
    3304              : 
    3305      6497175 :   gfc_free_array_spec (sym->as);
    3306              : 
    3307      6497175 :   free_components (sym->components);
    3308              : 
    3309      6497175 :   gfc_free_expr (sym->value);
    3310              : 
    3311      6497175 :   gfc_free_namelist (sym->namelist);
    3312              : 
    3313      6497175 :   if (sym->ns != sym->formal_ns)
    3314      6445423 :     gfc_free_namespace (sym->formal_ns);
    3315              : 
    3316      6497175 :   if (!sym->attr.generic_copy)
    3317      6497175 :     gfc_free_interface (sym->generic);
    3318              : 
    3319      6497175 :   gfc_free_formal_arglist (sym->formal);
    3320              : 
    3321              :   /* The pdt_type f2k_derived namespaces are copies of that of the pdt_template
    3322              :      and are only made if there are finalizers. The complete list of finalizers
    3323              :      is kept by the pdt_template and are freed with its f2k_derived.  */
    3324      6497175 :   if (!sym->attr.pdt_type)
    3325      6496918 :     gfc_free_namespace (sym->f2k_derived);
    3326          257 :   else if (sym->f2k_derived && sym->f2k_derived->finalizers)
    3327              :     {
    3328           12 :       gfc_finalizer *p, *q = NULL;
    3329           12 :       for (p = sym->f2k_derived->finalizers; p; p = q)
    3330              :         {
    3331            6 :           q = p->next;
    3332            6 :           free (p);
    3333              :         }
    3334            6 :       free (sym->f2k_derived);
    3335              :     }
    3336              : 
    3337      6497175 :   set_symbol_common_block (sym, NULL);
    3338              : 
    3339      6497175 :   if (sym->param_list)
    3340         2327 :     gfc_free_actual_arglist (sym->param_list);
    3341              : 
    3342      6497175 :   free (sym);
    3343      6497175 :   sym = NULL;
    3344              : }
    3345              : 
    3346              : 
    3347              : /* Returns true if the symbol SYM has, through its FORMAL_NS field, a reference
    3348              :    to itself which should be eliminated for the symbol memory to be released
    3349              :    via normal reference counting.
    3350              : 
    3351              :    The implementation is crucial as it controls the proper release of symbols,
    3352              :    especially (contained) procedure symbols, which can represent a lot of memory
    3353              :    through the namespace of their body.
    3354              : 
    3355              :    We try to avoid freeing too much memory (causing dangling pointers), to not
    3356              :    leak too much (wasting memory), and to avoid expensive walks of the symbol
    3357              :    tree (which would be the correct way to check for a cycle).  */
    3358              : 
    3359              : bool
    3360      6562525 : cyclic_reference_break_needed (gfc_symbol *sym)
    3361              : {
    3362              :   /* Normal symbols don't reference themselves.  */
    3363      6562525 :   if (sym->formal_ns == nullptr)
    3364              :     return false;
    3365              : 
    3366              :   /* Procedures at the root of the file do have a self reference, but they don't
    3367              :      have a reference in a parent namespace preventing the release of the
    3368              :      procedure namespace, so they can use the normal reference counting.  */
    3369       328298 :   if (sym->formal_ns == sym->ns)
    3370              :     return false;
    3371              : 
    3372              :   /* If sym->refs == 1, we can use normal reference counting.  If sym->refs > 2,
    3373              :      the symbol won't be freed anyway, with or without cyclic reference.  */
    3374       319565 :   if (sym->refs != 2)
    3375              :     return false;
    3376              : 
    3377              :   /* Procedure symbols host-associated from a module in submodules are special,
    3378              :      because the namespace of the procedure block in the submodule is different
    3379              :      from the FORMAL_NS namespace generated by host-association.  So there are
    3380              :      two different namespaces representing the same procedure namespace.  As
    3381              :      FORMAL_NS comes from host-association, which only imports symbols visible
    3382              :      from the outside (dummy arguments basically), we can assume there is no
    3383              :      self reference through FORMAL_NS in that case.  */
    3384        50580 :   if (sym->attr.host_assoc && sym->attr.used_in_submodule)
    3385          423 :     return false;
    3386              : 
    3387              :   /* We can assume that contained procedures have cyclic references, because
    3388              :      the symbol of the procedure itself is accessible in the procedure body
    3389              :      namespace.  So we assume that symbols with a formal namespace different
    3390              :      from the declaration namespace and two references, one of which is about
    3391              :      to be removed, are procedures with just the self reference left.  At this
    3392              :      point, the symbol SYM matches that pattern, so we return true here to
    3393              :      permit the release of SYM.  */
    3394              :   return true;
    3395              : }
    3396              : 
    3397              : 
    3398              : /* Decrease the reference counter and free memory when we reach zero.
    3399              :    Returns true if the symbol has been freed, false otherwise.  */
    3400              : 
    3401              : bool
    3402      6563223 : gfc_release_symbol (gfc_symbol *&sym)
    3403              : {
    3404      6563223 :   if (sym == NULL)
    3405              :     return false;
    3406              : 
    3407      6562525 :   if (cyclic_reference_break_needed (sym))
    3408              :     {
    3409              :       /* As formal_ns contains a reference to sym, delete formal_ns just
    3410              :          before the deletion of sym.  */
    3411        50157 :       gfc_namespace *ns = sym->formal_ns;
    3412        50157 :       sym->formal_ns = NULL;
    3413        50157 :       gfc_free_namespace (ns);
    3414              :     }
    3415              : 
    3416      6562525 :   sym->refs--;
    3417      6562525 :   if (sym->refs > 0)
    3418              :     return false;
    3419              : 
    3420      6442576 :   gcc_assert (sym->refs == 0);
    3421      6442576 :   gfc_free_symbol (sym);
    3422      6442576 :   return true;
    3423              : }
    3424              : 
    3425              : 
    3426              : /* Allocate and initialize a new symbol node.  */
    3427              : 
    3428              : gfc_symbol *
    3429      6584010 : gfc_new_symbol (const char *name, gfc_namespace *ns, locus *where)
    3430              : {
    3431      6584010 :   gfc_symbol *p;
    3432              : 
    3433      6584010 :   p = XCNEW (gfc_symbol);
    3434              : 
    3435      6584010 :   gfc_clear_ts (&p->ts);
    3436      6584010 :   gfc_clear_attr (&p->attr);
    3437      6584010 :   p->ns = ns;
    3438      6584010 :   p->declared_at = where ? *where : gfc_current_locus;
    3439      6584010 :   p->name = gfc_get_string ("%s", name);
    3440              : 
    3441      6584010 :   return p;
    3442              : }
    3443              : 
    3444              : 
    3445              : /* Generate an error if a symbol is ambiguous, and set the error flag
    3446              :    on it.  */
    3447              : 
    3448              : static void
    3449           40 : ambiguous_symbol (const char *name, gfc_symtree *st)
    3450              : {
    3451              : 
    3452           40 :   if (st->n.sym->error)
    3453              :     return;
    3454              : 
    3455           20 :   if (st->n.sym->module)
    3456           17 :     gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
    3457              :                "from module %qs", name, st->n.sym->name, st->n.sym->module);
    3458              :   else
    3459            3 :     gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
    3460              :                "from current program unit", name, st->n.sym->name);
    3461              : 
    3462           20 :   st->n.sym->error = 1;
    3463              : }
    3464              : 
    3465              : 
    3466              : /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
    3467              :    selector on the stack. If yes, replace it by the corresponding temporary.  */
    3468              : 
    3469              : static void
    3470     11137688 : select_type_insert_tmp (gfc_symtree **st)
    3471              : {
    3472     11190584 :   gfc_select_type_stack *stack = select_type_stack;
    3473     11374742 :   for (; stack; stack = stack->prev)
    3474       237054 :     if ((*st)->n.sym == stack->selector && stack->tmp)
    3475              :       {
    3476        52896 :         *st = stack->tmp;
    3477        52896 :         select_type_insert_tmp (st);
    3478        52896 :         return;
    3479              :       }
    3480              : }
    3481              : 
    3482              : 
    3483              : /* Look for a symtree in the current procedure -- that is, go up to
    3484              :    parent namespaces but only if inside a BLOCK.  Returns NULL if not found.  */
    3485              : 
    3486              : gfc_symtree*
    3487          241 : gfc_find_symtree_in_proc (const char* name, gfc_namespace* ns)
    3488              : {
    3489          290 :   while (ns)
    3490              :     {
    3491          290 :       gfc_symtree* st = gfc_find_symtree (ns->sym_root, name);
    3492          290 :       if (st)
    3493              :         return st;
    3494              : 
    3495           51 :       if (!ns->construct_entities)
    3496              :         break;
    3497           49 :       ns = ns->parent;
    3498              :     }
    3499              : 
    3500              :   return NULL;
    3501              : }
    3502              : 
    3503              : 
    3504              : /* Search for a symtree starting in the current namespace, resorting to
    3505              :    any parent namespaces if requested by a nonzero parent_flag.
    3506              :    Returns true if the name is ambiguous.  */
    3507              : 
    3508              : bool
    3509     20105306 : gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
    3510              :                    gfc_symtree **result)
    3511              : {
    3512     20105306 :   gfc_symtree *st;
    3513              : 
    3514     20105306 :   if (ns == NULL)
    3515      8045766 :     ns = gfc_current_ns;
    3516              : 
    3517     22866644 :   do
    3518              :     {
    3519     22866644 :       st = gfc_find_symtree (ns->sym_root, name);
    3520     22866644 :       if (st != NULL)
    3521              :         {
    3522     11137688 :           select_type_insert_tmp (&st);
    3523              : 
    3524     11137688 :           *result = st;
    3525              :           /* Ambiguous generic interfaces are permitted, as long
    3526              :              as the specific interfaces are different.  */
    3527     11137688 :           if (st->ambiguous && !st->n.sym->attr.generic)
    3528              :             {
    3529           36 :               ambiguous_symbol (name, st);
    3530           36 :               return true;
    3531              :             }
    3532              : 
    3533              :           return false;
    3534              :         }
    3535              : 
    3536     11728956 :       if (!parent_flag)
    3537              :         break;
    3538              : 
    3539              :       /* Don't escape an interface block.  */
    3540      8534317 :       if (ns && !ns->has_import_set
    3541      8520578 :           && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
    3542              :         break;
    3543              : 
    3544      8319892 :       ns = ns->parent;
    3545              :     }
    3546      8319892 :   while (ns != NULL);
    3547              : 
    3548      8967618 :   if (gfc_current_state() == COMP_DERIVED
    3549       233837 :       && gfc_current_block ()->attr.pdt_template)
    3550              :     {
    3551              :       gfc_symbol *der = gfc_current_block ();
    3552        46076 :       for (; der; der = gfc_get_derived_super_type (der))
    3553              :         {
    3554        27392 :           if (der->f2k_derived && der->f2k_derived->sym_root)
    3555              :             {
    3556        26814 :               st = gfc_find_symtree (der->f2k_derived->sym_root, name);
    3557        26814 :               if (st)
    3558              :                 break;
    3559              :             }
    3560              :         }
    3561        23769 :       *result = st;
    3562        23769 :       return false;
    3563              :     }
    3564              : 
    3565      8943849 :   *result = NULL;
    3566              : 
    3567      8943849 :   return false;
    3568              : }
    3569              : 
    3570              : 
    3571              : /* Same, but returns the symbol instead.  */
    3572              : 
    3573              : int
    3574      2757131 : gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
    3575              :                  gfc_symbol **result)
    3576              : {
    3577      2757131 :   gfc_symtree *st;
    3578      2757131 :   int i;
    3579              : 
    3580      2757131 :   i = gfc_find_sym_tree (name, ns, parent_flag, &st);
    3581              : 
    3582      2757131 :   if (st == NULL)
    3583              :     *result = NULL;
    3584              :   else
    3585       736442 :     *result = st->n.sym;
    3586              : 
    3587      2757131 :   return i;
    3588              : }
    3589              : 
    3590              : 
    3591              : /* Tells whether there is only one set of changes in the stack.  */
    3592              : 
    3593              : static bool
    3594     42699095 : single_undo_checkpoint_p (void)
    3595              : {
    3596     42699095 :   if (latest_undo_chgset == &default_undo_chgset_var)
    3597              :     {
    3598     42699095 :       gcc_assert (latest_undo_chgset->previous == NULL);
    3599              :       return true;
    3600              :     }
    3601              :   else
    3602              :     {
    3603            0 :       gcc_assert (latest_undo_chgset->previous != NULL);
    3604              :       return false;
    3605              :     }
    3606              : }
    3607              : 
    3608              : /* Save symbol with the information necessary to back it out.  */
    3609              : 
    3610              : void
    3611      6347472 : gfc_save_symbol_data (gfc_symbol *sym)
    3612              : {
    3613      6347472 :   gfc_symbol *s;
    3614      6347472 :   unsigned i;
    3615              : 
    3616      6347472 :   if (!single_undo_checkpoint_p ())
    3617              :     {
    3618              :       /* If there is more than one change set, look for the symbol in the
    3619              :          current one.  If it is found there, we can reuse it.  */
    3620            0 :       FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
    3621            0 :         if (s == sym)
    3622              :           {
    3623            0 :             gcc_assert (sym->gfc_new || sym->old_symbol != NULL);
    3624      6347472 :             return;
    3625              :           }
    3626              :     }
    3627      6347472 :   else if (sym->gfc_new || sym->old_symbol != NULL)
    3628              :     return;
    3629              : 
    3630      3220325 :   s = XCNEW (gfc_symbol);
    3631      3220325 :   *s = *sym;
    3632      3220325 :   sym->old_symbol = s;
    3633      3220325 :   sym->gfc_new = 0;
    3634              : 
    3635      3220325 :   latest_undo_chgset->syms.safe_push (sym);
    3636              : }
    3637              : 
    3638              : 
    3639              : /* Given a name, find a symbol, or create it if it does not exist yet
    3640              :    in the current namespace.  If the symbol is found we make sure that
    3641              :    it's OK.
    3642              : 
    3643              :    The integer return code indicates
    3644              :      0   All OK
    3645              :      1   The symbol name was ambiguous
    3646              :      2   The name meant to be established was already host associated.
    3647              : 
    3648              :    So if the return value is nonzero, then an error was issued.  */
    3649              : 
    3650              : int
    3651      6345032 : gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
    3652              :                   bool allow_subroutine, locus *where)
    3653              : {
    3654      6345032 :   gfc_symtree *st;
    3655      6345032 :   gfc_symbol *p;
    3656              : 
    3657              :   /* This doesn't usually happen during resolution.  */
    3658      6345032 :   if (ns == NULL)
    3659      3126281 :     ns = gfc_current_ns;
    3660              : 
    3661              :   /* Try to find the symbol in ns.  */
    3662      6345032 :   st = gfc_find_symtree (ns->sym_root, name);
    3663              : 
    3664      6345032 :   if (st == NULL && ns->omp_udr_ns)
    3665              :     {
    3666          321 :       ns = ns->parent;
    3667          321 :       st = gfc_find_symtree (ns->sym_root, name);
    3668              :     }
    3669              : 
    3670      5426037 :   if (st == NULL)
    3671              :     {
    3672              :       /* If not there, create a new symbol.  */
    3673      5425907 :       p = gfc_new_symbol (name, ns, where);
    3674              : 
    3675              :       /* Add to the list of tentative symbols.  */
    3676      5425907 :       p->old_symbol = NULL;
    3677      5425907 :       p->mark = 1;
    3678      5425907 :       p->gfc_new = 1;
    3679      5425907 :       latest_undo_chgset->syms.safe_push (p);
    3680              : 
    3681      5425907 :       st = gfc_new_symtree (&ns->sym_root, name);
    3682      5425907 :       st->n.sym = p;
    3683      5425907 :       p->refs++;
    3684              : 
    3685              :     }
    3686              :   else
    3687              :     {
    3688              :       /* Make sure the existing symbol is OK.  Ambiguous
    3689              :          generic interfaces are permitted, as long as the
    3690              :          specific interfaces are different.  */
    3691       919125 :       if (st->ambiguous && !st->n.sym->attr.generic)
    3692              :         {
    3693            4 :           ambiguous_symbol (name, st);
    3694            4 :           return 1;
    3695              :         }
    3696              : 
    3697       919121 :       p = st->n.sym;
    3698       919121 :       if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
    3699        10537 :           && !(allow_subroutine && p->attr.subroutine)
    3700        10527 :           && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
    3701        10485 :           && (ns->has_import_set || p->attr.imported)))
    3702              :         {
    3703              :           /* Symbol is from another namespace.  */
    3704           43 :           gfc_error ("Symbol %qs at %C has already been host associated",
    3705              :                      name);
    3706           43 :           return 2;
    3707              :         }
    3708              : 
    3709       919078 :       p->mark = 1;
    3710              : 
    3711              :       /* Copy in case this symbol is changed.  */
    3712       919078 :       gfc_save_symbol_data (p);
    3713              :     }
    3714              : 
    3715      6344985 :   *result = st;
    3716      6344985 :   return 0;
    3717              : }
    3718              : 
    3719              : 
    3720              : int
    3721      1085725 : gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result,
    3722              :                 locus *where)
    3723              : {
    3724      1085725 :   gfc_symtree *st;
    3725      1085725 :   int i;
    3726              : 
    3727      1085725 :   i = gfc_get_sym_tree (name, ns, &st, false, where);
    3728      1085725 :   if (i != 0)
    3729              :     return i;
    3730              : 
    3731      1085708 :   if (st)
    3732      1085708 :     *result = st->n.sym;
    3733              :   else
    3734            0 :     *result = NULL;
    3735              :   return i;
    3736              : }
    3737              : 
    3738              : 
    3739              : /* Subroutine that searches for a symbol, creating it if it doesn't
    3740              :    exist, but tries to host-associate the symbol if possible.  */
    3741              : 
    3742              : int
    3743      8214900 : gfc_get_ha_sym_tree (const char *name, gfc_symtree **result, locus *where)
    3744              : {
    3745      8214900 :   gfc_symtree *st;
    3746      8214900 :   int i;
    3747              : 
    3748      8214900 :   i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
    3749              : 
    3750      8214900 :   if (st != NULL)
    3751              :     {
    3752      5357336 :       gfc_save_symbol_data (st->n.sym);
    3753      5357336 :       *result = st;
    3754      5357336 :       return i;
    3755              :     }
    3756              : 
    3757      2857564 :   i = gfc_find_sym_tree (name, gfc_current_ns, 1, &st);
    3758      2857564 :   if (i)
    3759              :     return i;
    3760              : 
    3761      2857564 :   if (st != NULL)
    3762              :     {
    3763       285606 :       *result = st;
    3764       285606 :       return 0;
    3765              :     }
    3766              : 
    3767      2571958 :   return gfc_get_sym_tree (name, gfc_current_ns, result, false, where);
    3768              : }
    3769              : 
    3770              : 
    3771              : int
    3772        35152 : gfc_get_ha_symbol (const char *name, gfc_symbol **result, locus *where)
    3773              : {
    3774        35152 :   int i;
    3775        35152 :   gfc_symtree *st = NULL;
    3776              : 
    3777        35152 :   i = gfc_get_ha_sym_tree (name, &st, where);
    3778              : 
    3779        35152 :   if (st)
    3780        35152 :     *result = st->n.sym;
    3781              :   else
    3782              :     *result = NULL;
    3783              : 
    3784        35152 :   return i;
    3785              : }
    3786              : 
    3787              : 
    3788              : /* Search for the symtree belonging to a gfc_common_head; we cannot use
    3789              :    head->name as the common_root symtree's name might be mangled.  */
    3790              : 
    3791              : static gfc_symtree *
    3792           18 : find_common_symtree (gfc_symtree *st, gfc_common_head *head)
    3793              : {
    3794              : 
    3795           21 :   gfc_symtree *result;
    3796              : 
    3797           21 :   if (st == NULL)
    3798              :     return NULL;
    3799              : 
    3800           15 :   if (st->n.common == head)
    3801              :     return st;
    3802              : 
    3803            3 :   result = find_common_symtree (st->left, head);
    3804            3 :   if (!result)
    3805            3 :     result = find_common_symtree (st->right, head);
    3806              : 
    3807              :   return result;
    3808              : }
    3809              : 
    3810              : 
    3811              : /* Restore previous state of symbol.  Just copy simple stuff.  */
    3812              : 
    3813              : static void
    3814      1484464 : restore_old_symbol (gfc_symbol *p)
    3815              : {
    3816      1484464 :   gfc_symbol *old;
    3817              : 
    3818      1484464 :   p->mark = 0;
    3819      1484464 :   old = p->old_symbol;
    3820              : 
    3821              :   /* Restore the whole typespec, not just type/kind, so ts.u.cl doesn't
    3822              :      dangle.  */
    3823      1484464 :   p->ts = old->ts;
    3824              : 
    3825      1484464 :   p->attr = old->attr;
    3826              : 
    3827      1484464 :   if (p->value != old->value)
    3828              :     {
    3829            1 :       gcc_checking_assert (old->value == NULL);
    3830            1 :       gfc_free_expr (p->value);
    3831            1 :       p->value = NULL;
    3832              :     }
    3833              : 
    3834      1484464 :   if (p->as != old->as)
    3835              :     {
    3836            5 :       if (p->as)
    3837            5 :         gfc_free_array_spec (p->as);
    3838            5 :       p->as = old->as;
    3839              :     }
    3840              : 
    3841      1484464 :   p->generic = old->generic;
    3842      1484464 :   p->component_access = old->component_access;
    3843              : 
    3844      1484464 :   if (p->namelist != NULL && old->namelist == NULL)
    3845              :     {
    3846            0 :       gfc_free_namelist (p->namelist);
    3847            0 :       p->namelist = NULL;
    3848              :     }
    3849              :   else
    3850              :     {
    3851      1484464 :       if (p->namelist_tail != old->namelist_tail)
    3852              :         {
    3853            1 :           gfc_free_namelist (old->namelist_tail->next);
    3854            1 :           old->namelist_tail->next = NULL;
    3855              :         }
    3856              :     }
    3857              : 
    3858      1484464 :   p->namelist_tail = old->namelist_tail;
    3859              : 
    3860      1484464 :   if (p->formal != old->formal)
    3861              :     {
    3862           29 :       gfc_free_formal_arglist (p->formal);
    3863           29 :       p->formal = old->formal;
    3864              :     }
    3865              : 
    3866      1484464 :   set_symbol_common_block (p, old->common_block);
    3867      1484464 :   p->common_head = old->common_head;
    3868              : 
    3869      1484464 :   p->old_symbol = old->old_symbol;
    3870      1484464 :   free (old);
    3871      1484464 : }
    3872              : 
    3873              : 
    3874              : /* Frees the internal data of a gfc_undo_change_set structure.  Doesn't free
    3875              :    the structure itself.  */
    3876              : 
    3877              : static void
    3878        84170 : free_undo_change_set_data (gfc_undo_change_set &cs)
    3879              : {
    3880        84170 :   cs.syms.release ();
    3881        84170 :   cs.tbps.release ();
    3882        84170 :   cs.cls.release ();
    3883        84170 : }
    3884              : 
    3885              : 
    3886              : /* Given a change set pointer, free its target's contents and update it with
    3887              :    the address of the previous change set.  Note that only the contents are
    3888              :    freed, not the target itself (the contents' container).  It is not a problem
    3889              :    as the latter will be a local variable usually.  */
    3890              : 
    3891              : static void
    3892            0 : pop_undo_change_set (gfc_undo_change_set *&cs)
    3893              : {
    3894            0 :   free_undo_change_set_data (*cs);
    3895            0 :   cs = cs->previous;
    3896            0 : }
    3897              : 
    3898              : 
    3899              : static void free_old_symbol (gfc_symbol *sym);
    3900              : 
    3901              : 
    3902              : /* Merges the current change set into the previous one.  The changes themselves
    3903              :    are left untouched; only one checkpoint is forgotten.  */
    3904              : 
    3905              : void
    3906            0 : gfc_drop_last_undo_checkpoint (void)
    3907              : {
    3908            0 :   gfc_symbol *s, *t;
    3909            0 :   unsigned i, j;
    3910              : 
    3911            0 :   FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
    3912              :     {
    3913              :       /* No need to loop in this case.  */
    3914            0 :       if (s->old_symbol == NULL)
    3915            0 :         continue;
    3916              : 
    3917              :       /* Remove the duplicate symbols.  */
    3918            0 :       FOR_EACH_VEC_ELT (latest_undo_chgset->previous->syms, j, t)
    3919            0 :         if (t == s)
    3920              :           {
    3921            0 :             latest_undo_chgset->previous->syms.unordered_remove (j);
    3922              : 
    3923              :             /* S->OLD_SYMBOL is the backup symbol for S as it was at the
    3924              :                last checkpoint.  We drop that checkpoint, so S->OLD_SYMBOL
    3925              :                shall contain from now on the backup symbol for S as it was
    3926              :                at the checkpoint before.  */
    3927            0 :             if (s->old_symbol->gfc_new)
    3928              :               {
    3929            0 :                 gcc_assert (s->old_symbol->old_symbol == NULL);
    3930            0 :                 s->gfc_new = s->old_symbol->gfc_new;
    3931            0 :                 free_old_symbol (s);
    3932              :               }
    3933              :             else
    3934            0 :               restore_old_symbol (s->old_symbol);
    3935              :             break;
    3936              :           }
    3937              :     }
    3938              : 
    3939            0 :   latest_undo_chgset->previous->syms.safe_splice (latest_undo_chgset->syms);
    3940            0 :   latest_undo_chgset->previous->tbps.safe_splice (latest_undo_chgset->tbps);
    3941            0 :   latest_undo_chgset->previous->cls.safe_splice (latest_undo_chgset->cls);
    3942              : 
    3943            0 :   pop_undo_change_set (latest_undo_chgset);
    3944            0 : }
    3945              : 
    3946              : 
    3947              : /* Remove the reference to the symbol SYM in the symbol tree held by NS
    3948              :    and free SYM if the last reference to it has been removed.
    3949              :    Returns whether the symbol has been freed.  */
    3950              : 
    3951              : static bool
    3952      4351175 : delete_symbol_from_ns (gfc_symbol *sym, gfc_namespace *ns)
    3953              : {
    3954      4351175 :   if (ns == nullptr)
    3955              :     return false;
    3956              : 
    3957              :   /* The derived type is saved in the symtree with the first
    3958              :      letter capitalized; the all lower-case version to the
    3959              :      derived type contains its associated generic function.  */
    3960      4351104 :   const char *sym_name = gfc_fl_struct (sym->attr.flavor)
    3961      4351147 :                          ? gfc_dt_upper_string (sym->name)
    3962      4351137 :                          : sym->name;
    3963              : 
    3964      4351137 :   gfc_delete_symtree (&ns->sym_root, sym_name);
    3965              : 
    3966      4351137 :   return gfc_release_symbol (sym);
    3967              : }
    3968              : 
    3969              : 
    3970              : /* Undoes all the changes made to symbols since the previous checkpoint.
    3971              :    This subroutine is made simpler due to the fact that attributes are
    3972              :    never removed once added.  */
    3973              : 
    3974              : void
    3975     13725839 : gfc_restore_last_undo_checkpoint (void)
    3976              : {
    3977     13725839 :   gfc_symbol *p;
    3978     13725839 :   unsigned i;
    3979              : 
    3980     33255741 :   FOR_EACH_VEC_ELT_REVERSE (latest_undo_chgset->syms, i, p)
    3981              :     {
    3982              :       /* Symbol in a common block was new. Or was old and just put in common */
    3983      5835573 :       if (p->common_block
    3984         3735 :           && (p->gfc_new || !p->old_symbol->common_block))
    3985              :         {
    3986              :           /* If the symbol was added to any common block, it
    3987              :              needs to be removed to stop the resolver looking
    3988              :              for a (possibly) dead symbol.  */
    3989           81 :           if (p->common_block->head == p && !p->common_next)
    3990              :             {
    3991           15 :               gfc_symtree st, *st0;
    3992           15 :               st0 = find_common_symtree (p->ns->common_root,
    3993              :                                          p->common_block);
    3994           15 :               if (st0)
    3995              :                 {
    3996           12 :                   st.name = st0->name;
    3997           12 :                   gfc_delete_bbt (&p->ns->common_root, &st, compare_symtree);
    3998           12 :                   free (st0);
    3999              :                 }
    4000              :             }
    4001              : 
    4002           81 :           if (p->common_block->head == p)
    4003           15 :             p->common_block->head = p->common_next;
    4004              :           else
    4005              :             {
    4006           66 :               gfc_symbol *cparent, *csym;
    4007              : 
    4008           66 :               cparent = p->common_block->head;
    4009           66 :               csym = cparent->common_next;
    4010              : 
    4011          290 :               while (csym != p)
    4012              :                 {
    4013          224 :                   cparent = csym;
    4014          224 :                   csym = csym->common_next;
    4015              :                 }
    4016              : 
    4017           66 :               gcc_assert(cparent->common_next == p);
    4018           66 :               cparent->common_next = csym->common_next;
    4019              :             }
    4020           81 :           p->common_next = NULL;
    4021              :         }
    4022      5835573 :       if (p->gfc_new)
    4023              :         {
    4024      4351109 :           bool freed = delete_symbol_from_ns (p, p->ns);
    4025              : 
    4026              :           /* If the symbol is a procedure (function or subroutine), remove
    4027              :              it from the procedure body namespace as well as from the outer
    4028              :              namespace.  */
    4029      4351109 :           if (!freed
    4030           38 :               && p->formal_ns != p->ns)
    4031           38 :             freed = delete_symbol_from_ns (p, p->formal_ns);
    4032              : 
    4033              :           /* If the formal_ns field has not been set yet, the previous
    4034              :              conditional does nothing.  In that case, we can assume that
    4035              :              gfc_current_ns is the procedure body namespace, and remove the
    4036              :              symbol from there.  */
    4037           38 :           if (!freed
    4038           38 :               && gfc_current_ns != p->ns
    4039           28 :               && gfc_current_ns != p->formal_ns)
    4040           28 :             freed = delete_symbol_from_ns (p, gfc_current_ns);
    4041              :         }
    4042              :       else
    4043      1484464 :         restore_old_symbol (p);
    4044              :     }
    4045              : 
    4046     13725839 :   latest_undo_chgset->syms.truncate (0);
    4047     13725839 :   latest_undo_chgset->tbps.truncate (0);
    4048              : 
    4049              :   /* Remove charlens added during this failed parse attempt.  These are
    4050              :      zombie charlens whose length expressions may reference symtrees that
    4051              :      have just been freed above via delete_symbol_from_ns.  */
    4052     13725839 :   {
    4053     13725839 :     gfc_charlen *cl;
    4054     13725839 :     unsigned i;
    4055              : 
    4056     19165824 :     FOR_EACH_VEC_ELT_REVERSE (latest_undo_chgset->cls, i, cl)
    4057              :       {
    4058         1240 :         gfc_namespace *ns = cl->cl_ns;
    4059         1240 :         if (ns != NULL)
    4060              :           {
    4061         1240 :             if (ns->cl_list == cl)
    4062         1240 :               ns->cl_list = cl->next;
    4063              :             else
    4064              :               {
    4065              :                 gfc_charlen *prev;
    4066            0 :                 for (prev = ns->cl_list; prev && prev->next != cl;
    4067              :                      prev = prev->next)
    4068              :                   ;
    4069            0 :                 if (prev)
    4070            0 :                   prev->next = cl->next;
    4071              :               }
    4072              :           }
    4073         1240 :         gfc_free_expr (cl->length);
    4074         1240 :         free (cl);
    4075              :       }
    4076     13725839 :     latest_undo_chgset->cls.truncate (0);
    4077              :   }
    4078              : 
    4079     13725839 :   if (!single_undo_checkpoint_p ())
    4080            0 :     pop_undo_change_set (latest_undo_chgset);
    4081     13725839 : }
    4082              : 
    4083              : 
    4084              : /* Makes sure that there is only one set of changes; in other words we haven't
    4085              :    forgotten to pair a call to gfc_new_checkpoint with a call to either
    4086              :    gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint.  */
    4087              : 
    4088              : static void
    4089     22625784 : enforce_single_undo_checkpoint (void)
    4090              : {
    4091     22625784 :   gcc_checking_assert (single_undo_checkpoint_p ());
    4092     22625784 : }
    4093              : 
    4094              : 
    4095              : /* Undoes all the changes made to symbols in the current statement.  */
    4096              : 
    4097              : void
    4098     13725839 : gfc_undo_symbols (void)
    4099              : {
    4100     13725839 :   enforce_single_undo_checkpoint ();
    4101     13725839 :   gfc_restore_last_undo_checkpoint ();
    4102     13725839 : }
    4103              : 
    4104              : 
    4105              : /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
    4106              :    components of old_symbol that might need deallocation are the "allocatables"
    4107              :    that are restored in gfc_undo_symbols(), with two exceptions: namelist and
    4108              :    namelist_tail.  In case these differ between old_symbol and sym, it's just
    4109              :    because sym->namelist has gotten a few more items.  */
    4110              : 
    4111              : static void
    4112      2899668 : free_old_symbol (gfc_symbol *sym)
    4113              : {
    4114              : 
    4115      2899668 :   if (sym->old_symbol == NULL)
    4116              :     return;
    4117              : 
    4118      1735860 :   if (sym->old_symbol->as != NULL
    4119       281985 :       && sym->old_symbol->as != sym->as
    4120            2 :       && !(sym->ts.type == BT_CLASS
    4121            2 :            && sym->ts.u.derived->attr.is_class
    4122            2 :            && sym->old_symbol->as == CLASS_DATA (sym)->as))
    4123            0 :     gfc_free_array_spec (sym->old_symbol->as);
    4124              : 
    4125      1735860 :   if (sym->old_symbol->value != sym->value)
    4126         8381 :     gfc_free_expr (sym->old_symbol->value);
    4127              : 
    4128      1735860 :   if (sym->old_symbol->formal != sym->formal)
    4129        17976 :     gfc_free_formal_arglist (sym->old_symbol->formal);
    4130              : 
    4131      1735860 :   free (sym->old_symbol);
    4132      1735860 :   sym->old_symbol = NULL;
    4133              : }
    4134              : 
    4135              : 
    4136              : /* Makes the changes made in the current statement permanent-- gets
    4137              :    rid of undo information.  */
    4138              : 
    4139              : void
    4140      1636273 : gfc_commit_symbols (void)
    4141              : {
    4142      1636273 :   gfc_symbol *p;
    4143      1636273 :   gfc_typebound_proc *tbp;
    4144      1636273 :   unsigned i;
    4145              : 
    4146      1636273 :   enforce_single_undo_checkpoint ();
    4147              : 
    4148      5486599 :   FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
    4149              :     {
    4150      2214053 :       p->mark = 0;
    4151      2214053 :       p->gfc_new = 0;
    4152      2214053 :       free_old_symbol (p);
    4153              :     }
    4154      1636273 :   latest_undo_chgset->syms.truncate (0);
    4155              : 
    4156      3334734 :   FOR_EACH_VEC_ELT (latest_undo_chgset->tbps, i, tbp)
    4157        62188 :     tbp->error = 0;
    4158      1636273 :   latest_undo_chgset->tbps.truncate (0);
    4159              : 
    4160              :   /* Charlens are committed to the namespace; just clear the tracking vector.  */
    4161      1636273 :   latest_undo_chgset->cls.truncate (0);
    4162      1636273 : }
    4163              : 
    4164              : 
    4165              : /* Makes the changes made in one symbol permanent -- gets rid of undo
    4166              :    information.  */
    4167              : 
    4168              : void
    4169       685615 : gfc_commit_symbol (gfc_symbol *sym)
    4170              : {
    4171       685615 :   gfc_symbol *p;
    4172       685615 :   unsigned i;
    4173              : 
    4174       685615 :   enforce_single_undo_checkpoint ();
    4175              : 
    4176      2452289 :   FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
    4177      1676211 :     if (p == sym)
    4178              :       {
    4179       595152 :         latest_undo_chgset->syms.unordered_remove (i);
    4180       595152 :         break;
    4181              :       }
    4182              : 
    4183       685615 :   sym->mark = 0;
    4184       685615 :   sym->gfc_new = 0;
    4185              : 
    4186       685615 :   free_old_symbol (sym);
    4187       685615 : }
    4188              : 
    4189              : 
    4190              : /* Recursively free trees containing type-bound procedures.  */
    4191              : 
    4192              : static void
    4193      1132784 : free_tb_tree (gfc_symtree *t)
    4194              : {
    4195      1132784 :   if (t == NULL)
    4196              :     return;
    4197              : 
    4198         7399 :   free_tb_tree (t->left);
    4199         7399 :   free_tb_tree (t->right);
    4200              : 
    4201              :   /* TODO: Free type-bound procedure u.generic  */
    4202         7399 :   free (t->n.tb);
    4203         7399 :   t->n.tb = NULL;
    4204         7399 :   free (t);
    4205              : }
    4206              : 
    4207              : 
    4208              : /* Recursive function that deletes an entire tree and all the common
    4209              :    head structures it points to.  */
    4210              : 
    4211              : static void
    4212       562951 : free_common_tree (gfc_symtree * common_tree)
    4213              : {
    4214       562951 :   if (common_tree == NULL)
    4215              :     return;
    4216              : 
    4217         1979 :   free_common_tree (common_tree->left);
    4218         1979 :   free_common_tree (common_tree->right);
    4219              : 
    4220         1979 :   free (common_tree);
    4221              : }
    4222              : 
    4223              : 
    4224              : /* Recursive function that deletes an entire tree and all the common
    4225              :    head structures it points to.  */
    4226              : 
    4227              : static void
    4228       560061 : free_omp_udr_tree (gfc_symtree * omp_udr_tree)
    4229              : {
    4230       560061 :   if (omp_udr_tree == NULL)
    4231              :     return;
    4232              : 
    4233          534 :   free_omp_udr_tree (omp_udr_tree->left);
    4234          534 :   free_omp_udr_tree (omp_udr_tree->right);
    4235              : 
    4236          534 :   gfc_free_omp_udr (omp_udr_tree->n.omp_udr);
    4237          534 :   free (omp_udr_tree);
    4238              : }
    4239              : 
    4240              : /* Similar, for !$omp declare mappers.  */
    4241              : 
    4242              : static void
    4243       559051 : free_omp_udm_tree (gfc_symtree *omp_udm_tree)
    4244              : {
    4245       559051 :   if (omp_udm_tree == NULL)
    4246              :     return;
    4247              : 
    4248           29 :   free_omp_udm_tree (omp_udm_tree->left);
    4249           29 :   free_omp_udm_tree (omp_udm_tree->right);
    4250              : 
    4251           29 :   gfc_free_omp_udm (omp_udm_tree->n.omp_udm);
    4252           29 :   free (omp_udm_tree);
    4253              : }
    4254              : 
    4255              : 
    4256              : /* Recursive function that deletes an entire tree and all the user
    4257              :    operator nodes that it contains.  */
    4258              : 
    4259              : static void
    4260       559799 : free_uop_tree (gfc_symtree *uop_tree)
    4261              : {
    4262       559799 :   if (uop_tree == NULL)
    4263              :     return;
    4264              : 
    4265          403 :   free_uop_tree (uop_tree->left);
    4266          403 :   free_uop_tree (uop_tree->right);
    4267              : 
    4268          403 :   gfc_free_interface (uop_tree->n.uop->op);
    4269          403 :   free (uop_tree->n.uop);
    4270          403 :   free (uop_tree);
    4271              : }
    4272              : 
    4273              : 
    4274              : /* Recursive function that deletes an entire tree and all the symbols
    4275              :    that it contains.  */
    4276              : 
    4277              : static void
    4278      4972293 : free_sym_tree (gfc_symtree *sym_tree)
    4279              : {
    4280      4972293 :   if (sym_tree == NULL)
    4281              :     return;
    4282              : 
    4283      2206650 :   free_sym_tree (sym_tree->left);
    4284      2206650 :   free_sym_tree (sym_tree->right);
    4285              : 
    4286      2206650 :   gfc_release_symbol (sym_tree->n.sym);
    4287      2206650 :   free (sym_tree);
    4288              : }
    4289              : 
    4290              : 
    4291              : /* Free the gfc_equiv_info's.  */
    4292              : 
    4293              : static void
    4294        14673 : gfc_free_equiv_infos (gfc_equiv_info *s)
    4295              : {
    4296        14673 :   if (s == NULL)
    4297              :     return;
    4298         8117 :   gfc_free_equiv_infos (s->next);
    4299         8117 :   free (s);
    4300              : }
    4301              : 
    4302              : 
    4303              : /* Free the gfc_equiv_lists.  */
    4304              : 
    4305              : static void
    4306       565549 : gfc_free_equiv_lists (gfc_equiv_list *l)
    4307              : {
    4308       565549 :   if (l == NULL)
    4309              :     return;
    4310         6556 :   gfc_free_equiv_lists (l->next);
    4311         6556 :   gfc_free_equiv_infos (l->equiv);
    4312         6556 :   free (l);
    4313              : }
    4314              : 
    4315              : 
    4316              : /* Free a finalizer procedure list.  */
    4317              : 
    4318              : void
    4319         1208 : gfc_free_finalizer (gfc_finalizer* el)
    4320              : {
    4321         1208 :   if (el)
    4322              :     {
    4323         1208 :       gfc_release_symbol (el->proc_sym);
    4324         1208 :       free (el);
    4325              :     }
    4326         1208 : }
    4327              : 
    4328              : static void
    4329       558993 : gfc_free_finalizer_list (gfc_finalizer* list)
    4330              : {
    4331       560187 :   while (list)
    4332              :     {
    4333         1194 :       gfc_finalizer* current = list;
    4334         1194 :       list = list->next;
    4335         1194 :       gfc_free_finalizer (current);
    4336              :     }
    4337       558993 : }
    4338              : 
    4339              : 
    4340              : /* Create a new gfc_charlen structure and add it to a namespace.
    4341              :    If 'old_cl' is given, the newly created charlen will be a copy of it.  */
    4342              : 
    4343              : gfc_charlen*
    4344       308451 : gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
    4345              : {
    4346       308451 :   gfc_charlen *cl;
    4347              : 
    4348       308451 :   cl = gfc_get_charlen ();
    4349              : 
    4350              :   /* Copy old_cl.  */
    4351       308451 :   if (old_cl)
    4352              :     {
    4353        15225 :       cl->length = gfc_copy_expr (old_cl->length);
    4354        15225 :       cl->length_from_typespec = old_cl->length_from_typespec;
    4355        15225 :       cl->backend_decl = old_cl->backend_decl;
    4356        15225 :       cl->passed_length = old_cl->passed_length;
    4357        15225 :       cl->resolved = old_cl->resolved;
    4358              :     }
    4359              : 
    4360              :   /* Put into namespace.  */
    4361       308451 :   cl->next = ns->cl_list;
    4362       308451 :   ns->cl_list = cl;
    4363       308451 :   cl->cl_ns = ns;
    4364              : 
    4365              :   /* Track in undo mechanism so reject_statement can remove zombie charlens.  */
    4366       308451 :   latest_undo_chgset->cls.safe_push (cl);
    4367              : 
    4368       308451 :   return cl;
    4369              : }
    4370              : 
    4371              : 
    4372              : /* Remove the charlen without freeing it.  */
    4373              : 
    4374              : void
    4375         2253 : gfc_remove_saved_charlen (gfc_charlen *cl)
    4376              : {
    4377         2253 :   gfc_charlen *tracked;
    4378         2253 :   unsigned j;
    4379         2291 :   FOR_EACH_VEC_ELT (latest_undo_chgset->cls, j, tracked)
    4380         2277 :     if (tracked == cl)
    4381              :       {
    4382         2239 :         latest_undo_chgset->cls.unordered_remove (j);
    4383         2239 :         return;
    4384              :       }
    4385              : }
    4386              : 
    4387              : 
    4388              : /* Free the charlen list from cl to end (end is not freed).
    4389              :    Free the whole list if end is NULL.  */
    4390              : 
    4391              : static void
    4392       558993 : gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
    4393              : {
    4394       558993 :   gfc_charlen *cl2;
    4395              : 
    4396       865770 :   for (; cl != end; cl = cl2)
    4397              :     {
    4398       306777 :       gcc_assert (cl);
    4399              : 
    4400       306777 :       cl2 = cl->next;
    4401       306777 :       gfc_free_expr (cl->length);
    4402       306777 :       free (cl);
    4403              :     }
    4404       558993 : }
    4405              : 
    4406              : 
    4407              : /* Free entry list structs.  */
    4408              : 
    4409              : static void
    4410            0 : free_entry_list (gfc_entry_list *el)
    4411              : {
    4412       560484 :   gfc_entry_list *next;
    4413              : 
    4414       560484 :   if (el == NULL)
    4415            0 :     return;
    4416              : 
    4417         1491 :   next = el->next;
    4418         1491 :   free (el);
    4419         1491 :   free_entry_list (next);
    4420              : }
    4421              : 
    4422              : 
    4423              : /* Free a namespace structure and everything below it.  Interface
    4424              :    lists associated with intrinsic operators are not freed.  These are
    4425              :    taken care of when a specific name is freed.  */
    4426              : 
    4427              : void
    4428     13236410 : gfc_free_namespace (gfc_namespace *&ns)
    4429              : {
    4430     13236410 :   gfc_namespace *p, *q;
    4431     13236410 :   int i;
    4432     13236410 :   gfc_was_finalized *f;
    4433              : 
    4434     13236410 :   if (ns == NULL)
    4435     12677417 :     return;
    4436              : 
    4437       588101 :   ns->refs--;
    4438       588101 :   if (ns->refs > 0)
    4439              :     return;
    4440              : 
    4441       558993 :   gcc_assert (ns->refs == 0);
    4442              : 
    4443       558993 :   gfc_free_statements (ns->code);
    4444              : 
    4445       558993 :   free_sym_tree (ns->sym_root);
    4446       558993 :   free_uop_tree (ns->uop_root);
    4447       558993 :   free_common_tree (ns->common_root);
    4448       558993 :   free_omp_udr_tree (ns->omp_udr_root);
    4449       558993 :   free_omp_udm_tree (ns->omp_udm_root);
    4450       558993 :   free_tb_tree (ns->tb_sym_root);
    4451       558993 :   free_tb_tree (ns->tb_uop_root);
    4452       558993 :   gfc_free_finalizer_list (ns->finalizers);
    4453       558993 :   gfc_free_omp_declare_simd_list (ns->omp_declare_simd);
    4454       558993 :   gfc_free_omp_declare_variant_list (ns->omp_declare_variant);
    4455              : 
    4456              :   /* Remove charlen before freeing.  */
    4457       558993 :   {
    4458       558993 :     gfc_charlen *cl;
    4459       558993 :     unsigned j;
    4460      1352872 :     FOR_EACH_VEC_ELT (latest_undo_chgset->cls, j, cl)
    4461       234886 :       if (cl->cl_ns == ns)
    4462        10254 :         latest_undo_chgset->cls.unordered_remove (j--);
    4463              :   }
    4464              : 
    4465       558993 :   gfc_free_charlen (ns->cl_list, NULL);
    4466       558993 :   free_st_labels (ns->st_labels);
    4467              : 
    4468       558993 :   free_entry_list (ns->entries);
    4469       558993 :   gfc_free_equiv (ns->equiv);
    4470       558993 :   gfc_free_equiv_lists (ns->equiv_lists);
    4471       558993 :   gfc_free_use_stmts (ns->use_stmts);
    4472              : 
    4473     16769790 :   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
    4474     15651804 :     gfc_free_interface (ns->op[i]);
    4475              : 
    4476       558993 :   gfc_free_data (ns->data);
    4477              : 
    4478              :   /* Free all the expr + component combinations that have been
    4479              :      finalized.  */
    4480       558993 :   f = ns->was_finalized;
    4481       562175 :   while (f)
    4482              :     {
    4483         3182 :       gfc_was_finalized* current = f;
    4484         3182 :       f = f->next;
    4485         3182 :       free (current);
    4486              :     }
    4487       558993 :   if (ns->omp_assumes)
    4488              :     {
    4489           22 :       free (ns->omp_assumes->absent);
    4490           22 :       free (ns->omp_assumes->contains);
    4491           22 :       gfc_free_expr_list (ns->omp_assumes->holds);
    4492           22 :       free (ns->omp_assumes);
    4493              :     }
    4494       558993 :   p = ns->contained;
    4495       558993 :   free (ns);
    4496       558993 :   ns = NULL;
    4497              : 
    4498              :   /* Recursively free any contained namespaces.  */
    4499       613336 :   while (p != NULL)
    4500              :     {
    4501        54343 :       q = p;
    4502        54343 :       p = p->sibling;
    4503        54343 :       gfc_free_namespace (q);
    4504              :     }
    4505              : }
    4506              : 
    4507              : 
    4508              : void
    4509        83807 : gfc_symbol_init_2 (void)
    4510              : {
    4511              : 
    4512        83807 :   gfc_current_ns = gfc_get_namespace (NULL, 0);
    4513        83807 : }
    4514              : 
    4515              : 
    4516              : void
    4517        84170 : gfc_symbol_done_2 (void)
    4518              : {
    4519        84170 :   if (gfc_current_ns != NULL)
    4520              :     {
    4521              :       /* free everything from the root.  */
    4522        84186 :       while (gfc_current_ns->parent != NULL)
    4523           16 :         gfc_current_ns = gfc_current_ns->parent;
    4524        84170 :       gfc_free_namespace (gfc_current_ns);
    4525        84170 :       gfc_current_ns = NULL;
    4526              :     }
    4527        84170 :   gfc_derived_types = NULL;
    4528              : 
    4529        84170 :   enforce_single_undo_checkpoint ();
    4530        84170 :   free_undo_change_set_data (*latest_undo_chgset);
    4531        84170 : }
    4532              : 
    4533              : 
    4534              : /* Count how many nodes a symtree has.  */
    4535              : 
    4536              : static unsigned
    4537     27948038 : count_st_nodes (const gfc_symtree *st)
    4538              : {
    4539     52185545 :   unsigned nodes;
    4540     52185545 :   if (!st)
    4541     27948038 :     return 0;
    4542              : 
    4543     24237507 :   nodes = count_st_nodes (st->left);
    4544     24237507 :   nodes++;
    4545     24237507 :   nodes += count_st_nodes (st->right);
    4546              : 
    4547     24237507 :   return nodes;
    4548              : }
    4549              : 
    4550              : 
    4551              : /* Convert symtree tree into symtree vector.  */
    4552              : 
    4553              : static unsigned
    4554     27948038 : fill_st_vector (gfc_symtree *st, gfc_symtree **st_vec, unsigned node_cntr)
    4555              : {
    4556     52185545 :   if (!st)
    4557     27948038 :     return node_cntr;
    4558              : 
    4559     24237507 :   node_cntr = fill_st_vector (st->left, st_vec, node_cntr);
    4560     24237507 :   st_vec[node_cntr++] = st;
    4561     24237507 :   node_cntr = fill_st_vector (st->right, st_vec, node_cntr);
    4562              : 
    4563     24237507 :   return node_cntr;
    4564              : }
    4565              : 
    4566              : 
    4567              : /* Traverse namespace.  As the functions might modify the symtree, we store the
    4568              :    symtree as a vector and operate on this vector.  Note: We assume that
    4569              :    sym_func or st_func never deletes nodes from the symtree - only adding is
    4570              :    allowed. Additionally, newly added nodes are not traversed.  */
    4571              : 
    4572              : static void
    4573      3710531 : do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *),
    4574              :                      void (*sym_func) (gfc_symbol *))
    4575              : {
    4576      3710531 :   gfc_symtree **st_vec;
    4577      3710531 :   unsigned nodes, i, node_cntr;
    4578              : 
    4579      3710531 :   gcc_assert ((st_func && !sym_func) || (!st_func && sym_func));
    4580      3710531 :   nodes = count_st_nodes (st);
    4581      3710531 :   st_vec = XALLOCAVEC (gfc_symtree *, nodes);
    4582      3710531 :   node_cntr = 0;
    4583      3710531 :   fill_st_vector (st, st_vec, node_cntr);
    4584              : 
    4585      3710531 :   if (sym_func)
    4586              :     {
    4587              :       /* Clear marks.  */
    4588     27622502 :       for (i = 0; i < nodes; i++)
    4589     24056708 :         st_vec[i]->n.sym->mark = 0;
    4590     27622502 :       for (i = 0; i < nodes; i++)
    4591     24056708 :         if (!st_vec[i]->n.sym->mark)
    4592              :           {
    4593     23489949 :             (*sym_func) (st_vec[i]->n.sym);
    4594     23489949 :             st_vec[i]->n.sym->mark = 1;
    4595              :           }
    4596              :      }
    4597              :    else
    4598       325536 :       for (i = 0; i < nodes; i++)
    4599       180799 :         (*st_func) (st_vec[i]);
    4600      3710531 : }
    4601              : 
    4602              : 
    4603              : /* Recursively traverse the symtree nodes.  */
    4604              : 
    4605              : void
    4606       144737 : gfc_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *))
    4607              : {
    4608       144737 :   do_traverse_symtree (st, st_func, NULL);
    4609       144737 : }
    4610              : 
    4611              : 
    4612              : /* Call a given function for all symbols in the namespace.  We take
    4613              :    care that each gfc_symbol node is called exactly once.  */
    4614              : 
    4615              : void
    4616      3565793 : gfc_traverse_ns (gfc_namespace *ns, void (*sym_func) (gfc_symbol *))
    4617              : {
    4618      3565793 :   do_traverse_symtree (ns->sym_root, NULL, sym_func);
    4619      3565793 : }
    4620              : 
    4621              : 
    4622              : /* Return TRUE when name is the name of an intrinsic type.  */
    4623              : 
    4624              : bool
    4625        14909 : gfc_is_intrinsic_typename (const char *name)
    4626              : {
    4627        14909 :   if (strcmp (name, "integer") == 0
    4628        14906 :       || strcmp (name, "real") == 0
    4629        14903 :       || strcmp (name, "character") == 0
    4630        14901 :       || strcmp (name, "logical") == 0
    4631        14899 :       || strcmp (name, "complex") == 0
    4632        14895 :       || strcmp (name, "doubleprecision") == 0
    4633        14892 :       || strcmp (name, "doublecomplex") == 0)
    4634              :     return true;
    4635              :   else
    4636        14889 :     return false;
    4637              : }
    4638              : 
    4639              : 
    4640              : /* Return TRUE if the symbol is an automatic variable.  */
    4641              : 
    4642              : static bool
    4643          839 : gfc_is_var_automatic (gfc_symbol *sym)
    4644              : {
    4645              :   /* Pointer and allocatable variables are never automatic.  */
    4646          839 :   if (sym->attr.pointer || sym->attr.allocatable)
    4647              :     return false;
    4648              :   /* Check for arrays with non-constant size.  */
    4649           74 :   if (sym->attr.dimension && sym->as
    4650          831 :       && !gfc_is_compile_time_shape (sym->as))
    4651              :     return true;
    4652              :   /* Check for non-constant length character variables.  */
    4653          747 :   if (sym->ts.type == BT_CHARACTER
    4654           63 :       && sym->ts.u.cl
    4655          810 :       && !gfc_is_constant_expr (sym->ts.u.cl->length))
    4656              :     return true;
    4657              :   /* Variables with explicit AUTOMATIC attribute.  */
    4658          739 :   if (sym->attr.automatic)
    4659           13 :       return true;
    4660              : 
    4661              :   return false;
    4662              : }
    4663              : 
    4664              : /* Given a symbol, mark it as SAVEd if it is allowed.  */
    4665              : 
    4666              : static void
    4667         3075 : save_symbol (gfc_symbol *sym)
    4668              : {
    4669              : 
    4670         3075 :   if (sym->attr.use_assoc)
    4671              :     return;
    4672              : 
    4673         2330 :   if (sym->attr.in_common
    4674         2314 :       || sym->attr.in_equivalence
    4675         2156 :       || sym->attr.dummy
    4676         1917 :       || sym->attr.result
    4677         1906 :       || sym->attr.flavor != FL_VARIABLE)
    4678              :     return;
    4679              :   /* Automatic objects are not saved.  */
    4680          839 :   if (gfc_is_var_automatic (sym))
    4681              :     return;
    4682          808 :   gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, &sym->declared_at);
    4683              : }
    4684              : 
    4685              : 
    4686              : /* Mark those symbols which can be SAVEd as such.  */
    4687              : 
    4688              : void
    4689          315 : gfc_save_all (gfc_namespace *ns)
    4690              : {
    4691          315 :   gfc_traverse_ns (ns, save_symbol);
    4692          315 : }
    4693              : 
    4694              : 
    4695              : /* Make sure that no changes to symbols are pending.  */
    4696              : 
    4697              : void
    4698      6493887 : gfc_enforce_clean_symbol_state(void)
    4699              : {
    4700      6493887 :   enforce_single_undo_checkpoint ();
    4701      6493887 :   gcc_assert (latest_undo_chgset->syms.is_empty ());
    4702              :   /* Charlens may be accumulated by non-tentative contexts such as resolution
    4703              :      and translation.  Clear them here so tentative parsing in the next
    4704              :      statement starts with a clean slate.  */
    4705      6493887 :   latest_undo_chgset->cls.truncate (0);
    4706      6493887 : }
    4707              : 
    4708              : 
    4709              : /************** Global symbol handling ************/
    4710              : 
    4711              : 
    4712              : /* Search a tree for the global symbol.  */
    4713              : 
    4714              : gfc_gsymbol *
    4715       466864 : gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
    4716              : {
    4717       466864 :   int c;
    4718              : 
    4719       466864 :   if (symbol == NULL)
    4720              :     return NULL;
    4721              : 
    4722      1712329 :   while (symbol)
    4723              :     {
    4724      1442821 :       c = strcmp (name, symbol->name);
    4725      1442821 :       if (!c)
    4726              :         return symbol;
    4727              : 
    4728      1288333 :       symbol = (c < 0) ? symbol->left : symbol->right;
    4729              :     }
    4730              : 
    4731              :   return NULL;
    4732              : }
    4733              : 
    4734              : 
    4735              : /* Case insensitive search a tree for the global symbol.  */
    4736              : 
    4737              : gfc_gsymbol *
    4738         4681 : gfc_find_case_gsymbol (gfc_gsymbol *symbol, const char *name)
    4739              : {
    4740         4681 :   int c;
    4741              : 
    4742         4681 :   if (symbol == NULL)
    4743              :     return NULL;
    4744              : 
    4745        16183 :   while (symbol)
    4746              :     {
    4747        14453 :       c = strcasecmp (name, symbol->name);
    4748        14453 :       if (!c)
    4749              :         return symbol;
    4750              : 
    4751        11542 :       symbol = (c < 0) ? symbol->left : symbol->right;
    4752              :     }
    4753              : 
    4754              :   return NULL;
    4755              : }
    4756              : 
    4757              : 
    4758              : /* Compare two global symbols. Used for managing the BB tree.  */
    4759              : 
    4760              : static int
    4761       201907 : gsym_compare (void *_s1, void *_s2)
    4762              : {
    4763       201907 :   gfc_gsymbol *s1, *s2;
    4764              : 
    4765       201907 :   s1 = (gfc_gsymbol *) _s1;
    4766       201907 :   s2 = (gfc_gsymbol *) _s2;
    4767       201907 :   return strcmp (s1->name, s2->name);
    4768              : }
    4769              : 
    4770              : 
    4771              : /* Get a global symbol, creating it if it doesn't exist.  */
    4772              : 
    4773              : gfc_gsymbol *
    4774       126092 : gfc_get_gsymbol (const char *name, bool bind_c)
    4775              : {
    4776       126092 :   gfc_gsymbol *s;
    4777              : 
    4778       126092 :   s = gfc_find_gsymbol (gfc_gsym_root, name);
    4779       126092 :   if (s != NULL)
    4780              :     return s;
    4781              : 
    4782       100175 :   s = XCNEW (gfc_gsymbol);
    4783       100175 :   s->type = GSYM_UNKNOWN;
    4784       100175 :   s->name = gfc_get_string ("%s", name);
    4785       100175 :   s->bind_c = bind_c;
    4786              : 
    4787       100175 :   gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
    4788              : 
    4789       100175 :   return s;
    4790              : }
    4791              : 
    4792              : void
    4793            6 : gfc_traverse_gsymbol (gfc_gsymbol *gsym,
    4794              :                       void (*do_something) (gfc_gsymbol *, void *),
    4795              :                       void *data)
    4796              : {
    4797            8 :   if (gsym->left)
    4798            2 :     gfc_traverse_gsymbol (gsym->left, do_something, data);
    4799              : 
    4800            8 :   (*do_something) (gsym, data);
    4801              : 
    4802            8 :   if (gsym->right)
    4803              :     gfc_traverse_gsymbol (gsym->right, do_something, data);
    4804            6 : }
    4805              : 
    4806              : static gfc_symbol *
    4807           52 : get_iso_c_binding_dt (int sym_id)
    4808              : {
    4809           52 :   gfc_symbol *dt_list = gfc_derived_types;
    4810              : 
    4811              :   /* Loop through the derived types in the name list, searching for
    4812              :      the desired symbol from iso_c_binding.  Search the parent namespaces
    4813              :      if necessary and requested to (parent_flag).  */
    4814           52 :   if (dt_list)
    4815              :     {
    4816           25 :       while (dt_list->dt_next != gfc_derived_types)
    4817              :         {
    4818            0 :           if (dt_list->from_intmod != INTMOD_NONE
    4819            0 :               && dt_list->intmod_sym_id == sym_id)
    4820              :             return dt_list;
    4821              : 
    4822            0 :           dt_list = dt_list->dt_next;
    4823              :         }
    4824              :     }
    4825              : 
    4826              :   return NULL;
    4827              : }
    4828              : 
    4829              : 
    4830              : /* Verifies that the given derived type symbol, derived_sym, is interoperable
    4831              :    with C.  This is necessary for any derived type that is BIND(C) and for
    4832              :    derived types that are parameters to functions that are BIND(C).  All
    4833              :    fields of the derived type are required to be interoperable, and are tested
    4834              :    for such.  If an error occurs, the errors are reported here, allowing for
    4835              :    multiple errors to be handled for a single derived type.  */
    4836              : 
    4837              : bool
    4838        28088 : verify_bind_c_derived_type (gfc_symbol *derived_sym)
    4839              : {
    4840        28088 :   gfc_component *curr_comp = NULL;
    4841        28088 :   bool is_c_interop = false;
    4842        28088 :   bool retval = true;
    4843              : 
    4844        28088 :   if (derived_sym == NULL)
    4845            0 :     gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
    4846              :                         "unexpectedly NULL");
    4847              : 
    4848              :   /* If we've already looked at this derived symbol, do not look at it again
    4849              :      so we don't repeat warnings/errors.  */
    4850        28088 :   if (derived_sym->ts.is_c_interop)
    4851              :     return true;
    4852              : 
    4853              :   /* The derived type must have the BIND attribute to be interoperable
    4854              :      J3/04-007, Section 15.2.3.  */
    4855          406 :   if (derived_sym->attr.is_bind_c != 1)
    4856              :     {
    4857            2 :       derived_sym->ts.is_c_interop = 0;
    4858            2 :       gfc_error_now ("Derived type %qs declared at %L must have the BIND "
    4859              :                      "attribute to be C interoperable", derived_sym->name,
    4860              :                      &(derived_sym->declared_at));
    4861            2 :       retval = false;
    4862              :     }
    4863              : 
    4864          406 :   curr_comp = derived_sym->components;
    4865              : 
    4866              :   /* Fortran 2003 allows an empty derived type.  C99 appears to disallow an
    4867              :      empty struct.  Section 15.2 in Fortran 2003 states:  "The following
    4868              :      subclauses define the conditions under which a Fortran entity is
    4869              :      interoperable.  If a Fortran entity is interoperable, an equivalent
    4870              :      entity may be defined by means of C and the Fortran entity is said
    4871              :      to be interoperable with the C entity.  There does not have to be such
    4872              :      an interoperating C entity."
    4873              : 
    4874              :      However, later discussion on the J3 mailing list
    4875              :      (https://mailman.j3-fortran.org/pipermail/j3/2021-July/013190.html)
    4876              :      found this to be a defect, and Fortran 2018 added in section 18.3.4
    4877              :      the following constraint:
    4878              :      "C1805: A derived type with the BIND attribute shall have at least one
    4879              :      component."
    4880              : 
    4881              :      We thus allow empty derived types only as GNU extension while giving a
    4882              :      warning by default, or reject empty types in standard conformance mode.
    4883              :   */
    4884          406 :   if (curr_comp == NULL)
    4885              :     {
    4886            2 :       if (!gfc_notify_std (GFC_STD_GNU, "Derived type %qs with BIND(C) "
    4887              :                            "attribute at %L has no components",
    4888              :                            derived_sym->name, &(derived_sym->declared_at)))
    4889              :         return false;
    4890            1 :       else if (!pedantic)
    4891              :         /* Generally emit warning, but not twice if -pedantic is given.  */
    4892            1 :         gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L "
    4893              :                      "is empty, and may be inaccessible by the C "
    4894              :                      "companion processor",
    4895              :                      derived_sym->name, &(derived_sym->declared_at));
    4896            1 :       derived_sym->ts.is_c_interop = 1;
    4897            1 :       derived_sym->attr.is_bind_c = 1;
    4898            1 :       return true;
    4899              :     }
    4900              : 
    4901              : 
    4902              :   /* Initialize the derived type as being C interoperable.
    4903              :      If we find an error in the components, this will be set false.  */
    4904          404 :   derived_sym->ts.is_c_interop = 1;
    4905              : 
    4906              :   /* Loop through the list of components to verify that the kind of
    4907              :      each is a C interoperable type.  */
    4908          853 :   do
    4909              :     {
    4910              :       /* The components cannot be pointers (fortran sense).
    4911              :          J3/04-007, Section 15.2.3, C1505.      */
    4912          853 :       if (curr_comp->attr.pointer != 0)
    4913              :         {
    4914            3 :           gfc_error ("Component %qs at %L cannot have the "
    4915              :                      "POINTER attribute because it is a member "
    4916              :                      "of the BIND(C) derived type %qs at %L",
    4917              :                      curr_comp->name, &(curr_comp->loc),
    4918              :                      derived_sym->name, &(derived_sym->declared_at));
    4919            3 :           retval = false;
    4920              :         }
    4921              : 
    4922          853 :       if (curr_comp->attr.proc_pointer != 0)
    4923              :         {
    4924            1 :           gfc_error ("Procedure pointer component %qs at %L cannot be a member"
    4925              :                      " of the BIND(C) derived type %qs at %L", curr_comp->name,
    4926              :                      &curr_comp->loc, derived_sym->name,
    4927              :                      &derived_sym->declared_at);
    4928            1 :           retval = false;
    4929              :         }
    4930              : 
    4931              :       /* The components cannot be allocatable.
    4932              :          J3/04-007, Section 15.2.3, C1505.      */
    4933          853 :       if (curr_comp->attr.allocatable != 0)
    4934              :         {
    4935            3 :           gfc_error ("Component %qs at %L cannot have the "
    4936              :                      "ALLOCATABLE attribute because it is a member "
    4937              :                      "of the BIND(C) derived type %qs at %L",
    4938              :                      curr_comp->name, &(curr_comp->loc),
    4939              :                      derived_sym->name, &(derived_sym->declared_at));
    4940            3 :           retval = false;
    4941              :         }
    4942              : 
    4943              :       /* BIND(C) derived types must have interoperable components.  */
    4944          853 :       if (curr_comp->ts.type == BT_DERIVED
    4945           71 :           && curr_comp->ts.u.derived->ts.is_iso_c != 1
    4946           17 :           && curr_comp->ts.u.derived != derived_sym)
    4947              :         {
    4948              :           /* This should be allowed; the draft says a derived-type cannot
    4949              :              have type parameters if it is has the BIND attribute.  Type
    4950              :              parameters seem to be for making parameterized derived types.
    4951              :              There's no need to verify the type if it is c_ptr/c_funptr.  */
    4952           16 :           retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
    4953              :         }
    4954              :       else
    4955              :         {
    4956              :           /* Grab the typespec for the given component and test the kind.  */
    4957          837 :           is_c_interop = gfc_verify_c_interop (&(curr_comp->ts));
    4958              : 
    4959          837 :           if (!is_c_interop)
    4960              :             {
    4961              :               /* Report warning and continue since not fatal.  The
    4962              :                  draft does specify a constraint that requires all fields
    4963              :                  to interoperate, but if the user says real(4), etc., it
    4964              :                  may interoperate with *something* in C, but the compiler
    4965              :                  most likely won't know exactly what.  Further, it may not
    4966              :                  interoperate with the same data type(s) in C if the user
    4967              :                  recompiles with different flags (e.g., -m32 and -m64 on
    4968              :                  x86_64 and using integer(4) to claim interop with a
    4969              :                  C_LONG).  */
    4970           34 :               if (derived_sym->attr.is_bind_c == 1 && warn_c_binding_type)
    4971              :                 /* If the derived type is bind(c), all fields must be
    4972              :                    interop.  */
    4973            1 :                 gfc_warning (OPT_Wc_binding_type,
    4974              :                              "Component %qs in derived type %qs at %L "
    4975              :                              "may not be C interoperable, even though "
    4976              :                              "derived type %qs is BIND(C)",
    4977              :                              curr_comp->name, derived_sym->name,
    4978              :                              &(curr_comp->loc), derived_sym->name);
    4979           33 :               else if (warn_c_binding_type)
    4980              :                 /* If derived type is param to bind(c) routine, or to one
    4981              :                    of the iso_c_binding procs, it must be interoperable, so
    4982              :                    all fields must interop too.  */
    4983            0 :                 gfc_warning (OPT_Wc_binding_type,
    4984              :                              "Component %qs in derived type %qs at %L "
    4985              :                              "may not be C interoperable",
    4986              :                              curr_comp->name, derived_sym->name,
    4987              :                              &(curr_comp->loc));
    4988              :             }
    4989              :         }
    4990              : 
    4991          853 :       curr_comp = curr_comp->next;
    4992          853 :     } while (curr_comp != NULL);
    4993              : 
    4994          404 :   if (derived_sym->attr.sequence != 0)
    4995              :     {
    4996            0 :       gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
    4997              :                  "attribute because it is BIND(C)", derived_sym->name,
    4998              :                  &(derived_sym->declared_at));
    4999            0 :       retval = false;
    5000              :     }
    5001              : 
    5002              :   /* Mark the derived type as not being C interoperable if we found an
    5003              :      error.  If there were only warnings, proceed with the assumption
    5004              :      it's interoperable.  */
    5005          404 :   if (!retval)
    5006            8 :     derived_sym->ts.is_c_interop = 0;
    5007              : 
    5008              :   return retval;
    5009              : }
    5010              : 
    5011              : 
    5012              : /* Generate symbols for the named constants c_null_ptr and c_null_funptr.  */
    5013              : 
    5014              : static bool
    5015         6592 : gen_special_c_interop_ptr (gfc_symbol *tmp_sym, gfc_symtree *dt_symtree)
    5016              : {
    5017         6592 :   gfc_constructor *c;
    5018              : 
    5019         6592 :   gcc_assert (tmp_sym && dt_symtree && dt_symtree->n.sym);
    5020         6592 :   dt_symtree->n.sym->attr.referenced = 1;
    5021              : 
    5022         6592 :   tmp_sym->attr.is_c_interop = 1;
    5023         6592 :   tmp_sym->attr.is_bind_c = 1;
    5024         6592 :   tmp_sym->ts.is_c_interop = 1;
    5025         6592 :   tmp_sym->ts.is_iso_c = 1;
    5026         6592 :   tmp_sym->ts.type = BT_DERIVED;
    5027         6592 :   tmp_sym->ts.f90_type = BT_VOID;
    5028         6592 :   tmp_sym->attr.flavor = FL_PARAMETER;
    5029         6592 :   tmp_sym->ts.u.derived = dt_symtree->n.sym;
    5030              : 
    5031              :   /* Set the c_address field of c_null_ptr and c_null_funptr to
    5032              :      the value of NULL.  */
    5033         6592 :   tmp_sym->value = gfc_get_expr ();
    5034         6592 :   tmp_sym->value->expr_type = EXPR_STRUCTURE;
    5035         6592 :   tmp_sym->value->ts.type = BT_DERIVED;
    5036         6592 :   tmp_sym->value->ts.f90_type = BT_VOID;
    5037         6592 :   tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
    5038         6592 :   gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
    5039         6592 :   c = gfc_constructor_first (tmp_sym->value->value.constructor);
    5040         6592 :   c->expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
    5041         6592 :   c->expr->ts.is_iso_c = 1;
    5042              : 
    5043         6592 :   return true;
    5044              : }
    5045              : 
    5046              : 
    5047              : /* Add a formal argument, gfc_formal_arglist, to the
    5048              :    end of the given list of arguments.  Set the reference to the
    5049              :    provided symbol, param_sym, in the argument.  */
    5050              : 
    5051              : static void
    5052       109478 : add_formal_arg (gfc_formal_arglist **head,
    5053              :                 gfc_formal_arglist **tail,
    5054              :                 gfc_formal_arglist *formal_arg,
    5055              :                 gfc_symbol *param_sym)
    5056              : {
    5057              :   /* Put in list, either as first arg or at the tail (curr arg).  */
    5058            0 :   if (*head == NULL)
    5059            0 :     *head = *tail = formal_arg;
    5060              :   else
    5061              :     {
    5062        67188 :       (*tail)->next = formal_arg;
    5063        67188 :       (*tail) = formal_arg;
    5064              :     }
    5065              : 
    5066       109478 :   (*tail)->sym = param_sym;
    5067       109478 :   (*tail)->next = NULL;
    5068              : 
    5069       109478 :   return;
    5070              : }
    5071              : 
    5072              : 
    5073              : /* Add a procedure interface to the given symbol (i.e., store a
    5074              :    reference to the list of formal arguments).  */
    5075              : 
    5076              : static void
    5077        43038 : add_proc_interface (gfc_symbol *sym, ifsrc source, gfc_formal_arglist *formal)
    5078              : {
    5079              : 
    5080        43038 :   sym->formal = formal;
    5081        43038 :   sym->attr.if_source = source;
    5082            0 : }
    5083              : 
    5084              : 
    5085              : /* Copy the formal args from an existing symbol, src, into a new
    5086              :    symbol, dest.  New formal args are created, and the description of
    5087              :    each arg is set according to the existing ones.  This function is
    5088              :    used when creating procedure declaration variables from a procedure
    5089              :    declaration statement (see match_proc_decl()) to create the formal
    5090              :    args based on the args of a given named interface.
    5091              : 
    5092              :    When an actual argument list is provided, skip the absent arguments
    5093              :    unless copy_type is true.
    5094              :    To be used together with gfc_se->ignore_optional.  */
    5095              : 
    5096              : void
    5097        43038 : gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
    5098              :                            gfc_actual_arglist *actual, bool copy_type)
    5099              : {
    5100        43038 :   gfc_formal_arglist *head = NULL;
    5101        43038 :   gfc_formal_arglist *tail = NULL;
    5102        43038 :   gfc_formal_arglist *formal_arg = NULL;
    5103        43038 :   gfc_intrinsic_arg *curr_arg = NULL;
    5104        43038 :   gfc_formal_arglist *formal_prev = NULL;
    5105        43038 :   gfc_actual_arglist *act_arg = actual;
    5106              :   /* Save current namespace so we can change it for formal args.  */
    5107        43038 :   gfc_namespace *parent_ns = gfc_current_ns;
    5108              : 
    5109              :   /* Create a new namespace, which will be the formal ns (namespace
    5110              :      of the formal args).  */
    5111        43038 :   gfc_current_ns = gfc_get_namespace (parent_ns, 0);
    5112        43038 :   gfc_current_ns->proc_name = dest;
    5113              : 
    5114       155404 :   for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
    5115              :     {
    5116              :       /* Skip absent arguments.  */
    5117       112366 :       if (actual)
    5118              :         {
    5119        14924 :           gcc_assert (act_arg != NULL);
    5120        14924 :           if (act_arg->expr == NULL)
    5121              :             {
    5122         2888 :               act_arg = act_arg->next;
    5123         2888 :               continue;
    5124              :             }
    5125              :         }
    5126       109478 :       formal_arg = gfc_get_formal_arglist ();
    5127       109478 :       gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
    5128              : 
    5129              :       /* May need to copy more info for the symbol.  */
    5130       109478 :       if (copy_type && act_arg->expr != NULL)
    5131              :         {
    5132         5762 :           formal_arg->sym->ts = act_arg->expr->ts;
    5133         5762 :           if (act_arg->expr->rank > 0)
    5134              :             {
    5135         2595 :               formal_arg->sym->attr.dimension = 1;
    5136         2595 :               formal_arg->sym->as = gfc_get_array_spec();
    5137         2595 :               formal_arg->sym->as->rank = -1;
    5138         2595 :               formal_arg->sym->as->type = AS_ASSUMED_RANK;
    5139              :             }
    5140         5762 :           if (act_arg->name && strcmp (act_arg->name, "%VAL") == 0)
    5141         1316 :             formal_arg->sym->pass_as_value = 1;
    5142              :         }
    5143              :       else
    5144       103716 :         formal_arg->sym->ts = curr_arg->ts;
    5145              : 
    5146       109478 :       formal_arg->sym->attr.optional = curr_arg->optional;
    5147       109478 :       formal_arg->sym->attr.value = curr_arg->value;
    5148       109478 :       formal_arg->sym->attr.intent = curr_arg->intent;
    5149       109478 :       formal_arg->sym->attr.flavor = FL_VARIABLE;
    5150       109478 :       formal_arg->sym->attr.dummy = 1;
    5151              : 
    5152              :       /* Do not treat an actual deferred-length character argument wrongly
    5153              :          as template for the formal argument.  */
    5154       109478 :       if (formal_arg->sym->ts.type == BT_CHARACTER
    5155         8281 :           && !(formal_arg->sym->attr.allocatable
    5156         8281 :                || formal_arg->sym->attr.pointer))
    5157         8281 :         formal_arg->sym->ts.deferred = false;
    5158              : 
    5159       109478 :       if (formal_arg->sym->ts.type == BT_CHARACTER)
    5160         8281 :         formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5161              : 
    5162              :       /* If this isn't the first arg, set up the next ptr.  For the
    5163              :         last arg built, the formal_arg->next will never get set to
    5164              :         anything other than NULL.  */
    5165       109478 :       if (formal_prev != NULL)
    5166        67188 :         formal_prev->next = formal_arg;
    5167              :       else
    5168              :         formal_arg->next = NULL;
    5169              : 
    5170       109478 :       formal_prev = formal_arg;
    5171              : 
    5172              :       /* Add arg to list of formal args.  */
    5173       109478 :       add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
    5174              : 
    5175              :       /* Validate changes.  */
    5176       109478 :       gfc_commit_symbol (formal_arg->sym);
    5177       109478 :       if (actual)
    5178        12036 :         act_arg = act_arg->next;
    5179              :     }
    5180              : 
    5181              :   /* Add the interface to the symbol.  */
    5182        43038 :   add_proc_interface (dest, IFSRC_DECL, head);
    5183              : 
    5184              :   /* Store the formal namespace information.  */
    5185        43038 :   if (dest->formal != NULL)
    5186              :     /* The current ns should be that for the dest proc.  */
    5187        42290 :     dest->formal_ns = gfc_current_ns;
    5188              :   else
    5189          748 :     gfc_free_namespace (gfc_current_ns);
    5190              :   /* Restore the current namespace to what it was on entry.  */
    5191        43038 :   gfc_current_ns = parent_ns;
    5192        43038 : }
    5193              : 
    5194              : 
    5195              : static int
    5196       160202 : std_for_isocbinding_symbol (int id)
    5197              : {
    5198            0 :   switch (id)
    5199              :     {
    5200              : #define NAMED_INTCST(a,b,c,d) \
    5201              :       case a:\
    5202              :         return d;
    5203              : #include "iso-c-binding.def"
    5204              : #undef NAMED_INTCST
    5205              : 
    5206              : #define NAMED_UINTCST(a,b,c,d) \
    5207              :       case a:\
    5208              :         return d;
    5209              : #include "iso-c-binding.def"
    5210              : #undef NAMED_UINTCST
    5211              : 
    5212              : #define NAMED_FUNCTION(a,b,c,d) \
    5213              :       case a:\
    5214              :         return d;
    5215              : #define NAMED_SUBROUTINE(a,b,c,d) \
    5216              :       case a:\
    5217              :         return d;
    5218              : #include "iso-c-binding.def"
    5219              : #undef NAMED_FUNCTION
    5220              : #undef NAMED_SUBROUTINE
    5221              : 
    5222              :        default:
    5223              :          return GFC_STD_F2003;
    5224              :     }
    5225              : }
    5226              : 
    5227              : /* Generate the given set of C interoperable kind objects, or all
    5228              :    interoperable kinds.  This function will only be given kind objects
    5229              :    for valid iso_c_binding defined types because this is verified when
    5230              :    the 'use' statement is parsed.  If the user gives an 'only' clause,
    5231              :    the specific kinds are looked up; if they don't exist, an error is
    5232              :    reported.  If the user does not give an 'only' clause, all
    5233              :    iso_c_binding symbols are generated.  If a list of specific kinds
    5234              :    is given, it must have a NULL in the first empty spot to mark the
    5235              :    end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
    5236              :    point to the symtree for c_(fun)ptr.  */
    5237              : 
    5238              : gfc_symtree *
    5239       160202 : generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
    5240              :                              const char *local_name, gfc_symtree *dt_symtree,
    5241              :                              bool hidden)
    5242              : {
    5243       160202 :   const char *const name = (local_name && local_name[0])
    5244       160202 :                            ? local_name : c_interop_kinds_table[s].name;
    5245       160202 :   gfc_symtree *tmp_symtree;
    5246       160202 :   gfc_symbol *tmp_sym = NULL;
    5247       160202 :   int index;
    5248              : 
    5249       298419 :   if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
    5250              :     return NULL;
    5251              : 
    5252       160202 :   tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
    5253       160202 :   if (hidden
    5254           48 :       && (!tmp_symtree || !tmp_symtree->n.sym
    5255           14 :           || tmp_symtree->n.sym->from_intmod != INTMOD_ISO_C_BINDING
    5256           14 :           || tmp_symtree->n.sym->intmod_sym_id != s))
    5257           34 :     tmp_symtree = NULL;
    5258              : 
    5259              :   /* Already exists in this scope so don't re-add it.  */
    5260          318 :   if (tmp_symtree != NULL && (tmp_sym = tmp_symtree->n.sym) != NULL
    5261          318 :       && (!tmp_sym->attr.generic
    5262           52 :           || (tmp_sym = gfc_find_dt_in_generic (tmp_sym)) != NULL)
    5263       160520 :       && tmp_sym->from_intmod == INTMOD_ISO_C_BINDING)
    5264              :     {
    5265          318 :       if (tmp_sym->attr.flavor == FL_DERIVED
    5266          318 :           && !get_iso_c_binding_dt (tmp_sym->intmod_sym_id))
    5267              :         {
    5268           52 :           if (gfc_derived_types)
    5269              :             {
    5270           25 :               tmp_sym->dt_next = gfc_derived_types->dt_next;
    5271           25 :               gfc_derived_types->dt_next = tmp_sym;
    5272              :             }
    5273              :           else
    5274              :             {
    5275           27 :               tmp_sym->dt_next = tmp_sym;
    5276              :             }
    5277           52 :           gfc_derived_types = tmp_sym;
    5278              :         }
    5279              : 
    5280          318 :       return tmp_symtree;
    5281              :     }
    5282              : 
    5283              :   /* Create the sym tree in the current ns.  */
    5284       159884 :   if (hidden)
    5285              :     {
    5286           34 :       tmp_symtree = gfc_get_unique_symtree (gfc_current_ns);
    5287           34 :       tmp_sym = gfc_new_symbol (name, gfc_current_ns);
    5288              : 
    5289              :       /* Add to the list of tentative symbols.  */
    5290           34 :       latest_undo_chgset->syms.safe_push (tmp_sym);
    5291           34 :       tmp_sym->old_symbol = NULL;
    5292           34 :       tmp_sym->mark = 1;
    5293           34 :       tmp_sym->gfc_new = 1;
    5294              : 
    5295           34 :       tmp_symtree->n.sym = tmp_sym;
    5296           34 :       tmp_sym->refs++;
    5297              :     }
    5298              :   else
    5299              :     {
    5300       159850 :       gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
    5301       159850 :       gcc_assert (tmp_symtree);
    5302       159850 :       tmp_sym = tmp_symtree->n.sym;
    5303              :     }
    5304              : 
    5305              :   /* Say what module this symbol belongs to.  */
    5306       159884 :   tmp_sym->module = gfc_get_string ("%s", mod_name);
    5307       159884 :   tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
    5308       159884 :   tmp_sym->intmod_sym_id = s;
    5309       159884 :   tmp_sym->attr.is_iso_c = 1;
    5310       159884 :   tmp_sym->attr.use_assoc = 1;
    5311              : 
    5312       159884 :   gcc_assert (dt_symtree == NULL || s == ISOCBINDING_NULL_FUNPTR
    5313              :               || s == ISOCBINDING_NULL_PTR);
    5314              : 
    5315       156562 :   switch (s)
    5316              :     {
    5317              : 
    5318              : #define NAMED_INTCST(a,b,c,d) case a :
    5319              : #define NAMED_UINTCST(a,b,c,d) case a :
    5320              : #define NAMED_REALCST(a,b,c,d) case a :
    5321              : #define NAMED_CMPXCST(a,b,c,d) case a :
    5322              : #define NAMED_LOGCST(a,b,c) case a :
    5323              : #define NAMED_CHARKNDCST(a,b,c) case a :
    5324              : #include "iso-c-binding.def"
    5325              : 
    5326       236594 :         tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
    5327       118297 :                                            c_interop_kinds_table[s].value);
    5328              : 
    5329              :         /* Initialize an integer constant expression node.  */
    5330       118297 :         tmp_sym->attr.flavor = FL_PARAMETER;
    5331       118297 :         tmp_sym->ts.type = BT_INTEGER;
    5332       118297 :         tmp_sym->ts.kind = gfc_default_integer_kind;
    5333              : 
    5334              :         /* Mark this type as a C interoperable one.  */
    5335       118297 :         tmp_sym->ts.is_c_interop = 1;
    5336       118297 :         tmp_sym->ts.is_iso_c = 1;
    5337       118297 :         tmp_sym->value->ts.is_c_interop = 1;
    5338       118297 :         tmp_sym->value->ts.is_iso_c = 1;
    5339       118297 :         tmp_sym->attr.is_c_interop = 1;
    5340              : 
    5341              :         /* Tell what f90 type this c interop kind is valid.  */
    5342       118297 :         tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
    5343              : 
    5344       118297 :         break;
    5345              : 
    5346              : 
    5347              : #define NAMED_CHARCST(a,b,c) case a :
    5348              : #include "iso-c-binding.def"
    5349              : 
    5350              :         /* Initialize an integer constant expression node for the
    5351              :            length of the character.  */
    5352        26100 :         tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
    5353              :                                                  &gfc_current_locus, NULL, 1);
    5354        26100 :         tmp_sym->value->ts.is_c_interop = 1;
    5355        26100 :         tmp_sym->value->ts.is_iso_c = 1;
    5356        26100 :         tmp_sym->value->value.character.length = 1;
    5357        26100 :         tmp_sym->value->value.character.string[0]
    5358        26100 :           = (gfc_char_t) c_interop_kinds_table[s].value;
    5359        26100 :         tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5360        26100 :         tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    5361              :                                                      NULL, 1);
    5362              : 
    5363              :         /* May not need this in both attr and ts, but do need in
    5364              :            attr for writing module file.  */
    5365        26100 :         tmp_sym->attr.is_c_interop = 1;
    5366              : 
    5367        26100 :         tmp_sym->attr.flavor = FL_PARAMETER;
    5368        26100 :         tmp_sym->ts.type = BT_CHARACTER;
    5369              : 
    5370              :         /* Need to set it to the C_CHAR kind.  */
    5371        26100 :         tmp_sym->ts.kind = gfc_default_character_kind;
    5372              : 
    5373              :         /* Mark this type as a C interoperable one.  */
    5374        26100 :         tmp_sym->ts.is_c_interop = 1;
    5375        26100 :         tmp_sym->ts.is_iso_c = 1;
    5376              : 
    5377              :         /* Tell what f90 type this c interop kind is valid.  */
    5378        26100 :         tmp_sym->ts.f90_type = BT_CHARACTER;
    5379              : 
    5380        26100 :         break;
    5381              : 
    5382         8895 :       case ISOCBINDING_PTR:
    5383         8895 :       case ISOCBINDING_FUNPTR:
    5384         8895 :         {
    5385         8895 :           gfc_symbol *dt_sym;
    5386         8895 :           gfc_component *tmp_comp = NULL;
    5387              : 
    5388              :           /* Generate real derived type.  */
    5389         8895 :           if (hidden)
    5390              :             dt_sym = tmp_sym;
    5391              :           else
    5392              :             {
    5393         8861 :               const char *hidden_name;
    5394         8861 :               gfc_interface *intr, *head;
    5395              : 
    5396         8861 :               hidden_name = gfc_dt_upper_string (tmp_sym->name);
    5397         8861 :               tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
    5398              :                                               hidden_name);
    5399         8861 :               gcc_assert (tmp_symtree == NULL);
    5400         8861 :               gfc_get_sym_tree (hidden_name, gfc_current_ns, &tmp_symtree, false);
    5401         8861 :               dt_sym = tmp_symtree->n.sym;
    5402        12222 :               dt_sym->name = gfc_get_string (s == ISOCBINDING_PTR
    5403              :                                              ? "c_ptr" : "c_funptr");
    5404              : 
    5405              :               /* Generate an artificial generic function.  */
    5406         8861 :               head = tmp_sym->generic;
    5407         8861 :               intr = gfc_get_interface ();
    5408         8861 :               intr->sym = dt_sym;
    5409         8861 :               intr->where = gfc_current_locus;
    5410         8861 :               intr->next = head;
    5411         8861 :               tmp_sym->generic = intr;
    5412              : 
    5413         8861 :               if (!tmp_sym->attr.generic
    5414         8861 :                   && !gfc_add_generic (&tmp_sym->attr, tmp_sym->name, NULL))
    5415            0 :                 return NULL;
    5416              : 
    5417         8861 :               if (!tmp_sym->attr.function
    5418         8861 :                   && !gfc_add_function (&tmp_sym->attr, tmp_sym->name, NULL))
    5419              :                 return NULL;
    5420              :             }
    5421              : 
    5422              :           /* Say what module this symbol belongs to.  */
    5423         8895 :           dt_sym->module = gfc_get_string ("%s", mod_name);
    5424         8895 :           dt_sym->from_intmod = INTMOD_ISO_C_BINDING;
    5425         8895 :           dt_sym->intmod_sym_id = s;
    5426         8895 :           dt_sym->attr.use_assoc = 1;
    5427              : 
    5428              :           /* Initialize an integer constant expression node.  */
    5429         8895 :           dt_sym->attr.flavor = FL_DERIVED;
    5430         8895 :           dt_sym->ts.is_c_interop = 1;
    5431         8895 :           dt_sym->attr.is_c_interop = 1;
    5432         8895 :           dt_sym->attr.private_comp = 1;
    5433         8895 :           dt_sym->component_access = ACCESS_PRIVATE;
    5434         8895 :           dt_sym->ts.is_iso_c = 1;
    5435         8895 :           dt_sym->ts.type = BT_DERIVED;
    5436         8895 :           dt_sym->ts.f90_type = BT_VOID;
    5437              : 
    5438              :           /* A derived type must have the bind attribute to be
    5439              :              interoperable (J3/04-007, Section 15.2.3), even though
    5440              :              the binding label is not used.  */
    5441         8895 :           dt_sym->attr.is_bind_c = 1;
    5442              : 
    5443         8895 :           dt_sym->attr.referenced = 1;
    5444         8895 :           dt_sym->ts.u.derived = dt_sym;
    5445              : 
    5446              :           /* Add the symbol created for the derived type to the current ns.  */
    5447         8895 :           if (gfc_derived_types)
    5448              :             {
    5449         6830 :               dt_sym->dt_next = gfc_derived_types->dt_next;
    5450         6830 :               gfc_derived_types->dt_next = dt_sym;
    5451              :             }
    5452              :           else
    5453              :             {
    5454         2065 :               dt_sym->dt_next = dt_sym;
    5455              :             }
    5456         8895 :           gfc_derived_types = dt_sym;
    5457              : 
    5458         8895 :           gfc_add_component (dt_sym, "c_address", &tmp_comp);
    5459         8895 :           if (tmp_comp == NULL)
    5460            0 :             gcc_unreachable ();
    5461              : 
    5462         8895 :           tmp_comp->ts.type = BT_INTEGER;
    5463              : 
    5464              :           /* Set this because the module will need to read/write this field.  */
    5465         8895 :           tmp_comp->ts.f90_type = BT_INTEGER;
    5466              : 
    5467              :           /* The kinds for c_ptr and c_funptr are the same.  */
    5468         8895 :           index = get_c_kind ("c_ptr", c_interop_kinds_table);
    5469         8895 :           tmp_comp->ts.kind = c_interop_kinds_table[index].value;
    5470         8895 :           tmp_comp->attr.access = ACCESS_PRIVATE;
    5471              : 
    5472              :           /* Mark the component as C interoperable.  */
    5473         8895 :           tmp_comp->ts.is_c_interop = 1;
    5474              :         }
    5475              : 
    5476         8895 :         break;
    5477              : 
    5478         6592 :       case ISOCBINDING_NULL_PTR:
    5479         6592 :       case ISOCBINDING_NULL_FUNPTR:
    5480         6592 :         gen_special_c_interop_ptr (tmp_sym, dt_symtree);
    5481         6592 :         break;
    5482              : 
    5483            0 :       default:
    5484            0 :         gcc_unreachable ();
    5485              :     }
    5486       159884 :   gfc_commit_symbol (tmp_sym);
    5487       159884 :   return tmp_symtree;
    5488              : }
    5489              : 
    5490              : 
    5491              : /* Check that a symbol is already typed.  If strict is not set, an untyped
    5492              :    symbol is acceptable for non-standard-conforming mode.  */
    5493              : 
    5494              : bool
    5495        15151 : gfc_check_symbol_typed (gfc_symbol* sym, gfc_namespace* ns,
    5496              :                         bool strict, locus where)
    5497              : {
    5498        15151 :   gcc_assert (sym);
    5499              : 
    5500        15151 :   if (gfc_matching_prefix)
    5501              :     return true;
    5502              : 
    5503              :   /* Check for the type and try to give it an implicit one.  */
    5504        15108 :   if (sym->ts.type == BT_UNKNOWN
    5505        15108 :       && !gfc_set_default_type (sym, 0, ns))
    5506              :     {
    5507          451 :       if (strict)
    5508              :         {
    5509           11 :           gfc_error ("Symbol %qs is used before it is typed at %L",
    5510              :                      sym->name, &where);
    5511           11 :           return false;
    5512              :         }
    5513              : 
    5514          440 :       if (!gfc_notify_std (GFC_STD_GNU, "Symbol %qs is used before"
    5515              :                            " it is typed at %L", sym->name, &where))
    5516              :         return false;
    5517              :     }
    5518              : 
    5519              :   /* Everything is ok.  */
    5520              :   return true;
    5521              : }
    5522              : 
    5523              : 
    5524              : /* Construct a typebound-procedure structure.  Those are stored in a tentative
    5525              :    list and marked `error' until symbols are committed.  */
    5526              : 
    5527              : gfc_typebound_proc*
    5528        62202 : gfc_get_typebound_proc (gfc_typebound_proc *tb0)
    5529              : {
    5530        62202 :   gfc_typebound_proc *result;
    5531              : 
    5532        62202 :   result = XCNEW (gfc_typebound_proc);
    5533        62202 :   if (tb0)
    5534         3299 :     *result = *tb0;
    5535        62202 :   result->error = 1;
    5536              : 
    5537        62202 :   latest_undo_chgset->tbps.safe_push (result);
    5538              : 
    5539        62202 :   return result;
    5540              : }
    5541              : 
    5542              : 
    5543              : /* Get the super-type of a given derived type.  */
    5544              : 
    5545              : gfc_symbol*
    5546       828543 : gfc_get_derived_super_type (gfc_symbol* derived)
    5547              : {
    5548       828543 :   gcc_assert (derived);
    5549              : 
    5550       828543 :   if (derived->attr.generic)
    5551            3 :     derived = gfc_find_dt_in_generic (derived);
    5552              : 
    5553       828543 :   if (!derived->attr.extension)
    5554              :     return NULL;
    5555              : 
    5556       159715 :   gcc_assert (derived->components);
    5557       159715 :   gcc_assert (derived->components->ts.type == BT_DERIVED);
    5558       159715 :   gcc_assert (derived->components->ts.u.derived);
    5559              : 
    5560       159715 :   if (derived->components->ts.u.derived->attr.generic)
    5561            0 :     return gfc_find_dt_in_generic (derived->components->ts.u.derived);
    5562              : 
    5563              :   return derived->components->ts.u.derived;
    5564              : }
    5565              : 
    5566              : 
    5567              : /* Check if a derived type t2 is an extension of (or equal to) a type t1.  */
    5568              : 
    5569              : bool
    5570        32143 : gfc_type_is_extension_of (gfc_symbol *t1, gfc_symbol *t2)
    5571              : {
    5572        36520 :   while (!gfc_compare_derived_types (t1, t2) && t2->attr.extension)
    5573         4377 :     t2 = gfc_get_derived_super_type (t2);
    5574        32143 :   return gfc_compare_derived_types (t1, t2);
    5575              : }
    5576              : 
    5577              : /* Check if parameterized derived type t2 is an instance of pdt template t1
    5578              : 
    5579              :    gfc_symbol *t1 -> pdt template to verify t2 against.
    5580              :    gfc_symbol *t2 -> pdt instance to be verified.
    5581              : 
    5582              :    In decl.cc, gfc_get_pdt_instance, a pdt instance is given a 3 character
    5583              :    prefix PDT_PREFIX, followed by an underscore list of the kind parameters,
    5584              :    up to a maximum of 8 kind parameters.  To verify if a PDT Type corresponds
    5585              :    to the template, this functions extracts t2's derive_type name,
    5586              :    and compares it to the derive_type name of t1 for compatibility.
    5587              : 
    5588              :    For example:
    5589              : 
    5590              :    t2->name = PDT_PREFIXf_2_2; extract the 'f' and compare with t1->name.  */
    5591              : 
    5592              : bool
    5593           18 : gfc_pdt_is_instance_of (gfc_symbol *t1, gfc_symbol *t2)
    5594              : {
    5595           18 :   if ( !t1->attr.pdt_template || !t2->attr.pdt_type )
    5596              :     return false;
    5597              : 
    5598              :   /* Limit comparison to length of t1->name to ignore new kind params.  */
    5599           18 :   if ( !(strncmp (&(t2->name[PDT_PREFIX_LEN]), t1->name,
    5600              :                   strlen (t1->name)) == 0) )
    5601            0 :     return false;
    5602              : 
    5603              :   return true;
    5604              : }
    5605              : 
    5606              : /* Check if two typespecs are type compatible (F03:5.1.1.2):
    5607              :    If ts1 is nonpolymorphic, ts2 must be the same type.
    5608              :    If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1.  */
    5609              : 
    5610              : bool
    5611       303301 : gfc_type_compatible (gfc_typespec *ts1, gfc_typespec *ts2)
    5612              : {
    5613       303301 :   bool is_class1 = (ts1->type == BT_CLASS);
    5614       303301 :   bool is_class2 = (ts2->type == BT_CLASS);
    5615       303301 :   bool is_derived1 = (ts1->type == BT_DERIVED);
    5616       303301 :   bool is_derived2 = (ts2->type == BT_DERIVED);
    5617       303301 :   bool is_union1 = (ts1->type == BT_UNION);
    5618       303301 :   bool is_union2 = (ts2->type == BT_UNION);
    5619              : 
    5620              :   /* A boz-literal-constant has no type.  */
    5621       303301 :   if (ts1->type == BT_BOZ || ts2->type == BT_BOZ)
    5622              :     return false;
    5623              : 
    5624       303299 :   if (is_class1
    5625        30653 :       && ts1->u.derived->components
    5626        30493 :       && ((ts1->u.derived->attr.is_class
    5627        30486 :            && ts1->u.derived->components->ts.u.derived->attr
    5628        30486 :                                                         .unlimited_polymorphic)
    5629        29617 :           || ts1->u.derived->attr.unlimited_polymorphic))
    5630              :     return 1;
    5631              : 
    5632       302423 :   if (!is_derived1 && !is_derived2 && !is_class1 && !is_class2
    5633         2460 :       && !is_union1 && !is_union2)
    5634         2460 :     return (ts1->type == ts2->type);
    5635              : 
    5636       299963 :   if ((is_derived1 && is_derived2) || (is_union1 && is_union2))
    5637       269005 :     return gfc_compare_derived_types (ts1->u.derived, ts2->u.derived);
    5638              : 
    5639        30958 :   if (is_derived1 && is_class2)
    5640         1177 :     return gfc_compare_derived_types (ts1->u.derived,
    5641         1177 :                                       ts2->u.derived->attr.is_class ?
    5642         1174 :                                       ts2->u.derived->components->ts.u.derived
    5643         1177 :                                       : ts2->u.derived);
    5644        29781 :   if (is_class1 && is_derived2)
    5645        10384 :     return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
    5646        10383 :                                        ts1->u.derived->components->ts.u.derived
    5647              :                                      : ts1->u.derived,
    5648        20768 :                                      ts2->u.derived);
    5649        19397 :   else if (is_class1 && is_class2)
    5650        38620 :     return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
    5651        19227 :                                        ts1->u.derived->components->ts.u.derived
    5652              :                                      : ts1->u.derived,
    5653        19393 :                                      ts2->u.derived->attr.is_class ?
    5654        19228 :                                        ts2->u.derived->components->ts.u.derived
    5655        19393 :                                      : ts2->u.derived);
    5656              :   else
    5657              :     return 0;
    5658              : }
    5659              : 
    5660              : 
    5661              : /* Find the parent-namespace of the current function.  If we're inside
    5662              :    BLOCK constructs, it may not be the current one.  */
    5663              : 
    5664              : gfc_namespace*
    5665        67470 : gfc_find_proc_namespace (gfc_namespace* ns)
    5666              : {
    5667        68050 :   while (ns->construct_entities)
    5668              :     {
    5669          580 :       ns = ns->parent;
    5670          580 :       gcc_assert (ns);
    5671              :     }
    5672              : 
    5673        67470 :   return ns;
    5674              : }
    5675              : 
    5676              : 
    5677              : /* Check if an associate-variable should be translated as an `implicit' pointer
    5678              :    internally (if it is associated to a variable and not an array with
    5679              :    descriptor).  */
    5680              : 
    5681              : bool
    5682       518295 : gfc_is_associate_pointer (gfc_symbol* sym)
    5683              : {
    5684       518295 :   if (!sym->assoc)
    5685              :     return false;
    5686              : 
    5687        13478 :   if (sym->ts.type == BT_CLASS)
    5688              :     return true;
    5689              : 
    5690         7464 :   if (sym->ts.type == BT_CHARACTER
    5691         1374 :       && sym->ts.deferred
    5692          122 :       && sym->assoc->target
    5693          122 :       && sym->assoc->target->expr_type == EXPR_FUNCTION)
    5694              :     return true;
    5695              : 
    5696         7452 :   if (!sym->assoc->variable)
    5697              :     return false;
    5698              : 
    5699         6220 :   if ((sym->attr.dimension || sym->attr.codimension)
    5700            0 :       && sym->as->type != AS_EXPLICIT)
    5701            0 :     return false;
    5702              : 
    5703              :   return true;
    5704              : }
    5705              : 
    5706              : 
    5707              : /* Check if the actual argument of a dummy must be passed directly, rather
    5708              :    than through a copy-in/copy-out temporary.  A pointer associated with an
    5709              :    assumed shape or assumed rank TARGET dummy has to stay valid after the
    5710              :    call, so its actual argument is never copied and its elements can be
    5711              :    spaced by more than the element size.  CLASS and assumed type entities
    5712              :    already carry their element size and are excluded.  */
    5713              : 
    5714              : bool
    5715       622041 : gfc_dummy_requires_direct_arg (gfc_symbol *sym)
    5716              : {
    5717       622041 :   return sym->attr.dummy
    5718       416704 :          && sym->attr.target
    5719        29804 :          && !sym->attr.value
    5720        28266 :          && !sym->attr.contiguous
    5721        27610 :          && !IS_POINTER (sym)
    5722        27610 :          && sym->ts.type != BT_ASSUMED
    5723       649317 :          && ((sym->ts.type == BT_CLASS
    5724         3079 :               && sym->ts.u.derived->attr.is_class
    5725         3079 :               && CLASS_DATA (sym)->attr.dimension
    5726         1606 :               && !CLASS_DATA (sym)->attr.allocatable
    5727         1384 :               && CLASS_DATA (sym)->as
    5728         1384 :               && (CLASS_DATA (sym)->as->type == AS_ASSUMED_SHAPE
    5729           16 :                   || CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK))
    5730        25892 :              || (!(sym->ts.type == BT_CLASS
    5731         1695 :                    && sym->ts.u.derived->attr.is_class)
    5732        24197 :                  && sym->attr.dimension
    5733        19448 :                  && !sym->attr.allocatable
    5734        16168 :                  && sym->as
    5735        16168 :                  && (sym->as->type == AS_ASSUMED_SHAPE
    5736         3703 :                      || sym->as->type == AS_ASSUMED_RANK)));
    5737              : }
    5738              : 
    5739              : 
    5740              : gfc_symbol *
    5741        37780 : gfc_find_dt_in_generic (gfc_symbol *sym)
    5742              : {
    5743        37780 :   gfc_interface *intr = NULL;
    5744              : 
    5745        37780 :   if (!sym || gfc_fl_struct (sym->attr.flavor))
    5746              :     return sym;
    5747              : 
    5748        37780 :   if (sym->attr.generic)
    5749        39530 :     for (intr = sym->generic; intr; intr = intr->next)
    5750        25237 :       if (gfc_fl_struct (intr->sym->attr.flavor))
    5751              :         break;
    5752        37778 :   return intr ? intr->sym : NULL;
    5753              : }
    5754              : 
    5755              : 
    5756              : /* Get the dummy arguments from a procedure symbol. If it has been declared
    5757              :    via a PROCEDURE statement with a named interface, ts.interface will be set
    5758              :    and the arguments need to be taken from there.  */
    5759              : 
    5760              : gfc_formal_arglist *
    5761      3841267 : gfc_sym_get_dummy_args (gfc_symbol *sym)
    5762              : {
    5763      3841267 :   gfc_formal_arglist *dummies;
    5764              : 
    5765      3841267 :   if (sym == NULL)
    5766              :     return NULL;
    5767              : 
    5768      3841266 :   dummies = sym->formal;
    5769      3841266 :   if (dummies == NULL && sym->ts.interface != NULL)
    5770         7292 :     dummies = sym->ts.interface->formal;
    5771              : 
    5772              :   return dummies;
    5773              : }
    5774              : 
    5775              : 
    5776              : /* Given a procedure, returns the associated namespace.
    5777              :    The resulting NS should match the condition NS->PROC_NAME == SYM.  */
    5778              : 
    5779              : gfc_namespace *
    5780       814398 : gfc_get_procedure_ns (gfc_symbol *sym)
    5781              : {
    5782       814398 :   if (sym->formal_ns
    5783       622853 :       && sym->formal_ns->proc_name == sym
    5784              :       /* For module procedures used in submodules, there are two namespaces.
    5785              :          The one generated by the host association of the module is directly
    5786              :          accessible through SYM->FORMAL_NS but doesn't have any parent set.
    5787              :          The one generated by the parser is only accessible by walking the
    5788              :          contained namespace but has its parent set.  Prefer the one generated
    5789              :          by the parser below.  */
    5790       622429 :       && !(sym->attr.used_in_submodule
    5791         1064 :            && sym->attr.contained
    5792          479 :            && sym->formal_ns->parent == nullptr))
    5793              :     return sym->formal_ns;
    5794              : 
    5795              :   /* The above should have worked in most cases.  If it hasn't, try some other
    5796              :      heuristics, eventually returning SYM->NS.  */
    5797       192446 :   if (gfc_current_ns->proc_name == sym)
    5798              :     return gfc_current_ns;
    5799              : 
    5800              :   /* For contained procedures, the symbol's NS field is the
    5801              :      hosting namespace, not the procedure namespace.  */
    5802       165757 :   if (sym->attr.flavor == FL_PROCEDURE && sym->attr.contained)
    5803       185036 :     for (gfc_namespace *ns = sym->ns->contained; ns; ns = ns->sibling)
    5804       184682 :       if (ns->proc_name == sym)
    5805              :         return ns;
    5806              : 
    5807       121643 :   if (sym->formal_ns
    5808          424 :       && sym->formal_ns->proc_name == sym)
    5809              :     return sym->formal_ns;
    5810              : 
    5811       121643 :   if (sym->formal)
    5812         3948 :     for (gfc_formal_arglist *f = sym->formal; f != nullptr; f = f->next)
    5813         2290 :       if (f->sym)
    5814              :         {
    5815         2243 :           gfc_namespace *ns = f->sym->ns;
    5816         2243 :           if (ns && ns->proc_name == sym)
    5817              :             return ns;
    5818              :         }
    5819              : 
    5820       121643 :   return sym->ns;
    5821              : }
    5822              : 
    5823              : 
    5824              : /* Given a symbol, returns the namespace in which the symbol is specified.
    5825              :    In most cases, it is the namespace hosting the symbol.  This is the case
    5826              :    for variables.  For functions, however, it is the function namespace
    5827              :    itself.  This specification namespace is used to check conformance of
    5828              :    array spec bound expressions.  */
    5829              : 
    5830              : gfc_namespace *
    5831      1819759 : gfc_get_spec_ns (gfc_symbol *sym)
    5832              : {
    5833      1819759 :   if (sym->attr.flavor == FL_PROCEDURE
    5834       511520 :       && sym->attr.function)
    5835              :     {
    5836       340158 :       if (sym->result == sym)
    5837       247840 :         return gfc_get_procedure_ns (sym);
    5838              :       /* Generic and intrinsic functions can have a null result.  */
    5839        92318 :       else if (sym->result != nullptr)
    5840        38064 :         return sym->result->ns;
    5841              :     }
    5842              : 
    5843      1533855 :   return sym->ns;
    5844              : }
    5845              : 
    5846              : /* This section deals with looking up a symbol when the symtree name and symbol
    5847              :    name do not agree, so gfc_find_symbol() cannot be used.  */
    5848              : 
    5849              : static gfc_symbol* found_sym;           /* Where to store the symbol.  */
    5850              : static const char* sym_target_name;     /* What name to look for.  */
    5851              : 
    5852              : /* Helper function.  */
    5853              : 
    5854              : static void
    5855           26 : compare_target_sym_name (gfc_symbol *sym)
    5856              : {
    5857           26 :   if (strcmp(sym->name, sym_target_name) == 0)
    5858            1 :     found_sym = sym;
    5859           26 : }
    5860              : 
    5861              : /* Search for a symbol when the symtree name may be different from the
    5862              :    symbol name.  Return true if found.  */
    5863              : 
    5864              : bool
    5865            1 : gfc_find_symbol_by_name (const char *name, gfc_namespace *ns,
    5866              :                                gfc_symbol **result)
    5867              : {
    5868            1 :   found_sym = NULL;
    5869            1 :   sym_target_name = name;
    5870              : 
    5871            1 :   do_traverse_symtree (ns->sym_root, NULL, compare_target_sym_name);
    5872            1 :   *result = found_sym;
    5873            1 :   return result != 0;
    5874              : }
    5875              : 
    5876              : /* Note that the value of a variable has been set to a "higher" value and, if
    5877              :    loc is passed, where.  Return true of loc has been changed.  */
    5878              : 
    5879              : bool
    5880       448481 : gfc_value_set_at (gfc_symbol *sym, locus *loc, enum value_set how)
    5881              : {
    5882       448481 :   if (sym == NULL || sym->attr.flavor != FL_VARIABLE)
    5883              :     return false;
    5884              : 
    5885       434171 :   if (how <= sym->attr.value_set)
    5886              :     return false;
    5887              : 
    5888       152909 :   if (loc)
    5889       152909 :     sym->other_loc = *loc;
    5890              :   else
    5891            0 :     memset (&sym->other_loc, 0, sizeof(*loc));
    5892              : 
    5893       152909 :   sym->attr.value_set = how;
    5894       152909 :   return true;
    5895              : }
    5896              : 
    5897              : /* Callback function for setting the "value_used" flag.  We can also set
    5898              :    other_loc here because, in the event of an error message, at most one of
    5899              :    attr.value_used and attr.value_set can be true.  */
    5900              : 
    5901              : static int
    5902      3528477 : mark_vars_as_used (gfc_expr **e, int *walk_subtrees, void *data)
    5903              : {
    5904      3528477 :   gfc_expr *expr = *e;
    5905      3528477 :   gfc_symbol *sym;
    5906      3528477 :   enum value_used how_used = *(enum value_used *) data;
    5907              : 
    5908      3528477 :   if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
    5909              :     return 0;
    5910              : 
    5911      1471812 :   if (expr->symtree == NULL)
    5912              :     return 0;
    5913              : 
    5914              :   /* Some intrinsic functions do not evaluate some (or all) of their
    5915              :      aguments. Do not walk the expressions there.  */
    5916              : 
    5917      1471442 :   if (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym)
    5918              :     {
    5919       207981 :       gfc_actual_arglist *a = expr->value.function.actual;
    5920              : 
    5921       207981 :       switch (expr->value.function.isym->id)
    5922              :         {
    5923        22408 :         case GFC_ISYM_ALLOCATED:
    5924        22408 :         case GFC_ISYM_EXTENDS_TYPE_OF:
    5925        22408 :         case GFC_ISYM_SAME_TYPE_AS:
    5926        22408 :         case GFC_ISYM_ASSOCIATED:
    5927        22408 :         case GFC_ISYM_IS_CONTIGUOUS:
    5928        22408 :         case GFC_ISYM_PRESENT:
    5929        22408 :         case GFC_ISYM_RANK:
    5930        22408 :         case GFC_ISYM_STORAGE_SIZE:
    5931        22408 :         case GFC_ISYM_NULL:
    5932        22408 :           *walk_subtrees = 0;
    5933        22408 :           return 0;
    5934              : 
    5935        20210 :         case GFC_ISYM_LBOUND:
    5936        20210 :         case GFC_ISYM_UBOUND:
    5937        20210 :         case GFC_ISYM_SIZE:
    5938        20210 :           gfc_expr_walker (&a->next->expr, mark_vars_as_used, &how_used);
    5939        20210 :           *walk_subtrees = 0;
    5940        20210 :           return 0;
    5941              : 
    5942         1367 :         case GFC_ISYM_TRANSFER:
    5943              :           /* Source.  */
    5944         1367 :           gfc_expr_walker (&a->expr, mark_vars_as_used, &how_used);
    5945              :           /* Size.  */
    5946         1367 :           gfc_expr_walker (&a->next->next->expr, mark_vars_as_used, &how_used);
    5947         1367 :           *walk_subtrees = 0;
    5948         1367 :           return 0;
    5949              : 
    5950          468 :         case GFC_ISYM_OUT_OF_RANGE:
    5951          468 :           gfc_expr_walker (&a->next->expr, mark_vars_as_used, &how_used);
    5952          468 :           *walk_subtrees = 0;
    5953          468 :           return 0;
    5954              : 
    5955              :         default:
    5956              :           break;
    5957              :         }
    5958              :     }
    5959              : 
    5960      1426989 :   sym = expr->symtree->n.sym;
    5961              : 
    5962      1426989 :   if (sym->attr.flavor != FL_VARIABLE)
    5963              :     return 0;
    5964              : 
    5965      1188759 :   if (how_used <= sym->attr.value_used)
    5966              :     return 0;
    5967              : 
    5968       282896 :   sym->attr.value_used = how_used;
    5969       282896 :   sym->other_loc = expr->where;
    5970              : 
    5971       282896 :   return 0;
    5972              : }
    5973              : 
    5974              : /* Recursively visit every variable and mark it as used.  */
    5975              : 
    5976              : void
    5977      5339130 : gfc_value_used_expr (gfc_expr *expr, enum value_used how_used)
    5978              : {
    5979              : 
    5980      5339130 :   if (expr == NULL)
    5981              :     return;
    5982              : 
    5983      1660957 :   gfc_expr_walker (&expr, mark_vars_as_used, &how_used);
    5984              : }
    5985              : 
    5986              : /* For when we want to set everything in an expression as both
    5987              :    set and used, for example in an actual argument list.  */
    5988              : 
    5989              : void
    5990       172494 : gfc_value_set_and_used (gfc_expr *expr, locus *loc, enum value_set how_set,
    5991              :                         enum value_used how_used)
    5992              : {
    5993       172494 :   if (!expr)
    5994              :     return;
    5995              : 
    5996       172494 :   if (expr->expr_type == EXPR_VARIABLE)
    5997       107873 :     gfc_value_set_at (expr->symtree->n.sym, loc, how_set);
    5998              : 
    5999       172494 :   gfc_value_used_expr (expr, how_used);
    6000              : }
    6001              : 
    6002              : /* ALLOCATE (A(N)) means that N is used, but A is not marked as such.  */
    6003              : 
    6004              : void
    6005        21330 : gfc_used_in_allocate_expr (gfc_expr *expr, locus *loc, enum var_allocated how)
    6006              : {
    6007        21330 :   gfc_symbol *sym;
    6008        21330 :   enum value_used prev_used;
    6009        21330 :   locus prev_loc;
    6010              : 
    6011        21330 :   if (expr->expr_type != EXPR_VARIABLE)
    6012         6095 :     return;
    6013              : 
    6014        21258 :   sym = expr->symtree->n.sym;
    6015        21258 :   prev_used = sym->attr.value_used;
    6016        21258 :   prev_loc = sym->other_loc;
    6017        21258 :   gfc_value_used_expr (expr, VALUE_USED);
    6018        21258 :   sym->attr.value_used = prev_used;
    6019        21258 :   sym->other_loc = prev_loc;
    6020              : 
    6021        21258 :   if (how <= sym->attr.allocated)
    6022              :     return;
    6023              : 
    6024        15235 :   sym->attr.allocated = how;
    6025              : 
    6026        15235 :   if (sym->extra_loc.nextc == NULL)
    6027        15183 :     sym->extra_loc = *loc;
    6028              : }
    6029              : 
    6030              : /* Mark a symbol as allocated.  */
    6031              : 
    6032              : bool
    6033         8552 : gfc_lvalue_allocated_at (gfc_symbol *sym, locus *loc)
    6034              : {
    6035         8552 :   if (sym->other_loc.nextc == 0)
    6036         1225 :     sym->other_loc = *loc;
    6037              : 
    6038         8552 :   sym->attr.allocated = ALLOCATED_ASSIGNMENT;
    6039         8552 :   return true;
    6040              : }
    6041              : 
    6042              : /* Mark the variable of an expression in a vardef context as
    6043              :    set and mark everything in the references as used.  */
    6044              : 
    6045              : void
    6046       334293 : gfc_expr_set_at (gfc_expr *expr, locus *loc, enum value_set how_set)
    6047              : {
    6048       334293 :   enum value_used prev_used;
    6049       334293 :   gfc_symbol *sym;
    6050       334293 :   locus prev_loc;
    6051              : 
    6052       334293 :   if (!expr)
    6053           15 :     return;
    6054              : 
    6055       334293 :   if (expr->expr_type != EXPR_VARIABLE)
    6056              :     return;
    6057              : 
    6058       334278 :   sym = expr->symtree->n.sym;
    6059       334278 :   gfc_value_set_at (sym, loc, how_set);
    6060       334278 :   prev_used = sym->attr.value_used;
    6061       334278 :   prev_loc = sym->other_loc;
    6062       334278 :   gfc_value_used_expr (expr, VALUE_USED);
    6063       334278 :   sym->other_loc = prev_loc;
    6064       334278 :   sym->attr.value_used = prev_used;
    6065              : }
        

Generated by: LCOV version 2.5.0-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.