LCOV - code coverage report
Current view: top level - gcc/fortran - decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.8 % 6180 5614
Test Date: 2026-08-22 16:33:35 Functions: 100.0 % 138 138
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Declaration statement matcher
       2              :    Copyright (C) 2002-2026 Free Software Foundation, Inc.
       3              :    Contributed by Andy Vaught
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "options.h"
      25              : #include "tree.h"
      26              : #include "gfortran.h"
      27              : #include "stringpool.h"
      28              : #include "match.h"
      29              : #include "parse.h"
      30              : #include "constructor.h"
      31              : #include "target.h"
      32              : #include "flags.h"
      33              : 
      34              : /* Macros to access allocate memory for gfc_data_variable,
      35              :    gfc_data_value and gfc_data.  */
      36              : #define gfc_get_data_variable() XCNEW (gfc_data_variable)
      37              : #define gfc_get_data_value() XCNEW (gfc_data_value)
      38              : #define gfc_get_data() XCNEW (gfc_data)
      39              : 
      40              : 
      41              : static bool set_binding_label (const char **, const char *, int);
      42              : 
      43              : 
      44              : /* This flag is set if an old-style length selector is matched
      45              :    during a type-declaration statement.  */
      46              : 
      47              : static int old_char_selector;
      48              : 
      49              : /* When variables acquire types and attributes from a declaration
      50              :    statement, they get them from the following static variables.  The
      51              :    first part of a declaration sets these variables and the second
      52              :    part copies these into symbol structures.  */
      53              : 
      54              : static gfc_typespec current_ts;
      55              : 
      56              : static symbol_attribute current_attr;
      57              : static gfc_array_spec *current_as;
      58              : static int colon_seen;
      59              : static int attr_seen;
      60              : 
      61              : /* The current binding label (if any).  */
      62              : static const char* curr_binding_label;
      63              : /* Need to know how many identifiers are on the current data declaration
      64              :    line in case we're given the BIND(C) attribute with a NAME= specifier.  */
      65              : static int num_idents_on_line;
      66              : /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
      67              :    can supply a name if the curr_binding_label is nil and NAME= was not.  */
      68              : static int has_name_equals = 0;
      69              : 
      70              : /* Initializer of the previous enumerator.  */
      71              : 
      72              : static gfc_expr *last_initializer;
      73              : 
      74              : /* History of all the enumerators is maintained, so that
      75              :    kind values of all the enumerators could be updated depending
      76              :    upon the maximum initialized value.  */
      77              : 
      78              : typedef struct enumerator_history
      79              : {
      80              :   gfc_symbol *sym;
      81              :   gfc_expr *initializer;
      82              :   struct enumerator_history *next;
      83              : }
      84              : enumerator_history;
      85              : 
      86              : /* Header of enum history chain.  */
      87              : 
      88              : static enumerator_history *enum_history = NULL;
      89              : 
      90              : /* Pointer of enum history node containing largest initializer.  */
      91              : 
      92              : static enumerator_history *max_enum = NULL;
      93              : 
      94              : /* gfc_new_block points to the symbol of a newly matched block.  */
      95              : 
      96              : gfc_symbol *gfc_new_block;
      97              : 
      98              : bool gfc_matching_function;
      99              : 
     100              : /* Set upon parsing a !GCC$ unroll n directive for use in the next loop.  */
     101              : int directive_unroll = -1;
     102              : 
     103              : /* Set upon parsing supported !GCC$ pragmas for use in the next loop.  */
     104              : bool directive_ivdep = false;
     105              : bool directive_vector = false;
     106              : bool directive_novector = false;
     107              : 
     108              : /* Map of middle-end built-ins that should be vectorized.  */
     109              : hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
     110              : 
     111              : /* If a kind expression of a component of a parameterized derived type is
     112              :    parameterized, temporarily store the expression here.  */
     113              : static gfc_expr *saved_kind_expr = NULL;
     114              : 
     115              : /* Used to store the parameter list arising in a PDT declaration and
     116              :    in the typespec of a PDT variable or component.  */
     117              : static gfc_actual_arglist *decl_type_param_list;
     118              : static gfc_actual_arglist *type_param_spec_list;
     119              : 
     120              : /* Drop an unattached gfc_charlen node from the current namespace.  This is
     121              :    used when declaration processing created a length node for a symbol that is
     122              :    rejected before the node is attached to any surviving symbol.  */
     123              : static void
     124            1 : discard_pending_charlen (gfc_charlen *cl)
     125              : {
     126            1 :   if (!cl || !gfc_current_ns || gfc_current_ns->cl_list != cl)
     127              :     return;
     128              : 
     129            1 :   gfc_remove_saved_charlen (cl);
     130            1 :   gfc_current_ns->cl_list = cl->next;
     131            1 :   gfc_free_expr (cl->length);
     132            1 :   free (cl);
     133              : }
     134              : 
     135              : /* Drop the charlen nodes created while matching a declaration that is about
     136              :    to be rejected.  Callers must clear any surviving owners before using this
     137              :    helper, so only the statement-local nodes remain on the namespace list.  */
     138              : 
     139              : static void
     140            3 : discard_pending_charlens (gfc_charlen *saved_cl)
     141              : {
     142            3 :   if (!gfc_current_ns)
     143              :     return;
     144              : 
     145           14 :   while (gfc_current_ns->cl_list != saved_cl)
     146              :     {
     147           11 :       gfc_charlen *cl = gfc_current_ns->cl_list;
     148              : 
     149           11 :       gcc_assert (cl);
     150           11 :       gfc_remove_saved_charlen (cl);
     151           11 :       gfc_current_ns->cl_list = cl->next;
     152           11 :       gfc_free_expr (cl->length);
     153           11 :       free (cl);
     154              :     }
     155              : }
     156              : 
     157              : /********************* DATA statement subroutines *********************/
     158              : 
     159              : static bool in_match_data = false;
     160              : 
     161              : bool
     162         8317 : gfc_in_match_data (void)
     163              : {
     164         8317 :   return in_match_data;
     165              : }
     166              : 
     167              : static void
     168         4840 : set_in_match_data (bool set_value)
     169              : {
     170         4840 :   in_match_data = set_value;
     171            0 : }
     172              : 
     173              : /* Free a gfc_data_variable structure and everything beneath it.  */
     174              : 
     175              : static void
     176         5663 : free_variable (gfc_data_variable *p)
     177              : {
     178         5663 :   gfc_data_variable *q;
     179              : 
     180         8752 :   for (; p; p = q)
     181              :     {
     182         3089 :       q = p->next;
     183         3089 :       gfc_free_expr (p->expr);
     184         3089 :       gfc_free_iterator (&p->iter, 0);
     185         3089 :       free_variable (p->list);
     186         3089 :       free (p);
     187              :     }
     188         5663 : }
     189              : 
     190              : 
     191              : /* Free a gfc_data_value structure and everything beneath it.  */
     192              : 
     193              : static void
     194         2574 : free_value (gfc_data_value *p)
     195              : {
     196         2574 :   gfc_data_value *q;
     197              : 
     198        10886 :   for (; p; p = q)
     199              :     {
     200         8312 :       q = p->next;
     201         8312 :       mpz_clear (p->repeat);
     202         8312 :       gfc_free_expr (p->expr);
     203         8312 :       free (p);
     204              :     }
     205         2574 : }
     206              : 
     207              : 
     208              : /* Free a list of gfc_data structures.  */
     209              : 
     210              : void
     211       545687 : gfc_free_data (gfc_data *p)
     212              : {
     213       545687 :   gfc_data *q;
     214              : 
     215       548261 :   for (; p; p = q)
     216              :     {
     217         2574 :       q = p->next;
     218         2574 :       free_variable (p->var);
     219         2574 :       free_value (p->value);
     220         2574 :       free (p);
     221              :     }
     222       545687 : }
     223              : 
     224              : 
     225              : /* Free all data in a namespace.  */
     226              : 
     227              : static void
     228           41 : gfc_free_data_all (gfc_namespace *ns)
     229              : {
     230           41 :   gfc_data *d;
     231              : 
     232           47 :   for (;ns->data;)
     233              :     {
     234            6 :       d = ns->data->next;
     235            6 :       free (ns->data);
     236            6 :       ns->data = d;
     237              :     }
     238           41 : }
     239              : 
     240              : /* Reject data parsed since the last restore point was marked.  */
     241              : 
     242              : void
     243      9186367 : gfc_reject_data (gfc_namespace *ns)
     244              : {
     245      9186367 :   gfc_data *d;
     246              : 
     247      9186369 :   while (ns->data && ns->data != ns->old_data)
     248              :     {
     249            2 :       d = ns->data->next;
     250            2 :       free (ns->data);
     251            2 :       ns->data = d;
     252              :     }
     253      9186367 : }
     254              : 
     255              : static match var_element (gfc_data_variable *);
     256              : 
     257              : /* Match a list of variables terminated by an iterator and a right
     258              :    parenthesis.  */
     259              : 
     260              : static match
     261          154 : var_list (gfc_data_variable *parent)
     262              : {
     263          154 :   gfc_data_variable *tail, var;
     264          154 :   match m;
     265              : 
     266          154 :   m = var_element (&var);
     267          154 :   if (m == MATCH_ERROR)
     268              :     return MATCH_ERROR;
     269          154 :   if (m == MATCH_NO)
     270            0 :     goto syntax;
     271              : 
     272          154 :   tail = gfc_get_data_variable ();
     273          154 :   *tail = var;
     274              : 
     275          154 :   parent->list = tail;
     276              : 
     277          156 :   for (;;)
     278              :     {
     279          155 :       if (gfc_match_char (',') != MATCH_YES)
     280            0 :         goto syntax;
     281              : 
     282          155 :       m = gfc_match_iterator (&parent->iter, 1);
     283          155 :       if (m == MATCH_YES)
     284              :         break;
     285            1 :       if (m == MATCH_ERROR)
     286              :         return MATCH_ERROR;
     287              : 
     288            1 :       m = var_element (&var);
     289            1 :       if (m == MATCH_ERROR)
     290              :         return MATCH_ERROR;
     291            1 :       if (m == MATCH_NO)
     292            0 :         goto syntax;
     293              : 
     294            1 :       tail->next = gfc_get_data_variable ();
     295            1 :       tail = tail->next;
     296              : 
     297            1 :       *tail = var;
     298              :     }
     299              : 
     300          154 :   if (gfc_match_char (')') != MATCH_YES)
     301            0 :     goto syntax;
     302              :   return MATCH_YES;
     303              : 
     304            0 : syntax:
     305            0 :   gfc_syntax_error (ST_DATA);
     306            0 :   return MATCH_ERROR;
     307              : }
     308              : 
     309              : 
     310              : /* Match a single element in a data variable list, which can be a
     311              :    variable-iterator list.  */
     312              : 
     313              : static match
     314         3047 : var_element (gfc_data_variable *new_var)
     315              : {
     316         3047 :   match m;
     317         3047 :   gfc_symbol *sym;
     318              : 
     319         3047 :   memset (new_var, 0, sizeof (gfc_data_variable));
     320              : 
     321         3047 :   if (gfc_match_char ('(') == MATCH_YES)
     322          154 :     return var_list (new_var);
     323              : 
     324         2893 :   m = gfc_match_variable (&new_var->expr, 0);
     325         2893 :   if (m != MATCH_YES)
     326              :     return m;
     327              : 
     328         2889 :   if (new_var->expr->expr_type == EXPR_CONSTANT
     329            2 :       && new_var->expr->symtree == NULL)
     330              :     {
     331            2 :       gfc_error ("Inquiry parameter cannot appear in a "
     332              :                  "data-stmt-object-list at %C");
     333            2 :       return MATCH_ERROR;
     334              :     }
     335              : 
     336         2887 :   sym = new_var->expr->symtree->n.sym;
     337              : 
     338              :   /* Symbol should already have an associated type.  */
     339         2887 :   if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
     340              :     return MATCH_ERROR;
     341              : 
     342         2886 :   if (!sym->attr.function && gfc_current_ns->parent
     343          148 :       && gfc_current_ns->parent == sym->ns)
     344              :     {
     345            1 :       gfc_error ("Host associated variable %qs may not be in the DATA "
     346              :                  "statement at %C", sym->name);
     347            1 :       return MATCH_ERROR;
     348              :     }
     349              : 
     350         2885 :   if (gfc_current_state () != COMP_BLOCK_DATA
     351         2732 :       && sym->attr.in_common
     352         2914 :       && !gfc_notify_std (GFC_STD_GNU, "initialization of "
     353              :                           "common block variable %qs in DATA statement at %C",
     354              :                           sym->name))
     355              :     return MATCH_ERROR;
     356              : 
     357         2883 :   if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
     358            5 :     return MATCH_ERROR;
     359              : 
     360              :   return MATCH_YES;
     361              : }
     362              : 
     363              : 
     364              : /* Match the top-level list of data variables.  */
     365              : 
     366              : static match
     367         2517 : top_var_list (gfc_data *d)
     368              : {
     369         2517 :   gfc_data_variable var, *tail, *new_var;
     370         2517 :   match m;
     371              : 
     372         2517 :   tail = NULL;
     373              : 
     374         2892 :   for (;;)
     375              :     {
     376         2892 :       m = var_element (&var);
     377         2892 :       if (m == MATCH_NO)
     378            0 :         goto syntax;
     379         2892 :       if (m == MATCH_ERROR)
     380              :         return MATCH_ERROR;
     381              : 
     382         2877 :       new_var = gfc_get_data_variable ();
     383         2877 :       *new_var = var;
     384         2877 :       if (new_var->expr)
     385         2751 :         new_var->expr->where = gfc_current_locus;
     386              : 
     387         2877 :       if (tail == NULL)
     388         2502 :         d->var = new_var;
     389              :       else
     390          375 :         tail->next = new_var;
     391              : 
     392         2877 :       tail = new_var;
     393              : 
     394         2877 :       if (gfc_match_char ('/') == MATCH_YES)
     395              :         break;
     396          378 :       if (gfc_match_char (',') != MATCH_YES)
     397            3 :         goto syntax;
     398              :     }
     399              : 
     400              :   return MATCH_YES;
     401              : 
     402            3 : syntax:
     403            3 :   gfc_syntax_error (ST_DATA);
     404            3 :   gfc_free_data_all (gfc_current_ns);
     405            3 :   return MATCH_ERROR;
     406              : }
     407              : 
     408              : 
     409              : static match
     410         8713 : match_data_constant (gfc_expr **result)
     411              : {
     412         8713 :   char name[GFC_MAX_SYMBOL_LEN + 1];
     413         8713 :   gfc_symbol *sym, *dt_sym = NULL;
     414         8713 :   gfc_expr *expr;
     415         8713 :   match m;
     416         8713 :   locus old_loc;
     417         8713 :   gfc_symtree *symtree;
     418              : 
     419         8713 :   m = gfc_match_literal_constant (&expr, 1);
     420         8713 :   if (m == MATCH_YES)
     421              :     {
     422         8368 :       *result = expr;
     423         8368 :       return MATCH_YES;
     424              :     }
     425              : 
     426          345 :   if (m == MATCH_ERROR)
     427              :     return MATCH_ERROR;
     428              : 
     429          337 :   m = gfc_match_null (result);
     430          337 :   if (m != MATCH_NO)
     431              :     return m;
     432              : 
     433          329 :   old_loc = gfc_current_locus;
     434              : 
     435              :   /* Should this be a structure component, try to match it
     436              :      before matching a name.  */
     437          329 :   m = gfc_match_rvalue (result);
     438          329 :   if (m == MATCH_ERROR)
     439              :     return m;
     440              : 
     441          329 :   if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
     442              :     {
     443            4 :       if (!gfc_simplify_expr (*result, 0))
     444            0 :         m = MATCH_ERROR;
     445              :       return m;
     446              :     }
     447          319 :   else if (m == MATCH_YES)
     448              :     {
     449              :       /* If a parameter inquiry ends up here, symtree is NULL but **result
     450              :          contains the right constant expression.  Check here.  */
     451          319 :       if ((*result)->symtree == NULL
     452           37 :           && (*result)->expr_type == EXPR_CONSTANT
     453           37 :           && ((*result)->ts.type == BT_INTEGER
     454            1 :               || (*result)->ts.type == BT_REAL))
     455              :         return m;
     456              : 
     457              :       /* F2018:R845 data-stmt-constant is initial-data-target.
     458              :          A data-stmt-constant shall be ... initial-data-target if and
     459              :          only if the corresponding data-stmt-object has the POINTER
     460              :          attribute. ...  If data-stmt-constant is initial-data-target
     461              :          the corresponding data statement object shall be
     462              :          data-pointer-initialization compatible (7.5.4.6) with the initial
     463              :          data target; the data statement object is initially associated
     464              :          with the target.  */
     465          283 :       if ((*result)->symtree
     466          282 :           && (*result)->symtree->n.sym->attr.save
     467          218 :           && (*result)->symtree->n.sym->attr.target)
     468              :         return m;
     469          250 :       gfc_free_expr (*result);
     470              :     }
     471              : 
     472          256 :   gfc_current_locus = old_loc;
     473              : 
     474          256 :   m = gfc_match_name (name);
     475          256 :   if (m != MATCH_YES)
     476              :     return m;
     477              : 
     478          250 :   if (gfc_find_sym_tree (name, NULL, 1, &symtree))
     479              :     return MATCH_ERROR;
     480              : 
     481          250 :   sym = symtree->n.sym;
     482              : 
     483          250 :   if (sym && sym->attr.generic)
     484           60 :     dt_sym = gfc_find_dt_in_generic (sym);
     485              : 
     486           60 :   if (sym == NULL
     487          250 :       || (sym->attr.flavor != FL_PARAMETER
     488           65 :           && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
     489              :     {
     490            5 :       gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
     491              :                  name);
     492            5 :       *result = NULL;
     493            5 :       return MATCH_ERROR;
     494              :     }
     495          245 :   else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
     496           60 :     return gfc_match_structure_constructor (dt_sym, symtree, result);
     497              : 
     498              :   /* Check to see if the value is an initialization array expression.  */
     499          185 :   if (sym->value->expr_type == EXPR_ARRAY)
     500              :     {
     501           67 :       gfc_current_locus = old_loc;
     502              : 
     503           67 :       m = gfc_match_init_expr (result);
     504           67 :       if (m == MATCH_ERROR)
     505              :         return m;
     506              : 
     507           66 :       if (m == MATCH_YES)
     508              :         {
     509           66 :           if (!gfc_simplify_expr (*result, 0))
     510            0 :             m = MATCH_ERROR;
     511              : 
     512           66 :           if ((*result)->expr_type == EXPR_CONSTANT)
     513              :             return m;
     514              :           else
     515              :             {
     516            2 :               gfc_error ("Invalid initializer %s in Data statement at %C", name);
     517            2 :               return MATCH_ERROR;
     518              :             }
     519              :         }
     520              :     }
     521              : 
     522          118 :   *result = gfc_copy_expr (sym->value);
     523          118 :   return MATCH_YES;
     524              : }
     525              : 
     526              : 
     527              : /* Match a list of values in a DATA statement.  The leading '/' has
     528              :    already been seen at this point.  */
     529              : 
     530              : static match
     531         2560 : top_val_list (gfc_data *data)
     532              : {
     533         2560 :   gfc_data_value *new_val, *tail;
     534         2560 :   gfc_expr *expr;
     535         2560 :   match m;
     536              : 
     537         2560 :   tail = NULL;
     538              : 
     539         8349 :   for (;;)
     540              :     {
     541         8349 :       m = match_data_constant (&expr);
     542         8349 :       if (m == MATCH_NO)
     543            3 :         goto syntax;
     544         8346 :       if (m == MATCH_ERROR)
     545              :         return MATCH_ERROR;
     546              : 
     547         8324 :       new_val = gfc_get_data_value ();
     548         8324 :       mpz_init (new_val->repeat);
     549              : 
     550         8324 :       if (tail == NULL)
     551         2535 :         data->value = new_val;
     552              :       else
     553         5789 :         tail->next = new_val;
     554              : 
     555         8324 :       tail = new_val;
     556              : 
     557         8324 :       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
     558              :         {
     559         8119 :           tail->expr = expr;
     560         8119 :           mpz_set_ui (tail->repeat, 1);
     561              :         }
     562              :       else
     563              :         {
     564          205 :           mpz_set (tail->repeat, expr->value.integer);
     565          205 :           gfc_free_expr (expr);
     566              : 
     567          205 :           m = match_data_constant (&tail->expr);
     568          205 :           if (m == MATCH_NO)
     569            0 :             goto syntax;
     570          205 :           if (m == MATCH_ERROR)
     571              :             return MATCH_ERROR;
     572              :         }
     573              : 
     574         8320 :       if (gfc_match_char ('/') == MATCH_YES)
     575              :         break;
     576         5790 :       if (gfc_match_char (',') == MATCH_NO)
     577            1 :         goto syntax;
     578              :     }
     579              : 
     580              :   return MATCH_YES;
     581              : 
     582            4 : syntax:
     583            4 :   gfc_syntax_error (ST_DATA);
     584            4 :   gfc_free_data_all (gfc_current_ns);
     585            4 :   return MATCH_ERROR;
     586              : }
     587              : 
     588              : 
     589              : /* Matches an old style initialization.  */
     590              : 
     591              : static match
     592           70 : match_old_style_init (const char *name)
     593              : {
     594           70 :   match m;
     595           70 :   gfc_symtree *st;
     596           70 :   gfc_symbol *sym;
     597           70 :   gfc_data *newdata, *nd;
     598              : 
     599              :   /* Set up data structure to hold initializers.  */
     600           70 :   gfc_find_sym_tree (name, NULL, 0, &st);
     601           70 :   sym = st->n.sym;
     602              : 
     603           70 :   newdata = gfc_get_data ();
     604           70 :   newdata->var = gfc_get_data_variable ();
     605           70 :   newdata->var->expr = gfc_get_variable_expr (st);
     606           70 :   newdata->var->expr->where = sym->declared_at;
     607           70 :   newdata->where = gfc_current_locus;
     608              : 
     609              :   /* Match initial value list. This also eats the terminal '/'.  */
     610           70 :   m = top_val_list (newdata);
     611           70 :   if (m != MATCH_YES)
     612              :     {
     613            1 :       free (newdata);
     614            1 :       return m;
     615              :     }
     616              : 
     617              :   /* Check that a BOZ did not creep into an old-style initialization.  */
     618          137 :   for (nd = newdata; nd; nd = nd->next)
     619              :     {
     620           69 :       if (nd->value->expr->ts.type == BT_BOZ
     621           69 :           && gfc_invalid_boz (G_("BOZ at %L cannot appear in an old-style "
     622              :                               "initialization"), &nd->value->expr->where))
     623              :         return MATCH_ERROR;
     624              : 
     625           68 :       if (nd->var->expr->ts.type != BT_INTEGER
     626           27 :           && nd->var->expr->ts.type != BT_REAL
     627           21 :           && nd->value->expr->ts.type == BT_BOZ)
     628              :         {
     629            0 :           gfc_error (G_("BOZ literal constant near %L cannot be assigned to "
     630              :                      "a %qs variable in an old-style initialization"),
     631            0 :                      &nd->value->expr->where,
     632              :                      gfc_typename (&nd->value->expr->ts));
     633            0 :           return MATCH_ERROR;
     634              :         }
     635              :     }
     636              : 
     637           68 :   if (gfc_pure (NULL))
     638              :     {
     639            1 :       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
     640            1 :       free (newdata);
     641            1 :       return MATCH_ERROR;
     642              :     }
     643           67 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
     644              : 
     645              :   /* Mark the variable as having appeared in a data statement.  */
     646           67 :   if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
     647              :     {
     648            2 :       free (newdata);
     649            2 :       return MATCH_ERROR;
     650              :     }
     651              : 
     652              :   /* Chain in namespace list of DATA initializers.  */
     653           65 :   newdata->next = gfc_current_ns->data;
     654           65 :   gfc_current_ns->data = newdata;
     655              : 
     656           65 :   return m;
     657              : }
     658              : 
     659              : 
     660              : /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
     661              :    we are matching a DATA statement and are therefore issuing an error
     662              :    if we encounter something unexpected, if not, we're trying to match
     663              :    an old-style initialization expression of the form INTEGER I /2/.  */
     664              : 
     665              : match
     666         2422 : gfc_match_data (void)
     667              : {
     668         2422 :   gfc_data *new_data;
     669         2422 :   gfc_expr *e;
     670         2422 :   gfc_ref *ref;
     671         2422 :   match m;
     672         2422 :   char c;
     673              : 
     674              :   /* DATA has been matched.  In free form source code, the next character
     675              :      needs to be whitespace or '(' from an implied do-loop.  Check that
     676              :      here.  */
     677         2422 :   c = gfc_peek_ascii_char ();
     678         2422 :   if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
     679              :     return MATCH_NO;
     680              : 
     681              :   /* Before parsing the rest of a DATA statement, check F2008:c1206.  */
     682         2421 :   if ((gfc_current_state () == COMP_FUNCTION
     683         2421 :        || gfc_current_state () == COMP_SUBROUTINE)
     684         1153 :       && gfc_state_stack->previous->state == COMP_INTERFACE)
     685              :     {
     686            1 :       gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
     687            1 :       return MATCH_ERROR;
     688              :     }
     689              : 
     690         2420 :   set_in_match_data (true);
     691              : 
     692         2614 :   for (;;)
     693              :     {
     694         2517 :       new_data = gfc_get_data ();
     695         2517 :       new_data->where = gfc_current_locus;
     696              : 
     697         2517 :       m = top_var_list (new_data);
     698         2517 :       if (m != MATCH_YES)
     699           18 :         goto cleanup;
     700              : 
     701         2499 :       if (new_data->var->iter.var
     702          117 :           && new_data->var->iter.var->ts.type == BT_INTEGER
     703           74 :           && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
     704           68 :           && new_data->var->list
     705           68 :           && new_data->var->list->expr
     706           55 :           && new_data->var->list->expr->ts.type == BT_CHARACTER
     707            3 :           && new_data->var->list->expr->ref
     708            3 :           && new_data->var->list->expr->ref->type == REF_SUBSTRING)
     709              :         {
     710            1 :           gfc_error ("Invalid substring in data-implied-do at %L in DATA "
     711              :                      "statement", &new_data->var->list->expr->where);
     712            1 :           goto cleanup;
     713              :         }
     714              : 
     715              :       /* Check for an entity with an allocatable component, which is not
     716              :          allowed.  */
     717         2498 :       e = new_data->var->expr;
     718         2498 :       if (e)
     719              :         {
     720         2382 :           bool invalid;
     721              : 
     722         2382 :           invalid = false;
     723         3606 :           for (ref = e->ref; ref; ref = ref->next)
     724         1224 :             if ((ref->type == REF_COMPONENT
     725          140 :                  && ref->u.c.component->attr.allocatable)
     726         1222 :                 || (ref->type == REF_ARRAY
     727         1034 :                     && e->symtree->n.sym->attr.pointer != 1
     728         1031 :                     && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
     729         1224 :               invalid = true;
     730              : 
     731         2382 :           if (invalid)
     732              :             {
     733            2 :               gfc_error ("Allocatable component or deferred-shaped array "
     734              :                          "near %C in DATA statement");
     735            2 :               goto cleanup;
     736              :             }
     737              : 
     738              :           /* F2008:C567 (R536) A data-i-do-object or a variable that appears
     739              :              as a data-stmt-object shall not be an object designator in which
     740              :              a pointer appears other than as the entire rightmost part-ref.  */
     741         2380 :           if (!e->ref && e->ts.type == BT_DERIVED
     742           43 :               && e->symtree->n.sym->attr.pointer)
     743            4 :             goto partref;
     744              : 
     745         2376 :           ref = e->ref;
     746         2376 :           if (e->symtree->n.sym->ts.type == BT_DERIVED
     747          125 :               && e->symtree->n.sym->attr.pointer
     748            1 :               && ref->type == REF_COMPONENT)
     749            1 :             goto partref;
     750              : 
     751         3591 :           for (; ref; ref = ref->next)
     752         1217 :             if (ref->type == REF_COMPONENT
     753          135 :                 && ref->u.c.component->attr.pointer
     754           27 :                 && ref->next)
     755            1 :               goto partref;
     756              :         }
     757              : 
     758         2490 :       m = top_val_list (new_data);
     759         2490 :       if (m != MATCH_YES)
     760           29 :         goto cleanup;
     761              : 
     762         2461 :       new_data->next = gfc_current_ns->data;
     763         2461 :       gfc_current_ns->data = new_data;
     764              : 
     765              :       /* A BOZ literal constant cannot appear in a structure constructor.
     766              :          Check for that here for a data statement value.  */
     767         2461 :       if (new_data->value->expr->ts.type == BT_DERIVED
     768           37 :           && new_data->value->expr->value.constructor)
     769              :         {
     770           35 :           gfc_constructor *c;
     771           35 :           c = gfc_constructor_first (new_data->value->expr->value.constructor);
     772          106 :           for (; c; c = gfc_constructor_next (c))
     773           36 :             if (c->expr && c->expr->ts.type == BT_BOZ)
     774              :               {
     775            0 :                 gfc_error ("BOZ literal constant at %L cannot appear in a "
     776              :                            "structure constructor", &c->expr->where);
     777            0 :                 return MATCH_ERROR;
     778              :               }
     779              :         }
     780              : 
     781         2461 :       if (gfc_match_eos () == MATCH_YES)
     782              :         break;
     783              : 
     784           97 :       gfc_match_char (',');     /* Optional comma */
     785           97 :     }
     786              : 
     787         2364 :   set_in_match_data (false);
     788              : 
     789         2364 :   if (gfc_pure (NULL))
     790              :     {
     791            0 :       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
     792            0 :       return MATCH_ERROR;
     793              :     }
     794         2364 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
     795              : 
     796         2364 :   return MATCH_YES;
     797              : 
     798            6 : partref:
     799              : 
     800            6 :   gfc_error ("part-ref with pointer attribute near %L is not "
     801              :              "rightmost part-ref of data-stmt-object",
     802              :              &e->where);
     803              : 
     804           56 : cleanup:
     805           56 :   set_in_match_data (false);
     806           56 :   gfc_free_data (new_data);
     807           56 :   return MATCH_ERROR;
     808              : }
     809              : 
     810              : 
     811              : /************************ Declaration statements *********************/
     812              : 
     813              : 
     814              : /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
     815              :    list). The difference here is the expression is a list of constants
     816              :    and is surrounded by '/'.
     817              :    The typespec ts must match the typespec of the variable which the
     818              :    clist is initializing.
     819              :    The arrayspec tells whether this should match a list of constants
     820              :    corresponding to array elements or a scalar (as == NULL).  */
     821              : 
     822              : static match
     823           74 : match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
     824              : {
     825           74 :   gfc_constructor_base array_head = NULL;
     826           74 :   gfc_expr *expr = NULL;
     827           74 :   match m = MATCH_ERROR;
     828           74 :   locus where;
     829           74 :   mpz_t repeat, cons_size, as_size;
     830           74 :   bool scalar;
     831           74 :   int cmp;
     832              : 
     833           74 :   gcc_assert (ts);
     834              : 
     835              :   /* We have already matched '/' - now look for a constant list, as with
     836              :      top_val_list from decl.cc, but append the result to an array.  */
     837           74 :   if (gfc_match ("/") == MATCH_YES)
     838              :     {
     839            1 :       gfc_error ("Empty old style initializer list at %C");
     840            1 :       return MATCH_ERROR;
     841              :     }
     842              : 
     843           73 :   where = gfc_current_locus;
     844           73 :   scalar = !as || !as->rank;
     845              : 
     846           42 :   if (!scalar && !spec_size (as, &as_size))
     847              :     {
     848            2 :       gfc_error ("Array in initializer list at %L must have an explicit shape",
     849            1 :                  as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
     850              :       /* Nothing to cleanup yet.  */
     851            1 :       return MATCH_ERROR;
     852              :     }
     853              : 
     854           72 :   mpz_init_set_ui (repeat, 0);
     855              : 
     856          143 :   for (;;)
     857              :     {
     858          143 :       m = match_data_constant (&expr);
     859          143 :       if (m != MATCH_YES)
     860            3 :         expr = NULL; /* match_data_constant may set expr to garbage */
     861            3 :       if (m == MATCH_NO)
     862            2 :         goto syntax;
     863          141 :       if (m == MATCH_ERROR)
     864            1 :         goto cleanup;
     865              : 
     866              :       /* Found r in repeat spec r*c; look for the constant to repeat.  */
     867          140 :       if ( gfc_match_char ('*') == MATCH_YES)
     868              :         {
     869           18 :           if (scalar)
     870              :             {
     871            1 :               gfc_error ("Repeat spec invalid in scalar initializer at %C");
     872            1 :               goto cleanup;
     873              :             }
     874           17 :           if (expr->ts.type != BT_INTEGER)
     875              :             {
     876            1 :               gfc_error ("Repeat spec must be an integer at %C");
     877            1 :               goto cleanup;
     878              :             }
     879           16 :           mpz_set (repeat, expr->value.integer);
     880           16 :           gfc_free_expr (expr);
     881           16 :           expr = NULL;
     882              : 
     883           16 :           m = match_data_constant (&expr);
     884           16 :           if (m == MATCH_NO)
     885              :             {
     886            1 :               m = MATCH_ERROR;
     887            1 :               gfc_error ("Expected data constant after repeat spec at %C");
     888              :             }
     889           16 :           if (m != MATCH_YES)
     890            1 :             goto cleanup;
     891              :         }
     892              :       /* No repeat spec, we matched the data constant itself. */
     893              :       else
     894          122 :         mpz_set_ui (repeat, 1);
     895              : 
     896          137 :       if (!scalar)
     897              :         {
     898              :           /* Add the constant initializer as many times as repeated. */
     899          251 :           for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
     900              :             {
     901              :               /* Make sure types of elements match */
     902          144 :               if(ts && !gfc_compare_types (&expr->ts, ts)
     903           12 :                     && !gfc_convert_type (expr, ts, 1))
     904            0 :                 goto cleanup;
     905              : 
     906          144 :               gfc_constructor_append_expr (&array_head,
     907              :                   gfc_copy_expr (expr), &gfc_current_locus);
     908              :             }
     909              : 
     910          107 :           gfc_free_expr (expr);
     911          107 :           expr = NULL;
     912              :         }
     913              : 
     914              :       /* For scalar initializers quit after one element.  */
     915              :       else
     916              :         {
     917           30 :           if(gfc_match_char ('/') != MATCH_YES)
     918              :             {
     919            1 :               gfc_error ("End of scalar initializer expected at %C");
     920            1 :               goto cleanup;
     921              :             }
     922              :           break;
     923              :         }
     924              : 
     925          107 :       if (gfc_match_char ('/') == MATCH_YES)
     926              :         break;
     927           72 :       if (gfc_match_char (',') == MATCH_NO)
     928            1 :         goto syntax;
     929              :     }
     930              : 
     931              :   /* If we break early from here out, we encountered an error.  */
     932           64 :   m = MATCH_ERROR;
     933              : 
     934              :   /* Set up expr as an array constructor. */
     935           64 :   if (!scalar)
     936              :     {
     937           35 :       expr = gfc_get_array_expr (ts->type, ts->kind, &where);
     938           35 :       expr->ts = *ts;
     939           35 :       expr->value.constructor = array_head;
     940              : 
     941              :       /* Validate sizes.  We built expr ourselves, so cons_size will be
     942              :          constant (we fail above for non-constant expressions).
     943              :          We still need to verify that the sizes match.  */
     944           35 :       gcc_assert (gfc_array_size (expr, &cons_size));
     945           35 :       cmp = mpz_cmp (cons_size, as_size);
     946           35 :       if (cmp < 0)
     947            2 :         gfc_error ("Not enough elements in array initializer at %C");
     948           33 :       else if (cmp > 0)
     949            3 :         gfc_error ("Too many elements in array initializer at %C");
     950           35 :       mpz_clear (cons_size);
     951           35 :       if (cmp)
     952            5 :         goto cleanup;
     953              : 
     954              :       /* Set the rank/shape to match the LHS as auto-reshape is implied. */
     955           30 :       expr->rank = as->rank;
     956           30 :       expr->corank = as->corank;
     957           30 :       expr->shape = gfc_get_shape (as->rank);
     958           66 :       for (int i = 0; i < as->rank; ++i)
     959           36 :         spec_dimen_size (as, i, &expr->shape[i]);
     960              :     }
     961              : 
     962              :   /* Make sure scalar types match. */
     963           29 :   else if (!gfc_compare_types (&expr->ts, ts)
     964           29 :            && !gfc_convert_type (expr, ts, 1))
     965            2 :     goto cleanup;
     966              : 
     967           57 :   if (expr->ts.u.cl)
     968            1 :     expr->ts.u.cl->length_from_typespec = 1;
     969              : 
     970           57 :   *result = expr;
     971           57 :   m = MATCH_YES;
     972           57 :   goto done;
     973              : 
     974            3 : syntax:
     975            3 :   m = MATCH_ERROR;
     976            3 :   gfc_error ("Syntax error in old style initializer list at %C");
     977              : 
     978           15 : cleanup:
     979           15 :   if (expr)
     980           10 :     expr->value.constructor = NULL;
     981           15 :   gfc_free_expr (expr);
     982           15 :   gfc_constructor_free (array_head);
     983              : 
     984           72 : done:
     985           72 :   mpz_clear (repeat);
     986           72 :   if (!scalar)
     987           41 :     mpz_clear (as_size);
     988              :   return m;
     989              : }
     990              : 
     991              : 
     992              : /* Auxiliary function to merge DIMENSION and CODIMENSION array specs.  */
     993              : 
     994              : static bool
     995          114 : merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
     996              : {
     997          114 :   if ((from->type == AS_ASSUMED_RANK && to->corank)
     998          112 :       || (to->type == AS_ASSUMED_RANK && from->corank))
     999              :     {
    1000            5 :       gfc_error ("The assumed-rank array at %C shall not have a codimension");
    1001            5 :       return false;
    1002              :     }
    1003              : 
    1004          109 :   if (to->rank == 0 && from->rank > 0)
    1005              :     {
    1006           48 :       to->rank = from->rank;
    1007           48 :       to->type = from->type;
    1008           48 :       to->cray_pointee = from->cray_pointee;
    1009           48 :       to->cp_was_assumed = from->cp_was_assumed;
    1010              : 
    1011          152 :       for (int i = to->corank - 1; i >= 0; i--)
    1012              :         {
    1013              :           /* Do not exceed the limits on lower[] and upper[].  gfortran
    1014              :              cleans up elsewhere.  */
    1015          104 :           int j = from->rank + i;
    1016          104 :           if (j >= GFC_MAX_DIMENSIONS)
    1017              :             break;
    1018              : 
    1019          104 :           to->lower[j] = to->lower[i];
    1020          104 :           to->upper[j] = to->upper[i];
    1021              :         }
    1022          115 :       for (int i = 0; i < from->rank; i++)
    1023              :         {
    1024           67 :           if (copy)
    1025              :             {
    1026           43 :               to->lower[i] = gfc_copy_expr (from->lower[i]);
    1027           43 :               to->upper[i] = gfc_copy_expr (from->upper[i]);
    1028              :             }
    1029              :           else
    1030              :             {
    1031           24 :               to->lower[i] = from->lower[i];
    1032           24 :               to->upper[i] = from->upper[i];
    1033              :             }
    1034              :         }
    1035              :     }
    1036           61 :   else if (to->corank == 0 && from->corank > 0)
    1037              :     {
    1038           34 :       to->corank = from->corank;
    1039           34 :       to->cotype = from->cotype;
    1040              : 
    1041          104 :       for (int i = 0; i < from->corank; i++)
    1042              :         {
    1043              :           /* Do not exceed the limits on lower[] and upper[].  gfortran
    1044              :              cleans up elsewhere.  */
    1045           71 :           int k = from->rank + i;
    1046           71 :           int j = to->rank + i;
    1047           71 :           if (j >= GFC_MAX_DIMENSIONS)
    1048              :             break;
    1049              : 
    1050           70 :           if (copy)
    1051              :             {
    1052           37 :               to->lower[j] = gfc_copy_expr (from->lower[k]);
    1053           37 :               to->upper[j] = gfc_copy_expr (from->upper[k]);
    1054              :             }
    1055              :           else
    1056              :             {
    1057           33 :               to->lower[j] = from->lower[k];
    1058           33 :               to->upper[j] = from->upper[k];
    1059              :             }
    1060              :         }
    1061              :     }
    1062              : 
    1063          109 :   if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
    1064              :     {
    1065            1 :       gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
    1066              :                  "allowed dimensions of %d",
    1067              :                  to->rank, to->corank, GFC_MAX_DIMENSIONS);
    1068            1 :       to->corank = GFC_MAX_DIMENSIONS - to->rank;
    1069            1 :       return false;
    1070              :     }
    1071              :   return true;
    1072              : }
    1073              : 
    1074              : 
    1075              : /* Match an intent specification.  Since this can only happen after an
    1076              :    INTENT word, a legal intent-spec must follow.  */
    1077              : 
    1078              : static sym_intent
    1079        28341 : match_intent_spec (void)
    1080              : {
    1081              : 
    1082        28341 :   if (gfc_match (" ( in out )") == MATCH_YES)
    1083              :     return INTENT_INOUT;
    1084        25142 :   if (gfc_match (" ( in )") == MATCH_YES)
    1085              :     return INTENT_IN;
    1086         3742 :   if (gfc_match (" ( out )") == MATCH_YES)
    1087              :     return INTENT_OUT;
    1088              : 
    1089            2 :   gfc_error ("Bad INTENT specification at %C");
    1090            2 :   return INTENT_UNKNOWN;
    1091              : }
    1092              : 
    1093              : 
    1094              : /* Matches a character length specification, which is either a
    1095              :    specification expression, '*', or ':'.  */
    1096              : 
    1097              : static match
    1098        27905 : char_len_param_value (gfc_expr **expr, bool *deferred)
    1099              : {
    1100        27905 :   match m;
    1101        27905 :   gfc_expr *p;
    1102              : 
    1103        27905 :   *expr = NULL;
    1104        27905 :   *deferred = false;
    1105              : 
    1106        27905 :   if (gfc_match_char ('*') == MATCH_YES)
    1107              :     return MATCH_YES;
    1108              : 
    1109        21374 :   if (gfc_match_char (':') == MATCH_YES)
    1110              :     {
    1111         3384 :       if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
    1112              :         return MATCH_ERROR;
    1113              : 
    1114         3382 :       *deferred = true;
    1115              : 
    1116         3382 :       return MATCH_YES;
    1117              :     }
    1118              : 
    1119        17990 :   m = gfc_match_expr (expr);
    1120              : 
    1121        17990 :   if (m == MATCH_NO || m == MATCH_ERROR)
    1122              :     return m;
    1123              : 
    1124        17985 :   if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
    1125              :     return MATCH_ERROR;
    1126              : 
    1127              :   /* Try to simplify the expression to catch things like CHARACTER(([1])).   */
    1128        17979 :   p = gfc_copy_expr (*expr);
    1129        17979 :   if (gfc_is_constant_expr (p) && gfc_simplify_expr (p, 1))
    1130        14935 :     gfc_replace_expr (*expr, p);
    1131              :   else
    1132         3044 :     gfc_free_expr (p);
    1133              : 
    1134        17979 :   if ((*expr)->expr_type == EXPR_FUNCTION)
    1135              :     {
    1136         1021 :       if ((*expr)->ts.type == BT_INTEGER
    1137         1020 :           || ((*expr)->ts.type == BT_UNKNOWN
    1138         1020 :               && strcmp((*expr)->symtree->name, "null") != 0))
    1139              :         return MATCH_YES;
    1140              : 
    1141            2 :       goto syntax;
    1142              :     }
    1143        16958 :   else if ((*expr)->expr_type == EXPR_CONSTANT)
    1144              :     {
    1145              :       /* F2008, 4.4.3.1:  The length is a type parameter; its kind is
    1146              :          processor dependent and its value is greater than or equal to zero.
    1147              :          F2008, 4.4.3.2:  If the character length parameter value evaluates
    1148              :          to a negative value, the length of character entities declared
    1149              :          is zero.  */
    1150              : 
    1151        14863 :       if ((*expr)->ts.type == BT_INTEGER)
    1152              :         {
    1153        14845 :           if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
    1154            4 :             mpz_set_si ((*expr)->value.integer, 0);
    1155              :         }
    1156              :       else
    1157           18 :         goto syntax;
    1158              :     }
    1159         2095 :   else if ((*expr)->expr_type == EXPR_ARRAY)
    1160            8 :     goto syntax;
    1161         2087 :   else if ((*expr)->expr_type == EXPR_VARIABLE)
    1162              :     {
    1163         1516 :       bool t;
    1164         1516 :       gfc_expr *e;
    1165              : 
    1166         1516 :       e = gfc_copy_expr (*expr);
    1167              : 
    1168              :       /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
    1169              :          which causes an ICE if gfc_reduce_init_expr() is called.  */
    1170         1516 :       if (e->ref && e->ref->type == REF_ARRAY
    1171            8 :           && e->ref->u.ar.type == AR_UNKNOWN
    1172            7 :           && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
    1173            2 :         goto syntax;
    1174              : 
    1175         1514 :       t = gfc_reduce_init_expr (e);
    1176              : 
    1177         1514 :       if (!t && e->ts.type == BT_UNKNOWN
    1178            7 :           && e->symtree->n.sym->attr.untyped == 1
    1179            7 :           && (flag_implicit_none
    1180            5 :               || e->symtree->n.sym->ns->seen_implicit_none == 1
    1181            1 :               || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
    1182              :         {
    1183            7 :           gfc_free_expr (e);
    1184            7 :           goto syntax;
    1185              :         }
    1186              : 
    1187         1507 :       if ((e->ref && e->ref->type == REF_ARRAY
    1188            4 :            && e->ref->u.ar.type != AR_ELEMENT)
    1189         1506 :           || (!e->ref && e->expr_type == EXPR_ARRAY))
    1190              :         {
    1191            2 :           gfc_free_expr (e);
    1192            2 :           goto syntax;
    1193              :         }
    1194              : 
    1195         1505 :       gfc_free_expr (e);
    1196              :     }
    1197              : 
    1198        16921 :   if (gfc_seen_div0)
    1199           52 :     m = MATCH_ERROR;
    1200              : 
    1201              :   return m;
    1202              : 
    1203           39 : syntax:
    1204           39 :   gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
    1205           39 :   return MATCH_ERROR;
    1206              : }
    1207              : 
    1208              : 
    1209              : /* A character length is a '*' followed by a literal integer or a
    1210              :    char_len_param_value in parenthesis.  */
    1211              : 
    1212              : static match
    1213        63183 : match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
    1214              : {
    1215        63183 :   int length;
    1216        63183 :   match m;
    1217              : 
    1218        63183 :   *deferred = false;
    1219        63183 :   m = gfc_match_char ('*');
    1220        63183 :   if (m != MATCH_YES)
    1221              :     return m;
    1222              : 
    1223         2641 :   m = gfc_match_small_literal_int (&length, NULL);
    1224         2641 :   if (m == MATCH_ERROR)
    1225              :     return m;
    1226              : 
    1227         2641 :   if (m == MATCH_YES)
    1228              :     {
    1229         2137 :       if (obsolescent_check
    1230         2137 :           && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
    1231              :         return MATCH_ERROR;
    1232         2137 :       *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
    1233         2137 :       return m;
    1234              :     }
    1235              : 
    1236          504 :   if (gfc_match_char ('(') == MATCH_NO)
    1237            0 :     goto syntax;
    1238              : 
    1239          504 :   m = char_len_param_value (expr, deferred);
    1240          504 :   if (m != MATCH_YES && gfc_matching_function)
    1241              :     {
    1242            0 :       gfc_undo_symbols ();
    1243            0 :       m = MATCH_YES;
    1244              :     }
    1245              : 
    1246            1 :   if (m == MATCH_ERROR)
    1247              :     return m;
    1248          503 :   if (m == MATCH_NO)
    1249            0 :     goto syntax;
    1250              : 
    1251          503 :   if (gfc_match_char (')') == MATCH_NO)
    1252              :     {
    1253            0 :       gfc_free_expr (*expr);
    1254            0 :       *expr = NULL;
    1255            0 :       goto syntax;
    1256              :     }
    1257              : 
    1258          503 :   if (obsolescent_check
    1259          503 :       && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
    1260            0 :     return MATCH_ERROR;
    1261              : 
    1262              :   return MATCH_YES;
    1263              : 
    1264            0 : syntax:
    1265            0 :   gfc_error ("Syntax error in character length specification at %C");
    1266            0 :   return MATCH_ERROR;
    1267              : }
    1268              : 
    1269              : 
    1270              : /* Special subroutine for finding a symbol.  Check if the name is found
    1271              :    in the current name space.  If not, and we're compiling a function or
    1272              :    subroutine and the parent compilation unit is an interface, then check
    1273              :    to see if the name we've been given is the name of the interface
    1274              :    (located in another namespace).  */
    1275              : 
    1276              : static int
    1277       286470 : find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
    1278              : {
    1279       286470 :   gfc_state_data *s;
    1280       286470 :   gfc_symtree *st;
    1281       286470 :   int i;
    1282              : 
    1283       286470 :   i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
    1284       286470 :   if (i == 0)
    1285              :     {
    1286       286470 :       *result = st ? st->n.sym : NULL;
    1287       286470 :       goto end;
    1288              :     }
    1289              : 
    1290            0 :   if (gfc_current_state () != COMP_SUBROUTINE
    1291            0 :       && gfc_current_state () != COMP_FUNCTION)
    1292            0 :     goto end;
    1293              : 
    1294            0 :   s = gfc_state_stack->previous;
    1295            0 :   if (s == NULL)
    1296            0 :     goto end;
    1297              : 
    1298            0 :   if (s->state != COMP_INTERFACE)
    1299            0 :     goto end;
    1300            0 :   if (s->sym == NULL)
    1301            0 :     goto end;             /* Nameless interface.  */
    1302              : 
    1303            0 :   if (strcmp (name, s->sym->name) == 0)
    1304              :     {
    1305            0 :       *result = s->sym;
    1306            0 :       return 0;
    1307              :     }
    1308              : 
    1309            0 : end:
    1310              :   return i;
    1311              : }
    1312              : 
    1313              : 
    1314              : /* Special subroutine for getting a symbol node associated with a
    1315              :    procedure name, used in SUBROUTINE and FUNCTION statements.  The
    1316              :    symbol is created in the parent using with symtree node in the
    1317              :    child unit pointing to the symbol.  If the current namespace has no
    1318              :    parent, then the symbol is just created in the current unit.  */
    1319              : 
    1320              : static int
    1321        64867 : get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
    1322              : {
    1323        64867 :   gfc_symtree *st;
    1324        64867 :   gfc_symbol *sym;
    1325        64867 :   int rc = 0;
    1326              : 
    1327              :   /* Module functions have to be left in their own namespace because
    1328              :      they have potentially (almost certainly!) already been referenced.
    1329              :      In this sense, they are rather like external functions.  This is
    1330              :      fixed up in resolve.cc(resolve_entries), where the symbol name-
    1331              :      space is set to point to the master function, so that the fake
    1332              :      result mechanism can work.  */
    1333        64867 :   if (module_fcn_entry)
    1334              :     {
    1335              :       /* Present if entry is declared to be a module procedure.  */
    1336          260 :       rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
    1337              : 
    1338          260 :       if (*result == NULL)
    1339          217 :         rc = gfc_get_symbol (name, NULL, result);
    1340           86 :       else if (!gfc_get_symbol (name, NULL, &sym) && sym
    1341           43 :                  && (*result)->ts.type == BT_UNKNOWN
    1342           86 :                  && sym->attr.flavor == FL_UNKNOWN)
    1343              :         /* Pick up the typespec for the entry, if declared in the function
    1344              :            body.  Note that this symbol is FL_UNKNOWN because it will
    1345              :            only have appeared in a type declaration.  The local symtree
    1346              :            is set to point to the module symbol and a unique symtree
    1347              :            to the local version.  This latter ensures a correct clearing
    1348              :            of the symbols.  */
    1349              :         {
    1350              :           /* If the ENTRY proceeds its specification, we need to ensure
    1351              :              that this does not raise a "has no IMPLICIT type" error.  */
    1352           43 :           if (sym->ts.type == BT_UNKNOWN)
    1353           23 :             sym->attr.untyped = 1;
    1354              : 
    1355           43 :           (*result)->ts = sym->ts;
    1356              : 
    1357              :           /* Put the symbol in the procedure namespace so that, should
    1358              :              the ENTRY precede its specification, the specification
    1359              :              can be applied.  */
    1360           43 :           (*result)->ns = gfc_current_ns;
    1361              : 
    1362           43 :           gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
    1363           43 :           st->n.sym = *result;
    1364           43 :           st = gfc_get_unique_symtree (gfc_current_ns);
    1365           43 :           sym->refs++;
    1366           43 :           st->n.sym = sym;
    1367              :         }
    1368              :     }
    1369              :   else
    1370        64607 :     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
    1371              : 
    1372        64867 :   if (rc)
    1373              :     return rc;
    1374              : 
    1375        64866 :   sym = *result;
    1376        64866 :   if (sym->attr.proc == PROC_ST_FUNCTION)
    1377              :     return rc;
    1378              : 
    1379        64865 :   if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
    1380              :     {
    1381              :       /* Create a partially populated interface symbol to carry the
    1382              :          characteristics of the procedure and the result.  */
    1383          472 :       sym->tlink = gfc_new_symbol (name, sym->ns);
    1384          472 :       gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
    1385          472 :       gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
    1386          472 :       if (sym->attr.dimension)
    1387           17 :         sym->tlink->as = gfc_copy_array_spec (sym->as);
    1388              : 
    1389              :       /* Ideally, at this point, a copy would be made of the formal
    1390              :          arguments and their namespace. However, this does not appear
    1391              :          to be necessary, albeit at the expense of not being able to
    1392              :          use gfc_compare_interfaces directly.  */
    1393              : 
    1394          472 :       if (sym->result && sym->result != sym)
    1395              :         {
    1396          105 :           sym->tlink->result = sym->result;
    1397          105 :           sym->result = NULL;
    1398              :         }
    1399          367 :       else if (sym->result)
    1400              :         {
    1401           93 :           sym->tlink->result = sym->tlink;
    1402              :         }
    1403              :     }
    1404        64393 :   else if (sym && !sym->gfc_new
    1405        24854 :            && gfc_current_state () != COMP_INTERFACE)
    1406              :     {
    1407              :       /* Trap another encompassed procedure with the same name.  All
    1408              :          these conditions are necessary to avoid picking up an entry
    1409              :          whose name clashes with that of the encompassing procedure;
    1410              :          this is handled using gsymbols to register unique, globally
    1411              :          accessible names.  */
    1412        23519 :       if (sym->attr.flavor != 0
    1413        21436 :           && sym->attr.proc != 0
    1414         2398 :           && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
    1415            7 :           && sym->attr.if_source != IFSRC_UNKNOWN)
    1416              :         {
    1417            7 :           gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1418              :                          name, &sym->declared_at);
    1419            7 :           return true;
    1420              :         }
    1421        23512 :       if (sym->attr.flavor != 0
    1422        21429 :           && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
    1423              :         {
    1424            1 :           gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1425              :                          name, &sym->declared_at);
    1426            1 :           return true;
    1427              :         }
    1428              : 
    1429        23511 :       if (sym->attr.external && sym->attr.procedure
    1430            2 :           && gfc_current_state () == COMP_CONTAINS)
    1431              :         {
    1432            1 :           gfc_error_now ("Contained procedure %qs at %C clashes with "
    1433              :                          "procedure defined at %L",
    1434              :                          name, &sym->declared_at);
    1435            1 :           return true;
    1436              :         }
    1437              : 
    1438              :       /* Trap a procedure with a name the same as interface in the
    1439              :          encompassing scope.  */
    1440        23510 :       if (sym->attr.generic != 0
    1441           60 :           && (sym->attr.subroutine || sym->attr.function)
    1442            1 :           && !sym->attr.mod_proc)
    1443              :         {
    1444            1 :           gfc_error_now ("Name %qs at %C is already defined"
    1445              :                          " as a generic interface at %L",
    1446              :                          name, &sym->declared_at);
    1447            1 :           return true;
    1448              :         }
    1449              : 
    1450              :       /* Trap declarations of attributes in encompassing scope.  The
    1451              :          signature for this is that ts.kind is nonzero for no-CLASS
    1452              :          entity.  For a CLASS entity, ts.kind is zero.  */
    1453        23509 :       if ((sym->ts.kind != 0
    1454        23136 :            || sym->ts.type == BT_CLASS
    1455        23135 :            || sym->ts.type == BT_DERIVED)
    1456          397 :           && !sym->attr.implicit_type
    1457          396 :           && sym->attr.proc == 0
    1458          378 :           && gfc_current_ns->parent != NULL
    1459          138 :           && sym->attr.access == 0
    1460          136 :           && !module_fcn_entry)
    1461              :         {
    1462            5 :           gfc_error_now ("Procedure %qs at %C has an explicit interface "
    1463              :                        "from a previous declaration",  name);
    1464            5 :           return true;
    1465              :         }
    1466              :     }
    1467              : 
    1468              :   /* F2023: C1247 (R1526) MODULE shall appear only in the function-stmt or
    1469              :      subroutine-stmt of a module subprogram or of a nonabstract interface
    1470              :      body that is declared in the scoping unit of a module or submodule.  */
    1471        64850 :   if (sym->attr.external
    1472           92 :       && (sym->attr.subroutine || sym->attr.function)
    1473           91 :       && sym->attr.if_source == IFSRC_IFBODY
    1474           91 :       && !current_attr.module_procedure
    1475            3 :       && sym->attr.proc == PROC_MODULE
    1476            3 :       && gfc_state_stack->state == COMP_CONTAINS)
    1477            1 :     gfc_error_now ("Procedure %qs defined in interface body at %L "
    1478              :                    "clashes with internal procedure defined at %C",
    1479              :                    name, &sym->declared_at);
    1480              : 
    1481              :   /* This is the converse requirement: The separate-module-subprogram for a
    1482              :      module procedure shall have the MODULE prefix or be declared a MODULE
    1483              :      PROCEDURE, otherwise it would be ambiguous.  */
    1484        64850 :   if (sym->attr.module_procedure
    1485          472 :       && (sym->attr.subroutine || sym->attr.function)
    1486          472 :       && sym->attr.if_source == IFSRC_IFBODY
    1487          472 :       && !current_attr.module_procedure
    1488            4 :       && sym->attr.proc == PROC_MODULE
    1489            4 :       && gfc_state_stack->state == COMP_CONTAINS
    1490            2 :       && gfc_state_stack->previous
    1491            2 :       && gfc_state_stack->previous->state == COMP_SUBMODULE)
    1492            1 :     gfc_error_now ("Procedure %qs at %C requires the MODULE prefix because "
    1493              :                    "it is a module procedure declared in module %qs",
    1494            1 :                    name, sym->module ? sym->module : "");
    1495              : 
    1496        64850 :   if (sym && !sym->gfc_new
    1497        25311 :       && sym->attr.flavor != FL_UNKNOWN
    1498        22828 :       && sym->attr.referenced == 0 && sym->attr.subroutine == 1
    1499          244 :       && gfc_state_stack->state == COMP_CONTAINS
    1500          239 :       && gfc_state_stack->previous->state == COMP_SUBROUTINE)
    1501              :     {
    1502            1 :       gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1503              :                      name, &sym->declared_at);
    1504            1 :       return true;
    1505              :     }
    1506              : 
    1507        64849 :   if (gfc_current_ns->parent == NULL || *result == NULL)
    1508              :     return rc;
    1509              : 
    1510              :   /* Module function entries will already have a symtree in
    1511              :      the current namespace but will need one at module level.  */
    1512        52522 :   if (module_fcn_entry)
    1513              :     {
    1514              :       /* Present if entry is declared to be a module procedure.  */
    1515          258 :       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
    1516          258 :       if (st == NULL)
    1517          217 :         st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
    1518              :     }
    1519              :   else
    1520        52264 :     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
    1521              : 
    1522        52522 :   st->n.sym = sym;
    1523        52522 :   sym->refs++;
    1524              : 
    1525              :   /* See if the procedure should be a module procedure.  */
    1526              : 
    1527        52522 :   if (((sym->ns->proc_name != NULL
    1528        52522 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
    1529        21331 :         && sym->attr.proc != PROC_MODULE)
    1530        52522 :        || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
    1531        71012 :       && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
    1532              :     rc = 2;
    1533              : 
    1534              :   return rc;
    1535              : }
    1536              : 
    1537              : 
    1538              : /* Verify that the given symbol representing a parameter is C
    1539              :    interoperable, by checking to see if it was marked as such after
    1540              :    its declaration.  If the given symbol is not interoperable, a
    1541              :    warning is reported, thus removing the need to return the status to
    1542              :    the calling function.  The standard does not require the user use
    1543              :    one of the iso_c_binding named constants to declare an
    1544              :    interoperable parameter, but we can't be sure if the param is C
    1545              :    interop or not if the user doesn't.  For example, integer(4) may be
    1546              :    legal Fortran, but doesn't have meaning in C.  It may interop with
    1547              :    a number of the C types, which causes a problem because the
    1548              :    compiler can't know which one.  This code is almost certainly not
    1549              :    portable, and the user will get what they deserve if the C type
    1550              :    across platforms isn't always interoperable with integer(4).  If
    1551              :    the user had used something like integer(c_int) or integer(c_long),
    1552              :    the compiler could have automatically handled the varying sizes
    1553              :    across platforms.  */
    1554              : 
    1555              : bool
    1556        17316 : gfc_verify_c_interop_param (gfc_symbol *sym)
    1557              : {
    1558        17316 :   int is_c_interop = 0;
    1559        17316 :   bool retval = true;
    1560              : 
    1561              :   /* We check implicitly typed variables in symbol.cc:gfc_set_default_type().
    1562              :      Don't repeat the checks here.  */
    1563        17316 :   if (sym->attr.implicit_type)
    1564              :     return true;
    1565              : 
    1566              :   /* For subroutines or functions that are passed to a BIND(C) procedure,
    1567              :      they're interoperable if they're BIND(C) and their params are all
    1568              :      interoperable.  */
    1569        17316 :   if (sym->attr.flavor == FL_PROCEDURE)
    1570              :     {
    1571            4 :       if (sym->attr.is_bind_c == 0)
    1572              :         {
    1573            0 :           gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
    1574              :                          "attribute to be C interoperable", sym->name,
    1575              :                          &(sym->declared_at));
    1576            0 :           return false;
    1577              :         }
    1578              :       else
    1579              :         {
    1580            4 :           if (sym->attr.is_c_interop == 1)
    1581              :             /* We've already checked this procedure; don't check it again.  */
    1582              :             return true;
    1583              :           else
    1584            4 :             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
    1585            4 :                                       sym->common_block);
    1586              :         }
    1587              :     }
    1588              : 
    1589              :   /* See if we've stored a reference to a procedure that owns sym.  */
    1590        17312 :   if (sym->ns != NULL && sym->ns->proc_name != NULL)
    1591              :     {
    1592        17312 :       if (sym->ns->proc_name->attr.is_bind_c == 1)
    1593              :         {
    1594        17273 :           bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;
    1595        17273 :           bool f2018_added = false;
    1596              : 
    1597        17273 :           is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
    1598              : 
    1599              :           /* F2018:18.3.6 has the following text:
    1600              :              "(5) any dummy argument without the VALUE attribute corresponds to
    1601              :              a formal parameter of the prototype that is of a pointer type, and
    1602              :              either
    1603              :              • the dummy argument is interoperable with an entity of the
    1604              :              referenced type (ISO/IEC 9899:2011, 6.2.5, 7.19, and 7.20.1) of
    1605              :              the formal parameter (this is equivalent to the F2008 text),
    1606              :              • the dummy argument is a nonallocatable nonpointer variable of
    1607              :              type CHARACTER with assumed character length and the formal
    1608              :              parameter is a pointer to CFI_cdesc_t,
    1609              :              • the dummy argument is allocatable, assumed-shape, assumed-rank,
    1610              :              or a pointer without the CONTIGUOUS attribute, and the formal
    1611              :              parameter is a pointer to CFI_cdesc_t, or
    1612              :              • the dummy argument is assumed-type and not allocatable,
    1613              :              assumed-shape, assumed-rank, or a pointer, and the formal
    1614              :              parameter is a pointer to void,"  */
    1615         3731 :           if (is_c_interop == 0 && !sym->attr.value && f2018_allowed)
    1616              :             {
    1617         2364 :               bool as_ar = (sym->as
    1618         2364 :                             && (sym->as->type == AS_ASSUMED_SHAPE
    1619         2117 :                                 || sym->as->type == AS_ASSUMED_RANK));
    1620         4728 :               bool cond1 = (sym->ts.type == BT_CHARACTER
    1621         1565 :                             && !(sym->ts.u.cl && sym->ts.u.cl->length)
    1622          905 :                             && !sym->attr.allocatable
    1623         3251 :                             && !sym->attr.pointer);
    1624         4728 :               bool cond2 = (sym->attr.allocatable
    1625         2267 :                             || as_ar
    1626         3389 :                             || (IS_POINTER (sym) && !sym->attr.contiguous));
    1627         4728 :               bool cond3 = (sym->ts.type == BT_ASSUMED
    1628            0 :                             && !sym->attr.allocatable
    1629            0 :                             && !sym->attr.pointer
    1630         2364 :                             && !as_ar);
    1631         2364 :               f2018_added = cond1 || cond2 || cond3;
    1632              :             }
    1633              : 
    1634        17273 :           if (is_c_interop != 1 && !f2018_added)
    1635              :             {
    1636              :               /* Make personalized messages to give better feedback.  */
    1637         1837 :               if (sym->ts.type == BT_DERIVED)
    1638            1 :                 gfc_error ("Variable %qs at %L is a dummy argument to the "
    1639              :                            "BIND(C) procedure %qs but is not C interoperable "
    1640              :                            "because derived type %qs is not C interoperable",
    1641              :                            sym->name, &(sym->declared_at),
    1642            1 :                            sym->ns->proc_name->name,
    1643            1 :                            sym->ts.u.derived->name);
    1644         1836 :               else if (sym->ts.type == BT_CLASS)
    1645            6 :                 gfc_error ("Variable %qs at %L is a dummy argument to the "
    1646              :                            "BIND(C) procedure %qs but is not C interoperable "
    1647              :                            "because it is polymorphic",
    1648              :                            sym->name, &(sym->declared_at),
    1649            6 :                            sym->ns->proc_name->name);
    1650         1830 :               else if (warn_c_binding_type)
    1651           39 :                 gfc_warning (OPT_Wc_binding_type,
    1652              :                              "Variable %qs at %L is a dummy argument of the "
    1653              :                              "BIND(C) procedure %qs but may not be C "
    1654              :                              "interoperable",
    1655              :                              sym->name, &(sym->declared_at),
    1656           39 :                              sym->ns->proc_name->name);
    1657              :             }
    1658              : 
    1659              :           /* Per F2018, 18.3.6 (5), pointer + contiguous is not permitted.  */
    1660        17273 :           if (sym->attr.pointer && sym->attr.contiguous)
    1661            2 :             gfc_error ("Dummy argument %qs at %L may not be a pointer with "
    1662              :                        "CONTIGUOUS attribute as procedure %qs is BIND(C)",
    1663            2 :                        sym->name, &sym->declared_at, sym->ns->proc_name->name);
    1664              : 
    1665              :           /* Per F2018, C1557, pointer/allocatable dummies to a bind(c)
    1666              :              procedure that are default-initialized are not permitted.  */
    1667        16633 :           if ((sym->attr.pointer || sym->attr.allocatable)
    1668         1041 :               && sym->ts.type == BT_DERIVED
    1669        17651 :               && gfc_has_default_initializer (sym->ts.u.derived))
    1670              :             {
    1671            8 :               gfc_error ("Default-initialized dummy argument %qs with %s "
    1672              :                          "attribute at %L is not permitted in BIND(C) "
    1673              :                          "procedure %qs", sym->name,
    1674            4 :                          (sym->attr.pointer ? "POINTER" : "ALLOCATABLE"),
    1675            4 :                          &sym->declared_at, sym->ns->proc_name->name);
    1676            4 :               retval = false;
    1677              :             }
    1678              : 
    1679              :           /* Character strings are only C interoperable if they have a
    1680              :              length of 1.  However, as an argument they are also interoperable
    1681              :              when passed as descriptor (which requires len=: or len=*).  */
    1682        17273 :           if (sym->ts.type == BT_CHARACTER)
    1683              :             {
    1684         2344 :               gfc_charlen *cl = sym->ts.u.cl;
    1685              : 
    1686         2344 :               if (sym->attr.allocatable || sym->attr.pointer)
    1687              :                 {
    1688              :                   /* F2018, 18.3.6 (6).  */
    1689          195 :                   if (!sym->ts.deferred)
    1690              :                     {
    1691           64 :                       if (sym->attr.allocatable)
    1692           32 :                         gfc_error ("Allocatable character dummy argument %qs "
    1693              :                                    "at %L must have deferred length as "
    1694              :                                    "procedure %qs is BIND(C)", sym->name,
    1695           32 :                                    &sym->declared_at, sym->ns->proc_name->name);
    1696              :                       else
    1697           32 :                         gfc_error ("Pointer character dummy argument %qs at %L "
    1698              :                                    "must have deferred length as procedure %qs "
    1699              :                                    "is BIND(C)", sym->name, &sym->declared_at,
    1700           32 :                                    sym->ns->proc_name->name);
    1701              :                       retval = false;
    1702              :                     }
    1703          131 :                   else if (!gfc_notify_std (GFC_STD_F2018,
    1704              :                                             "Deferred-length character dummy "
    1705              :                                             "argument %qs at %L of procedure "
    1706              :                                             "%qs with BIND(C) attribute",
    1707              :                                             sym->name, &sym->declared_at,
    1708          131 :                                             sym->ns->proc_name->name))
    1709        17273 :                     retval = false;
    1710              :                 }
    1711         2149 :               else if (sym->attr.value
    1712          354 :                        && (!cl || !cl->length
    1713          354 :                            || cl->length->expr_type != EXPR_CONSTANT
    1714          354 :                            || mpz_cmp_si (cl->length->value.integer, 1) != 0))
    1715              :                 {
    1716            1 :                   gfc_error ("Character dummy argument %qs at %L must be "
    1717              :                              "of length 1 as it has the VALUE attribute",
    1718              :                              sym->name, &sym->declared_at);
    1719            1 :                   retval = false;
    1720              :                 }
    1721         2148 :               else if (!cl || !cl->length)
    1722              :                 {
    1723              :                   /* Assumed length; F2018, 18.3.6 (5)(2).
    1724              :                      Uses the CFI array descriptor - also for scalars and
    1725              :                      explicit-size/assumed-size arrays.  */
    1726          959 :                   if (!gfc_notify_std (GFC_STD_F2018,
    1727              :                                       "Assumed-length character dummy argument "
    1728              :                                       "%qs at %L of procedure %qs with BIND(C) "
    1729              :                                       "attribute", sym->name, &sym->declared_at,
    1730          959 :                                       sym->ns->proc_name->name))
    1731        17273 :                     retval = false;
    1732              :                 }
    1733         1189 :               else if (cl->length->expr_type != EXPR_CONSTANT
    1734          875 :                        || mpz_cmp_si (cl->length->value.integer, 1) != 0)
    1735              :                 {
    1736              :                   /* F2018, 18.3.6, (5), item 4.  */
    1737          653 :                   if (!sym->attr.dimension
    1738          645 :                       || sym->as->type == AS_ASSUMED_SIZE
    1739          639 :                       || sym->as->type == AS_EXPLICIT)
    1740              :                     {
    1741           20 :                       gfc_error ("Character dummy argument %qs at %L must be "
    1742              :                                  "of constant length of one or assumed length, "
    1743              :                                  "unless it has assumed shape or assumed rank, "
    1744              :                                  "as procedure %qs has the BIND(C) attribute",
    1745              :                                  sym->name, &sym->declared_at,
    1746           20 :                                  sym->ns->proc_name->name);
    1747           20 :                       retval = false;
    1748              :                     }
    1749              :                   /* else: valid only since F2018 - and an assumed-shape/rank
    1750              :                      array; however, gfc_notify_std is already called when
    1751              :                      those array types are used. Thus, silently accept F200x. */
    1752              :                 }
    1753              :             }
    1754              : 
    1755              :           /* We have to make sure that any param to a bind(c) routine does
    1756              :              not have the allocatable, pointer, or optional attributes,
    1757              :              according to J3/04-007, section 5.1.  */
    1758        17273 :           if (sym->attr.allocatable == 1
    1759        17674 :               && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
    1760              :                                   "ALLOCATABLE attribute in procedure %qs "
    1761              :                                   "with BIND(C)", sym->name,
    1762              :                                   &(sym->declared_at),
    1763          401 :                                   sym->ns->proc_name->name))
    1764              :             retval = false;
    1765              : 
    1766        17273 :           if (sym->attr.pointer == 1
    1767        17913 :               && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
    1768              :                                   "POINTER attribute in procedure %qs "
    1769              :                                   "with BIND(C)", sym->name,
    1770              :                                   &(sym->declared_at),
    1771          640 :                                   sym->ns->proc_name->name))
    1772              :             retval = false;
    1773              : 
    1774        17273 :           if (sym->attr.optional == 1 && sym->attr.value)
    1775              :             {
    1776            9 :               gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
    1777              :                          "and the VALUE attribute because procedure %qs "
    1778              :                          "is BIND(C)", sym->name, &(sym->declared_at),
    1779            9 :                          sym->ns->proc_name->name);
    1780            9 :               retval = false;
    1781              :             }
    1782        17264 :           else if (sym->attr.optional == 1
    1783        18218 :                    && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
    1784              :                                        "at %L with OPTIONAL attribute in "
    1785              :                                        "procedure %qs which is BIND(C)",
    1786              :                                        sym->name, &(sym->declared_at),
    1787          954 :                                        sym->ns->proc_name->name))
    1788              :             retval = false;
    1789              : 
    1790              :           /* Make sure that if it has the dimension attribute, that it is
    1791              :              either assumed size or explicit shape. Deferred shape is already
    1792              :              covered by the pointer/allocatable attribute.  */
    1793         5551 :           if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
    1794        18606 :               && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
    1795              :                                   "at %L as dummy argument to the BIND(C) "
    1796              :                                   "procedure %qs at %L", sym->name,
    1797              :                                   &(sym->declared_at),
    1798              :                                   sym->ns->proc_name->name,
    1799         1333 :                                   &(sym->ns->proc_name->declared_at)))
    1800              :             retval = false;
    1801              :         }
    1802              :     }
    1803              : 
    1804              :   return retval;
    1805              : }
    1806              : 
    1807              : 
    1808              : 
    1809              : /* Function called by variable_decl() that adds a name to the symbol table.  */
    1810              : 
    1811              : static bool
    1812       264695 : build_sym (const char *name, int elem, gfc_charlen *cl, bool cl_deferred,
    1813              :            gfc_array_spec **as, locus *var_locus)
    1814              : {
    1815       264695 :   symbol_attribute attr;
    1816       264695 :   gfc_symbol *sym;
    1817       264695 :   int upper;
    1818       264695 :   gfc_symtree *st, *host_st = NULL;
    1819              : 
    1820              :   /* Symbols in a submodule are host associated from the parent module or
    1821              :      submodules. Therefore, they can be overridden by declarations in the
    1822              :      submodule scope. Deal with this by attaching the existing symbol to
    1823              :      a new symtree and recycling the old symtree with a new symbol...  */
    1824       264695 :   st = gfc_find_symtree (gfc_current_ns->sym_root, name);
    1825       264695 :   if (((st && st->import_only) || (gfc_current_ns->import_state == IMPORT_ALL))
    1826            3 :       && gfc_current_ns->parent)
    1827            3 :     host_st = gfc_find_symtree (gfc_current_ns->parent->sym_root, name);
    1828              : 
    1829       264695 :   if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
    1830           12 :       && st->n.sym != NULL
    1831           12 :       && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
    1832              :     {
    1833           12 :       gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
    1834           12 :       s->n.sym = st->n.sym;
    1835           12 :       sym = gfc_new_symbol (name, gfc_current_ns, var_locus);
    1836              : 
    1837           12 :       st->n.sym = sym;
    1838           12 :       sym->refs++;
    1839           12 :       gfc_set_sym_referenced (sym);
    1840           12 :     }
    1841              :   /* ...Check that F2018 IMPORT, ONLY and IMPORT, ALL statements, within the
    1842              :      current scope are not violated by local redeclarations. Note that there is
    1843              :      no need to guard for std >= F2018 because import_only and IMPORT_ALL are
    1844              :      only set for these standards.  */
    1845       264683 :   else if (host_st && host_st->n.sym
    1846            2 :            && host_st->n.sym != gfc_current_ns->proc_name
    1847            2 :            && !(st && st->n.sym
    1848            1 :                 && (st->n.sym->attr.dummy || st->n.sym->attr.result)))
    1849              :     {
    1850            2 :       gfc_error ("F2018: C8102 %s at %L is already imported by an %s "
    1851              :                  "statement and must not be re-declared", name, var_locus,
    1852            1 :                  (st && st->import_only) ? "IMPORT, ONLY" : "IMPORT, ALL");
    1853            2 :       return false;
    1854              :     }
    1855              :   /* ...Otherwise generate a new symtree and new symbol.  */
    1856       264681 :   else if (gfc_get_symbol (name, NULL, &sym, var_locus))
    1857              :     return false;
    1858              : 
    1859              :   /* Check if the name has already been defined as a type.  The
    1860              :      first letter of the symtree will be in upper case then.  Of
    1861              :      course, this is only necessary if the upper case letter is
    1862              :      actually different.  */
    1863              : 
    1864       264693 :   upper = TOUPPER(name[0]);
    1865       264693 :   if (upper != name[0])
    1866              :     {
    1867       263943 :       char u_name[GFC_MAX_SYMBOL_LEN + 1];
    1868       263943 :       gfc_symtree *st;
    1869              : 
    1870       263943 :       gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
    1871       263943 :       strcpy (u_name, name);
    1872       263943 :       u_name[0] = upper;
    1873              : 
    1874       263943 :       st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
    1875              : 
    1876              :       /* STRUCTURE types can alias symbol names */
    1877       263943 :       if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
    1878              :         {
    1879            1 :           gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
    1880              :                      &st->n.sym->declared_at);
    1881            1 :           return false;
    1882              :         }
    1883              :     }
    1884              : 
    1885              :   /* Start updating the symbol table.  Add basic type attribute if present.  */
    1886       264692 :   if (current_ts.type != BT_UNKNOWN
    1887       264692 :       && (sym->attr.implicit_type == 0
    1888          186 :           || !gfc_compare_types (&sym->ts, &current_ts))
    1889       529202 :       && !gfc_add_type (sym, &current_ts, var_locus))
    1890              :     {
    1891              :       /* Duplicate-type rejection can leave a fresh CHARACTER length node on
    1892              :          the namespace list before it is attached to any surviving symbol.
    1893              :          Drop only that unattached node; shared constant charlen nodes are
    1894              :          already reachable from earlier declarations.  PR82721.  */
    1895           27 :       if (current_ts.type == BT_CHARACTER && cl && elem == 1)
    1896              :         {
    1897            1 :           discard_pending_charlen (cl);
    1898            1 :           gfc_clear_ts (&current_ts);
    1899              :         }
    1900           26 :       else if (current_ts.type == BT_CHARACTER && cl && cl != current_ts.u.cl)
    1901            0 :         discard_pending_charlen (cl);
    1902              :       return false;
    1903              :     }
    1904              : 
    1905       264665 :   if (sym->ts.type == BT_CHARACTER)
    1906              :     {
    1907        29128 :       if (elem > 1)
    1908         4160 :         sym->ts.u.cl = gfc_new_charlen (sym->ns, cl);
    1909              :       else
    1910              :         sym->ts.u.cl = cl;
    1911        29128 :       sym->ts.deferred = cl_deferred;
    1912              :     }
    1913              : 
    1914              :   /* Add dimension attribute if present.  */
    1915       264665 :   if (!gfc_set_array_spec (sym, *as, var_locus))
    1916              :     return false;
    1917       264663 :   *as = NULL;
    1918              : 
    1919              :   /* Add attribute to symbol.  The copy is so that we can reset the
    1920              :      dimension attribute.  */
    1921       264663 :   attr = current_attr;
    1922       264663 :   attr.dimension = 0;
    1923       264663 :   attr.codimension = 0;
    1924              : 
    1925       264663 :   if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
    1926              :     return false;
    1927              : 
    1928              :   /* Finish any work that may need to be done for the binding label,
    1929              :      if it's a bind(c).  The bind(c) attr is found before the symbol
    1930              :      is made, and before the symbol name (for data decls), so the
    1931              :      current_ts is holding the binding label, or nothing if the
    1932              :      name= attr wasn't given.  Therefore, test here if we're dealing
    1933              :      with a bind(c) and make sure the binding label is set correctly.  */
    1934       264649 :   if (sym->attr.is_bind_c == 1)
    1935              :     {
    1936         1788 :       if (!sym->binding_label)
    1937              :         {
    1938              :           /* Set the binding label and verify that if a NAME= was specified
    1939              :              then only one identifier was in the entity-decl-list.  */
    1940          137 :           if (!set_binding_label (&sym->binding_label, sym->name,
    1941              :                                   num_idents_on_line))
    1942              :             return false;
    1943              :         }
    1944              :     }
    1945              : 
    1946              :   /* See if we know we're in a common block, and if it's a bind(c)
    1947              :      common then we need to make sure we're an interoperable type.  */
    1948       264647 :   if (sym->attr.in_common == 1)
    1949              :     {
    1950              :       /* Test the common block object.  */
    1951          614 :       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
    1952            6 :           && sym->ts.is_c_interop != 1)
    1953              :         {
    1954            0 :           gfc_error_now ("Variable %qs in common block %qs at %C "
    1955              :                          "must be declared with a C interoperable "
    1956              :                          "kind since common block %qs is BIND(C)",
    1957              :                          sym->name, sym->common_block->name,
    1958            0 :                          sym->common_block->name);
    1959            0 :           gfc_clear_error ();
    1960              :         }
    1961              :     }
    1962              : 
    1963       264647 :   sym->attr.implied_index = 0;
    1964              : 
    1965              :   /* Use the parameter expressions for a parameterized derived type.  */
    1966       264647 :   if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    1967        37594 :       && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
    1968         1122 :     sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
    1969              : 
    1970       264647 :   if (sym->ts.type == BT_CLASS)
    1971        11382 :     return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
    1972              : 
    1973              :   return true;
    1974              : }
    1975              : 
    1976              : 
    1977              : /* Set character constant to the given length. The constant will be padded or
    1978              :    truncated.  If we're inside an array constructor without a typespec, we
    1979              :    additionally check that all elements have the same length; check_len -1
    1980              :    means no checking.  */
    1981              : 
    1982              : void
    1983        14487 : gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
    1984              :                                 gfc_charlen_t check_len)
    1985              : {
    1986        14487 :   gfc_char_t *s;
    1987        14487 :   gfc_charlen_t slen;
    1988              : 
    1989        14487 :   if (expr->ts.type != BT_CHARACTER)
    1990              :     return;
    1991              : 
    1992        14485 :   if (expr->expr_type != EXPR_CONSTANT)
    1993              :     {
    1994            1 :       gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
    1995            1 :       return;
    1996              :     }
    1997              : 
    1998        14484 :   slen = expr->value.character.length;
    1999        14484 :   if (len != slen)
    2000              :     {
    2001         2148 :       s = gfc_get_wide_string (len + 1);
    2002         2148 :       memcpy (s, expr->value.character.string,
    2003         2148 :               MIN (len, slen) * sizeof (gfc_char_t));
    2004         2148 :       if (len > slen)
    2005         1857 :         gfc_wide_memset (&s[slen], ' ', len - slen);
    2006              : 
    2007         2148 :       if (warn_character_truncation && slen > len)
    2008            1 :         gfc_warning_now (OPT_Wcharacter_truncation,
    2009              :                          "CHARACTER expression at %L is being truncated "
    2010              :                          "(%ld/%ld)", &expr->where,
    2011              :                          (long) slen, (long) len);
    2012              : 
    2013              :       /* Apply the standard by 'hand' otherwise it gets cleared for
    2014              :          initializers.  */
    2015         2148 :       if (check_len != -1 && slen != check_len)
    2016              :         {
    2017            3 :           if (!(gfc_option.allow_std & GFC_STD_GNU))
    2018            0 :             gfc_error_now ("The CHARACTER elements of the array constructor "
    2019              :                            "at %L must have the same length (%ld/%ld)",
    2020              :                            &expr->where, (long) slen,
    2021              :                            (long) check_len);
    2022              :           else
    2023            3 :             gfc_notify_std (GFC_STD_LEGACY,
    2024              :                             "The CHARACTER elements of the array constructor "
    2025              :                             "at %L must have the same length (%ld/%ld)",
    2026              :                             &expr->where, (long) slen,
    2027              :                             (long) check_len);
    2028              :         }
    2029              : 
    2030         2148 :       s[len] = '\0';
    2031         2148 :       free (expr->value.character.string);
    2032         2148 :       expr->value.character.string = s;
    2033         2148 :       expr->value.character.length = len;
    2034              :       /* If explicit representation was given, clear it
    2035              :          as it is no longer needed after padding.  */
    2036         2148 :       if (expr->representation.length)
    2037              :         {
    2038           45 :           expr->representation.length = 0;
    2039           45 :           free (expr->representation.string);
    2040           45 :           expr->representation.string = NULL;
    2041              :         }
    2042              :     }
    2043              : }
    2044              : 
    2045              : 
    2046              : /* Function to create and update the enumerator history
    2047              :    using the information passed as arguments.
    2048              :    Pointer "max_enum" is also updated, to point to
    2049              :    enum history node containing largest initializer.
    2050              : 
    2051              :    SYM points to the symbol node of enumerator.
    2052              :    INIT points to its enumerator value.  */
    2053              : 
    2054              : static void
    2055          543 : create_enum_history (gfc_symbol *sym, gfc_expr *init)
    2056              : {
    2057          543 :   enumerator_history *new_enum_history;
    2058          543 :   gcc_assert (sym != NULL && init != NULL);
    2059              : 
    2060          543 :   new_enum_history = XCNEW (enumerator_history);
    2061              : 
    2062          543 :   new_enum_history->sym = sym;
    2063          543 :   new_enum_history->initializer = init;
    2064          543 :   new_enum_history->next = NULL;
    2065              : 
    2066          543 :   if (enum_history == NULL)
    2067              :     {
    2068          160 :       enum_history = new_enum_history;
    2069          160 :       max_enum = enum_history;
    2070              :     }
    2071              :   else
    2072              :     {
    2073          383 :       new_enum_history->next = enum_history;
    2074          383 :       enum_history = new_enum_history;
    2075              : 
    2076          383 :       if (mpz_cmp (max_enum->initializer->value.integer,
    2077          383 :                    new_enum_history->initializer->value.integer) < 0)
    2078          381 :         max_enum = new_enum_history;
    2079              :     }
    2080          543 : }
    2081              : 
    2082              : 
    2083              : /* Function to free enum kind history.  */
    2084              : 
    2085              : void
    2086          175 : gfc_free_enum_history (void)
    2087              : {
    2088          175 :   enumerator_history *current = enum_history;
    2089          175 :   enumerator_history *next;
    2090              : 
    2091          718 :   while (current != NULL)
    2092              :     {
    2093          543 :       next = current->next;
    2094          543 :       free (current);
    2095          543 :       current = next;
    2096              :     }
    2097          175 :   max_enum = NULL;
    2098          175 :   enum_history = NULL;
    2099          175 : }
    2100              : 
    2101              : 
    2102              : /* Function to fix initializer character length if the length of the
    2103              :    symbol or component is constant.  */
    2104              : 
    2105              : static bool
    2106         2753 : fix_initializer_charlen (gfc_typespec *ts, gfc_expr *init)
    2107              : {
    2108         2753 :   if (!gfc_specification_expr (ts->u.cl->length))
    2109              :     return false;
    2110              : 
    2111         2753 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    2112              : 
    2113              :   /* resolve_charlen will complain later on if the length
    2114              :      is too large.  Just skip the initialization in that case.  */
    2115         2753 :   if (mpz_cmp (ts->u.cl->length->value.integer,
    2116         2753 :                gfc_integer_kinds[k].huge) <= 0)
    2117              :     {
    2118         2752 :       HOST_WIDE_INT len
    2119         2752 :                 = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    2120              : 
    2121         2752 :       if (init->expr_type == EXPR_CONSTANT)
    2122         2006 :         gfc_set_constant_character_len (len, init, -1);
    2123          746 :       else if (init->expr_type == EXPR_ARRAY)
    2124              :         {
    2125          745 :           gfc_constructor *cons;
    2126              : 
    2127              :           /* Build a new charlen to prevent simplification from
    2128              :              deleting the length before it is resolved.  */
    2129          745 :           init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2130          745 :           init->ts.u.cl->length = gfc_copy_expr (ts->u.cl->length);
    2131          745 :           cons = gfc_constructor_first (init->value.constructor);
    2132         5049 :           for (; cons; cons = gfc_constructor_next (cons))
    2133         3559 :             gfc_set_constant_character_len (len, cons->expr, -1);
    2134              :         }
    2135              :     }
    2136              : 
    2137              :   return true;
    2138              : }
    2139              : 
    2140              : 
    2141              : /* Function called by variable_decl() that adds an initialization
    2142              :    expression to a symbol.  */
    2143              : 
    2144              : static bool
    2145       273044 : add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus,
    2146              :                       gfc_charlen *saved_cl_list)
    2147              : {
    2148       273044 :   symbol_attribute attr;
    2149       273044 :   gfc_symbol *sym;
    2150       273044 :   gfc_expr *init;
    2151              : 
    2152       273044 :   init = *initp;
    2153       273044 :   if (find_special (name, &sym, false))
    2154              :     return false;
    2155              : 
    2156       273044 :   attr = sym->attr;
    2157              : 
    2158              :   /* If this symbol is confirming an implicit parameter type,
    2159              :      then an initialization expression is not allowed.  */
    2160       273044 :   if (attr.flavor == FL_PARAMETER && sym->value != NULL)
    2161              :     {
    2162            1 :       if (*initp != NULL)
    2163              :         {
    2164            0 :           gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
    2165              :                      sym->name);
    2166            0 :           return false;
    2167              :         }
    2168              :       else
    2169              :         return true;
    2170              :     }
    2171              : 
    2172       273043 :   if (init == NULL)
    2173              :     {
    2174              :       /* An initializer is required for PARAMETER declarations.  */
    2175       239584 :       if (attr.flavor == FL_PARAMETER)
    2176              :         {
    2177            1 :           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
    2178            1 :           return false;
    2179              :         }
    2180              :     }
    2181              :   else
    2182              :     {
    2183              :       /* If a variable appears in a DATA block, it cannot have an
    2184              :          initializer.  */
    2185        33459 :       if (sym->attr.data)
    2186              :         {
    2187            0 :           gfc_error ("Variable %qs at %C with an initializer already "
    2188              :                      "appears in a DATA statement", sym->name);
    2189            0 :           return false;
    2190              :         }
    2191              : 
    2192              :       /* Check if the assignment can happen. This has to be put off
    2193              :          until later for derived type variables and procedure pointers.  */
    2194        32278 :       if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
    2195        32255 :           && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
    2196        32205 :           && !sym->attr.proc_pointer
    2197        65555 :           && !gfc_check_assign_symbol (sym, NULL, init))
    2198              :         return false;
    2199              : 
    2200        33428 :       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
    2201         3446 :             && init->ts.type == BT_CHARACTER)
    2202              :         {
    2203              :           /* Update symbol character length according initializer.  */
    2204         3282 :           if (!gfc_check_assign_symbol (sym, NULL, init))
    2205              :             return false;
    2206              : 
    2207         3282 :           if (sym->ts.u.cl->length == NULL)
    2208              :             {
    2209          851 :               gfc_charlen_t clen;
    2210              :               /* If there are multiple CHARACTER variables declared on the
    2211              :                  same line, we don't want them to share the same length.  */
    2212          851 :               sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2213              : 
    2214          851 :               if (sym->attr.flavor == FL_PARAMETER)
    2215              :                 {
    2216          842 :                   if (init->expr_type == EXPR_CONSTANT)
    2217              :                     {
    2218          557 :                       clen = init->value.character.length;
    2219          557 :                       sym->ts.u.cl->length
    2220          557 :                                 = gfc_get_int_expr (gfc_charlen_int_kind,
    2221              :                                                     NULL, clen);
    2222              :                     }
    2223          285 :                   else if (init->expr_type == EXPR_ARRAY)
    2224              :                     {
    2225          285 :                       if (init->ts.u.cl && init->ts.u.cl->length)
    2226              :                         {
    2227          273 :                           const gfc_expr *length = init->ts.u.cl->length;
    2228          273 :                           if (length->expr_type != EXPR_CONSTANT)
    2229              :                             {
    2230            3 :                               gfc_error ("Cannot initialize parameter array "
    2231              :                                          "at %L "
    2232              :                                          "with variable length elements",
    2233              :                                          &sym->declared_at);
    2234              : 
    2235              :                               /* This rejection path can leave several
    2236              :                                  declaration-local charlens on cl_list,
    2237              :                                  including the replacement symbol charlen and
    2238              :                                  the array-constructor typespec charlen.
    2239              :                                  Clear the surviving owners first, then drop
    2240              :                                  only the nodes created by this declaration.  */
    2241            3 :                               sym->ts.u.cl = NULL;
    2242            3 :                               init->ts.u.cl = NULL;
    2243            3 :                               discard_pending_charlens (saved_cl_list);
    2244            3 :                               return false;
    2245              :                             }
    2246          270 :                           clen = mpz_get_si (length->value.integer);
    2247          270 :                         }
    2248           12 :                       else if (init->value.constructor)
    2249              :                         {
    2250           12 :                           gfc_constructor *c;
    2251           12 :                           c = gfc_constructor_first (init->value.constructor);
    2252           12 :                           clen = c->expr->value.character.length;
    2253              :                         }
    2254              :                       else
    2255            0 :                           gcc_unreachable ();
    2256          282 :                       sym->ts.u.cl->length
    2257          282 :                                 = gfc_get_int_expr (gfc_charlen_int_kind,
    2258              :                                                     NULL, clen);
    2259              :                     }
    2260            0 :                   else if (init->ts.u.cl && init->ts.u.cl->length)
    2261            0 :                     sym->ts.u.cl->length =
    2262            0 :                                 gfc_copy_expr (init->ts.u.cl->length);
    2263              :                 }
    2264              :             }
    2265              :           /* Update initializer character length according to symbol.  */
    2266         2431 :           else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2267         2431 :                    && !fix_initializer_charlen (&sym->ts, init))
    2268              :             return false;
    2269              :         }
    2270              : 
    2271        33425 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
    2272         3802 :           && sym->as->rank && init->rank && init->rank != sym->as->rank)
    2273              :         {
    2274            3 :           gfc_error ("Rank mismatch of array at %L and its initializer "
    2275              :                      "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
    2276            3 :           return false;
    2277              :         }
    2278              : 
    2279              :       /* If sym is implied-shape, set its upper bounds from init.  */
    2280        33422 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
    2281         3799 :           && sym->as && sym->as->type == AS_IMPLIED_SHAPE)
    2282              :         {
    2283         1041 :           int dim;
    2284              : 
    2285         1041 :           if (init->rank == 0)
    2286              :             {
    2287            1 :               gfc_error ("Cannot initialize implied-shape array at %L"
    2288              :                          " with scalar", &sym->declared_at);
    2289            1 :               return false;
    2290              :             }
    2291              : 
    2292              :           /* The shape may be NULL for EXPR_ARRAY, set it.  */
    2293         1040 :           if (init->shape == NULL)
    2294              :             {
    2295            5 :               if (init->expr_type != EXPR_ARRAY)
    2296              :                 {
    2297            2 :                   gfc_error ("Bad shape of initializer at %L", &init->where);
    2298            2 :                   return false;
    2299              :                 }
    2300              : 
    2301            3 :               init->shape = gfc_get_shape (1);
    2302            3 :               if (!gfc_array_size (init, &init->shape[0]))
    2303              :                 {
    2304            1 :                   gfc_error ("Cannot determine shape of initializer at %L",
    2305              :                              &init->where);
    2306            1 :                   free (init->shape);
    2307            1 :                   init->shape = NULL;
    2308            1 :                   return false;
    2309              :                 }
    2310              :             }
    2311              : 
    2312         2175 :           for (dim = 0; dim < sym->as->rank; ++dim)
    2313              :             {
    2314         1139 :               int k;
    2315         1139 :               gfc_expr *e, *lower;
    2316              : 
    2317         1139 :               lower = sym->as->lower[dim];
    2318              : 
    2319              :               /* If the lower bound is an array element from another
    2320              :                  parameterized array, then it is marked with EXPR_VARIABLE and
    2321              :                  is an initialization expression.  Try to reduce it.  */
    2322         1139 :               if (lower->expr_type == EXPR_VARIABLE)
    2323            7 :                 gfc_reduce_init_expr (lower);
    2324              : 
    2325         1139 :               if (lower->expr_type == EXPR_CONSTANT)
    2326              :                 {
    2327              :                   /* All dimensions must be without upper bound.  */
    2328         1138 :                   gcc_assert (!sym->as->upper[dim]);
    2329              : 
    2330         1138 :                   k = lower->ts.kind;
    2331         1138 :                   e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
    2332         1138 :                   mpz_add (e->value.integer, lower->value.integer,
    2333         1138 :                            init->shape[dim]);
    2334         1138 :                   mpz_sub_ui (e->value.integer, e->value.integer, 1);
    2335         1138 :                   sym->as->upper[dim] = e;
    2336              :                 }
    2337              :               else
    2338              :                 {
    2339            1 :                   gfc_error ("Non-constant lower bound in implied-shape"
    2340              :                              " declaration at %L", &lower->where);
    2341            1 :                   return false;
    2342              :                 }
    2343              :             }
    2344              : 
    2345         1036 :           sym->as->type = AS_EXPLICIT;
    2346              :         }
    2347              : 
    2348              :       /* Ensure that explicit bounds are simplified.  */
    2349        33417 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
    2350         3794 :           && sym->as && sym->as->type == AS_EXPLICIT)
    2351              :         {
    2352         8422 :           for (int dim = 0; dim < sym->as->rank; ++dim)
    2353              :             {
    2354         4640 :               gfc_expr *e;
    2355              : 
    2356         4640 :               e = sym->as->lower[dim];
    2357         4640 :               if (e->expr_type != EXPR_CONSTANT)
    2358           12 :                 gfc_reduce_init_expr (e);
    2359              : 
    2360         4640 :               e = sym->as->upper[dim];
    2361         4640 :               if (e->expr_type != EXPR_CONSTANT)
    2362          106 :                 gfc_reduce_init_expr (e);
    2363              :             }
    2364              :         }
    2365              : 
    2366              :       /* Need to check if the expression we initialized this
    2367              :          to was one of the iso_c_binding named constants.  If so,
    2368              :          and we're a parameter (constant), let it be iso_c.
    2369              :          For example:
    2370              :          integer(c_int), parameter :: my_int = c_int
    2371              :          integer(my_int) :: my_int_2
    2372              :          If we mark my_int as iso_c (since we can see it's value
    2373              :          is equal to one of the named constants), then my_int_2
    2374              :          will be considered C interoperable.  */
    2375        33417 :       if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
    2376              :         {
    2377        28796 :           sym->ts.is_iso_c |= init->ts.is_iso_c;
    2378        28796 :           sym->ts.is_c_interop |= init->ts.is_c_interop;
    2379              :           /* attr bits needed for module files.  */
    2380        28796 :           sym->attr.is_iso_c |= init->ts.is_iso_c;
    2381        28796 :           sym->attr.is_c_interop |= init->ts.is_c_interop;
    2382        28796 :           if (init->ts.is_iso_c)
    2383          118 :             sym->ts.f90_type = init->ts.f90_type;
    2384              :         }
    2385              : 
    2386              :       /* Catch the case:  type(t), parameter :: x = z'1'.  */
    2387        33417 :       if (sym->ts.type == BT_DERIVED && init->ts.type == BT_BOZ)
    2388              :         {
    2389            1 :           gfc_error ("Entity %qs at %L is incompatible with a BOZ "
    2390              :                      "literal constant", name, &sym->declared_at);
    2391            1 :           return false;
    2392              :         }
    2393              : 
    2394              :       /* Add initializer.  Make sure we keep the ranks sane.  */
    2395        33416 :       if (sym->attr.dimension && init->rank == 0)
    2396              :         {
    2397         1283 :           mpz_t size;
    2398         1283 :           gfc_expr *array;
    2399         1283 :           int n;
    2400         1283 :           if (sym->attr.flavor == FL_PARAMETER
    2401          468 :               && gfc_is_constant_expr (init)
    2402          467 :               && (init->expr_type == EXPR_CONSTANT
    2403           48 :                   || init->expr_type == EXPR_STRUCTURE)
    2404         1750 :               && spec_size (sym->as, &size))
    2405              :             {
    2406          463 :               array = gfc_get_array_expr (init->ts.type, init->ts.kind,
    2407              :                                           &init->where);
    2408          463 :               if (init->ts.type == BT_DERIVED)
    2409           48 :                 array->ts.u.derived = init->ts.u.derived;
    2410        67619 :               for (n = 0; n < (int)mpz_get_si (size); n++)
    2411       133990 :                 gfc_constructor_append_expr (&array->value.constructor,
    2412              :                                              n == 0
    2413              :                                                 ? init
    2414        66834 :                                                 : gfc_copy_expr (init),
    2415              :                                              &init->where);
    2416              : 
    2417          463 :               array->shape = gfc_get_shape (sym->as->rank);
    2418         1052 :               for (n = 0; n < sym->as->rank; n++)
    2419          589 :                 spec_dimen_size (sym->as, n, &array->shape[n]);
    2420              : 
    2421          463 :               init = array;
    2422          463 :               mpz_clear (size);
    2423              :             }
    2424         1283 :           init->rank = sym->as->rank;
    2425         1283 :           init->corank = sym->as->corank;
    2426              :         }
    2427              : 
    2428        33416 :       sym->value = init;
    2429        33416 :       if (sym->attr.save == SAVE_NONE)
    2430        28807 :         sym->attr.save = SAVE_IMPLICIT;
    2431        33416 :       *initp = NULL;
    2432              :     }
    2433              : 
    2434              :   return true;
    2435              : }
    2436              : 
    2437              : 
    2438              : /* Function called by variable_decl() that adds a name to a structure
    2439              :    being built.  */
    2440              : 
    2441              : static bool
    2442        18501 : build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
    2443              :               gfc_array_spec **as)
    2444              : {
    2445        18501 :   gfc_state_data *s;
    2446        18501 :   gfc_component *c;
    2447              : 
    2448              :   /* F03:C438/C439. If the current symbol is of the same derived type that we're
    2449              :      constructing, it must have the pointer attribute.  */
    2450        18501 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    2451         3545 :       && current_ts.u.derived == gfc_current_block ()
    2452          291 :       && current_attr.pointer == 0)
    2453              :     {
    2454          130 :       if (current_attr.allocatable
    2455          130 :           && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
    2456              :                              "must have the POINTER attribute"))
    2457              :         {
    2458              :           return false;
    2459              :         }
    2460          129 :       else if (current_attr.allocatable == 0)
    2461              :         {
    2462            0 :           gfc_error ("Component at %C must have the POINTER attribute");
    2463            0 :           return false;
    2464              :         }
    2465              :     }
    2466              : 
    2467              :   /* F03:C437.  */
    2468        18500 :   if (current_ts.type == BT_CLASS
    2469          887 :       && !(current_attr.pointer || current_attr.allocatable))
    2470              :     {
    2471            5 :       gfc_error ("Component %qs with CLASS at %C must be allocatable "
    2472              :                  "or pointer", name);
    2473            5 :       return false;
    2474              :     }
    2475              : 
    2476        18495 :   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
    2477              :     {
    2478            0 :       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
    2479              :         {
    2480            0 :           gfc_error ("Array component of structure at %C must have explicit "
    2481              :                      "or deferred shape");
    2482            0 :           return false;
    2483              :         }
    2484              :     }
    2485              : 
    2486              :   /* If we are in a nested union/map definition, gfc_add_component will not
    2487              :      properly find repeated components because:
    2488              :        (i) gfc_add_component does a flat search, where components of unions
    2489              :            and maps are implicity chained so nested components may conflict.
    2490              :       (ii) Unions and maps are not linked as components of their parent
    2491              :            structures until after they are parsed.
    2492              :      For (i) we use gfc_find_component which searches recursively, and for (ii)
    2493              :      we search each block directly from the parse stack until we find the top
    2494              :      level structure.  */
    2495              : 
    2496        18495 :   s = gfc_state_stack;
    2497        18495 :   if (s->state == COMP_UNION || s->state == COMP_MAP)
    2498              :     {
    2499         1434 :       while (s->state == COMP_UNION || gfc_comp_struct (s->state))
    2500              :         {
    2501         1434 :           c = gfc_find_component (s->sym, name, true, true, NULL);
    2502         1434 :           if (c != NULL)
    2503              :             {
    2504            0 :               gfc_error_now ("Component %qs at %C already declared at %L",
    2505              :                              name, &c->loc);
    2506            0 :               return false;
    2507              :             }
    2508              :           /* Break after we've searched the entire chain.  */
    2509         1434 :           if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
    2510              :             break;
    2511         1000 :           s = s->previous;
    2512              :         }
    2513              :     }
    2514              : 
    2515        18495 :   if (!gfc_add_component (gfc_current_block(), name, &c))
    2516              :     return false;
    2517              : 
    2518        18489 :   c->ts = current_ts;
    2519        18489 :   if (c->ts.type == BT_CHARACTER)
    2520              :     {
    2521         1958 :       c->ts.u.cl = cl;
    2522              :       /* The component struct is not tracked by the symbol undo mechanism,
    2523              :          so free the charlen here to prevent a double-free.  */
    2524         1958 :       gfc_remove_saved_charlen (cl);
    2525              :     }
    2526              : 
    2527        18489 :   if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
    2528        14950 :       && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
    2529         2144 :       && saved_kind_expr != NULL)
    2530          200 :     c->kind_expr = gfc_copy_expr (saved_kind_expr);
    2531              : 
    2532        18489 :   c->attr = current_attr;
    2533              : 
    2534        18489 :   c->initializer = *init;
    2535        18489 :   *init = NULL;
    2536              : 
    2537              :   /* Update initializer character length according to component.  */
    2538         1958 :   if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length
    2539         1551 :       && c->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2540         1486 :       && c->initializer && c->initializer->ts.type == BT_CHARACTER
    2541        18814 :       && !fix_initializer_charlen (&c->ts, c->initializer))
    2542              :     return false;
    2543              : 
    2544        18489 :   c->as = *as;
    2545        18489 :   if (c->as != NULL)
    2546              :     {
    2547         4957 :       if (c->as->corank)
    2548          113 :         c->attr.codimension = 1;
    2549         4957 :       if (c->as->rank)
    2550         4876 :         c->attr.dimension = 1;
    2551              :     }
    2552        18489 :   *as = NULL;
    2553              : 
    2554        18489 :   gfc_apply_init (&c->ts, &c->attr, c->initializer);
    2555              : 
    2556              :   /* Convert a class, PDT component of a non-derived type to a specific instance
    2557              :      before gfc_build_class_symbol gets to work on it.  */
    2558        18489 :   if (c->ts.type == BT_CLASS
    2559          882 :       && !(gfc_current_block ()->attr.pdt_template
    2560          882 :            || gfc_current_block ()->attr.pdt_type)
    2561          882 :       && c->ts.u.derived->attr.pdt_template)
    2562              :     {
    2563           12 :       match m = gfc_get_pdt_instance (decl_type_param_list, &c->ts.u.derived, NULL);
    2564           12 :       if (m != MATCH_YES)
    2565              :         {
    2566            0 :           if (!gfc_error_check ())
    2567            0 :             gfc_error ("Parameterized component of a non-parameterized "
    2568              :                        "derived type at %C could not be converted to a valid "
    2569              :                        "instance");
    2570              :           return false;
    2571              :         }
    2572              :     }
    2573              : 
    2574              :   /* Check array components.  */
    2575        18489 :   if (!c->attr.dimension)
    2576        13613 :     goto scalar;
    2577              : 
    2578         4876 :   if (c->attr.pointer)
    2579              :     {
    2580          732 :       if (c->as->type != AS_DEFERRED)
    2581              :         {
    2582            5 :           gfc_error ("Pointer array component of structure at %C must have a "
    2583              :                      "deferred shape");
    2584            5 :           return false;
    2585              :         }
    2586              :     }
    2587         4144 :   else if (c->attr.allocatable)
    2588              :     {
    2589         2465 :       const char *err = G_("Allocatable component of structure at %C must have "
    2590              :                            "a deferred shape");
    2591         2465 :       if (c->as->type != AS_DEFERRED)
    2592              :         {
    2593           14 :           if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
    2594              :             {
    2595              :               /* Issue an immediate error and allow this component to pass for
    2596              :                  the sake of clean error recovery.  Set the error flag for the
    2597              :                  containing derived type so that finalizers are not built.  */
    2598            4 :               gfc_error_now (err);
    2599            4 :               s->sym->error = 1;
    2600            4 :               c->as->type = AS_DEFERRED;
    2601              :             }
    2602              :           else
    2603              :             {
    2604           10 :               gfc_error (err);
    2605           10 :               return false;
    2606              :             }
    2607              :         }
    2608              :     }
    2609              :   else
    2610              :     {
    2611         1679 :       if (c->as->type != AS_EXPLICIT)
    2612              :         {
    2613            7 :           gfc_error ("Array component of structure at %C must have an "
    2614              :                      "explicit shape");
    2615            7 :           return false;
    2616              :         }
    2617              :     }
    2618              : 
    2619         1672 : scalar:
    2620        18467 :   if (c->ts.type == BT_CLASS)
    2621          879 :     return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
    2622              : 
    2623        17588 :   if (c->attr.pdt_kind || c->attr.pdt_len)
    2624              :     {
    2625          604 :       gfc_symbol *sym;
    2626          604 :       gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
    2627              :                        0, &sym);
    2628          604 :       if (sym == NULL)
    2629              :         {
    2630            0 :           gfc_error ("Type parameter %qs at %C has no corresponding entry "
    2631              :                      "in the type parameter name list at %L",
    2632            0 :                      c->name, &gfc_current_block ()->declared_at);
    2633            0 :           return false;
    2634              :         }
    2635          604 :       sym->ts = c->ts;
    2636          604 :       sym->attr.pdt_kind = c->attr.pdt_kind;
    2637          604 :       sym->attr.pdt_len = c->attr.pdt_len;
    2638          604 :       if (c->initializer)
    2639          246 :         sym->value = gfc_copy_expr (c->initializer);
    2640          604 :       sym->attr.flavor = FL_VARIABLE;
    2641              :     }
    2642              : 
    2643        17588 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    2644         2657 :       && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
    2645          130 :       && decl_type_param_list)
    2646          130 :     c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
    2647              : 
    2648              :   return true;
    2649              : }
    2650              : 
    2651              : 
    2652              : /* Match a 'NULL()', and possibly take care of some side effects.  */
    2653              : 
    2654              : match
    2655         1722 : gfc_match_null (gfc_expr **result)
    2656              : {
    2657         1722 :   gfc_symbol *sym;
    2658         1722 :   match m, m2 = MATCH_NO;
    2659              : 
    2660         1722 :   if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
    2661              :     return MATCH_ERROR;
    2662              : 
    2663         1722 :   if (m == MATCH_NO)
    2664              :     {
    2665          511 :       locus old_loc;
    2666          511 :       char name[GFC_MAX_SYMBOL_LEN + 1];
    2667              : 
    2668          511 :       if ((m2 = gfc_match (" null (")) != MATCH_YES)
    2669          505 :         return m2;
    2670              : 
    2671            6 :       old_loc = gfc_current_locus;
    2672            6 :       if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
    2673              :         return MATCH_ERROR;
    2674            6 :       if (m2 != MATCH_YES
    2675            6 :           && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
    2676              :         return MATCH_ERROR;
    2677            6 :       if (m2 == MATCH_NO)
    2678              :         {
    2679            0 :           gfc_current_locus = old_loc;
    2680            0 :           return MATCH_NO;
    2681              :         }
    2682              :     }
    2683              : 
    2684              :   /* The NULL symbol now has to be/become an intrinsic function.  */
    2685         1217 :   if (gfc_get_symbol ("null", NULL, &sym))
    2686              :     {
    2687            0 :       gfc_error ("NULL() initialization at %C is ambiguous");
    2688            0 :       return MATCH_ERROR;
    2689              :     }
    2690              : 
    2691         1217 :   gfc_intrinsic_symbol (sym);
    2692              : 
    2693         1217 :   if (sym->attr.proc != PROC_INTRINSIC
    2694          859 :       && !(sym->attr.use_assoc && sym->attr.intrinsic)
    2695         2075 :       && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
    2696          858 :           || !gfc_add_function (&sym->attr, sym->name, NULL)))
    2697              :     return MATCH_ERROR;
    2698              : 
    2699         1217 :   *result = gfc_get_null_expr (&gfc_current_locus);
    2700              : 
    2701              :   /* Invalid per F2008, C512.  */
    2702         1217 :   if (m2 == MATCH_YES)
    2703              :     {
    2704            6 :       gfc_error ("NULL() initialization at %C may not have MOLD");
    2705            6 :       return MATCH_ERROR;
    2706              :     }
    2707              : 
    2708              :   return MATCH_YES;
    2709              : }
    2710              : 
    2711              : 
    2712              : /* Match the initialization expr for a data pointer or procedure pointer.  */
    2713              : 
    2714              : static match
    2715         1386 : match_pointer_init (gfc_expr **init, int procptr)
    2716              : {
    2717         1386 :   match m;
    2718              : 
    2719         1386 :   if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
    2720              :     {
    2721            1 :       gfc_error ("Initialization of pointer at %C is not allowed in "
    2722              :                  "a PURE procedure");
    2723            1 :       return MATCH_ERROR;
    2724              :     }
    2725         1385 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    2726              : 
    2727              :   /* Match NULL() initialization.  */
    2728         1385 :   m = gfc_match_null (init);
    2729         1385 :   if (m != MATCH_NO)
    2730              :     return m;
    2731              : 
    2732              :   /* Match non-NULL initialization.  */
    2733          176 :   gfc_matching_ptr_assignment = !procptr;
    2734          176 :   gfc_matching_procptr_assignment = procptr;
    2735          176 :   m = gfc_match_rvalue (init);
    2736          176 :   gfc_matching_ptr_assignment = 0;
    2737          176 :   gfc_matching_procptr_assignment = 0;
    2738          176 :   if (m == MATCH_ERROR)
    2739              :     return MATCH_ERROR;
    2740          175 :   else if (m == MATCH_NO)
    2741              :     {
    2742            2 :       gfc_error ("Error in pointer initialization at %C");
    2743            2 :       return MATCH_ERROR;
    2744              :     }
    2745              : 
    2746          173 :   if (!procptr && !gfc_resolve_expr (*init))
    2747              :     return MATCH_ERROR;
    2748              : 
    2749          172 :   if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
    2750              :                        "initialization at %C"))
    2751            0 :     return MATCH_ERROR;
    2752              : 
    2753              :   return MATCH_YES;
    2754              : }
    2755              : 
    2756              : 
    2757              : static bool
    2758       293114 : check_function_name (char *name)
    2759              : {
    2760              :   /* In functions that have a RESULT variable defined, the function name always
    2761              :      refers to function calls.  Therefore, the name is not allowed to appear in
    2762              :      specification statements. When checking this, be careful about
    2763              :      'hidden' procedure pointer results ('ppr@').  */
    2764              : 
    2765       293114 :   if (gfc_current_state () == COMP_FUNCTION)
    2766              :     {
    2767        48040 :       gfc_symbol *block = gfc_current_block ();
    2768        48040 :       if (block && block->result && block->result != block
    2769        15455 :           && strcmp (block->result->name, "ppr@") != 0
    2770        15396 :           && strcmp (block->name, name) == 0)
    2771              :         {
    2772            9 :           gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
    2773              :                      "from appearing in a specification statement",
    2774              :                      block->result->name, &block->result->declared_at, name);
    2775            9 :           return false;
    2776              :         }
    2777              :     }
    2778              : 
    2779              :   return true;
    2780              : }
    2781              : 
    2782              : 
    2783              : /* Match a variable name with an optional initializer.  When this
    2784              :    subroutine is called, a variable is expected to be parsed next.
    2785              :    Depending on what is happening at the moment, updates either the
    2786              :    symbol table or the current interface.  */
    2787              : 
    2788              : static match
    2789       282883 : variable_decl (int elem)
    2790              : {
    2791       282883 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    2792       282883 :   static unsigned int fill_id = 0;
    2793       282883 :   gfc_expr *initializer, *char_len;
    2794       282883 :   gfc_array_spec *as;
    2795       282883 :   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
    2796       282883 :   gfc_charlen *cl;
    2797       282883 :   gfc_charlen *saved_cl_list;
    2798       282883 :   bool cl_deferred;
    2799       282883 :   locus var_locus;
    2800       282883 :   match m;
    2801       282883 :   bool t;
    2802       282883 :   gfc_symbol *sym;
    2803       282883 :   char c;
    2804              : 
    2805       282883 :   initializer = NULL;
    2806       282883 :   as = NULL;
    2807       282883 :   cp_as = NULL;
    2808       282883 :   saved_cl_list = gfc_current_ns->cl_list;
    2809              : 
    2810              :   /* When we get here, we've just matched a list of attributes and
    2811              :      maybe a type and a double colon.  The next thing we expect to see
    2812              :      is the name of the symbol.  */
    2813              : 
    2814              :   /* If we are parsing a structure with legacy support, we allow the symbol
    2815              :      name to be '%FILL' which gives it an anonymous (inaccessible) name.  */
    2816       282883 :   m = MATCH_NO;
    2817       282883 :   gfc_gobble_whitespace ();
    2818       282883 :   var_locus = gfc_current_locus;
    2819       282883 :   c = gfc_peek_ascii_char ();
    2820       282883 :   if (c == '%')
    2821              :     {
    2822           12 :       gfc_next_ascii_char ();   /* Burn % character.  */
    2823           12 :       m = gfc_match ("fill");
    2824           12 :       if (m == MATCH_YES)
    2825              :         {
    2826           11 :           if (gfc_current_state () != COMP_STRUCTURE)
    2827              :             {
    2828            2 :               if (flag_dec_structure)
    2829            1 :                 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
    2830              :               else
    2831            1 :                 gfc_error ("%qs at %C is a DEC extension, enable with "
    2832              :                        "%<-fdec-structure%>", "%FILL");
    2833            2 :               m = MATCH_ERROR;
    2834            2 :               goto cleanup;
    2835              :             }
    2836              : 
    2837            9 :           if (attr_seen)
    2838              :             {
    2839            1 :               gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
    2840            1 :               m = MATCH_ERROR;
    2841            1 :               goto cleanup;
    2842              :             }
    2843              : 
    2844              :           /* %FILL components are given invalid fortran names.  */
    2845            8 :           snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
    2846              :         }
    2847              :       else
    2848              :         {
    2849            1 :           gfc_error ("Invalid character %qc in variable name at %C", c);
    2850            1 :           return MATCH_ERROR;
    2851              :         }
    2852              :     }
    2853              :   else
    2854              :     {
    2855       282871 :       m = gfc_match_name (name);
    2856       282870 :       if (m != MATCH_YES)
    2857           10 :         goto cleanup;
    2858              :     }
    2859              : 
    2860              :   /* Now we could see the optional array spec. or character length.  */
    2861       282868 :   m = gfc_match_array_spec (&as, true, true);
    2862       282867 :   if (m == MATCH_ERROR)
    2863           57 :     goto cleanup;
    2864              : 
    2865       282810 :   if (m == MATCH_NO)
    2866       220983 :     as = gfc_copy_array_spec (current_as);
    2867        61827 :   else if (current_as
    2868        61827 :            && !merge_array_spec (current_as, as, true))
    2869              :     {
    2870            4 :       m = MATCH_ERROR;
    2871            4 :       goto cleanup;
    2872              :     }
    2873              : 
    2874       282806 :    var_locus = gfc_get_location_range (NULL, 0, &var_locus, 1,
    2875              :                                        &gfc_current_locus);
    2876       282806 :   if (flag_cray_pointer)
    2877         3063 :     cp_as = gfc_copy_array_spec (as);
    2878              : 
    2879              :   /* At this point, we know for sure if the symbol is PARAMETER and can thus
    2880              :      determine (and check) whether it can be implied-shape.  If it
    2881              :      was parsed as assumed-size, change it because PARAMETERs cannot
    2882              :      be assumed-size.
    2883              : 
    2884              :      An explicit-shape-array cannot appear under several conditions.
    2885              :      That check is done here as well.  */
    2886       282806 :   if (as)
    2887              :     {
    2888        84525 :       if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
    2889              :         {
    2890            2 :           m = MATCH_ERROR;
    2891            2 :           gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
    2892              :                      name, &var_locus);
    2893            2 :           goto cleanup;
    2894              :         }
    2895              : 
    2896        84523 :       if (as->type == AS_ASSUMED_SIZE && as->rank == 1
    2897         6515 :           && current_attr.flavor == FL_PARAMETER)
    2898          993 :         as->type = AS_IMPLIED_SHAPE;
    2899              : 
    2900        84523 :       if (as->type == AS_IMPLIED_SHAPE
    2901        84523 :           && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
    2902              :                               &var_locus))
    2903              :         {
    2904            1 :           m = MATCH_ERROR;
    2905            1 :           goto cleanup;
    2906              :         }
    2907              : 
    2908        84522 :       gfc_seen_div0 = false;
    2909              : 
    2910              :       /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
    2911              :          constant expressions shall appear only in a subprogram, derived
    2912              :          type definition, BLOCK construct, or interface body.  */
    2913        84522 :       if (as->type == AS_EXPLICIT
    2914        42156 :           && gfc_current_state () != COMP_BLOCK
    2915              :           && gfc_current_state () != COMP_DERIVED
    2916              :           && gfc_current_state () != COMP_FUNCTION
    2917              :           && gfc_current_state () != COMP_INTERFACE
    2918              :           && gfc_current_state () != COMP_SUBROUTINE)
    2919              :         {
    2920              :           gfc_expr *e;
    2921        50052 :           bool not_constant = false;
    2922              : 
    2923        50052 :           for (int i = 0; i < as->rank; i++)
    2924              :             {
    2925        28488 :               e = gfc_copy_expr (as->lower[i]);
    2926        28488 :               if (!gfc_resolve_expr (e) && gfc_seen_div0)
    2927              :                 {
    2928            0 :                   m = MATCH_ERROR;
    2929            0 :                   goto cleanup;
    2930              :                 }
    2931              : 
    2932        28488 :               gfc_simplify_expr (e, 0);
    2933        28488 :               if (e && (e->expr_type != EXPR_CONSTANT))
    2934              :                 {
    2935              :                   not_constant = true;
    2936              :                   break;
    2937              :                 }
    2938        28488 :               gfc_free_expr (e);
    2939              : 
    2940        28488 :               e = gfc_copy_expr (as->upper[i]);
    2941        28488 :               if (!gfc_resolve_expr (e)  && gfc_seen_div0)
    2942              :                 {
    2943            4 :                   m = MATCH_ERROR;
    2944            4 :                   goto cleanup;
    2945              :                 }
    2946              : 
    2947        28484 :               gfc_simplify_expr (e, 0);
    2948        28484 :               if (e && (e->expr_type != EXPR_CONSTANT))
    2949              :                 {
    2950              :                   not_constant = true;
    2951              :                   break;
    2952              :                 }
    2953        28471 :               gfc_free_expr (e);
    2954              :             }
    2955              : 
    2956        21577 :           if (not_constant && e->ts.type != BT_INTEGER)
    2957              :             {
    2958            4 :               gfc_error ("Explicit array shape at %C must be constant of "
    2959              :                          "INTEGER type and not %s type",
    2960              :                          gfc_basic_typename (e->ts.type));
    2961            4 :               m = MATCH_ERROR;
    2962            4 :               goto cleanup;
    2963              :             }
    2964            9 :           if (not_constant)
    2965              :             {
    2966            9 :               gfc_error ("Explicit shaped array with nonconstant bounds at %C");
    2967            9 :               m = MATCH_ERROR;
    2968            9 :               goto cleanup;
    2969              :             }
    2970              :         }
    2971        84505 :       if (as->type == AS_EXPLICIT)
    2972              :         {
    2973       100796 :           for (int i = 0; i < as->rank; i++)
    2974              :             {
    2975        58657 :               gfc_expr *e, *n;
    2976        58657 :               e = as->lower[i];
    2977        58657 :               if (e->expr_type != EXPR_CONSTANT)
    2978              :                 {
    2979          452 :                   n = gfc_copy_expr (e);
    2980          452 :                   if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
    2981              :                     {
    2982            0 :                       m = MATCH_ERROR;
    2983            0 :                       goto cleanup;
    2984              :                     }
    2985              : 
    2986          452 :                   if (n->expr_type == EXPR_CONSTANT)
    2987           22 :                     gfc_replace_expr (e, n);
    2988              :                   else
    2989          430 :                     gfc_free_expr (n);
    2990              :                 }
    2991        58657 :               e = as->upper[i];
    2992        58657 :               if (e->expr_type != EXPR_CONSTANT)
    2993              :                 {
    2994         6757 :                   n = gfc_copy_expr (e);
    2995         6757 :                   if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
    2996              :                     {
    2997            0 :                       m = MATCH_ERROR;
    2998            0 :                       goto cleanup;
    2999              :                     }
    3000              : 
    3001         6757 :                   if (n->expr_type == EXPR_CONSTANT)
    3002           45 :                     gfc_replace_expr (e, n);
    3003              :                   else
    3004         6712 :                     gfc_free_expr (n);
    3005              :                 }
    3006              :               /* For an explicit-shape spec with constant bounds, ensure
    3007              :                  that the effective upper bound is not lower than the
    3008              :                  respective lower bound minus one.  Otherwise adjust it so
    3009              :                  that the extent is trivially derived to be zero.  */
    3010        58657 :               if (as->lower[i]->expr_type == EXPR_CONSTANT
    3011        58227 :                   && as->upper[i]->expr_type == EXPR_CONSTANT
    3012        51939 :                   && as->lower[i]->ts.type == BT_INTEGER
    3013        51939 :                   && as->upper[i]->ts.type == BT_INTEGER
    3014        51934 :                   && mpz_cmp (as->upper[i]->value.integer,
    3015        51934 :                               as->lower[i]->value.integer) < 0)
    3016         1218 :                 mpz_sub_ui (as->upper[i]->value.integer,
    3017              :                             as->lower[i]->value.integer, 1);
    3018              :             }
    3019              :         }
    3020              :     }
    3021              : 
    3022       282786 :   char_len = NULL;
    3023       282786 :   cl = NULL;
    3024       282786 :   cl_deferred = false;
    3025              : 
    3026       282786 :   if (current_ts.type == BT_CHARACTER)
    3027              :     {
    3028        31127 :       switch (match_char_length (&char_len, &cl_deferred, false))
    3029              :         {
    3030          435 :         case MATCH_YES:
    3031          435 :           cl = gfc_new_charlen (gfc_current_ns, NULL);
    3032              : 
    3033          435 :           cl->length = char_len;
    3034          435 :           break;
    3035              : 
    3036              :         /* Non-constant lengths need to be copied after the first
    3037              :            element.  Also copy assumed lengths.  */
    3038        30691 :         case MATCH_NO:
    3039        30691 :           if (elem > 1
    3040         3929 :               && (current_ts.u.cl->length == NULL
    3041         2709 :                   || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
    3042              :             {
    3043         1275 :               cl = gfc_new_charlen (gfc_current_ns, NULL);
    3044         1275 :               cl->length = gfc_copy_expr (current_ts.u.cl->length);
    3045              :             }
    3046              :           else
    3047        29416 :             cl = current_ts.u.cl;
    3048              : 
    3049        30691 :           cl_deferred = current_ts.deferred;
    3050              : 
    3051        30691 :           break;
    3052              : 
    3053            1 :         case MATCH_ERROR:
    3054            1 :           goto cleanup;
    3055              :         }
    3056              :     }
    3057              : 
    3058              :   /* The dummy arguments and result of the abbreviated form of MODULE
    3059              :      PROCEDUREs, used in SUBMODULES should not be redefined.  */
    3060       282785 :   if (gfc_current_ns->proc_name
    3061       278297 :       && gfc_current_ns->proc_name->abr_modproc_decl)
    3062              :     {
    3063           44 :       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
    3064           44 :       if (sym != NULL && (sym->attr.dummy || sym->attr.result))
    3065              :         {
    3066            2 :           m = MATCH_ERROR;
    3067            2 :           gfc_error ("%qs at %L is a redefinition of the declaration "
    3068              :                      "in the corresponding interface for MODULE "
    3069              :                      "PROCEDURE %qs", sym->name, &var_locus,
    3070            2 :                      gfc_current_ns->proc_name->name);
    3071            2 :           goto cleanup;
    3072              :         }
    3073              :     }
    3074              : 
    3075              :   /* %FILL components may not have initializers.  */
    3076       282783 :   if (startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
    3077              :     {
    3078            1 :       gfc_error ("%qs entity cannot have an initializer at %L", "%FILL",
    3079              :                  &var_locus);
    3080            1 :       m = MATCH_ERROR;
    3081            1 :       goto cleanup;
    3082              :     }
    3083              : 
    3084              :   /*  If this symbol has already shown up in a Cray Pointer declaration,
    3085              :       and this is not a component declaration,
    3086              :       then we want to set the type & bail out.  */
    3087       282782 :   if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
    3088              :     {
    3089         2959 :       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
    3090         2959 :       if (sym != NULL && sym->attr.cray_pointee)
    3091              :         {
    3092          101 :           m = MATCH_YES;
    3093          101 :           if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
    3094              :             {
    3095            1 :               m = MATCH_ERROR;
    3096            1 :               goto cleanup;
    3097              :             }
    3098              : 
    3099              :           /* Check to see if we have an array specification.  */
    3100          100 :           if (cp_as != NULL)
    3101              :             {
    3102           49 :               if (sym->as != NULL)
    3103              :                 {
    3104            1 :                   gfc_error ("Duplicate array spec for Cray pointee at %L", &var_locus);
    3105            1 :                   gfc_free_array_spec (cp_as);
    3106            1 :                   m = MATCH_ERROR;
    3107            1 :                   goto cleanup;
    3108              :                 }
    3109              :               else
    3110              :                 {
    3111           48 :                   if (!gfc_set_array_spec (sym, cp_as, &var_locus))
    3112            0 :                     gfc_internal_error ("Cannot set pointee array spec.");
    3113              : 
    3114              :                   /* Fix the array spec.  */
    3115           48 :                   m = gfc_mod_pointee_as (sym->as);
    3116           48 :                   if (m == MATCH_ERROR)
    3117            0 :                     goto cleanup;
    3118              :                 }
    3119              :             }
    3120           99 :           goto cleanup;
    3121              :         }
    3122              :       else
    3123              :         {
    3124         2858 :           gfc_free_array_spec (cp_as);
    3125              :         }
    3126              :     }
    3127              :   else
    3128              :     {
    3129              :       /* Check to see if this is the declaration of the type and/or attributes
    3130              :          of an implicit function result, emanating from a module function
    3131              :          interface declared within the parent module or submodule of a
    3132              :          containing submodule.  */
    3133       279823 :       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
    3134       279823 :       if (gfc_current_state () == COMP_FUNCTION
    3135        46558 :           && sym == gfc_current_block ()
    3136         8278 :           && sym->attr.if_source == IFSRC_DECL
    3137         4952 :           && sym->attr.used_in_submodule
    3138            4 :           && sym == sym->result
    3139            4 :           && sym->ts.type != BT_UNKNOWN)
    3140              :         {
    3141            4 :           m = MATCH_YES;
    3142            4 :           goto cleanup;
    3143              :         }
    3144       279819 :       sym = NULL;
    3145              :     }
    3146              : 
    3147              :   /* Procedure pointer as function result.  */
    3148       282677 :   if (gfc_current_state () == COMP_FUNCTION
    3149        46668 :       && strcmp ("ppr@", gfc_current_block ()->name) == 0
    3150           25 :       && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
    3151            7 :     strcpy (name, "ppr@");
    3152              : 
    3153       282677 :   if (gfc_current_state () == COMP_FUNCTION
    3154        46668 :       && strcmp (name, gfc_current_block ()->name) == 0
    3155         8294 :       && gfc_current_block ()->result
    3156         8294 :       && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
    3157           16 :     strcpy (name, "ppr@");
    3158              : 
    3159              :   /* OK, we've successfully matched the declaration.  Now put the
    3160              :      symbol in the current namespace, because it might be used in the
    3161              :      optional initialization expression for this symbol, e.g. this is
    3162              :      perfectly legal:
    3163              : 
    3164              :      integer, parameter :: i = huge(i)
    3165              : 
    3166              :      This is only true for parameters or variables of a basic type.
    3167              :      For components of derived types, it is not true, so we don't
    3168              :      create a symbol for those yet.  If we fail to create the symbol,
    3169              :      bail out.  */
    3170       282677 :   if (!gfc_comp_struct (gfc_current_state ())
    3171       264147 :       && !build_sym (name, elem, cl, cl_deferred, &as, &var_locus))
    3172              :     {
    3173           48 :       m = MATCH_ERROR;
    3174           48 :       goto cleanup;
    3175              :     }
    3176              : 
    3177       282629 :   if (!check_function_name (name))
    3178              :     {
    3179            0 :       m = MATCH_ERROR;
    3180            0 :       goto cleanup;
    3181              :     }
    3182              : 
    3183              :   /* We allow old-style initializations of the form
    3184              :        integer i /2/, j(4) /3*3, 1/
    3185              :      (if no colon has been seen). These are different from data
    3186              :      statements in that initializers are only allowed to apply to the
    3187              :      variable immediately preceding, i.e.
    3188              :        integer i, j /1, 2/
    3189              :      is not allowed. Therefore we have to do some work manually, that
    3190              :      could otherwise be left to the matchers for DATA statements.  */
    3191              : 
    3192       282629 :   if (!colon_seen && gfc_match (" /") == MATCH_YES)
    3193              :     {
    3194          146 :       if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
    3195              :                            "initialization at %C"))
    3196              :         return MATCH_ERROR;
    3197              : 
    3198              :       /* Allow old style initializations for components of STRUCTUREs and MAPs
    3199              :          but not components of derived types.  */
    3200          146 :       else if (gfc_current_state () == COMP_DERIVED)
    3201              :         {
    3202            2 :           gfc_error ("Invalid old style initialization for derived type "
    3203              :                      "component at %C");
    3204            2 :           m = MATCH_ERROR;
    3205            2 :           goto cleanup;
    3206              :         }
    3207              : 
    3208              :       /* For structure components, read the initializer as a special
    3209              :          expression and let the rest of this function apply the initializer
    3210              :          as usual.  */
    3211          144 :       else if (gfc_comp_struct (gfc_current_state ()))
    3212              :         {
    3213           74 :           m = match_clist_expr (&initializer, &current_ts, as);
    3214           74 :           if (m == MATCH_NO)
    3215              :             gfc_error ("Syntax error in old style initialization of %s at %C",
    3216              :                        name);
    3217           74 :           if (m != MATCH_YES)
    3218           14 :             goto cleanup;
    3219              :         }
    3220              : 
    3221              :       /* Otherwise we treat the old style initialization just like a
    3222              :          DATA declaration for the current variable.  */
    3223              :       else
    3224           70 :         return match_old_style_init (name);
    3225              :     }
    3226              : 
    3227              :   /* The double colon must be present in order to have initializers.
    3228              :      Otherwise the statement is ambiguous with an assignment statement.  */
    3229       282543 :   if (colon_seen)
    3230              :     {
    3231       236295 :       if (gfc_match (" =>") == MATCH_YES)
    3232              :         {
    3233         1203 :           if (!current_attr.pointer)
    3234              :             {
    3235            0 :               gfc_error ("Initialization at %C isn't for a pointer variable");
    3236            0 :               m = MATCH_ERROR;
    3237            0 :               goto cleanup;
    3238              :             }
    3239              : 
    3240         1203 :           m = match_pointer_init (&initializer, 0);
    3241         1203 :           if (m != MATCH_YES)
    3242           10 :             goto cleanup;
    3243              : 
    3244              :           /* The target of a pointer initialization must have the SAVE
    3245              :              attribute.  A variable in PROGRAM, MODULE, or SUBMODULE scope
    3246              :              is implicit SAVEd.  Explicitly, set the SAVE_IMPLICIT value.  */
    3247         1193 :           if (initializer->expr_type == EXPR_VARIABLE
    3248          128 :               && initializer->symtree->n.sym->attr.save == SAVE_NONE
    3249           25 :               && (gfc_current_state () == COMP_PROGRAM
    3250              :                   || gfc_current_state () == COMP_MODULE
    3251           25 :                   || gfc_current_state () == COMP_SUBMODULE))
    3252           11 :             initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
    3253              :         }
    3254       235092 :       else if (gfc_match_char ('=') == MATCH_YES)
    3255              :         {
    3256        26372 :           if (current_attr.pointer)
    3257              :             {
    3258            0 :               gfc_error ("Pointer initialization at %C requires %<=>%>, "
    3259              :                          "not %<=%>");
    3260            0 :               m = MATCH_ERROR;
    3261            0 :               goto cleanup;
    3262              :             }
    3263              : 
    3264        26372 :           if (gfc_comp_struct (gfc_current_state ())
    3265         2526 :               && gfc_current_block ()->attr.pdt_template)
    3266              :             {
    3267          269 :               m = gfc_match_expr (&initializer);
    3268          269 :               if (initializer && initializer->ts.type == BT_UNKNOWN)
    3269          115 :                 initializer->ts = current_ts;
    3270              :             }
    3271              :           else
    3272        26103 :             m = gfc_match_init_expr (&initializer);
    3273              : 
    3274        26372 :           if (m == MATCH_NO)
    3275              :             {
    3276            1 :               gfc_error ("Expected an initialization expression at %C");
    3277            1 :               m = MATCH_ERROR;
    3278              :             }
    3279              : 
    3280        10317 :           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
    3281        26374 :               && !gfc_comp_struct (gfc_state_stack->state))
    3282              :             {
    3283            1 :               gfc_error ("Initialization of variable at %C is not allowed in "
    3284              :                          "a PURE procedure");
    3285            1 :               m = MATCH_ERROR;
    3286              :             }
    3287              : 
    3288        26372 :           if (current_attr.flavor != FL_PARAMETER
    3289        10317 :               && !gfc_comp_struct (gfc_state_stack->state))
    3290         7791 :             gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    3291              : 
    3292        26372 :           if (m != MATCH_YES)
    3293          160 :             goto cleanup;
    3294              :         }
    3295              :     }
    3296              : 
    3297       282373 :   if (initializer != NULL && current_attr.allocatable
    3298            3 :         && gfc_comp_struct (gfc_current_state ()))
    3299              :     {
    3300            2 :       gfc_error ("Initialization of allocatable component at %C is not "
    3301              :                  "allowed");
    3302            2 :       m = MATCH_ERROR;
    3303            2 :       goto cleanup;
    3304              :     }
    3305              : 
    3306       282371 :   if (gfc_current_state () == COMP_DERIVED
    3307        17488 :       && initializer && initializer->ts.type == BT_HOLLERITH)
    3308              :     {
    3309            1 :       gfc_error ("Initialization of structure component with a HOLLERITH "
    3310              :                  "constant at %L is not allowed", &initializer->where);
    3311            1 :       m = MATCH_ERROR;
    3312            1 :       goto cleanup;
    3313              :     }
    3314              : 
    3315       282370 :   if (gfc_current_state () == COMP_DERIVED
    3316        17487 :       && gfc_current_block ()->attr.pdt_template)
    3317              :     {
    3318         1146 :       gfc_symbol *param;
    3319         1146 :       gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
    3320              :                        0, &param);
    3321         1146 :       if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
    3322              :         {
    3323            1 :           gfc_error ("The component with KIND or LEN attribute at %C does not "
    3324              :                      "not appear in the type parameter list at %L",
    3325            1 :                      &gfc_current_block ()->declared_at);
    3326            1 :           m = MATCH_ERROR;
    3327            4 :           goto cleanup;
    3328              :         }
    3329         1145 :       else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
    3330              :         {
    3331            1 :           gfc_error ("The component at %C that appears in the type parameter "
    3332              :                      "list at %L has neither the KIND nor LEN attribute",
    3333            1 :                      &gfc_current_block ()->declared_at);
    3334            1 :           m = MATCH_ERROR;
    3335            1 :           goto cleanup;
    3336              :         }
    3337         1144 :       else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
    3338              :         {
    3339            1 :           gfc_error ("The component at %C which is a type parameter must be "
    3340              :                      "a scalar");
    3341            1 :           m = MATCH_ERROR;
    3342            1 :           goto cleanup;
    3343              :         }
    3344         1143 :       else if (param && initializer)
    3345              :         {
    3346          247 :           if (initializer->ts.type == BT_BOZ)
    3347              :             {
    3348            1 :               gfc_error ("BOZ literal constant at %L cannot appear as an "
    3349              :                          "initializer", &initializer->where);
    3350            1 :               m = MATCH_ERROR;
    3351            1 :               goto cleanup;
    3352              :             }
    3353          246 :           param->value = gfc_copy_expr (initializer);
    3354              :         }
    3355              :     }
    3356              : 
    3357              :   /* Before adding a possible initializer, do a simple check for compatibility
    3358              :      of lhs and rhs types.  Assigning a REAL value to a derived type is not a
    3359              :      good thing.  */
    3360        28846 :   if (current_ts.type == BT_DERIVED && initializer
    3361       283817 :       && (gfc_numeric_ts (&initializer->ts)
    3362         1449 :           || initializer->ts.type == BT_LOGICAL
    3363         1449 :           || initializer->ts.type == BT_CHARACTER))
    3364              :     {
    3365            2 :       gfc_error ("Incompatible initialization between a derived type "
    3366              :                  "entity and an entity with %qs type at %C",
    3367              :                   gfc_typename (initializer));
    3368            2 :       m = MATCH_ERROR;
    3369            2 :       goto cleanup;
    3370              :     }
    3371              : 
    3372              : 
    3373              :   /* Add the initializer.  Note that it is fine if initializer is
    3374              :      NULL here, because we sometimes also need to check if a
    3375              :      declaration *must* have an initialization expression.  */
    3376       282364 :   if (!gfc_comp_struct (gfc_current_state ()))
    3377       263863 :     t = add_init_expr_to_sym (name, &initializer, &var_locus,
    3378              :                               saved_cl_list);
    3379              :   else
    3380              :     {
    3381        18501 :       if (current_ts.type == BT_DERIVED
    3382         2657 :           && !current_attr.pointer && !initializer)
    3383         2098 :         initializer = gfc_default_initializer (&current_ts);
    3384        18501 :       t = build_struct (name, cl, &initializer, &as);
    3385              : 
    3386              :       /* If we match a nested structure definition we expect to see the
    3387              :        * body even if the variable declarations blow up, so we need to keep
    3388              :        * the structure declaration around.  */
    3389        18501 :       if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
    3390           34 :         gfc_commit_symbol (gfc_new_block);
    3391              :     }
    3392              : 
    3393       282364 :   m = (t) ? MATCH_YES : MATCH_ERROR;
    3394              : 
    3395       282810 : cleanup:
    3396              :   /* Free stuff up and return.  */
    3397       282810 :   gfc_seen_div0 = false;
    3398       282810 :   gfc_free_expr (initializer);
    3399       282810 :   gfc_free_array_spec (as);
    3400              : 
    3401       282810 :   return m;
    3402              : }
    3403              : 
    3404              : 
    3405              : /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
    3406              :    This assumes that the byte size is equal to the kind number for
    3407              :    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
    3408              : 
    3409              : static match
    3410       108132 : gfc_match_old_kind_spec (gfc_typespec *ts)
    3411              : {
    3412       108132 :   match m;
    3413       108132 :   int original_kind;
    3414              : 
    3415       108132 :   if (gfc_match_char ('*') != MATCH_YES)
    3416              :     return MATCH_NO;
    3417              : 
    3418         1150 :   m = gfc_match_small_literal_int (&ts->kind, NULL);
    3419         1150 :   if (m != MATCH_YES)
    3420              :     return MATCH_ERROR;
    3421              : 
    3422         1150 :   original_kind = ts->kind;
    3423              : 
    3424              :   /* Massage the kind numbers for complex types.  */
    3425         1150 :   if (ts->type == BT_COMPLEX)
    3426              :     {
    3427           79 :       if (ts->kind % 2)
    3428              :         {
    3429            0 :           gfc_error ("Old-style type declaration %s*%d not supported at %C",
    3430              :                      gfc_basic_typename (ts->type), original_kind);
    3431            0 :           return MATCH_ERROR;
    3432              :         }
    3433           79 :       ts->kind /= 2;
    3434              : 
    3435              :     }
    3436              : 
    3437         1150 :   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
    3438            0 :     ts->kind = 8;
    3439              : 
    3440         1150 :   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
    3441              :     {
    3442          858 :       if (ts->kind == 4)
    3443              :         {
    3444          224 :           if (flag_real4_kind == 8)
    3445           24 :             ts->kind =  8;
    3446          224 :           if (flag_real4_kind == 10)
    3447           24 :             ts->kind = 10;
    3448          224 :           if (flag_real4_kind == 16)
    3449           24 :             ts->kind = 16;
    3450              :         }
    3451          634 :       else if (ts->kind == 8)
    3452              :         {
    3453          629 :           if (flag_real8_kind == 4)
    3454           24 :             ts->kind = 4;
    3455          629 :           if (flag_real8_kind == 10)
    3456           24 :             ts->kind = 10;
    3457          629 :           if (flag_real8_kind == 16)
    3458           24 :             ts->kind = 16;
    3459              :         }
    3460              :     }
    3461              : 
    3462         1150 :   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
    3463              :     {
    3464            8 :       gfc_error ("Old-style type declaration %s*%d not supported at %C",
    3465              :                  gfc_basic_typename (ts->type), original_kind);
    3466            8 :       return MATCH_ERROR;
    3467              :     }
    3468              : 
    3469         1142 :   if (!gfc_notify_std (GFC_STD_GNU,
    3470              :                        "Nonstandard type declaration %s*%d at %C",
    3471              :                        gfc_basic_typename(ts->type), original_kind))
    3472            0 :     return MATCH_ERROR;
    3473              : 
    3474              :   return MATCH_YES;
    3475              : }
    3476              : 
    3477              : 
    3478              : /* Match a kind specification.  Since kinds are generally optional, we
    3479              :    usually return MATCH_NO if something goes wrong.  If a "kind="
    3480              :    string is found, then we know we have an error.  */
    3481              : 
    3482              : match
    3483       161305 : gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
    3484              : {
    3485       161305 :   locus where, loc;
    3486       161305 :   gfc_expr *e;
    3487       161305 :   match m, n;
    3488       161305 :   char c;
    3489              : 
    3490       161305 :   m = MATCH_NO;
    3491       161305 :   n = MATCH_YES;
    3492       161305 :   e = NULL;
    3493       161305 :   saved_kind_expr = NULL;
    3494              : 
    3495       161305 :   where = loc = gfc_current_locus;
    3496              : 
    3497       161305 :   if (kind_expr_only)
    3498            0 :     goto kind_expr;
    3499              : 
    3500       161305 :   if (gfc_match_char ('(') == MATCH_NO)
    3501              :     return MATCH_NO;
    3502              : 
    3503              :   /* Also gobbles optional text.  */
    3504        51672 :   if (gfc_match (" kind = ") == MATCH_YES)
    3505        51672 :     m = MATCH_ERROR;
    3506              : 
    3507        51672 :   loc = gfc_current_locus;
    3508              : 
    3509        51672 : kind_expr:
    3510              : 
    3511        51672 :   n = gfc_match_init_expr (&e);
    3512              : 
    3513        51672 :   if (gfc_derived_parameter_expr (e))
    3514              :     {
    3515          166 :       ts->kind = 0;
    3516          166 :       saved_kind_expr = gfc_copy_expr (e);
    3517          166 :       goto close_brackets;
    3518              :     }
    3519              : 
    3520        51506 :   if (n != MATCH_YES)
    3521              :     {
    3522          465 :       if (gfc_matching_function)
    3523              :         {
    3524              :           /* The function kind expression might include use associated or
    3525              :              imported parameters and try again after the specification
    3526              :              expressions.....  */
    3527          437 :           if (gfc_match_char (')') != MATCH_YES)
    3528              :             {
    3529            1 :               gfc_error ("Missing right parenthesis at %C");
    3530            1 :               m = MATCH_ERROR;
    3531            1 :               goto no_match;
    3532              :             }
    3533              : 
    3534          436 :           gfc_free_expr (e);
    3535          436 :           gfc_undo_symbols ();
    3536          436 :           return MATCH_YES;
    3537              :         }
    3538              :       else
    3539              :         {
    3540              :           /* ....or else, the match is real.  */
    3541           28 :           if (n == MATCH_NO)
    3542            0 :             gfc_error ("Expected initialization expression at %C");
    3543              :           if (n != MATCH_YES)
    3544              :             return MATCH_ERROR;
    3545              :         }
    3546              :     }
    3547              : 
    3548        51041 :   if (e->rank != 0)
    3549              :     {
    3550            0 :       gfc_error ("Expected scalar initialization expression at %C");
    3551            0 :       m = MATCH_ERROR;
    3552            0 :       goto no_match;
    3553              :     }
    3554              : 
    3555        51041 :   if (gfc_extract_int (e, &ts->kind, 1))
    3556              :     {
    3557            0 :       m = MATCH_ERROR;
    3558            0 :       goto no_match;
    3559              :     }
    3560              : 
    3561              :   /* Before throwing away the expression, let's see if we had a
    3562              :      C interoperable kind (and store the fact).  */
    3563        51041 :   if (e->ts.is_c_interop == 1)
    3564              :     {
    3565              :       /* Mark this as C interoperable if being declared with one
    3566              :          of the named constants from iso_c_binding.  */
    3567        18868 :       ts->is_c_interop = e->ts.is_iso_c;
    3568        18868 :       ts->f90_type = e->ts.f90_type;
    3569        18868 :       if (e->symtree)
    3570        18867 :         ts->interop_kind = e->symtree->n.sym;
    3571              :     }
    3572              : 
    3573        51041 :   gfc_free_expr (e);
    3574        51041 :   e = NULL;
    3575              : 
    3576              :   /* Ignore errors to this point, if we've gotten here.  This means
    3577              :      we ignore the m=MATCH_ERROR from above.  */
    3578        51041 :   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
    3579              :     {
    3580            7 :       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
    3581              :                  gfc_basic_typename (ts->type));
    3582            7 :       gfc_current_locus = where;
    3583            7 :       return MATCH_ERROR;
    3584              :     }
    3585              : 
    3586              :   /* Warn if, e.g., c_int is used for a REAL variable, but not
    3587              :      if, e.g., c_double is used for COMPLEX as the standard
    3588              :      explicitly says that the kind type parameter for complex and real
    3589              :      variable is the same, i.e. c_float == c_float_complex.  */
    3590        51034 :   if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
    3591           17 :       && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
    3592            1 :            || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
    3593           13 :     gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
    3594              :                      "is %s", gfc_basic_typename (ts->f90_type), &where,
    3595              :                      gfc_basic_typename (ts->type));
    3596              : 
    3597        51021 : close_brackets:
    3598              : 
    3599        51200 :   gfc_gobble_whitespace ();
    3600        51200 :   if ((c = gfc_next_ascii_char ()) != ')'
    3601        51200 :       && (ts->type != BT_CHARACTER || c != ','))
    3602              :     {
    3603            0 :       if (ts->type == BT_CHARACTER)
    3604            0 :         gfc_error ("Missing right parenthesis or comma at %C");
    3605              :       else
    3606            0 :         gfc_error ("Missing right parenthesis at %C");
    3607            0 :       m = MATCH_ERROR;
    3608            0 :       goto no_match;
    3609              :     }
    3610              :   else
    3611              :      /* All tests passed.  */
    3612        51200 :      m = MATCH_YES;
    3613              : 
    3614        51200 :   if(m == MATCH_ERROR)
    3615              :      gfc_current_locus = where;
    3616              : 
    3617        51200 :   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
    3618            0 :     ts->kind =  8;
    3619              : 
    3620        51200 :   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
    3621              :     {
    3622        14451 :       if (ts->kind == 4)
    3623              :         {
    3624         4605 :           if (flag_real4_kind == 8)
    3625           54 :             ts->kind =  8;
    3626         4605 :           if (flag_real4_kind == 10)
    3627           54 :             ts->kind = 10;
    3628         4605 :           if (flag_real4_kind == 16)
    3629           54 :             ts->kind = 16;
    3630              :         }
    3631         9846 :       else if (ts->kind == 8)
    3632              :         {
    3633         6690 :           if (flag_real8_kind == 4)
    3634           48 :             ts->kind = 4;
    3635         6690 :           if (flag_real8_kind == 10)
    3636           48 :             ts->kind = 10;
    3637         6690 :           if (flag_real8_kind == 16)
    3638           48 :             ts->kind = 16;
    3639              :         }
    3640              :     }
    3641              : 
    3642              :   /* Return what we know from the test(s).  */
    3643              :   return m;
    3644              : 
    3645            1 : no_match:
    3646            1 :   gfc_free_expr (e);
    3647            1 :   gfc_current_locus = where;
    3648            1 :   return m;
    3649              : }
    3650              : 
    3651              : 
    3652              : static match
    3653         4870 : match_char_kind (int * kind, int * is_iso_c)
    3654              : {
    3655         4870 :   locus where;
    3656         4870 :   gfc_expr *e;
    3657         4870 :   match m, n;
    3658         4870 :   bool fail;
    3659              : 
    3660         4870 :   m = MATCH_NO;
    3661         4870 :   e = NULL;
    3662         4870 :   where = gfc_current_locus;
    3663              : 
    3664         4870 :   n = gfc_match_init_expr (&e);
    3665              : 
    3666         4870 :   if (n != MATCH_YES && gfc_matching_function)
    3667              :     {
    3668              :       /* The expression might include use-associated or imported
    3669              :          parameters and try again after the specification
    3670              :          expressions.  */
    3671            7 :       gfc_free_expr (e);
    3672            7 :       gfc_undo_symbols ();
    3673            7 :       return MATCH_YES;
    3674              :     }
    3675              : 
    3676            7 :   if (n == MATCH_NO)
    3677            2 :     gfc_error ("Expected initialization expression at %C");
    3678         4863 :   if (n != MATCH_YES)
    3679              :     return MATCH_ERROR;
    3680              : 
    3681         4856 :   if (e->rank != 0)
    3682              :     {
    3683            0 :       gfc_error ("Expected scalar initialization expression at %C");
    3684            0 :       m = MATCH_ERROR;
    3685            0 :       goto no_match;
    3686              :     }
    3687              : 
    3688         4856 :   if (gfc_derived_parameter_expr (e))
    3689              :     {
    3690           14 :       saved_kind_expr = e;
    3691           14 :       *kind = 0;
    3692           14 :       return MATCH_YES;
    3693              :     }
    3694              : 
    3695         4842 :   fail = gfc_extract_int (e, kind, 1);
    3696         4842 :   *is_iso_c = e->ts.is_iso_c;
    3697         4842 :   if (fail)
    3698              :     {
    3699            0 :       m = MATCH_ERROR;
    3700            0 :       goto no_match;
    3701              :     }
    3702              : 
    3703         4842 :   gfc_free_expr (e);
    3704              : 
    3705              :   /* Ignore errors to this point, if we've gotten here.  This means
    3706              :      we ignore the m=MATCH_ERROR from above.  */
    3707         4842 :   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
    3708              :     {
    3709           14 :       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
    3710           14 :       m = MATCH_ERROR;
    3711              :     }
    3712              :   else
    3713              :      /* All tests passed.  */
    3714              :      m = MATCH_YES;
    3715              : 
    3716           14 :   if (m == MATCH_ERROR)
    3717           14 :      gfc_current_locus = where;
    3718              : 
    3719              :   /* Return what we know from the test(s).  */
    3720              :   return m;
    3721              : 
    3722            0 : no_match:
    3723            0 :   gfc_free_expr (e);
    3724            0 :   gfc_current_locus = where;
    3725            0 :   return m;
    3726              : }
    3727              : 
    3728              : 
    3729              : /* Match the various kind/length specifications in a CHARACTER
    3730              :    declaration.  We don't return MATCH_NO.  */
    3731              : 
    3732              : match
    3733        32056 : gfc_match_char_spec (gfc_typespec *ts)
    3734              : {
    3735        32056 :   int kind, seen_length, is_iso_c;
    3736        32056 :   gfc_charlen *cl;
    3737        32056 :   gfc_expr *len;
    3738        32056 :   match m;
    3739        32056 :   bool deferred;
    3740              : 
    3741        32056 :   len = NULL;
    3742        32056 :   seen_length = 0;
    3743        32056 :   kind = 0;
    3744        32056 :   is_iso_c = 0;
    3745        32056 :   deferred = false;
    3746              : 
    3747              :   /* Try the old-style specification first.  */
    3748        32056 :   old_char_selector = 0;
    3749              : 
    3750        32056 :   m = match_char_length (&len, &deferred, true);
    3751        32056 :   if (m != MATCH_NO)
    3752              :     {
    3753         2205 :       if (m == MATCH_YES)
    3754         2205 :         old_char_selector = 1;
    3755         2205 :       seen_length = 1;
    3756         2205 :       goto done;
    3757              :     }
    3758              : 
    3759        29851 :   m = gfc_match_char ('(');
    3760        29851 :   if (m != MATCH_YES)
    3761              :     {
    3762         1916 :       m = MATCH_YES;    /* Character without length is a single char.  */
    3763         1916 :       goto done;
    3764              :     }
    3765              : 
    3766              :   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
    3767        27935 :   if (gfc_match (" kind =") == MATCH_YES)
    3768              :     {
    3769         3391 :       m = match_char_kind (&kind, &is_iso_c);
    3770              : 
    3771         3391 :       if (m == MATCH_ERROR)
    3772           16 :         goto done;
    3773         3375 :       if (m == MATCH_NO)
    3774              :         goto syntax;
    3775              : 
    3776         3375 :       if (gfc_match (" , len =") == MATCH_NO)
    3777          518 :         goto rparen;
    3778              : 
    3779         2857 :       m = char_len_param_value (&len, &deferred);
    3780         2857 :       if (m == MATCH_NO)
    3781            0 :         goto syntax;
    3782         2857 :       if (m == MATCH_ERROR)
    3783            2 :         goto done;
    3784         2855 :       seen_length = 1;
    3785              : 
    3786         2855 :       goto rparen;
    3787              :     }
    3788              : 
    3789              :   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
    3790        24544 :   if (gfc_match (" len =") == MATCH_YES)
    3791              :     {
    3792        13981 :       m = char_len_param_value (&len, &deferred);
    3793        13981 :       if (m == MATCH_NO)
    3794            2 :         goto syntax;
    3795        13979 :       if (m == MATCH_ERROR)
    3796            8 :         goto done;
    3797        13971 :       seen_length = 1;
    3798              : 
    3799        13971 :       if (gfc_match_char (')') == MATCH_YES)
    3800        12666 :         goto done;
    3801              : 
    3802         1305 :       if (gfc_match (" , kind =") != MATCH_YES)
    3803            0 :         goto syntax;
    3804              : 
    3805         1305 :       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
    3806            2 :         goto done;
    3807              : 
    3808         1303 :       goto rparen;
    3809              :     }
    3810              : 
    3811              :   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
    3812        10563 :   m = char_len_param_value (&len, &deferred);
    3813        10563 :   if (m == MATCH_NO)
    3814            0 :     goto syntax;
    3815        10563 :   if (m == MATCH_ERROR)
    3816           44 :     goto done;
    3817        10519 :   seen_length = 1;
    3818              : 
    3819        10519 :   m = gfc_match_char (')');
    3820        10519 :   if (m == MATCH_YES)
    3821        10343 :     goto done;
    3822              : 
    3823          176 :   if (gfc_match_char (',') != MATCH_YES)
    3824            2 :     goto syntax;
    3825              : 
    3826          174 :   gfc_match (" kind =");      /* Gobble optional text.  */
    3827              : 
    3828          174 :   m = match_char_kind (&kind, &is_iso_c);
    3829          174 :   if (m == MATCH_ERROR)
    3830            3 :     goto done;
    3831              :   if (m == MATCH_NO)
    3832              :     goto syntax;
    3833              : 
    3834         4847 : rparen:
    3835              :   /* Require a right-paren at this point.  */
    3836         4847 :   m = gfc_match_char (')');
    3837         4847 :   if (m == MATCH_YES)
    3838         4847 :     goto done;
    3839              : 
    3840            0 : syntax:
    3841            4 :   gfc_error ("Syntax error in CHARACTER declaration at %C");
    3842            4 :   m = MATCH_ERROR;
    3843            4 :   gfc_free_expr (len);
    3844            4 :   return m;
    3845              : 
    3846        32052 : done:
    3847              :   /* Deal with character functions after USE and IMPORT statements.  */
    3848        32052 :   if (gfc_matching_function)
    3849              :     {
    3850         1431 :       gfc_free_expr (len);
    3851         1431 :       gfc_undo_symbols ();
    3852         1431 :       return MATCH_YES;
    3853              :     }
    3854              : 
    3855        30621 :   if (m != MATCH_YES)
    3856              :     {
    3857           65 :       gfc_free_expr (len);
    3858           65 :       return m;
    3859              :     }
    3860              : 
    3861              :   /* Do some final massaging of the length values.  */
    3862        30556 :   cl = gfc_new_charlen (gfc_current_ns, NULL);
    3863              : 
    3864        30556 :   if (seen_length == 0)
    3865         2382 :     cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    3866              :   else
    3867              :     {
    3868              :       /* If gfortran ends up here, then len may be reducible to a constant.
    3869              :          Try to do that here.  If it does not reduce, simply assign len to
    3870              :          charlen.  A complication occurs with user-defined generic functions,
    3871              :          which are not resolved.  Use a private namespace to deal with
    3872              :          generic functions.  */
    3873              : 
    3874        28174 :       if (len && len->expr_type != EXPR_CONSTANT)
    3875              :         {
    3876         3057 :           gfc_namespace *old_ns;
    3877         3057 :           gfc_expr *e;
    3878              : 
    3879         3057 :           old_ns = gfc_current_ns;
    3880         3057 :           gfc_current_ns = gfc_get_namespace (NULL, 0);
    3881              : 
    3882         3057 :           e = gfc_copy_expr (len);
    3883         3057 :           gfc_push_suppress_errors ();
    3884         3057 :           gfc_reduce_init_expr (e);
    3885         3057 :           gfc_pop_suppress_errors ();
    3886         3057 :           if (e->expr_type == EXPR_CONSTANT)
    3887              :             {
    3888          294 :               gfc_replace_expr (len, e);
    3889          294 :               if (mpz_cmp_si (len->value.integer, 0) < 0)
    3890            7 :                 mpz_set_ui (len->value.integer, 0);
    3891              :             }
    3892              :           else
    3893         2763 :             gfc_free_expr (e);
    3894              : 
    3895         3057 :           gfc_free_namespace (gfc_current_ns);
    3896         3057 :           gfc_current_ns = old_ns;
    3897              :         }
    3898              : 
    3899        28174 :       cl->length = len;
    3900              :     }
    3901              : 
    3902        30556 :   ts->u.cl = cl;
    3903        30556 :   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
    3904        30556 :   ts->deferred = deferred;
    3905              : 
    3906              :   /* We have to know if it was a C interoperable kind so we can
    3907              :      do accurate type checking of bind(c) procs, etc.  */
    3908        30556 :   if (kind != 0)
    3909              :     /* Mark this as C interoperable if being declared with one
    3910              :        of the named constants from iso_c_binding.  */
    3911         4753 :     ts->is_c_interop = is_iso_c;
    3912        25803 :   else if (len != NULL)
    3913              :     /* Here, we might have parsed something such as: character(c_char)
    3914              :        In this case, the parsing code above grabs the c_char when
    3915              :        looking for the length (line 1690, roughly).  it's the last
    3916              :        testcase for parsing the kind params of a character variable.
    3917              :        However, it's not actually the length.    this seems like it
    3918              :        could be an error.
    3919              :        To see if the user used a C interop kind, test the expr
    3920              :        of the so called length, and see if it's C interoperable.  */
    3921        16621 :     ts->is_c_interop = len->ts.is_iso_c;
    3922              : 
    3923              :   return MATCH_YES;
    3924              : }
    3925              : 
    3926              : 
    3927              : /* Matches a RECORD declaration. */
    3928              : 
    3929              : static match
    3930       972624 : match_record_decl (char *name)
    3931              : {
    3932       972624 :     locus old_loc;
    3933       972624 :     old_loc = gfc_current_locus;
    3934       972624 :     match m;
    3935              : 
    3936       972624 :     m = gfc_match (" record /");
    3937       972624 :     if (m == MATCH_YES)
    3938              :       {
    3939          353 :           if (!flag_dec_structure)
    3940              :             {
    3941            6 :                 gfc_current_locus = old_loc;
    3942            6 :                 gfc_error ("RECORD at %C is an extension, enable it with "
    3943              :                            "%<-fdec-structure%>");
    3944            6 :                 return MATCH_ERROR;
    3945              :             }
    3946          347 :           m = gfc_match (" %n/", name);
    3947          347 :           if (m == MATCH_YES)
    3948              :             return MATCH_YES;
    3949              :       }
    3950              : 
    3951       972274 :   gfc_current_locus = old_loc;
    3952       972274 :   if (flag_dec_structure
    3953       972274 :       && (gfc_match (" record% ") == MATCH_YES
    3954         8026 :           || gfc_match (" record%t") == MATCH_YES))
    3955            6 :     gfc_error ("Structure name expected after RECORD at %C");
    3956       972274 :   if (m == MATCH_NO)
    3957       972274 :     return MATCH_NO;
    3958              : 
    3959              :   return MATCH_ERROR;
    3960              : }
    3961              : 
    3962              : 
    3963              :   /* In parsing a PDT, it is possible that one of the type parameters has the
    3964              :      same name as a previously declared symbol that is not a type parameter.
    3965              :      Intercept this now by looking for the symtree in f2k_derived.  */
    3966              : 
    3967              : static bool
    3968          880 : correct_parm_expr (gfc_expr* e, gfc_symbol* pdt, int* f ATTRIBUTE_UNUSED)
    3969              : {
    3970          880 :   if (!e || (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION))
    3971              :     return false;
    3972              : 
    3973          711 :   if (!(e->symtree->n.sym->attr.pdt_len
    3974          122 :         || e->symtree->n.sym->attr.pdt_kind))
    3975              :     {
    3976           38 :       gfc_symtree *st;
    3977           38 :       st = gfc_find_symtree (pdt->f2k_derived->sym_root,
    3978              :                              e->symtree->n.sym->name);
    3979           38 :       if (st && st->n.sym
    3980           30 :           && (st->n.sym->attr.pdt_len || st->n.sym->attr.pdt_kind))
    3981              :         {
    3982           30 :           gfc_expr *new_expr;
    3983           30 :           gfc_set_sym_referenced (st->n.sym);
    3984           30 :           new_expr = gfc_get_expr ();
    3985           30 :           new_expr->ts = st->n.sym->ts;
    3986           30 :           new_expr->expr_type = EXPR_VARIABLE;
    3987           30 :           new_expr->symtree = st;
    3988           30 :           new_expr->where = e->where;
    3989           30 :           gfc_replace_expr (e, new_expr);
    3990              :         }
    3991              :     }
    3992              : 
    3993              :   return false;
    3994              : }
    3995              : 
    3996              : 
    3997              : void
    3998          648 : gfc_correct_parm_expr (gfc_symbol *pdt, gfc_expr **bound)
    3999              : {
    4000          648 :   if (!*bound || (*bound)->expr_type == EXPR_CONSTANT)
    4001              :     return;
    4002          617 :   gfc_traverse_expr (*bound, pdt, &correct_parm_expr, 0);
    4003              : }
    4004              : 
    4005              : /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
    4006              :    of expressions to substitute into the possibly parameterized expression
    4007              :    'e'. Using a list is inefficient but should not be too bad since the
    4008              :    number of type parameters is not likely to be large.  */
    4009              : static bool
    4010         3205 : insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    4011              :                         int* f)
    4012              : {
    4013         3205 :   gfc_actual_arglist *param;
    4014         3205 :   gfc_expr *copy;
    4015              : 
    4016         3205 :   if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
    4017              :     return false;
    4018              : 
    4019         1429 :   gcc_assert (e->symtree);
    4020         1429 :   if (e->symtree->n.sym->attr.pdt_kind
    4021         1050 :       || (*f != 0 && e->symtree->n.sym->attr.pdt_len)
    4022          513 :       || (e->expr_type == EXPR_FUNCTION && e->symtree->n.sym))
    4023              :     {
    4024         1414 :       for (param = type_param_spec_list; param; param = param->next)
    4025         1366 :         if (!strcmp (e->symtree->n.sym->name, param->name))
    4026              :           break;
    4027              : 
    4028          963 :       if (param && param->expr)
    4029              :         {
    4030          914 :           copy = gfc_copy_expr (param->expr);
    4031          914 :           gfc_replace_expr (e, copy);
    4032              :           /* Catch variables declared without a value expression.  */
    4033          914 :           if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_PROCEDURE)
    4034           21 :             e->ts = e->symtree->n.sym->ts;
    4035              :         }
    4036              :     }
    4037              : 
    4038              :   return false;
    4039              : }
    4040              : 
    4041              : 
    4042              : static bool
    4043          953 : gfc_insert_kind_parameter_exprs (gfc_expr *e)
    4044              : {
    4045          953 :   return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
    4046              : }
    4047              : 
    4048              : 
    4049              : bool
    4050         1803 : gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
    4051              : {
    4052         1803 :   gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
    4053         1803 :   type_param_spec_list = param_list;
    4054         1803 :   bool res = gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
    4055         1803 :   type_param_spec_list = old_param_spec_list;
    4056         1803 :   return res;
    4057              : }
    4058              : 
    4059              : /* Determines the instance of a parameterized derived type to be used by
    4060              :    matching determining the values of the kind parameters and using them
    4061              :    in the name of the instance. If the instance exists, it is used, otherwise
    4062              :    a new derived type is created.  */
    4063              : match
    4064         2795 : gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
    4065              :                       gfc_actual_arglist **ext_param_list)
    4066              : {
    4067              :   /* The PDT template symbol.  */
    4068         2795 :   gfc_symbol *pdt = *sym;
    4069              :   /* The symbol for the parameter in the template f2k_namespace.  */
    4070         2795 :   gfc_symbol *param;
    4071              :   /* The hoped for instance of the PDT.  */
    4072         2795 :   gfc_symbol *instance = NULL;
    4073              :   /* The list of parameters appearing in the PDT declaration.  */
    4074         2795 :   gfc_formal_arglist *type_param_name_list;
    4075              :   /* Used to store the parameter specification list during recursive calls.  */
    4076         2795 :   gfc_actual_arglist *old_param_spec_list;
    4077              :   /* Pointers to the parameter specification being used.  */
    4078         2795 :   gfc_actual_arglist *actual_param;
    4079         2795 :   gfc_actual_arglist *tail = NULL;
    4080              :   /* Used to build up the name of the PDT instance.  */
    4081         2795 :   char *name;
    4082         2795 :   bool name_seen = (param_list == NULL);
    4083         2795 :   bool assumed_seen = false;
    4084         2795 :   bool deferred_seen = false;
    4085         2795 :   bool spec_error = false;
    4086         2795 :   bool alloc_seen = false;
    4087         2795 :   bool ptr_seen = false;
    4088         2795 :   int i;
    4089         2795 :   gfc_expr *kind_expr;
    4090         2795 :   gfc_component *c1, *c2;
    4091         2795 :   match m;
    4092         2795 :   gfc_symtree *s = NULL;
    4093              : 
    4094         2795 :   type_param_spec_list = NULL;
    4095              : 
    4096         2795 :   type_param_name_list = pdt->formal;
    4097         2795 :   actual_param = param_list;
    4098              : 
    4099              :   /* Prevent a PDT component of the same type as the template from being
    4100              :      converted into an instance. Doing this results in the component being
    4101              :      lost.  */
    4102         2795 :   if (gfc_current_state () == COMP_DERIVED
    4103          113 :       && !(gfc_state_stack->previous
    4104          113 :            && gfc_state_stack->previous->state == COMP_DERIVED)
    4105          113 :       && gfc_current_block ()->attr.pdt_template)
    4106              :     {
    4107          100 :       if (ext_param_list)
    4108          100 :         *ext_param_list = gfc_copy_actual_arglist (param_list);
    4109              :       return MATCH_YES;
    4110              :     }
    4111              : 
    4112         2695 :   name = xasprintf ("%s%s", PDT_PREFIX, pdt->name);
    4113              : 
    4114              :   /* Run through the parameter name list and pick up the actual
    4115              :      parameter values or use the default values in the PDT declaration.  */
    4116         8960 :   for (; type_param_name_list;
    4117         3570 :        type_param_name_list = type_param_name_list->next)
    4118              :     {
    4119         3638 :       if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
    4120              :         {
    4121         3236 :           if (actual_param->spec_type == SPEC_ASSUMED)
    4122              :             spec_error = deferred_seen;
    4123              :           else
    4124         3236 :             spec_error = assumed_seen;
    4125              : 
    4126         3236 :           if (spec_error)
    4127              :             {
    4128              :               gfc_error ("The type parameter spec list at %C cannot contain "
    4129              :                          "both ASSUMED and DEFERRED parameters");
    4130              :               goto error_return;
    4131              :             }
    4132              :         }
    4133              : 
    4134         3236 :       if (actual_param && actual_param->name)
    4135         3638 :         name_seen = true;
    4136         3638 :       param = type_param_name_list->sym;
    4137              : 
    4138         3638 :       if (!param || !param->name)
    4139            2 :         continue;
    4140              : 
    4141         3636 :       c1 = gfc_find_component (pdt, param->name, false, true, NULL);
    4142              :       /* An error should already have been thrown in resolve.cc
    4143              :          (resolve_fl_derived0).  */
    4144         3636 :       if (!pdt->attr.use_assoc && !c1)
    4145            8 :         goto error_return;
    4146              : 
    4147              :       /* Resolution PDT class components of derived types are handled here.
    4148              :          They can arrive without a parameter list and no KIND parameters.  */
    4149         3628 :       if (!param_list && (!c1->attr.pdt_kind && !c1->initializer))
    4150           20 :         continue;
    4151              : 
    4152         3608 :       kind_expr = NULL;
    4153         3608 :       if (!name_seen)
    4154              :         {
    4155         2116 :           if (!actual_param && !(c1 && c1->initializer))
    4156              :             {
    4157            2 :               gfc_error ("The type parameter spec list at %C does not contain "
    4158              :                          "enough parameter expressions");
    4159            2 :               goto error_return;
    4160              :             }
    4161         2114 :           else if (!actual_param && c1 && c1->initializer)
    4162            5 :             kind_expr = gfc_copy_expr (c1->initializer);
    4163         2109 :           else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
    4164         1866 :             kind_expr = gfc_copy_expr (actual_param->expr);
    4165              :         }
    4166              :       else
    4167              :         {
    4168              :           actual_param = param_list;
    4169         1952 :           for (;actual_param; actual_param = actual_param->next)
    4170         1562 :             if (actual_param->name
    4171         1542 :                 && strcmp (actual_param->name, param->name) == 0)
    4172              :               break;
    4173         1492 :           if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
    4174          935 :             kind_expr = gfc_copy_expr (actual_param->expr);
    4175              :           else
    4176              :             {
    4177          557 :               if (c1->initializer)
    4178          493 :                 kind_expr = gfc_copy_expr (c1->initializer);
    4179           64 :               else if (!(actual_param && param->attr.pdt_len))
    4180              :                 {
    4181            9 :                   gfc_error ("The derived parameter %qs at %C does not "
    4182              :                              "have a default value", param->name);
    4183            9 :                   goto error_return;
    4184              :                 }
    4185              :             }
    4186              :         }
    4187              : 
    4188         3299 :       if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
    4189          282 :           && kind_expr->ts.type != BT_INTEGER
    4190          136 :           && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
    4191              :         {
    4192           12 :           gfc_error ("The type parameter expression at %L must be of INTEGER "
    4193              :                      "type and not %s", &kind_expr->where,
    4194              :                      gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
    4195           12 :           goto error_return;
    4196              :         }
    4197              : 
    4198              :       /* Store the current parameter expressions in a temporary actual
    4199              :          arglist 'list' so that they can be substituted in the corresponding
    4200              :          expressions in the PDT instance.  */
    4201         3585 :       if (type_param_spec_list == NULL)
    4202              :         {
    4203         2652 :           type_param_spec_list = gfc_get_actual_arglist ();
    4204         2652 :           tail = type_param_spec_list;
    4205              :         }
    4206              :       else
    4207              :         {
    4208          933 :           tail->next = gfc_get_actual_arglist ();
    4209          933 :           tail = tail->next;
    4210              :         }
    4211         3585 :       tail->name = param->name;
    4212              : 
    4213         3585 :       if (kind_expr)
    4214              :         {
    4215              :           /* Try simplification even for LEN expressions.  */
    4216         3287 :           bool ok;
    4217         3287 :           gfc_resolve_expr (kind_expr);
    4218              : 
    4219         3287 :           if (c1->attr.pdt_kind
    4220         1646 :               && kind_expr->expr_type != EXPR_CONSTANT
    4221           28 :               && type_param_spec_list)
    4222           28 :           gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
    4223              : 
    4224         3287 :           ok = gfc_simplify_expr (kind_expr, 1);
    4225              :           /* Variable expressions default to BT_PROCEDURE in the absence of an
    4226              :              initializer so allow for this.  */
    4227         3287 :           if (kind_expr->ts.type != BT_INTEGER
    4228          153 :               && kind_expr->ts.type != BT_PROCEDURE)
    4229              :             {
    4230           29 :               gfc_error ("The parameter expression at %C must be of "
    4231              :                          "INTEGER type and not %s type",
    4232              :                          gfc_basic_typename (kind_expr->ts.type));
    4233           29 :               goto error_return;
    4234              :             }
    4235         3258 :           if (kind_expr->ts.type == BT_INTEGER && !ok)
    4236              :             {
    4237            4 :               gfc_error ("The parameter expression at %C does not "
    4238              :                          "simplify to an INTEGER constant");
    4239            4 :               goto error_return;
    4240              :             }
    4241              : 
    4242         3254 :           tail->expr = gfc_copy_expr (kind_expr);
    4243              :         }
    4244              : 
    4245         3552 :       if (actual_param)
    4246         3164 :         tail->spec_type = actual_param->spec_type;
    4247              : 
    4248         3552 :       if (!param->attr.pdt_kind)
    4249              :         {
    4250         1931 :           if (!name_seen && actual_param)
    4251         1162 :             actual_param = actual_param->next;
    4252         1931 :           if (kind_expr)
    4253              :             {
    4254         1635 :               gfc_free_expr (kind_expr);
    4255         1635 :               kind_expr = NULL;
    4256              :             }
    4257         1931 :           continue;
    4258              :         }
    4259              : 
    4260         1621 :       if (actual_param
    4261         1277 :           && (actual_param->spec_type == SPEC_ASSUMED
    4262         1277 :               || actual_param->spec_type == SPEC_DEFERRED))
    4263              :         {
    4264            2 :           gfc_error ("The KIND parameter %qs at %C cannot either be "
    4265              :                      "ASSUMED or DEFERRED", param->name);
    4266            2 :           goto error_return;
    4267              :         }
    4268              : 
    4269         1619 :       if (!kind_expr || !gfc_is_constant_expr (kind_expr))
    4270              :         {
    4271            2 :           gfc_error ("The value for the KIND parameter %qs at %C does not "
    4272              :                      "reduce to a constant expression", param->name);
    4273            2 :           goto error_return;
    4274              :         }
    4275              : 
    4276              :       /* This can come about during the parsing of nested pdt_templates. An
    4277              :          error arises because the KIND parameter expression has not been
    4278              :          provided. Use the template instead of an incorrect instance.  */
    4279         1617 :       if (kind_expr->expr_type != EXPR_CONSTANT
    4280         1617 :           || kind_expr->ts.type != BT_INTEGER)
    4281              :         {
    4282            0 :           gfc_free_actual_arglist (type_param_spec_list);
    4283            0 :           free (name);
    4284            0 :           return MATCH_YES;
    4285              :         }
    4286              : 
    4287         1617 :       char *kind_value = mpz_get_str (NULL, 10, kind_expr->value.integer);
    4288         1617 :       char *old_name = name;
    4289         1617 :       name = xasprintf ("%s_%s", old_name, kind_value);
    4290         1617 :       free (old_name);
    4291         1617 :       free (kind_value);
    4292              : 
    4293         1617 :       if (!name_seen && actual_param)
    4294          898 :         actual_param = actual_param->next;
    4295         1617 :       gfc_free_expr (kind_expr);
    4296              :     }
    4297              : 
    4298         2627 :   if (!name_seen && actual_param)
    4299              :     {
    4300            2 :       gfc_error ("The type parameter spec list at %C contains too many "
    4301              :                  "parameter expressions");
    4302            2 :       goto error_return;
    4303              :     }
    4304              : 
    4305              :   /* Now we search for the PDT instance 'name'. If it doesn't exist, we
    4306              :      build it, using 'pdt' as a template.  */
    4307         2625 :   if (gfc_get_symbol (name, pdt->ns, &instance))
    4308              :     {
    4309            0 :       gfc_error ("Parameterized derived type at %C is ambiguous");
    4310            0 :       goto error_return;
    4311              :     }
    4312              : 
    4313              :   /* If we are in an interface body, the instance will not have been imported.
    4314              :      Make sure that it is imported implicitly.  */
    4315         2625 :   s = gfc_find_symtree (gfc_current_ns->sym_root, pdt->name);
    4316         2625 :   if (gfc_current_ns->proc_name
    4317         2578 :       && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
    4318           93 :       && s && s->import_only && pdt->attr.imported)
    4319              :     {
    4320            2 :       s = gfc_find_symtree (gfc_current_ns->sym_root, instance->name);
    4321            2 :       if (!s)
    4322              :         {
    4323            1 :           gfc_get_sym_tree (instance->name, gfc_current_ns, &s, false,
    4324              :                             &gfc_current_locus);
    4325            1 :           s->n.sym = instance;
    4326              :         }
    4327            2 :       s->n.sym->attr.imported = 1;
    4328            2 :       s->import_only = 1;
    4329              :     }
    4330              : 
    4331         2625 :   m = MATCH_YES;
    4332              : 
    4333         2625 :   if (instance->attr.flavor == FL_DERIVED
    4334         2086 :       && instance->attr.pdt_type
    4335         2086 :       && instance->components)
    4336              :     {
    4337         2086 :       instance->refs++;
    4338         2086 :       if (ext_param_list)
    4339          990 :         *ext_param_list = type_param_spec_list;
    4340         2086 :       *sym = instance;
    4341         2086 :       gfc_commit_symbols ();
    4342         2086 :       free (name);
    4343         2086 :       return m;
    4344              :     }
    4345              : 
    4346              :   /* Start building the new instance of the parameterized type.  */
    4347          539 :   gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
    4348          539 :   if (pdt->attr.use_assoc)
    4349           60 :     instance->module = pdt->module;
    4350          539 :   instance->attr.pdt_template = 0;
    4351          539 :   instance->attr.pdt_type = 1;
    4352          539 :   instance->declared_at = gfc_current_locus;
    4353              : 
    4354              :   /* In resolution, the finalizers are copied, according to the type of the
    4355              :      argument, to the instance finalizers. However, they are retained by the
    4356              :      template and procedures are freed there.  */
    4357          539 :   if (pdt->f2k_derived && pdt->f2k_derived->finalizers)
    4358              :     {
    4359           24 :       instance->f2k_derived = gfc_get_namespace (NULL, 0);
    4360           24 :       instance->template_sym = pdt;
    4361           24 :       *instance->f2k_derived = *pdt->f2k_derived;
    4362              :     }
    4363              : 
    4364              :   /* Add the components, replacing the parameters in all expressions
    4365              :      with the expressions for their values in 'type_param_spec_list'.  */
    4366          539 :   c1 = pdt->components;
    4367          539 :   tail = type_param_spec_list;
    4368         1972 :   for (; c1; c1 = c1->next)
    4369              :     {
    4370         1435 :       gfc_add_component (instance, c1->name, &c2);
    4371              : 
    4372         1435 :       c2->ts = c1->ts;
    4373         1435 :       c2->attr = c1->attr;
    4374         1435 :       if (c1->tb)
    4375              :         {
    4376            6 :           c2->tb = gfc_get_tbp ();
    4377            6 :           *c2->tb = *c1->tb;
    4378              :         }
    4379              : 
    4380              :       /* The order of declaration of the type_specs might not be the
    4381              :          same as that of the components.  */
    4382         1435 :       if (c1->attr.pdt_kind || c1->attr.pdt_len)
    4383              :         {
    4384         1010 :           for (tail = type_param_spec_list; tail; tail = tail->next)
    4385         1006 :             if (strcmp (c1->name, tail->name) == 0)
    4386              :               break;
    4387              :         }
    4388              : 
    4389              :       /* Deal with type extension by recursively calling this function
    4390              :          to obtain the instance of the extended type.  */
    4391         1435 :       if (gfc_current_state () != COMP_DERIVED
    4392         1421 :           && c1 == pdt->components
    4393          526 :           && c1->ts.type == BT_DERIVED
    4394           48 :           && c1->ts.u.derived
    4395         1483 :           && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
    4396              :         {
    4397           48 :           if (c1->ts.u.derived->attr.pdt_template)
    4398              :             {
    4399           41 :               gfc_formal_arglist *f;
    4400              : 
    4401           41 :               old_param_spec_list = type_param_spec_list;
    4402              : 
    4403              :               /* Obtain a spec list appropriate to the extended type..*/
    4404           41 :               actual_param = gfc_copy_actual_arglist (type_param_spec_list);
    4405           41 :               type_param_spec_list = actual_param;
    4406           73 :               for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
    4407           32 :                 actual_param = actual_param->next;
    4408           41 :               if (actual_param)
    4409              :                 {
    4410           41 :                   gfc_free_actual_arglist (actual_param->next);
    4411           41 :                   actual_param->next = NULL;
    4412              :                 }
    4413              : 
    4414              :               /* Now obtain the PDT instance for the extended type.  */
    4415           41 :               c2->param_list = type_param_spec_list;
    4416           41 :               m = gfc_get_pdt_instance (type_param_spec_list,
    4417              :                                         &c2->ts.u.derived,
    4418              :                                         &c2->param_list);
    4419           41 :               type_param_spec_list = old_param_spec_list;
    4420              :             }
    4421              :           else
    4422            7 :             c2->ts = c1->ts;
    4423              : 
    4424           48 :           c2->ts.u.derived->refs++;
    4425           48 :           gfc_set_sym_referenced (c2->ts.u.derived);
    4426              : 
    4427              :           /* If the component is allocatable or the parent has allocatable
    4428              :              components, make sure that the new instance also is marked as
    4429              :              having allocatable components.  */
    4430           48 :           if (c2->attr.allocatable || c2->ts.u.derived->attr.alloc_comp)
    4431            6 :             instance->attr.alloc_comp = 1;
    4432              : 
    4433              :           /* Set extension level.  */
    4434           48 :           if (c2->ts.u.derived->attr.extension == 255)
    4435              :             {
    4436              :               /* Since the extension field is 8 bit wide, we can only have
    4437              :                  up to 255 extension levels.  */
    4438            0 :               gfc_error ("Maximum extension level reached with type %qs at %L",
    4439              :                          c2->ts.u.derived->name,
    4440              :                          &c2->ts.u.derived->declared_at);
    4441            0 :               goto error_return;
    4442              :             }
    4443           48 :           instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
    4444              : 
    4445           48 :           continue;
    4446           48 :         }
    4447              : 
    4448              :       /* Addressing PR82943, this will fix the issue where a function or
    4449              :          subroutine is declared as not a member of the PDT instance.
    4450              :          The reason for this is because the PDT instance did not have access
    4451              :          to its template's f2k_derived namespace in order to find the
    4452              :          typebound procedures.
    4453              : 
    4454              :          The number of references to the PDT template's f2k_derived will
    4455              :          ensure that f2k_derived is properly freed later on.  */
    4456              : 
    4457         1387 :       if (!instance->f2k_derived && pdt->f2k_derived)
    4458              :         {
    4459          508 :           instance->f2k_derived = pdt->f2k_derived;
    4460          508 :           instance->f2k_derived->refs++;
    4461              :         }
    4462              : 
    4463              :       /* Set the component kind using the parameterized expression.  */
    4464         1387 :       if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
    4465          471 :            && c1->kind_expr != NULL)
    4466              :         {
    4467          278 :           gfc_expr *e = gfc_copy_expr (c1->kind_expr);
    4468          278 :           gfc_insert_kind_parameter_exprs (e);
    4469          278 :           gfc_simplify_expr (e, 1);
    4470          278 :           gfc_extract_int (e, &c2->ts.kind);
    4471          278 :           gfc_free_expr (e);
    4472          278 :           if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
    4473              :             {
    4474            2 :               gfc_error ("Kind %d not supported for type %s at %C",
    4475              :                          c2->ts.kind, gfc_basic_typename (c2->ts.type));
    4476            2 :               goto error_return;
    4477              :             }
    4478          276 :           if (c2->attr.proc_pointer && c2->attr.function
    4479            0 :               && c1->ts.interface && c1->ts.interface->ts.kind == 0)
    4480              :             {
    4481            0 :               c2->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    4482            0 :               c2->ts.interface->result = c2->ts.interface;
    4483            0 :               c2->ts.interface->ts = c2->ts;
    4484            0 :               c2->ts.interface->attr.flavor = FL_PROCEDURE;
    4485            0 :               c2->ts.interface->attr.function = 1;
    4486            0 :               c2->attr.function = 1;
    4487            0 :               c2->attr.if_source = IFSRC_UNKNOWN;
    4488              :             }
    4489              :         }
    4490              : 
    4491              :       /* Set up either the KIND/LEN initializer, if constant,
    4492              :          or the parameterized expression. Use the template
    4493              :          initializer if one is not already set in this instance.  */
    4494         1385 :       if (c2->attr.pdt_kind || c2->attr.pdt_len)
    4495              :         {
    4496          718 :           if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
    4497          590 :             c2->initializer = gfc_copy_expr (tail->expr);
    4498          128 :           else if (tail && tail->expr)
    4499              :             {
    4500           10 :               c2->param_list = gfc_get_actual_arglist ();
    4501           10 :               c2->param_list->name = tail->name;
    4502           10 :               c2->param_list->expr = gfc_copy_expr (tail->expr);
    4503           10 :               c2->param_list->next = NULL;
    4504              :             }
    4505              : 
    4506          718 :           if (!c2->initializer && c1->initializer)
    4507           24 :             c2->initializer = gfc_copy_expr (c1->initializer);
    4508              : 
    4509          718 :           if (c2->initializer)
    4510          614 :             gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4511              :         }
    4512              : 
    4513              :       /* Copy the array spec.  */
    4514         1385 :       c2->as = gfc_copy_array_spec (c1->as);
    4515         1385 :       if (c1->ts.type == BT_CLASS)
    4516            0 :         CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
    4517              : 
    4518         1385 :       if (c1->attr.allocatable)
    4519           76 :         alloc_seen = true;
    4520              : 
    4521         1385 :       if (c1->attr.pointer)
    4522           20 :         ptr_seen = true;
    4523              : 
    4524              :       /* Determine if an array spec is parameterized. If so, substitute
    4525              :          in the parameter expressions for the bounds and set the pdt_array
    4526              :          attribute. Notice that this attribute must be unconditionally set
    4527              :          if this is an array of parameterized character length.  */
    4528         1385 :       if (c1->as && c1->as->type == AS_EXPLICIT)
    4529              :         {
    4530              :           bool pdt_array = false;
    4531          514 :           bool all_constant = true;
    4532              : 
    4533              :           /* Are the bounds of the array parameterized?  */
    4534          514 :           for (i = 0; i < c1->as->rank; i++)
    4535              :             {
    4536          305 :               if (gfc_derived_parameter_expr (c1->as->lower[i]))
    4537            6 :                 pdt_array = true;
    4538          305 :               if (gfc_derived_parameter_expr (c1->as->upper[i]))
    4539          291 :                 pdt_array = true;
    4540              :             }
    4541              : 
    4542              :           /* If they are, free the expressions for the bounds and
    4543              :              replace them with the template expressions with substitute
    4544              :              values.  */
    4545          500 :           for (i = 0; pdt_array && i < c1->as->rank; i++)
    4546              :             {
    4547          291 :               gfc_expr *e;
    4548          291 :               e = gfc_copy_expr (c1->as->lower[i]);
    4549          291 :               gfc_insert_kind_parameter_exprs (e);
    4550          291 :               if (gfc_simplify_expr (e, 1))
    4551          291 :                 gfc_replace_expr (c2->as->lower[i], e);
    4552              :               else
    4553            0 :                 gfc_free_expr (e);
    4554          291 :               if (c2->as->lower[i]->expr_type != EXPR_CONSTANT)
    4555            6 :                 all_constant = false;
    4556          291 :               e = gfc_copy_expr (c1->as->upper[i]);
    4557          291 :               gfc_insert_kind_parameter_exprs (e);
    4558          291 :               if (gfc_simplify_expr (e, 1))
    4559          291 :                 gfc_replace_expr (c2->as->upper[i], e);
    4560              :               else
    4561            0 :                 gfc_free_expr (e);
    4562          291 :               if (c2->as->upper[i]->expr_type != EXPR_CONSTANT)
    4563          289 :                 all_constant = false;
    4564              :             }
    4565              : 
    4566          209 :           c2->attr.pdt_array = all_constant ? 0 : 1;
    4567          209 :           if (c1->initializer)
    4568              :             {
    4569            7 :               c2->initializer = gfc_copy_expr (c1->initializer);
    4570            7 :               gfc_insert_kind_parameter_exprs (c2->initializer);
    4571            7 :               gfc_simplify_expr (c2->initializer, 1);
    4572              :             }
    4573              :         }
    4574              : 
    4575              :       /* Similarly, set the string length if parameterized.  */
    4576         1385 :       if (c1->ts.type == BT_CHARACTER
    4577           87 :           && c1->ts.u.cl->length
    4578         1471 :           && gfc_derived_parameter_expr (c1->ts.u.cl->length))
    4579              :         {
    4580           86 :           gfc_expr *e;
    4581           86 :           e = gfc_copy_expr (c1->ts.u.cl->length);
    4582           86 :           gfc_insert_kind_parameter_exprs (e);
    4583           86 :           if (gfc_simplify_expr (e, 1))
    4584           86 :             gfc_replace_expr (c2->ts.u.cl->length, e);
    4585              :           else
    4586            0 :             gfc_free_expr (e);
    4587           86 :           if (c2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    4588           83 :             c2->attr.pdt_string = 1;
    4589              :         }
    4590              : 
    4591              :       /* Recurse into this function for PDT components.  */
    4592         1385 :       if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
    4593          131 :           && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
    4594              :         {
    4595          123 :           gfc_actual_arglist *params;
    4596              :           /* The component in the template has a list of specification
    4597              :              expressions derived from its declaration.  */
    4598          123 :           params = gfc_copy_actual_arglist (c1->param_list);
    4599          123 :           actual_param = params;
    4600              :           /* Substitute the template parameters with the expressions
    4601              :              from the specification list.  */
    4602          384 :           for (;actual_param; actual_param = actual_param->next)
    4603              :             {
    4604          138 :               gfc_correct_parm_expr (pdt, &actual_param->expr);
    4605          138 :               gfc_insert_parameter_exprs (actual_param->expr,
    4606              :                                           type_param_spec_list);
    4607              :             }
    4608              : 
    4609              :           /* Now obtain the PDT instance for the component.  */
    4610          123 :           old_param_spec_list = type_param_spec_list;
    4611          246 :           m = gfc_get_pdt_instance (params, &c2->ts.u.derived,
    4612          123 :                                     &c2->param_list);
    4613          123 :           type_param_spec_list = old_param_spec_list;
    4614              : 
    4615          123 :           if (!(c2->attr.pointer || c2->attr.allocatable))
    4616              :             {
    4617           83 :               if (!c1->initializer
    4618           58 :                   || c1->initializer->expr_type != EXPR_FUNCTION)
    4619           82 :                 c2->initializer = gfc_default_initializer (&c2->ts);
    4620              :               else
    4621              :                 {
    4622            1 :                   gfc_symtree *s;
    4623            1 :                   c2->initializer = gfc_copy_expr (c1->initializer);
    4624            1 :                   s = gfc_find_symtree (pdt->ns->sym_root,
    4625            1 :                                 gfc_dt_lower_string (c2->ts.u.derived->name));
    4626            1 :                   if (s)
    4627            0 :                     c2->initializer->symtree = s;
    4628            1 :                   c2->initializer->ts = c2->ts;
    4629            1 :                   if (!s)
    4630            1 :                     gfc_insert_parameter_exprs (c2->initializer,
    4631              :                                                 type_param_spec_list);
    4632            1 :                   gfc_simplify_expr (c2->initializer, 1);
    4633              :                 }
    4634              :             }
    4635              : 
    4636          123 :           if (c2->attr.allocatable
    4637           91 :               || (c2->ts.type == BT_DERIVED && c2->ts.u.derived
    4638           91 :                   && c2->ts.u.derived->attr.alloc_comp && !c2->attr.pointer))
    4639           61 :             instance->attr.alloc_comp = 1;
    4640              :         }
    4641         1262 :       else if (!(c2->attr.pdt_kind || c2->attr.pdt_len || c2->attr.pdt_string
    4642          461 :                  || c2->attr.pdt_array) && c1->initializer)
    4643              :         {
    4644           32 :           c2->initializer = gfc_copy_expr (c1->initializer);
    4645           32 :           if (c2->initializer->ts.type == BT_UNKNOWN)
    4646           12 :             c2->initializer->ts = c2->ts;
    4647           32 :           gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4648              :           /* The template initializers are parsed using gfc_match_expr rather
    4649              :              than gfc_match_init_expr. Apply the missing reduction to the
    4650              :              PDT instance initializers.  */
    4651           32 :           if (!gfc_reduce_init_expr (c2->initializer))
    4652              :             {
    4653            0 :               gfc_free_expr (c2->initializer);
    4654            0 :               goto error_return;
    4655              :             }
    4656           32 :           gfc_simplify_expr (c2->initializer, 1);
    4657              :         }
    4658              :     }
    4659              : 
    4660          537 :   if (alloc_seen)
    4661           73 :     instance->attr.alloc_comp = 1;
    4662          537 :   if (ptr_seen)
    4663           20 :     instance->attr.pointer_comp = 1;
    4664              : 
    4665              : 
    4666          537 :   gfc_commit_symbol (instance);
    4667          537 :   if (ext_param_list)
    4668          330 :     *ext_param_list = type_param_spec_list;
    4669          537 :   *sym = instance;
    4670          537 :   free (name);
    4671          537 :   return m;
    4672              : 
    4673           72 : error_return:
    4674           72 :   gfc_free_actual_arglist (type_param_spec_list);
    4675           72 :   free (name);
    4676           72 :   return MATCH_ERROR;
    4677              : }
    4678              : 
    4679              : 
    4680              : /* Match a legacy nonstandard BYTE type-spec.  */
    4681              : 
    4682              : static match
    4683      1196184 : match_byte_typespec (gfc_typespec *ts)
    4684              : {
    4685      1196184 :   if (gfc_match (" byte") == MATCH_YES)
    4686              :     {
    4687           33 :       if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
    4688              :         return MATCH_ERROR;
    4689              : 
    4690           31 :       if (gfc_current_form == FORM_FREE)
    4691              :         {
    4692           19 :           char c = gfc_peek_ascii_char ();
    4693           19 :           if (!gfc_is_whitespace (c) && c != ',')
    4694              :             return MATCH_NO;
    4695              :         }
    4696              : 
    4697           29 :       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
    4698              :         {
    4699            0 :           gfc_error ("BYTE type used at %C "
    4700              :                      "is not available on the target machine");
    4701            0 :           return MATCH_ERROR;
    4702              :         }
    4703              : 
    4704           29 :       ts->type = BT_INTEGER;
    4705           29 :       ts->kind = 1;
    4706           29 :       return MATCH_YES;
    4707              :     }
    4708              :   return MATCH_NO;
    4709              : }
    4710              : 
    4711              : 
    4712              : /* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
    4713              :    structure to the matched specification.  This is necessary for FUNCTION and
    4714              :    IMPLICIT statements.
    4715              : 
    4716              :    If implicit_flag is nonzero, then we don't check for the optional
    4717              :    kind specification.  Not doing so is needed for matching an IMPLICIT
    4718              :    statement correctly.  */
    4719              : 
    4720              : match
    4721      1196184 : gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
    4722              : {
    4723              :   /* Provide sufficient space to hold "pdtsymbol".  */
    4724      1196184 :   char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
    4725      1196184 :   gfc_symbol *sym, *dt_sym;
    4726      1196184 :   match m;
    4727      1196184 :   char c;
    4728      1196184 :   bool seen_deferred_kind, matched_type;
    4729      1196184 :   const char *dt_name;
    4730              : 
    4731      1196184 :   decl_type_param_list = NULL;
    4732              : 
    4733              :   /* A belt and braces check that the typespec is correctly being treated
    4734              :      as a deferred characteristic association.  */
    4735      2392368 :   seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
    4736        84356 :                           && (gfc_current_block ()->result->ts.kind == -1)
    4737      1208087 :                           && (ts->kind == -1);
    4738      1196184 :   gfc_clear_ts (ts);
    4739      1196184 :   if (seen_deferred_kind)
    4740         9668 :     ts->kind = -1;
    4741              : 
    4742              :   /* Clear the current binding label, in case one is given.  */
    4743      1196184 :   curr_binding_label = NULL;
    4744              : 
    4745              :   /* Match BYTE type-spec.  */
    4746      1196184 :   m = match_byte_typespec (ts);
    4747      1196184 :   if (m != MATCH_NO)
    4748              :     return m;
    4749              : 
    4750      1196153 :   m = gfc_match (" type (");
    4751      1196153 :   matched_type = (m == MATCH_YES);
    4752      1196153 :   if (matched_type)
    4753              :     {
    4754        31818 :       gfc_gobble_whitespace ();
    4755        31818 :       if (gfc_peek_ascii_char () == '*')
    4756              :         {
    4757         5617 :           if ((m = gfc_match ("* ) ")) != MATCH_YES)
    4758              :             return m;
    4759         5617 :           if (gfc_comp_struct (gfc_current_state ()))
    4760              :             {
    4761            2 :               gfc_error ("Assumed type at %C is not allowed for components");
    4762            2 :               return MATCH_ERROR;
    4763              :             }
    4764         5615 :           if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
    4765              :             return MATCH_ERROR;
    4766         5613 :           ts->type = BT_ASSUMED;
    4767         5613 :           return MATCH_YES;
    4768              :         }
    4769              : 
    4770        26201 :       m = gfc_match ("%n", name);
    4771        26201 :       matched_type = (m == MATCH_YES);
    4772              :     }
    4773              : 
    4774        26201 :   if ((matched_type && strcmp ("integer", name) == 0)
    4775      1190536 :       || (!matched_type && gfc_match (" integer") == MATCH_YES))
    4776              :     {
    4777       112815 :       ts->type = BT_INTEGER;
    4778       112815 :       ts->kind = gfc_default_integer_kind;
    4779       112815 :       goto get_kind;
    4780              :     }
    4781              : 
    4782      1077721 :   if (flag_unsigned)
    4783              :     {
    4784            0 :       if ((matched_type && strcmp ("unsigned", name) == 0)
    4785        22489 :           || (!matched_type && gfc_match (" unsigned") == MATCH_YES))
    4786              :         {
    4787         1036 :           ts->type = BT_UNSIGNED;
    4788         1036 :           ts->kind = gfc_default_integer_kind;
    4789         1036 :           goto get_kind;
    4790              :         }
    4791              :     }
    4792              : 
    4793        26195 :   if ((matched_type && strcmp ("character", name) == 0)
    4794      1076685 :       || (!matched_type && gfc_match (" character") == MATCH_YES))
    4795              :     {
    4796        29088 :       if (matched_type
    4797        29088 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4798              :                               "intrinsic-type-spec at %C"))
    4799              :         return MATCH_ERROR;
    4800              : 
    4801        29087 :       ts->type = BT_CHARACTER;
    4802        29087 :       if (implicit_flag == 0)
    4803        28981 :         m = gfc_match_char_spec (ts);
    4804              :       else
    4805              :         m = MATCH_YES;
    4806              : 
    4807        29087 :       if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
    4808              :         {
    4809            1 :           gfc_error ("Malformed type-spec at %C");
    4810            1 :           return MATCH_ERROR;
    4811              :         }
    4812              : 
    4813              :       return m;
    4814              :     }
    4815              : 
    4816        26191 :   if ((matched_type && strcmp ("real", name) == 0)
    4817      1047597 :       || (!matched_type && gfc_match (" real") == MATCH_YES))
    4818              :     {
    4819        30364 :       ts->type = BT_REAL;
    4820        30364 :       ts->kind = gfc_default_real_kind;
    4821        30364 :       goto get_kind;
    4822              :     }
    4823              : 
    4824      1017233 :   if ((matched_type
    4825        26188 :        && (strcmp ("doubleprecision", name) == 0
    4826        26187 :            || (strcmp ("double", name) == 0
    4827            5 :                && gfc_match (" precision") == MATCH_YES)))
    4828      1017233 :       || (!matched_type && gfc_match (" double precision") == MATCH_YES))
    4829              :     {
    4830         2614 :       if (matched_type
    4831         2614 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4832              :                               "intrinsic-type-spec at %C"))
    4833              :         return MATCH_ERROR;
    4834              : 
    4835         2613 :       if (matched_type && gfc_match_char (')') != MATCH_YES)
    4836              :         {
    4837            2 :           gfc_error ("Malformed type-spec at %C");
    4838            2 :           return MATCH_ERROR;
    4839              :         }
    4840              : 
    4841         2611 :       ts->type = BT_REAL;
    4842         2611 :       ts->kind = gfc_default_double_kind;
    4843         2611 :       return MATCH_YES;
    4844              :     }
    4845              : 
    4846        26184 :   if ((matched_type && strcmp ("complex", name) == 0)
    4847      1014619 :       || (!matched_type && gfc_match (" complex") == MATCH_YES))
    4848              :     {
    4849         4057 :       ts->type = BT_COMPLEX;
    4850         4057 :       ts->kind = gfc_default_complex_kind;
    4851         4057 :       goto get_kind;
    4852              :     }
    4853              : 
    4854      1010562 :   if ((matched_type
    4855        26184 :        && (strcmp ("doublecomplex", name) == 0
    4856        26183 :            || (strcmp ("double", name) == 0
    4857            2 :                && gfc_match (" complex") == MATCH_YES)))
    4858      1010562 :       || (!matched_type && gfc_match (" double complex") == MATCH_YES))
    4859              :     {
    4860          204 :       if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
    4861              :         return MATCH_ERROR;
    4862              : 
    4863          203 :       if (matched_type
    4864          203 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4865              :                               "intrinsic-type-spec at %C"))
    4866              :         return MATCH_ERROR;
    4867              : 
    4868          203 :       if (matched_type && gfc_match_char (')') != MATCH_YES)
    4869              :         {
    4870            2 :           gfc_error ("Malformed type-spec at %C");
    4871            2 :           return MATCH_ERROR;
    4872              :         }
    4873              : 
    4874          201 :       ts->type = BT_COMPLEX;
    4875          201 :       ts->kind = gfc_default_double_kind;
    4876          201 :       return MATCH_YES;
    4877              :     }
    4878              : 
    4879        26181 :   if ((matched_type && strcmp ("logical", name) == 0)
    4880      1010358 :       || (!matched_type && gfc_match (" logical") == MATCH_YES))
    4881              :     {
    4882        11556 :       ts->type = BT_LOGICAL;
    4883        11556 :       ts->kind = gfc_default_logical_kind;
    4884        11556 :       goto get_kind;
    4885              :     }
    4886              : 
    4887       998802 :   if (matched_type)
    4888              :     {
    4889        26178 :       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
    4890        26178 :       if (m == MATCH_ERROR)
    4891              :         return m;
    4892              : 
    4893        26178 :       gfc_gobble_whitespace ();
    4894        26178 :       if (gfc_peek_ascii_char () != ')')
    4895              :         {
    4896            1 :           gfc_error ("Malformed type-spec at %C");
    4897            1 :           return MATCH_ERROR;
    4898              :         }
    4899        26177 :       m = gfc_match_char (')'); /* Burn closing ')'.  */
    4900              :     }
    4901              : 
    4902       998801 :   if (m != MATCH_YES)
    4903       972624 :     m = match_record_decl (name);
    4904              : 
    4905       998801 :   if (matched_type || m == MATCH_YES)
    4906              :     {
    4907        26521 :       ts->type = BT_DERIVED;
    4908              :       /* We accept record/s/ or type(s) where s is a structure, but we
    4909              :        * don't need all the extra derived-type stuff for structures.  */
    4910        26521 :       if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
    4911              :         {
    4912            1 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    4913            1 :           return MATCH_ERROR;
    4914              :         }
    4915              : 
    4916        26520 :       if (sym && sym->attr.flavor == FL_DERIVED
    4917        25370 :           && sym->attr.pdt_template
    4918         1036 :           && gfc_current_state () != COMP_DERIVED)
    4919              :         {
    4920          921 :           m = gfc_get_pdt_instance (decl_type_param_list, &sym,  NULL);
    4921          921 :           if (m != MATCH_YES)
    4922              :             return m;
    4923          906 :           gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
    4924          906 :           ts->u.derived = sym;
    4925          906 :           const char* lower = gfc_dt_lower_string (sym->name);
    4926          906 :           size_t len = strlen (lower);
    4927              :           /* Reallocate with sufficient size.  */
    4928          906 :           if (len > GFC_MAX_SYMBOL_LEN)
    4929            2 :             name = XALLOCAVEC (char, len + 1);
    4930          906 :           memcpy (name, lower, len);
    4931          906 :           name[len] = '\0';
    4932              :         }
    4933              : 
    4934        26505 :       if (sym && sym->attr.flavor == FL_STRUCT)
    4935              :         {
    4936          361 :           ts->u.derived = sym;
    4937          361 :           return MATCH_YES;
    4938              :         }
    4939              :       /* Actually a derived type.  */
    4940              :     }
    4941              : 
    4942              :   else
    4943              :     {
    4944              :       /* Match nested STRUCTURE declarations; only valid within another
    4945              :          structure declaration.  */
    4946       972280 :       if (flag_dec_structure
    4947         8032 :           && (gfc_current_state () == COMP_STRUCTURE
    4948         7570 :               || gfc_current_state () == COMP_MAP))
    4949              :         {
    4950          732 :           m = gfc_match (" structure");
    4951          732 :           if (m == MATCH_YES)
    4952              :             {
    4953           27 :               m = gfc_match_structure_decl ();
    4954           27 :               if (m == MATCH_YES)
    4955              :                 {
    4956              :                   /* gfc_new_block is updated by match_structure_decl.  */
    4957           26 :                   ts->type = BT_DERIVED;
    4958           26 :                   ts->u.derived = gfc_new_block;
    4959           26 :                   return MATCH_YES;
    4960              :                 }
    4961              :             }
    4962          706 :           if (m == MATCH_ERROR)
    4963              :             return MATCH_ERROR;
    4964              :         }
    4965              : 
    4966              :       /* Match CLASS declarations.  */
    4967       972253 :       m = gfc_match (" class ( * )");
    4968       972253 :       if (m == MATCH_ERROR)
    4969              :         return MATCH_ERROR;
    4970       972253 :       else if (m == MATCH_YES)
    4971              :         {
    4972         2021 :           gfc_symbol *upe;
    4973         2021 :           gfc_symtree *st;
    4974         2021 :           ts->type = BT_CLASS;
    4975         2021 :           gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
    4976         2021 :           if (upe == NULL)
    4977              :             {
    4978         1219 :               upe = gfc_new_symbol ("STAR", gfc_current_ns);
    4979         1219 :               st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
    4980         1219 :               st->n.sym = upe;
    4981         1219 :               gfc_set_sym_referenced (upe);
    4982         1219 :               upe->refs++;
    4983         1219 :               upe->ts.type = BT_VOID;
    4984         1219 :               upe->attr.unlimited_polymorphic = 1;
    4985              :               /* This is essential to force the construction of
    4986              :                  unlimited polymorphic component class containers.  */
    4987         1219 :               upe->attr.zero_comp = 1;
    4988         1219 :               if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
    4989              :                                    &gfc_current_locus))
    4990              :               return MATCH_ERROR;
    4991              :             }
    4992              :           else
    4993              :             {
    4994          802 :               st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
    4995          802 :               st->n.sym = upe;
    4996          802 :               upe->refs++;
    4997              :             }
    4998         2021 :           ts->u.derived = upe;
    4999         2021 :           return m;
    5000              :         }
    5001              : 
    5002       970232 :       m = gfc_match (" class (");
    5003              : 
    5004       970232 :       if (m == MATCH_YES)
    5005         9221 :         m = gfc_match ("%n", name);
    5006              :       else
    5007              :         return m;
    5008              : 
    5009         9221 :       if (m != MATCH_YES)
    5010              :         return m;
    5011         9221 :       ts->type = BT_CLASS;
    5012              : 
    5013         9221 :       if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
    5014              :         return MATCH_ERROR;
    5015              : 
    5016         9220 :       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
    5017         9220 :       if (m == MATCH_ERROR)
    5018              :         return m;
    5019              : 
    5020         9220 :       m = gfc_match_char (')');
    5021         9220 :       if (m != MATCH_YES)
    5022              :         return m;
    5023              :     }
    5024              : 
    5025              :   /* This picks up function declarations with a PDT typespec. Since a
    5026              :      pdt_type has been generated, there is no more to do. Within the
    5027              :      function body, this type must be used for the typespec so that
    5028              :      the "being used before it is defined warning" does not arise.  */
    5029        35364 :   if (ts->type == BT_DERIVED
    5030        26144 :       && sym && sym->attr.pdt_type
    5031        36270 :       && (gfc_current_state () == COMP_CONTAINS
    5032          890 :           || (gfc_current_state () == COMP_FUNCTION
    5033          280 :               && gfc_current_block ()->ts.type == BT_DERIVED
    5034           60 :               && gfc_current_block ()->ts.u.derived == sym
    5035           30 :               && !gfc_find_symtree (gfc_current_ns->sym_root,
    5036              :                                     sym->name))))
    5037              :     {
    5038           42 :       if (gfc_current_state () == COMP_FUNCTION)
    5039              :         {
    5040           26 :           gfc_symtree *pdt_st;
    5041           26 :           pdt_st = gfc_new_symtree (&gfc_current_ns->sym_root,
    5042              :                                     sym->name);
    5043           26 :           pdt_st->n.sym = sym;
    5044           26 :           sym->refs++;
    5045              :         }
    5046           42 :       ts->u.derived = sym;
    5047           42 :       return MATCH_YES;
    5048              :     }
    5049              : 
    5050              :   /* Defer association of the derived type until the end of the
    5051              :      specification block.  However, if the derived type can be
    5052              :      found, add it to the typespec.  */
    5053        35322 :   if (gfc_matching_function)
    5054              :     {
    5055         1043 :       ts->u.derived = NULL;
    5056         1043 :       if (gfc_current_state () != COMP_INTERFACE
    5057         1043 :             && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
    5058              :         {
    5059          512 :           sym = gfc_find_dt_in_generic (sym);
    5060          512 :           ts->u.derived = sym;
    5061              :         }
    5062              :       return MATCH_YES;
    5063              :     }
    5064              : 
    5065              :   /* Search for the name but allow the components to be defined later.  If
    5066              :      type = -1, this typespec has been seen in a function declaration but
    5067              :      the type could not be accessed at that point.  The actual derived type is
    5068              :      stored in a symtree with the first letter of the name capitalized; the
    5069              :      symtree with the all lower-case name contains the associated
    5070              :      generic function.  */
    5071        34279 :   dt_name = gfc_dt_upper_string (name);
    5072        34279 :   sym = NULL;
    5073        34279 :   dt_sym = NULL;
    5074        34279 :   if (ts->kind != -1)
    5075              :     {
    5076        33067 :       gfc_get_ha_symbol (name, &sym);
    5077        33067 :       if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
    5078              :         {
    5079            0 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    5080            0 :           return MATCH_ERROR;
    5081              :         }
    5082        33067 :       if (sym->generic && !dt_sym)
    5083        14564 :         dt_sym = gfc_find_dt_in_generic (sym);
    5084              : 
    5085              :       /* Host associated PDTs can get confused with their constructors
    5086              :          because they are instantiated in the template's namespace.  */
    5087        33067 :       if (!dt_sym)
    5088              :         {
    5089          968 :           if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
    5090              :             {
    5091            0 :               gfc_error ("Type name %qs at %C is ambiguous", name);
    5092            0 :               return MATCH_ERROR;
    5093              :             }
    5094          968 :           if (dt_sym && !dt_sym->attr.pdt_type)
    5095            0 :             dt_sym = NULL;
    5096              :         }
    5097              :     }
    5098         1212 :   else if (ts->kind == -1)
    5099              :     {
    5100         2424 :       int iface = gfc_state_stack->previous->state != COMP_INTERFACE
    5101         1212 :                     || gfc_current_ns->has_import_set;
    5102         1212 :       gfc_find_symbol (name, NULL, iface, &sym);
    5103         1212 :       if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
    5104              :         {
    5105            0 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    5106            0 :           return MATCH_ERROR;
    5107              :         }
    5108         1212 :       if (sym && sym->generic && !dt_sym)
    5109            2 :         dt_sym = gfc_find_dt_in_generic (sym);
    5110              : 
    5111         1212 :       ts->kind = 0;
    5112         1212 :       if (sym == NULL)
    5113              :         return MATCH_NO;
    5114              :     }
    5115              : 
    5116        34262 :   if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
    5117        33516 :        && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
    5118        34260 :       || sym->attr.subroutine)
    5119              :     {
    5120            2 :       gfc_error ("Type name %qs at %C conflicts with previously declared "
    5121              :                  "entity at %L, which has the same name", name,
    5122              :                  &sym->declared_at);
    5123            2 :       return MATCH_ERROR;
    5124              :     }
    5125              : 
    5126        34260 :   if (dt_sym && decl_type_param_list
    5127          940 :       && dt_sym->attr.flavor == FL_DERIVED
    5128          940 :       && !dt_sym->attr.pdt_type
    5129          250 :       && !dt_sym->attr.pdt_template)
    5130              :     {
    5131            1 :       gfc_error ("Type %qs is not parameterized and so the type parameter spec "
    5132              :                  "list at %C may not appear", dt_sym->name);
    5133            1 :       return MATCH_ERROR;
    5134              :     }
    5135              : 
    5136        34259 :   if (sym && sym->attr.flavor == FL_DERIVED
    5137              :       && sym->attr.pdt_template
    5138              :       && gfc_current_state () != COMP_DERIVED)
    5139              :     {
    5140              :       m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
    5141              :       if (m != MATCH_YES)
    5142              :         return m;
    5143              :       gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
    5144              :       ts->u.derived = sym;
    5145              :       strcpy (name, gfc_dt_lower_string (sym->name));
    5146              :     }
    5147              : 
    5148        34259 :   gfc_save_symbol_data (sym);
    5149        34259 :   gfc_set_sym_referenced (sym);
    5150        34259 :   if (!sym->attr.generic
    5151        34259 :       && !gfc_add_generic (&sym->attr, sym->name, NULL))
    5152              :     return MATCH_ERROR;
    5153              : 
    5154        34259 :   if (!sym->attr.function
    5155        34259 :       && !gfc_add_function (&sym->attr, sym->name, NULL))
    5156              :     return MATCH_ERROR;
    5157              : 
    5158        34259 :   if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
    5159        34127 :       && dt_sym->attr.pdt_template
    5160          260 :       && gfc_current_state () != COMP_DERIVED)
    5161              :     {
    5162          133 :       m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
    5163          133 :       if (m != MATCH_YES)
    5164              :         return m;
    5165          133 :       gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
    5166              :     }
    5167              : 
    5168        34259 :   if (!dt_sym)
    5169              :     {
    5170          132 :       gfc_interface *intr, *head;
    5171              : 
    5172              :       /* Use upper case to save the actual derived-type symbol.  */
    5173          132 :       gfc_get_symbol (dt_name, NULL, &dt_sym);
    5174          132 :       dt_sym->name = gfc_get_string ("%s", sym->name);
    5175          132 :       head = sym->generic;
    5176          132 :       intr = gfc_get_interface ();
    5177          132 :       intr->sym = dt_sym;
    5178          132 :       intr->where = gfc_current_locus;
    5179          132 :       intr->next = head;
    5180          132 :       sym->generic = intr;
    5181          132 :       sym->attr.if_source = IFSRC_DECL;
    5182              :     }
    5183              :   else
    5184        34127 :     gfc_save_symbol_data (dt_sym);
    5185              : 
    5186        34259 :   gfc_set_sym_referenced (dt_sym);
    5187              : 
    5188          132 :   if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
    5189        34391 :       && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
    5190              :     return MATCH_ERROR;
    5191              : 
    5192        34259 :   ts->u.derived = dt_sym;
    5193              : 
    5194        34259 :   return MATCH_YES;
    5195              : 
    5196       159828 : get_kind:
    5197       159828 :   if (matched_type
    5198       159828 :       && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    5199              :                           "intrinsic-type-spec at %C"))
    5200              :     return MATCH_ERROR;
    5201              : 
    5202              :   /* For all types except double, derived and character, look for an
    5203              :      optional kind specifier.  MATCH_NO is actually OK at this point.  */
    5204       159825 :   if (implicit_flag == 1)
    5205              :     {
    5206          223 :         if (matched_type && gfc_match_char (')') != MATCH_YES)
    5207              :           return MATCH_ERROR;
    5208              : 
    5209              :         return MATCH_YES;
    5210              :     }
    5211              : 
    5212       159602 :   if (gfc_current_form == FORM_FREE)
    5213              :     {
    5214       144387 :       c = gfc_peek_ascii_char ();
    5215       144387 :       if (!gfc_is_whitespace (c) && c != '*' && c != '('
    5216        71233 :           && c != ':' && c != ',')
    5217              :         {
    5218          167 :           if (matched_type && c == ')')
    5219              :             {
    5220            3 :               gfc_next_ascii_char ();
    5221            3 :               return MATCH_YES;
    5222              :             }
    5223          164 :           gfc_error ("Malformed type-spec at %C");
    5224          164 :           return MATCH_NO;
    5225              :         }
    5226              :     }
    5227              : 
    5228       159435 :   m = gfc_match_kind_spec (ts, false);
    5229       159435 :   if (m == MATCH_ERROR)
    5230              :     return MATCH_ERROR;
    5231              : 
    5232       159399 :   if (m == MATCH_NO && ts->type != BT_CHARACTER)
    5233              :     {
    5234       108092 :       m = gfc_match_old_kind_spec (ts);
    5235       108092 :       if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
    5236              :          return MATCH_ERROR;
    5237              :     }
    5238              : 
    5239       159391 :   if (matched_type && gfc_match_char (')') != MATCH_YES)
    5240              :     {
    5241            0 :       gfc_error ("Malformed type-spec at %C");
    5242            0 :       return MATCH_ERROR;
    5243              :     }
    5244              : 
    5245              :   /* Defer association of the KIND expression of function results
    5246              :      until after USE and IMPORT statements.  */
    5247         4446 :   if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
    5248       163810 :          || gfc_matching_function)
    5249              :     return MATCH_YES;
    5250              : 
    5251       152158 :   if (m == MATCH_NO)
    5252       153089 :     m = MATCH_YES;              /* No kind specifier found.  */
    5253              : 
    5254              :   return m;
    5255              : }
    5256              : 
    5257              : 
    5258              : /* Match an IMPLICIT NONE statement.  Actually, this statement is
    5259              :    already matched in parse.cc, or we would not end up here in the
    5260              :    first place.  So the only thing we need to check, is if there is
    5261              :    trailing garbage.  If not, the match is successful.  */
    5262              : 
    5263              : match
    5264        24253 : gfc_match_implicit_none (void)
    5265              : {
    5266        24253 :   char c;
    5267        24253 :   match m;
    5268        24253 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    5269        24253 :   bool type = false;
    5270        24253 :   bool external = false;
    5271        24253 :   locus cur_loc = gfc_current_locus;
    5272              : 
    5273        24253 :   if (gfc_current_ns->seen_implicit_none
    5274        24251 :       || gfc_current_ns->has_implicit_none_export)
    5275              :     {
    5276            4 :       gfc_error ("Duplicate IMPLICIT NONE statement at %C");
    5277            4 :       return MATCH_ERROR;
    5278              :     }
    5279              : 
    5280        24249 :   gfc_gobble_whitespace ();
    5281        24249 :   c = gfc_peek_ascii_char ();
    5282        24249 :   if (c == '(')
    5283              :     {
    5284         1109 :       (void) gfc_next_ascii_char ();
    5285         1109 :       if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
    5286              :         return MATCH_ERROR;
    5287              : 
    5288         1108 :       gfc_gobble_whitespace ();
    5289         1108 :       if (gfc_peek_ascii_char () == ')')
    5290              :         {
    5291            1 :           (void) gfc_next_ascii_char ();
    5292            1 :           type = true;
    5293              :         }
    5294              :       else
    5295         3297 :         for(;;)
    5296              :           {
    5297         2202 :             m = gfc_match (" %n", name);
    5298         2202 :             if (m != MATCH_YES)
    5299              :               return MATCH_ERROR;
    5300              : 
    5301         2202 :             if (strcmp (name, "type") == 0)
    5302              :               type = true;
    5303         1107 :             else if (strcmp (name, "external") == 0)
    5304              :               external = true;
    5305              :             else
    5306              :               return MATCH_ERROR;
    5307              : 
    5308         2202 :             gfc_gobble_whitespace ();
    5309         2202 :             c = gfc_next_ascii_char ();
    5310         2202 :             if (c == ',')
    5311         1095 :               continue;
    5312         1107 :             if (c == ')')
    5313              :               break;
    5314              :             return MATCH_ERROR;
    5315              :           }
    5316              :     }
    5317              :   else
    5318              :     type = true;
    5319              : 
    5320        24248 :   if (gfc_match_eos () != MATCH_YES)
    5321              :     return MATCH_ERROR;
    5322              : 
    5323        24248 :   gfc_set_implicit_none (type, external, &cur_loc);
    5324              : 
    5325        24248 :   return MATCH_YES;
    5326              : }
    5327              : 
    5328              : 
    5329              : /* Match the letter range(s) of an IMPLICIT statement.  */
    5330              : 
    5331              : static match
    5332          600 : match_implicit_range (void)
    5333              : {
    5334          600 :   char c, c1, c2;
    5335          600 :   int inner;
    5336          600 :   locus cur_loc;
    5337              : 
    5338          600 :   cur_loc = gfc_current_locus;
    5339              : 
    5340          600 :   gfc_gobble_whitespace ();
    5341          600 :   c = gfc_next_ascii_char ();
    5342          600 :   if (c != '(')
    5343              :     {
    5344           59 :       gfc_error ("Missing character range in IMPLICIT at %C");
    5345           59 :       goto bad;
    5346              :     }
    5347              : 
    5348              :   inner = 1;
    5349         1195 :   while (inner)
    5350              :     {
    5351          722 :       gfc_gobble_whitespace ();
    5352          722 :       c1 = gfc_next_ascii_char ();
    5353          722 :       if (!ISALPHA (c1))
    5354           33 :         goto bad;
    5355              : 
    5356          689 :       gfc_gobble_whitespace ();
    5357          689 :       c = gfc_next_ascii_char ();
    5358              : 
    5359          689 :       switch (c)
    5360              :         {
    5361          201 :         case ')':
    5362          201 :           inner = 0;            /* Fall through.  */
    5363              : 
    5364              :         case ',':
    5365              :           c2 = c1;
    5366              :           break;
    5367              : 
    5368          439 :         case '-':
    5369          439 :           gfc_gobble_whitespace ();
    5370          439 :           c2 = gfc_next_ascii_char ();
    5371          439 :           if (!ISALPHA (c2))
    5372            0 :             goto bad;
    5373              : 
    5374          439 :           gfc_gobble_whitespace ();
    5375          439 :           c = gfc_next_ascii_char ();
    5376              : 
    5377          439 :           if ((c != ',') && (c != ')'))
    5378            0 :             goto bad;
    5379          439 :           if (c == ')')
    5380          272 :             inner = 0;
    5381              : 
    5382              :           break;
    5383              : 
    5384           35 :         default:
    5385           35 :           goto bad;
    5386              :         }
    5387              : 
    5388          654 :       if (c1 > c2)
    5389              :         {
    5390            0 :           gfc_error ("Letters must be in alphabetic order in "
    5391              :                      "IMPLICIT statement at %C");
    5392            0 :           goto bad;
    5393              :         }
    5394              : 
    5395              :       /* See if we can add the newly matched range to the pending
    5396              :          implicits from this IMPLICIT statement.  We do not check for
    5397              :          conflicts with whatever earlier IMPLICIT statements may have
    5398              :          set.  This is done when we've successfully finished matching
    5399              :          the current one.  */
    5400          654 :       if (!gfc_add_new_implicit_range (c1, c2))
    5401            0 :         goto bad;
    5402              :     }
    5403              : 
    5404              :   return MATCH_YES;
    5405              : 
    5406          127 : bad:
    5407          127 :   gfc_syntax_error (ST_IMPLICIT);
    5408              : 
    5409          127 :   gfc_current_locus = cur_loc;
    5410          127 :   return MATCH_ERROR;
    5411              : }
    5412              : 
    5413              : 
    5414              : /* Match an IMPLICIT statement, storing the types for
    5415              :    gfc_set_implicit() if the statement is accepted by the parser.
    5416              :    There is a strange looking, but legal syntactic construction
    5417              :    possible.  It looks like:
    5418              : 
    5419              :      IMPLICIT INTEGER (a-b) (c-d)
    5420              : 
    5421              :    This is legal if "a-b" is a constant expression that happens to
    5422              :    equal one of the legal kinds for integers.  The real problem
    5423              :    happens with an implicit specification that looks like:
    5424              : 
    5425              :      IMPLICIT INTEGER (a-b)
    5426              : 
    5427              :    In this case, a typespec matcher that is "greedy" (as most of the
    5428              :    matchers are) gobbles the character range as a kindspec, leaving
    5429              :    nothing left.  We therefore have to go a bit more slowly in the
    5430              :    matching process by inhibiting the kindspec checking during
    5431              :    typespec matching and checking for a kind later.  */
    5432              : 
    5433              : match
    5434        24679 : gfc_match_implicit (void)
    5435              : {
    5436        24679 :   gfc_typespec ts;
    5437        24679 :   locus cur_loc;
    5438        24679 :   char c;
    5439        24679 :   match m;
    5440              : 
    5441        24679 :   if (gfc_current_ns->seen_implicit_none)
    5442              :     {
    5443            4 :       gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
    5444              :                  "statement");
    5445            4 :       return MATCH_ERROR;
    5446              :     }
    5447              : 
    5448        24675 :   gfc_clear_ts (&ts);
    5449              : 
    5450              :   /* We don't allow empty implicit statements.  */
    5451        24675 :   if (gfc_match_eos () == MATCH_YES)
    5452              :     {
    5453            0 :       gfc_error ("Empty IMPLICIT statement at %C");
    5454            0 :       return MATCH_ERROR;
    5455              :     }
    5456              : 
    5457        24704 :   do
    5458              :     {
    5459              :       /* First cleanup.  */
    5460        24704 :       gfc_clear_new_implicit ();
    5461              : 
    5462              :       /* A basic type is mandatory here.  */
    5463        24704 :       m = gfc_match_decl_type_spec (&ts, 1);
    5464        24704 :       if (m == MATCH_ERROR)
    5465            0 :         goto error;
    5466        24704 :       if (m == MATCH_NO)
    5467        24251 :         goto syntax;
    5468              : 
    5469          453 :       cur_loc = gfc_current_locus;
    5470          453 :       m = match_implicit_range ();
    5471              : 
    5472          453 :       if (m == MATCH_YES)
    5473              :         {
    5474              :           /* We may have <TYPE> (<RANGE>).  */
    5475          326 :           gfc_gobble_whitespace ();
    5476          326 :           c = gfc_peek_ascii_char ();
    5477          326 :           if (c == ',' || c == '\n' || c == ';' || c == '!')
    5478              :             {
    5479              :               /* Check for CHARACTER with no length parameter.  */
    5480          299 :               if (ts.type == BT_CHARACTER && !ts.u.cl)
    5481              :                 {
    5482           32 :                   ts.kind = gfc_default_character_kind;
    5483           32 :                   ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5484           32 :                   ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    5485              :                                                       NULL, 1);
    5486              :                 }
    5487              : 
    5488              :               /* Record the Successful match.  */
    5489          299 :               if (!gfc_merge_new_implicit (&ts))
    5490              :                 return MATCH_ERROR;
    5491          297 :               if (c == ',')
    5492           28 :                 c = gfc_next_ascii_char ();
    5493          269 :               else if (gfc_match_eos () == MATCH_ERROR)
    5494            0 :                 goto error;
    5495          297 :               continue;
    5496              :             }
    5497              : 
    5498           27 :           gfc_current_locus = cur_loc;
    5499              :         }
    5500              : 
    5501              :       /* Discard the (incorrectly) matched range.  */
    5502          154 :       gfc_clear_new_implicit ();
    5503              : 
    5504              :       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
    5505          154 :       if (ts.type == BT_CHARACTER)
    5506           74 :         m = gfc_match_char_spec (&ts);
    5507           80 :       else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
    5508              :         {
    5509           76 :           m = gfc_match_kind_spec (&ts, false);
    5510           76 :           if (m == MATCH_NO)
    5511              :             {
    5512           40 :               m = gfc_match_old_kind_spec (&ts);
    5513           40 :               if (m == MATCH_ERROR)
    5514            0 :                 goto error;
    5515           40 :               if (m == MATCH_NO)
    5516            0 :                 goto syntax;
    5517              :             }
    5518              :         }
    5519          154 :       if (m == MATCH_ERROR)
    5520            7 :         goto error;
    5521              : 
    5522          147 :       m = match_implicit_range ();
    5523          147 :       if (m == MATCH_ERROR)
    5524            0 :         goto error;
    5525          147 :       if (m == MATCH_NO)
    5526              :         goto syntax;
    5527              : 
    5528          147 :       gfc_gobble_whitespace ();
    5529          147 :       c = gfc_next_ascii_char ();
    5530          147 :       if (c != ',' && gfc_match_eos () != MATCH_YES)
    5531            0 :         goto syntax;
    5532              : 
    5533          147 :       if (!gfc_merge_new_implicit (&ts))
    5534              :         return MATCH_ERROR;
    5535              :     }
    5536          444 :   while (c == ',');
    5537              : 
    5538              :   return MATCH_YES;
    5539              : 
    5540        24251 : syntax:
    5541        24251 :   gfc_syntax_error (ST_IMPLICIT);
    5542              : 
    5543        24679 : error:
    5544              :   return MATCH_ERROR;
    5545              : }
    5546              : 
    5547              : 
    5548              : /* Match the IMPORT statement.  IMPORT was added to F2003 as
    5549              : 
    5550              :    R1209 import-stmt  is IMPORT [[ :: ] import-name-list ]
    5551              : 
    5552              :    C1210 (R1209) The IMPORT statement is allowed only in an interface-body.
    5553              : 
    5554              :    C1211 (R1209) Each import-name shall be the name of an entity in the
    5555              :                  host scoping unit.
    5556              : 
    5557              :    under the description of an interface block. Under F2008, IMPORT was
    5558              :    split out of the interface block description to 12.4.3.3 and C1210
    5559              :    became
    5560              : 
    5561              :    C1210 (R1209) The IMPORT statement is allowed only in an interface-body
    5562              :                  that is not a module procedure interface body.
    5563              : 
    5564              :    Finally, F2018, section 8.8, has changed the IMPORT statement to
    5565              : 
    5566              :    R867 import-stmt  is IMPORT [[ :: ] import-name-list ]
    5567              :                      or IMPORT, ONLY : import-name-list
    5568              :                      or IMPORT, NONE
    5569              :                      or IMPORT, ALL
    5570              : 
    5571              :    C896 (R867) An IMPORT statement shall not appear in the scoping unit of
    5572              :                 a main-program, external-subprogram, module, or block-data.
    5573              : 
    5574              :    C897 (R867) Each import-name shall be the name of an entity in the host
    5575              :                 scoping unit.
    5576              : 
    5577              :    C898  If any IMPORT statement in a scoping unit has an ONLY specifier,
    5578              :          all IMPORT statements in that scoping unit shall have an ONLY
    5579              :          specifier.
    5580              : 
    5581              :    C899  IMPORT, NONE shall not appear in the scoping unit of a submodule.
    5582              : 
    5583              :    C8100 If an IMPORT, NONE or IMPORT, ALL statement appears in a scoping
    5584              :          unit, no other IMPORT statement shall appear in that scoping unit.
    5585              : 
    5586              :    C8101 Within an interface body, an entity that is accessed by host
    5587              :          association shall be accessible by host or use association within
    5588              :          the host scoping unit, or explicitly declared prior to the interface
    5589              :          body.
    5590              : 
    5591              :    C8102 An entity whose name appears as an import-name or which is made
    5592              :          accessible by an IMPORT, ALL statement shall not appear in any
    5593              :          context described in 19.5.1.4 that would cause the host entity
    5594              :          of that name to be inaccessible.  */
    5595              : 
    5596              : match
    5597         4032 : gfc_match_import (void)
    5598              : {
    5599         4032 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    5600         4032 :   match m;
    5601         4032 :   gfc_symbol *sym;
    5602         4032 :   gfc_symtree *st;
    5603         4032 :   bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;;
    5604         4032 :   importstate current_import_state = gfc_current_ns->import_state;
    5605              : 
    5606         4032 :   if (!f2018_allowed
    5607           13 :       && (gfc_current_ns->proc_name == NULL
    5608           12 :           || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY))
    5609              :     {
    5610            3 :       gfc_error ("IMPORT statement at %C only permitted in "
    5611              :                  "an INTERFACE body");
    5612            3 :       return MATCH_ERROR;
    5613              :     }
    5614              :   else if (f2018_allowed
    5615         4019 :            && (!gfc_current_ns->parent || gfc_current_ns->is_block_data))
    5616            4 :     goto C897;
    5617              : 
    5618         4015 :   if (f2018_allowed
    5619         4015 :       && (current_import_state == IMPORT_ALL
    5620         4015 :           || current_import_state == IMPORT_NONE))
    5621            2 :     goto C8100;
    5622              : 
    5623         4023 :   if (gfc_current_ns->proc_name
    5624         4022 :       && gfc_current_ns->proc_name->attr.module_procedure)
    5625              :     {
    5626            1 :       gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
    5627              :                  "in a module procedure interface body");
    5628            1 :       return MATCH_ERROR;
    5629              :     }
    5630              : 
    5631         4022 :   if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
    5632              :     return MATCH_ERROR;
    5633              : 
    5634         4018 :   gfc_current_ns->import_state = IMPORT_NOT_SET;
    5635         4018 :   if (f2018_allowed)
    5636              :     {
    5637         4012 :       if (gfc_match (" , none") == MATCH_YES)
    5638              :         {
    5639            8 :           if (current_import_state == IMPORT_ONLY)
    5640            0 :             goto C898;
    5641            8 :           if (gfc_current_state () == COMP_SUBMODULE)
    5642            0 :             goto C899;
    5643            8 :           gfc_current_ns->import_state = IMPORT_NONE;
    5644              :         }
    5645         4004 :       else if (gfc_match (" , only :") == MATCH_YES)
    5646              :         {
    5647           19 :           if (current_import_state != IMPORT_NOT_SET
    5648           19 :               && current_import_state != IMPORT_ONLY)
    5649            0 :             goto C898;
    5650           19 :           gfc_current_ns->import_state = IMPORT_ONLY;
    5651              :         }
    5652         3985 :       else if (gfc_match (" , all") == MATCH_YES)
    5653              :         {
    5654            1 :           if (current_import_state == IMPORT_ONLY)
    5655            0 :             goto C898;
    5656            1 :           gfc_current_ns->import_state = IMPORT_ALL;
    5657              :         }
    5658              : 
    5659         4012 :       if (current_import_state != IMPORT_NOT_SET
    5660            6 :           && (gfc_current_ns->import_state == IMPORT_NONE
    5661            6 :               || gfc_current_ns->import_state == IMPORT_ALL))
    5662            0 :         goto C8100;
    5663              :     }
    5664              : 
    5665              :   /* F2008 IMPORT<eos> is distinct from F2018 IMPORT, ALL.  */
    5666         4018 :   if (gfc_match_eos () == MATCH_YES)
    5667              :     {
    5668              :       /* This is the F2008 variant.  */
    5669          340 :       if (gfc_current_ns->import_state == IMPORT_NOT_SET)
    5670              :         {
    5671          331 :           if (current_import_state == IMPORT_ONLY)
    5672            0 :             goto C898;
    5673          331 :           gfc_current_ns->import_state = IMPORT_F2008;
    5674              :         }
    5675              : 
    5676              :       /* Host variables should be imported.  */
    5677          340 :       if (gfc_current_ns->import_state != IMPORT_NONE)
    5678          332 :         gfc_current_ns->has_import_set = 1;
    5679              :       return MATCH_YES;
    5680              :     }
    5681              : 
    5682         3678 :   if (gfc_match (" ::") == MATCH_YES
    5683         3678 :       && gfc_current_ns->import_state != IMPORT_ONLY)
    5684              :     {
    5685         1170 :       if (gfc_match_eos () == MATCH_YES)
    5686            1 :         goto expecting_list;
    5687         1169 :       gfc_current_ns->import_state = IMPORT_F2008;
    5688              :     }
    5689         2508 :   else if (gfc_current_ns->import_state == IMPORT_ONLY)
    5690              :     {
    5691           19 :       if (gfc_match_eos () == MATCH_YES)
    5692            0 :         goto expecting_list;
    5693              :     }
    5694              : 
    5695         4366 :   for(;;)
    5696              :     {
    5697         4366 :       sym = NULL;
    5698         4366 :       m = gfc_match (" %n", name);
    5699         4366 :       switch (m)
    5700              :         {
    5701         4366 :         case MATCH_YES:
    5702              :           /* Before checking if the symbol is available from host
    5703              :              association into a SUBROUTINE or FUNCTION within an
    5704              :              INTERFACE, check if it is already in local scope.  */
    5705         4366 :           gfc_find_symbol (name, gfc_current_ns, 1, &sym);
    5706         4366 :           if (sym
    5707           25 :               && gfc_state_stack->previous
    5708           25 :               && gfc_state_stack->previous->state == COMP_INTERFACE)
    5709              :             {
    5710            2 :                gfc_error ("import-name %qs at %C is in the "
    5711              :                           "local scope", name);
    5712            2 :                return MATCH_ERROR;
    5713              :             }
    5714              : 
    5715         4364 :           if (gfc_current_ns->parent != NULL
    5716         4364 :               && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
    5717              :             {
    5718            0 :                gfc_error ("Type name %qs at %C is ambiguous", name);
    5719            0 :                return MATCH_ERROR;
    5720              :             }
    5721         4364 :           else if (!sym
    5722            5 :                    && gfc_current_ns->proc_name
    5723            4 :                    && gfc_current_ns->proc_name->ns->parent
    5724         4365 :                    && gfc_find_symbol (name,
    5725              :                                        gfc_current_ns->proc_name->ns->parent,
    5726              :                                        1, &sym))
    5727              :             {
    5728            0 :                gfc_error ("Type name %qs at %C is ambiguous", name);
    5729            0 :                return MATCH_ERROR;
    5730              :             }
    5731              : 
    5732         4364 :           if (sym == NULL)
    5733              :             {
    5734            5 :               if (gfc_current_ns->proc_name
    5735            4 :                   && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
    5736              :                 {
    5737            1 :                   gfc_error ("Cannot IMPORT %qs from host scoping unit "
    5738              :                              "at %C - does not exist.", name);
    5739            1 :                   return MATCH_ERROR;
    5740              :                 }
    5741              :               else
    5742              :                 {
    5743              :                   /* This might be a procedure that has not yet been parsed. If
    5744              :                      so gfc_fixup_sibling_symbols will replace this symbol with
    5745              :                      that of the procedure.  */
    5746            4 :                   gfc_get_sym_tree (name, gfc_current_ns, &st, false,
    5747              :                                     &gfc_current_locus);
    5748            4 :                   st->n.sym->refs++;
    5749            4 :                   st->n.sym->attr.imported = 1;
    5750            4 :                   st->import_only = 1;
    5751            4 :                   goto next_item;
    5752              :                 }
    5753              :             }
    5754              : 
    5755         4359 :           st = gfc_find_symtree (gfc_current_ns->sym_root, name);
    5756         4359 :           if (st && st->n.sym && st->n.sym->attr.imported)
    5757              :             {
    5758            0 :               gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
    5759              :                            "at %C", name);
    5760            0 :               goto next_item;
    5761              :             }
    5762              : 
    5763         4359 :           st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
    5764         4359 :           st->n.sym = sym;
    5765         4359 :           sym->refs++;
    5766         4359 :           sym->attr.imported = 1;
    5767         4359 :           st->import_only = 1;
    5768              : 
    5769         4359 :           if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
    5770              :             {
    5771              :               /* The actual derived type is stored in a symtree with the first
    5772              :                  letter of the name capitalized; the symtree with the all
    5773              :                  lower-case name contains the associated generic function.  */
    5774          599 :               st = gfc_new_symtree (&gfc_current_ns->sym_root,
    5775              :                                     gfc_dt_upper_string (name));
    5776          599 :               st->n.sym = sym;
    5777          599 :               sym->refs++;
    5778          599 :               sym->attr.imported = 1;
    5779          599 :               st->import_only = 1;
    5780              :             }
    5781              : 
    5782         4359 :           goto next_item;
    5783              : 
    5784              :         case MATCH_NO:
    5785              :           break;
    5786              : 
    5787              :         case MATCH_ERROR:
    5788              :           return MATCH_ERROR;
    5789              :         }
    5790              : 
    5791         4363 :     next_item:
    5792         4363 :       if (gfc_match_eos () == MATCH_YES)
    5793              :         break;
    5794          689 :       if (gfc_match_char (',') != MATCH_YES)
    5795            0 :         goto syntax;
    5796              :     }
    5797              : 
    5798              :   return MATCH_YES;
    5799              : 
    5800            0 : syntax:
    5801            0 :   gfc_error ("Syntax error in IMPORT statement at %C");
    5802            0 :   return MATCH_ERROR;
    5803              : 
    5804            4 : C897:
    5805            4 :   gfc_error ("F2018: C897 IMPORT statement at %C cannot appear in a main "
    5806              :              "program, an external subprogram, a module or block data");
    5807            4 :   return MATCH_ERROR;
    5808              : 
    5809            0 : C898:
    5810            0 :   gfc_error ("F2018: C898 IMPORT statement at %C is not permitted because "
    5811              :              "a scoping unit has an ONLY specifier, can only have IMPORT "
    5812              :              "with an ONLY specifier");
    5813            0 :   return MATCH_ERROR;
    5814              : 
    5815            0 : C899:
    5816            0 :   gfc_error ("F2018: C899 IMPORT, NONE shall not appear in the scoping unit"
    5817              :              " of a submodule as at %C");
    5818            0 :   return MATCH_ERROR;
    5819              : 
    5820            2 : C8100:
    5821            4 :   gfc_error ("F2018: C8100 IMPORT statement at %C is not permitted because "
    5822              :              "%s has already been declared, which must be unique in the "
    5823              :              "scoping unit",
    5824            2 :              gfc_current_ns->import_state == IMPORT_ALL ? "IMPORT, ALL" :
    5825              :                                                           "IMPORT, NONE");
    5826            2 :   return MATCH_ERROR;
    5827              : 
    5828            1 : expecting_list:
    5829            1 :   gfc_error ("Expecting list of named entities at %C");
    5830            1 :   return MATCH_ERROR;
    5831              : }
    5832              : 
    5833              : 
    5834              : /* A minimal implementation of gfc_match without whitespace, escape
    5835              :    characters or variable arguments.  Returns true if the next
    5836              :    characters match the TARGET template exactly.  */
    5837              : 
    5838              : static bool
    5839       147858 : match_string_p (const char *target)
    5840              : {
    5841       147858 :   const char *p;
    5842              : 
    5843       928871 :   for (p = target; *p; p++)
    5844       781014 :     if ((char) gfc_next_ascii_char () != *p)
    5845              :       return false;
    5846              :   return true;
    5847              : }
    5848              : 
    5849              : /* Matches an attribute specification including array specs.  If
    5850              :    successful, leaves the variables current_attr and current_as
    5851              :    holding the specification.  Also sets the colon_seen variable for
    5852              :    later use by matchers associated with initializations.
    5853              : 
    5854              :    This subroutine is a little tricky in the sense that we don't know
    5855              :    if we really have an attr-spec until we hit the double colon.
    5856              :    Until that time, we can only return MATCH_NO.  This forces us to
    5857              :    check for duplicate specification at this level.  */
    5858              : 
    5859              : static match
    5860       218811 : match_attr_spec (void)
    5861              : {
    5862              :   /* Modifiers that can exist in a type statement.  */
    5863       218811 :   enum
    5864              :   { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
    5865              :     DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
    5866              :     DECL_DIMENSION, DECL_EXTERNAL,
    5867              :     DECL_INTRINSIC, DECL_OPTIONAL,
    5868              :     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
    5869              :     DECL_STATIC, DECL_AUTOMATIC,
    5870              :     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
    5871              :     DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
    5872              :     DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
    5873              :   };
    5874              : 
    5875              : /* GFC_DECL_END is the sentinel, index starts at 0.  */
    5876              : #define NUM_DECL GFC_DECL_END
    5877              : 
    5878              :   /* Make sure that values from sym_intent are safe to be used here.  */
    5879       218811 :   gcc_assert (INTENT_IN > 0);
    5880              : 
    5881       218811 :   locus start, seen_at[NUM_DECL];
    5882       218811 :   int seen[NUM_DECL];
    5883       218811 :   unsigned int d;
    5884       218811 :   const char *attr;
    5885       218811 :   match m;
    5886       218811 :   bool t;
    5887              : 
    5888       218811 :   gfc_clear_attr (&current_attr);
    5889       218811 :   start = gfc_current_locus;
    5890              : 
    5891       218811 :   current_as = NULL;
    5892       218811 :   colon_seen = 0;
    5893       218811 :   attr_seen = 0;
    5894              : 
    5895              :   /* See if we get all of the keywords up to the final double colon.  */
    5896      5907897 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    5897      5689086 :     seen[d] = 0;
    5898              : 
    5899       338680 :   for (;;)
    5900              :     {
    5901       338680 :       char ch;
    5902              : 
    5903       338680 :       d = DECL_NONE;
    5904       338680 :       gfc_gobble_whitespace ();
    5905              : 
    5906       338680 :       ch = gfc_next_ascii_char ();
    5907       338680 :       if (ch == ':')
    5908              :         {
    5909              :           /* This is the successful exit condition for the loop.  */
    5910       184958 :           if (gfc_next_ascii_char () == ':')
    5911              :             break;
    5912              :         }
    5913       153722 :       else if (ch == ',')
    5914              :         {
    5915       119881 :           gfc_gobble_whitespace ();
    5916       119881 :           switch (gfc_peek_ascii_char ())
    5917              :             {
    5918        18721 :             case 'a':
    5919        18721 :               gfc_next_ascii_char ();
    5920        18721 :               switch (gfc_next_ascii_char ())
    5921              :                 {
    5922        18655 :                 case 'l':
    5923        18655 :                   if (match_string_p ("locatable"))
    5924              :                     {
    5925              :                       /* Matched "allocatable".  */
    5926              :                       d = DECL_ALLOCATABLE;
    5927              :                     }
    5928              :                   break;
    5929              : 
    5930           25 :                 case 's':
    5931           25 :                   if (match_string_p ("ynchronous"))
    5932              :                     {
    5933              :                       /* Matched "asynchronous".  */
    5934              :                       d = DECL_ASYNCHRONOUS;
    5935              :                     }
    5936              :                   break;
    5937              : 
    5938           41 :                 case 'u':
    5939           41 :                   if (match_string_p ("tomatic"))
    5940              :                     {
    5941              :                       /* Matched "automatic".  */
    5942              :                       d = DECL_AUTOMATIC;
    5943              :                     }
    5944              :                   break;
    5945              :                 }
    5946              :               break;
    5947              : 
    5948          164 :             case 'b':
    5949              :               /* Try and match the bind(c).  */
    5950          164 :               m = gfc_match_bind_c (NULL, true);
    5951          164 :               if (m == MATCH_YES)
    5952              :                 d = DECL_IS_BIND_C;
    5953            0 :               else if (m == MATCH_ERROR)
    5954            0 :                 goto cleanup;
    5955              :               break;
    5956              : 
    5957         2164 :             case 'c':
    5958         2164 :               gfc_next_ascii_char ();
    5959         2164 :               if ('o' != gfc_next_ascii_char ())
    5960              :                 break;
    5961         2163 :               switch (gfc_next_ascii_char ())
    5962              :                 {
    5963           68 :                 case 'd':
    5964           68 :                   if (match_string_p ("imension"))
    5965              :                     {
    5966              :                       d = DECL_CODIMENSION;
    5967              :                       break;
    5968              :                     }
    5969              :                   /* FALLTHRU */
    5970         2095 :                 case 'n':
    5971         2095 :                   if (match_string_p ("tiguous"))
    5972              :                     {
    5973              :                       d = DECL_CONTIGUOUS;
    5974              :                       break;
    5975              :                     }
    5976              :                 }
    5977              :               break;
    5978              : 
    5979        19753 :             case 'd':
    5980        19753 :               if (match_string_p ("dimension"))
    5981              :                 d = DECL_DIMENSION;
    5982              :               break;
    5983              : 
    5984          177 :             case 'e':
    5985          177 :               if (match_string_p ("external"))
    5986              :                 d = DECL_EXTERNAL;
    5987              :               break;
    5988              : 
    5989        28142 :             case 'i':
    5990        28142 :               if (match_string_p ("int"))
    5991              :                 {
    5992        28142 :                   ch = gfc_next_ascii_char ();
    5993        28142 :                   if (ch == 'e')
    5994              :                     {
    5995        28136 :                       if (match_string_p ("nt"))
    5996              :                         {
    5997              :                           /* Matched "intent".  */
    5998        28135 :                           d = match_intent_spec ();
    5999        28135 :                           if (d == INTENT_UNKNOWN)
    6000              :                             {
    6001            2 :                               m = MATCH_ERROR;
    6002            2 :                               goto cleanup;
    6003              :                             }
    6004              :                         }
    6005              :                     }
    6006            6 :                   else if (ch == 'r')
    6007              :                     {
    6008            6 :                       if (match_string_p ("insic"))
    6009              :                         {
    6010              :                           /* Matched "intrinsic".  */
    6011              :                           d = DECL_INTRINSIC;
    6012              :                         }
    6013              :                     }
    6014              :                 }
    6015              :               break;
    6016              : 
    6017          293 :             case 'k':
    6018          293 :               if (match_string_p ("kind"))
    6019              :                 d = DECL_KIND;
    6020              :               break;
    6021              : 
    6022          313 :             case 'l':
    6023          313 :               if (match_string_p ("len"))
    6024              :                 d = DECL_LEN;
    6025              :               break;
    6026              : 
    6027         5073 :             case 'o':
    6028         5073 :               if (match_string_p ("optional"))
    6029              :                 d = DECL_OPTIONAL;
    6030              :               break;
    6031              : 
    6032        27193 :             case 'p':
    6033        27193 :               gfc_next_ascii_char ();
    6034        27193 :               switch (gfc_next_ascii_char ())
    6035              :                 {
    6036        14307 :                 case 'a':
    6037        14307 :                   if (match_string_p ("rameter"))
    6038              :                     {
    6039              :                       /* Matched "parameter".  */
    6040              :                       d = DECL_PARAMETER;
    6041              :                     }
    6042              :                   break;
    6043              : 
    6044        12365 :                 case 'o':
    6045        12365 :                   if (match_string_p ("inter"))
    6046              :                     {
    6047              :                       /* Matched "pointer".  */
    6048              :                       d = DECL_POINTER;
    6049              :                     }
    6050              :                   break;
    6051              : 
    6052          268 :                 case 'r':
    6053          268 :                   ch = gfc_next_ascii_char ();
    6054          268 :                   if (ch == 'i')
    6055              :                     {
    6056          217 :                       if (match_string_p ("vate"))
    6057              :                         {
    6058              :                           /* Matched "private".  */
    6059              :                           d = DECL_PRIVATE;
    6060              :                         }
    6061              :                     }
    6062           51 :                   else if (ch == 'o')
    6063              :                     {
    6064           51 :                       if (match_string_p ("tected"))
    6065              :                         {
    6066              :                           /* Matched "protected".  */
    6067              :                           d = DECL_PROTECTED;
    6068              :                         }
    6069              :                     }
    6070              :                   break;
    6071              : 
    6072          253 :                 case 'u':
    6073          253 :                   if (match_string_p ("blic"))
    6074              :                     {
    6075              :                       /* Matched "public".  */
    6076              :                       d = DECL_PUBLIC;
    6077              :                     }
    6078              :                   break;
    6079              :                 }
    6080              :               break;
    6081              : 
    6082         1216 :             case 's':
    6083         1216 :               gfc_next_ascii_char ();
    6084         1216 :               switch (gfc_next_ascii_char ())
    6085              :                 {
    6086         1203 :                   case 'a':
    6087         1203 :                     if (match_string_p ("ve"))
    6088              :                       {
    6089              :                         /* Matched "save".  */
    6090              :                         d = DECL_SAVE;
    6091              :                       }
    6092              :                     break;
    6093              : 
    6094           13 :                   case 't':
    6095           13 :                     if (match_string_p ("atic"))
    6096              :                       {
    6097              :                         /* Matched "static".  */
    6098              :                         d = DECL_STATIC;
    6099              :                       }
    6100              :                     break;
    6101              :                 }
    6102              :               break;
    6103              : 
    6104         5532 :             case 't':
    6105         5532 :               if (match_string_p ("target"))
    6106              :                 d = DECL_TARGET;
    6107              :               break;
    6108              : 
    6109        11140 :             case 'v':
    6110        11140 :               gfc_next_ascii_char ();
    6111        11140 :               ch = gfc_next_ascii_char ();
    6112        11140 :               if (ch == 'a')
    6113              :                 {
    6114        10631 :                   if (match_string_p ("lue"))
    6115              :                     {
    6116              :                       /* Matched "value".  */
    6117              :                       d = DECL_VALUE;
    6118              :                     }
    6119              :                 }
    6120          509 :               else if (ch == 'o')
    6121              :                 {
    6122          509 :                   if (match_string_p ("latile"))
    6123              :                     {
    6124              :                       /* Matched "volatile".  */
    6125              :                       d = DECL_VOLATILE;
    6126              :                     }
    6127              :                 }
    6128              :               break;
    6129              :             }
    6130              :         }
    6131              : 
    6132              :       /* No double colon and no recognizable decl_type, so assume that
    6133              :          we've been looking at something else the whole time.  */
    6134              :       if (d == DECL_NONE)
    6135              :         {
    6136        33844 :           m = MATCH_NO;
    6137        33844 :           goto cleanup;
    6138              :         }
    6139              : 
    6140              :       /* Check to make sure any parens are paired up correctly.  */
    6141       119877 :       if (gfc_match_parens () == MATCH_ERROR)
    6142              :         {
    6143            1 :           m = MATCH_ERROR;
    6144            1 :           goto cleanup;
    6145              :         }
    6146              : 
    6147       119876 :       seen[d]++;
    6148       119876 :       seen_at[d] = gfc_current_locus;
    6149              : 
    6150       119876 :       if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
    6151              :         {
    6152        19820 :           gfc_array_spec *as = NULL;
    6153              : 
    6154        19820 :           m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
    6155              :                                     d == DECL_CODIMENSION);
    6156              : 
    6157        19820 :           if (current_as == NULL)
    6158        19795 :             current_as = as;
    6159           25 :           else if (m == MATCH_YES)
    6160              :             {
    6161           25 :               if (!merge_array_spec (as, current_as, false))
    6162            2 :                 m = MATCH_ERROR;
    6163           25 :               free (as);
    6164              :             }
    6165              : 
    6166        19820 :           if (m == MATCH_NO)
    6167              :             {
    6168            0 :               if (d == DECL_CODIMENSION)
    6169            0 :                 gfc_error ("Missing codimension specification at %C");
    6170              :               else
    6171            0 :                 gfc_error ("Missing dimension specification at %C");
    6172              :               m = MATCH_ERROR;
    6173              :             }
    6174              : 
    6175        19820 :           if (m == MATCH_ERROR)
    6176            7 :             goto cleanup;
    6177              :         }
    6178              :     }
    6179              : 
    6180              :   /* Since we've seen a double colon, we have to be looking at an
    6181              :      attr-spec.  This means that we can now issue errors.  */
    6182      4993818 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    6183      4808863 :     if (seen[d] > 1)
    6184              :       {
    6185            2 :         switch (d)
    6186              :           {
    6187              :           case DECL_ALLOCATABLE:
    6188              :             attr = "ALLOCATABLE";
    6189              :             break;
    6190            0 :           case DECL_ASYNCHRONOUS:
    6191            0 :             attr = "ASYNCHRONOUS";
    6192            0 :             break;
    6193            0 :           case DECL_CODIMENSION:
    6194            0 :             attr = "CODIMENSION";
    6195            0 :             break;
    6196            0 :           case DECL_CONTIGUOUS:
    6197            0 :             attr = "CONTIGUOUS";
    6198            0 :             break;
    6199            0 :           case DECL_DIMENSION:
    6200            0 :             attr = "DIMENSION";
    6201            0 :             break;
    6202            0 :           case DECL_EXTERNAL:
    6203            0 :             attr = "EXTERNAL";
    6204            0 :             break;
    6205            0 :           case DECL_IN:
    6206            0 :             attr = "INTENT (IN)";
    6207            0 :             break;
    6208            0 :           case DECL_OUT:
    6209            0 :             attr = "INTENT (OUT)";
    6210            0 :             break;
    6211            0 :           case DECL_INOUT:
    6212            0 :             attr = "INTENT (IN OUT)";
    6213            0 :             break;
    6214            0 :           case DECL_INTRINSIC:
    6215            0 :             attr = "INTRINSIC";
    6216            0 :             break;
    6217            0 :           case DECL_OPTIONAL:
    6218            0 :             attr = "OPTIONAL";
    6219            0 :             break;
    6220            0 :           case DECL_KIND:
    6221            0 :             attr = "KIND";
    6222            0 :             break;
    6223            0 :           case DECL_LEN:
    6224            0 :             attr = "LEN";
    6225            0 :             break;
    6226            0 :           case DECL_PARAMETER:
    6227            0 :             attr = "PARAMETER";
    6228            0 :             break;
    6229            0 :           case DECL_POINTER:
    6230            0 :             attr = "POINTER";
    6231            0 :             break;
    6232            0 :           case DECL_PROTECTED:
    6233            0 :             attr = "PROTECTED";
    6234            0 :             break;
    6235            0 :           case DECL_PRIVATE:
    6236            0 :             attr = "PRIVATE";
    6237            0 :             break;
    6238            0 :           case DECL_PUBLIC:
    6239            0 :             attr = "PUBLIC";
    6240            0 :             break;
    6241            0 :           case DECL_SAVE:
    6242            0 :             attr = "SAVE";
    6243            0 :             break;
    6244            0 :           case DECL_STATIC:
    6245            0 :             attr = "STATIC";
    6246            0 :             break;
    6247            1 :           case DECL_AUTOMATIC:
    6248            1 :             attr = "AUTOMATIC";
    6249            1 :             break;
    6250            0 :           case DECL_TARGET:
    6251            0 :             attr = "TARGET";
    6252            0 :             break;
    6253            0 :           case DECL_IS_BIND_C:
    6254            0 :             attr = "IS_BIND_C";
    6255            0 :             break;
    6256            0 :           case DECL_VALUE:
    6257            0 :             attr = "VALUE";
    6258            0 :             break;
    6259            1 :           case DECL_VOLATILE:
    6260            1 :             attr = "VOLATILE";
    6261            1 :             break;
    6262            0 :           default:
    6263            0 :             attr = NULL;        /* This shouldn't happen.  */
    6264              :           }
    6265              : 
    6266            2 :         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
    6267            2 :         m = MATCH_ERROR;
    6268            2 :         goto cleanup;
    6269              :       }
    6270              : 
    6271              :   /* Now that we've dealt with duplicate attributes, add the attributes
    6272              :      to the current attribute.  */
    6273      4992998 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    6274              :     {
    6275      4808116 :       if (seen[d] == 0)
    6276      4688256 :         continue;
    6277              :       else
    6278       119860 :         attr_seen = 1;
    6279              : 
    6280       119860 :       if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
    6281           52 :           && !flag_dec_static)
    6282              :         {
    6283            3 :           gfc_error ("%s at %L is a DEC extension, enable with "
    6284              :                      "%<-fdec-static%>",
    6285              :                      d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
    6286            2 :           m = MATCH_ERROR;
    6287            2 :           goto cleanup;
    6288              :         }
    6289              :       /* Allow SAVE with STATIC, but don't complain.  */
    6290           50 :       if (d == DECL_STATIC && seen[DECL_SAVE])
    6291            0 :         continue;
    6292              : 
    6293       119858 :       if (gfc_comp_struct (gfc_current_state ())
    6294         6965 :           && d != DECL_DIMENSION && d != DECL_CODIMENSION
    6295         6001 :           && d != DECL_POINTER   && d != DECL_PRIVATE
    6296         4311 :           && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
    6297              :         {
    6298         4254 :           bool is_derived = gfc_current_state () == COMP_DERIVED;
    6299         4254 :           if (d == DECL_ALLOCATABLE)
    6300              :             {
    6301         3635 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6302              :                                    ? G_("ALLOCATABLE attribute at %C in a "
    6303              :                                         "TYPE definition")
    6304              :                                    : G_("ALLOCATABLE attribute at %C in a "
    6305              :                                         "STRUCTURE definition")))
    6306              :                 {
    6307            2 :                   m = MATCH_ERROR;
    6308            2 :                   goto cleanup;
    6309              :                 }
    6310              :             }
    6311          619 :           else if (d == DECL_KIND)
    6312              :             {
    6313          291 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6314              :                                    ? G_("KIND attribute at %C in a "
    6315              :                                         "TYPE definition")
    6316              :                                    : G_("KIND attribute at %C in a "
    6317              :                                         "STRUCTURE definition")))
    6318              :                 {
    6319            1 :                   m = MATCH_ERROR;
    6320            1 :                   goto cleanup;
    6321              :                 }
    6322          290 :               if (current_ts.type != BT_INTEGER)
    6323              :                 {
    6324            2 :                   gfc_error ("Component with KIND attribute at %C must be "
    6325              :                              "INTEGER");
    6326            2 :                   m = MATCH_ERROR;
    6327            2 :                   goto cleanup;
    6328              :                 }
    6329              :             }
    6330          328 :           else if (d == DECL_LEN)
    6331              :             {
    6332          312 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6333              :                                    ? G_("LEN attribute at %C in a "
    6334              :                                         "TYPE definition")
    6335              :                                    : G_("LEN attribute at %C in a "
    6336              :                                         "STRUCTURE definition")))
    6337              :                 {
    6338            0 :                   m = MATCH_ERROR;
    6339            0 :                   goto cleanup;
    6340              :                 }
    6341          312 :               if (current_ts.type != BT_INTEGER)
    6342              :                 {
    6343            1 :                   gfc_error ("Component with LEN attribute at %C must be "
    6344              :                              "INTEGER");
    6345            1 :                   m = MATCH_ERROR;
    6346            1 :                   goto cleanup;
    6347              :                 }
    6348              :             }
    6349              :           else
    6350              :             {
    6351           32 :               gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
    6352              :                                          "TYPE definition")
    6353              :                                     : G_("Attribute at %L is not allowed in a "
    6354              :                                          "STRUCTURE definition"), &seen_at[d]);
    6355           16 :               m = MATCH_ERROR;
    6356           16 :               goto cleanup;
    6357              :             }
    6358              :         }
    6359              : 
    6360       119836 :       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
    6361          470 :           && gfc_current_state () != COMP_MODULE)
    6362              :         {
    6363          147 :           if (d == DECL_PRIVATE)
    6364              :             attr = "PRIVATE";
    6365              :           else
    6366           43 :             attr = "PUBLIC";
    6367          147 :           if (gfc_current_state () == COMP_DERIVED
    6368          141 :               && gfc_state_stack->previous
    6369          141 :               && gfc_state_stack->previous->state == COMP_MODULE)
    6370              :             {
    6371          138 :               if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
    6372              :                                    "at %L in a TYPE definition", attr,
    6373              :                                    &seen_at[d]))
    6374              :                 {
    6375            2 :                   m = MATCH_ERROR;
    6376            2 :                   goto cleanup;
    6377              :                 }
    6378              :             }
    6379              :           else
    6380              :             {
    6381            9 :               gfc_error ("%s attribute at %L is not allowed outside of the "
    6382              :                          "specification part of a module", attr, &seen_at[d]);
    6383            9 :               m = MATCH_ERROR;
    6384            9 :               goto cleanup;
    6385              :             }
    6386              :         }
    6387              : 
    6388       119825 :       if (gfc_current_state () != COMP_DERIVED
    6389       112891 :           && (d == DECL_KIND || d == DECL_LEN))
    6390              :         {
    6391            3 :           gfc_error ("Attribute at %L is not allowed outside a TYPE "
    6392              :                      "definition", &seen_at[d]);
    6393            3 :           m = MATCH_ERROR;
    6394            3 :           goto cleanup;
    6395              :         }
    6396              : 
    6397       119822 :       switch (d)
    6398              :         {
    6399        18653 :         case DECL_ALLOCATABLE:
    6400        18653 :           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
    6401        18653 :           break;
    6402              : 
    6403           24 :         case DECL_ASYNCHRONOUS:
    6404           24 :           if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
    6405              :             t = false;
    6406              :           else
    6407           24 :             t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
    6408              :           break;
    6409              : 
    6410           66 :         case DECL_CODIMENSION:
    6411           66 :           t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
    6412           66 :           break;
    6413              : 
    6414         2095 :         case DECL_CONTIGUOUS:
    6415         2095 :           if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
    6416              :             t = false;
    6417              :           else
    6418         2094 :             t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
    6419              :           break;
    6420              : 
    6421        19745 :         case DECL_DIMENSION:
    6422        19745 :           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
    6423        19745 :           break;
    6424              : 
    6425          176 :         case DECL_EXTERNAL:
    6426          176 :           t = gfc_add_external (&current_attr, &seen_at[d]);
    6427          176 :           break;
    6428              : 
    6429        21228 :         case DECL_IN:
    6430        21228 :           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
    6431        21228 :           break;
    6432              : 
    6433         3736 :         case DECL_OUT:
    6434         3736 :           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
    6435         3736 :           break;
    6436              : 
    6437         3165 :         case DECL_INOUT:
    6438         3165 :           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
    6439         3165 :           break;
    6440              : 
    6441            5 :         case DECL_INTRINSIC:
    6442            5 :           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
    6443            5 :           break;
    6444              : 
    6445         5072 :         case DECL_OPTIONAL:
    6446         5072 :           t = gfc_add_optional (&current_attr, &seen_at[d]);
    6447         5072 :           break;
    6448              : 
    6449          288 :         case DECL_KIND:
    6450          288 :           t = gfc_add_kind (&current_attr, &seen_at[d]);
    6451          288 :           break;
    6452              : 
    6453          311 :         case DECL_LEN:
    6454          311 :           t = gfc_add_len (&current_attr, &seen_at[d]);
    6455          311 :           break;
    6456              : 
    6457        14306 :         case DECL_PARAMETER:
    6458        14306 :           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
    6459        14306 :           break;
    6460              : 
    6461        12364 :         case DECL_POINTER:
    6462        12364 :           t = gfc_add_pointer (&current_attr, &seen_at[d]);
    6463        12364 :           break;
    6464              : 
    6465           50 :         case DECL_PROTECTED:
    6466           50 :           if (gfc_current_state () != COMP_MODULE
    6467           48 :               || (gfc_current_ns->proc_name
    6468           48 :                   && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
    6469              :             {
    6470            2 :                gfc_error ("PROTECTED at %C only allowed in specification "
    6471              :                           "part of a module");
    6472            2 :                t = false;
    6473            2 :                break;
    6474              :             }
    6475              : 
    6476           48 :           if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
    6477              :             t = false;
    6478              :           else
    6479           44 :             t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
    6480              :           break;
    6481              : 
    6482          214 :         case DECL_PRIVATE:
    6483          214 :           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
    6484              :                               &seen_at[d]);
    6485          214 :           break;
    6486              : 
    6487          245 :         case DECL_PUBLIC:
    6488          245 :           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
    6489              :                               &seen_at[d]);
    6490          245 :           break;
    6491              : 
    6492         1213 :         case DECL_STATIC:
    6493         1213 :         case DECL_SAVE:
    6494         1213 :           t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
    6495         1213 :           break;
    6496              : 
    6497           37 :         case DECL_AUTOMATIC:
    6498           37 :           t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
    6499           37 :           break;
    6500              : 
    6501         5530 :         case DECL_TARGET:
    6502         5530 :           t = gfc_add_target (&current_attr, &seen_at[d]);
    6503         5530 :           break;
    6504              : 
    6505          163 :         case DECL_IS_BIND_C:
    6506          163 :            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
    6507          163 :            break;
    6508              : 
    6509        10630 :         case DECL_VALUE:
    6510        10630 :           if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
    6511              :             t = false;
    6512              :           else
    6513        10630 :             t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
    6514              :           break;
    6515              : 
    6516          506 :         case DECL_VOLATILE:
    6517          506 :           if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
    6518              :             t = false;
    6519              :           else
    6520          505 :             t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
    6521              :           break;
    6522              : 
    6523            0 :         default:
    6524            0 :           gfc_internal_error ("match_attr_spec(): Bad attribute");
    6525              :         }
    6526              : 
    6527       119816 :       if (!t)
    6528              :         {
    6529           35 :           m = MATCH_ERROR;
    6530           35 :           goto cleanup;
    6531              :         }
    6532              :     }
    6533              : 
    6534              :   /* Since Fortran 2008 module variables implicitly have the SAVE attribute.  */
    6535       184882 :   if ((gfc_current_state () == COMP_MODULE
    6536       184882 :        || gfc_current_state () == COMP_SUBMODULE)
    6537         5850 :       && !current_attr.save
    6538         5668 :       && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    6539         5576 :     current_attr.save = SAVE_IMPLICIT;
    6540              : 
    6541       184882 :   colon_seen = 1;
    6542       184882 :   return MATCH_YES;
    6543              : 
    6544        33929 : cleanup:
    6545        33929 :   gfc_current_locus = start;
    6546        33929 :   gfc_free_array_spec (current_as);
    6547        33929 :   current_as = NULL;
    6548        33929 :   attr_seen = 0;
    6549        33929 :   return m;
    6550              : }
    6551              : 
    6552              : 
    6553              : /* Set the binding label, dest_label, either with the binding label
    6554              :    stored in the given gfc_typespec, ts, or if none was provided, it
    6555              :    will be the symbol name in all lower case, as required by the draft
    6556              :    (J3/04-007, section 15.4.1).  If a binding label was given and
    6557              :    there is more than one argument (num_idents), it is an error.  */
    6558              : 
    6559              : static bool
    6560          347 : set_binding_label (const char **dest_label, const char *sym_name,
    6561              :                    int num_idents)
    6562              : {
    6563          347 :   if (num_idents > 1 && has_name_equals)
    6564              :     {
    6565            4 :       gfc_error ("Multiple identifiers provided with "
    6566              :                  "single NAME= specifier at %C");
    6567            4 :       return false;
    6568              :     }
    6569              : 
    6570          343 :   if (curr_binding_label)
    6571              :     /* Binding label given; store in temp holder till have sym.  */
    6572          108 :     *dest_label = curr_binding_label;
    6573              :   else
    6574              :     {
    6575              :       /* No binding label given, and the NAME= specifier did not exist,
    6576              :          which means there was no NAME="".  */
    6577          235 :       if (sym_name != NULL && has_name_equals == 0)
    6578          205 :         *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
    6579              :     }
    6580              : 
    6581              :   return true;
    6582              : }
    6583              : 
    6584              : 
    6585              : /* Set the status of the given common block as being BIND(C) or not,
    6586              :    depending on the given parameter, is_bind_c.  */
    6587              : 
    6588              : static void
    6589           76 : set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
    6590              : {
    6591           76 :   com_block->is_bind_c = is_bind_c;
    6592           76 :   return;
    6593              : }
    6594              : 
    6595              : 
    6596              : /* Verify that the given gfc_typespec is for a C interoperable type.  */
    6597              : 
    6598              : bool
    6599        21419 : gfc_verify_c_interop (gfc_typespec *ts)
    6600              : {
    6601        21419 :   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
    6602         4320 :     return ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c;
    6603        17099 :   else if (ts->type == BT_CLASS)
    6604              :     return false;
    6605        17091 :   else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
    6606         3983 :     return false;
    6607              : 
    6608              :   return true;
    6609              : }
    6610              : 
    6611              : 
    6612              : /* Verify that the variables of a given common block, which has been
    6613              :    defined with the attribute specifier bind(c), to be of a C
    6614              :    interoperable type.  Errors will be reported here, if
    6615              :    encountered.  */
    6616              : 
    6617              : bool
    6618            1 : verify_com_block_vars_c_interop (gfc_common_head *com_block)
    6619              : {
    6620            1 :   gfc_symbol *curr_sym = NULL;
    6621            1 :   bool retval = true;
    6622              : 
    6623            1 :   curr_sym = com_block->head;
    6624              : 
    6625              :   /* Make sure we have at least one symbol.  */
    6626            1 :   if (curr_sym == NULL)
    6627              :     return retval;
    6628              : 
    6629              :   /* Here we know we have a symbol, so we'll execute this loop
    6630              :      at least once.  */
    6631            1 :   do
    6632              :     {
    6633              :       /* The second to last param, 1, says this is in a common block.  */
    6634            1 :       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
    6635            1 :       curr_sym = curr_sym->common_next;
    6636            1 :     } while (curr_sym != NULL);
    6637              : 
    6638              :   return retval;
    6639              : }
    6640              : 
    6641              : 
    6642              : /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
    6643              :    an appropriate error message is reported.  */
    6644              : 
    6645              : bool
    6646         7399 : verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
    6647              :                    int is_in_common, gfc_common_head *com_block)
    6648              : {
    6649         7399 :   bool bind_c_function = false;
    6650         7399 :   bool retval = true;
    6651              : 
    6652         7399 :   if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
    6653         7399 :     bind_c_function = true;
    6654              : 
    6655         7399 :   if (tmp_sym->attr.function && tmp_sym->result != NULL)
    6656              :     {
    6657         3150 :       tmp_sym = tmp_sym->result;
    6658              :       /* Make sure it wasn't an implicitly typed result.  */
    6659         3150 :       if (tmp_sym->attr.implicit_type && warn_c_binding_type)
    6660              :         {
    6661            1 :           gfc_warning (OPT_Wc_binding_type,
    6662              :                        "Implicitly declared BIND(C) function %qs at "
    6663              :                        "%L may not be C interoperable", tmp_sym->name,
    6664              :                        &tmp_sym->declared_at);
    6665            1 :           tmp_sym->ts.f90_type = tmp_sym->ts.type;
    6666              :           /* Mark it as C interoperable to prevent duplicate warnings.  */
    6667            1 :           tmp_sym->ts.is_c_interop = 1;
    6668            1 :           tmp_sym->attr.is_c_interop = 1;
    6669              :         }
    6670              :     }
    6671              : 
    6672              :   /* Here, we know we have the bind(c) attribute, so if we have
    6673              :      enough type info, then verify that it's a C interop kind.
    6674              :      The info could be in the symbol already, or possibly still in
    6675              :      the given ts (current_ts), so look in both.  */
    6676         7399 :   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
    6677              :     {
    6678         3309 :       if (!gfc_verify_c_interop (&(tmp_sym->ts)))
    6679              :         {
    6680              :           /* See if we're dealing with a sym in a common block or not.  */
    6681          237 :           if (is_in_common == 1 && warn_c_binding_type)
    6682              :             {
    6683            0 :               gfc_warning (OPT_Wc_binding_type,
    6684              :                            "Variable %qs in common block %qs at %L "
    6685              :                            "may not be a C interoperable "
    6686              :                            "kind though common block %qs is BIND(C)",
    6687              :                            tmp_sym->name, com_block->name,
    6688            0 :                            &(tmp_sym->declared_at), com_block->name);
    6689              :             }
    6690              :           else
    6691              :             {
    6692          237 :               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
    6693          235 :                   || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
    6694              :                 {
    6695            3 :                   gfc_error ("Type declaration %qs at %L is not C "
    6696              :                              "interoperable but it is BIND(C)",
    6697              :                              tmp_sym->name, &(tmp_sym->declared_at));
    6698            3 :                   retval = false;
    6699              :                 }
    6700          234 :               else if (warn_c_binding_type)
    6701            3 :                 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
    6702              :                              "may not be a C interoperable "
    6703              :                              "kind but it is BIND(C)",
    6704              :                              tmp_sym->name, &(tmp_sym->declared_at));
    6705              :             }
    6706              :         }
    6707              : 
    6708              :       /* Variables declared w/in a common block can't be bind(c)
    6709              :          since there's no way for C to see these variables, so there's
    6710              :          semantically no reason for the attribute.  */
    6711         3309 :       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
    6712              :         {
    6713            1 :           gfc_error ("Variable %qs in common block %qs at "
    6714              :                      "%L cannot be declared with BIND(C) "
    6715              :                      "since it is not a global",
    6716            1 :                      tmp_sym->name, com_block->name,
    6717              :                      &(tmp_sym->declared_at));
    6718            1 :           retval = false;
    6719              :         }
    6720              : 
    6721              :       /* Scalar variables that are bind(c) cannot have the pointer
    6722              :          or allocatable attributes.  */
    6723         3309 :       if (tmp_sym->attr.is_bind_c == 1)
    6724              :         {
    6725         2771 :           if (tmp_sym->attr.pointer == 1)
    6726              :             {
    6727            1 :               gfc_error ("Variable %qs at %L cannot have both the "
    6728              :                          "POINTER and BIND(C) attributes",
    6729              :                          tmp_sym->name, &(tmp_sym->declared_at));
    6730            1 :               retval = false;
    6731              :             }
    6732              : 
    6733         2771 :           if (tmp_sym->attr.allocatable == 1)
    6734              :             {
    6735            0 :               gfc_error ("Variable %qs at %L cannot have both the "
    6736              :                          "ALLOCATABLE and BIND(C) attributes",
    6737              :                          tmp_sym->name, &(tmp_sym->declared_at));
    6738            0 :               retval = false;
    6739              :             }
    6740              : 
    6741              :         }
    6742              : 
    6743              :       /* If it is a BIND(C) function, make sure the return value is a
    6744              :          scalar value.  The previous tests in this function made sure
    6745              :          the type is interoperable.  */
    6746         3309 :       if (bind_c_function && tmp_sym->as != NULL)
    6747            2 :         gfc_error ("Return type of BIND(C) function %qs at %L cannot "
    6748              :                    "be an array", tmp_sym->name, &(tmp_sym->declared_at));
    6749              : 
    6750              :       /* BIND(C) functions cannot return a character string.  */
    6751         3150 :       if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
    6752          116 :         if (!gfc_length_one_character_type_p (&tmp_sym->ts))
    6753            4 :           gfc_error ("Return type of BIND(C) function %qs of character "
    6754              :                      "type at %L must have length 1", tmp_sym->name,
    6755              :                          &(tmp_sym->declared_at));
    6756              :     }
    6757              : 
    6758              :   /* See if the symbol has been marked as private.  If it has, warn if
    6759              :      there is a binding label with default binding name.  */
    6760         7399 :   if (tmp_sym->attr.access == ACCESS_PRIVATE
    6761           11 :       && tmp_sym->binding_label
    6762            8 :       && strcmp (tmp_sym->name, tmp_sym->binding_label) == 0
    6763            5 :       && (tmp_sym->attr.flavor == FL_VARIABLE
    6764            4 :           || tmp_sym->attr.if_source == IFSRC_DECL))
    6765            4 :     gfc_warning (OPT_Wsurprising,
    6766              :                  "Symbol %qs at %L is marked PRIVATE but is accessible "
    6767              :                  "via its default binding name %qs", tmp_sym->name,
    6768              :                  &(tmp_sym->declared_at), tmp_sym->binding_label);
    6769              : 
    6770         7399 :   return retval;
    6771              : }
    6772              : 
    6773              : 
    6774              : /* Set the appropriate fields for a symbol that's been declared as
    6775              :    BIND(C) (the is_bind_c flag and the binding label), and verify that
    6776              :    the type is C interoperable.  Errors are reported by the functions
    6777              :    used to set/test these fields.  */
    6778              : 
    6779              : static bool
    6780           47 : set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
    6781              : {
    6782           47 :   bool retval = true;
    6783              : 
    6784              :   /* TODO: Do we need to make sure the vars aren't marked private?  */
    6785              : 
    6786              :   /* Set the is_bind_c bit in symbol_attribute.  */
    6787           47 :   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
    6788              : 
    6789           47 :   if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
    6790              :     return false;
    6791              : 
    6792              :   return retval;
    6793              : }
    6794              : 
    6795              : 
    6796              : /* Set the fields marking the given common block as BIND(C), including
    6797              :    a binding label, and report any errors encountered.  */
    6798              : 
    6799              : static bool
    6800           76 : set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
    6801              : {
    6802           76 :   bool retval = true;
    6803              : 
    6804              :   /* destLabel, common name, typespec (which may have binding label).  */
    6805           76 :   if (!set_binding_label (&com_block->binding_label, com_block->name,
    6806              :                           num_idents))
    6807              :     return false;
    6808              : 
    6809              :   /* Set the given common block (com_block) to being bind(c) (1).  */
    6810           76 :   set_com_block_bind_c (com_block, 1);
    6811              : 
    6812           76 :   return retval;
    6813              : }
    6814              : 
    6815              : 
    6816              : /* Retrieve the list of one or more identifiers that the given bind(c)
    6817              :    attribute applies to.  */
    6818              : 
    6819              : static bool
    6820          102 : get_bind_c_idents (void)
    6821              : {
    6822          102 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    6823          102 :   int num_idents = 0;
    6824          102 :   gfc_symbol *tmp_sym = NULL;
    6825          102 :   match found_id;
    6826          102 :   gfc_common_head *com_block = NULL;
    6827              : 
    6828          102 :   if (gfc_match_name (name) == MATCH_YES)
    6829              :     {
    6830           38 :       found_id = MATCH_YES;
    6831           38 :       gfc_get_ha_symbol (name, &tmp_sym);
    6832              :     }
    6833           64 :   else if (gfc_match_common_name (name) == MATCH_YES)
    6834              :     {
    6835           64 :       found_id = MATCH_YES;
    6836           64 :       com_block = gfc_get_common (name, 0);
    6837              :     }
    6838              :   else
    6839              :     {
    6840            0 :       gfc_error ("Need either entity or common block name for "
    6841              :                  "attribute specification statement at %C");
    6842            0 :       return false;
    6843              :     }
    6844              : 
    6845              :   /* Save the current identifier and look for more.  */
    6846          123 :   do
    6847              :     {
    6848              :       /* Increment the number of identifiers found for this spec stmt.  */
    6849          123 :       num_idents++;
    6850              : 
    6851              :       /* Make sure we have a sym or com block, and verify that it can
    6852              :          be bind(c).  Set the appropriate field(s) and look for more
    6853              :          identifiers.  */
    6854          123 :       if (tmp_sym != NULL || com_block != NULL)
    6855              :         {
    6856          123 :           if (tmp_sym != NULL)
    6857              :             {
    6858           47 :               if (!set_verify_bind_c_sym (tmp_sym, num_idents))
    6859              :                 return false;
    6860              :             }
    6861              :           else
    6862              :             {
    6863           76 :               if (!set_verify_bind_c_com_block (com_block, num_idents))
    6864              :                 return false;
    6865              :             }
    6866              : 
    6867              :           /* Look to see if we have another identifier.  */
    6868          122 :           tmp_sym = NULL;
    6869          122 :           if (gfc_match_eos () == MATCH_YES)
    6870              :             found_id = MATCH_NO;
    6871           21 :           else if (gfc_match_char (',') != MATCH_YES)
    6872              :             found_id = MATCH_NO;
    6873           21 :           else if (gfc_match_name (name) == MATCH_YES)
    6874              :             {
    6875            9 :               found_id = MATCH_YES;
    6876            9 :               gfc_get_ha_symbol (name, &tmp_sym);
    6877              :             }
    6878           12 :           else if (gfc_match_common_name (name) == MATCH_YES)
    6879              :             {
    6880           12 :               found_id = MATCH_YES;
    6881           12 :               com_block = gfc_get_common (name, 0);
    6882              :             }
    6883              :           else
    6884              :             {
    6885            0 :               gfc_error ("Missing entity or common block name for "
    6886              :                          "attribute specification statement at %C");
    6887            0 :               return false;
    6888              :             }
    6889              :         }
    6890              :       else
    6891              :         {
    6892            0 :           gfc_internal_error ("Missing symbol");
    6893              :         }
    6894          122 :     } while (found_id == MATCH_YES);
    6895              : 
    6896              :   /* if we get here we were successful */
    6897              :   return true;
    6898              : }
    6899              : 
    6900              : 
    6901              : /* Try and match a BIND(C) attribute specification statement.  */
    6902              : 
    6903              : match
    6904          140 : gfc_match_bind_c_stmt (void)
    6905              : {
    6906          140 :   match found_match = MATCH_NO;
    6907          140 :   gfc_typespec *ts;
    6908              : 
    6909          140 :   ts = &current_ts;
    6910              : 
    6911              :   /* This may not be necessary.  */
    6912          140 :   gfc_clear_ts (ts);
    6913              :   /* Clear the temporary binding label holder.  */
    6914          140 :   curr_binding_label = NULL;
    6915              : 
    6916              :   /* Look for the bind(c).  */
    6917          140 :   found_match = gfc_match_bind_c (NULL, true);
    6918              : 
    6919          140 :   if (found_match == MATCH_YES)
    6920              :     {
    6921          103 :       if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
    6922              :         return MATCH_ERROR;
    6923              : 
    6924              :       /* Look for the :: now, but it is not required.  */
    6925          102 :       gfc_match (" :: ");
    6926              : 
    6927              :       /* Get the identifier(s) that needs to be updated.  This may need to
    6928              :          change to hand the flag(s) for the attr specified so all identifiers
    6929              :          found can have all appropriate parts updated (assuming that the same
    6930              :          spec stmt can have multiple attrs, such as both bind(c) and
    6931              :          allocatable...).  */
    6932          102 :       if (!get_bind_c_idents ())
    6933              :         /* Error message should have printed already.  */
    6934            1 :         return MATCH_ERROR;
    6935              :     }
    6936              : 
    6937              :   return found_match;
    6938              : }
    6939              : 
    6940              : 
    6941              : /* Match a data declaration statement.  */
    6942              : 
    6943              : match
    6944      1032770 : gfc_match_data_decl (void)
    6945              : {
    6946      1032770 :   gfc_symbol *sym;
    6947      1032770 :   match m;
    6948      1032770 :   int elem;
    6949      1032770 :   gfc_component *comp_tail = NULL;
    6950              : 
    6951      1032770 :   type_param_spec_list = NULL;
    6952      1032770 :   decl_type_param_list = NULL;
    6953              : 
    6954      1032770 :   num_idents_on_line = 0;
    6955              : 
    6956              :   /* Record the last component before we start, so that we can roll back
    6957              :      any components added during this statement on error.  PR106946.
    6958              :      Must be set before any 'goto cleanup' with m == MATCH_ERROR.  */
    6959      1032770 :   if (gfc_comp_struct (gfc_current_state ()))
    6960              :     {
    6961        32257 :       gfc_symbol *block = gfc_current_block ();
    6962        32257 :       if (block)
    6963              :         {
    6964        32257 :           comp_tail = block->components;
    6965        32257 :           if (comp_tail)
    6966        33553 :             while (comp_tail->next)
    6967              :               comp_tail = comp_tail->next;
    6968              :         }
    6969              :     }
    6970              : 
    6971      1032770 :   m = gfc_match_decl_type_spec (&current_ts, 0);
    6972      1032770 :   if (m != MATCH_YES)
    6973              :     return m;
    6974              : 
    6975       217636 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    6976        35595 :         && !gfc_comp_struct (gfc_current_state ()))
    6977              :     {
    6978        32131 :       sym = gfc_use_derived (current_ts.u.derived);
    6979              : 
    6980        32131 :       if (sym == NULL)
    6981              :         {
    6982           22 :           m = MATCH_ERROR;
    6983           22 :           goto cleanup;
    6984              :         }
    6985              : 
    6986        32109 :       current_ts.u.derived = sym;
    6987              :     }
    6988              : 
    6989       217614 :   m = match_attr_spec ();
    6990       217614 :   if (m == MATCH_ERROR)
    6991              :     {
    6992           84 :       m = MATCH_NO;
    6993           84 :       goto cleanup;
    6994              :     }
    6995              : 
    6996              :   /* F2018:C708.  */
    6997       217530 :   if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
    6998              :     {
    6999            6 :       gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
    7000            6 :       m = MATCH_ERROR;
    7001            6 :       goto cleanup;
    7002              :     }
    7003              : 
    7004       217524 :   if (current_ts.type == BT_CLASS
    7005        11168 :         && current_ts.u.derived->attr.unlimited_polymorphic)
    7006         1993 :     goto ok;
    7007              : 
    7008       215531 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    7009        33573 :       && current_ts.u.derived->components == NULL
    7010         2847 :       && !current_ts.u.derived->attr.zero_comp)
    7011              :     {
    7012              : 
    7013          210 :       if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
    7014          136 :         goto ok;
    7015              : 
    7016           74 :       if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
    7017           47 :         goto ok;
    7018              : 
    7019           27 :       gfc_find_symbol (current_ts.u.derived->name,
    7020           27 :                        current_ts.u.derived->ns, 1, &sym);
    7021              : 
    7022              :       /* Any symbol that we find had better be a type definition
    7023              :          which has its components defined, or be a structure definition
    7024              :          actively being parsed.  */
    7025           27 :       if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
    7026           26 :           && (current_ts.u.derived->components != NULL
    7027           26 :               || current_ts.u.derived->attr.zero_comp
    7028           26 :               || current_ts.u.derived == gfc_new_block))
    7029           26 :         goto ok;
    7030              : 
    7031            1 :       gfc_error ("Derived type at %C has not been previously defined "
    7032              :                  "and so cannot appear in a derived type definition");
    7033            1 :       m = MATCH_ERROR;
    7034            1 :       goto cleanup;
    7035              :     }
    7036              : 
    7037       215321 : ok:
    7038              :   /* If we have an old-style character declaration, and no new-style
    7039              :      attribute specifications, then there a comma is optional between
    7040              :      the type specification and the variable list.  */
    7041       217523 :   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
    7042         1407 :     gfc_match_char (',');
    7043              : 
    7044              :   /* Give the types/attributes to symbols that follow. Give the element
    7045              :      a number so that repeat character length expressions can be copied.  */
    7046       217523 :   elem = 1;
    7047       282883 :   for (;;)
    7048              :     {
    7049       282883 :       num_idents_on_line++;
    7050       282883 :       m = variable_decl (elem++);
    7051       282881 :       if (m == MATCH_ERROR)
    7052          415 :         goto cleanup;
    7053       282466 :       if (m == MATCH_NO)
    7054              :         break;
    7055              : 
    7056       282455 :       if (gfc_match_eos () == MATCH_YES)
    7057       217071 :         goto cleanup;
    7058        65384 :       if (gfc_match_char (',') != MATCH_YES)
    7059              :         break;
    7060              :     }
    7061              : 
    7062           35 :   if (!gfc_error_flag_test ())
    7063              :     {
    7064              :       /* An anonymous structure declaration is unambiguous; if we matched one
    7065              :          according to gfc_match_structure_decl, we need to return MATCH_YES
    7066              :          here to avoid confusing the remaining matchers, even if there was an
    7067              :          error during variable_decl.  We must flush any such errors.  Note this
    7068              :          causes the parser to gracefully continue parsing the remaining input
    7069              :          as a structure body, which likely follows.  */
    7070           11 :       if (current_ts.type == BT_DERIVED && current_ts.u.derived
    7071            1 :           && gfc_fl_struct (current_ts.u.derived->attr.flavor))
    7072              :         {
    7073            1 :           gfc_error_now ("Syntax error in anonymous structure declaration"
    7074              :                          " at %C");
    7075              :           /* Skip the bad variable_decl and line up for the start of the
    7076              :              structure body.  */
    7077            1 :           gfc_error_recovery ();
    7078            1 :           m = MATCH_YES;
    7079            1 :           goto cleanup;
    7080              :         }
    7081              : 
    7082           10 :       gfc_error ("Syntax error in data declaration at %C");
    7083              :     }
    7084              : 
    7085           34 :   m = MATCH_ERROR;
    7086              : 
    7087           34 :   gfc_free_data_all (gfc_current_ns);
    7088              : 
    7089       217634 : cleanup:
    7090              :   /* If we failed inside a derived type definition, remove any CLASS
    7091              :      components that were added during this failed statement.  For CLASS
    7092              :      components, gfc_build_class_symbol creates an extra container symbol in
    7093              :      the namespace outside the normal undo machinery.  When reject_statement
    7094              :      later calls gfc_undo_symbols, the declaration state is rolled back but
    7095              :      that helper symbol survives and leaves the component dangling.  Ordinary
    7096              :      components do not create that extra helper symbol, so leave them in
    7097              :      place for the usual follow-up diagnostics.  PR106946.
    7098              : 
    7099              :      CLASS containers are shared between components of the same class type
    7100              :      and attributes (gfc_build_class_symbol reuses existing containers).
    7101              :      We must not free a container that is still referenced by a previously
    7102              :      committed component.  Unlink and free the components first, then clean
    7103              :      up only orphaned containers.  PR124482.  */
    7104       217634 :   if (m == MATCH_ERROR && gfc_comp_struct (gfc_current_state ()))
    7105              :     {
    7106           86 :       gfc_symbol *block = gfc_current_block ();
    7107           86 :       if (block)
    7108              :         {
    7109           86 :           gfc_component **prev;
    7110           86 :           if (comp_tail)
    7111           43 :             prev = &comp_tail->next;
    7112              :           else
    7113           43 :             prev = &block->components;
    7114              : 
    7115              :           /* Record the CLASS container from the removed components.
    7116              :              Normally all components in one declaration share a single
    7117              :              container, but per-variable array specs can produce
    7118              :              additional ones; any beyond the first are harmlessly
    7119              :              leaked until namespace destruction.  */
    7120           86 :           gfc_symbol *fclass_container = NULL;
    7121              : 
    7122          120 :           while (*prev)
    7123              :             {
    7124           34 :               gfc_component *c = *prev;
    7125           34 :               if (c->ts.type == BT_CLASS && c->ts.u.derived
    7126            6 :                   && c->ts.u.derived->attr.is_class)
    7127              :                 {
    7128            3 :                   *prev = c->next;
    7129            3 :                   if (!fclass_container)
    7130            3 :                     fclass_container = c->ts.u.derived;
    7131            3 :                   c->ts.u.derived = NULL;
    7132            3 :                   gfc_free_component (c);
    7133              :                 }
    7134              :               else
    7135           31 :                 prev = &c->next;
    7136              :             }
    7137              : 
    7138              :           /* Free the container only if no remaining component still
    7139              :              references it.  CLASS containers are shared between
    7140              :              components of the same class type and attributes
    7141              :              (gfc_build_class_symbol reuses existing ones).  */
    7142           86 :           if (fclass_container)
    7143              :             {
    7144            3 :               bool shared = false;
    7145            3 :               for (gfc_component *q = block->components; q; q = q->next)
    7146            1 :                 if (q->ts.type == BT_CLASS
    7147            1 :                     && q->ts.u.derived == fclass_container)
    7148              :                   {
    7149              :                     shared = true;
    7150              :                     break;
    7151              :                   }
    7152            3 :               if (!shared)
    7153              :                 {
    7154            2 :                   if (gfc_find_symtree (fclass_container->ns->sym_root,
    7155              :                                         fclass_container->name))
    7156            2 :                     gfc_delete_symtree (&fclass_container->ns->sym_root,
    7157              :                                         fclass_container->name);
    7158            2 :                   gfc_release_symbol (fclass_container);
    7159              :                 }
    7160              :             }
    7161              :         }
    7162              :     }
    7163              : 
    7164       217634 :   if (saved_kind_expr)
    7165          180 :     gfc_free_expr (saved_kind_expr);
    7166       217634 :   if (type_param_spec_list)
    7167          985 :     gfc_free_actual_arglist (type_param_spec_list);
    7168       217634 :   if (decl_type_param_list)
    7169          942 :     gfc_free_actual_arglist (decl_type_param_list);
    7170       217634 :   saved_kind_expr = NULL;
    7171       217634 :   gfc_free_array_spec (current_as);
    7172       217634 :   current_as = NULL;
    7173       217634 :   return m;
    7174              : }
    7175              : 
    7176              : static bool
    7177        24717 : in_module_or_interface(void)
    7178              : {
    7179        24717 :   if (gfc_current_state () == COMP_MODULE
    7180        24717 :       || gfc_current_state () == COMP_SUBMODULE
    7181        24717 :       || gfc_current_state () == COMP_INTERFACE)
    7182              :     return true;
    7183              : 
    7184        20710 :   if (gfc_state_stack->state == COMP_CONTAINS
    7185        19828 :       || gfc_state_stack->state == COMP_FUNCTION
    7186        19722 :       || gfc_state_stack->state == COMP_SUBROUTINE)
    7187              :     {
    7188          988 :       gfc_state_data *p;
    7189         1032 :       for (p = gfc_state_stack->previous; p ; p = p->previous)
    7190              :         {
    7191         1028 :           if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
    7192          118 :               || p->state == COMP_INTERFACE)
    7193              :             return true;
    7194              :         }
    7195              :     }
    7196              :     return false;
    7197              : }
    7198              : 
    7199              : /* Match a prefix associated with a function or subroutine
    7200              :    declaration.  If the typespec pointer is nonnull, then a typespec
    7201              :    can be matched.  Note that if nothing matches, MATCH_YES is
    7202              :    returned (the null string was matched).  */
    7203              : 
    7204              : match
    7205       244683 : gfc_match_prefix (gfc_typespec *ts)
    7206              : {
    7207       244683 :   bool seen_type;
    7208       244683 :   bool seen_impure;
    7209       244683 :   bool found_prefix;
    7210              : 
    7211       244683 :   gfc_clear_attr (&current_attr);
    7212       244683 :   seen_type = false;
    7213       244683 :   seen_impure = false;
    7214              : 
    7215       244683 :   gcc_assert (!gfc_matching_prefix);
    7216       244683 :   gfc_matching_prefix = true;
    7217              : 
    7218       254438 :   do
    7219              :     {
    7220       274464 :       found_prefix = false;
    7221              : 
    7222              :       /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
    7223              :          corresponding attribute seems natural and distinguishes these
    7224              :          procedures from procedure types of PROC_MODULE, which these are
    7225              :          as well.  */
    7226       274464 :       if (gfc_match ("module% ") == MATCH_YES)
    7227              :         {
    7228        24992 :           if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
    7229          275 :             goto error;
    7230              : 
    7231        24717 :           if (!in_module_or_interface ())
    7232              :             {
    7233        19726 :               gfc_error ("MODULE prefix at %C found outside of a module, "
    7234              :                          "submodule, or interface");
    7235        19726 :               goto error;
    7236              :             }
    7237              : 
    7238         4991 :           current_attr.module_procedure = 1;
    7239         4991 :           found_prefix = true;
    7240              :         }
    7241              : 
    7242       254463 :       if (!seen_type && ts != NULL)
    7243              :         {
    7244       137081 :           match m;
    7245       137081 :           m = gfc_match_decl_type_spec (ts, 0);
    7246       137081 :           if (m == MATCH_ERROR)
    7247           15 :             goto error;
    7248       137066 :           if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
    7249              :             {
    7250              :               seen_type = true;
    7251              :               found_prefix = true;
    7252              :             }
    7253              :         }
    7254              : 
    7255       254448 :       if (gfc_match ("elemental% ") == MATCH_YES)
    7256              :         {
    7257         5377 :           if (!gfc_add_elemental (&current_attr, NULL))
    7258            2 :             goto error;
    7259              : 
    7260              :           found_prefix = true;
    7261              :         }
    7262              : 
    7263       254446 :       if (gfc_match ("pure% ") == MATCH_YES)
    7264              :         {
    7265         2454 :           if (!gfc_add_pure (&current_attr, NULL))
    7266            2 :             goto error;
    7267              : 
    7268              :           found_prefix = true;
    7269              :         }
    7270              : 
    7271       254444 :       if (gfc_match ("recursive% ") == MATCH_YES)
    7272              :         {
    7273          469 :           if (!gfc_add_recursive (&current_attr, NULL))
    7274            2 :             goto error;
    7275              : 
    7276              :           found_prefix = true;
    7277              :         }
    7278              : 
    7279              :       /* IMPURE is a somewhat special case, as it needs not set an actual
    7280              :          attribute but rather only prevents ELEMENTAL routines from being
    7281              :          automatically PURE.  */
    7282       254442 :       if (gfc_match ("impure% ") == MATCH_YES)
    7283              :         {
    7284          729 :           if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
    7285            4 :             goto error;
    7286              : 
    7287              :           seen_impure = true;
    7288              :           found_prefix = true;
    7289              :         }
    7290              :     }
    7291              :   while (found_prefix);
    7292              : 
    7293              :   /* IMPURE and PURE must not both appear, of course.  */
    7294       224657 :   if (seen_impure && current_attr.pure)
    7295              :     {
    7296            4 :       gfc_error ("PURE and IMPURE must not appear both at %C");
    7297            4 :       goto error;
    7298              :     }
    7299              : 
    7300              :   /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE.  */
    7301       223932 :   if (!seen_impure && current_attr.elemental && !current_attr.pure)
    7302              :     {
    7303         4682 :       if (!gfc_add_pure (&current_attr, NULL))
    7304            0 :         goto error;
    7305              :     }
    7306              : 
    7307              :   /* At this point, the next item is not a prefix.  */
    7308       224653 :   gcc_assert (gfc_matching_prefix);
    7309              : 
    7310       224653 :   gfc_matching_prefix = false;
    7311       224653 :   return MATCH_YES;
    7312              : 
    7313        20030 : error:
    7314        20030 :   gcc_assert (gfc_matching_prefix);
    7315        20030 :   gfc_matching_prefix = false;
    7316        20030 :   return MATCH_ERROR;
    7317              : }
    7318              : 
    7319              : 
    7320              : /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
    7321              : 
    7322              : static bool
    7323        63744 : copy_prefix (symbol_attribute *dest, locus *where)
    7324              : {
    7325        63744 :   if (dest->module_procedure)
    7326              :     {
    7327          732 :       if (current_attr.elemental)
    7328           13 :         dest->elemental = 1;
    7329              : 
    7330          732 :       if (current_attr.pure)
    7331           61 :         dest->pure = 1;
    7332              : 
    7333          732 :       if (current_attr.recursive)
    7334            8 :         dest->recursive = 1;
    7335              : 
    7336              :       /* Module procedures are unusual in that the 'dest' is copied from
    7337              :          the interface declaration. However, this is an opportunity to
    7338              :          check that the submodule declaration is compliant with the
    7339              :          interface.  */
    7340          732 :       if (dest->elemental && !current_attr.elemental)
    7341              :         {
    7342            1 :           gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
    7343              :                      "missing at %L", where);
    7344            1 :           return false;
    7345              :         }
    7346              : 
    7347          731 :       if (dest->pure && !current_attr.pure)
    7348              :         {
    7349            1 :           gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
    7350              :                      "missing at %L", where);
    7351            1 :           return false;
    7352              :         }
    7353              : 
    7354          730 :       if (dest->recursive && !current_attr.recursive)
    7355              :         {
    7356            1 :           gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
    7357              :                      "missing at %L", where);
    7358            1 :           return false;
    7359              :         }
    7360              : 
    7361              :       return true;
    7362              :     }
    7363              : 
    7364        63012 :   if (current_attr.elemental && !gfc_add_elemental (dest, where))
    7365              :     return false;
    7366              : 
    7367        63010 :   if (current_attr.pure && !gfc_add_pure (dest, where))
    7368              :     return false;
    7369              : 
    7370        63010 :   if (current_attr.recursive && !gfc_add_recursive (dest, where))
    7371              :     return false;
    7372              : 
    7373              :   return true;
    7374              : }
    7375              : 
    7376              : 
    7377              : /* Match a formal argument list or, if typeparam is true, a
    7378              :    type_param_name_list.  */
    7379              : 
    7380              : match
    7381       491668 : gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
    7382              :                           int null_flag, bool typeparam)
    7383              : {
    7384       491668 :   gfc_formal_arglist *head, *tail, *p, *q;
    7385       491668 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7386       491668 :   gfc_symbol *sym;
    7387       491668 :   match m;
    7388       491668 :   gfc_formal_arglist *formal = NULL;
    7389              : 
    7390       491668 :   head = tail = NULL;
    7391              : 
    7392              :   /* Keep the interface formal argument list and null it so that the
    7393              :      matching for the new declaration can be done.  The numbers and
    7394              :      names of the arguments are checked here. The interface formal
    7395              :      arguments are retained in formal_arglist and the characteristics
    7396              :      are compared in resolve.cc(resolve_fl_procedure).  See the remark
    7397              :      in get_proc_name about the eventual need to copy the formal_arglist
    7398              :      and populate the formal namespace of the interface symbol.  */
    7399       491668 :   if (progname->attr.module_procedure
    7400          736 :       && progname->attr.host_assoc)
    7401              :     {
    7402          196 :       formal = progname->formal;
    7403          196 :       progname->formal = NULL;
    7404              :     }
    7405              : 
    7406       491668 :   if (gfc_match_char ('(') != MATCH_YES)
    7407              :     {
    7408       290599 :       if (null_flag)
    7409         6750 :         goto ok;
    7410              :       return MATCH_NO;
    7411              :     }
    7412              : 
    7413       201069 :   if (gfc_match_char (')') == MATCH_YES)
    7414              :   {
    7415        10509 :     if (typeparam)
    7416              :       {
    7417            1 :         gfc_error_now ("A type parameter list is required at %C");
    7418            1 :         m = MATCH_ERROR;
    7419            1 :         goto cleanup;
    7420              :       }
    7421              :     else
    7422        10508 :       goto ok;
    7423              :   }
    7424              : 
    7425       253030 :   for (;;)
    7426              :     {
    7427       253030 :       gfc_gobble_whitespace ();
    7428       253030 :       if (gfc_match_char ('*') == MATCH_YES)
    7429              :         {
    7430        10437 :           sym = NULL;
    7431        10437 :           if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
    7432              :                              "Alternate-return argument at %C"))
    7433              :             {
    7434            1 :               m = MATCH_ERROR;
    7435            1 :               goto cleanup;
    7436              :             }
    7437        10436 :           else if (typeparam)
    7438            2 :             gfc_error_now ("A parameter name is required at %C");
    7439              :         }
    7440              :       else
    7441              :         {
    7442       242593 :           locus loc = gfc_current_locus;
    7443       242593 :           m = gfc_match_name (name);
    7444       242593 :           if (m != MATCH_YES)
    7445              :             {
    7446        16718 :               if(typeparam)
    7447            1 :                 gfc_error_now ("A parameter name is required at %C");
    7448        16734 :               goto cleanup;
    7449              :             }
    7450       225875 :           loc = gfc_get_location_range (NULL, 0, &loc, 1, &gfc_current_locus);
    7451              : 
    7452       225875 :           if (!typeparam && gfc_get_symbol (name, NULL, &sym, &loc))
    7453           16 :             goto cleanup;
    7454       225859 :           else if (typeparam
    7455       225859 :                    && gfc_get_symbol (name, progname->f2k_derived, &sym, &loc))
    7456            0 :             goto cleanup;
    7457              :         }
    7458              : 
    7459       236295 :       p = gfc_get_formal_arglist ();
    7460              : 
    7461       236295 :       if (head == NULL)
    7462              :         head = tail = p;
    7463              :       else
    7464              :         {
    7465        61767 :           tail->next = p;
    7466        61767 :           tail = p;
    7467              :         }
    7468              : 
    7469       236295 :       tail->sym = sym;
    7470              : 
    7471              :       /* We don't add the VARIABLE flavor because the name could be a
    7472              :          dummy procedure.  We don't apply these attributes to formal
    7473              :          arguments of statement functions.  */
    7474       225859 :       if (sym != NULL && !st_flag
    7475       337838 :           && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
    7476       101543 :               || !gfc_missing_attr (&sym->attr, NULL)))
    7477              :         {
    7478            0 :           m = MATCH_ERROR;
    7479            0 :           goto cleanup;
    7480              :         }
    7481              : 
    7482              :       /* The name of a program unit can be in a different namespace,
    7483              :          so check for it explicitly.  After the statement is accepted,
    7484              :          the name is checked for especially in gfc_get_symbol().  */
    7485       236295 :       if (gfc_new_block != NULL && sym != NULL && !typeparam
    7486       100279 :           && strcmp (sym->name, gfc_new_block->name) == 0)
    7487              :         {
    7488            0 :           gfc_error ("Name %qs at %C is the name of the procedure",
    7489              :                      sym->name);
    7490            0 :           m = MATCH_ERROR;
    7491            0 :           goto cleanup;
    7492              :         }
    7493              : 
    7494       236295 :       if (gfc_match_char (')') == MATCH_YES)
    7495       125456 :         goto ok;
    7496              : 
    7497       110839 :       m = gfc_match_char (',');
    7498       110839 :       if (m != MATCH_YES)
    7499              :         {
    7500        48369 :           if (typeparam)
    7501            1 :             gfc_error_now ("Expected parameter list in type declaration "
    7502              :                            "at %C");
    7503              :           else
    7504        48368 :             gfc_error ("Unexpected junk in formal argument list at %C");
    7505        48369 :           goto cleanup;
    7506              :         }
    7507              :     }
    7508              : 
    7509       142714 : ok:
    7510              :   /* Check for duplicate symbols in the formal argument list.  */
    7511       142714 :   if (head != NULL)
    7512              :     {
    7513       185582 :       for (p = head; p->next; p = p->next)
    7514              :         {
    7515        60174 :           if (p->sym == NULL)
    7516          338 :             continue;
    7517              : 
    7518       237249 :           for (q = p->next; q; q = q->next)
    7519       177461 :             if (p->sym == q->sym)
    7520              :               {
    7521           48 :                 if (typeparam)
    7522            1 :                   gfc_error_now ("Duplicate name %qs in parameter "
    7523              :                                  "list at %C", p->sym->name);
    7524              :                 else
    7525           47 :                   gfc_error ("Duplicate symbol %qs in formal argument "
    7526              :                              "list at %C", p->sym->name);
    7527              : 
    7528           48 :                 m = MATCH_ERROR;
    7529           48 :                 goto cleanup;
    7530              :               }
    7531              :         }
    7532              :     }
    7533              : 
    7534       142666 :   if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
    7535              :     {
    7536            0 :       m = MATCH_ERROR;
    7537            0 :       goto cleanup;
    7538              :     }
    7539              : 
    7540              :   /* gfc_error_now used in following and return with MATCH_YES because
    7541              :      doing otherwise results in a cascade of extraneous errors and in
    7542              :      some cases an ICE in symbol.cc(gfc_release_symbol).  */
    7543       142666 :   if (progname->attr.module_procedure && progname->attr.host_assoc)
    7544              :     {
    7545          195 :       bool arg_count_mismatch = false;
    7546              : 
    7547          195 :       if (!formal && head)
    7548              :         arg_count_mismatch = true;
    7549              : 
    7550              :       /* Abbreviated module procedure declaration is not meant to have any
    7551              :          formal arguments!  */
    7552          195 :       if (!progname->abr_modproc_decl && formal && !head)
    7553          195 :         arg_count_mismatch = true;
    7554              : 
    7555          377 :       for (p = formal, q = head; p && q; p = p->next, q = q->next)
    7556              :         {
    7557          182 :           if ((p->next != NULL && q->next == NULL)
    7558          181 :               || (p->next == NULL && q->next != NULL))
    7559              :             arg_count_mismatch = true;
    7560          180 :           else if ((p->sym == NULL && q->sym == NULL)
    7561          180 :                     || (p->sym && q->sym
    7562          178 :                         && strcmp (p->sym->name, q->sym->name) == 0))
    7563          176 :             continue;
    7564              :           else
    7565              :             {
    7566            4 :               if (q->sym == NULL)
    7567            1 :                 gfc_error_now ("MODULE PROCEDURE formal argument %qs "
    7568              :                                "conflicts with alternate return at %C",
    7569              :                                p->sym->name);
    7570            3 :               else if (p->sym == NULL)
    7571            1 :                 gfc_error_now ("MODULE PROCEDURE formal argument is "
    7572              :                                "alternate return and conflicts with "
    7573              :                                "%qs in the separate declaration at %C",
    7574              :                                q->sym->name);
    7575              :               else
    7576            2 :                 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
    7577              :                                "argument names (%s/%s) at %C",
    7578              :                                p->sym->name, q->sym->name);
    7579              :             }
    7580              :         }
    7581              : 
    7582          195 :       if (arg_count_mismatch)
    7583            4 :         gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
    7584              :                        "formal arguments at %C");
    7585              :     }
    7586              : 
    7587              :   return MATCH_YES;
    7588              : 
    7589        65153 : cleanup:
    7590        65153 :   gfc_free_formal_arglist (head);
    7591        65153 :   return m;
    7592              : }
    7593              : 
    7594              : 
    7595              : /* Match a RESULT specification following a function declaration or
    7596              :    ENTRY statement.  Also matches the end-of-statement.  */
    7597              : 
    7598              : static match
    7599         8620 : match_result (gfc_symbol *function, gfc_symbol **result)
    7600              : {
    7601         8620 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7602         8620 :   gfc_symbol *r;
    7603         8620 :   match m;
    7604              : 
    7605         8620 :   if (gfc_match (" result (") != MATCH_YES)
    7606              :     return MATCH_NO;
    7607              : 
    7608         6064 :   m = gfc_match_name (name);
    7609         6064 :   if (m != MATCH_YES)
    7610              :     return m;
    7611              : 
    7612              :   /* Get the right paren, and that's it because there could be the
    7613              :      bind(c) attribute after the result clause.  */
    7614         6064 :   if (gfc_match_char (')') != MATCH_YES)
    7615              :     {
    7616              :      /* TODO: should report the missing right paren here.  */
    7617              :       return MATCH_ERROR;
    7618              :     }
    7619              : 
    7620         6064 :   if (strcmp (function->name, name) == 0)
    7621              :     {
    7622            1 :       gfc_error ("RESULT variable at %C must be different than function name");
    7623            1 :       return MATCH_ERROR;
    7624              :     }
    7625              : 
    7626         6063 :   if (gfc_get_symbol (name, NULL, &r))
    7627              :     return MATCH_ERROR;
    7628              : 
    7629         6063 :   if (!gfc_add_result (&r->attr, r->name, NULL))
    7630              :     return MATCH_ERROR;
    7631              : 
    7632         6063 :   *result = r;
    7633              : 
    7634         6063 :   return MATCH_YES;
    7635              : }
    7636              : 
    7637              : 
    7638              : /* Match a function suffix, which could be a combination of a result
    7639              :    clause and BIND(C), either one, or neither.  The draft does not
    7640              :    require them to come in a specific order.  */
    7641              : 
    7642              : static match
    7643         8624 : gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
    7644              : {
    7645         8624 :   match is_bind_c;   /* Found bind(c).  */
    7646         8624 :   match is_result;   /* Found result clause.  */
    7647         8624 :   match found_match; /* Status of whether we've found a good match.  */
    7648         8624 :   char peek_char;    /* Character we're going to peek at.  */
    7649         8624 :   bool allow_binding_name;
    7650              : 
    7651              :   /* Initialize to having found nothing.  */
    7652         8624 :   found_match = MATCH_NO;
    7653         8624 :   is_bind_c = MATCH_NO;
    7654         8624 :   is_result = MATCH_NO;
    7655              : 
    7656              :   /* Get the next char to narrow between result and bind(c).  */
    7657         8624 :   gfc_gobble_whitespace ();
    7658         8624 :   peek_char = gfc_peek_ascii_char ();
    7659              : 
    7660              :   /* C binding names are not allowed for internal procedures.  */
    7661         8624 :   if (gfc_current_state () == COMP_CONTAINS
    7662         4809 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    7663              :     allow_binding_name = false;
    7664              :   else
    7665         6926 :     allow_binding_name = true;
    7666              : 
    7667         8624 :   switch (peek_char)
    7668              :     {
    7669         5693 :     case 'r':
    7670              :       /* Look for result clause.  */
    7671         5693 :       is_result = match_result (sym, result);
    7672         5693 :       if (is_result == MATCH_YES)
    7673              :         {
    7674              :           /* Now see if there is a bind(c) after it.  */
    7675         5692 :           is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    7676              :           /* We've found the result clause and possibly bind(c).  */
    7677         5692 :           found_match = MATCH_YES;
    7678              :         }
    7679              :       else
    7680              :         /* This should only be MATCH_ERROR.  */
    7681              :         found_match = is_result;
    7682              :       break;
    7683         2931 :     case 'b':
    7684              :       /* Look for bind(c) first.  */
    7685         2931 :       is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    7686         2931 :       if (is_bind_c == MATCH_YES)
    7687              :         {
    7688              :           /* Now see if a result clause followed it.  */
    7689         2927 :           is_result = match_result (sym, result);
    7690         2927 :           found_match = MATCH_YES;
    7691              :         }
    7692              :       else
    7693              :         {
    7694              :           /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
    7695              :           found_match = MATCH_ERROR;
    7696              :         }
    7697              :       break;
    7698            0 :     default:
    7699            0 :       gfc_error ("Unexpected junk after function declaration at %C");
    7700            0 :       found_match = MATCH_ERROR;
    7701            0 :       break;
    7702              :     }
    7703              : 
    7704         8619 :   if (is_bind_c == MATCH_YES)
    7705              :     {
    7706              :       /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
    7707         3094 :       if (gfc_current_state () == COMP_CONTAINS
    7708          423 :           && sym->ns->proc_name->attr.flavor != FL_MODULE
    7709         3112 :           && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
    7710              :                               "at %L may not be specified for an internal "
    7711              :                               "procedure", &gfc_current_locus))
    7712              :         return MATCH_ERROR;
    7713              : 
    7714         3091 :       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
    7715            0 :         return MATCH_ERROR;
    7716              :     }
    7717              : 
    7718              :   return found_match;
    7719              : }
    7720              : 
    7721              : 
    7722              : /* Procedure pointer return value without RESULT statement:
    7723              :    Add "hidden" result variable named "ppr@".  */
    7724              : 
    7725              : static bool
    7726        75422 : add_hidden_procptr_result (gfc_symbol *sym)
    7727              : {
    7728        75422 :   bool case1,case2;
    7729              : 
    7730        75422 :   if (gfc_notification_std (GFC_STD_F2003) == ERROR)
    7731              :     return false;
    7732              : 
    7733              :   /* First usage case: PROCEDURE and EXTERNAL statements.  */
    7734         1539 :   case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
    7735         1539 :           && strcmp (gfc_current_block ()->name, sym->name) == 0
    7736        75821 :           && sym->attr.external;
    7737              :   /* Second usage case: INTERFACE statements.  */
    7738        14874 :   case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
    7739        14874 :           && gfc_state_stack->previous->state == COMP_FUNCTION
    7740        75469 :           && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
    7741              : 
    7742        75238 :   if (case1 || case2)
    7743              :     {
    7744          125 :       gfc_symtree *stree;
    7745          125 :       if (case1)
    7746           95 :         gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
    7747              :       else
    7748              :         {
    7749           30 :           gfc_symtree *st2;
    7750           30 :           gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
    7751           30 :           st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
    7752           30 :           st2->n.sym = stree->n.sym;
    7753           30 :           stree->n.sym->refs++;
    7754              :         }
    7755          125 :       sym->result = stree->n.sym;
    7756              : 
    7757          125 :       sym->result->attr.proc_pointer = sym->attr.proc_pointer;
    7758          125 :       sym->result->attr.pointer = sym->attr.pointer;
    7759          125 :       sym->result->attr.external = sym->attr.external;
    7760          125 :       sym->result->attr.referenced = sym->attr.referenced;
    7761          125 :       sym->result->ts = sym->ts;
    7762          125 :       sym->attr.proc_pointer = 0;
    7763          125 :       sym->attr.pointer = 0;
    7764          125 :       sym->attr.external = 0;
    7765          125 :       if (sym->result->attr.external && sym->result->attr.pointer)
    7766              :         {
    7767            4 :           sym->result->attr.pointer = 0;
    7768            4 :           sym->result->attr.proc_pointer = 1;
    7769              :         }
    7770              : 
    7771          125 :       return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
    7772              :     }
    7773              :   /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
    7774        75143 :   else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
    7775          411 :            && sym->result && sym->result != sym && sym->result->attr.external
    7776           28 :            && sym == gfc_current_ns->proc_name
    7777           28 :            && sym == sym->result->ns->proc_name
    7778           28 :            && strcmp ("ppr@", sym->result->name) == 0)
    7779              :     {
    7780           28 :       sym->result->attr.proc_pointer = 1;
    7781           28 :       sym->attr.pointer = 0;
    7782           28 :       return true;
    7783              :     }
    7784              :   else
    7785              :     return false;
    7786              : }
    7787              : 
    7788              : 
    7789              : /* Match the interface for a PROCEDURE declaration,
    7790              :    including brackets (R1212).  */
    7791              : 
    7792              : static match
    7793         1630 : match_procedure_interface (gfc_symbol **proc_if)
    7794              : {
    7795         1630 :   match m;
    7796         1630 :   gfc_symtree *st;
    7797         1630 :   locus old_loc, entry_loc;
    7798         1630 :   gfc_namespace *old_ns = gfc_current_ns;
    7799         1630 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7800              : 
    7801         1630 :   old_loc = entry_loc = gfc_current_locus;
    7802         1630 :   gfc_clear_ts (&current_ts);
    7803              : 
    7804         1630 :   if (gfc_match (" (") != MATCH_YES)
    7805              :     {
    7806            1 :       gfc_current_locus = entry_loc;
    7807            1 :       return MATCH_NO;
    7808              :     }
    7809              : 
    7810              :   /* Get the type spec. for the procedure interface.  */
    7811         1629 :   old_loc = gfc_current_locus;
    7812         1629 :   m = gfc_match_decl_type_spec (&current_ts, 0);
    7813         1629 :   gfc_gobble_whitespace ();
    7814         1629 :   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
    7815          395 :     goto got_ts;
    7816              : 
    7817         1234 :   if (m == MATCH_ERROR)
    7818              :     return m;
    7819              : 
    7820              :   /* Procedure interface is itself a procedure.  */
    7821         1234 :   gfc_current_locus = old_loc;
    7822         1234 :   m = gfc_match_name (name);
    7823              : 
    7824              :   /* First look to see if it is already accessible in the current
    7825              :      namespace because it is use associated or contained.  */
    7826         1234 :   st = NULL;
    7827         1234 :   if (gfc_find_sym_tree (name, NULL, 0, &st))
    7828              :     return MATCH_ERROR;
    7829              : 
    7830              :   /* If it is still not found, then try the parent namespace, if it
    7831              :      exists and create the symbol there if it is still not found.  */
    7832         1234 :   if (gfc_current_ns->parent)
    7833          435 :     gfc_current_ns = gfc_current_ns->parent;
    7834         1234 :   if (st == NULL && gfc_get_ha_sym_tree (name, &st))
    7835              :     return MATCH_ERROR;
    7836              : 
    7837         1234 :   gfc_current_ns = old_ns;
    7838         1234 :   *proc_if = st->n.sym;
    7839              : 
    7840         1234 :   if (*proc_if)
    7841              :     {
    7842         1234 :       (*proc_if)->refs++;
    7843              :       /* Resolve interface if possible. That way, attr.procedure is only set
    7844              :          if it is declared by a later procedure-declaration-stmt, which is
    7845              :          invalid per F08:C1216 (cf. resolve_procedure_interface).  */
    7846         1234 :       while ((*proc_if)->ts.interface
    7847         1241 :              && *proc_if != (*proc_if)->ts.interface)
    7848            7 :         *proc_if = (*proc_if)->ts.interface;
    7849              : 
    7850         1234 :       if ((*proc_if)->attr.flavor == FL_UNKNOWN
    7851          389 :           && (*proc_if)->ts.type == BT_UNKNOWN
    7852         1623 :           && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
    7853              :                               (*proc_if)->name, NULL))
    7854              :         return MATCH_ERROR;
    7855              :     }
    7856              : 
    7857            0 : got_ts:
    7858         1629 :   if (gfc_match (" )") != MATCH_YES)
    7859              :     {
    7860            0 :       gfc_current_locus = entry_loc;
    7861            0 :       return MATCH_NO;
    7862              :     }
    7863              : 
    7864              :   return MATCH_YES;
    7865              : }
    7866              : 
    7867              : 
    7868              : /* Match a PROCEDURE declaration (R1211).  */
    7869              : 
    7870              : static match
    7871         1197 : match_procedure_decl (void)
    7872              : {
    7873         1197 :   match m;
    7874         1197 :   gfc_symbol *sym, *proc_if = NULL;
    7875         1197 :   int num;
    7876         1197 :   gfc_expr *initializer = NULL;
    7877              : 
    7878              :   /* Parse interface (with brackets).  */
    7879         1197 :   m = match_procedure_interface (&proc_if);
    7880         1197 :   if (m != MATCH_YES)
    7881              :     return m;
    7882              : 
    7883              :   /* Parse attributes (with colons).  */
    7884         1197 :   m = match_attr_spec();
    7885         1197 :   if (m == MATCH_ERROR)
    7886              :     return MATCH_ERROR;
    7887              : 
    7888         1196 :   if (current_attr.allocatable)
    7889              :     {
    7890            2 :       current_attr.procedure = 1;
    7891            2 :       gfc_check_conflict (&current_attr, NULL, &gfc_current_locus);
    7892            2 :       return MATCH_ERROR;
    7893              :     }
    7894              : 
    7895         1194 :   if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
    7896              :     {
    7897           53 :       current_attr.is_bind_c = 1;
    7898           53 :       has_name_equals = 0;
    7899           53 :       curr_binding_label = NULL;
    7900              :     }
    7901              : 
    7902              :   /* Get procedure symbols.  */
    7903         1194 :   for(num=1;;num++)
    7904              :     {
    7905         1273 :       m = gfc_match_symbol (&sym, 0);
    7906         1273 :       if (m == MATCH_NO)
    7907            1 :         goto syntax;
    7908         1272 :       else if (m == MATCH_ERROR)
    7909              :         return m;
    7910              : 
    7911              :       /* Add current_attr to the symbol attributes.  */
    7912         1272 :       if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
    7913              :         return MATCH_ERROR;
    7914              : 
    7915         1270 :       if (sym->attr.is_bind_c)
    7916              :         {
    7917              :           /* Check for C1218.  */
    7918           90 :           if (!proc_if || !proc_if->attr.is_bind_c)
    7919              :             {
    7920            1 :               gfc_error ("BIND(C) attribute at %C requires "
    7921              :                         "an interface with BIND(C)");
    7922            1 :               return MATCH_ERROR;
    7923              :             }
    7924              :           /* Check for C1217.  */
    7925           89 :           if (has_name_equals && sym->attr.pointer)
    7926              :             {
    7927            1 :               gfc_error ("BIND(C) procedure with NAME may not have "
    7928              :                         "POINTER attribute at %C");
    7929            1 :               return MATCH_ERROR;
    7930              :             }
    7931           88 :           if (has_name_equals && sym->attr.dummy)
    7932              :             {
    7933            1 :               gfc_error ("Dummy procedure at %C may not have "
    7934              :                         "BIND(C) attribute with NAME");
    7935            1 :               return MATCH_ERROR;
    7936              :             }
    7937              :           /* Set binding label for BIND(C).  */
    7938           87 :           if (!set_binding_label (&sym->binding_label, sym->name, num))
    7939              :             return MATCH_ERROR;
    7940              :         }
    7941              : 
    7942         1266 :       if (!gfc_add_external (&sym->attr, NULL))
    7943              :         return MATCH_ERROR;
    7944              : 
    7945         1262 :       if (add_hidden_procptr_result (sym))
    7946           68 :         sym = sym->result;
    7947              : 
    7948         1262 :       if (!gfc_add_proc (&sym->attr, sym->name, NULL))
    7949              :         return MATCH_ERROR;
    7950              : 
    7951              :       /* Set interface.  */
    7952         1262 :       if (proc_if != NULL)
    7953              :         {
    7954          919 :           if (sym->ts.type != BT_UNKNOWN)
    7955              :             {
    7956            1 :               gfc_error ("Procedure %qs at %L already has basic type of %s",
    7957              :                          sym->name, &gfc_current_locus,
    7958              :                          gfc_basic_typename (sym->ts.type));
    7959            1 :               return MATCH_ERROR;
    7960              :             }
    7961          918 :           sym->ts.interface = proc_if;
    7962          918 :           sym->attr.untyped = 1;
    7963          918 :           sym->attr.if_source = IFSRC_IFBODY;
    7964              :         }
    7965          343 :       else if (current_ts.type != BT_UNKNOWN)
    7966              :         {
    7967          199 :           if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
    7968              :             return MATCH_ERROR;
    7969          198 :           sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    7970          198 :           sym->ts.interface->ts = current_ts;
    7971          198 :           sym->ts.interface->attr.flavor = FL_PROCEDURE;
    7972          198 :           sym->ts.interface->attr.function = 1;
    7973          198 :           sym->attr.function = 1;
    7974          198 :           sym->attr.if_source = IFSRC_UNKNOWN;
    7975              :         }
    7976              : 
    7977         1260 :       if (gfc_match (" =>") == MATCH_YES)
    7978              :         {
    7979          110 :           if (!current_attr.pointer)
    7980              :             {
    7981            0 :               gfc_error ("Initialization at %C isn't for a pointer variable");
    7982            0 :               m = MATCH_ERROR;
    7983            0 :               goto cleanup;
    7984              :             }
    7985              : 
    7986          110 :           m = match_pointer_init (&initializer, 1);
    7987          110 :           if (m != MATCH_YES)
    7988            1 :             goto cleanup;
    7989              : 
    7990          109 :           if (!add_init_expr_to_sym (sym->name, &initializer,
    7991              :                                      &gfc_current_locus,
    7992              :                                      gfc_current_ns->cl_list))
    7993            0 :             goto cleanup;
    7994              : 
    7995              :         }
    7996              : 
    7997         1259 :       if (gfc_match_eos () == MATCH_YES)
    7998              :         return MATCH_YES;
    7999           79 :       if (gfc_match_char (',') != MATCH_YES)
    8000            0 :         goto syntax;
    8001              :     }
    8002              : 
    8003            1 : syntax:
    8004            1 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
    8005            1 :   return MATCH_ERROR;
    8006              : 
    8007            1 : cleanup:
    8008              :   /* Free stuff up and return.  */
    8009            1 :   gfc_free_expr (initializer);
    8010            1 :   return m;
    8011              : }
    8012              : 
    8013              : 
    8014              : static match
    8015              : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
    8016              : 
    8017              : 
    8018              : /* Match a procedure pointer component declaration (R445).  */
    8019              : 
    8020              : static match
    8021          433 : match_ppc_decl (void)
    8022              : {
    8023          433 :   match m;
    8024          433 :   gfc_symbol *proc_if = NULL;
    8025          433 :   gfc_typespec ts;
    8026          433 :   int num;
    8027          433 :   gfc_component *c;
    8028          433 :   gfc_expr *initializer = NULL;
    8029          433 :   gfc_typebound_proc* tb;
    8030          433 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8031              : 
    8032              :   /* Parse interface (with brackets).  */
    8033          433 :   m = match_procedure_interface (&proc_if);
    8034          433 :   if (m != MATCH_YES)
    8035            1 :     goto syntax;
    8036              : 
    8037              :   /* Parse attributes.  */
    8038          432 :   tb = XCNEW (gfc_typebound_proc);
    8039          432 :   tb->where = gfc_current_locus;
    8040          432 :   m = match_binding_attributes (tb, false, true);
    8041          432 :   if (m == MATCH_ERROR)
    8042              :     return m;
    8043              : 
    8044          429 :   gfc_clear_attr (&current_attr);
    8045          429 :   current_attr.procedure = 1;
    8046          429 :   current_attr.proc_pointer = 1;
    8047          429 :   current_attr.access = tb->access;
    8048          429 :   current_attr.flavor = FL_PROCEDURE;
    8049              : 
    8050              :   /* Match the colons (required).  */
    8051          429 :   if (gfc_match (" ::") != MATCH_YES)
    8052              :     {
    8053            1 :       gfc_error ("Expected %<::%> after binding-attributes at %C");
    8054            1 :       return MATCH_ERROR;
    8055              :     }
    8056              : 
    8057              :   /* Check for C450.  */
    8058          428 :   if (!tb->nopass && proc_if == NULL)
    8059              :     {
    8060            2 :       gfc_error("NOPASS or explicit interface required at %C");
    8061            2 :       return MATCH_ERROR;
    8062              :     }
    8063              : 
    8064          426 :   if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
    8065              :     return MATCH_ERROR;
    8066              : 
    8067              :   /* Match PPC names.  */
    8068          425 :   ts = current_ts;
    8069          425 :   for(num=1;;num++)
    8070              :     {
    8071          426 :       m = gfc_match_name (name);
    8072          426 :       if (m == MATCH_NO)
    8073            0 :         goto syntax;
    8074          426 :       else if (m == MATCH_ERROR)
    8075              :         return m;
    8076              : 
    8077          426 :       if (!gfc_add_component (gfc_current_block(), name, &c))
    8078              :         return MATCH_ERROR;
    8079              : 
    8080              :       /* Add current_attr to the symbol attributes.  */
    8081          426 :       if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
    8082              :         return MATCH_ERROR;
    8083              : 
    8084          426 :       if (!gfc_add_external (&c->attr, NULL))
    8085              :         return MATCH_ERROR;
    8086              : 
    8087          426 :       if (!gfc_add_proc (&c->attr, name, NULL))
    8088              :         return MATCH_ERROR;
    8089              : 
    8090          426 :       if (num == 1)
    8091          425 :         c->tb = tb;
    8092              :       else
    8093              :         {
    8094            1 :           c->tb = XCNEW (gfc_typebound_proc);
    8095            1 :           c->tb->where = gfc_current_locus;
    8096            1 :           *c->tb = *tb;
    8097              :         }
    8098              : 
    8099          426 :       if (saved_kind_expr)
    8100            0 :         c->kind_expr = gfc_copy_expr (saved_kind_expr);
    8101              : 
    8102              :       /* Set interface.  */
    8103          426 :       if (proc_if != NULL)
    8104              :         {
    8105          359 :           c->ts.interface = proc_if;
    8106          359 :           c->attr.untyped = 1;
    8107          359 :           c->attr.if_source = IFSRC_IFBODY;
    8108              :         }
    8109           67 :       else if (ts.type != BT_UNKNOWN)
    8110              :         {
    8111           29 :           c->ts = ts;
    8112           29 :           c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    8113           29 :           c->ts.interface->result = c->ts.interface;
    8114           29 :           c->ts.interface->ts = ts;
    8115           29 :           c->ts.interface->attr.flavor = FL_PROCEDURE;
    8116           29 :           c->ts.interface->attr.function = 1;
    8117           29 :           c->attr.function = 1;
    8118           29 :           c->attr.if_source = IFSRC_UNKNOWN;
    8119              :         }
    8120              : 
    8121          426 :       if (gfc_match (" =>") == MATCH_YES)
    8122              :         {
    8123           73 :           m = match_pointer_init (&initializer, 1);
    8124           73 :           if (m != MATCH_YES)
    8125              :             {
    8126            0 :               gfc_free_expr (initializer);
    8127            0 :               return m;
    8128              :             }
    8129           73 :           c->initializer = initializer;
    8130              :         }
    8131              : 
    8132          426 :       if (gfc_match_eos () == MATCH_YES)
    8133              :         return MATCH_YES;
    8134            1 :       if (gfc_match_char (',') != MATCH_YES)
    8135            0 :         goto syntax;
    8136              :     }
    8137              : 
    8138            1 : syntax:
    8139            1 :   gfc_error ("Syntax error in procedure pointer component at %C");
    8140            1 :   return MATCH_ERROR;
    8141              : }
    8142              : 
    8143              : 
    8144              : /* Match a PROCEDURE declaration inside an interface (R1206).  */
    8145              : 
    8146              : static match
    8147         1561 : match_procedure_in_interface (void)
    8148              : {
    8149         1561 :   match m;
    8150         1561 :   gfc_symbol *sym;
    8151         1561 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8152         1561 :   locus old_locus;
    8153              : 
    8154         1561 :   if (current_interface.type == INTERFACE_NAMELESS
    8155         1561 :       || current_interface.type == INTERFACE_ABSTRACT)
    8156              :     {
    8157            1 :       gfc_error ("PROCEDURE at %C must be in a generic interface");
    8158            1 :       return MATCH_ERROR;
    8159              :     }
    8160              : 
    8161              :   /* Check if the F2008 optional double colon appears.  */
    8162         1560 :   gfc_gobble_whitespace ();
    8163         1560 :   old_locus = gfc_current_locus;
    8164         1560 :   if (gfc_match ("::") == MATCH_YES)
    8165              :     {
    8166          875 :       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
    8167              :                            "MODULE PROCEDURE statement at %L", &old_locus))
    8168              :         return MATCH_ERROR;
    8169              :     }
    8170              :   else
    8171          685 :     gfc_current_locus = old_locus;
    8172              : 
    8173         2214 :   for(;;)
    8174              :     {
    8175         2214 :       m = gfc_match_name (name);
    8176         2214 :       if (m == MATCH_NO)
    8177            0 :         goto syntax;
    8178         2214 :       else if (m == MATCH_ERROR)
    8179              :         return m;
    8180         2214 :       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
    8181              :         return MATCH_ERROR;
    8182              : 
    8183         2214 :       if (!gfc_add_interface (sym))
    8184              :         return MATCH_ERROR;
    8185              : 
    8186         2213 :       if (gfc_match_eos () == MATCH_YES)
    8187              :         break;
    8188          655 :       if (gfc_match_char (',') != MATCH_YES)
    8189            0 :         goto syntax;
    8190              :     }
    8191              : 
    8192              :   return MATCH_YES;
    8193              : 
    8194            0 : syntax:
    8195            0 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
    8196            0 :   return MATCH_ERROR;
    8197              : }
    8198              : 
    8199              : 
    8200              : /* General matcher for PROCEDURE declarations.  */
    8201              : 
    8202              : static match match_procedure_in_type (void);
    8203              : 
    8204              : match
    8205         6433 : gfc_match_procedure (void)
    8206              : {
    8207         6433 :   match m;
    8208              : 
    8209         6433 :   switch (gfc_current_state ())
    8210              :     {
    8211         1197 :     case COMP_NONE:
    8212         1197 :     case COMP_PROGRAM:
    8213         1197 :     case COMP_MODULE:
    8214         1197 :     case COMP_SUBMODULE:
    8215         1197 :     case COMP_SUBROUTINE:
    8216         1197 :     case COMP_FUNCTION:
    8217         1197 :     case COMP_BLOCK:
    8218         1197 :       m = match_procedure_decl ();
    8219         1197 :       break;
    8220         1561 :     case COMP_INTERFACE:
    8221         1561 :       m = match_procedure_in_interface ();
    8222         1561 :       break;
    8223          433 :     case COMP_DERIVED:
    8224          433 :       m = match_ppc_decl ();
    8225          433 :       break;
    8226         3242 :     case COMP_DERIVED_CONTAINS:
    8227         3242 :       m = match_procedure_in_type ();
    8228         3242 :       break;
    8229              :     default:
    8230              :       return MATCH_NO;
    8231              :     }
    8232              : 
    8233         6433 :   if (m != MATCH_YES)
    8234              :     return m;
    8235              : 
    8236         6376 :   if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
    8237            4 :     return MATCH_ERROR;
    8238              : 
    8239              :   return m;
    8240              : }
    8241              : 
    8242              : 
    8243              : /* Warn if a matched procedure has the same name as an intrinsic; this is
    8244              :    simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
    8245              :    parser-state-stack to find out whether we're in a module.  */
    8246              : 
    8247              : static void
    8248        63741 : do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
    8249              : {
    8250        63741 :   bool in_module;
    8251              : 
    8252       127482 :   in_module = (gfc_state_stack->previous
    8253        63741 :                && (gfc_state_stack->previous->state == COMP_MODULE
    8254        51962 :                    || gfc_state_stack->previous->state == COMP_SUBMODULE));
    8255              : 
    8256        63741 :   gfc_warn_intrinsic_shadow (sym, in_module, func);
    8257        63741 : }
    8258              : 
    8259              : 
    8260              : /* Match a function declaration.  */
    8261              : 
    8262              : match
    8263       130404 : gfc_match_function_decl (void)
    8264              : {
    8265       130404 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8266       130404 :   gfc_symbol *sym, *result;
    8267       130404 :   locus old_loc;
    8268       130404 :   match m;
    8269       130404 :   match suffix_match;
    8270       130404 :   match found_match; /* Status returned by match func.  */
    8271              : 
    8272       130404 :   if (gfc_current_state () != COMP_NONE
    8273        82292 :       && gfc_current_state () != COMP_INTERFACE
    8274        52871 :       && gfc_current_state () != COMP_CONTAINS)
    8275              :     return MATCH_NO;
    8276              : 
    8277       130404 :   gfc_clear_ts (&current_ts);
    8278              : 
    8279       130404 :   old_loc = gfc_current_locus;
    8280              : 
    8281       130404 :   m = gfc_match_prefix (&current_ts);
    8282       130404 :   if (m != MATCH_YES)
    8283              :     {
    8284        10017 :       gfc_current_locus = old_loc;
    8285        10017 :       return m;
    8286              :     }
    8287              : 
    8288       120387 :   if (gfc_match ("function% %n", name) != MATCH_YES)
    8289              :     {
    8290       100234 :       gfc_current_locus = old_loc;
    8291       100234 :       return MATCH_NO;
    8292              :     }
    8293              : 
    8294        20153 :   if (get_proc_name (name, &sym, false))
    8295              :     return MATCH_ERROR;
    8296              : 
    8297        20148 :   if (add_hidden_procptr_result (sym))
    8298           20 :     sym = sym->result;
    8299              : 
    8300        20148 :   if (current_attr.module_procedure)
    8301              :     {
    8302          304 :       sym->attr.module_procedure = 1;
    8303          304 :       if (gfc_current_state () == COMP_INTERFACE)
    8304          215 :         gfc_current_ns->has_import_set = 1;
    8305              :     }
    8306              : 
    8307        20148 :   gfc_new_block = sym;
    8308              : 
    8309        20148 :   m = gfc_match_formal_arglist (sym, 0, 0);
    8310        20148 :   if (m == MATCH_NO)
    8311              :     {
    8312            6 :       gfc_error ("Expected formal argument list in function "
    8313              :                  "definition at %C");
    8314            6 :       m = MATCH_ERROR;
    8315            6 :       goto cleanup;
    8316              :     }
    8317        20142 :   else if (m == MATCH_ERROR)
    8318            0 :     goto cleanup;
    8319              : 
    8320        20142 :   result = NULL;
    8321              : 
    8322              :   /* According to the draft, the bind(c) and result clause can
    8323              :      come in either order after the formal_arg_list (i.e., either
    8324              :      can be first, both can exist together or by themselves or neither
    8325              :      one).  Therefore, the match_result can't match the end of the
    8326              :      string, and check for the bind(c) or result clause in either order.  */
    8327        20142 :   found_match = gfc_match_eos ();
    8328              : 
    8329              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8330              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8331              :      not allowed for procedures.  */
    8332        20142 :   if (sym->attr.is_bind_c == 1)
    8333              :     {
    8334            3 :       sym->attr.is_bind_c = 0;
    8335              : 
    8336            3 :       if (gfc_state_stack->previous
    8337            3 :           && gfc_state_stack->previous->state != COMP_SUBMODULE)
    8338              :         {
    8339            1 :           locus loc;
    8340            1 :           loc = sym->old_symbol != NULL
    8341            1 :             ? sym->old_symbol->declared_at : gfc_current_locus;
    8342            1 :           gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8343              :                          "variables or common blocks", &loc);
    8344              :         }
    8345              :     }
    8346              : 
    8347        20142 :   if (found_match != MATCH_YES)
    8348              :     {
    8349              :       /* If we haven't found the end-of-statement, look for a suffix.  */
    8350         8375 :       suffix_match = gfc_match_suffix (sym, &result);
    8351         8375 :       if (suffix_match == MATCH_YES)
    8352              :         /* Need to get the eos now.  */
    8353         8367 :         found_match = gfc_match_eos ();
    8354              :       else
    8355              :         found_match = suffix_match;
    8356              :     }
    8357              : 
    8358              :   /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
    8359              :      subprogram and a binding label is specified, it shall be the
    8360              :      same as the binding label specified in the corresponding module
    8361              :      procedure interface body.  */
    8362        20142 :     if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
    8363            3 :         && strcmp (sym->name, sym->old_symbol->name) == 0
    8364            3 :         && sym->binding_label && sym->old_symbol->binding_label
    8365            2 :         && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
    8366              :       {
    8367            1 :           const char *null = "NULL", *s1, *s2;
    8368            1 :           s1 = sym->binding_label;
    8369            1 :           if (!s1) s1 = null;
    8370            1 :           s2 = sym->old_symbol->binding_label;
    8371            1 :           if (!s2) s2 = null;
    8372            1 :           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
    8373            1 :           sym->refs++;       /* Needed to avoid an ICE in gfc_release_symbol */
    8374            1 :           return MATCH_ERROR;
    8375              :       }
    8376              : 
    8377        20141 :   if(found_match != MATCH_YES)
    8378              :     m = MATCH_ERROR;
    8379              :   else
    8380              :     {
    8381              :       /* Make changes to the symbol.  */
    8382        20133 :       m = MATCH_ERROR;
    8383              : 
    8384        20133 :       if (!gfc_add_function (&sym->attr, sym->name, NULL))
    8385            0 :         goto cleanup;
    8386              : 
    8387        20133 :       if (!gfc_missing_attr (&sym->attr, NULL))
    8388            0 :         goto cleanup;
    8389              : 
    8390        20133 :       if (!copy_prefix (&sym->attr, &sym->declared_at))
    8391              :         {
    8392            1 :           if(!sym->attr.module_procedure)
    8393            1 :         goto cleanup;
    8394              :           else
    8395            0 :             gfc_error_check ();
    8396              :         }
    8397              : 
    8398              :       /* Delay matching the function characteristics until after the
    8399              :          specification block by signalling kind=-1.  */
    8400        20132 :       sym->declared_at = old_loc;
    8401        20132 :       if (current_ts.type != BT_UNKNOWN)
    8402              :         current_ts.kind = -1;
    8403              :       else
    8404        13196 :         current_ts.kind = 0;
    8405              : 
    8406        20132 :       if (result == NULL)
    8407              :         {
    8408        14281 :           if (current_ts.type != BT_UNKNOWN
    8409        14281 :               && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
    8410            1 :             goto cleanup;
    8411        14280 :           sym->result = sym;
    8412              :         }
    8413              :       else
    8414              :         {
    8415         5851 :           if (current_ts.type != BT_UNKNOWN
    8416         5851 :               && !gfc_add_type (result, &current_ts, &gfc_current_locus))
    8417            0 :             goto cleanup;
    8418         5851 :           sym->result = result;
    8419              :         }
    8420              : 
    8421              :       /* Warn if this procedure has the same name as an intrinsic.  */
    8422        20131 :       do_warn_intrinsic_shadow (sym, true);
    8423              : 
    8424        20131 :       return MATCH_YES;
    8425              :     }
    8426              : 
    8427           16 : cleanup:
    8428           16 :   gfc_current_locus = old_loc;
    8429           16 :   return m;
    8430              : }
    8431              : 
    8432              : 
    8433              : /* This is mostly a copy of parse.cc(add_global_procedure) but modified to
    8434              :    pass the name of the entry, rather than the gfc_current_block name, and
    8435              :    to return false upon finding an existing global entry.  */
    8436              : 
    8437              : static bool
    8438          539 : add_global_entry (const char *name, const char *binding_label, bool sub,
    8439              :                   locus *where)
    8440              : {
    8441          539 :   gfc_gsymbol *s;
    8442          539 :   enum gfc_symbol_type type;
    8443              : 
    8444          539 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    8445              : 
    8446              :   /* Only in Fortran 2003: For procedures with a binding label also the Fortran
    8447              :      name is a global identifier.  */
    8448          539 :   if (!binding_label || gfc_notification_std (GFC_STD_F2008))
    8449              :     {
    8450          516 :       s = gfc_get_gsymbol (name, false);
    8451              : 
    8452          516 :       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
    8453              :         {
    8454            2 :           gfc_global_used (s, where);
    8455            2 :           return false;
    8456              :         }
    8457              :       else
    8458              :         {
    8459          514 :           s->type = type;
    8460          514 :           s->sym_name = name;
    8461          514 :           s->where = *where;
    8462          514 :           s->defined = 1;
    8463          514 :           s->ns = gfc_current_ns;
    8464              :         }
    8465              :     }
    8466              : 
    8467              :   /* Don't add the symbol multiple times.  */
    8468          537 :   if (binding_label
    8469          537 :       && (!gfc_notification_std (GFC_STD_F2008)
    8470            0 :           || strcmp (name, binding_label) != 0))
    8471              :     {
    8472           23 :       s = gfc_get_gsymbol (binding_label, true);
    8473              : 
    8474           23 :       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
    8475              :         {
    8476            1 :           gfc_global_used (s, where);
    8477            1 :           return false;
    8478              :         }
    8479              :       else
    8480              :         {
    8481           22 :           s->type = type;
    8482           22 :           s->sym_name = gfc_get_string ("%s", name);
    8483           22 :           s->binding_label = binding_label;
    8484           22 :           s->where = *where;
    8485           22 :           s->defined = 1;
    8486           22 :           s->ns = gfc_current_ns;
    8487              :         }
    8488              :     }
    8489              : 
    8490              :   return true;
    8491              : }
    8492              : 
    8493              : 
    8494              : /* Match an ENTRY statement.  */
    8495              : 
    8496              : match
    8497          805 : gfc_match_entry (void)
    8498              : {
    8499          805 :   gfc_symbol *proc;
    8500          805 :   gfc_symbol *result;
    8501          805 :   gfc_symbol *entry;
    8502          805 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8503          805 :   gfc_compile_state state;
    8504          805 :   match m;
    8505          805 :   gfc_entry_list *el;
    8506          805 :   locus old_loc;
    8507          805 :   bool module_procedure;
    8508          805 :   char peek_char;
    8509          805 :   match is_bind_c;
    8510              : 
    8511          805 :   m = gfc_match_name (name);
    8512          805 :   if (m != MATCH_YES)
    8513              :     return m;
    8514              : 
    8515          805 :   if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
    8516              :     return MATCH_ERROR;
    8517              : 
    8518          805 :   state = gfc_current_state ();
    8519          805 :   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
    8520              :     {
    8521            3 :       switch (state)
    8522              :         {
    8523            0 :           case COMP_PROGRAM:
    8524            0 :             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
    8525            0 :             break;
    8526            0 :           case COMP_MODULE:
    8527            0 :             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
    8528            0 :             break;
    8529            0 :           case COMP_SUBMODULE:
    8530            0 :             gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
    8531            0 :             break;
    8532            0 :           case COMP_BLOCK_DATA:
    8533            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8534              :                        "a BLOCK DATA");
    8535            0 :             break;
    8536            0 :           case COMP_INTERFACE:
    8537            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8538              :                        "an INTERFACE");
    8539            0 :             break;
    8540            1 :           case COMP_STRUCTURE:
    8541            1 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8542              :                        "a STRUCTURE block");
    8543            1 :             break;
    8544            0 :           case COMP_DERIVED:
    8545            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8546              :                        "a DERIVED TYPE block");
    8547            0 :             break;
    8548            0 :           case COMP_IF:
    8549            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8550              :                        "an IF-THEN block");
    8551            0 :             break;
    8552            0 :           case COMP_DO:
    8553            0 :           case COMP_DO_CONCURRENT:
    8554            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8555              :                        "a DO block");
    8556            0 :             break;
    8557            0 :           case COMP_SELECT:
    8558            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8559              :                        "a SELECT block");
    8560            0 :             break;
    8561            0 :           case COMP_FORALL:
    8562            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8563              :                        "a FORALL block");
    8564            0 :             break;
    8565            0 :           case COMP_WHERE:
    8566            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8567              :                        "a WHERE block");
    8568            0 :             break;
    8569            0 :           case COMP_CONTAINS:
    8570            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8571              :                        "a contained subprogram");
    8572            0 :             break;
    8573            2 :           default:
    8574            2 :             gfc_error ("Unexpected ENTRY statement at %C");
    8575              :         }
    8576              :       return MATCH_ERROR;
    8577              :     }
    8578              : 
    8579          802 :   if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
    8580          802 :       && gfc_state_stack->previous->state == COMP_INTERFACE)
    8581              :     {
    8582            1 :       gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
    8583            1 :       return MATCH_ERROR;
    8584              :     }
    8585              : 
    8586         1602 :   module_procedure = gfc_current_ns->parent != NULL
    8587          260 :                    && gfc_current_ns->parent->proc_name
    8588          801 :                    && gfc_current_ns->parent->proc_name->attr.flavor
    8589          260 :                       == FL_MODULE;
    8590              : 
    8591          801 :   if (gfc_current_ns->parent != NULL
    8592          260 :       && gfc_current_ns->parent->proc_name
    8593          260 :       && !module_procedure)
    8594              :     {
    8595            0 :       gfc_error("ENTRY statement at %C cannot appear in a "
    8596              :                 "contained procedure");
    8597            0 :       return MATCH_ERROR;
    8598              :     }
    8599              : 
    8600              :   /* Module function entries need special care in get_proc_name
    8601              :      because previous references within the function will have
    8602              :      created symbols attached to the current namespace.  */
    8603         1342 :   if (get_proc_name (name, &entry,
    8604              :                      gfc_current_ns->parent != NULL
    8605              :                      && module_procedure))
    8606              :     return MATCH_ERROR;
    8607              : 
    8608          799 :   proc = gfc_current_block ();
    8609              : 
    8610              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8611              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8612              :      not allowed for procedures.  */
    8613          799 :   if (entry->attr.is_bind_c == 1)
    8614              :     {
    8615            0 :       locus loc;
    8616              : 
    8617            0 :       entry->attr.is_bind_c = 0;
    8618              : 
    8619            0 :       loc = entry->old_symbol != NULL
    8620            0 :         ? entry->old_symbol->declared_at : gfc_current_locus;
    8621            0 :       gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8622              :                      "variables or common blocks", &loc);
    8623              :      }
    8624              : 
    8625              :   /* Check what next non-whitespace character is so we can tell if there
    8626              :      is the required parens if we have a BIND(C).  */
    8627          799 :   old_loc = gfc_current_locus;
    8628          799 :   gfc_gobble_whitespace ();
    8629          799 :   peek_char = gfc_peek_ascii_char ();
    8630              : 
    8631          799 :   if (state == COMP_SUBROUTINE)
    8632              :     {
    8633          138 :       m = gfc_match_formal_arglist (entry, 0, 1);
    8634          138 :       if (m != MATCH_YES)
    8635              :         return MATCH_ERROR;
    8636              : 
    8637              :       /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
    8638              :          never be an internal procedure.  */
    8639          138 :       is_bind_c = gfc_match_bind_c (entry, true);
    8640          138 :       if (is_bind_c == MATCH_ERROR)
    8641              :         return MATCH_ERROR;
    8642          138 :       if (is_bind_c == MATCH_YES)
    8643              :         {
    8644           22 :           if (peek_char != '(')
    8645              :             {
    8646            0 :               gfc_error ("Missing required parentheses before BIND(C) at %C");
    8647            0 :               return MATCH_ERROR;
    8648              :             }
    8649              : 
    8650           22 :           if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
    8651           22 :                                   &(entry->declared_at), 1))
    8652              :             return MATCH_ERROR;
    8653              : 
    8654              :         }
    8655              : 
    8656          138 :       if (!gfc_current_ns->parent
    8657          138 :           && !add_global_entry (name, entry->binding_label, true,
    8658              :                                 &old_loc))
    8659              :         return MATCH_ERROR;
    8660              : 
    8661              :       /* An entry in a subroutine.  */
    8662          135 :       if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8663          135 :           || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
    8664              :         return MATCH_ERROR;
    8665              :     }
    8666              :   else
    8667              :     {
    8668              :       /* An entry in a function.
    8669              :          We need to take special care because writing
    8670              :             ENTRY f()
    8671              :          as
    8672              :             ENTRY f
    8673              :          is allowed, whereas
    8674              :             ENTRY f() RESULT (r)
    8675              :          can't be written as
    8676              :             ENTRY f RESULT (r).  */
    8677          661 :       if (gfc_match_eos () == MATCH_YES)
    8678              :         {
    8679           24 :           gfc_current_locus = old_loc;
    8680              :           /* Match the empty argument list, and add the interface to
    8681              :              the symbol.  */
    8682           24 :           m = gfc_match_formal_arglist (entry, 0, 1);
    8683              :         }
    8684              :       else
    8685          637 :         m = gfc_match_formal_arglist (entry, 0, 0);
    8686              : 
    8687          661 :       if (m != MATCH_YES)
    8688              :         return MATCH_ERROR;
    8689              : 
    8690          660 :       result = NULL;
    8691              : 
    8692          660 :       if (gfc_match_eos () == MATCH_YES)
    8693              :         {
    8694          411 :           if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8695          411 :               || !gfc_add_function (&entry->attr, entry->name, NULL))
    8696              :             return MATCH_ERROR;
    8697              : 
    8698          409 :           entry->result = entry;
    8699              :         }
    8700              :       else
    8701              :         {
    8702          249 :           m = gfc_match_suffix (entry, &result);
    8703          249 :           if (m == MATCH_NO)
    8704            0 :             gfc_syntax_error (ST_ENTRY);
    8705          249 :           if (m != MATCH_YES)
    8706              :             return MATCH_ERROR;
    8707              : 
    8708          249 :           if (result)
    8709              :             {
    8710          212 :               if (!gfc_add_result (&result->attr, result->name, NULL)
    8711          212 :                   || !gfc_add_entry (&entry->attr, result->name, NULL)
    8712          424 :                   || !gfc_add_function (&entry->attr, result->name, NULL))
    8713              :                 return MATCH_ERROR;
    8714          212 :               entry->result = result;
    8715              :             }
    8716              :           else
    8717              :             {
    8718           37 :               if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8719           37 :                   || !gfc_add_function (&entry->attr, entry->name, NULL))
    8720              :                 return MATCH_ERROR;
    8721           37 :               entry->result = entry;
    8722              :             }
    8723              :         }
    8724              : 
    8725          658 :       if (!gfc_current_ns->parent
    8726          658 :           && !add_global_entry (name, entry->binding_label, false,
    8727              :                                 &old_loc))
    8728              :         return MATCH_ERROR;
    8729              :     }
    8730              : 
    8731          790 :   if (gfc_match_eos () != MATCH_YES)
    8732              :     {
    8733            0 :       gfc_syntax_error (ST_ENTRY);
    8734            0 :       return MATCH_ERROR;
    8735              :     }
    8736              : 
    8737              :   /* F2018:C1546 An elemental procedure shall not have the BIND attribute.  */
    8738          790 :   if (proc->attr.elemental && entry->attr.is_bind_c)
    8739              :     {
    8740            2 :       gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
    8741              :                  "elemental procedure", &entry->declared_at);
    8742            2 :       return MATCH_ERROR;
    8743              :     }
    8744              : 
    8745          788 :   entry->attr.recursive = proc->attr.recursive;
    8746          788 :   entry->attr.elemental = proc->attr.elemental;
    8747          788 :   entry->attr.pure = proc->attr.pure;
    8748              : 
    8749          788 :   el = gfc_get_entry_list ();
    8750          788 :   el->sym = entry;
    8751          788 :   el->next = gfc_current_ns->entries;
    8752          788 :   gfc_current_ns->entries = el;
    8753          788 :   if (el->next)
    8754           85 :     el->id = el->next->id + 1;
    8755              :   else
    8756              :     el->id = 1;
    8757              : 
    8758          788 :   new_st.op = EXEC_ENTRY;
    8759          788 :   new_st.ext.entry = el;
    8760              : 
    8761          788 :   return MATCH_YES;
    8762              : }
    8763              : 
    8764              : 
    8765              : /* Match a subroutine statement, including optional prefixes.  */
    8766              : 
    8767              : match
    8768       814161 : gfc_match_subroutine (void)
    8769              : {
    8770       814161 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8771       814161 :   gfc_symbol *sym;
    8772       814161 :   match m;
    8773       814161 :   match is_bind_c;
    8774       814161 :   char peek_char;
    8775       814161 :   bool allow_binding_name;
    8776       814161 :   locus loc;
    8777              : 
    8778       814161 :   if (gfc_current_state () != COMP_NONE
    8779       771949 :       && gfc_current_state () != COMP_INTERFACE
    8780       749108 :       && gfc_current_state () != COMP_CONTAINS)
    8781              :     return MATCH_NO;
    8782              : 
    8783       107345 :   m = gfc_match_prefix (NULL);
    8784       107345 :   if (m != MATCH_YES)
    8785              :     return m;
    8786              : 
    8787        97338 :   loc = gfc_current_locus;
    8788        97338 :   m = gfc_match ("subroutine% %n", name);
    8789        97338 :   if (m != MATCH_YES)
    8790              :     return m;
    8791              : 
    8792        43646 :   if (get_proc_name (name, &sym, false))
    8793              :     return MATCH_ERROR;
    8794              : 
    8795              :   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
    8796              :      the symbol existed before.  */
    8797        43635 :   sym->declared_at = gfc_get_location_range (NULL, 0, &loc, 1,
    8798              :                                              &gfc_current_locus);
    8799              : 
    8800        43635 :   if (current_attr.module_procedure)
    8801              :     {
    8802          429 :       sym->attr.module_procedure = 1;
    8803          429 :       if (gfc_current_state () == COMP_INTERFACE)
    8804          302 :         gfc_current_ns->has_import_set = 1;
    8805              :     }
    8806              : 
    8807        43635 :   if (add_hidden_procptr_result (sym))
    8808            9 :     sym = sym->result;
    8809              : 
    8810        43635 :   gfc_new_block = sym;
    8811              : 
    8812              :   /* Check what next non-whitespace character is so we can tell if there
    8813              :      is the required parens if we have a BIND(C).  */
    8814        43635 :   gfc_gobble_whitespace ();
    8815        43635 :   peek_char = gfc_peek_ascii_char ();
    8816              : 
    8817        43635 :   if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
    8818              :     return MATCH_ERROR;
    8819              : 
    8820        43632 :   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
    8821              :     return MATCH_ERROR;
    8822              : 
    8823              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8824              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8825              :      not allowed for procedures.  */
    8826        43632 :   if (sym->attr.is_bind_c == 1)
    8827              :     {
    8828            4 :       sym->attr.is_bind_c = 0;
    8829              : 
    8830            4 :       if (gfc_state_stack->previous
    8831            4 :           && gfc_state_stack->previous->state != COMP_SUBMODULE)
    8832              :         {
    8833            2 :           locus loc;
    8834            2 :           loc = sym->old_symbol != NULL
    8835            2 :             ? sym->old_symbol->declared_at : gfc_current_locus;
    8836            2 :           gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8837              :                          "variables or common blocks", &loc);
    8838              :         }
    8839              :     }
    8840              : 
    8841              :   /* C binding names are not allowed for internal procedures.  */
    8842        43632 :   if (gfc_current_state () == COMP_CONTAINS
    8843        26536 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    8844              :     allow_binding_name = false;
    8845              :   else
    8846        28492 :     allow_binding_name = true;
    8847              : 
    8848              :   /* Here, we are just checking if it has the bind(c) attribute, and if
    8849              :      so, then we need to make sure it's all correct.  If it doesn't,
    8850              :      we still need to continue matching the rest of the subroutine line.  */
    8851        43632 :   gfc_gobble_whitespace ();
    8852        43632 :   loc = gfc_current_locus;
    8853        43632 :   is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    8854        43632 :   if (is_bind_c == MATCH_ERROR)
    8855              :     {
    8856              :       /* There was an attempt at the bind(c), but it was wrong.  An
    8857              :          error message should have been printed w/in the gfc_match_bind_c
    8858              :          so here we'll just return the MATCH_ERROR.  */
    8859              :       return MATCH_ERROR;
    8860              :     }
    8861              : 
    8862        43619 :   if (is_bind_c == MATCH_YES)
    8863              :     {
    8864         4053 :       gfc_formal_arglist *arg;
    8865              : 
    8866              :       /* The following is allowed in the Fortran 2008 draft.  */
    8867         4053 :       if (gfc_current_state () == COMP_CONTAINS
    8868         1299 :           && sym->ns->proc_name->attr.flavor != FL_MODULE
    8869         4464 :           && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
    8870              :                               "at %L may not be specified for an internal "
    8871              :                               "procedure", &gfc_current_locus))
    8872              :         return MATCH_ERROR;
    8873              : 
    8874         4050 :       if (peek_char != '(')
    8875              :         {
    8876            1 :           gfc_error ("Missing required parentheses before BIND(C) at %C");
    8877            1 :           return MATCH_ERROR;
    8878              :         }
    8879              : 
    8880              :       /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
    8881              :          subprogram and a binding label is specified, it shall be the
    8882              :          same as the binding label specified in the corresponding module
    8883              :          procedure interface body.  */
    8884         4049 :       if (sym->attr.module_procedure && sym->old_symbol
    8885            3 :           && strcmp (sym->name, sym->old_symbol->name) == 0
    8886            3 :           && sym->binding_label && sym->old_symbol->binding_label
    8887            2 :           && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
    8888              :         {
    8889            1 :           const char *null = "NULL", *s1, *s2;
    8890            1 :           s1 = sym->binding_label;
    8891            1 :           if (!s1) s1 = null;
    8892            1 :           s2 = sym->old_symbol->binding_label;
    8893            1 :           if (!s2) s2 = null;
    8894            1 :           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
    8895            1 :           sym->refs++;       /* Needed to avoid an ICE in gfc_release_symbol */
    8896            1 :           return MATCH_ERROR;
    8897              :         }
    8898              : 
    8899              :       /* Scan the dummy arguments for an alternate return.  */
    8900        12535 :       for (arg = sym->formal; arg; arg = arg->next)
    8901         8488 :         if (!arg->sym)
    8902              :           {
    8903            1 :             gfc_error ("Alternate return dummy argument cannot appear in a "
    8904              :                        "SUBROUTINE with the BIND(C) attribute at %L", &loc);
    8905            1 :             return MATCH_ERROR;
    8906              :           }
    8907              : 
    8908         4047 :       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
    8909              :         return MATCH_ERROR;
    8910              :     }
    8911              : 
    8912        43612 :   if (gfc_match_eos () != MATCH_YES)
    8913              :     {
    8914            1 :       gfc_syntax_error (ST_SUBROUTINE);
    8915            1 :       return MATCH_ERROR;
    8916              :     }
    8917              : 
    8918        43611 :   if (!copy_prefix (&sym->attr, &sym->declared_at))
    8919              :     {
    8920            4 :       if(!sym->attr.module_procedure)
    8921              :         return MATCH_ERROR;
    8922              :       else
    8923            3 :         gfc_error_check ();
    8924              :     }
    8925              : 
    8926              :   /* Warn if it has the same name as an intrinsic.  */
    8927        43610 :   do_warn_intrinsic_shadow (sym, false);
    8928              : 
    8929        43610 :   return MATCH_YES;
    8930              : }
    8931              : 
    8932              : 
    8933              : /* Check that the NAME identifier in a BIND attribute or statement
    8934              :    is conform to C identifier rules.  */
    8935              : 
    8936              : match
    8937         1187 : check_bind_name_identifier (char **name)
    8938              : {
    8939         1187 :   char *n = *name, *p;
    8940              : 
    8941              :   /* Remove leading spaces.  */
    8942         1213 :   while (*n == ' ')
    8943           26 :     n++;
    8944              : 
    8945              :   /* On an empty string, free memory and set name to NULL.  */
    8946         1187 :   if (*n == '\0')
    8947              :     {
    8948           42 :       free (*name);
    8949           42 :       *name = NULL;
    8950           42 :       return MATCH_YES;
    8951              :     }
    8952              : 
    8953              :   /* Remove trailing spaces.  */
    8954         1145 :   p = n + strlen(n) - 1;
    8955         1161 :   while (*p == ' ')
    8956           16 :     *(p--) = '\0';
    8957              : 
    8958              :   /* Insert the identifier into the symbol table.  */
    8959         1145 :   p = xstrdup (n);
    8960         1145 :   free (*name);
    8961         1145 :   *name = p;
    8962              : 
    8963              :   /* Now check that identifier is valid under C rules.  */
    8964         1145 :   if (ISDIGIT (*p))
    8965              :     {
    8966            2 :       gfc_error ("Invalid C identifier in NAME= specifier at %C");
    8967            2 :       return MATCH_ERROR;
    8968              :     }
    8969              : 
    8970        12512 :   for (; *p; p++)
    8971        11372 :     if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
    8972              :       {
    8973            3 :         gfc_error ("Invalid C identifier in NAME= specifier at %C");
    8974            3 :         return MATCH_ERROR;
    8975              :       }
    8976              : 
    8977              :   return MATCH_YES;
    8978              : }
    8979              : 
    8980              : 
    8981              : /* Match a BIND(C) specifier, with the optional 'name=' specifier if
    8982              :    given, and set the binding label in either the given symbol (if not
    8983              :    NULL), or in the current_ts.  The symbol may be NULL because we may
    8984              :    encounter the BIND(C) before the declaration itself.  Return
    8985              :    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
    8986              :    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
    8987              :    or MATCH_YES if the specifier was correct and the binding label and
    8988              :    bind(c) fields were set correctly for the given symbol or the
    8989              :    current_ts. If allow_binding_name is false, no binding name may be
    8990              :    given.  */
    8991              : 
    8992              : match
    8993        52697 : gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
    8994              : {
    8995        52697 :   char *binding_label = NULL;
    8996        52697 :   gfc_expr *e = NULL;
    8997              : 
    8998              :   /* Initialize the flag that specifies whether we encountered a NAME=
    8999              :      specifier or not.  */
    9000        52697 :   has_name_equals = 0;
    9001              : 
    9002              :   /* This much we have to be able to match, in this order, if
    9003              :      there is a bind(c) label.  */
    9004        52697 :   if (gfc_match (" bind ( c ") != MATCH_YES)
    9005              :     return MATCH_NO;
    9006              : 
    9007              :   /* Now see if there is a binding label, or if we've reached the
    9008              :      end of the bind(c) attribute without one.  */
    9009         7458 :   if (gfc_match_char (',') == MATCH_YES)
    9010              :     {
    9011         1194 :       if (gfc_match (" name = ") != MATCH_YES)
    9012              :         {
    9013            1 :           gfc_error ("Syntax error in NAME= specifier for binding label "
    9014              :                      "at %C");
    9015              :           /* should give an error message here */
    9016            1 :           return MATCH_ERROR;
    9017              :         }
    9018              : 
    9019         1193 :       has_name_equals = 1;
    9020              : 
    9021         1193 :       if (gfc_match_init_expr (&e) != MATCH_YES)
    9022              :         {
    9023            2 :           gfc_free_expr (e);
    9024            2 :           return MATCH_ERROR;
    9025              :         }
    9026              : 
    9027         1191 :       if (!gfc_simplify_expr(e, 0))
    9028              :         {
    9029            0 :           gfc_error ("NAME= specifier at %C should be a constant expression");
    9030            0 :           gfc_free_expr (e);
    9031            0 :           return MATCH_ERROR;
    9032              :         }
    9033              : 
    9034         1191 :       if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
    9035         1188 :           || e->ts.kind != gfc_default_character_kind || e->rank != 0)
    9036              :         {
    9037            4 :           gfc_error ("NAME= specifier at %C should be a scalar of "
    9038              :                      "default character kind");
    9039            4 :           gfc_free_expr(e);
    9040            4 :           return MATCH_ERROR;
    9041              :         }
    9042              : 
    9043              :       // Get a C string from the Fortran string constant
    9044         2374 :       binding_label = gfc_widechar_to_char (e->value.character.string,
    9045         1187 :                                             e->value.character.length);
    9046         1187 :       gfc_free_expr(e);
    9047              : 
    9048              :       // Check that it is valid (old gfc_match_name_C)
    9049         1187 :       if (check_bind_name_identifier (&binding_label) != MATCH_YES)
    9050              :         return MATCH_ERROR;
    9051              :     }
    9052              : 
    9053              :   /* Get the required right paren.  */
    9054         7446 :   if (gfc_match_char (')') != MATCH_YES)
    9055              :     {
    9056            1 :       gfc_error ("Missing closing paren for binding label at %C");
    9057            1 :       return MATCH_ERROR;
    9058              :     }
    9059              : 
    9060         7445 :   if (has_name_equals && !allow_binding_name)
    9061              :     {
    9062            6 :       gfc_error ("No binding name is allowed in BIND(C) at %C");
    9063            6 :       return MATCH_ERROR;
    9064              :     }
    9065              : 
    9066         7439 :   if (has_name_equals && sym != NULL && sym->attr.dummy)
    9067              :     {
    9068            2 :       gfc_error ("For dummy procedure %s, no binding name is "
    9069              :                  "allowed in BIND(C) at %C", sym->name);
    9070            2 :       return MATCH_ERROR;
    9071              :     }
    9072              : 
    9073              : 
    9074              :   /* Save the binding label to the symbol.  If sym is null, we're
    9075              :      probably matching the typespec attributes of a declaration and
    9076              :      haven't gotten the name yet, and therefore, no symbol yet.  */
    9077         7437 :   if (binding_label)
    9078              :     {
    9079         1133 :       if (sym != NULL)
    9080         1023 :         sym->binding_label = binding_label;
    9081              :       else
    9082          110 :         curr_binding_label = binding_label;
    9083              :     }
    9084         6304 :   else if (allow_binding_name)
    9085              :     {
    9086              :       /* No binding label, but if symbol isn't null, we
    9087              :          can set the label for it here.
    9088              :          If name="" or allow_binding_name is false, no C binding name is
    9089              :          created.  */
    9090         5875 :       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
    9091         5708 :         sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
    9092              :     }
    9093              : 
    9094         7437 :   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
    9095          741 :       && current_interface.type == INTERFACE_ABSTRACT)
    9096              :     {
    9097            1 :       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
    9098            1 :       return MATCH_ERROR;
    9099              :     }
    9100              : 
    9101              :   return MATCH_YES;
    9102              : }
    9103              : 
    9104              : 
    9105              : /* Return nonzero if we're currently compiling a contained procedure.  */
    9106              : 
    9107              : static int
    9108        64065 : contained_procedure (void)
    9109              : {
    9110        64065 :   gfc_state_data *s = gfc_state_stack;
    9111              : 
    9112        64065 :   if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
    9113        63100 :       && s->previous != NULL && s->previous->state == COMP_CONTAINS)
    9114        37030 :     return 1;
    9115              : 
    9116              :   return 0;
    9117              : }
    9118              : 
    9119              : /* Set the kind of each enumerator.  The kind is selected such that it is
    9120              :    interoperable with the corresponding C enumeration type, making
    9121              :    sure that -fshort-enums is honored.  */
    9122              : 
    9123              : static void
    9124          158 : set_enum_kind(void)
    9125              : {
    9126          158 :   enumerator_history *current_history = NULL;
    9127          158 :   int kind;
    9128          158 :   int i;
    9129              : 
    9130          158 :   if (max_enum == NULL || enum_history == NULL)
    9131              :     return;
    9132              : 
    9133          150 :   if (!flag_short_enums)
    9134              :     return;
    9135              : 
    9136              :   i = 0;
    9137           48 :   do
    9138              :     {
    9139           48 :       kind = gfc_integer_kinds[i++].kind;
    9140              :     }
    9141           48 :   while (kind < gfc_c_int_kind
    9142           72 :          && gfc_check_integer_range (max_enum->initializer->value.integer,
    9143              :                                      kind) != ARITH_OK);
    9144              : 
    9145           24 :   current_history = enum_history;
    9146           96 :   while (current_history != NULL)
    9147              :     {
    9148           72 :       current_history->sym->ts.kind = kind;
    9149           72 :       current_history = current_history->next;
    9150              :     }
    9151              : }
    9152              : 
    9153              : 
    9154              : /* Match any of the various end-block statements.  Returns the type of
    9155              :    END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
    9156              :    and END BLOCK statements cannot be replaced by a single END statement.  */
    9157              : 
    9158              : match
    9159       187927 : gfc_match_end (gfc_statement *st)
    9160              : {
    9161       187927 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    9162       187927 :   gfc_compile_state state;
    9163       187927 :   locus old_loc;
    9164       187927 :   const char *block_name;
    9165       187927 :   const char *target;
    9166       187927 :   int eos_ok;
    9167       187927 :   match m;
    9168       187927 :   gfc_namespace *parent_ns, *ns, *prev_ns;
    9169       187927 :   gfc_namespace **nsp;
    9170       187927 :   bool abbreviated_modproc_decl = false;
    9171       187927 :   bool got_matching_end = false;
    9172              : 
    9173       187927 :   old_loc = gfc_current_locus;
    9174       187927 :   if (gfc_match ("end") != MATCH_YES)
    9175              :     return MATCH_NO;
    9176              : 
    9177       182781 :   state = gfc_current_state ();
    9178       100196 :   block_name = gfc_current_block () == NULL
    9179       182781 :              ? NULL : gfc_current_block ()->name;
    9180              : 
    9181       182781 :   switch (state)
    9182              :     {
    9183         3148 :     case COMP_ASSOCIATE:
    9184         3148 :     case COMP_BLOCK:
    9185         3148 :     case COMP_CHANGE_TEAM:
    9186         3148 :       if (startswith (block_name, "block@"))
    9187              :         block_name = NULL;
    9188              :       break;
    9189              : 
    9190        17789 :     case COMP_CONTAINS:
    9191        17789 :     case COMP_DERIVED_CONTAINS:
    9192        17789 :     case COMP_OMP_BEGIN_METADIRECTIVE:
    9193        17789 :       state = gfc_state_stack->previous->state;
    9194        16223 :       block_name = gfc_state_stack->previous->sym == NULL
    9195        17789 :                  ? NULL : gfc_state_stack->previous->sym->name;
    9196        17789 :       abbreviated_modproc_decl = gfc_state_stack->previous->sym
    9197        17789 :                 && gfc_state_stack->previous->sym->abr_modproc_decl;
    9198              :       break;
    9199              : 
    9200              :     case COMP_OMP_METADIRECTIVE:
    9201              :       {
    9202              :         /* Metadirectives can be nested, so we need to drill down to the
    9203              :            first state that is not COMP_OMP_METADIRECTIVE.  */
    9204              :         gfc_state_data *state_data = gfc_state_stack;
    9205              : 
    9206           85 :         do
    9207              :           {
    9208           85 :             state_data = state_data->previous;
    9209           85 :             state = state_data->state;
    9210           77 :             block_name = (state_data->sym == NULL
    9211           85 :                           ? NULL : state_data->sym->name);
    9212          170 :             abbreviated_modproc_decl = (state_data->sym
    9213           85 :                                         && state_data->sym->abr_modproc_decl);
    9214              :           }
    9215           85 :         while (state == COMP_OMP_METADIRECTIVE);
    9216              : 
    9217           83 :         if (block_name && startswith (block_name, "block@"))
    9218              :           block_name = NULL;
    9219              :       }
    9220              :       break;
    9221              : 
    9222              :     default:
    9223              :       break;
    9224              :     }
    9225              : 
    9226           83 :   if (!abbreviated_modproc_decl)
    9227       182780 :     abbreviated_modproc_decl = gfc_current_block ()
    9228       182780 :                               && gfc_current_block ()->abr_modproc_decl;
    9229              : 
    9230       182781 :   switch (state)
    9231              :     {
    9232        28222 :     case COMP_NONE:
    9233        28222 :     case COMP_PROGRAM:
    9234        28222 :       *st = ST_END_PROGRAM;
    9235        28222 :       target = " program";
    9236        28222 :       eos_ok = 1;
    9237        28222 :       break;
    9238              : 
    9239        43800 :     case COMP_SUBROUTINE:
    9240        43800 :       *st = ST_END_SUBROUTINE;
    9241        43800 :       if (!abbreviated_modproc_decl)
    9242              :         target = " subroutine";
    9243              :       else
    9244          148 :         target = " procedure";
    9245        43800 :       eos_ok = !contained_procedure ();
    9246        43800 :       break;
    9247              : 
    9248        20265 :     case COMP_FUNCTION:
    9249        20265 :       *st = ST_END_FUNCTION;
    9250        20265 :       if (!abbreviated_modproc_decl)
    9251              :         target = " function";
    9252              :       else
    9253          117 :         target = " procedure";
    9254        20265 :       eos_ok = !contained_procedure ();
    9255        20265 :       break;
    9256              : 
    9257           87 :     case COMP_BLOCK_DATA:
    9258           87 :       *st = ST_END_BLOCK_DATA;
    9259           87 :       target = " block data";
    9260           87 :       eos_ok = 1;
    9261           87 :       break;
    9262              : 
    9263         9999 :     case COMP_MODULE:
    9264         9999 :       *st = ST_END_MODULE;
    9265         9999 :       target = " module";
    9266         9999 :       eos_ok = 1;
    9267         9999 :       break;
    9268              : 
    9269          268 :     case COMP_SUBMODULE:
    9270          268 :       *st = ST_END_SUBMODULE;
    9271          268 :       target = " submodule";
    9272          268 :       eos_ok = 1;
    9273          268 :       break;
    9274              : 
    9275        11353 :     case COMP_INTERFACE:
    9276        11353 :       *st = ST_END_INTERFACE;
    9277        11353 :       target = " interface";
    9278        11353 :       eos_ok = 0;
    9279        11353 :       break;
    9280              : 
    9281          257 :     case COMP_MAP:
    9282          257 :       *st = ST_END_MAP;
    9283          257 :       target = " map";
    9284          257 :       eos_ok = 0;
    9285          257 :       break;
    9286              : 
    9287          132 :     case COMP_UNION:
    9288          132 :       *st = ST_END_UNION;
    9289          132 :       target = " union";
    9290          132 :       eos_ok = 0;
    9291          132 :       break;
    9292              : 
    9293          313 :     case COMP_STRUCTURE:
    9294          313 :       *st = ST_END_STRUCTURE;
    9295          313 :       target = " structure";
    9296          313 :       eos_ok = 0;
    9297          313 :       break;
    9298              : 
    9299        13270 :     case COMP_DERIVED:
    9300        13270 :     case COMP_DERIVED_CONTAINS:
    9301        13270 :       *st = ST_END_TYPE;
    9302        13270 :       target = " type";
    9303        13270 :       eos_ok = 0;
    9304        13270 :       break;
    9305              : 
    9306         1579 :     case COMP_ASSOCIATE:
    9307         1579 :       *st = ST_END_ASSOCIATE;
    9308         1579 :       target = " associate";
    9309         1579 :       eos_ok = 0;
    9310         1579 :       break;
    9311              : 
    9312         1501 :     case COMP_BLOCK:
    9313         1501 :     case COMP_OMP_STRICTLY_STRUCTURED_BLOCK:
    9314         1501 :       *st = ST_END_BLOCK;
    9315         1501 :       target = " block";
    9316         1501 :       eos_ok = 0;
    9317         1501 :       break;
    9318              : 
    9319        14959 :     case COMP_IF:
    9320        14959 :       *st = ST_ENDIF;
    9321        14959 :       target = " if";
    9322        14959 :       eos_ok = 0;
    9323        14959 :       break;
    9324              : 
    9325        30855 :     case COMP_DO:
    9326        30855 :     case COMP_DO_CONCURRENT:
    9327        30855 :       *st = ST_ENDDO;
    9328        30855 :       target = " do";
    9329        30855 :       eos_ok = 0;
    9330        30855 :       break;
    9331              : 
    9332           54 :     case COMP_CRITICAL:
    9333           54 :       *st = ST_END_CRITICAL;
    9334           54 :       target = " critical";
    9335           54 :       eos_ok = 0;
    9336           54 :       break;
    9337              : 
    9338         4720 :     case COMP_SELECT:
    9339         4720 :     case COMP_SELECT_TYPE:
    9340         4720 :     case COMP_SELECT_RANK:
    9341         4720 :       *st = ST_END_SELECT;
    9342         4720 :       target = " select";
    9343         4720 :       eos_ok = 0;
    9344         4720 :       break;
    9345              : 
    9346          509 :     case COMP_FORALL:
    9347          509 :       *st = ST_END_FORALL;
    9348          509 :       target = " forall";
    9349          509 :       eos_ok = 0;
    9350          509 :       break;
    9351              : 
    9352          373 :     case COMP_WHERE:
    9353          373 :       *st = ST_END_WHERE;
    9354          373 :       target = " where";
    9355          373 :       eos_ok = 0;
    9356          373 :       break;
    9357              : 
    9358          158 :     case COMP_ENUM:
    9359          158 :       *st = ST_END_ENUM;
    9360          158 :       target = " enum";
    9361          158 :       eos_ok = 0;
    9362          158 :       last_initializer = NULL;
    9363          158 :       set_enum_kind ();
    9364          158 :       gfc_free_enum_history ();
    9365          158 :       break;
    9366              : 
    9367            0 :     case COMP_OMP_BEGIN_METADIRECTIVE:
    9368            0 :       *st = ST_OMP_END_METADIRECTIVE;
    9369            0 :       target = " metadirective";
    9370            0 :       eos_ok = 0;
    9371            0 :       break;
    9372              : 
    9373           98 :     case COMP_CHANGE_TEAM:
    9374           98 :       *st = ST_END_TEAM;
    9375           98 :       target = " team";
    9376           98 :       eos_ok = 0;
    9377           98 :       break;
    9378              : 
    9379            9 :     default:
    9380            9 :       gfc_error ("Unexpected END statement at %C");
    9381            9 :       goto cleanup;
    9382              :     }
    9383              : 
    9384       182772 :   old_loc = gfc_current_locus;
    9385       182772 :   if (gfc_match_eos () == MATCH_YES)
    9386              :     {
    9387        20820 :       if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
    9388              :         {
    9389         8167 :           if (!gfc_notify_std (GFC_STD_F2008, "END statement "
    9390              :                                "instead of %s statement at %L",
    9391              :                                abbreviated_modproc_decl ? "END PROCEDURE"
    9392         4071 :                                : gfc_ascii_statement(*st), &old_loc))
    9393            4 :             goto cleanup;
    9394              :         }
    9395            9 :       else if (!eos_ok)
    9396              :         {
    9397              :           /* We would have required END [something].  */
    9398            9 :           gfc_error ("%s statement expected at %L",
    9399              :                      gfc_ascii_statement (*st), &old_loc);
    9400            9 :           goto cleanup;
    9401              :         }
    9402              : 
    9403              :       return MATCH_YES;
    9404              :     }
    9405              : 
    9406              :   /* Verify that we've got the sort of end-block that we're expecting.  */
    9407       161952 :   if (gfc_match (target) != MATCH_YES)
    9408              :     {
    9409          331 :       gfc_error ("Expecting %s statement at %L", abbreviated_modproc_decl
    9410          165 :                  ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
    9411          166 :       goto cleanup;
    9412              :     }
    9413              :   else
    9414       161786 :     got_matching_end = true;
    9415              : 
    9416       161786 :   if (*st == ST_END_TEAM && gfc_match_end_team () == MATCH_ERROR)
    9417              :     /* Emit errors of stat and errmsg parsing now to finish the block and
    9418              :        continue analysis of compilation unit.  */
    9419            2 :     gfc_error_check ();
    9420              : 
    9421       161786 :   old_loc = gfc_current_locus;
    9422              :   /* If we're at the end, make sure a block name wasn't required.  */
    9423       161786 :   if (gfc_match_eos () == MATCH_YES)
    9424              :     {
    9425       106798 :       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
    9426              :           && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
    9427              :           && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL
    9428              :           && *st != ST_END_TEAM)
    9429              :         return MATCH_YES;
    9430              : 
    9431        54149 :       if (!block_name)
    9432              :         return MATCH_YES;
    9433              : 
    9434            8 :       gfc_error ("Expected block name of %qs in %s statement at %L",
    9435              :                  block_name, gfc_ascii_statement (*st), &old_loc);
    9436              : 
    9437            8 :       return MATCH_ERROR;
    9438              :     }
    9439              : 
    9440              :   /* END INTERFACE has a special handler for its several possible endings.  */
    9441        54988 :   if (*st == ST_END_INTERFACE)
    9442          696 :     return gfc_match_end_interface ();
    9443              : 
    9444              :   /* We haven't hit the end of statement, so what is left must be an
    9445              :      end-name.  */
    9446        54292 :   m = gfc_match_space ();
    9447        54292 :   if (m == MATCH_YES)
    9448        54292 :     m = gfc_match_name (name);
    9449              : 
    9450        54292 :   if (m == MATCH_NO)
    9451            0 :     gfc_error ("Expected terminating name at %C");
    9452        54292 :   if (m != MATCH_YES)
    9453            0 :     goto cleanup;
    9454              : 
    9455        54292 :   if (block_name == NULL)
    9456           15 :     goto syntax;
    9457              : 
    9458              :   /* We have to pick out the declared submodule name from the composite
    9459              :      required by F2008:11.2.3 para 2, which ends in the declared name.  */
    9460        54277 :   if (state == COMP_SUBMODULE)
    9461          137 :     block_name = strchr (block_name, '.') + 1;
    9462              : 
    9463        54277 :   if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
    9464              :     {
    9465            8 :       gfc_error ("Expected label %qs for %s statement at %C", block_name,
    9466              :                  gfc_ascii_statement (*st));
    9467            8 :       goto cleanup;
    9468              :     }
    9469              :   /* Procedure pointer as function result.  */
    9470        54269 :   else if (strcmp (block_name, "ppr@") == 0
    9471           21 :            && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
    9472              :     {
    9473            0 :       gfc_error ("Expected label %qs for %s statement at %C",
    9474            0 :                  gfc_current_block ()->ns->proc_name->name,
    9475              :                  gfc_ascii_statement (*st));
    9476            0 :       goto cleanup;
    9477              :     }
    9478              : 
    9479        54269 :   if (gfc_match_eos () == MATCH_YES)
    9480              :     return MATCH_YES;
    9481              : 
    9482            0 : syntax:
    9483           15 :   gfc_syntax_error (*st);
    9484              : 
    9485          211 : cleanup:
    9486          211 :   gfc_current_locus = old_loc;
    9487              : 
    9488              :   /* If we are missing an END BLOCK, we created a half-ready namespace.
    9489              :      Remove it from the parent namespace's sibling list.  */
    9490              : 
    9491          211 :   if (state == COMP_BLOCK && !got_matching_end)
    9492              :     {
    9493            7 :       parent_ns = gfc_current_ns->parent;
    9494              : 
    9495            7 :       nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
    9496              : 
    9497            7 :       prev_ns = NULL;
    9498            7 :       ns = *nsp;
    9499           14 :       while (ns)
    9500              :         {
    9501            7 :           if (ns == gfc_current_ns)
    9502              :             {
    9503            7 :               if (prev_ns == NULL)
    9504            7 :                 *nsp = NULL;
    9505              :               else
    9506            0 :                 prev_ns->sibling = ns->sibling;
    9507              :             }
    9508            7 :           prev_ns = ns;
    9509            7 :           ns = ns->sibling;
    9510              :         }
    9511              : 
    9512              :       /* The namespace can still be referenced by parser state and code nodes;
    9513              :          let normal block unwinding/freeing own its lifetime.  */
    9514            7 :       gfc_current_ns = parent_ns;
    9515            7 :       gfc_state_stack = gfc_state_stack->previous;
    9516            7 :       state = gfc_current_state ();
    9517              :     }
    9518              : 
    9519              :   return MATCH_ERROR;
    9520              : }
    9521              : 
    9522              : 
    9523              : 
    9524              : /***************** Attribute declaration statements ****************/
    9525              : 
    9526              : /* Set the attribute of a single variable.  */
    9527              : 
    9528              : static match
    9529        10427 : attr_decl1 (void)
    9530              : {
    9531        10427 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    9532        10427 :   gfc_array_spec *as;
    9533              : 
    9534              :   /* Workaround -Wmaybe-uninitialized false positive during
    9535              :      profiledbootstrap by initializing them.  */
    9536        10427 :   gfc_symbol *sym = NULL;
    9537        10427 :   locus var_locus;
    9538        10427 :   match m;
    9539              : 
    9540        10427 :   as = NULL;
    9541              : 
    9542        10427 :   m = gfc_match_name (name);
    9543        10427 :   if (m != MATCH_YES)
    9544            0 :     goto cleanup;
    9545              : 
    9546        10427 :   if (find_special (name, &sym, false))
    9547              :     return MATCH_ERROR;
    9548              : 
    9549        10427 :   if (!check_function_name (name))
    9550              :     {
    9551            7 :       m = MATCH_ERROR;
    9552            7 :       goto cleanup;
    9553              :     }
    9554              : 
    9555        10420 :   var_locus = gfc_current_locus;
    9556              : 
    9557              :   /* Deal with possible array specification for certain attributes.  */
    9558        10420 :   if (current_attr.dimension
    9559         8841 :       || current_attr.codimension
    9560         8819 :       || current_attr.allocatable
    9561         8395 :       || current_attr.pointer
    9562         7672 :       || current_attr.target)
    9563              :     {
    9564         6174 :       m = gfc_match_array_spec (&as, !current_attr.codimension,
    9565              :                                 !current_attr.dimension
    9566         1395 :                                 && !current_attr.pointer
    9567              :                                 && !current_attr.target);
    9568         2974 :       if (m == MATCH_ERROR)
    9569            2 :         goto cleanup;
    9570              : 
    9571         2972 :       if (current_attr.dimension && m == MATCH_NO)
    9572              :         {
    9573            0 :           gfc_error ("Missing array specification at %L in DIMENSION "
    9574              :                      "statement", &var_locus);
    9575            0 :           m = MATCH_ERROR;
    9576            0 :           goto cleanup;
    9577              :         }
    9578              : 
    9579         2972 :       if (current_attr.dimension && sym->value)
    9580              :         {
    9581            1 :           gfc_error ("Dimensions specified for %s at %L after its "
    9582              :                      "initialization", sym->name, &var_locus);
    9583            1 :           m = MATCH_ERROR;
    9584            1 :           goto cleanup;
    9585              :         }
    9586              : 
    9587         2971 :       if (current_attr.codimension && m == MATCH_NO)
    9588              :         {
    9589            0 :           gfc_error ("Missing array specification at %L in CODIMENSION "
    9590              :                      "statement", &var_locus);
    9591            0 :           m = MATCH_ERROR;
    9592            0 :           goto cleanup;
    9593              :         }
    9594              : 
    9595         2971 :       if ((current_attr.allocatable || current_attr.pointer)
    9596         1147 :           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
    9597              :         {
    9598            0 :           gfc_error ("Array specification must be deferred at %L", &var_locus);
    9599            0 :           m = MATCH_ERROR;
    9600            0 :           goto cleanup;
    9601              :         }
    9602              :     }
    9603              : 
    9604        10417 :   if (sym->ts.type == BT_CLASS
    9605          200 :       && sym->ts.u.derived
    9606          200 :       && sym->ts.u.derived->attr.is_class)
    9607              :     {
    9608          177 :       sym->attr.pointer = CLASS_DATA(sym)->attr.class_pointer;
    9609          177 :       sym->attr.allocatable = CLASS_DATA(sym)->attr.allocatable;
    9610          177 :       sym->attr.dimension = CLASS_DATA(sym)->attr.dimension;
    9611          177 :       sym->attr.codimension = CLASS_DATA(sym)->attr.codimension;
    9612          177 :       if (CLASS_DATA (sym)->as)
    9613          123 :         sym->as = gfc_copy_array_spec (CLASS_DATA (sym)->as);
    9614              :     }
    9615         8840 :   if (current_attr.dimension == 0 && current_attr.codimension == 0
    9616        19236 :       && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
    9617              :     {
    9618           22 :       m = MATCH_ERROR;
    9619           22 :       goto cleanup;
    9620              :     }
    9621        10395 :   if (!gfc_set_array_spec (sym, as, &var_locus))
    9622              :     {
    9623           18 :       m = MATCH_ERROR;
    9624           18 :       goto cleanup;
    9625              :     }
    9626              : 
    9627        10377 :   if (sym->attr.cray_pointee && sym->as != NULL)
    9628              :     {
    9629              :       /* Fix the array spec.  */
    9630            2 :       m = gfc_mod_pointee_as (sym->as);
    9631            2 :       if (m == MATCH_ERROR)
    9632            0 :         goto cleanup;
    9633              :     }
    9634              : 
    9635        10377 :   if (!gfc_add_attribute (&sym->attr, &var_locus))
    9636              :     {
    9637            0 :       m = MATCH_ERROR;
    9638            0 :       goto cleanup;
    9639              :     }
    9640              : 
    9641         5740 :   if ((current_attr.external || current_attr.intrinsic)
    9642         6289 :       && sym->attr.flavor != FL_PROCEDURE
    9643        16634 :       && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
    9644              :     {
    9645            0 :       m = MATCH_ERROR;
    9646            0 :       goto cleanup;
    9647              :     }
    9648              : 
    9649        10377 :   if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class
    9650          169 :       && !as && !current_attr.pointer && !current_attr.allocatable
    9651          136 :       && !current_attr.external)
    9652              :     {
    9653          136 :       sym->attr.pointer = 0;
    9654          136 :       sym->attr.allocatable = 0;
    9655          136 :       sym->attr.dimension = 0;
    9656          136 :       sym->attr.codimension = 0;
    9657          136 :       gfc_free_array_spec (sym->as);
    9658          136 :       sym->as = NULL;
    9659              :     }
    9660        10241 :   else if (sym->ts.type == BT_CLASS
    9661        10241 :       && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
    9662              :     {
    9663            0 :       m = MATCH_ERROR;
    9664            0 :       goto cleanup;
    9665              :     }
    9666              : 
    9667        10377 :   add_hidden_procptr_result (sym);
    9668              : 
    9669        10377 :   return MATCH_YES;
    9670              : 
    9671           50 : cleanup:
    9672           50 :   gfc_free_array_spec (as);
    9673           50 :   return m;
    9674              : }
    9675              : 
    9676              : 
    9677              : /* Generic attribute declaration subroutine.  Used for attributes that
    9678              :    just have a list of names.  */
    9679              : 
    9680              : static match
    9681         6712 : attr_decl (void)
    9682              : {
    9683         6712 :   match m;
    9684              : 
    9685              :   /* Gobble the optional double colon, by simply ignoring the result
    9686              :      of gfc_match().  */
    9687         6712 :   gfc_match (" ::");
    9688              : 
    9689        10427 :   for (;;)
    9690              :     {
    9691        10427 :       m = attr_decl1 ();
    9692        10427 :       if (m != MATCH_YES)
    9693              :         break;
    9694              : 
    9695        10377 :       if (gfc_match_eos () == MATCH_YES)
    9696              :         {
    9697              :           m = MATCH_YES;
    9698              :           break;
    9699              :         }
    9700              : 
    9701         3715 :       if (gfc_match_char (',') != MATCH_YES)
    9702              :         {
    9703            0 :           gfc_error ("Unexpected character in variable list at %C");
    9704            0 :           m = MATCH_ERROR;
    9705            0 :           break;
    9706              :         }
    9707              :     }
    9708              : 
    9709         6712 :   return m;
    9710              : }
    9711              : 
    9712              : 
    9713              : /* This routine matches Cray Pointer declarations of the form:
    9714              :    pointer ( <pointer>, <pointee> )
    9715              :    or
    9716              :    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
    9717              :    The pointer, if already declared, should be an integer.  Otherwise, we
    9718              :    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
    9719              :    be either a scalar, or an array declaration.  No space is allocated for
    9720              :    the pointee.  For the statement
    9721              :    pointer (ipt, ar(10))
    9722              :    any subsequent uses of ar will be translated (in C-notation) as
    9723              :    ar(i) => ((<type> *) ipt)(i)
    9724              :    After gimplification, pointee variable will disappear in the code.  */
    9725              : 
    9726              : static match
    9727          334 : cray_pointer_decl (void)
    9728              : {
    9729          334 :   match m;
    9730          334 :   gfc_array_spec *as = NULL;
    9731          334 :   gfc_symbol *cptr; /* Pointer symbol.  */
    9732          334 :   gfc_symbol *cpte; /* Pointee symbol.  */
    9733          334 :   locus var_locus;
    9734          334 :   bool done = false;
    9735              : 
    9736          334 :   while (!done)
    9737              :     {
    9738          347 :       if (gfc_match_char ('(') != MATCH_YES)
    9739              :         {
    9740            1 :           gfc_error ("Expected %<(%> at %C");
    9741            1 :           return MATCH_ERROR;
    9742              :         }
    9743              : 
    9744              :       /* Match pointer.  */
    9745          346 :       var_locus = gfc_current_locus;
    9746          346 :       gfc_clear_attr (&current_attr);
    9747          346 :       gfc_add_cray_pointer (&current_attr, &var_locus);
    9748          346 :       current_ts.type = BT_INTEGER;
    9749          346 :       current_ts.kind = gfc_index_integer_kind;
    9750              : 
    9751          346 :       m = gfc_match_symbol (&cptr, 0);
    9752          346 :       if (m != MATCH_YES)
    9753              :         {
    9754            2 :           gfc_error ("Expected variable name at %C");
    9755            2 :           return m;
    9756              :         }
    9757              : 
    9758          344 :       if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
    9759              :         return MATCH_ERROR;
    9760              : 
    9761          341 :       gfc_set_sym_referenced (cptr);
    9762              : 
    9763          341 :       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
    9764              :         {
    9765          327 :           cptr->ts.type = BT_INTEGER;
    9766          327 :           cptr->ts.kind = gfc_index_integer_kind;
    9767              :         }
    9768           14 :       else if (cptr->ts.type != BT_INTEGER)
    9769              :         {
    9770            1 :           gfc_error ("Cray pointer at %C must be an integer");
    9771            1 :           return MATCH_ERROR;
    9772              :         }
    9773           13 :       else if (cptr->ts.kind < gfc_index_integer_kind)
    9774            0 :         gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
    9775              :                      " memory addresses require %d bytes",
    9776              :                      cptr->ts.kind, gfc_index_integer_kind);
    9777              : 
    9778          340 :       if (gfc_match_char (',') != MATCH_YES)
    9779              :         {
    9780            2 :           gfc_error ("Expected \",\" at %C");
    9781            2 :           return MATCH_ERROR;
    9782              :         }
    9783              : 
    9784              :       /* Match Pointee.  */
    9785          338 :       var_locus = gfc_current_locus;
    9786          338 :       gfc_clear_attr (&current_attr);
    9787          338 :       gfc_add_cray_pointee (&current_attr, &var_locus);
    9788          338 :       current_ts.type = BT_UNKNOWN;
    9789          338 :       current_ts.kind = 0;
    9790              : 
    9791          338 :       m = gfc_match_symbol (&cpte, 0);
    9792          338 :       if (m != MATCH_YES)
    9793              :         {
    9794            2 :           gfc_error ("Expected variable name at %C");
    9795            2 :           return m;
    9796              :         }
    9797              : 
    9798              :       /* Check for an optional array spec.  */
    9799          336 :       m = gfc_match_array_spec (&as, true, false);
    9800          336 :       if (m == MATCH_ERROR)
    9801              :         {
    9802            0 :           gfc_free_array_spec (as);
    9803            0 :           return m;
    9804              :         }
    9805          336 :       else if (m == MATCH_NO)
    9806              :         {
    9807          226 :           gfc_free_array_spec (as);
    9808          226 :           as = NULL;
    9809              :         }
    9810              : 
    9811          336 :       if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
    9812              :         return MATCH_ERROR;
    9813              : 
    9814          329 :       gfc_set_sym_referenced (cpte);
    9815              : 
    9816          329 :       if (cpte->as == NULL)
    9817              :         {
    9818          247 :           if (!gfc_set_array_spec (cpte, as, &var_locus))
    9819            0 :             gfc_internal_error ("Cannot set Cray pointee array spec.");
    9820              :         }
    9821           82 :       else if (as != NULL)
    9822              :         {
    9823            1 :           gfc_error ("Duplicate array spec for Cray pointee at %C");
    9824            1 :           gfc_free_array_spec (as);
    9825            1 :           return MATCH_ERROR;
    9826              :         }
    9827              : 
    9828          328 :       as = NULL;
    9829              : 
    9830          328 :       if (cpte->as != NULL)
    9831              :         {
    9832              :           /* Fix array spec.  */
    9833          190 :           m = gfc_mod_pointee_as (cpte->as);
    9834          190 :           if (m == MATCH_ERROR)
    9835              :             return m;
    9836              :         }
    9837              : 
    9838              :       /* Point the Pointee at the Pointer.  */
    9839          328 :       cpte->cp_pointer = cptr;
    9840              : 
    9841          328 :       if (gfc_match_char (')') != MATCH_YES)
    9842              :         {
    9843            2 :           gfc_error ("Expected \")\" at %C");
    9844            2 :           return MATCH_ERROR;
    9845              :         }
    9846          326 :       m = gfc_match_char (',');
    9847          326 :       if (m != MATCH_YES)
    9848              :         done = true; /* Stop searching for more declarations.  */
    9849              : 
    9850              :     }
    9851              : 
    9852          313 :   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
    9853          313 :       || gfc_match_eos () != MATCH_YES)
    9854              :     {
    9855            0 :       gfc_error ("Expected %<,%> or end of statement at %C");
    9856            0 :       return MATCH_ERROR;
    9857              :     }
    9858              :   return MATCH_YES;
    9859              : }
    9860              : 
    9861              : 
    9862              : match
    9863         3215 : gfc_match_external (void)
    9864              : {
    9865              : 
    9866         3215 :   gfc_clear_attr (&current_attr);
    9867         3215 :   current_attr.external = 1;
    9868              : 
    9869         3215 :   return attr_decl ();
    9870              : }
    9871              : 
    9872              : 
    9873              : match
    9874          208 : gfc_match_intent (void)
    9875              : {
    9876          208 :   sym_intent intent;
    9877              : 
    9878              :   /* This is not allowed within a BLOCK construct!  */
    9879          208 :   if (gfc_current_state () == COMP_BLOCK)
    9880              :     {
    9881            2 :       gfc_error ("INTENT is not allowed inside of BLOCK at %C");
    9882            2 :       return MATCH_ERROR;
    9883              :     }
    9884              : 
    9885          206 :   intent = match_intent_spec ();
    9886          206 :   if (intent == INTENT_UNKNOWN)
    9887              :     return MATCH_ERROR;
    9888              : 
    9889          206 :   gfc_clear_attr (&current_attr);
    9890          206 :   current_attr.intent = intent;
    9891              : 
    9892          206 :   return attr_decl ();
    9893              : }
    9894              : 
    9895              : 
    9896              : match
    9897         1482 : gfc_match_intrinsic (void)
    9898              : {
    9899              : 
    9900         1482 :   gfc_clear_attr (&current_attr);
    9901         1482 :   current_attr.intrinsic = 1;
    9902              : 
    9903         1482 :   return attr_decl ();
    9904              : }
    9905              : 
    9906              : 
    9907              : match
    9908          220 : gfc_match_optional (void)
    9909              : {
    9910              :   /* This is not allowed within a BLOCK construct!  */
    9911          220 :   if (gfc_current_state () == COMP_BLOCK)
    9912              :     {
    9913            2 :       gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
    9914            2 :       return MATCH_ERROR;
    9915              :     }
    9916              : 
    9917          218 :   gfc_clear_attr (&current_attr);
    9918          218 :   current_attr.optional = 1;
    9919              : 
    9920          218 :   return attr_decl ();
    9921              : }
    9922              : 
    9923              : 
    9924              : match
    9925          915 : gfc_match_pointer (void)
    9926              : {
    9927          915 :   gfc_gobble_whitespace ();
    9928          915 :   if (gfc_peek_ascii_char () == '(')
    9929              :     {
    9930          335 :       if (!flag_cray_pointer)
    9931              :         {
    9932            1 :           gfc_error ("Cray pointer declaration at %C requires "
    9933              :                      "%<-fcray-pointer%> flag");
    9934            1 :           return MATCH_ERROR;
    9935              :         }
    9936          334 :       return cray_pointer_decl ();
    9937              :     }
    9938              :   else
    9939              :     {
    9940          580 :       gfc_clear_attr (&current_attr);
    9941          580 :       current_attr.pointer = 1;
    9942              : 
    9943          580 :       return attr_decl ();
    9944              :     }
    9945              : }
    9946              : 
    9947              : 
    9948              : match
    9949          162 : gfc_match_allocatable (void)
    9950              : {
    9951          162 :   gfc_clear_attr (&current_attr);
    9952          162 :   current_attr.allocatable = 1;
    9953              : 
    9954          162 :   return attr_decl ();
    9955              : }
    9956              : 
    9957              : 
    9958              : match
    9959           23 : gfc_match_codimension (void)
    9960              : {
    9961           23 :   gfc_clear_attr (&current_attr);
    9962           23 :   current_attr.codimension = 1;
    9963              : 
    9964           23 :   return attr_decl ();
    9965              : }
    9966              : 
    9967              : 
    9968              : match
    9969           80 : gfc_match_contiguous (void)
    9970              : {
    9971           80 :   if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
    9972              :     return MATCH_ERROR;
    9973              : 
    9974           79 :   gfc_clear_attr (&current_attr);
    9975           79 :   current_attr.contiguous = 1;
    9976              : 
    9977           79 :   return attr_decl ();
    9978              : }
    9979              : 
    9980              : 
    9981              : match
    9982          648 : gfc_match_dimension (void)
    9983              : {
    9984          648 :   gfc_clear_attr (&current_attr);
    9985          648 :   current_attr.dimension = 1;
    9986              : 
    9987          648 :   return attr_decl ();
    9988              : }
    9989              : 
    9990              : 
    9991              : match
    9992           99 : gfc_match_target (void)
    9993              : {
    9994           99 :   gfc_clear_attr (&current_attr);
    9995           99 :   current_attr.target = 1;
    9996              : 
    9997           99 :   return attr_decl ();
    9998              : }
    9999              : 
   10000              : 
   10001              : /* Match the list of entities being specified in a PUBLIC or PRIVATE
   10002              :    statement.  */
   10003              : 
   10004              : static match
   10005         1766 : access_attr_decl (gfc_statement st)
   10006              : {
   10007         1766 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10008         1766 :   interface_type type;
   10009         1766 :   gfc_user_op *uop;
   10010         1766 :   gfc_symbol *sym, *dt_sym;
   10011         1766 :   gfc_intrinsic_op op;
   10012         1766 :   match m;
   10013         1766 :   gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
   10014              : 
   10015         1766 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10016            0 :     goto done;
   10017              : 
   10018         2916 :   for (;;)
   10019              :     {
   10020         2916 :       m = gfc_match_generic_spec (&type, name, &op);
   10021         2916 :       if (m == MATCH_NO)
   10022            0 :         goto syntax;
   10023         2916 :       if (m == MATCH_ERROR)
   10024            0 :         goto done;
   10025              : 
   10026         2916 :       switch (type)
   10027              :         {
   10028            0 :         case INTERFACE_NAMELESS:
   10029            0 :         case INTERFACE_ABSTRACT:
   10030            0 :           goto syntax;
   10031              : 
   10032         2839 :         case INTERFACE_GENERIC:
   10033         2839 :         case INTERFACE_DTIO:
   10034              : 
   10035         2839 :           if (gfc_get_symbol (name, NULL, &sym))
   10036            0 :             goto done;
   10037              : 
   10038         2839 :           if (type == INTERFACE_DTIO
   10039           26 :               && gfc_current_ns->proc_name
   10040           26 :               && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
   10041           26 :               && sym->attr.flavor == FL_UNKNOWN)
   10042            2 :             sym->attr.flavor = FL_PROCEDURE;
   10043              : 
   10044         2839 :           if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
   10045            4 :             goto done;
   10046              : 
   10047          330 :           if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
   10048         2892 :               && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
   10049            0 :             goto done;
   10050              : 
   10051              :           break;
   10052              : 
   10053           72 :         case INTERFACE_INTRINSIC_OP:
   10054           72 :           if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
   10055              :             {
   10056           72 :               gfc_intrinsic_op other_op;
   10057              : 
   10058           72 :               gfc_current_ns->operator_access[op] = access;
   10059              : 
   10060              :               /* Handle the case if there is another op with the same
   10061              :                  function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on.  */
   10062           72 :               other_op = gfc_equivalent_op (op);
   10063              : 
   10064           72 :               if (other_op != INTRINSIC_NONE)
   10065           21 :                 gfc_current_ns->operator_access[other_op] = access;
   10066              :             }
   10067              :           else
   10068              :             {
   10069            0 :               gfc_error ("Access specification of the %s operator at %C has "
   10070              :                          "already been specified", gfc_op2string (op));
   10071            0 :               goto done;
   10072              :             }
   10073              : 
   10074              :           break;
   10075              : 
   10076            5 :         case INTERFACE_USER_OP:
   10077            5 :           uop = gfc_get_uop (name);
   10078              : 
   10079            5 :           if (uop->access == ACCESS_UNKNOWN)
   10080              :             {
   10081            4 :               uop->access = access;
   10082              :             }
   10083              :           else
   10084              :             {
   10085            1 :               gfc_error ("Access specification of the .%s. operator at %C "
   10086              :                          "has already been specified", uop->name);
   10087            1 :               goto done;
   10088              :             }
   10089              : 
   10090            4 :           break;
   10091              :         }
   10092              : 
   10093         2911 :       if (gfc_match_char (',') == MATCH_NO)
   10094              :         break;
   10095              :     }
   10096              : 
   10097         1761 :   if (gfc_match_eos () != MATCH_YES)
   10098            0 :     goto syntax;
   10099              :   return MATCH_YES;
   10100              : 
   10101            0 : syntax:
   10102            0 :   gfc_syntax_error (st);
   10103              : 
   10104         1766 : done:
   10105              :   return MATCH_ERROR;
   10106              : }
   10107              : 
   10108              : 
   10109              : match
   10110           23 : gfc_match_protected (void)
   10111              : {
   10112           23 :   gfc_symbol *sym;
   10113           23 :   match m;
   10114           23 :   char c;
   10115              : 
   10116              :   /* PROTECTED has already been seen, but must be followed by whitespace
   10117              :      or ::.  */
   10118           23 :   c = gfc_peek_ascii_char ();
   10119           23 :   if (!gfc_is_whitespace (c) && c != ':')
   10120              :     return MATCH_NO;
   10121              : 
   10122           22 :   if (!gfc_current_ns->proc_name
   10123           20 :       || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
   10124              :     {
   10125            3 :        gfc_error ("PROTECTED at %C only allowed in specification "
   10126              :                   "part of a module");
   10127            3 :        return MATCH_ERROR;
   10128              : 
   10129              :     }
   10130              : 
   10131           19 :   gfc_match (" ::");
   10132              : 
   10133           19 :   if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
   10134              :     return MATCH_ERROR;
   10135              : 
   10136              :   /* PROTECTED has an entity-list.  */
   10137           18 :   if (gfc_match_eos () == MATCH_YES)
   10138            0 :     goto syntax;
   10139              : 
   10140           26 :   for(;;)
   10141              :     {
   10142           26 :       m = gfc_match_symbol (&sym, 0);
   10143           26 :       switch (m)
   10144              :         {
   10145           26 :         case MATCH_YES:
   10146           26 :           if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
   10147              :             return MATCH_ERROR;
   10148           25 :           goto next_item;
   10149              : 
   10150              :         case MATCH_NO:
   10151              :           break;
   10152              : 
   10153              :         case MATCH_ERROR:
   10154              :           return MATCH_ERROR;
   10155              :         }
   10156              : 
   10157           25 :     next_item:
   10158           25 :       if (gfc_match_eos () == MATCH_YES)
   10159              :         break;
   10160            8 :       if (gfc_match_char (',') != MATCH_YES)
   10161            0 :         goto syntax;
   10162              :     }
   10163              : 
   10164              :   return MATCH_YES;
   10165              : 
   10166            0 : syntax:
   10167            0 :   gfc_error ("Syntax error in PROTECTED statement at %C");
   10168            0 :   return MATCH_ERROR;
   10169              : }
   10170              : 
   10171              : 
   10172              : /* The PRIVATE statement is a bit weird in that it can be an attribute
   10173              :    declaration, but also works as a standalone statement inside of a
   10174              :    type declaration or a module.  */
   10175              : 
   10176              : match
   10177        29356 : gfc_match_private (gfc_statement *st)
   10178              : {
   10179        29356 :   gfc_state_data *prev;
   10180              : 
   10181        29356 :   if (gfc_match ("private") != MATCH_YES)
   10182              :     return MATCH_NO;
   10183              : 
   10184              :   /* Try matching PRIVATE without an access-list.  */
   10185         1635 :   if (gfc_match_eos () == MATCH_YES)
   10186              :     {
   10187         1348 :       prev = gfc_state_stack->previous;
   10188         1348 :       if (gfc_current_state () != COMP_MODULE
   10189          367 :           && !(gfc_current_state () == COMP_DERIVED
   10190          334 :                 && prev && prev->state == COMP_MODULE)
   10191           34 :           && !(gfc_current_state () == COMP_DERIVED_CONTAINS
   10192           32 :                 && prev->previous && prev->previous->state == COMP_MODULE))
   10193              :         {
   10194            2 :           gfc_error ("PRIVATE statement at %C is only allowed in the "
   10195              :                      "specification part of a module");
   10196            2 :           return MATCH_ERROR;
   10197              :         }
   10198              : 
   10199         1346 :       *st = ST_PRIVATE;
   10200         1346 :       return MATCH_YES;
   10201              :     }
   10202              : 
   10203              :   /* At this point in free-form source code, PRIVATE must be followed
   10204              :      by whitespace or ::.  */
   10205          287 :   if (gfc_current_form == FORM_FREE)
   10206              :     {
   10207          285 :       char c = gfc_peek_ascii_char ();
   10208          285 :       if (!gfc_is_whitespace (c) && c != ':')
   10209              :         return MATCH_NO;
   10210              :     }
   10211              : 
   10212          286 :   prev = gfc_state_stack->previous;
   10213          286 :   if (gfc_current_state () != COMP_MODULE
   10214            1 :       && !(gfc_current_state () == COMP_DERIVED
   10215            0 :            && prev && prev->state == COMP_MODULE)
   10216            1 :       && !(gfc_current_state () == COMP_DERIVED_CONTAINS
   10217            0 :            && prev->previous && prev->previous->state == COMP_MODULE))
   10218              :     {
   10219            1 :       gfc_error ("PRIVATE statement at %C is only allowed in the "
   10220              :                  "specification part of a module");
   10221            1 :       return MATCH_ERROR;
   10222              :     }
   10223              : 
   10224          285 :   *st = ST_ATTR_DECL;
   10225          285 :   return access_attr_decl (ST_PRIVATE);
   10226              : }
   10227              : 
   10228              : 
   10229              : match
   10230         1880 : gfc_match_public (gfc_statement *st)
   10231              : {
   10232         1880 :   if (gfc_match ("public") != MATCH_YES)
   10233              :     return MATCH_NO;
   10234              : 
   10235              :   /* Try matching PUBLIC without an access-list.  */
   10236         1528 :   if (gfc_match_eos () == MATCH_YES)
   10237              :     {
   10238           45 :       if (gfc_current_state () != COMP_MODULE)
   10239              :         {
   10240            2 :           gfc_error ("PUBLIC statement at %C is only allowed in the "
   10241              :                      "specification part of a module");
   10242            2 :           return MATCH_ERROR;
   10243              :         }
   10244              : 
   10245           43 :       *st = ST_PUBLIC;
   10246           43 :       return MATCH_YES;
   10247              :     }
   10248              : 
   10249              :   /* At this point in free-form source code, PUBLIC must be followed
   10250              :      by whitespace or ::.  */
   10251         1483 :   if (gfc_current_form == FORM_FREE)
   10252              :     {
   10253         1481 :       char c = gfc_peek_ascii_char ();
   10254         1481 :       if (!gfc_is_whitespace (c) && c != ':')
   10255              :         return MATCH_NO;
   10256              :     }
   10257              : 
   10258         1482 :   if (gfc_current_state () != COMP_MODULE)
   10259              :     {
   10260            1 :       gfc_error ("PUBLIC statement at %C is only allowed in the "
   10261              :                  "specification part of a module");
   10262            1 :       return MATCH_ERROR;
   10263              :     }
   10264              : 
   10265         1481 :   *st = ST_ATTR_DECL;
   10266         1481 :   return access_attr_decl (ST_PUBLIC);
   10267              : }
   10268              : 
   10269              : 
   10270              : /* Workhorse for gfc_match_parameter.  */
   10271              : 
   10272              : static match
   10273         8533 : do_parm (void)
   10274              : {
   10275         8533 :   gfc_symbol *sym;
   10276         8533 :   gfc_expr *init;
   10277         8533 :   gfc_charlen *saved_cl_list;
   10278         8533 :   match m;
   10279         8533 :   bool t;
   10280              : 
   10281         8533 :   saved_cl_list = gfc_current_ns->cl_list;
   10282              : 
   10283         8533 :   m = gfc_match_symbol (&sym, 0);
   10284         8533 :   if (m == MATCH_NO)
   10285            0 :     gfc_error ("Expected variable name at %C in PARAMETER statement");
   10286              : 
   10287         8533 :   if (m != MATCH_YES)
   10288              :     return m;
   10289              : 
   10290         8533 :   if (gfc_match_char ('=') == MATCH_NO)
   10291              :     {
   10292            0 :       gfc_error ("Expected = sign in PARAMETER statement at %C");
   10293            0 :       return MATCH_ERROR;
   10294              :     }
   10295              : 
   10296         8533 :   m = gfc_match_init_expr (&init);
   10297         8533 :   if (m == MATCH_NO)
   10298            0 :     gfc_error ("Expected expression at %C in PARAMETER statement");
   10299         8533 :   if (m != MATCH_YES)
   10300              :     return m;
   10301              : 
   10302         8532 :   if (sym->ts.type == BT_UNKNOWN
   10303         8532 :       && !gfc_set_default_type (sym, 1, NULL))
   10304              :     {
   10305            1 :       m = MATCH_ERROR;
   10306            1 :       goto cleanup;
   10307              :     }
   10308              : 
   10309         8531 :   if (!gfc_check_assign_symbol (sym, NULL, init)
   10310         8531 :       || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
   10311              :     {
   10312            1 :       m = MATCH_ERROR;
   10313            1 :       goto cleanup;
   10314              :     }
   10315              : 
   10316         8530 :   if (sym->value)
   10317              :     {
   10318            1 :       gfc_error ("Initializing already initialized variable at %C");
   10319            1 :       m = MATCH_ERROR;
   10320            1 :       goto cleanup;
   10321              :     }
   10322              : 
   10323         8529 :   t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus,
   10324              :                             saved_cl_list);
   10325         8529 :   return (t) ? MATCH_YES : MATCH_ERROR;
   10326              : 
   10327            3 : cleanup:
   10328            3 :   gfc_free_expr (init);
   10329            3 :   return m;
   10330              : }
   10331              : 
   10332              : 
   10333              : /* Match a parameter statement, with the weird syntax that these have.  */
   10334              : 
   10335              : match
   10336         7820 : gfc_match_parameter (void)
   10337              : {
   10338         7820 :   const char *term = " )%t";
   10339         7820 :   match m;
   10340              : 
   10341         7820 :   if (gfc_match_char ('(') == MATCH_NO)
   10342              :     {
   10343              :       /* With legacy PARAMETER statements, don't expect a terminating ')'.  */
   10344           28 :       if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
   10345              :         return MATCH_NO;
   10346         7819 :       term = " %t";
   10347              :     }
   10348              : 
   10349         8533 :   for (;;)
   10350              :     {
   10351         8533 :       m = do_parm ();
   10352         8533 :       if (m != MATCH_YES)
   10353              :         break;
   10354              : 
   10355         8529 :       if (gfc_match (term) == MATCH_YES)
   10356              :         break;
   10357              : 
   10358          714 :       if (gfc_match_char (',') != MATCH_YES)
   10359              :         {
   10360            0 :           gfc_error ("Unexpected characters in PARAMETER statement at %C");
   10361            0 :           m = MATCH_ERROR;
   10362            0 :           break;
   10363              :         }
   10364              :     }
   10365              : 
   10366              :   return m;
   10367              : }
   10368              : 
   10369              : 
   10370              : match
   10371            8 : gfc_match_automatic (void)
   10372              : {
   10373            8 :   gfc_symbol *sym;
   10374            8 :   match m;
   10375            8 :   bool seen_symbol = false;
   10376              : 
   10377            8 :   if (!flag_dec_static)
   10378              :     {
   10379            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
   10380              :                  "%<-fdec-static%>",
   10381              :                  "AUTOMATIC"
   10382              :                  );
   10383            2 :       return MATCH_ERROR;
   10384              :     }
   10385              : 
   10386            6 :   gfc_match (" ::");
   10387              : 
   10388            6 :   for (;;)
   10389              :     {
   10390            6 :       m = gfc_match_symbol (&sym, 0);
   10391            6 :       switch (m)
   10392              :       {
   10393              :       case MATCH_NO:
   10394              :         break;
   10395              : 
   10396              :       case MATCH_ERROR:
   10397              :         return MATCH_ERROR;
   10398              : 
   10399            4 :       case MATCH_YES:
   10400            4 :         if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
   10401              :           return MATCH_ERROR;
   10402              :         seen_symbol = true;
   10403              :         break;
   10404              :       }
   10405              : 
   10406            4 :       if (gfc_match_eos () == MATCH_YES)
   10407              :         break;
   10408            0 :       if (gfc_match_char (',') != MATCH_YES)
   10409            0 :         goto syntax;
   10410              :     }
   10411              : 
   10412            4 :   if (!seen_symbol)
   10413              :     {
   10414            2 :       gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
   10415            2 :       return MATCH_ERROR;
   10416              :     }
   10417              : 
   10418              :   return MATCH_YES;
   10419              : 
   10420            0 : syntax:
   10421            0 :   gfc_error ("Syntax error in AUTOMATIC statement at %C");
   10422            0 :   return MATCH_ERROR;
   10423              : }
   10424              : 
   10425              : 
   10426              : match
   10427            7 : gfc_match_static (void)
   10428              : {
   10429            7 :   gfc_symbol *sym;
   10430            7 :   match m;
   10431            7 :   bool seen_symbol = false;
   10432              : 
   10433            7 :   if (!flag_dec_static)
   10434              :     {
   10435            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
   10436              :                  "%<-fdec-static%>",
   10437              :                  "STATIC");
   10438            2 :       return MATCH_ERROR;
   10439              :     }
   10440              : 
   10441            5 :   gfc_match (" ::");
   10442              : 
   10443            5 :   for (;;)
   10444              :     {
   10445            5 :       m = gfc_match_symbol (&sym, 0);
   10446            5 :       switch (m)
   10447              :       {
   10448              :       case MATCH_NO:
   10449              :         break;
   10450              : 
   10451              :       case MATCH_ERROR:
   10452              :         return MATCH_ERROR;
   10453              : 
   10454            3 :       case MATCH_YES:
   10455            3 :         if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
   10456              :                           &gfc_current_locus))
   10457              :           return MATCH_ERROR;
   10458              :         seen_symbol = true;
   10459              :         break;
   10460              :       }
   10461              : 
   10462            3 :       if (gfc_match_eos () == MATCH_YES)
   10463              :         break;
   10464            0 :       if (gfc_match_char (',') != MATCH_YES)
   10465            0 :         goto syntax;
   10466              :     }
   10467              : 
   10468            3 :   if (!seen_symbol)
   10469              :     {
   10470            2 :       gfc_error ("Expected entity-list in STATIC statement at %C");
   10471            2 :       return MATCH_ERROR;
   10472              :     }
   10473              : 
   10474              :   return MATCH_YES;
   10475              : 
   10476            0 : syntax:
   10477            0 :   gfc_error ("Syntax error in STATIC statement at %C");
   10478            0 :   return MATCH_ERROR;
   10479              : }
   10480              : 
   10481              : 
   10482              : /* Save statements have a special syntax.  */
   10483              : 
   10484              : match
   10485          272 : gfc_match_save (void)
   10486              : {
   10487          272 :   char n[GFC_MAX_SYMBOL_LEN+1];
   10488          272 :   gfc_common_head *c;
   10489          272 :   gfc_symbol *sym;
   10490          272 :   match m;
   10491              : 
   10492          272 :   if (gfc_match_eos () == MATCH_YES)
   10493              :     {
   10494          150 :       if (gfc_current_ns->seen_save)
   10495              :         {
   10496            7 :           if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
   10497              :                                "follows previous SAVE statement"))
   10498              :             return MATCH_ERROR;
   10499              :         }
   10500              : 
   10501          149 :       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
   10502          149 :       return MATCH_YES;
   10503              :     }
   10504              : 
   10505          122 :   if (gfc_current_ns->save_all)
   10506              :     {
   10507            7 :       if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
   10508              :                            "blanket SAVE statement"))
   10509              :         return MATCH_ERROR;
   10510              :     }
   10511              : 
   10512          121 :   gfc_match (" ::");
   10513              : 
   10514          183 :   for (;;)
   10515              :     {
   10516          183 :       m = gfc_match_symbol (&sym, 0);
   10517          183 :       switch (m)
   10518              :         {
   10519          181 :         case MATCH_YES:
   10520          181 :           if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
   10521              :                              &gfc_current_locus))
   10522              :             return MATCH_ERROR;
   10523          179 :           goto next_item;
   10524              : 
   10525              :         case MATCH_NO:
   10526              :           break;
   10527              : 
   10528              :         case MATCH_ERROR:
   10529              :           return MATCH_ERROR;
   10530              :         }
   10531              : 
   10532            2 :       m = gfc_match (" / %n /", &n);
   10533            2 :       if (m == MATCH_ERROR)
   10534              :         return MATCH_ERROR;
   10535            2 :       if (m == MATCH_NO)
   10536            0 :         goto syntax;
   10537              : 
   10538              :       /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
   10539              :          saved-entity-list that does not specify a common-block-name.  */
   10540            2 :       if (gfc_current_state () == COMP_BLOCK)
   10541              :         {
   10542            1 :           gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
   10543              :                      "in a BLOCK construct", n);
   10544            1 :           return MATCH_ERROR;
   10545              :         }
   10546              : 
   10547            1 :       c = gfc_get_common (n, 0);
   10548            1 :       c->saved = 1;
   10549              : 
   10550            1 :       gfc_current_ns->seen_save = 1;
   10551              : 
   10552          180 :     next_item:
   10553          180 :       if (gfc_match_eos () == MATCH_YES)
   10554              :         break;
   10555           62 :       if (gfc_match_char (',') != MATCH_YES)
   10556            0 :         goto syntax;
   10557              :     }
   10558              : 
   10559              :   return MATCH_YES;
   10560              : 
   10561            0 : syntax:
   10562            0 :   if (gfc_current_ns->seen_save)
   10563              :     {
   10564            0 :       gfc_error ("Syntax error in SAVE statement at %C");
   10565            0 :       return MATCH_ERROR;
   10566              :     }
   10567              :   else
   10568              :       return MATCH_NO;
   10569              : }
   10570              : 
   10571              : 
   10572              : match
   10573           93 : gfc_match_value (void)
   10574              : {
   10575           93 :   gfc_symbol *sym;
   10576           93 :   match m;
   10577              : 
   10578              :   /* This is not allowed within a BLOCK construct!  */
   10579           93 :   if (gfc_current_state () == COMP_BLOCK)
   10580              :     {
   10581            2 :       gfc_error ("VALUE is not allowed inside of BLOCK at %C");
   10582            2 :       return MATCH_ERROR;
   10583              :     }
   10584              : 
   10585           91 :   if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
   10586              :     return MATCH_ERROR;
   10587              : 
   10588           90 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10589              :     {
   10590              :       return MATCH_ERROR;
   10591              :     }
   10592              : 
   10593           90 :   if (gfc_match_eos () == MATCH_YES)
   10594            0 :     goto syntax;
   10595              : 
   10596          116 :   for(;;)
   10597              :     {
   10598          116 :       m = gfc_match_symbol (&sym, 0);
   10599          116 :       switch (m)
   10600              :         {
   10601          116 :         case MATCH_YES:
   10602          116 :           if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
   10603              :             return MATCH_ERROR;
   10604          109 :           goto next_item;
   10605              : 
   10606              :         case MATCH_NO:
   10607              :           break;
   10608              : 
   10609              :         case MATCH_ERROR:
   10610              :           return MATCH_ERROR;
   10611              :         }
   10612              : 
   10613          109 :     next_item:
   10614          109 :       if (gfc_match_eos () == MATCH_YES)
   10615              :         break;
   10616           26 :       if (gfc_match_char (',') != MATCH_YES)
   10617            0 :         goto syntax;
   10618              :     }
   10619              : 
   10620              :   return MATCH_YES;
   10621              : 
   10622            0 : syntax:
   10623            0 :   gfc_error ("Syntax error in VALUE statement at %C");
   10624            0 :   return MATCH_ERROR;
   10625              : }
   10626              : 
   10627              : 
   10628              : match
   10629           45 : gfc_match_volatile (void)
   10630              : {
   10631           45 :   gfc_symbol *sym;
   10632           45 :   char *name;
   10633           45 :   match m;
   10634              : 
   10635           45 :   if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
   10636              :     return MATCH_ERROR;
   10637              : 
   10638           44 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10639              :     {
   10640              :       return MATCH_ERROR;
   10641              :     }
   10642              : 
   10643           44 :   if (gfc_match_eos () == MATCH_YES)
   10644            1 :     goto syntax;
   10645              : 
   10646           48 :   for(;;)
   10647              :     {
   10648              :       /* VOLATILE is special because it can be added to host-associated
   10649              :          symbols locally.  Except for coarrays.  */
   10650           48 :       m = gfc_match_symbol (&sym, 1);
   10651           48 :       switch (m)
   10652              :         {
   10653           48 :         case MATCH_YES:
   10654           48 :           name = XALLOCAVAR (char, strlen (sym->name) + 1);
   10655           48 :           strcpy (name, sym->name);
   10656           48 :           if (!check_function_name (name))
   10657              :             return MATCH_ERROR;
   10658              :           /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
   10659              :              for variable in a BLOCK which is defined outside of the BLOCK.  */
   10660           47 :           if (sym->ns != gfc_current_ns && sym->attr.codimension)
   10661              :             {
   10662            2 :               gfc_error ("Specifying VOLATILE for coarray variable %qs at "
   10663              :                          "%C, which is use-/host-associated", sym->name);
   10664            2 :               return MATCH_ERROR;
   10665              :             }
   10666           45 :           if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
   10667              :             return MATCH_ERROR;
   10668           42 :           goto next_item;
   10669              : 
   10670              :         case MATCH_NO:
   10671              :           break;
   10672              : 
   10673              :         case MATCH_ERROR:
   10674              :           return MATCH_ERROR;
   10675              :         }
   10676              : 
   10677           42 :     next_item:
   10678           42 :       if (gfc_match_eos () == MATCH_YES)
   10679              :         break;
   10680            5 :       if (gfc_match_char (',') != MATCH_YES)
   10681            0 :         goto syntax;
   10682              :     }
   10683              : 
   10684              :   return MATCH_YES;
   10685              : 
   10686            1 : syntax:
   10687            1 :   gfc_error ("Syntax error in VOLATILE statement at %C");
   10688            1 :   return MATCH_ERROR;
   10689              : }
   10690              : 
   10691              : 
   10692              : match
   10693           11 : gfc_match_asynchronous (void)
   10694              : {
   10695           11 :   gfc_symbol *sym;
   10696           11 :   char *name;
   10697           11 :   match m;
   10698              : 
   10699           11 :   if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
   10700              :     return MATCH_ERROR;
   10701              : 
   10702           10 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10703              :     {
   10704              :       return MATCH_ERROR;
   10705              :     }
   10706              : 
   10707           10 :   if (gfc_match_eos () == MATCH_YES)
   10708            0 :     goto syntax;
   10709              : 
   10710           10 :   for(;;)
   10711              :     {
   10712              :       /* ASYNCHRONOUS is special because it can be added to host-associated
   10713              :          symbols locally.  */
   10714           10 :       m = gfc_match_symbol (&sym, 1);
   10715           10 :       switch (m)
   10716              :         {
   10717           10 :         case MATCH_YES:
   10718           10 :           name = XALLOCAVAR (char, strlen (sym->name) + 1);
   10719           10 :           strcpy (name, sym->name);
   10720           10 :           if (!check_function_name (name))
   10721              :             return MATCH_ERROR;
   10722            9 :           if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
   10723              :             return MATCH_ERROR;
   10724            7 :           goto next_item;
   10725              : 
   10726              :         case MATCH_NO:
   10727              :           break;
   10728              : 
   10729              :         case MATCH_ERROR:
   10730              :           return MATCH_ERROR;
   10731              :         }
   10732              : 
   10733            7 :     next_item:
   10734            7 :       if (gfc_match_eos () == MATCH_YES)
   10735              :         break;
   10736            0 :       if (gfc_match_char (',') != MATCH_YES)
   10737            0 :         goto syntax;
   10738              :     }
   10739              : 
   10740              :   return MATCH_YES;
   10741              : 
   10742            0 : syntax:
   10743            0 :   gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
   10744            0 :   return MATCH_ERROR;
   10745              : }
   10746              : 
   10747              : 
   10748              : /* Match a module procedure statement in a submodule.  */
   10749              : 
   10750              : match
   10751       770551 : gfc_match_submod_proc (void)
   10752              : {
   10753       770551 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10754       770551 :   gfc_symbol *sym, *fsym;
   10755       770551 :   match m;
   10756       770551 :   gfc_formal_arglist *formal, *head, *tail;
   10757              : 
   10758       770551 :   if (gfc_current_state () != COMP_CONTAINS
   10759        15771 :       || !(gfc_state_stack->previous
   10760        15771 :            && (gfc_state_stack->previous->state == COMP_SUBMODULE
   10761        15771 :                || gfc_state_stack->previous->state == COMP_MODULE)))
   10762              :     return MATCH_NO;
   10763              : 
   10764         7870 :   m = gfc_match (" module% procedure% %n", name);
   10765         7870 :   if (m != MATCH_YES)
   10766              :     return m;
   10767              : 
   10768          267 :   if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
   10769              :                                       "at %C"))
   10770              :     return MATCH_ERROR;
   10771              : 
   10772          267 :   if (get_proc_name (name, &sym, false))
   10773              :     return MATCH_ERROR;
   10774              : 
   10775              :   /* Make sure that the result field is appropriately filled.  */
   10776          267 :   if (sym->tlink && sym->tlink->attr.function)
   10777              :     {
   10778          117 :       if (sym->tlink->result && sym->tlink->result != sym->tlink)
   10779              :         {
   10780           67 :           sym->result = sym->tlink->result;
   10781           67 :           if (!sym->result->attr.use_assoc)
   10782              :             {
   10783           20 :               gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
   10784              :                                                  sym->result->name);
   10785           20 :               st->n.sym = sym->result;
   10786           20 :               sym->result->refs++;
   10787              :             }
   10788              :         }
   10789              :       else
   10790           50 :         sym->result = sym;
   10791              :     }
   10792              : 
   10793              :   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
   10794              :      the symbol existed before.  */
   10795          267 :   sym->declared_at = gfc_current_locus;
   10796              : 
   10797          267 :   if (!sym->attr.module_procedure)
   10798              :     return MATCH_ERROR;
   10799              : 
   10800              :   /* Signal match_end to expect "end procedure".  */
   10801          265 :   sym->abr_modproc_decl = 1;
   10802              : 
   10803              :   /* Change from IFSRC_IFBODY coming from the interface declaration.  */
   10804          265 :   sym->attr.if_source = IFSRC_DECL;
   10805              : 
   10806          265 :   gfc_new_block = sym;
   10807              : 
   10808              :   /* Make a new formal arglist with the symbols in the procedure
   10809              :       namespace.  */
   10810          265 :   head = tail = NULL;
   10811          600 :   for (formal = sym->formal; formal && formal->sym; formal = formal->next)
   10812              :     {
   10813          335 :       if (formal == sym->formal)
   10814          238 :         head = tail = gfc_get_formal_arglist ();
   10815              :       else
   10816              :         {
   10817           97 :           tail->next = gfc_get_formal_arglist ();
   10818           97 :           tail = tail->next;
   10819              :         }
   10820              : 
   10821          335 :       if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
   10822            0 :         goto cleanup;
   10823              : 
   10824          335 :       tail->sym = fsym;
   10825          335 :       gfc_set_sym_referenced (fsym);
   10826              :     }
   10827              : 
   10828              :   /* The dummy symbols get cleaned up, when the formal_namespace of the
   10829              :      interface declaration is cleared.  This allows us to add the
   10830              :      explicit interface as is done for other type of procedure.  */
   10831          265 :   if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
   10832              :                                    &gfc_current_locus))
   10833              :     return MATCH_ERROR;
   10834              : 
   10835          265 :   if (gfc_match_eos () != MATCH_YES)
   10836              :     {
   10837              :       /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
   10838              :          undone, such that the st->n.sym->formal points to the original symbol;
   10839              :          if now this namespace is finalized, the formal namespace is freed,
   10840              :          but it might be still needed in the parent namespace.  */
   10841            1 :       gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
   10842            1 :       st->n.sym = NULL;
   10843            1 :       gfc_free_symbol (sym->tlink);
   10844            1 :       sym->tlink = NULL;
   10845            1 :       sym->refs--;
   10846            1 :       gfc_syntax_error (ST_MODULE_PROC);
   10847            1 :       return MATCH_ERROR;
   10848              :     }
   10849              : 
   10850              :   return MATCH_YES;
   10851              : 
   10852            0 : cleanup:
   10853            0 :   gfc_free_formal_arglist (head);
   10854            0 :   return MATCH_ERROR;
   10855              : }
   10856              : 
   10857              : 
   10858              : /* Match a module procedure statement.  Note that we have to modify
   10859              :    symbols in the parent's namespace because the current one was there
   10860              :    to receive symbols that are in an interface's formal argument list.  */
   10861              : 
   10862              : match
   10863         1620 : gfc_match_modproc (void)
   10864              : {
   10865         1620 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10866         1620 :   gfc_symbol *sym;
   10867         1620 :   match m;
   10868         1620 :   locus old_locus;
   10869         1620 :   gfc_namespace *module_ns;
   10870         1620 :   gfc_interface *old_interface_head, *interface;
   10871              : 
   10872         1620 :   if (gfc_state_stack->previous == NULL
   10873         1618 :       || (gfc_state_stack->state != COMP_INTERFACE
   10874            5 :           && (gfc_state_stack->state != COMP_CONTAINS
   10875            4 :               || gfc_state_stack->previous->state != COMP_INTERFACE))
   10876         1613 :       || current_interface.type == INTERFACE_NAMELESS
   10877         1613 :       || current_interface.type == INTERFACE_ABSTRACT)
   10878              :     {
   10879            8 :       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
   10880              :                  "interface");
   10881            8 :       return MATCH_ERROR;
   10882              :     }
   10883              : 
   10884         1612 :   module_ns = gfc_current_ns->parent;
   10885         1618 :   for (; module_ns; module_ns = module_ns->parent)
   10886         1618 :     if (module_ns->proc_name->attr.flavor == FL_MODULE
   10887           29 :         || module_ns->proc_name->attr.flavor == FL_PROGRAM
   10888           12 :         || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
   10889           12 :             && !module_ns->proc_name->attr.contained))
   10890              :       break;
   10891              : 
   10892         1612 :   if (module_ns == NULL)
   10893              :     return MATCH_ERROR;
   10894              : 
   10895              :   /* Store the current state of the interface. We will need it if we
   10896              :      end up with a syntax error and need to recover.  */
   10897         1612 :   old_interface_head = gfc_current_interface_head ();
   10898              : 
   10899              :   /* Check if the F2008 optional double colon appears.  */
   10900         1612 :   gfc_gobble_whitespace ();
   10901         1612 :   old_locus = gfc_current_locus;
   10902         1612 :   if (gfc_match ("::") == MATCH_YES)
   10903              :     {
   10904           25 :       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
   10905              :                            "MODULE PROCEDURE statement at %L", &old_locus))
   10906              :         return MATCH_ERROR;
   10907              :     }
   10908              :   else
   10909         1587 :     gfc_current_locus = old_locus;
   10910              : 
   10911         1967 :   for (;;)
   10912              :     {
   10913         1967 :       bool last = false;
   10914         1967 :       old_locus = gfc_current_locus;
   10915              : 
   10916         1967 :       m = gfc_match_name (name);
   10917         1967 :       if (m == MATCH_NO)
   10918            1 :         goto syntax;
   10919         1966 :       if (m != MATCH_YES)
   10920              :         return MATCH_ERROR;
   10921              : 
   10922              :       /* Check for syntax error before starting to add symbols to the
   10923              :          current namespace.  */
   10924         1966 :       if (gfc_match_eos () == MATCH_YES)
   10925              :         last = true;
   10926              : 
   10927          360 :       if (!last && gfc_match_char (',') != MATCH_YES)
   10928            2 :         goto syntax;
   10929              : 
   10930              :       /* Now we're sure the syntax is valid, we process this item
   10931              :          further.  */
   10932         1964 :       if (gfc_get_symbol (name, module_ns, &sym))
   10933              :         return MATCH_ERROR;
   10934              : 
   10935         1964 :       if (sym->attr.intrinsic)
   10936              :         {
   10937            1 :           gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
   10938              :                      "PROCEDURE", &old_locus);
   10939            1 :           return MATCH_ERROR;
   10940              :         }
   10941              : 
   10942         1963 :       if (sym->attr.proc != PROC_MODULE
   10943         1963 :           && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
   10944              :         return MATCH_ERROR;
   10945              : 
   10946         1960 :       if (!gfc_add_interface (sym))
   10947              :         return MATCH_ERROR;
   10948              : 
   10949         1957 :       sym->attr.mod_proc = 1;
   10950         1957 :       sym->declared_at = old_locus;
   10951              : 
   10952         1957 :       if (last)
   10953              :         break;
   10954              :     }
   10955              : 
   10956              :   return MATCH_YES;
   10957              : 
   10958            3 : syntax:
   10959              :   /* Restore the previous state of the interface.  */
   10960            3 :   interface = gfc_current_interface_head ();
   10961            3 :   gfc_set_current_interface_head (old_interface_head);
   10962              : 
   10963              :   /* Free the new interfaces.  */
   10964           10 :   while (interface != old_interface_head)
   10965              :   {
   10966            4 :     gfc_interface *i = interface->next;
   10967            4 :     free (interface);
   10968            4 :     interface = i;
   10969              :   }
   10970              : 
   10971              :   /* And issue a syntax error.  */
   10972            3 :   gfc_syntax_error (ST_MODULE_PROC);
   10973            3 :   return MATCH_ERROR;
   10974              : }
   10975              : 
   10976              : 
   10977              : /* Check a derived type that is being extended.  */
   10978              : 
   10979              : static gfc_symbol*
   10980         1551 : check_extended_derived_type (char *name)
   10981              : {
   10982         1551 :   gfc_symbol *extended;
   10983              : 
   10984         1551 :   if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
   10985              :     {
   10986            0 :       gfc_error ("Ambiguous symbol in TYPE definition at %C");
   10987            0 :       return NULL;
   10988              :     }
   10989              : 
   10990         1551 :   extended = gfc_find_dt_in_generic (extended);
   10991              : 
   10992              :   /* F08:C428.  */
   10993         1551 :   if (!extended)
   10994              :     {
   10995            2 :       gfc_error ("Symbol %qs at %C has not been previously defined", name);
   10996            2 :       return NULL;
   10997              :     }
   10998              : 
   10999         1549 :   if (extended->attr.flavor != FL_DERIVED)
   11000              :     {
   11001            0 :       gfc_error ("%qs in EXTENDS expression at %C is not a "
   11002              :                  "derived type", name);
   11003            0 :       return NULL;
   11004              :     }
   11005              : 
   11006         1549 :   if (extended->attr.is_bind_c)
   11007              :     {
   11008            1 :       gfc_error ("%qs cannot be extended at %C because it "
   11009              :                  "is BIND(C)", extended->name);
   11010            1 :       return NULL;
   11011              :     }
   11012              : 
   11013         1548 :   if (extended->attr.sequence)
   11014              :     {
   11015            1 :       gfc_error ("%qs cannot be extended at %C because it "
   11016              :                  "is a SEQUENCE type", extended->name);
   11017            1 :       return NULL;
   11018              :     }
   11019              : 
   11020              :   return extended;
   11021              : }
   11022              : 
   11023              : 
   11024              : /* Match the optional attribute specifiers for a type declaration.
   11025              :    Return MATCH_ERROR if an error is encountered in one of the handled
   11026              :    attributes (public, private, bind(c)), MATCH_NO if what's found is
   11027              :    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
   11028              :    checking on attribute conflicts needs to be done.  */
   11029              : 
   11030              : static match
   11031        19824 : gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
   11032              : {
   11033              :   /* See if the derived type is marked as private.  */
   11034        19824 :   if (gfc_match (" , private") == MATCH_YES)
   11035              :     {
   11036           15 :       if (gfc_current_state () != COMP_MODULE)
   11037              :         {
   11038            1 :           gfc_error ("Derived type at %C can only be PRIVATE in the "
   11039              :                      "specification part of a module");
   11040            1 :           return MATCH_ERROR;
   11041              :         }
   11042              : 
   11043           14 :       if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
   11044            0 :         return MATCH_ERROR;
   11045              :     }
   11046        19809 :   else if (gfc_match (" , public") == MATCH_YES)
   11047              :     {
   11048          558 :       if (gfc_current_state () != COMP_MODULE)
   11049              :         {
   11050            0 :           gfc_error ("Derived type at %C can only be PUBLIC in the "
   11051              :                      "specification part of a module");
   11052            0 :           return MATCH_ERROR;
   11053              :         }
   11054              : 
   11055          558 :       if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
   11056            0 :         return MATCH_ERROR;
   11057              :     }
   11058        19251 :   else if (gfc_match (" , bind ( c )") == MATCH_YES)
   11059              :     {
   11060              :       /* If the type is defined to be bind(c) it then needs to make
   11061              :          sure that all fields are interoperable.  This will
   11062              :          need to be a semantic check on the finished derived type.
   11063              :          See 15.2.3 (lines 9-12) of F2003 draft.  */
   11064          407 :       if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
   11065            0 :         return MATCH_ERROR;
   11066              : 
   11067              :       /* TODO: attr conflicts need to be checked, probably in symbol.cc.  */
   11068              :     }
   11069        18844 :   else if (gfc_match (" , abstract") == MATCH_YES)
   11070              :     {
   11071          343 :       if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
   11072              :         return MATCH_ERROR;
   11073              : 
   11074          342 :       if (!gfc_add_abstract (attr, &gfc_current_locus))
   11075            1 :         return MATCH_ERROR;
   11076              :     }
   11077        18501 :   else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
   11078              :     {
   11079         1552 :       if (!gfc_add_extension (attr, &gfc_current_locus))
   11080            0 :         return MATCH_ERROR;
   11081              :     }
   11082              :   else
   11083              :     return MATCH_NO;
   11084              : 
   11085              :   /* If we get here, something matched.  */
   11086              :   return MATCH_YES;
   11087              : }
   11088              : 
   11089              : 
   11090              : /* Common function for type declaration blocks similar to derived types, such
   11091              :    as STRUCTURES and MAPs. Unlike derived types, a structure type
   11092              :    does NOT have a generic symbol matching the name given by the user.
   11093              :    STRUCTUREs can share names with variables and PARAMETERs so we must allow
   11094              :    for the creation of an independent symbol.
   11095              :    Other parameters are a message to prefix errors with, the name of the new
   11096              :    type to be created, and the flavor to add to the resulting symbol. */
   11097              : 
   11098              : static bool
   11099          717 : get_struct_decl (const char *name, sym_flavor fl, locus *decl,
   11100              :                  gfc_symbol **result)
   11101              : {
   11102          717 :   gfc_symbol *sym;
   11103          717 :   locus where;
   11104              : 
   11105          717 :   gcc_assert (name[0] == (char) TOUPPER (name[0]));
   11106              : 
   11107          717 :   if (decl)
   11108          717 :     where = *decl;
   11109              :   else
   11110            0 :     where = gfc_current_locus;
   11111              : 
   11112          717 :   if (gfc_get_symbol (name, NULL, &sym))
   11113              :     return false;
   11114              : 
   11115          717 :   if (!sym)
   11116              :     {
   11117            0 :       gfc_internal_error ("Failed to create structure type '%s' at %C", name);
   11118              :       return false;
   11119              :     }
   11120              : 
   11121          717 :   if (sym->components != NULL || sym->attr.zero_comp)
   11122              :     {
   11123            3 :       gfc_error ("Type definition of %qs at %C was already defined at %L",
   11124              :                  sym->name, &sym->declared_at);
   11125            3 :       return false;
   11126              :     }
   11127              : 
   11128          714 :   sym->declared_at = where;
   11129              : 
   11130          714 :   if (sym->attr.flavor != fl
   11131          714 :       && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
   11132              :     return false;
   11133              : 
   11134          714 :   if (!sym->hash_value)
   11135              :       /* Set the hash for the compound name for this type.  */
   11136          713 :     sym->hash_value = gfc_hash_value (sym);
   11137              : 
   11138              :   /* Normally the type is expected to have been completely parsed by the time
   11139              :      a field declaration with this type is seen. For unions, maps, and nested
   11140              :      structure declarations, we need to indicate that it is okay that we
   11141              :      haven't seen any components yet. This will be updated after the structure
   11142              :      is fully parsed. */
   11143          714 :   sym->attr.zero_comp = 0;
   11144              : 
   11145              :   /* Structures always act like derived-types with the SEQUENCE attribute */
   11146          714 :   gfc_add_sequence (&sym->attr, sym->name, NULL);
   11147              : 
   11148          714 :   if (result) *result = sym;
   11149              : 
   11150              :   return true;
   11151              : }
   11152              : 
   11153              : 
   11154              : /* Match the opening of a MAP block. Like a struct within a union in C;
   11155              :    behaves identical to STRUCTURE blocks.  */
   11156              : 
   11157              : match
   11158          259 : gfc_match_map (void)
   11159              : {
   11160              :   /* Counter used to give unique internal names to map structures. */
   11161          259 :   static unsigned int gfc_map_id = 0;
   11162          259 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11163          259 :   gfc_symbol *sym;
   11164          259 :   locus old_loc;
   11165              : 
   11166          259 :   old_loc = gfc_current_locus;
   11167              : 
   11168          259 :   if (gfc_match_eos () != MATCH_YES)
   11169              :     {
   11170            1 :         gfc_error ("Junk after MAP statement at %C");
   11171            1 :         gfc_current_locus = old_loc;
   11172            1 :         return MATCH_ERROR;
   11173              :     }
   11174              : 
   11175              :   /* Map blocks are anonymous so we make up unique names for the symbol table
   11176              :      which are invalid Fortran identifiers.  */
   11177          258 :   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
   11178              : 
   11179          258 :   if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
   11180              :     return MATCH_ERROR;
   11181              : 
   11182          258 :   gfc_new_block = sym;
   11183              : 
   11184          258 :   return MATCH_YES;
   11185              : }
   11186              : 
   11187              : 
   11188              : /* Match the opening of a UNION block.  */
   11189              : 
   11190              : match
   11191          133 : gfc_match_union (void)
   11192              : {
   11193              :   /* Counter used to give unique internal names to union types. */
   11194          133 :   static unsigned int gfc_union_id = 0;
   11195          133 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11196          133 :   gfc_symbol *sym;
   11197          133 :   locus old_loc;
   11198              : 
   11199          133 :   old_loc = gfc_current_locus;
   11200              : 
   11201          133 :   if (gfc_match_eos () != MATCH_YES)
   11202              :     {
   11203            1 :         gfc_error ("Junk after UNION statement at %C");
   11204            1 :         gfc_current_locus = old_loc;
   11205            1 :         return MATCH_ERROR;
   11206              :     }
   11207              : 
   11208              :   /* Unions are anonymous so we make up unique names for the symbol table
   11209              :      which are invalid Fortran identifiers.  */
   11210          132 :   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
   11211              : 
   11212          132 :   if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
   11213              :     return MATCH_ERROR;
   11214              : 
   11215          132 :   gfc_new_block = sym;
   11216              : 
   11217          132 :   return MATCH_YES;
   11218              : }
   11219              : 
   11220              : 
   11221              : /* Match the beginning of a STRUCTURE declaration. This is similar to
   11222              :    matching the beginning of a derived type declaration with a few
   11223              :    twists. The resulting type symbol has no access control or other
   11224              :    interesting attributes.  */
   11225              : 
   11226              : match
   11227          336 : gfc_match_structure_decl (void)
   11228              : {
   11229              :   /* Counter used to give unique internal names to anonymous structures.  */
   11230          336 :   static unsigned int gfc_structure_id = 0;
   11231          336 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11232          336 :   gfc_symbol *sym;
   11233          336 :   match m;
   11234          336 :   locus where;
   11235              : 
   11236          336 :   if (!flag_dec_structure)
   11237              :     {
   11238            3 :       gfc_error ("%s at %C is a DEC extension, enable with "
   11239              :                  "%<-fdec-structure%>",
   11240              :                  "STRUCTURE");
   11241            3 :       return MATCH_ERROR;
   11242              :     }
   11243              : 
   11244          333 :   name[0] = '\0';
   11245              : 
   11246          333 :   m = gfc_match (" /%n/", name);
   11247          333 :   if (m != MATCH_YES)
   11248              :     {
   11249              :       /* Non-nested structure declarations require a structure name.  */
   11250           24 :       if (!gfc_comp_struct (gfc_current_state ()))
   11251              :         {
   11252            4 :             gfc_error ("Structure name expected in non-nested structure "
   11253              :                        "declaration at %C");
   11254            4 :             return MATCH_ERROR;
   11255              :         }
   11256              :       /* This is an anonymous structure; make up a unique name for it
   11257              :          (upper-case letters never make it to symbol names from the source).
   11258              :          The important thing is initializing the type variable
   11259              :          and setting gfc_new_symbol, which is immediately used by
   11260              :          parse_structure () and variable_decl () to add components of
   11261              :          this type.  */
   11262           20 :       snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
   11263              :     }
   11264              : 
   11265          329 :   where = gfc_current_locus;
   11266              :   /* No field list allowed after non-nested structure declaration.  */
   11267          329 :   if (!gfc_comp_struct (gfc_current_state ())
   11268          296 :       && gfc_match_eos () != MATCH_YES)
   11269              :     {
   11270            1 :       gfc_error ("Junk after non-nested STRUCTURE statement at %C");
   11271            1 :       return MATCH_ERROR;
   11272              :     }
   11273              : 
   11274              :   /* Make sure the name is not the name of an intrinsic type.  */
   11275          328 :   if (gfc_is_intrinsic_typename (name))
   11276              :     {
   11277            1 :       gfc_error ("Structure name %qs at %C cannot be the same as an"
   11278              :                  " intrinsic type", name);
   11279            1 :       return MATCH_ERROR;
   11280              :     }
   11281              : 
   11282              :   /* Store the actual type symbol for the structure with an upper-case first
   11283              :      letter (an invalid Fortran identifier).  */
   11284              : 
   11285          327 :   if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
   11286              :     return MATCH_ERROR;
   11287              : 
   11288          324 :   gfc_new_block = sym;
   11289          324 :   return MATCH_YES;
   11290              : }
   11291              : 
   11292              : 
   11293              : /* This function does some work to determine which matcher should be used to
   11294              :  * match a statement beginning with "TYPE".  This is used to disambiguate TYPE
   11295              :  * as an alias for PRINT from derived type declarations, TYPE IS statements,
   11296              :  * and [parameterized] derived type declarations.  */
   11297              : 
   11298              : match
   11299       534792 : gfc_match_type (gfc_statement *st)
   11300              : {
   11301       534792 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11302       534792 :   match m;
   11303       534792 :   locus old_loc;
   11304              : 
   11305              :   /* Requires -fdec.  */
   11306       534792 :   if (!flag_dec)
   11307              :     return MATCH_NO;
   11308              : 
   11309         2483 :   m = gfc_match ("type");
   11310         2483 :   if (m != MATCH_YES)
   11311              :     return m;
   11312              :   /* If we already have an error in the buffer, it is probably from failing to
   11313              :    * match a derived type data declaration. Let it happen.  */
   11314           20 :   else if (gfc_error_flag_test ())
   11315              :     return MATCH_NO;
   11316              : 
   11317           20 :   old_loc = gfc_current_locus;
   11318           20 :   *st = ST_NONE;
   11319              : 
   11320              :   /* If we see an attribute list before anything else it's definitely a derived
   11321              :    * type declaration.  */
   11322           20 :   if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
   11323            8 :     goto derived;
   11324              : 
   11325              :   /* By now "TYPE" has already been matched. If we do not see a name, this may
   11326              :    * be something like "TYPE *" or "TYPE <fmt>".  */
   11327           12 :   m = gfc_match_name (name);
   11328           12 :   if (m != MATCH_YES)
   11329              :     {
   11330              :       /* Let print match if it can, otherwise throw an error from
   11331              :        * gfc_match_derived_decl.  */
   11332            7 :       gfc_current_locus = old_loc;
   11333            7 :       if (gfc_match_print () == MATCH_YES)
   11334              :         {
   11335            7 :           *st = ST_WRITE;
   11336            7 :           return MATCH_YES;
   11337              :         }
   11338            0 :       goto derived;
   11339              :     }
   11340              : 
   11341              :   /* Check for EOS.  */
   11342            5 :   if (gfc_match_eos () == MATCH_YES)
   11343              :     {
   11344              :       /* By now we have "TYPE <name> <EOS>". Check first if the name is an
   11345              :        * intrinsic typename - if so let gfc_match_derived_decl dump an error.
   11346              :        * Otherwise if gfc_match_derived_decl fails it's probably an existing
   11347              :        * symbol which can be printed.  */
   11348            3 :       gfc_current_locus = old_loc;
   11349            3 :       m = gfc_match_derived_decl ();
   11350            3 :       if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
   11351              :         {
   11352            2 :           *st = ST_DERIVED_DECL;
   11353            2 :           return m;
   11354              :         }
   11355              :     }
   11356              :   else
   11357              :     {
   11358              :       /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
   11359              :          like <type name(parameter)>.  */
   11360            2 :       gfc_gobble_whitespace ();
   11361            2 :       bool paren = gfc_peek_ascii_char () == '(';
   11362            2 :       if (paren)
   11363              :         {
   11364            1 :           if (strcmp ("is", name) == 0)
   11365            1 :             goto typeis;
   11366              :           else
   11367            0 :             goto derived;
   11368              :         }
   11369              :     }
   11370              : 
   11371              :   /* Treat TYPE... like PRINT...  */
   11372            2 :   gfc_current_locus = old_loc;
   11373            2 :   *st = ST_WRITE;
   11374            2 :   return gfc_match_print ();
   11375              : 
   11376            8 : derived:
   11377            8 :   gfc_current_locus = old_loc;
   11378            8 :   *st = ST_DERIVED_DECL;
   11379            8 :   return gfc_match_derived_decl ();
   11380              : 
   11381            1 : typeis:
   11382            1 :   gfc_current_locus = old_loc;
   11383            1 :   *st = ST_TYPE_IS;
   11384            1 :   return gfc_match_type_is ();
   11385              : }
   11386              : 
   11387              : 
   11388              : /* Match the beginning of a derived type declaration.  If a type name
   11389              :    was the result of a function, then it is possible to have a symbol
   11390              :    already to be known as a derived type yet have no components.  */
   11391              : 
   11392              : match
   11393        16956 : gfc_match_derived_decl (void)
   11394              : {
   11395        16956 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11396        16956 :   char parent[GFC_MAX_SYMBOL_LEN + 1];
   11397        16956 :   symbol_attribute attr;
   11398        16956 :   gfc_symbol *sym, *gensym;
   11399        16956 :   gfc_symbol *extended;
   11400        16956 :   match m;
   11401        16956 :   match is_type_attr_spec = MATCH_NO;
   11402        16956 :   bool seen_attr = false;
   11403        16956 :   gfc_interface *intr = NULL, *head;
   11404        16956 :   bool parameterized_type = false;
   11405        16956 :   bool seen_colons = false;
   11406              : 
   11407        16956 :   if (gfc_comp_struct (gfc_current_state ()))
   11408              :     return MATCH_NO;
   11409              : 
   11410        16952 :   name[0] = '\0';
   11411        16952 :   parent[0] = '\0';
   11412        16952 :   gfc_clear_attr (&attr);
   11413        16952 :   extended = NULL;
   11414              : 
   11415        19824 :   do
   11416              :     {
   11417        19824 :       is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
   11418        19824 :       if (is_type_attr_spec == MATCH_ERROR)
   11419              :         return MATCH_ERROR;
   11420        19821 :       if (is_type_attr_spec == MATCH_YES)
   11421         2872 :         seen_attr = true;
   11422        19821 :     } while (is_type_attr_spec == MATCH_YES);
   11423              : 
   11424              :   /* Deal with derived type extensions.  The extension attribute has
   11425              :      been added to 'attr' but now the parent type must be found and
   11426              :      checked.  */
   11427        16949 :   if (parent[0])
   11428         1551 :     extended = check_extended_derived_type (parent);
   11429              : 
   11430        16949 :   if (parent[0] && !extended)
   11431              :     return MATCH_ERROR;
   11432              : 
   11433        16945 :   m = gfc_match (" ::");
   11434        16945 :   if (m == MATCH_YES)
   11435              :     {
   11436              :       seen_colons = true;
   11437              :     }
   11438        10602 :   else if (seen_attr)
   11439              :     {
   11440            5 :       gfc_error ("Expected :: in TYPE definition at %C");
   11441            5 :       return MATCH_ERROR;
   11442              :     }
   11443              : 
   11444              :   /*  In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
   11445              :       But, we need to simply return for TYPE(.  */
   11446        10597 :   if (m == MATCH_NO && gfc_current_form == FORM_FREE)
   11447              :     {
   11448        10548 :       char c = gfc_peek_ascii_char ();
   11449        10548 :       if (c == '(')
   11450              :         return m;
   11451        10467 :       if (!gfc_is_whitespace (c))
   11452              :         {
   11453            4 :           gfc_error ("Mangled derived type definition at %C");
   11454            4 :           return MATCH_NO;
   11455              :         }
   11456              :     }
   11457              : 
   11458        16855 :   m = gfc_match (" %n ", name);
   11459        16855 :   if (m != MATCH_YES)
   11460              :     return m;
   11461              : 
   11462              :   /* Make sure that we don't identify TYPE IS (...) as a parameterized
   11463              :      derived type named 'is'.
   11464              :      TODO Expand the check, when 'name' = "is" by matching " (tname) "
   11465              :      and checking if this is a(n intrinsic) typename.  This picks up
   11466              :      misplaced TYPE IS statements such as in select_type_1.f03.  */
   11467        16843 :   if (gfc_peek_ascii_char () == '(')
   11468              :     {
   11469         3995 :       if (gfc_current_state () == COMP_SELECT_TYPE
   11470          459 :           || (!seen_colons && !strcmp (name, "is")))
   11471              :         return MATCH_NO;
   11472              :       parameterized_type = true;
   11473              :     }
   11474              : 
   11475        13305 :   m = gfc_match_eos ();
   11476        13305 :   if (m != MATCH_YES && !parameterized_type)
   11477              :     return m;
   11478              : 
   11479              :   /* Make sure the name is not the name of an intrinsic type.  */
   11480        13302 :   if (gfc_is_intrinsic_typename (name))
   11481              :     {
   11482           18 :       gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
   11483              :                  "type", name);
   11484           18 :       return MATCH_ERROR;
   11485              :     }
   11486              : 
   11487        13284 :   if (gfc_get_symbol (name, NULL, &gensym))
   11488              :     return MATCH_ERROR;
   11489              : 
   11490        13284 :   if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
   11491              :     {
   11492            5 :       if (gensym->ts.u.derived)
   11493            0 :         gfc_error ("Derived type name %qs at %C already has a basic type "
   11494              :                    "of %s", gensym->name, gfc_typename (&gensym->ts));
   11495              :       else
   11496            5 :         gfc_error ("Derived type name %qs at %C already has a basic type",
   11497              :                    gensym->name);
   11498              :       return MATCH_ERROR;
   11499              :     }
   11500              : 
   11501        13279 :   if (!gensym->attr.generic
   11502        13279 :       && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
   11503              :     return MATCH_ERROR;
   11504              : 
   11505        13275 :   if (!gensym->attr.function
   11506        13275 :       && !gfc_add_function (&gensym->attr, gensym->name, NULL))
   11507              :     return MATCH_ERROR;
   11508              : 
   11509        13274 :   if (gensym->attr.dummy)
   11510              :     {
   11511            1 :       gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
   11512              :                  name, &gensym->declared_at);
   11513            1 :       return MATCH_ERROR;
   11514              :     }
   11515              : 
   11516        13273 :   sym = gfc_find_dt_in_generic (gensym);
   11517              : 
   11518        13273 :   if (sym && (sym->components != NULL || sym->attr.zero_comp))
   11519              :     {
   11520            1 :       gfc_error ("Derived type definition of %qs at %C has already been "
   11521              :                  "defined", sym->name);
   11522            1 :       return MATCH_ERROR;
   11523              :     }
   11524              : 
   11525        13272 :   if (!sym)
   11526              :     {
   11527              :       /* Use upper case to save the actual derived-type symbol.  */
   11528        13182 :       gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
   11529        13182 :       sym->name = gfc_get_string ("%s", gensym->name);
   11530        13182 :       head = gensym->generic;
   11531        13182 :       intr = gfc_get_interface ();
   11532        13182 :       intr->sym = sym;
   11533        13182 :       intr->where = gfc_current_locus;
   11534        13182 :       intr->sym->declared_at = gfc_current_locus;
   11535        13182 :       intr->next = head;
   11536        13182 :       gensym->generic = intr;
   11537        13182 :       gensym->attr.if_source = IFSRC_DECL;
   11538              :     }
   11539              : 
   11540              :   /* The symbol may already have the derived attribute without the
   11541              :      components.  The ways this can happen is via a function
   11542              :      definition, an INTRINSIC statement or a subtype in another
   11543              :      derived type that is a pointer.  The first part of the AND clause
   11544              :      is true if the symbol is not the return value of a function.  */
   11545        13272 :   if (sym->attr.flavor != FL_DERIVED
   11546        13272 :       && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
   11547              :     return MATCH_ERROR;
   11548              : 
   11549        13272 :   if (attr.access != ACCESS_UNKNOWN
   11550        13272 :       && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
   11551              :     return MATCH_ERROR;
   11552        13272 :   else if (sym->attr.access == ACCESS_UNKNOWN
   11553        12704 :            && gensym->attr.access != ACCESS_UNKNOWN
   11554        13620 :            && !gfc_add_access (&sym->attr, gensym->attr.access,
   11555              :                                sym->name, NULL))
   11556              :     return MATCH_ERROR;
   11557              : 
   11558        13272 :   if (sym->attr.access != ACCESS_UNKNOWN
   11559          916 :       && gensym->attr.access == ACCESS_UNKNOWN)
   11560          568 :     gensym->attr.access = sym->attr.access;
   11561              : 
   11562              :   /* See if the derived type was labeled as bind(c).  */
   11563        13272 :   if (attr.is_bind_c != 0)
   11564          404 :     sym->attr.is_bind_c = attr.is_bind_c;
   11565              : 
   11566              :   /* Construct the f2k_derived namespace if it is not yet there.  */
   11567        13272 :   if (!sym->f2k_derived)
   11568        13272 :     sym->f2k_derived = gfc_get_namespace (NULL, 0);
   11569              : 
   11570        13272 :   if (parameterized_type)
   11571              :     {
   11572              :       /* Ignore error or mismatches by going to the end of the statement
   11573              :          in order to avoid the component declarations causing problems.  */
   11574          457 :       m = gfc_match_formal_arglist (sym, 0, 0, true);
   11575          457 :       if (m != MATCH_YES)
   11576            4 :         gfc_error_recovery ();
   11577              :       else
   11578          453 :         sym->attr.pdt_template = 1;
   11579          457 :       m = gfc_match_eos ();
   11580          457 :       if (m != MATCH_YES)
   11581              :         {
   11582            1 :           gfc_error_recovery ();
   11583            1 :           gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
   11584              :         }
   11585              :     }
   11586              : 
   11587        13272 :   if (extended && !sym->components)
   11588              :     {
   11589         1547 :       gfc_component *p;
   11590         1547 :       gfc_formal_arglist *f, *g, *h;
   11591              : 
   11592              :       /* Add the extended derived type as the first component.  */
   11593         1547 :       gfc_add_component (sym, parent, &p);
   11594         1547 :       extended->refs++;
   11595         1547 :       gfc_set_sym_referenced (extended);
   11596              : 
   11597         1547 :       p->ts.type = BT_DERIVED;
   11598         1547 :       p->ts.u.derived = extended;
   11599         1547 :       p->initializer = gfc_default_initializer (&p->ts);
   11600              : 
   11601              :       /* Set extension level.  */
   11602         1547 :       if (extended->attr.extension == 255)
   11603              :         {
   11604              :           /* Since the extension field is 8 bit wide, we can only have
   11605              :              up to 255 extension levels.  */
   11606            0 :           gfc_error ("Maximum extension level reached with type %qs at %L",
   11607              :                      extended->name, &extended->declared_at);
   11608            0 :           return MATCH_ERROR;
   11609              :         }
   11610         1547 :       sym->attr.extension = extended->attr.extension + 1;
   11611              : 
   11612              :       /* Provide the links between the extended type and its extension.  */
   11613         1547 :       if (!extended->f2k_derived)
   11614            1 :         extended->f2k_derived = gfc_get_namespace (NULL, 0);
   11615              : 
   11616              :       /* Copy the extended type-param-name-list from the extended type,
   11617              :          append those of the extension and add the whole lot to the
   11618              :          extension.  */
   11619         1547 :       if (extended->attr.pdt_template)
   11620              :         {
   11621           40 :           g = h = NULL;
   11622           40 :           sym->attr.pdt_template = 1;
   11623          111 :           for (f = extended->formal; f; f = f->next)
   11624              :             {
   11625           71 :               if (f == extended->formal)
   11626              :                 {
   11627           40 :                   g = gfc_get_formal_arglist ();
   11628           40 :                   h = g;
   11629              :                 }
   11630              :               else
   11631              :                 {
   11632           31 :                   g->next = gfc_get_formal_arglist ();
   11633           31 :                   g = g->next;
   11634              :                 }
   11635           71 :               g->sym = f->sym;
   11636              :             }
   11637           40 :           g->next = sym->formal;
   11638           40 :           sym->formal = h;
   11639              :         }
   11640              :     }
   11641              : 
   11642        13272 :   if (!sym->hash_value)
   11643              :     /* Set the hash for the compound name for this type.  */
   11644        13272 :     sym->hash_value = gfc_hash_value (sym);
   11645              : 
   11646              :   /* Take over the ABSTRACT attribute.  */
   11647        13272 :   sym->attr.abstract = attr.abstract;
   11648              : 
   11649        13272 :   gfc_new_block = sym;
   11650              : 
   11651        13272 :   return MATCH_YES;
   11652              : }
   11653              : 
   11654              : 
   11655              : /* Cray Pointees can be declared as:
   11656              :       pointer (ipt, a (n,m,...,*))  */
   11657              : 
   11658              : match
   11659          240 : gfc_mod_pointee_as (gfc_array_spec *as)
   11660              : {
   11661          240 :   as->cray_pointee = true; /* This will be useful to know later.  */
   11662          240 :   if (as->type == AS_ASSUMED_SIZE)
   11663           72 :     as->cp_was_assumed = true;
   11664          168 :   else if (as->type == AS_ASSUMED_SHAPE)
   11665              :     {
   11666            0 :       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
   11667            0 :       return MATCH_ERROR;
   11668              :     }
   11669              :   return MATCH_YES;
   11670              : }
   11671              : 
   11672              : 
   11673              : /* Match the enum definition statement, here we are trying to match
   11674              :    the first line of enum definition statement.
   11675              :    Returns MATCH_YES if match is found.  */
   11676              : 
   11677              : match
   11678          158 : gfc_match_enum (void)
   11679              : {
   11680          158 :   match m;
   11681              : 
   11682          158 :   m = gfc_match_eos ();
   11683          158 :   if (m != MATCH_YES)
   11684              :     return m;
   11685              : 
   11686          158 :   if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
   11687            0 :     return MATCH_ERROR;
   11688              : 
   11689              :   return MATCH_YES;
   11690              : }
   11691              : 
   11692              : 
   11693              : /* Returns an initializer whose value is one higher than the value of the
   11694              :    LAST_INITIALIZER argument.  If the argument is NULL, the
   11695              :    initializers value will be set to zero.  The initializer's kind
   11696              :    will be set to gfc_c_int_kind.
   11697              : 
   11698              :    If -fshort-enums is given, the appropriate kind will be selected
   11699              :    later after all enumerators have been parsed.  A warning is issued
   11700              :    here if an initializer exceeds gfc_c_int_kind.  */
   11701              : 
   11702              : static gfc_expr *
   11703          377 : enum_initializer (gfc_expr *last_initializer, locus where)
   11704              : {
   11705          377 :   gfc_expr *result;
   11706          377 :   result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
   11707              : 
   11708          377 :   mpz_init (result->value.integer);
   11709              : 
   11710          377 :   if (last_initializer != NULL)
   11711              :     {
   11712          266 :       mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
   11713          266 :       result->where = last_initializer->where;
   11714              : 
   11715          266 :       if (gfc_check_integer_range (result->value.integer,
   11716              :              gfc_c_int_kind) != ARITH_OK)
   11717              :         {
   11718            0 :           gfc_error ("Enumerator exceeds the C integer type at %C");
   11719            0 :           return NULL;
   11720              :         }
   11721              :     }
   11722              :   else
   11723              :     {
   11724              :       /* Control comes here, if it's the very first enumerator and no
   11725              :          initializer has been given.  It will be initialized to zero.  */
   11726          111 :       mpz_set_si (result->value.integer, 0);
   11727              :     }
   11728              : 
   11729              :   return result;
   11730              : }
   11731              : 
   11732              : 
   11733              : /* Match a variable name with an optional initializer.  When this
   11734              :    subroutine is called, a variable is expected to be parsed next.
   11735              :    Depending on what is happening at the moment, updates either the
   11736              :    symbol table or the current interface.  */
   11737              : 
   11738              : static match
   11739          549 : enumerator_decl (void)
   11740              : {
   11741          549 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11742          549 :   gfc_expr *initializer;
   11743          549 :   gfc_array_spec *as = NULL;
   11744          549 :   gfc_charlen *saved_cl_list;
   11745          549 :   gfc_symbol *sym;
   11746          549 :   locus var_locus;
   11747          549 :   match m;
   11748          549 :   bool t;
   11749          549 :   locus old_locus;
   11750              : 
   11751          549 :   initializer = NULL;
   11752          549 :   saved_cl_list = gfc_current_ns->cl_list;
   11753          549 :   old_locus = gfc_current_locus;
   11754              : 
   11755              :   /* When we get here, we've just matched a list of attributes and
   11756              :      maybe a type and a double colon.  The next thing we expect to see
   11757              :      is the name of the symbol.  */
   11758          549 :   m = gfc_match_name (name);
   11759          549 :   if (m != MATCH_YES)
   11760            1 :     goto cleanup;
   11761              : 
   11762          548 :   var_locus = gfc_current_locus;
   11763              : 
   11764              :   /* OK, we've successfully matched the declaration.  Now put the
   11765              :      symbol in the current namespace. If we fail to create the symbol,
   11766              :      bail out.  */
   11767          548 :   if (!build_sym (name, 1, NULL, false, &as, &var_locus))
   11768              :     {
   11769            1 :       m = MATCH_ERROR;
   11770            1 :       goto cleanup;
   11771              :     }
   11772              : 
   11773              :   /* The double colon must be present in order to have initializers.
   11774              :      Otherwise the statement is ambiguous with an assignment statement.  */
   11775          547 :   if (colon_seen)
   11776              :     {
   11777          471 :       if (gfc_match_char ('=') == MATCH_YES)
   11778              :         {
   11779          170 :           m = gfc_match_init_expr (&initializer);
   11780          170 :           if (m == MATCH_NO)
   11781              :             {
   11782            0 :               gfc_error ("Expected an initialization expression at %C");
   11783            0 :               m = MATCH_ERROR;
   11784              :             }
   11785              : 
   11786          170 :           if (m != MATCH_YES)
   11787            2 :             goto cleanup;
   11788              :         }
   11789              :     }
   11790              : 
   11791              :   /* If we do not have an initializer, the initialization value of the
   11792              :      previous enumerator (stored in last_initializer) is incremented
   11793              :      by 1 and is used to initialize the current enumerator.  */
   11794          545 :   if (initializer == NULL)
   11795          377 :     initializer = enum_initializer (last_initializer, old_locus);
   11796              : 
   11797          545 :   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
   11798              :     {
   11799            2 :       gfc_error ("ENUMERATOR %L not initialized with integer expression",
   11800              :                  &var_locus);
   11801            2 :       m = MATCH_ERROR;
   11802            2 :       goto cleanup;
   11803              :     }
   11804              : 
   11805              :   /* Store this current initializer, for the next enumerator variable
   11806              :      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
   11807              :      use last_initializer below.  */
   11808          543 :   last_initializer = initializer;
   11809          543 :   t = add_init_expr_to_sym (name, &initializer, &var_locus,
   11810              :                             saved_cl_list);
   11811              : 
   11812              :   /* Maintain enumerator history.  */
   11813          543 :   gfc_find_symbol (name, NULL, 0, &sym);
   11814          543 :   create_enum_history (sym, last_initializer);
   11815              : 
   11816          543 :   return (t) ? MATCH_YES : MATCH_ERROR;
   11817              : 
   11818            6 : cleanup:
   11819              :   /* Free stuff up and return.  */
   11820            6 :   gfc_free_expr (initializer);
   11821              : 
   11822            6 :   return m;
   11823              : }
   11824              : 
   11825              : 
   11826              : /* Match the enumerator definition statement.  */
   11827              : 
   11828              : match
   11829       815696 : gfc_match_enumerator_def (void)
   11830              : {
   11831       815696 :   match m;
   11832       815696 :   bool t;
   11833              : 
   11834       815696 :   gfc_clear_ts (&current_ts);
   11835              : 
   11836       815696 :   m = gfc_match (" enumerator");
   11837       815696 :   if (m != MATCH_YES)
   11838              :     return m;
   11839              : 
   11840          269 :   m = gfc_match (" :: ");
   11841          269 :   if (m == MATCH_ERROR)
   11842              :     return m;
   11843              : 
   11844          269 :   colon_seen = (m == MATCH_YES);
   11845              : 
   11846          269 :   if (gfc_current_state () != COMP_ENUM)
   11847              :     {
   11848            4 :       gfc_error ("ENUM definition statement expected before %C");
   11849            4 :       gfc_free_enum_history ();
   11850            4 :       return MATCH_ERROR;
   11851              :     }
   11852              : 
   11853          265 :   (&current_ts)->type = BT_INTEGER;
   11854          265 :   (&current_ts)->kind = gfc_c_int_kind;
   11855              : 
   11856          265 :   gfc_clear_attr (&current_attr);
   11857          265 :   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
   11858          265 :   if (!t)
   11859              :     {
   11860            0 :       m = MATCH_ERROR;
   11861            0 :       goto cleanup;
   11862              :     }
   11863              : 
   11864          549 :   for (;;)
   11865              :     {
   11866          549 :       m = enumerator_decl ();
   11867          549 :       if (m == MATCH_ERROR)
   11868              :         {
   11869            6 :           gfc_free_enum_history ();
   11870            6 :           goto cleanup;
   11871              :         }
   11872          543 :       if (m == MATCH_NO)
   11873              :         break;
   11874              : 
   11875          542 :       if (gfc_match_eos () == MATCH_YES)
   11876          256 :         goto cleanup;
   11877          286 :       if (gfc_match_char (',') != MATCH_YES)
   11878              :         break;
   11879              :     }
   11880              : 
   11881            3 :   if (gfc_current_state () == COMP_ENUM)
   11882              :     {
   11883            3 :       gfc_free_enum_history ();
   11884            3 :       gfc_error ("Syntax error in ENUMERATOR definition at %C");
   11885            3 :       m = MATCH_ERROR;
   11886              :     }
   11887              : 
   11888            0 : cleanup:
   11889          265 :   gfc_free_array_spec (current_as);
   11890          265 :   current_as = NULL;
   11891          265 :   return m;
   11892              : 
   11893              : }
   11894              : 
   11895              : 
   11896              : /* Match binding attributes.  */
   11897              : 
   11898              : static match
   11899         4726 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
   11900              : {
   11901         4726 :   bool found_passing = false;
   11902         4726 :   bool seen_ptr = false;
   11903         4726 :   match m = MATCH_YES;
   11904              : 
   11905              :   /* Initialize to defaults.  Do so even before the MATCH_NO check so that in
   11906              :      this case the defaults are in there.  */
   11907         4726 :   ba->access = ACCESS_UNKNOWN;
   11908         4726 :   ba->pass_arg = NULL;
   11909         4726 :   ba->pass_arg_num = 0;
   11910         4726 :   ba->nopass = 0;
   11911         4726 :   ba->non_overridable = 0;
   11912         4726 :   ba->deferred = 0;
   11913         4726 :   ba->ppc = ppc;
   11914              : 
   11915              :   /* If we find a comma, we believe there are binding attributes.  */
   11916         4726 :   m = gfc_match_char (',');
   11917         4726 :   if (m == MATCH_NO)
   11918         2482 :     goto done;
   11919              : 
   11920         2793 :   do
   11921              :     {
   11922              :       /* Access specifier.  */
   11923              : 
   11924         2793 :       m = gfc_match (" public");
   11925         2793 :       if (m == MATCH_ERROR)
   11926            0 :         goto error;
   11927         2793 :       if (m == MATCH_YES)
   11928              :         {
   11929          250 :           if (ba->access != ACCESS_UNKNOWN)
   11930              :             {
   11931            0 :               gfc_error ("Duplicate access-specifier at %C");
   11932            0 :               goto error;
   11933              :             }
   11934              : 
   11935          250 :           ba->access = ACCESS_PUBLIC;
   11936          250 :           continue;
   11937              :         }
   11938              : 
   11939         2543 :       m = gfc_match (" private");
   11940         2543 :       if (m == MATCH_ERROR)
   11941            0 :         goto error;
   11942         2543 :       if (m == MATCH_YES)
   11943              :         {
   11944          181 :           if (ba->access != ACCESS_UNKNOWN)
   11945              :             {
   11946            1 :               gfc_error ("Duplicate access-specifier at %C");
   11947            1 :               goto error;
   11948              :             }
   11949              : 
   11950          180 :           ba->access = ACCESS_PRIVATE;
   11951          180 :           continue;
   11952              :         }
   11953              : 
   11954              :       /* If inside GENERIC, the following is not allowed.  */
   11955         2362 :       if (!generic)
   11956              :         {
   11957              : 
   11958              :           /* NOPASS flag.  */
   11959         2361 :           m = gfc_match (" nopass");
   11960         2361 :           if (m == MATCH_ERROR)
   11961            0 :             goto error;
   11962         2361 :           if (m == MATCH_YES)
   11963              :             {
   11964          707 :               if (found_passing)
   11965              :                 {
   11966            1 :                   gfc_error ("Binding attributes already specify passing,"
   11967              :                              " illegal NOPASS at %C");
   11968            1 :                   goto error;
   11969              :                 }
   11970              : 
   11971          706 :               found_passing = true;
   11972          706 :               ba->nopass = 1;
   11973          706 :               continue;
   11974              :             }
   11975              : 
   11976              :           /* PASS possibly including argument.  */
   11977         1654 :           m = gfc_match (" pass");
   11978         1654 :           if (m == MATCH_ERROR)
   11979            0 :             goto error;
   11980         1654 :           if (m == MATCH_YES)
   11981              :             {
   11982          901 :               char arg[GFC_MAX_SYMBOL_LEN + 1];
   11983              : 
   11984          901 :               if (found_passing)
   11985              :                 {
   11986            2 :                   gfc_error ("Binding attributes already specify passing,"
   11987              :                              " illegal PASS at %C");
   11988            2 :                   goto error;
   11989              :                 }
   11990              : 
   11991          899 :               m = gfc_match (" ( %n )", arg);
   11992          899 :               if (m == MATCH_ERROR)
   11993            0 :                 goto error;
   11994          899 :               if (m == MATCH_YES)
   11995          490 :                 ba->pass_arg = gfc_get_string ("%s", arg);
   11996          899 :               gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
   11997              : 
   11998          899 :               found_passing = true;
   11999          899 :               ba->nopass = 0;
   12000          899 :               continue;
   12001          899 :             }
   12002              : 
   12003          753 :           if (ppc)
   12004              :             {
   12005              :               /* POINTER flag.  */
   12006          431 :               m = gfc_match (" pointer");
   12007          431 :               if (m == MATCH_ERROR)
   12008            0 :                 goto error;
   12009          431 :               if (m == MATCH_YES)
   12010              :                 {
   12011          431 :                   if (seen_ptr)
   12012              :                     {
   12013            1 :                       gfc_error ("Duplicate POINTER attribute at %C");
   12014            1 :                       goto error;
   12015              :                     }
   12016              : 
   12017          430 :                   seen_ptr = true;
   12018          430 :                   continue;
   12019              :                 }
   12020              :             }
   12021              :           else
   12022              :             {
   12023              :               /* NON_OVERRIDABLE flag.  */
   12024          322 :               m = gfc_match (" non_overridable");
   12025          322 :               if (m == MATCH_ERROR)
   12026            0 :                 goto error;
   12027          322 :               if (m == MATCH_YES)
   12028              :                 {
   12029           62 :                   if (ba->non_overridable)
   12030              :                     {
   12031            1 :                       gfc_error ("Duplicate NON_OVERRIDABLE at %C");
   12032            1 :                       goto error;
   12033              :                     }
   12034              : 
   12035           61 :                   ba->non_overridable = 1;
   12036           61 :                   continue;
   12037              :                 }
   12038              : 
   12039              :               /* DEFERRED flag.  */
   12040          260 :               m = gfc_match (" deferred");
   12041          260 :               if (m == MATCH_ERROR)
   12042            0 :                 goto error;
   12043          260 :               if (m == MATCH_YES)
   12044              :                 {
   12045          260 :                   if (ba->deferred)
   12046              :                     {
   12047            1 :                       gfc_error ("Duplicate DEFERRED at %C");
   12048            1 :                       goto error;
   12049              :                     }
   12050              : 
   12051          259 :                   ba->deferred = 1;
   12052          259 :                   continue;
   12053              :                 }
   12054              :             }
   12055              : 
   12056              :         }
   12057              : 
   12058              :       /* Nothing matching found.  */
   12059            1 :       if (generic)
   12060            1 :         gfc_error ("Expected access-specifier at %C");
   12061              :       else
   12062            0 :         gfc_error ("Expected binding attribute at %C");
   12063            1 :       goto error;
   12064              :     }
   12065         2785 :   while (gfc_match_char (',') == MATCH_YES);
   12066              : 
   12067              :   /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
   12068         2236 :   if (ba->non_overridable && ba->deferred)
   12069              :     {
   12070            1 :       gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
   12071            1 :       goto error;
   12072              :     }
   12073              : 
   12074              :   m = MATCH_YES;
   12075              : 
   12076         4717 : done:
   12077         4717 :   if (ba->access == ACCESS_UNKNOWN)
   12078         4288 :     ba->access = ppc ? gfc_current_block()->component_access
   12079              :                      : gfc_typebound_default_access;
   12080              : 
   12081         4717 :   if (ppc && !seen_ptr)
   12082              :     {
   12083            2 :       gfc_error ("POINTER attribute is required for procedure pointer component"
   12084              :                  " at %C");
   12085            2 :       goto error;
   12086              :     }
   12087              : 
   12088              :   return m;
   12089              : 
   12090         4726 : error:
   12091              :   return MATCH_ERROR;
   12092              : }
   12093              : 
   12094              : 
   12095              : /* Match a PROCEDURE specific binding inside a derived type.  */
   12096              : 
   12097              : static match
   12098         3242 : match_procedure_in_type (void)
   12099              : {
   12100         3242 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12101         3242 :   char target_buf[GFC_MAX_SYMBOL_LEN + 1];
   12102         3242 :   char* target = NULL, *ifc = NULL;
   12103         3242 :   gfc_typebound_proc tb;
   12104         3242 :   bool seen_colons;
   12105         3242 :   bool seen_attrs;
   12106         3242 :   match m;
   12107         3242 :   gfc_symtree* stree;
   12108         3242 :   gfc_namespace* ns;
   12109         3242 :   gfc_symbol* block;
   12110         3242 :   int num;
   12111              : 
   12112              :   /* Check current state.  */
   12113         3242 :   gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
   12114         3242 :   block = gfc_state_stack->previous->sym;
   12115         3242 :   gcc_assert (block);
   12116              : 
   12117              :   /* Try to match PROCEDURE(interface).  */
   12118         3242 :   if (gfc_match (" (") == MATCH_YES)
   12119              :     {
   12120          261 :       m = gfc_match_name (target_buf);
   12121          261 :       if (m == MATCH_ERROR)
   12122              :         return m;
   12123          261 :       if (m != MATCH_YES)
   12124              :         {
   12125            1 :           gfc_error ("Interface-name expected after %<(%> at %C");
   12126            1 :           return MATCH_ERROR;
   12127              :         }
   12128              : 
   12129          260 :       if (gfc_match (" )") != MATCH_YES)
   12130              :         {
   12131            1 :           gfc_error ("%<)%> expected at %C");
   12132            1 :           return MATCH_ERROR;
   12133              :         }
   12134              : 
   12135              :       ifc = target_buf;
   12136              :     }
   12137              : 
   12138              :   /* Construct the data structure.  */
   12139         3240 :   memset (&tb, 0, sizeof (tb));
   12140         3240 :   tb.where = gfc_current_locus;
   12141              : 
   12142              :   /* Match binding attributes.  */
   12143         3240 :   m = match_binding_attributes (&tb, false, false);
   12144         3240 :   if (m == MATCH_ERROR)
   12145              :     return m;
   12146         3233 :   seen_attrs = (m == MATCH_YES);
   12147              : 
   12148              :   /* Check that attribute DEFERRED is given if an interface is specified.  */
   12149         3233 :   if (tb.deferred && !ifc)
   12150              :     {
   12151            1 :       gfc_error ("Interface must be specified for DEFERRED binding at %C");
   12152            1 :       return MATCH_ERROR;
   12153              :     }
   12154         3232 :   if (ifc && !tb.deferred)
   12155              :     {
   12156            1 :       gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
   12157            1 :       return MATCH_ERROR;
   12158              :     }
   12159              : 
   12160              :   /* Match the colons.  */
   12161         3231 :   m = gfc_match (" ::");
   12162         3231 :   if (m == MATCH_ERROR)
   12163              :     return m;
   12164         3231 :   seen_colons = (m == MATCH_YES);
   12165         3231 :   if (seen_attrs && !seen_colons)
   12166              :     {
   12167            4 :       gfc_error ("Expected %<::%> after binding-attributes at %C");
   12168            4 :       return MATCH_ERROR;
   12169              :     }
   12170              : 
   12171              :   /* Match the binding names.  */
   12172           19 :   for(num=1;;num++)
   12173              :     {
   12174         3246 :       m = gfc_match_name (name);
   12175         3246 :       if (m == MATCH_ERROR)
   12176              :         return m;
   12177         3246 :       if (m == MATCH_NO)
   12178              :         {
   12179            5 :           gfc_error ("Expected binding name at %C");
   12180            5 :           return MATCH_ERROR;
   12181              :         }
   12182              : 
   12183         3241 :       if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
   12184              :         return MATCH_ERROR;
   12185              : 
   12186              :       /* Try to match the '=> target', if it's there.  */
   12187         3240 :       target = ifc;
   12188         3240 :       m = gfc_match (" =>");
   12189         3240 :       if (m == MATCH_ERROR)
   12190              :         return m;
   12191         3240 :       if (m == MATCH_YES)
   12192              :         {
   12193         1250 :           if (tb.deferred)
   12194              :             {
   12195            1 :               gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
   12196            1 :               return MATCH_ERROR;
   12197              :             }
   12198              : 
   12199         1249 :           if (!seen_colons)
   12200              :             {
   12201            1 :               gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
   12202              :                          " at %C");
   12203            1 :               return MATCH_ERROR;
   12204              :             }
   12205              : 
   12206         1248 :           m = gfc_match_name (target_buf);
   12207         1248 :           if (m == MATCH_ERROR)
   12208              :             return m;
   12209         1248 :           if (m == MATCH_NO)
   12210              :             {
   12211            2 :               gfc_error ("Expected binding target after %<=>%> at %C");
   12212            2 :               return MATCH_ERROR;
   12213              :             }
   12214              :           target = target_buf;
   12215              :         }
   12216              : 
   12217              :       /* If no target was found, it has the same name as the binding.  */
   12218         1990 :       if (!target)
   12219         1735 :         target = name;
   12220              : 
   12221              :       /* Get the namespace to insert the symbols into.  */
   12222         3236 :       ns = block->f2k_derived;
   12223         3236 :       gcc_assert (ns);
   12224              : 
   12225              :       /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
   12226         3236 :       if (tb.deferred && !block->attr.abstract)
   12227              :         {
   12228            1 :           gfc_error ("Type %qs containing DEFERRED binding at %C "
   12229              :                      "is not ABSTRACT", block->name);
   12230            1 :           return MATCH_ERROR;
   12231              :         }
   12232              : 
   12233              :       /* See if we already have a binding with this name in the symtree which
   12234              :          would be an error.  If a GENERIC already targeted this binding, it may
   12235              :          be already there but then typebound is still NULL.  */
   12236         3235 :       stree = gfc_find_symtree (ns->tb_sym_root, name);
   12237         3235 :       if (stree && stree->n.tb)
   12238              :         {
   12239            2 :           gfc_error ("There is already a procedure with binding name %qs for "
   12240              :                      "the derived type %qs at %C", name, block->name);
   12241            2 :           return MATCH_ERROR;
   12242              :         }
   12243              : 
   12244              :       /* Insert it and set attributes.  */
   12245              : 
   12246         3114 :       if (!stree)
   12247              :         {
   12248         3114 :           stree = gfc_new_symtree (&ns->tb_sym_root, name);
   12249         3114 :           gcc_assert (stree);
   12250              :         }
   12251         3233 :       stree->n.tb = gfc_get_typebound_proc (&tb);
   12252              : 
   12253         3233 :       if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
   12254              :                             false))
   12255              :         return MATCH_ERROR;
   12256         3233 :       gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
   12257         3233 :       gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
   12258         3233 :                      target, &stree->n.tb->u.specific->n.sym->declared_at);
   12259              : 
   12260         3233 :       if (gfc_match_eos () == MATCH_YES)
   12261              :         return MATCH_YES;
   12262           20 :       if (gfc_match_char (',') != MATCH_YES)
   12263            1 :         goto syntax;
   12264              :     }
   12265              : 
   12266            1 : syntax:
   12267            1 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
   12268            1 :   return MATCH_ERROR;
   12269              : }
   12270              : 
   12271              : 
   12272              : /* Match a GENERIC statement.
   12273              : F2018 15.4.3.3 GENERIC statement
   12274              : 
   12275              : A GENERIC statement specifies a generic identifier for one or more specific
   12276              : procedures, in the same way as a generic interface block that does not contain
   12277              : interface bodies.
   12278              : 
   12279              : R1510 generic-stmt is:
   12280              : GENERIC [ , access-spec ] :: generic-spec => specific-procedure-list
   12281              : 
   12282              : C1510 (R1510) A specific-procedure in a GENERIC statement shall not specify a
   12283              : procedure that was specified previously in any accessible interface with the
   12284              : same generic identifier.
   12285              : 
   12286              : If access-spec appears, it specifies the accessibility (8.5.2) of generic-spec.
   12287              : 
   12288              : For GENERIC statements outside of a derived type, use is made of the existing,
   12289              : typebound matching functions to obtain access-spec and generic-spec.  After
   12290              : this the standard INTERFACE machinery is used. */
   12291              : 
   12292              : static match
   12293          100 : match_generic_stmt (void)
   12294              : {
   12295          100 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12296              :   /* Allow space for OPERATOR(...).  */
   12297          100 :   char generic_spec_name[GFC_MAX_SYMBOL_LEN + 16];
   12298              :   /* Generics other than uops  */
   12299          100 :   gfc_symbol* generic_spec = NULL;
   12300              :   /* Generic uops  */
   12301          100 :   gfc_user_op *generic_uop = NULL;
   12302              :   /* For the matching calls  */
   12303          100 :   gfc_typebound_proc tbattr;
   12304          100 :   gfc_namespace* ns = gfc_current_ns;
   12305          100 :   interface_type op_type;
   12306          100 :   gfc_intrinsic_op op;
   12307          100 :   match m;
   12308          100 :   gfc_symtree* st;
   12309              :   /* The specific-procedure-list  */
   12310          100 :   gfc_interface *generic = NULL;
   12311              :   /* The head of the specific-procedure-list  */
   12312          100 :   gfc_interface **generic_tail = NULL;
   12313              : 
   12314          100 :   memset (&tbattr, 0, sizeof (tbattr));
   12315          100 :   tbattr.where = gfc_current_locus;
   12316              : 
   12317              :   /* See if we get an access-specifier.  */
   12318          100 :   m = match_binding_attributes (&tbattr, true, false);
   12319          100 :   tbattr.where = gfc_current_locus;
   12320          100 :   if (m == MATCH_ERROR)
   12321            0 :     goto error;
   12322              : 
   12323              :   /* Now the colons, those are required.  */
   12324          100 :   if (gfc_match (" ::") != MATCH_YES)
   12325              :     {
   12326            0 :       gfc_error ("Expected %<::%> at %C");
   12327            0 :       goto error;
   12328              :     }
   12329              : 
   12330              :   /* Match the generic-spec name; depending on type (operator / generic) format
   12331              :      it for future error messages in 'generic_spec_name'.  */
   12332          100 :   m = gfc_match_generic_spec (&op_type, name, &op);
   12333          100 :   if (m == MATCH_ERROR)
   12334              :     return MATCH_ERROR;
   12335          100 :   if (m == MATCH_NO)
   12336              :     {
   12337            0 :       gfc_error ("Expected generic name or operator descriptor at %C");
   12338            0 :       goto error;
   12339              :     }
   12340              : 
   12341          100 :   switch (op_type)
   12342              :     {
   12343           63 :     case INTERFACE_GENERIC:
   12344           63 :     case INTERFACE_DTIO:
   12345           63 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "%s", name);
   12346           63 :       break;
   12347              : 
   12348           22 :     case INTERFACE_USER_OP:
   12349           22 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(.%s.)", name);
   12350           22 :       break;
   12351              : 
   12352           13 :     case INTERFACE_INTRINSIC_OP:
   12353           13 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(%s)",
   12354              :                 gfc_op2string (op));
   12355           13 :       break;
   12356              : 
   12357            2 :     case INTERFACE_NAMELESS:
   12358            2 :       gfc_error ("Malformed GENERIC statement at %C");
   12359            2 :       goto error;
   12360            0 :       break;
   12361              : 
   12362            0 :     default:
   12363            0 :       gcc_unreachable ();
   12364              :     }
   12365              : 
   12366              :   /* Match the required =>.  */
   12367           98 :   if (gfc_match (" =>") != MATCH_YES)
   12368              :     {
   12369            1 :       gfc_error ("Expected %<=>%> at %C");
   12370            1 :       goto error;
   12371              :     }
   12372              : 
   12373              : 
   12374           97 :   if (gfc_current_state () != COMP_MODULE && tbattr.access != ACCESS_UNKNOWN)
   12375              :     {
   12376            1 :       gfc_error ("The access specification at %L not in a module",
   12377              :                  &tbattr.where);
   12378            1 :       goto error;
   12379              :     }
   12380              : 
   12381              :   /* Try to find existing generic-spec with this name for this operator;
   12382              :      if there is something, check that it is another generic-spec and then
   12383              :      extend it rather than building a new symbol. Otherwise, create a new
   12384              :      one with the right attributes.  */
   12385              : 
   12386           96 :   switch (op_type)
   12387              :     {
   12388           61 :     case INTERFACE_DTIO:
   12389           61 :     case INTERFACE_GENERIC:
   12390           61 :       st = gfc_find_symtree (ns->sym_root, name);
   12391           61 :       generic_spec = st ? st->n.sym : NULL;
   12392           61 :       if (generic_spec)
   12393              :         {
   12394           25 :           if (generic_spec->attr.flavor != FL_PROCEDURE
   12395           11 :                && generic_spec->attr.flavor != FL_UNKNOWN)
   12396              :             {
   12397            1 :               gfc_error ("The generic-spec name %qs at %C clashes with the "
   12398              :                          "name of an entity declared at %L that is not a "
   12399              :                          "procedure", name, &generic_spec->declared_at);
   12400            1 :               goto error;
   12401              :             }
   12402              : 
   12403           24 :           if (op_type == INTERFACE_GENERIC && !generic_spec->attr.generic
   12404           10 :                && generic_spec->attr.flavor != FL_UNKNOWN)
   12405              :             {
   12406            0 :               gfc_error ("There's already a non-generic procedure with "
   12407              :                          "name %qs at %C", generic_spec->name);
   12408            0 :               goto error;
   12409              :             }
   12410              : 
   12411           24 :           if (tbattr.access != ACCESS_UNKNOWN)
   12412              :             {
   12413            2 :               if (generic_spec->attr.access != tbattr.access)
   12414              :                 {
   12415            1 :                   gfc_error ("The access specification at %L conflicts with "
   12416              :                              "that already given to %qs", &tbattr.where,
   12417              :                              generic_spec->name);
   12418            1 :                   goto error;
   12419              :                 }
   12420              :               else
   12421              :                 {
   12422            1 :                   gfc_error ("The access specification at %L repeats that "
   12423              :                              "already given to %qs", &tbattr.where,
   12424              :                              generic_spec->name);
   12425            1 :                   goto error;
   12426              :                 }
   12427              :             }
   12428              : 
   12429           22 :           if (generic_spec->ts.type != BT_UNKNOWN)
   12430              :             {
   12431            1 :               gfc_error ("The generic-spec in the generic statement at %C "
   12432              :                          "has a type from the declaration at %L",
   12433              :                          &generic_spec->declared_at);
   12434            1 :               goto error;
   12435              :             }
   12436              :         }
   12437              : 
   12438              :       /* Now create the generic_spec if it doesn't already exist and provide
   12439              :          is with the appropriate attributes.  */
   12440           57 :       if (!generic_spec || generic_spec->attr.flavor != FL_PROCEDURE)
   12441              :         {
   12442           45 :           if (!generic_spec)
   12443              :             {
   12444           36 :               gfc_get_symbol (name, ns, &generic_spec, &gfc_current_locus);
   12445           36 :               gfc_set_sym_referenced (generic_spec);
   12446           36 :               generic_spec->attr.access = tbattr.access;
   12447              :             }
   12448            9 :           else if (generic_spec->attr.access == ACCESS_UNKNOWN)
   12449            0 :             generic_spec->attr.access = tbattr.access;
   12450           45 :           generic_spec->refs++;
   12451           45 :           generic_spec->attr.generic = 1;
   12452           45 :           generic_spec->attr.flavor = FL_PROCEDURE;
   12453              : 
   12454           45 :           generic_spec->declared_at = gfc_current_locus;
   12455              :         }
   12456              : 
   12457              :       /* Prepare to add the specific procedures.  */
   12458           57 :       generic = generic_spec->generic;
   12459           57 :       generic_tail = &generic_spec->generic;
   12460           57 :       break;
   12461              : 
   12462           22 :     case INTERFACE_USER_OP:
   12463           22 :       st = gfc_find_symtree (ns->uop_root, name);
   12464           22 :       generic_uop = st ? st->n.uop : NULL;
   12465            2 :       if (generic_uop)
   12466              :         {
   12467            2 :           if (generic_uop->access != ACCESS_UNKNOWN
   12468            2 :               && tbattr.access != ACCESS_UNKNOWN)
   12469              :             {
   12470            2 :               if (generic_uop->access != tbattr.access)
   12471              :                 {
   12472            1 :                   gfc_error ("The user operator at %L must have the same "
   12473              :                              "access specification as already defined user "
   12474              :                              "operator %qs", &tbattr.where, generic_spec_name);
   12475            1 :                   goto error;
   12476              :                 }
   12477              :               else
   12478              :                 {
   12479            1 :                   gfc_error ("The user operator at %L repeats the access "
   12480              :                              "specification of already defined user operator "                                   "%qs", &tbattr.where, generic_spec_name);
   12481            1 :                   goto error;
   12482              :                 }
   12483              :             }
   12484            0 :           else if (generic_uop->access == ACCESS_UNKNOWN)
   12485            0 :             generic_uop->access = tbattr.access;
   12486              :         }
   12487              :       else
   12488              :         {
   12489           20 :           generic_uop = gfc_get_uop (name);
   12490           20 :           generic_uop->access = tbattr.access;
   12491              :         }
   12492              : 
   12493              :       /* Prepare to add the specific procedures.  */
   12494           20 :       generic = generic_uop->op;
   12495           20 :       generic_tail = &generic_uop->op;
   12496           20 :       break;
   12497              : 
   12498           13 :     case INTERFACE_INTRINSIC_OP:
   12499           13 :       generic = ns->op[op];
   12500           13 :       generic_tail = &ns->op[op];
   12501           13 :       break;
   12502              : 
   12503            0 :     default:
   12504            0 :       gcc_unreachable ();
   12505              :     }
   12506              : 
   12507              :   /* Now, match all following names in the specific-procedure-list.  */
   12508          154 :   do
   12509              :     {
   12510          154 :       m = gfc_match_name (name);
   12511          154 :       if (m == MATCH_ERROR)
   12512            0 :         goto error;
   12513          154 :       if (m == MATCH_NO)
   12514              :         {
   12515            0 :           gfc_error ("Expected specific procedure name at %C");
   12516            0 :           goto error;
   12517              :         }
   12518              : 
   12519          154 :       if (op_type == INTERFACE_GENERIC
   12520           95 :           && !strcmp (generic_spec->name, name))
   12521              :         {
   12522            2 :           gfc_error ("The name %qs of the specific procedure at %C conflicts "
   12523              :                      "with that of the generic-spec", name);
   12524            2 :           goto error;
   12525              :         }
   12526              : 
   12527          152 :       generic = *generic_tail;
   12528          242 :       for (; generic; generic = generic->next)
   12529              :         {
   12530           90 :           if (!strcmp (generic->sym->name, name))
   12531              :             {
   12532            0 :               gfc_error ("%qs already defined as a specific procedure for the"
   12533              :                          " generic %qs at %C", name, generic_spec->name);
   12534            0 :               goto error;
   12535              :             }
   12536              :         }
   12537              : 
   12538          152 :       gfc_find_sym_tree (name, ns, 1, &st);
   12539          152 :       if (!st)
   12540              :         {
   12541              :           /* This might be a procedure that has not yet been parsed. If
   12542              :              so gfc_fixup_sibling_symbols will replace this symbol with
   12543              :              that of the procedure.  */
   12544           75 :           gfc_get_sym_tree (name, ns, &st, false);
   12545           75 :           st->n.sym->refs++;
   12546              :         }
   12547              : 
   12548          152 :       generic = gfc_get_interface();
   12549          152 :       generic->next = *generic_tail;
   12550          152 :       *generic_tail = generic;
   12551          152 :       generic->where = gfc_current_locus;
   12552          152 :       generic->sym = st->n.sym;
   12553              :     }
   12554          152 :   while (gfc_match (" ,") == MATCH_YES);
   12555              : 
   12556           88 :   if (gfc_match_eos () != MATCH_YES)
   12557              :     {
   12558            0 :       gfc_error ("Junk after GENERIC statement at %C");
   12559            0 :       goto error;
   12560              :     }
   12561              : 
   12562           88 :   gfc_commit_symbols ();
   12563           88 :   return MATCH_YES;
   12564              : 
   12565          100 : error:
   12566              :   return MATCH_ERROR;
   12567              : }
   12568              : 
   12569              : 
   12570              : /* Match a GENERIC procedure binding inside a derived type.  */
   12571              : 
   12572              : static match
   12573          954 : match_typebound_generic (void)
   12574              : {
   12575          954 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12576          954 :   char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
   12577          954 :   gfc_symbol* block;
   12578          954 :   gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
   12579          954 :   gfc_typebound_proc* tb;
   12580          954 :   gfc_namespace* ns;
   12581          954 :   interface_type op_type;
   12582          954 :   gfc_intrinsic_op op;
   12583          954 :   match m;
   12584              : 
   12585              :   /* Check current state.  */
   12586          954 :   if (gfc_current_state () == COMP_DERIVED)
   12587              :     {
   12588            0 :       gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
   12589            0 :       return MATCH_ERROR;
   12590              :     }
   12591          954 :   if (gfc_current_state () != COMP_DERIVED_CONTAINS)
   12592              :     return MATCH_NO;
   12593          954 :   block = gfc_state_stack->previous->sym;
   12594          954 :   ns = block->f2k_derived;
   12595          954 :   gcc_assert (block && ns);
   12596              : 
   12597          954 :   memset (&tbattr, 0, sizeof (tbattr));
   12598          954 :   tbattr.where = gfc_current_locus;
   12599              : 
   12600              :   /* See if we get an access-specifier.  */
   12601          954 :   m = match_binding_attributes (&tbattr, true, false);
   12602          954 :   if (m == MATCH_ERROR)
   12603            1 :     goto error;
   12604              : 
   12605              :   /* Now the colons, those are required.  */
   12606          953 :   if (gfc_match (" ::") != MATCH_YES)
   12607              :     {
   12608            0 :       gfc_error ("Expected %<::%> at %C");
   12609            0 :       goto error;
   12610              :     }
   12611              : 
   12612              :   /* Match the binding name; depending on type (operator / generic) format
   12613              :      it for future error messages into bind_name.  */
   12614              : 
   12615          953 :   m = gfc_match_generic_spec (&op_type, name, &op);
   12616          953 :   if (m == MATCH_ERROR)
   12617              :     return MATCH_ERROR;
   12618          953 :   if (m == MATCH_NO)
   12619              :     {
   12620            0 :       gfc_error ("Expected generic name or operator descriptor at %C");
   12621            0 :       goto error;
   12622              :     }
   12623              : 
   12624          953 :   switch (op_type)
   12625              :     {
   12626          470 :     case INTERFACE_GENERIC:
   12627          470 :     case INTERFACE_DTIO:
   12628          470 :       snprintf (bind_name, sizeof (bind_name), "%s", name);
   12629          470 :       break;
   12630              : 
   12631           47 :     case INTERFACE_USER_OP:
   12632           47 :       snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
   12633           47 :       break;
   12634              : 
   12635          435 :     case INTERFACE_INTRINSIC_OP:
   12636          435 :       snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
   12637              :                 gfc_op2string (op));
   12638          435 :       break;
   12639              : 
   12640            1 :     case INTERFACE_NAMELESS:
   12641            1 :       gfc_error ("Malformed GENERIC statement at %C");
   12642            1 :       goto error;
   12643            0 :       break;
   12644              : 
   12645            0 :     default:
   12646            0 :       gcc_unreachable ();
   12647              :     }
   12648              : 
   12649              :   /* Match the required =>.  */
   12650          952 :   if (gfc_match (" =>") != MATCH_YES)
   12651              :     {
   12652            0 :       gfc_error ("Expected %<=>%> at %C");
   12653            0 :       goto error;
   12654              :     }
   12655              : 
   12656              :   /* Try to find existing GENERIC binding with this name / for this operator;
   12657              :      if there is something, check that it is another GENERIC and then extend
   12658              :      it rather than building a new node.  Otherwise, create it and put it
   12659              :      at the right position.  */
   12660              : 
   12661          952 :   switch (op_type)
   12662              :     {
   12663          517 :     case INTERFACE_DTIO:
   12664          517 :     case INTERFACE_USER_OP:
   12665          517 :     case INTERFACE_GENERIC:
   12666          517 :       {
   12667          517 :         const bool is_op = (op_type == INTERFACE_USER_OP);
   12668          517 :         gfc_symtree* st;
   12669              : 
   12670          517 :         st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
   12671          517 :         tb = st ? st->n.tb : NULL;
   12672              :         break;
   12673              :       }
   12674              : 
   12675          435 :     case INTERFACE_INTRINSIC_OP:
   12676          435 :       tb = ns->tb_op[op];
   12677          435 :       break;
   12678              : 
   12679            0 :     default:
   12680            0 :       gcc_unreachable ();
   12681              :     }
   12682              : 
   12683          446 :   if (tb)
   12684              :     {
   12685            9 :       if (!tb->is_generic)
   12686              :         {
   12687            1 :           gcc_assert (op_type == INTERFACE_GENERIC);
   12688            1 :           gfc_error ("There's already a non-generic procedure with binding name"
   12689              :                      " %qs for the derived type %qs at %C",
   12690              :                      bind_name, block->name);
   12691            1 :           goto error;
   12692              :         }
   12693              : 
   12694            8 :       if (tb->access != tbattr.access)
   12695              :         {
   12696            2 :           gfc_error ("Binding at %C must have the same access as already"
   12697              :                      " defined binding %qs", bind_name);
   12698            2 :           goto error;
   12699              :         }
   12700              :     }
   12701              :   else
   12702              :     {
   12703          943 :       tb = gfc_get_typebound_proc (NULL);
   12704          943 :       tb->where = gfc_current_locus;
   12705          943 :       tb->access = tbattr.access;
   12706          943 :       tb->is_generic = 1;
   12707          943 :       tb->u.generic = NULL;
   12708              : 
   12709          943 :       switch (op_type)
   12710              :         {
   12711          508 :         case INTERFACE_DTIO:
   12712          508 :         case INTERFACE_GENERIC:
   12713          508 :         case INTERFACE_USER_OP:
   12714          508 :           {
   12715          508 :             const bool is_op = (op_type == INTERFACE_USER_OP);
   12716          508 :             gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
   12717              :                                                    &ns->tb_sym_root, name);
   12718          508 :             gcc_assert (st);
   12719          508 :             st->n.tb = tb;
   12720              : 
   12721          508 :             break;
   12722              :           }
   12723              : 
   12724          435 :         case INTERFACE_INTRINSIC_OP:
   12725          435 :           ns->tb_op[op] = tb;
   12726          435 :           break;
   12727              : 
   12728            0 :         default:
   12729            0 :           gcc_unreachable ();
   12730              :         }
   12731              :     }
   12732              : 
   12733              :   /* Now, match all following names as specific targets.  */
   12734         1106 :   do
   12735              :     {
   12736         1106 :       gfc_symtree* target_st;
   12737         1106 :       gfc_tbp_generic* target;
   12738              : 
   12739         1106 :       m = gfc_match_name (name);
   12740         1106 :       if (m == MATCH_ERROR)
   12741            0 :         goto error;
   12742         1106 :       if (m == MATCH_NO)
   12743              :         {
   12744            1 :           gfc_error ("Expected specific binding name at %C");
   12745            1 :           goto error;
   12746              :         }
   12747              : 
   12748         1105 :       target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
   12749              : 
   12750              :       /* See if this is a duplicate specification.  */
   12751         1340 :       for (target = tb->u.generic; target; target = target->next)
   12752          236 :         if (target_st == target->specific_st)
   12753              :           {
   12754            1 :             gfc_error ("%qs already defined as specific binding for the"
   12755              :                        " generic %qs at %C", name, bind_name);
   12756            1 :             goto error;
   12757              :           }
   12758              : 
   12759         1104 :       target = gfc_get_tbp_generic ();
   12760         1104 :       target->specific_st = target_st;
   12761         1104 :       target->specific = NULL;
   12762         1104 :       target->next = tb->u.generic;
   12763         1104 :       target->is_operator = ((op_type == INTERFACE_USER_OP)
   12764         1104 :                              || (op_type == INTERFACE_INTRINSIC_OP));
   12765         1104 :       tb->u.generic = target;
   12766              :     }
   12767         1104 :   while (gfc_match (" ,") == MATCH_YES);
   12768              : 
   12769              :   /* Here should be the end.  */
   12770          947 :   if (gfc_match_eos () != MATCH_YES)
   12771              :     {
   12772            1 :       gfc_error ("Junk after GENERIC binding at %C");
   12773            1 :       goto error;
   12774              :     }
   12775              : 
   12776              :   return MATCH_YES;
   12777              : 
   12778          954 : error:
   12779              :   return MATCH_ERROR;
   12780              : }
   12781              : 
   12782              : 
   12783              : match
   12784         1054 : gfc_match_generic ()
   12785              : {
   12786         1054 :   if (gfc_option.allow_std & ~GFC_STD_OPT_F08
   12787         1052 :       && gfc_current_state () != COMP_DERIVED_CONTAINS)
   12788          100 :     return match_generic_stmt ();
   12789              :   else
   12790          954 :     return match_typebound_generic ();
   12791              : }
   12792              : 
   12793              : 
   12794              : /* Match a FINAL declaration inside a derived type.  */
   12795              : 
   12796              : match
   12797          484 : gfc_match_final_decl (void)
   12798              : {
   12799          484 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12800          484 :   gfc_symbol* sym;
   12801          484 :   match m;
   12802          484 :   gfc_namespace* module_ns;
   12803          484 :   bool first, last;
   12804          484 :   gfc_symbol* block;
   12805              : 
   12806          484 :   if (gfc_current_form == FORM_FREE)
   12807              :     {
   12808          484 :       char c = gfc_peek_ascii_char ();
   12809          484 :       if (!gfc_is_whitespace (c) && c != ':')
   12810              :         return MATCH_NO;
   12811              :     }
   12812              : 
   12813          483 :   if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
   12814              :     {
   12815            1 :       if (gfc_current_form == FORM_FIXED)
   12816              :         return MATCH_NO;
   12817              : 
   12818            1 :       gfc_error ("FINAL declaration at %C must be inside a derived type "
   12819              :                  "CONTAINS section");
   12820            1 :       return MATCH_ERROR;
   12821              :     }
   12822              : 
   12823          482 :   block = gfc_state_stack->previous->sym;
   12824          482 :   gcc_assert (block);
   12825              : 
   12826          482 :   if (gfc_state_stack->previous->previous
   12827          482 :       && gfc_state_stack->previous->previous->state != COMP_MODULE
   12828            6 :       && gfc_state_stack->previous->previous->state != COMP_SUBMODULE)
   12829              :     {
   12830            0 :       gfc_error ("Derived type declaration with FINAL at %C must be in the"
   12831              :                  " specification part of a MODULE");
   12832            0 :       return MATCH_ERROR;
   12833              :     }
   12834              : 
   12835          482 :   module_ns = gfc_current_ns;
   12836          482 :   gcc_assert (module_ns);
   12837          482 :   gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
   12838              : 
   12839              :   /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
   12840          482 :   if (gfc_match (" ::") == MATCH_ERROR)
   12841              :     return MATCH_ERROR;
   12842              : 
   12843              :   /* Match the sequence of procedure names.  */
   12844              :   first = true;
   12845              :   last = false;
   12846          574 :   do
   12847              :     {
   12848          574 :       gfc_finalizer* f;
   12849              : 
   12850          574 :       if (first && gfc_match_eos () == MATCH_YES)
   12851              :         {
   12852            2 :           gfc_error ("Empty FINAL at %C");
   12853            2 :           return MATCH_ERROR;
   12854              :         }
   12855              : 
   12856          572 :       m = gfc_match_name (name);
   12857          572 :       if (m == MATCH_NO)
   12858              :         {
   12859            1 :           gfc_error ("Expected module procedure name at %C");
   12860            1 :           return MATCH_ERROR;
   12861              :         }
   12862          571 :       else if (m != MATCH_YES)
   12863              :         return MATCH_ERROR;
   12864              : 
   12865          571 :       if (gfc_match_eos () == MATCH_YES)
   12866              :         last = true;
   12867           93 :       if (!last && gfc_match_char (',') != MATCH_YES)
   12868              :         {
   12869            1 :           gfc_error ("Expected %<,%> at %C");
   12870            1 :           return MATCH_ERROR;
   12871              :         }
   12872              : 
   12873          570 :       if (gfc_get_symbol (name, module_ns, &sym))
   12874              :         {
   12875            0 :           gfc_error ("Unknown procedure name %qs at %C", name);
   12876            0 :           return MATCH_ERROR;
   12877              :         }
   12878              : 
   12879              :       /* Mark the symbol as module procedure.  */
   12880          570 :       if (sym->attr.proc != PROC_MODULE
   12881          570 :           && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
   12882              :         return MATCH_ERROR;
   12883              : 
   12884              :       /* Check if we already have this symbol in the list, this is an error.  */
   12885          769 :       for (f = block->f2k_derived->finalizers; f; f = f->next)
   12886          200 :         if (f->proc_sym == sym)
   12887              :           {
   12888            1 :             gfc_error ("%qs at %C is already defined as FINAL procedure",
   12889              :                        name);
   12890            1 :             return MATCH_ERROR;
   12891              :           }
   12892              : 
   12893              :       /* Add this symbol to the list of finalizers.  */
   12894          569 :       gcc_assert (block->f2k_derived);
   12895          569 :       sym->refs++;
   12896          569 :       f = XCNEW (gfc_finalizer);
   12897          569 :       f->proc_sym = sym;
   12898          569 :       f->proc_tree = NULL;
   12899          569 :       f->where = gfc_current_locus;
   12900          569 :       f->next = block->f2k_derived->finalizers;
   12901          569 :       block->f2k_derived->finalizers = f;
   12902              : 
   12903          569 :       first = false;
   12904              :     }
   12905          569 :   while (!last);
   12906              : 
   12907              :   return MATCH_YES;
   12908              : }
   12909              : 
   12910              : 
   12911              : const ext_attr_t ext_attr_list[] = {
   12912              :   { "dllimport",    EXT_ATTR_DLLIMPORT,    "dllimport" },
   12913              :   { "dllexport",    EXT_ATTR_DLLEXPORT,    "dllexport" },
   12914              :   { "cdecl",        EXT_ATTR_CDECL,        "cdecl"     },
   12915              :   { "stdcall",      EXT_ATTR_STDCALL,      "stdcall"   },
   12916              :   { "fastcall",     EXT_ATTR_FASTCALL,     "fastcall"  },
   12917              :   { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL              },
   12918              :   { "deprecated",   EXT_ATTR_DEPRECATED,   NULL              },
   12919              :   { "noinline",     EXT_ATTR_NOINLINE,     NULL              },
   12920              :   { "noreturn",     EXT_ATTR_NORETURN,     NULL              },
   12921              :   { "weak",       EXT_ATTR_WEAK,         NULL        },
   12922              :   { "inline",       EXT_ATTR_INLINE,       NULL              },
   12923              :   { "always_inline",EXT_ATTR_ALWAYS_INLINE,NULL              },
   12924              :   { NULL,           EXT_ATTR_LAST,         NULL        }
   12925              : };
   12926              : 
   12927              : /* Match a !GCC$ ATTRIBUTES statement of the form:
   12928              :       !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
   12929              :    When we come here, we have already matched the !GCC$ ATTRIBUTES string.
   12930              : 
   12931              :    TODO: We should support all GCC attributes using the same syntax for
   12932              :    the attribute list, i.e. the list in C
   12933              :       __attributes(( attribute-list ))
   12934              :    matches then
   12935              :       !GCC$ ATTRIBUTES attribute-list ::
   12936              :    Cf. c-parser.cc's c_parser_attributes; the data can then directly be
   12937              :    saved into a TREE.
   12938              : 
   12939              :    As there is absolutely no risk of confusion, we should never return
   12940              :    MATCH_NO.  */
   12941              : match
   12942         2984 : gfc_match_gcc_attributes (void)
   12943              : {
   12944         2984 :   symbol_attribute attr;
   12945         2984 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12946         2984 :   unsigned id;
   12947         2984 :   gfc_symbol *sym;
   12948         2984 :   match m;
   12949              : 
   12950         2984 :   gfc_clear_attr (&attr);
   12951         2988 :   for(;;)
   12952              :     {
   12953         2986 :       char ch;
   12954              : 
   12955         2986 :       if (gfc_match_name (name) != MATCH_YES)
   12956              :         return MATCH_ERROR;
   12957              : 
   12958        18042 :       for (id = 0; id < EXT_ATTR_LAST; id++)
   12959        18042 :         if (strcmp (name, ext_attr_list[id].name) == 0)
   12960              :           break;
   12961              : 
   12962         2986 :       if (id == EXT_ATTR_LAST)
   12963              :         {
   12964            0 :           gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
   12965            0 :           return MATCH_ERROR;
   12966              :         }
   12967              : 
   12968         2986 :       if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
   12969              :         return MATCH_ERROR;
   12970              : 
   12971         2986 :       gfc_gobble_whitespace ();
   12972         2986 :       ch = gfc_next_ascii_char ();
   12973         2986 :       if (ch == ':')
   12974              :         {
   12975              :           /* This is the successful exit condition for the loop.  */
   12976         2984 :           if (gfc_next_ascii_char () == ':')
   12977              :             break;
   12978              :         }
   12979              : 
   12980            2 :       if (ch == ',')
   12981            2 :         continue;
   12982              : 
   12983            0 :       goto syntax;
   12984            2 :     }
   12985              : 
   12986         2984 :   if (gfc_match_eos () == MATCH_YES)
   12987            0 :     goto syntax;
   12988              : 
   12989         2999 :   for(;;)
   12990              :     {
   12991         2999 :       m = gfc_match_name (name);
   12992         2999 :       if (m != MATCH_YES)
   12993              :         return m;
   12994              : 
   12995         2999 :       if (find_special (name, &sym, true))
   12996              :         return MATCH_ERROR;
   12997              : 
   12998         2999 :       sym->attr.ext_attr |= attr.ext_attr;
   12999              : 
   13000              :       /* INLINE and ALWAYS_INLINE are incompatible with NOINLINE.  In the
   13001              :          middle-end the DECL_UNINLINABLE flag set by NOINLINE always wins, so
   13002              :          the inline request would be silently ignored.  Warn and drop it.  */
   13003         2999 :       if (sym->attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
   13004              :         {
   13005            5 :           if (sym->attr.ext_attr & (1 << EXT_ATTR_ALWAYS_INLINE))
   13006              :             {
   13007            2 :               gfc_warning (0, "Attribute %<ALWAYS_INLINE%> at %C is "
   13008              :                            "incompatible with %<NOINLINE%> for %qs and will "
   13009              :                            "be ignored", sym->name);
   13010            2 :               sym->attr.ext_attr &= ~(1 << EXT_ATTR_ALWAYS_INLINE);
   13011              :             }
   13012            5 :           if (sym->attr.ext_attr & (1 << EXT_ATTR_INLINE))
   13013              :             {
   13014            2 :               gfc_warning (0, "Attribute %<INLINE%> at %C is incompatible "
   13015              :                            "with %<NOINLINE%> for %qs and will be ignored",
   13016              :                            sym->name);
   13017            2 :               sym->attr.ext_attr &= ~(1 << EXT_ATTR_INLINE);
   13018              :             }
   13019              :         }
   13020              : 
   13021         2999 :       if (gfc_match_eos () == MATCH_YES)
   13022              :         break;
   13023              : 
   13024           15 :       if (gfc_match_char (',') != MATCH_YES)
   13025            0 :         goto syntax;
   13026              :     }
   13027              : 
   13028              :   return MATCH_YES;
   13029              : 
   13030            0 : syntax:
   13031            0 :   gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
   13032            0 :   return MATCH_ERROR;
   13033              : }
   13034              : 
   13035              : 
   13036              : /* Match a !GCC$ UNROLL statement of the form:
   13037              :       !GCC$ UNROLL n
   13038              : 
   13039              :    The parameter n is the number of times we are supposed to unroll.
   13040              : 
   13041              :    When we come here, we have already matched the !GCC$ UNROLL string.  */
   13042              : match
   13043           19 : gfc_match_gcc_unroll (void)
   13044              : {
   13045           19 :   int value;
   13046              : 
   13047              :   /* FIXME: use gfc_match_small_literal_int instead, delete small_int  */
   13048           19 :   if (gfc_match_small_int (&value) == MATCH_YES)
   13049              :     {
   13050           19 :       if (value < 0 || value > USHRT_MAX)
   13051              :         {
   13052            2 :           gfc_error ("%<GCC unroll%> directive requires a"
   13053              :               " non-negative integral constant"
   13054              :               " less than or equal to %u at %C",
   13055              :               USHRT_MAX
   13056              :           );
   13057            2 :           return MATCH_ERROR;
   13058              :         }
   13059           17 :       if (gfc_match_eos () == MATCH_YES)
   13060              :         {
   13061           17 :           directive_unroll = value == 0 ? 1 : value;
   13062           17 :           return MATCH_YES;
   13063              :         }
   13064              :     }
   13065              : 
   13066            0 :   gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
   13067            0 :   return MATCH_ERROR;
   13068              : }
   13069              : 
   13070              : /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
   13071              : 
   13072              :    The parameter b is name of a middle-end built-in.
   13073              :    FLAGS is optional and must be one of:
   13074              :      - (inbranch)
   13075              :      - (notinbranch)
   13076              : 
   13077              :    IF('target') is optional and TARGET is a name of a multilib ABI.
   13078              : 
   13079              :    When we come here, we have already matched the !GCC$ builtin string.  */
   13080              : 
   13081              : match
   13082      3463809 : gfc_match_gcc_builtin (void)
   13083              : {
   13084      3463809 :   char builtin[GFC_MAX_SYMBOL_LEN + 1];
   13085      3463809 :   char target[GFC_MAX_SYMBOL_LEN + 1];
   13086              : 
   13087      3463809 :   if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
   13088              :     return MATCH_ERROR;
   13089              : 
   13090      3463809 :   gfc_simd_clause clause = SIMD_NONE;
   13091      3463809 :   if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
   13092              :     clause = SIMD_NOTINBRANCH;
   13093           21 :   else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
   13094           15 :     clause = SIMD_INBRANCH;
   13095              : 
   13096      3463809 :   if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
   13097              :     {
   13098      3463779 :       if (strcmp (target, "fastmath") == 0)
   13099              :         {
   13100            0 :           if (!fast_math_flags_set_p (&global_options))
   13101              :             return MATCH_YES;
   13102              :         }
   13103              :       else
   13104              :         {
   13105      3463779 :           const char *abi = targetm.get_multilib_abi_name ();
   13106      3463779 :           if (abi == NULL || strcmp (abi, target) != 0)
   13107              :             return MATCH_YES;
   13108              :         }
   13109              :     }
   13110              : 
   13111      1709834 :   if (gfc_vectorized_builtins == NULL)
   13112        31669 :     gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
   13113              : 
   13114      1709834 :   char *r = XNEWVEC (char, strlen (builtin) + 32);
   13115      1709834 :   sprintf (r, "__builtin_%s", builtin);
   13116              : 
   13117      1709834 :   bool existed;
   13118      1709834 :   int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
   13119      1709834 :   value |= clause;
   13120      1709834 :   if (existed)
   13121           23 :     free (r);
   13122              : 
   13123              :   return MATCH_YES;
   13124              : }
   13125              : 
   13126              : /* Match an !GCC$ IVDEP statement.
   13127              :    When we come here, we have already matched the !GCC$ IVDEP string.  */
   13128              : 
   13129              : match
   13130            3 : gfc_match_gcc_ivdep (void)
   13131              : {
   13132            3 :   if (gfc_match_eos () == MATCH_YES)
   13133              :     {
   13134            3 :       directive_ivdep = true;
   13135            3 :       return MATCH_YES;
   13136              :     }
   13137              : 
   13138            0 :   gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
   13139            0 :   return MATCH_ERROR;
   13140              : }
   13141              : 
   13142              : /* Match an !GCC$ VECTOR statement.
   13143              :    When we come here, we have already matched the !GCC$ VECTOR string.  */
   13144              : 
   13145              : match
   13146            3 : gfc_match_gcc_vector (void)
   13147              : {
   13148            3 :   if (gfc_match_eos () == MATCH_YES)
   13149              :     {
   13150            3 :       directive_vector = true;
   13151            3 :       directive_novector = false;
   13152            3 :       return MATCH_YES;
   13153              :     }
   13154              : 
   13155            0 :   gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
   13156            0 :   return MATCH_ERROR;
   13157              : }
   13158              : 
   13159              : /* Match an !GCC$ NOVECTOR statement.
   13160              :    When we come here, we have already matched the !GCC$ NOVECTOR string.  */
   13161              : 
   13162              : match
   13163            3 : gfc_match_gcc_novector (void)
   13164              : {
   13165            3 :   if (gfc_match_eos () == MATCH_YES)
   13166              :     {
   13167            3 :       directive_novector = true;
   13168            3 :       directive_vector = false;
   13169            3 :       return MATCH_YES;
   13170              :     }
   13171              : 
   13172            0 :   gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
   13173            0 :   return MATCH_ERROR;
   13174              : }
        

Generated by: LCOV version 2.4-beta

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