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