LCOV - code coverage report
Current view: top level - gcc/fortran - symbol.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.4 % 2450 2240
Test Date: 2026-05-30 15:37:04 Functions: 95.5 % 179 171
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        23628 : gfc_set_implicit_none (bool type, bool external, locus *loc)
     127              : {
     128        23628 :   int i;
     129              : 
     130        23628 :   if (external)
     131         1084 :     gfc_current_ns->has_implicit_none_export = 1;
     132              : 
     133        23628 :   if (type)
     134              :     {
     135        23615 :       gfc_current_ns->seen_implicit_none = 1;
     136       637554 :       for (i = 0; i < GFC_LETTERS; i++)
     137              :         {
     138       613941 :           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       613939 :           gfc_clear_ts (&gfc_current_ns->default_type[i]);
     145       613939 :           gfc_current_ns->set_flag[i] = 1;
     146              :         }
     147              :     }
     148              : }
     149              : 
     150              : 
     151              : /* Reset the implicit range flags.  */
     152              : 
     153              : void
     154        24238 : gfc_clear_new_implicit (void)
     155              : {
     156        24238 :   int i;
     157              : 
     158       654426 :   for (i = 0; i < GFC_LETTERS; i++)
     159       630188 :     new_flag[i] = 0;
     160        24238 : }
     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      2967276 : gfc_get_default_type (const char *name, gfc_namespace *ns)
     227              : {
     228      2967276 :   char letter;
     229              : 
     230      2967276 :   letter = name[0];
     231              : 
     232      2967276 :   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      2967276 :   if (letter < 'a' || letter > 'z')
     238            0 :     gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name);
     239              : 
     240      2967276 :   if (ns == NULL)
     241       277516 :     ns = gfc_current_ns;
     242              : 
     243      2967276 :   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          532 : lookup_symbol_fuzzy_find_candidates (gfc_symtree *sym,
     252              :                                      char **&candidates,
     253              :                                      size_t &candidates_len)
     254              : {
     255          919 :   gfc_symtree *p;
     256              : 
     257          919 :   if (sym == NULL)
     258              :     return;
     259              : 
     260          919 :   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          919 :   p = sym->left;
     263          919 :   if (p)
     264          403 :     lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
     265              : 
     266          919 :   p = sym->right;
     267          919 :   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       115605 : gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
     291              : {
     292       115605 :   gfc_typespec *ts;
     293       115605 :   gfc_expr *e;
     294              : 
     295              :   /* Check to see if a function selector of unknown type can be resolved.  */
     296       115605 :   if (sym->assoc
     297           18 :       && (e = sym->assoc->target)
     298       115623 :       && 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       115601 :   if (sym->ts.type != BT_UNKNOWN)
     308            0 :     gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
     309              : 
     310       115601 :   ts = gfc_get_default_type (sym->name, ns);
     311              : 
     312       115601 :   if (ts->type == BT_UNKNOWN)
     313              :     {
     314        60206 :       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        60206 :       return false;
     328              :     }
     329              : 
     330        55395 :   sym->ts = *ts;
     331        55395 :   sym->attr.implicit_type = 1;
     332              : 
     333        55395 :   if (ts->type == BT_CHARACTER && ts->u.cl)
     334          457 :     sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
     335        54938 :   else if (ts->type == BT_CLASS
     336        54938 :            && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
     337              :     return false;
     338              : 
     339        55395 :   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        55395 :   if (sym->attr.dummy != 0)
     349              :     {
     350         4352 :       if (sym->ns->proc_name != NULL
     351         4351 :           && (sym->ns->proc_name->attr.subroutine != 0
     352          401 :               || sym->ns->proc_name->attr.function != 0)
     353         4351 :           && sym->ns->proc_name->attr.is_bind_c != 0
     354           57 :           && 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        12740 : gfc_check_function_type (gfc_namespace *ns)
     378              : {
     379        12740 :   gfc_symbol *proc = ns->proc_name;
     380              : 
     381        12740 :   if (!proc->attr.contained || proc->result->attr.implicit_type)
     382              :     return;
     383              : 
     384         9975 :   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        17236 : conflict_std (int standard, const char *a1, const char *a2, const char *name,
     415              :               locus *where)
     416              : {
     417        17236 :   if (name == NULL)
     418              :     {
     419        10331 :       return gfc_notify_std (standard, "%s attribute conflicts "
     420              :                              "with %s attribute at %L", a1, a2,
     421        10331 :                              where);
     422              :     }
     423              :   else
     424              :     {
     425         6905 :       return gfc_notify_std (standard, "%s attribute conflicts "
     426              :                              "with %s attribute in %qs at %L",
     427         6905 :                              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      6984106 : gfc_check_conflict (symbol_attribute *attr, const char *name, locus *where)
     442              : {
     443      6984106 :   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      6984106 :   static const char *threadprivate = "THREADPRIVATE";
     462      6984106 :   static const char *omp_groupprivate = "OpenMP GROUPPRIVATE";
     463      6984106 :   static const char *omp_declare_target = "OMP DECLARE TARGET";
     464      6984106 :   static const char *omp_declare_target_link = "OMP DECLARE TARGET LINK";
     465      6984106 :   static const char *omp_declare_target_local = "OMP DECLARE TARGET LOCAL";
     466      6984106 :   static const char *oacc_declare_copyin = "OACC DECLARE COPYIN";
     467      6984106 :   static const char *oacc_declare_create = "OACC DECLARE CREATE";
     468      6984106 :   static const char *oacc_declare_deviceptr = "OACC DECLARE DEVICEPTR";
     469      6984106 :   static const char *oacc_declare_device_resident =
     470              :                                                 "OACC DECLARE DEVICE_RESIDENT";
     471              : 
     472      6984106 :   const char *a1, *a2;
     473              : 
     474      6984106 :   if (attr->artificial)
     475              :     return true;
     476              : 
     477      6984080 :   if (where == NULL)
     478      4582211 :     where = &gfc_current_locus;
     479              : 
     480      6984080 :   if (attr->pointer && attr->intent != INTENT_UNKNOWN)
     481         4416 :     conf_std (pointer, intent, GFC_STD_F2003);
     482              : 
     483      6984079 :   conf_std (in_namelist, allocatable, GFC_STD_F2003);
     484      6984079 :   conf_std (in_namelist, pointer, GFC_STD_F2003);
     485              : 
     486              :   /* Check for attributes not allowed in a BLOCK DATA.  */
     487      6984078 :   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      6984077 :   if (attr->save == SAVE_EXPLICIT)
     516              :     {
     517         6691 :       conf (dummy, save);
     518         6689 :       conf (in_common, save);
     519         6675 :       conf (result, save);
     520         6672 :       conf (automatic, save);
     521              : 
     522         6670 :       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      6984052 :   if (name && attr->dummy
     550       258493 :       && (attr->function || attr->subroutine)
     551         1659 :       && 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      6984052 :   conf (dummy, entry);
     557      6984050 :   conf (dummy, intrinsic);
     558      6984049 :   conf (dummy, threadprivate);
     559      6984049 :   conf (dummy, omp_groupprivate);
     560      6984049 :   conf (dummy, omp_declare_target);
     561      6984049 :   conf (dummy, omp_declare_target_link);
     562      6984049 :   conf (dummy, omp_declare_target_local);
     563      6984049 :   conf (pointer, target);
     564      6984049 :   conf (pointer, intrinsic);
     565      6984049 :   conf (pointer, elemental);
     566      6984047 :   conf (pointer, codimension);
     567      6984013 :   conf (allocatable, elemental);
     568      6984012 :   conf (threadprivate, omp_groupprivate);
     569              : 
     570      6984004 :   conf (in_common, automatic);
     571      6983998 :   conf (result, automatic);
     572      6983996 :   conf (use_assoc, automatic);
     573      6983996 :   conf (dummy, automatic);
     574              : 
     575      6983994 :   conf (target, external);
     576      6983994 :   conf (target, intrinsic);
     577              : 
     578      6983994 :   if (!attr->if_source)
     579      6880285 :     conf (external, dimension);   /* See Fortran 95's R504.  */
     580              : 
     581      6983994 :   conf (external, intrinsic);
     582      6983992 :   conf (entry, intrinsic);
     583      6983991 :   conf (abstract, intrinsic);
     584              : 
     585      6983988 :   if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
     586        87369 :     conf (external, subroutine);
     587              : 
     588      6983986 :   if (attr->proc_pointer && !gfc_notify_std (GFC_STD_F2003,
     589              :                                              "Procedure pointer at %C"))
     590              :     return false;
     591              : 
     592      6983980 :   conf (allocatable, pointer);
     593      6983980 :   conf_std (allocatable, dummy, GFC_STD_F2003);
     594      6983980 :   conf_std (allocatable, function, GFC_STD_F2003);
     595      6983980 :   conf_std (allocatable, result, GFC_STD_F2003);
     596      6983980 :   conf_std (elemental, recursive, GFC_STD_F2018);
     597              : 
     598      6983980 :   conf (in_common, dummy);
     599      6983980 :   conf (in_common, allocatable);
     600      6983980 :   conf (in_common, codimension);
     601      6983980 :   conf (in_common, result);
     602              : 
     603      6983980 :   conf (in_equivalence, use_assoc);
     604      6983979 :   conf (in_equivalence, codimension);
     605      6983979 :   conf (in_equivalence, dummy);
     606      6983978 :   conf (in_equivalence, target);
     607      6983977 :   conf (in_equivalence, pointer);
     608      6983976 :   conf (in_equivalence, function);
     609      6983976 :   conf (in_equivalence, result);
     610      6983976 :   conf (in_equivalence, entry);
     611      6983976 :   conf (in_equivalence, allocatable);
     612      6983973 :   conf (in_equivalence, threadprivate);
     613      6983973 :   conf (in_equivalence, omp_groupprivate);
     614      6983973 :   conf (in_equivalence, omp_declare_target);
     615      6983973 :   conf (in_equivalence, omp_declare_target_link);
     616      6983973 :   conf (in_equivalence, omp_declare_target_local);
     617      6983973 :   conf (in_equivalence, oacc_declare_create);
     618      6983973 :   conf (in_equivalence, oacc_declare_copyin);
     619      6983973 :   conf (in_equivalence, oacc_declare_deviceptr);
     620      6983973 :   conf (in_equivalence, oacc_declare_device_resident);
     621      6983973 :   conf (in_equivalence, is_bind_c);
     622              : 
     623      6983972 :   conf (dummy, result);
     624      6983972 :   conf (entry, result);
     625      6983971 :   conf (generic, result);
     626      6983968 :   conf (generic, omp_declare_target);
     627      6983968 :   conf (generic, omp_declare_target_local);
     628      6983968 :   conf (generic, omp_declare_target_link);
     629              : 
     630      6983968 :   conf (function, subroutine);
     631              : 
     632      6983908 :   if (!function && !subroutine)
     633            0 :     conf (is_bind_c, dummy);
     634              : 
     635      6983908 :   conf (is_bind_c, cray_pointer);
     636      6983908 :   conf (is_bind_c, cray_pointee);
     637      6983908 :   conf (is_bind_c, codimension);
     638      6983907 :   conf (is_bind_c, allocatable);
     639      6983906 :   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      6983904 :   conf (cray_pointer, cray_pointee);
     647      6983903 :   conf (cray_pointer, dimension);
     648      6983902 :   conf (cray_pointer, codimension);
     649      6983902 :   conf (cray_pointer, contiguous);
     650      6983902 :   conf (cray_pointer, pointer);
     651      6983901 :   conf (cray_pointer, target);
     652      6983900 :   conf (cray_pointer, allocatable);
     653      6983900 :   conf (cray_pointer, external);
     654      6983900 :   conf (cray_pointer, intrinsic);
     655      6983900 :   conf (cray_pointer, in_namelist);
     656      6983900 :   conf (cray_pointer, function);
     657      6983900 :   conf (cray_pointer, subroutine);
     658      6983900 :   conf (cray_pointer, entry);
     659              : 
     660      6983900 :   conf (cray_pointee, allocatable);
     661      6983900 :   conf (cray_pointee, contiguous);
     662      6983900 :   conf (cray_pointee, codimension);
     663      6983900 :   conf (cray_pointee, intent);
     664      6983900 :   conf (cray_pointee, optional);
     665      6983900 :   conf (cray_pointee, dummy);
     666      6983899 :   conf (cray_pointee, target);
     667      6983898 :   conf (cray_pointee, intrinsic);
     668      6983898 :   conf (cray_pointee, pointer);
     669      6983897 :   conf (cray_pointee, entry);
     670      6983897 :   conf (cray_pointee, in_common);
     671      6983894 :   conf (cray_pointee, in_equivalence);
     672      6983892 :   conf (cray_pointee, threadprivate);
     673      6983891 :   conf (cray_pointee, omp_groupprivate);
     674      6983891 :   conf (cray_pointee, omp_declare_target);
     675      6983891 :   conf (cray_pointee, omp_declare_target_link);
     676      6983891 :   conf (cray_pointee, omp_declare_target_local);
     677      6983891 :   conf (cray_pointee, oacc_declare_create);
     678      6983891 :   conf (cray_pointee, oacc_declare_copyin);
     679      6983891 :   conf (cray_pointee, oacc_declare_deviceptr);
     680      6983891 :   conf (cray_pointee, oacc_declare_device_resident);
     681              : 
     682      6983891 :   conf (data, dummy);
     683      6983888 :   conf (data, function);
     684      6983887 :   conf (data, result);
     685      6983886 :   conf (data, allocatable);
     686              : 
     687      6983885 :   conf (value, pointer)
     688      6983884 :   conf (value, allocatable)
     689      6983884 :   conf (value, subroutine)
     690      6983884 :   conf (value, function)
     691      6983883 :   conf (value, volatile_)
     692      6983883 :   conf (value, dimension)
     693      6983879 :   conf (value, codimension)
     694      6983879 :   conf (value, external)
     695              : 
     696      6983878 :   conf (codimension, result)
     697              : 
     698      6983875 :   if (attr->value
     699        41440 :       && (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      6983871 :   conf (is_protected, intrinsic)
     707      6983871 :   conf (is_protected, in_common)
     708              : 
     709      6983867 :   conf (asynchronous, intrinsic)
     710      6983867 :   conf (asynchronous, external)
     711              : 
     712      6983867 :   conf (volatile_, intrinsic)
     713      6983866 :   conf (volatile_, external)
     714              : 
     715      6983865 :   if (attr->volatile_ && attr->intent == INTENT_IN)
     716              :     {
     717            1 :       a1 = volatile_;
     718            1 :       a2 = intent_in;
     719            1 :       goto conflict;
     720              :     }
     721              : 
     722      6983864 :   conf (procedure, allocatable)
     723      6983863 :   conf (procedure, dimension)
     724      6983863 :   conf (procedure, codimension)
     725      6983863 :   conf (procedure, intrinsic)
     726      6983863 :   conf (procedure, target)
     727      6983863 :   conf (procedure, value)
     728      6983863 :   conf (procedure, volatile_)
     729      6983863 :   conf (procedure, asynchronous)
     730      6983863 :   conf (procedure, entry)
     731              : 
     732      6983862 :   conf (proc_pointer, abstract)
     733      6983860 :   conf (proc_pointer, omp_declare_target)
     734      6983860 :   conf (proc_pointer, omp_declare_target_local)
     735      6983860 :   conf (proc_pointer, omp_declare_target_link)
     736              : 
     737      6983860 :   conf (entry, omp_declare_target)
     738      6983860 :   conf (entry, omp_declare_target_local)
     739      6983860 :   conf (entry, omp_declare_target_link)
     740      6983860 :   conf (entry, oacc_declare_create)
     741      6983860 :   conf (entry, oacc_declare_copyin)
     742      6983860 :   conf (entry, oacc_declare_deviceptr)
     743      6983860 :   conf (entry, oacc_declare_device_resident)
     744              : 
     745      6983860 :   conf (pdt_kind, allocatable)
     746      6983859 :   conf (pdt_kind, pointer)
     747      6983858 :   conf (pdt_kind, dimension)
     748      6983857 :   conf (pdt_kind, codimension)
     749              : 
     750      6983857 :   conf (pdt_len, allocatable)
     751      6983856 :   conf (pdt_len, pointer)
     752      6983855 :   conf (pdt_len, dimension)
     753      6983854 :   conf (pdt_len, codimension)
     754      6983854 :   conf (pdt_len, pdt_kind)
     755              : 
     756      6983852 :   if (attr->access == ACCESS_PRIVATE)
     757              :     {
     758         2134 :       a1 = privat;
     759         2134 :       conf2 (pdt_kind);
     760         2133 :       conf2 (pdt_len);
     761              :     }
     762              : 
     763      6983850 :   a1 = gfc_code2string (flavors, attr->flavor);
     764              : 
     765      6983850 :   if (attr->in_namelist
     766         4553 :       && attr->flavor != FL_VARIABLE
     767         1989 :       && attr->flavor != FL_PROCEDURE
     768         1980 :       && attr->flavor != FL_UNKNOWN)
     769              :     {
     770            0 :       a2 = in_namelist;
     771            0 :       goto conflict;
     772              :     }
     773              : 
     774      6983850 :   switch (attr->flavor)
     775              :     {
     776       167115 :     case FL_PROGRAM:
     777       167115 :     case FL_BLOCK_DATA:
     778       167115 :     case FL_MODULE:
     779       167115 :     case FL_LABEL:
     780       167115 :       conf2 (codimension);
     781       167115 :       conf2 (dimension);
     782       167114 :       conf2 (dummy);
     783       167114 :       conf2 (volatile_);
     784       167112 :       conf2 (asynchronous);
     785       167111 :       conf2 (contiguous);
     786       167111 :       conf2 (pointer);
     787       167111 :       conf2 (is_protected);
     788       167110 :       conf2 (target);
     789       167110 :       conf2 (external);
     790       167109 :       conf2 (intrinsic);
     791       167109 :       conf2 (allocatable);
     792       167109 :       conf2 (result);
     793       167109 :       conf2 (in_namelist);
     794       167109 :       conf2 (optional);
     795       167109 :       conf2 (function);
     796       167109 :       conf2 (subroutine);
     797       167108 :       conf2 (threadprivate);
     798       167108 :       conf2 (omp_groupprivate);
     799       167108 :       conf2 (omp_declare_target);
     800       167108 :       conf2 (omp_declare_target_link);
     801       167108 :       conf2 (omp_declare_target_local);
     802       167108 :       conf2 (oacc_declare_create);
     803       167108 :       conf2 (oacc_declare_copyin);
     804       167108 :       conf2 (oacc_declare_deviceptr);
     805       167108 :       conf2 (oacc_declare_device_resident);
     806              : 
     807       167108 :       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       167106 :       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          809 :     case FL_NAMELIST:
     827          809 :       conf2 (result);
     828              :       break;
     829              : 
     830      4349811 :     case FL_PROCEDURE:
     831              :       /* Conflicts with INTENT, SAVE and RESULT will be checked
     832              :          at resolution stage, see "resolve_fl_procedure".  */
     833              : 
     834      4349811 :       if (attr->subroutine)
     835              :         {
     836       112626 :           a1 = subroutine;
     837       112626 :           conf2 (target);
     838       112626 :           conf2 (allocatable);
     839       112626 :           conf2 (volatile_);
     840       112625 :           conf2 (asynchronous);
     841       112624 :           conf2 (in_namelist);
     842       112624 :           conf2 (codimension);
     843       112624 :           conf2 (dimension);
     844       112623 :           conf2 (function);
     845       112623 :           if (!attr->proc_pointer)
     846              :             {
     847       112436 :               conf2 (threadprivate);
     848       112436 :               conf2 (omp_groupprivate);
     849              :             }
     850              :         }
     851              : 
     852              :       /* Procedure pointers in COMMON blocks are allowed in F03,
     853              :        * but forbidden per F08:C5100.  */
     854      4349808 :       if (!attr->proc_pointer || (gfc_option.allow_std & GFC_STD_F2008))
     855      4349638 :         conf2 (in_common);
     856              : 
     857      4349804 :       conf2 (omp_declare_target_local);
     858      4349802 :       conf2 (omp_declare_target_link);
     859              : 
     860      4349798 :       switch (attr->proc)
     861              :         {
     862       832846 :         case PROC_ST_FUNCTION:
     863       832846 :           conf2 (dummy);
     864       832845 :           conf2 (target);
     865              :           break;
     866              : 
     867        52465 :         case PROC_MODULE:
     868        52465 :           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        36627 :     case_fl_struct:
     884        36627 :       conf2 (dummy);
     885        36627 :       conf2 (pointer);
     886        36627 :       conf2 (target);
     887        36627 :       conf2 (external);
     888        36627 :       conf2 (intrinsic);
     889        36627 :       conf2 (allocatable);
     890        36627 :       conf2 (optional);
     891        36627 :       conf2 (entry);
     892        36627 :       conf2 (function);
     893        36627 :       conf2 (subroutine);
     894        36627 :       conf2 (threadprivate);
     895        36627 :       conf2 (omp_groupprivate);
     896        36627 :       conf2 (result);
     897        36627 :       conf2 (omp_declare_target);
     898        36627 :       conf2 (omp_declare_target_local);
     899        36627 :       conf2 (omp_declare_target_link);
     900        36627 :       conf2 (oacc_declare_create);
     901        36627 :       conf2 (oacc_declare_copyin);
     902        36627 :       conf2 (oacc_declare_deviceptr);
     903        36627 :       conf2 (oacc_declare_device_resident);
     904              : 
     905        36627 :       if (attr->intent != INTENT_UNKNOWN)
     906              :         {
     907            0 :           a2 = intent;
     908            0 :           goto conflict;
     909              :         }
     910              :       break;
     911              : 
     912        39787 :     case FL_PARAMETER:
     913        39787 :       conf2 (external);
     914        39787 :       conf2 (intrinsic);
     915        39787 :       conf2 (optional);
     916        39787 :       conf2 (allocatable);
     917        39787 :       conf2 (function);
     918        39787 :       conf2 (subroutine);
     919        39787 :       conf2 (entry);
     920        39787 :       conf2 (contiguous);
     921        39787 :       conf2 (pointer);
     922        39787 :       conf2 (is_protected);
     923        39787 :       conf2 (target);
     924        39787 :       conf2 (dummy);
     925        39787 :       conf2 (in_common);
     926        39787 :       conf2 (value);
     927        39786 :       conf2 (volatile_);
     928        39785 :       conf2 (asynchronous);
     929        39785 :       conf2 (threadprivate);
     930        39785 :       conf2 (omp_groupprivate);
     931        39785 :       conf2 (value);
     932        39785 :       conf2 (codimension);
     933        39784 :       conf2 (result);
     934        39783 :       if (!attr->is_iso_c)
     935        39753 :         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      8327044 : gfc_set_sym_referenced (gfc_symbol *sym)
     964              : {
     965      8327044 :   if (sym->attr.referenced)
     966              :     return;
     967              : 
     968      4174795 :   sym->attr.referenced = 1;
     969              : 
     970              :   /* Remember the declaration order.  */
     971      4174795 :   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      2333395 : check_used (symbol_attribute *attr, const char *name, locus *where)
     982              : {
     983              : 
     984      2333395 :   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        10287 : gfc_add_attribute (symbol_attribute *attr, locus *where)
    1028              : {
    1029        10287 :   if (check_used (attr, NULL, where))
    1030              :     return false;
    1031              : 
    1032        10287 :   return gfc_check_conflict (attr, NULL, where);
    1033              : }
    1034              : 
    1035              : 
    1036              : bool
    1037        36634 : gfc_add_allocatable (symbol_attribute *attr, locus *where)
    1038              : {
    1039              : 
    1040        36634 :   if (check_used (attr, NULL, where))
    1041              :     return false;
    1042              : 
    1043        36634 :   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        36722 :       && !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        36632 :   attr->allocatable = 1;
    1058        36632 :   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         1617 : gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
    1079              : {
    1080              : 
    1081         1617 :   if (check_used (attr, name, where))
    1082              :     return false;
    1083              : 
    1084         1617 :   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         1616 :       && !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         1615 :   attr->codimension = 1;
    1099         1615 :   return gfc_check_conflict (attr, name, where);
    1100              : }
    1101              : 
    1102              : 
    1103              : bool
    1104       101252 : gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
    1105              : {
    1106              : 
    1107       101252 :   if (check_used (attr, name, where))
    1108              :     return false;
    1109              : 
    1110       101252 :   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       101489 :       && !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       101249 :   attr->dimension = 1;
    1125       101249 :   return gfc_check_conflict (attr, name, where);
    1126              : }
    1127              : 
    1128              : 
    1129              : bool
    1130         4402 : gfc_add_contiguous (symbol_attribute *attr, const char *name, locus *where)
    1131              : {
    1132              : 
    1133         4402 :   if (check_used (attr, name, where))
    1134              :     return false;
    1135              : 
    1136         4402 :   if (attr->contiguous)
    1137              :     {
    1138            2 :       duplicate_attr ("CONTIGUOUS", where);
    1139            2 :       return false;
    1140              :     }
    1141              : 
    1142         4400 :   attr->contiguous = 1;
    1143         4400 :   return gfc_check_conflict (attr, name, where);
    1144              : }
    1145              : 
    1146              : 
    1147              : bool
    1148        20118 : gfc_add_external (symbol_attribute *attr, locus *where)
    1149              : {
    1150              : 
    1151        20118 :   if (check_used (attr, NULL, where))
    1152              :     return false;
    1153              : 
    1154        20115 :   if (attr->external)
    1155              :     {
    1156            4 :       duplicate_attr ("EXTERNAL", where);
    1157            4 :       return false;
    1158              :     }
    1159              : 
    1160        20111 :   if (attr->pointer && attr->if_source != IFSRC_IFBODY)
    1161              :     {
    1162          835 :       attr->pointer = 0;
    1163          835 :       attr->proc_pointer = 1;
    1164              :     }
    1165              : 
    1166        20111 :   attr->external = 1;
    1167              : 
    1168        20111 :   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        11779 : gfc_add_optional (symbol_attribute *attr, locus *where)
    1193              : {
    1194              : 
    1195        11779 :   if (check_used (attr, NULL, where))
    1196              :     return false;
    1197              : 
    1198        11779 :   if (attr->optional)
    1199              :     {
    1200            1 :       duplicate_attr ("OPTIONAL", where);
    1201            1 :       return false;
    1202              :     }
    1203              : 
    1204        11778 :   attr->optional = 1;
    1205        11778 :   return gfc_check_conflict (attr, NULL, where);
    1206              : }
    1207              : 
    1208              : bool
    1209          281 : gfc_add_kind (symbol_attribute *attr, locus *where)
    1210              : {
    1211          281 :   if (attr->pdt_kind)
    1212              :     {
    1213            0 :       duplicate_attr ("KIND", where);
    1214            0 :       return false;
    1215              :     }
    1216              : 
    1217          281 :   attr->pdt_kind = 1;
    1218          281 :   return gfc_check_conflict (attr, NULL, where);
    1219              : }
    1220              : 
    1221              : bool
    1222          299 : gfc_add_len (symbol_attribute *attr, locus *where)
    1223              : {
    1224          299 :   if (attr->pdt_len)
    1225              :     {
    1226            0 :       duplicate_attr ("LEN", where);
    1227            0 :       return false;
    1228              :     }
    1229              : 
    1230          299 :   attr->pdt_len = 1;
    1231          299 :   return gfc_check_conflict (attr, NULL, where);
    1232              : }
    1233              : 
    1234              : 
    1235              : bool
    1236        26608 : gfc_add_pointer (symbol_attribute *attr, locus *where)
    1237              : {
    1238              : 
    1239        26608 :   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        26609 :       && ! gfc_submodule_procedure(attr))
    1245              :     {
    1246            1 :       duplicate_attr ("POINTER", where);
    1247            1 :       return false;
    1248              :     }
    1249              : 
    1250        26599 :   if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
    1251        53185 :       || (attr->if_source == IFSRC_IFBODY
    1252          495 :       && !gfc_find_state (COMP_INTERFACE)))
    1253           36 :     attr->proc_pointer = 1;
    1254              :   else
    1255        26571 :     attr->pointer = 1;
    1256              : 
    1257        26607 :   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         8686 : gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
    1313              : {
    1314              : 
    1315         8686 :   if (check_used (attr, name, where))
    1316              :     return false;
    1317              : 
    1318         8686 :   attr->result = 1;
    1319         8686 :   return gfc_check_conflict (attr, name, where);
    1320              : }
    1321              : 
    1322              : 
    1323              : bool
    1324        10476 : gfc_add_save (symbol_attribute *attr, save_state s, const char *name,
    1325              :               locus *where)
    1326              : {
    1327              : 
    1328        10476 :   if (check_used (attr, name, where))
    1329              :     return false;
    1330              : 
    1331        10476 :   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        10474 :   if (s == SAVE_EXPLICIT)
    1339         3805 :     gfc_unset_implicit_pure (NULL);
    1340              : 
    1341         3805 :   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        10471 :   attr->save = s;
    1356        10471 :   return gfc_check_conflict (attr, name, where);
    1357              : }
    1358              : 
    1359              : 
    1360              : bool
    1361        23404 : gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
    1362              : {
    1363              : 
    1364        23404 :   if (check_used (attr, name, where))
    1365              :     return false;
    1366              : 
    1367        23404 :   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        23404 :   attr->value = 1;
    1376        23404 :   return gfc_check_conflict (attr, name, where);
    1377              : }
    1378              : 
    1379              : 
    1380              : bool
    1381         1235 : 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         1235 :   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         1235 :   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         1233 :   attr->volatile_ = 1;
    1408         1233 :   attr->volatile_ns = gfc_current_ns;
    1409         1233 :   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         1121 : gfc_add_omp_declare_target (symbol_attribute *attr, const char *name,
    1471              :                             locus *where)
    1472              : {
    1473              : 
    1474         1121 :   if (check_used (attr, name, where))
    1475              :     return false;
    1476              : 
    1477         1098 :   if (attr->omp_declare_target)
    1478              :     return true;
    1479              : 
    1480         1047 :   attr->omp_declare_target = 1;
    1481         1047 :   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        12191 : gfc_add_target (symbol_attribute *attr, locus *where)
    1579              : {
    1580              : 
    1581        12191 :   if (check_used (attr, NULL, where))
    1582              :     return false;
    1583              : 
    1584        12191 :   if (attr->target)
    1585              :     {
    1586            1 :       duplicate_attr ("TARGET", where);
    1587            1 :       return false;
    1588              :     }
    1589              : 
    1590        12190 :   attr->target = 1;
    1591        12190 :   return gfc_check_conflict (attr, NULL, where);
    1592              : }
    1593              : 
    1594              : 
    1595              : bool
    1596       100104 : gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
    1597              : {
    1598              : 
    1599       100104 :   if (check_used (attr, name, where))
    1600              :     return false;
    1601              : 
    1602              :   /* Duplicate dummy arguments are allowed due to ENTRY statements.  */
    1603       100104 :   attr->dummy = 1;
    1604       100104 :   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         2068 : gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
    1651              : {
    1652              : 
    1653         2068 :   attr->in_namelist = 1;
    1654         2068 :   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         8558 : gfc_add_elemental (symbol_attribute *attr, locus *where)
    1672              : {
    1673              : 
    1674         8558 :   if (check_used (attr, NULL, where))
    1675              :     return false;
    1676              : 
    1677         8558 :   if (attr->elemental)
    1678              :     {
    1679            2 :       duplicate_attr ("ELEMENTAL", where);
    1680            2 :       return false;
    1681              :     }
    1682              : 
    1683         8556 :   attr->elemental = 1;
    1684         8556 :   return gfc_check_conflict (attr, NULL, where);
    1685              : }
    1686              : 
    1687              : 
    1688              : bool
    1689        11376 : gfc_add_pure (symbol_attribute *attr, locus *where)
    1690              : {
    1691              : 
    1692        11376 :   if (check_used (attr, NULL, where))
    1693              :     return false;
    1694              : 
    1695        11376 :   if (attr->pure)
    1696              :     {
    1697            2 :       duplicate_attr ("PURE", where);
    1698            2 :       return false;
    1699              :     }
    1700              : 
    1701        11374 :   attr->pure = 1;
    1702        11374 :   return gfc_check_conflict (attr, NULL, where);
    1703              : }
    1704              : 
    1705              : 
    1706              : bool
    1707          769 : gfc_add_recursive (symbol_attribute *attr, locus *where)
    1708              : {
    1709              : 
    1710          769 :   if (check_used (attr, NULL, where))
    1711              :     return false;
    1712              : 
    1713          769 :   if (attr->recursive)
    1714              :     {
    1715            2 :       duplicate_attr ("RECURSIVE", where);
    1716            2 :       return false;
    1717              :     }
    1718              : 
    1719          767 :   attr->recursive = 1;
    1720          767 :   return gfc_check_conflict (attr, NULL, where);
    1721              : }
    1722              : 
    1723              : 
    1724              : bool
    1725          795 : gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
    1726              : {
    1727              : 
    1728          795 :   if (check_used (attr, name, where))
    1729              :     return false;
    1730              : 
    1731          795 :   if (attr->entry)
    1732              :     {
    1733            0 :       duplicate_attr ("ENTRY", where);
    1734            0 :       return false;
    1735              :     }
    1736              : 
    1737          795 :   attr->entry = 1;
    1738          795 :   return gfc_check_conflict (attr, name, where);
    1739              : }
    1740              : 
    1741              : 
    1742              : bool
    1743      1031357 : gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
    1744              : {
    1745              : 
    1746      1031357 :   if (attr->flavor != FL_PROCEDURE
    1747      1031357 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1748              :     return false;
    1749              : 
    1750      1031357 :   attr->function = 1;
    1751      1031357 :   return gfc_check_conflict (attr, name, where);
    1752              : }
    1753              : 
    1754              : 
    1755              : bool
    1756        84966 : gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
    1757              : {
    1758              : 
    1759        84966 :   if (attr->flavor != FL_PROCEDURE
    1760        84966 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1761              :     return false;
    1762              : 
    1763        84963 :   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        84963 :   if (name && *name != '_' && gfc_current_state () != COMP_BLOCK_DATA)
    1770        83141 :     return gfc_check_conflict (attr, name, where);
    1771              :   else
    1772              :     return true;
    1773              : }
    1774              : 
    1775              : 
    1776              : bool
    1777        26145 : gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
    1778              : {
    1779              : 
    1780        26145 :   if (attr->flavor != FL_PROCEDURE
    1781        26145 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1782              :     return false;
    1783              : 
    1784        26143 :   attr->generic = 1;
    1785        26143 :   return gfc_check_conflict (attr, name, where);
    1786              : }
    1787              : 
    1788              : 
    1789              : bool
    1790         1658 : gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
    1791              : {
    1792              : 
    1793         1658 :   if (check_used (attr, NULL, where))
    1794              :     return false;
    1795              : 
    1796         1658 :   if (attr->flavor != FL_PROCEDURE
    1797         1658 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1798              :     return false;
    1799              : 
    1800         1658 :   if (attr->procedure)
    1801              :     {
    1802            0 :       duplicate_attr ("PROCEDURE", where);
    1803            0 :       return false;
    1804              :     }
    1805              : 
    1806         1658 :   attr->procedure = 1;
    1807              : 
    1808         1658 :   return gfc_check_conflict (attr, NULL, where);
    1809              : }
    1810              : 
    1811              : 
    1812              : bool
    1813          809 : gfc_add_abstract (symbol_attribute* attr, locus* where)
    1814              : {
    1815          809 :   if (attr->abstract)
    1816              :     {
    1817            1 :       duplicate_attr ("ABSTRACT", where);
    1818            1 :       return false;
    1819              :     }
    1820              : 
    1821          808 :   attr->abstract = 1;
    1822              : 
    1823          808 :   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      3862415 : gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
    1832              :                 locus *where)
    1833              : {
    1834              : 
    1835      3862415 :   if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
    1836      3862415 :        || f == FL_PARAMETER || f == FL_LABEL || gfc_fl_struct(f)
    1837       241887 :        || f == FL_NAMELIST) && check_used (attr, name, where))
    1838              :     return false;
    1839              : 
    1840      3862415 :   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      3862413 :   if (attr->flavor == f && f == FL_PROCEDURE
    1847          573 :       && gfc_new_block && gfc_new_block->abr_modproc_decl)
    1848              :     return true;
    1849              : 
    1850      3862401 :   if (attr->flavor != FL_UNKNOWN)
    1851              :     {
    1852          625 :       if (where == NULL)
    1853          513 :         where = &gfc_current_locus;
    1854              : 
    1855          625 :       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          898 :         gfc_error ("%s attribute conflicts with %s attribute at %L",
    1861          449 :                    gfc_code2string (flavors, attr->flavor),
    1862              :                    gfc_code2string (flavors, f), where);
    1863              : 
    1864          625 :       return false;
    1865              :     }
    1866              : 
    1867      3861776 :   attr->flavor = f;
    1868              : 
    1869      3861776 :   return gfc_check_conflict (attr, name, where);
    1870              : }
    1871              : 
    1872              : 
    1873              : bool
    1874      1468623 : gfc_add_procedure (symbol_attribute *attr, procedure_type t,
    1875              :                    const char *name, locus *where)
    1876              : {
    1877              : 
    1878      1468623 :   if (check_used (attr, name, where))
    1879              :     return false;
    1880              : 
    1881      1468594 :   if (attr->flavor != FL_PROCEDURE
    1882      1468594 :       && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
    1883              :     return false;
    1884              : 
    1885      1468544 :   if (where == NULL)
    1886      1449077 :     where = &gfc_current_locus;
    1887              : 
    1888      1468544 :   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      1468263 :   attr->proc = t;
    1908              : 
    1909              :   /* Statement functions are always scalar and functions.  */
    1910      1468263 :   if (t == PROC_ST_FUNCTION
    1911      1468263 :       && ((!attr->function && !gfc_add_function (attr, name, where))
    1912       416440 :           || attr->dimension))
    1913           68 :     return false;
    1914              : 
    1915      1468195 :   return gfc_check_conflict (attr, name, where);
    1916              : }
    1917              : 
    1918              : 
    1919              : bool
    1920        58615 : gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
    1921              : {
    1922              : 
    1923        58615 :   if (check_used (attr, NULL, where))
    1924              :     return false;
    1925              : 
    1926        58615 :   if (attr->intent == INTENT_UNKNOWN)
    1927              :     {
    1928        58615 :       attr->intent = intent;
    1929        58615 :       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         5784 : gfc_add_access (symbol_attribute *attr, gfc_access access,
    1947              :                 const char *name, locus *where)
    1948              : {
    1949              : 
    1950         5784 :   if (attr->access == ACCESS_UNKNOWN
    1951            5 :         || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
    1952              :     {
    1953         5780 :       attr->access = access;
    1954         5780 :       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         7521 : gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
    1969              :                    int is_proc_lang_bind_spec)
    1970              : {
    1971              : 
    1972         7521 :   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         7516 :   else if (attr->is_bind_c)
    1976            1 :     gfc_error_now ("Duplicate BIND attribute specified at %L", where);
    1977              :   else
    1978         7515 :     attr->is_bind_c = 1;
    1979              : 
    1980         7521 :   if (where == NULL)
    1981           90 :     where = &gfc_current_locus;
    1982              : 
    1983         7521 :   if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) at %L", where))
    1984              :     return false;
    1985              : 
    1986         7521 :   return gfc_check_conflict (attr, name, where);
    1987              : }
    1988              : 
    1989              : 
    1990              : /* Set the extension field for the given symbol_attribute.  */
    1991              : 
    1992              : bool
    1993         1478 : gfc_add_extension (symbol_attribute *attr, locus *where)
    1994              : {
    1995         1478 :   if (where == NULL)
    1996            0 :     where = &gfc_current_locus;
    1997              : 
    1998         1478 :   if (attr->extension)
    1999            0 :     gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
    2000              :   else
    2001         1478 :     attr->extension = 1;
    2002              : 
    2003         1478 :   if (!gfc_notify_std (GFC_STD_F2003, "EXTENDS at %L", where))
    2004              :     return false;
    2005              : 
    2006              :   return true;
    2007              : }
    2008              : 
    2009              : 
    2010              : bool
    2011       153155 : gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
    2012              :                             gfc_formal_arglist * formal, locus *where)
    2013              : {
    2014       153155 :   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       153155 :   if (sym->attr.module_procedure == 1
    2020         1424 :       && source == IFSRC_DECL)
    2021          941 :     goto finish;
    2022              : 
    2023       152214 :   if (where == NULL)
    2024       152214 :     where = &gfc_current_locus;
    2025              : 
    2026       152214 :   if (sym->attr.if_source != IFSRC_UNKNOWN
    2027       152214 :       && 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       152214 :   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       152212 : finish:
    2042       153153 :   sym->formal = formal;
    2043       153153 :   sym->attr.if_source = source;
    2044              : 
    2045       153153 :   return true;
    2046              : }
    2047              : 
    2048              : 
    2049              : /* Add a type to a symbol.  */
    2050              : 
    2051              : bool
    2052       273544 : gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
    2053              : {
    2054       273544 :   sym_flavor flavor;
    2055       273544 :   bt type;
    2056              : 
    2057       273544 :   if (where == NULL)
    2058         5662 :     where = &gfc_current_locus;
    2059              : 
    2060       273544 :   if (sym->result)
    2061         8290 :     type = sym->result->ts.type;
    2062              :   else
    2063       265254 :     type = sym->ts.type;
    2064              : 
    2065       273544 :   if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
    2066         4376 :     type = sym->ns->proc_name->ts.type;
    2067              : 
    2068       273544 :   if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
    2069           94 :       && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
    2070           75 :            && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
    2071           53 :       && !sym->attr.module_procedure)
    2072              :     {
    2073           27 :       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           25 :       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           24 :         gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
    2082              :                    where, gfc_basic_typename (type));
    2083           27 :       return false;
    2084              :     }
    2085              : 
    2086       273517 :   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       273516 :   flavor = sym->attr.flavor;
    2094              : 
    2095       273516 :   if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
    2096       273516 :       || flavor == FL_LABEL
    2097       273514 :       || (flavor == FL_PROCEDURE && sym->attr.subroutine)
    2098       273512 :       || 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       273512 :   sym->ts = *ts;
    2107       273512 :   return true;
    2108              : }
    2109              : 
    2110              : 
    2111              : /* Clears all attributes.  */
    2112              : 
    2113              : void
    2114      7827164 : gfc_clear_attr (symbol_attribute *attr)
    2115              : {
    2116      7827164 :   memset (attr, 0, sizeof (symbol_attribute));
    2117      7827164 : }
    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       390360 : gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
    2125              :                   locus *where ATTRIBUTE_UNUSED)
    2126              : {
    2127              : 
    2128       390360 :   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       271760 : gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
    2138              : {
    2139       271760 :   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       271760 :   dest->ext_attr |= src->ext_attr;
    2144              : 
    2145       271760 :   if (src->allocatable && !gfc_add_allocatable (dest, where))
    2146            4 :     goto fail;
    2147              : 
    2148       271756 :   if (src->automatic && !gfc_add_automatic (dest, NULL, where))
    2149            2 :     goto fail;
    2150       271754 :   if (src->dimension && !gfc_add_dimension (dest, NULL, where))
    2151            0 :     goto fail;
    2152       271754 :   if (src->codimension && !gfc_add_codimension (dest, NULL, where))
    2153            0 :     goto fail;
    2154       271754 :   if (src->contiguous && !gfc_add_contiguous (dest, NULL, where))
    2155            2 :     goto fail;
    2156       271752 :   if (src->optional && !gfc_add_optional (dest, where))
    2157            1 :     goto fail;
    2158       271751 :   if (src->pointer && !gfc_add_pointer (dest, where))
    2159            8 :     goto fail;
    2160       271743 :   if (src->is_protected && !gfc_add_protected (dest, NULL, where))
    2161            0 :     goto fail;
    2162       271743 :   if (src->save && !gfc_add_save (dest, src->save, NULL, where))
    2163            4 :     goto fail;
    2164       271739 :   if (src->value && !gfc_add_value (dest, NULL, where))
    2165            2 :     goto fail;
    2166       271737 :   if (src->volatile_ && !gfc_add_volatile (dest, NULL, where))
    2167            0 :     goto fail;
    2168       271737 :   if (src->asynchronous && !gfc_add_asynchronous (dest, NULL, where))
    2169            0 :     goto fail;
    2170       271737 :   if (src->omp_groupprivate
    2171       271737 :       && !gfc_add_omp_groupprivate (dest, NULL, where))
    2172            0 :     goto fail;
    2173       271737 :   if (src->threadprivate
    2174       271737 :       && !gfc_add_threadprivate (dest, NULL, where))
    2175            0 :     goto fail;
    2176       271737 :   if (src->omp_declare_target
    2177       271737 :       && !gfc_add_omp_declare_target (dest, NULL, where))
    2178            0 :     goto fail;
    2179       271737 :   if (src->omp_declare_target_link
    2180       271737 :       && !gfc_add_omp_declare_target_link (dest, NULL, where))
    2181            0 :     goto fail;
    2182       271737 :   if (src->omp_declare_target_local
    2183       271737 :       && !gfc_add_omp_declare_target_local (dest, NULL, where))
    2184            0 :     goto fail;
    2185       271737 :   if (src->oacc_declare_create
    2186       271737 :       && !gfc_add_oacc_declare_create (dest, NULL, where))
    2187            0 :     goto fail;
    2188       271737 :   if (src->oacc_declare_copyin
    2189       271737 :       && !gfc_add_oacc_declare_copyin (dest, NULL, where))
    2190            0 :     goto fail;
    2191       271737 :   if (src->oacc_declare_deviceptr
    2192       271737 :       && !gfc_add_oacc_declare_deviceptr (dest, NULL, where))
    2193            0 :     goto fail;
    2194       271737 :   if (src->oacc_declare_device_resident
    2195       271737 :       && !gfc_add_oacc_declare_device_resident (dest, NULL, where))
    2196            0 :     goto fail;
    2197       271737 :   if (src->target && !gfc_add_target (dest, where))
    2198            2 :     goto fail;
    2199       271735 :   if (src->dummy && !gfc_add_dummy (dest, NULL, where))
    2200            0 :     goto fail;
    2201       271735 :   if (src->result && !gfc_add_result (dest, NULL, where))
    2202            0 :     goto fail;
    2203       271735 :   if (src->entry)
    2204            0 :     dest->entry = 1;
    2205              : 
    2206       271735 :   if (src->in_namelist && !gfc_add_in_namelist (dest, NULL, where))
    2207            0 :     goto fail;
    2208              : 
    2209       271735 :   if (src->in_common && !gfc_add_in_common (dest, NULL, where))
    2210            0 :     goto fail;
    2211              : 
    2212       271735 :   if (src->generic && !gfc_add_generic (dest, NULL, where))
    2213            0 :     goto fail;
    2214       271735 :   if (src->function && !gfc_add_function (dest, NULL, where))
    2215            0 :     goto fail;
    2216       271735 :   if (src->subroutine && !gfc_add_subroutine (dest, NULL, where))
    2217            0 :     goto fail;
    2218              : 
    2219       271735 :   if (src->sequence && !gfc_add_sequence (dest, NULL, where))
    2220            0 :     goto fail;
    2221       271735 :   if (src->elemental && !gfc_add_elemental (dest, where))
    2222            0 :     goto fail;
    2223       271735 :   if (src->pure && !gfc_add_pure (dest, where))
    2224            0 :     goto fail;
    2225       271735 :   if (src->recursive && !gfc_add_recursive (dest, where))
    2226            0 :     goto fail;
    2227              : 
    2228       271735 :   if (src->flavor != FL_UNKNOWN
    2229       271735 :       && !gfc_add_flavor (dest, src->flavor, NULL, where))
    2230          451 :     goto fail;
    2231              : 
    2232       271284 :   if (src->intent != INTENT_UNKNOWN
    2233       271284 :       && !gfc_add_intent (dest, src->intent, where))
    2234            0 :     goto fail;
    2235              : 
    2236       271284 :   if (src->access != ACCESS_UNKNOWN
    2237       271284 :       && !gfc_add_access (dest, src->access, NULL, where))
    2238            1 :     goto fail;
    2239              : 
    2240       271283 :   if (!gfc_missing_attr (dest, where))
    2241            0 :     goto fail;
    2242              : 
    2243       271283 :   if (src->cray_pointer && !gfc_add_cray_pointer (dest, where))
    2244            0 :     goto fail;
    2245       271283 :   if (src->cray_pointee && !gfc_add_cray_pointee (dest, where))
    2246            0 :     goto fail;
    2247              : 
    2248       271283 :   is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
    2249       271283 :   if (src->is_bind_c
    2250       271283 :       && !gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec))
    2251              :     return false;
    2252              : 
    2253       271282 :   if (src->is_c_interop)
    2254            0 :     dest->is_c_interop = 1;
    2255       271282 :   if (src->is_iso_c)
    2256            0 :     dest->is_iso_c = 1;
    2257              : 
    2258       271282 :   if (src->external && !gfc_add_external (dest, where))
    2259            5 :     goto fail;
    2260       271277 :   if (src->intrinsic && !gfc_add_intrinsic (dest, where))
    2261            4 :     goto fail;
    2262       271273 :   if (src->proc_pointer)
    2263          433 :     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          376 : gfc_copy_dummy_sym (gfc_symbol **dsym, gfc_symbol *sym, int result)
    2278              : {
    2279          376 :   int rc;
    2280              : 
    2281          376 :   rc = gfc_get_symbol (sym->name, NULL, dsym);
    2282          376 :   if (rc)
    2283              :     return rc;
    2284              : 
    2285          376 :   if (!gfc_add_type (*dsym, &(sym->ts), &gfc_current_locus))
    2286              :     return 1;
    2287              : 
    2288          376 :   if (sym->attr.external
    2289           11 :       && (sym->attr.codimension || sym->attr.dimension))
    2290            1 :     (*dsym)->attr.if_source = IFSRC_DECL;
    2291              : 
    2292          376 :   if (!gfc_copy_attr (&(*dsym)->attr, &(sym->attr),
    2293              :       &gfc_current_locus))
    2294              :     return 1;
    2295              : 
    2296          376 :   if ((*dsym)->attr.dimension)
    2297           64 :     (*dsym)->as = gfc_copy_array_spec (sym->as);
    2298              : 
    2299          376 :   (*dsym)->attr.class_ok = sym->attr.class_ok;
    2300              : 
    2301          376 :   if ((*dsym) != NULL && !result
    2302          329 :       && (!gfc_add_dummy(&(*dsym)->attr, (*dsym)->name, NULL)
    2303          329 :           || !gfc_missing_attr (&(*dsym)->attr, NULL)))
    2304            0 :     return 1;
    2305          376 :   else if ((*dsym) != NULL && result
    2306          423 :       && (!gfc_add_result(&(*dsym)->attr, (*dsym)->name, NULL)
    2307           47 :           || !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       131434 : gfc_add_component (gfc_symbol *sym, const char *name,
    2328              :                    gfc_component **component)
    2329              : {
    2330       131434 :   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       131434 :   tail = NULL;
    2338              : 
    2339       425399 :   for (p = sym->components; p; p = p->next)
    2340              :     {
    2341       293969 :       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       293965 :       tail = p;
    2349              :     }
    2350              : 
    2351       131430 :   if (sym->attr.extension
    2352       131430 :         && 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       131428 :   p = gfc_get_component ();
    2362              : 
    2363       131428 :   if (tail == NULL)
    2364        41114 :     sym->components = p;
    2365              :   else
    2366        90314 :     tail->next = p;
    2367              : 
    2368       131428 :   p->name = gfc_get_string ("%s", name);
    2369       131428 :   p->loc = gfc_current_locus;
    2370       131428 :   p->ts.type = BT_UNKNOWN;
    2371              : 
    2372       131428 :   *component = p;
    2373       131428 :   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       374899 : gfc_use_derived (gfc_symbol *sym)
    2417              : {
    2418       374899 :   gfc_symbol *s;
    2419       374899 :   gfc_typespec *t;
    2420       374899 :   gfc_symtree *st;
    2421       374899 :   int i;
    2422              : 
    2423       374899 :   if (!sym)
    2424              :     return NULL;
    2425              : 
    2426       374895 :   if (sym->attr.unlimited_polymorphic)
    2427              :     return sym;
    2428              : 
    2429       373203 :   if (sym->attr.generic)
    2430            0 :     sym = gfc_find_dt_in_generic (sym);
    2431              : 
    2432       373203 :   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        14596 : find_derived_types (gfc_symbol *sym, gfc_symtree *st, const char *name,
    2489              :                     bool contained, bool stash)
    2490              : {
    2491        14596 :   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        16664 :       && 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        14596 :   if (st->left)
    2507         5790 :     find_derived_types (sym, st->left, name, contained, stash);
    2508              : 
    2509        14596 :   if (st->right)
    2510         6768 :     find_derived_types (sym, st->right, name, contained, stash);
    2511        14596 : }
    2512              : 
    2513              : int
    2514         1092 : gfc_find_derived_types (gfc_symbol *sym, gfc_namespace *ns,
    2515              :                         const char *name, bool stash)
    2516              : {
    2517         1092 :   gfc_namespace *encompassing = NULL;
    2518         1092 :   gcc_assert (sym->assoc);
    2519              : 
    2520         1092 :   cts = 0;
    2521         3240 :   while (ns->parent)
    2522              :     {
    2523         2148 :       if (!ns->parent->parent && ns->proc_name
    2524         1092 :           && (ns->proc_name->attr.function || ns->proc_name->attr.subroutine))
    2525         2148 :         encompassing = ns;
    2526              :       ns = ns->parent;
    2527              :     }
    2528              : 
    2529              :   /* Search the top level namespace first.  */
    2530         1092 :   find_derived_types (sym, ns->sym_root, name, false, stash);
    2531              : 
    2532              :   /* Then the encompassing namespace.  */
    2533         1092 :   if (encompassing && encompassing != ns)
    2534          946 :     find_derived_types (sym, encompassing->sym_root, name, true, stash);
    2535              : 
    2536         1092 :   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       347430 : gfc_find_component (gfc_symbol *sym, const char *name,
    2625              :                     bool noaccess, bool silent, gfc_ref **ref)
    2626              : {
    2627       347430 :   gfc_component *p, *check;
    2628       347430 :   gfc_ref *sref = NULL, *tmp = NULL;
    2629              : 
    2630       347430 :   if (name == NULL || sym == NULL)
    2631              :     return NULL;
    2632              : 
    2633       342435 :   if (sym->attr.flavor == FL_DERIVED)
    2634       333672 :     sym = gfc_use_derived (sym);
    2635              :   else
    2636         8763 :     gcc_assert (gfc_fl_struct (sym->attr.flavor));
    2637              : 
    2638       333672 :   if (sym == NULL)
    2639              :     return NULL;
    2640              : 
    2641              :   /* Handle UNIONs specially - mutually recursive with gfc_find_component. */
    2642       342433 :   if (sym->attr.flavor == FL_UNION)
    2643          500 :     return find_union_component (sym, name, noaccess, ref);
    2644              : 
    2645       341933 :   if (ref) *ref = NULL;
    2646       740075 :   for (p = sym->components; p; p = p->next)
    2647              :     {
    2648              :       /* Nest search into union's maps. */
    2649       704081 :       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       702389 :       else if (strcmp (p->name, name) == 0)
    2668              :         break;
    2669              : 
    2670       398142 :       continue;
    2671              :     }
    2672              : 
    2673       340241 :   if (p && sym->attr.use_assoc && !noaccess)
    2674              :     {
    2675        52811 :       bool is_parent_comp = sym->attr.extension && (p == sym->components);
    2676        52811 :       if (p->attr.access == ACCESS_PRIVATE ||
    2677              :           (p->attr.access != ACCESS_PUBLIC
    2678        51956 :            && 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        35994 :         && sym->attr.extension
    2690        24427 :         && sym->components->ts.type == BT_DERIVED)
    2691              :     {
    2692        24427 :       p = gfc_find_component (sym->components->ts.u.derived, name,
    2693              :                               noaccess, silent, ref);
    2694              :       /* Do not overwrite the error.  */
    2695        24427 :       if (p == NULL)
    2696              :         return p;
    2697              :     }
    2698              : 
    2699       339807 :   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       339807 :   if (p != NULL && ref)
    2713              :     {
    2714       268030 :       tmp = gfc_get_ref ();
    2715       268030 :       tmp->type = REF_COMPONENT;
    2716       268030 :       tmp->u.c.component = p;
    2717       268030 :       tmp->u.c.sym = sym;
    2718              :       /* Link the final component ref to the end of the chain of subrefs. */
    2719       268030 :       if (sref)
    2720              :         {
    2721              :           *ref = sref;
    2722              :           for (; sref->next; sref = sref->next)
    2723              :             ;
    2724              :           sref->next = tmp;
    2725              :         }
    2726              :       else
    2727       268030 :         *ref = tmp;
    2728              :     }
    2729              : 
    2730              :   return p;
    2731       398142 : }
    2732              : 
    2733              : 
    2734              : /* Given a symbol, free all of the component structures and everything
    2735              :    they point to.  */
    2736              : 
    2737              : void
    2738       280003 : gfc_free_component (gfc_component *p)
    2739              : {
    2740       280003 :   gfc_free_array_spec (p->as);
    2741       280003 :   gfc_free_expr (p->initializer);
    2742       280003 :   if (p->kind_expr)
    2743          270 :     gfc_free_expr (p->kind_expr);
    2744       280003 :   if (p->param_list)
    2745          240 :     gfc_free_actual_arglist (p->param_list);
    2746       280003 :   free (p->tb);
    2747       280003 :   p->tb = NULL;
    2748       280003 :   free (p);
    2749       280003 : }
    2750              : 
    2751              : 
    2752              : static void
    2753      6213086 : free_components (gfc_component *p)
    2754              : {
    2755      6213086 :   gfc_component *q;
    2756              : 
    2757      6493086 :   for (; p; p = q)
    2758              :     {
    2759       280000 :       q = p->next;
    2760       280000 :       gfc_free_component (p);
    2761              :     }
    2762            0 : }
    2763              : 
    2764              : 
    2765              : /******************** Statement label management ********************/
    2766              : 
    2767              : /* Comparison function for statement labels, used for managing the
    2768              :    binary tree.  */
    2769              : 
    2770              : static int
    2771         7673 : compare_st_labels (void *a1, void *b1)
    2772              : {
    2773         7673 :   gfc_st_label *a = (gfc_st_label *) a1;
    2774         7673 :   gfc_st_label *b = (gfc_st_label *) b1;
    2775              : 
    2776         7673 :   if (a->omp_region == b->omp_region)
    2777         7610 :     return b->value - a->value;
    2778              :   else
    2779           63 :     return b->omp_region - a->omp_region;
    2780              : }
    2781              : 
    2782              : 
    2783              : /* Free a single gfc_st_label structure, making sure the tree is not
    2784              :    messed up.  This function is called only when some parse error
    2785              :    occurs.  */
    2786              : 
    2787              : void
    2788            3 : gfc_free_st_label (gfc_st_label *label)
    2789              : {
    2790              : 
    2791            3 :   if (label == NULL)
    2792              :     return;
    2793              : 
    2794            3 :   gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
    2795              : 
    2796            3 :   if (label->format != NULL)
    2797            0 :     gfc_free_expr (label->format);
    2798              : 
    2799            3 :   free (label);
    2800              : }
    2801              : 
    2802              : 
    2803              : /* Free a whole tree of gfc_st_label structures.  */
    2804              : 
    2805              : static void
    2806       534801 : free_st_labels (gfc_st_label *label)
    2807              : {
    2808              : 
    2809       534801 :   if (label == NULL)
    2810              :     return;
    2811              : 
    2812         4698 :   free_st_labels (label->left);
    2813         4698 :   free_st_labels (label->right);
    2814              : 
    2815         4698 :   if (label->format != NULL)
    2816         1014 :     gfc_free_expr (label->format);
    2817         4698 :   free (label);
    2818              : }
    2819              : 
    2820              : 
    2821              : /* Given a label number, search for and return a pointer to the label
    2822              :    structure, creating it if it does not exist.  */
    2823              : 
    2824              : gfc_st_label *
    2825        13566 : gfc_get_st_label (int labelno)
    2826              : {
    2827        13566 :   gfc_st_label *lp;
    2828        13566 :   gfc_namespace *ns;
    2829        13566 :   int omp_region = gfc_omp_metadirective_region_stack.last ();
    2830              : 
    2831        13566 :   if (gfc_current_state () == COMP_DERIVED)
    2832            3 :     ns = gfc_current_block ()->f2k_derived;
    2833              :   else
    2834              :     {
    2835              :       /* Find the namespace of the scoping unit:
    2836              :          If we're in a BLOCK construct, jump to the parent namespace.  */
    2837        13563 :       ns = gfc_current_ns;
    2838        13574 :       while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
    2839           11 :         ns = ns->parent;
    2840              :     }
    2841              : 
    2842              :   /* First see if the label is already in this namespace.  */
    2843        13566 :   gcc_checking_assert (gfc_omp_metadirective_region_stack.length () > 0);
    2844        18343 :   for (int omp_region_idx = gfc_omp_metadirective_region_stack.length () - 1;
    2845        18343 :        omp_region_idx >= 0; omp_region_idx--)
    2846              :     {
    2847        13642 :       int omp_region2 = gfc_omp_metadirective_region_stack[omp_region_idx];
    2848        13642 :       lp = ns->st_labels;
    2849        31618 :       while (lp)
    2850              :         {
    2851        26841 :           if (lp->omp_region == omp_region2)
    2852              :             {
    2853        26583 :               if (lp->value == labelno)
    2854              :                 return lp;
    2855        17718 :               if (lp->value < labelno)
    2856        12799 :                 lp = lp->left;
    2857              :               else
    2858         4919 :                 lp = lp->right;
    2859              :             }
    2860          258 :           else if (lp->omp_region < omp_region2)
    2861          177 :             lp = lp->left;
    2862              :           else
    2863           81 :             lp = lp->right;
    2864              :         }
    2865              :     }
    2866              : 
    2867         4701 :   lp = XCNEW (gfc_st_label);
    2868              : 
    2869         4701 :   lp->value = labelno;
    2870         4701 :   lp->defined = ST_LABEL_UNKNOWN;
    2871         4701 :   lp->referenced = ST_LABEL_UNKNOWN;
    2872         4701 :   lp->ns = ns;
    2873         4701 :   lp->omp_region = omp_region;
    2874              : 
    2875         4701 :   gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
    2876              : 
    2877         4701 :   return lp;
    2878              : }
    2879              : 
    2880              : /* Rebind a statement label to a new OpenMP region. If a label with the same
    2881              :    value already exists in the new region, update it and return it. Otherwise,
    2882              :    move the label to the new region.  */
    2883              : 
    2884              : gfc_st_label *
    2885           44 : gfc_rebind_label (gfc_st_label *label, int new_omp_region)
    2886              : {
    2887           44 :   gfc_st_label *lp = label->ns->st_labels;
    2888           44 :   int labelno = label->value;
    2889              : 
    2890          106 :   while (lp)
    2891              :     {
    2892           97 :       if (lp->omp_region == new_omp_region)
    2893              :         {
    2894           38 :           if (lp->value == labelno)
    2895              :             {
    2896           35 :               if (lp == label)
    2897              :                 return label;
    2898            0 :               if (lp->defined == ST_LABEL_UNKNOWN
    2899            0 :                   && label->defined != ST_LABEL_UNKNOWN)
    2900            0 :                 lp->defined = label->defined;
    2901            0 :               if (lp->referenced == ST_LABEL_UNKNOWN
    2902            0 :                   && label->referenced != ST_LABEL_UNKNOWN)
    2903            0 :                 lp->referenced = label->referenced;
    2904            0 :               if (lp->format == NULL && label->format != NULL)
    2905            0 :                 lp->format = label->format;
    2906            0 :               gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
    2907            0 :               return lp;
    2908              :             }
    2909            3 :           if (lp->value < labelno)
    2910            2 :             lp = lp->left;
    2911              :           else
    2912            1 :             lp = lp->right;
    2913              :         }
    2914           59 :       else if (lp->omp_region < new_omp_region)
    2915           29 :         lp = lp->left;
    2916              :       else
    2917           30 :         lp = lp->right;
    2918              :     }
    2919              : 
    2920            9 :   gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
    2921            9 :   label->left = nullptr;
    2922            9 :   label->right = nullptr;
    2923            9 :   label->omp_region = new_omp_region;
    2924            9 :   gfc_insert_bbt (&label->ns->st_labels, label, compare_st_labels);
    2925            9 :   return label;
    2926              : }
    2927              : 
    2928              : /* Called when a statement with a statement label is about to be
    2929              :    accepted.  We add the label to the list of the current namespace,
    2930              :    making sure it hasn't been defined previously and referenced
    2931              :    correctly.  */
    2932              : 
    2933              : void
    2934         4685 : gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
    2935              : {
    2936         4685 :   int labelno;
    2937              : 
    2938         4685 :   labelno = lp->value;
    2939              : 
    2940         4685 :   if (lp->defined != ST_LABEL_UNKNOWN && !gfc_in_omp_metadirective_body)
    2941            2 :     gfc_error ("Duplicate statement label %d at %L and %L", labelno,
    2942              :                &lp->where, label_locus);
    2943              :   else
    2944              :     {
    2945         4683 :       lp->where = *label_locus;
    2946              : 
    2947         4683 :       switch (type)
    2948              :         {
    2949         1017 :         case ST_LABEL_FORMAT:
    2950         1017 :           if (lp->referenced == ST_LABEL_TARGET
    2951         1017 :               || lp->referenced == ST_LABEL_DO_TARGET)
    2952            0 :             gfc_error ("Label %d at %C already referenced as branch target",
    2953              :                        labelno);
    2954              :           else
    2955         1017 :             lp->defined = ST_LABEL_FORMAT;
    2956              : 
    2957              :           break;
    2958              : 
    2959         3659 :         case ST_LABEL_TARGET:
    2960         3659 :         case ST_LABEL_DO_TARGET:
    2961         3659 :           if (lp->referenced == ST_LABEL_FORMAT)
    2962            2 :             gfc_error ("Label %d at %C already referenced as a format label",
    2963              :                        labelno);
    2964              :           else
    2965         3657 :             lp->defined = type;
    2966              : 
    2967         1720 :           if (lp->referenced == ST_LABEL_DO_TARGET && type != ST_LABEL_DO_TARGET
    2968         3791 :               && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
    2969              :                                   "DO termination statement which is not END DO"
    2970              :                                   " or CONTINUE with label %d at %C", labelno))
    2971              :             return;
    2972              :           break;
    2973              : 
    2974            7 :         default:
    2975            7 :           lp->defined = ST_LABEL_BAD_TARGET;
    2976            7 :           lp->referenced = ST_LABEL_BAD_TARGET;
    2977              :         }
    2978              :     }
    2979              : }
    2980              : 
    2981              : 
    2982              : /* Reference a label.  Given a label and its type, see if that
    2983              :    reference is consistent with what is known about that label,
    2984              :    updating the unknown state.  Returns false if something goes
    2985              :    wrong.  */
    2986              : 
    2987              : bool
    2988        18026 : gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
    2989              : {
    2990        18026 :   gfc_sl_type label_type;
    2991        18026 :   int labelno;
    2992        18026 :   bool rc;
    2993              : 
    2994        18026 :   if (lp == NULL)
    2995              :     return true;
    2996              : 
    2997         7628 :   labelno = lp->value;
    2998              : 
    2999         7628 :   if (lp->defined != ST_LABEL_UNKNOWN)
    3000              :     label_type = lp->defined;
    3001              :   else
    3002              :     {
    3003         5968 :       label_type = lp->referenced;
    3004         5968 :       lp->where = gfc_current_locus;
    3005              :     }
    3006              : 
    3007         7628 :   if (label_type == ST_LABEL_FORMAT
    3008         1127 :       && (type == ST_LABEL_TARGET || type == ST_LABEL_DO_TARGET))
    3009              :     {
    3010            0 :       gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
    3011            0 :       rc = false;
    3012            0 :       goto done;
    3013              :     }
    3014              : 
    3015         7628 :   if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_DO_TARGET
    3016         7628 :        || label_type == ST_LABEL_BAD_TARGET)
    3017         2440 :       && type == ST_LABEL_FORMAT)
    3018              :     {
    3019            5 :       gfc_error ("Label %d at %C previously used as branch target", labelno);
    3020            5 :       rc = false;
    3021            5 :       goto done;
    3022              :     }
    3023              : 
    3024          622 :   if (lp->referenced == ST_LABEL_DO_TARGET && type == ST_LABEL_DO_TARGET
    3025          543 :       && !gfc_in_omp_metadirective_body
    3026         8164 :       && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
    3027              :                           "Shared DO termination label %d at %C", labelno))
    3028              :     return false;
    3029              : 
    3030         7623 :   if (type == ST_LABEL_DO_TARGET
    3031         7623 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "Labeled DO statement "
    3032              :                           "at %L", &gfc_current_locus))
    3033              :     return false;
    3034              : 
    3035         7623 :   if (lp->referenced != ST_LABEL_DO_TARGET)
    3036         7001 :     lp->referenced = type;
    3037              :   rc = true;
    3038              : 
    3039              : done:
    3040              :   return rc;
    3041              : }
    3042              : 
    3043              : 
    3044              : /************** Symbol table management subroutines ****************/
    3045              : 
    3046              : /* Basic details: Fortran 95 requires a potentially unlimited number
    3047              :    of distinct namespaces when compiling a program unit.  This case
    3048              :    occurs during a compilation of internal subprograms because all of
    3049              :    the internal subprograms must be read before we can start
    3050              :    generating code for the host.
    3051              : 
    3052              :    Given the tricky nature of the Fortran grammar, we must be able to
    3053              :    undo changes made to a symbol table if the current interpretation
    3054              :    of a statement is found to be incorrect.  Whenever a symbol is
    3055              :    looked up, we make a copy of it and link to it.  All of these
    3056              :    symbols are kept in a vector so that we can commit or
    3057              :    undo the changes at a later time.
    3058              : 
    3059              :    A symtree may point to a symbol node outside of its namespace.  In
    3060              :    this case, that symbol has been used as a host associated variable
    3061              :    at some previous time.  */
    3062              : 
    3063              : /* Allocate a new namespace structure.  Copies the implicit types from
    3064              :    PARENT if PARENT_TYPES is set.  */
    3065              : 
    3066              : gfc_namespace *
    3067       553316 : gfc_get_namespace (gfc_namespace *parent, int parent_types)
    3068              : {
    3069       553316 :   gfc_namespace *ns;
    3070       553316 :   gfc_typespec *ts;
    3071       553316 :   int in;
    3072       553316 :   int i;
    3073              : 
    3074       553316 :   ns = XCNEW (gfc_namespace);
    3075       553316 :   ns->sym_root = NULL;
    3076       553316 :   ns->uop_root = NULL;
    3077       553316 :   ns->tb_sym_root = NULL;
    3078       553316 :   ns->finalizers = NULL;
    3079       553316 :   ns->default_access = ACCESS_UNKNOWN;
    3080       553316 :   ns->parent = parent;
    3081              : 
    3082     16046164 :   for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
    3083              :     {
    3084     15492848 :       ns->operator_access[in] = ACCESS_UNKNOWN;
    3085     15492848 :       ns->tb_op[in] = NULL;
    3086              :     }
    3087              : 
    3088              :   /* Initialize default implicit types.  */
    3089     14939532 :   for (i = 'a'; i <= 'z'; i++)
    3090              :     {
    3091     14386216 :       ns->set_flag[i - 'a'] = 0;
    3092     14386216 :       ts = &ns->default_type[i - 'a'];
    3093              : 
    3094     14386216 :       if (parent_types && ns->parent != NULL)
    3095              :         {
    3096              :           /* Copy parent settings.  */
    3097      1746706 :           *ts = ns->parent->default_type[i - 'a'];
    3098      1746706 :           continue;
    3099              :         }
    3100              : 
    3101     12639510 :       if (flag_implicit_none != 0)
    3102              :         {
    3103       108602 :           gfc_clear_ts (ts);
    3104       108602 :           continue;
    3105              :         }
    3106              : 
    3107     12530908 :       if ('i' <= i && i <= 'n')
    3108              :         {
    3109      2891748 :           ts->type = BT_INTEGER;
    3110      2891748 :           ts->kind = gfc_default_integer_kind;
    3111              :         }
    3112              :       else
    3113              :         {
    3114      9639160 :           ts->type = BT_REAL;
    3115      9639160 :           ts->kind = gfc_default_real_kind;
    3116              :         }
    3117              :     }
    3118              : 
    3119       553316 :   ns->refs = 1;
    3120              : 
    3121       553316 :   return ns;
    3122              : }
    3123              : 
    3124              : 
    3125              : /* Comparison function for symtree nodes.  */
    3126              : 
    3127              : static int
    3128     34911280 : compare_symtree (void *_st1, void *_st2)
    3129              : {
    3130     34911280 :   gfc_symtree *st1, *st2;
    3131              : 
    3132     34911280 :   st1 = (gfc_symtree *) _st1;
    3133     34911280 :   st2 = (gfc_symtree *) _st2;
    3134              : 
    3135     34911280 :   return strcmp (st1->name, st2->name);
    3136              : }
    3137              : 
    3138              : 
    3139              : /* Allocate a new symtree node and associate it with the new symbol.  */
    3140              : 
    3141              : gfc_symtree *
    3142      6396726 : gfc_new_symtree (gfc_symtree **root, const char *name)
    3143              : {
    3144      6396726 :   gfc_symtree *st;
    3145              : 
    3146      6396726 :   st = XCNEW (gfc_symtree);
    3147      6396726 :   st->name = gfc_get_string ("%s", name);
    3148              : 
    3149      6396726 :   gfc_insert_bbt (root, st, compare_symtree);
    3150      6396726 :   return st;
    3151              : }
    3152              : 
    3153              : 
    3154              : /* Delete a symbol from the tree.  Does not free the symbol itself!  */
    3155              : 
    3156              : void
    3157      4178773 : gfc_delete_symtree (gfc_symtree **root, const char *name)
    3158              : {
    3159      4178773 :   gfc_symtree st, *st0;
    3160      4178773 :   const char *p;
    3161              : 
    3162              :   /* Submodules are marked as mod.submod.  When freeing a submodule
    3163              :      symbol, the symtree only has "submod", so adjust that here.  */
    3164              : 
    3165      4178773 :   p = strrchr(name, '.');
    3166      4178773 :   if (p)
    3167            0 :     p++;
    3168              :   else
    3169              :     p = name;
    3170              : 
    3171      4178773 :   st.name = gfc_get_string ("%s", p);
    3172      4178773 :   st0 = (gfc_symtree *) gfc_delete_bbt (root, &st, compare_symtree);
    3173              : 
    3174      4178773 :   free (st0);
    3175      4178773 : }
    3176              : 
    3177              : 
    3178              : /* Given a root symtree node and a name, try to find the symbol within
    3179              :    the namespace.  Returns NULL if the symbol is not found.  */
    3180              : 
    3181              : gfc_symtree *
    3182     30286910 : gfc_find_symtree (gfc_symtree *st, const char *name)
    3183              : {
    3184     30286910 :   int c;
    3185              : 
    3186    130398140 :   while (st != NULL)
    3187              :     {
    3188    112170090 :       c = strcmp (name, st->name);
    3189    112170090 :       if (c == 0)
    3190              :         return st;
    3191              : 
    3192    100111230 :       st = (c < 0) ? st->left : st->right;
    3193              :     }
    3194              : 
    3195              :   return NULL;
    3196              : }
    3197              : 
    3198              : 
    3199              : /* Return a symtree node with a name that is guaranteed to be unique
    3200              :    within the namespace and corresponds to an illegal fortran name.  */
    3201              : 
    3202              : gfc_symtree *
    3203       650858 : gfc_get_unique_symtree (gfc_namespace *ns)
    3204              : {
    3205       650858 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    3206       650858 :   static int serial = 0;
    3207              : 
    3208       650858 :   sprintf (name, "@%d", serial++);
    3209       650858 :   if (ns)
    3210       650846 :     return gfc_new_symtree (&ns->sym_root, name);
    3211              :   else
    3212              :     {
    3213              :       /* Some uses need a symtree that is cleaned up locally.  */
    3214           12 :       gfc_symtree *st = XCNEW (gfc_symtree);
    3215           12 :       st->name = gfc_get_string ("%s", name);
    3216           12 :       return st;
    3217              :     }
    3218              : }
    3219              : 
    3220              : 
    3221              : /* Given a name find a user operator node, creating it if it doesn't
    3222              :    exist.  These are much simpler than symbols because they can't be
    3223              :    ambiguous with one another.  */
    3224              : 
    3225              : gfc_user_op *
    3226          974 : gfc_get_uop (const char *name)
    3227              : {
    3228          974 :   gfc_user_op *uop;
    3229          974 :   gfc_symtree *st;
    3230          974 :   gfc_namespace *ns = gfc_current_ns;
    3231              : 
    3232          974 :   if (ns->omp_udr_ns)
    3233           35 :     ns = ns->parent;
    3234          974 :   st = gfc_find_symtree (ns->uop_root, name);
    3235          974 :   if (st != NULL)
    3236          594 :     return st->n.uop;
    3237              : 
    3238          380 :   st = gfc_new_symtree (&ns->uop_root, name);
    3239              : 
    3240          380 :   uop = st->n.uop = XCNEW (gfc_user_op);
    3241          380 :   uop->name = gfc_get_string ("%s", name);
    3242          380 :   uop->access = ACCESS_UNKNOWN;
    3243          380 :   uop->ns = ns;
    3244              : 
    3245          380 :   return uop;
    3246              : }
    3247              : 
    3248              : 
    3249              : /* Given a name find the user operator node.  Returns NULL if it does
    3250              :    not exist.  */
    3251              : 
    3252              : gfc_user_op *
    3253         6882 : gfc_find_uop (const char *name, gfc_namespace *ns)
    3254              : {
    3255         6882 :   gfc_symtree *st;
    3256              : 
    3257         6882 :   if (ns == NULL)
    3258           18 :     ns = gfc_current_ns;
    3259              : 
    3260         6882 :   st = gfc_find_symtree (ns->uop_root, name);
    3261         6882 :   return (st == NULL) ? NULL : st->n.uop;
    3262              : }
    3263              : 
    3264              : 
    3265              : /* Update a symbol's common_block field, and take care of the associated
    3266              :    memory management.  */
    3267              : 
    3268              : static void
    3269      7662874 : set_symbol_common_block (gfc_symbol *sym, gfc_common_head *common_block)
    3270              : {
    3271      7662874 :   if (sym->common_block == common_block)
    3272              :     return;
    3273              : 
    3274         5855 :   if (sym->common_block && sym->common_block->name[0] != '\0')
    3275              :     {
    3276         5570 :       sym->common_block->refs--;
    3277         5570 :       if (sym->common_block->refs == 0)
    3278         1803 :         free (sym->common_block);
    3279              :     }
    3280         5855 :   sym->common_block = common_block;
    3281              : }
    3282              : 
    3283              : 
    3284              : /* Remove a gfc_symbol structure and everything it points to.  */
    3285              : 
    3286              : void
    3287      6368186 : gfc_free_symbol (gfc_symbol *&sym)
    3288              : {
    3289              : 
    3290      6368186 :   if (sym == NULL)
    3291              :     return;
    3292              : 
    3293      6213086 :   gfc_free_array_spec (sym->as);
    3294              : 
    3295      6213086 :   free_components (sym->components);
    3296              : 
    3297      6213086 :   gfc_free_expr (sym->value);
    3298              : 
    3299      6213086 :   gfc_free_namelist (sym->namelist);
    3300              : 
    3301      6213086 :   if (sym->ns != sym->formal_ns)
    3302      6162045 :     gfc_free_namespace (sym->formal_ns);
    3303              : 
    3304      6213086 :   if (!sym->attr.generic_copy)
    3305      6213086 :     gfc_free_interface (sym->generic);
    3306              : 
    3307      6213086 :   gfc_free_formal_arglist (sym->formal);
    3308              : 
    3309              :   /* The pdt_type f2k_derived namespaces are copies of that of the pdt_template
    3310              :      and are only made if there are finalizers. The complete list of finalizers
    3311              :      is kept by the pdt_template and are freed with its f2k_derived.  */
    3312      6213086 :   if (!sym->attr.pdt_type)
    3313      6212949 :     gfc_free_namespace (sym->f2k_derived);
    3314          137 :   else if (sym->f2k_derived && sym->f2k_derived->finalizers)
    3315              :     {
    3316            0 :       gfc_finalizer *p, *q = NULL;
    3317            0 :       for (p = sym->f2k_derived->finalizers; p; p = q)
    3318              :         {
    3319            0 :           q = p->next;
    3320            0 :           free (p);
    3321              :         }
    3322            0 :       free (sym->f2k_derived);
    3323              :     }
    3324              : 
    3325      6213086 :   set_symbol_common_block (sym, NULL);
    3326              : 
    3327      6213086 :   if (sym->param_list)
    3328         1420 :     gfc_free_actual_arglist (sym->param_list);
    3329              : 
    3330      6213086 :   free (sym);
    3331      6213086 :   sym = NULL;
    3332              : }
    3333              : 
    3334              : 
    3335              : /* Returns true if the symbol SYM has, through its FORMAL_NS field, a reference
    3336              :    to itself which should be eliminated for the symbol memory to be released
    3337              :    via normal reference counting.
    3338              : 
    3339              :    The implementation is crucial as it controls the proper release of symbols,
    3340              :    especially (contained) procedure symbols, which can represent a lot of memory
    3341              :    through the namespace of their body.
    3342              : 
    3343              :    We try to avoid freeing too much memory (causing dangling pointers), to not
    3344              :    leak too much (wasting memory), and to avoid expensive walks of the symbol
    3345              :    tree (which would be the correct way to check for a cycle).  */
    3346              : 
    3347              : bool
    3348      6274754 : cyclic_reference_break_needed (gfc_symbol *sym)
    3349              : {
    3350              :   /* Normal symbols don't reference themselves.  */
    3351      6274754 :   if (sym->formal_ns == nullptr)
    3352              :     return false;
    3353              : 
    3354              :   /* Procedures at the root of the file do have a self reference, but they don't
    3355              :      have a reference in a parent namespace preventing the release of the
    3356              :      procedure namespace, so they can use the normal reference counting.  */
    3357       306149 :   if (sym->formal_ns == sym->ns)
    3358              :     return false;
    3359              : 
    3360              :   /* If sym->refs == 1, we can use normal reference counting.  If sym->refs > 2,
    3361              :      the symbol won't be freed anyway, with or without cyclic reference.  */
    3362       297578 :   if (sym->refs != 2)
    3363              :     return false;
    3364              : 
    3365              :   /* Procedure symbols host-associated from a module in submodules are special,
    3366              :      because the namespace of the procedure block in the submodule is different
    3367              :      from the FORMAL_NS namespace generated by host-association.  So there are
    3368              :      two different namespaces representing the same procedure namespace.  As
    3369              :      FORMAL_NS comes from host-association, which only imports symbols visible
    3370              :      from the outside (dummy arguments basically), we can assume there is no
    3371              :      self reference through FORMAL_NS in that case.  */
    3372        48032 :   if (sym->attr.host_assoc && sym->attr.used_in_submodule)
    3373          364 :     return false;
    3374              : 
    3375              :   /* We can assume that contained procedures have cyclic references, because
    3376              :      the symbol of the procedure itself is accessible in the procedure body
    3377              :      namespace.  So we assume that symbols with a formal namespace different
    3378              :      from the declaration namespace and two references, one of which is about
    3379              :      to be removed, are procedures with just the self reference left.  At this
    3380              :      point, the symbol SYM matches that pattern, so we return true here to
    3381              :      permit the release of SYM.  */
    3382              :   return true;
    3383              : }
    3384              : 
    3385              : 
    3386              : /* Decrease the reference counter and free memory when we reach zero.
    3387              :    Returns true if the symbol has been freed, false otherwise.  */
    3388              : 
    3389              : bool
    3390      6275374 : gfc_release_symbol (gfc_symbol *&sym)
    3391              : {
    3392      6275374 :   if (sym == NULL)
    3393              :     return false;
    3394              : 
    3395      6274754 :   if (cyclic_reference_break_needed (sym))
    3396              :     {
    3397              :       /* As formal_ns contains a reference to sym, delete formal_ns just
    3398              :          before the deletion of sym.  */
    3399        47668 :       gfc_namespace *ns = sym->formal_ns;
    3400        47668 :       sym->formal_ns = NULL;
    3401        47668 :       gfc_free_namespace (ns);
    3402              :     }
    3403              : 
    3404      6274754 :   sym->refs--;
    3405      6274754 :   if (sym->refs > 0)
    3406              :     return false;
    3407              : 
    3408      6159189 :   gcc_assert (sym->refs == 0);
    3409      6159189 :   gfc_free_symbol (sym);
    3410      6159189 :   return true;
    3411              : }
    3412              : 
    3413              : 
    3414              : /* Allocate and initialize a new symbol node.  */
    3415              : 
    3416              : gfc_symbol *
    3417      6294519 : gfc_new_symbol (const char *name, gfc_namespace *ns, locus *where)
    3418              : {
    3419      6294519 :   gfc_symbol *p;
    3420              : 
    3421      6294519 :   p = XCNEW (gfc_symbol);
    3422              : 
    3423      6294519 :   gfc_clear_ts (&p->ts);
    3424      6294519 :   gfc_clear_attr (&p->attr);
    3425      6294519 :   p->ns = ns;
    3426      6294519 :   p->declared_at = where ? *where : gfc_current_locus;
    3427      6294519 :   p->name = gfc_get_string ("%s", name);
    3428              : 
    3429      6294519 :   return p;
    3430              : }
    3431              : 
    3432              : 
    3433              : /* Generate an error if a symbol is ambiguous, and set the error flag
    3434              :    on it.  */
    3435              : 
    3436              : static void
    3437           40 : ambiguous_symbol (const char *name, gfc_symtree *st)
    3438              : {
    3439              : 
    3440           40 :   if (st->n.sym->error)
    3441              :     return;
    3442              : 
    3443           20 :   if (st->n.sym->module)
    3444           17 :     gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
    3445              :                "from module %qs", name, st->n.sym->name, st->n.sym->module);
    3446              :   else
    3447            3 :     gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
    3448              :                "from current program unit", name, st->n.sym->name);
    3449              : 
    3450           20 :   st->n.sym->error = 1;
    3451              : }
    3452              : 
    3453              : 
    3454              : /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
    3455              :    selector on the stack. If yes, replace it by the corresponding temporary.  */
    3456              : 
    3457              : static void
    3458     10719644 : select_type_insert_tmp (gfc_symtree **st)
    3459              : {
    3460     10769890 :   gfc_select_type_stack *stack = select_type_stack;
    3461     10945447 :   for (; stack; stack = stack->prev)
    3462       225803 :     if ((*st)->n.sym == stack->selector && stack->tmp)
    3463              :       {
    3464        50246 :         *st = stack->tmp;
    3465        50246 :         select_type_insert_tmp (st);
    3466        50246 :         return;
    3467              :       }
    3468              : }
    3469              : 
    3470              : 
    3471              : /* Look for a symtree in the current procedure -- that is, go up to
    3472              :    parent namespaces but only if inside a BLOCK.  Returns NULL if not found.  */
    3473              : 
    3474              : gfc_symtree*
    3475          241 : gfc_find_symtree_in_proc (const char* name, gfc_namespace* ns)
    3476              : {
    3477          290 :   while (ns)
    3478              :     {
    3479          290 :       gfc_symtree* st = gfc_find_symtree (ns->sym_root, name);
    3480          290 :       if (st)
    3481              :         return st;
    3482              : 
    3483           51 :       if (!ns->construct_entities)
    3484              :         break;
    3485           49 :       ns = ns->parent;
    3486              :     }
    3487              : 
    3488              :   return NULL;
    3489              : }
    3490              : 
    3491              : 
    3492              : /* Search for a symtree starting in the current namespace, resorting to
    3493              :    any parent namespaces if requested by a nonzero parent_flag.
    3494              :    Returns true if the name is ambiguous.  */
    3495              : 
    3496              : bool
    3497     19187268 : gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
    3498              :                    gfc_symtree **result)
    3499              : {
    3500     19187268 :   gfc_symtree *st;
    3501              : 
    3502     19187268 :   if (ns == NULL)
    3503      7798405 :     ns = gfc_current_ns;
    3504              : 
    3505     21828445 :   do
    3506              :     {
    3507     21828445 :       st = gfc_find_symtree (ns->sym_root, name);
    3508     21828445 :       if (st != NULL)
    3509              :         {
    3510     10719644 :           select_type_insert_tmp (&st);
    3511              : 
    3512     10719644 :           *result = st;
    3513              :           /* Ambiguous generic interfaces are permitted, as long
    3514              :              as the specific interfaces are different.  */
    3515     10719644 :           if (st->ambiguous && !st->n.sym->attr.generic)
    3516              :             {
    3517           36 :               ambiguous_symbol (name, st);
    3518           36 :               return true;
    3519              :             }
    3520              : 
    3521              :           return false;
    3522              :         }
    3523              : 
    3524     11108801 :       if (!parent_flag)
    3525              :         break;
    3526              : 
    3527              :       /* Don't escape an interface block.  */
    3528      8203488 :       if (ns && !ns->has_import_set
    3529      8191076 :           && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
    3530              :         break;
    3531              : 
    3532      8003428 :       ns = ns->parent;
    3533              :     }
    3534      8003428 :   while (ns != NULL);
    3535              : 
    3536      8467624 :   if (gfc_current_state() == COMP_DERIVED
    3537       192264 :       && gfc_current_block ()->attr.pdt_template)
    3538              :     {
    3539              :       gfc_symbol *der = gfc_current_block ();
    3540        23686 :       for (; der; der = gfc_get_derived_super_type (der))
    3541              :         {
    3542        13272 :           if (der->f2k_derived && der->f2k_derived->sym_root)
    3543              :             {
    3544        12916 :               st = gfc_find_symtree (der->f2k_derived->sym_root, name);
    3545        12916 :               if (st)
    3546              :                 break;
    3547              :             }
    3548              :         }
    3549        12602 :       *result = st;
    3550        12602 :       return false;
    3551              :     }
    3552              : 
    3553      8455022 :   *result = NULL;
    3554              : 
    3555      8455022 :   return false;
    3556              : }
    3557              : 
    3558              : 
    3559              : /* Same, but returns the symbol instead.  */
    3560              : 
    3561              : int
    3562      2352572 : gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
    3563              :                  gfc_symbol **result)
    3564              : {
    3565      2352572 :   gfc_symtree *st;
    3566      2352572 :   int i;
    3567              : 
    3568      2352572 :   i = gfc_find_sym_tree (name, ns, parent_flag, &st);
    3569              : 
    3570      2352572 :   if (st == NULL)
    3571      1766817 :     *result = NULL;
    3572              :   else
    3573       585755 :     *result = st->n.sym;
    3574              : 
    3575      2352572 :   return i;
    3576              : }
    3577              : 
    3578              : 
    3579              : /* Tells whether there is only one set of changes in the stack.  */
    3580              : 
    3581              : static bool
    3582     41138368 : single_undo_checkpoint_p (void)
    3583              : {
    3584     41138368 :   if (latest_undo_chgset == &default_undo_chgset_var)
    3585              :     {
    3586     41138368 :       gcc_assert (latest_undo_chgset->previous == NULL);
    3587              :       return true;
    3588              :     }
    3589              :   else
    3590              :     {
    3591            0 :       gcc_assert (latest_undo_chgset->previous != NULL);
    3592              :       return false;
    3593              :     }
    3594              : }
    3595              : 
    3596              : /* Save symbol with the information necessary to back it out.  */
    3597              : 
    3598              : void
    3599      6171326 : gfc_save_symbol_data (gfc_symbol *sym)
    3600              : {
    3601      6171326 :   gfc_symbol *s;
    3602      6171326 :   unsigned i;
    3603              : 
    3604      6171326 :   if (!single_undo_checkpoint_p ())
    3605              :     {
    3606              :       /* If there is more than one change set, look for the symbol in the
    3607              :          current one.  If it is found there, we can reuse it.  */
    3608            0 :       FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
    3609            0 :         if (s == sym)
    3610              :           {
    3611            0 :             gcc_assert (sym->gfc_new || sym->old_symbol != NULL);
    3612      6171326 :             return;
    3613              :           }
    3614              :     }
    3615      6171326 :   else if (sym->gfc_new || sym->old_symbol != NULL)
    3616              :     return;
    3617              : 
    3618      3141537 :   s = XCNEW (gfc_symbol);
    3619      3141537 :   *s = *sym;
    3620      3141537 :   sym->old_symbol = s;
    3621      3141537 :   sym->gfc_new = 0;
    3622              : 
    3623      3141537 :   latest_undo_chgset->syms.safe_push (sym);
    3624              : }
    3625              : 
    3626              : 
    3627              : /* Given a name, find a symbol, or create it if it does not exist yet
    3628              :    in the current namespace.  If the symbol is found we make sure that
    3629              :    it's OK.
    3630              : 
    3631              :    The integer return code indicates
    3632              :      0   All OK
    3633              :      1   The symbol name was ambiguous
    3634              :      2   The name meant to be established was already host associated.
    3635              : 
    3636              :    So if the return value is nonzero, then an error was issued.  */
    3637              : 
    3638              : int
    3639      6083521 : gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
    3640              :                   bool allow_subroutine, locus *where)
    3641              : {
    3642      6083521 :   gfc_symtree *st;
    3643      6083521 :   gfc_symbol *p;
    3644              : 
    3645              :   /* This doesn't usually happen during resolution.  */
    3646      6083521 :   if (ns == NULL)
    3647      2988237 :     ns = gfc_current_ns;
    3648              : 
    3649              :   /* Try to find the symbol in ns.  */
    3650      6083521 :   st = gfc_find_symtree (ns->sym_root, name);
    3651              : 
    3652      6083521 :   if (st == NULL && ns->omp_udr_ns)
    3653              :     {
    3654          319 :       ns = ns->parent;
    3655          319 :       st = gfc_find_symtree (ns->sym_root, name);
    3656              :     }
    3657              : 
    3658      5205532 :   if (st == NULL)
    3659              :     {
    3660              :       /* If not there, create a new symbol.  */
    3661      5205402 :       p = gfc_new_symbol (name, ns, where);
    3662              : 
    3663              :       /* Add to the list of tentative symbols.  */
    3664      5205402 :       p->old_symbol = NULL;
    3665      5205402 :       p->mark = 1;
    3666      5205402 :       p->gfc_new = 1;
    3667      5205402 :       latest_undo_chgset->syms.safe_push (p);
    3668              : 
    3669      5205402 :       st = gfc_new_symtree (&ns->sym_root, name);
    3670      5205402 :       st->n.sym = p;
    3671      5205402 :       p->refs++;
    3672              : 
    3673              :     }
    3674              :   else
    3675              :     {
    3676              :       /* Make sure the existing symbol is OK.  Ambiguous
    3677              :          generic interfaces are permitted, as long as the
    3678              :          specific interfaces are different.  */
    3679       878119 :       if (st->ambiguous && !st->n.sym->attr.generic)
    3680              :         {
    3681            4 :           ambiguous_symbol (name, st);
    3682            4 :           return 1;
    3683              :         }
    3684              : 
    3685       878115 :       p = st->n.sym;
    3686       878115 :       if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
    3687        10370 :           && !(allow_subroutine && p->attr.subroutine)
    3688        10362 :           && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
    3689        10320 :           && (ns->has_import_set || p->attr.imported)))
    3690              :         {
    3691              :           /* Symbol is from another namespace.  */
    3692           43 :           gfc_error ("Symbol %qs at %C has already been host associated",
    3693              :                      name);
    3694           43 :           return 2;
    3695              :         }
    3696              : 
    3697       878072 :       p->mark = 1;
    3698              : 
    3699              :       /* Copy in case this symbol is changed.  */
    3700       878072 :       gfc_save_symbol_data (p);
    3701              :     }
    3702              : 
    3703      6083474 :   *result = st;
    3704      6083474 :   return 0;
    3705              : }
    3706              : 
    3707              : 
    3708              : int
    3709      1029131 : gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result,
    3710              :                 locus *where)
    3711              : {
    3712      1029131 :   gfc_symtree *st;
    3713      1029131 :   int i;
    3714              : 
    3715      1029131 :   i = gfc_get_sym_tree (name, ns, &st, false, where);
    3716      1029131 :   if (i != 0)
    3717              :     return i;
    3718              : 
    3719      1029114 :   if (st)
    3720      1029114 :     *result = st->n.sym;
    3721              :   else
    3722            0 :     *result = NULL;
    3723              :   return i;
    3724              : }
    3725              : 
    3726              : 
    3727              : /* Subroutine that searches for a symbol, creating it if it doesn't
    3728              :    exist, but tries to host-associate the symbol if possible.  */
    3729              : 
    3730              : int
    3731      7980375 : gfc_get_ha_sym_tree (const char *name, gfc_symtree **result, locus *where)
    3732              : {
    3733      7980375 :   gfc_symtree *st;
    3734      7980375 :   int i;
    3735              : 
    3736      7980375 :   i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
    3737              : 
    3738      7980375 :   if (st != NULL)
    3739              :     {
    3740      5226614 :       gfc_save_symbol_data (st->n.sym);
    3741      5226614 :       *result = st;
    3742      5226614 :       return i;
    3743              :     }
    3744              : 
    3745      2753761 :   i = gfc_find_sym_tree (name, gfc_current_ns, 1, &st);
    3746      2753761 :   if (i)
    3747              :     return i;
    3748              : 
    3749      2753761 :   if (st != NULL)
    3750              :     {
    3751       273661 :       *result = st;
    3752       273661 :       return 0;
    3753              :     }
    3754              : 
    3755      2480100 :   return gfc_get_sym_tree (name, gfc_current_ns, result, false, where);
    3756              : }
    3757              : 
    3758              : 
    3759              : int
    3760        32945 : gfc_get_ha_symbol (const char *name, gfc_symbol **result, locus *where)
    3761              : {
    3762        32945 :   int i;
    3763        32945 :   gfc_symtree *st = NULL;
    3764              : 
    3765        32945 :   i = gfc_get_ha_sym_tree (name, &st, where);
    3766              : 
    3767        32945 :   if (st)
    3768        32945 :     *result = st->n.sym;
    3769              :   else
    3770            0 :     *result = NULL;
    3771              : 
    3772        32945 :   return i;
    3773              : }
    3774              : 
    3775              : 
    3776              : /* Search for the symtree belonging to a gfc_common_head; we cannot use
    3777              :    head->name as the common_root symtree's name might be mangled.  */
    3778              : 
    3779              : static gfc_symtree *
    3780           18 : find_common_symtree (gfc_symtree *st, gfc_common_head *head)
    3781              : {
    3782              : 
    3783           21 :   gfc_symtree *result;
    3784              : 
    3785           21 :   if (st == NULL)
    3786              :     return NULL;
    3787              : 
    3788           15 :   if (st->n.common == head)
    3789              :     return st;
    3790              : 
    3791            3 :   result = find_common_symtree (st->left, head);
    3792            3 :   if (!result)
    3793            3 :     result = find_common_symtree (st->right, head);
    3794              : 
    3795              :   return result;
    3796              : }
    3797              : 
    3798              : 
    3799              : /* Restore previous state of symbol.  Just copy simple stuff.  */
    3800              : 
    3801              : static void
    3802      1449788 : restore_old_symbol (gfc_symbol *p)
    3803              : {
    3804      1449788 :   gfc_symbol *old;
    3805              : 
    3806      1449788 :   p->mark = 0;
    3807      1449788 :   old = p->old_symbol;
    3808              : 
    3809      1449788 :   p->ts.type = old->ts.type;
    3810      1449788 :   p->ts.kind = old->ts.kind;
    3811              : 
    3812      1449788 :   p->attr = old->attr;
    3813              : 
    3814      1449788 :   if (p->value != old->value)
    3815              :     {
    3816            1 :       gcc_checking_assert (old->value == NULL);
    3817            1 :       gfc_free_expr (p->value);
    3818            1 :       p->value = NULL;
    3819              :     }
    3820              : 
    3821      1449788 :   if (p->as != old->as)
    3822              :     {
    3823            7 :       if (p->as)
    3824            7 :         gfc_free_array_spec (p->as);
    3825            7 :       p->as = old->as;
    3826              :     }
    3827              : 
    3828      1449788 :   p->generic = old->generic;
    3829      1449788 :   p->component_access = old->component_access;
    3830              : 
    3831      1449788 :   if (p->namelist != NULL && old->namelist == NULL)
    3832              :     {
    3833            0 :       gfc_free_namelist (p->namelist);
    3834            0 :       p->namelist = NULL;
    3835              :     }
    3836              :   else
    3837              :     {
    3838      1449788 :       if (p->namelist_tail != old->namelist_tail)
    3839              :         {
    3840            1 :           gfc_free_namelist (old->namelist_tail->next);
    3841            1 :           old->namelist_tail->next = NULL;
    3842              :         }
    3843              :     }
    3844              : 
    3845      1449788 :   p->namelist_tail = old->namelist_tail;
    3846              : 
    3847      1449788 :   if (p->formal != old->formal)
    3848              :     {
    3849           28 :       gfc_free_formal_arglist (p->formal);
    3850           28 :       p->formal = old->formal;
    3851              :     }
    3852              : 
    3853      1449788 :   set_symbol_common_block (p, old->common_block);
    3854      1449788 :   p->common_head = old->common_head;
    3855              : 
    3856      1449788 :   p->old_symbol = old->old_symbol;
    3857      1449788 :   free (old);
    3858      1449788 : }
    3859              : 
    3860              : 
    3861              : /* Frees the internal data of a gfc_undo_change_set structure.  Doesn't free
    3862              :    the structure itself.  */
    3863              : 
    3864              : static void
    3865        81126 : free_undo_change_set_data (gfc_undo_change_set &cs)
    3866              : {
    3867            0 :   cs.syms.release ();
    3868        81126 :   cs.tbps.release ();
    3869            0 : }
    3870              : 
    3871              : 
    3872              : /* Given a change set pointer, free its target's contents and update it with
    3873              :    the address of the previous change set.  Note that only the contents are
    3874              :    freed, not the target itself (the contents' container).  It is not a problem
    3875              :    as the latter will be a local variable usually.  */
    3876              : 
    3877              : static void
    3878            0 : pop_undo_change_set (gfc_undo_change_set *&cs)
    3879              : {
    3880            0 :   free_undo_change_set_data (*cs);
    3881            0 :   cs = cs->previous;
    3882            0 : }
    3883              : 
    3884              : 
    3885              : static void free_old_symbol (gfc_symbol *sym);
    3886              : 
    3887              : 
    3888              : /* Merges the current change set into the previous one.  The changes themselves
    3889              :    are left untouched; only one checkpoint is forgotten.  */
    3890              : 
    3891              : void
    3892            0 : gfc_drop_last_undo_checkpoint (void)
    3893              : {
    3894            0 :   gfc_symbol *s, *t;
    3895            0 :   unsigned i, j;
    3896              : 
    3897            0 :   FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
    3898              :     {
    3899              :       /* No need to loop in this case.  */
    3900            0 :       if (s->old_symbol == NULL)
    3901            0 :         continue;
    3902              : 
    3903              :       /* Remove the duplicate symbols.  */
    3904            0 :       FOR_EACH_VEC_ELT (latest_undo_chgset->previous->syms, j, t)
    3905            0 :         if (t == s)
    3906              :           {
    3907            0 :             latest_undo_chgset->previous->syms.unordered_remove (j);
    3908              : 
    3909              :             /* S->OLD_SYMBOL is the backup symbol for S as it was at the
    3910              :                last checkpoint.  We drop that checkpoint, so S->OLD_SYMBOL
    3911              :                shall contain from now on the backup symbol for S as it was
    3912              :                at the checkpoint before.  */
    3913            0 :             if (s->old_symbol->gfc_new)
    3914              :               {
    3915            0 :                 gcc_assert (s->old_symbol->old_symbol == NULL);
    3916            0 :                 s->gfc_new = s->old_symbol->gfc_new;
    3917            0 :                 free_old_symbol (s);
    3918              :               }
    3919              :             else
    3920            0 :               restore_old_symbol (s->old_symbol);
    3921              :             break;
    3922              :           }
    3923              :     }
    3924              : 
    3925            0 :   latest_undo_chgset->previous->syms.safe_splice (latest_undo_chgset->syms);
    3926            0 :   latest_undo_chgset->previous->tbps.safe_splice (latest_undo_chgset->tbps);
    3927              : 
    3928            0 :   pop_undo_change_set (latest_undo_chgset);
    3929            0 : }
    3930              : 
    3931              : 
    3932              : /* Remove the reference to the symbol SYM in the symbol tree held by NS
    3933              :    and free SYM if the last reference to it has been removed.
    3934              :    Returns whether the symbol has been freed.  */
    3935              : 
    3936              : static bool
    3937      4178809 : delete_symbol_from_ns (gfc_symbol *sym, gfc_namespace *ns)
    3938              : {
    3939      4178809 :   if (ns == nullptr)
    3940              :     return false;
    3941              : 
    3942              :   /* The derived type is saved in the symtree with the first
    3943              :      letter capitalized; the all lower-case version to the
    3944              :      derived type contains its associated generic function.  */
    3945      4178771 :   const char *sym_name = gfc_fl_struct (sym->attr.flavor)
    3946           43 :                          ? gfc_dt_upper_string (sym->name)
    3947      4178771 :                          : sym->name;
    3948              : 
    3949      4178771 :   gfc_delete_symtree (&ns->sym_root, sym_name);
    3950              : 
    3951      4178771 :   return gfc_release_symbol (sym);
    3952              : }
    3953              : 
    3954              : 
    3955              : /* Undoes all the changes made to symbols since the previous checkpoint.
    3956              :    This subroutine is made simpler due to the fact that attributes are
    3957              :    never removed once added.  */
    3958              : 
    3959              : void
    3960     13191649 : gfc_restore_last_undo_checkpoint (void)
    3961              : {
    3962     13191649 :   gfc_symbol *p;
    3963     13191649 :   unsigned i;
    3964              : 
    3965     31981382 :   FOR_EACH_VEC_ELT_REVERSE (latest_undo_chgset->syms, i, p)
    3966              :     {
    3967              :       /* Symbol in a common block was new. Or was old and just put in common */
    3968      5628531 :       if (p->common_block
    3969         3735 :           && (p->gfc_new || !p->old_symbol->common_block))
    3970              :         {
    3971              :           /* If the symbol was added to any common block, it
    3972              :              needs to be removed to stop the resolver looking
    3973              :              for a (possibly) dead symbol.  */
    3974           81 :           if (p->common_block->head == p && !p->common_next)
    3975              :             {
    3976           15 :               gfc_symtree st, *st0;
    3977           15 :               st0 = find_common_symtree (p->ns->common_root,
    3978              :                                          p->common_block);
    3979           15 :               if (st0)
    3980              :                 {
    3981           12 :                   st.name = st0->name;
    3982           12 :                   gfc_delete_bbt (&p->ns->common_root, &st, compare_symtree);
    3983           12 :                   free (st0);
    3984              :                 }
    3985              :             }
    3986              : 
    3987           81 :           if (p->common_block->head == p)
    3988           15 :             p->common_block->head = p->common_next;
    3989              :           else
    3990              :             {
    3991           66 :               gfc_symbol *cparent, *csym;
    3992              : 
    3993           66 :               cparent = p->common_block->head;
    3994           66 :               csym = cparent->common_next;
    3995              : 
    3996          290 :               while (csym != p)
    3997              :                 {
    3998          224 :                   cparent = csym;
    3999          224 :                   csym = csym->common_next;
    4000              :                 }
    4001              : 
    4002           66 :               gcc_assert(cparent->common_next == p);
    4003           66 :               cparent->common_next = csym->common_next;
    4004              :             }
    4005           81 :           p->common_next = NULL;
    4006              :         }
    4007      5628531 :       if (p->gfc_new)
    4008              :         {
    4009      4178743 :           bool freed = delete_symbol_from_ns (p, p->ns);
    4010              : 
    4011              :           /* If the symbol is a procedure (function or subroutine), remove
    4012              :              it from the procedure body namespace as well as from the outer
    4013              :              namespace.  */
    4014      4178743 :           if (!freed
    4015           38 :               && p->formal_ns != p->ns)
    4016           38 :             freed = delete_symbol_from_ns (p, p->formal_ns);
    4017              : 
    4018              :           /* If the formal_ns field has not been set yet, the previous
    4019              :              conditional does nothing.  In that case, we can assume that
    4020              :              gfc_current_ns is the procedure body namespace, and remove the
    4021              :              symbol from there.  */
    4022           38 :           if (!freed
    4023           38 :               && gfc_current_ns != p->ns
    4024           28 :               && gfc_current_ns != p->formal_ns)
    4025           28 :             freed = delete_symbol_from_ns (p, gfc_current_ns);
    4026              :         }
    4027              :       else
    4028      1449788 :         restore_old_symbol (p);
    4029              :     }
    4030              : 
    4031     13191649 :   latest_undo_chgset->syms.truncate (0);
    4032     13191649 :   latest_undo_chgset->tbps.truncate (0);
    4033              : 
    4034     13191649 :   if (!single_undo_checkpoint_p ())
    4035            0 :     pop_undo_change_set (latest_undo_chgset);
    4036     13191649 : }
    4037              : 
    4038              : 
    4039              : /* Makes sure that there is only one set of changes; in other words we haven't
    4040              :    forgotten to pair a call to gfc_new_checkpoint with a call to either
    4041              :    gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint.  */
    4042              : 
    4043              : static void
    4044     21775393 : enforce_single_undo_checkpoint (void)
    4045              : {
    4046     21775393 :   gcc_checking_assert (single_undo_checkpoint_p ());
    4047     21775393 : }
    4048              : 
    4049              : 
    4050              : /* Undoes all the changes made to symbols in the current statement.  */
    4051              : 
    4052              : void
    4053     13191649 : gfc_undo_symbols (void)
    4054              : {
    4055     13191649 :   enforce_single_undo_checkpoint ();
    4056     13191649 :   gfc_restore_last_undo_checkpoint ();
    4057     13191649 : }
    4058              : 
    4059              : 
    4060              : /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
    4061              :    components of old_symbol that might need deallocation are the "allocatables"
    4062              :    that are restored in gfc_undo_symbols(), with two exceptions: namelist and
    4063              :    namelist_tail.  In case these differ between old_symbol and sym, it's just
    4064              :    because sym->namelist has gotten a few more items.  */
    4065              : 
    4066              : static void
    4067      2802154 : free_old_symbol (gfc_symbol *sym)
    4068              : {
    4069              : 
    4070      2802154 :   if (sym->old_symbol == NULL)
    4071              :     return;
    4072              : 
    4073      1691748 :   if (sym->old_symbol->as != NULL
    4074       276671 :       && sym->old_symbol->as != sym->as
    4075            2 :       && !(sym->ts.type == BT_CLASS
    4076            2 :            && sym->ts.u.derived->attr.is_class
    4077            2 :            && sym->old_symbol->as == CLASS_DATA (sym)->as))
    4078            0 :     gfc_free_array_spec (sym->old_symbol->as);
    4079              : 
    4080      1691748 :   if (sym->old_symbol->value != sym->value)
    4081         8065 :     gfc_free_expr (sym->old_symbol->value);
    4082              : 
    4083      1691748 :   if (sym->old_symbol->formal != sym->formal)
    4084        17112 :     gfc_free_formal_arglist (sym->old_symbol->formal);
    4085              : 
    4086      1691748 :   free (sym->old_symbol);
    4087      1691748 :   sym->old_symbol = NULL;
    4088              : }
    4089              : 
    4090              : 
    4091              : /* Makes the changes made in the current statement permanent-- gets
    4092              :    rid of undo information.  */
    4093              : 
    4094              : void
    4095      1576200 : gfc_commit_symbols (void)
    4096              : {
    4097      1576200 :   gfc_symbol *p;
    4098      1576200 :   gfc_typebound_proc *tbp;
    4099      1576200 :   unsigned i;
    4100              : 
    4101      1576200 :   enforce_single_undo_checkpoint ();
    4102              : 
    4103      5299833 :   FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
    4104              :     {
    4105      2147433 :       p->mark = 0;
    4106      2147433 :       p->gfc_new = 0;
    4107      2147433 :       free_old_symbol (p);
    4108              :     }
    4109      1576200 :   latest_undo_chgset->syms.truncate (0);
    4110              : 
    4111      3211561 :   FOR_EACH_VEC_ELT (latest_undo_chgset->tbps, i, tbp)
    4112        59161 :     tbp->error = 0;
    4113      1576200 :   latest_undo_chgset->tbps.truncate (0);
    4114      1576200 : }
    4115              : 
    4116              : 
    4117              : /* Makes the changes made in one symbol permanent -- gets rid of undo
    4118              :    information.  */
    4119              : 
    4120              : void
    4121       654721 : gfc_commit_symbol (gfc_symbol *sym)
    4122              : {
    4123       654721 :   gfc_symbol *p;
    4124       654721 :   unsigned i;
    4125              : 
    4126       654721 :   enforce_single_undo_checkpoint ();
    4127              : 
    4128      2331134 :   FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
    4129      1591225 :     if (p == sym)
    4130              :       {
    4131       569533 :         latest_undo_chgset->syms.unordered_remove (i);
    4132       569533 :         break;
    4133              :       }
    4134              : 
    4135       654721 :   sym->mark = 0;
    4136       654721 :   sym->gfc_new = 0;
    4137              : 
    4138       654721 :   free_old_symbol (sym);
    4139       654721 : }
    4140              : 
    4141              : 
    4142              : /* Recursively free trees containing type-bound procedures.  */
    4143              : 
    4144              : static void
    4145      1065016 : free_tb_tree (gfc_symtree *t)
    4146              : {
    4147      1065016 :   if (t == NULL)
    4148              :     return;
    4149              : 
    4150         7103 :   free_tb_tree (t->left);
    4151         7103 :   free_tb_tree (t->right);
    4152              : 
    4153              :   /* TODO: Free type-bound procedure u.generic  */
    4154         7103 :   free (t->n.tb);
    4155         7103 :   t->n.tb = NULL;
    4156         7103 :   free (t);
    4157              : }
    4158              : 
    4159              : 
    4160              : /* Recursive function that deletes an entire tree and all the common
    4161              :    head structures it points to.  */
    4162              : 
    4163              : static void
    4164       529361 : free_common_tree (gfc_symtree * common_tree)
    4165              : {
    4166       529361 :   if (common_tree == NULL)
    4167              :     return;
    4168              : 
    4169         1978 :   free_common_tree (common_tree->left);
    4170         1978 :   free_common_tree (common_tree->right);
    4171              : 
    4172         1978 :   free (common_tree);
    4173              : }
    4174              : 
    4175              : 
    4176              : /* Recursive function that deletes an entire tree and all the common
    4177              :    head structures it points to.  */
    4178              : 
    4179              : static void
    4180       526413 : free_omp_udr_tree (gfc_symtree * omp_udr_tree)
    4181              : {
    4182       526413 :   if (omp_udr_tree == NULL)
    4183              :     return;
    4184              : 
    4185          504 :   free_omp_udr_tree (omp_udr_tree->left);
    4186          504 :   free_omp_udr_tree (omp_udr_tree->right);
    4187              : 
    4188          504 :   gfc_free_omp_udr (omp_udr_tree->n.omp_udr);
    4189          504 :   free (omp_udr_tree);
    4190              : }
    4191              : 
    4192              : /* Similar, for !$omp declare mappers.  */
    4193              : 
    4194              : static void
    4195       525417 : free_omp_udm_tree (gfc_symtree *omp_udm_tree)
    4196              : {
    4197       525417 :   if (omp_udm_tree == NULL)
    4198              :     return;
    4199              : 
    4200            6 :   free_omp_udm_tree (omp_udm_tree->left);
    4201            6 :   free_omp_udm_tree (omp_udm_tree->right);
    4202              : 
    4203            6 :   gfc_free_omp_udm (omp_udm_tree->n.omp_udm);
    4204            6 :   free (omp_udm_tree);
    4205              : }
    4206              : 
    4207              : 
    4208              : /* Recursive function that deletes an entire tree and all the user
    4209              :    operator nodes that it contains.  */
    4210              : 
    4211              : static void
    4212       526165 : free_uop_tree (gfc_symtree *uop_tree)
    4213              : {
    4214       526165 :   if (uop_tree == NULL)
    4215              :     return;
    4216              : 
    4217          380 :   free_uop_tree (uop_tree->left);
    4218          380 :   free_uop_tree (uop_tree->right);
    4219              : 
    4220          380 :   gfc_free_interface (uop_tree->n.uop->op);
    4221          380 :   free (uop_tree->n.uop);
    4222          380 :   free (uop_tree);
    4223              : }
    4224              : 
    4225              : 
    4226              : /* Recursive function that deletes an entire tree and all the symbols
    4227              :    that it contains.  */
    4228              : 
    4229              : static void
    4230      4708217 : free_sym_tree (gfc_symtree *sym_tree)
    4231              : {
    4232      4708217 :   if (sym_tree == NULL)
    4233              :     return;
    4234              : 
    4235      2091406 :   free_sym_tree (sym_tree->left);
    4236      2091406 :   free_sym_tree (sym_tree->right);
    4237              : 
    4238      2091406 :   gfc_release_symbol (sym_tree->n.sym);
    4239      2091406 :   free (sym_tree);
    4240              : }
    4241              : 
    4242              : 
    4243              : /* Free the gfc_equiv_info's.  */
    4244              : 
    4245              : static void
    4246        14669 : gfc_free_equiv_infos (gfc_equiv_info *s)
    4247              : {
    4248        14669 :   if (s == NULL)
    4249              :     return;
    4250         8115 :   gfc_free_equiv_infos (s->next);
    4251         8115 :   free (s);
    4252              : }
    4253              : 
    4254              : 
    4255              : /* Free the gfc_equiv_lists.  */
    4256              : 
    4257              : static void
    4258       531959 : gfc_free_equiv_lists (gfc_equiv_list *l)
    4259              : {
    4260       531959 :   if (l == NULL)
    4261              :     return;
    4262         6554 :   gfc_free_equiv_lists (l->next);
    4263         6554 :   gfc_free_equiv_infos (l->equiv);
    4264         6554 :   free (l);
    4265              : }
    4266              : 
    4267              : 
    4268              : /* Free a finalizer procedure list.  */
    4269              : 
    4270              : void
    4271         1082 : gfc_free_finalizer (gfc_finalizer* el)
    4272              : {
    4273         1082 :   if (el)
    4274              :     {
    4275         1082 :       gfc_release_symbol (el->proc_sym);
    4276         1082 :       free (el);
    4277              :     }
    4278         1082 : }
    4279              : 
    4280              : static void
    4281       525405 : gfc_free_finalizer_list (gfc_finalizer* list)
    4282              : {
    4283       526473 :   while (list)
    4284              :     {
    4285         1068 :       gfc_finalizer* current = list;
    4286         1068 :       list = list->next;
    4287         1068 :       gfc_free_finalizer (current);
    4288              :     }
    4289       525405 : }
    4290              : 
    4291              : 
    4292              : /* Create a new gfc_charlen structure and add it to a namespace.
    4293              :    If 'old_cl' is given, the newly created charlen will be a copy of it.  */
    4294              : 
    4295              : gfc_charlen*
    4296       301142 : gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
    4297              : {
    4298       301142 :   gfc_charlen *cl;
    4299              : 
    4300       301142 :   cl = gfc_get_charlen ();
    4301              : 
    4302              :   /* Copy old_cl.  */
    4303       301142 :   if (old_cl)
    4304              :     {
    4305        15078 :       cl->length = gfc_copy_expr (old_cl->length);
    4306        15078 :       cl->length_from_typespec = old_cl->length_from_typespec;
    4307        15078 :       cl->backend_decl = old_cl->backend_decl;
    4308        15078 :       cl->passed_length = old_cl->passed_length;
    4309        15078 :       cl->resolved = old_cl->resolved;
    4310              :     }
    4311              : 
    4312              :   /* Put into namespace.  */
    4313       301142 :   cl->next = ns->cl_list;
    4314       301142 :   ns->cl_list = cl;
    4315              : 
    4316       301142 :   return cl;
    4317              : }
    4318              : 
    4319              : 
    4320              : /* Free the charlen list from cl to end (end is not freed).
    4321              :    Free the whole list if end is NULL.  */
    4322              : 
    4323              : static void
    4324       525405 : gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
    4325              : {
    4326       525405 :   gfc_charlen *cl2;
    4327              : 
    4328       826119 :   for (; cl != end; cl = cl2)
    4329              :     {
    4330       300714 :       gcc_assert (cl);
    4331              : 
    4332       300714 :       cl2 = cl->next;
    4333       300714 :       gfc_free_expr (cl->length);
    4334       300714 :       free (cl);
    4335              :     }
    4336       525405 : }
    4337              : 
    4338              : 
    4339              : /* Free entry list structs.  */
    4340              : 
    4341              : static void
    4342            0 : free_entry_list (gfc_entry_list *el)
    4343              : {
    4344       526896 :   gfc_entry_list *next;
    4345              : 
    4346       526896 :   if (el == NULL)
    4347            0 :     return;
    4348              : 
    4349         1491 :   next = el->next;
    4350         1491 :   free (el);
    4351         1491 :   free_entry_list (next);
    4352              : }
    4353              : 
    4354              : 
    4355              : /* Free a namespace structure and everything below it.  Interface
    4356              :    lists associated with intrinsic operators are not freed.  These are
    4357              :    taken care of when a specific name is freed.  */
    4358              : 
    4359              : void
    4360     12636358 : gfc_free_namespace (gfc_namespace *&ns)
    4361              : {
    4362     12636358 :   gfc_namespace *p, *q;
    4363     12636358 :   int i;
    4364     12636358 :   gfc_was_finalized *f;
    4365              : 
    4366     12636358 :   if (ns == NULL)
    4367     12110953 :     return;
    4368              : 
    4369       552108 :   ns->refs--;
    4370       552108 :   if (ns->refs > 0)
    4371              :     return;
    4372              : 
    4373       525405 :   gcc_assert (ns->refs == 0);
    4374              : 
    4375       525405 :   gfc_free_statements (ns->code);
    4376              : 
    4377       525405 :   free_sym_tree (ns->sym_root);
    4378       525405 :   free_uop_tree (ns->uop_root);
    4379       525405 :   free_common_tree (ns->common_root);
    4380       525405 :   free_omp_udr_tree (ns->omp_udr_root);
    4381       525405 :   free_omp_udm_tree (ns->omp_udm_root);
    4382       525405 :   free_tb_tree (ns->tb_sym_root);
    4383       525405 :   free_tb_tree (ns->tb_uop_root);
    4384       525405 :   gfc_free_finalizer_list (ns->finalizers);
    4385       525405 :   gfc_free_omp_declare_simd_list (ns->omp_declare_simd);
    4386       525405 :   gfc_free_omp_declare_variant_list (ns->omp_declare_variant);
    4387       525405 :   gfc_free_charlen (ns->cl_list, NULL);
    4388       525405 :   free_st_labels (ns->st_labels);
    4389              : 
    4390       525405 :   free_entry_list (ns->entries);
    4391       525405 :   gfc_free_equiv (ns->equiv);
    4392       525405 :   gfc_free_equiv_lists (ns->equiv_lists);
    4393       525405 :   gfc_free_use_stmts (ns->use_stmts);
    4394              : 
    4395     15762150 :   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
    4396     14711340 :     gfc_free_interface (ns->op[i]);
    4397              : 
    4398       525405 :   gfc_free_data (ns->data);
    4399              : 
    4400              :   /* Free all the expr + component combinations that have been
    4401              :      finalized.  */
    4402       525405 :   f = ns->was_finalized;
    4403       528217 :   while (f)
    4404              :     {
    4405         2812 :       gfc_was_finalized* current = f;
    4406         2812 :       f = f->next;
    4407         2812 :       free (current);
    4408              :     }
    4409       525405 :   if (ns->omp_assumes)
    4410              :     {
    4411           19 :       free (ns->omp_assumes->absent);
    4412           19 :       free (ns->omp_assumes->contains);
    4413           19 :       gfc_free_expr_list (ns->omp_assumes->holds);
    4414           19 :       free (ns->omp_assumes);
    4415              :     }
    4416       525405 :   p = ns->contained;
    4417       525405 :   free (ns);
    4418       525405 :   ns = NULL;
    4419              : 
    4420              :   /* Recursively free any contained namespaces.  */
    4421       576524 :   while (p != NULL)
    4422              :     {
    4423        51119 :       q = p;
    4424        51119 :       p = p->sibling;
    4425        51119 :       gfc_free_namespace (q);
    4426              :     }
    4427              : }
    4428              : 
    4429              : 
    4430              : void
    4431        80784 : gfc_symbol_init_2 (void)
    4432              : {
    4433              : 
    4434        80784 :   gfc_current_ns = gfc_get_namespace (NULL, 0);
    4435        80784 : }
    4436              : 
    4437              : 
    4438              : void
    4439        81126 : gfc_symbol_done_2 (void)
    4440              : {
    4441        81126 :   if (gfc_current_ns != NULL)
    4442              :     {
    4443              :       /* free everything from the root.  */
    4444        81142 :       while (gfc_current_ns->parent != NULL)
    4445           16 :         gfc_current_ns = gfc_current_ns->parent;
    4446        81126 :       gfc_free_namespace (gfc_current_ns);
    4447        81126 :       gfc_current_ns = NULL;
    4448              :     }
    4449        81126 :   gfc_derived_types = NULL;
    4450              : 
    4451        81126 :   enforce_single_undo_checkpoint ();
    4452        81126 :   free_undo_change_set_data (*latest_undo_chgset);
    4453        81126 : }
    4454              : 
    4455              : 
    4456              : /* Count how many nodes a symtree has.  */
    4457              : 
    4458              : static unsigned
    4459     26412735 : count_st_nodes (const gfc_symtree *st)
    4460              : {
    4461     49356760 :   unsigned nodes;
    4462     49356760 :   if (!st)
    4463     26412735 :     return 0;
    4464              : 
    4465     22944025 :   nodes = count_st_nodes (st->left);
    4466     22944025 :   nodes++;
    4467     22944025 :   nodes += count_st_nodes (st->right);
    4468              : 
    4469     22944025 :   return nodes;
    4470              : }
    4471              : 
    4472              : 
    4473              : /* Convert symtree tree into symtree vector.  */
    4474              : 
    4475              : static unsigned
    4476     26412735 : fill_st_vector (gfc_symtree *st, gfc_symtree **st_vec, unsigned node_cntr)
    4477              : {
    4478     49356760 :   if (!st)
    4479     26412735 :     return node_cntr;
    4480              : 
    4481     22944025 :   node_cntr = fill_st_vector (st->left, st_vec, node_cntr);
    4482     22944025 :   st_vec[node_cntr++] = st;
    4483     22944025 :   node_cntr = fill_st_vector (st->right, st_vec, node_cntr);
    4484              : 
    4485     22944025 :   return node_cntr;
    4486              : }
    4487              : 
    4488              : 
    4489              : /* Traverse namespace.  As the functions might modify the symtree, we store the
    4490              :    symtree as a vector and operate on this vector.  Note: We assume that
    4491              :    sym_func or st_func never deletes nodes from the symtree - only adding is
    4492              :    allowed. Additionally, newly added nodes are not traversed.  */
    4493              : 
    4494              : static void
    4495      3468710 : do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *),
    4496              :                      void (*sym_func) (gfc_symbol *))
    4497              : {
    4498      3468710 :   gfc_symtree **st_vec;
    4499      3468710 :   unsigned nodes, i, node_cntr;
    4500              : 
    4501      3468710 :   gcc_assert ((st_func && !sym_func) || (!st_func && sym_func));
    4502      3468710 :   nodes = count_st_nodes (st);
    4503      3468710 :   st_vec = XALLOCAVEC (gfc_symtree *, nodes);
    4504      3468710 :   node_cntr = 0;
    4505      3468710 :   fill_st_vector (st, st_vec, node_cntr);
    4506              : 
    4507      3468710 :   if (sym_func)
    4508              :     {
    4509              :       /* Clear marks.  */
    4510     26105128 :       for (i = 0; i < nodes; i++)
    4511     22772493 :         st_vec[i]->n.sym->mark = 0;
    4512     26105128 :       for (i = 0; i < nodes; i++)
    4513     22772493 :         if (!st_vec[i]->n.sym->mark)
    4514              :           {
    4515     22208980 :             (*sym_func) (st_vec[i]->n.sym);
    4516     22208980 :             st_vec[i]->n.sym->mark = 1;
    4517              :           }
    4518              :      }
    4519              :    else
    4520       307607 :       for (i = 0; i < nodes; i++)
    4521       171532 :         (*st_func) (st_vec[i]);
    4522      3468710 : }
    4523              : 
    4524              : 
    4525              : /* Recursively traverse the symtree nodes.  */
    4526              : 
    4527              : void
    4528       136075 : gfc_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *))
    4529              : {
    4530       136075 :   do_traverse_symtree (st, st_func, NULL);
    4531       136075 : }
    4532              : 
    4533              : 
    4534              : /* Call a given function for all symbols in the namespace.  We take
    4535              :    care that each gfc_symbol node is called exactly once.  */
    4536              : 
    4537              : void
    4538      3332634 : gfc_traverse_ns (gfc_namespace *ns, void (*sym_func) (gfc_symbol *))
    4539              : {
    4540      3332634 :   do_traverse_symtree (ns->sym_root, NULL, sym_func);
    4541      3332634 : }
    4542              : 
    4543              : 
    4544              : /* Return TRUE when name is the name of an intrinsic type.  */
    4545              : 
    4546              : bool
    4547        13674 : gfc_is_intrinsic_typename (const char *name)
    4548              : {
    4549        13674 :   if (strcmp (name, "integer") == 0
    4550        13671 :       || strcmp (name, "real") == 0
    4551        13668 :       || strcmp (name, "character") == 0
    4552        13666 :       || strcmp (name, "logical") == 0
    4553        13664 :       || strcmp (name, "complex") == 0
    4554        13660 :       || strcmp (name, "doubleprecision") == 0
    4555        13657 :       || strcmp (name, "doublecomplex") == 0)
    4556              :     return true;
    4557              :   else
    4558        13654 :     return false;
    4559              : }
    4560              : 
    4561              : 
    4562              : /* Return TRUE if the symbol is an automatic variable.  */
    4563              : 
    4564              : static bool
    4565          845 : gfc_is_var_automatic (gfc_symbol *sym)
    4566              : {
    4567              :   /* Pointer and allocatable variables are never automatic.  */
    4568          845 :   if (sym->attr.pointer || sym->attr.allocatable)
    4569              :     return false;
    4570              :   /* Check for arrays with non-constant size.  */
    4571           74 :   if (sym->attr.dimension && sym->as
    4572          837 :       && !gfc_is_compile_time_shape (sym->as))
    4573              :     return true;
    4574              :   /* Check for non-constant length character variables.  */
    4575          753 :   if (sym->ts.type == BT_CHARACTER
    4576           63 :       && sym->ts.u.cl
    4577          816 :       && !gfc_is_constant_expr (sym->ts.u.cl->length))
    4578              :     return true;
    4579              :   /* Variables with explicit AUTOMATIC attribute.  */
    4580          745 :   if (sym->attr.automatic)
    4581              :       return true;
    4582              : 
    4583              :   return false;
    4584              : }
    4585              : 
    4586              : /* Given a symbol, mark it as SAVEd if it is allowed.  */
    4587              : 
    4588              : static void
    4589         3057 : save_symbol (gfc_symbol *sym)
    4590              : {
    4591              : 
    4592         3057 :   if (sym->attr.use_assoc)
    4593              :     return;
    4594              : 
    4595         2336 :   if (sym->attr.in_common
    4596         2320 :       || sym->attr.in_equivalence
    4597         2162 :       || sym->attr.dummy
    4598         1923 :       || sym->attr.result
    4599         1912 :       || sym->attr.flavor != FL_VARIABLE)
    4600              :     return;
    4601              :   /* Automatic objects are not saved.  */
    4602          845 :   if (gfc_is_var_automatic (sym))
    4603              :     return;
    4604          814 :   gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, &sym->declared_at);
    4605              : }
    4606              : 
    4607              : 
    4608              : /* Mark those symbols which can be SAVEd as such.  */
    4609              : 
    4610              : void
    4611          315 : gfc_save_all (gfc_namespace *ns)
    4612              : {
    4613          315 :   gfc_traverse_ns (ns, save_symbol);
    4614          315 : }
    4615              : 
    4616              : 
    4617              : /* Make sure that no changes to symbols are pending.  */
    4618              : 
    4619              : void
    4620      6271697 : gfc_enforce_clean_symbol_state(void)
    4621              : {
    4622      6271697 :   enforce_single_undo_checkpoint ();
    4623      6271697 :   gcc_assert (latest_undo_chgset->syms.is_empty ());
    4624      6271697 : }
    4625              : 
    4626              : 
    4627              : /************** Global symbol handling ************/
    4628              : 
    4629              : 
    4630              : /* Search a tree for the global symbol.  */
    4631              : 
    4632              : gfc_gsymbol *
    4633       425472 : gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
    4634              : {
    4635       425472 :   int c;
    4636              : 
    4637       425472 :   if (symbol == NULL)
    4638              :     return NULL;
    4639              : 
    4640      1468757 :   while (symbol)
    4641              :     {
    4642      1224856 :       c = strcmp (name, symbol->name);
    4643      1224856 :       if (!c)
    4644              :         return symbol;
    4645              : 
    4646      1085030 :       symbol = (c < 0) ? symbol->left : symbol->right;
    4647              :     }
    4648              : 
    4649              :   return NULL;
    4650              : }
    4651              : 
    4652              : 
    4653              : /* Case insensitive search a tree for the global symbol.  */
    4654              : 
    4655              : gfc_gsymbol *
    4656         4650 : gfc_find_case_gsymbol (gfc_gsymbol *symbol, const char *name)
    4657              : {
    4658         4650 :   int c;
    4659              : 
    4660         4650 :   if (symbol == NULL)
    4661              :     return NULL;
    4662              : 
    4663        16129 :   while (symbol)
    4664              :     {
    4665        14411 :       c = strcasecmp (name, symbol->name);
    4666        14411 :       if (!c)
    4667              :         return symbol;
    4668              : 
    4669        11519 :       symbol = (c < 0) ? symbol->left : symbol->right;
    4670              :     }
    4671              : 
    4672              :   return NULL;
    4673              : }
    4674              : 
    4675              : 
    4676              : /* Compare two global symbols. Used for managing the BB tree.  */
    4677              : 
    4678              : static int
    4679       167206 : gsym_compare (void *_s1, void *_s2)
    4680              : {
    4681       167206 :   gfc_gsymbol *s1, *s2;
    4682              : 
    4683       167206 :   s1 = (gfc_gsymbol *) _s1;
    4684       167206 :   s2 = (gfc_gsymbol *) _s2;
    4685       167206 :   return strcmp (s1->name, s2->name);
    4686              : }
    4687              : 
    4688              : 
    4689              : /* Get a global symbol, creating it if it doesn't exist.  */
    4690              : 
    4691              : gfc_gsymbol *
    4692       114179 : gfc_get_gsymbol (const char *name, bool bind_c)
    4693              : {
    4694       114179 :   gfc_gsymbol *s;
    4695              : 
    4696       114179 :   s = gfc_find_gsymbol (gfc_gsym_root, name);
    4697       114179 :   if (s != NULL)
    4698              :     return s;
    4699              : 
    4700        88608 :   s = XCNEW (gfc_gsymbol);
    4701        88608 :   s->type = GSYM_UNKNOWN;
    4702        88608 :   s->name = gfc_get_string ("%s", name);
    4703        88608 :   s->bind_c = bind_c;
    4704              : 
    4705        88608 :   gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
    4706              : 
    4707        88608 :   return s;
    4708              : }
    4709              : 
    4710              : void
    4711            0 : gfc_traverse_gsymbol (gfc_gsymbol *gsym,
    4712              :                       void (*do_something) (gfc_gsymbol *, void *),
    4713              :                       void *data)
    4714              : {
    4715            0 :   if (gsym->left)
    4716            0 :     gfc_traverse_gsymbol (gsym->left, do_something, data);
    4717              : 
    4718            0 :   (*do_something) (gsym, data);
    4719              : 
    4720            0 :   if (gsym->right)
    4721              :     gfc_traverse_gsymbol (gsym->right, do_something, data);
    4722            0 : }
    4723              : 
    4724              : static gfc_symbol *
    4725           52 : get_iso_c_binding_dt (int sym_id)
    4726              : {
    4727           52 :   gfc_symbol *dt_list = gfc_derived_types;
    4728              : 
    4729              :   /* Loop through the derived types in the name list, searching for
    4730              :      the desired symbol from iso_c_binding.  Search the parent namespaces
    4731              :      if necessary and requested to (parent_flag).  */
    4732           52 :   if (dt_list)
    4733              :     {
    4734           25 :       while (dt_list->dt_next != gfc_derived_types)
    4735              :         {
    4736            0 :           if (dt_list->from_intmod != INTMOD_NONE
    4737            0 :               && dt_list->intmod_sym_id == sym_id)
    4738              :             return dt_list;
    4739              : 
    4740              :           dt_list = dt_list->dt_next;
    4741              :         }
    4742              :     }
    4743              : 
    4744              :   return NULL;
    4745              : }
    4746              : 
    4747              : 
    4748              : /* Verifies that the given derived type symbol, derived_sym, is interoperable
    4749              :    with C.  This is necessary for any derived type that is BIND(C) and for
    4750              :    derived types that are parameters to functions that are BIND(C).  All
    4751              :    fields of the derived type are required to be interoperable, and are tested
    4752              :    for such.  If an error occurs, the errors are reported here, allowing for
    4753              :    multiple errors to be handled for a single derived type.  */
    4754              : 
    4755              : bool
    4756        27168 : verify_bind_c_derived_type (gfc_symbol *derived_sym)
    4757              : {
    4758        27168 :   gfc_component *curr_comp = NULL;
    4759        27168 :   bool is_c_interop = false;
    4760        27168 :   bool retval = true;
    4761              : 
    4762        27168 :   if (derived_sym == NULL)
    4763            0 :     gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
    4764              :                         "unexpectedly NULL");
    4765              : 
    4766              :   /* If we've already looked at this derived symbol, do not look at it again
    4767              :      so we don't repeat warnings/errors.  */
    4768        27168 :   if (derived_sym->ts.is_c_interop)
    4769              :     return true;
    4770              : 
    4771              :   /* The derived type must have the BIND attribute to be interoperable
    4772              :      J3/04-007, Section 15.2.3.  */
    4773          406 :   if (derived_sym->attr.is_bind_c != 1)
    4774              :     {
    4775            2 :       derived_sym->ts.is_c_interop = 0;
    4776            2 :       gfc_error_now ("Derived type %qs declared at %L must have the BIND "
    4777              :                      "attribute to be C interoperable", derived_sym->name,
    4778              :                      &(derived_sym->declared_at));
    4779            2 :       retval = false;
    4780              :     }
    4781              : 
    4782          406 :   curr_comp = derived_sym->components;
    4783              : 
    4784              :   /* Fortran 2003 allows an empty derived type.  C99 appears to disallow an
    4785              :      empty struct.  Section 15.2 in Fortran 2003 states:  "The following
    4786              :      subclauses define the conditions under which a Fortran entity is
    4787              :      interoperable.  If a Fortran entity is interoperable, an equivalent
    4788              :      entity may be defined by means of C and the Fortran entity is said
    4789              :      to be interoperable with the C entity.  There does not have to be such
    4790              :      an interoperating C entity."
    4791              : 
    4792              :      However, later discussion on the J3 mailing list
    4793              :      (https://mailman.j3-fortran.org/pipermail/j3/2021-July/013190.html)
    4794              :      found this to be a defect, and Fortran 2018 added in section 18.3.4
    4795              :      the following constraint:
    4796              :      "C1805: A derived type with the BIND attribute shall have at least one
    4797              :      component."
    4798              : 
    4799              :      We thus allow empty derived types only as GNU extension while giving a
    4800              :      warning by default, or reject empty types in standard conformance mode.
    4801              :   */
    4802          406 :   if (curr_comp == NULL)
    4803              :     {
    4804            2 :       if (!gfc_notify_std (GFC_STD_GNU, "Derived type %qs with BIND(C) "
    4805              :                            "attribute at %L has no components",
    4806              :                            derived_sym->name, &(derived_sym->declared_at)))
    4807              :         return false;
    4808            1 :       else if (!pedantic)
    4809              :         /* Generally emit warning, but not twice if -pedantic is given.  */
    4810            1 :         gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L "
    4811              :                      "is empty, and may be inaccessible by the C "
    4812              :                      "companion processor",
    4813              :                      derived_sym->name, &(derived_sym->declared_at));
    4814            1 :       derived_sym->ts.is_c_interop = 1;
    4815            1 :       derived_sym->attr.is_bind_c = 1;
    4816            1 :       return true;
    4817              :     }
    4818              : 
    4819              : 
    4820              :   /* Initialize the derived type as being C interoperable.
    4821              :      If we find an error in the components, this will be set false.  */
    4822          404 :   derived_sym->ts.is_c_interop = 1;
    4823              : 
    4824              :   /* Loop through the list of components to verify that the kind of
    4825              :      each is a C interoperable type.  */
    4826          853 :   do
    4827              :     {
    4828              :       /* The components cannot be pointers (fortran sense).
    4829              :          J3/04-007, Section 15.2.3, C1505.      */
    4830          853 :       if (curr_comp->attr.pointer != 0)
    4831              :         {
    4832            3 :           gfc_error ("Component %qs at %L cannot have the "
    4833              :                      "POINTER attribute because it is a member "
    4834              :                      "of the BIND(C) derived type %qs at %L",
    4835              :                      curr_comp->name, &(curr_comp->loc),
    4836              :                      derived_sym->name, &(derived_sym->declared_at));
    4837            3 :           retval = false;
    4838              :         }
    4839              : 
    4840          853 :       if (curr_comp->attr.proc_pointer != 0)
    4841              :         {
    4842            1 :           gfc_error ("Procedure pointer component %qs at %L cannot be a member"
    4843              :                      " of the BIND(C) derived type %qs at %L", curr_comp->name,
    4844              :                      &curr_comp->loc, derived_sym->name,
    4845              :                      &derived_sym->declared_at);
    4846            1 :           retval = false;
    4847              :         }
    4848              : 
    4849              :       /* The components cannot be allocatable.
    4850              :          J3/04-007, Section 15.2.3, C1505.      */
    4851          853 :       if (curr_comp->attr.allocatable != 0)
    4852              :         {
    4853            3 :           gfc_error ("Component %qs at %L cannot have the "
    4854              :                      "ALLOCATABLE attribute because it is a member "
    4855              :                      "of the BIND(C) derived type %qs at %L",
    4856              :                      curr_comp->name, &(curr_comp->loc),
    4857              :                      derived_sym->name, &(derived_sym->declared_at));
    4858            3 :           retval = false;
    4859              :         }
    4860              : 
    4861              :       /* BIND(C) derived types must have interoperable components.  */
    4862          853 :       if (curr_comp->ts.type == BT_DERIVED
    4863           71 :           && curr_comp->ts.u.derived->ts.is_iso_c != 1
    4864           17 :           && curr_comp->ts.u.derived != derived_sym)
    4865              :         {
    4866              :           /* This should be allowed; the draft says a derived-type cannot
    4867              :              have type parameters if it is has the BIND attribute.  Type
    4868              :              parameters seem to be for making parameterized derived types.
    4869              :              There's no need to verify the type if it is c_ptr/c_funptr.  */
    4870           16 :           retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
    4871              :         }
    4872              :       else
    4873              :         {
    4874              :           /* Grab the typespec for the given component and test the kind.  */
    4875          837 :           is_c_interop = gfc_verify_c_interop (&(curr_comp->ts));
    4876              : 
    4877          837 :           if (!is_c_interop)
    4878              :             {
    4879              :               /* Report warning and continue since not fatal.  The
    4880              :                  draft does specify a constraint that requires all fields
    4881              :                  to interoperate, but if the user says real(4), etc., it
    4882              :                  may interoperate with *something* in C, but the compiler
    4883              :                  most likely won't know exactly what.  Further, it may not
    4884              :                  interoperate with the same data type(s) in C if the user
    4885              :                  recompiles with different flags (e.g., -m32 and -m64 on
    4886              :                  x86_64 and using integer(4) to claim interop with a
    4887              :                  C_LONG).  */
    4888           34 :               if (derived_sym->attr.is_bind_c == 1 && warn_c_binding_type)
    4889              :                 /* If the derived type is bind(c), all fields must be
    4890              :                    interop.  */
    4891            1 :                 gfc_warning (OPT_Wc_binding_type,
    4892              :                              "Component %qs in derived type %qs at %L "
    4893              :                              "may not be C interoperable, even though "
    4894              :                              "derived type %qs is BIND(C)",
    4895              :                              curr_comp->name, derived_sym->name,
    4896              :                              &(curr_comp->loc), derived_sym->name);
    4897           33 :               else if (warn_c_binding_type)
    4898              :                 /* If derived type is param to bind(c) routine, or to one
    4899              :                    of the iso_c_binding procs, it must be interoperable, so
    4900              :                    all fields must interop too.  */
    4901            0 :                 gfc_warning (OPT_Wc_binding_type,
    4902              :                              "Component %qs in derived type %qs at %L "
    4903              :                              "may not be C interoperable",
    4904              :                              curr_comp->name, derived_sym->name,
    4905              :                              &(curr_comp->loc));
    4906              :             }
    4907              :         }
    4908              : 
    4909          853 :       curr_comp = curr_comp->next;
    4910          853 :     } while (curr_comp != NULL);
    4911              : 
    4912          404 :   if (derived_sym->attr.sequence != 0)
    4913              :     {
    4914            0 :       gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
    4915              :                  "attribute because it is BIND(C)", derived_sym->name,
    4916              :                  &(derived_sym->declared_at));
    4917            0 :       retval = false;
    4918              :     }
    4919              : 
    4920              :   /* Mark the derived type as not being C interoperable if we found an
    4921              :      error.  If there were only warnings, proceed with the assumption
    4922              :      it's interoperable.  */
    4923          404 :   if (!retval)
    4924            8 :     derived_sym->ts.is_c_interop = 0;
    4925              : 
    4926              :   return retval;
    4927              : }
    4928              : 
    4929              : 
    4930              : /* Generate symbols for the named constants c_null_ptr and c_null_funptr.  */
    4931              : 
    4932              : static bool
    4933         6454 : gen_special_c_interop_ptr (gfc_symbol *tmp_sym, gfc_symtree *dt_symtree)
    4934              : {
    4935         6454 :   gfc_constructor *c;
    4936              : 
    4937         6454 :   gcc_assert (tmp_sym && dt_symtree && dt_symtree->n.sym);
    4938         6454 :   dt_symtree->n.sym->attr.referenced = 1;
    4939              : 
    4940         6454 :   tmp_sym->attr.is_c_interop = 1;
    4941         6454 :   tmp_sym->attr.is_bind_c = 1;
    4942         6454 :   tmp_sym->ts.is_c_interop = 1;
    4943         6454 :   tmp_sym->ts.is_iso_c = 1;
    4944         6454 :   tmp_sym->ts.type = BT_DERIVED;
    4945         6454 :   tmp_sym->ts.f90_type = BT_VOID;
    4946         6454 :   tmp_sym->attr.flavor = FL_PARAMETER;
    4947         6454 :   tmp_sym->ts.u.derived = dt_symtree->n.sym;
    4948              : 
    4949              :   /* Set the c_address field of c_null_ptr and c_null_funptr to
    4950              :      the value of NULL.  */
    4951         6454 :   tmp_sym->value = gfc_get_expr ();
    4952         6454 :   tmp_sym->value->expr_type = EXPR_STRUCTURE;
    4953         6454 :   tmp_sym->value->ts.type = BT_DERIVED;
    4954         6454 :   tmp_sym->value->ts.f90_type = BT_VOID;
    4955         6454 :   tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
    4956         6454 :   gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
    4957         6454 :   c = gfc_constructor_first (tmp_sym->value->value.constructor);
    4958         6454 :   c->expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
    4959         6454 :   c->expr->ts.is_iso_c = 1;
    4960              : 
    4961         6454 :   return true;
    4962              : }
    4963              : 
    4964              : 
    4965              : /* Add a formal argument, gfc_formal_arglist, to the
    4966              :    end of the given list of arguments.  Set the reference to the
    4967              :    provided symbol, param_sym, in the argument.  */
    4968              : 
    4969              : static void
    4970       106300 : add_formal_arg (gfc_formal_arglist **head,
    4971              :                 gfc_formal_arglist **tail,
    4972              :                 gfc_formal_arglist *formal_arg,
    4973              :                 gfc_symbol *param_sym)
    4974              : {
    4975              :   /* Put in list, either as first arg or at the tail (curr arg).  */
    4976            0 :   if (*head == NULL)
    4977            0 :     *head = *tail = formal_arg;
    4978              :   else
    4979              :     {
    4980        65340 :       (*tail)->next = formal_arg;
    4981        65340 :       (*tail) = formal_arg;
    4982              :     }
    4983              : 
    4984       106300 :   (*tail)->sym = param_sym;
    4985       106300 :   (*tail)->next = NULL;
    4986              : 
    4987       106300 :   return;
    4988              : }
    4989              : 
    4990              : 
    4991              : /* Add a procedure interface to the given symbol (i.e., store a
    4992              :    reference to the list of formal arguments).  */
    4993              : 
    4994              : static void
    4995        41696 : add_proc_interface (gfc_symbol *sym, ifsrc source, gfc_formal_arglist *formal)
    4996              : {
    4997              : 
    4998        41696 :   sym->formal = formal;
    4999        41696 :   sym->attr.if_source = source;
    5000            0 : }
    5001              : 
    5002              : 
    5003              : /* Copy the formal args from an existing symbol, src, into a new
    5004              :    symbol, dest.  New formal args are created, and the description of
    5005              :    each arg is set according to the existing ones.  This function is
    5006              :    used when creating procedure declaration variables from a procedure
    5007              :    declaration statement (see match_proc_decl()) to create the formal
    5008              :    args based on the args of a given named interface.
    5009              : 
    5010              :    When an actual argument list is provided, skip the absent arguments
    5011              :    unless copy_type is true.
    5012              :    To be used together with gfc_se->ignore_optional.  */
    5013              : 
    5014              : void
    5015        41696 : gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
    5016              :                            gfc_actual_arglist *actual, bool copy_type)
    5017              : {
    5018        41696 :   gfc_formal_arglist *head = NULL;
    5019        41696 :   gfc_formal_arglist *tail = NULL;
    5020        41696 :   gfc_formal_arglist *formal_arg = NULL;
    5021        41696 :   gfc_intrinsic_arg *curr_arg = NULL;
    5022        41696 :   gfc_formal_arglist *formal_prev = NULL;
    5023        41696 :   gfc_actual_arglist *act_arg = actual;
    5024              :   /* Save current namespace so we can change it for formal args.  */
    5025        41696 :   gfc_namespace *parent_ns = gfc_current_ns;
    5026              : 
    5027              :   /* Create a new namespace, which will be the formal ns (namespace
    5028              :      of the formal args).  */
    5029        41696 :   gfc_current_ns = gfc_get_namespace (parent_ns, 0);
    5030        41696 :   gfc_current_ns->proc_name = dest;
    5031              : 
    5032       150870 :   for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
    5033              :     {
    5034              :       /* Skip absent arguments.  */
    5035       109174 :       if (actual)
    5036              :         {
    5037        14860 :           gcc_assert (act_arg != NULL);
    5038        14860 :           if (act_arg->expr == NULL)
    5039              :             {
    5040         2874 :               act_arg = act_arg->next;
    5041         2874 :               continue;
    5042              :             }
    5043              :         }
    5044       106300 :       formal_arg = gfc_get_formal_arglist ();
    5045       106300 :       gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
    5046              : 
    5047              :       /* May need to copy more info for the symbol.  */
    5048       106300 :       if (copy_type && act_arg->expr != NULL)
    5049              :         {
    5050         5720 :           formal_arg->sym->ts = act_arg->expr->ts;
    5051         5720 :           if (act_arg->expr->rank > 0)
    5052              :             {
    5053         2575 :               formal_arg->sym->attr.dimension = 1;
    5054         2575 :               formal_arg->sym->as = gfc_get_array_spec();
    5055         2575 :               formal_arg->sym->as->rank = -1;
    5056         2575 :               formal_arg->sym->as->type = AS_ASSUMED_RANK;
    5057              :             }
    5058         5720 :           if (act_arg->name && strcmp (act_arg->name, "%VAL") == 0)
    5059         1300 :             formal_arg->sym->pass_as_value = 1;
    5060              :         }
    5061              :       else
    5062       100580 :         formal_arg->sym->ts = curr_arg->ts;
    5063              : 
    5064       106300 :       formal_arg->sym->attr.optional = curr_arg->optional;
    5065       106300 :       formal_arg->sym->attr.value = curr_arg->value;
    5066       106300 :       formal_arg->sym->attr.intent = curr_arg->intent;
    5067       106300 :       formal_arg->sym->attr.flavor = FL_VARIABLE;
    5068       106300 :       formal_arg->sym->attr.dummy = 1;
    5069              : 
    5070              :       /* Do not treat an actual deferred-length character argument wrongly
    5071              :          as template for the formal argument.  */
    5072       106300 :       if (formal_arg->sym->ts.type == BT_CHARACTER
    5073         8143 :           && !(formal_arg->sym->attr.allocatable
    5074         8143 :                || formal_arg->sym->attr.pointer))
    5075         8143 :         formal_arg->sym->ts.deferred = false;
    5076              : 
    5077       106300 :       if (formal_arg->sym->ts.type == BT_CHARACTER)
    5078         8143 :         formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5079              : 
    5080              :       /* If this isn't the first arg, set up the next ptr.  For the
    5081              :         last arg built, the formal_arg->next will never get set to
    5082              :         anything other than NULL.  */
    5083       106300 :       if (formal_prev != NULL)
    5084        65340 :         formal_prev->next = formal_arg;
    5085              :       else
    5086              :         formal_arg->next = NULL;
    5087              : 
    5088       106300 :       formal_prev = formal_arg;
    5089              : 
    5090              :       /* Add arg to list of formal args.  */
    5091       106300 :       add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
    5092              : 
    5093              :       /* Validate changes.  */
    5094       106300 :       gfc_commit_symbol (formal_arg->sym);
    5095       106300 :       if (actual)
    5096        11986 :         act_arg = act_arg->next;
    5097              :     }
    5098              : 
    5099              :   /* Add the interface to the symbol.  */
    5100        41696 :   add_proc_interface (dest, IFSRC_DECL, head);
    5101              : 
    5102              :   /* Store the formal namespace information.  */
    5103        41696 :   if (dest->formal != NULL)
    5104              :     /* The current ns should be that for the dest proc.  */
    5105        40960 :     dest->formal_ns = gfc_current_ns;
    5106              :   else
    5107          736 :     gfc_free_namespace (gfc_current_ns);
    5108              :   /* Restore the current namespace to what it was on entry.  */
    5109        41696 :   gfc_current_ns = parent_ns;
    5110        41696 : }
    5111              : 
    5112              : 
    5113              : static int
    5114       156160 : std_for_isocbinding_symbol (int id)
    5115              : {
    5116            0 :   switch (id)
    5117              :     {
    5118              : #define NAMED_INTCST(a,b,c,d) \
    5119              :       case a:\
    5120              :         return d;
    5121              : #include "iso-c-binding.def"
    5122              : #undef NAMED_INTCST
    5123              : 
    5124              : #define NAMED_UINTCST(a,b,c,d) \
    5125              :       case a:\
    5126              :         return d;
    5127              : #include "iso-c-binding.def"
    5128              : #undef NAMED_UINTCST
    5129              : 
    5130              : #define NAMED_FUNCTION(a,b,c,d) \
    5131              :       case a:\
    5132              :         return d;
    5133              : #define NAMED_SUBROUTINE(a,b,c,d) \
    5134              :       case a:\
    5135              :         return d;
    5136              : #include "iso-c-binding.def"
    5137              : #undef NAMED_FUNCTION
    5138              : #undef NAMED_SUBROUTINE
    5139              : 
    5140              :        default:
    5141              :          return GFC_STD_F2003;
    5142              :     }
    5143              : }
    5144              : 
    5145              : /* Generate the given set of C interoperable kind objects, or all
    5146              :    interoperable kinds.  This function will only be given kind objects
    5147              :    for valid iso_c_binding defined types because this is verified when
    5148              :    the 'use' statement is parsed.  If the user gives an 'only' clause,
    5149              :    the specific kinds are looked up; if they don't exist, an error is
    5150              :    reported.  If the user does not give an 'only' clause, all
    5151              :    iso_c_binding symbols are generated.  If a list of specific kinds
    5152              :    is given, it must have a NULL in the first empty spot to mark the
    5153              :    end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
    5154              :    point to the symtree for c_(fun)ptr.  */
    5155              : 
    5156              : gfc_symtree *
    5157       156160 : generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
    5158              :                              const char *local_name, gfc_symtree *dt_symtree,
    5159              :                              bool hidden)
    5160              : {
    5161       156160 :   const char *const name = (local_name && local_name[0])
    5162       156160 :                            ? local_name : c_interop_kinds_table[s].name;
    5163       156160 :   gfc_symtree *tmp_symtree;
    5164       156160 :   gfc_symbol *tmp_sym = NULL;
    5165       156160 :   int index;
    5166              : 
    5167       291519 :   if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
    5168              :     return NULL;
    5169              : 
    5170       156160 :   tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
    5171       156160 :   if (hidden
    5172           48 :       && (!tmp_symtree || !tmp_symtree->n.sym
    5173           14 :           || tmp_symtree->n.sym->from_intmod != INTMOD_ISO_C_BINDING
    5174           14 :           || tmp_symtree->n.sym->intmod_sym_id != s))
    5175           34 :     tmp_symtree = NULL;
    5176              : 
    5177              :   /* Already exists in this scope so don't re-add it.  */
    5178          318 :   if (tmp_symtree != NULL && (tmp_sym = tmp_symtree->n.sym) != NULL
    5179          318 :       && (!tmp_sym->attr.generic
    5180           52 :           || (tmp_sym = gfc_find_dt_in_generic (tmp_sym)) != NULL)
    5181       156478 :       && tmp_sym->from_intmod == INTMOD_ISO_C_BINDING)
    5182              :     {
    5183          318 :       if (tmp_sym->attr.flavor == FL_DERIVED
    5184          318 :           && !get_iso_c_binding_dt (tmp_sym->intmod_sym_id))
    5185              :         {
    5186           52 :           if (gfc_derived_types)
    5187              :             {
    5188           25 :               tmp_sym->dt_next = gfc_derived_types->dt_next;
    5189           25 :               gfc_derived_types->dt_next = tmp_sym;
    5190              :             }
    5191              :           else
    5192              :             {
    5193           27 :               tmp_sym->dt_next = tmp_sym;
    5194              :             }
    5195           52 :           gfc_derived_types = tmp_sym;
    5196              :         }
    5197              : 
    5198          318 :       return tmp_symtree;
    5199              :     }
    5200              : 
    5201              :   /* Create the sym tree in the current ns.  */
    5202       155842 :   if (hidden)
    5203              :     {
    5204           34 :       tmp_symtree = gfc_get_unique_symtree (gfc_current_ns);
    5205           34 :       tmp_sym = gfc_new_symbol (name, gfc_current_ns);
    5206              : 
    5207              :       /* Add to the list of tentative symbols.  */
    5208           34 :       latest_undo_chgset->syms.safe_push (tmp_sym);
    5209           34 :       tmp_sym->old_symbol = NULL;
    5210           34 :       tmp_sym->mark = 1;
    5211           34 :       tmp_sym->gfc_new = 1;
    5212              : 
    5213           34 :       tmp_symtree->n.sym = tmp_sym;
    5214           34 :       tmp_sym->refs++;
    5215              :     }
    5216              :   else
    5217              :     {
    5218       155808 :       gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
    5219       155808 :       gcc_assert (tmp_symtree);
    5220       155808 :       tmp_sym = tmp_symtree->n.sym;
    5221              :     }
    5222              : 
    5223              :   /* Say what module this symbol belongs to.  */
    5224       155842 :   tmp_sym->module = gfc_get_string ("%s", mod_name);
    5225       155842 :   tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
    5226       155842 :   tmp_sym->intmod_sym_id = s;
    5227       155842 :   tmp_sym->attr.is_iso_c = 1;
    5228       155842 :   tmp_sym->attr.use_assoc = 1;
    5229              : 
    5230       155842 :   gcc_assert (dt_symtree == NULL || s == ISOCBINDING_NULL_FUNPTR
    5231              :               || s == ISOCBINDING_NULL_PTR);
    5232              : 
    5233       152589 :   switch (s)
    5234              :     {
    5235              : 
    5236              : #define NAMED_INTCST(a,b,c,d) case a :
    5237              : #define NAMED_UINTCST(a,b,c,d) case a :
    5238              : #define NAMED_REALCST(a,b,c,d) case a :
    5239              : #define NAMED_CMPXCST(a,b,c,d) case a :
    5240              : #define NAMED_LOGCST(a,b,c) case a :
    5241              : #define NAMED_CHARKNDCST(a,b,c) case a :
    5242              : #include "iso-c-binding.def"
    5243              : 
    5244       230206 :         tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
    5245       115103 :                                            c_interop_kinds_table[s].value);
    5246              : 
    5247              :         /* Initialize an integer constant expression node.  */
    5248       115103 :         tmp_sym->attr.flavor = FL_PARAMETER;
    5249       115103 :         tmp_sym->ts.type = BT_INTEGER;
    5250       115103 :         tmp_sym->ts.kind = gfc_default_integer_kind;
    5251              : 
    5252              :         /* Mark this type as a C interoperable one.  */
    5253       115103 :         tmp_sym->ts.is_c_interop = 1;
    5254       115103 :         tmp_sym->ts.is_iso_c = 1;
    5255       115103 :         tmp_sym->value->ts.is_c_interop = 1;
    5256       115103 :         tmp_sym->value->ts.is_iso_c = 1;
    5257       115103 :         tmp_sym->attr.is_c_interop = 1;
    5258              : 
    5259              :         /* Tell what f90 type this c interop kind is valid.  */
    5260       115103 :         tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
    5261              : 
    5262       115103 :         break;
    5263              : 
    5264              : 
    5265              : #define NAMED_CHARCST(a,b,c) case a :
    5266              : #include "iso-c-binding.def"
    5267              : 
    5268              :         /* Initialize an integer constant expression node for the
    5269              :            length of the character.  */
    5270        25548 :         tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
    5271              :                                                  &gfc_current_locus, NULL, 1);
    5272        25548 :         tmp_sym->value->ts.is_c_interop = 1;
    5273        25548 :         tmp_sym->value->ts.is_iso_c = 1;
    5274        25548 :         tmp_sym->value->value.character.length = 1;
    5275        25548 :         tmp_sym->value->value.character.string[0]
    5276        25548 :           = (gfc_char_t) c_interop_kinds_table[s].value;
    5277        25548 :         tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5278        25548 :         tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    5279              :                                                      NULL, 1);
    5280              : 
    5281              :         /* May not need this in both attr and ts, but do need in
    5282              :            attr for writing module file.  */
    5283        25548 :         tmp_sym->attr.is_c_interop = 1;
    5284              : 
    5285        25548 :         tmp_sym->attr.flavor = FL_PARAMETER;
    5286        25548 :         tmp_sym->ts.type = BT_CHARACTER;
    5287              : 
    5288              :         /* Need to set it to the C_CHAR kind.  */
    5289        25548 :         tmp_sym->ts.kind = gfc_default_character_kind;
    5290              : 
    5291              :         /* Mark this type as a C interoperable one.  */
    5292        25548 :         tmp_sym->ts.is_c_interop = 1;
    5293        25548 :         tmp_sym->ts.is_iso_c = 1;
    5294              : 
    5295              :         /* Tell what f90 type this c interop kind is valid.  */
    5296        25548 :         tmp_sym->ts.f90_type = BT_CHARACTER;
    5297              : 
    5298        25548 :         break;
    5299              : 
    5300         8737 :       case ISOCBINDING_PTR:
    5301         8737 :       case ISOCBINDING_FUNPTR:
    5302         8737 :         {
    5303         8737 :           gfc_symbol *dt_sym;
    5304         8737 :           gfc_component *tmp_comp = NULL;
    5305              : 
    5306              :           /* Generate real derived type.  */
    5307         8737 :           if (hidden)
    5308              :             dt_sym = tmp_sym;
    5309              :           else
    5310              :             {
    5311         8703 :               const char *hidden_name;
    5312         8703 :               gfc_interface *intr, *head;
    5313              : 
    5314         8703 :               hidden_name = gfc_dt_upper_string (tmp_sym->name);
    5315         8703 :               tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
    5316              :                                               hidden_name);
    5317         8703 :               gcc_assert (tmp_symtree == NULL);
    5318         8703 :               gfc_get_sym_tree (hidden_name, gfc_current_ns, &tmp_symtree, false);
    5319         8703 :               dt_sym = tmp_symtree->n.sym;
    5320        11995 :               dt_sym->name = gfc_get_string (s == ISOCBINDING_PTR
    5321              :                                              ? "c_ptr" : "c_funptr");
    5322              : 
    5323              :               /* Generate an artificial generic function.  */
    5324         8703 :               head = tmp_sym->generic;
    5325         8703 :               intr = gfc_get_interface ();
    5326         8703 :               intr->sym = dt_sym;
    5327         8703 :               intr->where = gfc_current_locus;
    5328         8703 :               intr->next = head;
    5329         8703 :               tmp_sym->generic = intr;
    5330              : 
    5331         8703 :               if (!tmp_sym->attr.generic
    5332         8703 :                   && !gfc_add_generic (&tmp_sym->attr, tmp_sym->name, NULL))
    5333            0 :                 return NULL;
    5334              : 
    5335         8703 :               if (!tmp_sym->attr.function
    5336         8703 :                   && !gfc_add_function (&tmp_sym->attr, tmp_sym->name, NULL))
    5337              :                 return NULL;
    5338              :             }
    5339              : 
    5340              :           /* Say what module this symbol belongs to.  */
    5341         8737 :           dt_sym->module = gfc_get_string ("%s", mod_name);
    5342         8737 :           dt_sym->from_intmod = INTMOD_ISO_C_BINDING;
    5343         8737 :           dt_sym->intmod_sym_id = s;
    5344         8737 :           dt_sym->attr.use_assoc = 1;
    5345              : 
    5346              :           /* Initialize an integer constant expression node.  */
    5347         8737 :           dt_sym->attr.flavor = FL_DERIVED;
    5348         8737 :           dt_sym->ts.is_c_interop = 1;
    5349         8737 :           dt_sym->attr.is_c_interop = 1;
    5350         8737 :           dt_sym->attr.private_comp = 1;
    5351         8737 :           dt_sym->component_access = ACCESS_PRIVATE;
    5352         8737 :           dt_sym->ts.is_iso_c = 1;
    5353         8737 :           dt_sym->ts.type = BT_DERIVED;
    5354         8737 :           dt_sym->ts.f90_type = BT_VOID;
    5355              : 
    5356              :           /* A derived type must have the bind attribute to be
    5357              :              interoperable (J3/04-007, Section 15.2.3), even though
    5358              :              the binding label is not used.  */
    5359         8737 :           dt_sym->attr.is_bind_c = 1;
    5360              : 
    5361         8737 :           dt_sym->attr.referenced = 1;
    5362         8737 :           dt_sym->ts.u.derived = dt_sym;
    5363              : 
    5364              :           /* Add the symbol created for the derived type to the current ns.  */
    5365         8737 :           if (gfc_derived_types)
    5366              :             {
    5367         6681 :               dt_sym->dt_next = gfc_derived_types->dt_next;
    5368         6681 :               gfc_derived_types->dt_next = dt_sym;
    5369              :             }
    5370              :           else
    5371              :             {
    5372         2056 :               dt_sym->dt_next = dt_sym;
    5373              :             }
    5374         8737 :           gfc_derived_types = dt_sym;
    5375              : 
    5376         8737 :           gfc_add_component (dt_sym, "c_address", &tmp_comp);
    5377         8737 :           if (tmp_comp == NULL)
    5378            0 :             gcc_unreachable ();
    5379              : 
    5380         8737 :           tmp_comp->ts.type = BT_INTEGER;
    5381              : 
    5382              :           /* Set this because the module will need to read/write this field.  */
    5383         8737 :           tmp_comp->ts.f90_type = BT_INTEGER;
    5384              : 
    5385              :           /* The kinds for c_ptr and c_funptr are the same.  */
    5386         8737 :           index = get_c_kind ("c_ptr", c_interop_kinds_table);
    5387         8737 :           tmp_comp->ts.kind = c_interop_kinds_table[index].value;
    5388         8737 :           tmp_comp->attr.access = ACCESS_PRIVATE;
    5389              : 
    5390              :           /* Mark the component as C interoperable.  */
    5391         8737 :           tmp_comp->ts.is_c_interop = 1;
    5392              :         }
    5393              : 
    5394         8737 :         break;
    5395              : 
    5396         6454 :       case ISOCBINDING_NULL_PTR:
    5397         6454 :       case ISOCBINDING_NULL_FUNPTR:
    5398         6454 :         gen_special_c_interop_ptr (tmp_sym, dt_symtree);
    5399         6454 :         break;
    5400              : 
    5401            0 :       default:
    5402            0 :         gcc_unreachable ();
    5403              :     }
    5404       155842 :   gfc_commit_symbol (tmp_sym);
    5405       155842 :   return tmp_symtree;
    5406              : }
    5407              : 
    5408              : 
    5409              : /* Check that a symbol is already typed.  If strict is not set, an untyped
    5410              :    symbol is acceptable for non-standard-conforming mode.  */
    5411              : 
    5412              : bool
    5413        14495 : gfc_check_symbol_typed (gfc_symbol* sym, gfc_namespace* ns,
    5414              :                         bool strict, locus where)
    5415              : {
    5416        14495 :   gcc_assert (sym);
    5417              : 
    5418        14495 :   if (gfc_matching_prefix)
    5419              :     return true;
    5420              : 
    5421              :   /* Check for the type and try to give it an implicit one.  */
    5422        14452 :   if (sym->ts.type == BT_UNKNOWN
    5423        14452 :       && !gfc_set_default_type (sym, 0, ns))
    5424              :     {
    5425          451 :       if (strict)
    5426              :         {
    5427           11 :           gfc_error ("Symbol %qs is used before it is typed at %L",
    5428              :                      sym->name, &where);
    5429           11 :           return false;
    5430              :         }
    5431              : 
    5432          440 :       if (!gfc_notify_std (GFC_STD_GNU, "Symbol %qs is used before"
    5433              :                            " it is typed at %L", sym->name, &where))
    5434              :         return false;
    5435              :     }
    5436              : 
    5437              :   /* Everything is ok.  */
    5438              :   return true;
    5439              : }
    5440              : 
    5441              : 
    5442              : /* Construct a typebound-procedure structure.  Those are stored in a tentative
    5443              :    list and marked `error' until symbols are committed.  */
    5444              : 
    5445              : gfc_typebound_proc*
    5446        59175 : gfc_get_typebound_proc (gfc_typebound_proc *tb0)
    5447              : {
    5448        59175 :   gfc_typebound_proc *result;
    5449              : 
    5450        59175 :   result = XCNEW (gfc_typebound_proc);
    5451        59175 :   if (tb0)
    5452         3163 :     *result = *tb0;
    5453        59175 :   result->error = 1;
    5454              : 
    5455        59175 :   latest_undo_chgset->tbps.safe_push (result);
    5456              : 
    5457        59175 :   return result;
    5458              : }
    5459              : 
    5460              : 
    5461              : /* Get the super-type of a given derived type.  */
    5462              : 
    5463              : gfc_symbol*
    5464       674090 : gfc_get_derived_super_type (gfc_symbol* derived)
    5465              : {
    5466       674090 :   gcc_assert (derived);
    5467              : 
    5468       674090 :   if (derived->attr.generic)
    5469            2 :     derived = gfc_find_dt_in_generic (derived);
    5470              : 
    5471       674090 :   if (!derived->attr.extension)
    5472              :     return NULL;
    5473              : 
    5474       125461 :   gcc_assert (derived->components);
    5475       125461 :   gcc_assert (derived->components->ts.type == BT_DERIVED);
    5476       125461 :   gcc_assert (derived->components->ts.u.derived);
    5477              : 
    5478       125461 :   if (derived->components->ts.u.derived->attr.generic)
    5479            0 :     return gfc_find_dt_in_generic (derived->components->ts.u.derived);
    5480              : 
    5481              :   return derived->components->ts.u.derived;
    5482              : }
    5483              : 
    5484              : 
    5485              : /* Check if a derived type t2 is an extension of (or equal to) a type t1.  */
    5486              : 
    5487              : bool
    5488        30599 : gfc_type_is_extension_of (gfc_symbol *t1, gfc_symbol *t2)
    5489              : {
    5490        34701 :   while (!gfc_compare_derived_types (t1, t2) && t2->attr.extension)
    5491         4102 :     t2 = gfc_get_derived_super_type (t2);
    5492        30599 :   return gfc_compare_derived_types (t1, t2);
    5493              : }
    5494              : 
    5495              : /* Check if parameterized derived type t2 is an instance of pdt template t1
    5496              : 
    5497              :    gfc_symbol *t1 -> pdt template to verify t2 against.
    5498              :    gfc_symbol *t2 -> pdt instance to be verified.
    5499              : 
    5500              :    In decl.cc, gfc_get_pdt_instance, a pdt instance is given a 3 character
    5501              :    prefix PDT_PREFIX, followed by an underscore list of the kind parameters,
    5502              :    up to a maximum of 8 kind parameters.  To verify if a PDT Type corresponds
    5503              :    to the template, this functions extracts t2's derive_type name,
    5504              :    and compares it to the derive_type name of t1 for compatibility.
    5505              : 
    5506              :    For example:
    5507              : 
    5508              :    t2->name = PDT_PREFIXf_2_2; extract the 'f' and compare with t1->name.  */
    5509              : 
    5510              : bool
    5511           18 : gfc_pdt_is_instance_of (gfc_symbol *t1, gfc_symbol *t2)
    5512              : {
    5513           18 :   if ( !t1->attr.pdt_template || !t2->attr.pdt_type )
    5514              :     return false;
    5515              : 
    5516              :   /* Limit comparison to length of t1->name to ignore new kind params.  */
    5517           18 :   if ( !(strncmp (&(t2->name[PDT_PREFIX_LEN]), t1->name,
    5518              :                   strlen (t1->name)) == 0) )
    5519            0 :     return false;
    5520              : 
    5521              :   return true;
    5522              : }
    5523              : 
    5524              : /* Check if two typespecs are type compatible (F03:5.1.1.2):
    5525              :    If ts1 is nonpolymorphic, ts2 must be the same type.
    5526              :    If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1.  */
    5527              : 
    5528              : bool
    5529       287677 : gfc_type_compatible (gfc_typespec *ts1, gfc_typespec *ts2)
    5530              : {
    5531       287677 :   bool is_class1 = (ts1->type == BT_CLASS);
    5532       287677 :   bool is_class2 = (ts2->type == BT_CLASS);
    5533       287677 :   bool is_derived1 = (ts1->type == BT_DERIVED);
    5534       287677 :   bool is_derived2 = (ts2->type == BT_DERIVED);
    5535       287677 :   bool is_union1 = (ts1->type == BT_UNION);
    5536       287677 :   bool is_union2 = (ts2->type == BT_UNION);
    5537              : 
    5538              :   /* A boz-literal-constant has no type.  */
    5539       287677 :   if (ts1->type == BT_BOZ || ts2->type == BT_BOZ)
    5540              :     return false;
    5541              : 
    5542       287675 :   if (is_class1
    5543        29147 :       && ts1->u.derived->components
    5544        28987 :       && ((ts1->u.derived->attr.is_class
    5545        28980 :            && ts1->u.derived->components->ts.u.derived->attr
    5546        28980 :                                                         .unlimited_polymorphic)
    5547        28183 :           || ts1->u.derived->attr.unlimited_polymorphic))
    5548              :     return 1;
    5549              : 
    5550       286871 :   if (!is_derived1 && !is_derived2 && !is_class1 && !is_class2
    5551         2371 :       && !is_union1 && !is_union2)
    5552         2371 :     return (ts1->type == ts2->type);
    5553              : 
    5554       284500 :   if ((is_derived1 && is_derived2) || (is_union1 && is_union2))
    5555       255108 :     return gfc_compare_derived_types (ts1->u.derived, ts2->u.derived);
    5556              : 
    5557        29392 :   if (is_derived1 && is_class2)
    5558         1045 :     return gfc_compare_derived_types (ts1->u.derived,
    5559         1045 :                                       ts2->u.derived->attr.is_class ?
    5560         1042 :                                       ts2->u.derived->components->ts.u.derived
    5561         1045 :                                       : ts2->u.derived);
    5562        28347 :   if (is_class1 && is_derived2)
    5563         9874 :     return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
    5564         9873 :                                        ts1->u.derived->components->ts.u.derived
    5565              :                                      : ts1->u.derived,
    5566        19748 :                                      ts2->u.derived);
    5567        18473 :   else if (is_class1 && is_class2)
    5568        36772 :     return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
    5569        18303 :                                        ts1->u.derived->components->ts.u.derived
    5570              :                                      : ts1->u.derived,
    5571        18469 :                                      ts2->u.derived->attr.is_class ?
    5572        18304 :                                        ts2->u.derived->components->ts.u.derived
    5573        18469 :                                      : ts2->u.derived);
    5574              :   else
    5575              :     return 0;
    5576              : }
    5577              : 
    5578              : 
    5579              : /* Find the parent-namespace of the current function.  If we're inside
    5580              :    BLOCK constructs, it may not be the current one.  */
    5581              : 
    5582              : gfc_namespace*
    5583        63826 : gfc_find_proc_namespace (gfc_namespace* ns)
    5584              : {
    5585        64388 :   while (ns->construct_entities)
    5586              :     {
    5587          562 :       ns = ns->parent;
    5588          562 :       gcc_assert (ns);
    5589              :     }
    5590              : 
    5591        63826 :   return ns;
    5592              : }
    5593              : 
    5594              : 
    5595              : /* Check if an associate-variable should be translated as an `implicit' pointer
    5596              :    internally (if it is associated to a variable and not an array with
    5597              :    descriptor).  */
    5598              : 
    5599              : bool
    5600       494377 : gfc_is_associate_pointer (gfc_symbol* sym)
    5601              : {
    5602       494377 :   if (!sym->assoc)
    5603              :     return false;
    5604              : 
    5605        12078 :   if (sym->ts.type == BT_CLASS)
    5606              :     return true;
    5607              : 
    5608         6693 :   if (sym->ts.type == BT_CHARACTER
    5609         1260 :       && sym->ts.deferred
    5610           56 :       && sym->assoc->target
    5611           56 :       && sym->assoc->target->expr_type == EXPR_FUNCTION)
    5612              :     return true;
    5613              : 
    5614         6687 :   if (!sym->assoc->variable)
    5615              :     return false;
    5616              : 
    5617         5775 :   if ((sym->attr.dimension || sym->attr.codimension)
    5618            0 :       && sym->as->type != AS_EXPLICIT)
    5619            0 :     return false;
    5620              : 
    5621              :   return true;
    5622              : }
    5623              : 
    5624              : 
    5625              : gfc_symbol *
    5626        34215 : gfc_find_dt_in_generic (gfc_symbol *sym)
    5627              : {
    5628        34215 :   gfc_interface *intr = NULL;
    5629              : 
    5630        34215 :   if (!sym || gfc_fl_struct (sym->attr.flavor))
    5631              :     return sym;
    5632              : 
    5633        34215 :   if (sym->attr.generic)
    5634        35959 :     for (intr = sym->generic; intr; intr = intr->next)
    5635        22856 :       if (gfc_fl_struct (intr->sym->attr.flavor))
    5636              :         break;
    5637        34213 :   return intr ? intr->sym : NULL;
    5638              : }
    5639              : 
    5640              : 
    5641              : /* Get the dummy arguments from a procedure symbol. If it has been declared
    5642              :    via a PROCEDURE statement with a named interface, ts.interface will be set
    5643              :    and the arguments need to be taken from there.  */
    5644              : 
    5645              : gfc_formal_arglist *
    5646      3719543 : gfc_sym_get_dummy_args (gfc_symbol *sym)
    5647              : {
    5648      3719543 :   gfc_formal_arglist *dummies;
    5649              : 
    5650      3719543 :   if (sym == NULL)
    5651              :     return NULL;
    5652              : 
    5653      3719542 :   dummies = sym->formal;
    5654      3719542 :   if (dummies == NULL && sym->ts.interface != NULL)
    5655         7118 :     dummies = sym->ts.interface->formal;
    5656              : 
    5657              :   return dummies;
    5658              : }
    5659              : 
    5660              : 
    5661              : /* Given a procedure, returns the associated namespace.
    5662              :    The resulting NS should match the condition NS->PROC_NAME == SYM.  */
    5663              : 
    5664              : gfc_namespace *
    5665       756114 : gfc_get_procedure_ns (gfc_symbol *sym)
    5666              : {
    5667       756114 :   if (sym->formal_ns
    5668       574172 :       && sym->formal_ns->proc_name == sym
    5669              :       /* For module procedures used in submodules, there are two namespaces.
    5670              :          The one generated by the host association of the module is directly
    5671              :          accessible through SYM->FORMAL_NS but doesn't have any parent set.
    5672              :          The one generated by the parser is only accessible by walking the
    5673              :          contained namespace but has its parent set.  Prefer the one generated
    5674              :          by the parser below.  */
    5675       573748 :       && !(sym->attr.used_in_submodule
    5676          993 :            && sym->attr.contained
    5677          417 :            && sym->formal_ns->parent == nullptr))
    5678              :     return sym->formal_ns;
    5679              : 
    5680              :   /* The above should have worked in most cases.  If it hasn't, try some other
    5681              :      heuristics, eventually returning SYM->NS.  */
    5682       182781 :   if (gfc_current_ns->proc_name == sym)
    5683              :     return gfc_current_ns;
    5684              : 
    5685              :   /* For contained procedures, the symbol's NS field is the
    5686              :      hosting namespace, not the procedure namespace.  */
    5687       157608 :   if (sym->attr.flavor == FL_PROCEDURE && sym->attr.contained)
    5688       178030 :     for (gfc_namespace *ns = sym->ns->contained; ns; ns = ns->sibling)
    5689       177676 :       if (ns->proc_name == sym)
    5690              :         return ns;
    5691              : 
    5692       115569 :   if (sym->formal_ns
    5693          424 :       && sym->formal_ns->proc_name == sym)
    5694              :     return sym->formal_ns;
    5695              : 
    5696       115569 :   if (sym->formal)
    5697         3948 :     for (gfc_formal_arglist *f = sym->formal; f != nullptr; f = f->next)
    5698         2290 :       if (f->sym)
    5699              :         {
    5700         2243 :           gfc_namespace *ns = f->sym->ns;
    5701         2243 :           if (ns && ns->proc_name == sym)
    5702              :             return ns;
    5703              :         }
    5704              : 
    5705       115569 :   return sym->ns;
    5706              : }
    5707              : 
    5708              : 
    5709              : /* Given a symbol, returns the namespace in which the symbol is specified.
    5710              :    In most cases, it is the namespace hosting the symbol.  This is the case
    5711              :    for variables.  For functions, however, it is the function namespace
    5712              :    itself.  This specification namespace is used to check conformance of
    5713              :    array spec bound expressions.  */
    5714              : 
    5715              : gfc_namespace *
    5716      1726421 : gfc_get_spec_ns (gfc_symbol *sym)
    5717              : {
    5718      1726421 :   if (sym->attr.flavor == FL_PROCEDURE
    5719       481167 :       && sym->attr.function)
    5720              :     {
    5721       320769 :       if (sym->result == sym)
    5722       231781 :         return gfc_get_procedure_ns (sym);
    5723              :       /* Generic and intrinsic functions can have a null result.  */
    5724        88988 :       else if (sym->result != nullptr)
    5725        37314 :         return sym->result->ns;
    5726              :     }
    5727              : 
    5728      1457326 :   return sym->ns;
    5729              : }
    5730              : 
    5731              : /* This section deals with looking up a symbol when the symtree name and symbol
    5732              :    name do not agree, so gfc_find_symbol() cannot be used.  */
    5733              : 
    5734              : static gfc_symbol* found_sym;           /* Where to store the symbol.  */
    5735              : static const char* sym_target_name;     /* What name to look for.  */
    5736              : 
    5737              : /* Helper function.  */
    5738              : 
    5739              : static void
    5740           26 : compare_target_sym_name (gfc_symbol *sym)
    5741              : {
    5742           26 :   if (strcmp(sym->name, sym_target_name) == 0)
    5743            1 :     found_sym = sym;
    5744           26 : }
    5745              : 
    5746              : /* Search for a symbol when the symtree name may be different from the
    5747              :    symbol name.  Return true if found.  */
    5748              : 
    5749              : bool
    5750            1 : gfc_find_symbol_by_name (const char *name, gfc_namespace *ns,
    5751              :                                gfc_symbol **result)
    5752              : {
    5753            1 :   found_sym = NULL;
    5754            1 :   sym_target_name = name;
    5755              : 
    5756            1 :   do_traverse_symtree (ns->sym_root, NULL, compare_target_sym_name);
    5757            1 :   *result = found_sym;
    5758            1 :   return result != 0;
    5759              : }
        

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.