LCOV - code coverage report
Current view: top level - gcc/fortran - decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.8 % 6207 5639
Test Date: 2026-12-05 16:10:02 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         8624 : gfc_in_match_data (void)
     163              : {
     164         8624 :   return in_match_data;
     165              : }
     166              : 
     167              : static void
     168         4876 : set_in_match_data (bool set_value)
     169              : {
     170         4876 :   in_match_data = set_value;
     171            0 : }
     172              : 
     173              : /* Free a gfc_data_variable structure and everything beneath it.  */
     174              : 
     175              : static void
     176         5699 : free_variable (gfc_data_variable *p)
     177              : {
     178         5699 :   gfc_data_variable *q;
     179              : 
     180         8806 :   for (; p; p = q)
     181              :     {
     182         3107 :       q = p->next;
     183         3107 :       gfc_free_expr (p->expr);
     184         3107 :       gfc_free_iterator (&p->iter, 0);
     185         3107 :       free_variable (p->list);
     186         3107 :       free (p);
     187              :     }
     188         5699 : }
     189              : 
     190              : 
     191              : /* Free a gfc_data_value structure and everything beneath it.  */
     192              : 
     193              : static void
     194         2592 : free_value (gfc_data_value *p)
     195              : {
     196         2592 :   gfc_data_value *q;
     197              : 
     198        10922 :   for (; p; p = q)
     199              :     {
     200         8330 :       q = p->next;
     201         8330 :       mpz_clear (p->repeat);
     202         8330 :       gfc_free_expr (p->expr);
     203         8330 :       free (p);
     204              :     }
     205         2592 : }
     206              : 
     207              : 
     208              : /* Free a list of gfc_data structures.  */
     209              : 
     210              : void
     211       564010 : gfc_free_data (gfc_data *p)
     212              : {
     213       564010 :   gfc_data *q;
     214              : 
     215       566602 :   for (; p; p = q)
     216              :     {
     217         2592 :       q = p->next;
     218         2592 :       free_variable (p->var);
     219         2592 :       free_value (p->value);
     220         2592 :       free (p);
     221              :     }
     222       564010 : }
     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      9475389 : gfc_reject_data (gfc_namespace *ns)
     244              : {
     245      9475389 :   gfc_data *d;
     246              : 
     247      9475391 :   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      9475389 : }
     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         3065 : var_element (gfc_data_variable *new_var)
     315              : {
     316         3065 :   match m;
     317         3065 :   gfc_symbol *sym;
     318              : 
     319         3065 :   memset (new_var, 0, sizeof (gfc_data_variable));
     320              : 
     321         3065 :   if (gfc_match_char ('(') == MATCH_YES)
     322          154 :     return var_list (new_var);
     323              : 
     324         2911 :   m = gfc_match_variable (&new_var->expr, 0);
     325         2911 :   if (m != MATCH_YES)
     326              :     return m;
     327              : 
     328         2907 :   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         2905 :   sym = new_var->expr->symtree->n.sym;
     337              : 
     338              :   /* Symbol should already have an associated type.  */
     339         2905 :   if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
     340              :     return MATCH_ERROR;
     341              : 
     342         2904 :   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         2903 :   if (gfc_current_state () != COMP_BLOCK_DATA
     351         2750 :       && sym->attr.in_common
     352         2932 :       && !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         2901 :   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         2535 : top_var_list (gfc_data *d)
     368              : {
     369         2535 :   gfc_data_variable var, *tail, *new_var;
     370         2535 :   match m;
     371              : 
     372         2535 :   tail = NULL;
     373              : 
     374         2910 :   for (;;)
     375              :     {
     376         2910 :       m = var_element (&var);
     377         2910 :       if (m == MATCH_NO)
     378            0 :         goto syntax;
     379         2910 :       if (m == MATCH_ERROR)
     380              :         return MATCH_ERROR;
     381              : 
     382         2895 :       new_var = gfc_get_data_variable ();
     383         2895 :       *new_var = var;
     384         2895 :       if (new_var->expr)
     385         2769 :         new_var->expr->where = gfc_current_locus;
     386              : 
     387         2895 :       if (tail == NULL)
     388         2520 :         d->var = new_var;
     389              :       else
     390          375 :         tail->next = new_var;
     391              : 
     392         2895 :       tail = new_var;
     393              : 
     394         2895 :       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         8731 : match_data_constant (gfc_expr **result)
     411              : {
     412         8731 :   char name[GFC_MAX_SYMBOL_LEN + 1];
     413         8731 :   gfc_symbol *sym, *dt_sym = NULL;
     414         8731 :   gfc_expr *expr;
     415         8731 :   match m;
     416         8731 :   locus old_loc;
     417         8731 :   gfc_symtree *symtree;
     418              : 
     419         8731 :   m = gfc_match_literal_constant (&expr, 1);
     420         8731 :   if (m == MATCH_YES)
     421              :     {
     422         8368 :       *result = expr;
     423         8368 :       return MATCH_YES;
     424              :     }
     425              : 
     426          363 :   if (m == MATCH_ERROR)
     427              :     return MATCH_ERROR;
     428              : 
     429          355 :   m = gfc_match_null (result);
     430          355 :   if (m != MATCH_NO)
     431              :     return m;
     432              : 
     433          347 :   old_loc = gfc_current_locus;
     434              : 
     435              :   /* Should this be a structure component, try to match it
     436              :      before matching a name.  */
     437          347 :   m = gfc_match_rvalue (result);
     438          347 :   if (m == MATCH_ERROR)
     439              :     return m;
     440              : 
     441          347 :   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          337 :   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          337 :       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          301 :       if ((*result)->symtree
     466          300 :           && (*result)->symtree->n.sym->attr.save
     467          236 :           && (*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         2578 : top_val_list (gfc_data *data)
     532              : {
     533         2578 :   gfc_data_value *new_val, *tail;
     534         2578 :   gfc_expr *expr;
     535         2578 :   match m;
     536              : 
     537         2578 :   tail = NULL;
     538              : 
     539         8367 :   for (;;)
     540              :     {
     541         8367 :       m = match_data_constant (&expr);
     542         8367 :       if (m == MATCH_NO)
     543            3 :         goto syntax;
     544         8364 :       if (m == MATCH_ERROR)
     545              :         return MATCH_ERROR;
     546              : 
     547         8342 :       new_val = gfc_get_data_value ();
     548         8342 :       mpz_init (new_val->repeat);
     549              : 
     550         8342 :       if (tail == NULL)
     551         2553 :         data->value = new_val;
     552              :       else
     553         5789 :         tail->next = new_val;
     554              : 
     555         8342 :       tail = new_val;
     556              : 
     557         8342 :       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
     558              :         {
     559         8137 :           tail->expr = expr;
     560         8137 :           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         8338 :       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         2440 : gfc_match_data (void)
     667              : {
     668         2440 :   gfc_data *new_data;
     669         2440 :   gfc_expr *e;
     670         2440 :   gfc_ref *ref;
     671         2440 :   match m;
     672         2440 :   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         2440 :   c = gfc_peek_ascii_char ();
     678         2440 :   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         2439 :   if ((gfc_current_state () == COMP_FUNCTION
     683         2439 :        || 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         2438 :   set_in_match_data (true);
     691              : 
     692         2632 :   for (;;)
     693              :     {
     694         2535 :       new_data = gfc_get_data ();
     695         2535 :       new_data->where = gfc_current_locus;
     696              : 
     697         2535 :       m = top_var_list (new_data);
     698         2535 :       if (m != MATCH_YES)
     699           18 :         goto cleanup;
     700              : 
     701         2517 :       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         2516 :       e = new_data->var->expr;
     718         2516 :       if (e)
     719              :         {
     720         2400 :           bool invalid;
     721              : 
     722         2400 :           invalid = false;
     723         3624 :           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         2400 :           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         2398 :           if (!e->ref && e->ts.type == BT_DERIVED
     742           43 :               && e->symtree->n.sym->attr.pointer)
     743            4 :             goto partref;
     744              : 
     745         2394 :           ref = e->ref;
     746         2394 :           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         3609 :           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         2508 :       m = top_val_list (new_data);
     759         2508 :       if (m != MATCH_YES)
     760           29 :         goto cleanup;
     761              : 
     762         2479 :       new_data->next = gfc_current_ns->data;
     763         2479 :       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         2479 :       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         2479 :       if (gfc_match_eos () == MATCH_YES)
     782              :         break;
     783              : 
     784           97 :       gfc_match_char (',');     /* Optional comma */
     785           97 :     }
     786              : 
     787         2382 :   set_in_match_data (false);
     788              : 
     789         2382 :   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         2382 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
     795              : 
     796         2382 :   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        29758 : match_intent_spec (void)
    1080              : {
    1081              : 
    1082        29758 :   if (gfc_match (" ( in out )") == MATCH_YES)
    1083              :     return INTENT_INOUT;
    1084        26450 :   if (gfc_match (" ( in )") == MATCH_YES)
    1085              :     return INTENT_IN;
    1086         3880 :   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        28840 : char_len_param_value (gfc_expr **expr, bool *deferred)
    1099              : {
    1100        28840 :   match m;
    1101        28840 :   gfc_expr *p;
    1102              : 
    1103        28840 :   *expr = NULL;
    1104        28840 :   *deferred = false;
    1105              : 
    1106        28840 :   if (gfc_match_char ('*') == MATCH_YES)
    1107              :     return MATCH_YES;
    1108              : 
    1109        22162 :   if (gfc_match_char (':') == MATCH_YES)
    1110              :     {
    1111         3462 :       if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
    1112              :         return MATCH_ERROR;
    1113              : 
    1114         3460 :       *deferred = true;
    1115              : 
    1116         3460 :       return MATCH_YES;
    1117              :     }
    1118              : 
    1119        18700 :   m = gfc_match_expr (expr);
    1120              : 
    1121        18700 :   if (m == MATCH_NO || m == MATCH_ERROR)
    1122              :     return m;
    1123              : 
    1124        18695 :   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        18689 :   p = gfc_copy_expr (*expr);
    1129        18689 :   if (gfc_is_constant_expr (p) && gfc_simplify_expr (p, 1))
    1130        15494 :     gfc_replace_expr (*expr, p);
    1131              :   else
    1132         3195 :     gfc_free_expr (p);
    1133              : 
    1134        18689 :   if ((*expr)->expr_type == EXPR_FUNCTION)
    1135              :     {
    1136         1033 :       if ((*expr)->ts.type == BT_INTEGER
    1137         1032 :           || ((*expr)->ts.type == BT_UNKNOWN
    1138         1032 :               && strcmp((*expr)->symtree->name, "null") != 0))
    1139              :         return MATCH_YES;
    1140              : 
    1141            2 :       goto syntax;
    1142              :     }
    1143        17656 :   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        15355 :       if ((*expr)->ts.type == BT_INTEGER)
    1152              :         {
    1153        15337 :           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         2301 :   else if ((*expr)->expr_type == EXPR_ARRAY)
    1160            8 :     goto syntax;
    1161         2293 :   else if ((*expr)->expr_type == EXPR_VARIABLE)
    1162              :     {
    1163         1626 :       bool t;
    1164         1626 :       gfc_expr *e;
    1165              : 
    1166         1626 :       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         1626 :       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         1624 :       t = gfc_reduce_init_expr (e);
    1176              : 
    1177         1624 :       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         1617 :       if ((e->ref && e->ref->type == REF_ARRAY
    1188            4 :            && e->ref->u.ar.type != AR_ELEMENT)
    1189         1616 :           || (!e->ref && e->expr_type == EXPR_ARRAY))
    1190              :         {
    1191            2 :           gfc_free_expr (e);
    1192            2 :           goto syntax;
    1193              :         }
    1194              : 
    1195         1615 :       gfc_free_expr (e);
    1196              :     }
    1197              : 
    1198        17619 :   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        65199 : match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
    1214              : {
    1215        65199 :   int length;
    1216        65199 :   match m;
    1217              : 
    1218        65199 :   *deferred = false;
    1219        65199 :   m = gfc_match_char ('*');
    1220        65199 :   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       294255 : find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
    1278              : {
    1279       294255 :   gfc_state_data *s;
    1280       294255 :   gfc_symtree *st;
    1281       294255 :   int i;
    1282              : 
    1283       294255 :   i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
    1284       294255 :   if (i == 0)
    1285              :     {
    1286       294255 :       *result = st ? st->n.sym : NULL;
    1287       294255 :       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        67036 : get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
    1322              : {
    1323        67036 :   gfc_symtree *st;
    1324        67036 :   gfc_symbol *sym;
    1325        67036 :   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        67036 :   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        66776 :     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
    1371              : 
    1372        67036 :   if (rc)
    1373              :     return rc;
    1374              : 
    1375        67035 :   sym = *result;
    1376        67035 :   if (sym->attr.proc == PROC_ST_FUNCTION)
    1377              :     return rc;
    1378              : 
    1379        67034 :   if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
    1380              :     {
    1381          511 :       gfc_actual_arglist *parms = NULL;
    1382              :       /* Create a partially populated interface symbol to carry the
    1383              :          characteristics of the procedure and the result.  */
    1384          511 :       sym->tlink = gfc_new_symbol (name, sym->ns);
    1385          511 :       gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
    1386          511 :       gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
    1387          511 :       if (sym->attr.dimension)
    1388           17 :         sym->tlink->as = gfc_copy_array_spec (sym->as);
    1389              : 
    1390              :       /* The result parameters coming from the interface have to be copied
    1391              :          to the procedure symbol itself because of the modifications to
    1392              :          the result further downstream. This is required in trans-decl.cc
    1393              :          when the result is initialized.  */
    1394          511 :       if (sym->result && IS_PDT (sym->result) && sym->result->param_list)
    1395           40 :           parms = gfc_copy_actual_arglist (sym->result->param_list);
    1396              : 
    1397              :       /* Ideally, at this point, a copy would be made of the formal
    1398              :          arguments and their namespace. However, this does not appear
    1399              :          to be necessary, albeit at the expense of not being able to
    1400              :          use gfc_compare_interfaces directly.  */
    1401              : 
    1402          511 :       if (sym->result && sym->result != sym)
    1403              :         {
    1404          143 :           sym->tlink->result = sym->result;
    1405          143 :           sym->tlink->param_list = parms;
    1406          143 :           sym->result = NULL;
    1407              :         }
    1408          368 :       else if (sym->result)
    1409              :         {
    1410           93 :           sym->tlink->result = sym->tlink;
    1411           93 :           sym->tlink->param_list = parms;
    1412              :         }
    1413              :     }
    1414        66523 :   else if (sym && !sym->gfc_new
    1415        25634 :            && gfc_current_state () != COMP_INTERFACE)
    1416              :     {
    1417              :       /* Trap another encompassed procedure with the same name.  All
    1418              :          these conditions are necessary to avoid picking up an entry
    1419              :          whose name clashes with that of the encompassing procedure;
    1420              :          this is handled using gsymbols to register unique, globally
    1421              :          accessible names.  */
    1422        24292 :       if (sym->attr.flavor != 0
    1423        22209 :           && sym->attr.proc != 0
    1424         2446 :           && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
    1425            7 :           && sym->attr.if_source != IFSRC_UNKNOWN)
    1426              :         {
    1427            7 :           gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1428              :                          name, &sym->declared_at);
    1429            7 :           return true;
    1430              :         }
    1431        24285 :       if (sym->attr.flavor != 0
    1432        22202 :           && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
    1433              :         {
    1434            1 :           gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1435              :                          name, &sym->declared_at);
    1436            1 :           return true;
    1437              :         }
    1438              : 
    1439        24284 :       if (sym->attr.external && sym->attr.procedure
    1440            2 :           && gfc_current_state () == COMP_CONTAINS)
    1441              :         {
    1442            1 :           gfc_error_now ("Contained procedure %qs at %C clashes with "
    1443              :                          "procedure defined at %L",
    1444              :                          name, &sym->declared_at);
    1445            1 :           return true;
    1446              :         }
    1447              : 
    1448              :       /* Trap a procedure with a name the same as interface in the
    1449              :          encompassing scope.  */
    1450        24283 :       if (sym->attr.generic != 0
    1451           60 :           && (sym->attr.subroutine || sym->attr.function)
    1452            1 :           && !sym->attr.mod_proc)
    1453              :         {
    1454            1 :           gfc_error_now ("Name %qs at %C is already defined"
    1455              :                          " as a generic interface at %L",
    1456              :                          name, &sym->declared_at);
    1457            1 :           return true;
    1458              :         }
    1459              : 
    1460              :       /* Trap declarations of attributes in encompassing scope.  The
    1461              :          signature for this is that ts.kind is nonzero for no-CLASS
    1462              :          entity.  For a CLASS entity, ts.kind is zero.  */
    1463        24282 :       if ((sym->ts.kind != 0
    1464        23909 :            || sym->ts.type == BT_CLASS
    1465        23908 :            || sym->ts.type == BT_DERIVED)
    1466          397 :           && !sym->attr.implicit_type
    1467          396 :           && sym->attr.proc == 0
    1468          378 :           && gfc_current_ns->parent != NULL
    1469          138 :           && sym->attr.access == 0
    1470          136 :           && !module_fcn_entry)
    1471              :         {
    1472            5 :           gfc_error_now ("Procedure %qs at %C has an explicit interface "
    1473              :                        "from a previous declaration",  name);
    1474            5 :           return true;
    1475              :         }
    1476              :     }
    1477              : 
    1478              :   /* F2023: C1247 (R1526) MODULE shall appear only in the function-stmt or
    1479              :      subroutine-stmt of a module subprogram or of a nonabstract interface
    1480              :      body that is declared in the scoping unit of a module or submodule.  */
    1481        67019 :   if (sym->attr.external
    1482           92 :       && (sym->attr.subroutine || sym->attr.function)
    1483           91 :       && sym->attr.if_source == IFSRC_IFBODY
    1484           91 :       && !current_attr.module_procedure
    1485            3 :       && sym->attr.proc == PROC_MODULE
    1486            3 :       && gfc_state_stack->state == COMP_CONTAINS)
    1487            1 :     gfc_error_now ("Procedure %qs defined in interface body at %L "
    1488              :                    "clashes with internal procedure defined at %C",
    1489              :                    name, &sym->declared_at);
    1490              : 
    1491              :   /* This is the converse requirement: The separate-module-subprogram for a
    1492              :      module procedure shall have the MODULE prefix or be declared a MODULE
    1493              :      PROCEDURE, otherwise it would be ambiguous.  */
    1494        67019 :   if (sym->attr.module_procedure
    1495          511 :       && (sym->attr.subroutine || sym->attr.function)
    1496          511 :       && sym->attr.if_source == IFSRC_IFBODY
    1497          511 :       && !current_attr.module_procedure
    1498            4 :       && sym->attr.proc == PROC_MODULE
    1499            4 :       && gfc_state_stack->state == COMP_CONTAINS
    1500            2 :       && gfc_state_stack->previous
    1501            2 :       && gfc_state_stack->previous->state == COMP_SUBMODULE)
    1502            1 :     gfc_error_now ("Procedure %qs at %C requires the MODULE prefix because "
    1503              :                    "it is a module procedure declared in module %qs",
    1504            1 :                    name, sym->module ? sym->module : "");
    1505              : 
    1506        67019 :   if (sym && !sym->gfc_new
    1507        26130 :       && sym->attr.flavor != FL_UNKNOWN
    1508        23646 :       && sym->attr.referenced == 0 && sym->attr.subroutine == 1
    1509          245 :       && gfc_state_stack->state == COMP_CONTAINS
    1510          240 :       && gfc_state_stack->previous->state == COMP_SUBROUTINE)
    1511              :     {
    1512            1 :       gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1513              :                      name, &sym->declared_at);
    1514            1 :       return true;
    1515              :     }
    1516              : 
    1517        67018 :   if (gfc_current_ns->parent == NULL || *result == NULL)
    1518              :     return rc;
    1519              : 
    1520              :   /* Module function entries will already have a symtree in
    1521              :      the current namespace but will need one at module level.  */
    1522        54629 :   if (module_fcn_entry)
    1523              :     {
    1524              :       /* Present if entry is declared to be a module procedure.  */
    1525          258 :       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
    1526          258 :       if (st == NULL)
    1527          217 :         st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
    1528              :     }
    1529              :   else
    1530        54371 :     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
    1531              : 
    1532        54629 :   st->n.sym = sym;
    1533        54629 :   sym->refs++;
    1534              : 
    1535              :   /* See if the procedure should be a module procedure.  */
    1536              : 
    1537        54629 :   if (((sym->ns->proc_name != NULL
    1538        54629 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
    1539        22428 :         && sym->attr.proc != PROC_MODULE)
    1540        54629 :        || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
    1541        74129 :       && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
    1542              :     rc = 2;
    1543              : 
    1544              :   return rc;
    1545              : }
    1546              : 
    1547              : 
    1548              : /* Verify that the given symbol representing a parameter is C
    1549              :    interoperable, by checking to see if it was marked as such after
    1550              :    its declaration.  If the given symbol is not interoperable, a
    1551              :    warning is reported, thus removing the need to return the status to
    1552              :    the calling function.  The standard does not require the user use
    1553              :    one of the iso_c_binding named constants to declare an
    1554              :    interoperable parameter, but we can't be sure if the param is C
    1555              :    interop or not if the user doesn't.  For example, integer(4) may be
    1556              :    legal Fortran, but doesn't have meaning in C.  It may interop with
    1557              :    a number of the C types, which causes a problem because the
    1558              :    compiler can't know which one.  This code is almost certainly not
    1559              :    portable, and the user will get what they deserve if the C type
    1560              :    across platforms isn't always interoperable with integer(4).  If
    1561              :    the user had used something like integer(c_int) or integer(c_long),
    1562              :    the compiler could have automatically handled the varying sizes
    1563              :    across platforms.  */
    1564              : 
    1565              : bool
    1566        17668 : gfc_verify_c_interop_param (gfc_symbol *sym)
    1567              : {
    1568        17668 :   int is_c_interop = 0;
    1569        17668 :   bool retval = true;
    1570              : 
    1571              :   /* We check implicitly typed variables in symbol.cc:gfc_set_default_type().
    1572              :      Don't repeat the checks here.  */
    1573        17668 :   if (sym->attr.implicit_type)
    1574              :     return true;
    1575              : 
    1576              :   /* For subroutines or functions that are passed to a BIND(C) procedure,
    1577              :      they're interoperable if they're BIND(C) and their params are all
    1578              :      interoperable.  */
    1579        17668 :   if (sym->attr.flavor == FL_PROCEDURE)
    1580              :     {
    1581            4 :       if (sym->attr.is_bind_c == 0)
    1582              :         {
    1583            0 :           gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
    1584              :                          "attribute to be C interoperable", sym->name,
    1585              :                          &(sym->declared_at));
    1586            0 :           return false;
    1587              :         }
    1588              :       else
    1589              :         {
    1590            4 :           if (sym->attr.is_c_interop == 1)
    1591              :             /* We've already checked this procedure; don't check it again.  */
    1592              :             return true;
    1593              :           else
    1594            4 :             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
    1595            4 :                                       sym->common_block);
    1596              :         }
    1597              :     }
    1598              : 
    1599              :   /* See if we've stored a reference to a procedure that owns sym.  */
    1600        17664 :   if (sym->ns != NULL && sym->ns->proc_name != NULL)
    1601              :     {
    1602        17664 :       if (sym->ns->proc_name->attr.is_bind_c == 1)
    1603              :         {
    1604        17625 :           bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;
    1605        17625 :           bool f2018_added = false;
    1606              : 
    1607        17625 :           is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
    1608              : 
    1609              :           /* F2018:18.3.6 has the following text:
    1610              :              "(5) any dummy argument without the VALUE attribute corresponds to
    1611              :              a formal parameter of the prototype that is of a pointer type, and
    1612              :              either
    1613              :              • the dummy argument is interoperable with an entity of the
    1614              :              referenced type (ISO/IEC 9899:2011, 6.2.5, 7.19, and 7.20.1) of
    1615              :              the formal parameter (this is equivalent to the F2008 text),
    1616              :              • the dummy argument is a nonallocatable nonpointer variable of
    1617              :              type CHARACTER with assumed character length and the formal
    1618              :              parameter is a pointer to CFI_cdesc_t,
    1619              :              • the dummy argument is allocatable, assumed-shape, assumed-rank,
    1620              :              or a pointer without the CONTIGUOUS attribute, and the formal
    1621              :              parameter is a pointer to CFI_cdesc_t, or
    1622              :              • the dummy argument is assumed-type and not allocatable,
    1623              :              assumed-shape, assumed-rank, or a pointer, and the formal
    1624              :              parameter is a pointer to void,"  */
    1625         3731 :           if (is_c_interop == 0 && !sym->attr.value && f2018_allowed)
    1626              :             {
    1627         2364 :               bool as_ar = (sym->as
    1628         2364 :                             && (sym->as->type == AS_ASSUMED_SHAPE
    1629         2117 :                                 || sym->as->type == AS_ASSUMED_RANK));
    1630         4728 :               bool cond1 = (sym->ts.type == BT_CHARACTER
    1631         1565 :                             && !(sym->ts.u.cl && sym->ts.u.cl->length)
    1632          905 :                             && !sym->attr.allocatable
    1633         3251 :                             && !sym->attr.pointer);
    1634         4728 :               bool cond2 = (sym->attr.allocatable
    1635         2267 :                             || as_ar
    1636         3389 :                             || (IS_POINTER (sym) && !sym->attr.contiguous));
    1637         4728 :               bool cond3 = (sym->ts.type == BT_ASSUMED
    1638            0 :                             && !sym->attr.allocatable
    1639            0 :                             && !sym->attr.pointer
    1640         2364 :                             && !as_ar);
    1641         2364 :               f2018_added = cond1 || cond2 || cond3;
    1642              :             }
    1643              : 
    1644        17625 :           if (is_c_interop != 1 && !f2018_added)
    1645              :             {
    1646              :               /* Make personalized messages to give better feedback.  */
    1647         1837 :               if (sym->ts.type == BT_DERIVED)
    1648            1 :                 gfc_error ("Variable %qs at %L is a dummy argument to the "
    1649              :                            "BIND(C) procedure %qs but is not C interoperable "
    1650              :                            "because derived type %qs is not C interoperable",
    1651              :                            sym->name, &(sym->declared_at),
    1652            1 :                            sym->ns->proc_name->name,
    1653            1 :                            sym->ts.u.derived->name);
    1654         1836 :               else if (sym->ts.type == BT_CLASS)
    1655            6 :                 gfc_error ("Variable %qs at %L is a dummy argument to the "
    1656              :                            "BIND(C) procedure %qs but is not C interoperable "
    1657              :                            "because it is polymorphic",
    1658              :                            sym->name, &(sym->declared_at),
    1659            6 :                            sym->ns->proc_name->name);
    1660         1830 :               else if (warn_c_binding_type)
    1661           39 :                 gfc_warning (OPT_Wc_binding_type,
    1662              :                              "Variable %qs at %L is a dummy argument of the "
    1663              :                              "BIND(C) procedure %qs but may not be C "
    1664              :                              "interoperable",
    1665              :                              sym->name, &(sym->declared_at),
    1666           39 :                              sym->ns->proc_name->name);
    1667              :             }
    1668              : 
    1669              :           /* Per F2018, 18.3.6 (5), pointer + contiguous is not permitted.  */
    1670        17625 :           if (sym->attr.pointer && sym->attr.contiguous)
    1671            2 :             gfc_error ("Dummy argument %qs at %L may not be a pointer with "
    1672              :                        "CONTIGUOUS attribute as procedure %qs is BIND(C)",
    1673            2 :                        sym->name, &sym->declared_at, sym->ns->proc_name->name);
    1674              : 
    1675              :           /* Per F2018, C1557, pointer/allocatable dummies to a bind(c)
    1676              :              procedure that are default-initialized are not permitted.  */
    1677        16985 :           if ((sym->attr.pointer || sym->attr.allocatable)
    1678         1041 :               && sym->ts.type == BT_DERIVED
    1679        18003 :               && gfc_has_default_initializer (sym->ts.u.derived))
    1680              :             {
    1681            8 :               gfc_error ("Default-initialized dummy argument %qs with %s "
    1682              :                          "attribute at %L is not permitted in BIND(C) "
    1683              :                          "procedure %qs", sym->name,
    1684            4 :                          (sym->attr.pointer ? "POINTER" : "ALLOCATABLE"),
    1685            4 :                          &sym->declared_at, sym->ns->proc_name->name);
    1686            4 :               retval = false;
    1687              :             }
    1688              : 
    1689              :           /* Character strings are only C interoperable if they have a
    1690              :              length of 1.  However, as an argument they are also interoperable
    1691              :              when passed as descriptor (which requires len=: or len=*).  */
    1692        17625 :           if (sym->ts.type == BT_CHARACTER)
    1693              :             {
    1694         2350 :               gfc_charlen *cl = sym->ts.u.cl;
    1695              : 
    1696         2350 :               if (sym->attr.allocatable || sym->attr.pointer)
    1697              :                 {
    1698              :                   /* F2018, 18.3.6 (6).  */
    1699          195 :                   if (!sym->ts.deferred)
    1700              :                     {
    1701           64 :                       if (sym->attr.allocatable)
    1702           32 :                         gfc_error ("Allocatable character dummy argument %qs "
    1703              :                                    "at %L must have deferred length as "
    1704              :                                    "procedure %qs is BIND(C)", sym->name,
    1705           32 :                                    &sym->declared_at, sym->ns->proc_name->name);
    1706              :                       else
    1707           32 :                         gfc_error ("Pointer character dummy argument %qs at %L "
    1708              :                                    "must have deferred length as procedure %qs "
    1709              :                                    "is BIND(C)", sym->name, &sym->declared_at,
    1710           32 :                                    sym->ns->proc_name->name);
    1711              :                       retval = false;
    1712              :                     }
    1713          131 :                   else if (!gfc_notify_std (GFC_STD_F2018,
    1714              :                                             "Deferred-length character dummy "
    1715              :                                             "argument %qs at %L of procedure "
    1716              :                                             "%qs with BIND(C) attribute",
    1717              :                                             sym->name, &sym->declared_at,
    1718          131 :                                             sym->ns->proc_name->name))
    1719        17625 :                     retval = false;
    1720              :                 }
    1721         2155 :               else if (sym->attr.value
    1722          360 :                        && (!cl || !cl->length
    1723          360 :                            || cl->length->expr_type != EXPR_CONSTANT
    1724          360 :                            || mpz_cmp_si (cl->length->value.integer, 1) != 0))
    1725              :                 {
    1726            1 :                   gfc_error ("Character dummy argument %qs at %L must be "
    1727              :                              "of length 1 as it has the VALUE attribute",
    1728              :                              sym->name, &sym->declared_at);
    1729            1 :                   retval = false;
    1730              :                 }
    1731         2154 :               else if (!cl || !cl->length)
    1732              :                 {
    1733              :                   /* Assumed length; F2018, 18.3.6 (5)(2).
    1734              :                      Uses the CFI array descriptor - also for scalars and
    1735              :                      explicit-size/assumed-size arrays.  */
    1736          959 :                   if (!gfc_notify_std (GFC_STD_F2018,
    1737              :                                       "Assumed-length character dummy argument "
    1738              :                                       "%qs at %L of procedure %qs with BIND(C) "
    1739              :                                       "attribute", sym->name, &sym->declared_at,
    1740          959 :                                       sym->ns->proc_name->name))
    1741        17625 :                     retval = false;
    1742              :                 }
    1743         1195 :               else if (cl->length->expr_type != EXPR_CONSTANT
    1744          881 :                        || mpz_cmp_si (cl->length->value.integer, 1) != 0)
    1745              :                 {
    1746              :                   /* F2018, 18.3.6, (5), item 4.  */
    1747          653 :                   if (!sym->attr.dimension
    1748          645 :                       || sym->as->type == AS_ASSUMED_SIZE
    1749          639 :                       || sym->as->type == AS_EXPLICIT)
    1750              :                     {
    1751           20 :                       gfc_error ("Character dummy argument %qs at %L must be "
    1752              :                                  "of constant length of one or assumed length, "
    1753              :                                  "unless it has assumed shape or assumed rank, "
    1754              :                                  "as procedure %qs has the BIND(C) attribute",
    1755              :                                  sym->name, &sym->declared_at,
    1756           20 :                                  sym->ns->proc_name->name);
    1757           20 :                       retval = false;
    1758              :                     }
    1759              :                   /* else: valid only since F2018 - and an assumed-shape/rank
    1760              :                      array; however, gfc_notify_std is already called when
    1761              :                      those array types are used. Thus, silently accept F200x. */
    1762              :                 }
    1763              :             }
    1764              : 
    1765              :           /* We have to make sure that any param to a bind(c) routine does
    1766              :              not have the allocatable, pointer, or optional attributes,
    1767              :              according to J3/04-007, section 5.1.  */
    1768        17625 :           if (sym->attr.allocatable == 1
    1769        18026 :               && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
    1770              :                                   "ALLOCATABLE attribute in procedure %qs "
    1771              :                                   "with BIND(C)", sym->name,
    1772              :                                   &(sym->declared_at),
    1773          401 :                                   sym->ns->proc_name->name))
    1774              :             retval = false;
    1775              : 
    1776        17625 :           if (sym->attr.pointer == 1
    1777        18265 :               && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
    1778              :                                   "POINTER attribute in procedure %qs "
    1779              :                                   "with BIND(C)", sym->name,
    1780              :                                   &(sym->declared_at),
    1781          640 :                                   sym->ns->proc_name->name))
    1782              :             retval = false;
    1783              : 
    1784        17625 :           if (sym->attr.optional == 1 && sym->attr.value)
    1785              :             {
    1786            9 :               gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
    1787              :                          "and the VALUE attribute because procedure %qs "
    1788              :                          "is BIND(C)", sym->name, &(sym->declared_at),
    1789            9 :                          sym->ns->proc_name->name);
    1790            9 :               retval = false;
    1791              :             }
    1792        17616 :           else if (sym->attr.optional == 1
    1793        18570 :                    && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
    1794              :                                        "at %L with OPTIONAL attribute in "
    1795              :                                        "procedure %qs which is BIND(C)",
    1796              :                                        sym->name, &(sym->declared_at),
    1797          954 :                                        sym->ns->proc_name->name))
    1798              :             retval = false;
    1799              : 
    1800              :           /* Make sure that if it has the dimension attribute, that it is
    1801              :              either assumed size or explicit shape. Deferred shape is already
    1802              :              covered by the pointer/allocatable attribute.  */
    1803         5555 :           if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
    1804        18959 :               && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
    1805              :                                   "at %L as dummy argument to the BIND(C) "
    1806              :                                   "procedure %qs at %L", sym->name,
    1807              :                                   &(sym->declared_at),
    1808              :                                   sym->ns->proc_name->name,
    1809         1334 :                                   &(sym->ns->proc_name->declared_at)))
    1810              :             retval = false;
    1811              :         }
    1812              :     }
    1813              : 
    1814              :   return retval;
    1815              : }
    1816              : 
    1817              : 
    1818              : 
    1819              : /* Function called by variable_decl() that adds a name to the symbol table.  */
    1820              : 
    1821              : static bool
    1822       272184 : build_sym (const char *name, int elem, gfc_charlen *cl, bool cl_deferred,
    1823              :            gfc_array_spec **as, locus *var_locus)
    1824              : {
    1825       272184 :   symbol_attribute attr;
    1826       272184 :   gfc_symbol *sym;
    1827       272184 :   int upper;
    1828       272184 :   gfc_symtree *st, *host_st = NULL;
    1829              : 
    1830              :   /* Symbols in a submodule are host associated from the parent module or
    1831              :      submodules. Therefore, they can be overridden by declarations in the
    1832              :      submodule scope. Deal with this by attaching the existing symbol to
    1833              :      a new symtree and recycling the old symtree with a new symbol...  */
    1834       272184 :   st = gfc_find_symtree (gfc_current_ns->sym_root, name);
    1835       272184 :   if (((st && st->import_only) || (gfc_current_ns->import_state == IMPORT_ALL))
    1836            3 :       && gfc_current_ns->parent)
    1837            3 :     host_st = gfc_find_symtree (gfc_current_ns->parent->sym_root, name);
    1838              : 
    1839       272184 :   if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
    1840           12 :       && st->n.sym != NULL
    1841           12 :       && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
    1842              :     {
    1843           12 :       gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
    1844           12 :       s->n.sym = st->n.sym;
    1845           12 :       sym = gfc_new_symbol (name, gfc_current_ns, var_locus);
    1846              : 
    1847           12 :       st->n.sym = sym;
    1848           12 :       sym->refs++;
    1849           12 :       gfc_set_sym_referenced (sym);
    1850           12 :     }
    1851              :   /* ...Check that F2018 IMPORT, ONLY and IMPORT, ALL statements, within the
    1852              :      current scope are not violated by local redeclarations. Note that there is
    1853              :      no need to guard for std >= F2018 because import_only and IMPORT_ALL are
    1854              :      only set for these standards.  */
    1855       272172 :   else if (host_st && host_st->n.sym
    1856            2 :            && host_st->n.sym != gfc_current_ns->proc_name
    1857            2 :            && !(st && st->n.sym
    1858            1 :                 && (st->n.sym->attr.dummy || st->n.sym->attr.result)))
    1859              :     {
    1860            2 :       gfc_error ("F2018: C8102 %s at %L is already imported by an %s "
    1861              :                  "statement and must not be re-declared", name, var_locus,
    1862            1 :                  (st && st->import_only) ? "IMPORT, ONLY" : "IMPORT, ALL");
    1863            2 :       return false;
    1864              :     }
    1865              :   /* ...Otherwise generate a new symtree and new symbol.  */
    1866       272170 :   else if (gfc_get_symbol (name, NULL, &sym, var_locus))
    1867              :     return false;
    1868              : 
    1869              :   /* Check if the name has already been defined as a type.  The
    1870              :      first letter of the symtree will be in upper case then.  Of
    1871              :      course, this is only necessary if the upper case letter is
    1872              :      actually different.  */
    1873              : 
    1874       272182 :   upper = TOUPPER(name[0]);
    1875       272182 :   if (upper != name[0])
    1876              :     {
    1877       271432 :       char u_name[GFC_MAX_SYMBOL_LEN + 1];
    1878       271432 :       gfc_symtree *st;
    1879              : 
    1880       271432 :       gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
    1881       271432 :       strcpy (u_name, name);
    1882       271432 :       u_name[0] = upper;
    1883              : 
    1884       271432 :       st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
    1885              : 
    1886              :       /* STRUCTURE types can alias symbol names */
    1887       271432 :       if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
    1888              :         {
    1889            1 :           gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
    1890              :                      &st->n.sym->declared_at);
    1891            1 :           return false;
    1892              :         }
    1893              :     }
    1894              : 
    1895              :   /* Start updating the symbol table.  Add basic type attribute if present.  */
    1896       272181 :   if (current_ts.type != BT_UNKNOWN
    1897       272181 :       && (sym->attr.implicit_type == 0
    1898          186 :           || !gfc_compare_types (&sym->ts, &current_ts))
    1899       544180 :       && !gfc_add_type (sym, &current_ts, var_locus))
    1900              :     {
    1901              :       /* Duplicate-type rejection can leave a fresh CHARACTER length node on
    1902              :          the namespace list before it is attached to any surviving symbol.
    1903              :          Drop only that unattached node; shared constant charlen nodes are
    1904              :          already reachable from earlier declarations.  PR82721.  */
    1905           27 :       if (current_ts.type == BT_CHARACTER && cl && elem == 1)
    1906              :         {
    1907            1 :           discard_pending_charlen (cl);
    1908            1 :           gfc_clear_ts (&current_ts);
    1909              :         }
    1910           26 :       else if (current_ts.type == BT_CHARACTER && cl && cl != current_ts.u.cl)
    1911            0 :         discard_pending_charlen (cl);
    1912              :       return false;
    1913              :     }
    1914              : 
    1915       272154 :   if (sym->ts.type == BT_CHARACTER)
    1916              :     {
    1917        29912 :       if (elem > 1)
    1918         4238 :         sym->ts.u.cl = gfc_new_charlen (sym->ns, cl);
    1919              :       else
    1920              :         sym->ts.u.cl = cl;
    1921        29912 :       sym->ts.deferred = cl_deferred;
    1922              :     }
    1923              : 
    1924              :   /* Add dimension attribute if present.  */
    1925       272154 :   if (!gfc_set_array_spec (sym, *as, var_locus))
    1926              :     return false;
    1927       272152 :   *as = NULL;
    1928              : 
    1929              :   /* Add attribute to symbol.  The copy is so that we can reset the
    1930              :      dimension attribute.  */
    1931       272152 :   attr = current_attr;
    1932       272152 :   attr.dimension = 0;
    1933       272152 :   attr.codimension = 0;
    1934              : 
    1935       272152 :   if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
    1936              :     return false;
    1937              : 
    1938              :   /* Finish any work that may need to be done for the binding label,
    1939              :      if it's a bind(c).  The bind(c) attr is found before the symbol
    1940              :      is made, and before the symbol name (for data decls), so the
    1941              :      current_ts is holding the binding label, or nothing if the
    1942              :      name= attr wasn't given.  Therefore, test here if we're dealing
    1943              :      with a bind(c) and make sure the binding label is set correctly.  */
    1944       272140 :   if (sym->attr.is_bind_c == 1)
    1945              :     {
    1946         1902 :       if (!sym->binding_label)
    1947              :         {
    1948              :           /* Set the binding label and verify that if a NAME= was specified
    1949              :              then only one identifier was in the entity-decl-list.  */
    1950          137 :           if (!set_binding_label (&sym->binding_label, sym->name,
    1951              :                                   num_idents_on_line))
    1952              :             return false;
    1953              :         }
    1954              :     }
    1955              : 
    1956              :   /* See if we know we're in a common block, and if it's a bind(c)
    1957              :      common then we need to make sure we're an interoperable type.  */
    1958       272138 :   if (sym->attr.in_common == 1)
    1959              :     {
    1960              :       /* Test the common block object.  */
    1961          614 :       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
    1962            6 :           && sym->ts.is_c_interop != 1)
    1963              :         {
    1964            0 :           gfc_error_now ("Variable %qs in common block %qs at %C "
    1965              :                          "must be declared with a C interoperable "
    1966              :                          "kind since common block %qs is BIND(C)",
    1967              :                          sym->name, sym->common_block->name,
    1968            0 :                          sym->common_block->name);
    1969            0 :           gfc_clear_error ();
    1970              :         }
    1971              :     }
    1972              : 
    1973       272138 :   sym->attr.implied_index = 0;
    1974              : 
    1975              :   /* Use the parameter expressions for a parameterized derived type.  */
    1976       272138 :   if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    1977        39687 :       && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
    1978         1869 :     sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
    1979              : 
    1980       272138 :   if (sym->ts.type == BT_CLASS)
    1981        11981 :     return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
    1982              : 
    1983              :   return true;
    1984              : }
    1985              : 
    1986              : 
    1987              : /* Set character constant to the given length. The constant will be padded or
    1988              :    truncated.  If we're inside an array constructor without a typespec, we
    1989              :    additionally check that all elements have the same length; check_len -1
    1990              :    means no checking.  */
    1991              : 
    1992              : void
    1993        15497 : gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
    1994              :                                 gfc_charlen_t check_len)
    1995              : {
    1996        15497 :   gfc_char_t *s;
    1997        15497 :   gfc_charlen_t slen;
    1998              : 
    1999        15497 :   if (expr->ts.type != BT_CHARACTER)
    2000              :     return;
    2001              : 
    2002        15495 :   if (expr->expr_type != EXPR_CONSTANT)
    2003              :     {
    2004            1 :       gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
    2005            1 :       return;
    2006              :     }
    2007              : 
    2008        15494 :   slen = expr->value.character.length;
    2009        15494 :   if (len != slen)
    2010              :     {
    2011         2249 :       s = gfc_get_wide_string (len + 1);
    2012         2249 :       memcpy (s, expr->value.character.string,
    2013         2249 :               MIN (len, slen) * sizeof (gfc_char_t));
    2014         2249 :       if (len > slen)
    2015         1958 :         gfc_wide_memset (&s[slen], ' ', len - slen);
    2016              : 
    2017         2249 :       if (warn_character_truncation && slen > len)
    2018            1 :         gfc_warning_now (OPT_Wcharacter_truncation,
    2019              :                          "CHARACTER expression at %L is being truncated "
    2020              :                          "(%ld/%ld)", &expr->where,
    2021              :                          (long) slen, (long) len);
    2022              : 
    2023              :       /* Apply the standard by 'hand' otherwise it gets cleared for
    2024              :          initializers.  */
    2025         2249 :       if (check_len != -1 && slen != check_len)
    2026              :         {
    2027            3 :           if (!(gfc_option.allow_std & GFC_STD_GNU))
    2028            0 :             gfc_error_now ("The CHARACTER elements of the array constructor "
    2029              :                            "at %L must have the same length (%ld/%ld)",
    2030              :                            &expr->where, (long) slen,
    2031              :                            (long) check_len);
    2032              :           else
    2033            3 :             gfc_notify_std (GFC_STD_LEGACY,
    2034              :                             "The CHARACTER elements of the array constructor "
    2035              :                             "at %L must have the same length (%ld/%ld)",
    2036              :                             &expr->where, (long) slen,
    2037              :                             (long) check_len);
    2038              :         }
    2039              : 
    2040         2249 :       s[len] = '\0';
    2041         2249 :       free (expr->value.character.string);
    2042         2249 :       expr->value.character.string = s;
    2043         2249 :       expr->value.character.length = len;
    2044              :       /* If explicit representation was given, clear it
    2045              :          as it is no longer needed after padding.  */
    2046         2249 :       if (expr->representation.length)
    2047              :         {
    2048           45 :           expr->representation.length = 0;
    2049           45 :           free (expr->representation.string);
    2050           45 :           expr->representation.string = NULL;
    2051              :         }
    2052              :     }
    2053              : }
    2054              : 
    2055              : 
    2056              : /* Function to create and update the enumerator history
    2057              :    using the information passed as arguments.
    2058              :    Pointer "max_enum" is also updated, to point to
    2059              :    enum history node containing largest initializer.
    2060              : 
    2061              :    SYM points to the symbol node of enumerator.
    2062              :    INIT points to its enumerator value.  */
    2063              : 
    2064              : static void
    2065          543 : create_enum_history (gfc_symbol *sym, gfc_expr *init)
    2066              : {
    2067          543 :   enumerator_history *new_enum_history;
    2068          543 :   gcc_assert (sym != NULL && init != NULL);
    2069              : 
    2070          543 :   new_enum_history = XCNEW (enumerator_history);
    2071              : 
    2072          543 :   new_enum_history->sym = sym;
    2073          543 :   new_enum_history->initializer = init;
    2074          543 :   new_enum_history->next = NULL;
    2075              : 
    2076          543 :   if (enum_history == NULL)
    2077              :     {
    2078          160 :       enum_history = new_enum_history;
    2079          160 :       max_enum = enum_history;
    2080              :     }
    2081              :   else
    2082              :     {
    2083          383 :       new_enum_history->next = enum_history;
    2084          383 :       enum_history = new_enum_history;
    2085              : 
    2086          383 :       if (mpz_cmp (max_enum->initializer->value.integer,
    2087          383 :                    new_enum_history->initializer->value.integer) < 0)
    2088          381 :         max_enum = new_enum_history;
    2089              :     }
    2090          543 : }
    2091              : 
    2092              : 
    2093              : /* Function to free enum kind history.  */
    2094              : 
    2095              : void
    2096          175 : gfc_free_enum_history (void)
    2097              : {
    2098          175 :   enumerator_history *current = enum_history;
    2099          175 :   enumerator_history *next;
    2100              : 
    2101          718 :   while (current != NULL)
    2102              :     {
    2103          543 :       next = current->next;
    2104          543 :       free (current);
    2105          543 :       current = next;
    2106              :     }
    2107          175 :   max_enum = NULL;
    2108          175 :   enum_history = NULL;
    2109          175 : }
    2110              : 
    2111              : 
    2112              : /* Function to fix initializer character length if the length of the
    2113              :    symbol or component is constant.  */
    2114              : 
    2115              : static bool
    2116         2982 : fix_initializer_charlen (gfc_typespec *ts, gfc_expr *init)
    2117              : {
    2118         2982 :   if (!gfc_specification_expr (ts->u.cl->length))
    2119              :     return false;
    2120              : 
    2121         2982 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    2122              : 
    2123              :   /* resolve_charlen will complain later on if the length
    2124              :      is too large.  Just skip the initialization in that case.  */
    2125         2982 :   if (mpz_cmp (ts->u.cl->length->value.integer,
    2126         2982 :                gfc_integer_kinds[k].huge) <= 0)
    2127              :     {
    2128         2981 :       HOST_WIDE_INT len
    2129         2981 :                 = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    2130              : 
    2131         2981 :       if (init->expr_type == EXPR_CONSTANT)
    2132         2150 :         gfc_set_constant_character_len (len, init, -1);
    2133          831 :       else if (init->expr_type == EXPR_ARRAY)
    2134              :         {
    2135          812 :           gfc_constructor *cons;
    2136              : 
    2137              :           /* Build a new charlen to prevent simplification from
    2138              :              deleting the length before it is resolved.  */
    2139          812 :           init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2140          812 :           init->ts.u.cl->length = gfc_copy_expr (ts->u.cl->length);
    2141          812 :           cons = gfc_constructor_first (init->value.constructor);
    2142         5509 :           for (; cons; cons = gfc_constructor_next (cons))
    2143         3885 :             gfc_set_constant_character_len (len, cons->expr, -1);
    2144              :         }
    2145              :     }
    2146              : 
    2147              :   return true;
    2148              : }
    2149              : 
    2150              : 
    2151              : /* Function called by variable_decl() that adds an initialization
    2152              :    expression to a symbol.  */
    2153              : 
    2154              : static bool
    2155       280776 : add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus,
    2156              :                       gfc_charlen *saved_cl_list)
    2157              : {
    2158       280776 :   symbol_attribute attr;
    2159       280776 :   gfc_symbol *sym;
    2160       280776 :   gfc_expr *init;
    2161              : 
    2162       280776 :   init = *initp;
    2163       280776 :   if (find_special (name, &sym, false))
    2164              :     return false;
    2165              : 
    2166       280776 :   attr = sym->attr;
    2167              : 
    2168              :   /* If this symbol is confirming an implicit parameter type,
    2169              :      then an initialization expression is not allowed.  */
    2170       280776 :   if (attr.flavor == FL_PARAMETER && sym->value != NULL)
    2171              :     {
    2172            1 :       if (*initp != NULL)
    2173              :         {
    2174            0 :           gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
    2175              :                      sym->name);
    2176            0 :           return false;
    2177              :         }
    2178              :       else
    2179              :         return true;
    2180              :     }
    2181              : 
    2182       280775 :   if (init == NULL)
    2183              :     {
    2184              :       /* An initializer is required for PARAMETER declarations.  */
    2185       246232 :       if (attr.flavor == FL_PARAMETER)
    2186              :         {
    2187            1 :           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
    2188            1 :           return false;
    2189              :         }
    2190              :     }
    2191              :   else
    2192              :     {
    2193              :       /* If a variable appears in a DATA block, it cannot have an
    2194              :          initializer.  */
    2195        34543 :       if (sym->attr.data)
    2196              :         {
    2197            0 :           gfc_error ("Variable %qs at %C with an initializer already "
    2198              :                      "appears in a DATA statement", sym->name);
    2199            0 :           return false;
    2200              :         }
    2201              : 
    2202              :       /* Check if the assignment can happen. This has to be put off
    2203              :          until later for derived type variables and procedure pointers.  */
    2204        33247 :       if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
    2205        33224 :           && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
    2206        33174 :           && !sym->attr.proc_pointer
    2207        67605 :           && !gfc_check_assign_symbol (sym, NULL, init))
    2208              :         return false;
    2209              : 
    2210        34512 :       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
    2211         3657 :             && init->ts.type == BT_CHARACTER)
    2212              :         {
    2213              :           /* Update symbol character length according initializer.  */
    2214         3493 :           if (!gfc_check_assign_symbol (sym, NULL, init))
    2215              :             return false;
    2216              : 
    2217         3493 :           if (sym->ts.u.cl->length == NULL)
    2218              :             {
    2219          893 :               gfc_charlen_t clen;
    2220              :               /* If there are multiple CHARACTER variables declared on the
    2221              :                  same line, we don't want them to share the same length.  */
    2222          893 :               sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2223              : 
    2224          893 :               if (sym->attr.flavor == FL_PARAMETER)
    2225              :                 {
    2226          860 :                   if (init->expr_type == EXPR_CONSTANT)
    2227              :                     {
    2228          569 :                       clen = init->value.character.length;
    2229          569 :                       sym->ts.u.cl->length
    2230          569 :                                 = gfc_get_int_expr (gfc_charlen_int_kind,
    2231              :                                                     NULL, clen);
    2232              :                     }
    2233          291 :                   else if (init->expr_type == EXPR_ARRAY)
    2234              :                     {
    2235          291 :                       if (init->ts.u.cl && init->ts.u.cl->length)
    2236              :                         {
    2237          279 :                           const gfc_expr *length = init->ts.u.cl->length;
    2238          279 :                           if (length->expr_type != EXPR_CONSTANT)
    2239              :                             {
    2240            3 :                               gfc_error ("Cannot initialize parameter array "
    2241              :                                          "at %L "
    2242              :                                          "with variable length elements",
    2243              :                                          &sym->declared_at);
    2244              : 
    2245              :                               /* This rejection path can leave several
    2246              :                                  declaration-local charlens on cl_list,
    2247              :                                  including the replacement symbol charlen and
    2248              :                                  the array-constructor typespec charlen.
    2249              :                                  Clear the surviving owners first, then drop
    2250              :                                  only the nodes created by this declaration.  */
    2251            3 :                               sym->ts.u.cl = NULL;
    2252            3 :                               init->ts.u.cl = NULL;
    2253            3 :                               discard_pending_charlens (saved_cl_list);
    2254            3 :                               return false;
    2255              :                             }
    2256          276 :                           clen = mpz_get_si (length->value.integer);
    2257          276 :                         }
    2258           12 :                       else if (init->value.constructor)
    2259              :                         {
    2260           12 :                           gfc_constructor *c;
    2261           12 :                           c = gfc_constructor_first (init->value.constructor);
    2262           12 :                           clen = c->expr->value.character.length;
    2263              :                         }
    2264              :                       else
    2265            0 :                           gcc_unreachable ();
    2266          288 :                       sym->ts.u.cl->length
    2267          288 :                                 = gfc_get_int_expr (gfc_charlen_int_kind,
    2268              :                                                     NULL, clen);
    2269              :                     }
    2270            0 :                   else if (init->ts.u.cl && init->ts.u.cl->length)
    2271            0 :                     sym->ts.u.cl->length =
    2272            0 :                                 gfc_copy_expr (init->ts.u.cl->length);
    2273              :                 }
    2274              :             }
    2275              :           /* Update initializer character length according to symbol.  */
    2276         2600 :           else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2277         2600 :                    && !fix_initializer_charlen (&sym->ts, init))
    2278              :             return false;
    2279              :         }
    2280              : 
    2281        34509 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
    2282         3876 :           && sym->as->rank && init->rank && init->rank != sym->as->rank)
    2283              :         {
    2284            3 :           gfc_error ("Rank mismatch of array at %L and its initializer "
    2285              :                      "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
    2286            3 :           return false;
    2287              :         }
    2288              : 
    2289              :       /* If sym is implied-shape, set its upper bounds from init.  */
    2290        34506 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
    2291         3873 :           && sym->as && sym->as->type == AS_IMPLIED_SHAPE)
    2292              :         {
    2293         1043 :           int dim;
    2294              : 
    2295         1043 :           if (init->rank == 0)
    2296              :             {
    2297            1 :               gfc_error ("Cannot initialize implied-shape array at %L"
    2298              :                          " with scalar", &sym->declared_at);
    2299            1 :               return false;
    2300              :             }
    2301              : 
    2302              :           /* The shape may be NULL for EXPR_ARRAY, set it.  */
    2303         1042 :           if (init->shape == NULL)
    2304              :             {
    2305            5 :               if (init->expr_type != EXPR_ARRAY)
    2306              :                 {
    2307            2 :                   gfc_error ("Bad shape of initializer at %L", &init->where);
    2308            2 :                   return false;
    2309              :                 }
    2310              : 
    2311            3 :               init->shape = gfc_get_shape (1);
    2312            3 :               if (!gfc_array_size (init, &init->shape[0]))
    2313              :                 {
    2314            1 :                   gfc_error ("Cannot determine shape of initializer at %L",
    2315              :                              &init->where);
    2316            1 :                   free (init->shape);
    2317            1 :                   init->shape = NULL;
    2318            1 :                   return false;
    2319              :                 }
    2320              :             }
    2321              : 
    2322         2179 :           for (dim = 0; dim < sym->as->rank; ++dim)
    2323              :             {
    2324         1141 :               int k;
    2325         1141 :               gfc_expr *e, *lower;
    2326              : 
    2327         1141 :               lower = sym->as->lower[dim];
    2328              : 
    2329              :               /* If the lower bound is an array element from another
    2330              :                  parameterized array, then it is marked with EXPR_VARIABLE and
    2331              :                  is an initialization expression.  Try to reduce it.  */
    2332         1141 :               if (lower->expr_type == EXPR_VARIABLE)
    2333            7 :                 gfc_reduce_init_expr (lower);
    2334              : 
    2335         1141 :               if (lower->expr_type == EXPR_CONSTANT)
    2336              :                 {
    2337              :                   /* All dimensions must be without upper bound.  */
    2338         1140 :                   gcc_assert (!sym->as->upper[dim]);
    2339              : 
    2340         1140 :                   k = lower->ts.kind;
    2341         1140 :                   e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
    2342         1140 :                   mpz_add (e->value.integer, lower->value.integer,
    2343         1140 :                            init->shape[dim]);
    2344         1140 :                   mpz_sub_ui (e->value.integer, e->value.integer, 1);
    2345         1140 :                   sym->as->upper[dim] = e;
    2346              :                 }
    2347              :               else
    2348              :                 {
    2349            1 :                   gfc_error ("Non-constant lower bound in implied-shape"
    2350              :                              " declaration at %L", &lower->where);
    2351            1 :                   return false;
    2352              :                 }
    2353              :             }
    2354              : 
    2355         1038 :           sym->as->type = AS_EXPLICIT;
    2356              :         }
    2357              : 
    2358              :       /* Ensure that explicit bounds are simplified.  */
    2359        34501 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
    2360         3868 :           && sym->as && sym->as->type == AS_EXPLICIT)
    2361              :         {
    2362         8583 :           for (int dim = 0; dim < sym->as->rank; ++dim)
    2363              :             {
    2364         4727 :               gfc_expr *e;
    2365              : 
    2366         4727 :               e = sym->as->lower[dim];
    2367         4727 :               if (e->expr_type != EXPR_CONSTANT)
    2368           12 :                 gfc_reduce_init_expr (e);
    2369              : 
    2370         4727 :               e = sym->as->upper[dim];
    2371         4727 :               if (e->expr_type != EXPR_CONSTANT)
    2372          106 :                 gfc_reduce_init_expr (e);
    2373              :             }
    2374              :         }
    2375              : 
    2376              :       /* Need to check if the expression we initialized this
    2377              :          to was one of the iso_c_binding named constants.  If so,
    2378              :          and we're a parameter (constant), let it be iso_c.
    2379              :          For example:
    2380              :          integer(c_int), parameter :: my_int = c_int
    2381              :          integer(my_int) :: my_int_2
    2382              :          If we mark my_int as iso_c (since we can see it's value
    2383              :          is equal to one of the named constants), then my_int_2
    2384              :          will be considered C interoperable.  */
    2385        34501 :       if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
    2386              :         {
    2387        29554 :           sym->ts.is_iso_c |= init->ts.is_iso_c;
    2388        29554 :           sym->ts.is_c_interop |= init->ts.is_c_interop;
    2389              :           /* attr bits needed for module files.  */
    2390        29554 :           sym->attr.is_iso_c |= init->ts.is_iso_c;
    2391        29554 :           sym->attr.is_c_interop |= init->ts.is_c_interop;
    2392        29554 :           if (init->ts.is_iso_c)
    2393          120 :             sym->ts.f90_type = init->ts.f90_type;
    2394              :         }
    2395              : 
    2396              :       /* Catch the case:  type(t), parameter :: x = z'1'.  */
    2397        34501 :       if (sym->ts.type == BT_DERIVED && init->ts.type == BT_BOZ)
    2398              :         {
    2399            1 :           gfc_error ("Entity %qs at %L is incompatible with a BOZ "
    2400              :                      "literal constant", name, &sym->declared_at);
    2401            1 :           return false;
    2402              :         }
    2403              : 
    2404              :       /* Add initializer.  Make sure we keep the ranks sane.  */
    2405        34500 :       if (sym->attr.dimension && init->rank == 0)
    2406              :         {
    2407         1331 :           mpz_t size;
    2408         1331 :           gfc_expr *array;
    2409         1331 :           int n;
    2410         1331 :           if (sym->attr.flavor == FL_PARAMETER
    2411          474 :               && gfc_is_constant_expr (init)
    2412          473 :               && (init->expr_type == EXPR_CONSTANT
    2413           48 :                   || init->expr_type == EXPR_STRUCTURE)
    2414         1804 :               && spec_size (sym->as, &size))
    2415              :             {
    2416          469 :               array = gfc_get_array_expr (init->ts.type, init->ts.kind,
    2417              :                                           &init->where);
    2418          469 :               if (init->ts.type == BT_DERIVED)
    2419           48 :                 array->ts.u.derived = init->ts.u.derived;
    2420        67637 :               for (n = 0; n < (int)mpz_get_si (size); n++)
    2421       134008 :                 gfc_constructor_append_expr (&array->value.constructor,
    2422              :                                              n == 0
    2423              :                                                 ? init
    2424        66840 :                                                 : gfc_copy_expr (init),
    2425              :                                              &init->where);
    2426              : 
    2427          469 :               array->shape = gfc_get_shape (sym->as->rank);
    2428         1064 :               for (n = 0; n < sym->as->rank; n++)
    2429          595 :                 spec_dimen_size (sym->as, n, &array->shape[n]);
    2430              : 
    2431          469 :               init = array;
    2432          469 :               mpz_clear (size);
    2433              :             }
    2434         1331 :           init->rank = sym->as->rank;
    2435         1331 :           init->corank = sym->as->corank;
    2436              :         }
    2437              : 
    2438        34500 :       sym->value = init;
    2439        34500 :       if (sym->attr.save == SAVE_NONE)
    2440        29593 :         sym->attr.save = SAVE_IMPLICIT;
    2441        34500 :       *initp = NULL;
    2442              :     }
    2443              : 
    2444              :   return true;
    2445              : }
    2446              : 
    2447              : 
    2448              : /* Function called by variable_decl() that adds a name to a structure
    2449              :    being built.  */
    2450              : 
    2451              : static bool
    2452        20530 : build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
    2453              :               gfc_array_spec **as)
    2454              : {
    2455        20530 :   gfc_state_data *s;
    2456        20530 :   gfc_component *c;
    2457              : 
    2458              :   /* F03:C438/C439. If the current symbol is of the same derived type that we're
    2459              :      constructing, it must have the pointer attribute.  */
    2460        20530 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    2461         3825 :       && current_ts.u.derived == gfc_current_block ()
    2462          303 :       && current_attr.pointer == 0)
    2463              :     {
    2464          142 :       if (current_attr.allocatable
    2465          142 :           && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
    2466              :                              "must have the POINTER attribute"))
    2467              :         {
    2468              :           return false;
    2469              :         }
    2470          141 :       else if (current_attr.allocatable == 0)
    2471              :         {
    2472            0 :           gfc_error ("Component at %C must have the POINTER attribute");
    2473            0 :           return false;
    2474              :         }
    2475              :     }
    2476              : 
    2477              :   /* F03:C437.  */
    2478        20529 :   if (current_ts.type == BT_CLASS
    2479          991 :       && !(current_attr.pointer || current_attr.allocatable))
    2480              :     {
    2481            5 :       gfc_error ("Component %qs with CLASS at %C must be allocatable "
    2482              :                  "or pointer", name);
    2483            5 :       return false;
    2484              :     }
    2485              : 
    2486        20524 :   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
    2487              :     {
    2488            0 :       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
    2489              :         {
    2490            0 :           gfc_error ("Array component of structure at %C must have explicit "
    2491              :                      "or deferred shape");
    2492            0 :           return false;
    2493              :         }
    2494              :     }
    2495              : 
    2496              :   /* If we are in a nested union/map definition, gfc_add_component will not
    2497              :      properly find repeated components because:
    2498              :        (i) gfc_add_component does a flat search, where components of unions
    2499              :            and maps are implicity chained so nested components may conflict.
    2500              :       (ii) Unions and maps are not linked as components of their parent
    2501              :            structures until after they are parsed.
    2502              :      For (i) we use gfc_find_component which searches recursively, and for (ii)
    2503              :      we search each block directly from the parse stack until we find the top
    2504              :      level structure.  */
    2505              : 
    2506        20524 :   s = gfc_state_stack;
    2507        20524 :   if (s->state == COMP_UNION || s->state == COMP_MAP)
    2508              :     {
    2509         1434 :       while (s->state == COMP_UNION || gfc_comp_struct (s->state))
    2510              :         {
    2511         1434 :           c = gfc_find_component (s->sym, name, true, true, NULL);
    2512         1434 :           if (c != NULL)
    2513              :             {
    2514            0 :               gfc_error_now ("Component %qs at %C already declared at %L",
    2515              :                              name, &c->loc);
    2516            0 :               return false;
    2517              :             }
    2518              :           /* Break after we've searched the entire chain.  */
    2519         1434 :           if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
    2520              :             break;
    2521         1000 :           s = s->previous;
    2522              :         }
    2523              :     }
    2524              : 
    2525        20524 :   if (!gfc_add_component (gfc_current_block(), name, &c))
    2526              :     return false;
    2527              : 
    2528        20518 :   c->ts = current_ts;
    2529        20518 :   if (c->ts.type == BT_CHARACTER)
    2530              :     {
    2531         2188 :       c->ts.u.cl = cl;
    2532              :       /* The component struct is not tracked by the symbol undo mechanism,
    2533              :          so free the charlen here to prevent a double-free.  */
    2534         2188 :       gfc_remove_saved_charlen (cl);
    2535              :     }
    2536              : 
    2537        20518 :   if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
    2538        16699 :       && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
    2539         2543 :       && saved_kind_expr != NULL)
    2540          441 :     c->kind_expr = gfc_copy_expr (saved_kind_expr);
    2541              : 
    2542        20518 :   c->attr = current_attr;
    2543              : 
    2544        20518 :   c->initializer = *init;
    2545        20518 :   *init = NULL;
    2546              : 
    2547              :   /* Update initializer character length according to component.  */
    2548         2188 :   if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length
    2549         1769 :       && c->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2550         1607 :       && c->initializer && c->initializer->ts.type == BT_CHARACTER
    2551        20903 :       && !fix_initializer_charlen (&c->ts, c->initializer))
    2552              :     return false;
    2553              : 
    2554        20518 :   c->as = *as;
    2555        20518 :   if (c->as != NULL)
    2556              :     {
    2557         5581 :       if (c->as->corank)
    2558          113 :         c->attr.codimension = 1;
    2559         5581 :       if (c->as->rank)
    2560         5500 :         c->attr.dimension = 1;
    2561              :     }
    2562        20518 :   *as = NULL;
    2563              : 
    2564        20518 :   gfc_apply_init (&c->ts, &c->attr, c->initializer);
    2565              : 
    2566              :   /* Convert a class, PDT component of a non-derived type to a specific instance
    2567              :      before gfc_build_class_symbol gets to work on it.  */
    2568        20518 :   if (c->ts.type == BT_CLASS
    2569          986 :       && !(gfc_current_block ()->attr.pdt_template
    2570          986 :            || gfc_current_block ()->attr.pdt_type)
    2571          986 :       && c->ts.u.derived->attr.pdt_template)
    2572              :     {
    2573           12 :       match m = gfc_get_pdt_instance (decl_type_param_list, &c->ts.u.derived, NULL);
    2574           12 :       if (m != MATCH_YES)
    2575              :         {
    2576            0 :           if (!gfc_error_check ())
    2577            0 :             gfc_error ("Parameterized component of a non-parameterized "
    2578              :                        "derived type at %C could not be converted to a valid "
    2579              :                        "instance");
    2580              :           return false;
    2581              :         }
    2582              :     }
    2583              : 
    2584              :   /* Check array components.  */
    2585        20518 :   if (!c->attr.dimension)
    2586        15018 :     goto scalar;
    2587              : 
    2588         5500 :   if (c->attr.pointer)
    2589              :     {
    2590          756 :       if (c->as->type != AS_DEFERRED)
    2591              :         {
    2592            5 :           gfc_error ("Pointer array component of structure at %C must have a "
    2593              :                      "deferred shape");
    2594            5 :           return false;
    2595              :         }
    2596              :     }
    2597         4744 :   else if (c->attr.allocatable)
    2598              :     {
    2599         2624 :       const char *err = G_("Allocatable component of structure at %C must have "
    2600              :                            "a deferred shape");
    2601         2624 :       if (c->as->type != AS_DEFERRED)
    2602              :         {
    2603           14 :           if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
    2604              :             {
    2605              :               /* Issue an immediate error and allow this component to pass for
    2606              :                  the sake of clean error recovery.  Set the error flag for the
    2607              :                  containing derived type so that finalizers are not built.  */
    2608            4 :               gfc_error_now (err);
    2609            4 :               s->sym->error = 1;
    2610            4 :               c->as->type = AS_DEFERRED;
    2611              :             }
    2612              :           else
    2613              :             {
    2614           10 :               gfc_error (err);
    2615           10 :               return false;
    2616              :             }
    2617              :         }
    2618              :     }
    2619              :   else
    2620              :     {
    2621         2120 :       if (c->as->type != AS_EXPLICIT)
    2622              :         {
    2623            7 :           gfc_error ("Array component of structure at %C must have an "
    2624              :                      "explicit shape");
    2625            7 :           return false;
    2626              :         }
    2627              :     }
    2628              : 
    2629         2113 : scalar:
    2630        20496 :   if (c->ts.type == BT_CLASS)
    2631          983 :     return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
    2632              : 
    2633        19513 :   if (c->attr.pdt_kind || c->attr.pdt_len)
    2634              :     {
    2635         1026 :       gfc_symbol *sym;
    2636         1026 :       gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
    2637              :                        0, &sym);
    2638         1026 :       if (sym == NULL)
    2639              :         {
    2640            0 :           gfc_error ("Type parameter %qs at %C has no corresponding entry "
    2641              :                      "in the type parameter name list at %L",
    2642            0 :                      c->name, &gfc_current_block ()->declared_at);
    2643            0 :           return false;
    2644              :         }
    2645         1026 :       sym->ts = c->ts;
    2646         1026 :       sym->attr.pdt_kind = c->attr.pdt_kind;
    2647         1026 :       sym->attr.pdt_len = c->attr.pdt_len;
    2648         1026 :       if (c->initializer)
    2649          505 :         sym->value = gfc_copy_expr (c->initializer);
    2650         1026 :       sym->attr.flavor = FL_VARIABLE;
    2651              :     }
    2652              : 
    2653        19513 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    2654         2833 :       && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
    2655          178 :       && decl_type_param_list)
    2656          178 :     c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
    2657              : 
    2658              :   return true;
    2659              : }
    2660              : 
    2661              : 
    2662              : /* Match a 'NULL()', and possibly take care of some side effects.  */
    2663              : 
    2664              : match
    2665         1852 : gfc_match_null (gfc_expr **result)
    2666              : {
    2667         1852 :   gfc_symbol *sym;
    2668         1852 :   match m, m2 = MATCH_NO;
    2669              : 
    2670         1852 :   if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
    2671              :     return MATCH_ERROR;
    2672              : 
    2673         1852 :   if (m == MATCH_NO)
    2674              :     {
    2675          565 :       locus old_loc;
    2676          565 :       char name[GFC_MAX_SYMBOL_LEN + 1];
    2677              : 
    2678          565 :       if ((m2 = gfc_match (" null (")) != MATCH_YES)
    2679          559 :         return m2;
    2680              : 
    2681            6 :       old_loc = gfc_current_locus;
    2682            6 :       if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
    2683              :         return MATCH_ERROR;
    2684            6 :       if (m2 != MATCH_YES
    2685            6 :           && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
    2686              :         return MATCH_ERROR;
    2687            6 :       if (m2 == MATCH_NO)
    2688              :         {
    2689            0 :           gfc_current_locus = old_loc;
    2690            0 :           return MATCH_NO;
    2691              :         }
    2692              :     }
    2693              : 
    2694              :   /* The NULL symbol now has to be/become an intrinsic function.  */
    2695         1293 :   if (gfc_get_symbol ("null", NULL, &sym))
    2696              :     {
    2697            0 :       gfc_error ("NULL() initialization at %C is ambiguous");
    2698            0 :       return MATCH_ERROR;
    2699              :     }
    2700              : 
    2701         1293 :   gfc_intrinsic_symbol (sym);
    2702              : 
    2703         1293 :   if (sym->attr.proc != PROC_INTRINSIC
    2704          914 :       && !(sym->attr.use_assoc && sym->attr.intrinsic)
    2705         2206 :       && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
    2706          913 :           || !gfc_add_function (&sym->attr, sym->name, NULL)))
    2707              :     return MATCH_ERROR;
    2708              : 
    2709         1293 :   *result = gfc_get_null_expr (&gfc_current_locus);
    2710              : 
    2711              :   /* Invalid per F2008, C512.  */
    2712         1293 :   if (m2 == MATCH_YES)
    2713              :     {
    2714            6 :       gfc_error ("NULL() initialization at %C may not have MOLD");
    2715            6 :       return MATCH_ERROR;
    2716              :     }
    2717              : 
    2718              :   return MATCH_YES;
    2719              : }
    2720              : 
    2721              : 
    2722              : /* Match the initialization expr for a data pointer or procedure pointer.  */
    2723              : 
    2724              : static match
    2725         1498 : match_pointer_init (gfc_expr **init, int procptr)
    2726              : {
    2727         1498 :   match m;
    2728              : 
    2729         1498 :   if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
    2730              :     {
    2731            1 :       gfc_error ("Initialization of pointer at %C is not allowed in "
    2732              :                  "a PURE procedure");
    2733            1 :       return MATCH_ERROR;
    2734              :     }
    2735         1497 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    2736              : 
    2737              :   /* Match NULL() initialization.  */
    2738         1497 :   m = gfc_match_null (init);
    2739         1497 :   if (m != MATCH_NO)
    2740              :     return m;
    2741              : 
    2742              :   /* Match non-NULL initialization.  */
    2743          212 :   gfc_matching_ptr_assignment = !procptr;
    2744          212 :   gfc_matching_procptr_assignment = procptr;
    2745          212 :   m = gfc_match_rvalue (init);
    2746          212 :   gfc_matching_ptr_assignment = 0;
    2747          212 :   gfc_matching_procptr_assignment = 0;
    2748          212 :   if (m == MATCH_ERROR)
    2749              :     return MATCH_ERROR;
    2750          211 :   else if (m == MATCH_NO)
    2751              :     {
    2752            2 :       gfc_error ("Error in pointer initialization at %C");
    2753            2 :       return MATCH_ERROR;
    2754              :     }
    2755              : 
    2756          209 :   if (!procptr && !gfc_resolve_expr (*init))
    2757              :     return MATCH_ERROR;
    2758              : 
    2759          208 :   if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
    2760              :                        "initialization at %C"))
    2761            0 :     return MATCH_ERROR;
    2762              : 
    2763              :   return MATCH_YES;
    2764              : }
    2765              : 
    2766              : 
    2767              : static bool
    2768       302690 : check_function_name (char *name)
    2769              : {
    2770              :   /* In functions that have a RESULT variable defined, the function name always
    2771              :      refers to function calls.  Therefore, the name is not allowed to appear in
    2772              :      specification statements. When checking this, be careful about
    2773              :      'hidden' procedure pointer results ('ppr@').  */
    2774              : 
    2775       302690 :   if (gfc_current_state () == COMP_FUNCTION)
    2776              :     {
    2777        49389 :       gfc_symbol *block = gfc_current_block ();
    2778        49389 :       if (block && block->result && block->result != block
    2779        16191 :           && strcmp (block->result->name, "ppr@") != 0
    2780        16132 :           && strcmp (block->name, name) == 0)
    2781              :         {
    2782            9 :           gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
    2783              :                      "from appearing in a specification statement",
    2784              :                      block->result->name, &block->result->declared_at, name);
    2785            9 :           return false;
    2786              :         }
    2787              :     }
    2788              : 
    2789              :   return true;
    2790              : }
    2791              : 
    2792              : 
    2793              : /* Match a variable name with an optional initializer.  When this
    2794              :    subroutine is called, a variable is expected to be parsed next.
    2795              :    Depending on what is happening at the moment, updates either the
    2796              :    symbol table or the current interface.  */
    2797              : 
    2798              : static match
    2799       292401 : variable_decl (int elem)
    2800              : {
    2801       292401 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    2802       292401 :   static unsigned int fill_id = 0;
    2803       292401 :   gfc_expr *initializer, *char_len;
    2804       292401 :   gfc_array_spec *as;
    2805       292401 :   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
    2806       292401 :   gfc_charlen *cl;
    2807       292401 :   gfc_charlen *saved_cl_list;
    2808       292401 :   bool cl_deferred;
    2809       292401 :   locus var_locus;
    2810       292401 :   match m;
    2811       292401 :   bool t;
    2812       292401 :   gfc_symbol *sym;
    2813       292401 :   char c;
    2814              : 
    2815       292401 :   initializer = NULL;
    2816       292401 :   as = NULL;
    2817       292401 :   cp_as = NULL;
    2818       292401 :   saved_cl_list = gfc_current_ns->cl_list;
    2819              : 
    2820              :   /* When we get here, we've just matched a list of attributes and
    2821              :      maybe a type and a double colon.  The next thing we expect to see
    2822              :      is the name of the symbol.  */
    2823              : 
    2824              :   /* If we are parsing a structure with legacy support, we allow the symbol
    2825              :      name to be '%FILL' which gives it an anonymous (inaccessible) name.  */
    2826       292401 :   m = MATCH_NO;
    2827       292401 :   gfc_gobble_whitespace ();
    2828       292401 :   var_locus = gfc_current_locus;
    2829       292401 :   c = gfc_peek_ascii_char ();
    2830       292401 :   if (c == '%')
    2831              :     {
    2832           12 :       gfc_next_ascii_char ();   /* Burn % character.  */
    2833           12 :       m = gfc_match ("fill");
    2834           12 :       if (m == MATCH_YES)
    2835              :         {
    2836           11 :           if (gfc_current_state () != COMP_STRUCTURE)
    2837              :             {
    2838            2 :               if (flag_dec_structure)
    2839            1 :                 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
    2840              :               else
    2841            1 :                 gfc_error ("%qs at %C is a DEC extension, enable with "
    2842              :                        "%<-fdec-structure%>", "%FILL");
    2843            2 :               m = MATCH_ERROR;
    2844            2 :               goto cleanup;
    2845              :             }
    2846              : 
    2847            9 :           if (attr_seen)
    2848              :             {
    2849            1 :               gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
    2850            1 :               m = MATCH_ERROR;
    2851            1 :               goto cleanup;
    2852              :             }
    2853              : 
    2854              :           /* %FILL components are given invalid fortran names.  */
    2855            8 :           snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
    2856              :         }
    2857              :       else
    2858              :         {
    2859            1 :           gfc_error ("Invalid character %qc in variable name at %C", c);
    2860            1 :           return MATCH_ERROR;
    2861              :         }
    2862              :     }
    2863              :   else
    2864              :     {
    2865       292389 :       m = gfc_match_name (name);
    2866       292388 :       if (m != MATCH_YES)
    2867           10 :         goto cleanup;
    2868              :     }
    2869              : 
    2870              :   /* Now we could see the optional array spec. or character length.  */
    2871       292386 :   m = gfc_match_array_spec (&as, true, true);
    2872       292385 :   if (m == MATCH_ERROR)
    2873           57 :     goto cleanup;
    2874              : 
    2875       292328 :   if (m == MATCH_NO)
    2876       228041 :     as = gfc_copy_array_spec (current_as);
    2877        64287 :   else if (current_as
    2878        64287 :            && !merge_array_spec (current_as, as, true))
    2879              :     {
    2880            4 :       m = MATCH_ERROR;
    2881            4 :       goto cleanup;
    2882              :     }
    2883              : 
    2884       292324 :    var_locus = gfc_get_location_range (NULL, 0, &var_locus, 1,
    2885              :                                        &gfc_current_locus);
    2886       292324 :   if (flag_cray_pointer)
    2887         3063 :     cp_as = gfc_copy_array_spec (as);
    2888              : 
    2889              :   /* At this point, we know for sure if the symbol is PARAMETER and can thus
    2890              :      determine (and check) whether it can be implied-shape.  If it
    2891              :      was parsed as assumed-size, change it because PARAMETERs cannot
    2892              :      be assumed-size.
    2893              : 
    2894              :      An explicit-shape-array cannot appear under several conditions.
    2895              :      That check is done here as well.  */
    2896       292324 :   if (as)
    2897              :     {
    2898        87220 :       if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
    2899              :         {
    2900            2 :           m = MATCH_ERROR;
    2901            2 :           gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
    2902              :                      name, &var_locus);
    2903            2 :           goto cleanup;
    2904              :         }
    2905              : 
    2906        87218 :       if (as->type == AS_ASSUMED_SIZE && as->rank == 1
    2907         6536 :           && current_attr.flavor == FL_PARAMETER)
    2908          995 :         as->type = AS_IMPLIED_SHAPE;
    2909              : 
    2910        87218 :       if (as->type == AS_IMPLIED_SHAPE
    2911        87218 :           && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
    2912              :                               &var_locus))
    2913              :         {
    2914            1 :           m = MATCH_ERROR;
    2915            1 :           goto cleanup;
    2916              :         }
    2917              : 
    2918        87217 :       gfc_seen_div0 = false;
    2919              : 
    2920              :       /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
    2921              :          constant expressions shall appear only in a subprogram, derived
    2922              :          type definition, BLOCK construct, or interface body.  */
    2923        87217 :       if (as->type == AS_EXPLICIT
    2924        43469 :           && gfc_current_state () != COMP_BLOCK
    2925              :           && gfc_current_state () != COMP_DERIVED
    2926              :           && gfc_current_state () != COMP_FUNCTION
    2927              :           && gfc_current_state () != COMP_INTERFACE
    2928              :           && gfc_current_state () != COMP_SUBROUTINE)
    2929              :         {
    2930              :           gfc_expr *e;
    2931        51305 :           bool not_constant = false;
    2932              : 
    2933        51305 :           for (int i = 0; i < as->rank; i++)
    2934              :             {
    2935        29173 :               e = gfc_copy_expr (as->lower[i]);
    2936        29173 :               if (!gfc_resolve_expr (e) && gfc_seen_div0)
    2937              :                 {
    2938            0 :                   m = MATCH_ERROR;
    2939            0 :                   goto cleanup;
    2940              :                 }
    2941              : 
    2942        29173 :               gfc_simplify_expr (e, 0);
    2943        29173 :               if (e && (e->expr_type != EXPR_CONSTANT))
    2944              :                 {
    2945              :                   not_constant = true;
    2946              :                   break;
    2947              :                 }
    2948        29173 :               gfc_free_expr (e);
    2949              : 
    2950        29173 :               e = gfc_copy_expr (as->upper[i]);
    2951        29173 :               if (!gfc_resolve_expr (e)  && gfc_seen_div0)
    2952              :                 {
    2953            4 :                   m = MATCH_ERROR;
    2954            4 :                   goto cleanup;
    2955              :                 }
    2956              : 
    2957        29169 :               gfc_simplify_expr (e, 0);
    2958        29169 :               if (e && (e->expr_type != EXPR_CONSTANT))
    2959              :                 {
    2960              :                   not_constant = true;
    2961              :                   break;
    2962              :                 }
    2963        29156 :               gfc_free_expr (e);
    2964              :             }
    2965              : 
    2966        22145 :           if (not_constant && e->ts.type != BT_INTEGER)
    2967              :             {
    2968            4 :               gfc_error ("Explicit array shape at %C must be constant of "
    2969              :                          "INTEGER type and not %s type",
    2970              :                          gfc_basic_typename (e->ts.type));
    2971            4 :               m = MATCH_ERROR;
    2972            4 :               goto cleanup;
    2973              :             }
    2974            9 :           if (not_constant)
    2975              :             {
    2976            9 :               gfc_error ("Explicit shaped array with nonconstant bounds at %C");
    2977            9 :               m = MATCH_ERROR;
    2978            9 :               goto cleanup;
    2979              :             }
    2980              :         }
    2981        87200 :       if (as->type == AS_EXPLICIT)
    2982              :         {
    2983       103630 :           for (int i = 0; i < as->rank; i++)
    2984              :             {
    2985        60178 :               gfc_expr *e, *n;
    2986        60178 :               e = as->lower[i];
    2987        60178 :               if (e->expr_type != EXPR_CONSTANT)
    2988              :                 {
    2989          458 :                   n = gfc_copy_expr (e);
    2990          458 :                   if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
    2991              :                     {
    2992            0 :                       m = MATCH_ERROR;
    2993            0 :                       goto cleanup;
    2994              :                     }
    2995              : 
    2996          458 :                   if (n->expr_type == EXPR_CONSTANT)
    2997           22 :                     gfc_replace_expr (e, n);
    2998              :                   else
    2999          436 :                     gfc_free_expr (n);
    3000              :                 }
    3001        60178 :               e = as->upper[i];
    3002        60178 :               if (e->expr_type != EXPR_CONSTANT)
    3003              :                 {
    3004         7077 :                   n = gfc_copy_expr (e);
    3005         7077 :                   if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
    3006              :                     {
    3007            0 :                       m = MATCH_ERROR;
    3008            0 :                       goto cleanup;
    3009              :                     }
    3010              : 
    3011         7077 :                   if (n->expr_type == EXPR_CONSTANT)
    3012           45 :                     gfc_replace_expr (e, n);
    3013              :                   else
    3014         7032 :                     gfc_free_expr (n);
    3015              :                 }
    3016              :               /* For an explicit-shape spec with constant bounds, ensure
    3017              :                  that the effective upper bound is not lower than the
    3018              :                  respective lower bound minus one.  Otherwise adjust it so
    3019              :                  that the extent is trivially derived to be zero.  */
    3020        60178 :               if (as->lower[i]->expr_type == EXPR_CONSTANT
    3021        59742 :                   && as->upper[i]->expr_type == EXPR_CONSTANT
    3022        53134 :                   && as->lower[i]->ts.type == BT_INTEGER
    3023        53134 :                   && as->upper[i]->ts.type == BT_INTEGER
    3024        53129 :                   && mpz_cmp (as->upper[i]->value.integer,
    3025        53129 :                               as->lower[i]->value.integer) < 0)
    3026         1218 :                 mpz_sub_ui (as->upper[i]->value.integer,
    3027              :                             as->lower[i]->value.integer, 1);
    3028              :             }
    3029              :         }
    3030              :     }
    3031              : 
    3032       292304 :   char_len = NULL;
    3033       292304 :   cl = NULL;
    3034       292304 :   cl_deferred = false;
    3035              : 
    3036       292304 :   if (current_ts.type == BT_CHARACTER)
    3037              :     {
    3038        32141 :       switch (match_char_length (&char_len, &cl_deferred, false))
    3039              :         {
    3040          435 :         case MATCH_YES:
    3041          435 :           cl = gfc_new_charlen (gfc_current_ns, NULL);
    3042              : 
    3043          435 :           cl->length = char_len;
    3044          435 :           break;
    3045              : 
    3046              :         /* Non-constant lengths need to be copied after the first
    3047              :            element.  Also copy assumed lengths.  */
    3048        31705 :         case MATCH_NO:
    3049        31705 :           if (elem > 1
    3050         4007 :               && (current_ts.u.cl->length == NULL
    3051         2775 :                   || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
    3052              :             {
    3053         1287 :               cl = gfc_new_charlen (gfc_current_ns, NULL);
    3054         1287 :               cl->length = gfc_copy_expr (current_ts.u.cl->length);
    3055              :             }
    3056              :           else
    3057        30418 :             cl = current_ts.u.cl;
    3058              : 
    3059        31705 :           cl_deferred = current_ts.deferred;
    3060              : 
    3061        31705 :           break;
    3062              : 
    3063            1 :         case MATCH_ERROR:
    3064            1 :           goto cleanup;
    3065              :         }
    3066              :     }
    3067              : 
    3068              :   /* The dummy arguments and result of the abbreviated form of MODULE
    3069              :      PROCEDUREs, used in SUBMODULES should not be redefined.  */
    3070       292303 :   if (gfc_current_ns->proc_name
    3071       287818 :       && gfc_current_ns->proc_name->abr_modproc_decl)
    3072              :     {
    3073           50 :       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
    3074           50 :       if (sym != NULL && (sym->attr.dummy || sym->attr.result))
    3075              :         {
    3076            2 :           m = MATCH_ERROR;
    3077            2 :           gfc_error ("%qs at %L is a redefinition of the declaration "
    3078              :                      "in the corresponding interface for MODULE "
    3079              :                      "PROCEDURE %qs", sym->name, &var_locus,
    3080            2 :                      gfc_current_ns->proc_name->name);
    3081            2 :           goto cleanup;
    3082              :         }
    3083              :     }
    3084              : 
    3085              :   /* %FILL components may not have initializers.  */
    3086       292301 :   if (startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
    3087              :     {
    3088            1 :       gfc_error ("%qs entity cannot have an initializer at %L", "%FILL",
    3089              :                  &var_locus);
    3090            1 :       m = MATCH_ERROR;
    3091            1 :       goto cleanup;
    3092              :     }
    3093              : 
    3094              :   /*  If this symbol has already shown up in a Cray Pointer declaration,
    3095              :       and this is not a component declaration,
    3096              :       then we want to set the type & bail out.  */
    3097       292300 :   if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
    3098              :     {
    3099         2959 :       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
    3100         2959 :       if (sym != NULL && sym->attr.cray_pointee)
    3101              :         {
    3102          101 :           m = MATCH_YES;
    3103          101 :           if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
    3104              :             {
    3105            1 :               m = MATCH_ERROR;
    3106            1 :               goto cleanup;
    3107              :             }
    3108              : 
    3109              :           /* Check to see if we have an array specification.  */
    3110          100 :           if (cp_as != NULL)
    3111              :             {
    3112           49 :               if (sym->as != NULL)
    3113              :                 {
    3114            1 :                   gfc_error ("Duplicate array spec for Cray pointee at %L", &var_locus);
    3115            1 :                   gfc_free_array_spec (cp_as);
    3116            1 :                   m = MATCH_ERROR;
    3117            1 :                   goto cleanup;
    3118              :                 }
    3119              :               else
    3120              :                 {
    3121           48 :                   if (!gfc_set_array_spec (sym, cp_as, &var_locus))
    3122            0 :                     gfc_internal_error ("Cannot set pointee array spec.");
    3123              : 
    3124              :                   /* Fix the array spec.  */
    3125           48 :                   m = gfc_mod_pointee_as (sym->as);
    3126           48 :                   if (m == MATCH_ERROR)
    3127            0 :                     goto cleanup;
    3128              :                 }
    3129              :             }
    3130           99 :           goto cleanup;
    3131              :         }
    3132              :       else
    3133              :         {
    3134         2858 :           gfc_free_array_spec (cp_as);
    3135              :         }
    3136              :     }
    3137              :   else
    3138              :     {
    3139              :       /* Check to see if this is the declaration of the type and/or attributes
    3140              :          of an implicit function result, emanating from a module function
    3141              :          interface declared within the parent module or submodule of a
    3142              :          containing submodule.  */
    3143       289341 :       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
    3144       289341 :       if (gfc_current_state () == COMP_FUNCTION
    3145        47907 :           && sym == gfc_current_block ()
    3146         8393 :           && sym->attr.if_source == IFSRC_DECL
    3147         4952 :           && sym->attr.used_in_submodule
    3148            4 :           && sym == sym->result
    3149            4 :           && sym->ts.type != BT_UNKNOWN)
    3150              :         {
    3151            4 :           m = MATCH_YES;
    3152            4 :           goto cleanup;
    3153              :         }
    3154       289337 :       sym = NULL;
    3155              :     }
    3156              : 
    3157              :   /* Procedure pointer as function result.  */
    3158       292195 :   if (gfc_current_state () == COMP_FUNCTION
    3159        48017 :       && strcmp ("ppr@", gfc_current_block ()->name) == 0
    3160           25 :       && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
    3161            7 :     strcpy (name, "ppr@");
    3162              : 
    3163       292195 :   if (gfc_current_state () == COMP_FUNCTION
    3164        48017 :       && strcmp (name, gfc_current_block ()->name) == 0
    3165         8409 :       && gfc_current_block ()->result
    3166         8409 :       && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
    3167           16 :     strcpy (name, "ppr@");
    3168              : 
    3169              :   /* OK, we've successfully matched the declaration.  Now put the
    3170              :      symbol in the current namespace, because it might be used in the
    3171              :      optional initialization expression for this symbol, e.g. this is
    3172              :      perfectly legal:
    3173              : 
    3174              :      integer, parameter :: i = huge(i)
    3175              : 
    3176              :      This is only true for parameters or variables of a basic type.
    3177              :      For components of derived types, it is not true, so we don't
    3178              :      create a symbol for those yet.  If we fail to create the symbol,
    3179              :      bail out.  */
    3180       292195 :   if (!gfc_comp_struct (gfc_current_state ())
    3181       271636 :       && !build_sym (name, elem, cl, cl_deferred, &as, &var_locus))
    3182              :     {
    3183           46 :       m = MATCH_ERROR;
    3184           46 :       goto cleanup;
    3185              :     }
    3186              : 
    3187       292149 :   if (!check_function_name (name))
    3188              :     {
    3189            0 :       m = MATCH_ERROR;
    3190            0 :       goto cleanup;
    3191              :     }
    3192              : 
    3193              :   /* We allow old-style initializations of the form
    3194              :        integer i /2/, j(4) /3*3, 1/
    3195              :      (if no colon has been seen). These are different from data
    3196              :      statements in that initializers are only allowed to apply to the
    3197              :      variable immediately preceding, i.e.
    3198              :        integer i, j /1, 2/
    3199              :      is not allowed. Therefore we have to do some work manually, that
    3200              :      could otherwise be left to the matchers for DATA statements.  */
    3201              : 
    3202       292149 :   if (!colon_seen && gfc_match (" /") == MATCH_YES)
    3203              :     {
    3204          146 :       if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
    3205              :                            "initialization at %C"))
    3206              :         return MATCH_ERROR;
    3207              : 
    3208              :       /* Allow old style initializations for components of STRUCTUREs and MAPs
    3209              :          but not components of derived types.  */
    3210          146 :       else if (gfc_current_state () == COMP_DERIVED)
    3211              :         {
    3212            2 :           gfc_error ("Invalid old style initialization for derived type "
    3213              :                      "component at %C");
    3214            2 :           m = MATCH_ERROR;
    3215            2 :           goto cleanup;
    3216              :         }
    3217              : 
    3218              :       /* For structure components, read the initializer as a special
    3219              :          expression and let the rest of this function apply the initializer
    3220              :          as usual.  */
    3221          144 :       else if (gfc_comp_struct (gfc_current_state ()))
    3222              :         {
    3223           74 :           m = match_clist_expr (&initializer, &current_ts, as);
    3224           74 :           if (m == MATCH_NO)
    3225              :             gfc_error ("Syntax error in old style initialization of %s at %C",
    3226              :                        name);
    3227           74 :           if (m != MATCH_YES)
    3228           14 :             goto cleanup;
    3229              :         }
    3230              : 
    3231              :       /* Otherwise we treat the old style initialization just like a
    3232              :          DATA declaration for the current variable.  */
    3233              :       else
    3234           70 :         return match_old_style_init (name);
    3235              :     }
    3236              : 
    3237              :   /* The double colon must be present in order to have initializers.
    3238              :      Otherwise the statement is ambiguous with an assignment statement.  */
    3239       292063 :   if (colon_seen)
    3240              :     {
    3241       245576 :       if (gfc_match (" =>") == MATCH_YES)
    3242              :         {
    3243         1287 :           if (!current_attr.pointer)
    3244              :             {
    3245            0 :               gfc_error ("Initialization at %C isn't for a pointer variable");
    3246            0 :               m = MATCH_ERROR;
    3247            0 :               goto cleanup;
    3248              :             }
    3249              : 
    3250         1287 :           m = match_pointer_init (&initializer, 0);
    3251         1287 :           if (m != MATCH_YES)
    3252           10 :             goto cleanup;
    3253              : 
    3254              :           /* The target of a pointer initialization must have the SAVE
    3255              :              attribute.  A variable in PROGRAM, MODULE, or SUBMODULE scope
    3256              :              is implicit SAVEd.  Explicitly, set the SAVE_IMPLICIT value.  */
    3257         1277 :           if (initializer->expr_type == EXPR_VARIABLE
    3258          164 :               && initializer->symtree->n.sym->attr.save == SAVE_NONE
    3259           25 :               && (gfc_current_state () == COMP_PROGRAM
    3260              :                   || gfc_current_state () == COMP_MODULE
    3261           25 :                   || gfc_current_state () == COMP_SUBMODULE))
    3262           11 :             initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
    3263              :         }
    3264       244289 :       else if (gfc_match_char ('=') == MATCH_YES)
    3265              :         {
    3266        27807 :           if (current_attr.pointer)
    3267              :             {
    3268            0 :               gfc_error ("Pointer initialization at %C requires %<=>%>, "
    3269              :                          "not %<=%>");
    3270            0 :               m = MATCH_ERROR;
    3271            0 :               goto cleanup;
    3272              :             }
    3273              : 
    3274        27807 :           if (gfc_comp_struct (gfc_current_state ())
    3275         3178 :               && gfc_current_block ()->attr.pdt_template)
    3276              :             {
    3277          613 :               m = gfc_match_expr (&initializer);
    3278          613 :               if (initializer && initializer->ts.type == BT_UNKNOWN)
    3279          182 :                 initializer->ts = current_ts;
    3280              :             }
    3281              :           else
    3282        27194 :             m = gfc_match_init_expr (&initializer);
    3283              : 
    3284        27807 :           if (m == MATCH_NO)
    3285              :             {
    3286            1 :               gfc_error ("Expected an initialization expression at %C");
    3287            1 :               m = MATCH_ERROR;
    3288              :             }
    3289              : 
    3290        11314 :           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
    3291        27809 :               && !gfc_comp_struct (gfc_state_stack->state))
    3292              :             {
    3293            1 :               gfc_error ("Initialization of variable at %C is not allowed in "
    3294              :                          "a PURE procedure");
    3295            1 :               m = MATCH_ERROR;
    3296              :             }
    3297              : 
    3298        27807 :           if (current_attr.flavor != FL_PARAMETER
    3299        11314 :               && !gfc_comp_struct (gfc_state_stack->state))
    3300         8136 :             gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    3301              : 
    3302        27807 :           if (m != MATCH_YES)
    3303          160 :             goto cleanup;
    3304              :         }
    3305              :     }
    3306              : 
    3307       291893 :   if (initializer != NULL && current_attr.allocatable
    3308            3 :         && gfc_comp_struct (gfc_current_state ()))
    3309              :     {
    3310            2 :       gfc_error ("Initialization of allocatable component at %C is not "
    3311              :                  "allowed");
    3312            2 :       m = MATCH_ERROR;
    3313            2 :       goto cleanup;
    3314              :     }
    3315              : 
    3316       291891 :   if (gfc_current_state () == COMP_DERIVED
    3317        19517 :       && initializer && initializer->ts.type == BT_HOLLERITH)
    3318              :     {
    3319            1 :       gfc_error ("Initialization of structure component with a HOLLERITH "
    3320              :                  "constant at %L is not allowed", &initializer->where);
    3321            1 :       m = MATCH_ERROR;
    3322            1 :       goto cleanup;
    3323              :     }
    3324              : 
    3325       291890 :   if (gfc_current_state () == COMP_DERIVED
    3326        19516 :       && gfc_current_block ()->attr.pdt_template)
    3327              :     {
    3328         2160 :       gfc_symbol *param;
    3329         2160 :       gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
    3330              :                        0, &param);
    3331         2160 :       if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
    3332              :         {
    3333            1 :           gfc_error ("The component with KIND or LEN attribute at %C does not "
    3334              :                      "not appear in the type parameter list at %L",
    3335            1 :                      &gfc_current_block ()->declared_at);
    3336            1 :           m = MATCH_ERROR;
    3337            4 :           goto cleanup;
    3338              :         }
    3339         2159 :       else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
    3340              :         {
    3341            1 :           gfc_error ("The component at %C that appears in the type parameter "
    3342              :                      "list at %L has neither the KIND nor LEN attribute",
    3343            1 :                      &gfc_current_block ()->declared_at);
    3344            1 :           m = MATCH_ERROR;
    3345            1 :           goto cleanup;
    3346              :         }
    3347         2158 :       else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
    3348              :         {
    3349            1 :           gfc_error ("The component at %C which is a type parameter must be "
    3350              :                      "a scalar");
    3351            1 :           m = MATCH_ERROR;
    3352            1 :           goto cleanup;
    3353              :         }
    3354         2157 :       else if (param && initializer)
    3355              :         {
    3356          506 :           if (initializer->ts.type == BT_BOZ)
    3357              :             {
    3358            1 :               gfc_error ("BOZ literal constant at %L cannot appear as an "
    3359              :                          "initializer", &initializer->where);
    3360            1 :               m = MATCH_ERROR;
    3361            1 :               goto cleanup;
    3362              :             }
    3363          505 :           param->value = gfc_copy_expr (initializer);
    3364              :         }
    3365              :     }
    3366              : 
    3367              :   /* Before adding a possible initializer, do a simple check for compatibility
    3368              :      of lhs and rhs types.  Assigning a REAL value to a derived type is not a
    3369              :      good thing.  */
    3370        30516 :   if (current_ts.type == BT_DERIVED && initializer
    3371       293452 :       && (gfc_numeric_ts (&initializer->ts)
    3372         1564 :           || initializer->ts.type == BT_LOGICAL
    3373         1564 :           || initializer->ts.type == BT_CHARACTER))
    3374              :     {
    3375            2 :       gfc_error ("Incompatible initialization between a derived type "
    3376              :                  "entity and an entity with %qs type at %C",
    3377              :                   gfc_typename (initializer));
    3378            2 :       m = MATCH_ERROR;
    3379            2 :       goto cleanup;
    3380              :     }
    3381              : 
    3382              : 
    3383              :   /* Add the initializer.  Note that it is fine if initializer is
    3384              :      NULL here, because we sometimes also need to check if a
    3385              :      declaration *must* have an initialization expression.  */
    3386       291884 :   if (!gfc_comp_struct (gfc_current_state ()))
    3387       271354 :     t = add_init_expr_to_sym (name, &initializer, &var_locus,
    3388              :                               saved_cl_list);
    3389              :   else
    3390              :     {
    3391        20530 :       if (current_ts.type == BT_DERIVED
    3392         2833 :           && !current_attr.pointer && !initializer)
    3393         2268 :         initializer = gfc_default_initializer (&current_ts);
    3394        20530 :       t = build_struct (name, cl, &initializer, &as);
    3395              : 
    3396              :       /* If we match a nested structure definition we expect to see the
    3397              :        * body even if the variable declarations blow up, so we need to keep
    3398              :        * the structure declaration around.  */
    3399        20530 :       if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
    3400           34 :         gfc_commit_symbol (gfc_new_block);
    3401              :     }
    3402              : 
    3403       291884 :   m = (t) ? MATCH_YES : MATCH_ERROR;
    3404              : 
    3405       292328 : cleanup:
    3406              :   /* Free stuff up and return.  */
    3407       292328 :   gfc_seen_div0 = false;
    3408       292328 :   gfc_free_expr (initializer);
    3409       292328 :   gfc_free_array_spec (as);
    3410              : 
    3411       292328 :   return m;
    3412              : }
    3413              : 
    3414              : 
    3415              : /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
    3416              :    This assumes that the byte size is equal to the kind number for
    3417              :    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
    3418              : 
    3419              : static match
    3420       112025 : gfc_match_old_kind_spec (gfc_typespec *ts)
    3421              : {
    3422       112025 :   match m;
    3423       112025 :   int original_kind;
    3424              : 
    3425       112025 :   if (gfc_match_char ('*') != MATCH_YES)
    3426              :     return MATCH_NO;
    3427              : 
    3428         1150 :   m = gfc_match_small_literal_int (&ts->kind, NULL);
    3429         1150 :   if (m != MATCH_YES)
    3430              :     return MATCH_ERROR;
    3431              : 
    3432         1150 :   original_kind = ts->kind;
    3433              : 
    3434              :   /* Massage the kind numbers for complex types.  */
    3435         1150 :   if (ts->type == BT_COMPLEX)
    3436              :     {
    3437           79 :       if (ts->kind % 2)
    3438              :         {
    3439            0 :           gfc_error ("Old-style type declaration %s*%d not supported at %C",
    3440              :                      gfc_basic_typename (ts->type), original_kind);
    3441            0 :           return MATCH_ERROR;
    3442              :         }
    3443           79 :       ts->kind /= 2;
    3444              : 
    3445              :     }
    3446              : 
    3447         1150 :   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
    3448            0 :     ts->kind = 8;
    3449              : 
    3450         1150 :   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
    3451              :     {
    3452          858 :       if (ts->kind == 4)
    3453              :         {
    3454          224 :           if (flag_real4_kind == 8)
    3455           24 :             ts->kind =  8;
    3456          224 :           if (flag_real4_kind == 10)
    3457           24 :             ts->kind = 10;
    3458          224 :           if (flag_real4_kind == 16)
    3459           24 :             ts->kind = 16;
    3460              :         }
    3461          634 :       else if (ts->kind == 8)
    3462              :         {
    3463          629 :           if (flag_real8_kind == 4)
    3464           24 :             ts->kind = 4;
    3465          629 :           if (flag_real8_kind == 10)
    3466           24 :             ts->kind = 10;
    3467          629 :           if (flag_real8_kind == 16)
    3468           24 :             ts->kind = 16;
    3469              :         }
    3470              :     }
    3471              : 
    3472         1150 :   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
    3473              :     {
    3474            8 :       gfc_error ("Old-style type declaration %s*%d not supported at %C",
    3475              :                  gfc_basic_typename (ts->type), original_kind);
    3476            8 :       return MATCH_ERROR;
    3477              :     }
    3478              : 
    3479         1142 :   if (!gfc_notify_std (GFC_STD_GNU,
    3480              :                        "Nonstandard type declaration %s*%d at %C",
    3481              :                        gfc_basic_typename(ts->type), original_kind))
    3482            0 :     return MATCH_ERROR;
    3483              : 
    3484              :   return MATCH_YES;
    3485              : }
    3486              : 
    3487              : 
    3488              : /* Match a kind specification.  Since kinds are generally optional, we
    3489              :    usually return MATCH_NO if something goes wrong.  If a "kind="
    3490              :    string is found, then we know we have an error.  */
    3491              : 
    3492              : match
    3493       166766 : gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
    3494              : {
    3495       166766 :   locus where, loc;
    3496       166766 :   gfc_expr *e;
    3497       166766 :   match m, n;
    3498       166766 :   char c;
    3499              : 
    3500       166766 :   m = MATCH_NO;
    3501       166766 :   n = MATCH_YES;
    3502       166766 :   e = NULL;
    3503       166766 :   saved_kind_expr = NULL;
    3504              : 
    3505       166766 :   where = loc = gfc_current_locus;
    3506              : 
    3507       166766 :   if (kind_expr_only)
    3508            0 :     goto kind_expr;
    3509              : 
    3510       166766 :   if (gfc_match_char ('(') == MATCH_NO)
    3511              :     return MATCH_NO;
    3512              : 
    3513              :   /* Also gobbles optional text.  */
    3514        53088 :   if (gfc_match (" kind = ") == MATCH_YES)
    3515        53088 :     m = MATCH_ERROR;
    3516              : 
    3517        53088 :   loc = gfc_current_locus;
    3518              : 
    3519        53088 : kind_expr:
    3520              : 
    3521        53088 :   n = gfc_match_init_expr (&e);
    3522              : 
    3523        53088 :   if (gfc_derived_parameter_expr (e))
    3524              :     {
    3525          335 :       ts->kind = 0;
    3526          335 :       saved_kind_expr = gfc_copy_expr (e);
    3527          335 :       goto close_brackets;
    3528              :     }
    3529              : 
    3530        52753 :   if (n != MATCH_YES)
    3531              :     {
    3532          522 :       if (gfc_matching_function)
    3533              :         {
    3534              :           /* The function kind expression might include use associated or
    3535              :              imported parameters and try again after the specification
    3536              :              expressions.....  */
    3537          494 :           if (gfc_match_char (')') != MATCH_YES)
    3538              :             {
    3539            1 :               gfc_error ("Missing right parenthesis at %C");
    3540            1 :               m = MATCH_ERROR;
    3541            1 :               goto no_match;
    3542              :             }
    3543              : 
    3544          493 :           gfc_free_expr (e);
    3545          493 :           gfc_undo_symbols ();
    3546          493 :           return MATCH_YES;
    3547              :         }
    3548              :       else
    3549              :         {
    3550              :           /* ....or else, the match is real.  */
    3551           28 :           if (n == MATCH_NO)
    3552            0 :             gfc_error ("Expected initialization expression at %C");
    3553              :           if (n != MATCH_YES)
    3554              :             return MATCH_ERROR;
    3555              :         }
    3556              :     }
    3557              : 
    3558        52231 :   if (e->rank != 0)
    3559              :     {
    3560            0 :       gfc_error ("Expected scalar initialization expression at %C");
    3561            0 :       m = MATCH_ERROR;
    3562            0 :       goto no_match;
    3563              :     }
    3564              : 
    3565        52231 :   if (gfc_extract_int (e, &ts->kind, 1))
    3566              :     {
    3567            0 :       m = MATCH_ERROR;
    3568            0 :       goto no_match;
    3569              :     }
    3570              : 
    3571              :   /* Before throwing away the expression, let's see if we had a
    3572              :      C interoperable kind (and store the fact).  */
    3573        52231 :   if (e->ts.is_c_interop == 1)
    3574              :     {
    3575              :       /* Mark this as C interoperable if being declared with one
    3576              :          of the named constants from iso_c_binding.  */
    3577        19368 :       ts->is_c_interop = e->ts.is_iso_c;
    3578        19368 :       ts->f90_type = e->ts.f90_type;
    3579        19368 :       if (e->symtree)
    3580        19367 :         ts->interop_kind = e->symtree->n.sym;
    3581              :     }
    3582              : 
    3583        52231 :   gfc_free_expr (e);
    3584        52231 :   e = NULL;
    3585              : 
    3586              :   /* Ignore errors to this point, if we've gotten here.  This means
    3587              :      we ignore the m=MATCH_ERROR from above.  */
    3588        52231 :   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
    3589              :     {
    3590            7 :       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
    3591              :                  gfc_basic_typename (ts->type));
    3592            7 :       gfc_current_locus = where;
    3593            7 :       return MATCH_ERROR;
    3594              :     }
    3595              : 
    3596              :   /* Warn if, e.g., c_int is used for a REAL variable, but not
    3597              :      if, e.g., c_double is used for COMPLEX as the standard
    3598              :      explicitly says that the kind type parameter for complex and real
    3599              :      variable is the same, i.e. c_float == c_float_complex.  */
    3600        52224 :   if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
    3601           17 :       && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
    3602            1 :            || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
    3603           13 :     gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
    3604              :                      "is %s", gfc_basic_typename (ts->f90_type), &where,
    3605              :                      gfc_basic_typename (ts->type));
    3606              : 
    3607        52211 : close_brackets:
    3608              : 
    3609        52559 :   gfc_gobble_whitespace ();
    3610        52559 :   if ((c = gfc_next_ascii_char ()) != ')'
    3611        52559 :       && (ts->type != BT_CHARACTER || c != ','))
    3612              :     {
    3613            0 :       if (ts->type == BT_CHARACTER)
    3614            0 :         gfc_error ("Missing right parenthesis or comma at %C");
    3615              :       else
    3616            0 :         gfc_error ("Missing right parenthesis at %C");
    3617            0 :       m = MATCH_ERROR;
    3618            0 :       goto no_match;
    3619              :     }
    3620              :   else
    3621              :      /* All tests passed.  */
    3622        52559 :      m = MATCH_YES;
    3623              : 
    3624        52559 :   if(m == MATCH_ERROR)
    3625              :      gfc_current_locus = where;
    3626              : 
    3627        52559 :   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
    3628            0 :     ts->kind =  8;
    3629              : 
    3630        52559 :   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
    3631              :     {
    3632        14676 :       if (ts->kind == 4)
    3633              :         {
    3634         4618 :           if (flag_real4_kind == 8)
    3635           54 :             ts->kind =  8;
    3636         4618 :           if (flag_real4_kind == 10)
    3637           54 :             ts->kind = 10;
    3638         4618 :           if (flag_real4_kind == 16)
    3639           54 :             ts->kind = 16;
    3640              :         }
    3641        10058 :       else if (ts->kind == 8)
    3642              :         {
    3643         6795 :           if (flag_real8_kind == 4)
    3644           48 :             ts->kind = 4;
    3645         6795 :           if (flag_real8_kind == 10)
    3646           48 :             ts->kind = 10;
    3647         6795 :           if (flag_real8_kind == 16)
    3648           48 :             ts->kind = 16;
    3649              :         }
    3650              :     }
    3651              : 
    3652              :   /* Return what we know from the test(s).  */
    3653              :   return m;
    3654              : 
    3655            1 : no_match:
    3656            1 :   gfc_free_expr (e);
    3657            1 :   gfc_current_locus = where;
    3658            1 :   return m;
    3659              : }
    3660              : 
    3661              : 
    3662              : static match
    3663         5075 : match_char_kind (int * kind, int * is_iso_c)
    3664              : {
    3665         5075 :   locus where;
    3666         5075 :   gfc_expr *e;
    3667         5075 :   match m, n;
    3668         5075 :   bool fail;
    3669              : 
    3670         5075 :   m = MATCH_NO;
    3671         5075 :   e = NULL;
    3672         5075 :   where = gfc_current_locus;
    3673              : 
    3674         5075 :   n = gfc_match_init_expr (&e);
    3675              : 
    3676         5075 :   if (n != MATCH_YES && gfc_matching_function)
    3677              :     {
    3678              :       /* The expression might include use-associated or imported
    3679              :          parameters and try again after the specification
    3680              :          expressions.  */
    3681            7 :       gfc_free_expr (e);
    3682            7 :       gfc_undo_symbols ();
    3683            7 :       return MATCH_YES;
    3684              :     }
    3685              : 
    3686            7 :   if (n == MATCH_NO)
    3687            2 :     gfc_error ("Expected initialization expression at %C");
    3688         5068 :   if (n != MATCH_YES)
    3689              :     return MATCH_ERROR;
    3690              : 
    3691         5061 :   if (e->rank != 0)
    3692              :     {
    3693            0 :       gfc_error ("Expected scalar initialization expression at %C");
    3694            0 :       m = MATCH_ERROR;
    3695            0 :       goto no_match;
    3696              :     }
    3697              : 
    3698         5061 :   if (gfc_derived_parameter_expr (e))
    3699              :     {
    3700           86 :       saved_kind_expr = e;
    3701           86 :       *kind = 0;
    3702           86 :       return MATCH_YES;
    3703              :     }
    3704              : 
    3705         4975 :   fail = gfc_extract_int (e, kind, 1);
    3706         4975 :   *is_iso_c = e->ts.is_iso_c;
    3707         4975 :   if (fail)
    3708              :     {
    3709            0 :       m = MATCH_ERROR;
    3710            0 :       goto no_match;
    3711              :     }
    3712              : 
    3713         4975 :   gfc_free_expr (e);
    3714              : 
    3715              :   /* Ignore errors to this point, if we've gotten here.  This means
    3716              :      we ignore the m=MATCH_ERROR from above.  */
    3717         4975 :   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
    3718              :     {
    3719           14 :       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
    3720           14 :       m = MATCH_ERROR;
    3721              :     }
    3722              :   else
    3723              :      /* All tests passed.  */
    3724              :      m = MATCH_YES;
    3725              : 
    3726           14 :   if (m == MATCH_ERROR)
    3727           14 :      gfc_current_locus = where;
    3728              : 
    3729              :   /* Return what we know from the test(s).  */
    3730              :   return m;
    3731              : 
    3732            0 : no_match:
    3733            0 :   gfc_free_expr (e);
    3734            0 :   gfc_current_locus = where;
    3735            0 :   return m;
    3736              : }
    3737              : 
    3738              : 
    3739              : /* Match the various kind/length specifications in a CHARACTER
    3740              :    declaration.  We don't return MATCH_NO.  */
    3741              : 
    3742              : match
    3743        33058 : gfc_match_char_spec (gfc_typespec *ts)
    3744              : {
    3745        33058 :   int kind, seen_length, is_iso_c;
    3746        33058 :   gfc_charlen *cl;
    3747        33058 :   gfc_expr *len;
    3748        33058 :   match m;
    3749        33058 :   bool deferred;
    3750              : 
    3751        33058 :   len = NULL;
    3752        33058 :   seen_length = 0;
    3753        33058 :   kind = 0;
    3754        33058 :   is_iso_c = 0;
    3755        33058 :   deferred = false;
    3756              : 
    3757              :   /* Try the old-style specification first.  */
    3758        33058 :   old_char_selector = 0;
    3759              : 
    3760        33058 :   m = match_char_length (&len, &deferred, true);
    3761        33058 :   if (m != MATCH_NO)
    3762              :     {
    3763         2205 :       if (m == MATCH_YES)
    3764         2205 :         old_char_selector = 1;
    3765         2205 :       seen_length = 1;
    3766         2205 :       goto done;
    3767              :     }
    3768              : 
    3769        30853 :   m = gfc_match_char ('(');
    3770        30853 :   if (m != MATCH_YES)
    3771              :     {
    3772         1965 :       m = MATCH_YES;    /* Character without length is a single char.  */
    3773         1965 :       goto done;
    3774              :     }
    3775              : 
    3776              :   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
    3777        28888 :   if (gfc_match (" kind =") == MATCH_YES)
    3778              :     {
    3779         3577 :       m = match_char_kind (&kind, &is_iso_c);
    3780              : 
    3781         3577 :       if (m == MATCH_ERROR)
    3782           16 :         goto done;
    3783         3561 :       if (m == MATCH_NO)
    3784              :         goto syntax;
    3785              : 
    3786         3561 :       if (gfc_match (" , len =") == MATCH_NO)
    3787          536 :         goto rparen;
    3788              : 
    3789         3025 :       m = char_len_param_value (&len, &deferred);
    3790         3025 :       if (m == MATCH_NO)
    3791            0 :         goto syntax;
    3792         3025 :       if (m == MATCH_ERROR)
    3793            2 :         goto done;
    3794         3023 :       seen_length = 1;
    3795              : 
    3796         3023 :       goto rparen;
    3797              :     }
    3798              : 
    3799              :   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
    3800        25311 :   if (gfc_match (" len =") == MATCH_YES)
    3801              :     {
    3802        14257 :       m = char_len_param_value (&len, &deferred);
    3803        14257 :       if (m == MATCH_NO)
    3804            2 :         goto syntax;
    3805        14255 :       if (m == MATCH_ERROR)
    3806            8 :         goto done;
    3807        14247 :       seen_length = 1;
    3808              : 
    3809        14247 :       if (gfc_match_char (')') == MATCH_YES)
    3810        12941 :         goto done;
    3811              : 
    3812         1306 :       if (gfc_match (" , kind =") != MATCH_YES)
    3813            0 :         goto syntax;
    3814              : 
    3815         1306 :       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
    3816            2 :         goto done;
    3817              : 
    3818         1304 :       goto rparen;
    3819              :     }
    3820              : 
    3821              :   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
    3822        11054 :   m = char_len_param_value (&len, &deferred);
    3823        11054 :   if (m == MATCH_NO)
    3824            0 :     goto syntax;
    3825        11054 :   if (m == MATCH_ERROR)
    3826           44 :     goto done;
    3827        11010 :   seen_length = 1;
    3828              : 
    3829        11010 :   m = gfc_match_char (')');
    3830        11010 :   if (m == MATCH_YES)
    3831        10816 :     goto done;
    3832              : 
    3833          194 :   if (gfc_match_char (',') != MATCH_YES)
    3834            2 :     goto syntax;
    3835              : 
    3836          192 :   gfc_match (" kind =");      /* Gobble optional text.  */
    3837              : 
    3838          192 :   m = match_char_kind (&kind, &is_iso_c);
    3839          192 :   if (m == MATCH_ERROR)
    3840            3 :     goto done;
    3841              :   if (m == MATCH_NO)
    3842              :     goto syntax;
    3843              : 
    3844         5052 : rparen:
    3845              :   /* Require a right-paren at this point.  */
    3846         5052 :   m = gfc_match_char (')');
    3847         5052 :   if (m == MATCH_YES)
    3848         5052 :     goto done;
    3849              : 
    3850            0 : syntax:
    3851            4 :   gfc_error ("Syntax error in CHARACTER declaration at %C");
    3852            4 :   m = MATCH_ERROR;
    3853            4 :   gfc_free_expr (len);
    3854            4 :   return m;
    3855              : 
    3856        33054 : done:
    3857              :   /* Deal with character functions after USE and IMPORT statements.  */
    3858        33054 :   if (gfc_matching_function)
    3859              :     {
    3860         1431 :       gfc_free_expr (len);
    3861         1431 :       gfc_undo_symbols ();
    3862         1431 :       return MATCH_YES;
    3863              :     }
    3864              : 
    3865        31623 :   if (m != MATCH_YES)
    3866              :     {
    3867           65 :       gfc_free_expr (len);
    3868           65 :       return m;
    3869              :     }
    3870              : 
    3871              :   /* Do some final massaging of the length values.  */
    3872        31558 :   cl = gfc_new_charlen (gfc_current_ns, NULL);
    3873              : 
    3874        31558 :   if (seen_length == 0)
    3875         2449 :     cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    3876              :   else
    3877              :     {
    3878              :       /* If gfortran ends up here, then len may be reducible to a constant.
    3879              :          Try to do that here.  If it does not reduce, simply assign len to
    3880              :          charlen.  A complication occurs with user-defined generic functions,
    3881              :          which are not resolved.  Use a private namespace to deal with
    3882              :          generic functions.  */
    3883              : 
    3884        29109 :       if (len && len->expr_type != EXPR_CONSTANT)
    3885              :         {
    3886         3275 :           gfc_namespace *old_ns;
    3887         3275 :           gfc_expr *e;
    3888              : 
    3889         3275 :           old_ns = gfc_current_ns;
    3890         3275 :           gfc_current_ns = gfc_get_namespace (NULL, 0);
    3891              : 
    3892         3275 :           e = gfc_copy_expr (len);
    3893         3275 :           gfc_push_suppress_errors ();
    3894         3275 :           gfc_reduce_init_expr (e);
    3895         3275 :           gfc_pop_suppress_errors ();
    3896         3275 :           if (e->expr_type == EXPR_CONSTANT)
    3897              :             {
    3898          318 :               gfc_replace_expr (len, e);
    3899          318 :               if (mpz_cmp_si (len->value.integer, 0) < 0)
    3900            7 :                 mpz_set_ui (len->value.integer, 0);
    3901              :             }
    3902              :           else
    3903         2957 :             gfc_free_expr (e);
    3904              : 
    3905         3275 :           gfc_free_namespace (gfc_current_ns);
    3906         3275 :           gfc_current_ns = old_ns;
    3907              :         }
    3908              : 
    3909        29109 :       cl->length = len;
    3910              :     }
    3911              : 
    3912        31558 :   ts->u.cl = cl;
    3913        31558 :   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
    3914        31558 :   ts->deferred = deferred;
    3915              : 
    3916              :   /* We have to know if it was a C interoperable kind so we can
    3917              :      do accurate type checking of bind(c) procs, etc.  */
    3918        31558 :   if (kind != 0)
    3919              :     /* Mark this as C interoperable if being declared with one
    3920              :        of the named constants from iso_c_binding.  */
    3921         4886 :     ts->is_c_interop = is_iso_c;
    3922        26672 :   else if (len != NULL)
    3923              :     /* Here, we might have parsed something such as: character(c_char)
    3924              :        In this case, the parsing code above grabs the c_char when
    3925              :        looking for the length (line 1690, roughly).  it's the last
    3926              :        testcase for parsing the kind params of a character variable.
    3927              :        However, it's not actually the length.    this seems like it
    3928              :        could be an error.
    3929              :        To see if the user used a C interop kind, test the expr
    3930              :        of the so called length, and see if it's C interoperable.  */
    3931        17246 :     ts->is_c_interop = len->ts.is_iso_c;
    3932              : 
    3933              :   return MATCH_YES;
    3934              : }
    3935              : 
    3936              : 
    3937              : /* Matches a RECORD declaration. */
    3938              : 
    3939              : static match
    3940      1002146 : match_record_decl (char *name)
    3941              : {
    3942      1002146 :     locus old_loc;
    3943      1002146 :     old_loc = gfc_current_locus;
    3944      1002146 :     match m;
    3945              : 
    3946      1002146 :     m = gfc_match (" record /");
    3947      1002146 :     if (m == MATCH_YES)
    3948              :       {
    3949          353 :           if (!flag_dec_structure)
    3950              :             {
    3951            6 :                 gfc_current_locus = old_loc;
    3952            6 :                 gfc_error ("RECORD at %C is an extension, enable it with "
    3953              :                            "%<-fdec-structure%>");
    3954            6 :                 return MATCH_ERROR;
    3955              :             }
    3956          347 :           m = gfc_match (" %n/", name);
    3957          347 :           if (m == MATCH_YES)
    3958              :             return MATCH_YES;
    3959              :       }
    3960              : 
    3961      1001796 :   gfc_current_locus = old_loc;
    3962      1001796 :   if (flag_dec_structure
    3963      1001796 :       && (gfc_match (" record% ") == MATCH_YES
    3964         8026 :           || gfc_match (" record%t") == MATCH_YES))
    3965            6 :     gfc_error ("Structure name expected after RECORD at %C");
    3966      1001796 :   if (m == MATCH_NO)
    3967      1001796 :     return MATCH_NO;
    3968              : 
    3969              :   return MATCH_ERROR;
    3970              : }
    3971              : 
    3972              : 
    3973              :   /* In parsing a PDT, it is possible that one of the type parameters has the
    3974              :      same name as a previously declared symbol that is not a type parameter.
    3975              :      Intercept this now by looking for the symtree in f2k_derived.  */
    3976              : 
    3977              : static bool
    3978         1736 : correct_parm_expr (gfc_expr* e, gfc_symbol* pdt, int* f ATTRIBUTE_UNUSED)
    3979              : {
    3980         1736 :   if (!e || (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION))
    3981              :     return false;
    3982              : 
    3983         1384 :   if (!(e->symtree->n.sym->attr.pdt_len
    3984          278 :         || e->symtree->n.sym->attr.pdt_kind))
    3985              :     {
    3986          158 :       gfc_symtree *st;
    3987          158 :       st = gfc_find_symtree (pdt->f2k_derived->sym_root,
    3988              :                              e->symtree->n.sym->name);
    3989          158 :       if (st && st->n.sym
    3990           30 :           && (st->n.sym->attr.pdt_len || st->n.sym->attr.pdt_kind))
    3991              :         {
    3992           30 :           gfc_expr *new_expr;
    3993           30 :           gfc_set_sym_referenced (st->n.sym);
    3994           30 :           new_expr = gfc_get_expr ();
    3995           30 :           new_expr->ts = st->n.sym->ts;
    3996           30 :           new_expr->expr_type = EXPR_VARIABLE;
    3997           30 :           new_expr->symtree = st;
    3998           30 :           new_expr->where = e->where;
    3999           30 :           gfc_replace_expr (e, new_expr);
    4000              :         }
    4001              :     }
    4002              : 
    4003              :   return false;
    4004              : }
    4005              : 
    4006              : 
    4007              : void
    4008         1432 : gfc_correct_parm_expr (gfc_symbol *pdt, gfc_expr **bound)
    4009              : {
    4010         1432 :   if (!*bound || (*bound)->expr_type == EXPR_CONSTANT)
    4011              :     return;
    4012         1185 :   gfc_traverse_expr (*bound, pdt, &correct_parm_expr, 0);
    4013              : }
    4014              : 
    4015              : /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
    4016              :    of expressions to substitute into the possibly parameterized expression
    4017              :    'e'. Using a list is inefficient but should not be too bad since the
    4018              :    number of type parameters is not likely to be large.  */
    4019              : static bool
    4020         7241 : insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    4021              :                         int* f)
    4022              : {
    4023         7241 :   gfc_actual_arglist *param;
    4024         7241 :   gfc_expr *copy;
    4025              : 
    4026         7241 :   if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
    4027              :     return false;
    4028              : 
    4029         3148 :   gcc_assert (e->symtree);
    4030         3148 :   if (e->symtree->n.sym->attr.pdt_kind
    4031         2330 :       || (*f != 0 && e->symtree->n.sym->attr.pdt_len)
    4032         1037 :       || (e->expr_type == EXPR_FUNCTION && e->symtree->n.sym))
    4033              :     {
    4034         3353 :       for (param = type_param_spec_list; param; param = param->next)
    4035         3179 :         if (!strcmp (e->symtree->n.sym->name, param->name))
    4036              :           break;
    4037              : 
    4038         2284 :       if (param && param->expr)
    4039              :         {
    4040         2109 :           copy = gfc_copy_expr (param->expr);
    4041         2109 :           gfc_replace_expr (e, copy);
    4042              :           /* Catch variables declared without a value expression.  */
    4043         2109 :           if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_PROCEDURE)
    4044           33 :             e->ts = e->symtree->n.sym->ts;
    4045              :         }
    4046              :     }
    4047              : 
    4048              :   return false;
    4049              : }
    4050              : 
    4051              : 
    4052              : static bool
    4053         1863 : gfc_insert_kind_parameter_exprs (gfc_expr *e)
    4054              : {
    4055         1863 :   return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
    4056              : }
    4057              : 
    4058              : 
    4059              : bool
    4060         4107 : gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
    4061              : {
    4062         4107 :   gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
    4063         4107 :   type_param_spec_list = param_list;
    4064         4107 :   bool res = gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
    4065         4107 :   type_param_spec_list = old_param_spec_list;
    4066         4107 :   return res;
    4067              : }
    4068              : 
    4069              : /* Determines the instance of a parameterized derived type to be used by
    4070              :    matching determining the values of the kind parameters and using them
    4071              :    in the name of the instance. If the instance exists, it is used, otherwise
    4072              :    a new derived type is created.  */
    4073              : match
    4074         4656 : gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
    4075              :                       gfc_actual_arglist **ext_param_list)
    4076              : {
    4077              :   /* The PDT template symbol.  */
    4078         4656 :   gfc_symbol *pdt = *sym;
    4079              :   /* The symbol for the parameter in the template f2k_namespace.  */
    4080         4656 :   gfc_symbol *param;
    4081              :   /* The hoped for instance of the PDT.  */
    4082         4656 :   gfc_symbol *instance = NULL;
    4083              :   /* The list of parameters appearing in the PDT declaration.  */
    4084         4656 :   gfc_formal_arglist *type_param_name_list;
    4085              :   /* Used to store the parameter specification list during recursive calls.  */
    4086         4656 :   gfc_actual_arglist *old_param_spec_list;
    4087              :   /* Pointers to the parameter specification being used.  */
    4088         4656 :   gfc_actual_arglist *actual_param;
    4089         4656 :   gfc_actual_arglist *tail = NULL;
    4090              :   /* Used to build up the name of the PDT instance.  */
    4091         4656 :   char *name;
    4092         4656 :   bool name_seen = (param_list == NULL);
    4093         4656 :   bool assumed_seen = false;
    4094         4656 :   bool deferred_seen = false;
    4095         4656 :   bool spec_error = false;
    4096         4656 :   bool alloc_seen = false;
    4097         4656 :   bool ptr_seen = false;
    4098         4656 :   int i;
    4099         4656 :   gfc_expr *kind_expr;
    4100         4656 :   gfc_component *c1, *c2;
    4101              :   /* Head of the charlen list before the instance's components are built.  */
    4102         4656 :   gfc_charlen *saved_cl = NULL;
    4103         4656 :   match m;
    4104         4656 :   gfc_symtree *s = NULL;
    4105              : 
    4106         4656 :   type_param_spec_list = NULL;
    4107              : 
    4108         4656 :   type_param_name_list = pdt->formal;
    4109         4656 :   actual_param = param_list;
    4110              : 
    4111              :   /* Prevent a PDT component of the same type as the template from being
    4112              :      converted into an instance. Doing this results in the component being
    4113              :      lost.  */
    4114         4656 :   if (gfc_current_state () == COMP_DERIVED
    4115          155 :       && !(gfc_state_stack->previous
    4116          155 :            && gfc_state_stack->previous->state == COMP_DERIVED)
    4117          155 :       && gfc_current_block ()->attr.pdt_template)
    4118              :     {
    4119          136 :       if (ext_param_list)
    4120          136 :         *ext_param_list = gfc_copy_actual_arglist (param_list);
    4121              :       return MATCH_YES;
    4122              :     }
    4123              : 
    4124         4520 :   name = xasprintf ("%s%s", PDT_PREFIX, pdt->name);
    4125              : 
    4126              :   /* Run through the parameter name list and pick up the actual
    4127              :      parameter values or use the default values in the PDT declaration.  */
    4128        15215 :   for (; type_param_name_list;
    4129         6175 :        type_param_name_list = type_param_name_list->next)
    4130              :     {
    4131         6243 :       if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
    4132              :         {
    4133         5774 :           if (actual_param->spec_type == SPEC_ASSUMED)
    4134              :             spec_error = deferred_seen;
    4135              :           else
    4136         5774 :             spec_error = assumed_seen;
    4137              : 
    4138         5774 :           if (spec_error)
    4139              :             {
    4140              :               gfc_error ("The type parameter spec list at %C cannot contain "
    4141              :                          "both ASSUMED and DEFERRED parameters");
    4142              :               goto error_return;
    4143              :             }
    4144              :         }
    4145              : 
    4146         5774 :       if (actual_param && actual_param->name)
    4147         6243 :         name_seen = true;
    4148         6243 :       param = type_param_name_list->sym;
    4149              : 
    4150         6243 :       if (!param || !param->name)
    4151            2 :         continue;
    4152              : 
    4153         6241 :       c1 = gfc_find_component (pdt, param->name, false, true, NULL);
    4154              :       /* An error should already have been thrown in resolve.cc
    4155              :          (resolve_fl_derived0).  */
    4156         6241 :       if (!pdt->attr.use_assoc && !c1)
    4157            8 :         goto error_return;
    4158              : 
    4159              :       /* Resolution PDT class components of derived types are handled here.
    4160              :          They can arrive without a parameter list and no KIND parameters.  */
    4161         6233 :       if (!param_list && (!c1->attr.pdt_kind && !c1->initializer))
    4162           20 :         continue;
    4163              : 
    4164         6213 :       kind_expr = NULL;
    4165         6213 :       if (!name_seen)
    4166              :         {
    4167         4125 :           if (!actual_param && !(c1 && c1->initializer))
    4168              :             {
    4169            2 :               gfc_error ("The type parameter spec list at %C does not contain "
    4170              :                          "enough parameter expressions");
    4171            2 :               goto error_return;
    4172              :             }
    4173         4123 :           else if (!actual_param && c1 && c1->initializer)
    4174            5 :             kind_expr = gfc_copy_expr (c1->initializer);
    4175         4118 :           else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
    4176         3503 :             kind_expr = gfc_copy_expr (actual_param->expr);
    4177              :         }
    4178              :       else
    4179              :         {
    4180              :           actual_param = param_list;
    4181         3040 :           for (;actual_param; actual_param = actual_param->next)
    4182         2583 :             if (actual_param->name
    4183         2563 :                 && strcmp (actual_param->name, param->name) == 0)
    4184              :               break;
    4185         2088 :           if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
    4186         1440 :             kind_expr = gfc_copy_expr (actual_param->expr);
    4187              :           else
    4188              :             {
    4189          648 :               if (c1->initializer)
    4190          584 :                 kind_expr = gfc_copy_expr (c1->initializer);
    4191           64 :               else if (!(actual_param && param->attr.pdt_len))
    4192              :                 {
    4193            9 :                   gfc_error ("The derived parameter %qs at %C does not "
    4194              :                              "have a default value", param->name);
    4195            9 :                   goto error_return;
    4196              :                 }
    4197              :             }
    4198              :         }
    4199              : 
    4200         5532 :       if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
    4201          498 :           && kind_expr->ts.type != BT_INTEGER
    4202          172 :           && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
    4203              :         {
    4204           12 :           gfc_error ("The type parameter expression at %L must be of INTEGER "
    4205              :                      "type and not %s", &kind_expr->where,
    4206              :                      gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
    4207           12 :           goto error_return;
    4208              :         }
    4209              : 
    4210              :       /* Store the current parameter expressions in a temporary actual
    4211              :          arglist 'list' so that they can be substituted in the corresponding
    4212              :          expressions in the PDT instance.  */
    4213         6190 :       if (type_param_spec_list == NULL)
    4214              :         {
    4215         4477 :           type_param_spec_list = gfc_get_actual_arglist ();
    4216         4477 :           tail = type_param_spec_list;
    4217              :         }
    4218              :       else
    4219              :         {
    4220         1713 :           tail->next = gfc_get_actual_arglist ();
    4221         1713 :           tail = tail->next;
    4222              :         }
    4223         6190 :       tail->name = param->name;
    4224              : 
    4225         6190 :       if (kind_expr)
    4226              :         {
    4227              :           /* Try simplification even for LEN expressions.  */
    4228         5520 :           bool ok;
    4229         5520 :           gfc_resolve_expr (kind_expr);
    4230              : 
    4231         5520 :           if (c1->attr.pdt_kind
    4232         2673 :               && kind_expr->expr_type != EXPR_CONSTANT
    4233           28 :               && type_param_spec_list)
    4234           28 :           gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
    4235              : 
    4236         5520 :           ok = gfc_simplify_expr (kind_expr, 1);
    4237              :           /* Variable expressions default to BT_PROCEDURE in the absence of an
    4238              :              initializer so allow for this.  */
    4239         5520 :           if (kind_expr->ts.type != BT_INTEGER
    4240          189 :               && kind_expr->ts.type != BT_PROCEDURE)
    4241              :             {
    4242           29 :               gfc_error ("The parameter expression at %C must be of "
    4243              :                          "INTEGER type and not %s type",
    4244              :                          gfc_basic_typename (kind_expr->ts.type));
    4245           29 :               goto error_return;
    4246              :             }
    4247         5491 :           if (kind_expr->ts.type == BT_INTEGER && !ok)
    4248              :             {
    4249            4 :               gfc_error ("The parameter expression at %C does not "
    4250              :                          "simplify to an INTEGER constant");
    4251            4 :               goto error_return;
    4252              :             }
    4253              : 
    4254         5487 :           tail->expr = gfc_copy_expr (kind_expr);
    4255              :         }
    4256              : 
    4257         6157 :       if (actual_param)
    4258         5702 :         tail->spec_type = actual_param->spec_type;
    4259              : 
    4260         6157 :       if (!param->attr.pdt_kind)
    4261              :         {
    4262         3509 :           if (!name_seen && actual_param)
    4263         2553 :             actual_param = actual_param->next;
    4264         3509 :           if (kind_expr)
    4265              :             {
    4266         2841 :               gfc_free_expr (kind_expr);
    4267         2841 :               kind_expr = NULL;
    4268              :             }
    4269         3509 :           continue;
    4270              :         }
    4271              : 
    4272         2648 :       if (actual_param
    4273         2237 :           && (actual_param->spec_type == SPEC_ASSUMED
    4274         2237 :               || actual_param->spec_type == SPEC_DEFERRED))
    4275              :         {
    4276            2 :           gfc_error ("The KIND parameter %qs at %C cannot either be "
    4277              :                      "ASSUMED or DEFERRED", param->name);
    4278            2 :           goto error_return;
    4279              :         }
    4280              : 
    4281         2646 :       if (!kind_expr || !gfc_is_constant_expr (kind_expr))
    4282              :         {
    4283            2 :           gfc_error ("The value for the KIND parameter %qs at %C does not "
    4284              :                      "reduce to a constant expression", param->name);
    4285            2 :           goto error_return;
    4286              :         }
    4287              : 
    4288              :       /* This can come about during the parsing of nested pdt_templates. An
    4289              :          error arises because the KIND parameter expression has not been
    4290              :          provided. Use the template instead of an incorrect instance.  */
    4291         2644 :       if (kind_expr->expr_type != EXPR_CONSTANT
    4292         2644 :           || kind_expr->ts.type != BT_INTEGER)
    4293              :         {
    4294            0 :           gfc_free_actual_arglist (type_param_spec_list);
    4295            0 :           free (name);
    4296            0 :           return MATCH_YES;
    4297              :         }
    4298              : 
    4299         2644 :       char *kind_value = mpz_get_str (NULL, 10, kind_expr->value.integer);
    4300         2644 :       char *old_name = name;
    4301         2644 :       name = xasprintf ("%s_%s", old_name, kind_value);
    4302         2644 :       free (old_name);
    4303         2644 :       free (kind_value);
    4304              : 
    4305         2644 :       if (!name_seen && actual_param)
    4306         1516 :         actual_param = actual_param->next;
    4307         2644 :       gfc_free_expr (kind_expr);
    4308              :     }
    4309              : 
    4310         4452 :   if (!name_seen && actual_param)
    4311              :     {
    4312            2 :       gfc_error ("The type parameter spec list at %C contains too many "
    4313              :                  "parameter expressions");
    4314            2 :       goto error_return;
    4315              :     }
    4316              : 
    4317              :   /* Now we search for the PDT instance 'name'. If it doesn't exist, we
    4318              :      build it, using 'pdt' as a template.  */
    4319         4450 :   if (gfc_get_symbol (name, pdt->ns, &instance))
    4320              :     {
    4321            0 :       gfc_error ("Parameterized derived type at %C is ambiguous");
    4322            0 :       goto error_return;
    4323              :     }
    4324              : 
    4325              :   /* If we are in an interface body, the instance will not have been imported.
    4326              :      Make sure that it is imported implicitly.  */
    4327         4450 :   s = gfc_find_symtree (gfc_current_ns->sym_root, pdt->name);
    4328         4450 :   if (gfc_current_ns->proc_name
    4329         4403 :       && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
    4330          195 :       && s && s->import_only && pdt->attr.imported)
    4331              :     {
    4332            8 :       s = gfc_find_symtree (gfc_current_ns->sym_root, instance->name);
    4333            8 :       if (!s)
    4334              :         {
    4335            7 :           gfc_get_sym_tree (instance->name, gfc_current_ns, &s, false,
    4336              :                             &gfc_current_locus);
    4337            7 :           s->n.sym = instance;
    4338            7 :           instance->refs++;
    4339              :         }
    4340            8 :       s->n.sym->attr.imported = 1;
    4341            8 :       s->import_only = 1;
    4342              :     }
    4343              : 
    4344         4450 :   m = MATCH_YES;
    4345              : 
    4346         4450 :   if (instance->attr.flavor == FL_DERIVED
    4347         3507 :       && instance->attr.pdt_type
    4348         3507 :       && instance->components)
    4349              :     {
    4350         3507 :       instance->refs++;
    4351         3507 :       if (ext_param_list)
    4352         1712 :         *ext_param_list = type_param_spec_list;
    4353         3507 :       *sym = instance;
    4354         3507 :       gfc_commit_symbols ();
    4355         3507 :       free (name);
    4356         3507 :       return m;
    4357              :     }
    4358              : 
    4359              :   /* Start building the new instance of the parameterized type.  */
    4360          943 :   saved_cl = gfc_current_ns->cl_list;
    4361          943 :   gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
    4362          943 :   if (pdt->attr.use_assoc)
    4363          228 :     instance->module = pdt->module;
    4364          943 :   instance->attr.pdt_template = 0;
    4365          943 :   instance->attr.pdt_type = 1;
    4366          943 :   instance->declared_at = gfc_current_locus;
    4367              : 
    4368              :   /* In resolution, the finalizers are copied, according to the type of the
    4369              :      argument, to the instance finalizers. However, they are retained by the
    4370              :      template and procedures are freed there.  */
    4371          943 :   if (pdt->f2k_derived && pdt->f2k_derived->finalizers)
    4372              :     {
    4373           42 :       instance->f2k_derived = gfc_get_namespace (NULL, 0);
    4374           42 :       instance->template_sym = pdt;
    4375           42 :       *instance->f2k_derived = *pdt->f2k_derived;
    4376              :     }
    4377              : 
    4378              :   /* Add the components, replacing the parameters in all expressions
    4379              :      with the expressions for their values in 'type_param_spec_list'.  */
    4380          943 :   c1 = pdt->components;
    4381          943 :   tail = type_param_spec_list;
    4382         3666 :   for (; c1; c1 = c1->next)
    4383              :     {
    4384         2725 :       gfc_add_component (instance, c1->name, &c2);
    4385              : 
    4386         2725 :       c2->ts = c1->ts;
    4387         2725 :       c2->attr = c1->attr;
    4388         2725 :       if (c1->tb)
    4389              :         {
    4390           12 :           c2->tb = gfc_get_tbp ();
    4391           12 :           *c2->tb = *c1->tb;
    4392              :         }
    4393              : 
    4394              :       /* The order of declaration of the type_specs might not be the
    4395              :          same as that of the components.  */
    4396         2725 :       if (c1->attr.pdt_kind || c1->attr.pdt_len)
    4397              :         {
    4398         1810 :           for (tail = type_param_spec_list; tail; tail = tail->next)
    4399         1806 :             if (strcmp (c1->name, tail->name) == 0)
    4400              :               break;
    4401              :         }
    4402              : 
    4403              :       /* Deal with type extension by recursively calling this function
    4404              :          to obtain the instance of the extended type.  */
    4405         2725 :       if (gfc_current_state () != COMP_DERIVED
    4406         2693 :           && c1 == pdt->components
    4407          924 :           && c1->ts.type == BT_DERIVED
    4408          108 :           && c1->ts.u.derived
    4409         2833 :           && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
    4410              :         {
    4411          108 :           if (c1->ts.u.derived->attr.pdt_template)
    4412              :             {
    4413           95 :               gfc_formal_arglist *f;
    4414              : 
    4415           95 :               old_param_spec_list = type_param_spec_list;
    4416              : 
    4417              :               /* Obtain a spec list appropriate to the extended type..*/
    4418           95 :               actual_param = gfc_copy_actual_arglist (type_param_spec_list);
    4419           95 :               type_param_spec_list = actual_param;
    4420          175 :               for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
    4421           80 :                 actual_param = actual_param->next;
    4422           95 :               if (actual_param)
    4423              :                 {
    4424           95 :                   gfc_free_actual_arglist (actual_param->next);
    4425           95 :                   actual_param->next = NULL;
    4426              :                 }
    4427              : 
    4428              :               /* Now obtain the PDT instance for the extended type.  */
    4429           95 :               c2->param_list = type_param_spec_list;
    4430           95 :               m = gfc_get_pdt_instance (type_param_spec_list,
    4431              :                                         &c2->ts.u.derived,
    4432              :                                         &c2->param_list);
    4433           95 :               type_param_spec_list = old_param_spec_list;
    4434              :             }
    4435              :           else
    4436           13 :             c2->ts = c1->ts;
    4437              : 
    4438          108 :           c2->ts.u.derived->refs++;
    4439          108 :           gfc_set_sym_referenced (c2->ts.u.derived);
    4440              : 
    4441              :           /* If the component is allocatable or the parent has allocatable
    4442              :              components, make sure that the new instance also is marked as
    4443              :              having allocatable components.  */
    4444          108 :           if (c2->attr.allocatable || c2->ts.u.derived->attr.alloc_comp)
    4445            6 :             instance->attr.alloc_comp = 1;
    4446              : 
    4447              :           /* Set extension level.  */
    4448          108 :           if (c2->ts.u.derived->attr.extension == 255)
    4449              :             {
    4450              :               /* Since the extension field is 8 bit wide, we can only have
    4451              :                  up to 255 extension levels.  */
    4452            0 :               gfc_error ("Maximum extension level reached with type %qs at %L",
    4453              :                          c2->ts.u.derived->name,
    4454              :                          &c2->ts.u.derived->declared_at);
    4455            0 :               goto error_return;
    4456              :             }
    4457          108 :           instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
    4458              : 
    4459              :           /* Add the parent initializer.  */
    4460          108 :           if (!IS_PDT (c2) && !c2->ts.u.derived->attr.alloc_comp)
    4461            7 :             c2->initializer = gfc_generate_initializer (&c2->ts, false);
    4462          108 :           continue;
    4463          108 :         }
    4464              : 
    4465              :       /* Addressing PR82943, this will fix the issue where a function or
    4466              :          subroutine is declared as not a member of the PDT instance.
    4467              :          The reason for this is because the PDT instance did not have access
    4468              :          to its template's f2k_derived namespace in order to find the
    4469              :          typebound procedures.
    4470              : 
    4471              :          The number of references to the PDT template's f2k_derived will
    4472              :          ensure that f2k_derived is properly freed later on.  */
    4473              : 
    4474         2617 :       if (!instance->f2k_derived && pdt->f2k_derived)
    4475              :         {
    4476          894 :           instance->f2k_derived = pdt->f2k_derived;
    4477          894 :           instance->f2k_derived->refs++;
    4478              :         }
    4479              : 
    4480              :       /* Set the component kind using the parameterized expression.  */
    4481         2617 :       if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
    4482          917 :            && c1->kind_expr != NULL)
    4483              :         {
    4484          573 :           gfc_expr *e = gfc_copy_expr (c1->kind_expr);
    4485          573 :           gfc_insert_kind_parameter_exprs (e);
    4486          573 :           gfc_simplify_expr (e, 1);
    4487          573 :           gfc_extract_int (e, &c2->ts.kind);
    4488          573 :           gfc_free_expr (e);
    4489          573 :           if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
    4490              :             {
    4491            2 :               gfc_error ("Kind %d not supported for type %s at %C",
    4492              :                          c2->ts.kind, gfc_basic_typename (c2->ts.type));
    4493            2 :               goto error_return;
    4494              :             }
    4495          571 :           if (c2->attr.proc_pointer && c2->attr.function
    4496            0 :               && c1->ts.interface && c1->ts.interface->ts.kind == 0)
    4497              :             {
    4498            0 :               c2->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    4499            0 :               c2->ts.interface->result = c2->ts.interface;
    4500            0 :               c2->ts.interface->ts = c2->ts;
    4501            0 :               c2->ts.interface->attr.flavor = FL_PROCEDURE;
    4502            0 :               c2->ts.interface->attr.function = 1;
    4503            0 :               c2->attr.function = 1;
    4504            0 :               c2->attr.if_source = IFSRC_UNKNOWN;
    4505              :             }
    4506              :         }
    4507              : 
    4508              :       /* Set up either the KIND/LEN initializer, if constant,
    4509              :          or the parameterized expression. Use the template
    4510              :          initializer if one is not already set in this instance.  */
    4511         2615 :       if (c2->attr.pdt_kind || c2->attr.pdt_len)
    4512              :         {
    4513         1242 :           if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
    4514          982 :             c2->initializer = gfc_copy_expr (tail->expr);
    4515          260 :           else if (tail && tail->expr)
    4516              :             {
    4517           40 :               c2->param_list = gfc_get_actual_arglist ();
    4518           40 :               c2->param_list->name = tail->name;
    4519           40 :               c2->param_list->expr = gfc_copy_expr (tail->expr);
    4520           40 :               c2->param_list->next = NULL;
    4521              :             }
    4522              : 
    4523              :           /* Initializer expressions in PDT templates, such as character_kinds(1),
    4524              :              can end up being mutilated when use associated. Simplify now.  */
    4525         1242 :           if (c1->initializer && c1->initializer->expr_type != EXPR_CONSTANT)
    4526          203 :             gfc_simplify_expr (c1->initializer, 1);
    4527              : 
    4528         1242 :           if (!c2->initializer && c1->initializer)
    4529          102 :             c2->initializer = gfc_copy_expr (c1->initializer);
    4530              : 
    4531         1242 :           if (c2->initializer)
    4532         1084 :             gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4533              :         }
    4534              : 
    4535              :       /* Copy the array spec.  */
    4536         2615 :       c2->as = gfc_copy_array_spec (c1->as);
    4537         2615 :       if (c1->ts.type == BT_CLASS)
    4538            0 :         CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
    4539              : 
    4540         2615 :       if (c1->attr.allocatable)
    4541          118 :         alloc_seen = true;
    4542              : 
    4543         2615 :       if (c1->attr.pointer)
    4544           32 :         ptr_seen = true;
    4545              : 
    4546              :       /* Determine if an array spec is parameterized. If so, substitute
    4547              :          in the parameter expressions for the bounds and set the pdt_array
    4548              :          attribute. Notice that this attribute must be unconditionally set
    4549              :          if this is an array of parameterized character length.  */
    4550         2615 :       if (c1->as && c1->as->type == AS_EXPLICIT)
    4551              :         {
    4552              :           bool pdt_array = false;
    4553         1200 :           bool all_constant = true;
    4554              : 
    4555              :           /* Are the bounds of the array parameterized?  */
    4556         1200 :           for (i = 0; i < c1->as->rank; i++)
    4557              :             {
    4558          654 :               if (gfc_derived_parameter_expr (c1->as->lower[i]))
    4559           12 :                 pdt_array = true;
    4560          654 :               if (gfc_derived_parameter_expr (c1->as->upper[i]))
    4561          526 :                 pdt_array = true;
    4562              :             }
    4563              : 
    4564              :           /* If they are, free the expressions for the bounds and
    4565              :              replace them with the template expressions with substitute
    4566              :              values.  */
    4567         1078 :           for (i = 0; pdt_array && i < c1->as->rank; i++)
    4568              :             {
    4569          532 :               gfc_expr *e;
    4570          532 :               e = gfc_copy_expr (c1->as->lower[i]);
    4571          532 :               gfc_insert_kind_parameter_exprs (e);
    4572          532 :               if (gfc_simplify_expr (e, 1))
    4573          532 :                 gfc_replace_expr (c2->as->lower[i], e);
    4574              :               else
    4575            0 :                 gfc_free_expr (e);
    4576          532 :               if (c2->as->lower[i]->expr_type != EXPR_CONSTANT)
    4577           12 :                 all_constant = false;
    4578          532 :               e = gfc_copy_expr (c1->as->upper[i]);
    4579          532 :               gfc_insert_kind_parameter_exprs (e);
    4580          532 :               if (gfc_simplify_expr (e, 1))
    4581          532 :                 gfc_replace_expr (c2->as->upper[i], e);
    4582              :               else
    4583            0 :                 gfc_free_expr (e);
    4584          532 :               if (c2->as->upper[i]->expr_type != EXPR_CONSTANT)
    4585          524 :                 all_constant = false;
    4586              :             }
    4587              : 
    4588          546 :           c2->attr.pdt_array = all_constant ? 0 : 1;
    4589          546 :           if (c1->initializer)
    4590              :             {
    4591           37 :               c2->initializer = gfc_copy_expr (c1->initializer);
    4592           37 :               gfc_insert_kind_parameter_exprs (c2->initializer);
    4593           37 :               gfc_simplify_expr (c2->initializer, 1);
    4594              :             }
    4595              :         }
    4596              : 
    4597              :       /* Similarly, set the string length if parameterized.  */
    4598         2615 :       if (c1->ts.type == BT_CHARACTER
    4599          250 :           && c1->ts.u.cl->length
    4600         2864 :           && gfc_derived_parameter_expr (c1->ts.u.cl->length))
    4601              :         {
    4602          189 :           gfc_expr *e;
    4603          189 :           e = gfc_copy_expr (c1->ts.u.cl->length);
    4604          189 :           gfc_insert_kind_parameter_exprs (e);
    4605          189 :           if (gfc_simplify_expr (e, 1))
    4606          189 :             gfc_replace_expr (c2->ts.u.cl->length, e);
    4607              :           else
    4608            0 :             gfc_free_expr (e);
    4609          189 :           if (c2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    4610          186 :             c2->attr.pdt_string = 1;
    4611          189 :           if (c1->as && c1->as->type == AS_EXPLICIT)
    4612           60 :             c2->attr.pdt_array = 1;
    4613          129 :           else if (c1->attr.allocatable)
    4614            6 :             c2->ts.deferred = 1;
    4615              :         }
    4616              : 
    4617              :       /* Recurse into this function for PDT components.  */
    4618         2615 :       if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
    4619          197 :           && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
    4620              :         {
    4621          171 :           gfc_actual_arglist *params;
    4622              :           /* The component in the template has a list of specification
    4623              :              expressions derived from its declaration.  */
    4624          171 :           params = gfc_copy_actual_arglist (c1->param_list);
    4625          171 :           actual_param = params;
    4626              :           /* Substitute the template parameters with the expressions
    4627              :              from the specification list.  */
    4628          546 :           for (;actual_param; actual_param = actual_param->next)
    4629              :             {
    4630          204 :               gfc_correct_parm_expr (pdt, &actual_param->expr);
    4631          204 :               gfc_insert_parameter_exprs (actual_param->expr,
    4632              :                                           type_param_spec_list);
    4633              :             }
    4634              : 
    4635              :           /* Now obtain the PDT instance for the component.  */
    4636          171 :           old_param_spec_list = type_param_spec_list;
    4637          342 :           m = gfc_get_pdt_instance (params, &c2->ts.u.derived,
    4638          171 :                                     &c2->param_list);
    4639          171 :           type_param_spec_list = old_param_spec_list;
    4640              : 
    4641          171 :           if (!(c2->attr.pointer || c2->attr.allocatable))
    4642              :             {
    4643          119 :               if (!c1->initializer
    4644           94 :                   || c1->initializer->expr_type != EXPR_FUNCTION)
    4645          118 :                 c2->initializer = gfc_default_initializer (&c2->ts);
    4646              :               else
    4647              :                 {
    4648            1 :                   gfc_symtree *s;
    4649            1 :                   c2->initializer = gfc_copy_expr (c1->initializer);
    4650            1 :                   s = gfc_find_symtree (pdt->ns->sym_root,
    4651            1 :                                 gfc_dt_lower_string (c2->ts.u.derived->name));
    4652            1 :                   if (s)
    4653            0 :                     c2->initializer->symtree = s;
    4654            0 :                   if (!s)
    4655            1 :                     gfc_insert_parameter_exprs (c2->initializer,
    4656              :                                                 type_param_spec_list);
    4657              :                 }
    4658              :             }
    4659              : 
    4660          171 :           if (c2->attr.allocatable
    4661          127 :               || (c2->ts.type == BT_DERIVED && c2->ts.u.derived
    4662          127 :                   && c2->ts.u.derived->attr.alloc_comp && !c2->attr.pointer))
    4663           79 :             instance->attr.alloc_comp = 1;
    4664              :         }
    4665         2444 :       else if (!(c2->attr.pdt_kind || c2->attr.pdt_len || c2->attr.pdt_string
    4666         1016 :                  || c2->attr.pdt_array) && c1->initializer)
    4667              :         {
    4668          183 :           c2->initializer = gfc_copy_expr (c1->initializer);
    4669          183 :           gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4670              :           /* The template initializers are parsed using gfc_match_expr rather
    4671              :              than gfc_match_init_expr. Apply the missing reduction to the
    4672              :              PDT instance initializers.  */
    4673          183 :           if (!gfc_reduce_init_expr (c2->initializer))
    4674              :             {
    4675            0 :               gfc_free_expr (c2->initializer);
    4676            0 :               goto error_return;
    4677              :             }
    4678              :         }
    4679              : 
    4680              :       /* Pick up any remaining initializers and, if necessary, substitite
    4681              :          parameters, simplify and correct the typespec.  */
    4682         2615 :       if (c1->initializer)
    4683              :         {
    4684          963 :           if (!c2->initializer)
    4685           37 :             c2->initializer = gfc_copy_expr (c1->initializer);
    4686              : 
    4687          963 :           if (gfc_derived_parameter_expr (c2->initializer))
    4688            0 :             gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4689              : 
    4690          963 :           if (!gfc_is_constant_expr (c2->initializer))
    4691           25 :             gfc_simplify_expr (c2->initializer, 1);
    4692              : 
    4693          963 :           if (c2->initializer->ts.type == BT_UNKNOWN
    4694          926 :               || (c2->initializer->ts.type == c2->ts.type))
    4695          951 :             c2->initializer->ts = c2->ts;
    4696              :         }
    4697              :     }
    4698              : 
    4699          941 :   if (alloc_seen)
    4700          115 :     instance->attr.alloc_comp = 1;
    4701          941 :   if (ptr_seen)
    4702           32 :     instance->attr.pointer_comp = 1;
    4703              : 
    4704          941 :   gfc_set_sym_referenced (instance);
    4705          941 :   gfc_commit_symbol (instance);
    4706              : 
    4707              :   /* The instance survives a rejected statement, so its charlens must too.  */
    4708         1100 :   for (gfc_charlen *cl = gfc_current_ns->cl_list; cl && cl != saved_cl;
    4709          159 :        cl = cl->next)
    4710          159 :     gfc_remove_saved_charlen (cl);
    4711              : 
    4712          941 :   if (ext_param_list)
    4713          631 :     *ext_param_list = type_param_spec_list;
    4714          941 :   *sym = instance;
    4715          941 :   free (name);
    4716          941 :   return m;
    4717              : 
    4718           72 : error_return:
    4719           72 :   gfc_free_actual_arglist (type_param_spec_list);
    4720           72 :   free (name);
    4721           72 :   return MATCH_ERROR;
    4722              : }
    4723              : 
    4724              : 
    4725              : /* Match a legacy nonstandard BYTE type-spec.  */
    4726              : 
    4727              : static match
    4728      1233476 : match_byte_typespec (gfc_typespec *ts)
    4729              : {
    4730      1233476 :   if (gfc_match (" byte") == MATCH_YES)
    4731              :     {
    4732           33 :       if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
    4733              :         return MATCH_ERROR;
    4734              : 
    4735           31 :       if (gfc_current_form == FORM_FREE)
    4736              :         {
    4737           19 :           char c = gfc_peek_ascii_char ();
    4738           19 :           if (!gfc_is_whitespace (c) && c != ',')
    4739              :             return MATCH_NO;
    4740              :         }
    4741              : 
    4742           29 :       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
    4743              :         {
    4744            0 :           gfc_error ("BYTE type used at %C "
    4745              :                      "is not available on the target machine");
    4746            0 :           return MATCH_ERROR;
    4747              :         }
    4748              : 
    4749           29 :       ts->type = BT_INTEGER;
    4750           29 :       ts->kind = 1;
    4751           29 :       return MATCH_YES;
    4752              :     }
    4753              :   return MATCH_NO;
    4754              : }
    4755              : 
    4756              : 
    4757              : /* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
    4758              :    structure to the matched specification.  This is necessary for FUNCTION and
    4759              :    IMPLICIT statements.
    4760              : 
    4761              :    If implicit_flag is nonzero, then we don't check for the optional
    4762              :    kind specification.  Not doing so is needed for matching an IMPLICIT
    4763              :    statement correctly.  */
    4764              : 
    4765              : match
    4766      1233476 : gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
    4767              : {
    4768              :   /* Provide sufficient space to hold "pdtsymbol".  */
    4769      1233476 :   char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
    4770      1233476 :   gfc_symbol *sym, *dt_sym;
    4771      1233476 :   match m;
    4772      1233476 :   char c;
    4773      1233476 :   bool seen_deferred_kind, matched_type;
    4774      1233476 :   const char *dt_name;
    4775              : 
    4776      1233476 :   decl_type_param_list = NULL;
    4777              : 
    4778              :   /* A belt and braces check that the typespec is correctly being treated
    4779              :      as a deferred characteristic association.  */
    4780      2466952 :   seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
    4781        86972 :                           && (gfc_current_block ()->result->ts.kind == -1)
    4782      1245689 :                           && (ts->kind == -1);
    4783      1233476 :   gfc_clear_ts (ts);
    4784      1233476 :   if (seen_deferred_kind)
    4785         9978 :     ts->kind = -1;
    4786              : 
    4787              :   /* Clear the current binding label, in case one is given.  */
    4788      1233476 :   curr_binding_label = NULL;
    4789              : 
    4790              :   /* Match BYTE type-spec.  */
    4791      1233476 :   m = match_byte_typespec (ts);
    4792      1233476 :   if (m != MATCH_NO)
    4793              :     return m;
    4794              : 
    4795      1233445 :   m = gfc_match (" type (");
    4796      1233445 :   matched_type = (m == MATCH_YES);
    4797      1233445 :   if (matched_type)
    4798              :     {
    4799        33375 :       gfc_gobble_whitespace ();
    4800        33375 :       if (gfc_peek_ascii_char () == '*')
    4801              :         {
    4802         5635 :           if ((m = gfc_match ("* ) ")) != MATCH_YES)
    4803              :             return m;
    4804         5635 :           if (gfc_comp_struct (gfc_current_state ()))
    4805              :             {
    4806            2 :               gfc_error ("Assumed type at %C is not allowed for components");
    4807            2 :               return MATCH_ERROR;
    4808              :             }
    4809         5633 :           if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
    4810              :             return MATCH_ERROR;
    4811         5631 :           ts->type = BT_ASSUMED;
    4812         5631 :           return MATCH_YES;
    4813              :         }
    4814              : 
    4815        27740 :       m = gfc_match ("%n", name);
    4816        27740 :       matched_type = (m == MATCH_YES);
    4817              :     }
    4818              : 
    4819        27740 :   if ((matched_type && strcmp ("integer", name) == 0)
    4820      1227810 :       || (!matched_type && gfc_match (" integer") == MATCH_YES))
    4821              :     {
    4822       117119 :       ts->type = BT_INTEGER;
    4823       117119 :       ts->kind = gfc_default_integer_kind;
    4824       117119 :       goto get_kind;
    4825              :     }
    4826              : 
    4827      1110691 :   if (flag_unsigned)
    4828              :     {
    4829            0 :       if ((matched_type && strcmp ("unsigned", name) == 0)
    4830        23032 :           || (!matched_type && gfc_match (" unsigned") == MATCH_YES))
    4831              :         {
    4832         1090 :           ts->type = BT_UNSIGNED;
    4833         1090 :           ts->kind = gfc_default_integer_kind;
    4834         1090 :           goto get_kind;
    4835              :         }
    4836              :     }
    4837              : 
    4838        27734 :   if ((matched_type && strcmp ("character", name) == 0)
    4839      1109601 :       || (!matched_type && gfc_match (" character") == MATCH_YES))
    4840              :     {
    4841        30024 :       if (matched_type
    4842        30024 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4843              :                               "intrinsic-type-spec at %C"))
    4844              :         return MATCH_ERROR;
    4845              : 
    4846        30023 :       ts->type = BT_CHARACTER;
    4847        30023 :       if (implicit_flag == 0)
    4848        29917 :         m = gfc_match_char_spec (ts);
    4849              :       else
    4850              :         m = MATCH_YES;
    4851              : 
    4852        30023 :       if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
    4853              :         {
    4854            1 :           gfc_error ("Malformed type-spec at %C");
    4855            1 :           return MATCH_ERROR;
    4856              :         }
    4857              : 
    4858              :       return m;
    4859              :     }
    4860              : 
    4861        27730 :   if ((matched_type && strcmp ("real", name) == 0)
    4862      1079577 :       || (!matched_type && gfc_match (" real") == MATCH_YES))
    4863              :     {
    4864        30990 :       ts->type = BT_REAL;
    4865        30990 :       ts->kind = gfc_default_real_kind;
    4866        30990 :       goto get_kind;
    4867              :     }
    4868              : 
    4869      1048587 :   if ((matched_type
    4870        27727 :        && (strcmp ("doubleprecision", name) == 0
    4871        27726 :            || (strcmp ("double", name) == 0
    4872            5 :                && gfc_match (" precision") == MATCH_YES)))
    4873      1048587 :       || (!matched_type && gfc_match (" double precision") == MATCH_YES))
    4874              :     {
    4875         2614 :       if (matched_type
    4876         2614 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4877              :                               "intrinsic-type-spec at %C"))
    4878              :         return MATCH_ERROR;
    4879              : 
    4880         2613 :       if (matched_type && gfc_match_char (')') != MATCH_YES)
    4881              :         {
    4882            2 :           gfc_error ("Malformed type-spec at %C");
    4883            2 :           return MATCH_ERROR;
    4884              :         }
    4885              : 
    4886         2611 :       ts->type = BT_REAL;
    4887         2611 :       ts->kind = gfc_default_double_kind;
    4888         2611 :       return MATCH_YES;
    4889              :     }
    4890              : 
    4891        27723 :   if ((matched_type && strcmp ("complex", name) == 0)
    4892      1045973 :       || (!matched_type && gfc_match (" complex") == MATCH_YES))
    4893              :     {
    4894         4184 :       ts->type = BT_COMPLEX;
    4895         4184 :       ts->kind = gfc_default_complex_kind;
    4896         4184 :       goto get_kind;
    4897              :     }
    4898              : 
    4899      1041789 :   if ((matched_type
    4900        27723 :        && (strcmp ("doublecomplex", name) == 0
    4901        27722 :            || (strcmp ("double", name) == 0
    4902            2 :                && gfc_match (" complex") == MATCH_YES)))
    4903      1041789 :       || (!matched_type && gfc_match (" double complex") == MATCH_YES))
    4904              :     {
    4905          204 :       if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
    4906              :         return MATCH_ERROR;
    4907              : 
    4908          203 :       if (matched_type
    4909          203 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4910              :                               "intrinsic-type-spec at %C"))
    4911              :         return MATCH_ERROR;
    4912              : 
    4913          203 :       if (matched_type && gfc_match_char (')') != MATCH_YES)
    4914              :         {
    4915            2 :           gfc_error ("Malformed type-spec at %C");
    4916            2 :           return MATCH_ERROR;
    4917              :         }
    4918              : 
    4919          201 :       ts->type = BT_COMPLEX;
    4920          201 :       ts->kind = gfc_default_double_kind;
    4921          201 :       return MATCH_YES;
    4922              :     }
    4923              : 
    4924        27720 :   if ((matched_type && strcmp ("logical", name) == 0)
    4925      1041585 :       || (!matched_type && gfc_match (" logical") == MATCH_YES))
    4926              :     {
    4927        11722 :       ts->type = BT_LOGICAL;
    4928        11722 :       ts->kind = gfc_default_logical_kind;
    4929        11722 :       goto get_kind;
    4930              :     }
    4931              : 
    4932      1029863 :   if (matched_type)
    4933              :     {
    4934        27717 :       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
    4935        27717 :       if (m == MATCH_ERROR)
    4936              :         return m;
    4937              : 
    4938        27717 :       gfc_gobble_whitespace ();
    4939        27717 :       if (gfc_peek_ascii_char () != ')')
    4940              :         {
    4941            1 :           gfc_error ("Malformed type-spec at %C");
    4942            1 :           return MATCH_ERROR;
    4943              :         }
    4944        27716 :       m = gfc_match_char (')'); /* Burn closing ')'.  */
    4945              :     }
    4946              : 
    4947      1029862 :   if (m != MATCH_YES)
    4948      1002146 :     m = match_record_decl (name);
    4949              : 
    4950      1029862 :   if (matched_type || m == MATCH_YES)
    4951              :     {
    4952        28060 :       ts->type = BT_DERIVED;
    4953              :       /* We accept record/s/ or type(s) where s is a structure, but we
    4954              :        * don't need all the extra derived-type stuff for structures.  */
    4955        28060 :       if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
    4956              :         {
    4957            1 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    4958            1 :           return MATCH_ERROR;
    4959              :         }
    4960              : 
    4961        28059 :       if (sym && sym->attr.flavor == FL_DERIVED
    4962        26909 :           && sym->attr.pdt_template
    4963         1668 :           && gfc_current_state () != COMP_DERIVED)
    4964              :         {
    4965         1505 :           m = gfc_get_pdt_instance (decl_type_param_list, &sym,  NULL);
    4966         1505 :           if (m != MATCH_YES)
    4967              :             return m;
    4968         1490 :           gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
    4969         1490 :           ts->u.derived = sym;
    4970         1490 :           const char* lower = gfc_dt_lower_string (sym->name);
    4971         1490 :           size_t len = strlen (lower);
    4972              :           /* Reallocate with sufficient size.  */
    4973         1490 :           if (len > GFC_MAX_SYMBOL_LEN)
    4974            2 :             name = XALLOCAVEC (char, len + 1);
    4975         1490 :           memcpy (name, lower, len);
    4976         1490 :           name[len] = '\0';
    4977              :         }
    4978              : 
    4979        28044 :       if (sym && sym->attr.flavor == FL_STRUCT)
    4980              :         {
    4981          361 :           ts->u.derived = sym;
    4982          361 :           return MATCH_YES;
    4983              :         }
    4984              :       /* Actually a derived type.  */
    4985              :     }
    4986              : 
    4987              :   else
    4988              :     {
    4989              :       /* Match nested STRUCTURE declarations; only valid within another
    4990              :          structure declaration.  */
    4991      1001802 :       if (flag_dec_structure
    4992         8032 :           && (gfc_current_state () == COMP_STRUCTURE
    4993         7570 :               || gfc_current_state () == COMP_MAP))
    4994              :         {
    4995          732 :           m = gfc_match (" structure");
    4996          732 :           if (m == MATCH_YES)
    4997              :             {
    4998           27 :               m = gfc_match_structure_decl ();
    4999           27 :               if (m == MATCH_YES)
    5000              :                 {
    5001              :                   /* gfc_new_block is updated by match_structure_decl.  */
    5002           26 :                   ts->type = BT_DERIVED;
    5003           26 :                   ts->u.derived = gfc_new_block;
    5004           26 :                   return MATCH_YES;
    5005              :                 }
    5006              :             }
    5007          706 :           if (m == MATCH_ERROR)
    5008              :             return MATCH_ERROR;
    5009              :         }
    5010              : 
    5011              :       /* Match CLASS declarations.  */
    5012      1001775 :       m = gfc_match (" class ( * )");
    5013      1001775 :       if (m == MATCH_ERROR)
    5014              :         return MATCH_ERROR;
    5015      1001775 :       else if (m == MATCH_YES)
    5016              :         {
    5017         2160 :           gfc_symbol *upe;
    5018         2160 :           gfc_symtree *st;
    5019         2160 :           ts->type = BT_CLASS;
    5020         2160 :           gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
    5021         2160 :           if (upe == NULL)
    5022              :             {
    5023         1310 :               upe = gfc_new_symbol ("STAR", gfc_current_ns);
    5024         1310 :               st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
    5025         1310 :               st->n.sym = upe;
    5026         1310 :               gfc_set_sym_referenced (upe);
    5027         1310 :               upe->refs++;
    5028         1310 :               upe->ts.type = BT_VOID;
    5029         1310 :               upe->attr.unlimited_polymorphic = 1;
    5030              :               /* This is essential to force the construction of
    5031              :                  unlimited polymorphic component class containers.  */
    5032         1310 :               upe->attr.zero_comp = 1;
    5033         1310 :               if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
    5034              :                                    &gfc_current_locus))
    5035              :               return MATCH_ERROR;
    5036              :             }
    5037              :           else
    5038              :             {
    5039          850 :               st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
    5040          850 :               st->n.sym = upe;
    5041          850 :               upe->refs++;
    5042              :             }
    5043         2160 :           ts->u.derived = upe;
    5044         2160 :           return m;
    5045              :         }
    5046              : 
    5047       999615 :       m = gfc_match (" class (");
    5048              : 
    5049       999615 :       if (m == MATCH_YES)
    5050         9756 :         m = gfc_match ("%n", name);
    5051              :       else
    5052              :         return m;
    5053              : 
    5054         9756 :       if (m != MATCH_YES)
    5055              :         return m;
    5056         9756 :       ts->type = BT_CLASS;
    5057              : 
    5058         9756 :       if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
    5059              :         return MATCH_ERROR;
    5060              : 
    5061         9755 :       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
    5062         9755 :       if (m == MATCH_ERROR)
    5063              :         return m;
    5064              : 
    5065         9755 :       m = gfc_match_char (')');
    5066         9755 :       if (m != MATCH_YES)
    5067              :         return m;
    5068              :     }
    5069              : 
    5070              :   /* This picks up function declarations with a PDT typespec. Since a
    5071              :      pdt_type has been generated, there is no more to do. Within the
    5072              :      function body, this type must be used for the typespec so that
    5073              :      the "being used before it is defined warning" does not arise.  */
    5074        37438 :   if (ts->type == BT_DERIVED
    5075        27683 :       && sym && sym->attr.pdt_type
    5076        38928 :       && (gfc_current_state () == COMP_CONTAINS
    5077         1474 :           || (gfc_current_state () == COMP_FUNCTION
    5078          382 :               && gfc_current_block ()->ts.type == BT_DERIVED
    5079           90 :               && gfc_current_block ()->ts.u.derived == sym
    5080           60 :               && !gfc_find_symtree (gfc_current_ns->sym_root,
    5081              :                                     sym->name))))
    5082              :     {
    5083           60 :       if (gfc_current_state () == COMP_FUNCTION)
    5084              :         {
    5085           44 :           gfc_symtree *pdt_st;
    5086           44 :           pdt_st = gfc_new_symtree (&gfc_current_ns->sym_root,
    5087              :                                     sym->name);
    5088           44 :           pdt_st->n.sym = sym;
    5089           44 :           sym->refs++;
    5090              :         }
    5091           60 :       ts->u.derived = sym;
    5092           60 :       return MATCH_YES;
    5093              :     }
    5094              : 
    5095              :   /* Defer association of the derived type until the end of the
    5096              :      specification block.  However, if the derived type can be
    5097              :      found, add it to the typespec.  */
    5098        37378 :   if (gfc_matching_function)
    5099              :     {
    5100         1044 :       ts->u.derived = NULL;
    5101         1044 :       if (gfc_current_state () != COMP_INTERFACE
    5102         1044 :             && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
    5103              :         {
    5104          513 :           sym = gfc_find_dt_in_generic (sym);
    5105          513 :           ts->u.derived = sym;
    5106              :         }
    5107              :       return MATCH_YES;
    5108              :     }
    5109              : 
    5110              :   /* Search for the name but allow the components to be defined later.  If
    5111              :      type = -1, this typespec has been seen in a function declaration but
    5112              :      the type could not be accessed at that point.  The actual derived type is
    5113              :      stored in a symtree with the first letter of the name capitalized; the
    5114              :      symtree with the all lower-case name contains the associated
    5115              :      generic function.  */
    5116        36334 :   dt_name = gfc_dt_upper_string (name);
    5117        36334 :   sym = NULL;
    5118        36334 :   dt_sym = NULL;
    5119        36334 :   if (ts->kind != -1)
    5120              :     {
    5121        35121 :       gfc_get_ha_symbol (name, &sym);
    5122        35121 :       if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
    5123              :         {
    5124            0 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    5125            0 :           return MATCH_ERROR;
    5126              :         }
    5127        35121 :       if (sym->generic && !dt_sym)
    5128        15347 :         dt_sym = gfc_find_dt_in_generic (sym);
    5129              : 
    5130              :       /* Host associated PDTs can get confused with their constructors
    5131              :          because they are instantiated in the template's namespace.  */
    5132        35121 :       if (!dt_sym)
    5133              :         {
    5134         1534 :           if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
    5135              :             {
    5136            0 :               gfc_error ("Type name %qs at %C is ambiguous", name);
    5137            0 :               return MATCH_ERROR;
    5138              :             }
    5139         1534 :           if (dt_sym && !dt_sym->attr.pdt_type)
    5140            0 :             dt_sym = NULL;
    5141              :         }
    5142              :     }
    5143         1213 :   else if (ts->kind == -1)
    5144              :     {
    5145         2426 :       int iface = gfc_state_stack->previous->state != COMP_INTERFACE
    5146         1213 :                     || gfc_current_ns->has_import_set;
    5147         1213 :       gfc_find_symbol (name, NULL, iface, &sym);
    5148         1213 :       if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
    5149              :         {
    5150            0 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    5151            0 :           return MATCH_ERROR;
    5152              :         }
    5153         1213 :       if (sym && sym->generic && !dt_sym)
    5154            2 :         dt_sym = gfc_find_dt_in_generic (sym);
    5155              : 
    5156         1213 :       ts->kind = 0;
    5157         1213 :       if (sym == NULL)
    5158              :         return MATCH_NO;
    5159              :     }
    5160              : 
    5161        36317 :   if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
    5162        35108 :        && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
    5163        36315 :       || sym->attr.subroutine)
    5164              :     {
    5165            2 :       gfc_error ("Type name %qs at %C conflicts with previously declared "
    5166              :                  "entity at %L, which has the same name", name,
    5167              :                  &sym->declared_at);
    5168            2 :       return MATCH_ERROR;
    5169              :     }
    5170              : 
    5171        36315 :   if (dt_sym && decl_type_param_list
    5172         1620 :       && dt_sym->attr.flavor == FL_DERIVED
    5173         1620 :       && !dt_sym->attr.pdt_type
    5174          394 :       && !dt_sym->attr.pdt_template)
    5175              :     {
    5176            1 :       gfc_error ("Type %qs is not parameterized and so the type parameter spec "
    5177              :                  "list at %C may not appear", dt_sym->name);
    5178            1 :       return MATCH_ERROR;
    5179              :     }
    5180              : 
    5181        36314 :   if (sym && sym->attr.flavor == FL_DERIVED
    5182              :       && sym->attr.pdt_template
    5183              :       && gfc_current_state () != COMP_DERIVED)
    5184              :     {
    5185              :       m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
    5186              :       if (m != MATCH_YES)
    5187              :         return m;
    5188              :       gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
    5189              :       ts->u.derived = sym;
    5190              :       strcpy (name, gfc_dt_lower_string (sym->name));
    5191              :     }
    5192              : 
    5193        36314 :   gfc_save_symbol_data (sym);
    5194        36314 :   gfc_set_sym_referenced (sym);
    5195        36314 :   if (!sym->attr.generic
    5196        36314 :       && !gfc_add_generic (&sym->attr, sym->name, NULL))
    5197              :     return MATCH_ERROR;
    5198              : 
    5199        36314 :   if (!sym->attr.function
    5200        36314 :       && !gfc_add_function (&sym->attr, sym->name, NULL))
    5201              :     return MATCH_ERROR;
    5202              : 
    5203        36314 :   if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
    5204        36182 :       && dt_sym->attr.pdt_template
    5205          405 :       && gfc_current_state () != COMP_DERIVED)
    5206              :     {
    5207          230 :       m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
    5208          230 :       if (m != MATCH_YES)
    5209              :         return m;
    5210          230 :       gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
    5211              :     }
    5212              : 
    5213        36314 :   if (!dt_sym)
    5214              :     {
    5215          132 :       gfc_interface *intr, *head;
    5216              : 
    5217              :       /* Use upper case to save the actual derived-type symbol.  */
    5218          132 :       gfc_get_symbol (dt_name, NULL, &dt_sym);
    5219          132 :       dt_sym->name = gfc_get_string ("%s", sym->name);
    5220          132 :       head = sym->generic;
    5221          132 :       intr = gfc_get_interface ();
    5222          132 :       intr->sym = dt_sym;
    5223          132 :       intr->where = gfc_current_locus;
    5224          132 :       intr->next = head;
    5225          132 :       sym->generic = intr;
    5226          132 :       sym->attr.if_source = IFSRC_DECL;
    5227              :     }
    5228              :   else
    5229        36182 :     gfc_save_symbol_data (dt_sym);
    5230              : 
    5231        36314 :   gfc_set_sym_referenced (dt_sym);
    5232              : 
    5233          132 :   if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
    5234        36446 :       && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
    5235              :     return MATCH_ERROR;
    5236              : 
    5237        36314 :   ts->u.derived = dt_sym;
    5238              : 
    5239        36314 :   return MATCH_YES;
    5240              : 
    5241       165105 : get_kind:
    5242       165105 :   if (matched_type
    5243       165105 :       && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    5244              :                           "intrinsic-type-spec at %C"))
    5245              :     return MATCH_ERROR;
    5246              : 
    5247              :   /* For all types except double, derived and character, look for an
    5248              :      optional kind specifier.  MATCH_NO is actually OK at this point.  */
    5249       165102 :   if (implicit_flag == 1)
    5250              :     {
    5251          223 :         if (matched_type && gfc_match_char (')') != MATCH_YES)
    5252              :           return MATCH_ERROR;
    5253              : 
    5254              :         return MATCH_YES;
    5255              :     }
    5256              : 
    5257       164879 :   if (gfc_current_form == FORM_FREE)
    5258              :     {
    5259       149258 :       c = gfc_peek_ascii_char ();
    5260       149258 :       if (!gfc_is_whitespace (c) && c != '*' && c != '('
    5261        73566 :           && c != ':' && c != ',')
    5262              :         {
    5263          172 :           if (matched_type && c == ')')
    5264              :             {
    5265            3 :               gfc_next_ascii_char ();
    5266            3 :               return MATCH_YES;
    5267              :             }
    5268          169 :           gfc_error ("Malformed type-spec at %C");
    5269          169 :           return MATCH_NO;
    5270              :         }
    5271              :     }
    5272              : 
    5273       164707 :   m = gfc_match_kind_spec (ts, false);
    5274       164707 :   if (m == MATCH_ERROR)
    5275              :     return MATCH_ERROR;
    5276              : 
    5277       164671 :   if (m == MATCH_NO && ts->type != BT_CHARACTER)
    5278              :     {
    5279       111985 :       m = gfc_match_old_kind_spec (ts);
    5280       111985 :       if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
    5281              :          return MATCH_ERROR;
    5282              :     }
    5283              : 
    5284       164663 :   if (matched_type && gfc_match_char (')') != MATCH_YES)
    5285              :     {
    5286            0 :       gfc_error ("Malformed type-spec at %C");
    5287            0 :       return MATCH_ERROR;
    5288              :     }
    5289              : 
    5290              :   /* Defer association of the KIND expression of function results
    5291              :      until after USE and IMPORT statements.  */
    5292         4441 :   if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
    5293       169077 :          || gfc_matching_function)
    5294              :     return MATCH_YES;
    5295              : 
    5296       157149 :   if (m == MATCH_NO)
    5297       159133 :     m = MATCH_YES;              /* No kind specifier found.  */
    5298              : 
    5299              :   return m;
    5300              : }
    5301              : 
    5302              : 
    5303              : /* Match an IMPLICIT NONE statement.  Actually, this statement is
    5304              :    already matched in parse.cc, or we would not end up here in the
    5305              :    first place.  So the only thing we need to check, is if there is
    5306              :    trailing garbage.  If not, the match is successful.  */
    5307              : 
    5308              : match
    5309        25063 : gfc_match_implicit_none (void)
    5310              : {
    5311        25063 :   char c;
    5312        25063 :   match m;
    5313        25063 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    5314        25063 :   bool type = false;
    5315        25063 :   bool external = false;
    5316        25063 :   locus cur_loc = gfc_current_locus;
    5317              : 
    5318        25063 :   if (gfc_current_ns->seen_implicit_none
    5319        25061 :       || gfc_current_ns->has_implicit_none_export)
    5320              :     {
    5321            4 :       gfc_error ("Duplicate IMPLICIT NONE statement at %C");
    5322            4 :       return MATCH_ERROR;
    5323              :     }
    5324              : 
    5325        25059 :   gfc_gobble_whitespace ();
    5326        25059 :   c = gfc_peek_ascii_char ();
    5327        25059 :   if (c == '(')
    5328              :     {
    5329         1124 :       (void) gfc_next_ascii_char ();
    5330         1124 :       if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
    5331              :         return MATCH_ERROR;
    5332              : 
    5333         1123 :       gfc_gobble_whitespace ();
    5334         1123 :       if (gfc_peek_ascii_char () == ')')
    5335              :         {
    5336            1 :           (void) gfc_next_ascii_char ();
    5337            1 :           type = true;
    5338              :         }
    5339              :       else
    5340         3342 :         for(;;)
    5341              :           {
    5342         2232 :             m = gfc_match (" %n", name);
    5343         2232 :             if (m != MATCH_YES)
    5344              :               return MATCH_ERROR;
    5345              : 
    5346         2232 :             if (strcmp (name, "type") == 0)
    5347              :               type = true;
    5348         1122 :             else if (strcmp (name, "external") == 0)
    5349              :               external = true;
    5350              :             else
    5351              :               return MATCH_ERROR;
    5352              : 
    5353         2232 :             gfc_gobble_whitespace ();
    5354         2232 :             c = gfc_next_ascii_char ();
    5355         2232 :             if (c == ',')
    5356         1110 :               continue;
    5357         1122 :             if (c == ')')
    5358              :               break;
    5359              :             return MATCH_ERROR;
    5360              :           }
    5361              :     }
    5362              :   else
    5363              :     type = true;
    5364              : 
    5365        25058 :   if (gfc_match_eos () != MATCH_YES)
    5366              :     return MATCH_ERROR;
    5367              : 
    5368        25058 :   gfc_set_implicit_none (type, external, &cur_loc);
    5369              : 
    5370        25058 :   return MATCH_YES;
    5371              : }
    5372              : 
    5373              : 
    5374              : /* Match the letter range(s) of an IMPLICIT statement.  */
    5375              : 
    5376              : static match
    5377          600 : match_implicit_range (void)
    5378              : {
    5379          600 :   char c, c1, c2;
    5380          600 :   int inner;
    5381          600 :   locus cur_loc;
    5382              : 
    5383          600 :   cur_loc = gfc_current_locus;
    5384              : 
    5385          600 :   gfc_gobble_whitespace ();
    5386          600 :   c = gfc_next_ascii_char ();
    5387          600 :   if (c != '(')
    5388              :     {
    5389           59 :       gfc_error ("Missing character range in IMPLICIT at %C");
    5390           59 :       goto bad;
    5391              :     }
    5392              : 
    5393              :   inner = 1;
    5394         1195 :   while (inner)
    5395              :     {
    5396          722 :       gfc_gobble_whitespace ();
    5397          722 :       c1 = gfc_next_ascii_char ();
    5398          722 :       if (!ISALPHA (c1))
    5399           33 :         goto bad;
    5400              : 
    5401          689 :       gfc_gobble_whitespace ();
    5402          689 :       c = gfc_next_ascii_char ();
    5403              : 
    5404          689 :       switch (c)
    5405              :         {
    5406          201 :         case ')':
    5407          201 :           inner = 0;            /* Fall through.  */
    5408              : 
    5409              :         case ',':
    5410              :           c2 = c1;
    5411              :           break;
    5412              : 
    5413          439 :         case '-':
    5414          439 :           gfc_gobble_whitespace ();
    5415          439 :           c2 = gfc_next_ascii_char ();
    5416          439 :           if (!ISALPHA (c2))
    5417            0 :             goto bad;
    5418              : 
    5419          439 :           gfc_gobble_whitespace ();
    5420          439 :           c = gfc_next_ascii_char ();
    5421              : 
    5422          439 :           if ((c != ',') && (c != ')'))
    5423            0 :             goto bad;
    5424          439 :           if (c == ')')
    5425          272 :             inner = 0;
    5426              : 
    5427              :           break;
    5428              : 
    5429           35 :         default:
    5430           35 :           goto bad;
    5431              :         }
    5432              : 
    5433          654 :       if (c1 > c2)
    5434              :         {
    5435            0 :           gfc_error ("Letters must be in alphabetic order in "
    5436              :                      "IMPLICIT statement at %C");
    5437            0 :           goto bad;
    5438              :         }
    5439              : 
    5440              :       /* See if we can add the newly matched range to the pending
    5441              :          implicits from this IMPLICIT statement.  We do not check for
    5442              :          conflicts with whatever earlier IMPLICIT statements may have
    5443              :          set.  This is done when we've successfully finished matching
    5444              :          the current one.  */
    5445          654 :       if (!gfc_add_new_implicit_range (c1, c2))
    5446            0 :         goto bad;
    5447              :     }
    5448              : 
    5449              :   return MATCH_YES;
    5450              : 
    5451          127 : bad:
    5452          127 :   gfc_syntax_error (ST_IMPLICIT);
    5453              : 
    5454          127 :   gfc_current_locus = cur_loc;
    5455          127 :   return MATCH_ERROR;
    5456              : }
    5457              : 
    5458              : 
    5459              : /* Match an IMPLICIT statement, storing the types for
    5460              :    gfc_set_implicit() if the statement is accepted by the parser.
    5461              :    There is a strange looking, but legal syntactic construction
    5462              :    possible.  It looks like:
    5463              : 
    5464              :      IMPLICIT INTEGER (a-b) (c-d)
    5465              : 
    5466              :    This is legal if "a-b" is a constant expression that happens to
    5467              :    equal one of the legal kinds for integers.  The real problem
    5468              :    happens with an implicit specification that looks like:
    5469              : 
    5470              :      IMPLICIT INTEGER (a-b)
    5471              : 
    5472              :    In this case, a typespec matcher that is "greedy" (as most of the
    5473              :    matchers are) gobbles the character range as a kindspec, leaving
    5474              :    nothing left.  We therefore have to go a bit more slowly in the
    5475              :    matching process by inhibiting the kindspec checking during
    5476              :    typespec matching and checking for a kind later.  */
    5477              : 
    5478              : match
    5479        25489 : gfc_match_implicit (void)
    5480              : {
    5481        25489 :   gfc_typespec ts;
    5482        25489 :   locus cur_loc;
    5483        25489 :   char c;
    5484        25489 :   match m;
    5485              : 
    5486        25489 :   if (gfc_current_ns->seen_implicit_none)
    5487              :     {
    5488            4 :       gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
    5489              :                  "statement");
    5490            4 :       return MATCH_ERROR;
    5491              :     }
    5492              : 
    5493        25485 :   gfc_clear_ts (&ts);
    5494              : 
    5495              :   /* We don't allow empty implicit statements.  */
    5496        25485 :   if (gfc_match_eos () == MATCH_YES)
    5497              :     {
    5498            0 :       gfc_error ("Empty IMPLICIT statement at %C");
    5499            0 :       return MATCH_ERROR;
    5500              :     }
    5501              : 
    5502        25514 :   do
    5503              :     {
    5504              :       /* First cleanup.  */
    5505        25514 :       gfc_clear_new_implicit ();
    5506              : 
    5507              :       /* A basic type is mandatory here.  */
    5508        25514 :       m = gfc_match_decl_type_spec (&ts, 1);
    5509        25514 :       if (m == MATCH_ERROR)
    5510            0 :         goto error;
    5511        25514 :       if (m == MATCH_NO)
    5512        25061 :         goto syntax;
    5513              : 
    5514          453 :       cur_loc = gfc_current_locus;
    5515          453 :       m = match_implicit_range ();
    5516              : 
    5517          453 :       if (m == MATCH_YES)
    5518              :         {
    5519              :           /* We may have <TYPE> (<RANGE>).  */
    5520          326 :           gfc_gobble_whitespace ();
    5521          326 :           c = gfc_peek_ascii_char ();
    5522          326 :           if (c == ',' || c == '\n' || c == ';' || c == '!')
    5523              :             {
    5524              :               /* Check for CHARACTER with no length parameter.  */
    5525          299 :               if (ts.type == BT_CHARACTER && !ts.u.cl)
    5526              :                 {
    5527           32 :                   ts.kind = gfc_default_character_kind;
    5528           32 :                   ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5529           32 :                   ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    5530              :                                                       NULL, 1);
    5531              :                 }
    5532              : 
    5533              :               /* Record the Successful match.  */
    5534          299 :               if (!gfc_merge_new_implicit (&ts))
    5535              :                 return MATCH_ERROR;
    5536          297 :               if (c == ',')
    5537           28 :                 c = gfc_next_ascii_char ();
    5538          269 :               else if (gfc_match_eos () == MATCH_ERROR)
    5539            0 :                 goto error;
    5540          297 :               continue;
    5541              :             }
    5542              : 
    5543           27 :           gfc_current_locus = cur_loc;
    5544              :         }
    5545              : 
    5546              :       /* Discard the (incorrectly) matched range.  */
    5547          154 :       gfc_clear_new_implicit ();
    5548              : 
    5549              :       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
    5550          154 :       if (ts.type == BT_CHARACTER)
    5551           74 :         m = gfc_match_char_spec (&ts);
    5552           80 :       else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
    5553              :         {
    5554           76 :           m = gfc_match_kind_spec (&ts, false);
    5555           76 :           if (m == MATCH_NO)
    5556              :             {
    5557           40 :               m = gfc_match_old_kind_spec (&ts);
    5558           40 :               if (m == MATCH_ERROR)
    5559            0 :                 goto error;
    5560           40 :               if (m == MATCH_NO)
    5561            0 :                 goto syntax;
    5562              :             }
    5563              :         }
    5564          154 :       if (m == MATCH_ERROR)
    5565            7 :         goto error;
    5566              : 
    5567          147 :       m = match_implicit_range ();
    5568          147 :       if (m == MATCH_ERROR)
    5569            0 :         goto error;
    5570          147 :       if (m == MATCH_NO)
    5571              :         goto syntax;
    5572              : 
    5573          147 :       gfc_gobble_whitespace ();
    5574          147 :       c = gfc_next_ascii_char ();
    5575          147 :       if (c != ',' && gfc_match_eos () != MATCH_YES)
    5576            0 :         goto syntax;
    5577              : 
    5578          147 :       if (!gfc_merge_new_implicit (&ts))
    5579              :         return MATCH_ERROR;
    5580              :     }
    5581          444 :   while (c == ',');
    5582              : 
    5583              :   return MATCH_YES;
    5584              : 
    5585        25061 : syntax:
    5586        25061 :   gfc_syntax_error (ST_IMPLICIT);
    5587              : 
    5588        25489 : error:
    5589              :   return MATCH_ERROR;
    5590              : }
    5591              : 
    5592              : 
    5593              : /* Match the IMPORT statement.  IMPORT was added to F2003 as
    5594              : 
    5595              :    R1209 import-stmt  is IMPORT [[ :: ] import-name-list ]
    5596              : 
    5597              :    C1210 (R1209) The IMPORT statement is allowed only in an interface-body.
    5598              : 
    5599              :    C1211 (R1209) Each import-name shall be the name of an entity in the
    5600              :                  host scoping unit.
    5601              : 
    5602              :    under the description of an interface block. Under F2008, IMPORT was
    5603              :    split out of the interface block description to 12.4.3.3 and C1210
    5604              :    became
    5605              : 
    5606              :    C1210 (R1209) The IMPORT statement is allowed only in an interface-body
    5607              :                  that is not a module procedure interface body.
    5608              : 
    5609              :    Finally, F2018, section 8.8, has changed the IMPORT statement to
    5610              : 
    5611              :    R867 import-stmt  is IMPORT [[ :: ] import-name-list ]
    5612              :                      or IMPORT, ONLY : import-name-list
    5613              :                      or IMPORT, NONE
    5614              :                      or IMPORT, ALL
    5615              : 
    5616              :    C896 (R867) An IMPORT statement shall not appear in the scoping unit of
    5617              :                 a main-program, external-subprogram, module, or block-data.
    5618              : 
    5619              :    C897 (R867) Each import-name shall be the name of an entity in the host
    5620              :                 scoping unit.
    5621              : 
    5622              :    C898  If any IMPORT statement in a scoping unit has an ONLY specifier,
    5623              :          all IMPORT statements in that scoping unit shall have an ONLY
    5624              :          specifier.
    5625              : 
    5626              :    C899  IMPORT, NONE shall not appear in the scoping unit of a submodule.
    5627              : 
    5628              :    C8100 If an IMPORT, NONE or IMPORT, ALL statement appears in a scoping
    5629              :          unit, no other IMPORT statement shall appear in that scoping unit.
    5630              : 
    5631              :    C8101 Within an interface body, an entity that is accessed by host
    5632              :          association shall be accessible by host or use association within
    5633              :          the host scoping unit, or explicitly declared prior to the interface
    5634              :          body.
    5635              : 
    5636              :    C8102 An entity whose name appears as an import-name or which is made
    5637              :          accessible by an IMPORT, ALL statement shall not appear in any
    5638              :          context described in 19.5.1.4 that would cause the host entity
    5639              :          of that name to be inaccessible.  */
    5640              : 
    5641              : match
    5642         4101 : gfc_match_import (void)
    5643              : {
    5644         4101 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    5645         4101 :   match m;
    5646         4101 :   gfc_symbol *sym;
    5647         4101 :   gfc_symtree *st;
    5648         4101 :   bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;;
    5649         4101 :   importstate current_import_state = gfc_current_ns->import_state;
    5650              : 
    5651         4101 :   if (!f2018_allowed
    5652           13 :       && (gfc_current_ns->proc_name == NULL
    5653           12 :           || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY))
    5654              :     {
    5655            3 :       gfc_error ("IMPORT statement at %C only permitted in "
    5656              :                  "an INTERFACE body");
    5657            3 :       return MATCH_ERROR;
    5658              :     }
    5659              :   else if (f2018_allowed
    5660         4088 :            && (!gfc_current_ns->parent || gfc_current_ns->is_block_data))
    5661            4 :     goto C897;
    5662              : 
    5663         4084 :   if (f2018_allowed
    5664         4084 :       && (current_import_state == IMPORT_ALL
    5665         4084 :           || current_import_state == IMPORT_NONE))
    5666            2 :     goto C8100;
    5667              : 
    5668         4092 :   if (gfc_current_ns->proc_name
    5669         4091 :       && gfc_current_ns->proc_name->attr.module_procedure)
    5670              :     {
    5671            1 :       gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
    5672              :                  "in a module procedure interface body");
    5673            1 :       return MATCH_ERROR;
    5674              :     }
    5675              : 
    5676         4091 :   if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
    5677              :     return MATCH_ERROR;
    5678              : 
    5679         4087 :   gfc_current_ns->import_state = IMPORT_NOT_SET;
    5680         4087 :   if (f2018_allowed)
    5681              :     {
    5682         4081 :       if (gfc_match (" , none") == MATCH_YES)
    5683              :         {
    5684            8 :           if (current_import_state == IMPORT_ONLY)
    5685            0 :             goto C898;
    5686            8 :           if (gfc_current_state () == COMP_SUBMODULE)
    5687            0 :             goto C899;
    5688            8 :           gfc_current_ns->import_state = IMPORT_NONE;
    5689              :         }
    5690         4073 :       else if (gfc_match (" , only :") == MATCH_YES)
    5691              :         {
    5692           19 :           if (current_import_state != IMPORT_NOT_SET
    5693           19 :               && current_import_state != IMPORT_ONLY)
    5694            0 :             goto C898;
    5695           19 :           gfc_current_ns->import_state = IMPORT_ONLY;
    5696              :         }
    5697         4054 :       else if (gfc_match (" , all") == MATCH_YES)
    5698              :         {
    5699            1 :           if (current_import_state == IMPORT_ONLY)
    5700            0 :             goto C898;
    5701            1 :           gfc_current_ns->import_state = IMPORT_ALL;
    5702              :         }
    5703              : 
    5704         4081 :       if (current_import_state != IMPORT_NOT_SET
    5705            6 :           && (gfc_current_ns->import_state == IMPORT_NONE
    5706            6 :               || gfc_current_ns->import_state == IMPORT_ALL))
    5707            0 :         goto C8100;
    5708              :     }
    5709              : 
    5710              :   /* F2008 IMPORT<eos> is distinct from F2018 IMPORT, ALL.  */
    5711         4087 :   if (gfc_match_eos () == MATCH_YES)
    5712              :     {
    5713              :       /* This is the F2008 variant.  */
    5714          340 :       if (gfc_current_ns->import_state == IMPORT_NOT_SET)
    5715              :         {
    5716          331 :           if (current_import_state == IMPORT_ONLY)
    5717            0 :             goto C898;
    5718          331 :           gfc_current_ns->import_state = IMPORT_F2008;
    5719              :         }
    5720              : 
    5721              :       /* Host variables should be imported.  */
    5722          340 :       if (gfc_current_ns->import_state != IMPORT_NONE)
    5723          332 :         gfc_current_ns->has_import_set = 1;
    5724              :       return MATCH_YES;
    5725              :     }
    5726              : 
    5727         3747 :   if (gfc_match (" ::") == MATCH_YES
    5728         3747 :       && gfc_current_ns->import_state != IMPORT_ONLY)
    5729              :     {
    5730         1239 :       if (gfc_match_eos () == MATCH_YES)
    5731            1 :         goto expecting_list;
    5732         1238 :       gfc_current_ns->import_state = IMPORT_F2008;
    5733              :     }
    5734         2508 :   else if (gfc_current_ns->import_state == IMPORT_ONLY)
    5735              :     {
    5736           19 :       if (gfc_match_eos () == MATCH_YES)
    5737            0 :         goto expecting_list;
    5738              :     }
    5739              : 
    5740         4441 :   for(;;)
    5741              :     {
    5742         4441 :       sym = NULL;
    5743         4441 :       m = gfc_match (" %n", name);
    5744         4441 :       switch (m)
    5745              :         {
    5746         4441 :         case MATCH_YES:
    5747              :           /* Before checking if the symbol is available from host
    5748              :              association into a SUBROUTINE or FUNCTION within an
    5749              :              INTERFACE, check if it is already in local scope.  */
    5750         4441 :           gfc_find_symbol (name, gfc_current_ns, 1, &sym);
    5751         4441 :           if (sym
    5752           25 :               && gfc_state_stack->previous
    5753           25 :               && gfc_state_stack->previous->state == COMP_INTERFACE)
    5754              :             {
    5755            2 :                gfc_error ("import-name %qs at %C is in the "
    5756              :                           "local scope", name);
    5757            2 :                return MATCH_ERROR;
    5758              :             }
    5759              : 
    5760         4439 :           if (gfc_current_ns->parent != NULL
    5761         4439 :               && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
    5762              :             {
    5763            0 :                gfc_error ("Type name %qs at %C is ambiguous", name);
    5764            0 :                return MATCH_ERROR;
    5765              :             }
    5766         4439 :           else if (!sym
    5767            5 :                    && gfc_current_ns->proc_name
    5768            4 :                    && gfc_current_ns->proc_name->ns->parent
    5769         4440 :                    && gfc_find_symbol (name,
    5770              :                                        gfc_current_ns->proc_name->ns->parent,
    5771              :                                        1, &sym))
    5772              :             {
    5773            0 :                gfc_error ("Type name %qs at %C is ambiguous", name);
    5774            0 :                return MATCH_ERROR;
    5775              :             }
    5776              : 
    5777         4439 :           if (sym == NULL)
    5778              :             {
    5779            5 :               if (gfc_current_ns->proc_name
    5780            4 :                   && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
    5781              :                 {
    5782            1 :                   gfc_error ("Cannot IMPORT %qs from host scoping unit "
    5783              :                              "at %C - does not exist.", name);
    5784            1 :                   return MATCH_ERROR;
    5785              :                 }
    5786              :               else
    5787              :                 {
    5788              :                   /* This might be a procedure that has not yet been parsed. If
    5789              :                      so gfc_fixup_sibling_symbols will replace this symbol with
    5790              :                      that of the procedure.  */
    5791            4 :                   gfc_get_sym_tree (name, gfc_current_ns, &st, false,
    5792              :                                     &gfc_current_locus);
    5793            4 :                   st->n.sym->refs++;
    5794            4 :                   st->n.sym->attr.imported = 1;
    5795            4 :                   st->import_only = 1;
    5796            4 :                   goto next_item;
    5797              :                 }
    5798              :             }
    5799              : 
    5800         4434 :           st = gfc_find_symtree (gfc_current_ns->sym_root, name);
    5801         4434 :           if (st && st->n.sym && st->n.sym->attr.imported)
    5802              :             {
    5803            0 :               gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
    5804              :                            "at %C", name);
    5805            0 :               goto next_item;
    5806              :             }
    5807              : 
    5808         4434 :           st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
    5809         4434 :           st->n.sym = sym;
    5810         4434 :           sym->refs++;
    5811         4434 :           sym->attr.imported = 1;
    5812         4434 :           st->import_only = 1;
    5813              : 
    5814         4434 :           if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
    5815              :             {
    5816              :               /* The actual derived type is stored in a symtree with the first
    5817              :                  letter of the name capitalized; the symtree with the all
    5818              :                  lower-case name contains the associated generic function.  */
    5819          605 :               st = gfc_new_symtree (&gfc_current_ns->sym_root,
    5820              :                                     gfc_dt_upper_string (name));
    5821          605 :               st->n.sym = sym;
    5822          605 :               sym->refs++;
    5823          605 :               sym->attr.imported = 1;
    5824          605 :               st->import_only = 1;
    5825              :             }
    5826              : 
    5827         4434 :           goto next_item;
    5828              : 
    5829              :         case MATCH_NO:
    5830              :           break;
    5831              : 
    5832              :         case MATCH_ERROR:
    5833              :           return MATCH_ERROR;
    5834              :         }
    5835              : 
    5836         4438 :     next_item:
    5837         4438 :       if (gfc_match_eos () == MATCH_YES)
    5838              :         break;
    5839          695 :       if (gfc_match_char (',') != MATCH_YES)
    5840            0 :         goto syntax;
    5841              :     }
    5842              : 
    5843              :   return MATCH_YES;
    5844              : 
    5845            0 : syntax:
    5846            0 :   gfc_error ("Syntax error in IMPORT statement at %C");
    5847            0 :   return MATCH_ERROR;
    5848              : 
    5849            4 : C897:
    5850            4 :   gfc_error ("F2018: C897 IMPORT statement at %C cannot appear in a main "
    5851              :              "program, an external subprogram, a module or block data");
    5852            4 :   return MATCH_ERROR;
    5853              : 
    5854            0 : C898:
    5855            0 :   gfc_error ("F2018: C898 IMPORT statement at %C is not permitted because "
    5856              :              "a scoping unit has an ONLY specifier, can only have IMPORT "
    5857              :              "with an ONLY specifier");
    5858            0 :   return MATCH_ERROR;
    5859              : 
    5860            0 : C899:
    5861            0 :   gfc_error ("F2018: C899 IMPORT, NONE shall not appear in the scoping unit"
    5862              :              " of a submodule as at %C");
    5863            0 :   return MATCH_ERROR;
    5864              : 
    5865            2 : C8100:
    5866            4 :   gfc_error ("F2018: C8100 IMPORT statement at %C is not permitted because "
    5867              :              "%s has already been declared, which must be unique in the "
    5868              :              "scoping unit",
    5869            2 :              gfc_current_ns->import_state == IMPORT_ALL ? "IMPORT, ALL" :
    5870              :                                                           "IMPORT, NONE");
    5871            2 :   return MATCH_ERROR;
    5872              : 
    5873            1 : expecting_list:
    5874            1 :   gfc_error ("Expecting list of named entities at %C");
    5875            1 :   return MATCH_ERROR;
    5876              : }
    5877              : 
    5878              : 
    5879              : /* A minimal implementation of gfc_match without whitespace, escape
    5880              :    characters or variable arguments.  Returns true if the next
    5881              :    characters match the TARGET template exactly.  */
    5882              : 
    5883              : static bool
    5884       153870 : match_string_p (const char *target)
    5885              : {
    5886       153870 :   const char *p;
    5887              : 
    5888       961424 :   for (p = target; *p; p++)
    5889       807555 :     if ((char) gfc_next_ascii_char () != *p)
    5890              :       return false;
    5891              :   return true;
    5892              : }
    5893              : 
    5894              : /* Matches an attribute specification including array specs.  If
    5895              :    successful, leaves the variables current_attr and current_as
    5896              :    holding the specification.  Also sets the colon_seen variable for
    5897              :    later use by matchers associated with initializations.
    5898              : 
    5899              :    This subroutine is a little tricky in the sense that we don't know
    5900              :    if we really have an attr-spec until we hit the double colon.
    5901              :    Until that time, we can only return MATCH_NO.  This forces us to
    5902              :    check for duplicate specification at this level.  */
    5903              : 
    5904              : static match
    5905       226692 : match_attr_spec (void)
    5906              : {
    5907              :   /* Modifiers that can exist in a type statement.  */
    5908       226692 :   enum
    5909              :   { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
    5910              :     DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
    5911              :     DECL_DIMENSION, DECL_EXTERNAL,
    5912              :     DECL_INTRINSIC, DECL_OPTIONAL,
    5913              :     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
    5914              :     DECL_STATIC, DECL_AUTOMATIC,
    5915              :     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
    5916              :     DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
    5917              :     DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
    5918              :   };
    5919              : 
    5920              : /* GFC_DECL_END is the sentinel, index starts at 0.  */
    5921              : #define NUM_DECL GFC_DECL_END
    5922              : 
    5923              :   /* Make sure that values from sym_intent are safe to be used here.  */
    5924       226692 :   gcc_assert (INTENT_IN > 0);
    5925              : 
    5926       226692 :   locus start, seen_at[NUM_DECL];
    5927       226692 :   int seen[NUM_DECL];
    5928       226692 :   unsigned int d;
    5929       226692 :   const char *attr;
    5930       226692 :   match m;
    5931       226692 :   bool t;
    5932              : 
    5933       226692 :   gfc_clear_attr (&current_attr);
    5934       226692 :   start = gfc_current_locus;
    5935              : 
    5936       226692 :   current_as = NULL;
    5937       226692 :   colon_seen = 0;
    5938       226692 :   attr_seen = 0;
    5939              : 
    5940              :   /* See if we get all of the keywords up to the final double colon.  */
    5941      6120684 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    5942      5893992 :     seen[d] = 0;
    5943              : 
    5944       351156 :   for (;;)
    5945              :     {
    5946       351156 :       char ch;
    5947              : 
    5948       351156 :       d = DECL_NONE;
    5949       351156 :       gfc_gobble_whitespace ();
    5950              : 
    5951       351156 :       ch = gfc_next_ascii_char ();
    5952       351156 :       if (ch == ':')
    5953              :         {
    5954              :           /* This is the successful exit condition for the loop.  */
    5955       192611 :           if (gfc_next_ascii_char () == ':')
    5956              :             break;
    5957              :         }
    5958       158545 :       else if (ch == ',')
    5959              :         {
    5960       124476 :           gfc_gobble_whitespace ();
    5961       124476 :           switch (gfc_peek_ascii_char ())
    5962              :             {
    5963        19515 :             case 'a':
    5964        19515 :               gfc_next_ascii_char ();
    5965        19515 :               switch (gfc_next_ascii_char ())
    5966              :                 {
    5967        19444 :                 case 'l':
    5968        19444 :                   if (match_string_p ("locatable"))
    5969              :                     {
    5970              :                       /* Matched "allocatable".  */
    5971              :                       d = DECL_ALLOCATABLE;
    5972              :                     }
    5973              :                   break;
    5974              : 
    5975           30 :                 case 's':
    5976           30 :                   if (match_string_p ("ynchronous"))
    5977              :                     {
    5978              :                       /* Matched "asynchronous".  */
    5979              :                       d = DECL_ASYNCHRONOUS;
    5980              :                     }
    5981              :                   break;
    5982              : 
    5983           41 :                 case 'u':
    5984           41 :                   if (match_string_p ("tomatic"))
    5985              :                     {
    5986              :                       /* Matched "automatic".  */
    5987              :                       d = DECL_AUTOMATIC;
    5988              :                     }
    5989              :                   break;
    5990              :                 }
    5991              :               break;
    5992              : 
    5993          164 :             case 'b':
    5994              :               /* Try and match the bind(c).  */
    5995          164 :               m = gfc_match_bind_c (NULL, true);
    5996          164 :               if (m == MATCH_YES)
    5997              :                 d = DECL_IS_BIND_C;
    5998            0 :               else if (m == MATCH_ERROR)
    5999            0 :                 goto cleanup;
    6000              :               break;
    6001              : 
    6002         2194 :             case 'c':
    6003         2194 :               gfc_next_ascii_char ();
    6004         2194 :               if ('o' != gfc_next_ascii_char ())
    6005              :                 break;
    6006         2193 :               switch (gfc_next_ascii_char ())
    6007              :                 {
    6008           83 :                 case 'd':
    6009           83 :                   if (match_string_p ("imension"))
    6010              :                     {
    6011              :                       d = DECL_CODIMENSION;
    6012              :                       break;
    6013              :                     }
    6014              :                   /* FALLTHRU */
    6015         2110 :                 case 'n':
    6016         2110 :                   if (match_string_p ("tiguous"))
    6017              :                     {
    6018              :                       d = DECL_CONTIGUOUS;
    6019              :                       break;
    6020              :                     }
    6021              :                 }
    6022              :               break;
    6023              : 
    6024        19820 :             case 'd':
    6025        19820 :               if (match_string_p ("dimension"))
    6026              :                 d = DECL_DIMENSION;
    6027              :               break;
    6028              : 
    6029          177 :             case 'e':
    6030          177 :               if (match_string_p ("external"))
    6031              :                 d = DECL_EXTERNAL;
    6032              :               break;
    6033              : 
    6034        29559 :             case 'i':
    6035        29559 :               if (match_string_p ("int"))
    6036              :                 {
    6037        29559 :                   ch = gfc_next_ascii_char ();
    6038        29559 :                   if (ch == 'e')
    6039              :                     {
    6040        29553 :                       if (match_string_p ("nt"))
    6041              :                         {
    6042              :                           /* Matched "intent".  */
    6043        29552 :                           d = match_intent_spec ();
    6044        29552 :                           if (d == INTENT_UNKNOWN)
    6045              :                             {
    6046            2 :                               m = MATCH_ERROR;
    6047            2 :                               goto cleanup;
    6048              :                             }
    6049              :                         }
    6050              :                     }
    6051            6 :                   else if (ch == 'r')
    6052              :                     {
    6053            6 :                       if (match_string_p ("insic"))
    6054              :                         {
    6055              :                           /* Matched "intrinsic".  */
    6056              :                           d = DECL_INTRINSIC;
    6057              :                         }
    6058              :                     }
    6059              :                 }
    6060              :               break;
    6061              : 
    6062          462 :             case 'k':
    6063          462 :               if (match_string_p ("kind"))
    6064              :                 d = DECL_KIND;
    6065              :               break;
    6066              : 
    6067          524 :             case 'l':
    6068          524 :               if (match_string_p ("len"))
    6069              :                 d = DECL_LEN;
    6070              :               break;
    6071              : 
    6072         5310 :             case 'o':
    6073         5310 :               if (match_string_p ("optional"))
    6074              :                 d = DECL_OPTIONAL;
    6075              :               break;
    6076              : 
    6077        27921 :             case 'p':
    6078        27921 :               gfc_next_ascii_char ();
    6079        27921 :               switch (gfc_next_ascii_char ())
    6080              :                 {
    6081        14707 :                 case 'a':
    6082        14707 :                   if (match_string_p ("rameter"))
    6083              :                     {
    6084              :                       /* Matched "parameter".  */
    6085              :                       d = DECL_PARAMETER;
    6086              :                     }
    6087              :                   break;
    6088              : 
    6089        12692 :                 case 'o':
    6090        12692 :                   if (match_string_p ("inter"))
    6091              :                     {
    6092              :                       /* Matched "pointer".  */
    6093              :                       d = DECL_POINTER;
    6094              :                     }
    6095              :                   break;
    6096              : 
    6097          269 :                 case 'r':
    6098          269 :                   ch = gfc_next_ascii_char ();
    6099          269 :                   if (ch == 'i')
    6100              :                     {
    6101          218 :                       if (match_string_p ("vate"))
    6102              :                         {
    6103              :                           /* Matched "private".  */
    6104              :                           d = DECL_PRIVATE;
    6105              :                         }
    6106              :                     }
    6107           51 :                   else if (ch == 'o')
    6108              :                     {
    6109           51 :                       if (match_string_p ("tected"))
    6110              :                         {
    6111              :                           /* Matched "protected".  */
    6112              :                           d = DECL_PROTECTED;
    6113              :                         }
    6114              :                     }
    6115              :                   break;
    6116              : 
    6117          253 :                 case 'u':
    6118          253 :                   if (match_string_p ("blic"))
    6119              :                     {
    6120              :                       /* Matched "public".  */
    6121              :                       d = DECL_PUBLIC;
    6122              :                     }
    6123              :                   break;
    6124              :                 }
    6125              :               break;
    6126              : 
    6127         1229 :             case 's':
    6128         1229 :               gfc_next_ascii_char ();
    6129         1229 :               switch (gfc_next_ascii_char ())
    6130              :                 {
    6131         1216 :                   case 'a':
    6132         1216 :                     if (match_string_p ("ve"))
    6133              :                       {
    6134              :                         /* Matched "save".  */
    6135              :                         d = DECL_SAVE;
    6136              :                       }
    6137              :                     break;
    6138              : 
    6139           13 :                   case 't':
    6140           13 :                     if (match_string_p ("atic"))
    6141              :                       {
    6142              :                         /* Matched "static".  */
    6143              :                         d = DECL_STATIC;
    6144              :                       }
    6145              :                     break;
    6146              :                 }
    6147              :               break;
    6148              : 
    6149         5860 :             case 't':
    6150         5860 :               if (match_string_p ("target"))
    6151              :                 d = DECL_TARGET;
    6152              :               break;
    6153              : 
    6154        11741 :             case 'v':
    6155        11741 :               gfc_next_ascii_char ();
    6156        11741 :               ch = gfc_next_ascii_char ();
    6157        11741 :               if (ch == 'a')
    6158              :                 {
    6159        11220 :                   if (match_string_p ("lue"))
    6160              :                     {
    6161              :                       /* Matched "value".  */
    6162              :                       d = DECL_VALUE;
    6163              :                     }
    6164              :                 }
    6165          521 :               else if (ch == 'o')
    6166              :                 {
    6167          521 :                   if (match_string_p ("latile"))
    6168              :                     {
    6169              :                       /* Matched "volatile".  */
    6170              :                       d = DECL_VOLATILE;
    6171              :                     }
    6172              :                 }
    6173              :               break;
    6174              :             }
    6175              :         }
    6176              : 
    6177              :       /* No double colon and no recognizable decl_type, so assume that
    6178              :          we've been looking at something else the whole time.  */
    6179              :       if (d == DECL_NONE)
    6180              :         {
    6181        34072 :           m = MATCH_NO;
    6182        34072 :           goto cleanup;
    6183              :         }
    6184              : 
    6185              :       /* Check to make sure any parens are paired up correctly.  */
    6186       124472 :       if (gfc_match_parens () == MATCH_ERROR)
    6187              :         {
    6188            1 :           m = MATCH_ERROR;
    6189            1 :           goto cleanup;
    6190              :         }
    6191              : 
    6192       124471 :       seen[d]++;
    6193       124471 :       seen_at[d] = gfc_current_locus;
    6194              : 
    6195       124471 :       if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
    6196              :         {
    6197        19902 :           gfc_array_spec *as = NULL;
    6198              : 
    6199        19902 :           m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
    6200              :                                     d == DECL_CODIMENSION);
    6201              : 
    6202        19902 :           if (current_as == NULL)
    6203        19877 :             current_as = as;
    6204           25 :           else if (m == MATCH_YES)
    6205              :             {
    6206           25 :               if (!merge_array_spec (as, current_as, false))
    6207            2 :                 m = MATCH_ERROR;
    6208           25 :               free (as);
    6209              :             }
    6210              : 
    6211        19902 :           if (m == MATCH_NO)
    6212              :             {
    6213            0 :               if (d == DECL_CODIMENSION)
    6214            0 :                 gfc_error ("Missing codimension specification at %C");
    6215              :               else
    6216            0 :                 gfc_error ("Missing dimension specification at %C");
    6217              :               m = MATCH_ERROR;
    6218              :             }
    6219              : 
    6220        19902 :           if (m == MATCH_ERROR)
    6221            7 :             goto cleanup;
    6222              :         }
    6223              :     }
    6224              : 
    6225              :   /* Since we've seen a double colon, we have to be looking at an
    6226              :      attr-spec.  This means that we can now issue errors.  */
    6227      5200449 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    6228      5007841 :     if (seen[d] > 1)
    6229              :       {
    6230            2 :         switch (d)
    6231              :           {
    6232              :           case DECL_ALLOCATABLE:
    6233              :             attr = "ALLOCATABLE";
    6234              :             break;
    6235            0 :           case DECL_ASYNCHRONOUS:
    6236            0 :             attr = "ASYNCHRONOUS";
    6237            0 :             break;
    6238            0 :           case DECL_CODIMENSION:
    6239            0 :             attr = "CODIMENSION";
    6240            0 :             break;
    6241            0 :           case DECL_CONTIGUOUS:
    6242            0 :             attr = "CONTIGUOUS";
    6243            0 :             break;
    6244            0 :           case DECL_DIMENSION:
    6245            0 :             attr = "DIMENSION";
    6246            0 :             break;
    6247            0 :           case DECL_EXTERNAL:
    6248            0 :             attr = "EXTERNAL";
    6249            0 :             break;
    6250            0 :           case DECL_IN:
    6251            0 :             attr = "INTENT (IN)";
    6252            0 :             break;
    6253            0 :           case DECL_OUT:
    6254            0 :             attr = "INTENT (OUT)";
    6255            0 :             break;
    6256            0 :           case DECL_INOUT:
    6257            0 :             attr = "INTENT (IN OUT)";
    6258            0 :             break;
    6259            0 :           case DECL_INTRINSIC:
    6260            0 :             attr = "INTRINSIC";
    6261            0 :             break;
    6262            0 :           case DECL_OPTIONAL:
    6263            0 :             attr = "OPTIONAL";
    6264            0 :             break;
    6265            0 :           case DECL_KIND:
    6266            0 :             attr = "KIND";
    6267            0 :             break;
    6268            0 :           case DECL_LEN:
    6269            0 :             attr = "LEN";
    6270            0 :             break;
    6271            0 :           case DECL_PARAMETER:
    6272            0 :             attr = "PARAMETER";
    6273            0 :             break;
    6274            0 :           case DECL_POINTER:
    6275            0 :             attr = "POINTER";
    6276            0 :             break;
    6277            0 :           case DECL_PROTECTED:
    6278            0 :             attr = "PROTECTED";
    6279            0 :             break;
    6280            0 :           case DECL_PRIVATE:
    6281            0 :             attr = "PRIVATE";
    6282            0 :             break;
    6283            0 :           case DECL_PUBLIC:
    6284            0 :             attr = "PUBLIC";
    6285            0 :             break;
    6286            0 :           case DECL_SAVE:
    6287            0 :             attr = "SAVE";
    6288            0 :             break;
    6289            0 :           case DECL_STATIC:
    6290            0 :             attr = "STATIC";
    6291            0 :             break;
    6292            1 :           case DECL_AUTOMATIC:
    6293            1 :             attr = "AUTOMATIC";
    6294            1 :             break;
    6295            0 :           case DECL_TARGET:
    6296            0 :             attr = "TARGET";
    6297            0 :             break;
    6298            0 :           case DECL_IS_BIND_C:
    6299            0 :             attr = "IS_BIND_C";
    6300            0 :             break;
    6301            0 :           case DECL_VALUE:
    6302            0 :             attr = "VALUE";
    6303            0 :             break;
    6304            1 :           case DECL_VOLATILE:
    6305            1 :             attr = "VOLATILE";
    6306            1 :             break;
    6307            0 :           default:
    6308            0 :             attr = NULL;        /* This shouldn't happen.  */
    6309              :           }
    6310              : 
    6311            2 :         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
    6312            2 :         m = MATCH_ERROR;
    6313            2 :         goto cleanup;
    6314              :       }
    6315              : 
    6316              :   /* Now that we've dealt with duplicate attributes, add the attributes
    6317              :      to the current attribute.  */
    6318      5199629 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    6319              :     {
    6320      5007094 :       if (seen[d] == 0)
    6321      4882639 :         continue;
    6322              :       else
    6323       124455 :         attr_seen = 1;
    6324              : 
    6325       124455 :       if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
    6326           52 :           && !flag_dec_static)
    6327              :         {
    6328            3 :           gfc_error ("%s at %L is a DEC extension, enable with "
    6329              :                      "%<-fdec-static%>",
    6330              :                      d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
    6331            2 :           m = MATCH_ERROR;
    6332            2 :           goto cleanup;
    6333              :         }
    6334              :       /* Allow SAVE with STATIC, but don't complain.  */
    6335           50 :       if (d == DECL_STATIC && seen[DECL_SAVE])
    6336            0 :         continue;
    6337              : 
    6338       124453 :       if (gfc_comp_struct (gfc_current_state ())
    6339         7614 :           && d != DECL_DIMENSION && d != DECL_CODIMENSION
    6340         6650 :           && d != DECL_POINTER   && d != DECL_PRIVATE
    6341         4911 :           && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
    6342              :         {
    6343         4854 :           bool is_derived = gfc_current_state () == COMP_DERIVED;
    6344         4854 :           if (d == DECL_ALLOCATABLE)
    6345              :             {
    6346         3855 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6347              :                                    ? G_("ALLOCATABLE attribute at %C in a "
    6348              :                                         "TYPE definition")
    6349              :                                    : G_("ALLOCATABLE attribute at %C in a "
    6350              :                                         "STRUCTURE definition")))
    6351              :                 {
    6352            2 :                   m = MATCH_ERROR;
    6353            2 :                   goto cleanup;
    6354              :                 }
    6355              :             }
    6356          999 :           else if (d == DECL_KIND)
    6357              :             {
    6358          460 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6359              :                                    ? G_("KIND attribute at %C in a "
    6360              :                                         "TYPE definition")
    6361              :                                    : G_("KIND attribute at %C in a "
    6362              :                                         "STRUCTURE definition")))
    6363              :                 {
    6364            1 :                   m = MATCH_ERROR;
    6365            1 :                   goto cleanup;
    6366              :                 }
    6367          459 :               if (current_ts.type != BT_INTEGER)
    6368              :                 {
    6369            2 :                   gfc_error ("Component with KIND attribute at %C must be "
    6370              :                              "INTEGER");
    6371            2 :                   m = MATCH_ERROR;
    6372            2 :                   goto cleanup;
    6373              :                 }
    6374              :             }
    6375          539 :           else if (d == DECL_LEN)
    6376              :             {
    6377          523 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6378              :                                    ? G_("LEN attribute at %C in a "
    6379              :                                         "TYPE definition")
    6380              :                                    : G_("LEN attribute at %C in a "
    6381              :                                         "STRUCTURE definition")))
    6382              :                 {
    6383            0 :                   m = MATCH_ERROR;
    6384            0 :                   goto cleanup;
    6385              :                 }
    6386          523 :               if (current_ts.type != BT_INTEGER)
    6387              :                 {
    6388            1 :                   gfc_error ("Component with LEN attribute at %C must be "
    6389              :                              "INTEGER");
    6390            1 :                   m = MATCH_ERROR;
    6391            1 :                   goto cleanup;
    6392              :                 }
    6393              :             }
    6394              :           else
    6395              :             {
    6396           32 :               gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
    6397              :                                          "TYPE definition")
    6398              :                                     : G_("Attribute at %L is not allowed in a "
    6399              :                                          "STRUCTURE definition"), &seen_at[d]);
    6400           16 :               m = MATCH_ERROR;
    6401           16 :               goto cleanup;
    6402              :             }
    6403              :         }
    6404              : 
    6405       124431 :       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
    6406          471 :           && gfc_current_state () != COMP_MODULE)
    6407              :         {
    6408          148 :           if (d == DECL_PRIVATE)
    6409              :             attr = "PRIVATE";
    6410              :           else
    6411           43 :             attr = "PUBLIC";
    6412          148 :           if (gfc_current_state () == COMP_DERIVED
    6413          142 :               && gfc_state_stack->previous
    6414          142 :               && gfc_state_stack->previous->state == COMP_MODULE)
    6415              :             {
    6416          139 :               if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
    6417              :                                    "at %L in a TYPE definition", attr,
    6418              :                                    &seen_at[d]))
    6419              :                 {
    6420            2 :                   m = MATCH_ERROR;
    6421            2 :                   goto cleanup;
    6422              :                 }
    6423              :             }
    6424              :           else
    6425              :             {
    6426            9 :               gfc_error ("%s attribute at %L is not allowed outside of the "
    6427              :                          "specification part of a module", attr, &seen_at[d]);
    6428            9 :               m = MATCH_ERROR;
    6429            9 :               goto cleanup;
    6430              :             }
    6431              :         }
    6432              : 
    6433       124420 :       if (gfc_current_state () != COMP_DERIVED
    6434       116837 :           && (d == DECL_KIND || d == DECL_LEN))
    6435              :         {
    6436            3 :           gfc_error ("Attribute at %L is not allowed outside a TYPE "
    6437              :                      "definition", &seen_at[d]);
    6438            3 :           m = MATCH_ERROR;
    6439            3 :           goto cleanup;
    6440              :         }
    6441              : 
    6442       124417 :       switch (d)
    6443              :         {
    6444        19442 :         case DECL_ALLOCATABLE:
    6445        19442 :           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
    6446        19442 :           break;
    6447              : 
    6448           29 :         case DECL_ASYNCHRONOUS:
    6449           29 :           if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
    6450              :             t = false;
    6451              :           else
    6452           29 :             t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
    6453              :           break;
    6454              : 
    6455           81 :         case DECL_CODIMENSION:
    6456           81 :           t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
    6457           81 :           break;
    6458              : 
    6459         2110 :         case DECL_CONTIGUOUS:
    6460         2110 :           if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
    6461              :             t = false;
    6462              :           else
    6463         2109 :             t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
    6464              :           break;
    6465              : 
    6466        19812 :         case DECL_DIMENSION:
    6467        19812 :           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
    6468        19812 :           break;
    6469              : 
    6470          176 :         case DECL_EXTERNAL:
    6471          176 :           t = gfc_add_external (&current_attr, &seen_at[d]);
    6472          176 :           break;
    6473              : 
    6474        22398 :         case DECL_IN:
    6475        22398 :           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
    6476        22398 :           break;
    6477              : 
    6478         3874 :         case DECL_OUT:
    6479         3874 :           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
    6480         3874 :           break;
    6481              : 
    6482         3274 :         case DECL_INOUT:
    6483         3274 :           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
    6484         3274 :           break;
    6485              : 
    6486            5 :         case DECL_INTRINSIC:
    6487            5 :           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
    6488            5 :           break;
    6489              : 
    6490         5309 :         case DECL_OPTIONAL:
    6491         5309 :           t = gfc_add_optional (&current_attr, &seen_at[d]);
    6492         5309 :           break;
    6493              : 
    6494          457 :         case DECL_KIND:
    6495          457 :           t = gfc_add_kind (&current_attr, &seen_at[d]);
    6496          457 :           break;
    6497              : 
    6498          522 :         case DECL_LEN:
    6499          522 :           t = gfc_add_len (&current_attr, &seen_at[d]);
    6500          522 :           break;
    6501              : 
    6502        14706 :         case DECL_PARAMETER:
    6503        14706 :           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
    6504        14706 :           break;
    6505              : 
    6506        12691 :         case DECL_POINTER:
    6507        12691 :           t = gfc_add_pointer (&current_attr, &seen_at[d]);
    6508        12691 :           break;
    6509              : 
    6510           50 :         case DECL_PROTECTED:
    6511           50 :           if (gfc_current_state () != COMP_MODULE
    6512           48 :               || (gfc_current_ns->proc_name
    6513           48 :                   && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
    6514              :             {
    6515            2 :                gfc_error ("PROTECTED at %C only allowed in specification "
    6516              :                           "part of a module");
    6517            2 :                t = false;
    6518            2 :                break;
    6519              :             }
    6520              : 
    6521           48 :           if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
    6522              :             t = false;
    6523              :           else
    6524           44 :             t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
    6525              :           break;
    6526              : 
    6527          215 :         case DECL_PRIVATE:
    6528          215 :           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
    6529              :                               &seen_at[d]);
    6530          215 :           break;
    6531              : 
    6532          245 :         case DECL_PUBLIC:
    6533          245 :           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
    6534              :                               &seen_at[d]);
    6535          245 :           break;
    6536              : 
    6537         1226 :         case DECL_STATIC:
    6538         1226 :         case DECL_SAVE:
    6539         1226 :           t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
    6540         1226 :           break;
    6541              : 
    6542           37 :         case DECL_AUTOMATIC:
    6543           37 :           t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
    6544           37 :           break;
    6545              : 
    6546         5858 :         case DECL_TARGET:
    6547         5858 :           t = gfc_add_target (&current_attr, &seen_at[d]);
    6548         5858 :           break;
    6549              : 
    6550          163 :         case DECL_IS_BIND_C:
    6551          163 :            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
    6552          163 :            break;
    6553              : 
    6554        11219 :         case DECL_VALUE:
    6555        11219 :           if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
    6556              :             t = false;
    6557              :           else
    6558        11219 :             t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
    6559              :           break;
    6560              : 
    6561          518 :         case DECL_VOLATILE:
    6562          518 :           if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
    6563              :             t = false;
    6564              :           else
    6565          517 :             t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
    6566              :           break;
    6567              : 
    6568            0 :         default:
    6569            0 :           gfc_internal_error ("match_attr_spec(): Bad attribute");
    6570              :         }
    6571              : 
    6572       124411 :       if (!t)
    6573              :         {
    6574           35 :           m = MATCH_ERROR;
    6575           35 :           goto cleanup;
    6576              :         }
    6577              :     }
    6578              : 
    6579              :   /* Since Fortran 2008 module variables implicitly have the SAVE attribute.  */
    6580       192535 :   if ((gfc_current_state () == COMP_MODULE
    6581       192535 :        || gfc_current_state () == COMP_SUBMODULE)
    6582         6196 :       && !current_attr.save
    6583         6014 :       && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    6584         5922 :     current_attr.save = SAVE_IMPLICIT;
    6585              : 
    6586       192535 :   colon_seen = 1;
    6587       192535 :   return MATCH_YES;
    6588              : 
    6589        34157 : cleanup:
    6590        34157 :   gfc_current_locus = start;
    6591        34157 :   gfc_free_array_spec (current_as);
    6592        34157 :   current_as = NULL;
    6593        34157 :   attr_seen = 0;
    6594        34157 :   return m;
    6595              : }
    6596              : 
    6597              : 
    6598              : /* Set the binding label, dest_label, either with the binding label
    6599              :    stored in the given gfc_typespec, ts, or if none was provided, it
    6600              :    will be the symbol name in all lower case, as required by the draft
    6601              :    (J3/04-007, section 15.4.1).  If a binding label was given and
    6602              :    there is more than one argument (num_idents), it is an error.  */
    6603              : 
    6604              : static bool
    6605          347 : set_binding_label (const char **dest_label, const char *sym_name,
    6606              :                    int num_idents)
    6607              : {
    6608          347 :   if (num_idents > 1 && has_name_equals)
    6609              :     {
    6610            4 :       gfc_error ("Multiple identifiers provided with "
    6611              :                  "single NAME= specifier at %C");
    6612            4 :       return false;
    6613              :     }
    6614              : 
    6615          343 :   if (curr_binding_label)
    6616              :     /* Binding label given; store in temp holder till have sym.  */
    6617          108 :     *dest_label = curr_binding_label;
    6618              :   else
    6619              :     {
    6620              :       /* No binding label given, and the NAME= specifier did not exist,
    6621              :          which means there was no NAME="".  */
    6622          235 :       if (sym_name != NULL && has_name_equals == 0)
    6623          205 :         *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
    6624              :     }
    6625              : 
    6626              :   return true;
    6627              : }
    6628              : 
    6629              : 
    6630              : /* Set the status of the given common block as being BIND(C) or not,
    6631              :    depending on the given parameter, is_bind_c.  */
    6632              : 
    6633              : static void
    6634           76 : set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
    6635              : {
    6636           76 :   com_block->is_bind_c = is_bind_c;
    6637           76 :   return;
    6638              : }
    6639              : 
    6640              : 
    6641              : /* Verify that the given gfc_typespec is for a C interoperable type.  */
    6642              : 
    6643              : bool
    6644        21895 : gfc_verify_c_interop (gfc_typespec *ts)
    6645              : {
    6646        21895 :   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
    6647         4324 :     return ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c;
    6648        17571 :   else if (ts->type == BT_CLASS)
    6649              :     return false;
    6650        17563 :   else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
    6651         3984 :     return false;
    6652              : 
    6653              :   return true;
    6654              : }
    6655              : 
    6656              : 
    6657              : /* Verify that the variables of a given common block, which has been
    6658              :    defined with the attribute specifier bind(c), to be of a C
    6659              :    interoperable type.  Errors will be reported here, if
    6660              :    encountered.  */
    6661              : 
    6662              : bool
    6663            1 : verify_com_block_vars_c_interop (gfc_common_head *com_block)
    6664              : {
    6665            1 :   gfc_symbol *curr_sym = NULL;
    6666            1 :   bool retval = true;
    6667              : 
    6668            1 :   curr_sym = com_block->head;
    6669              : 
    6670              :   /* Make sure we have at least one symbol.  */
    6671            1 :   if (curr_sym == NULL)
    6672              :     return retval;
    6673              : 
    6674              :   /* Here we know we have a symbol, so we'll execute this loop
    6675              :      at least once.  */
    6676            1 :   do
    6677              :     {
    6678              :       /* The second to last param, 1, says this is in a common block.  */
    6679            1 :       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
    6680            1 :       curr_sym = curr_sym->common_next;
    6681            1 :     } while (curr_sym != NULL);
    6682              : 
    6683              :   return retval;
    6684              : }
    6685              : 
    6686              : 
    6687              : /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
    6688              :    an appropriate error message is reported.  */
    6689              : 
    6690              : bool
    6691         7637 : verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
    6692              :                    int is_in_common, gfc_common_head *com_block)
    6693              : {
    6694         7637 :   bool bind_c_function = false;
    6695         7637 :   bool retval = true;
    6696              : 
    6697         7637 :   if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
    6698         7637 :     bind_c_function = true;
    6699              : 
    6700         7637 :   if (tmp_sym->attr.function && tmp_sym->result != NULL)
    6701              :     {
    6702         3272 :       tmp_sym = tmp_sym->result;
    6703              :       /* Make sure it wasn't an implicitly typed result.  */
    6704         3272 :       if (tmp_sym->attr.implicit_type && warn_c_binding_type)
    6705              :         {
    6706            1 :           gfc_warning (OPT_Wc_binding_type,
    6707              :                        "Implicitly declared BIND(C) function %qs at "
    6708              :                        "%L may not be C interoperable", tmp_sym->name,
    6709              :                        &tmp_sym->declared_at);
    6710            1 :           tmp_sym->ts.f90_type = tmp_sym->ts.type;
    6711              :           /* Mark it as C interoperable to prevent duplicate warnings.  */
    6712            1 :           tmp_sym->ts.is_c_interop = 1;
    6713            1 :           tmp_sym->attr.is_c_interop = 1;
    6714              :         }
    6715              :     }
    6716              : 
    6717              :   /* Here, we know we have the bind(c) attribute, so if we have
    6718              :      enough type info, then verify that it's a C interop kind.
    6719              :      The info could be in the symbol already, or possibly still in
    6720              :      the given ts (current_ts), so look in both.  */
    6721         7637 :   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
    6722              :     {
    6723         3431 :       if (!gfc_verify_c_interop (&(tmp_sym->ts)))
    6724              :         {
    6725              :           /* See if we're dealing with a sym in a common block or not.  */
    6726          237 :           if (is_in_common == 1 && warn_c_binding_type)
    6727              :             {
    6728            0 :               gfc_warning (OPT_Wc_binding_type,
    6729              :                            "Variable %qs in common block %qs at %L "
    6730              :                            "may not be a C interoperable "
    6731              :                            "kind though common block %qs is BIND(C)",
    6732              :                            tmp_sym->name, com_block->name,
    6733            0 :                            &(tmp_sym->declared_at), com_block->name);
    6734              :             }
    6735              :           else
    6736              :             {
    6737          237 :               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
    6738          235 :                   || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
    6739              :                 {
    6740            3 :                   gfc_error ("Type declaration %qs at %L is not C "
    6741              :                              "interoperable but it is BIND(C)",
    6742              :                              tmp_sym->name, &(tmp_sym->declared_at));
    6743            3 :                   retval = false;
    6744              :                 }
    6745          234 :               else if (warn_c_binding_type)
    6746            3 :                 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
    6747              :                              "may not be a C interoperable "
    6748              :                              "kind but it is BIND(C)",
    6749              :                              tmp_sym->name, &(tmp_sym->declared_at));
    6750              :             }
    6751              :         }
    6752              : 
    6753              :       /* Variables declared w/in a common block can't be bind(c)
    6754              :          since there's no way for C to see these variables, so there's
    6755              :          semantically no reason for the attribute.  */
    6756         3431 :       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
    6757              :         {
    6758            1 :           gfc_error ("Variable %qs in common block %qs at "
    6759              :                      "%L cannot be declared with BIND(C) "
    6760              :                      "since it is not a global",
    6761            1 :                      tmp_sym->name, com_block->name,
    6762              :                      &(tmp_sym->declared_at));
    6763            1 :           retval = false;
    6764              :         }
    6765              : 
    6766              :       /* Scalar variables that are bind(c) cannot have the pointer
    6767              :          or allocatable attributes.  */
    6768         3431 :       if (tmp_sym->attr.is_bind_c == 1)
    6769              :         {
    6770         2885 :           if (tmp_sym->attr.pointer == 1)
    6771              :             {
    6772            1 :               gfc_error ("Variable %qs at %L cannot have both the "
    6773              :                          "POINTER and BIND(C) attributes",
    6774              :                          tmp_sym->name, &(tmp_sym->declared_at));
    6775            1 :               retval = false;
    6776              :             }
    6777              : 
    6778         2885 :           if (tmp_sym->attr.allocatable == 1)
    6779              :             {
    6780            0 :               gfc_error ("Variable %qs at %L cannot have both the "
    6781              :                          "ALLOCATABLE and BIND(C) attributes",
    6782              :                          tmp_sym->name, &(tmp_sym->declared_at));
    6783            0 :               retval = false;
    6784              :             }
    6785              : 
    6786              :         }
    6787              : 
    6788              :       /* If it is a BIND(C) function, make sure the return value is a
    6789              :          scalar value.  The previous tests in this function made sure
    6790              :          the type is interoperable.  */
    6791         3431 :       if (bind_c_function && tmp_sym->as != NULL)
    6792            2 :         gfc_error ("Return type of BIND(C) function %qs at %L cannot "
    6793              :                    "be an array", tmp_sym->name, &(tmp_sym->declared_at));
    6794              : 
    6795              :       /* BIND(C) functions cannot return a character string.  */
    6796         3272 :       if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
    6797          116 :         if (!gfc_length_one_character_type_p (&tmp_sym->ts))
    6798            4 :           gfc_error ("Return type of BIND(C) function %qs of character "
    6799              :                      "type at %L must have length 1", tmp_sym->name,
    6800              :                          &(tmp_sym->declared_at));
    6801              :     }
    6802              : 
    6803              :   /* See if the symbol has been marked as private.  If it has, warn if
    6804              :      there is a binding label with default binding name.  */
    6805         7637 :   if (tmp_sym->attr.access == ACCESS_PRIVATE
    6806           11 :       && tmp_sym->binding_label
    6807            8 :       && strcmp (tmp_sym->name, tmp_sym->binding_label) == 0
    6808            5 :       && (tmp_sym->attr.flavor == FL_VARIABLE
    6809            4 :           || tmp_sym->attr.if_source == IFSRC_DECL))
    6810            4 :     gfc_warning (OPT_Wsurprising,
    6811              :                  "Symbol %qs at %L is marked PRIVATE but is accessible "
    6812              :                  "via its default binding name %qs", tmp_sym->name,
    6813              :                  &(tmp_sym->declared_at), tmp_sym->binding_label);
    6814              : 
    6815         7637 :   return retval;
    6816              : }
    6817              : 
    6818              : 
    6819              : /* Set the appropriate fields for a symbol that's been declared as
    6820              :    BIND(C) (the is_bind_c flag and the binding label), and verify that
    6821              :    the type is C interoperable.  Errors are reported by the functions
    6822              :    used to set/test these fields.  */
    6823              : 
    6824              : static bool
    6825           47 : set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
    6826              : {
    6827           47 :   bool retval = true;
    6828              : 
    6829              :   /* TODO: Do we need to make sure the vars aren't marked private?  */
    6830              : 
    6831              :   /* Set the is_bind_c bit in symbol_attribute.  */
    6832           47 :   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
    6833              : 
    6834           47 :   if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
    6835              :     return false;
    6836              : 
    6837              :   return retval;
    6838              : }
    6839              : 
    6840              : 
    6841              : /* Set the fields marking the given common block as BIND(C), including
    6842              :    a binding label, and report any errors encountered.  */
    6843              : 
    6844              : static bool
    6845           76 : set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
    6846              : {
    6847           76 :   bool retval = true;
    6848              : 
    6849              :   /* destLabel, common name, typespec (which may have binding label).  */
    6850           76 :   if (!set_binding_label (&com_block->binding_label, com_block->name,
    6851              :                           num_idents))
    6852              :     return false;
    6853              : 
    6854              :   /* Set the given common block (com_block) to being bind(c) (1).  */
    6855           76 :   set_com_block_bind_c (com_block, 1);
    6856              : 
    6857           76 :   return retval;
    6858              : }
    6859              : 
    6860              : 
    6861              : /* Retrieve the list of one or more identifiers that the given bind(c)
    6862              :    attribute applies to.  */
    6863              : 
    6864              : static bool
    6865          102 : get_bind_c_idents (void)
    6866              : {
    6867          102 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    6868          102 :   int num_idents = 0;
    6869          102 :   gfc_symbol *tmp_sym = NULL;
    6870          102 :   match found_id;
    6871          102 :   gfc_common_head *com_block = NULL;
    6872              : 
    6873          102 :   if (gfc_match_name (name) == MATCH_YES)
    6874              :     {
    6875           38 :       found_id = MATCH_YES;
    6876           38 :       gfc_get_ha_symbol (name, &tmp_sym);
    6877              :     }
    6878           64 :   else if (gfc_match_common_name (name) == MATCH_YES)
    6879              :     {
    6880           64 :       found_id = MATCH_YES;
    6881           64 :       com_block = gfc_get_common (name, 0);
    6882              :     }
    6883              :   else
    6884              :     {
    6885            0 :       gfc_error ("Need either entity or common block name for "
    6886              :                  "attribute specification statement at %C");
    6887            0 :       return false;
    6888              :     }
    6889              : 
    6890              :   /* Save the current identifier and look for more.  */
    6891          123 :   do
    6892              :     {
    6893              :       /* Increment the number of identifiers found for this spec stmt.  */
    6894          123 :       num_idents++;
    6895              : 
    6896              :       /* Make sure we have a sym or com block, and verify that it can
    6897              :          be bind(c).  Set the appropriate field(s) and look for more
    6898              :          identifiers.  */
    6899          123 :       if (tmp_sym != NULL || com_block != NULL)
    6900              :         {
    6901          123 :           if (tmp_sym != NULL)
    6902              :             {
    6903           47 :               if (!set_verify_bind_c_sym (tmp_sym, num_idents))
    6904              :                 return false;
    6905              :             }
    6906              :           else
    6907              :             {
    6908           76 :               if (!set_verify_bind_c_com_block (com_block, num_idents))
    6909              :                 return false;
    6910              :             }
    6911              : 
    6912              :           /* Look to see if we have another identifier.  */
    6913          122 :           tmp_sym = NULL;
    6914          122 :           if (gfc_match_eos () == MATCH_YES)
    6915              :             found_id = MATCH_NO;
    6916           21 :           else if (gfc_match_char (',') != MATCH_YES)
    6917              :             found_id = MATCH_NO;
    6918           21 :           else if (gfc_match_name (name) == MATCH_YES)
    6919              :             {
    6920            9 :               found_id = MATCH_YES;
    6921            9 :               gfc_get_ha_symbol (name, &tmp_sym);
    6922              :             }
    6923           12 :           else if (gfc_match_common_name (name) == MATCH_YES)
    6924              :             {
    6925           12 :               found_id = MATCH_YES;
    6926           12 :               com_block = gfc_get_common (name, 0);
    6927              :             }
    6928              :           else
    6929              :             {
    6930            0 :               gfc_error ("Missing entity or common block name for "
    6931              :                          "attribute specification statement at %C");
    6932            0 :               return false;
    6933              :             }
    6934              :         }
    6935              :       else
    6936              :         {
    6937            0 :           gfc_internal_error ("Missing symbol");
    6938              :         }
    6939          122 :     } while (found_id == MATCH_YES);
    6940              : 
    6941              :   /* if we get here we were successful */
    6942              :   return true;
    6943              : }
    6944              : 
    6945              : 
    6946              : /* Try and match a BIND(C) attribute specification statement.  */
    6947              : 
    6948              : match
    6949          140 : gfc_match_bind_c_stmt (void)
    6950              : {
    6951          140 :   match found_match = MATCH_NO;
    6952          140 :   gfc_typespec *ts;
    6953              : 
    6954          140 :   ts = &current_ts;
    6955              : 
    6956              :   /* This may not be necessary.  */
    6957          140 :   gfc_clear_ts (ts);
    6958              :   /* Clear the temporary binding label holder.  */
    6959          140 :   curr_binding_label = NULL;
    6960              : 
    6961              :   /* Look for the bind(c).  */
    6962          140 :   found_match = gfc_match_bind_c (NULL, true);
    6963              : 
    6964          140 :   if (found_match == MATCH_YES)
    6965              :     {
    6966          103 :       if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
    6967              :         return MATCH_ERROR;
    6968              : 
    6969              :       /* Look for the :: now, but it is not required.  */
    6970          102 :       gfc_match (" :: ");
    6971              : 
    6972              :       /* Get the identifier(s) that needs to be updated.  This may need to
    6973              :          change to hand the flag(s) for the attr specified so all identifiers
    6974              :          found can have all appropriate parts updated (assuming that the same
    6975              :          spec stmt can have multiple attrs, such as both bind(c) and
    6976              :          allocatable...).  */
    6977          102 :       if (!get_bind_c_idents ())
    6978              :         /* Error message should have printed already.  */
    6979            1 :         return MATCH_ERROR;
    6980              :     }
    6981              : 
    6982              :   return found_match;
    6983              : }
    6984              : 
    6985              : 
    6986              : /* Match a data declaration statement.  */
    6987              : 
    6988              : match
    6989      1064861 : gfc_match_data_decl (void)
    6990              : {
    6991      1064861 :   gfc_symbol *sym;
    6992      1064861 :   match m;
    6993      1064861 :   int elem;
    6994      1064861 :   gfc_component *comp_tail = NULL;
    6995              : 
    6996      1064861 :   type_param_spec_list = NULL;
    6997      1064861 :   decl_type_param_list = NULL;
    6998              : 
    6999      1064861 :   num_idents_on_line = 0;
    7000              : 
    7001              :   /* Record the last component before we start, so that we can roll back
    7002              :      any components added during this statement on error.  PR106946.
    7003              :      Must be set before any 'goto cleanup' with m == MATCH_ERROR.  */
    7004      1064861 :   if (gfc_comp_struct (gfc_current_state ()))
    7005              :     {
    7006        35229 :       gfc_symbol *block = gfc_current_block ();
    7007        35229 :       if (block)
    7008              :         {
    7009        35229 :           comp_tail = block->components;
    7010        35229 :           if (comp_tail)
    7011        37745 :             while (comp_tail->next)
    7012              :               comp_tail = comp_tail->next;
    7013              :         }
    7014              :     }
    7015              : 
    7016      1064861 :   m = gfc_match_decl_type_spec (&current_ts, 0);
    7017      1064861 :   if (m != MATCH_YES)
    7018              :     return m;
    7019              : 
    7020       225508 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    7021        37806 :         && !gfc_comp_struct (gfc_current_state ()))
    7022              :     {
    7023        34063 :       sym = gfc_use_derived (current_ts.u.derived);
    7024              : 
    7025        34063 :       if (sym == NULL)
    7026              :         {
    7027           22 :           m = MATCH_ERROR;
    7028           22 :           goto cleanup;
    7029              :         }
    7030              : 
    7031        34041 :       current_ts.u.derived = sym;
    7032              :     }
    7033              : 
    7034       225486 :   m = match_attr_spec ();
    7035       225486 :   if (m == MATCH_ERROR)
    7036              :     {
    7037           84 :       m = MATCH_NO;
    7038           84 :       goto cleanup;
    7039              :     }
    7040              : 
    7041              :   /* F2018:C708.  */
    7042       225402 :   if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
    7043              :     {
    7044            6 :       gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
    7045            6 :       m = MATCH_ERROR;
    7046            6 :       goto cleanup;
    7047              :     }
    7048              : 
    7049       225396 :   if (current_ts.type == BT_CLASS
    7050        11842 :         && current_ts.u.derived->attr.unlimited_polymorphic)
    7051         2132 :     goto ok;
    7052              : 
    7053       223264 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    7054        35645 :       && current_ts.u.derived->components == NULL
    7055         2887 :       && !current_ts.u.derived->attr.zero_comp)
    7056              :     {
    7057              : 
    7058          210 :       if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
    7059          136 :         goto ok;
    7060              : 
    7061           74 :       if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
    7062           47 :         goto ok;
    7063              : 
    7064           27 :       gfc_find_symbol (current_ts.u.derived->name,
    7065           27 :                        current_ts.u.derived->ns, 1, &sym);
    7066              : 
    7067              :       /* Any symbol that we find had better be a type definition
    7068              :          which has its components defined, or be a structure definition
    7069              :          actively being parsed.  */
    7070           27 :       if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
    7071           26 :           && (current_ts.u.derived->components != NULL
    7072           26 :               || current_ts.u.derived->attr.zero_comp
    7073           26 :               || current_ts.u.derived == gfc_new_block))
    7074           26 :         goto ok;
    7075              : 
    7076            1 :       gfc_error ("Derived type at %C has not been previously defined "
    7077              :                  "and so cannot appear in a derived type definition");
    7078            1 :       m = MATCH_ERROR;
    7079            1 :       goto cleanup;
    7080              :     }
    7081              : 
    7082       223054 : ok:
    7083              :   /* If we have an old-style character declaration, and no new-style
    7084              :      attribute specifications, then there a comma is optional between
    7085              :      the type specification and the variable list.  */
    7086       225395 :   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
    7087         1407 :     gfc_match_char (',');
    7088              : 
    7089              :   /* Give the types/attributes to symbols that follow. Give the element
    7090              :      a number so that repeat character length expressions can be copied.  */
    7091       225395 :   elem = 1;
    7092       292401 :   for (;;)
    7093              :     {
    7094       292401 :       num_idents_on_line++;
    7095       292401 :       m = variable_decl (elem++);
    7096       292399 :       if (m == MATCH_ERROR)
    7097          413 :         goto cleanup;
    7098       291986 :       if (m == MATCH_NO)
    7099              :         break;
    7100              : 
    7101       291975 :       if (gfc_match_eos () == MATCH_YES)
    7102       224945 :         goto cleanup;
    7103        67030 :       if (gfc_match_char (',') != MATCH_YES)
    7104              :         break;
    7105              :     }
    7106              : 
    7107           35 :   if (!gfc_error_flag_test ())
    7108              :     {
    7109              :       /* An anonymous structure declaration is unambiguous; if we matched one
    7110              :          according to gfc_match_structure_decl, we need to return MATCH_YES
    7111              :          here to avoid confusing the remaining matchers, even if there was an
    7112              :          error during variable_decl.  We must flush any such errors.  Note this
    7113              :          causes the parser to gracefully continue parsing the remaining input
    7114              :          as a structure body, which likely follows.  */
    7115           11 :       if (current_ts.type == BT_DERIVED && current_ts.u.derived
    7116            1 :           && gfc_fl_struct (current_ts.u.derived->attr.flavor))
    7117              :         {
    7118            1 :           gfc_error_now ("Syntax error in anonymous structure declaration"
    7119              :                          " at %C");
    7120              :           /* Skip the bad variable_decl and line up for the start of the
    7121              :              structure body.  */
    7122            1 :           gfc_error_recovery ();
    7123            1 :           m = MATCH_YES;
    7124            1 :           goto cleanup;
    7125              :         }
    7126              : 
    7127           10 :       gfc_error ("Syntax error in data declaration at %C");
    7128              :     }
    7129              : 
    7130           34 :   m = MATCH_ERROR;
    7131              : 
    7132           34 :   gfc_free_data_all (gfc_current_ns);
    7133              : 
    7134       225506 : cleanup:
    7135              :   /* If we failed inside a derived type definition, remove any CLASS
    7136              :      components that were added during this failed statement.  For CLASS
    7137              :      components, gfc_build_class_symbol creates an extra container symbol in
    7138              :      the namespace outside the normal undo machinery.  When reject_statement
    7139              :      later calls gfc_undo_symbols, the declaration state is rolled back but
    7140              :      that helper symbol survives and leaves the component dangling.  Ordinary
    7141              :      components do not create that extra helper symbol, so leave them in
    7142              :      place for the usual follow-up diagnostics.  PR106946.
    7143              : 
    7144              :      CLASS containers are shared between components of the same class type
    7145              :      and attributes (gfc_build_class_symbol reuses existing containers).
    7146              :      We must not free a container that is still referenced by a previously
    7147              :      committed component.  Unlink and free the components first, then clean
    7148              :      up only orphaned containers.  PR124482.  */
    7149       225506 :   if (m == MATCH_ERROR && gfc_comp_struct (gfc_current_state ()))
    7150              :     {
    7151           86 :       gfc_symbol *block = gfc_current_block ();
    7152           86 :       if (block)
    7153              :         {
    7154           86 :           gfc_component **prev;
    7155           86 :           if (comp_tail)
    7156           43 :             prev = &comp_tail->next;
    7157              :           else
    7158           43 :             prev = &block->components;
    7159              : 
    7160              :           /* Record the CLASS container from the removed components.
    7161              :              Normally all components in one declaration share a single
    7162              :              container, but per-variable array specs can produce
    7163              :              additional ones; any beyond the first are harmlessly
    7164              :              leaked until namespace destruction.  */
    7165           86 :           gfc_symbol *fclass_container = NULL;
    7166              : 
    7167          120 :           while (*prev)
    7168              :             {
    7169           34 :               gfc_component *c = *prev;
    7170           34 :               if (c->ts.type == BT_CLASS && c->ts.u.derived
    7171            6 :                   && c->ts.u.derived->attr.is_class)
    7172              :                 {
    7173            3 :                   *prev = c->next;
    7174            3 :                   if (!fclass_container)
    7175            3 :                     fclass_container = c->ts.u.derived;
    7176            3 :                   c->ts.u.derived = NULL;
    7177            3 :                   gfc_free_component (c);
    7178              :                 }
    7179              :               else
    7180           31 :                 prev = &c->next;
    7181              :             }
    7182              : 
    7183              :           /* Free the container only if no remaining component still
    7184              :              references it.  CLASS containers are shared between
    7185              :              components of the same class type and attributes
    7186              :              (gfc_build_class_symbol reuses existing ones).  */
    7187           86 :           if (fclass_container)
    7188              :             {
    7189            3 :               bool shared = false;
    7190            3 :               for (gfc_component *q = block->components; q; q = q->next)
    7191            1 :                 if (q->ts.type == BT_CLASS
    7192            1 :                     && q->ts.u.derived == fclass_container)
    7193              :                   {
    7194              :                     shared = true;
    7195              :                     break;
    7196              :                   }
    7197            3 :               if (!shared)
    7198              :                 {
    7199            2 :                   if (gfc_find_symtree (fclass_container->ns->sym_root,
    7200              :                                         fclass_container->name))
    7201            2 :                     gfc_delete_symtree (&fclass_container->ns->sym_root,
    7202              :                                         fclass_container->name);
    7203            2 :                   gfc_release_symbol (fclass_container);
    7204              :                 }
    7205              :             }
    7206              :         }
    7207              :     }
    7208              : 
    7209       225506 :   if (saved_kind_expr)
    7210          421 :     gfc_free_expr (saved_kind_expr);
    7211       225506 :   if (type_param_spec_list)
    7212         1666 :     gfc_free_actual_arglist (type_param_spec_list);
    7213       225506 :   if (decl_type_param_list)
    7214         1640 :     gfc_free_actual_arglist (decl_type_param_list);
    7215       225506 :   saved_kind_expr = NULL;
    7216       225506 :   gfc_free_array_spec (current_as);
    7217       225506 :   current_as = NULL;
    7218       225506 :   return m;
    7219              : }
    7220              : 
    7221              : static bool
    7222        25739 : in_module_or_interface(void)
    7223              : {
    7224        25739 :   if (gfc_current_state () == COMP_MODULE
    7225        25739 :       || gfc_current_state () == COMP_SUBMODULE
    7226        25739 :       || gfc_current_state () == COMP_INTERFACE)
    7227              :     return true;
    7228              : 
    7229        21680 :   if (gfc_state_stack->state == COMP_CONTAINS
    7230        20740 :       || gfc_state_stack->state == COMP_FUNCTION
    7231        20634 :       || gfc_state_stack->state == COMP_SUBROUTINE)
    7232              :     {
    7233         1046 :       gfc_state_data *p;
    7234         1090 :       for (p = gfc_state_stack->previous; p ; p = p->previous)
    7235              :         {
    7236         1086 :           if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
    7237          118 :               || p->state == COMP_INTERFACE)
    7238              :             return true;
    7239              :         }
    7240              :     }
    7241              :     return false;
    7242              : }
    7243              : 
    7244              : /* Match a prefix associated with a function or subroutine
    7245              :    declaration.  If the typespec pointer is nonnull, then a typespec
    7246              :    can be matched.  Note that if nothing matches, MATCH_YES is
    7247              :    returned (the null string was matched).  */
    7248              : 
    7249              : match
    7250       252907 : gfc_match_prefix (gfc_typespec *ts)
    7251              : {
    7252       252907 :   bool seen_type;
    7253       252907 :   bool seen_impure;
    7254       252907 :   bool found_prefix;
    7255              : 
    7256       252907 :   gfc_clear_attr (&current_attr);
    7257       252907 :   seen_type = false;
    7258       252907 :   seen_impure = false;
    7259              : 
    7260       252907 :   gcc_assert (!gfc_matching_prefix);
    7261       252907 :   gfc_matching_prefix = true;
    7262              : 
    7263       262542 :   do
    7264              :     {
    7265       283480 :       found_prefix = false;
    7266              : 
    7267              :       /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
    7268              :          corresponding attribute seems natural and distinguishes these
    7269              :          procedures from procedure types of PROC_MODULE, which these are
    7270              :          as well.  */
    7271       283480 :       if (gfc_match ("module% ") == MATCH_YES)
    7272              :         {
    7273        26014 :           if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
    7274          275 :             goto error;
    7275              : 
    7276        25739 :           if (!in_module_or_interface ())
    7277              :             {
    7278        20638 :               gfc_error ("MODULE prefix at %C found outside of a module, "
    7279              :                          "submodule, or interface");
    7280        20638 :               goto error;
    7281              :             }
    7282              : 
    7283         5101 :           current_attr.module_procedure = 1;
    7284         5101 :           found_prefix = true;
    7285              :         }
    7286              : 
    7287       262567 :       if (!seen_type && ts != NULL)
    7288              :         {
    7289       141438 :           match m;
    7290       141438 :           m = gfc_match_decl_type_spec (ts, 0);
    7291       141438 :           if (m == MATCH_ERROR)
    7292           15 :             goto error;
    7293       141423 :           if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
    7294              :             {
    7295              :               seen_type = true;
    7296              :               found_prefix = true;
    7297              :             }
    7298              :         }
    7299              : 
    7300       262552 :       if (gfc_match ("elemental% ") == MATCH_YES)
    7301              :         {
    7302         5408 :           if (!gfc_add_elemental (&current_attr, NULL))
    7303            2 :             goto error;
    7304              : 
    7305              :           found_prefix = true;
    7306              :         }
    7307              : 
    7308       262550 :       if (gfc_match ("pure% ") == MATCH_YES)
    7309              :         {
    7310         2539 :           if (!gfc_add_pure (&current_attr, NULL))
    7311            2 :             goto error;
    7312              : 
    7313              :           found_prefix = true;
    7314              :         }
    7315              : 
    7316       262548 :       if (gfc_match ("recursive% ") == MATCH_YES)
    7317              :         {
    7318          469 :           if (!gfc_add_recursive (&current_attr, NULL))
    7319            2 :             goto error;
    7320              : 
    7321              :           found_prefix = true;
    7322              :         }
    7323              : 
    7324              :       /* IMPURE is a somewhat special case, as it needs not set an actual
    7325              :          attribute but rather only prevents ELEMENTAL routines from being
    7326              :          automatically PURE.  */
    7327       262546 :       if (gfc_match ("impure% ") == MATCH_YES)
    7328              :         {
    7329          729 :           if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
    7330            4 :             goto error;
    7331              : 
    7332              :           seen_impure = true;
    7333              :           found_prefix = true;
    7334              :         }
    7335              :     }
    7336              :   while (found_prefix);
    7337              : 
    7338              :   /* IMPURE and PURE must not both appear, of course.  */
    7339       231969 :   if (seen_impure && current_attr.pure)
    7340              :     {
    7341            4 :       gfc_error ("PURE and IMPURE must not appear both at %C");
    7342            4 :       goto error;
    7343              :     }
    7344              : 
    7345              :   /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE.  */
    7346       231244 :   if (!seen_impure && current_attr.elemental && !current_attr.pure)
    7347              :     {
    7348         4713 :       if (!gfc_add_pure (&current_attr, NULL))
    7349            0 :         goto error;
    7350              :     }
    7351              : 
    7352              :   /* At this point, the next item is not a prefix.  */
    7353       231965 :   gcc_assert (gfc_matching_prefix);
    7354              : 
    7355       231965 :   gfc_matching_prefix = false;
    7356       231965 :   return MATCH_YES;
    7357              : 
    7358        20942 : error:
    7359        20942 :   gcc_assert (gfc_matching_prefix);
    7360        20942 :   gfc_matching_prefix = false;
    7361        20942 :   return MATCH_ERROR;
    7362              : }
    7363              : 
    7364              : 
    7365              : /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
    7366              : 
    7367              : static bool
    7368        65889 : copy_prefix (symbol_attribute *dest, locus *where)
    7369              : {
    7370        65889 :   if (dest->module_procedure)
    7371              :     {
    7372          792 :       if (current_attr.elemental)
    7373           13 :         dest->elemental = 1;
    7374              : 
    7375          792 :       if (current_attr.pure)
    7376           61 :         dest->pure = 1;
    7377              : 
    7378          792 :       if (current_attr.recursive)
    7379            8 :         dest->recursive = 1;
    7380              : 
    7381              :       /* Module procedures are unusual in that the 'dest' is copied from
    7382              :          the interface declaration. However, this is an opportunity to
    7383              :          check that the submodule declaration is compliant with the
    7384              :          interface.  */
    7385          792 :       if (dest->elemental && !current_attr.elemental)
    7386              :         {
    7387            1 :           gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
    7388              :                      "missing at %L", where);
    7389            1 :           return false;
    7390              :         }
    7391              : 
    7392          791 :       if (dest->pure && !current_attr.pure)
    7393              :         {
    7394            1 :           gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
    7395              :                      "missing at %L", where);
    7396            1 :           return false;
    7397              :         }
    7398              : 
    7399          790 :       if (dest->recursive && !current_attr.recursive)
    7400              :         {
    7401            1 :           gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
    7402              :                      "missing at %L", where);
    7403            1 :           return false;
    7404              :         }
    7405              : 
    7406              :       return true;
    7407              :     }
    7408              : 
    7409        65097 :   if (current_attr.elemental && !gfc_add_elemental (dest, where))
    7410              :     return false;
    7411              : 
    7412        65095 :   if (current_attr.pure && !gfc_add_pure (dest, where))
    7413              :     return false;
    7414              : 
    7415        65095 :   if (current_attr.recursive && !gfc_add_recursive (dest, where))
    7416              :     return false;
    7417              : 
    7418              :   return true;
    7419              : }
    7420              : 
    7421              : 
    7422              : /* Match a formal argument list or, if typeparam is true, a
    7423              :    type_param_name_list.  */
    7424              : 
    7425              : match
    7426       509121 : gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
    7427              :                           int null_flag, bool typeparam)
    7428              : {
    7429       509121 :   gfc_formal_arglist *head, *tail, *p, *q;
    7430       509121 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7431       509121 :   gfc_symbol *sym;
    7432       509121 :   match m;
    7433       509121 :   gfc_formal_arglist *formal = NULL;
    7434              : 
    7435       509121 :   head = tail = NULL;
    7436              : 
    7437              :   /* Keep the interface formal argument list and null it so that the
    7438              :      matching for the new declaration can be done.  The numbers and
    7439              :      names of the arguments are checked here. The interface formal
    7440              :      arguments are retained in formal_arglist and the characteristics
    7441              :      are compared in resolve.cc(resolve_fl_procedure).  See the remark
    7442              :      in get_proc_name about the eventual need to copy the formal_arglist
    7443              :      and populate the formal namespace of the interface symbol.  */
    7444       509121 :   if (progname->attr.module_procedure
    7445          796 :       && progname->attr.host_assoc)
    7446              :     {
    7447          217 :       formal = progname->formal;
    7448          217 :       progname->formal = NULL;
    7449              :     }
    7450              : 
    7451       509121 :   if (gfc_match_char ('(') != MATCH_YES)
    7452              :     {
    7453       300360 :       if (null_flag)
    7454         6897 :         goto ok;
    7455              :       return MATCH_NO;
    7456              :     }
    7457              : 
    7458       208761 :   if (gfc_match_char (')') == MATCH_YES)
    7459              :   {
    7460        10806 :     if (typeparam)
    7461              :       {
    7462            1 :         gfc_error_now ("A type parameter list is required at %C");
    7463            1 :         m = MATCH_ERROR;
    7464            1 :         goto cleanup;
    7465              :       }
    7466              :     else
    7467        10805 :       goto ok;
    7468              :   }
    7469              : 
    7470       261502 :   for (;;)
    7471              :     {
    7472       261502 :       gfc_gobble_whitespace ();
    7473       261502 :       if (gfc_match_char ('*') == MATCH_YES)
    7474              :         {
    7475        10656 :           sym = NULL;
    7476        10656 :           if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
    7477              :                              "Alternate-return argument at %C"))
    7478              :             {
    7479            1 :               m = MATCH_ERROR;
    7480            1 :               goto cleanup;
    7481              :             }
    7482        10655 :           else if (typeparam)
    7483            2 :             gfc_error_now ("A parameter name is required at %C");
    7484              :         }
    7485              :       else
    7486              :         {
    7487       250846 :           locus loc = gfc_current_locus;
    7488       250846 :           m = gfc_match_name (name);
    7489       250846 :           if (m != MATCH_YES)
    7490              :             {
    7491        17267 :               if(typeparam)
    7492            1 :                 gfc_error_now ("A parameter name is required at %C");
    7493        17283 :               goto cleanup;
    7494              :             }
    7495       233579 :           loc = gfc_get_location_range (NULL, 0, &loc, 1, &gfc_current_locus);
    7496              : 
    7497       233579 :           if (!typeparam && gfc_get_symbol (name, NULL, &sym, &loc))
    7498           16 :             goto cleanup;
    7499       233563 :           else if (typeparam
    7500       233563 :                    && gfc_get_symbol (name, progname->f2k_derived, &sym, &loc))
    7501            0 :             goto cleanup;
    7502              :         }
    7503              : 
    7504       244218 :       p = gfc_get_formal_arglist ();
    7505              : 
    7506       244218 :       if (head == NULL)
    7507              :         head = tail = p;
    7508              :       else
    7509              :         {
    7510        62837 :           tail->next = p;
    7511        62837 :           tail = p;
    7512              :         }
    7513              : 
    7514       244218 :       tail->sym = sym;
    7515              : 
    7516              :       /* We don't add the VARIABLE flavor because the name could be a
    7517              :          dummy procedure.  We don't apply these attributes to formal
    7518              :          arguments of statement functions.  */
    7519       233563 :       if (sym != NULL && !st_flag
    7520       348783 :           && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
    7521       104565 :               || !gfc_missing_attr (&sym->attr, NULL)))
    7522              :         {
    7523            0 :           m = MATCH_ERROR;
    7524            0 :           goto cleanup;
    7525              :         }
    7526              : 
    7527              :       /* The name of a program unit can be in a different namespace,
    7528              :          so check for it explicitly.  After the statement is accepted,
    7529              :          the name is checked for especially in gfc_get_symbol().  */
    7530       244218 :       if (gfc_new_block != NULL && sym != NULL && !typeparam
    7531       102861 :           && strcmp (sym->name, gfc_new_block->name) == 0)
    7532              :         {
    7533            0 :           gfc_error ("Name %qs at %C is the name of the procedure",
    7534              :                      sym->name);
    7535            0 :           m = MATCH_ERROR;
    7536            0 :           goto cleanup;
    7537              :         }
    7538              : 
    7539       244218 :       if (gfc_match_char (')') == MATCH_YES)
    7540       130004 :         goto ok;
    7541              : 
    7542       114214 :       m = gfc_match_char (',');
    7543       114214 :       if (m != MATCH_YES)
    7544              :         {
    7545        50667 :           if (typeparam)
    7546            1 :             gfc_error_now ("Expected parameter list in type declaration "
    7547              :                            "at %C");
    7548              :           else
    7549        50666 :             gfc_error ("Unexpected junk in formal argument list at %C");
    7550        50667 :           goto cleanup;
    7551              :         }
    7552              :     }
    7553              : 
    7554       147706 : ok:
    7555              :   /* Check for duplicate symbols in the formal argument list.  */
    7556       147706 :   if (head != NULL)
    7557              :     {
    7558       191188 :       for (p = head; p->next; p = p->next)
    7559              :         {
    7560        61232 :           if (p->sym == NULL)
    7561          338 :             continue;
    7562              : 
    7563       239762 :           for (q = p->next; q; q = q->next)
    7564       178916 :             if (p->sym == q->sym)
    7565              :               {
    7566           48 :                 if (typeparam)
    7567            1 :                   gfc_error_now ("Duplicate name %qs in parameter "
    7568              :                                  "list at %C", p->sym->name);
    7569              :                 else
    7570           47 :                   gfc_error ("Duplicate symbol %qs in formal argument "
    7571              :                              "list at %C", p->sym->name);
    7572              : 
    7573           48 :                 m = MATCH_ERROR;
    7574           48 :                 goto cleanup;
    7575              :               }
    7576              :         }
    7577              :     }
    7578              : 
    7579       147658 :   if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
    7580              :     {
    7581            0 :       m = MATCH_ERROR;
    7582            0 :       goto cleanup;
    7583              :     }
    7584              : 
    7585              :   /* gfc_error_now used in following and return with MATCH_YES because
    7586              :      doing otherwise results in a cascade of extraneous errors and in
    7587              :      some cases an ICE in symbol.cc(gfc_release_symbol).  */
    7588       147658 :   if (progname->attr.module_procedure && progname->attr.host_assoc)
    7589              :     {
    7590          216 :       bool arg_count_mismatch = false;
    7591              : 
    7592          216 :       if (!formal && head)
    7593              :         arg_count_mismatch = true;
    7594              : 
    7595              :       /* Abbreviated module procedure declaration is not meant to have any
    7596              :          formal arguments!  */
    7597          216 :       if (!progname->abr_modproc_decl && formal && !head)
    7598          216 :         arg_count_mismatch = true;
    7599              : 
    7600          418 :       for (p = formal, q = head; p && q; p = p->next, q = q->next)
    7601              :         {
    7602          202 :           if ((p->next != NULL && q->next == NULL)
    7603          201 :               || (p->next == NULL && q->next != NULL))
    7604              :             arg_count_mismatch = true;
    7605          200 :           else if ((p->sym == NULL && q->sym == NULL)
    7606          200 :                     || (p->sym && q->sym
    7607          198 :                         && strcmp (p->sym->name, q->sym->name) == 0))
    7608          196 :             continue;
    7609              :           else
    7610              :             {
    7611            4 :               if (q->sym == NULL)
    7612            1 :                 gfc_error_now ("MODULE PROCEDURE formal argument %qs "
    7613              :                                "conflicts with alternate return at %C",
    7614              :                                p->sym->name);
    7615            3 :               else if (p->sym == NULL)
    7616            1 :                 gfc_error_now ("MODULE PROCEDURE formal argument is "
    7617              :                                "alternate return and conflicts with "
    7618              :                                "%qs in the separate declaration at %C",
    7619              :                                q->sym->name);
    7620              :               else
    7621            2 :                 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
    7622              :                                "argument names (%s/%s) at %C",
    7623              :                                p->sym->name, q->sym->name);
    7624              :             }
    7625              :         }
    7626              : 
    7627          216 :       if (arg_count_mismatch)
    7628            4 :         gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
    7629              :                        "formal arguments at %C");
    7630              :     }
    7631              : 
    7632              :   return MATCH_YES;
    7633              : 
    7634        68000 : cleanup:
    7635        68000 :   gfc_free_formal_arglist (head);
    7636        68000 :   return m;
    7637              : }
    7638              : 
    7639              : 
    7640              : /* Match a RESULT specification following a function declaration or
    7641              :    ENTRY statement.  Also matches the end-of-statement.  */
    7642              : 
    7643              : static match
    7644         9127 : match_result (gfc_symbol *function, gfc_symbol **result)
    7645              : {
    7646         9127 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7647         9127 :   gfc_symbol *r;
    7648         9127 :   match m;
    7649              : 
    7650         9127 :   if (gfc_match (" result (") != MATCH_YES)
    7651              :     return MATCH_NO;
    7652              : 
    7653         6457 :   m = gfc_match_name (name);
    7654         6457 :   if (m != MATCH_YES)
    7655              :     return m;
    7656              : 
    7657              :   /* Get the right paren, and that's it because there could be the
    7658              :      bind(c) attribute after the result clause.  */
    7659         6457 :   if (gfc_match_char (')') != MATCH_YES)
    7660              :     {
    7661              :      /* TODO: should report the missing right paren here.  */
    7662              :       return MATCH_ERROR;
    7663              :     }
    7664              : 
    7665         6457 :   if (strcmp (function->name, name) == 0)
    7666              :     {
    7667            1 :       gfc_error ("RESULT variable at %C must be different than function name");
    7668            1 :       return MATCH_ERROR;
    7669              :     }
    7670              : 
    7671         6456 :   if (gfc_get_symbol (name, NULL, &r))
    7672              :     return MATCH_ERROR;
    7673              : 
    7674         6456 :   if (!gfc_add_result (&r->attr, r->name, NULL))
    7675              :     return MATCH_ERROR;
    7676              : 
    7677         6456 :   *result = r;
    7678              : 
    7679         6456 :   return MATCH_YES;
    7680              : }
    7681              : 
    7682              : 
    7683              : /* Match a function suffix, which could be a combination of a result
    7684              :    clause and BIND(C), either one, or neither.  The draft does not
    7685              :    require them to come in a specific order.  */
    7686              : 
    7687              : static match
    7688         9131 : gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
    7689              : {
    7690         9131 :   match is_bind_c;   /* Found bind(c).  */
    7691         9131 :   match is_result;   /* Found result clause.  */
    7692         9131 :   match found_match; /* Status of whether we've found a good match.  */
    7693         9131 :   char peek_char;    /* Character we're going to peek at.  */
    7694         9131 :   bool allow_binding_name;
    7695              : 
    7696              :   /* Initialize to having found nothing.  */
    7697         9131 :   found_match = MATCH_NO;
    7698         9131 :   is_bind_c = MATCH_NO;
    7699         9131 :   is_result = MATCH_NO;
    7700              : 
    7701              :   /* Get the next char to narrow between result and bind(c).  */
    7702         9131 :   gfc_gobble_whitespace ();
    7703         9131 :   peek_char = gfc_peek_ascii_char ();
    7704              : 
    7705              :   /* C binding names are not allowed for internal procedures.  */
    7706         9131 :   if (gfc_current_state () == COMP_CONTAINS
    7707         5138 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    7708              :     allow_binding_name = false;
    7709              :   else
    7710         7298 :     allow_binding_name = true;
    7711              : 
    7712         9131 :   switch (peek_char)
    7713              :     {
    7714         6076 :     case 'r':
    7715              :       /* Look for result clause.  */
    7716         6076 :       is_result = match_result (sym, result);
    7717         6076 :       if (is_result == MATCH_YES)
    7718              :         {
    7719              :           /* Now see if there is a bind(c) after it.  */
    7720         6075 :           is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    7721              :           /* We've found the result clause and possibly bind(c).  */
    7722         6075 :           found_match = MATCH_YES;
    7723              :         }
    7724              :       else
    7725              :         /* This should only be MATCH_ERROR.  */
    7726              :         found_match = is_result;
    7727              :       break;
    7728         3055 :     case 'b':
    7729              :       /* Look for bind(c) first.  */
    7730         3055 :       is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    7731         3055 :       if (is_bind_c == MATCH_YES)
    7732              :         {
    7733              :           /* Now see if a result clause followed it.  */
    7734         3051 :           is_result = match_result (sym, result);
    7735         3051 :           found_match = MATCH_YES;
    7736              :         }
    7737              :       else
    7738              :         {
    7739              :           /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
    7740              :           found_match = MATCH_ERROR;
    7741              :         }
    7742              :       break;
    7743            0 :     default:
    7744            0 :       gfc_error ("Unexpected junk after function declaration at %C");
    7745            0 :       found_match = MATCH_ERROR;
    7746            0 :       break;
    7747              :     }
    7748              : 
    7749         9126 :   if (is_bind_c == MATCH_YES)
    7750              :     {
    7751              :       /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
    7752         3218 :       if (gfc_current_state () == COMP_CONTAINS
    7753          425 :           && sym->ns->proc_name->attr.flavor != FL_MODULE
    7754         3236 :           && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
    7755              :                               "at %L may not be specified for an internal "
    7756              :                               "procedure", &gfc_current_locus))
    7757              :         return MATCH_ERROR;
    7758              : 
    7759         3215 :       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
    7760            0 :         return MATCH_ERROR;
    7761              :     }
    7762              : 
    7763              :   return found_match;
    7764              : }
    7765              : 
    7766              : 
    7767              : /* Procedure pointer return value without RESULT statement:
    7768              :    Add "hidden" result variable named "ppr@".  */
    7769              : 
    7770              : static bool
    7771        77630 : add_hidden_procptr_result (gfc_symbol *sym)
    7772              : {
    7773        77630 :   bool case1,case2;
    7774              : 
    7775        77630 :   if (gfc_notification_std (GFC_STD_F2003) == ERROR)
    7776              :     return false;
    7777              : 
    7778              :   /* First usage case: PROCEDURE and EXTERNAL statements.  */
    7779         1543 :   case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
    7780         1543 :           && strcmp (gfc_current_block ()->name, sym->name) == 0
    7781        78029 :           && sym->attr.external;
    7782              :   /* Second usage case: INTERFACE statements.  */
    7783        15259 :   case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
    7784        15259 :           && gfc_state_stack->previous->state == COMP_FUNCTION
    7785        77677 :           && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
    7786              : 
    7787        77446 :   if (case1 || case2)
    7788              :     {
    7789          125 :       gfc_symtree *stree;
    7790          125 :       if (case1)
    7791           95 :         gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
    7792              :       else
    7793              :         {
    7794           30 :           gfc_symtree *st2;
    7795           30 :           gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
    7796           30 :           st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
    7797           30 :           st2->n.sym = stree->n.sym;
    7798           30 :           stree->n.sym->refs++;
    7799              :         }
    7800          125 :       sym->result = stree->n.sym;
    7801              : 
    7802          125 :       sym->result->attr.proc_pointer = sym->attr.proc_pointer;
    7803          125 :       sym->result->attr.pointer = sym->attr.pointer;
    7804          125 :       sym->result->attr.external = sym->attr.external;
    7805          125 :       sym->result->attr.referenced = sym->attr.referenced;
    7806          125 :       sym->result->ts = sym->ts;
    7807          125 :       sym->attr.proc_pointer = 0;
    7808          125 :       sym->attr.pointer = 0;
    7809          125 :       sym->attr.external = 0;
    7810          125 :       if (sym->result->attr.external && sym->result->attr.pointer)
    7811              :         {
    7812            4 :           sym->result->attr.pointer = 0;
    7813            4 :           sym->result->attr.proc_pointer = 1;
    7814              :         }
    7815              : 
    7816          125 :       return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
    7817              :     }
    7818              :   /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
    7819        77351 :   else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
    7820          411 :            && sym->result && sym->result != sym && sym->result->attr.external
    7821           28 :            && sym == gfc_current_ns->proc_name
    7822           28 :            && sym == sym->result->ns->proc_name
    7823           28 :            && strcmp ("ppr@", sym->result->name) == 0)
    7824              :     {
    7825           28 :       sym->result->attr.proc_pointer = 1;
    7826           28 :       sym->attr.pointer = 0;
    7827           28 :       return true;
    7828              :     }
    7829              :   else
    7830              :     return false;
    7831              : }
    7832              : 
    7833              : 
    7834              : /* Match the interface for a PROCEDURE declaration,
    7835              :    including brackets (R1212).  */
    7836              : 
    7837              : static match
    7838         1664 : match_procedure_interface (gfc_symbol **proc_if)
    7839              : {
    7840         1664 :   match m;
    7841         1664 :   gfc_symtree *st;
    7842         1664 :   locus old_loc, entry_loc;
    7843         1664 :   gfc_namespace *old_ns = gfc_current_ns;
    7844         1664 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7845              : 
    7846         1664 :   old_loc = entry_loc = gfc_current_locus;
    7847         1664 :   gfc_clear_ts (&current_ts);
    7848              : 
    7849         1664 :   if (gfc_match (" (") != MATCH_YES)
    7850              :     {
    7851            1 :       gfc_current_locus = entry_loc;
    7852            1 :       return MATCH_NO;
    7853              :     }
    7854              : 
    7855              :   /* Get the type spec. for the procedure interface.  */
    7856         1663 :   old_loc = gfc_current_locus;
    7857         1663 :   m = gfc_match_decl_type_spec (&current_ts, 0);
    7858         1663 :   gfc_gobble_whitespace ();
    7859         1663 :   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
    7860          401 :     goto got_ts;
    7861              : 
    7862         1262 :   if (m == MATCH_ERROR)
    7863              :     return m;
    7864              : 
    7865              :   /* Procedure interface is itself a procedure.  */
    7866         1262 :   gfc_current_locus = old_loc;
    7867         1262 :   m = gfc_match_name (name);
    7868              : 
    7869              :   /* First look to see if it is already accessible in the current
    7870              :      namespace because it is use associated or contained.  */
    7871         1262 :   st = NULL;
    7872         1262 :   if (gfc_find_sym_tree (name, NULL, 0, &st))
    7873              :     return MATCH_ERROR;
    7874              : 
    7875              :   /* If it is still not found, then try the parent namespace, if it
    7876              :      exists and create the symbol there if it is still not found.  */
    7877         1262 :   if (gfc_current_ns->parent)
    7878          435 :     gfc_current_ns = gfc_current_ns->parent;
    7879         1262 :   if (st == NULL && gfc_get_ha_sym_tree (name, &st))
    7880              :     return MATCH_ERROR;
    7881              : 
    7882         1262 :   gfc_current_ns = old_ns;
    7883         1262 :   *proc_if = st->n.sym;
    7884              : 
    7885         1262 :   if (*proc_if)
    7886              :     {
    7887         1262 :       (*proc_if)->refs++;
    7888              :       /* Resolve interface if possible. That way, attr.procedure is only set
    7889              :          if it is declared by a later procedure-declaration-stmt, which is
    7890              :          invalid per F08:C1216 (cf. resolve_procedure_interface).  */
    7891         1262 :       while ((*proc_if)->ts.interface
    7892         1269 :              && *proc_if != (*proc_if)->ts.interface)
    7893            7 :         *proc_if = (*proc_if)->ts.interface;
    7894              : 
    7895         1262 :       if ((*proc_if)->attr.flavor == FL_UNKNOWN
    7896          395 :           && (*proc_if)->ts.type == BT_UNKNOWN
    7897         1657 :           && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
    7898              :                               (*proc_if)->name, NULL))
    7899              :         return MATCH_ERROR;
    7900              :     }
    7901              : 
    7902            0 : got_ts:
    7903         1663 :   if (gfc_match (" )") != MATCH_YES)
    7904              :     {
    7905            0 :       gfc_current_locus = entry_loc;
    7906            0 :       return MATCH_NO;
    7907              :     }
    7908              : 
    7909              :   return MATCH_YES;
    7910              : }
    7911              : 
    7912              : 
    7913              : /* Match a PROCEDURE declaration (R1211).  */
    7914              : 
    7915              : static match
    7916         1206 : match_procedure_decl (void)
    7917              : {
    7918         1206 :   match m;
    7919         1206 :   gfc_symbol *sym, *proc_if = NULL;
    7920         1206 :   int num;
    7921         1206 :   gfc_expr *initializer = NULL;
    7922              : 
    7923              :   /* Parse interface (with brackets).  */
    7924         1206 :   m = match_procedure_interface (&proc_if);
    7925         1206 :   if (m != MATCH_YES)
    7926              :     return m;
    7927              : 
    7928              :   /* Parse attributes (with colons).  */
    7929         1206 :   m = match_attr_spec();
    7930         1206 :   if (m == MATCH_ERROR)
    7931              :     return MATCH_ERROR;
    7932              : 
    7933         1205 :   if (current_attr.allocatable)
    7934              :     {
    7935            2 :       current_attr.procedure = 1;
    7936            2 :       gfc_check_conflict (&current_attr, NULL, &gfc_current_locus);
    7937            2 :       return MATCH_ERROR;
    7938              :     }
    7939              : 
    7940         1203 :   if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
    7941              :     {
    7942           53 :       current_attr.is_bind_c = 1;
    7943           53 :       has_name_equals = 0;
    7944           53 :       curr_binding_label = NULL;
    7945              :     }
    7946              : 
    7947              :   /* Get procedure symbols.  */
    7948         1203 :   for(num=1;;num++)
    7949              :     {
    7950         1282 :       m = gfc_match_symbol (&sym, 0);
    7951         1282 :       if (m == MATCH_NO)
    7952            1 :         goto syntax;
    7953         1281 :       else if (m == MATCH_ERROR)
    7954              :         return m;
    7955              : 
    7956              :       /* Add current_attr to the symbol attributes.  */
    7957         1281 :       if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
    7958              :         return MATCH_ERROR;
    7959              : 
    7960         1279 :       if (sym->attr.is_bind_c)
    7961              :         {
    7962              :           /* Check for C1218.  */
    7963           90 :           if (!proc_if || !proc_if->attr.is_bind_c)
    7964              :             {
    7965            1 :               gfc_error ("BIND(C) attribute at %C requires "
    7966              :                         "an interface with BIND(C)");
    7967            1 :               return MATCH_ERROR;
    7968              :             }
    7969              :           /* Check for C1217.  */
    7970           89 :           if (has_name_equals && sym->attr.pointer)
    7971              :             {
    7972            1 :               gfc_error ("BIND(C) procedure with NAME may not have "
    7973              :                         "POINTER attribute at %C");
    7974            1 :               return MATCH_ERROR;
    7975              :             }
    7976           88 :           if (has_name_equals && sym->attr.dummy)
    7977              :             {
    7978            1 :               gfc_error ("Dummy procedure at %C may not have "
    7979              :                         "BIND(C) attribute with NAME");
    7980            1 :               return MATCH_ERROR;
    7981              :             }
    7982              :           /* Set binding label for BIND(C).  */
    7983           87 :           if (!set_binding_label (&sym->binding_label, sym->name, num))
    7984              :             return MATCH_ERROR;
    7985              :         }
    7986              : 
    7987         1275 :       if (!gfc_add_external (&sym->attr, NULL))
    7988              :         return MATCH_ERROR;
    7989              : 
    7990         1271 :       if (add_hidden_procptr_result (sym))
    7991           68 :         sym = sym->result;
    7992              : 
    7993         1271 :       if (!gfc_add_proc (&sym->attr, sym->name, NULL))
    7994              :         return MATCH_ERROR;
    7995              : 
    7996              :       /* Set interface.  */
    7997         1271 :       if (proc_if != NULL)
    7998              :         {
    7999          923 :           if (sym->ts.type != BT_UNKNOWN)
    8000              :             {
    8001            1 :               gfc_error ("Procedure %qs at %L already has basic type of %s",
    8002              :                          sym->name, &gfc_current_locus,
    8003              :                          gfc_basic_typename (sym->ts.type));
    8004            1 :               return MATCH_ERROR;
    8005              :             }
    8006          922 :           sym->ts.interface = proc_if;
    8007          922 :           sym->attr.untyped = 1;
    8008          922 :           sym->attr.if_source = IFSRC_IFBODY;
    8009              :         }
    8010          348 :       else if (current_ts.type != BT_UNKNOWN)
    8011              :         {
    8012          203 :           if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
    8013              :             return MATCH_ERROR;
    8014          202 :           sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    8015          202 :           sym->ts.interface->ts = current_ts;
    8016          202 :           sym->ts.interface->attr.flavor = FL_PROCEDURE;
    8017          202 :           sym->ts.interface->attr.function = 1;
    8018          202 :           sym->attr.function = 1;
    8019          202 :           sym->attr.if_source = IFSRC_UNKNOWN;
    8020              :         }
    8021              : 
    8022         1269 :       if (gfc_match (" =>") == MATCH_YES)
    8023              :         {
    8024          113 :           if (!current_attr.pointer)
    8025              :             {
    8026            0 :               gfc_error ("Initialization at %C isn't for a pointer variable");
    8027            0 :               m = MATCH_ERROR;
    8028            0 :               goto cleanup;
    8029              :             }
    8030              : 
    8031          113 :           m = match_pointer_init (&initializer, 1);
    8032          113 :           if (m != MATCH_YES)
    8033            1 :             goto cleanup;
    8034              : 
    8035          112 :           if (!add_init_expr_to_sym (sym->name, &initializer,
    8036              :                                      &gfc_current_locus,
    8037              :                                      gfc_current_ns->cl_list))
    8038            0 :             goto cleanup;
    8039              : 
    8040              :         }
    8041              : 
    8042         1268 :       if (gfc_match_eos () == MATCH_YES)
    8043              :         return MATCH_YES;
    8044           79 :       if (gfc_match_char (',') != MATCH_YES)
    8045            0 :         goto syntax;
    8046              :     }
    8047              : 
    8048            1 : syntax:
    8049            1 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
    8050            1 :   return MATCH_ERROR;
    8051              : 
    8052            1 : cleanup:
    8053              :   /* Free stuff up and return.  */
    8054            1 :   gfc_free_expr (initializer);
    8055            1 :   return m;
    8056              : }
    8057              : 
    8058              : 
    8059              : static match
    8060              : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
    8061              : 
    8062              : 
    8063              : /* Match a procedure pointer component declaration (R445).  */
    8064              : 
    8065              : static match
    8066          458 : match_ppc_decl (void)
    8067              : {
    8068          458 :   match m;
    8069          458 :   gfc_symbol *proc_if = NULL;
    8070          458 :   gfc_typespec ts;
    8071          458 :   int num;
    8072          458 :   gfc_component *c;
    8073          458 :   gfc_expr *initializer = NULL;
    8074          458 :   gfc_typebound_proc* tb;
    8075          458 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8076              : 
    8077              :   /* Parse interface (with brackets).  */
    8078          458 :   m = match_procedure_interface (&proc_if);
    8079          458 :   if (m != MATCH_YES)
    8080            1 :     goto syntax;
    8081              : 
    8082              :   /* Parse attributes.  */
    8083          457 :   tb = XCNEW (gfc_typebound_proc);
    8084          457 :   tb->where = gfc_current_locus;
    8085          457 :   m = match_binding_attributes (tb, false, true);
    8086          457 :   if (m == MATCH_ERROR)
    8087              :     return m;
    8088              : 
    8089          454 :   gfc_clear_attr (&current_attr);
    8090          454 :   current_attr.procedure = 1;
    8091          454 :   current_attr.proc_pointer = 1;
    8092          454 :   current_attr.access = tb->access;
    8093          454 :   current_attr.flavor = FL_PROCEDURE;
    8094              : 
    8095              :   /* Match the colons (required).  */
    8096          454 :   if (gfc_match (" ::") != MATCH_YES)
    8097              :     {
    8098            1 :       gfc_error ("Expected %<::%> after binding-attributes at %C");
    8099            1 :       return MATCH_ERROR;
    8100              :     }
    8101              : 
    8102              :   /* Check for C450.  */
    8103          453 :   if (!tb->nopass && proc_if == NULL)
    8104              :     {
    8105            2 :       gfc_error("NOPASS or explicit interface required at %C");
    8106            2 :       return MATCH_ERROR;
    8107              :     }
    8108              : 
    8109          451 :   if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
    8110              :     return MATCH_ERROR;
    8111              : 
    8112              :   /* Match PPC names.  */
    8113          450 :   ts = current_ts;
    8114          450 :   for(num=1;;num++)
    8115              :     {
    8116          451 :       m = gfc_match_name (name);
    8117          451 :       if (m == MATCH_NO)
    8118            0 :         goto syntax;
    8119          451 :       else if (m == MATCH_ERROR)
    8120              :         return m;
    8121              : 
    8122          451 :       if (!gfc_add_component (gfc_current_block(), name, &c))
    8123              :         return MATCH_ERROR;
    8124              : 
    8125              :       /* Add current_attr to the symbol attributes.  */
    8126          451 :       if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
    8127              :         return MATCH_ERROR;
    8128              : 
    8129          451 :       if (!gfc_add_external (&c->attr, NULL))
    8130              :         return MATCH_ERROR;
    8131              : 
    8132          451 :       if (!gfc_add_proc (&c->attr, name, NULL))
    8133              :         return MATCH_ERROR;
    8134              : 
    8135          451 :       if (num == 1)
    8136          450 :         c->tb = tb;
    8137              :       else
    8138              :         {
    8139            1 :           c->tb = XCNEW (gfc_typebound_proc);
    8140            1 :           c->tb->where = gfc_current_locus;
    8141            1 :           *c->tb = *tb;
    8142              :         }
    8143              : 
    8144          451 :       if (saved_kind_expr)
    8145            0 :         c->kind_expr = gfc_copy_expr (saved_kind_expr);
    8146              : 
    8147              :       /* Set interface.  */
    8148          451 :       if (proc_if != NULL)
    8149              :         {
    8150          383 :           c->ts.interface = proc_if;
    8151          383 :           c->attr.untyped = 1;
    8152          383 :           c->attr.if_source = IFSRC_IFBODY;
    8153              :         }
    8154           68 :       else if (ts.type != BT_UNKNOWN)
    8155              :         {
    8156           30 :           c->ts = ts;
    8157           30 :           c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    8158           30 :           c->ts.interface->result = c->ts.interface;
    8159           30 :           c->ts.interface->ts = ts;
    8160           30 :           c->ts.interface->attr.flavor = FL_PROCEDURE;
    8161           30 :           c->ts.interface->attr.function = 1;
    8162           30 :           c->attr.function = 1;
    8163           30 :           c->attr.if_source = IFSRC_UNKNOWN;
    8164              :         }
    8165              : 
    8166          451 :       if (gfc_match (" =>") == MATCH_YES)
    8167              :         {
    8168           98 :           m = match_pointer_init (&initializer, 1);
    8169           98 :           if (m != MATCH_YES)
    8170              :             {
    8171            0 :               gfc_free_expr (initializer);
    8172            0 :               return m;
    8173              :             }
    8174           98 :           c->initializer = initializer;
    8175              :         }
    8176              : 
    8177          451 :       if (gfc_match_eos () == MATCH_YES)
    8178              :         return MATCH_YES;
    8179            1 :       if (gfc_match_char (',') != MATCH_YES)
    8180            0 :         goto syntax;
    8181              :     }
    8182              : 
    8183            1 : syntax:
    8184            1 :   gfc_error ("Syntax error in procedure pointer component at %C");
    8185            1 :   return MATCH_ERROR;
    8186              : }
    8187              : 
    8188              : 
    8189              : /* Match a PROCEDURE declaration inside an interface (R1206).  */
    8190              : 
    8191              : static match
    8192         1561 : match_procedure_in_interface (void)
    8193              : {
    8194         1561 :   match m;
    8195         1561 :   gfc_symbol *sym;
    8196         1561 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8197         1561 :   locus old_locus;
    8198              : 
    8199         1561 :   if (current_interface.type == INTERFACE_NAMELESS
    8200         1561 :       || current_interface.type == INTERFACE_ABSTRACT)
    8201              :     {
    8202            1 :       gfc_error ("PROCEDURE at %C must be in a generic interface");
    8203            1 :       return MATCH_ERROR;
    8204              :     }
    8205              : 
    8206              :   /* Check if the F2008 optional double colon appears.  */
    8207         1560 :   gfc_gobble_whitespace ();
    8208         1560 :   old_locus = gfc_current_locus;
    8209         1560 :   if (gfc_match ("::") == MATCH_YES)
    8210              :     {
    8211          875 :       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
    8212              :                            "MODULE PROCEDURE statement at %L", &old_locus))
    8213              :         return MATCH_ERROR;
    8214              :     }
    8215              :   else
    8216          685 :     gfc_current_locus = old_locus;
    8217              : 
    8218         2214 :   for(;;)
    8219              :     {
    8220         2214 :       m = gfc_match_name (name);
    8221         2214 :       if (m == MATCH_NO)
    8222            0 :         goto syntax;
    8223         2214 :       else if (m == MATCH_ERROR)
    8224              :         return m;
    8225         2214 :       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
    8226              :         return MATCH_ERROR;
    8227              : 
    8228         2214 :       if (!gfc_add_interface (sym))
    8229              :         return MATCH_ERROR;
    8230              : 
    8231         2213 :       if (gfc_match_eos () == MATCH_YES)
    8232              :         break;
    8233          655 :       if (gfc_match_char (',') != MATCH_YES)
    8234            0 :         goto syntax;
    8235              :     }
    8236              : 
    8237              :   return MATCH_YES;
    8238              : 
    8239            0 : syntax:
    8240            0 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
    8241            0 :   return MATCH_ERROR;
    8242              : }
    8243              : 
    8244              : 
    8245              : /* General matcher for PROCEDURE declarations.  */
    8246              : 
    8247              : static match match_procedure_in_type (void);
    8248              : 
    8249              : match
    8250         6551 : gfc_match_procedure (void)
    8251              : {
    8252         6551 :   match m;
    8253              : 
    8254         6551 :   switch (gfc_current_state ())
    8255              :     {
    8256         1206 :     case COMP_NONE:
    8257         1206 :     case COMP_PROGRAM:
    8258         1206 :     case COMP_MODULE:
    8259         1206 :     case COMP_SUBMODULE:
    8260         1206 :     case COMP_SUBROUTINE:
    8261         1206 :     case COMP_FUNCTION:
    8262         1206 :     case COMP_BLOCK:
    8263         1206 :       m = match_procedure_decl ();
    8264         1206 :       break;
    8265         1561 :     case COMP_INTERFACE:
    8266         1561 :       m = match_procedure_in_interface ();
    8267         1561 :       break;
    8268          458 :     case COMP_DERIVED:
    8269          458 :       m = match_ppc_decl ();
    8270          458 :       break;
    8271         3326 :     case COMP_DERIVED_CONTAINS:
    8272         3326 :       m = match_procedure_in_type ();
    8273         3326 :       break;
    8274              :     default:
    8275              :       return MATCH_NO;
    8276              :     }
    8277              : 
    8278         6551 :   if (m != MATCH_YES)
    8279              :     return m;
    8280              : 
    8281         6494 :   if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
    8282            4 :     return MATCH_ERROR;
    8283              : 
    8284              :   return m;
    8285              : }
    8286              : 
    8287              : 
    8288              : /* Warn if a matched procedure has the same name as an intrinsic; this is
    8289              :    simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
    8290              :    parser-state-stack to find out whether we're in a module.  */
    8291              : 
    8292              : static void
    8293        65886 : do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
    8294              : {
    8295        65886 :   bool in_module;
    8296              : 
    8297       131772 :   in_module = (gfc_state_stack->previous
    8298        65886 :                && (gfc_state_stack->previous->state == COMP_MODULE
    8299        54051 :                    || gfc_state_stack->previous->state == COMP_SUBMODULE));
    8300              : 
    8301        65886 :   gfc_warn_intrinsic_shadow (sym, in_module, func);
    8302        65886 : }
    8303              : 
    8304              : 
    8305              : /* Match a function declaration.  */
    8306              : 
    8307              : match
    8308       134750 : gfc_match_function_decl (void)
    8309              : {
    8310       134750 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8311       134750 :   gfc_symbol *sym, *result;
    8312       134750 :   locus old_loc;
    8313       134750 :   match m;
    8314       134750 :   match suffix_match;
    8315       134750 :   match found_match; /* Status returned by match func.  */
    8316              : 
    8317       134750 :   if (gfc_current_state () != COMP_NONE
    8318        85358 :       && gfc_current_state () != COMP_INTERFACE
    8319        55205 :       && gfc_current_state () != COMP_CONTAINS)
    8320              :     return MATCH_NO;
    8321              : 
    8322       134750 :   gfc_clear_ts (&current_ts);
    8323              : 
    8324       134750 :   old_loc = gfc_current_locus;
    8325              : 
    8326       134750 :   m = gfc_match_prefix (&current_ts);
    8327       134750 :   if (m != MATCH_YES)
    8328              :     {
    8329        10473 :       gfc_current_locus = old_loc;
    8330        10473 :       return m;
    8331              :     }
    8332              : 
    8333       124277 :   if (gfc_match ("function% %n", name) != MATCH_YES)
    8334              :     {
    8335       103368 :       gfc_current_locus = old_loc;
    8336       103368 :       return MATCH_NO;
    8337              :     }
    8338              : 
    8339        20909 :   if (get_proc_name (name, &sym, false))
    8340              :     return MATCH_ERROR;
    8341              : 
    8342        20904 :   if (add_hidden_procptr_result (sym))
    8343           20 :     sym = sym->result;
    8344              : 
    8345        20904 :   if (current_attr.module_procedure)
    8346              :     {
    8347          362 :       sym->attr.module_procedure = 1;
    8348          362 :       if (gfc_current_state () == COMP_INTERFACE)
    8349          253 :         gfc_current_ns->has_import_set = 1;
    8350              :     }
    8351              : 
    8352        20904 :   gfc_new_block = sym;
    8353              : 
    8354        20904 :   m = gfc_match_formal_arglist (sym, 0, 0);
    8355        20904 :   if (m == MATCH_NO)
    8356              :     {
    8357            6 :       gfc_error ("Expected formal argument list in function "
    8358              :                  "definition at %C");
    8359            6 :       m = MATCH_ERROR;
    8360            6 :       goto cleanup;
    8361              :     }
    8362        20898 :   else if (m == MATCH_ERROR)
    8363            0 :     goto cleanup;
    8364              : 
    8365        20898 :   result = NULL;
    8366              : 
    8367              :   /* According to the draft, the bind(c) and result clause can
    8368              :      come in either order after the formal_arg_list (i.e., either
    8369              :      can be first, both can exist together or by themselves or neither
    8370              :      one).  Therefore, the match_result can't match the end of the
    8371              :      string, and check for the bind(c) or result clause in either order.  */
    8372        20898 :   found_match = gfc_match_eos ();
    8373              : 
    8374              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8375              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8376              :      not allowed for procedures.  */
    8377        20898 :   if (sym->attr.is_bind_c == 1)
    8378              :     {
    8379            5 :       sym->attr.is_bind_c = 0;
    8380              : 
    8381            5 :       if (gfc_state_stack->previous
    8382            5 :           && gfc_state_stack->previous->state != COMP_SUBMODULE)
    8383              :         {
    8384            1 :           locus loc;
    8385            1 :           loc = sym->old_symbol != NULL
    8386            1 :             ? sym->old_symbol->declared_at : gfc_current_locus;
    8387            1 :           gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8388              :                          "variables or common blocks", &loc);
    8389              :         }
    8390              :     }
    8391              : 
    8392        20898 :   if (found_match != MATCH_YES)
    8393              :     {
    8394              :       /* If we haven't found the end-of-statement, look for a suffix.  */
    8395         8882 :       suffix_match = gfc_match_suffix (sym, &result);
    8396         8882 :       if (suffix_match == MATCH_YES)
    8397              :         /* Need to get the eos now.  */
    8398         8874 :         found_match = gfc_match_eos ();
    8399              :       else
    8400              :         found_match = suffix_match;
    8401              :     }
    8402              : 
    8403              :   /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
    8404              :      subprogram and a binding label is specified, it shall be the
    8405              :      same as the binding label specified in the corresponding module
    8406              :      procedure interface body.  */
    8407        20898 :     if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
    8408            5 :         && strcmp (sym->name, sym->old_symbol->name) == 0
    8409            5 :         && sym->binding_label && sym->old_symbol->binding_label
    8410            4 :         && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
    8411              :       {
    8412            1 :           const char *null = "NULL", *s1, *s2;
    8413            1 :           s1 = sym->binding_label;
    8414            1 :           if (!s1) s1 = null;
    8415            1 :           s2 = sym->old_symbol->binding_label;
    8416            1 :           if (!s2) s2 = null;
    8417            1 :           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
    8418            1 :           sym->refs++;       /* Needed to avoid an ICE in gfc_release_symbol */
    8419            1 :           return MATCH_ERROR;
    8420              :       }
    8421              : 
    8422        20897 :   if(found_match != MATCH_YES)
    8423              :     m = MATCH_ERROR;
    8424              :   else
    8425              :     {
    8426              :       /* Make changes to the symbol.  */
    8427        20889 :       m = MATCH_ERROR;
    8428              : 
    8429        20889 :       if (!gfc_add_function (&sym->attr, sym->name, NULL))
    8430            0 :         goto cleanup;
    8431              : 
    8432        20889 :       if (!gfc_missing_attr (&sym->attr, NULL))
    8433            0 :         goto cleanup;
    8434              : 
    8435        20889 :       if (!copy_prefix (&sym->attr, &sym->declared_at))
    8436              :         {
    8437            1 :           if(!sym->attr.module_procedure)
    8438            1 :         goto cleanup;
    8439              :           else
    8440            0 :             gfc_error_check ();
    8441              :         }
    8442              : 
    8443              :       /* Delay matching the function characteristics until after the
    8444              :          specification block by signalling kind=-1.  */
    8445        20888 :       sym->declared_at = old_loc;
    8446        20888 :       if (current_ts.type != BT_UNKNOWN)
    8447              :         current_ts.kind = -1;
    8448              :       else
    8449        13667 :         current_ts.kind = 0;
    8450              : 
    8451        20888 :       if (result == NULL)
    8452              :         {
    8453        14644 :           if (current_ts.type != BT_UNKNOWN
    8454        14644 :               && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
    8455            1 :             goto cleanup;
    8456        14643 :           sym->result = sym;
    8457              :         }
    8458              :       else
    8459              :         {
    8460         6244 :           if (current_ts.type != BT_UNKNOWN
    8461         6244 :               && !gfc_add_type (result, &current_ts, &gfc_current_locus))
    8462            0 :             goto cleanup;
    8463         6244 :           sym->result = result;
    8464              :         }
    8465              : 
    8466              :       /* Warn if this procedure has the same name as an intrinsic.  */
    8467        20887 :       do_warn_intrinsic_shadow (sym, true);
    8468              : 
    8469        20887 :       return MATCH_YES;
    8470              :     }
    8471              : 
    8472           16 : cleanup:
    8473           16 :   gfc_current_locus = old_loc;
    8474           16 :   return m;
    8475              : }
    8476              : 
    8477              : 
    8478              : /* This is mostly a copy of parse.cc(add_global_procedure) but modified to
    8479              :    pass the name of the entry, rather than the gfc_current_block name, and
    8480              :    to return false upon finding an existing global entry.  */
    8481              : 
    8482              : static bool
    8483          545 : add_global_entry (const char *name, const char *binding_label, bool sub,
    8484              :                   locus *where)
    8485              : {
    8486          545 :   gfc_gsymbol *s;
    8487          545 :   enum gfc_symbol_type type;
    8488              : 
    8489          545 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    8490              : 
    8491              :   /* Only in Fortran 2003: For procedures with a binding label also the Fortran
    8492              :      name is a global identifier.  */
    8493          545 :   if (!binding_label || gfc_notification_std (GFC_STD_F2008))
    8494              :     {
    8495          522 :       s = gfc_get_gsymbol (name, false);
    8496              : 
    8497          522 :       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
    8498              :         {
    8499            2 :           gfc_global_used (s, where);
    8500            2 :           return false;
    8501              :         }
    8502              :       else
    8503              :         {
    8504          520 :           s->type = type;
    8505          520 :           s->sym_name = name;
    8506          520 :           s->where = *where;
    8507          520 :           s->defined = 1;
    8508          520 :           s->ns = gfc_current_ns;
    8509              :         }
    8510              :     }
    8511              : 
    8512              :   /* Don't add the symbol multiple times.  */
    8513          543 :   if (binding_label
    8514          543 :       && (!gfc_notification_std (GFC_STD_F2008)
    8515            0 :           || strcmp (name, binding_label) != 0))
    8516              :     {
    8517           23 :       s = gfc_get_gsymbol (binding_label, true);
    8518              : 
    8519           23 :       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
    8520              :         {
    8521            1 :           gfc_global_used (s, where);
    8522            1 :           return false;
    8523              :         }
    8524              :       else
    8525              :         {
    8526           22 :           s->type = type;
    8527           22 :           s->sym_name = gfc_get_string ("%s", name);
    8528           22 :           s->binding_label = binding_label;
    8529           22 :           s->where = *where;
    8530           22 :           s->defined = 1;
    8531           22 :           s->ns = gfc_current_ns;
    8532              :         }
    8533              :     }
    8534              : 
    8535              :   return true;
    8536              : }
    8537              : 
    8538              : 
    8539              : /* Match an ENTRY statement.  */
    8540              : 
    8541              : match
    8542          811 : gfc_match_entry (void)
    8543              : {
    8544          811 :   gfc_symbol *proc;
    8545          811 :   gfc_symbol *result;
    8546          811 :   gfc_symbol *entry;
    8547          811 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8548          811 :   gfc_compile_state state;
    8549          811 :   match m;
    8550          811 :   gfc_entry_list *el;
    8551          811 :   locus old_loc;
    8552          811 :   bool module_procedure;
    8553          811 :   char peek_char;
    8554          811 :   match is_bind_c;
    8555              : 
    8556          811 :   m = gfc_match_name (name);
    8557          811 :   if (m != MATCH_YES)
    8558              :     return m;
    8559              : 
    8560          811 :   if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
    8561              :     return MATCH_ERROR;
    8562              : 
    8563          811 :   state = gfc_current_state ();
    8564          811 :   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
    8565              :     {
    8566            3 :       switch (state)
    8567              :         {
    8568            0 :           case COMP_PROGRAM:
    8569            0 :             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
    8570            0 :             break;
    8571            0 :           case COMP_MODULE:
    8572            0 :             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
    8573            0 :             break;
    8574            0 :           case COMP_SUBMODULE:
    8575            0 :             gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
    8576            0 :             break;
    8577            0 :           case COMP_BLOCK_DATA:
    8578            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8579              :                        "a BLOCK DATA");
    8580            0 :             break;
    8581            0 :           case COMP_INTERFACE:
    8582            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8583              :                        "an INTERFACE");
    8584            0 :             break;
    8585            1 :           case COMP_STRUCTURE:
    8586            1 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8587              :                        "a STRUCTURE block");
    8588            1 :             break;
    8589            0 :           case COMP_DERIVED:
    8590            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8591              :                        "a DERIVED TYPE block");
    8592            0 :             break;
    8593            0 :           case COMP_IF:
    8594            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8595              :                        "an IF-THEN block");
    8596            0 :             break;
    8597            0 :           case COMP_DO:
    8598            0 :           case COMP_DO_CONCURRENT:
    8599            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8600              :                        "a DO block");
    8601            0 :             break;
    8602            0 :           case COMP_SELECT:
    8603            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8604              :                        "a SELECT block");
    8605            0 :             break;
    8606            0 :           case COMP_FORALL:
    8607            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8608              :                        "a FORALL block");
    8609            0 :             break;
    8610            0 :           case COMP_WHERE:
    8611            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8612              :                        "a WHERE block");
    8613            0 :             break;
    8614            0 :           case COMP_CONTAINS:
    8615            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8616              :                        "a contained subprogram");
    8617            0 :             break;
    8618            2 :           default:
    8619            2 :             gfc_error ("Unexpected ENTRY statement at %C");
    8620              :         }
    8621              :       return MATCH_ERROR;
    8622              :     }
    8623              : 
    8624          808 :   if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
    8625          808 :       && gfc_state_stack->previous->state == COMP_INTERFACE)
    8626              :     {
    8627            1 :       gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
    8628            1 :       return MATCH_ERROR;
    8629              :     }
    8630              : 
    8631         1614 :   module_procedure = gfc_current_ns->parent != NULL
    8632          260 :                    && gfc_current_ns->parent->proc_name
    8633          807 :                    && gfc_current_ns->parent->proc_name->attr.flavor
    8634          260 :                       == FL_MODULE;
    8635              : 
    8636          807 :   if (gfc_current_ns->parent != NULL
    8637          260 :       && gfc_current_ns->parent->proc_name
    8638          260 :       && !module_procedure)
    8639              :     {
    8640            0 :       gfc_error("ENTRY statement at %C cannot appear in a "
    8641              :                 "contained procedure");
    8642            0 :       return MATCH_ERROR;
    8643              :     }
    8644              : 
    8645              :   /* Module function entries need special care in get_proc_name
    8646              :      because previous references within the function will have
    8647              :      created symbols attached to the current namespace.  */
    8648         1354 :   if (get_proc_name (name, &entry,
    8649              :                      gfc_current_ns->parent != NULL
    8650              :                      && module_procedure))
    8651              :     return MATCH_ERROR;
    8652              : 
    8653          805 :   proc = gfc_current_block ();
    8654              : 
    8655              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8656              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8657              :      not allowed for procedures.  */
    8658          805 :   if (entry->attr.is_bind_c == 1)
    8659              :     {
    8660            0 :       locus loc;
    8661              : 
    8662            0 :       entry->attr.is_bind_c = 0;
    8663              : 
    8664            0 :       loc = entry->old_symbol != NULL
    8665            0 :         ? entry->old_symbol->declared_at : gfc_current_locus;
    8666            0 :       gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8667              :                      "variables or common blocks", &loc);
    8668              :      }
    8669              : 
    8670              :   /* Check what next non-whitespace character is so we can tell if there
    8671              :      is the required parens if we have a BIND(C).  */
    8672          805 :   old_loc = gfc_current_locus;
    8673          805 :   gfc_gobble_whitespace ();
    8674          805 :   peek_char = gfc_peek_ascii_char ();
    8675              : 
    8676          805 :   if (state == COMP_SUBROUTINE)
    8677              :     {
    8678          144 :       m = gfc_match_formal_arglist (entry, 0, 1);
    8679          144 :       if (m != MATCH_YES)
    8680              :         return MATCH_ERROR;
    8681              : 
    8682              :       /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
    8683              :          never be an internal procedure.  */
    8684          144 :       is_bind_c = gfc_match_bind_c (entry, true);
    8685          144 :       if (is_bind_c == MATCH_ERROR)
    8686              :         return MATCH_ERROR;
    8687          144 :       if (is_bind_c == MATCH_YES)
    8688              :         {
    8689           22 :           if (peek_char != '(')
    8690              :             {
    8691            0 :               gfc_error ("Missing required parentheses before BIND(C) at %C");
    8692            0 :               return MATCH_ERROR;
    8693              :             }
    8694              : 
    8695           22 :           if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
    8696           22 :                                   &(entry->declared_at), 1))
    8697              :             return MATCH_ERROR;
    8698              : 
    8699              :         }
    8700              : 
    8701          144 :       if (!gfc_current_ns->parent
    8702          144 :           && !add_global_entry (name, entry->binding_label, true,
    8703              :                                 &old_loc))
    8704              :         return MATCH_ERROR;
    8705              : 
    8706              :       /* An entry in a subroutine.  */
    8707          141 :       if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8708          141 :           || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
    8709              :         return MATCH_ERROR;
    8710              :     }
    8711              :   else
    8712              :     {
    8713              :       /* An entry in a function.
    8714              :          We need to take special care because writing
    8715              :             ENTRY f()
    8716              :          as
    8717              :             ENTRY f
    8718              :          is allowed, whereas
    8719              :             ENTRY f() RESULT (r)
    8720              :          can't be written as
    8721              :             ENTRY f RESULT (r).  */
    8722          661 :       if (gfc_match_eos () == MATCH_YES)
    8723              :         {
    8724           24 :           gfc_current_locus = old_loc;
    8725              :           /* Match the empty argument list, and add the interface to
    8726              :              the symbol.  */
    8727           24 :           m = gfc_match_formal_arglist (entry, 0, 1);
    8728              :         }
    8729              :       else
    8730          637 :         m = gfc_match_formal_arglist (entry, 0, 0);
    8731              : 
    8732          661 :       if (m != MATCH_YES)
    8733              :         return MATCH_ERROR;
    8734              : 
    8735          660 :       result = NULL;
    8736              : 
    8737          660 :       if (gfc_match_eos () == MATCH_YES)
    8738              :         {
    8739          411 :           if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8740          411 :               || !gfc_add_function (&entry->attr, entry->name, NULL))
    8741              :             return MATCH_ERROR;
    8742              : 
    8743          409 :           entry->result = entry;
    8744              :         }
    8745              :       else
    8746              :         {
    8747          249 :           m = gfc_match_suffix (entry, &result);
    8748          249 :           if (m == MATCH_NO)
    8749            0 :             gfc_syntax_error (ST_ENTRY);
    8750          249 :           if (m != MATCH_YES)
    8751              :             return MATCH_ERROR;
    8752              : 
    8753          249 :           if (result)
    8754              :             {
    8755          212 :               if (!gfc_add_result (&result->attr, result->name, NULL)
    8756          212 :                   || !gfc_add_entry (&entry->attr, result->name, NULL)
    8757          424 :                   || !gfc_add_function (&entry->attr, result->name, NULL))
    8758              :                 return MATCH_ERROR;
    8759          212 :               entry->result = result;
    8760              :             }
    8761              :           else
    8762              :             {
    8763           37 :               if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8764           37 :                   || !gfc_add_function (&entry->attr, entry->name, NULL))
    8765              :                 return MATCH_ERROR;
    8766           37 :               entry->result = entry;
    8767              :             }
    8768              :         }
    8769              : 
    8770          658 :       if (!gfc_current_ns->parent
    8771          658 :           && !add_global_entry (name, entry->binding_label, false,
    8772              :                                 &old_loc))
    8773              :         return MATCH_ERROR;
    8774              :     }
    8775              : 
    8776          796 :   if (gfc_match_eos () != MATCH_YES)
    8777              :     {
    8778            0 :       gfc_syntax_error (ST_ENTRY);
    8779            0 :       return MATCH_ERROR;
    8780              :     }
    8781              : 
    8782              :   /* F2018:C1546 An elemental procedure shall not have the BIND attribute.  */
    8783          796 :   if (proc->attr.elemental && entry->attr.is_bind_c)
    8784              :     {
    8785            2 :       gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
    8786              :                  "elemental procedure", &entry->declared_at);
    8787            2 :       return MATCH_ERROR;
    8788              :     }
    8789              : 
    8790          794 :   entry->attr.recursive = proc->attr.recursive;
    8791          794 :   entry->attr.elemental = proc->attr.elemental;
    8792          794 :   entry->attr.pure = proc->attr.pure;
    8793              : 
    8794          794 :   el = gfc_get_entry_list ();
    8795          794 :   el->sym = entry;
    8796          794 :   el->next = gfc_current_ns->entries;
    8797          794 :   gfc_current_ns->entries = el;
    8798          794 :   if (el->next)
    8799           85 :     el->id = el->next->id + 1;
    8800              :   else
    8801              :     el->id = 1;
    8802              : 
    8803          794 :   new_st.op = EXEC_ENTRY;
    8804          794 :   new_st.ext.entry = el;
    8805              : 
    8806          794 :   return MATCH_YES;
    8807              : }
    8808              : 
    8809              : 
    8810              : /* Match a subroutine statement, including optional prefixes.  */
    8811              : 
    8812              : match
    8813       838377 : gfc_match_subroutine (void)
    8814              : {
    8815       838377 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8816       838377 :   gfc_symbol *sym;
    8817       838377 :   match m;
    8818       838377 :   match is_bind_c;
    8819       838377 :   char peek_char;
    8820       838377 :   bool allow_binding_name;
    8821       838377 :   locus loc;
    8822              : 
    8823       838377 :   if (gfc_current_state () != COMP_NONE
    8824       794889 :       && gfc_current_state () != COMP_INTERFACE
    8825       771572 :       && gfc_current_state () != COMP_CONTAINS)
    8826              :     return MATCH_NO;
    8827              : 
    8828       110938 :   m = gfc_match_prefix (NULL);
    8829       110938 :   if (m != MATCH_YES)
    8830              :     return m;
    8831              : 
    8832       100475 :   loc = gfc_current_locus;
    8833       100475 :   m = gfc_match ("subroutine% %n", name);
    8834       100475 :   if (m != MATCH_YES)
    8835              :     return m;
    8836              : 
    8837        45035 :   if (get_proc_name (name, &sym, false))
    8838              :     return MATCH_ERROR;
    8839              : 
    8840              :   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
    8841              :      the symbol existed before.  */
    8842        45024 :   sym->declared_at = gfc_get_location_range (NULL, 0, &loc, 1,
    8843              :                                              &gfc_current_locus);
    8844              : 
    8845        45024 :   if (current_attr.module_procedure)
    8846              :     {
    8847          431 :       sym->attr.module_procedure = 1;
    8848          431 :       if (gfc_current_state () == COMP_INTERFACE)
    8849          303 :         gfc_current_ns->has_import_set = 1;
    8850              :     }
    8851              : 
    8852        45024 :   if (add_hidden_procptr_result (sym))
    8853            9 :     sym = sym->result;
    8854              : 
    8855        45024 :   gfc_new_block = sym;
    8856              : 
    8857              :   /* Check what next non-whitespace character is so we can tell if there
    8858              :      is the required parens if we have a BIND(C).  */
    8859        45024 :   gfc_gobble_whitespace ();
    8860        45024 :   peek_char = gfc_peek_ascii_char ();
    8861              : 
    8862        45024 :   if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
    8863              :     return MATCH_ERROR;
    8864              : 
    8865        45021 :   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
    8866              :     return MATCH_ERROR;
    8867              : 
    8868              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8869              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8870              :      not allowed for procedures.  */
    8871        45021 :   if (sym->attr.is_bind_c == 1)
    8872              :     {
    8873            4 :       sym->attr.is_bind_c = 0;
    8874              : 
    8875            4 :       if (gfc_state_stack->previous
    8876            4 :           && gfc_state_stack->previous->state != COMP_SUBMODULE)
    8877              :         {
    8878            2 :           locus loc;
    8879            2 :           loc = sym->old_symbol != NULL
    8880            2 :             ? sym->old_symbol->declared_at : gfc_current_locus;
    8881            2 :           gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8882              :                          "variables or common blocks", &loc);
    8883              :         }
    8884              :     }
    8885              : 
    8886              :   /* C binding names are not allowed for internal procedures.  */
    8887        45021 :   if (gfc_current_state () == COMP_CONTAINS
    8888        27747 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    8889              :     allow_binding_name = false;
    8890              :   else
    8891        29303 :     allow_binding_name = true;
    8892              : 
    8893              :   /* Here, we are just checking if it has the bind(c) attribute, and if
    8894              :      so, then we need to make sure it's all correct.  If it doesn't,
    8895              :      we still need to continue matching the rest of the subroutine line.  */
    8896        45021 :   gfc_gobble_whitespace ();
    8897        45021 :   loc = gfc_current_locus;
    8898        45021 :   is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    8899        45021 :   if (is_bind_c == MATCH_ERROR)
    8900              :     {
    8901              :       /* There was an attempt at the bind(c), but it was wrong.  An
    8902              :          error message should have been printed w/in the gfc_match_bind_c
    8903              :          so here we'll just return the MATCH_ERROR.  */
    8904              :       return MATCH_ERROR;
    8905              :     }
    8906              : 
    8907        45008 :   if (is_bind_c == MATCH_YES)
    8908              :     {
    8909         4169 :       gfc_formal_arglist *arg;
    8910              : 
    8911              :       /* The following is allowed in the Fortran 2008 draft.  */
    8912         4169 :       if (gfc_current_state () == COMP_CONTAINS
    8913         1301 :           && sym->ns->proc_name->attr.flavor != FL_MODULE
    8914         4580 :           && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
    8915              :                               "at %L may not be specified for an internal "
    8916              :                               "procedure", &gfc_current_locus))
    8917              :         return MATCH_ERROR;
    8918              : 
    8919         4166 :       if (peek_char != '(')
    8920              :         {
    8921            1 :           gfc_error ("Missing required parentheses before BIND(C) at %C");
    8922            1 :           return MATCH_ERROR;
    8923              :         }
    8924              : 
    8925              :       /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
    8926              :          subprogram and a binding label is specified, it shall be the
    8927              :          same as the binding label specified in the corresponding module
    8928              :          procedure interface body.  */
    8929         4165 :       if (sym->attr.module_procedure && sym->old_symbol
    8930            3 :           && strcmp (sym->name, sym->old_symbol->name) == 0
    8931            3 :           && sym->binding_label && sym->old_symbol->binding_label
    8932            2 :           && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
    8933              :         {
    8934            1 :           const char *null = "NULL", *s1, *s2;
    8935            1 :           s1 = sym->binding_label;
    8936            1 :           if (!s1) s1 = null;
    8937            1 :           s2 = sym->old_symbol->binding_label;
    8938            1 :           if (!s2) s2 = null;
    8939            1 :           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
    8940            1 :           sym->refs++;       /* Needed to avoid an ICE in gfc_release_symbol */
    8941            1 :           return MATCH_ERROR;
    8942              :         }
    8943              : 
    8944              :       /* Scan the dummy arguments for an alternate return.  */
    8945        12881 :       for (arg = sym->formal; arg; arg = arg->next)
    8946         8718 :         if (!arg->sym)
    8947              :           {
    8948            1 :             gfc_error ("Alternate return dummy argument cannot appear in a "
    8949              :                        "SUBROUTINE with the BIND(C) attribute at %L", &loc);
    8950            1 :             return MATCH_ERROR;
    8951              :           }
    8952              : 
    8953         4163 :       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
    8954              :         return MATCH_ERROR;
    8955              :     }
    8956              : 
    8957        45001 :   if (gfc_match_eos () != MATCH_YES)
    8958              :     {
    8959            1 :       gfc_syntax_error (ST_SUBROUTINE);
    8960            1 :       return MATCH_ERROR;
    8961              :     }
    8962              : 
    8963        45000 :   if (!copy_prefix (&sym->attr, &sym->declared_at))
    8964              :     {
    8965            4 :       if(!sym->attr.module_procedure)
    8966              :         return MATCH_ERROR;
    8967              :       else
    8968            3 :         gfc_error_check ();
    8969              :     }
    8970              : 
    8971              :   /* Warn if it has the same name as an intrinsic.  */
    8972        44999 :   do_warn_intrinsic_shadow (sym, false);
    8973              : 
    8974        44999 :   return MATCH_YES;
    8975              : }
    8976              : 
    8977              : 
    8978              : /* Check that the NAME identifier in a BIND attribute or statement
    8979              :    is conform to C identifier rules.  */
    8980              : 
    8981              : match
    8982         1193 : check_bind_name_identifier (char **name)
    8983              : {
    8984         1193 :   char *n = *name, *p;
    8985              : 
    8986              :   /* Remove leading spaces.  */
    8987         1219 :   while (*n == ' ')
    8988           26 :     n++;
    8989              : 
    8990              :   /* On an empty string, free memory and set name to NULL.  */
    8991         1193 :   if (*n == '\0')
    8992              :     {
    8993           42 :       free (*name);
    8994           42 :       *name = NULL;
    8995           42 :       return MATCH_YES;
    8996              :     }
    8997              : 
    8998              :   /* Remove trailing spaces.  */
    8999         1151 :   p = n + strlen(n) - 1;
    9000         1167 :   while (*p == ' ')
    9001           16 :     *(p--) = '\0';
    9002              : 
    9003              :   /* Insert the identifier into the symbol table.  */
    9004         1151 :   p = xstrdup (n);
    9005         1151 :   free (*name);
    9006         1151 :   *name = p;
    9007              : 
    9008              :   /* Now check that identifier is valid under C rules.  */
    9009         1151 :   if (ISDIGIT (*p))
    9010              :     {
    9011            2 :       gfc_error ("Invalid C identifier in NAME= specifier at %C");
    9012            2 :       return MATCH_ERROR;
    9013              :     }
    9014              : 
    9015        12572 :   for (; *p; p++)
    9016        11426 :     if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
    9017              :       {
    9018            3 :         gfc_error ("Invalid C identifier in NAME= specifier at %C");
    9019            3 :         return MATCH_ERROR;
    9020              :       }
    9021              : 
    9022              :   return MATCH_YES;
    9023              : }
    9024              : 
    9025              : 
    9026              : /* Match a BIND(C) specifier, with the optional 'name=' specifier if
    9027              :    given, and set the binding label in either the given symbol (if not
    9028              :    NULL), or in the current_ts.  The symbol may be NULL because we may
    9029              :    encounter the BIND(C) before the declaration itself.  Return
    9030              :    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
    9031              :    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
    9032              :    or MATCH_YES if the specifier was correct and the binding label and
    9033              :    bind(c) fields were set correctly for the given symbol or the
    9034              :    current_ts. If allow_binding_name is false, no binding name may be
    9035              :    given.  */
    9036              : 
    9037              : match
    9038        54599 : gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
    9039              : {
    9040        54599 :   char *binding_label = NULL;
    9041        54599 :   gfc_expr *e = NULL;
    9042              : 
    9043              :   /* Initialize the flag that specifies whether we encountered a NAME=
    9044              :      specifier or not.  */
    9045        54599 :   has_name_equals = 0;
    9046              : 
    9047              :   /* This much we have to be able to match, in this order, if
    9048              :      there is a bind(c) label.  */
    9049        54599 :   if (gfc_match (" bind ( c ") != MATCH_YES)
    9050              :     return MATCH_NO;
    9051              : 
    9052              :   /* Now see if there is a binding label, or if we've reached the
    9053              :      end of the bind(c) attribute without one.  */
    9054         7698 :   if (gfc_match_char (',') == MATCH_YES)
    9055              :     {
    9056         1200 :       if (gfc_match (" name = ") != MATCH_YES)
    9057              :         {
    9058            1 :           gfc_error ("Syntax error in NAME= specifier for binding label "
    9059              :                      "at %C");
    9060              :           /* should give an error message here */
    9061            1 :           return MATCH_ERROR;
    9062              :         }
    9063              : 
    9064         1199 :       has_name_equals = 1;
    9065              : 
    9066         1199 :       if (gfc_match_init_expr (&e) != MATCH_YES)
    9067              :         {
    9068            2 :           gfc_free_expr (e);
    9069            2 :           return MATCH_ERROR;
    9070              :         }
    9071              : 
    9072         1197 :       if (!gfc_simplify_expr(e, 0))
    9073              :         {
    9074            0 :           gfc_error ("NAME= specifier at %C should be a constant expression");
    9075            0 :           gfc_free_expr (e);
    9076            0 :           return MATCH_ERROR;
    9077              :         }
    9078              : 
    9079         1197 :       if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
    9080         1194 :           || e->ts.kind != gfc_default_character_kind || e->rank != 0)
    9081              :         {
    9082            4 :           gfc_error ("NAME= specifier at %C should be a scalar of "
    9083              :                      "default character kind");
    9084            4 :           gfc_free_expr(e);
    9085            4 :           return MATCH_ERROR;
    9086              :         }
    9087              : 
    9088              :       // Get a C string from the Fortran string constant
    9089         2386 :       binding_label = gfc_widechar_to_char (e->value.character.string,
    9090         1193 :                                             e->value.character.length);
    9091         1193 :       gfc_free_expr(e);
    9092              : 
    9093              :       // Check that it is valid (old gfc_match_name_C)
    9094         1193 :       if (check_bind_name_identifier (&binding_label) != MATCH_YES)
    9095              :         return MATCH_ERROR;
    9096              :     }
    9097              : 
    9098              :   /* Get the required right paren.  */
    9099         7686 :   if (gfc_match_char (')') != MATCH_YES)
    9100              :     {
    9101            1 :       gfc_error ("Missing closing paren for binding label at %C");
    9102            1 :       return MATCH_ERROR;
    9103              :     }
    9104              : 
    9105         7685 :   if (has_name_equals && !allow_binding_name)
    9106              :     {
    9107            6 :       gfc_error ("No binding name is allowed in BIND(C) at %C");
    9108            6 :       return MATCH_ERROR;
    9109              :     }
    9110              : 
    9111         7679 :   if (has_name_equals && sym != NULL && sym->attr.dummy)
    9112              :     {
    9113            2 :       gfc_error ("For dummy procedure %s, no binding name is "
    9114              :                  "allowed in BIND(C) at %C", sym->name);
    9115            2 :       return MATCH_ERROR;
    9116              :     }
    9117              : 
    9118              : 
    9119              :   /* Save the binding label to the symbol.  If sym is null, we're
    9120              :      probably matching the typespec attributes of a declaration and
    9121              :      haven't gotten the name yet, and therefore, no symbol yet.  */
    9122         7677 :   if (binding_label)
    9123              :     {
    9124         1139 :       if (sym != NULL)
    9125         1029 :         sym->binding_label = binding_label;
    9126              :       else
    9127          110 :         curr_binding_label = binding_label;
    9128              :     }
    9129         6538 :   else if (allow_binding_name)
    9130              :     {
    9131              :       /* No binding label, but if symbol isn't null, we
    9132              :          can set the label for it here.
    9133              :          If name="" or allow_binding_name is false, no C binding name is
    9134              :          created.  */
    9135         6109 :       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
    9136         5942 :         sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
    9137              :     }
    9138              : 
    9139         7677 :   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
    9140          747 :       && current_interface.type == INTERFACE_ABSTRACT)
    9141              :     {
    9142            1 :       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
    9143            1 :       return MATCH_ERROR;
    9144              :     }
    9145              : 
    9146              :   return MATCH_YES;
    9147              : }
    9148              : 
    9149              : 
    9150              : /* Return nonzero if we're currently compiling a contained procedure.  */
    9151              : 
    9152              : static int
    9153        66231 : contained_procedure (void)
    9154              : {
    9155        66231 :   gfc_state_data *s = gfc_state_stack;
    9156              : 
    9157        66231 :   if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
    9158        65241 :       && s->previous != NULL && s->previous->state == COMP_CONTAINS)
    9159        38744 :     return 1;
    9160              : 
    9161              :   return 0;
    9162              : }
    9163              : 
    9164              : /* Set the kind of each enumerator.  The kind is selected such that it is
    9165              :    interoperable with the corresponding C enumeration type, making
    9166              :    sure that -fshort-enums is honored.  */
    9167              : 
    9168              : static void
    9169          158 : set_enum_kind(void)
    9170              : {
    9171          158 :   enumerator_history *current_history = NULL;
    9172          158 :   int kind;
    9173          158 :   int i;
    9174              : 
    9175          158 :   if (max_enum == NULL || enum_history == NULL)
    9176              :     return;
    9177              : 
    9178          150 :   if (!flag_short_enums)
    9179              :     return;
    9180              : 
    9181              :   i = 0;
    9182           48 :   do
    9183              :     {
    9184           48 :       kind = gfc_integer_kinds[i++].kind;
    9185              :     }
    9186           48 :   while (kind < gfc_c_int_kind
    9187           72 :          && gfc_check_integer_range (max_enum->initializer->value.integer,
    9188              :                                      kind) != ARITH_OK);
    9189              : 
    9190           24 :   current_history = enum_history;
    9191           96 :   while (current_history != NULL)
    9192              :     {
    9193           72 :       current_history->sym->ts.kind = kind;
    9194           72 :       current_history = current_history->next;
    9195              :     }
    9196              : }
    9197              : 
    9198              : 
    9199              : /* Match any of the various end-block statements.  Returns the type of
    9200              :    END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
    9201              :    and END BLOCK statements cannot be replaced by a single END statement.  */
    9202              : 
    9203              : match
    9204       194585 : gfc_match_end (gfc_statement *st)
    9205              : {
    9206       194585 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    9207       194585 :   gfc_compile_state state;
    9208       194585 :   locus old_loc;
    9209       194585 :   const char *block_name;
    9210       194585 :   const char *target;
    9211       194585 :   int eos_ok;
    9212       194585 :   match m;
    9213       194585 :   gfc_namespace *parent_ns, *ns, *prev_ns;
    9214       194585 :   gfc_namespace **nsp;
    9215       194585 :   bool abbreviated_modproc_decl = false;
    9216       194585 :   bool got_matching_end = false;
    9217              : 
    9218       194585 :   old_loc = gfc_current_locus;
    9219       194585 :   if (gfc_match ("end") != MATCH_YES)
    9220              :     return MATCH_NO;
    9221              : 
    9222       189432 :   state = gfc_current_state ();
    9223       104271 :   block_name = gfc_current_block () == NULL
    9224       189432 :              ? NULL : gfc_current_block ()->name;
    9225              : 
    9226       189432 :   switch (state)
    9227              :     {
    9228         3484 :     case COMP_ASSOCIATE:
    9229         3484 :     case COMP_BLOCK:
    9230         3484 :     case COMP_CHANGE_TEAM:
    9231         3484 :       if (startswith (block_name, "block@"))
    9232              :         block_name = NULL;
    9233              :       break;
    9234              : 
    9235        18485 :     case COMP_CONTAINS:
    9236        18485 :     case COMP_DERIVED_CONTAINS:
    9237        18485 :     case COMP_OMP_BEGIN_METADIRECTIVE:
    9238        18485 :       state = gfc_state_stack->previous->state;
    9239        16905 :       block_name = gfc_state_stack->previous->sym == NULL
    9240        18485 :                  ? NULL : gfc_state_stack->previous->sym->name;
    9241        18485 :       abbreviated_modproc_decl = gfc_state_stack->previous->sym
    9242        18485 :                 && gfc_state_stack->previous->sym->abr_modproc_decl;
    9243              :       break;
    9244              : 
    9245              :     case COMP_OMP_METADIRECTIVE:
    9246              :       {
    9247              :         /* Metadirectives can be nested, so we need to drill down to the
    9248              :            first state that is not COMP_OMP_METADIRECTIVE.  */
    9249              :         gfc_state_data *state_data = gfc_state_stack;
    9250              : 
    9251           93 :         do
    9252              :           {
    9253           93 :             state_data = state_data->previous;
    9254           93 :             state = state_data->state;
    9255           81 :             block_name = (state_data->sym == NULL
    9256           93 :                           ? NULL : state_data->sym->name);
    9257          186 :             abbreviated_modproc_decl = (state_data->sym
    9258           93 :                                         && state_data->sym->abr_modproc_decl);
    9259              :           }
    9260           93 :         while (state == COMP_OMP_METADIRECTIVE);
    9261              : 
    9262           87 :         if (block_name && startswith (block_name, "block@"))
    9263              :           block_name = NULL;
    9264              :       }
    9265              :       break;
    9266              : 
    9267              :     default:
    9268              :       break;
    9269              :     }
    9270              : 
    9271           87 :   if (!abbreviated_modproc_decl)
    9272       189431 :     abbreviated_modproc_decl = gfc_current_block ()
    9273       189431 :                               && gfc_current_block ()->abr_modproc_decl;
    9274              : 
    9275       189432 :   switch (state)
    9276              :     {
    9277        29025 :     case COMP_NONE:
    9278        29025 :     case COMP_PROGRAM:
    9279        29025 :       *st = ST_END_PROGRAM;
    9280        29025 :       target = " program";
    9281        29025 :       eos_ok = 1;
    9282        29025 :       break;
    9283              : 
    9284        45192 :     case COMP_SUBROUTINE:
    9285        45192 :       *st = ST_END_SUBROUTINE;
    9286        45192 :       if (!abbreviated_modproc_decl)
    9287              :         target = " subroutine";
    9288              :       else
    9289          148 :         target = " procedure";
    9290        45192 :       eos_ok = !contained_procedure ();
    9291        45192 :       break;
    9292              : 
    9293        21039 :     case COMP_FUNCTION:
    9294        21039 :       *st = ST_END_FUNCTION;
    9295        21039 :       if (!abbreviated_modproc_decl)
    9296              :         target = " function";
    9297              :       else
    9298          135 :         target = " procedure";
    9299        21039 :       eos_ok = !contained_procedure ();
    9300        21039 :       break;
    9301              : 
    9302           87 :     case COMP_BLOCK_DATA:
    9303           87 :       *st = ST_END_BLOCK_DATA;
    9304           87 :       target = " block data";
    9305           87 :       eos_ok = 1;
    9306           87 :       break;
    9307              : 
    9308        10455 :     case COMP_MODULE:
    9309        10455 :       *st = ST_END_MODULE;
    9310        10455 :       target = " module";
    9311        10455 :       eos_ok = 1;
    9312        10455 :       break;
    9313              : 
    9314          276 :     case COMP_SUBMODULE:
    9315          276 :       *st = ST_END_SUBMODULE;
    9316          276 :       target = " submodule";
    9317          276 :       eos_ok = 1;
    9318          276 :       break;
    9319              : 
    9320        11694 :     case COMP_INTERFACE:
    9321        11694 :       *st = ST_END_INTERFACE;
    9322        11694 :       target = " interface";
    9323        11694 :       eos_ok = 0;
    9324        11694 :       break;
    9325              : 
    9326          257 :     case COMP_MAP:
    9327          257 :       *st = ST_END_MAP;
    9328          257 :       target = " map";
    9329          257 :       eos_ok = 0;
    9330          257 :       break;
    9331              : 
    9332          132 :     case COMP_UNION:
    9333          132 :       *st = ST_END_UNION;
    9334          132 :       target = " union";
    9335          132 :       eos_ok = 0;
    9336          132 :       break;
    9337              : 
    9338          313 :     case COMP_STRUCTURE:
    9339          313 :       *st = ST_END_STRUCTURE;
    9340          313 :       target = " structure";
    9341          313 :       eos_ok = 0;
    9342          313 :       break;
    9343              : 
    9344        14304 :     case COMP_DERIVED:
    9345        14304 :     case COMP_DERIVED_CONTAINS:
    9346        14304 :       *st = ST_END_TYPE;
    9347        14304 :       target = " type";
    9348        14304 :       eos_ok = 0;
    9349        14304 :       break;
    9350              : 
    9351         1815 :     case COMP_ASSOCIATE:
    9352         1815 :       *st = ST_END_ASSOCIATE;
    9353         1815 :       target = " associate";
    9354         1815 :       eos_ok = 0;
    9355         1815 :       break;
    9356              : 
    9357         1567 :     case COMP_BLOCK:
    9358         1567 :     case COMP_OMP_STRICTLY_STRUCTURED_BLOCK:
    9359         1567 :       *st = ST_END_BLOCK;
    9360         1567 :       target = " block";
    9361         1567 :       eos_ok = 0;
    9362         1567 :       break;
    9363              : 
    9364        15190 :     case COMP_IF:
    9365        15190 :       *st = ST_ENDIF;
    9366        15190 :       target = " if";
    9367        15190 :       eos_ok = 0;
    9368        15190 :       break;
    9369              : 
    9370        31889 :     case COMP_DO:
    9371        31889 :     case COMP_DO_CONCURRENT:
    9372        31889 :       *st = ST_ENDDO;
    9373        31889 :       target = " do";
    9374        31889 :       eos_ok = 0;
    9375        31889 :       break;
    9376              : 
    9377           54 :     case COMP_CRITICAL:
    9378           54 :       *st = ST_END_CRITICAL;
    9379           54 :       target = " critical";
    9380           54 :       eos_ok = 0;
    9381           54 :       break;
    9382              : 
    9383         4962 :     case COMP_SELECT:
    9384         4962 :     case COMP_SELECT_TYPE:
    9385         4962 :     case COMP_SELECT_RANK:
    9386         4962 :       *st = ST_END_SELECT;
    9387         4962 :       target = " select";
    9388         4962 :       eos_ok = 0;
    9389         4962 :       break;
    9390              : 
    9391          509 :     case COMP_FORALL:
    9392          509 :       *st = ST_END_FORALL;
    9393          509 :       target = " forall";
    9394          509 :       eos_ok = 0;
    9395          509 :       break;
    9396              : 
    9397          373 :     case COMP_WHERE:
    9398          373 :       *st = ST_END_WHERE;
    9399          373 :       target = " where";
    9400          373 :       eos_ok = 0;
    9401          373 :       break;
    9402              : 
    9403          158 :     case COMP_ENUM:
    9404          158 :       *st = ST_END_ENUM;
    9405          158 :       target = " enum";
    9406          158 :       eos_ok = 0;
    9407          158 :       last_initializer = NULL;
    9408          158 :       set_enum_kind ();
    9409          158 :       gfc_free_enum_history ();
    9410          158 :       break;
    9411              : 
    9412            0 :     case COMP_OMP_BEGIN_METADIRECTIVE:
    9413            0 :       *st = ST_OMP_END_METADIRECTIVE;
    9414            0 :       target = " metadirective";
    9415            0 :       eos_ok = 0;
    9416            0 :       break;
    9417              : 
    9418          132 :     case COMP_CHANGE_TEAM:
    9419          132 :       *st = ST_END_TEAM;
    9420          132 :       target = " team";
    9421          132 :       eos_ok = 0;
    9422          132 :       break;
    9423              : 
    9424            9 :     default:
    9425            9 :       gfc_error ("Unexpected END statement at %C");
    9426            9 :       goto cleanup;
    9427              :     }
    9428              : 
    9429       189423 :   old_loc = gfc_current_locus;
    9430       189423 :   if (gfc_match_eos () == MATCH_YES)
    9431              :     {
    9432        21003 :       if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
    9433              :         {
    9434         8331 :           if (!gfc_notify_std (GFC_STD_F2008, "END statement "
    9435              :                                "instead of %s statement at %L",
    9436              :                                abbreviated_modproc_decl ? "END PROCEDURE"
    9437         4153 :                                : gfc_ascii_statement(*st), &old_loc))
    9438            4 :             goto cleanup;
    9439              :         }
    9440            9 :       else if (!eos_ok)
    9441              :         {
    9442              :           /* We would have required END [something].  */
    9443            9 :           gfc_error ("%s statement expected at %L",
    9444              :                      gfc_ascii_statement (*st), &old_loc);
    9445            9 :           goto cleanup;
    9446              :         }
    9447              : 
    9448              :       return MATCH_YES;
    9449              :     }
    9450              : 
    9451              :   /* Verify that we've got the sort of end-block that we're expecting.  */
    9452       168420 :   if (gfc_match (target) != MATCH_YES)
    9453              :     {
    9454          331 :       gfc_error ("Expecting %s statement at %L", abbreviated_modproc_decl
    9455          165 :                  ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
    9456          166 :       goto cleanup;
    9457              :     }
    9458              :   else
    9459       168254 :     got_matching_end = true;
    9460              : 
    9461       168254 :   if (*st == ST_END_TEAM && gfc_match_end_team () == MATCH_ERROR)
    9462              :     /* Emit errors of stat and errmsg parsing now to finish the block and
    9463              :        continue analysis of compilation unit.  */
    9464            2 :     gfc_error_check ();
    9465              : 
    9466       168254 :   old_loc = gfc_current_locus;
    9467              :   /* If we're at the end, make sure a block name wasn't required.  */
    9468       168254 :   if (gfc_match_eos () == MATCH_YES)
    9469              :     {
    9470       112177 :       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
    9471              :           && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
    9472              :           && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL
    9473              :           && *st != ST_END_TEAM)
    9474              :         return MATCH_YES;
    9475              : 
    9476        55992 :       if (!block_name)
    9477              :         return MATCH_YES;
    9478              : 
    9479            8 :       gfc_error ("Expected block name of %qs in %s statement at %L",
    9480              :                  block_name, gfc_ascii_statement (*st), &old_loc);
    9481              : 
    9482            8 :       return MATCH_ERROR;
    9483              :     }
    9484              : 
    9485              :   /* END INTERFACE has a special handler for its several possible endings.  */
    9486        56077 :   if (*st == ST_END_INTERFACE)
    9487          697 :     return gfc_match_end_interface ();
    9488              : 
    9489              :   /* We haven't hit the end of statement, so what is left must be an
    9490              :      end-name.  */
    9491        55380 :   m = gfc_match_space ();
    9492        55380 :   if (m == MATCH_YES)
    9493        55380 :     m = gfc_match_name (name);
    9494              : 
    9495        55380 :   if (m == MATCH_NO)
    9496            0 :     gfc_error ("Expected terminating name at %C");
    9497        55380 :   if (m != MATCH_YES)
    9498            0 :     goto cleanup;
    9499              : 
    9500        55380 :   if (block_name == NULL)
    9501           15 :     goto syntax;
    9502              : 
    9503              :   /* We have to pick out the declared submodule name from the composite
    9504              :      required by F2008:11.2.3 para 2, which ends in the declared name.  */
    9505        55365 :   if (state == COMP_SUBMODULE)
    9506          138 :     block_name = strchr (block_name, '.') + 1;
    9507              : 
    9508        55365 :   if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
    9509              :     {
    9510            8 :       gfc_error ("Expected label %qs for %s statement at %C", block_name,
    9511              :                  gfc_ascii_statement (*st));
    9512            8 :       goto cleanup;
    9513              :     }
    9514              :   /* Procedure pointer as function result.  */
    9515        55357 :   else if (strcmp (block_name, "ppr@") == 0
    9516           21 :            && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
    9517              :     {
    9518            0 :       gfc_error ("Expected label %qs for %s statement at %C",
    9519            0 :                  gfc_current_block ()->ns->proc_name->name,
    9520              :                  gfc_ascii_statement (*st));
    9521            0 :       goto cleanup;
    9522              :     }
    9523              : 
    9524        55357 :   if (gfc_match_eos () == MATCH_YES)
    9525              :     return MATCH_YES;
    9526              : 
    9527            0 : syntax:
    9528           15 :   gfc_syntax_error (*st);
    9529              : 
    9530          211 : cleanup:
    9531          211 :   gfc_current_locus = old_loc;
    9532              : 
    9533              :   /* If we are missing an END BLOCK, we created a half-ready namespace.
    9534              :      Remove it from the parent namespace's sibling list.  */
    9535              : 
    9536          211 :   if (state == COMP_BLOCK && !got_matching_end)
    9537              :     {
    9538            7 :       parent_ns = gfc_current_ns->parent;
    9539              : 
    9540            7 :       nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
    9541              : 
    9542            7 :       prev_ns = NULL;
    9543            7 :       ns = *nsp;
    9544           14 :       while (ns)
    9545              :         {
    9546            7 :           if (ns == gfc_current_ns)
    9547              :             {
    9548            7 :               if (prev_ns == NULL)
    9549            7 :                 *nsp = NULL;
    9550              :               else
    9551            0 :                 prev_ns->sibling = ns->sibling;
    9552              :             }
    9553            7 :           prev_ns = ns;
    9554            7 :           ns = ns->sibling;
    9555              :         }
    9556              : 
    9557              :       /* The namespace can still be referenced by parser state and code nodes;
    9558              :          let normal block unwinding/freeing own its lifetime.  */
    9559            7 :       gfc_current_ns = parent_ns;
    9560            7 :       gfc_state_stack = gfc_state_stack->previous;
    9561            7 :       state = gfc_current_state ();
    9562              :     }
    9563              : 
    9564              :   return MATCH_ERROR;
    9565              : }
    9566              : 
    9567              : 
    9568              : 
    9569              : /***************** Attribute declaration statements ****************/
    9570              : 
    9571              : /* Set the attribute of a single variable.  */
    9572              : 
    9573              : static match
    9574        10480 : attr_decl1 (void)
    9575              : {
    9576        10480 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    9577        10480 :   gfc_array_spec *as;
    9578              : 
    9579              :   /* Workaround -Wmaybe-uninitialized false positive during
    9580              :      profiledbootstrap by initializing them.  */
    9581        10480 :   gfc_symbol *sym = NULL;
    9582        10480 :   locus var_locus;
    9583        10480 :   match m;
    9584              : 
    9585        10480 :   as = NULL;
    9586              : 
    9587        10480 :   m = gfc_match_name (name);
    9588        10480 :   if (m != MATCH_YES)
    9589            0 :     goto cleanup;
    9590              : 
    9591        10480 :   if (find_special (name, &sym, false))
    9592              :     return MATCH_ERROR;
    9593              : 
    9594        10480 :   if (!check_function_name (name))
    9595              :     {
    9596            7 :       m = MATCH_ERROR;
    9597            7 :       goto cleanup;
    9598              :     }
    9599              : 
    9600        10473 :   var_locus = gfc_current_locus;
    9601              : 
    9602              :   /* Deal with possible array specification for certain attributes.  */
    9603        10473 :   if (current_attr.dimension
    9604         8894 :       || current_attr.codimension
    9605         8872 :       || current_attr.allocatable
    9606         8432 :       || current_attr.pointer
    9607         7693 :       || current_attr.target)
    9608              :     {
    9609         6238 :       m = gfc_match_array_spec (&as, !current_attr.codimension,
    9610              :                                 !current_attr.dimension
    9611         1427 :                                 && !current_attr.pointer
    9612              :                                 && !current_attr.target);
    9613         3006 :       if (m == MATCH_ERROR)
    9614            2 :         goto cleanup;
    9615              : 
    9616         3004 :       if (current_attr.dimension && m == MATCH_NO)
    9617              :         {
    9618            0 :           gfc_error ("Missing array specification at %L in DIMENSION "
    9619              :                      "statement", &var_locus);
    9620            0 :           m = MATCH_ERROR;
    9621            0 :           goto cleanup;
    9622              :         }
    9623              : 
    9624         3004 :       if (current_attr.dimension && sym->value)
    9625              :         {
    9626            1 :           gfc_error ("Dimensions specified for %s at %L after its "
    9627              :                      "initialization", sym->name, &var_locus);
    9628            1 :           m = MATCH_ERROR;
    9629            1 :           goto cleanup;
    9630              :         }
    9631              : 
    9632         3003 :       if (current_attr.codimension && m == MATCH_NO)
    9633              :         {
    9634            0 :           gfc_error ("Missing array specification at %L in CODIMENSION "
    9635              :                      "statement", &var_locus);
    9636            0 :           m = MATCH_ERROR;
    9637            0 :           goto cleanup;
    9638              :         }
    9639              : 
    9640         3003 :       if ((current_attr.allocatable || current_attr.pointer)
    9641         1179 :           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
    9642              :         {
    9643            0 :           gfc_error ("Array specification must be deferred at %L", &var_locus);
    9644            0 :           m = MATCH_ERROR;
    9645            0 :           goto cleanup;
    9646              :         }
    9647              :     }
    9648              : 
    9649        10470 :   if (sym->ts.type == BT_CLASS
    9650          200 :       && sym->ts.u.derived
    9651          200 :       && sym->ts.u.derived->attr.is_class)
    9652              :     {
    9653          177 :       sym->attr.pointer = CLASS_DATA(sym)->attr.class_pointer;
    9654          177 :       sym->attr.allocatable = CLASS_DATA(sym)->attr.allocatable;
    9655          177 :       sym->attr.dimension = CLASS_DATA(sym)->attr.dimension;
    9656          177 :       sym->attr.codimension = CLASS_DATA(sym)->attr.codimension;
    9657          177 :       if (CLASS_DATA (sym)->as)
    9658          123 :         sym->as = gfc_copy_array_spec (CLASS_DATA (sym)->as);
    9659              :     }
    9660         8893 :   if (current_attr.dimension == 0 && current_attr.codimension == 0
    9661        19342 :       && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
    9662              :     {
    9663           22 :       m = MATCH_ERROR;
    9664           22 :       goto cleanup;
    9665              :     }
    9666        10448 :   if (!gfc_set_array_spec (sym, as, &var_locus))
    9667              :     {
    9668           17 :       m = MATCH_ERROR;
    9669           17 :       goto cleanup;
    9670              :     }
    9671              : 
    9672        10431 :   if (sym->attr.cray_pointee && sym->as != NULL)
    9673              :     {
    9674              :       /* Fix the array spec.  */
    9675            2 :       m = gfc_mod_pointee_as (sym->as);
    9676            2 :       if (m == MATCH_ERROR)
    9677            0 :         goto cleanup;
    9678              :     }
    9679              : 
    9680        10431 :   if (!gfc_add_attribute (&sym->attr, &var_locus))
    9681              :     {
    9682            0 :       m = MATCH_ERROR;
    9683            0 :       goto cleanup;
    9684              :     }
    9685              : 
    9686         5793 :   if ((current_attr.external || current_attr.intrinsic)
    9687         6290 :       && sym->attr.flavor != FL_PROCEDURE
    9688        16689 :       && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
    9689              :     {
    9690            0 :       m = MATCH_ERROR;
    9691            0 :       goto cleanup;
    9692              :     }
    9693              : 
    9694        10431 :   if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class
    9695          169 :       && !as && !current_attr.pointer && !current_attr.allocatable
    9696          136 :       && !current_attr.external)
    9697              :     {
    9698          136 :       sym->attr.pointer = 0;
    9699          136 :       sym->attr.allocatable = 0;
    9700          136 :       sym->attr.dimension = 0;
    9701          136 :       sym->attr.codimension = 0;
    9702          136 :       gfc_free_array_spec (sym->as);
    9703          136 :       sym->as = NULL;
    9704              :     }
    9705        10295 :   else if (sym->ts.type == BT_CLASS
    9706        10295 :       && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
    9707              :     {
    9708            0 :       m = MATCH_ERROR;
    9709            0 :       goto cleanup;
    9710              :     }
    9711              : 
    9712        10431 :   add_hidden_procptr_result (sym);
    9713              : 
    9714        10431 :   return MATCH_YES;
    9715              : 
    9716           49 : cleanup:
    9717           49 :   gfc_free_array_spec (as);
    9718           49 :   return m;
    9719              : }
    9720              : 
    9721              : 
    9722              : /* Generic attribute declaration subroutine.  Used for attributes that
    9723              :    just have a list of names.  */
    9724              : 
    9725              : static match
    9726         6734 : attr_decl (void)
    9727              : {
    9728         6734 :   match m;
    9729              : 
    9730              :   /* Gobble the optional double colon, by simply ignoring the result
    9731              :      of gfc_match().  */
    9732         6734 :   gfc_match (" ::");
    9733              : 
    9734        10480 :   for (;;)
    9735              :     {
    9736        10480 :       m = attr_decl1 ();
    9737        10480 :       if (m != MATCH_YES)
    9738              :         break;
    9739              : 
    9740        10431 :       if (gfc_match_eos () == MATCH_YES)
    9741              :         {
    9742              :           m = MATCH_YES;
    9743              :           break;
    9744              :         }
    9745              : 
    9746         3746 :       if (gfc_match_char (',') != MATCH_YES)
    9747              :         {
    9748            0 :           gfc_error ("Unexpected character in variable list at %C");
    9749            0 :           m = MATCH_ERROR;
    9750            0 :           break;
    9751              :         }
    9752              :     }
    9753              : 
    9754         6734 :   return m;
    9755              : }
    9756              : 
    9757              : 
    9758              : /* This routine matches Cray Pointer declarations of the form:
    9759              :    pointer ( <pointer>, <pointee> )
    9760              :    or
    9761              :    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
    9762              :    The pointer, if already declared, should be an integer.  Otherwise, we
    9763              :    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
    9764              :    be either a scalar, or an array declaration.  No space is allocated for
    9765              :    the pointee.  For the statement
    9766              :    pointer (ipt, ar(10))
    9767              :    any subsequent uses of ar will be translated (in C-notation) as
    9768              :    ar(i) => ((<type> *) ipt)(i)
    9769              :    After gimplification, pointee variable will disappear in the code.  */
    9770              : 
    9771              : static match
    9772          334 : cray_pointer_decl (void)
    9773              : {
    9774          334 :   match m;
    9775          334 :   gfc_array_spec *as = NULL;
    9776          334 :   gfc_symbol *cptr; /* Pointer symbol.  */
    9777          334 :   gfc_symbol *cpte; /* Pointee symbol.  */
    9778          334 :   locus var_locus;
    9779          334 :   bool done = false;
    9780              : 
    9781          334 :   while (!done)
    9782              :     {
    9783          347 :       if (gfc_match_char ('(') != MATCH_YES)
    9784              :         {
    9785            1 :           gfc_error ("Expected %<(%> at %C");
    9786            1 :           return MATCH_ERROR;
    9787              :         }
    9788              : 
    9789              :       /* Match pointer.  */
    9790          346 :       var_locus = gfc_current_locus;
    9791          346 :       gfc_clear_attr (&current_attr);
    9792          346 :       gfc_add_cray_pointer (&current_attr, &var_locus);
    9793          346 :       current_ts.type = BT_INTEGER;
    9794          346 :       current_ts.kind = gfc_index_integer_kind;
    9795              : 
    9796          346 :       m = gfc_match_symbol (&cptr, 0);
    9797          346 :       if (m != MATCH_YES)
    9798              :         {
    9799            2 :           gfc_error ("Expected variable name at %C");
    9800            2 :           return m;
    9801              :         }
    9802              : 
    9803          344 :       if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
    9804              :         return MATCH_ERROR;
    9805              : 
    9806          341 :       gfc_set_sym_referenced (cptr);
    9807              : 
    9808          341 :       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
    9809              :         {
    9810          327 :           cptr->ts.type = BT_INTEGER;
    9811          327 :           cptr->ts.kind = gfc_index_integer_kind;
    9812              :         }
    9813           14 :       else if (cptr->ts.type != BT_INTEGER)
    9814              :         {
    9815            1 :           gfc_error ("Cray pointer at %C must be an integer");
    9816            1 :           return MATCH_ERROR;
    9817              :         }
    9818           13 :       else if (cptr->ts.kind < gfc_index_integer_kind)
    9819            0 :         gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
    9820              :                      " memory addresses require %d bytes",
    9821              :                      cptr->ts.kind, gfc_index_integer_kind);
    9822              : 
    9823          340 :       if (gfc_match_char (',') != MATCH_YES)
    9824              :         {
    9825            2 :           gfc_error ("Expected \",\" at %C");
    9826            2 :           return MATCH_ERROR;
    9827              :         }
    9828              : 
    9829              :       /* Match Pointee.  */
    9830          338 :       var_locus = gfc_current_locus;
    9831          338 :       gfc_clear_attr (&current_attr);
    9832          338 :       gfc_add_cray_pointee (&current_attr, &var_locus);
    9833          338 :       current_ts.type = BT_UNKNOWN;
    9834          338 :       current_ts.kind = 0;
    9835              : 
    9836          338 :       m = gfc_match_symbol (&cpte, 0);
    9837          338 :       if (m != MATCH_YES)
    9838              :         {
    9839            2 :           gfc_error ("Expected variable name at %C");
    9840            2 :           return m;
    9841              :         }
    9842              : 
    9843              :       /* Check for an optional array spec.  */
    9844          336 :       m = gfc_match_array_spec (&as, true, false);
    9845          336 :       if (m == MATCH_ERROR)
    9846              :         {
    9847            0 :           gfc_free_array_spec (as);
    9848            0 :           return m;
    9849              :         }
    9850          336 :       else if (m == MATCH_NO)
    9851              :         {
    9852          226 :           gfc_free_array_spec (as);
    9853          226 :           as = NULL;
    9854              :         }
    9855              : 
    9856          336 :       if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
    9857              :         return MATCH_ERROR;
    9858              : 
    9859          329 :       gfc_set_sym_referenced (cpte);
    9860              : 
    9861          329 :       if (cpte->as == NULL)
    9862              :         {
    9863          247 :           if (!gfc_set_array_spec (cpte, as, &var_locus))
    9864            0 :             gfc_internal_error ("Cannot set Cray pointee array spec.");
    9865              :         }
    9866           82 :       else if (as != NULL)
    9867              :         {
    9868            1 :           gfc_error ("Duplicate array spec for Cray pointee at %C");
    9869            1 :           gfc_free_array_spec (as);
    9870            1 :           return MATCH_ERROR;
    9871              :         }
    9872              : 
    9873          328 :       as = NULL;
    9874              : 
    9875          328 :       if (cpte->as != NULL)
    9876              :         {
    9877              :           /* Fix array spec.  */
    9878          190 :           m = gfc_mod_pointee_as (cpte->as);
    9879          190 :           if (m == MATCH_ERROR)
    9880              :             return m;
    9881              :         }
    9882              : 
    9883              :       /* Point the Pointee at the Pointer.  */
    9884          328 :       cpte->cp_pointer = cptr;
    9885              : 
    9886          328 :       if (gfc_match_char (')') != MATCH_YES)
    9887              :         {
    9888            2 :           gfc_error ("Expected \")\" at %C");
    9889            2 :           return MATCH_ERROR;
    9890              :         }
    9891          326 :       m = gfc_match_char (',');
    9892          326 :       if (m != MATCH_YES)
    9893              :         done = true; /* Stop searching for more declarations.  */
    9894              : 
    9895              :     }
    9896              : 
    9897          313 :   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
    9898          313 :       || gfc_match_eos () != MATCH_YES)
    9899              :     {
    9900            0 :       gfc_error ("Expected %<,%> or end of statement at %C");
    9901            0 :       return MATCH_ERROR;
    9902              :     }
    9903              :   return MATCH_YES;
    9904              : }
    9905              : 
    9906              : 
    9907              : match
    9908         3216 : gfc_match_external (void)
    9909              : {
    9910              : 
    9911         3216 :   gfc_clear_attr (&current_attr);
    9912         3216 :   current_attr.external = 1;
    9913              : 
    9914         3216 :   return attr_decl ();
    9915              : }
    9916              : 
    9917              : 
    9918              : match
    9919          208 : gfc_match_intent (void)
    9920              : {
    9921          208 :   sym_intent intent;
    9922              : 
    9923              :   /* This is not allowed within a BLOCK construct!  */
    9924          208 :   if (gfc_current_state () == COMP_BLOCK)
    9925              :     {
    9926            2 :       gfc_error ("INTENT is not allowed inside of BLOCK at %C");
    9927            2 :       return MATCH_ERROR;
    9928              :     }
    9929              : 
    9930          206 :   intent = match_intent_spec ();
    9931          206 :   if (intent == INTENT_UNKNOWN)
    9932              :     return MATCH_ERROR;
    9933              : 
    9934          206 :   gfc_clear_attr (&current_attr);
    9935          206 :   current_attr.intent = intent;
    9936              : 
    9937          206 :   return attr_decl ();
    9938              : }
    9939              : 
    9940              : 
    9941              : match
    9942         1482 : gfc_match_intrinsic (void)
    9943              : {
    9944              : 
    9945         1482 :   gfc_clear_attr (&current_attr);
    9946         1482 :   current_attr.intrinsic = 1;
    9947              : 
    9948         1482 :   return attr_decl ();
    9949              : }
    9950              : 
    9951              : 
    9952              : match
    9953          227 : gfc_match_optional (void)
    9954              : {
    9955              :   /* This is not allowed within a BLOCK construct!  */
    9956          227 :   if (gfc_current_state () == COMP_BLOCK)
    9957              :     {
    9958            2 :       gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
    9959            2 :       return MATCH_ERROR;
    9960              :     }
    9961              : 
    9962          225 :   gfc_clear_attr (&current_attr);
    9963          225 :   current_attr.optional = 1;
    9964              : 
    9965          225 :   return attr_decl ();
    9966              : }
    9967              : 
    9968              : 
    9969              : match
    9970          922 : gfc_match_pointer (void)
    9971              : {
    9972          922 :   gfc_gobble_whitespace ();
    9973          922 :   if (gfc_peek_ascii_char () == '(')
    9974              :     {
    9975          335 :       if (!flag_cray_pointer)
    9976              :         {
    9977            1 :           gfc_error ("Cray pointer declaration at %C requires "
    9978              :                      "%<-fcray-pointer%> flag");
    9979            1 :           return MATCH_ERROR;
    9980              :         }
    9981          334 :       return cray_pointer_decl ();
    9982              :     }
    9983              :   else
    9984              :     {
    9985          587 :       gfc_clear_attr (&current_attr);
    9986          587 :       current_attr.pointer = 1;
    9987              : 
    9988          587 :       return attr_decl ();
    9989              :     }
    9990              : }
    9991              : 
    9992              : 
    9993              : match
    9994          169 : gfc_match_allocatable (void)
    9995              : {
    9996          169 :   gfc_clear_attr (&current_attr);
    9997          169 :   current_attr.allocatable = 1;
    9998              : 
    9999          169 :   return attr_decl ();
   10000              : }
   10001              : 
   10002              : 
   10003              : match
   10004           23 : gfc_match_codimension (void)
   10005              : {
   10006           23 :   gfc_clear_attr (&current_attr);
   10007           23 :   current_attr.codimension = 1;
   10008              : 
   10009           23 :   return attr_decl ();
   10010              : }
   10011              : 
   10012              : 
   10013              : match
   10014           80 : gfc_match_contiguous (void)
   10015              : {
   10016           80 :   if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
   10017              :     return MATCH_ERROR;
   10018              : 
   10019           79 :   gfc_clear_attr (&current_attr);
   10020           79 :   current_attr.contiguous = 1;
   10021              : 
   10022           79 :   return attr_decl ();
   10023              : }
   10024              : 
   10025              : 
   10026              : match
   10027          648 : gfc_match_dimension (void)
   10028              : {
   10029          648 :   gfc_clear_attr (&current_attr);
   10030          648 :   current_attr.dimension = 1;
   10031              : 
   10032          648 :   return attr_decl ();
   10033              : }
   10034              : 
   10035              : 
   10036              : match
   10037           99 : gfc_match_target (void)
   10038              : {
   10039           99 :   gfc_clear_attr (&current_attr);
   10040           99 :   current_attr.target = 1;
   10041              : 
   10042           99 :   return attr_decl ();
   10043              : }
   10044              : 
   10045              : 
   10046              : /* Match the list of entities being specified in a PUBLIC or PRIVATE
   10047              :    statement.  */
   10048              : 
   10049              : static match
   10050         1772 : access_attr_decl (gfc_statement st)
   10051              : {
   10052         1772 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10053         1772 :   interface_type type;
   10054         1772 :   gfc_user_op *uop;
   10055         1772 :   gfc_symbol *sym, *dt_sym;
   10056         1772 :   gfc_intrinsic_op op;
   10057         1772 :   match m;
   10058         1772 :   gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
   10059              : 
   10060         1772 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10061            0 :     goto done;
   10062              : 
   10063         2934 :   for (;;)
   10064              :     {
   10065         2934 :       m = gfc_match_generic_spec (&type, name, &op);
   10066         2934 :       if (m == MATCH_NO)
   10067            0 :         goto syntax;
   10068         2934 :       if (m == MATCH_ERROR)
   10069            0 :         goto done;
   10070              : 
   10071         2934 :       switch (type)
   10072              :         {
   10073            0 :         case INTERFACE_NAMELESS:
   10074            0 :         case INTERFACE_ABSTRACT:
   10075            0 :           goto syntax;
   10076              : 
   10077         2857 :         case INTERFACE_GENERIC:
   10078         2857 :         case INTERFACE_DTIO:
   10079              : 
   10080         2857 :           if (gfc_get_symbol (name, NULL, &sym))
   10081            0 :             goto done;
   10082              : 
   10083         2857 :           if (type == INTERFACE_DTIO
   10084           26 :               && gfc_current_ns->proc_name
   10085           26 :               && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
   10086           26 :               && sym->attr.flavor == FL_UNKNOWN)
   10087            2 :             sym->attr.flavor = FL_PROCEDURE;
   10088              : 
   10089         2857 :           if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
   10090            4 :             goto done;
   10091              : 
   10092          330 :           if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
   10093         2910 :               && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
   10094            0 :             goto done;
   10095              : 
   10096              :           break;
   10097              : 
   10098           72 :         case INTERFACE_INTRINSIC_OP:
   10099           72 :           if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
   10100              :             {
   10101           72 :               gfc_intrinsic_op other_op;
   10102              : 
   10103           72 :               gfc_current_ns->operator_access[op] = access;
   10104              : 
   10105              :               /* Handle the case if there is another op with the same
   10106              :                  function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on.  */
   10107           72 :               other_op = gfc_equivalent_op (op);
   10108              : 
   10109           72 :               if (other_op != INTRINSIC_NONE)
   10110           21 :                 gfc_current_ns->operator_access[other_op] = access;
   10111              :             }
   10112              :           else
   10113              :             {
   10114            0 :               gfc_error ("Access specification of the %s operator at %C has "
   10115              :                          "already been specified", gfc_op2string (op));
   10116            0 :               goto done;
   10117              :             }
   10118              : 
   10119              :           break;
   10120              : 
   10121            5 :         case INTERFACE_USER_OP:
   10122            5 :           uop = gfc_get_uop (name);
   10123              : 
   10124            5 :           if (uop->access == ACCESS_UNKNOWN)
   10125              :             {
   10126            4 :               uop->access = access;
   10127              :             }
   10128              :           else
   10129              :             {
   10130            1 :               gfc_error ("Access specification of the .%s. operator at %C "
   10131              :                          "has already been specified", uop->name);
   10132            1 :               goto done;
   10133              :             }
   10134              : 
   10135            4 :           break;
   10136              :         }
   10137              : 
   10138         2929 :       if (gfc_match_char (',') == MATCH_NO)
   10139              :         break;
   10140              :     }
   10141              : 
   10142         1767 :   if (gfc_match_eos () != MATCH_YES)
   10143            0 :     goto syntax;
   10144              :   return MATCH_YES;
   10145              : 
   10146            0 : syntax:
   10147            0 :   gfc_syntax_error (st);
   10148              : 
   10149         1772 : done:
   10150              :   return MATCH_ERROR;
   10151              : }
   10152              : 
   10153              : 
   10154              : match
   10155           23 : gfc_match_protected (void)
   10156              : {
   10157           23 :   gfc_symbol *sym;
   10158           23 :   match m;
   10159           23 :   char c;
   10160              : 
   10161              :   /* PROTECTED has already been seen, but must be followed by whitespace
   10162              :      or ::.  */
   10163           23 :   c = gfc_peek_ascii_char ();
   10164           23 :   if (!gfc_is_whitespace (c) && c != ':')
   10165              :     return MATCH_NO;
   10166              : 
   10167           22 :   if (!gfc_current_ns->proc_name
   10168           20 :       || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
   10169              :     {
   10170            3 :        gfc_error ("PROTECTED at %C only allowed in specification "
   10171              :                   "part of a module");
   10172            3 :        return MATCH_ERROR;
   10173              : 
   10174              :     }
   10175              : 
   10176           19 :   gfc_match (" ::");
   10177              : 
   10178           19 :   if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
   10179              :     return MATCH_ERROR;
   10180              : 
   10181              :   /* PROTECTED has an entity-list.  */
   10182           18 :   if (gfc_match_eos () == MATCH_YES)
   10183            0 :     goto syntax;
   10184              : 
   10185           26 :   for(;;)
   10186              :     {
   10187           26 :       m = gfc_match_symbol (&sym, 0);
   10188           26 :       switch (m)
   10189              :         {
   10190           26 :         case MATCH_YES:
   10191           26 :           if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
   10192              :             return MATCH_ERROR;
   10193           25 :           goto next_item;
   10194              : 
   10195              :         case MATCH_NO:
   10196              :           break;
   10197              : 
   10198              :         case MATCH_ERROR:
   10199              :           return MATCH_ERROR;
   10200              :         }
   10201              : 
   10202           25 :     next_item:
   10203           25 :       if (gfc_match_eos () == MATCH_YES)
   10204              :         break;
   10205            8 :       if (gfc_match_char (',') != MATCH_YES)
   10206            0 :         goto syntax;
   10207              :     }
   10208              : 
   10209              :   return MATCH_YES;
   10210              : 
   10211            0 : syntax:
   10212            0 :   gfc_error ("Syntax error in PROTECTED statement at %C");
   10213            0 :   return MATCH_ERROR;
   10214              : }
   10215              : 
   10216              : 
   10217              : /* The PRIVATE statement is a bit weird in that it can be an attribute
   10218              :    declaration, but also works as a standalone statement inside of a
   10219              :    type declaration or a module.  */
   10220              : 
   10221              : match
   10222        30242 : gfc_match_private (gfc_statement *st)
   10223              : {
   10224        30242 :   gfc_state_data *prev;
   10225              : 
   10226        30242 :   if (gfc_match ("private") != MATCH_YES)
   10227              :     return MATCH_NO;
   10228              : 
   10229              :   /* Try matching PRIVATE without an access-list.  */
   10230         1641 :   if (gfc_match_eos () == MATCH_YES)
   10231              :     {
   10232         1354 :       prev = gfc_state_stack->previous;
   10233         1354 :       if (gfc_current_state () != COMP_MODULE
   10234          367 :           && !(gfc_current_state () == COMP_DERIVED
   10235          334 :                 && prev && prev->state == COMP_MODULE)
   10236           34 :           && !(gfc_current_state () == COMP_DERIVED_CONTAINS
   10237           32 :                 && prev->previous && prev->previous->state == COMP_MODULE))
   10238              :         {
   10239            2 :           gfc_error ("PRIVATE statement at %C is only allowed in the "
   10240              :                      "specification part of a module");
   10241            2 :           return MATCH_ERROR;
   10242              :         }
   10243              : 
   10244         1352 :       *st = ST_PRIVATE;
   10245         1352 :       return MATCH_YES;
   10246              :     }
   10247              : 
   10248              :   /* At this point in free-form source code, PRIVATE must be followed
   10249              :      by whitespace or ::.  */
   10250          287 :   if (gfc_current_form == FORM_FREE)
   10251              :     {
   10252          285 :       char c = gfc_peek_ascii_char ();
   10253          285 :       if (!gfc_is_whitespace (c) && c != ':')
   10254              :         return MATCH_NO;
   10255              :     }
   10256              : 
   10257          286 :   prev = gfc_state_stack->previous;
   10258          286 :   if (gfc_current_state () != COMP_MODULE
   10259            1 :       && !(gfc_current_state () == COMP_DERIVED
   10260            0 :            && prev && prev->state == COMP_MODULE)
   10261            1 :       && !(gfc_current_state () == COMP_DERIVED_CONTAINS
   10262            0 :            && prev->previous && prev->previous->state == COMP_MODULE))
   10263              :     {
   10264            1 :       gfc_error ("PRIVATE statement at %C is only allowed in the "
   10265              :                  "specification part of a module");
   10266            1 :       return MATCH_ERROR;
   10267              :     }
   10268              : 
   10269          285 :   *st = ST_ATTR_DECL;
   10270          285 :   return access_attr_decl (ST_PRIVATE);
   10271              : }
   10272              : 
   10273              : 
   10274              : match
   10275         1888 : gfc_match_public (gfc_statement *st)
   10276              : {
   10277         1888 :   if (gfc_match ("public") != MATCH_YES)
   10278              :     return MATCH_NO;
   10279              : 
   10280              :   /* Try matching PUBLIC without an access-list.  */
   10281         1534 :   if (gfc_match_eos () == MATCH_YES)
   10282              :     {
   10283           45 :       if (gfc_current_state () != COMP_MODULE)
   10284              :         {
   10285            2 :           gfc_error ("PUBLIC statement at %C is only allowed in the "
   10286              :                      "specification part of a module");
   10287            2 :           return MATCH_ERROR;
   10288              :         }
   10289              : 
   10290           43 :       *st = ST_PUBLIC;
   10291           43 :       return MATCH_YES;
   10292              :     }
   10293              : 
   10294              :   /* At this point in free-form source code, PUBLIC must be followed
   10295              :      by whitespace or ::.  */
   10296         1489 :   if (gfc_current_form == FORM_FREE)
   10297              :     {
   10298         1487 :       char c = gfc_peek_ascii_char ();
   10299         1487 :       if (!gfc_is_whitespace (c) && c != ':')
   10300              :         return MATCH_NO;
   10301              :     }
   10302              : 
   10303         1488 :   if (gfc_current_state () != COMP_MODULE)
   10304              :     {
   10305            1 :       gfc_error ("PUBLIC statement at %C is only allowed in the "
   10306              :                  "specification part of a module");
   10307            1 :       return MATCH_ERROR;
   10308              :     }
   10309              : 
   10310         1487 :   *st = ST_ATTR_DECL;
   10311         1487 :   return access_attr_decl (ST_PUBLIC);
   10312              : }
   10313              : 
   10314              : 
   10315              : /* Workhorse for gfc_match_parameter.  */
   10316              : 
   10317              : static match
   10318         8771 : do_parm (void)
   10319              : {
   10320         8771 :   gfc_symbol *sym;
   10321         8771 :   gfc_expr *init;
   10322         8771 :   gfc_charlen *saved_cl_list;
   10323         8771 :   match m;
   10324         8771 :   bool t;
   10325              : 
   10326         8771 :   saved_cl_list = gfc_current_ns->cl_list;
   10327              : 
   10328         8771 :   m = gfc_match_symbol (&sym, 0);
   10329         8771 :   if (m == MATCH_NO)
   10330            0 :     gfc_error ("Expected variable name at %C in PARAMETER statement");
   10331              : 
   10332         8771 :   if (m != MATCH_YES)
   10333              :     return m;
   10334              : 
   10335         8771 :   if (gfc_match_char ('=') == MATCH_NO)
   10336              :     {
   10337            0 :       gfc_error ("Expected = sign in PARAMETER statement at %C");
   10338            0 :       return MATCH_ERROR;
   10339              :     }
   10340              : 
   10341         8771 :   m = gfc_match_init_expr (&init);
   10342         8771 :   if (m == MATCH_NO)
   10343            0 :     gfc_error ("Expected expression at %C in PARAMETER statement");
   10344         8771 :   if (m != MATCH_YES)
   10345              :     return m;
   10346              : 
   10347         8770 :   if (sym->ts.type == BT_UNKNOWN
   10348         8770 :       && !gfc_set_default_type (sym, 1, NULL))
   10349              :     {
   10350            1 :       m = MATCH_ERROR;
   10351            1 :       goto cleanup;
   10352              :     }
   10353              : 
   10354         8769 :   if (!gfc_check_assign_symbol (sym, NULL, init)
   10355         8769 :       || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
   10356              :     {
   10357            1 :       m = MATCH_ERROR;
   10358            1 :       goto cleanup;
   10359              :     }
   10360              : 
   10361         8768 :   if (sym->value)
   10362              :     {
   10363            1 :       gfc_error ("Initializing already initialized variable at %C");
   10364            1 :       m = MATCH_ERROR;
   10365            1 :       goto cleanup;
   10366              :     }
   10367              : 
   10368         8767 :   t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus,
   10369              :                             saved_cl_list);
   10370         8767 :   return (t) ? MATCH_YES : MATCH_ERROR;
   10371              : 
   10372            3 : cleanup:
   10373            3 :   gfc_free_expr (init);
   10374            3 :   return m;
   10375              : }
   10376              : 
   10377              : 
   10378              : /* Match a parameter statement, with the weird syntax that these have.  */
   10379              : 
   10380              : match
   10381         8058 : gfc_match_parameter (void)
   10382              : {
   10383         8058 :   const char *term = " )%t";
   10384         8058 :   match m;
   10385              : 
   10386         8058 :   if (gfc_match_char ('(') == MATCH_NO)
   10387              :     {
   10388              :       /* With legacy PARAMETER statements, don't expect a terminating ')'.  */
   10389           28 :       if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
   10390              :         return MATCH_NO;
   10391         8057 :       term = " %t";
   10392              :     }
   10393              : 
   10394         8771 :   for (;;)
   10395              :     {
   10396         8771 :       m = do_parm ();
   10397         8771 :       if (m != MATCH_YES)
   10398              :         break;
   10399              : 
   10400         8767 :       if (gfc_match (term) == MATCH_YES)
   10401              :         break;
   10402              : 
   10403          714 :       if (gfc_match_char (',') != MATCH_YES)
   10404              :         {
   10405            0 :           gfc_error ("Unexpected characters in PARAMETER statement at %C");
   10406            0 :           m = MATCH_ERROR;
   10407            0 :           break;
   10408              :         }
   10409              :     }
   10410              : 
   10411              :   return m;
   10412              : }
   10413              : 
   10414              : 
   10415              : match
   10416            8 : gfc_match_automatic (void)
   10417              : {
   10418            8 :   gfc_symbol *sym;
   10419            8 :   match m;
   10420            8 :   bool seen_symbol = false;
   10421              : 
   10422            8 :   if (!flag_dec_static)
   10423              :     {
   10424            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
   10425              :                  "%<-fdec-static%>",
   10426              :                  "AUTOMATIC"
   10427              :                  );
   10428            2 :       return MATCH_ERROR;
   10429              :     }
   10430              : 
   10431            6 :   gfc_match (" ::");
   10432              : 
   10433            6 :   for (;;)
   10434              :     {
   10435            6 :       m = gfc_match_symbol (&sym, 0);
   10436            6 :       switch (m)
   10437              :       {
   10438              :       case MATCH_NO:
   10439              :         break;
   10440              : 
   10441              :       case MATCH_ERROR:
   10442              :         return MATCH_ERROR;
   10443              : 
   10444            4 :       case MATCH_YES:
   10445            4 :         if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
   10446              :           return MATCH_ERROR;
   10447              :         seen_symbol = true;
   10448              :         break;
   10449              :       }
   10450              : 
   10451            4 :       if (gfc_match_eos () == MATCH_YES)
   10452              :         break;
   10453            0 :       if (gfc_match_char (',') != MATCH_YES)
   10454            0 :         goto syntax;
   10455              :     }
   10456              : 
   10457            4 :   if (!seen_symbol)
   10458              :     {
   10459            2 :       gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
   10460            2 :       return MATCH_ERROR;
   10461              :     }
   10462              : 
   10463              :   return MATCH_YES;
   10464              : 
   10465            0 : syntax:
   10466            0 :   gfc_error ("Syntax error in AUTOMATIC statement at %C");
   10467            0 :   return MATCH_ERROR;
   10468              : }
   10469              : 
   10470              : 
   10471              : match
   10472            7 : gfc_match_static (void)
   10473              : {
   10474            7 :   gfc_symbol *sym;
   10475            7 :   match m;
   10476            7 :   bool seen_symbol = false;
   10477              : 
   10478            7 :   if (!flag_dec_static)
   10479              :     {
   10480            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
   10481              :                  "%<-fdec-static%>",
   10482              :                  "STATIC");
   10483            2 :       return MATCH_ERROR;
   10484              :     }
   10485              : 
   10486            5 :   gfc_match (" ::");
   10487              : 
   10488            5 :   for (;;)
   10489              :     {
   10490            5 :       m = gfc_match_symbol (&sym, 0);
   10491            5 :       switch (m)
   10492              :       {
   10493              :       case MATCH_NO:
   10494              :         break;
   10495              : 
   10496              :       case MATCH_ERROR:
   10497              :         return MATCH_ERROR;
   10498              : 
   10499            3 :       case MATCH_YES:
   10500            3 :         if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
   10501              :                           &gfc_current_locus))
   10502              :           return MATCH_ERROR;
   10503              :         seen_symbol = true;
   10504              :         break;
   10505              :       }
   10506              : 
   10507            3 :       if (gfc_match_eos () == MATCH_YES)
   10508              :         break;
   10509            0 :       if (gfc_match_char (',') != MATCH_YES)
   10510            0 :         goto syntax;
   10511              :     }
   10512              : 
   10513            3 :   if (!seen_symbol)
   10514              :     {
   10515            2 :       gfc_error ("Expected entity-list in STATIC statement at %C");
   10516            2 :       return MATCH_ERROR;
   10517              :     }
   10518              : 
   10519              :   return MATCH_YES;
   10520              : 
   10521            0 : syntax:
   10522            0 :   gfc_error ("Syntax error in STATIC statement at %C");
   10523            0 :   return MATCH_ERROR;
   10524              : }
   10525              : 
   10526              : 
   10527              : /* Save statements have a special syntax.  */
   10528              : 
   10529              : match
   10530          272 : gfc_match_save (void)
   10531              : {
   10532          272 :   char n[GFC_MAX_SYMBOL_LEN+1];
   10533          272 :   gfc_common_head *c;
   10534          272 :   gfc_symbol *sym;
   10535          272 :   match m;
   10536              : 
   10537          272 :   if (gfc_match_eos () == MATCH_YES)
   10538              :     {
   10539          150 :       if (gfc_current_ns->seen_save)
   10540              :         {
   10541            7 :           if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
   10542              :                                "follows previous SAVE statement"))
   10543              :             return MATCH_ERROR;
   10544              :         }
   10545              : 
   10546          149 :       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
   10547          149 :       return MATCH_YES;
   10548              :     }
   10549              : 
   10550          122 :   if (gfc_current_ns->save_all)
   10551              :     {
   10552            7 :       if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
   10553              :                            "blanket SAVE statement"))
   10554              :         return MATCH_ERROR;
   10555              :     }
   10556              : 
   10557          121 :   gfc_match (" ::");
   10558              : 
   10559          183 :   for (;;)
   10560              :     {
   10561          183 :       m = gfc_match_symbol (&sym, 0);
   10562          183 :       switch (m)
   10563              :         {
   10564          181 :         case MATCH_YES:
   10565          181 :           if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
   10566              :                              &gfc_current_locus))
   10567              :             return MATCH_ERROR;
   10568          179 :           goto next_item;
   10569              : 
   10570              :         case MATCH_NO:
   10571              :           break;
   10572              : 
   10573              :         case MATCH_ERROR:
   10574              :           return MATCH_ERROR;
   10575              :         }
   10576              : 
   10577            2 :       m = gfc_match (" / %n /", &n);
   10578            2 :       if (m == MATCH_ERROR)
   10579              :         return MATCH_ERROR;
   10580            2 :       if (m == MATCH_NO)
   10581            0 :         goto syntax;
   10582              : 
   10583              :       /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
   10584              :          saved-entity-list that does not specify a common-block-name.  */
   10585            2 :       if (gfc_current_state () == COMP_BLOCK)
   10586              :         {
   10587            1 :           gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
   10588              :                      "in a BLOCK construct", n);
   10589            1 :           return MATCH_ERROR;
   10590              :         }
   10591              : 
   10592            1 :       c = gfc_get_common (n, 0);
   10593            1 :       c->saved = 1;
   10594              : 
   10595            1 :       gfc_current_ns->seen_save = 1;
   10596              : 
   10597          180 :     next_item:
   10598          180 :       if (gfc_match_eos () == MATCH_YES)
   10599              :         break;
   10600           62 :       if (gfc_match_char (',') != MATCH_YES)
   10601            0 :         goto syntax;
   10602              :     }
   10603              : 
   10604              :   return MATCH_YES;
   10605              : 
   10606            0 : syntax:
   10607            0 :   if (gfc_current_ns->seen_save)
   10608              :     {
   10609            0 :       gfc_error ("Syntax error in SAVE statement at %C");
   10610            0 :       return MATCH_ERROR;
   10611              :     }
   10612              :   else
   10613              :       return MATCH_NO;
   10614              : }
   10615              : 
   10616              : 
   10617              : match
   10618           96 : gfc_match_value (void)
   10619              : {
   10620           96 :   gfc_symbol *sym;
   10621           96 :   match m;
   10622              : 
   10623              :   /* This is not allowed within a BLOCK construct!  */
   10624           96 :   if (gfc_current_state () == COMP_BLOCK)
   10625              :     {
   10626            2 :       gfc_error ("VALUE is not allowed inside of BLOCK at %C");
   10627            2 :       return MATCH_ERROR;
   10628              :     }
   10629              : 
   10630           94 :   if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
   10631              :     return MATCH_ERROR;
   10632              : 
   10633           93 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10634              :     {
   10635              :       return MATCH_ERROR;
   10636              :     }
   10637              : 
   10638           93 :   if (gfc_match_eos () == MATCH_YES)
   10639            0 :     goto syntax;
   10640              : 
   10641          119 :   for(;;)
   10642              :     {
   10643          119 :       m = gfc_match_symbol (&sym, 0);
   10644          119 :       switch (m)
   10645              :         {
   10646          119 :         case MATCH_YES:
   10647          119 :           if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
   10648              :             return MATCH_ERROR;
   10649          113 :           goto next_item;
   10650              : 
   10651              :         case MATCH_NO:
   10652              :           break;
   10653              : 
   10654              :         case MATCH_ERROR:
   10655              :           return MATCH_ERROR;
   10656              :         }
   10657              : 
   10658          113 :     next_item:
   10659          113 :       if (gfc_match_eos () == MATCH_YES)
   10660              :         break;
   10661           26 :       if (gfc_match_char (',') != MATCH_YES)
   10662            0 :         goto syntax;
   10663              :     }
   10664              : 
   10665              :   return MATCH_YES;
   10666              : 
   10667            0 : syntax:
   10668            0 :   gfc_error ("Syntax error in VALUE statement at %C");
   10669            0 :   return MATCH_ERROR;
   10670              : }
   10671              : 
   10672              : 
   10673              : match
   10674           47 : gfc_match_volatile (void)
   10675              : {
   10676           47 :   gfc_symbol *sym;
   10677           47 :   char *name;
   10678           47 :   match m;
   10679              : 
   10680           47 :   if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
   10681              :     return MATCH_ERROR;
   10682              : 
   10683           46 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10684              :     {
   10685              :       return MATCH_ERROR;
   10686              :     }
   10687              : 
   10688           46 :   if (gfc_match_eos () == MATCH_YES)
   10689            1 :     goto syntax;
   10690              : 
   10691           51 :   for(;;)
   10692              :     {
   10693              :       /* VOLATILE is special because it can be added to host-associated
   10694              :          symbols locally.  Except for coarrays.  */
   10695           51 :       m = gfc_match_symbol (&sym, 1);
   10696           51 :       switch (m)
   10697              :         {
   10698           51 :         case MATCH_YES:
   10699           51 :           name = XALLOCAVAR (char, strlen (sym->name) + 1);
   10700           51 :           strcpy (name, sym->name);
   10701           51 :           if (!check_function_name (name))
   10702              :             return MATCH_ERROR;
   10703              :           /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
   10704              :              for variable in a BLOCK which is defined outside of the BLOCK.  */
   10705           50 :           if (sym->ns != gfc_current_ns && sym->attr.codimension)
   10706              :             {
   10707            2 :               gfc_error ("Specifying VOLATILE for coarray variable %qs at "
   10708              :                          "%C, which is use-/host-associated", sym->name);
   10709            2 :               return MATCH_ERROR;
   10710              :             }
   10711           48 :           if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
   10712              :             return MATCH_ERROR;
   10713           45 :           goto next_item;
   10714              : 
   10715              :         case MATCH_NO:
   10716              :           break;
   10717              : 
   10718              :         case MATCH_ERROR:
   10719              :           return MATCH_ERROR;
   10720              :         }
   10721              : 
   10722           45 :     next_item:
   10723           45 :       if (gfc_match_eos () == MATCH_YES)
   10724              :         break;
   10725            6 :       if (gfc_match_char (',') != MATCH_YES)
   10726            0 :         goto syntax;
   10727              :     }
   10728              : 
   10729              :   return MATCH_YES;
   10730              : 
   10731            1 : syntax:
   10732            1 :   gfc_error ("Syntax error in VOLATILE statement at %C");
   10733            1 :   return MATCH_ERROR;
   10734              : }
   10735              : 
   10736              : 
   10737              : match
   10738           11 : gfc_match_asynchronous (void)
   10739              : {
   10740           11 :   gfc_symbol *sym;
   10741           11 :   char *name;
   10742           11 :   match m;
   10743              : 
   10744           11 :   if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
   10745              :     return MATCH_ERROR;
   10746              : 
   10747           10 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10748              :     {
   10749              :       return MATCH_ERROR;
   10750              :     }
   10751              : 
   10752           10 :   if (gfc_match_eos () == MATCH_YES)
   10753            0 :     goto syntax;
   10754              : 
   10755           10 :   for(;;)
   10756              :     {
   10757              :       /* ASYNCHRONOUS is special because it can be added to host-associated
   10758              :          symbols locally.  */
   10759           10 :       m = gfc_match_symbol (&sym, 1);
   10760           10 :       switch (m)
   10761              :         {
   10762           10 :         case MATCH_YES:
   10763           10 :           name = XALLOCAVAR (char, strlen (sym->name) + 1);
   10764           10 :           strcpy (name, sym->name);
   10765           10 :           if (!check_function_name (name))
   10766              :             return MATCH_ERROR;
   10767            9 :           if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
   10768              :             return MATCH_ERROR;
   10769            7 :           goto next_item;
   10770              : 
   10771              :         case MATCH_NO:
   10772              :           break;
   10773              : 
   10774              :         case MATCH_ERROR:
   10775              :           return MATCH_ERROR;
   10776              :         }
   10777              : 
   10778            7 :     next_item:
   10779            7 :       if (gfc_match_eos () == MATCH_YES)
   10780              :         break;
   10781            0 :       if (gfc_match_char (',') != MATCH_YES)
   10782            0 :         goto syntax;
   10783              :     }
   10784              : 
   10785              :   return MATCH_YES;
   10786              : 
   10787            0 : syntax:
   10788            0 :   gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
   10789            0 :   return MATCH_ERROR;
   10790              : }
   10791              : 
   10792              : 
   10793              : /* Match a module procedure statement in a submodule.  */
   10794              : 
   10795              : match
   10796       793378 : gfc_match_submod_proc (void)
   10797              : {
   10798       793378 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10799       793378 :   gfc_symbol *sym, *fsym;
   10800       793378 :   match m;
   10801       793378 :   gfc_formal_arglist *formal, *head, *tail;
   10802              : 
   10803       793378 :   if (gfc_current_state () != COMP_CONTAINS
   10804        16401 :       || !(gfc_state_stack->previous
   10805        16401 :            && (gfc_state_stack->previous->state == COMP_SUBMODULE
   10806        16401 :                || gfc_state_stack->previous->state == COMP_MODULE)))
   10807              :     return MATCH_NO;
   10808              : 
   10809         8181 :   m = gfc_match (" module% procedure% %n", name);
   10810         8181 :   if (m != MATCH_YES)
   10811              :     return m;
   10812              : 
   10813          285 :   if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
   10814              :                                       "at %C"))
   10815              :     return MATCH_ERROR;
   10816              : 
   10817          285 :   if (get_proc_name (name, &sym, false))
   10818              :     return MATCH_ERROR;
   10819              : 
   10820              :   /* Make sure that the result field is appropriately filled.  */
   10821          285 :   if (sym->tlink && sym->tlink->attr.function)
   10822              :     {
   10823          135 :       if (sym->tlink->result && sym->tlink->result != sym->tlink)
   10824              :         {
   10825           85 :           sym->result = sym->tlink->result;
   10826           85 :           if (!sym->result->attr.use_assoc)
   10827              :             {
   10828           20 :               gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
   10829              :                                                  sym->result->name);
   10830           20 :               st->n.sym = sym->result;
   10831           20 :               sym->result->refs++;
   10832              :             }
   10833           85 :           sym->param_list = gfc_copy_actual_arglist (sym->tlink->param_list);
   10834           85 :         }
   10835              :       else
   10836           50 :         sym->result = sym;
   10837              :     }
   10838              : 
   10839              :   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
   10840              :      the symbol existed before.  */
   10841          285 :   sym->declared_at = gfc_current_locus;
   10842              : 
   10843          285 :   if (!sym->attr.module_procedure)
   10844              :     return MATCH_ERROR;
   10845              : 
   10846              :   /* Signal match_end to expect "end procedure".  */
   10847          283 :   sym->abr_modproc_decl = 1;
   10848              : 
   10849              :   /* Change from IFSRC_IFBODY coming from the interface declaration.  */
   10850          283 :   sym->attr.if_source = IFSRC_DECL;
   10851              : 
   10852          283 :   gfc_new_block = sym;
   10853              : 
   10854              :   /* Make a new formal arglist with the symbols in the procedure
   10855              :       namespace.  */
   10856          283 :   head = tail = NULL;
   10857          636 :   for (formal = sym->formal; formal && formal->sym; formal = formal->next)
   10858              :     {
   10859          353 :       if (formal == sym->formal)
   10860          256 :         head = tail = gfc_get_formal_arglist ();
   10861              :       else
   10862              :         {
   10863           97 :           tail->next = gfc_get_formal_arglist ();
   10864           97 :           tail = tail->next;
   10865              :         }
   10866              : 
   10867          353 :       if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
   10868            0 :         goto cleanup;
   10869              : 
   10870          353 :       tail->sym = fsym;
   10871          353 :       gfc_set_sym_referenced (fsym);
   10872              :     }
   10873              : 
   10874              :   /* The dummy symbols get cleaned up, when the formal_namespace of the
   10875              :      interface declaration is cleared.  This allows us to add the
   10876              :      explicit interface as is done for other type of procedure.  */
   10877          283 :   if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
   10878              :                                    &gfc_current_locus))
   10879              :     return MATCH_ERROR;
   10880              : 
   10881          283 :   if (gfc_match_eos () != MATCH_YES)
   10882              :     {
   10883              :       /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
   10884              :          undone, such that the st->n.sym->formal points to the original symbol;
   10885              :          if now this namespace is finalized, the formal namespace is freed,
   10886              :          but it might be still needed in the parent namespace.  */
   10887            1 :       gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
   10888            1 :       st->n.sym = NULL;
   10889            1 :       gfc_free_symbol (sym->tlink);
   10890            1 :       sym->tlink = NULL;
   10891            1 :       sym->refs--;
   10892            1 :       gfc_syntax_error (ST_MODULE_PROC);
   10893            1 :       return MATCH_ERROR;
   10894              :     }
   10895              : 
   10896              :   return MATCH_YES;
   10897              : 
   10898            0 : cleanup:
   10899            0 :   gfc_free_formal_arglist (head);
   10900            0 :   return MATCH_ERROR;
   10901              : }
   10902              : 
   10903              : 
   10904              : /* Match a module procedure statement.  Note that we have to modify
   10905              :    symbols in the parent's namespace because the current one was there
   10906              :    to receive symbols that are in an interface's formal argument list.  */
   10907              : 
   10908              : match
   10909         1626 : gfc_match_modproc (void)
   10910              : {
   10911         1626 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10912         1626 :   gfc_symbol *sym;
   10913         1626 :   match m;
   10914         1626 :   locus old_locus;
   10915         1626 :   gfc_namespace *module_ns;
   10916         1626 :   gfc_interface *old_interface_head, *interface;
   10917              : 
   10918         1626 :   if (gfc_state_stack->previous == NULL
   10919         1624 :       || (gfc_state_stack->state != COMP_INTERFACE
   10920            5 :           && (gfc_state_stack->state != COMP_CONTAINS
   10921            4 :               || gfc_state_stack->previous->state != COMP_INTERFACE))
   10922         1619 :       || current_interface.type == INTERFACE_NAMELESS
   10923         1619 :       || current_interface.type == INTERFACE_ABSTRACT)
   10924              :     {
   10925            8 :       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
   10926              :                  "interface");
   10927            8 :       return MATCH_ERROR;
   10928              :     }
   10929              : 
   10930         1618 :   module_ns = gfc_current_ns->parent;
   10931         1624 :   for (; module_ns; module_ns = module_ns->parent)
   10932         1624 :     if (module_ns->proc_name->attr.flavor == FL_MODULE
   10933           29 :         || module_ns->proc_name->attr.flavor == FL_PROGRAM
   10934           12 :         || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
   10935           12 :             && !module_ns->proc_name->attr.contained))
   10936              :       break;
   10937              : 
   10938         1618 :   if (module_ns == NULL)
   10939              :     return MATCH_ERROR;
   10940              : 
   10941              :   /* Store the current state of the interface. We will need it if we
   10942              :      end up with a syntax error and need to recover.  */
   10943         1618 :   old_interface_head = gfc_current_interface_head ();
   10944              : 
   10945              :   /* Check if the F2008 optional double colon appears.  */
   10946         1618 :   gfc_gobble_whitespace ();
   10947         1618 :   old_locus = gfc_current_locus;
   10948         1618 :   if (gfc_match ("::") == MATCH_YES)
   10949              :     {
   10950           31 :       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
   10951              :                            "MODULE PROCEDURE statement at %L", &old_locus))
   10952              :         return MATCH_ERROR;
   10953              :     }
   10954              :   else
   10955         1587 :     gfc_current_locus = old_locus;
   10956              : 
   10957         1973 :   for (;;)
   10958              :     {
   10959         1973 :       bool last = false;
   10960         1973 :       old_locus = gfc_current_locus;
   10961              : 
   10962         1973 :       m = gfc_match_name (name);
   10963         1973 :       if (m == MATCH_NO)
   10964            1 :         goto syntax;
   10965         1972 :       if (m != MATCH_YES)
   10966              :         return MATCH_ERROR;
   10967              : 
   10968              :       /* Check for syntax error before starting to add symbols to the
   10969              :          current namespace.  */
   10970         1972 :       if (gfc_match_eos () == MATCH_YES)
   10971              :         last = true;
   10972              : 
   10973          360 :       if (!last && gfc_match_char (',') != MATCH_YES)
   10974            2 :         goto syntax;
   10975              : 
   10976              :       /* Now we're sure the syntax is valid, we process this item
   10977              :          further.  */
   10978         1970 :       if (gfc_get_symbol (name, module_ns, &sym))
   10979              :         return MATCH_ERROR;
   10980              : 
   10981         1970 :       if (sym->attr.intrinsic)
   10982              :         {
   10983            1 :           gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
   10984              :                      "PROCEDURE", &old_locus);
   10985            1 :           return MATCH_ERROR;
   10986              :         }
   10987              : 
   10988         1969 :       if (sym->attr.proc != PROC_MODULE
   10989         1969 :           && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
   10990              :         return MATCH_ERROR;
   10991              : 
   10992         1966 :       if (!gfc_add_interface (sym))
   10993              :         return MATCH_ERROR;
   10994              : 
   10995         1963 :       sym->attr.mod_proc = 1;
   10996         1963 :       sym->declared_at = old_locus;
   10997              : 
   10998         1963 :       if (last)
   10999              :         break;
   11000              :     }
   11001              : 
   11002              :   return MATCH_YES;
   11003              : 
   11004            3 : syntax:
   11005              :   /* Restore the previous state of the interface.  */
   11006            3 :   interface = gfc_current_interface_head ();
   11007            3 :   gfc_set_current_interface_head (old_interface_head);
   11008              : 
   11009              :   /* Free the new interfaces.  */
   11010           10 :   while (interface != old_interface_head)
   11011              :   {
   11012            4 :     gfc_interface *i = interface->next;
   11013            4 :     free (interface);
   11014            4 :     interface = i;
   11015              :   }
   11016              : 
   11017              :   /* And issue a syntax error.  */
   11018            3 :   gfc_syntax_error (ST_MODULE_PROC);
   11019            3 :   return MATCH_ERROR;
   11020              : }
   11021              : 
   11022              : 
   11023              : /* Check a derived type that is being extended.  */
   11024              : 
   11025              : static gfc_symbol*
   11026         1701 : check_extended_derived_type (char *name)
   11027              : {
   11028         1701 :   gfc_symbol *extended;
   11029              : 
   11030         1701 :   if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
   11031              :     {
   11032            0 :       gfc_error ("Ambiguous symbol in TYPE definition at %C");
   11033            0 :       return NULL;
   11034              :     }
   11035              : 
   11036         1701 :   extended = gfc_find_dt_in_generic (extended);
   11037              : 
   11038              :   /* F08:C428.  */
   11039         1701 :   if (!extended)
   11040              :     {
   11041            2 :       gfc_error ("Symbol %qs at %C has not been previously defined", name);
   11042            2 :       return NULL;
   11043              :     }
   11044              : 
   11045         1699 :   if (extended->attr.flavor != FL_DERIVED)
   11046              :     {
   11047            0 :       gfc_error ("%qs in EXTENDS expression at %C is not a "
   11048              :                  "derived type", name);
   11049            0 :       return NULL;
   11050              :     }
   11051              : 
   11052         1699 :   if (extended->attr.is_bind_c)
   11053              :     {
   11054            1 :       gfc_error ("%qs cannot be extended at %C because it "
   11055              :                  "is BIND(C)", extended->name);
   11056            1 :       return NULL;
   11057              :     }
   11058              : 
   11059         1698 :   if (extended->attr.sequence)
   11060              :     {
   11061            1 :       gfc_error ("%qs cannot be extended at %C because it "
   11062              :                  "is a SEQUENCE type", extended->name);
   11063            1 :       return NULL;
   11064              :     }
   11065              : 
   11066              :   return extended;
   11067              : }
   11068              : 
   11069              : 
   11070              : /* Match the optional attribute specifiers for a type declaration.
   11071              :    Return MATCH_ERROR if an error is encountered in one of the handled
   11072              :    attributes (public, private, bind(c)), MATCH_NO if what's found is
   11073              :    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
   11074              :    checking on attribute conflicts needs to be done.  */
   11075              : 
   11076              : static match
   11077        21218 : gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
   11078              : {
   11079              :   /* See if the derived type is marked as private.  */
   11080        21218 :   if (gfc_match (" , private") == MATCH_YES)
   11081              :     {
   11082           15 :       if (gfc_current_state () != COMP_MODULE)
   11083              :         {
   11084            1 :           gfc_error ("Derived type at %C can only be PRIVATE in the "
   11085              :                      "specification part of a module");
   11086            1 :           return MATCH_ERROR;
   11087              :         }
   11088              : 
   11089           14 :       if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
   11090            0 :         return MATCH_ERROR;
   11091              :     }
   11092        21203 :   else if (gfc_match (" , public") == MATCH_YES)
   11093              :     {
   11094          558 :       if (gfc_current_state () != COMP_MODULE)
   11095              :         {
   11096            0 :           gfc_error ("Derived type at %C can only be PUBLIC in the "
   11097              :                      "specification part of a module");
   11098            0 :           return MATCH_ERROR;
   11099              :         }
   11100              : 
   11101          558 :       if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
   11102            0 :         return MATCH_ERROR;
   11103              :     }
   11104        20645 :   else if (gfc_match (" , bind ( c )") == MATCH_YES)
   11105              :     {
   11106              :       /* If the type is defined to be bind(c) it then needs to make
   11107              :          sure that all fields are interoperable.  This will
   11108              :          need to be a semantic check on the finished derived type.
   11109              :          See 15.2.3 (lines 9-12) of F2003 draft.  */
   11110          409 :       if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
   11111            0 :         return MATCH_ERROR;
   11112              : 
   11113              :       /* TODO: attr conflicts need to be checked, probably in symbol.cc.  */
   11114              :     }
   11115        20236 :   else if (gfc_match (" , abstract") == MATCH_YES)
   11116              :     {
   11117          349 :       if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
   11118              :         return MATCH_ERROR;
   11119              : 
   11120          348 :       if (!gfc_add_abstract (attr, &gfc_current_locus))
   11121            1 :         return MATCH_ERROR;
   11122              :     }
   11123        19887 :   else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
   11124              :     {
   11125         1702 :       if (!gfc_add_extension (attr, &gfc_current_locus))
   11126            0 :         return MATCH_ERROR;
   11127              :     }
   11128              :   else
   11129              :     return MATCH_NO;
   11130              : 
   11131              :   /* If we get here, something matched.  */
   11132              :   return MATCH_YES;
   11133              : }
   11134              : 
   11135              : 
   11136              : /* Common function for type declaration blocks similar to derived types, such
   11137              :    as STRUCTURES and MAPs. Unlike derived types, a structure type
   11138              :    does NOT have a generic symbol matching the name given by the user.
   11139              :    STRUCTUREs can share names with variables and PARAMETERs so we must allow
   11140              :    for the creation of an independent symbol.
   11141              :    Other parameters are a message to prefix errors with, the name of the new
   11142              :    type to be created, and the flavor to add to the resulting symbol. */
   11143              : 
   11144              : static bool
   11145          717 : get_struct_decl (const char *name, sym_flavor fl, locus *decl,
   11146              :                  gfc_symbol **result)
   11147              : {
   11148          717 :   gfc_symbol *sym;
   11149          717 :   locus where;
   11150              : 
   11151          717 :   gcc_assert (name[0] == (char) TOUPPER (name[0]));
   11152              : 
   11153          717 :   if (decl)
   11154          717 :     where = *decl;
   11155              :   else
   11156            0 :     where = gfc_current_locus;
   11157              : 
   11158          717 :   if (gfc_get_symbol (name, NULL, &sym))
   11159              :     return false;
   11160              : 
   11161          717 :   if (!sym)
   11162              :     {
   11163            0 :       gfc_internal_error ("Failed to create structure type '%s' at %C", name);
   11164              :       return false;
   11165              :     }
   11166              : 
   11167          717 :   if (sym->components != NULL || sym->attr.zero_comp)
   11168              :     {
   11169            3 :       gfc_error ("Type definition of %qs at %C was already defined at %L",
   11170              :                  sym->name, &sym->declared_at);
   11171            3 :       return false;
   11172              :     }
   11173              : 
   11174          714 :   sym->declared_at = where;
   11175              : 
   11176          714 :   if (sym->attr.flavor != fl
   11177          714 :       && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
   11178              :     return false;
   11179              : 
   11180          714 :   if (!sym->hash_value)
   11181              :       /* Set the hash for the compound name for this type.  */
   11182          713 :     sym->hash_value = gfc_hash_value (sym);
   11183              : 
   11184              :   /* Normally the type is expected to have been completely parsed by the time
   11185              :      a field declaration with this type is seen. For unions, maps, and nested
   11186              :      structure declarations, we need to indicate that it is okay that we
   11187              :      haven't seen any components yet. This will be updated after the structure
   11188              :      is fully parsed. */
   11189          714 :   sym->attr.zero_comp = 0;
   11190              : 
   11191              :   /* Structures always act like derived-types with the SEQUENCE attribute */
   11192          714 :   gfc_add_sequence (&sym->attr, sym->name, NULL);
   11193              : 
   11194          714 :   if (result) *result = sym;
   11195              : 
   11196              :   return true;
   11197              : }
   11198              : 
   11199              : 
   11200              : /* Match the opening of a MAP block. Like a struct within a union in C;
   11201              :    behaves identical to STRUCTURE blocks.  */
   11202              : 
   11203              : match
   11204          259 : gfc_match_map (void)
   11205              : {
   11206              :   /* Counter used to give unique internal names to map structures. */
   11207          259 :   static unsigned int gfc_map_id = 0;
   11208          259 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11209          259 :   gfc_symbol *sym;
   11210          259 :   locus old_loc;
   11211              : 
   11212          259 :   old_loc = gfc_current_locus;
   11213              : 
   11214          259 :   if (gfc_match_eos () != MATCH_YES)
   11215              :     {
   11216            1 :         gfc_error ("Junk after MAP statement at %C");
   11217            1 :         gfc_current_locus = old_loc;
   11218            1 :         return MATCH_ERROR;
   11219              :     }
   11220              : 
   11221              :   /* Map blocks are anonymous so we make up unique names for the symbol table
   11222              :      which are invalid Fortran identifiers.  */
   11223          258 :   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
   11224              : 
   11225          258 :   if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
   11226              :     return MATCH_ERROR;
   11227              : 
   11228          258 :   gfc_new_block = sym;
   11229              : 
   11230          258 :   return MATCH_YES;
   11231              : }
   11232              : 
   11233              : 
   11234              : /* Match the opening of a UNION block.  */
   11235              : 
   11236              : match
   11237          133 : gfc_match_union (void)
   11238              : {
   11239              :   /* Counter used to give unique internal names to union types. */
   11240          133 :   static unsigned int gfc_union_id = 0;
   11241          133 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11242          133 :   gfc_symbol *sym;
   11243          133 :   locus old_loc;
   11244              : 
   11245          133 :   old_loc = gfc_current_locus;
   11246              : 
   11247          133 :   if (gfc_match_eos () != MATCH_YES)
   11248              :     {
   11249            1 :         gfc_error ("Junk after UNION statement at %C");
   11250            1 :         gfc_current_locus = old_loc;
   11251            1 :         return MATCH_ERROR;
   11252              :     }
   11253              : 
   11254              :   /* Unions are anonymous so we make up unique names for the symbol table
   11255              :      which are invalid Fortran identifiers.  */
   11256          132 :   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
   11257              : 
   11258          132 :   if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
   11259              :     return MATCH_ERROR;
   11260              : 
   11261          132 :   gfc_new_block = sym;
   11262              : 
   11263          132 :   return MATCH_YES;
   11264              : }
   11265              : 
   11266              : 
   11267              : /* Match the beginning of a STRUCTURE declaration. This is similar to
   11268              :    matching the beginning of a derived type declaration with a few
   11269              :    twists. The resulting type symbol has no access control or other
   11270              :    interesting attributes.  */
   11271              : 
   11272              : match
   11273          336 : gfc_match_structure_decl (void)
   11274              : {
   11275              :   /* Counter used to give unique internal names to anonymous structures.  */
   11276          336 :   static unsigned int gfc_structure_id = 0;
   11277          336 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11278          336 :   gfc_symbol *sym;
   11279          336 :   match m;
   11280          336 :   locus where;
   11281              : 
   11282          336 :   if (!flag_dec_structure)
   11283              :     {
   11284            3 :       gfc_error ("%s at %C is a DEC extension, enable with "
   11285              :                  "%<-fdec-structure%>",
   11286              :                  "STRUCTURE");
   11287            3 :       return MATCH_ERROR;
   11288              :     }
   11289              : 
   11290          333 :   name[0] = '\0';
   11291              : 
   11292          333 :   m = gfc_match (" /%n/", name);
   11293          333 :   if (m != MATCH_YES)
   11294              :     {
   11295              :       /* Non-nested structure declarations require a structure name.  */
   11296           24 :       if (!gfc_comp_struct (gfc_current_state ()))
   11297              :         {
   11298            4 :             gfc_error ("Structure name expected in non-nested structure "
   11299              :                        "declaration at %C");
   11300            4 :             return MATCH_ERROR;
   11301              :         }
   11302              :       /* This is an anonymous structure; make up a unique name for it
   11303              :          (upper-case letters never make it to symbol names from the source).
   11304              :          The important thing is initializing the type variable
   11305              :          and setting gfc_new_symbol, which is immediately used by
   11306              :          parse_structure () and variable_decl () to add components of
   11307              :          this type.  */
   11308           20 :       snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
   11309              :     }
   11310              : 
   11311          329 :   where = gfc_current_locus;
   11312              :   /* No field list allowed after non-nested structure declaration.  */
   11313          329 :   if (!gfc_comp_struct (gfc_current_state ())
   11314          296 :       && gfc_match_eos () != MATCH_YES)
   11315              :     {
   11316            1 :       gfc_error ("Junk after non-nested STRUCTURE statement at %C");
   11317            1 :       return MATCH_ERROR;
   11318              :     }
   11319              : 
   11320              :   /* Make sure the name is not the name of an intrinsic type.  */
   11321          328 :   if (gfc_is_intrinsic_typename (name))
   11322              :     {
   11323            1 :       gfc_error ("Structure name %qs at %C cannot be the same as an"
   11324              :                  " intrinsic type", name);
   11325            1 :       return MATCH_ERROR;
   11326              :     }
   11327              : 
   11328              :   /* Store the actual type symbol for the structure with an upper-case first
   11329              :      letter (an invalid Fortran identifier).  */
   11330              : 
   11331          327 :   if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
   11332              :     return MATCH_ERROR;
   11333              : 
   11334          324 :   gfc_new_block = sym;
   11335          324 :   return MATCH_YES;
   11336              : }
   11337              : 
   11338              : 
   11339              : /* This function does some work to determine which matcher should be used to
   11340              :  * match a statement beginning with "TYPE".  This is used to disambiguate TYPE
   11341              :  * as an alias for PRINT from derived type declarations, TYPE IS statements,
   11342              :  * and [parameterized] derived type declarations.  */
   11343              : 
   11344              : match
   11345       552077 : gfc_match_type (gfc_statement *st)
   11346              : {
   11347       552077 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11348       552077 :   match m;
   11349       552077 :   locus old_loc;
   11350              : 
   11351              :   /* Requires -fdec.  */
   11352       552077 :   if (!flag_dec)
   11353              :     return MATCH_NO;
   11354              : 
   11355         2483 :   m = gfc_match ("type");
   11356         2483 :   if (m != MATCH_YES)
   11357              :     return m;
   11358              :   /* If we already have an error in the buffer, it is probably from failing to
   11359              :    * match a derived type data declaration. Let it happen.  */
   11360           20 :   else if (gfc_error_flag_test ())
   11361              :     return MATCH_NO;
   11362              : 
   11363           20 :   old_loc = gfc_current_locus;
   11364           20 :   *st = ST_NONE;
   11365              : 
   11366              :   /* If we see an attribute list before anything else it's definitely a derived
   11367              :    * type declaration.  */
   11368           20 :   if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
   11369            8 :     goto derived;
   11370              : 
   11371              :   /* By now "TYPE" has already been matched. If we do not see a name, this may
   11372              :    * be something like "TYPE *" or "TYPE <fmt>".  */
   11373           12 :   m = gfc_match_name (name);
   11374           12 :   if (m != MATCH_YES)
   11375              :     {
   11376              :       /* Let print match if it can, otherwise throw an error from
   11377              :        * gfc_match_derived_decl.  */
   11378            7 :       gfc_current_locus = old_loc;
   11379            7 :       if (gfc_match_print () == MATCH_YES)
   11380              :         {
   11381            7 :           *st = ST_WRITE;
   11382            7 :           return MATCH_YES;
   11383              :         }
   11384            0 :       goto derived;
   11385              :     }
   11386              : 
   11387              :   /* Check for EOS.  */
   11388            5 :   if (gfc_match_eos () == MATCH_YES)
   11389              :     {
   11390              :       /* By now we have "TYPE <name> <EOS>". Check first if the name is an
   11391              :        * intrinsic typename - if so let gfc_match_derived_decl dump an error.
   11392              :        * Otherwise if gfc_match_derived_decl fails it's probably an existing
   11393              :        * symbol which can be printed.  */
   11394            3 :       gfc_current_locus = old_loc;
   11395            3 :       m = gfc_match_derived_decl ();
   11396            3 :       if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
   11397              :         {
   11398            2 :           *st = ST_DERIVED_DECL;
   11399            2 :           return m;
   11400              :         }
   11401              :     }
   11402              :   else
   11403              :     {
   11404              :       /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
   11405              :          like <type name(parameter)>.  */
   11406            2 :       gfc_gobble_whitespace ();
   11407            2 :       bool paren = gfc_peek_ascii_char () == '(';
   11408            2 :       if (paren)
   11409              :         {
   11410            1 :           if (strcmp ("is", name) == 0)
   11411            1 :             goto typeis;
   11412              :           else
   11413            0 :             goto derived;
   11414              :         }
   11415              :     }
   11416              : 
   11417              :   /* Treat TYPE... like PRINT...  */
   11418            2 :   gfc_current_locus = old_loc;
   11419            2 :   *st = ST_WRITE;
   11420            2 :   return gfc_match_print ();
   11421              : 
   11422            8 : derived:
   11423            8 :   gfc_current_locus = old_loc;
   11424            8 :   *st = ST_DERIVED_DECL;
   11425            8 :   return gfc_match_derived_decl ();
   11426              : 
   11427            1 : typeis:
   11428            1 :   gfc_current_locus = old_loc;
   11429            1 :   *st = ST_TYPE_IS;
   11430            1 :   return gfc_match_type_is ();
   11431              : }
   11432              : 
   11433              : 
   11434              : /* Match the beginning of a derived type declaration.  If a type name
   11435              :    was the result of a function, then it is possible to have a symbol
   11436              :    already to be known as a derived type yet have no components.  */
   11437              : 
   11438              : match
   11439        18192 : gfc_match_derived_decl (void)
   11440              : {
   11441        18192 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11442        18192 :   char parent[GFC_MAX_SYMBOL_LEN + 1];
   11443        18192 :   symbol_attribute attr;
   11444        18192 :   gfc_symbol *sym, *gensym;
   11445        18192 :   gfc_symbol *extended;
   11446        18192 :   match m;
   11447        18192 :   match is_type_attr_spec = MATCH_NO;
   11448        18192 :   bool seen_attr = false;
   11449        18192 :   gfc_interface *intr = NULL, *head;
   11450        18192 :   bool parameterized_type = false;
   11451        18192 :   bool seen_colons = false;
   11452              : 
   11453        18192 :   if (gfc_comp_struct (gfc_current_state ()))
   11454              :     return MATCH_NO;
   11455              : 
   11456        18188 :   name[0] = '\0';
   11457        18188 :   parent[0] = '\0';
   11458        18188 :   gfc_clear_attr (&attr);
   11459        18188 :   extended = NULL;
   11460              : 
   11461        21218 :   do
   11462              :     {
   11463        21218 :       is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
   11464        21218 :       if (is_type_attr_spec == MATCH_ERROR)
   11465              :         return MATCH_ERROR;
   11466        21215 :       if (is_type_attr_spec == MATCH_YES)
   11467         3030 :         seen_attr = true;
   11468        21215 :     } while (is_type_attr_spec == MATCH_YES);
   11469              : 
   11470              :   /* Deal with derived type extensions.  The extension attribute has
   11471              :      been added to 'attr' but now the parent type must be found and
   11472              :      checked.  */
   11473        18185 :   if (parent[0])
   11474         1701 :     extended = check_extended_derived_type (parent);
   11475              : 
   11476        18185 :   if (parent[0] && !extended)
   11477              :     return MATCH_ERROR;
   11478              : 
   11479        18181 :   m = gfc_match (" ::");
   11480        18181 :   if (m == MATCH_YES)
   11481              :     {
   11482              :       seen_colons = true;
   11483              :     }
   11484        11116 :   else if (seen_attr)
   11485              :     {
   11486            5 :       gfc_error ("Expected :: in TYPE definition at %C");
   11487            5 :       return MATCH_ERROR;
   11488              :     }
   11489              : 
   11490              :   /*  In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
   11491              :       But, we need to simply return for TYPE(.  */
   11492        11111 :   if (m == MATCH_NO && gfc_current_form == FORM_FREE)
   11493              :     {
   11494        11062 :       char c = gfc_peek_ascii_char ();
   11495        11062 :       if (c == '(')
   11496              :         return m;
   11497        10981 :       if (!gfc_is_whitespace (c))
   11498              :         {
   11499            4 :           gfc_error ("Mangled derived type definition at %C");
   11500            4 :           return MATCH_NO;
   11501              :         }
   11502              :     }
   11503              : 
   11504        18091 :   m = gfc_match (" %n ", name);
   11505        18091 :   if (m != MATCH_YES)
   11506              :     return m;
   11507              : 
   11508              :   /* Make sure that we don't identify TYPE IS (...) as a parameterized
   11509              :      derived type named 'is'.
   11510              :      TODO Expand the check, when 'name' = "is" by matching " (tname) "
   11511              :      and checking if this is a(n intrinsic) typename.  This picks up
   11512              :      misplaced TYPE IS statements such as in select_type_1.f03.  */
   11513        18079 :   if (gfc_peek_ascii_char () == '(')
   11514              :     {
   11515         4517 :       if (gfc_current_state () == COMP_SELECT_TYPE
   11516          779 :           || (!seen_colons && !strcmp (name, "is")))
   11517              :         return MATCH_NO;
   11518              :       parameterized_type = true;
   11519              :     }
   11520              : 
   11521        14339 :   m = gfc_match_eos ();
   11522        14339 :   if (m != MATCH_YES && !parameterized_type)
   11523              :     return m;
   11524              : 
   11525              :   /* Make sure the name is not the name of an intrinsic type.  */
   11526        14336 :   if (gfc_is_intrinsic_typename (name))
   11527              :     {
   11528           18 :       gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
   11529              :                  "type", name);
   11530           18 :       return MATCH_ERROR;
   11531              :     }
   11532              : 
   11533        14318 :   if (gfc_get_symbol (name, NULL, &gensym))
   11534              :     return MATCH_ERROR;
   11535              : 
   11536        14318 :   if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
   11537              :     {
   11538            5 :       if (gensym->ts.u.derived)
   11539            0 :         gfc_error ("Derived type name %qs at %C already has a basic type "
   11540              :                    "of %s", gensym->name, gfc_typename (&gensym->ts));
   11541              :       else
   11542            5 :         gfc_error ("Derived type name %qs at %C already has a basic type",
   11543              :                    gensym->name);
   11544              :       return MATCH_ERROR;
   11545              :     }
   11546              : 
   11547        14313 :   if (!gensym->attr.generic
   11548        14313 :       && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
   11549              :     return MATCH_ERROR;
   11550              : 
   11551        14309 :   if (!gensym->attr.function
   11552        14309 :       && !gfc_add_function (&gensym->attr, gensym->name, NULL))
   11553              :     return MATCH_ERROR;
   11554              : 
   11555        14308 :   if (gensym->attr.dummy)
   11556              :     {
   11557            1 :       gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
   11558              :                  name, &gensym->declared_at);
   11559            1 :       return MATCH_ERROR;
   11560              :     }
   11561              : 
   11562        14307 :   sym = gfc_find_dt_in_generic (gensym);
   11563              : 
   11564        14307 :   if (sym && (sym->components != NULL || sym->attr.zero_comp))
   11565              :     {
   11566            1 :       gfc_error ("Derived type definition of %qs at %C has already been "
   11567              :                  "defined", sym->name);
   11568            1 :       return MATCH_ERROR;
   11569              :     }
   11570              : 
   11571        14306 :   if (!sym)
   11572              :     {
   11573              :       /* Use upper case to save the actual derived-type symbol.  */
   11574        14216 :       gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
   11575        14216 :       sym->name = gfc_get_string ("%s", gensym->name);
   11576        14216 :       head = gensym->generic;
   11577        14216 :       intr = gfc_get_interface ();
   11578        14216 :       intr->sym = sym;
   11579        14216 :       intr->where = gfc_current_locus;
   11580        14216 :       intr->sym->declared_at = gfc_current_locus;
   11581        14216 :       intr->next = head;
   11582        14216 :       gensym->generic = intr;
   11583        14216 :       gensym->attr.if_source = IFSRC_DECL;
   11584              :     }
   11585              : 
   11586              :   /* The symbol may already have the derived attribute without the
   11587              :      components.  The ways this can happen is via a function
   11588              :      definition, an INTRINSIC statement or a subtype in another
   11589              :      derived type that is a pointer.  The first part of the AND clause
   11590              :      is true if the symbol is not the return value of a function.  */
   11591        14306 :   if (sym->attr.flavor != FL_DERIVED
   11592        14306 :       && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
   11593              :     return MATCH_ERROR;
   11594              : 
   11595        14306 :   if (attr.access != ACCESS_UNKNOWN
   11596        14306 :       && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
   11597              :     return MATCH_ERROR;
   11598        14306 :   else if (sym->attr.access == ACCESS_UNKNOWN
   11599        13738 :            && gensym->attr.access != ACCESS_UNKNOWN
   11600        14654 :            && !gfc_add_access (&sym->attr, gensym->attr.access,
   11601              :                                sym->name, NULL))
   11602              :     return MATCH_ERROR;
   11603              : 
   11604        14306 :   if (sym->attr.access != ACCESS_UNKNOWN
   11605          916 :       && gensym->attr.access == ACCESS_UNKNOWN)
   11606          568 :     gensym->attr.access = sym->attr.access;
   11607              : 
   11608              :   /* See if the derived type was labeled as bind(c).  */
   11609        14306 :   if (attr.is_bind_c != 0)
   11610          406 :     sym->attr.is_bind_c = attr.is_bind_c;
   11611              : 
   11612              :   /* Construct the f2k_derived namespace if it is not yet there.  */
   11613        14306 :   if (!sym->f2k_derived)
   11614        14306 :     sym->f2k_derived = gfc_get_namespace (NULL, 0);
   11615              : 
   11616        14306 :   if (parameterized_type)
   11617              :     {
   11618              :       /* Ignore error or mismatches by going to the end of the statement
   11619              :          in order to avoid the component declarations causing problems.  */
   11620          777 :       m = gfc_match_formal_arglist (sym, 0, 0, true);
   11621          777 :       if (m != MATCH_YES)
   11622            4 :         gfc_error_recovery ();
   11623              :       else
   11624          773 :         sym->attr.pdt_template = 1;
   11625          777 :       m = gfc_match_eos ();
   11626          777 :       if (m != MATCH_YES)
   11627              :         {
   11628            1 :           gfc_error_recovery ();
   11629            1 :           gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
   11630              :         }
   11631              :     }
   11632              : 
   11633        14306 :   if (extended && !sym->components)
   11634              :     {
   11635         1697 :       gfc_component *p;
   11636         1697 :       gfc_formal_arglist *f, *g, *h;
   11637              : 
   11638              :       /* Add the extended derived type as the first component.  */
   11639         1697 :       gfc_add_component (sym, parent, &p);
   11640         1697 :       extended->refs++;
   11641         1697 :       gfc_set_sym_referenced (extended);
   11642              : 
   11643         1697 :       p->ts.type = BT_DERIVED;
   11644         1697 :       p->ts.u.derived = extended;
   11645         1697 :       p->initializer = gfc_default_initializer (&p->ts);
   11646              : 
   11647              :       /* Set extension level.  */
   11648         1697 :       if (extended->attr.extension == 255)
   11649              :         {
   11650              :           /* Since the extension field is 8 bit wide, we can only have
   11651              :              up to 255 extension levels.  */
   11652            0 :           gfc_error ("Maximum extension level reached with type %qs at %L",
   11653              :                      extended->name, &extended->declared_at);
   11654            0 :           return MATCH_ERROR;
   11655              :         }
   11656         1697 :       sym->attr.extension = extended->attr.extension + 1;
   11657              : 
   11658              :       /* Provide the links between the extended type and its extension.  */
   11659         1697 :       if (!extended->f2k_derived)
   11660            1 :         extended->f2k_derived = gfc_get_namespace (NULL, 0);
   11661              : 
   11662              :       /* Copy the extended type-param-name-list from the extended type,
   11663              :          append those of the extension and add the whole lot to the
   11664              :          extension.  */
   11665         1697 :       if (extended->attr.pdt_template)
   11666              :         {
   11667           88 :           g = h = NULL;
   11668           88 :           sym->attr.pdt_template = 1;
   11669          255 :           for (f = extended->formal; f; f = f->next)
   11670              :             {
   11671          167 :               if (f == extended->formal)
   11672              :                 {
   11673           88 :                   g = gfc_get_formal_arglist ();
   11674           88 :                   h = g;
   11675              :                 }
   11676              :               else
   11677              :                 {
   11678           79 :                   g->next = gfc_get_formal_arglist ();
   11679           79 :                   g = g->next;
   11680              :                 }
   11681          167 :               g->sym = f->sym;
   11682              :             }
   11683           88 :           g->next = sym->formal;
   11684           88 :           sym->formal = h;
   11685              :         }
   11686              :     }
   11687              : 
   11688        14306 :   if (!sym->hash_value)
   11689              :     /* Set the hash for the compound name for this type.  */
   11690        14306 :     sym->hash_value = gfc_hash_value (sym);
   11691              : 
   11692              :   /* Take over the ABSTRACT attribute.  */
   11693        14306 :   sym->attr.abstract = attr.abstract;
   11694              : 
   11695        14306 :   gfc_new_block = sym;
   11696              : 
   11697        14306 :   return MATCH_YES;
   11698              : }
   11699              : 
   11700              : 
   11701              : /* Cray Pointees can be declared as:
   11702              :       pointer (ipt, a (n,m,...,*))  */
   11703              : 
   11704              : match
   11705          240 : gfc_mod_pointee_as (gfc_array_spec *as)
   11706              : {
   11707          240 :   as->cray_pointee = true; /* This will be useful to know later.  */
   11708          240 :   if (as->type == AS_ASSUMED_SIZE)
   11709           72 :     as->cp_was_assumed = true;
   11710          168 :   else if (as->type == AS_ASSUMED_SHAPE)
   11711              :     {
   11712            0 :       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
   11713            0 :       return MATCH_ERROR;
   11714              :     }
   11715              :   return MATCH_YES;
   11716              : }
   11717              : 
   11718              : 
   11719              : /* Match the enum definition statement, here we are trying to match
   11720              :    the first line of enum definition statement.
   11721              :    Returns MATCH_YES if match is found.  */
   11722              : 
   11723              : match
   11724          158 : gfc_match_enum (void)
   11725              : {
   11726          158 :   match m;
   11727              : 
   11728          158 :   m = gfc_match_eos ();
   11729          158 :   if (m != MATCH_YES)
   11730              :     return m;
   11731              : 
   11732          158 :   if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
   11733            0 :     return MATCH_ERROR;
   11734              : 
   11735              :   return MATCH_YES;
   11736              : }
   11737              : 
   11738              : 
   11739              : /* Returns an initializer whose value is one higher than the value of the
   11740              :    LAST_INITIALIZER argument.  If the argument is NULL, the
   11741              :    initializers value will be set to zero.  The initializer's kind
   11742              :    will be set to gfc_c_int_kind.
   11743              : 
   11744              :    If -fshort-enums is given, the appropriate kind will be selected
   11745              :    later after all enumerators have been parsed.  A warning is issued
   11746              :    here if an initializer exceeds gfc_c_int_kind.  */
   11747              : 
   11748              : static gfc_expr *
   11749          377 : enum_initializer (gfc_expr *last_initializer, locus where)
   11750              : {
   11751          377 :   gfc_expr *result;
   11752          377 :   result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
   11753              : 
   11754          377 :   mpz_init (result->value.integer);
   11755              : 
   11756          377 :   if (last_initializer != NULL)
   11757              :     {
   11758          266 :       mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
   11759          266 :       result->where = last_initializer->where;
   11760              : 
   11761          266 :       if (gfc_check_integer_range (result->value.integer,
   11762              :              gfc_c_int_kind) != ARITH_OK)
   11763              :         {
   11764            0 :           gfc_error ("Enumerator exceeds the C integer type at %C");
   11765            0 :           return NULL;
   11766              :         }
   11767              :     }
   11768              :   else
   11769              :     {
   11770              :       /* Control comes here, if it's the very first enumerator and no
   11771              :          initializer has been given.  It will be initialized to zero.  */
   11772          111 :       mpz_set_si (result->value.integer, 0);
   11773              :     }
   11774              : 
   11775              :   return result;
   11776              : }
   11777              : 
   11778              : 
   11779              : /* Match a variable name with an optional initializer.  When this
   11780              :    subroutine is called, a variable is expected to be parsed next.
   11781              :    Depending on what is happening at the moment, updates either the
   11782              :    symbol table or the current interface.  */
   11783              : 
   11784              : static match
   11785          549 : enumerator_decl (void)
   11786              : {
   11787          549 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11788          549 :   gfc_expr *initializer;
   11789          549 :   gfc_array_spec *as = NULL;
   11790          549 :   gfc_charlen *saved_cl_list;
   11791          549 :   gfc_symbol *sym;
   11792          549 :   locus var_locus;
   11793          549 :   match m;
   11794          549 :   bool t;
   11795          549 :   locus old_locus;
   11796              : 
   11797          549 :   initializer = NULL;
   11798          549 :   saved_cl_list = gfc_current_ns->cl_list;
   11799          549 :   old_locus = gfc_current_locus;
   11800              : 
   11801              :   /* When we get here, we've just matched a list of attributes and
   11802              :      maybe a type and a double colon.  The next thing we expect to see
   11803              :      is the name of the symbol.  */
   11804          549 :   m = gfc_match_name (name);
   11805          549 :   if (m != MATCH_YES)
   11806            1 :     goto cleanup;
   11807              : 
   11808          548 :   var_locus = gfc_current_locus;
   11809              : 
   11810              :   /* OK, we've successfully matched the declaration.  Now put the
   11811              :      symbol in the current namespace. If we fail to create the symbol,
   11812              :      bail out.  */
   11813          548 :   if (!build_sym (name, 1, NULL, false, &as, &var_locus))
   11814              :     {
   11815            1 :       m = MATCH_ERROR;
   11816            1 :       goto cleanup;
   11817              :     }
   11818              : 
   11819              :   /* The double colon must be present in order to have initializers.
   11820              :      Otherwise the statement is ambiguous with an assignment statement.  */
   11821          547 :   if (colon_seen)
   11822              :     {
   11823          471 :       if (gfc_match_char ('=') == MATCH_YES)
   11824              :         {
   11825          170 :           m = gfc_match_init_expr (&initializer);
   11826          170 :           if (m == MATCH_NO)
   11827              :             {
   11828            0 :               gfc_error ("Expected an initialization expression at %C");
   11829            0 :               m = MATCH_ERROR;
   11830              :             }
   11831              : 
   11832          170 :           if (m != MATCH_YES)
   11833            2 :             goto cleanup;
   11834              :         }
   11835              :     }
   11836              : 
   11837              :   /* If we do not have an initializer, the initialization value of the
   11838              :      previous enumerator (stored in last_initializer) is incremented
   11839              :      by 1 and is used to initialize the current enumerator.  */
   11840          545 :   if (initializer == NULL)
   11841          377 :     initializer = enum_initializer (last_initializer, old_locus);
   11842              : 
   11843          545 :   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
   11844              :     {
   11845            2 :       gfc_error ("ENUMERATOR %L not initialized with integer expression",
   11846              :                  &var_locus);
   11847            2 :       m = MATCH_ERROR;
   11848            2 :       goto cleanup;
   11849              :     }
   11850              : 
   11851              :   /* Store this current initializer, for the next enumerator variable
   11852              :      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
   11853              :      use last_initializer below.  */
   11854          543 :   last_initializer = initializer;
   11855          543 :   t = add_init_expr_to_sym (name, &initializer, &var_locus,
   11856              :                             saved_cl_list);
   11857              : 
   11858              :   /* Maintain enumerator history.  */
   11859          543 :   gfc_find_symbol (name, NULL, 0, &sym);
   11860          543 :   create_enum_history (sym, last_initializer);
   11861              : 
   11862          543 :   return (t) ? MATCH_YES : MATCH_ERROR;
   11863              : 
   11864            6 : cleanup:
   11865              :   /* Free stuff up and return.  */
   11866            6 :   gfc_free_expr (initializer);
   11867              : 
   11868            6 :   return m;
   11869              : }
   11870              : 
   11871              : 
   11872              : /* Match the enumerator definition statement.  */
   11873              : 
   11874              : match
   11875       839913 : gfc_match_enumerator_def (void)
   11876              : {
   11877       839913 :   match m;
   11878       839913 :   bool t;
   11879              : 
   11880       839913 :   gfc_clear_ts (&current_ts);
   11881              : 
   11882       839913 :   m = gfc_match (" enumerator");
   11883       839913 :   if (m != MATCH_YES)
   11884              :     return m;
   11885              : 
   11886          269 :   m = gfc_match (" :: ");
   11887          269 :   if (m == MATCH_ERROR)
   11888              :     return m;
   11889              : 
   11890          269 :   colon_seen = (m == MATCH_YES);
   11891              : 
   11892          269 :   if (gfc_current_state () != COMP_ENUM)
   11893              :     {
   11894            4 :       gfc_error ("ENUM definition statement expected before %C");
   11895            4 :       gfc_free_enum_history ();
   11896            4 :       return MATCH_ERROR;
   11897              :     }
   11898              : 
   11899          265 :   (&current_ts)->type = BT_INTEGER;
   11900          265 :   (&current_ts)->kind = gfc_c_int_kind;
   11901              : 
   11902          265 :   gfc_clear_attr (&current_attr);
   11903          265 :   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
   11904          265 :   if (!t)
   11905              :     {
   11906            0 :       m = MATCH_ERROR;
   11907            0 :       goto cleanup;
   11908              :     }
   11909              : 
   11910          549 :   for (;;)
   11911              :     {
   11912          549 :       m = enumerator_decl ();
   11913          549 :       if (m == MATCH_ERROR)
   11914              :         {
   11915            6 :           gfc_free_enum_history ();
   11916            6 :           goto cleanup;
   11917              :         }
   11918          543 :       if (m == MATCH_NO)
   11919              :         break;
   11920              : 
   11921          542 :       if (gfc_match_eos () == MATCH_YES)
   11922          256 :         goto cleanup;
   11923          286 :       if (gfc_match_char (',') != MATCH_YES)
   11924              :         break;
   11925              :     }
   11926              : 
   11927            3 :   if (gfc_current_state () == COMP_ENUM)
   11928              :     {
   11929            3 :       gfc_free_enum_history ();
   11930            3 :       gfc_error ("Syntax error in ENUMERATOR definition at %C");
   11931            3 :       m = MATCH_ERROR;
   11932              :     }
   11933              : 
   11934            0 : cleanup:
   11935          265 :   gfc_free_array_spec (current_as);
   11936          265 :   current_as = NULL;
   11937          265 :   return m;
   11938              : 
   11939              : }
   11940              : 
   11941              : 
   11942              : /* Match binding attributes.  */
   11943              : 
   11944              : static match
   11945         4853 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
   11946              : {
   11947         4853 :   bool found_passing = false;
   11948         4853 :   bool seen_ptr = false;
   11949         4853 :   match m = MATCH_YES;
   11950              : 
   11951              :   /* Initialize to defaults.  Do so even before the MATCH_NO check so that in
   11952              :      this case the defaults are in there.  */
   11953         4853 :   ba->access = ACCESS_UNKNOWN;
   11954         4853 :   ba->pass_arg = NULL;
   11955         4853 :   ba->pass_arg_num = 0;
   11956         4853 :   ba->nopass = 0;
   11957         4853 :   ba->non_overridable = 0;
   11958         4853 :   ba->deferred = 0;
   11959         4853 :   ba->ppc = ppc;
   11960              : 
   11961              :   /* If we find a comma, we believe there are binding attributes.  */
   11962         4853 :   m = gfc_match_char (',');
   11963         4853 :   if (m == MATCH_NO)
   11964         2566 :     goto done;
   11965              : 
   11966         2861 :   do
   11967              :     {
   11968              :       /* Access specifier.  */
   11969              : 
   11970         2861 :       m = gfc_match (" public");
   11971         2861 :       if (m == MATCH_ERROR)
   11972            0 :         goto error;
   11973         2861 :       if (m == MATCH_YES)
   11974              :         {
   11975          250 :           if (ba->access != ACCESS_UNKNOWN)
   11976              :             {
   11977            0 :               gfc_error ("Duplicate access-specifier at %C");
   11978            0 :               goto error;
   11979              :             }
   11980              : 
   11981          250 :           ba->access = ACCESS_PUBLIC;
   11982          250 :           continue;
   11983              :         }
   11984              : 
   11985         2611 :       m = gfc_match (" private");
   11986         2611 :       if (m == MATCH_ERROR)
   11987            0 :         goto error;
   11988         2611 :       if (m == MATCH_YES)
   11989              :         {
   11990          187 :           if (ba->access != ACCESS_UNKNOWN)
   11991              :             {
   11992            1 :               gfc_error ("Duplicate access-specifier at %C");
   11993            1 :               goto error;
   11994              :             }
   11995              : 
   11996          186 :           ba->access = ACCESS_PRIVATE;
   11997          186 :           continue;
   11998              :         }
   11999              : 
   12000              :       /* If inside GENERIC, the following is not allowed.  */
   12001         2424 :       if (!generic)
   12002              :         {
   12003              : 
   12004              :           /* NOPASS flag.  */
   12005         2423 :           m = gfc_match (" nopass");
   12006         2423 :           if (m == MATCH_ERROR)
   12007            0 :             goto error;
   12008         2423 :           if (m == MATCH_YES)
   12009              :             {
   12010          744 :               if (found_passing)
   12011              :                 {
   12012            1 :                   gfc_error ("Binding attributes already specify passing,"
   12013              :                              " illegal NOPASS at %C");
   12014            1 :                   goto error;
   12015              :                 }
   12016              : 
   12017          743 :               found_passing = true;
   12018          743 :               ba->nopass = 1;
   12019          743 :               continue;
   12020              :             }
   12021              : 
   12022              :           /* PASS possibly including argument.  */
   12023         1679 :           m = gfc_match (" pass");
   12024         1679 :           if (m == MATCH_ERROR)
   12025            0 :             goto error;
   12026         1679 :           if (m == MATCH_YES)
   12027              :             {
   12028          901 :               char arg[GFC_MAX_SYMBOL_LEN + 1];
   12029              : 
   12030          901 :               if (found_passing)
   12031              :                 {
   12032            2 :                   gfc_error ("Binding attributes already specify passing,"
   12033              :                              " illegal PASS at %C");
   12034            2 :                   goto error;
   12035              :                 }
   12036              : 
   12037          899 :               m = gfc_match (" ( %n )", arg);
   12038          899 :               if (m == MATCH_ERROR)
   12039            0 :                 goto error;
   12040          899 :               if (m == MATCH_YES)
   12041          490 :                 ba->pass_arg = gfc_get_string ("%s", arg);
   12042          899 :               gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
   12043              : 
   12044          899 :               found_passing = true;
   12045          899 :               ba->nopass = 0;
   12046          899 :               continue;
   12047          899 :             }
   12048              : 
   12049          778 :           if (ppc)
   12050              :             {
   12051              :               /* POINTER flag.  */
   12052          456 :               m = gfc_match (" pointer");
   12053          456 :               if (m == MATCH_ERROR)
   12054            0 :                 goto error;
   12055          456 :               if (m == MATCH_YES)
   12056              :                 {
   12057          456 :                   if (seen_ptr)
   12058              :                     {
   12059            1 :                       gfc_error ("Duplicate POINTER attribute at %C");
   12060            1 :                       goto error;
   12061              :                     }
   12062              : 
   12063          455 :                   seen_ptr = true;
   12064          455 :                   continue;
   12065              :                 }
   12066              :             }
   12067              :           else
   12068              :             {
   12069              :               /* NON_OVERRIDABLE flag.  */
   12070          322 :               m = gfc_match (" non_overridable");
   12071          322 :               if (m == MATCH_ERROR)
   12072            0 :                 goto error;
   12073          322 :               if (m == MATCH_YES)
   12074              :                 {
   12075           62 :                   if (ba->non_overridable)
   12076              :                     {
   12077            1 :                       gfc_error ("Duplicate NON_OVERRIDABLE at %C");
   12078            1 :                       goto error;
   12079              :                     }
   12080              : 
   12081           61 :                   ba->non_overridable = 1;
   12082           61 :                   continue;
   12083              :                 }
   12084              : 
   12085              :               /* DEFERRED flag.  */
   12086          260 :               m = gfc_match (" deferred");
   12087          260 :               if (m == MATCH_ERROR)
   12088            0 :                 goto error;
   12089          260 :               if (m == MATCH_YES)
   12090              :                 {
   12091          260 :                   if (ba->deferred)
   12092              :                     {
   12093            1 :                       gfc_error ("Duplicate DEFERRED at %C");
   12094            1 :                       goto error;
   12095              :                     }
   12096              : 
   12097          259 :                   ba->deferred = 1;
   12098          259 :                   continue;
   12099              :                 }
   12100              :             }
   12101              : 
   12102              :         }
   12103              : 
   12104              :       /* Nothing matching found.  */
   12105            1 :       if (generic)
   12106            1 :         gfc_error ("Expected access-specifier at %C");
   12107              :       else
   12108            0 :         gfc_error ("Expected binding attribute at %C");
   12109            1 :       goto error;
   12110              :     }
   12111         2853 :   while (gfc_match_char (',') == MATCH_YES);
   12112              : 
   12113              :   /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
   12114         2279 :   if (ba->non_overridable && ba->deferred)
   12115              :     {
   12116            1 :       gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
   12117            1 :       goto error;
   12118              :     }
   12119              : 
   12120              :   m = MATCH_YES;
   12121              : 
   12122         4844 : done:
   12123         4844 :   if (ba->access == ACCESS_UNKNOWN)
   12124         4409 :     ba->access = ppc ? gfc_current_block()->component_access
   12125              :                      : gfc_typebound_default_access;
   12126              : 
   12127         4844 :   if (ppc && !seen_ptr)
   12128              :     {
   12129            2 :       gfc_error ("POINTER attribute is required for procedure pointer component"
   12130              :                  " at %C");
   12131            2 :       goto error;
   12132              :     }
   12133              : 
   12134              :   return m;
   12135              : 
   12136         4853 : error:
   12137              :   return MATCH_ERROR;
   12138              : }
   12139              : 
   12140              : 
   12141              : /* Match a PROCEDURE specific binding inside a derived type.  */
   12142              : 
   12143              : static match
   12144         3326 : match_procedure_in_type (void)
   12145              : {
   12146         3326 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12147         3326 :   char target_buf[GFC_MAX_SYMBOL_LEN + 1];
   12148         3326 :   char* target = NULL, *ifc = NULL;
   12149         3326 :   gfc_typebound_proc tb;
   12150         3326 :   bool seen_colons;
   12151         3326 :   bool seen_attrs;
   12152         3326 :   match m;
   12153         3326 :   gfc_symtree* stree;
   12154         3326 :   gfc_namespace* ns;
   12155         3326 :   gfc_symbol* block;
   12156         3326 :   int num;
   12157              : 
   12158              :   /* Check current state.  */
   12159         3326 :   gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
   12160         3326 :   block = gfc_state_stack->previous->sym;
   12161         3326 :   gcc_assert (block);
   12162              : 
   12163              :   /* Try to match PROCEDURE(interface).  */
   12164         3326 :   if (gfc_match (" (") == MATCH_YES)
   12165              :     {
   12166          261 :       m = gfc_match_name (target_buf);
   12167          261 :       if (m == MATCH_ERROR)
   12168              :         return m;
   12169          261 :       if (m != MATCH_YES)
   12170              :         {
   12171            1 :           gfc_error ("Interface-name expected after %<(%> at %C");
   12172            1 :           return MATCH_ERROR;
   12173              :         }
   12174              : 
   12175          260 :       if (gfc_match (" )") != MATCH_YES)
   12176              :         {
   12177            1 :           gfc_error ("%<)%> expected at %C");
   12178            1 :           return MATCH_ERROR;
   12179              :         }
   12180              : 
   12181              :       ifc = target_buf;
   12182              :     }
   12183              : 
   12184              :   /* Construct the data structure.  */
   12185         3324 :   memset (&tb, 0, sizeof (tb));
   12186         3324 :   tb.where = gfc_current_locus;
   12187              : 
   12188              :   /* Match binding attributes.  */
   12189         3324 :   m = match_binding_attributes (&tb, false, false);
   12190         3324 :   if (m == MATCH_ERROR)
   12191              :     return m;
   12192         3317 :   seen_attrs = (m == MATCH_YES);
   12193              : 
   12194              :   /* Check that attribute DEFERRED is given if an interface is specified.  */
   12195         3317 :   if (tb.deferred && !ifc)
   12196              :     {
   12197            1 :       gfc_error ("Interface must be specified for DEFERRED binding at %C");
   12198            1 :       return MATCH_ERROR;
   12199              :     }
   12200         3316 :   if (ifc && !tb.deferred)
   12201              :     {
   12202            1 :       gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
   12203            1 :       return MATCH_ERROR;
   12204              :     }
   12205              : 
   12206              :   /* Match the colons.  */
   12207         3315 :   m = gfc_match (" ::");
   12208         3315 :   if (m == MATCH_ERROR)
   12209              :     return m;
   12210         3315 :   seen_colons = (m == MATCH_YES);
   12211         3315 :   if (seen_attrs && !seen_colons)
   12212              :     {
   12213            4 :       gfc_error ("Expected %<::%> after binding-attributes at %C");
   12214            4 :       return MATCH_ERROR;
   12215              :     }
   12216              : 
   12217              :   /* Match the binding names.  */
   12218           25 :   for(num=1;;num++)
   12219              :     {
   12220         3336 :       m = gfc_match_name (name);
   12221         3336 :       if (m == MATCH_ERROR)
   12222              :         return m;
   12223         3336 :       if (m == MATCH_NO)
   12224              :         {
   12225            5 :           gfc_error ("Expected binding name at %C");
   12226            5 :           return MATCH_ERROR;
   12227              :         }
   12228              : 
   12229         3331 :       if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
   12230              :         return MATCH_ERROR;
   12231              : 
   12232              :       /* Try to match the '=> target', if it's there.  */
   12233         3330 :       target = ifc;
   12234         3330 :       m = gfc_match (" =>");
   12235         3330 :       if (m == MATCH_ERROR)
   12236              :         return m;
   12237         3330 :       if (m == MATCH_YES)
   12238              :         {
   12239         1274 :           if (tb.deferred)
   12240              :             {
   12241            1 :               gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
   12242            1 :               return MATCH_ERROR;
   12243              :             }
   12244              : 
   12245         1273 :           if (!seen_colons)
   12246              :             {
   12247            1 :               gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
   12248              :                          " at %C");
   12249            1 :               return MATCH_ERROR;
   12250              :             }
   12251              : 
   12252         1272 :           m = gfc_match_name (target_buf);
   12253         1272 :           if (m == MATCH_ERROR)
   12254              :             return m;
   12255         1272 :           if (m == MATCH_NO)
   12256              :             {
   12257            2 :               gfc_error ("Expected binding target after %<=>%> at %C");
   12258            2 :               return MATCH_ERROR;
   12259              :             }
   12260              :           target = target_buf;
   12261              :         }
   12262              : 
   12263              :       /* If no target was found, it has the same name as the binding.  */
   12264         2056 :       if (!target)
   12265         1801 :         target = name;
   12266              : 
   12267              :       /* Get the namespace to insert the symbols into.  */
   12268         3326 :       ns = block->f2k_derived;
   12269         3326 :       gcc_assert (ns);
   12270              : 
   12271              :       /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
   12272         3326 :       if (tb.deferred && !block->attr.abstract)
   12273              :         {
   12274            1 :           gfc_error ("Type %qs containing DEFERRED binding at %C "
   12275              :                      "is not ABSTRACT", block->name);
   12276            1 :           return MATCH_ERROR;
   12277              :         }
   12278              : 
   12279              :       /* See if we already have a binding with this name in the symtree which
   12280              :          would be an error.  If a GENERIC already targeted this binding, it may
   12281              :          be already there but then typebound is still NULL.  */
   12282         3325 :       stree = gfc_find_symtree (ns->tb_sym_root, name);
   12283         3325 :       if (stree && stree->n.tb)
   12284              :         {
   12285            2 :           gfc_error ("There is already a procedure with binding name %qs for "
   12286              :                      "the derived type %qs at %C", name, block->name);
   12287            2 :           return MATCH_ERROR;
   12288              :         }
   12289              : 
   12290              :       /* Insert it and set attributes.  */
   12291              : 
   12292         3192 :       if (!stree)
   12293              :         {
   12294         3192 :           stree = gfc_new_symtree (&ns->tb_sym_root, name);
   12295         3192 :           gcc_assert (stree);
   12296              :         }
   12297         3323 :       stree->n.tb = gfc_get_typebound_proc (&tb);
   12298              : 
   12299         3323 :       if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
   12300              :                             false))
   12301              :         return MATCH_ERROR;
   12302         3323 :       gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
   12303         3323 :       gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
   12304         3323 :                      target, &stree->n.tb->u.specific->n.sym->declared_at);
   12305              : 
   12306         3323 :       if (gfc_match_eos () == MATCH_YES)
   12307              :         return MATCH_YES;
   12308           26 :       if (gfc_match_char (',') != MATCH_YES)
   12309            1 :         goto syntax;
   12310              :     }
   12311              : 
   12312            1 : syntax:
   12313            1 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
   12314            1 :   return MATCH_ERROR;
   12315              : }
   12316              : 
   12317              : 
   12318              : /* Match a GENERIC statement.
   12319              : F2018 15.4.3.3 GENERIC statement
   12320              : 
   12321              : A GENERIC statement specifies a generic identifier for one or more specific
   12322              : procedures, in the same way as a generic interface block that does not contain
   12323              : interface bodies.
   12324              : 
   12325              : R1510 generic-stmt is:
   12326              : GENERIC [ , access-spec ] :: generic-spec => specific-procedure-list
   12327              : 
   12328              : C1510 (R1510) A specific-procedure in a GENERIC statement shall not specify a
   12329              : procedure that was specified previously in any accessible interface with the
   12330              : same generic identifier.
   12331              : 
   12332              : If access-spec appears, it specifies the accessibility (8.5.2) of generic-spec.
   12333              : 
   12334              : For GENERIC statements outside of a derived type, use is made of the existing,
   12335              : typebound matching functions to obtain access-spec and generic-spec.  After
   12336              : this the standard INTERFACE machinery is used. */
   12337              : 
   12338              : static match
   12339          100 : match_generic_stmt (void)
   12340              : {
   12341          100 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12342              :   /* Allow space for OPERATOR(...).  */
   12343          100 :   char generic_spec_name[GFC_MAX_SYMBOL_LEN + 16];
   12344              :   /* Generics other than uops  */
   12345          100 :   gfc_symbol* generic_spec = NULL;
   12346              :   /* Generic uops  */
   12347          100 :   gfc_user_op *generic_uop = NULL;
   12348              :   /* For the matching calls  */
   12349          100 :   gfc_typebound_proc tbattr;
   12350          100 :   gfc_namespace* ns = gfc_current_ns;
   12351          100 :   interface_type op_type;
   12352          100 :   gfc_intrinsic_op op;
   12353          100 :   match m;
   12354          100 :   gfc_symtree* st;
   12355              :   /* The specific-procedure-list  */
   12356          100 :   gfc_interface *generic = NULL;
   12357              :   /* The head of the specific-procedure-list  */
   12358          100 :   gfc_interface **generic_tail = NULL;
   12359              : 
   12360          100 :   memset (&tbattr, 0, sizeof (tbattr));
   12361          100 :   tbattr.where = gfc_current_locus;
   12362              : 
   12363              :   /* See if we get an access-specifier.  */
   12364          100 :   m = match_binding_attributes (&tbattr, true, false);
   12365          100 :   tbattr.where = gfc_current_locus;
   12366          100 :   if (m == MATCH_ERROR)
   12367            0 :     goto error;
   12368              : 
   12369              :   /* Now the colons, those are required.  */
   12370          100 :   if (gfc_match (" ::") != MATCH_YES)
   12371              :     {
   12372            0 :       gfc_error ("Expected %<::%> at %C");
   12373            0 :       goto error;
   12374              :     }
   12375              : 
   12376              :   /* Match the generic-spec name; depending on type (operator / generic) format
   12377              :      it for future error messages in 'generic_spec_name'.  */
   12378          100 :   m = gfc_match_generic_spec (&op_type, name, &op);
   12379          100 :   if (m == MATCH_ERROR)
   12380              :     return MATCH_ERROR;
   12381          100 :   if (m == MATCH_NO)
   12382              :     {
   12383            0 :       gfc_error ("Expected generic name or operator descriptor at %C");
   12384            0 :       goto error;
   12385              :     }
   12386              : 
   12387          100 :   switch (op_type)
   12388              :     {
   12389           63 :     case INTERFACE_GENERIC:
   12390           63 :     case INTERFACE_DTIO:
   12391           63 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "%s", name);
   12392           63 :       break;
   12393              : 
   12394           22 :     case INTERFACE_USER_OP:
   12395           22 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(.%s.)", name);
   12396           22 :       break;
   12397              : 
   12398           13 :     case INTERFACE_INTRINSIC_OP:
   12399           13 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(%s)",
   12400              :                 gfc_op2string (op));
   12401           13 :       break;
   12402              : 
   12403            2 :     case INTERFACE_NAMELESS:
   12404            2 :       gfc_error ("Malformed GENERIC statement at %C");
   12405            2 :       goto error;
   12406            0 :       break;
   12407              : 
   12408            0 :     default:
   12409            0 :       gcc_unreachable ();
   12410              :     }
   12411              : 
   12412              :   /* Match the required =>.  */
   12413           98 :   if (gfc_match (" =>") != MATCH_YES)
   12414              :     {
   12415            1 :       gfc_error ("Expected %<=>%> at %C");
   12416            1 :       goto error;
   12417              :     }
   12418              : 
   12419              : 
   12420           97 :   if (gfc_current_state () != COMP_MODULE && tbattr.access != ACCESS_UNKNOWN)
   12421              :     {
   12422            1 :       gfc_error ("The access specification at %L not in a module",
   12423              :                  &tbattr.where);
   12424            1 :       goto error;
   12425              :     }
   12426              : 
   12427              :   /* Try to find existing generic-spec with this name for this operator;
   12428              :      if there is something, check that it is another generic-spec and then
   12429              :      extend it rather than building a new symbol. Otherwise, create a new
   12430              :      one with the right attributes.  */
   12431              : 
   12432           96 :   switch (op_type)
   12433              :     {
   12434           61 :     case INTERFACE_DTIO:
   12435           61 :     case INTERFACE_GENERIC:
   12436           61 :       st = gfc_find_symtree (ns->sym_root, name);
   12437           61 :       generic_spec = st ? st->n.sym : NULL;
   12438           61 :       if (generic_spec)
   12439              :         {
   12440           25 :           if (generic_spec->attr.flavor != FL_PROCEDURE
   12441           11 :                && generic_spec->attr.flavor != FL_UNKNOWN)
   12442              :             {
   12443            1 :               gfc_error ("The generic-spec name %qs at %C clashes with the "
   12444              :                          "name of an entity declared at %L that is not a "
   12445              :                          "procedure", name, &generic_spec->declared_at);
   12446            1 :               goto error;
   12447              :             }
   12448              : 
   12449           24 :           if (op_type == INTERFACE_GENERIC && !generic_spec->attr.generic
   12450           10 :                && generic_spec->attr.flavor != FL_UNKNOWN)
   12451              :             {
   12452            0 :               gfc_error ("There's already a non-generic procedure with "
   12453              :                          "name %qs at %C", generic_spec->name);
   12454            0 :               goto error;
   12455              :             }
   12456              : 
   12457           24 :           if (tbattr.access != ACCESS_UNKNOWN)
   12458              :             {
   12459            2 :               if (generic_spec->attr.access != tbattr.access)
   12460              :                 {
   12461            1 :                   gfc_error ("The access specification at %L conflicts with "
   12462              :                              "that already given to %qs", &tbattr.where,
   12463              :                              generic_spec->name);
   12464            1 :                   goto error;
   12465              :                 }
   12466              :               else
   12467              :                 {
   12468            1 :                   gfc_error ("The access specification at %L repeats that "
   12469              :                              "already given to %qs", &tbattr.where,
   12470              :                              generic_spec->name);
   12471            1 :                   goto error;
   12472              :                 }
   12473              :             }
   12474              : 
   12475           22 :           if (generic_spec->ts.type != BT_UNKNOWN)
   12476              :             {
   12477            1 :               gfc_error ("The generic-spec in the generic statement at %C "
   12478              :                          "has a type from the declaration at %L",
   12479              :                          &generic_spec->declared_at);
   12480            1 :               goto error;
   12481              :             }
   12482              :         }
   12483              : 
   12484              :       /* Now create the generic_spec if it doesn't already exist and provide
   12485              :          is with the appropriate attributes.  */
   12486           57 :       if (!generic_spec || generic_spec->attr.flavor != FL_PROCEDURE)
   12487              :         {
   12488           45 :           if (!generic_spec)
   12489              :             {
   12490           36 :               gfc_get_symbol (name, ns, &generic_spec, &gfc_current_locus);
   12491           36 :               gfc_set_sym_referenced (generic_spec);
   12492           36 :               generic_spec->attr.access = tbattr.access;
   12493              :             }
   12494            9 :           else if (generic_spec->attr.access == ACCESS_UNKNOWN)
   12495            0 :             generic_spec->attr.access = tbattr.access;
   12496           45 :           generic_spec->refs++;
   12497           45 :           generic_spec->attr.generic = 1;
   12498           45 :           generic_spec->attr.flavor = FL_PROCEDURE;
   12499              : 
   12500           45 :           generic_spec->declared_at = gfc_current_locus;
   12501              :         }
   12502              : 
   12503              :       /* Prepare to add the specific procedures.  */
   12504           57 :       generic = generic_spec->generic;
   12505           57 :       generic_tail = &generic_spec->generic;
   12506           57 :       break;
   12507              : 
   12508           22 :     case INTERFACE_USER_OP:
   12509           22 :       st = gfc_find_symtree (ns->uop_root, name);
   12510           22 :       generic_uop = st ? st->n.uop : NULL;
   12511            2 :       if (generic_uop)
   12512              :         {
   12513            2 :           if (generic_uop->access != ACCESS_UNKNOWN
   12514            2 :               && tbattr.access != ACCESS_UNKNOWN)
   12515              :             {
   12516            2 :               if (generic_uop->access != tbattr.access)
   12517              :                 {
   12518            1 :                   gfc_error ("The user operator at %L must have the same "
   12519              :                              "access specification as already defined user "
   12520              :                              "operator %qs", &tbattr.where, generic_spec_name);
   12521            1 :                   goto error;
   12522              :                 }
   12523              :               else
   12524              :                 {
   12525            1 :                   gfc_error ("The user operator at %L repeats the access "
   12526              :                              "specification of already defined user operator "                                   "%qs", &tbattr.where, generic_spec_name);
   12527            1 :                   goto error;
   12528              :                 }
   12529              :             }
   12530            0 :           else if (generic_uop->access == ACCESS_UNKNOWN)
   12531            0 :             generic_uop->access = tbattr.access;
   12532              :         }
   12533              :       else
   12534              :         {
   12535           20 :           generic_uop = gfc_get_uop (name);
   12536           20 :           generic_uop->access = tbattr.access;
   12537              :         }
   12538              : 
   12539              :       /* Prepare to add the specific procedures.  */
   12540           20 :       generic = generic_uop->op;
   12541           20 :       generic_tail = &generic_uop->op;
   12542           20 :       break;
   12543              : 
   12544           13 :     case INTERFACE_INTRINSIC_OP:
   12545           13 :       generic = ns->op[op];
   12546           13 :       generic_tail = &ns->op[op];
   12547           13 :       break;
   12548              : 
   12549            0 :     default:
   12550            0 :       gcc_unreachable ();
   12551              :     }
   12552              : 
   12553              :   /* Now, match all following names in the specific-procedure-list.  */
   12554          154 :   do
   12555              :     {
   12556          154 :       m = gfc_match_name (name);
   12557          154 :       if (m == MATCH_ERROR)
   12558            0 :         goto error;
   12559          154 :       if (m == MATCH_NO)
   12560              :         {
   12561            0 :           gfc_error ("Expected specific procedure name at %C");
   12562            0 :           goto error;
   12563              :         }
   12564              : 
   12565          154 :       if (op_type == INTERFACE_GENERIC
   12566           95 :           && !strcmp (generic_spec->name, name))
   12567              :         {
   12568            2 :           gfc_error ("The name %qs of the specific procedure at %C conflicts "
   12569              :                      "with that of the generic-spec", name);
   12570            2 :           goto error;
   12571              :         }
   12572              : 
   12573          152 :       generic = *generic_tail;
   12574          242 :       for (; generic; generic = generic->next)
   12575              :         {
   12576           90 :           if (!strcmp (generic->sym->name, name))
   12577              :             {
   12578            0 :               gfc_error ("%qs already defined as a specific procedure for the"
   12579              :                          " generic %qs at %C", name, generic_spec->name);
   12580            0 :               goto error;
   12581              :             }
   12582              :         }
   12583              : 
   12584          152 :       gfc_find_sym_tree (name, ns, 1, &st);
   12585          152 :       if (!st)
   12586              :         {
   12587              :           /* This might be a procedure that has not yet been parsed. If
   12588              :              so gfc_fixup_sibling_symbols will replace this symbol with
   12589              :              that of the procedure.  */
   12590           75 :           gfc_get_sym_tree (name, ns, &st, false);
   12591           75 :           st->n.sym->refs++;
   12592              :         }
   12593              : 
   12594          152 :       generic = gfc_get_interface();
   12595          152 :       generic->next = *generic_tail;
   12596          152 :       *generic_tail = generic;
   12597          152 :       generic->where = gfc_current_locus;
   12598          152 :       generic->sym = st->n.sym;
   12599              :     }
   12600          152 :   while (gfc_match (" ,") == MATCH_YES);
   12601              : 
   12602           88 :   if (gfc_match_eos () != MATCH_YES)
   12603              :     {
   12604            0 :       gfc_error ("Junk after GENERIC statement at %C");
   12605            0 :       goto error;
   12606              :     }
   12607              : 
   12608           88 :   gfc_commit_symbols ();
   12609           88 :   return MATCH_YES;
   12610              : 
   12611          100 : error:
   12612              :   return MATCH_ERROR;
   12613              : }
   12614              : 
   12615              : 
   12616              : /* Match a GENERIC procedure binding inside a derived type.  */
   12617              : 
   12618              : static match
   12619          972 : match_typebound_generic (void)
   12620              : {
   12621          972 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12622          972 :   char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
   12623          972 :   gfc_symbol* block;
   12624          972 :   gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
   12625          972 :   gfc_typebound_proc* tb;
   12626          972 :   gfc_namespace* ns;
   12627          972 :   interface_type op_type;
   12628          972 :   gfc_intrinsic_op op;
   12629          972 :   match m;
   12630              : 
   12631              :   /* Check current state.  */
   12632          972 :   if (gfc_current_state () == COMP_DERIVED)
   12633              :     {
   12634            0 :       gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
   12635            0 :       return MATCH_ERROR;
   12636              :     }
   12637          972 :   if (gfc_current_state () != COMP_DERIVED_CONTAINS)
   12638              :     return MATCH_NO;
   12639          972 :   block = gfc_state_stack->previous->sym;
   12640          972 :   ns = block->f2k_derived;
   12641          972 :   gcc_assert (block && ns);
   12642              : 
   12643          972 :   memset (&tbattr, 0, sizeof (tbattr));
   12644          972 :   tbattr.where = gfc_current_locus;
   12645              : 
   12646              :   /* See if we get an access-specifier.  */
   12647          972 :   m = match_binding_attributes (&tbattr, true, false);
   12648          972 :   if (m == MATCH_ERROR)
   12649            1 :     goto error;
   12650              : 
   12651              :   /* Now the colons, those are required.  */
   12652          971 :   if (gfc_match (" ::") != MATCH_YES)
   12653              :     {
   12654            0 :       gfc_error ("Expected %<::%> at %C");
   12655            0 :       goto error;
   12656              :     }
   12657              : 
   12658              :   /* Match the binding name; depending on type (operator / generic) format
   12659              :      it for future error messages into bind_name.  */
   12660              : 
   12661          971 :   m = gfc_match_generic_spec (&op_type, name, &op);
   12662          971 :   if (m == MATCH_ERROR)
   12663              :     return MATCH_ERROR;
   12664          971 :   if (m == MATCH_NO)
   12665              :     {
   12666            0 :       gfc_error ("Expected generic name or operator descriptor at %C");
   12667            0 :       goto error;
   12668              :     }
   12669              : 
   12670          971 :   switch (op_type)
   12671              :     {
   12672          476 :     case INTERFACE_GENERIC:
   12673          476 :     case INTERFACE_DTIO:
   12674          476 :       snprintf (bind_name, sizeof (bind_name), "%s", name);
   12675          476 :       break;
   12676              : 
   12677           47 :     case INTERFACE_USER_OP:
   12678           47 :       snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
   12679           47 :       break;
   12680              : 
   12681          447 :     case INTERFACE_INTRINSIC_OP:
   12682          447 :       snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
   12683              :                 gfc_op2string (op));
   12684          447 :       break;
   12685              : 
   12686            1 :     case INTERFACE_NAMELESS:
   12687            1 :       gfc_error ("Malformed GENERIC statement at %C");
   12688            1 :       goto error;
   12689            0 :       break;
   12690              : 
   12691            0 :     default:
   12692            0 :       gcc_unreachable ();
   12693              :     }
   12694              : 
   12695              :   /* Match the required =>.  */
   12696          970 :   if (gfc_match (" =>") != MATCH_YES)
   12697              :     {
   12698            0 :       gfc_error ("Expected %<=>%> at %C");
   12699            0 :       goto error;
   12700              :     }
   12701              : 
   12702              :   /* Try to find existing GENERIC binding with this name / for this operator;
   12703              :      if there is something, check that it is another GENERIC and then extend
   12704              :      it rather than building a new node.  Otherwise, create it and put it
   12705              :      at the right position.  */
   12706              : 
   12707          970 :   switch (op_type)
   12708              :     {
   12709          523 :     case INTERFACE_DTIO:
   12710          523 :     case INTERFACE_USER_OP:
   12711          523 :     case INTERFACE_GENERIC:
   12712          523 :       {
   12713          523 :         const bool is_op = (op_type == INTERFACE_USER_OP);
   12714          523 :         gfc_symtree* st;
   12715              : 
   12716          523 :         st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
   12717          523 :         tb = st ? st->n.tb : NULL;
   12718              :         break;
   12719              :       }
   12720              : 
   12721          447 :     case INTERFACE_INTRINSIC_OP:
   12722          447 :       tb = ns->tb_op[op];
   12723          447 :       break;
   12724              : 
   12725            0 :     default:
   12726            0 :       gcc_unreachable ();
   12727              :     }
   12728              : 
   12729          458 :   if (tb)
   12730              :     {
   12731            9 :       if (!tb->is_generic)
   12732              :         {
   12733            1 :           gcc_assert (op_type == INTERFACE_GENERIC);
   12734            1 :           gfc_error ("There's already a non-generic procedure with binding name"
   12735              :                      " %qs for the derived type %qs at %C",
   12736              :                      bind_name, block->name);
   12737            1 :           goto error;
   12738              :         }
   12739              : 
   12740            8 :       if (tb->access != tbattr.access)
   12741              :         {
   12742            2 :           gfc_error ("Binding at %C must have the same access as already"
   12743              :                      " defined binding %qs", bind_name);
   12744            2 :           goto error;
   12745              :         }
   12746              :     }
   12747              :   else
   12748              :     {
   12749          961 :       tb = gfc_get_typebound_proc (NULL);
   12750          961 :       tb->where = gfc_current_locus;
   12751          961 :       tb->access = tbattr.access;
   12752          961 :       tb->is_generic = 1;
   12753          961 :       tb->u.generic = NULL;
   12754              : 
   12755          961 :       switch (op_type)
   12756              :         {
   12757          514 :         case INTERFACE_DTIO:
   12758          514 :         case INTERFACE_GENERIC:
   12759          514 :         case INTERFACE_USER_OP:
   12760          514 :           {
   12761          514 :             const bool is_op = (op_type == INTERFACE_USER_OP);
   12762          514 :             gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
   12763              :                                                    &ns->tb_sym_root, name);
   12764          514 :             gcc_assert (st);
   12765          514 :             st->n.tb = tb;
   12766              : 
   12767          514 :             break;
   12768              :           }
   12769              : 
   12770          447 :         case INTERFACE_INTRINSIC_OP:
   12771          447 :           ns->tb_op[op] = tb;
   12772          447 :           break;
   12773              : 
   12774            0 :         default:
   12775            0 :           gcc_unreachable ();
   12776              :         }
   12777              :     }
   12778              : 
   12779              :   /* Now, match all following names as specific targets.  */
   12780         1130 :   do
   12781              :     {
   12782         1130 :       gfc_symtree* target_st;
   12783         1130 :       gfc_tbp_generic* target;
   12784              : 
   12785         1130 :       m = gfc_match_name (name);
   12786         1130 :       if (m == MATCH_ERROR)
   12787            0 :         goto error;
   12788         1130 :       if (m == MATCH_NO)
   12789              :         {
   12790            1 :           gfc_error ("Expected specific binding name at %C");
   12791            1 :           goto error;
   12792              :         }
   12793              : 
   12794         1129 :       target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
   12795              : 
   12796              :       /* See if this is a duplicate specification.  */
   12797         1370 :       for (target = tb->u.generic; target; target = target->next)
   12798          242 :         if (target_st == target->specific_st)
   12799              :           {
   12800            1 :             gfc_error ("%qs already defined as specific binding for the"
   12801              :                        " generic %qs at %C", name, bind_name);
   12802            1 :             goto error;
   12803              :           }
   12804              : 
   12805         1128 :       target = gfc_get_tbp_generic ();
   12806         1128 :       target->specific_st = target_st;
   12807         1128 :       target->specific = NULL;
   12808         1128 :       target->next = tb->u.generic;
   12809         1128 :       target->is_operator = ((op_type == INTERFACE_USER_OP)
   12810         1128 :                              || (op_type == INTERFACE_INTRINSIC_OP));
   12811         1128 :       tb->u.generic = target;
   12812              :     }
   12813         1128 :   while (gfc_match (" ,") == MATCH_YES);
   12814              : 
   12815              :   /* Here should be the end.  */
   12816          965 :   if (gfc_match_eos () != MATCH_YES)
   12817              :     {
   12818            1 :       gfc_error ("Junk after GENERIC binding at %C");
   12819            1 :       goto error;
   12820              :     }
   12821              : 
   12822              :   return MATCH_YES;
   12823              : 
   12824          972 : error:
   12825              :   return MATCH_ERROR;
   12826              : }
   12827              : 
   12828              : 
   12829              : match
   12830         1072 : gfc_match_generic ()
   12831              : {
   12832         1072 :   if (gfc_option.allow_std & ~GFC_STD_OPT_F08
   12833         1070 :       && gfc_current_state () != COMP_DERIVED_CONTAINS)
   12834          100 :     return match_generic_stmt ();
   12835              :   else
   12836          972 :     return match_typebound_generic ();
   12837              : }
   12838              : 
   12839              : 
   12840              : /* Match a FINAL declaration inside a derived type.  */
   12841              : 
   12842              : match
   12843          514 : gfc_match_final_decl (void)
   12844              : {
   12845          514 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12846          514 :   gfc_symbol* sym;
   12847          514 :   match m;
   12848          514 :   gfc_namespace* module_ns;
   12849          514 :   bool first, last;
   12850          514 :   gfc_symbol* block;
   12851              : 
   12852          514 :   if (gfc_current_form == FORM_FREE)
   12853              :     {
   12854          514 :       char c = gfc_peek_ascii_char ();
   12855          514 :       if (!gfc_is_whitespace (c) && c != ':')
   12856              :         return MATCH_NO;
   12857              :     }
   12858              : 
   12859          513 :   if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
   12860              :     {
   12861            1 :       if (gfc_current_form == FORM_FIXED)
   12862              :         return MATCH_NO;
   12863              : 
   12864            1 :       gfc_error ("FINAL declaration at %C must be inside a derived type "
   12865              :                  "CONTAINS section");
   12866            1 :       return MATCH_ERROR;
   12867              :     }
   12868              : 
   12869          512 :   block = gfc_state_stack->previous->sym;
   12870          512 :   gcc_assert (block);
   12871              : 
   12872          512 :   if (gfc_state_stack->previous->previous
   12873          512 :       && gfc_state_stack->previous->previous->state != COMP_MODULE
   12874            6 :       && gfc_state_stack->previous->previous->state != COMP_SUBMODULE)
   12875              :     {
   12876            0 :       gfc_error ("Derived type declaration with FINAL at %C must be in the"
   12877              :                  " specification part of a MODULE");
   12878            0 :       return MATCH_ERROR;
   12879              :     }
   12880              : 
   12881          512 :   module_ns = gfc_current_ns;
   12882          512 :   gcc_assert (module_ns);
   12883          512 :   gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
   12884              : 
   12885              :   /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
   12886          512 :   if (gfc_match (" ::") == MATCH_ERROR)
   12887              :     return MATCH_ERROR;
   12888              : 
   12889              :   /* Match the sequence of procedure names.  */
   12890              :   first = true;
   12891              :   last = false;
   12892          616 :   do
   12893              :     {
   12894          616 :       gfc_finalizer* f;
   12895              : 
   12896          616 :       if (first && gfc_match_eos () == MATCH_YES)
   12897              :         {
   12898            2 :           gfc_error ("Empty FINAL at %C");
   12899            2 :           return MATCH_ERROR;
   12900              :         }
   12901              : 
   12902          614 :       m = gfc_match_name (name);
   12903          614 :       if (m == MATCH_NO)
   12904              :         {
   12905            1 :           gfc_error ("Expected module procedure name at %C");
   12906            1 :           return MATCH_ERROR;
   12907              :         }
   12908          613 :       else if (m != MATCH_YES)
   12909              :         return MATCH_ERROR;
   12910              : 
   12911          613 :       if (gfc_match_eos () == MATCH_YES)
   12912              :         last = true;
   12913          105 :       if (!last && gfc_match_char (',') != MATCH_YES)
   12914              :         {
   12915            1 :           gfc_error ("Expected %<,%> at %C");
   12916            1 :           return MATCH_ERROR;
   12917              :         }
   12918              : 
   12919          612 :       if (gfc_get_symbol (name, module_ns, &sym))
   12920              :         {
   12921            0 :           gfc_error ("Unknown procedure name %qs at %C", name);
   12922            0 :           return MATCH_ERROR;
   12923              :         }
   12924              : 
   12925              :       /* Mark the symbol as module procedure.  */
   12926          612 :       if (sym->attr.proc != PROC_MODULE
   12927          612 :           && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
   12928              :         return MATCH_ERROR;
   12929              : 
   12930              :       /* Check if we already have this symbol in the list, this is an error.  */
   12931          829 :       for (f = block->f2k_derived->finalizers; f; f = f->next)
   12932          218 :         if (f->proc_sym == sym)
   12933              :           {
   12934            1 :             gfc_error ("%qs at %C is already defined as FINAL procedure",
   12935              :                        name);
   12936            1 :             return MATCH_ERROR;
   12937              :           }
   12938              : 
   12939              :       /* Add this symbol to the list of finalizers.  */
   12940          611 :       gcc_assert (block->f2k_derived);
   12941          611 :       sym->refs++;
   12942          611 :       f = XCNEW (gfc_finalizer);
   12943          611 :       f->proc_sym = sym;
   12944          611 :       f->proc_tree = NULL;
   12945          611 :       f->where = gfc_current_locus;
   12946          611 :       f->next = block->f2k_derived->finalizers;
   12947          611 :       block->f2k_derived->finalizers = f;
   12948              : 
   12949          611 :       first = false;
   12950              :     }
   12951          611 :   while (!last);
   12952              : 
   12953              :   return MATCH_YES;
   12954              : }
   12955              : 
   12956              : 
   12957              : const ext_attr_t ext_attr_list[] = {
   12958              :   { "dllimport",    EXT_ATTR_DLLIMPORT,    "dllimport" },
   12959              :   { "dllexport",    EXT_ATTR_DLLEXPORT,    "dllexport" },
   12960              :   { "cdecl",        EXT_ATTR_CDECL,        "cdecl"     },
   12961              :   { "stdcall",      EXT_ATTR_STDCALL,      "stdcall"   },
   12962              :   { "fastcall",     EXT_ATTR_FASTCALL,     "fastcall"  },
   12963              :   { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL              },
   12964              :   { "deprecated",   EXT_ATTR_DEPRECATED,   NULL              },
   12965              :   { "noinline",     EXT_ATTR_NOINLINE,     NULL              },
   12966              :   { "noreturn",     EXT_ATTR_NORETURN,     NULL              },
   12967              :   { "weak",       EXT_ATTR_WEAK,         NULL        },
   12968              :   { "inline",       EXT_ATTR_INLINE,       NULL              },
   12969              :   { "always_inline",EXT_ATTR_ALWAYS_INLINE,NULL              },
   12970              :   { NULL,           EXT_ATTR_LAST,         NULL        }
   12971              : };
   12972              : 
   12973              : /* Match a !GCC$ ATTRIBUTES statement of the form:
   12974              :       !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
   12975              :    When we come here, we have already matched the !GCC$ ATTRIBUTES string.
   12976              : 
   12977              :    TODO: We should support all GCC attributes using the same syntax for
   12978              :    the attribute list, i.e. the list in C
   12979              :       __attributes(( attribute-list ))
   12980              :    matches then
   12981              :       !GCC$ ATTRIBUTES attribute-list ::
   12982              :    Cf. c-parser.cc's c_parser_attributes; the data can then directly be
   12983              :    saved into a TREE.
   12984              : 
   12985              :    As there is absolutely no risk of confusion, we should never return
   12986              :    MATCH_NO.  */
   12987              : match
   12988         2984 : gfc_match_gcc_attributes (void)
   12989              : {
   12990         2984 :   symbol_attribute attr;
   12991         2984 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12992         2984 :   unsigned id;
   12993         2984 :   gfc_symbol *sym;
   12994         2984 :   match m;
   12995              : 
   12996         2984 :   gfc_clear_attr (&attr);
   12997         2988 :   for(;;)
   12998              :     {
   12999         2986 :       char ch;
   13000              : 
   13001         2986 :       if (gfc_match_name (name) != MATCH_YES)
   13002              :         return MATCH_ERROR;
   13003              : 
   13004        18042 :       for (id = 0; id < EXT_ATTR_LAST; id++)
   13005        18042 :         if (strcmp (name, ext_attr_list[id].name) == 0)
   13006              :           break;
   13007              : 
   13008         2986 :       if (id == EXT_ATTR_LAST)
   13009              :         {
   13010            0 :           gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
   13011            0 :           return MATCH_ERROR;
   13012              :         }
   13013              : 
   13014         2986 :       if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
   13015              :         return MATCH_ERROR;
   13016              : 
   13017         2986 :       gfc_gobble_whitespace ();
   13018         2986 :       ch = gfc_next_ascii_char ();
   13019         2986 :       if (ch == ':')
   13020              :         {
   13021              :           /* This is the successful exit condition for the loop.  */
   13022         2984 :           if (gfc_next_ascii_char () == ':')
   13023              :             break;
   13024              :         }
   13025              : 
   13026            2 :       if (ch == ',')
   13027            2 :         continue;
   13028              : 
   13029            0 :       goto syntax;
   13030            2 :     }
   13031              : 
   13032         2984 :   if (gfc_match_eos () == MATCH_YES)
   13033            0 :     goto syntax;
   13034              : 
   13035         2999 :   for(;;)
   13036              :     {
   13037         2999 :       m = gfc_match_name (name);
   13038         2999 :       if (m != MATCH_YES)
   13039              :         return m;
   13040              : 
   13041         2999 :       if (find_special (name, &sym, true))
   13042              :         return MATCH_ERROR;
   13043              : 
   13044         2999 :       sym->attr.ext_attr |= attr.ext_attr;
   13045              : 
   13046              :       /* INLINE and ALWAYS_INLINE are incompatible with NOINLINE.  In the
   13047              :          middle-end the DECL_UNINLINABLE flag set by NOINLINE always wins, so
   13048              :          the inline request would be silently ignored.  Warn and drop it.  */
   13049         2999 :       if (sym->attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
   13050              :         {
   13051            5 :           if (sym->attr.ext_attr & (1 << EXT_ATTR_ALWAYS_INLINE))
   13052              :             {
   13053            2 :               gfc_warning (0, "Attribute %<ALWAYS_INLINE%> at %C is "
   13054              :                            "incompatible with %<NOINLINE%> for %qs and will "
   13055              :                            "be ignored", sym->name);
   13056            2 :               sym->attr.ext_attr &= ~(1 << EXT_ATTR_ALWAYS_INLINE);
   13057              :             }
   13058            5 :           if (sym->attr.ext_attr & (1 << EXT_ATTR_INLINE))
   13059              :             {
   13060            2 :               gfc_warning (0, "Attribute %<INLINE%> at %C is incompatible "
   13061              :                            "with %<NOINLINE%> for %qs and will be ignored",
   13062              :                            sym->name);
   13063            2 :               sym->attr.ext_attr &= ~(1 << EXT_ATTR_INLINE);
   13064              :             }
   13065              :         }
   13066              : 
   13067         2999 :       if (gfc_match_eos () == MATCH_YES)
   13068              :         break;
   13069              : 
   13070           15 :       if (gfc_match_char (',') != MATCH_YES)
   13071            0 :         goto syntax;
   13072              :     }
   13073              : 
   13074              :   return MATCH_YES;
   13075              : 
   13076            0 : syntax:
   13077            0 :   gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
   13078            0 :   return MATCH_ERROR;
   13079              : }
   13080              : 
   13081              : 
   13082              : /* Match a !GCC$ UNROLL statement of the form:
   13083              :       !GCC$ UNROLL n
   13084              : 
   13085              :    The parameter n is the number of times we are supposed to unroll.
   13086              : 
   13087              :    When we come here, we have already matched the !GCC$ UNROLL string.  */
   13088              : match
   13089           19 : gfc_match_gcc_unroll (void)
   13090              : {
   13091           19 :   int value;
   13092              : 
   13093              :   /* FIXME: use gfc_match_small_literal_int instead, delete small_int  */
   13094           19 :   if (gfc_match_small_int (&value) == MATCH_YES)
   13095              :     {
   13096           19 :       if (value < 0 || value > USHRT_MAX)
   13097              :         {
   13098            2 :           gfc_error ("%<GCC unroll%> directive requires a"
   13099              :               " non-negative integral constant"
   13100              :               " less than or equal to %u at %C",
   13101              :               USHRT_MAX
   13102              :           );
   13103            2 :           return MATCH_ERROR;
   13104              :         }
   13105           17 :       if (gfc_match_eos () == MATCH_YES)
   13106              :         {
   13107           17 :           directive_unroll = value == 0 ? 1 : value;
   13108           17 :           return MATCH_YES;
   13109              :         }
   13110              :     }
   13111              : 
   13112            0 :   gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
   13113            0 :   return MATCH_ERROR;
   13114              : }
   13115              : 
   13116              : /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
   13117              : 
   13118              :    The parameter b is name of a middle-end built-in.
   13119              :    FLAGS is optional and must be one of:
   13120              :      - (inbranch)
   13121              :      - (notinbranch)
   13122              : 
   13123              :    IF('target') is optional and TARGET is a name of a multilib ABI.
   13124              : 
   13125              :    When we come here, we have already matched the !GCC$ builtin string.  */
   13126              : 
   13127              : match
   13128      3554745 : gfc_match_gcc_builtin (void)
   13129              : {
   13130      3554745 :   char builtin[GFC_MAX_SYMBOL_LEN + 1];
   13131      3554745 :   char target[GFC_MAX_SYMBOL_LEN + 1];
   13132              : 
   13133      3554745 :   if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
   13134              :     return MATCH_ERROR;
   13135              : 
   13136      3554745 :   gfc_simd_clause clause = SIMD_NONE;
   13137      3554745 :   if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
   13138              :     clause = SIMD_NOTINBRANCH;
   13139           21 :   else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
   13140           15 :     clause = SIMD_INBRANCH;
   13141              : 
   13142      3554745 :   if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
   13143              :     {
   13144      3554715 :       if (strcmp (target, "fastmath") == 0)
   13145              :         {
   13146            0 :           if (!fast_math_flags_set_p (&global_options))
   13147              :             return MATCH_YES;
   13148              :         }
   13149              :       else
   13150              :         {
   13151      3554715 :           const char *abi = targetm.get_multilib_abi_name ();
   13152      3554715 :           if (abi == NULL || strcmp (abi, target) != 0)
   13153              :             return MATCH_YES;
   13154              :         }
   13155              :     }
   13156              : 
   13157      1755302 :   if (gfc_vectorized_builtins == NULL)
   13158        32511 :     gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
   13159              : 
   13160      1755302 :   char *r = XNEWVEC (char, strlen (builtin) + 32);
   13161      1755302 :   sprintf (r, "__builtin_%s", builtin);
   13162              : 
   13163      1755302 :   bool existed;
   13164      1755302 :   int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
   13165      1755302 :   value |= clause;
   13166      1755302 :   if (existed)
   13167           23 :     free (r);
   13168              : 
   13169              :   return MATCH_YES;
   13170              : }
   13171              : 
   13172              : /* Match an !GCC$ IVDEP statement.
   13173              :    When we come here, we have already matched the !GCC$ IVDEP string.  */
   13174              : 
   13175              : match
   13176            3 : gfc_match_gcc_ivdep (void)
   13177              : {
   13178            3 :   if (gfc_match_eos () == MATCH_YES)
   13179              :     {
   13180            3 :       directive_ivdep = true;
   13181            3 :       return MATCH_YES;
   13182              :     }
   13183              : 
   13184            0 :   gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
   13185            0 :   return MATCH_ERROR;
   13186              : }
   13187              : 
   13188              : /* Match an !GCC$ VECTOR statement.
   13189              :    When we come here, we have already matched the !GCC$ VECTOR string.  */
   13190              : 
   13191              : match
   13192            3 : gfc_match_gcc_vector (void)
   13193              : {
   13194            3 :   if (gfc_match_eos () == MATCH_YES)
   13195              :     {
   13196            3 :       directive_vector = true;
   13197            3 :       directive_novector = false;
   13198            3 :       return MATCH_YES;
   13199              :     }
   13200              : 
   13201            0 :   gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
   13202            0 :   return MATCH_ERROR;
   13203              : }
   13204              : 
   13205              : /* Match an !GCC$ NOVECTOR statement.
   13206              :    When we come here, we have already matched the !GCC$ NOVECTOR string.  */
   13207              : 
   13208              : match
   13209            3 : gfc_match_gcc_novector (void)
   13210              : {
   13211            3 :   if (gfc_match_eos () == MATCH_YES)
   13212              :     {
   13213            3 :       directive_novector = true;
   13214            3 :       directive_vector = false;
   13215            3 :       return MATCH_YES;
   13216              :     }
   13217              : 
   13218            0 :   gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
   13219            0 :   return MATCH_ERROR;
   13220              : }
        

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