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

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.