LCOV - code coverage report
Current view: top level - gcc/fortran - decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.8 % 6194 5627
Test Date: 2026-10-03 16:17:38 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         8455 : gfc_in_match_data (void)
     163              : {
     164         8455 :   return in_match_data;
     165              : }
     166              : 
     167              : static void
     168         4840 : set_in_match_data (bool set_value)
     169              : {
     170         4840 :   in_match_data = set_value;
     171            0 : }
     172              : 
     173              : /* Free a gfc_data_variable structure and everything beneath it.  */
     174              : 
     175              : static void
     176         5663 : free_variable (gfc_data_variable *p)
     177              : {
     178         5663 :   gfc_data_variable *q;
     179              : 
     180         8752 :   for (; p; p = q)
     181              :     {
     182         3089 :       q = p->next;
     183         3089 :       gfc_free_expr (p->expr);
     184         3089 :       gfc_free_iterator (&p->iter, 0);
     185         3089 :       free_variable (p->list);
     186         3089 :       free (p);
     187              :     }
     188         5663 : }
     189              : 
     190              : 
     191              : /* Free a gfc_data_value structure and everything beneath it.  */
     192              : 
     193              : static void
     194         2574 : free_value (gfc_data_value *p)
     195              : {
     196         2574 :   gfc_data_value *q;
     197              : 
     198        10886 :   for (; p; p = q)
     199              :     {
     200         8312 :       q = p->next;
     201         8312 :       mpz_clear (p->repeat);
     202         8312 :       gfc_free_expr (p->expr);
     203         8312 :       free (p);
     204              :     }
     205         2574 : }
     206              : 
     207              : 
     208              : /* Free a list of gfc_data structures.  */
     209              : 
     210              : void
     211       548145 : gfc_free_data (gfc_data *p)
     212              : {
     213       548145 :   gfc_data *q;
     214              : 
     215       550719 :   for (; p; p = q)
     216              :     {
     217         2574 :       q = p->next;
     218         2574 :       free_variable (p->var);
     219         2574 :       free_value (p->value);
     220         2574 :       free (p);
     221              :     }
     222       548145 : }
     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      9250656 : gfc_reject_data (gfc_namespace *ns)
     244              : {
     245      9250656 :   gfc_data *d;
     246              : 
     247      9250658 :   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      9250656 : }
     254              : 
     255              : static match var_element (gfc_data_variable *);
     256              : 
     257              : /* Match a list of variables terminated by an iterator and a right
     258              :    parenthesis.  */
     259              : 
     260              : static match
     261          154 : var_list (gfc_data_variable *parent)
     262              : {
     263          154 :   gfc_data_variable *tail, var;
     264          154 :   match m;
     265              : 
     266          154 :   m = var_element (&var);
     267          154 :   if (m == MATCH_ERROR)
     268              :     return MATCH_ERROR;
     269          154 :   if (m == MATCH_NO)
     270            0 :     goto syntax;
     271              : 
     272          154 :   tail = gfc_get_data_variable ();
     273          154 :   *tail = var;
     274              : 
     275          154 :   parent->list = tail;
     276              : 
     277          156 :   for (;;)
     278              :     {
     279          155 :       if (gfc_match_char (',') != MATCH_YES)
     280            0 :         goto syntax;
     281              : 
     282          155 :       m = gfc_match_iterator (&parent->iter, 1);
     283          155 :       if (m == MATCH_YES)
     284              :         break;
     285            1 :       if (m == MATCH_ERROR)
     286              :         return MATCH_ERROR;
     287              : 
     288            1 :       m = var_element (&var);
     289            1 :       if (m == MATCH_ERROR)
     290              :         return MATCH_ERROR;
     291            1 :       if (m == MATCH_NO)
     292            0 :         goto syntax;
     293              : 
     294            1 :       tail->next = gfc_get_data_variable ();
     295            1 :       tail = tail->next;
     296              : 
     297            1 :       *tail = var;
     298              :     }
     299              : 
     300          154 :   if (gfc_match_char (')') != MATCH_YES)
     301            0 :     goto syntax;
     302              :   return MATCH_YES;
     303              : 
     304            0 : syntax:
     305            0 :   gfc_syntax_error (ST_DATA);
     306            0 :   return MATCH_ERROR;
     307              : }
     308              : 
     309              : 
     310              : /* Match a single element in a data variable list, which can be a
     311              :    variable-iterator list.  */
     312              : 
     313              : static match
     314         3047 : var_element (gfc_data_variable *new_var)
     315              : {
     316         3047 :   match m;
     317         3047 :   gfc_symbol *sym;
     318              : 
     319         3047 :   memset (new_var, 0, sizeof (gfc_data_variable));
     320              : 
     321         3047 :   if (gfc_match_char ('(') == MATCH_YES)
     322          154 :     return var_list (new_var);
     323              : 
     324         2893 :   m = gfc_match_variable (&new_var->expr, 0);
     325         2893 :   if (m != MATCH_YES)
     326              :     return m;
     327              : 
     328         2889 :   if (new_var->expr->expr_type == EXPR_CONSTANT
     329            2 :       && new_var->expr->symtree == NULL)
     330              :     {
     331            2 :       gfc_error ("Inquiry parameter cannot appear in a "
     332              :                  "data-stmt-object-list at %C");
     333            2 :       return MATCH_ERROR;
     334              :     }
     335              : 
     336         2887 :   sym = new_var->expr->symtree->n.sym;
     337              : 
     338              :   /* Symbol should already have an associated type.  */
     339         2887 :   if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
     340              :     return MATCH_ERROR;
     341              : 
     342         2886 :   if (!sym->attr.function && gfc_current_ns->parent
     343          148 :       && gfc_current_ns->parent == sym->ns)
     344              :     {
     345            1 :       gfc_error ("Host associated variable %qs may not be in the DATA "
     346              :                  "statement at %C", sym->name);
     347            1 :       return MATCH_ERROR;
     348              :     }
     349              : 
     350         2885 :   if (gfc_current_state () != COMP_BLOCK_DATA
     351         2732 :       && sym->attr.in_common
     352         2914 :       && !gfc_notify_std (GFC_STD_GNU, "initialization of "
     353              :                           "common block variable %qs in DATA statement at %C",
     354              :                           sym->name))
     355              :     return MATCH_ERROR;
     356              : 
     357         2883 :   if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
     358            5 :     return MATCH_ERROR;
     359              : 
     360              :   return MATCH_YES;
     361              : }
     362              : 
     363              : 
     364              : /* Match the top-level list of data variables.  */
     365              : 
     366              : static match
     367         2517 : top_var_list (gfc_data *d)
     368              : {
     369         2517 :   gfc_data_variable var, *tail, *new_var;
     370         2517 :   match m;
     371              : 
     372         2517 :   tail = NULL;
     373              : 
     374         2892 :   for (;;)
     375              :     {
     376         2892 :       m = var_element (&var);
     377         2892 :       if (m == MATCH_NO)
     378            0 :         goto syntax;
     379         2892 :       if (m == MATCH_ERROR)
     380              :         return MATCH_ERROR;
     381              : 
     382         2877 :       new_var = gfc_get_data_variable ();
     383         2877 :       *new_var = var;
     384         2877 :       if (new_var->expr)
     385         2751 :         new_var->expr->where = gfc_current_locus;
     386              : 
     387         2877 :       if (tail == NULL)
     388         2502 :         d->var = new_var;
     389              :       else
     390          375 :         tail->next = new_var;
     391              : 
     392         2877 :       tail = new_var;
     393              : 
     394         2877 :       if (gfc_match_char ('/') == MATCH_YES)
     395              :         break;
     396          378 :       if (gfc_match_char (',') != MATCH_YES)
     397            3 :         goto syntax;
     398              :     }
     399              : 
     400              :   return MATCH_YES;
     401              : 
     402            3 : syntax:
     403            3 :   gfc_syntax_error (ST_DATA);
     404            3 :   gfc_free_data_all (gfc_current_ns);
     405            3 :   return MATCH_ERROR;
     406              : }
     407              : 
     408              : 
     409              : static match
     410         8713 : match_data_constant (gfc_expr **result)
     411              : {
     412         8713 :   char name[GFC_MAX_SYMBOL_LEN + 1];
     413         8713 :   gfc_symbol *sym, *dt_sym = NULL;
     414         8713 :   gfc_expr *expr;
     415         8713 :   match m;
     416         8713 :   locus old_loc;
     417         8713 :   gfc_symtree *symtree;
     418              : 
     419         8713 :   m = gfc_match_literal_constant (&expr, 1);
     420         8713 :   if (m == MATCH_YES)
     421              :     {
     422         8368 :       *result = expr;
     423         8368 :       return MATCH_YES;
     424              :     }
     425              : 
     426          345 :   if (m == MATCH_ERROR)
     427              :     return MATCH_ERROR;
     428              : 
     429          337 :   m = gfc_match_null (result);
     430          337 :   if (m != MATCH_NO)
     431              :     return m;
     432              : 
     433          329 :   old_loc = gfc_current_locus;
     434              : 
     435              :   /* Should this be a structure component, try to match it
     436              :      before matching a name.  */
     437          329 :   m = gfc_match_rvalue (result);
     438          329 :   if (m == MATCH_ERROR)
     439              :     return m;
     440              : 
     441          329 :   if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
     442              :     {
     443            4 :       if (!gfc_simplify_expr (*result, 0))
     444            0 :         m = MATCH_ERROR;
     445              :       return m;
     446              :     }
     447          319 :   else if (m == MATCH_YES)
     448              :     {
     449              :       /* If a parameter inquiry ends up here, symtree is NULL but **result
     450              :          contains the right constant expression.  Check here.  */
     451          319 :       if ((*result)->symtree == NULL
     452           37 :           && (*result)->expr_type == EXPR_CONSTANT
     453           37 :           && ((*result)->ts.type == BT_INTEGER
     454            1 :               || (*result)->ts.type == BT_REAL))
     455              :         return m;
     456              : 
     457              :       /* F2018:R845 data-stmt-constant is initial-data-target.
     458              :          A data-stmt-constant shall be ... initial-data-target if and
     459              :          only if the corresponding data-stmt-object has the POINTER
     460              :          attribute. ...  If data-stmt-constant is initial-data-target
     461              :          the corresponding data statement object shall be
     462              :          data-pointer-initialization compatible (7.5.4.6) with the initial
     463              :          data target; the data statement object is initially associated
     464              :          with the target.  */
     465          283 :       if ((*result)->symtree
     466          282 :           && (*result)->symtree->n.sym->attr.save
     467          218 :           && (*result)->symtree->n.sym->attr.target)
     468              :         return m;
     469          250 :       gfc_free_expr (*result);
     470              :     }
     471              : 
     472          256 :   gfc_current_locus = old_loc;
     473              : 
     474          256 :   m = gfc_match_name (name);
     475          256 :   if (m != MATCH_YES)
     476              :     return m;
     477              : 
     478          250 :   if (gfc_find_sym_tree (name, NULL, 1, &symtree))
     479              :     return MATCH_ERROR;
     480              : 
     481          250 :   sym = symtree->n.sym;
     482              : 
     483          250 :   if (sym && sym->attr.generic)
     484           60 :     dt_sym = gfc_find_dt_in_generic (sym);
     485              : 
     486           60 :   if (sym == NULL
     487          250 :       || (sym->attr.flavor != FL_PARAMETER
     488           65 :           && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
     489              :     {
     490            5 :       gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
     491              :                  name);
     492            5 :       *result = NULL;
     493            5 :       return MATCH_ERROR;
     494              :     }
     495          245 :   else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
     496           60 :     return gfc_match_structure_constructor (dt_sym, symtree, result);
     497              : 
     498              :   /* Check to see if the value is an initialization array expression.  */
     499          185 :   if (sym->value->expr_type == EXPR_ARRAY)
     500              :     {
     501           67 :       gfc_current_locus = old_loc;
     502              : 
     503           67 :       m = gfc_match_init_expr (result);
     504           67 :       if (m == MATCH_ERROR)
     505              :         return m;
     506              : 
     507           66 :       if (m == MATCH_YES)
     508              :         {
     509           66 :           if (!gfc_simplify_expr (*result, 0))
     510            0 :             m = MATCH_ERROR;
     511              : 
     512           66 :           if ((*result)->expr_type == EXPR_CONSTANT)
     513              :             return m;
     514              :           else
     515              :             {
     516            2 :               gfc_error ("Invalid initializer %s in Data statement at %C", name);
     517            2 :               return MATCH_ERROR;
     518              :             }
     519              :         }
     520              :     }
     521              : 
     522          118 :   *result = gfc_copy_expr (sym->value);
     523          118 :   return MATCH_YES;
     524              : }
     525              : 
     526              : 
     527              : /* Match a list of values in a DATA statement.  The leading '/' has
     528              :    already been seen at this point.  */
     529              : 
     530              : static match
     531         2560 : top_val_list (gfc_data *data)
     532              : {
     533         2560 :   gfc_data_value *new_val, *tail;
     534         2560 :   gfc_expr *expr;
     535         2560 :   match m;
     536              : 
     537         2560 :   tail = NULL;
     538              : 
     539         8349 :   for (;;)
     540              :     {
     541         8349 :       m = match_data_constant (&expr);
     542         8349 :       if (m == MATCH_NO)
     543            3 :         goto syntax;
     544         8346 :       if (m == MATCH_ERROR)
     545              :         return MATCH_ERROR;
     546              : 
     547         8324 :       new_val = gfc_get_data_value ();
     548         8324 :       mpz_init (new_val->repeat);
     549              : 
     550         8324 :       if (tail == NULL)
     551         2535 :         data->value = new_val;
     552              :       else
     553         5789 :         tail->next = new_val;
     554              : 
     555         8324 :       tail = new_val;
     556              : 
     557         8324 :       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
     558              :         {
     559         8119 :           tail->expr = expr;
     560         8119 :           mpz_set_ui (tail->repeat, 1);
     561              :         }
     562              :       else
     563              :         {
     564          205 :           mpz_set (tail->repeat, expr->value.integer);
     565          205 :           gfc_free_expr (expr);
     566              : 
     567          205 :           m = match_data_constant (&tail->expr);
     568          205 :           if (m == MATCH_NO)
     569            0 :             goto syntax;
     570          205 :           if (m == MATCH_ERROR)
     571              :             return MATCH_ERROR;
     572              :         }
     573              : 
     574         8320 :       if (gfc_match_char ('/') == MATCH_YES)
     575              :         break;
     576         5790 :       if (gfc_match_char (',') == MATCH_NO)
     577            1 :         goto syntax;
     578              :     }
     579              : 
     580              :   return MATCH_YES;
     581              : 
     582            4 : syntax:
     583            4 :   gfc_syntax_error (ST_DATA);
     584            4 :   gfc_free_data_all (gfc_current_ns);
     585            4 :   return MATCH_ERROR;
     586              : }
     587              : 
     588              : 
     589              : /* Matches an old style initialization.  */
     590              : 
     591              : static match
     592           70 : match_old_style_init (const char *name)
     593              : {
     594           70 :   match m;
     595           70 :   gfc_symtree *st;
     596           70 :   gfc_symbol *sym;
     597           70 :   gfc_data *newdata, *nd;
     598              : 
     599              :   /* Set up data structure to hold initializers.  */
     600           70 :   gfc_find_sym_tree (name, NULL, 0, &st);
     601           70 :   sym = st->n.sym;
     602              : 
     603           70 :   newdata = gfc_get_data ();
     604           70 :   newdata->var = gfc_get_data_variable ();
     605           70 :   newdata->var->expr = gfc_get_variable_expr (st);
     606           70 :   newdata->var->expr->where = sym->declared_at;
     607           70 :   newdata->where = gfc_current_locus;
     608              : 
     609              :   /* Match initial value list. This also eats the terminal '/'.  */
     610           70 :   m = top_val_list (newdata);
     611           70 :   if (m != MATCH_YES)
     612              :     {
     613            1 :       free (newdata);
     614            1 :       return m;
     615              :     }
     616              : 
     617              :   /* Check that a BOZ did not creep into an old-style initialization.  */
     618          137 :   for (nd = newdata; nd; nd = nd->next)
     619              :     {
     620           69 :       if (nd->value->expr->ts.type == BT_BOZ
     621           69 :           && gfc_invalid_boz (G_("BOZ at %L cannot appear in an old-style "
     622              :                               "initialization"), &nd->value->expr->where))
     623              :         return MATCH_ERROR;
     624              : 
     625           68 :       if (nd->var->expr->ts.type != BT_INTEGER
     626           27 :           && nd->var->expr->ts.type != BT_REAL
     627           21 :           && nd->value->expr->ts.type == BT_BOZ)
     628              :         {
     629            0 :           gfc_error (G_("BOZ literal constant near %L cannot be assigned to "
     630              :                      "a %qs variable in an old-style initialization"),
     631            0 :                      &nd->value->expr->where,
     632              :                      gfc_typename (&nd->value->expr->ts));
     633            0 :           return MATCH_ERROR;
     634              :         }
     635              :     }
     636              : 
     637           68 :   if (gfc_pure (NULL))
     638              :     {
     639            1 :       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
     640            1 :       free (newdata);
     641            1 :       return MATCH_ERROR;
     642              :     }
     643           67 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
     644              : 
     645              :   /* Mark the variable as having appeared in a data statement.  */
     646           67 :   if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
     647              :     {
     648            2 :       free (newdata);
     649            2 :       return MATCH_ERROR;
     650              :     }
     651              : 
     652              :   /* Chain in namespace list of DATA initializers.  */
     653           65 :   newdata->next = gfc_current_ns->data;
     654           65 :   gfc_current_ns->data = newdata;
     655              : 
     656           65 :   return m;
     657              : }
     658              : 
     659              : 
     660              : /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
     661              :    we are matching a DATA statement and are therefore issuing an error
     662              :    if we encounter something unexpected, if not, we're trying to match
     663              :    an old-style initialization expression of the form INTEGER I /2/.  */
     664              : 
     665              : match
     666         2422 : gfc_match_data (void)
     667              : {
     668         2422 :   gfc_data *new_data;
     669         2422 :   gfc_expr *e;
     670         2422 :   gfc_ref *ref;
     671         2422 :   match m;
     672         2422 :   char c;
     673              : 
     674              :   /* DATA has been matched.  In free form source code, the next character
     675              :      needs to be whitespace or '(' from an implied do-loop.  Check that
     676              :      here.  */
     677         2422 :   c = gfc_peek_ascii_char ();
     678         2422 :   if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
     679              :     return MATCH_NO;
     680              : 
     681              :   /* Before parsing the rest of a DATA statement, check F2008:c1206.  */
     682         2421 :   if ((gfc_current_state () == COMP_FUNCTION
     683         2421 :        || gfc_current_state () == COMP_SUBROUTINE)
     684         1153 :       && gfc_state_stack->previous->state == COMP_INTERFACE)
     685              :     {
     686            1 :       gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
     687            1 :       return MATCH_ERROR;
     688              :     }
     689              : 
     690         2420 :   set_in_match_data (true);
     691              : 
     692         2614 :   for (;;)
     693              :     {
     694         2517 :       new_data = gfc_get_data ();
     695         2517 :       new_data->where = gfc_current_locus;
     696              : 
     697         2517 :       m = top_var_list (new_data);
     698         2517 :       if (m != MATCH_YES)
     699           18 :         goto cleanup;
     700              : 
     701         2499 :       if (new_data->var->iter.var
     702          117 :           && new_data->var->iter.var->ts.type == BT_INTEGER
     703           74 :           && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
     704           68 :           && new_data->var->list
     705           68 :           && new_data->var->list->expr
     706           55 :           && new_data->var->list->expr->ts.type == BT_CHARACTER
     707            3 :           && new_data->var->list->expr->ref
     708            3 :           && new_data->var->list->expr->ref->type == REF_SUBSTRING)
     709              :         {
     710            1 :           gfc_error ("Invalid substring in data-implied-do at %L in DATA "
     711              :                      "statement", &new_data->var->list->expr->where);
     712            1 :           goto cleanup;
     713              :         }
     714              : 
     715              :       /* Check for an entity with an allocatable component, which is not
     716              :          allowed.  */
     717         2498 :       e = new_data->var->expr;
     718         2498 :       if (e)
     719              :         {
     720         2382 :           bool invalid;
     721              : 
     722         2382 :           invalid = false;
     723         3606 :           for (ref = e->ref; ref; ref = ref->next)
     724         1224 :             if ((ref->type == REF_COMPONENT
     725          140 :                  && ref->u.c.component->attr.allocatable)
     726         1222 :                 || (ref->type == REF_ARRAY
     727         1034 :                     && e->symtree->n.sym->attr.pointer != 1
     728         1031 :                     && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
     729         1224 :               invalid = true;
     730              : 
     731         2382 :           if (invalid)
     732              :             {
     733            2 :               gfc_error ("Allocatable component or deferred-shaped array "
     734              :                          "near %C in DATA statement");
     735            2 :               goto cleanup;
     736              :             }
     737              : 
     738              :           /* F2008:C567 (R536) A data-i-do-object or a variable that appears
     739              :              as a data-stmt-object shall not be an object designator in which
     740              :              a pointer appears other than as the entire rightmost part-ref.  */
     741         2380 :           if (!e->ref && e->ts.type == BT_DERIVED
     742           43 :               && e->symtree->n.sym->attr.pointer)
     743            4 :             goto partref;
     744              : 
     745         2376 :           ref = e->ref;
     746         2376 :           if (e->symtree->n.sym->ts.type == BT_DERIVED
     747          125 :               && e->symtree->n.sym->attr.pointer
     748            1 :               && ref->type == REF_COMPONENT)
     749            1 :             goto partref;
     750              : 
     751         3591 :           for (; ref; ref = ref->next)
     752         1217 :             if (ref->type == REF_COMPONENT
     753          135 :                 && ref->u.c.component->attr.pointer
     754           27 :                 && ref->next)
     755            1 :               goto partref;
     756              :         }
     757              : 
     758         2490 :       m = top_val_list (new_data);
     759         2490 :       if (m != MATCH_YES)
     760           29 :         goto cleanup;
     761              : 
     762         2461 :       new_data->next = gfc_current_ns->data;
     763         2461 :       gfc_current_ns->data = new_data;
     764              : 
     765              :       /* A BOZ literal constant cannot appear in a structure constructor.
     766              :          Check for that here for a data statement value.  */
     767         2461 :       if (new_data->value->expr->ts.type == BT_DERIVED
     768           37 :           && new_data->value->expr->value.constructor)
     769              :         {
     770           35 :           gfc_constructor *c;
     771           35 :           c = gfc_constructor_first (new_data->value->expr->value.constructor);
     772          106 :           for (; c; c = gfc_constructor_next (c))
     773           36 :             if (c->expr && c->expr->ts.type == BT_BOZ)
     774              :               {
     775            0 :                 gfc_error ("BOZ literal constant at %L cannot appear in a "
     776              :                            "structure constructor", &c->expr->where);
     777            0 :                 return MATCH_ERROR;
     778              :               }
     779              :         }
     780              : 
     781         2461 :       if (gfc_match_eos () == MATCH_YES)
     782              :         break;
     783              : 
     784           97 :       gfc_match_char (',');     /* Optional comma */
     785           97 :     }
     786              : 
     787         2364 :   set_in_match_data (false);
     788              : 
     789         2364 :   if (gfc_pure (NULL))
     790              :     {
     791            0 :       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
     792            0 :       return MATCH_ERROR;
     793              :     }
     794         2364 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
     795              : 
     796         2364 :   return MATCH_YES;
     797              : 
     798            6 : partref:
     799              : 
     800            6 :   gfc_error ("part-ref with pointer attribute near %L is not "
     801              :              "rightmost part-ref of data-stmt-object",
     802              :              &e->where);
     803              : 
     804           56 : cleanup:
     805           56 :   set_in_match_data (false);
     806           56 :   gfc_free_data (new_data);
     807           56 :   return MATCH_ERROR;
     808              : }
     809              : 
     810              : 
     811              : /************************ Declaration statements *********************/
     812              : 
     813              : 
     814              : /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
     815              :    list). The difference here is the expression is a list of constants
     816              :    and is surrounded by '/'.
     817              :    The typespec ts must match the typespec of the variable which the
     818              :    clist is initializing.
     819              :    The arrayspec tells whether this should match a list of constants
     820              :    corresponding to array elements or a scalar (as == NULL).  */
     821              : 
     822              : static match
     823           74 : match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
     824              : {
     825           74 :   gfc_constructor_base array_head = NULL;
     826           74 :   gfc_expr *expr = NULL;
     827           74 :   match m = MATCH_ERROR;
     828           74 :   locus where;
     829           74 :   mpz_t repeat, cons_size, as_size;
     830           74 :   bool scalar;
     831           74 :   int cmp;
     832              : 
     833           74 :   gcc_assert (ts);
     834              : 
     835              :   /* We have already matched '/' - now look for a constant list, as with
     836              :      top_val_list from decl.cc, but append the result to an array.  */
     837           74 :   if (gfc_match ("/") == MATCH_YES)
     838              :     {
     839            1 :       gfc_error ("Empty old style initializer list at %C");
     840            1 :       return MATCH_ERROR;
     841              :     }
     842              : 
     843           73 :   where = gfc_current_locus;
     844           73 :   scalar = !as || !as->rank;
     845              : 
     846           42 :   if (!scalar && !spec_size (as, &as_size))
     847              :     {
     848            2 :       gfc_error ("Array in initializer list at %L must have an explicit shape",
     849            1 :                  as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
     850              :       /* Nothing to cleanup yet.  */
     851            1 :       return MATCH_ERROR;
     852              :     }
     853              : 
     854           72 :   mpz_init_set_ui (repeat, 0);
     855              : 
     856          143 :   for (;;)
     857              :     {
     858          143 :       m = match_data_constant (&expr);
     859          143 :       if (m != MATCH_YES)
     860            3 :         expr = NULL; /* match_data_constant may set expr to garbage */
     861            3 :       if (m == MATCH_NO)
     862            2 :         goto syntax;
     863          141 :       if (m == MATCH_ERROR)
     864            1 :         goto cleanup;
     865              : 
     866              :       /* Found r in repeat spec r*c; look for the constant to repeat.  */
     867          140 :       if ( gfc_match_char ('*') == MATCH_YES)
     868              :         {
     869           18 :           if (scalar)
     870              :             {
     871            1 :               gfc_error ("Repeat spec invalid in scalar initializer at %C");
     872            1 :               goto cleanup;
     873              :             }
     874           17 :           if (expr->ts.type != BT_INTEGER)
     875              :             {
     876            1 :               gfc_error ("Repeat spec must be an integer at %C");
     877            1 :               goto cleanup;
     878              :             }
     879           16 :           mpz_set (repeat, expr->value.integer);
     880           16 :           gfc_free_expr (expr);
     881           16 :           expr = NULL;
     882              : 
     883           16 :           m = match_data_constant (&expr);
     884           16 :           if (m == MATCH_NO)
     885              :             {
     886            1 :               m = MATCH_ERROR;
     887            1 :               gfc_error ("Expected data constant after repeat spec at %C");
     888              :             }
     889           16 :           if (m != MATCH_YES)
     890            1 :             goto cleanup;
     891              :         }
     892              :       /* No repeat spec, we matched the data constant itself. */
     893              :       else
     894          122 :         mpz_set_ui (repeat, 1);
     895              : 
     896          137 :       if (!scalar)
     897              :         {
     898              :           /* Add the constant initializer as many times as repeated. */
     899          251 :           for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
     900              :             {
     901              :               /* Make sure types of elements match */
     902          144 :               if(ts && !gfc_compare_types (&expr->ts, ts)
     903           12 :                     && !gfc_convert_type (expr, ts, 1))
     904            0 :                 goto cleanup;
     905              : 
     906          144 :               gfc_constructor_append_expr (&array_head,
     907              :                   gfc_copy_expr (expr), &gfc_current_locus);
     908              :             }
     909              : 
     910          107 :           gfc_free_expr (expr);
     911          107 :           expr = NULL;
     912              :         }
     913              : 
     914              :       /* For scalar initializers quit after one element.  */
     915              :       else
     916              :         {
     917           30 :           if(gfc_match_char ('/') != MATCH_YES)
     918              :             {
     919            1 :               gfc_error ("End of scalar initializer expected at %C");
     920            1 :               goto cleanup;
     921              :             }
     922              :           break;
     923              :         }
     924              : 
     925          107 :       if (gfc_match_char ('/') == MATCH_YES)
     926              :         break;
     927           72 :       if (gfc_match_char (',') == MATCH_NO)
     928            1 :         goto syntax;
     929              :     }
     930              : 
     931              :   /* If we break early from here out, we encountered an error.  */
     932           64 :   m = MATCH_ERROR;
     933              : 
     934              :   /* Set up expr as an array constructor. */
     935           64 :   if (!scalar)
     936              :     {
     937           35 :       expr = gfc_get_array_expr (ts->type, ts->kind, &where);
     938           35 :       expr->ts = *ts;
     939           35 :       expr->value.constructor = array_head;
     940              : 
     941              :       /* Validate sizes.  We built expr ourselves, so cons_size will be
     942              :          constant (we fail above for non-constant expressions).
     943              :          We still need to verify that the sizes match.  */
     944           35 :       gcc_assert (gfc_array_size (expr, &cons_size));
     945           35 :       cmp = mpz_cmp (cons_size, as_size);
     946           35 :       if (cmp < 0)
     947            2 :         gfc_error ("Not enough elements in array initializer at %C");
     948           33 :       else if (cmp > 0)
     949            3 :         gfc_error ("Too many elements in array initializer at %C");
     950           35 :       mpz_clear (cons_size);
     951           35 :       if (cmp)
     952            5 :         goto cleanup;
     953              : 
     954              :       /* Set the rank/shape to match the LHS as auto-reshape is implied. */
     955           30 :       expr->rank = as->rank;
     956           30 :       expr->corank = as->corank;
     957           30 :       expr->shape = gfc_get_shape (as->rank);
     958           66 :       for (int i = 0; i < as->rank; ++i)
     959           36 :         spec_dimen_size (as, i, &expr->shape[i]);
     960              :     }
     961              : 
     962              :   /* Make sure scalar types match. */
     963           29 :   else if (!gfc_compare_types (&expr->ts, ts)
     964           29 :            && !gfc_convert_type (expr, ts, 1))
     965            2 :     goto cleanup;
     966              : 
     967           57 :   if (expr->ts.u.cl)
     968            1 :     expr->ts.u.cl->length_from_typespec = 1;
     969              : 
     970           57 :   *result = expr;
     971           57 :   m = MATCH_YES;
     972           57 :   goto done;
     973              : 
     974            3 : syntax:
     975            3 :   m = MATCH_ERROR;
     976            3 :   gfc_error ("Syntax error in old style initializer list at %C");
     977              : 
     978           15 : cleanup:
     979           15 :   if (expr)
     980           10 :     expr->value.constructor = NULL;
     981           15 :   gfc_free_expr (expr);
     982           15 :   gfc_constructor_free (array_head);
     983              : 
     984           72 : done:
     985           72 :   mpz_clear (repeat);
     986           72 :   if (!scalar)
     987           41 :     mpz_clear (as_size);
     988              :   return m;
     989              : }
     990              : 
     991              : 
     992              : /* Auxiliary function to merge DIMENSION and CODIMENSION array specs.  */
     993              : 
     994              : static bool
     995          114 : merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
     996              : {
     997          114 :   if ((from->type == AS_ASSUMED_RANK && to->corank)
     998          112 :       || (to->type == AS_ASSUMED_RANK && from->corank))
     999              :     {
    1000            5 :       gfc_error ("The assumed-rank array at %C shall not have a codimension");
    1001            5 :       return false;
    1002              :     }
    1003              : 
    1004          109 :   if (to->rank == 0 && from->rank > 0)
    1005              :     {
    1006           48 :       to->rank = from->rank;
    1007           48 :       to->type = from->type;
    1008           48 :       to->cray_pointee = from->cray_pointee;
    1009           48 :       to->cp_was_assumed = from->cp_was_assumed;
    1010              : 
    1011          152 :       for (int i = to->corank - 1; i >= 0; i--)
    1012              :         {
    1013              :           /* Do not exceed the limits on lower[] and upper[].  gfortran
    1014              :              cleans up elsewhere.  */
    1015          104 :           int j = from->rank + i;
    1016          104 :           if (j >= GFC_MAX_DIMENSIONS)
    1017              :             break;
    1018              : 
    1019          104 :           to->lower[j] = to->lower[i];
    1020          104 :           to->upper[j] = to->upper[i];
    1021              :         }
    1022          115 :       for (int i = 0; i < from->rank; i++)
    1023              :         {
    1024           67 :           if (copy)
    1025              :             {
    1026           43 :               to->lower[i] = gfc_copy_expr (from->lower[i]);
    1027           43 :               to->upper[i] = gfc_copy_expr (from->upper[i]);
    1028              :             }
    1029              :           else
    1030              :             {
    1031           24 :               to->lower[i] = from->lower[i];
    1032           24 :               to->upper[i] = from->upper[i];
    1033              :             }
    1034              :         }
    1035              :     }
    1036           61 :   else if (to->corank == 0 && from->corank > 0)
    1037              :     {
    1038           34 :       to->corank = from->corank;
    1039           34 :       to->cotype = from->cotype;
    1040              : 
    1041          104 :       for (int i = 0; i < from->corank; i++)
    1042              :         {
    1043              :           /* Do not exceed the limits on lower[] and upper[].  gfortran
    1044              :              cleans up elsewhere.  */
    1045           71 :           int k = from->rank + i;
    1046           71 :           int j = to->rank + i;
    1047           71 :           if (j >= GFC_MAX_DIMENSIONS)
    1048              :             break;
    1049              : 
    1050           70 :           if (copy)
    1051              :             {
    1052           37 :               to->lower[j] = gfc_copy_expr (from->lower[k]);
    1053           37 :               to->upper[j] = gfc_copy_expr (from->upper[k]);
    1054              :             }
    1055              :           else
    1056              :             {
    1057           33 :               to->lower[j] = from->lower[k];
    1058           33 :               to->upper[j] = from->upper[k];
    1059              :             }
    1060              :         }
    1061              :     }
    1062              : 
    1063          109 :   if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
    1064              :     {
    1065            1 :       gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
    1066              :                  "allowed dimensions of %d",
    1067              :                  to->rank, to->corank, GFC_MAX_DIMENSIONS);
    1068            1 :       to->corank = GFC_MAX_DIMENSIONS - to->rank;
    1069            1 :       return false;
    1070              :     }
    1071              :   return true;
    1072              : }
    1073              : 
    1074              : 
    1075              : /* Match an intent specification.  Since this can only happen after an
    1076              :    INTENT word, a legal intent-spec must follow.  */
    1077              : 
    1078              : static sym_intent
    1079        28671 : match_intent_spec (void)
    1080              : {
    1081              : 
    1082        28671 :   if (gfc_match (" ( in out )") == MATCH_YES)
    1083              :     return INTENT_INOUT;
    1084        25460 :   if (gfc_match (" ( in )") == MATCH_YES)
    1085              :     return INTENT_IN;
    1086         3754 :   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        28230 : char_len_param_value (gfc_expr **expr, bool *deferred)
    1099              : {
    1100        28230 :   match m;
    1101        28230 :   gfc_expr *p;
    1102              : 
    1103        28230 :   *expr = NULL;
    1104        28230 :   *deferred = false;
    1105              : 
    1106        28230 :   if (gfc_match_char ('*') == MATCH_YES)
    1107              :     return MATCH_YES;
    1108              : 
    1109        21644 :   if (gfc_match_char (':') == MATCH_YES)
    1110              :     {
    1111         3414 :       if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
    1112              :         return MATCH_ERROR;
    1113              : 
    1114         3412 :       *deferred = true;
    1115              : 
    1116         3412 :       return MATCH_YES;
    1117              :     }
    1118              : 
    1119        18230 :   m = gfc_match_expr (expr);
    1120              : 
    1121        18230 :   if (m == MATCH_NO || m == MATCH_ERROR)
    1122              :     return m;
    1123              : 
    1124        18225 :   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        18219 :   p = gfc_copy_expr (*expr);
    1129        18219 :   if (gfc_is_constant_expr (p) && gfc_simplify_expr (p, 1))
    1130        15055 :     gfc_replace_expr (*expr, p);
    1131              :   else
    1132         3164 :     gfc_free_expr (p);
    1133              : 
    1134        18219 :   if ((*expr)->expr_type == EXPR_FUNCTION)
    1135              :     {
    1136         1021 :       if ((*expr)->ts.type == BT_INTEGER
    1137         1020 :           || ((*expr)->ts.type == BT_UNKNOWN
    1138         1020 :               && strcmp((*expr)->symtree->name, "null") != 0))
    1139              :         return MATCH_YES;
    1140              : 
    1141            2 :       goto syntax;
    1142              :     }
    1143        17198 :   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        14965 :       if ((*expr)->ts.type == BT_INTEGER)
    1152              :         {
    1153        14947 :           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         2233 :   else if ((*expr)->expr_type == EXPR_ARRAY)
    1160            8 :     goto syntax;
    1161         2225 :   else if ((*expr)->expr_type == EXPR_VARIABLE)
    1162              :     {
    1163         1576 :       bool t;
    1164         1576 :       gfc_expr *e;
    1165              : 
    1166         1576 :       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         1576 :       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         1574 :       t = gfc_reduce_init_expr (e);
    1176              : 
    1177         1574 :       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         1567 :       if ((e->ref && e->ref->type == REF_ARRAY
    1188            4 :            && e->ref->u.ar.type != AR_ELEMENT)
    1189         1566 :           || (!e->ref && e->expr_type == EXPR_ARRAY))
    1190              :         {
    1191            2 :           gfc_free_expr (e);
    1192            2 :           goto syntax;
    1193              :         }
    1194              : 
    1195         1565 :       gfc_free_expr (e);
    1196              :     }
    1197              : 
    1198        17161 :   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        63821 : match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
    1214              : {
    1215        63821 :   int length;
    1216        63821 :   match m;
    1217              : 
    1218        63821 :   *deferred = false;
    1219        63821 :   m = gfc_match_char ('*');
    1220        63821 :   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       288086 : find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
    1278              : {
    1279       288086 :   gfc_state_data *s;
    1280       288086 :   gfc_symtree *st;
    1281       288086 :   int i;
    1282              : 
    1283       288086 :   i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
    1284       288086 :   if (i == 0)
    1285              :     {
    1286       288086 :       *result = st ? st->n.sym : NULL;
    1287       288086 :       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        65328 : get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
    1322              : {
    1323        65328 :   gfc_symtree *st;
    1324        65328 :   gfc_symbol *sym;
    1325        65328 :   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        65328 :   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        65068 :     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
    1371              : 
    1372        65328 :   if (rc)
    1373              :     return rc;
    1374              : 
    1375        65327 :   sym = *result;
    1376        65327 :   if (sym->attr.proc == PROC_ST_FUNCTION)
    1377              :     return rc;
    1378              : 
    1379        65326 :   if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
    1380              :     {
    1381              :       /* Create a partially populated interface symbol to carry the
    1382              :          characteristics of the procedure and the result.  */
    1383          472 :       sym->tlink = gfc_new_symbol (name, sym->ns);
    1384          472 :       gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
    1385          472 :       gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
    1386          472 :       if (sym->attr.dimension)
    1387           17 :         sym->tlink->as = gfc_copy_array_spec (sym->as);
    1388              : 
    1389              :       /* Ideally, at this point, a copy would be made of the formal
    1390              :          arguments and their namespace. However, this does not appear
    1391              :          to be necessary, albeit at the expense of not being able to
    1392              :          use gfc_compare_interfaces directly.  */
    1393              : 
    1394          472 :       if (sym->result && sym->result != sym)
    1395              :         {
    1396          105 :           sym->tlink->result = sym->result;
    1397          105 :           sym->result = NULL;
    1398              :         }
    1399          367 :       else if (sym->result)
    1400              :         {
    1401           93 :           sym->tlink->result = sym->tlink;
    1402              :         }
    1403              :     }
    1404        64854 :   else if (sym && !sym->gfc_new
    1405        25070 :            && gfc_current_state () != COMP_INTERFACE)
    1406              :     {
    1407              :       /* Trap another encompassed procedure with the same name.  All
    1408              :          these conditions are necessary to avoid picking up an entry
    1409              :          whose name clashes with that of the encompassing procedure;
    1410              :          this is handled using gsymbols to register unique, globally
    1411              :          accessible names.  */
    1412        23735 :       if (sym->attr.flavor != 0
    1413        21652 :           && sym->attr.proc != 0
    1414         2404 :           && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
    1415            7 :           && sym->attr.if_source != IFSRC_UNKNOWN)
    1416              :         {
    1417            7 :           gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1418              :                          name, &sym->declared_at);
    1419            7 :           return true;
    1420              :         }
    1421        23728 :       if (sym->attr.flavor != 0
    1422        21645 :           && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
    1423              :         {
    1424            1 :           gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1425              :                          name, &sym->declared_at);
    1426            1 :           return true;
    1427              :         }
    1428              : 
    1429        23727 :       if (sym->attr.external && sym->attr.procedure
    1430            2 :           && gfc_current_state () == COMP_CONTAINS)
    1431              :         {
    1432            1 :           gfc_error_now ("Contained procedure %qs at %C clashes with "
    1433              :                          "procedure defined at %L",
    1434              :                          name, &sym->declared_at);
    1435            1 :           return true;
    1436              :         }
    1437              : 
    1438              :       /* Trap a procedure with a name the same as interface in the
    1439              :          encompassing scope.  */
    1440        23726 :       if (sym->attr.generic != 0
    1441           60 :           && (sym->attr.subroutine || sym->attr.function)
    1442            1 :           && !sym->attr.mod_proc)
    1443              :         {
    1444            1 :           gfc_error_now ("Name %qs at %C is already defined"
    1445              :                          " as a generic interface at %L",
    1446              :                          name, &sym->declared_at);
    1447            1 :           return true;
    1448              :         }
    1449              : 
    1450              :       /* Trap declarations of attributes in encompassing scope.  The
    1451              :          signature for this is that ts.kind is nonzero for no-CLASS
    1452              :          entity.  For a CLASS entity, ts.kind is zero.  */
    1453        23725 :       if ((sym->ts.kind != 0
    1454        23352 :            || sym->ts.type == BT_CLASS
    1455        23351 :            || sym->ts.type == BT_DERIVED)
    1456          397 :           && !sym->attr.implicit_type
    1457          396 :           && sym->attr.proc == 0
    1458          378 :           && gfc_current_ns->parent != NULL
    1459          138 :           && sym->attr.access == 0
    1460          136 :           && !module_fcn_entry)
    1461              :         {
    1462            5 :           gfc_error_now ("Procedure %qs at %C has an explicit interface "
    1463              :                        "from a previous declaration",  name);
    1464            5 :           return true;
    1465              :         }
    1466              :     }
    1467              : 
    1468              :   /* F2023: C1247 (R1526) MODULE shall appear only in the function-stmt or
    1469              :      subroutine-stmt of a module subprogram or of a nonabstract interface
    1470              :      body that is declared in the scoping unit of a module or submodule.  */
    1471        65311 :   if (sym->attr.external
    1472           92 :       && (sym->attr.subroutine || sym->attr.function)
    1473           91 :       && sym->attr.if_source == IFSRC_IFBODY
    1474           91 :       && !current_attr.module_procedure
    1475            3 :       && sym->attr.proc == PROC_MODULE
    1476            3 :       && gfc_state_stack->state == COMP_CONTAINS)
    1477            1 :     gfc_error_now ("Procedure %qs defined in interface body at %L "
    1478              :                    "clashes with internal procedure defined at %C",
    1479              :                    name, &sym->declared_at);
    1480              : 
    1481              :   /* This is the converse requirement: The separate-module-subprogram for a
    1482              :      module procedure shall have the MODULE prefix or be declared a MODULE
    1483              :      PROCEDURE, otherwise it would be ambiguous.  */
    1484        65311 :   if (sym->attr.module_procedure
    1485          472 :       && (sym->attr.subroutine || sym->attr.function)
    1486          472 :       && sym->attr.if_source == IFSRC_IFBODY
    1487          472 :       && !current_attr.module_procedure
    1488            4 :       && sym->attr.proc == PROC_MODULE
    1489            4 :       && gfc_state_stack->state == COMP_CONTAINS
    1490            2 :       && gfc_state_stack->previous
    1491            2 :       && gfc_state_stack->previous->state == COMP_SUBMODULE)
    1492            1 :     gfc_error_now ("Procedure %qs at %C requires the MODULE prefix because "
    1493              :                    "it is a module procedure declared in module %qs",
    1494            1 :                    name, sym->module ? sym->module : "");
    1495              : 
    1496        65311 :   if (sym && !sym->gfc_new
    1497        25527 :       && sym->attr.flavor != FL_UNKNOWN
    1498        23044 :       && sym->attr.referenced == 0 && sym->attr.subroutine == 1
    1499          244 :       && gfc_state_stack->state == COMP_CONTAINS
    1500          239 :       && gfc_state_stack->previous->state == COMP_SUBROUTINE)
    1501              :     {
    1502            1 :       gfc_error_now ("Procedure %qs at %C is already defined at %L",
    1503              :                      name, &sym->declared_at);
    1504            1 :       return true;
    1505              :     }
    1506              : 
    1507        65310 :   if (gfc_current_ns->parent == NULL || *result == NULL)
    1508              :     return rc;
    1509              : 
    1510              :   /* Module function entries will already have a symtree in
    1511              :      the current namespace but will need one at module level.  */
    1512        52964 :   if (module_fcn_entry)
    1513              :     {
    1514              :       /* Present if entry is declared to be a module procedure.  */
    1515          258 :       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
    1516          258 :       if (st == NULL)
    1517          217 :         st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
    1518              :     }
    1519              :   else
    1520        52706 :     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
    1521              : 
    1522        52964 :   st->n.sym = sym;
    1523        52964 :   sym->refs++;
    1524              : 
    1525              :   /* See if the procedure should be a module procedure.  */
    1526              : 
    1527        52964 :   if (((sym->ns->proc_name != NULL
    1528        52964 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
    1529        21556 :         && sym->attr.proc != PROC_MODULE)
    1530        52964 :        || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
    1531        71673 :       && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
    1532              :     rc = 2;
    1533              : 
    1534              :   return rc;
    1535              : }
    1536              : 
    1537              : 
    1538              : /* Verify that the given symbol representing a parameter is C
    1539              :    interoperable, by checking to see if it was marked as such after
    1540              :    its declaration.  If the given symbol is not interoperable, a
    1541              :    warning is reported, thus removing the need to return the status to
    1542              :    the calling function.  The standard does not require the user use
    1543              :    one of the iso_c_binding named constants to declare an
    1544              :    interoperable parameter, but we can't be sure if the param is C
    1545              :    interop or not if the user doesn't.  For example, integer(4) may be
    1546              :    legal Fortran, but doesn't have meaning in C.  It may interop with
    1547              :    a number of the C types, which causes a problem because the
    1548              :    compiler can't know which one.  This code is almost certainly not
    1549              :    portable, and the user will get what they deserve if the C type
    1550              :    across platforms isn't always interoperable with integer(4).  If
    1551              :    the user had used something like integer(c_int) or integer(c_long),
    1552              :    the compiler could have automatically handled the varying sizes
    1553              :    across platforms.  */
    1554              : 
    1555              : bool
    1556        17318 : gfc_verify_c_interop_param (gfc_symbol *sym)
    1557              : {
    1558        17318 :   int is_c_interop = 0;
    1559        17318 :   bool retval = true;
    1560              : 
    1561              :   /* We check implicitly typed variables in symbol.cc:gfc_set_default_type().
    1562              :      Don't repeat the checks here.  */
    1563        17318 :   if (sym->attr.implicit_type)
    1564              :     return true;
    1565              : 
    1566              :   /* For subroutines or functions that are passed to a BIND(C) procedure,
    1567              :      they're interoperable if they're BIND(C) and their params are all
    1568              :      interoperable.  */
    1569        17318 :   if (sym->attr.flavor == FL_PROCEDURE)
    1570              :     {
    1571            4 :       if (sym->attr.is_bind_c == 0)
    1572              :         {
    1573            0 :           gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
    1574              :                          "attribute to be C interoperable", sym->name,
    1575              :                          &(sym->declared_at));
    1576            0 :           return false;
    1577              :         }
    1578              :       else
    1579              :         {
    1580            4 :           if (sym->attr.is_c_interop == 1)
    1581              :             /* We've already checked this procedure; don't check it again.  */
    1582              :             return true;
    1583              :           else
    1584            4 :             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
    1585            4 :                                       sym->common_block);
    1586              :         }
    1587              :     }
    1588              : 
    1589              :   /* See if we've stored a reference to a procedure that owns sym.  */
    1590        17314 :   if (sym->ns != NULL && sym->ns->proc_name != NULL)
    1591              :     {
    1592        17314 :       if (sym->ns->proc_name->attr.is_bind_c == 1)
    1593              :         {
    1594        17275 :           bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;
    1595        17275 :           bool f2018_added = false;
    1596              : 
    1597        17275 :           is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
    1598              : 
    1599              :           /* F2018:18.3.6 has the following text:
    1600              :              "(5) any dummy argument without the VALUE attribute corresponds to
    1601              :              a formal parameter of the prototype that is of a pointer type, and
    1602              :              either
    1603              :              • the dummy argument is interoperable with an entity of the
    1604              :              referenced type (ISO/IEC 9899:2011, 6.2.5, 7.19, and 7.20.1) of
    1605              :              the formal parameter (this is equivalent to the F2008 text),
    1606              :              • the dummy argument is a nonallocatable nonpointer variable of
    1607              :              type CHARACTER with assumed character length and the formal
    1608              :              parameter is a pointer to CFI_cdesc_t,
    1609              :              • the dummy argument is allocatable, assumed-shape, assumed-rank,
    1610              :              or a pointer without the CONTIGUOUS attribute, and the formal
    1611              :              parameter is a pointer to CFI_cdesc_t, or
    1612              :              • the dummy argument is assumed-type and not allocatable,
    1613              :              assumed-shape, assumed-rank, or a pointer, and the formal
    1614              :              parameter is a pointer to void,"  */
    1615         3731 :           if (is_c_interop == 0 && !sym->attr.value && f2018_allowed)
    1616              :             {
    1617         2364 :               bool as_ar = (sym->as
    1618         2364 :                             && (sym->as->type == AS_ASSUMED_SHAPE
    1619         2117 :                                 || sym->as->type == AS_ASSUMED_RANK));
    1620         4728 :               bool cond1 = (sym->ts.type == BT_CHARACTER
    1621         1565 :                             && !(sym->ts.u.cl && sym->ts.u.cl->length)
    1622          905 :                             && !sym->attr.allocatable
    1623         3251 :                             && !sym->attr.pointer);
    1624         4728 :               bool cond2 = (sym->attr.allocatable
    1625         2267 :                             || as_ar
    1626         3389 :                             || (IS_POINTER (sym) && !sym->attr.contiguous));
    1627         4728 :               bool cond3 = (sym->ts.type == BT_ASSUMED
    1628            0 :                             && !sym->attr.allocatable
    1629            0 :                             && !sym->attr.pointer
    1630         2364 :                             && !as_ar);
    1631         2364 :               f2018_added = cond1 || cond2 || cond3;
    1632              :             }
    1633              : 
    1634        17275 :           if (is_c_interop != 1 && !f2018_added)
    1635              :             {
    1636              :               /* Make personalized messages to give better feedback.  */
    1637         1837 :               if (sym->ts.type == BT_DERIVED)
    1638            1 :                 gfc_error ("Variable %qs at %L is a dummy argument to the "
    1639              :                            "BIND(C) procedure %qs but is not C interoperable "
    1640              :                            "because derived type %qs is not C interoperable",
    1641              :                            sym->name, &(sym->declared_at),
    1642            1 :                            sym->ns->proc_name->name,
    1643            1 :                            sym->ts.u.derived->name);
    1644         1836 :               else if (sym->ts.type == BT_CLASS)
    1645            6 :                 gfc_error ("Variable %qs at %L is a dummy argument to the "
    1646              :                            "BIND(C) procedure %qs but is not C interoperable "
    1647              :                            "because it is polymorphic",
    1648              :                            sym->name, &(sym->declared_at),
    1649            6 :                            sym->ns->proc_name->name);
    1650         1830 :               else if (warn_c_binding_type)
    1651           39 :                 gfc_warning (OPT_Wc_binding_type,
    1652              :                              "Variable %qs at %L is a dummy argument of the "
    1653              :                              "BIND(C) procedure %qs but may not be C "
    1654              :                              "interoperable",
    1655              :                              sym->name, &(sym->declared_at),
    1656           39 :                              sym->ns->proc_name->name);
    1657              :             }
    1658              : 
    1659              :           /* Per F2018, 18.3.6 (5), pointer + contiguous is not permitted.  */
    1660        17275 :           if (sym->attr.pointer && sym->attr.contiguous)
    1661            2 :             gfc_error ("Dummy argument %qs at %L may not be a pointer with "
    1662              :                        "CONTIGUOUS attribute as procedure %qs is BIND(C)",
    1663            2 :                        sym->name, &sym->declared_at, sym->ns->proc_name->name);
    1664              : 
    1665              :           /* Per F2018, C1557, pointer/allocatable dummies to a bind(c)
    1666              :              procedure that are default-initialized are not permitted.  */
    1667        16635 :           if ((sym->attr.pointer || sym->attr.allocatable)
    1668         1041 :               && sym->ts.type == BT_DERIVED
    1669        17653 :               && gfc_has_default_initializer (sym->ts.u.derived))
    1670              :             {
    1671            8 :               gfc_error ("Default-initialized dummy argument %qs with %s "
    1672              :                          "attribute at %L is not permitted in BIND(C) "
    1673              :                          "procedure %qs", sym->name,
    1674            4 :                          (sym->attr.pointer ? "POINTER" : "ALLOCATABLE"),
    1675            4 :                          &sym->declared_at, sym->ns->proc_name->name);
    1676            4 :               retval = false;
    1677              :             }
    1678              : 
    1679              :           /* Character strings are only C interoperable if they have a
    1680              :              length of 1.  However, as an argument they are also interoperable
    1681              :              when passed as descriptor (which requires len=: or len=*).  */
    1682        17275 :           if (sym->ts.type == BT_CHARACTER)
    1683              :             {
    1684         2344 :               gfc_charlen *cl = sym->ts.u.cl;
    1685              : 
    1686         2344 :               if (sym->attr.allocatable || sym->attr.pointer)
    1687              :                 {
    1688              :                   /* F2018, 18.3.6 (6).  */
    1689          195 :                   if (!sym->ts.deferred)
    1690              :                     {
    1691           64 :                       if (sym->attr.allocatable)
    1692           32 :                         gfc_error ("Allocatable character dummy argument %qs "
    1693              :                                    "at %L must have deferred length as "
    1694              :                                    "procedure %qs is BIND(C)", sym->name,
    1695           32 :                                    &sym->declared_at, sym->ns->proc_name->name);
    1696              :                       else
    1697           32 :                         gfc_error ("Pointer character dummy argument %qs at %L "
    1698              :                                    "must have deferred length as procedure %qs "
    1699              :                                    "is BIND(C)", sym->name, &sym->declared_at,
    1700           32 :                                    sym->ns->proc_name->name);
    1701              :                       retval = false;
    1702              :                     }
    1703          131 :                   else if (!gfc_notify_std (GFC_STD_F2018,
    1704              :                                             "Deferred-length character dummy "
    1705              :                                             "argument %qs at %L of procedure "
    1706              :                                             "%qs with BIND(C) attribute",
    1707              :                                             sym->name, &sym->declared_at,
    1708          131 :                                             sym->ns->proc_name->name))
    1709        17275 :                     retval = false;
    1710              :                 }
    1711         2149 :               else if (sym->attr.value
    1712          354 :                        && (!cl || !cl->length
    1713          354 :                            || cl->length->expr_type != EXPR_CONSTANT
    1714          354 :                            || mpz_cmp_si (cl->length->value.integer, 1) != 0))
    1715              :                 {
    1716            1 :                   gfc_error ("Character dummy argument %qs at %L must be "
    1717              :                              "of length 1 as it has the VALUE attribute",
    1718              :                              sym->name, &sym->declared_at);
    1719            1 :                   retval = false;
    1720              :                 }
    1721         2148 :               else if (!cl || !cl->length)
    1722              :                 {
    1723              :                   /* Assumed length; F2018, 18.3.6 (5)(2).
    1724              :                      Uses the CFI array descriptor - also for scalars and
    1725              :                      explicit-size/assumed-size arrays.  */
    1726          959 :                   if (!gfc_notify_std (GFC_STD_F2018,
    1727              :                                       "Assumed-length character dummy argument "
    1728              :                                       "%qs at %L of procedure %qs with BIND(C) "
    1729              :                                       "attribute", sym->name, &sym->declared_at,
    1730          959 :                                       sym->ns->proc_name->name))
    1731        17275 :                     retval = false;
    1732              :                 }
    1733         1189 :               else if (cl->length->expr_type != EXPR_CONSTANT
    1734          875 :                        || mpz_cmp_si (cl->length->value.integer, 1) != 0)
    1735              :                 {
    1736              :                   /* F2018, 18.3.6, (5), item 4.  */
    1737          653 :                   if (!sym->attr.dimension
    1738          645 :                       || sym->as->type == AS_ASSUMED_SIZE
    1739          639 :                       || sym->as->type == AS_EXPLICIT)
    1740              :                     {
    1741           20 :                       gfc_error ("Character dummy argument %qs at %L must be "
    1742              :                                  "of constant length of one or assumed length, "
    1743              :                                  "unless it has assumed shape or assumed rank, "
    1744              :                                  "as procedure %qs has the BIND(C) attribute",
    1745              :                                  sym->name, &sym->declared_at,
    1746           20 :                                  sym->ns->proc_name->name);
    1747           20 :                       retval = false;
    1748              :                     }
    1749              :                   /* else: valid only since F2018 - and an assumed-shape/rank
    1750              :                      array; however, gfc_notify_std is already called when
    1751              :                      those array types are used. Thus, silently accept F200x. */
    1752              :                 }
    1753              :             }
    1754              : 
    1755              :           /* We have to make sure that any param to a bind(c) routine does
    1756              :              not have the allocatable, pointer, or optional attributes,
    1757              :              according to J3/04-007, section 5.1.  */
    1758        17275 :           if (sym->attr.allocatable == 1
    1759        17676 :               && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
    1760              :                                   "ALLOCATABLE attribute in procedure %qs "
    1761              :                                   "with BIND(C)", sym->name,
    1762              :                                   &(sym->declared_at),
    1763          401 :                                   sym->ns->proc_name->name))
    1764              :             retval = false;
    1765              : 
    1766        17275 :           if (sym->attr.pointer == 1
    1767        17915 :               && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
    1768              :                                   "POINTER attribute in procedure %qs "
    1769              :                                   "with BIND(C)", sym->name,
    1770              :                                   &(sym->declared_at),
    1771          640 :                                   sym->ns->proc_name->name))
    1772              :             retval = false;
    1773              : 
    1774        17275 :           if (sym->attr.optional == 1 && sym->attr.value)
    1775              :             {
    1776            9 :               gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
    1777              :                          "and the VALUE attribute because procedure %qs "
    1778              :                          "is BIND(C)", sym->name, &(sym->declared_at),
    1779            9 :                          sym->ns->proc_name->name);
    1780            9 :               retval = false;
    1781              :             }
    1782        17266 :           else if (sym->attr.optional == 1
    1783        18220 :                    && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
    1784              :                                        "at %L with OPTIONAL attribute in "
    1785              :                                        "procedure %qs which is BIND(C)",
    1786              :                                        sym->name, &(sym->declared_at),
    1787          954 :                                        sym->ns->proc_name->name))
    1788              :             retval = false;
    1789              : 
    1790              :           /* Make sure that if it has the dimension attribute, that it is
    1791              :              either assumed size or explicit shape. Deferred shape is already
    1792              :              covered by the pointer/allocatable attribute.  */
    1793         5553 :           if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
    1794        18609 :               && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
    1795              :                                   "at %L as dummy argument to the BIND(C) "
    1796              :                                   "procedure %qs at %L", sym->name,
    1797              :                                   &(sym->declared_at),
    1798              :                                   sym->ns->proc_name->name,
    1799         1334 :                                   &(sym->ns->proc_name->declared_at)))
    1800              :             retval = false;
    1801              :         }
    1802              :     }
    1803              : 
    1804              :   return retval;
    1805              : }
    1806              : 
    1807              : 
    1808              : 
    1809              : /* Function called by variable_decl() that adds a name to the symbol table.  */
    1810              : 
    1811              : static bool
    1812       266309 : build_sym (const char *name, int elem, gfc_charlen *cl, bool cl_deferred,
    1813              :            gfc_array_spec **as, locus *var_locus)
    1814              : {
    1815       266309 :   symbol_attribute attr;
    1816       266309 :   gfc_symbol *sym;
    1817       266309 :   int upper;
    1818       266309 :   gfc_symtree *st, *host_st = NULL;
    1819              : 
    1820              :   /* Symbols in a submodule are host associated from the parent module or
    1821              :      submodules. Therefore, they can be overridden by declarations in the
    1822              :      submodule scope. Deal with this by attaching the existing symbol to
    1823              :      a new symtree and recycling the old symtree with a new symbol...  */
    1824       266309 :   st = gfc_find_symtree (gfc_current_ns->sym_root, name);
    1825       266309 :   if (((st && st->import_only) || (gfc_current_ns->import_state == IMPORT_ALL))
    1826            3 :       && gfc_current_ns->parent)
    1827            3 :     host_st = gfc_find_symtree (gfc_current_ns->parent->sym_root, name);
    1828              : 
    1829       266309 :   if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
    1830           12 :       && st->n.sym != NULL
    1831           12 :       && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
    1832              :     {
    1833           12 :       gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
    1834           12 :       s->n.sym = st->n.sym;
    1835           12 :       sym = gfc_new_symbol (name, gfc_current_ns, var_locus);
    1836              : 
    1837           12 :       st->n.sym = sym;
    1838           12 :       sym->refs++;
    1839           12 :       gfc_set_sym_referenced (sym);
    1840           12 :     }
    1841              :   /* ...Check that F2018 IMPORT, ONLY and IMPORT, ALL statements, within the
    1842              :      current scope are not violated by local redeclarations. Note that there is
    1843              :      no need to guard for std >= F2018 because import_only and IMPORT_ALL are
    1844              :      only set for these standards.  */
    1845       266297 :   else if (host_st && host_st->n.sym
    1846            2 :            && host_st->n.sym != gfc_current_ns->proc_name
    1847            2 :            && !(st && st->n.sym
    1848            1 :                 && (st->n.sym->attr.dummy || st->n.sym->attr.result)))
    1849              :     {
    1850            2 :       gfc_error ("F2018: C8102 %s at %L is already imported by an %s "
    1851              :                  "statement and must not be re-declared", name, var_locus,
    1852            1 :                  (st && st->import_only) ? "IMPORT, ONLY" : "IMPORT, ALL");
    1853            2 :       return false;
    1854              :     }
    1855              :   /* ...Otherwise generate a new symtree and new symbol.  */
    1856       266295 :   else if (gfc_get_symbol (name, NULL, &sym, var_locus))
    1857              :     return false;
    1858              : 
    1859              :   /* Check if the name has already been defined as a type.  The
    1860              :      first letter of the symtree will be in upper case then.  Of
    1861              :      course, this is only necessary if the upper case letter is
    1862              :      actually different.  */
    1863              : 
    1864       266307 :   upper = TOUPPER(name[0]);
    1865       266307 :   if (upper != name[0])
    1866              :     {
    1867       265557 :       char u_name[GFC_MAX_SYMBOL_LEN + 1];
    1868       265557 :       gfc_symtree *st;
    1869              : 
    1870       265557 :       gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
    1871       265557 :       strcpy (u_name, name);
    1872       265557 :       u_name[0] = upper;
    1873              : 
    1874       265557 :       st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
    1875              : 
    1876              :       /* STRUCTURE types can alias symbol names */
    1877       265557 :       if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
    1878              :         {
    1879            1 :           gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
    1880              :                      &st->n.sym->declared_at);
    1881            1 :           return false;
    1882              :         }
    1883              :     }
    1884              : 
    1885              :   /* Start updating the symbol table.  Add basic type attribute if present.  */
    1886       266306 :   if (current_ts.type != BT_UNKNOWN
    1887       266306 :       && (sym->attr.implicit_type == 0
    1888          186 :           || !gfc_compare_types (&sym->ts, &current_ts))
    1889       532430 :       && !gfc_add_type (sym, &current_ts, var_locus))
    1890              :     {
    1891              :       /* Duplicate-type rejection can leave a fresh CHARACTER length node on
    1892              :          the namespace list before it is attached to any surviving symbol.
    1893              :          Drop only that unattached node; shared constant charlen nodes are
    1894              :          already reachable from earlier declarations.  PR82721.  */
    1895           27 :       if (current_ts.type == BT_CHARACTER && cl && elem == 1)
    1896              :         {
    1897            1 :           discard_pending_charlen (cl);
    1898            1 :           gfc_clear_ts (&current_ts);
    1899              :         }
    1900           26 :       else if (current_ts.type == BT_CHARACTER && cl && cl != current_ts.u.cl)
    1901            0 :         discard_pending_charlen (cl);
    1902              :       return false;
    1903              :     }
    1904              : 
    1905       266279 :   if (sym->ts.type == BT_CHARACTER)
    1906              :     {
    1907        29345 :       if (elem > 1)
    1908         4166 :         sym->ts.u.cl = gfc_new_charlen (sym->ns, cl);
    1909              :       else
    1910              :         sym->ts.u.cl = cl;
    1911        29345 :       sym->ts.deferred = cl_deferred;
    1912              :     }
    1913              : 
    1914              :   /* Add dimension attribute if present.  */
    1915       266279 :   if (!gfc_set_array_spec (sym, *as, var_locus))
    1916              :     return false;
    1917       266277 :   *as = NULL;
    1918              : 
    1919              :   /* Add attribute to symbol.  The copy is so that we can reset the
    1920              :      dimension attribute.  */
    1921       266277 :   attr = current_attr;
    1922       266277 :   attr.dimension = 0;
    1923       266277 :   attr.codimension = 0;
    1924              : 
    1925       266277 :   if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
    1926              :     return false;
    1927              : 
    1928              :   /* Finish any work that may need to be done for the binding label,
    1929              :      if it's a bind(c).  The bind(c) attr is found before the symbol
    1930              :      is made, and before the symbol name (for data decls), so the
    1931              :      current_ts is holding the binding label, or nothing if the
    1932              :      name= attr wasn't given.  Therefore, test here if we're dealing
    1933              :      with a bind(c) and make sure the binding label is set correctly.  */
    1934       266265 :   if (sym->attr.is_bind_c == 1)
    1935              :     {
    1936         1788 :       if (!sym->binding_label)
    1937              :         {
    1938              :           /* Set the binding label and verify that if a NAME= was specified
    1939              :              then only one identifier was in the entity-decl-list.  */
    1940          137 :           if (!set_binding_label (&sym->binding_label, sym->name,
    1941              :                                   num_idents_on_line))
    1942              :             return false;
    1943              :         }
    1944              :     }
    1945              : 
    1946              :   /* See if we know we're in a common block, and if it's a bind(c)
    1947              :      common then we need to make sure we're an interoperable type.  */
    1948       266263 :   if (sym->attr.in_common == 1)
    1949              :     {
    1950              :       /* Test the common block object.  */
    1951          614 :       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
    1952            6 :           && sym->ts.is_c_interop != 1)
    1953              :         {
    1954            0 :           gfc_error_now ("Variable %qs in common block %qs at %C "
    1955              :                          "must be declared with a C interoperable "
    1956              :                          "kind since common block %qs is BIND(C)",
    1957              :                          sym->name, sym->common_block->name,
    1958            0 :                          sym->common_block->name);
    1959            0 :           gfc_clear_error ();
    1960              :         }
    1961              :     }
    1962              : 
    1963       266263 :   sym->attr.implied_index = 0;
    1964              : 
    1965              :   /* Use the parameter expressions for a parameterized derived type.  */
    1966       266263 :   if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    1967        37885 :       && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
    1968         1206 :     sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
    1969              : 
    1970       266263 :   if (sym->ts.type == BT_CLASS)
    1971        11414 :     return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
    1972              : 
    1973              :   return true;
    1974              : }
    1975              : 
    1976              : 
    1977              : /* Set character constant to the given length. The constant will be padded or
    1978              :    truncated.  If we're inside an array constructor without a typespec, we
    1979              :    additionally check that all elements have the same length; check_len -1
    1980              :    means no checking.  */
    1981              : 
    1982              : void
    1983        14589 : gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
    1984              :                                 gfc_charlen_t check_len)
    1985              : {
    1986        14589 :   gfc_char_t *s;
    1987        14589 :   gfc_charlen_t slen;
    1988              : 
    1989        14589 :   if (expr->ts.type != BT_CHARACTER)
    1990              :     return;
    1991              : 
    1992        14587 :   if (expr->expr_type != EXPR_CONSTANT)
    1993              :     {
    1994            1 :       gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
    1995            1 :       return;
    1996              :     }
    1997              : 
    1998        14586 :   slen = expr->value.character.length;
    1999        14586 :   if (len != slen)
    2000              :     {
    2001         2178 :       s = gfc_get_wide_string (len + 1);
    2002         2178 :       memcpy (s, expr->value.character.string,
    2003         2178 :               MIN (len, slen) * sizeof (gfc_char_t));
    2004         2178 :       if (len > slen)
    2005         1887 :         gfc_wide_memset (&s[slen], ' ', len - slen);
    2006              : 
    2007         2178 :       if (warn_character_truncation && slen > len)
    2008            1 :         gfc_warning_now (OPT_Wcharacter_truncation,
    2009              :                          "CHARACTER expression at %L is being truncated "
    2010              :                          "(%ld/%ld)", &expr->where,
    2011              :                          (long) slen, (long) len);
    2012              : 
    2013              :       /* Apply the standard by 'hand' otherwise it gets cleared for
    2014              :          initializers.  */
    2015         2178 :       if (check_len != -1 && slen != check_len)
    2016              :         {
    2017            3 :           if (!(gfc_option.allow_std & GFC_STD_GNU))
    2018            0 :             gfc_error_now ("The CHARACTER elements of the array constructor "
    2019              :                            "at %L must have the same length (%ld/%ld)",
    2020              :                            &expr->where, (long) slen,
    2021              :                            (long) check_len);
    2022              :           else
    2023            3 :             gfc_notify_std (GFC_STD_LEGACY,
    2024              :                             "The CHARACTER elements of the array constructor "
    2025              :                             "at %L must have the same length (%ld/%ld)",
    2026              :                             &expr->where, (long) slen,
    2027              :                             (long) check_len);
    2028              :         }
    2029              : 
    2030         2178 :       s[len] = '\0';
    2031         2178 :       free (expr->value.character.string);
    2032         2178 :       expr->value.character.string = s;
    2033         2178 :       expr->value.character.length = len;
    2034              :       /* If explicit representation was given, clear it
    2035              :          as it is no longer needed after padding.  */
    2036         2178 :       if (expr->representation.length)
    2037              :         {
    2038           45 :           expr->representation.length = 0;
    2039           45 :           free (expr->representation.string);
    2040           45 :           expr->representation.string = NULL;
    2041              :         }
    2042              :     }
    2043              : }
    2044              : 
    2045              : 
    2046              : /* Function to create and update the enumerator history
    2047              :    using the information passed as arguments.
    2048              :    Pointer "max_enum" is also updated, to point to
    2049              :    enum history node containing largest initializer.
    2050              : 
    2051              :    SYM points to the symbol node of enumerator.
    2052              :    INIT points to its enumerator value.  */
    2053              : 
    2054              : static void
    2055          543 : create_enum_history (gfc_symbol *sym, gfc_expr *init)
    2056              : {
    2057          543 :   enumerator_history *new_enum_history;
    2058          543 :   gcc_assert (sym != NULL && init != NULL);
    2059              : 
    2060          543 :   new_enum_history = XCNEW (enumerator_history);
    2061              : 
    2062          543 :   new_enum_history->sym = sym;
    2063          543 :   new_enum_history->initializer = init;
    2064          543 :   new_enum_history->next = NULL;
    2065              : 
    2066          543 :   if (enum_history == NULL)
    2067              :     {
    2068          160 :       enum_history = new_enum_history;
    2069          160 :       max_enum = enum_history;
    2070              :     }
    2071              :   else
    2072              :     {
    2073          383 :       new_enum_history->next = enum_history;
    2074          383 :       enum_history = new_enum_history;
    2075              : 
    2076          383 :       if (mpz_cmp (max_enum->initializer->value.integer,
    2077          383 :                    new_enum_history->initializer->value.integer) < 0)
    2078          381 :         max_enum = new_enum_history;
    2079              :     }
    2080          543 : }
    2081              : 
    2082              : 
    2083              : /* Function to free enum kind history.  */
    2084              : 
    2085              : void
    2086          175 : gfc_free_enum_history (void)
    2087              : {
    2088          175 :   enumerator_history *current = enum_history;
    2089          175 :   enumerator_history *next;
    2090              : 
    2091          718 :   while (current != NULL)
    2092              :     {
    2093          543 :       next = current->next;
    2094          543 :       free (current);
    2095          543 :       current = next;
    2096              :     }
    2097          175 :   max_enum = NULL;
    2098          175 :   enum_history = NULL;
    2099          175 : }
    2100              : 
    2101              : 
    2102              : /* Function to fix initializer character length if the length of the
    2103              :    symbol or component is constant.  */
    2104              : 
    2105              : static bool
    2106         2777 : fix_initializer_charlen (gfc_typespec *ts, gfc_expr *init)
    2107              : {
    2108         2777 :   if (!gfc_specification_expr (ts->u.cl->length))
    2109              :     return false;
    2110              : 
    2111         2777 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    2112              : 
    2113              :   /* resolve_charlen will complain later on if the length
    2114              :      is too large.  Just skip the initialization in that case.  */
    2115         2777 :   if (mpz_cmp (ts->u.cl->length->value.integer,
    2116         2777 :                gfc_integer_kinds[k].huge) <= 0)
    2117              :     {
    2118         2776 :       HOST_WIDE_INT len
    2119         2776 :                 = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    2120              : 
    2121         2776 :       if (init->expr_type == EXPR_CONSTANT)
    2122         2012 :         gfc_set_constant_character_len (len, init, -1);
    2123          764 :       else if (init->expr_type == EXPR_ARRAY)
    2124              :         {
    2125          757 :           gfc_constructor *cons;
    2126              : 
    2127              :           /* Build a new charlen to prevent simplification from
    2128              :              deleting the length before it is resolved.  */
    2129          757 :           init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2130          757 :           init->ts.u.cl->length = gfc_copy_expr (ts->u.cl->length);
    2131          757 :           cons = gfc_constructor_first (init->value.constructor);
    2132         5121 :           for (; cons; cons = gfc_constructor_next (cons))
    2133         3607 :             gfc_set_constant_character_len (len, cons->expr, -1);
    2134              :         }
    2135              :     }
    2136              : 
    2137              :   return true;
    2138              : }
    2139              : 
    2140              : 
    2141              : /* Function called by variable_decl() that adds an initialization
    2142              :    expression to a symbol.  */
    2143              : 
    2144              : static bool
    2145       274660 : add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus,
    2146              :                       gfc_charlen *saved_cl_list)
    2147              : {
    2148       274660 :   symbol_attribute attr;
    2149       274660 :   gfc_symbol *sym;
    2150       274660 :   gfc_expr *init;
    2151              : 
    2152       274660 :   init = *initp;
    2153       274660 :   if (find_special (name, &sym, false))
    2154              :     return false;
    2155              : 
    2156       274660 :   attr = sym->attr;
    2157              : 
    2158              :   /* If this symbol is confirming an implicit parameter type,
    2159              :      then an initialization expression is not allowed.  */
    2160       274660 :   if (attr.flavor == FL_PARAMETER && sym->value != NULL)
    2161              :     {
    2162            1 :       if (*initp != NULL)
    2163              :         {
    2164            0 :           gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
    2165              :                      sym->name);
    2166            0 :           return false;
    2167              :         }
    2168              :       else
    2169              :         return true;
    2170              :     }
    2171              : 
    2172       274659 :   if (init == NULL)
    2173              :     {
    2174              :       /* An initializer is required for PARAMETER declarations.  */
    2175       240989 :       if (attr.flavor == FL_PARAMETER)
    2176              :         {
    2177            1 :           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
    2178            1 :           return false;
    2179              :         }
    2180              :     }
    2181              :   else
    2182              :     {
    2183              :       /* If a variable appears in a DATA block, it cannot have an
    2184              :          initializer.  */
    2185        33670 :       if (sym->attr.data)
    2186              :         {
    2187            0 :           gfc_error ("Variable %qs at %C with an initializer already "
    2188              :                      "appears in a DATA statement", sym->name);
    2189            0 :           return false;
    2190              :         }
    2191              : 
    2192              :       /* Check if the assignment can happen. This has to be put off
    2193              :          until later for derived type variables and procedure pointers.  */
    2194        32483 :       if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
    2195        32460 :           && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
    2196        32410 :           && !sym->attr.proc_pointer
    2197        65971 :           && !gfc_check_assign_symbol (sym, NULL, init))
    2198              :         return false;
    2199              : 
    2200        33639 :       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
    2201         3476 :             && init->ts.type == BT_CHARACTER)
    2202              :         {
    2203              :           /* Update symbol character length according initializer.  */
    2204         3312 :           if (!gfc_check_assign_symbol (sym, NULL, init))
    2205              :             return false;
    2206              : 
    2207         3312 :           if (sym->ts.u.cl->length == NULL)
    2208              :             {
    2209          863 :               gfc_charlen_t clen;
    2210              :               /* If there are multiple CHARACTER variables declared on the
    2211              :                  same line, we don't want them to share the same length.  */
    2212          863 :               sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2213              : 
    2214          863 :               if (sym->attr.flavor == FL_PARAMETER)
    2215              :                 {
    2216          854 :                   if (init->expr_type == EXPR_CONSTANT)
    2217              :                     {
    2218          563 :                       clen = init->value.character.length;
    2219          563 :                       sym->ts.u.cl->length
    2220          563 :                                 = gfc_get_int_expr (gfc_charlen_int_kind,
    2221              :                                                     NULL, clen);
    2222              :                     }
    2223          291 :                   else if (init->expr_type == EXPR_ARRAY)
    2224              :                     {
    2225          291 :                       if (init->ts.u.cl && init->ts.u.cl->length)
    2226              :                         {
    2227          279 :                           const gfc_expr *length = init->ts.u.cl->length;
    2228          279 :                           if (length->expr_type != EXPR_CONSTANT)
    2229              :                             {
    2230            3 :                               gfc_error ("Cannot initialize parameter array "
    2231              :                                          "at %L "
    2232              :                                          "with variable length elements",
    2233              :                                          &sym->declared_at);
    2234              : 
    2235              :                               /* This rejection path can leave several
    2236              :                                  declaration-local charlens on cl_list,
    2237              :                                  including the replacement symbol charlen and
    2238              :                                  the array-constructor typespec charlen.
    2239              :                                  Clear the surviving owners first, then drop
    2240              :                                  only the nodes created by this declaration.  */
    2241            3 :                               sym->ts.u.cl = NULL;
    2242            3 :                               init->ts.u.cl = NULL;
    2243            3 :                               discard_pending_charlens (saved_cl_list);
    2244            3 :                               return false;
    2245              :                             }
    2246          276 :                           clen = mpz_get_si (length->value.integer);
    2247          276 :                         }
    2248           12 :                       else if (init->value.constructor)
    2249              :                         {
    2250           12 :                           gfc_constructor *c;
    2251           12 :                           c = gfc_constructor_first (init->value.constructor);
    2252           12 :                           clen = c->expr->value.character.length;
    2253              :                         }
    2254              :                       else
    2255            0 :                           gcc_unreachable ();
    2256          288 :                       sym->ts.u.cl->length
    2257          288 :                                 = gfc_get_int_expr (gfc_charlen_int_kind,
    2258              :                                                     NULL, clen);
    2259              :                     }
    2260            0 :                   else if (init->ts.u.cl && init->ts.u.cl->length)
    2261            0 :                     sym->ts.u.cl->length =
    2262            0 :                                 gfc_copy_expr (init->ts.u.cl->length);
    2263              :                 }
    2264              :             }
    2265              :           /* Update initializer character length according to symbol.  */
    2266         2449 :           else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2267         2449 :                    && !fix_initializer_charlen (&sym->ts, init))
    2268              :             return false;
    2269              :         }
    2270              : 
    2271        33636 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
    2272         3814 :           && sym->as->rank && init->rank && init->rank != sym->as->rank)
    2273              :         {
    2274            3 :           gfc_error ("Rank mismatch of array at %L and its initializer "
    2275              :                      "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
    2276            3 :           return false;
    2277              :         }
    2278              : 
    2279              :       /* If sym is implied-shape, set its upper bounds from init.  */
    2280        33633 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
    2281         3811 :           && sym->as && sym->as->type == AS_IMPLIED_SHAPE)
    2282              :         {
    2283         1041 :           int dim;
    2284              : 
    2285         1041 :           if (init->rank == 0)
    2286              :             {
    2287            1 :               gfc_error ("Cannot initialize implied-shape array at %L"
    2288              :                          " with scalar", &sym->declared_at);
    2289            1 :               return false;
    2290              :             }
    2291              : 
    2292              :           /* The shape may be NULL for EXPR_ARRAY, set it.  */
    2293         1040 :           if (init->shape == NULL)
    2294              :             {
    2295            5 :               if (init->expr_type != EXPR_ARRAY)
    2296              :                 {
    2297            2 :                   gfc_error ("Bad shape of initializer at %L", &init->where);
    2298            2 :                   return false;
    2299              :                 }
    2300              : 
    2301            3 :               init->shape = gfc_get_shape (1);
    2302            3 :               if (!gfc_array_size (init, &init->shape[0]))
    2303              :                 {
    2304            1 :                   gfc_error ("Cannot determine shape of initializer at %L",
    2305              :                              &init->where);
    2306            1 :                   free (init->shape);
    2307            1 :                   init->shape = NULL;
    2308            1 :                   return false;
    2309              :                 }
    2310              :             }
    2311              : 
    2312         2175 :           for (dim = 0; dim < sym->as->rank; ++dim)
    2313              :             {
    2314         1139 :               int k;
    2315         1139 :               gfc_expr *e, *lower;
    2316              : 
    2317         1139 :               lower = sym->as->lower[dim];
    2318              : 
    2319              :               /* If the lower bound is an array element from another
    2320              :                  parameterized array, then it is marked with EXPR_VARIABLE and
    2321              :                  is an initialization expression.  Try to reduce it.  */
    2322         1139 :               if (lower->expr_type == EXPR_VARIABLE)
    2323            7 :                 gfc_reduce_init_expr (lower);
    2324              : 
    2325         1139 :               if (lower->expr_type == EXPR_CONSTANT)
    2326              :                 {
    2327              :                   /* All dimensions must be without upper bound.  */
    2328         1138 :                   gcc_assert (!sym->as->upper[dim]);
    2329              : 
    2330         1138 :                   k = lower->ts.kind;
    2331         1138 :                   e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
    2332         1138 :                   mpz_add (e->value.integer, lower->value.integer,
    2333         1138 :                            init->shape[dim]);
    2334         1138 :                   mpz_sub_ui (e->value.integer, e->value.integer, 1);
    2335         1138 :                   sym->as->upper[dim] = e;
    2336              :                 }
    2337              :               else
    2338              :                 {
    2339            1 :                   gfc_error ("Non-constant lower bound in implied-shape"
    2340              :                              " declaration at %L", &lower->where);
    2341            1 :                   return false;
    2342              :                 }
    2343              :             }
    2344              : 
    2345         1036 :           sym->as->type = AS_EXPLICIT;
    2346              :         }
    2347              : 
    2348              :       /* Ensure that explicit bounds are simplified.  */
    2349        33628 :       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
    2350         3806 :           && sym->as && sym->as->type == AS_EXPLICIT)
    2351              :         {
    2352         8446 :           for (int dim = 0; dim < sym->as->rank; ++dim)
    2353              :             {
    2354         4652 :               gfc_expr *e;
    2355              : 
    2356         4652 :               e = sym->as->lower[dim];
    2357         4652 :               if (e->expr_type != EXPR_CONSTANT)
    2358           12 :                 gfc_reduce_init_expr (e);
    2359              : 
    2360         4652 :               e = sym->as->upper[dim];
    2361         4652 :               if (e->expr_type != EXPR_CONSTANT)
    2362          106 :                 gfc_reduce_init_expr (e);
    2363              :             }
    2364              :         }
    2365              : 
    2366              :       /* Need to check if the expression we initialized this
    2367              :          to was one of the iso_c_binding named constants.  If so,
    2368              :          and we're a parameter (constant), let it be iso_c.
    2369              :          For example:
    2370              :          integer(c_int), parameter :: my_int = c_int
    2371              :          integer(my_int) :: my_int_2
    2372              :          If we mark my_int as iso_c (since we can see it's value
    2373              :          is equal to one of the named constants), then my_int_2
    2374              :          will be considered C interoperable.  */
    2375        33628 :       if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
    2376              :         {
    2377        28971 :           sym->ts.is_iso_c |= init->ts.is_iso_c;
    2378        28971 :           sym->ts.is_c_interop |= init->ts.is_c_interop;
    2379              :           /* attr bits needed for module files.  */
    2380        28971 :           sym->attr.is_iso_c |= init->ts.is_iso_c;
    2381        28971 :           sym->attr.is_c_interop |= init->ts.is_c_interop;
    2382        28971 :           if (init->ts.is_iso_c)
    2383          118 :             sym->ts.f90_type = init->ts.f90_type;
    2384              :         }
    2385              : 
    2386              :       /* Catch the case:  type(t), parameter :: x = z'1'.  */
    2387        33628 :       if (sym->ts.type == BT_DERIVED && init->ts.type == BT_BOZ)
    2388              :         {
    2389            1 :           gfc_error ("Entity %qs at %L is incompatible with a BOZ "
    2390              :                      "literal constant", name, &sym->declared_at);
    2391            1 :           return false;
    2392              :         }
    2393              : 
    2394              :       /* Add initializer.  Make sure we keep the ranks sane.  */
    2395        33627 :       if (sym->attr.dimension && init->rank == 0)
    2396              :         {
    2397         1313 :           mpz_t size;
    2398         1313 :           gfc_expr *array;
    2399         1313 :           int n;
    2400         1313 :           if (sym->attr.flavor == FL_PARAMETER
    2401          468 :               && gfc_is_constant_expr (init)
    2402          467 :               && (init->expr_type == EXPR_CONSTANT
    2403           48 :                   || init->expr_type == EXPR_STRUCTURE)
    2404         1780 :               && spec_size (sym->as, &size))
    2405              :             {
    2406          463 :               array = gfc_get_array_expr (init->ts.type, init->ts.kind,
    2407              :                                           &init->where);
    2408          463 :               if (init->ts.type == BT_DERIVED)
    2409           48 :                 array->ts.u.derived = init->ts.u.derived;
    2410        67619 :               for (n = 0; n < (int)mpz_get_si (size); n++)
    2411       133990 :                 gfc_constructor_append_expr (&array->value.constructor,
    2412              :                                              n == 0
    2413              :                                                 ? init
    2414        66834 :                                                 : gfc_copy_expr (init),
    2415              :                                              &init->where);
    2416              : 
    2417          463 :               array->shape = gfc_get_shape (sym->as->rank);
    2418         1052 :               for (n = 0; n < sym->as->rank; n++)
    2419          589 :                 spec_dimen_size (sym->as, n, &array->shape[n]);
    2420              : 
    2421          463 :               init = array;
    2422          463 :               mpz_clear (size);
    2423              :             }
    2424         1313 :           init->rank = sym->as->rank;
    2425         1313 :           init->corank = sym->as->corank;
    2426              :         }
    2427              : 
    2428        33627 :       sym->value = init;
    2429        33627 :       if (sym->attr.save == SAVE_NONE)
    2430        28895 :         sym->attr.save = SAVE_IMPLICIT;
    2431        33627 :       *initp = NULL;
    2432              :     }
    2433              : 
    2434              :   return true;
    2435              : }
    2436              : 
    2437              : 
    2438              : /* Function called by variable_decl() that adds a name to a structure
    2439              :    being built.  */
    2440              : 
    2441              : static bool
    2442        18946 : build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
    2443              :               gfc_array_spec **as)
    2444              : {
    2445        18946 :   gfc_state_data *s;
    2446        18946 :   gfc_component *c;
    2447              : 
    2448              :   /* F03:C438/C439. If the current symbol is of the same derived type that we're
    2449              :      constructing, it must have the pointer attribute.  */
    2450        18946 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    2451         3557 :       && current_ts.u.derived == gfc_current_block ()
    2452          291 :       && current_attr.pointer == 0)
    2453              :     {
    2454          130 :       if (current_attr.allocatable
    2455          130 :           && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
    2456              :                              "must have the POINTER attribute"))
    2457              :         {
    2458              :           return false;
    2459              :         }
    2460          129 :       else if (current_attr.allocatable == 0)
    2461              :         {
    2462            0 :           gfc_error ("Component at %C must have the POINTER attribute");
    2463            0 :           return false;
    2464              :         }
    2465              :     }
    2466              : 
    2467              :   /* F03:C437.  */
    2468        18945 :   if (current_ts.type == BT_CLASS
    2469          887 :       && !(current_attr.pointer || current_attr.allocatable))
    2470              :     {
    2471            5 :       gfc_error ("Component %qs with CLASS at %C must be allocatable "
    2472              :                  "or pointer", name);
    2473            5 :       return false;
    2474              :     }
    2475              : 
    2476        18940 :   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
    2477              :     {
    2478            0 :       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
    2479              :         {
    2480            0 :           gfc_error ("Array component of structure at %C must have explicit "
    2481              :                      "or deferred shape");
    2482            0 :           return false;
    2483              :         }
    2484              :     }
    2485              : 
    2486              :   /* If we are in a nested union/map definition, gfc_add_component will not
    2487              :      properly find repeated components because:
    2488              :        (i) gfc_add_component does a flat search, where components of unions
    2489              :            and maps are implicity chained so nested components may conflict.
    2490              :       (ii) Unions and maps are not linked as components of their parent
    2491              :            structures until after they are parsed.
    2492              :      For (i) we use gfc_find_component which searches recursively, and for (ii)
    2493              :      we search each block directly from the parse stack until we find the top
    2494              :      level structure.  */
    2495              : 
    2496        18940 :   s = gfc_state_stack;
    2497        18940 :   if (s->state == COMP_UNION || s->state == COMP_MAP)
    2498              :     {
    2499         1434 :       while (s->state == COMP_UNION || gfc_comp_struct (s->state))
    2500              :         {
    2501         1434 :           c = gfc_find_component (s->sym, name, true, true, NULL);
    2502         1434 :           if (c != NULL)
    2503              :             {
    2504            0 :               gfc_error_now ("Component %qs at %C already declared at %L",
    2505              :                              name, &c->loc);
    2506            0 :               return false;
    2507              :             }
    2508              :           /* Break after we've searched the entire chain.  */
    2509         1434 :           if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
    2510              :             break;
    2511         1000 :           s = s->previous;
    2512              :         }
    2513              :     }
    2514              : 
    2515        18940 :   if (!gfc_add_component (gfc_current_block(), name, &c))
    2516              :     return false;
    2517              : 
    2518        18934 :   c->ts = current_ts;
    2519        18934 :   if (c->ts.type == BT_CHARACTER)
    2520              :     {
    2521         2054 :       c->ts.u.cl = cl;
    2522              :       /* The component struct is not tracked by the symbol undo mechanism,
    2523              :          so free the charlen here to prevent a double-free.  */
    2524         2054 :       gfc_remove_saved_charlen (cl);
    2525              :     }
    2526              : 
    2527        18934 :   if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
    2528        15383 :       && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
    2529         2330 :       && saved_kind_expr != NULL)
    2530          356 :     c->kind_expr = gfc_copy_expr (saved_kind_expr);
    2531              : 
    2532        18934 :   c->attr = current_attr;
    2533              : 
    2534        18934 :   c->initializer = *init;
    2535        18934 :   *init = NULL;
    2536              : 
    2537              :   /* Update initializer character length according to component.  */
    2538         2054 :   if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length
    2539         1641 :       && c->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2540         1522 :       && c->initializer && c->initializer->ts.type == BT_CHARACTER
    2541        19265 :       && !fix_initializer_charlen (&c->ts, c->initializer))
    2542              :     return false;
    2543              : 
    2544        18934 :   c->as = *as;
    2545        18934 :   if (c->as != NULL)
    2546              :     {
    2547         5077 :       if (c->as->corank)
    2548          113 :         c->attr.codimension = 1;
    2549         5077 :       if (c->as->rank)
    2550         4996 :         c->attr.dimension = 1;
    2551              :     }
    2552        18934 :   *as = NULL;
    2553              : 
    2554        18934 :   gfc_apply_init (&c->ts, &c->attr, c->initializer);
    2555              : 
    2556              :   /* Convert a class, PDT component of a non-derived type to a specific instance
    2557              :      before gfc_build_class_symbol gets to work on it.  */
    2558        18934 :   if (c->ts.type == BT_CLASS
    2559          882 :       && !(gfc_current_block ()->attr.pdt_template
    2560          882 :            || gfc_current_block ()->attr.pdt_type)
    2561          882 :       && c->ts.u.derived->attr.pdt_template)
    2562              :     {
    2563           12 :       match m = gfc_get_pdt_instance (decl_type_param_list, &c->ts.u.derived, NULL);
    2564           12 :       if (m != MATCH_YES)
    2565              :         {
    2566            0 :           if (!gfc_error_check ())
    2567            0 :             gfc_error ("Parameterized component of a non-parameterized "
    2568              :                        "derived type at %C could not be converted to a valid "
    2569              :                        "instance");
    2570              :           return false;
    2571              :         }
    2572              :     }
    2573              : 
    2574              :   /* Check array components.  */
    2575        18934 :   if (!c->attr.dimension)
    2576        13938 :     goto scalar;
    2577              : 
    2578         4996 :   if (c->attr.pointer)
    2579              :     {
    2580          732 :       if (c->as->type != AS_DEFERRED)
    2581              :         {
    2582            5 :           gfc_error ("Pointer array component of structure at %C must have a "
    2583              :                      "deferred shape");
    2584            5 :           return false;
    2585              :         }
    2586              :     }
    2587         4264 :   else if (c->attr.allocatable)
    2588              :     {
    2589         2501 :       const char *err = G_("Allocatable component of structure at %C must have "
    2590              :                            "a deferred shape");
    2591         2501 :       if (c->as->type != AS_DEFERRED)
    2592              :         {
    2593           14 :           if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
    2594              :             {
    2595              :               /* Issue an immediate error and allow this component to pass for
    2596              :                  the sake of clean error recovery.  Set the error flag for the
    2597              :                  containing derived type so that finalizers are not built.  */
    2598            4 :               gfc_error_now (err);
    2599            4 :               s->sym->error = 1;
    2600            4 :               c->as->type = AS_DEFERRED;
    2601              :             }
    2602              :           else
    2603              :             {
    2604           10 :               gfc_error (err);
    2605           10 :               return false;
    2606              :             }
    2607              :         }
    2608              :     }
    2609              :   else
    2610              :     {
    2611         1763 :       if (c->as->type != AS_EXPLICIT)
    2612              :         {
    2613            7 :           gfc_error ("Array component of structure at %C must have an "
    2614              :                      "explicit shape");
    2615            7 :           return false;
    2616              :         }
    2617              :     }
    2618              : 
    2619         1756 : scalar:
    2620        18912 :   if (c->ts.type == BT_CLASS)
    2621          879 :     return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
    2622              : 
    2623        18033 :   if (c->attr.pdt_kind || c->attr.pdt_len)
    2624              :     {
    2625          700 :       gfc_symbol *sym;
    2626          700 :       gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
    2627              :                        0, &sym);
    2628          700 :       if (sym == NULL)
    2629              :         {
    2630            0 :           gfc_error ("Type parameter %qs at %C has no corresponding entry "
    2631              :                      "in the type parameter name list at %L",
    2632            0 :                      c->name, &gfc_current_block ()->declared_at);
    2633            0 :           return false;
    2634              :         }
    2635          700 :       sym->ts = c->ts;
    2636          700 :       sym->attr.pdt_kind = c->attr.pdt_kind;
    2637          700 :       sym->attr.pdt_len = c->attr.pdt_len;
    2638          700 :       if (c->initializer)
    2639          264 :         sym->value = gfc_copy_expr (c->initializer);
    2640          700 :       sym->attr.flavor = FL_VARIABLE;
    2641              :     }
    2642              : 
    2643        18033 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    2644         2669 :       && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
    2645          130 :       && decl_type_param_list)
    2646          130 :     c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
    2647              : 
    2648              :   return true;
    2649              : }
    2650              : 
    2651              : 
    2652              : /* Match a 'NULL()', and possibly take care of some side effects.  */
    2653              : 
    2654              : match
    2655         1752 : gfc_match_null (gfc_expr **result)
    2656              : {
    2657         1752 :   gfc_symbol *sym;
    2658         1752 :   match m, m2 = MATCH_NO;
    2659              : 
    2660         1752 :   if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
    2661              :     return MATCH_ERROR;
    2662              : 
    2663         1752 :   if (m == MATCH_NO)
    2664              :     {
    2665          511 :       locus old_loc;
    2666          511 :       char name[GFC_MAX_SYMBOL_LEN + 1];
    2667              : 
    2668          511 :       if ((m2 = gfc_match (" null (")) != MATCH_YES)
    2669          505 :         return m2;
    2670              : 
    2671            6 :       old_loc = gfc_current_locus;
    2672            6 :       if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
    2673              :         return MATCH_ERROR;
    2674            6 :       if (m2 != MATCH_YES
    2675            6 :           && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
    2676              :         return MATCH_ERROR;
    2677            6 :       if (m2 == MATCH_NO)
    2678              :         {
    2679            0 :           gfc_current_locus = old_loc;
    2680            0 :           return MATCH_NO;
    2681              :         }
    2682              :     }
    2683              : 
    2684              :   /* The NULL symbol now has to be/become an intrinsic function.  */
    2685         1247 :   if (gfc_get_symbol ("null", NULL, &sym))
    2686              :     {
    2687            0 :       gfc_error ("NULL() initialization at %C is ambiguous");
    2688            0 :       return MATCH_ERROR;
    2689              :     }
    2690              : 
    2691         1247 :   gfc_intrinsic_symbol (sym);
    2692              : 
    2693         1247 :   if (sym->attr.proc != PROC_INTRINSIC
    2694          877 :       && !(sym->attr.use_assoc && sym->attr.intrinsic)
    2695         2123 :       && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
    2696          876 :           || !gfc_add_function (&sym->attr, sym->name, NULL)))
    2697              :     return MATCH_ERROR;
    2698              : 
    2699         1247 :   *result = gfc_get_null_expr (&gfc_current_locus);
    2700              : 
    2701              :   /* Invalid per F2008, C512.  */
    2702         1247 :   if (m2 == MATCH_YES)
    2703              :     {
    2704            6 :       gfc_error ("NULL() initialization at %C may not have MOLD");
    2705            6 :       return MATCH_ERROR;
    2706              :     }
    2707              : 
    2708              :   return MATCH_YES;
    2709              : }
    2710              : 
    2711              : 
    2712              : /* Match the initialization expr for a data pointer or procedure pointer.  */
    2713              : 
    2714              : static match
    2715         1416 : match_pointer_init (gfc_expr **init, int procptr)
    2716              : {
    2717         1416 :   match m;
    2718              : 
    2719         1416 :   if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
    2720              :     {
    2721            1 :       gfc_error ("Initialization of pointer at %C is not allowed in "
    2722              :                  "a PURE procedure");
    2723            1 :       return MATCH_ERROR;
    2724              :     }
    2725         1415 :   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    2726              : 
    2727              :   /* Match NULL() initialization.  */
    2728         1415 :   m = gfc_match_null (init);
    2729         1415 :   if (m != MATCH_NO)
    2730              :     return m;
    2731              : 
    2732              :   /* Match non-NULL initialization.  */
    2733          176 :   gfc_matching_ptr_assignment = !procptr;
    2734          176 :   gfc_matching_procptr_assignment = procptr;
    2735          176 :   m = gfc_match_rvalue (init);
    2736          176 :   gfc_matching_ptr_assignment = 0;
    2737          176 :   gfc_matching_procptr_assignment = 0;
    2738          176 :   if (m == MATCH_ERROR)
    2739              :     return MATCH_ERROR;
    2740          175 :   else if (m == MATCH_NO)
    2741              :     {
    2742            2 :       gfc_error ("Error in pointer initialization at %C");
    2743            2 :       return MATCH_ERROR;
    2744              :     }
    2745              : 
    2746          173 :   if (!procptr && !gfc_resolve_expr (*init))
    2747              :     return MATCH_ERROR;
    2748              : 
    2749          172 :   if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
    2750              :                        "initialization at %C"))
    2751            0 :     return MATCH_ERROR;
    2752              : 
    2753              :   return MATCH_YES;
    2754              : }
    2755              : 
    2756              : 
    2757              : static bool
    2758       295175 : check_function_name (char *name)
    2759              : {
    2760              :   /* In functions that have a RESULT variable defined, the function name always
    2761              :      refers to function calls.  Therefore, the name is not allowed to appear in
    2762              :      specification statements. When checking this, be careful about
    2763              :      'hidden' procedure pointer results ('ppr@').  */
    2764              : 
    2765       295175 :   if (gfc_current_state () == COMP_FUNCTION)
    2766              :     {
    2767        48318 :       gfc_symbol *block = gfc_current_block ();
    2768        48318 :       if (block && block->result && block->result != block
    2769        15677 :           && strcmp (block->result->name, "ppr@") != 0
    2770        15618 :           && strcmp (block->name, name) == 0)
    2771              :         {
    2772            9 :           gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
    2773              :                      "from appearing in a specification statement",
    2774              :                      block->result->name, &block->result->declared_at, name);
    2775            9 :           return false;
    2776              :         }
    2777              :     }
    2778              : 
    2779              :   return true;
    2780              : }
    2781              : 
    2782              : 
    2783              : /* Match a variable name with an optional initializer.  When this
    2784              :    subroutine is called, a variable is expected to be parsed next.
    2785              :    Depending on what is happening at the moment, updates either the
    2786              :    symbol table or the current interface.  */
    2787              : 
    2788              : static match
    2789       284942 : variable_decl (int elem)
    2790              : {
    2791       284942 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    2792       284942 :   static unsigned int fill_id = 0;
    2793       284942 :   gfc_expr *initializer, *char_len;
    2794       284942 :   gfc_array_spec *as;
    2795       284942 :   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
    2796       284942 :   gfc_charlen *cl;
    2797       284942 :   gfc_charlen *saved_cl_list;
    2798       284942 :   bool cl_deferred;
    2799       284942 :   locus var_locus;
    2800       284942 :   match m;
    2801       284942 :   bool t;
    2802       284942 :   gfc_symbol *sym;
    2803       284942 :   char c;
    2804              : 
    2805       284942 :   initializer = NULL;
    2806       284942 :   as = NULL;
    2807       284942 :   cp_as = NULL;
    2808       284942 :   saved_cl_list = gfc_current_ns->cl_list;
    2809              : 
    2810              :   /* When we get here, we've just matched a list of attributes and
    2811              :      maybe a type and a double colon.  The next thing we expect to see
    2812              :      is the name of the symbol.  */
    2813              : 
    2814              :   /* If we are parsing a structure with legacy support, we allow the symbol
    2815              :      name to be '%FILL' which gives it an anonymous (inaccessible) name.  */
    2816       284942 :   m = MATCH_NO;
    2817       284942 :   gfc_gobble_whitespace ();
    2818       284942 :   var_locus = gfc_current_locus;
    2819       284942 :   c = gfc_peek_ascii_char ();
    2820       284942 :   if (c == '%')
    2821              :     {
    2822           12 :       gfc_next_ascii_char ();   /* Burn % character.  */
    2823           12 :       m = gfc_match ("fill");
    2824           12 :       if (m == MATCH_YES)
    2825              :         {
    2826           11 :           if (gfc_current_state () != COMP_STRUCTURE)
    2827              :             {
    2828            2 :               if (flag_dec_structure)
    2829            1 :                 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
    2830              :               else
    2831            1 :                 gfc_error ("%qs at %C is a DEC extension, enable with "
    2832              :                        "%<-fdec-structure%>", "%FILL");
    2833            2 :               m = MATCH_ERROR;
    2834            2 :               goto cleanup;
    2835              :             }
    2836              : 
    2837            9 :           if (attr_seen)
    2838              :             {
    2839            1 :               gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
    2840            1 :               m = MATCH_ERROR;
    2841            1 :               goto cleanup;
    2842              :             }
    2843              : 
    2844              :           /* %FILL components are given invalid fortran names.  */
    2845            8 :           snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
    2846              :         }
    2847              :       else
    2848              :         {
    2849            1 :           gfc_error ("Invalid character %qc in variable name at %C", c);
    2850            1 :           return MATCH_ERROR;
    2851              :         }
    2852              :     }
    2853              :   else
    2854              :     {
    2855       284930 :       m = gfc_match_name (name);
    2856       284929 :       if (m != MATCH_YES)
    2857           10 :         goto cleanup;
    2858              :     }
    2859              : 
    2860              :   /* Now we could see the optional array spec. or character length.  */
    2861       284927 :   m = gfc_match_array_spec (&as, true, true);
    2862       284926 :   if (m == MATCH_ERROR)
    2863           57 :     goto cleanup;
    2864              : 
    2865       284869 :   if (m == MATCH_NO)
    2866       222396 :     as = gfc_copy_array_spec (current_as);
    2867        62473 :   else if (current_as
    2868        62473 :            && !merge_array_spec (current_as, as, true))
    2869              :     {
    2870            4 :       m = MATCH_ERROR;
    2871            4 :       goto cleanup;
    2872              :     }
    2873              : 
    2874       284865 :    var_locus = gfc_get_location_range (NULL, 0, &var_locus, 1,
    2875              :                                        &gfc_current_locus);
    2876       284865 :   if (flag_cray_pointer)
    2877         3063 :     cp_as = gfc_copy_array_spec (as);
    2878              : 
    2879              :   /* At this point, we know for sure if the symbol is PARAMETER and can thus
    2880              :      determine (and check) whether it can be implied-shape.  If it
    2881              :      was parsed as assumed-size, change it because PARAMETERs cannot
    2882              :      be assumed-size.
    2883              : 
    2884              :      An explicit-shape-array cannot appear under several conditions.
    2885              :      That check is done here as well.  */
    2886       284865 :   if (as)
    2887              :     {
    2888        85178 :       if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
    2889              :         {
    2890            2 :           m = MATCH_ERROR;
    2891            2 :           gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
    2892              :                      name, &var_locus);
    2893            2 :           goto cleanup;
    2894              :         }
    2895              : 
    2896        85176 :       if (as->type == AS_ASSUMED_SIZE && as->rank == 1
    2897         6516 :           && current_attr.flavor == FL_PARAMETER)
    2898          993 :         as->type = AS_IMPLIED_SHAPE;
    2899              : 
    2900        85176 :       if (as->type == AS_IMPLIED_SHAPE
    2901        85176 :           && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
    2902              :                               &var_locus))
    2903              :         {
    2904            1 :           m = MATCH_ERROR;
    2905            1 :           goto cleanup;
    2906              :         }
    2907              : 
    2908        85175 :       gfc_seen_div0 = false;
    2909              : 
    2910              :       /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
    2911              :          constant expressions shall appear only in a subprogram, derived
    2912              :          type definition, BLOCK construct, or interface body.  */
    2913        85175 :       if (as->type == AS_EXPLICIT
    2914        42475 :           && gfc_current_state () != COMP_BLOCK
    2915              :           && gfc_current_state () != COMP_DERIVED
    2916              :           && gfc_current_state () != COMP_FUNCTION
    2917              :           && gfc_current_state () != COMP_INTERFACE
    2918              :           && gfc_current_state () != COMP_SUBROUTINE)
    2919              :         {
    2920              :           gfc_expr *e;
    2921        50361 :           bool not_constant = false;
    2922              : 
    2923        50361 :           for (int i = 0; i < as->rank; i++)
    2924              :             {
    2925        28644 :               e = gfc_copy_expr (as->lower[i]);
    2926        28644 :               if (!gfc_resolve_expr (e) && gfc_seen_div0)
    2927              :                 {
    2928            0 :                   m = MATCH_ERROR;
    2929            0 :                   goto cleanup;
    2930              :                 }
    2931              : 
    2932        28644 :               gfc_simplify_expr (e, 0);
    2933        28644 :               if (e && (e->expr_type != EXPR_CONSTANT))
    2934              :                 {
    2935              :                   not_constant = true;
    2936              :                   break;
    2937              :                 }
    2938        28644 :               gfc_free_expr (e);
    2939              : 
    2940        28644 :               e = gfc_copy_expr (as->upper[i]);
    2941        28644 :               if (!gfc_resolve_expr (e)  && gfc_seen_div0)
    2942              :                 {
    2943            4 :                   m = MATCH_ERROR;
    2944            4 :                   goto cleanup;
    2945              :                 }
    2946              : 
    2947        28640 :               gfc_simplify_expr (e, 0);
    2948        28640 :               if (e && (e->expr_type != EXPR_CONSTANT))
    2949              :                 {
    2950              :                   not_constant = true;
    2951              :                   break;
    2952              :                 }
    2953        28627 :               gfc_free_expr (e);
    2954              :             }
    2955              : 
    2956        21730 :           if (not_constant && e->ts.type != BT_INTEGER)
    2957              :             {
    2958            4 :               gfc_error ("Explicit array shape at %C must be constant of "
    2959              :                          "INTEGER type and not %s type",
    2960              :                          gfc_basic_typename (e->ts.type));
    2961            4 :               m = MATCH_ERROR;
    2962            4 :               goto cleanup;
    2963              :             }
    2964            9 :           if (not_constant)
    2965              :             {
    2966            9 :               gfc_error ("Explicit shaped array with nonconstant bounds at %C");
    2967            9 :               m = MATCH_ERROR;
    2968            9 :               goto cleanup;
    2969              :             }
    2970              :         }
    2971        85158 :       if (as->type == AS_EXPLICIT)
    2972              :         {
    2973       101448 :           for (int i = 0; i < as->rank; i++)
    2974              :             {
    2975        58990 :               gfc_expr *e, *n;
    2976        58990 :               e = as->lower[i];
    2977        58990 :               if (e->expr_type != EXPR_CONSTANT)
    2978              :                 {
    2979          452 :                   n = gfc_copy_expr (e);
    2980          452 :                   if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
    2981              :                     {
    2982            0 :                       m = MATCH_ERROR;
    2983            0 :                       goto cleanup;
    2984              :                     }
    2985              : 
    2986          452 :                   if (n->expr_type == EXPR_CONSTANT)
    2987           22 :                     gfc_replace_expr (e, n);
    2988              :                   else
    2989          430 :                     gfc_free_expr (n);
    2990              :                 }
    2991        58990 :               e = as->upper[i];
    2992        58990 :               if (e->expr_type != EXPR_CONSTANT)
    2993              :                 {
    2994         6843 :                   n = gfc_copy_expr (e);
    2995         6843 :                   if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
    2996              :                     {
    2997            0 :                       m = MATCH_ERROR;
    2998            0 :                       goto cleanup;
    2999              :                     }
    3000              : 
    3001         6843 :                   if (n->expr_type == EXPR_CONSTANT)
    3002           45 :                     gfc_replace_expr (e, n);
    3003              :                   else
    3004         6798 :                     gfc_free_expr (n);
    3005              :                 }
    3006              :               /* For an explicit-shape spec with constant bounds, ensure
    3007              :                  that the effective upper bound is not lower than the
    3008              :                  respective lower bound minus one.  Otherwise adjust it so
    3009              :                  that the extent is trivially derived to be zero.  */
    3010        58990 :               if (as->lower[i]->expr_type == EXPR_CONSTANT
    3011        58560 :                   && as->upper[i]->expr_type == EXPR_CONSTANT
    3012        52186 :                   && as->lower[i]->ts.type == BT_INTEGER
    3013        52186 :                   && as->upper[i]->ts.type == BT_INTEGER
    3014        52181 :                   && mpz_cmp (as->upper[i]->value.integer,
    3015        52181 :                               as->lower[i]->value.integer) < 0)
    3016         1218 :                 mpz_sub_ui (as->upper[i]->value.integer,
    3017              :                             as->lower[i]->value.integer, 1);
    3018              :             }
    3019              :         }
    3020              :     }
    3021              : 
    3022       284845 :   char_len = NULL;
    3023       284845 :   cl = NULL;
    3024       284845 :   cl_deferred = false;
    3025              : 
    3026       284845 :   if (current_ts.type == BT_CHARACTER)
    3027              :     {
    3028        31440 :       switch (match_char_length (&char_len, &cl_deferred, false))
    3029              :         {
    3030          435 :         case MATCH_YES:
    3031          435 :           cl = gfc_new_charlen (gfc_current_ns, NULL);
    3032              : 
    3033          435 :           cl->length = char_len;
    3034          435 :           break;
    3035              : 
    3036              :         /* Non-constant lengths need to be copied after the first
    3037              :            element.  Also copy assumed lengths.  */
    3038        31004 :         case MATCH_NO:
    3039        31004 :           if (elem > 1
    3040         3935 :               && (current_ts.u.cl->length == NULL
    3041         2709 :                   || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
    3042              :             {
    3043         1281 :               cl = gfc_new_charlen (gfc_current_ns, NULL);
    3044         1281 :               cl->length = gfc_copy_expr (current_ts.u.cl->length);
    3045              :             }
    3046              :           else
    3047        29723 :             cl = current_ts.u.cl;
    3048              : 
    3049        31004 :           cl_deferred = current_ts.deferred;
    3050              : 
    3051        31004 :           break;
    3052              : 
    3053            1 :         case MATCH_ERROR:
    3054            1 :           goto cleanup;
    3055              :         }
    3056              :     }
    3057              : 
    3058              :   /* The dummy arguments and result of the abbreviated form of MODULE
    3059              :      PROCEDUREs, used in SUBMODULES should not be redefined.  */
    3060       284844 :   if (gfc_current_ns->proc_name
    3061       280354 :       && gfc_current_ns->proc_name->abr_modproc_decl)
    3062              :     {
    3063           44 :       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
    3064           44 :       if (sym != NULL && (sym->attr.dummy || sym->attr.result))
    3065              :         {
    3066            2 :           m = MATCH_ERROR;
    3067            2 :           gfc_error ("%qs at %L is a redefinition of the declaration "
    3068              :                      "in the corresponding interface for MODULE "
    3069              :                      "PROCEDURE %qs", sym->name, &var_locus,
    3070            2 :                      gfc_current_ns->proc_name->name);
    3071            2 :           goto cleanup;
    3072              :         }
    3073              :     }
    3074              : 
    3075              :   /* %FILL components may not have initializers.  */
    3076       284842 :   if (startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
    3077              :     {
    3078            1 :       gfc_error ("%qs entity cannot have an initializer at %L", "%FILL",
    3079              :                  &var_locus);
    3080            1 :       m = MATCH_ERROR;
    3081            1 :       goto cleanup;
    3082              :     }
    3083              : 
    3084              :   /*  If this symbol has already shown up in a Cray Pointer declaration,
    3085              :       and this is not a component declaration,
    3086              :       then we want to set the type & bail out.  */
    3087       284841 :   if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
    3088              :     {
    3089         2959 :       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
    3090         2959 :       if (sym != NULL && sym->attr.cray_pointee)
    3091              :         {
    3092          101 :           m = MATCH_YES;
    3093          101 :           if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
    3094              :             {
    3095            1 :               m = MATCH_ERROR;
    3096            1 :               goto cleanup;
    3097              :             }
    3098              : 
    3099              :           /* Check to see if we have an array specification.  */
    3100          100 :           if (cp_as != NULL)
    3101              :             {
    3102           49 :               if (sym->as != NULL)
    3103              :                 {
    3104            1 :                   gfc_error ("Duplicate array spec for Cray pointee at %L", &var_locus);
    3105            1 :                   gfc_free_array_spec (cp_as);
    3106            1 :                   m = MATCH_ERROR;
    3107            1 :                   goto cleanup;
    3108              :                 }
    3109              :               else
    3110              :                 {
    3111           48 :                   if (!gfc_set_array_spec (sym, cp_as, &var_locus))
    3112            0 :                     gfc_internal_error ("Cannot set pointee array spec.");
    3113              : 
    3114              :                   /* Fix the array spec.  */
    3115           48 :                   m = gfc_mod_pointee_as (sym->as);
    3116           48 :                   if (m == MATCH_ERROR)
    3117            0 :                     goto cleanup;
    3118              :                 }
    3119              :             }
    3120           99 :           goto cleanup;
    3121              :         }
    3122              :       else
    3123              :         {
    3124         2858 :           gfc_free_array_spec (cp_as);
    3125              :         }
    3126              :     }
    3127              :   else
    3128              :     {
    3129              :       /* Check to see if this is the declaration of the type and/or attributes
    3130              :          of an implicit function result, emanating from a module function
    3131              :          interface declared within the parent module or submodule of a
    3132              :          containing submodule.  */
    3133       281882 :       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
    3134       281882 :       if (gfc_current_state () == COMP_FUNCTION
    3135        46836 :           && sym == gfc_current_block ()
    3136         8278 :           && sym->attr.if_source == IFSRC_DECL
    3137         4952 :           && sym->attr.used_in_submodule
    3138            4 :           && sym == sym->result
    3139            4 :           && sym->ts.type != BT_UNKNOWN)
    3140              :         {
    3141            4 :           m = MATCH_YES;
    3142            4 :           goto cleanup;
    3143              :         }
    3144       281878 :       sym = NULL;
    3145              :     }
    3146              : 
    3147              :   /* Procedure pointer as function result.  */
    3148       284736 :   if (gfc_current_state () == COMP_FUNCTION
    3149        46946 :       && strcmp ("ppr@", gfc_current_block ()->name) == 0
    3150           25 :       && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
    3151            7 :     strcpy (name, "ppr@");
    3152              : 
    3153       284736 :   if (gfc_current_state () == COMP_FUNCTION
    3154        46946 :       && strcmp (name, gfc_current_block ()->name) == 0
    3155         8294 :       && gfc_current_block ()->result
    3156         8294 :       && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
    3157           16 :     strcpy (name, "ppr@");
    3158              : 
    3159              :   /* OK, we've successfully matched the declaration.  Now put the
    3160              :      symbol in the current namespace, because it might be used in the
    3161              :      optional initialization expression for this symbol, e.g. this is
    3162              :      perfectly legal:
    3163              : 
    3164              :      integer, parameter :: i = huge(i)
    3165              : 
    3166              :      This is only true for parameters or variables of a basic type.
    3167              :      For components of derived types, it is not true, so we don't
    3168              :      create a symbol for those yet.  If we fail to create the symbol,
    3169              :      bail out.  */
    3170       284736 :   if (!gfc_comp_struct (gfc_current_state ())
    3171       265761 :       && !build_sym (name, elem, cl, cl_deferred, &as, &var_locus))
    3172              :     {
    3173           46 :       m = MATCH_ERROR;
    3174           46 :       goto cleanup;
    3175              :     }
    3176              : 
    3177       284690 :   if (!check_function_name (name))
    3178              :     {
    3179            0 :       m = MATCH_ERROR;
    3180            0 :       goto cleanup;
    3181              :     }
    3182              : 
    3183              :   /* We allow old-style initializations of the form
    3184              :        integer i /2/, j(4) /3*3, 1/
    3185              :      (if no colon has been seen). These are different from data
    3186              :      statements in that initializers are only allowed to apply to the
    3187              :      variable immediately preceding, i.e.
    3188              :        integer i, j /1, 2/
    3189              :      is not allowed. Therefore we have to do some work manually, that
    3190              :      could otherwise be left to the matchers for DATA statements.  */
    3191              : 
    3192       284690 :   if (!colon_seen && gfc_match (" /") == MATCH_YES)
    3193              :     {
    3194          146 :       if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
    3195              :                            "initialization at %C"))
    3196              :         return MATCH_ERROR;
    3197              : 
    3198              :       /* Allow old style initializations for components of STRUCTUREs and MAPs
    3199              :          but not components of derived types.  */
    3200          146 :       else if (gfc_current_state () == COMP_DERIVED)
    3201              :         {
    3202            2 :           gfc_error ("Invalid old style initialization for derived type "
    3203              :                      "component at %C");
    3204            2 :           m = MATCH_ERROR;
    3205            2 :           goto cleanup;
    3206              :         }
    3207              : 
    3208              :       /* For structure components, read the initializer as a special
    3209              :          expression and let the rest of this function apply the initializer
    3210              :          as usual.  */
    3211          144 :       else if (gfc_comp_struct (gfc_current_state ()))
    3212              :         {
    3213           74 :           m = match_clist_expr (&initializer, &current_ts, as);
    3214           74 :           if (m == MATCH_NO)
    3215              :             gfc_error ("Syntax error in old style initialization of %s at %C",
    3216              :                        name);
    3217           74 :           if (m != MATCH_YES)
    3218           14 :             goto cleanup;
    3219              :         }
    3220              : 
    3221              :       /* Otherwise we treat the old style initialization just like a
    3222              :          DATA declaration for the current variable.  */
    3223              :       else
    3224           70 :         return match_old_style_init (name);
    3225              :     }
    3226              : 
    3227              :   /* The double colon must be present in order to have initializers.
    3228              :      Otherwise the statement is ambiguous with an assignment statement.  */
    3229       284604 :   if (colon_seen)
    3230              :     {
    3231       238349 :       if (gfc_match (" =>") == MATCH_YES)
    3232              :         {
    3233         1227 :           if (!current_attr.pointer)
    3234              :             {
    3235            0 :               gfc_error ("Initialization at %C isn't for a pointer variable");
    3236            0 :               m = MATCH_ERROR;
    3237            0 :               goto cleanup;
    3238              :             }
    3239              : 
    3240         1227 :           m = match_pointer_init (&initializer, 0);
    3241         1227 :           if (m != MATCH_YES)
    3242           10 :             goto cleanup;
    3243              : 
    3244              :           /* The target of a pointer initialization must have the SAVE
    3245              :              attribute.  A variable in PROGRAM, MODULE, or SUBMODULE scope
    3246              :              is implicit SAVEd.  Explicitly, set the SAVE_IMPLICIT value.  */
    3247         1217 :           if (initializer->expr_type == EXPR_VARIABLE
    3248          128 :               && initializer->symtree->n.sym->attr.save == SAVE_NONE
    3249           25 :               && (gfc_current_state () == COMP_PROGRAM
    3250              :                   || gfc_current_state () == COMP_MODULE
    3251           25 :                   || gfc_current_state () == COMP_SUBMODULE))
    3252           11 :             initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
    3253              :         }
    3254       237122 :       else if (gfc_match_char ('=') == MATCH_YES)
    3255              :         {
    3256        26590 :           if (current_attr.pointer)
    3257              :             {
    3258            0 :               gfc_error ("Pointer initialization at %C requires %<=>%>, "
    3259              :                          "not %<=%>");
    3260            0 :               m = MATCH_ERROR;
    3261            0 :               goto cleanup;
    3262              :             }
    3263              : 
    3264        26590 :           if (gfc_comp_struct (gfc_current_state ())
    3265         2557 :               && gfc_current_block ()->attr.pdt_template)
    3266              :             {
    3267          293 :               m = gfc_match_expr (&initializer);
    3268          293 :               if (initializer && initializer->ts.type == BT_UNKNOWN)
    3269          127 :                 initializer->ts = current_ts;
    3270              :             }
    3271              :           else
    3272        26297 :             m = gfc_match_init_expr (&initializer);
    3273              : 
    3274        26590 :           if (m == MATCH_NO)
    3275              :             {
    3276            1 :               gfc_error ("Expected an initialization expression at %C");
    3277            1 :               m = MATCH_ERROR;
    3278              :             }
    3279              : 
    3280        10402 :           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
    3281        26592 :               && !gfc_comp_struct (gfc_state_stack->state))
    3282              :             {
    3283            1 :               gfc_error ("Initialization of variable at %C is not allowed in "
    3284              :                          "a PURE procedure");
    3285            1 :               m = MATCH_ERROR;
    3286              :             }
    3287              : 
    3288        26590 :           if (current_attr.flavor != FL_PARAMETER
    3289        10402 :               && !gfc_comp_struct (gfc_state_stack->state))
    3290         7845 :             gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    3291              : 
    3292        26590 :           if (m != MATCH_YES)
    3293          160 :             goto cleanup;
    3294              :         }
    3295              :     }
    3296              : 
    3297       284434 :   if (initializer != NULL && current_attr.allocatable
    3298            3 :         && gfc_comp_struct (gfc_current_state ()))
    3299              :     {
    3300            2 :       gfc_error ("Initialization of allocatable component at %C is not "
    3301              :                  "allowed");
    3302            2 :       m = MATCH_ERROR;
    3303            2 :       goto cleanup;
    3304              :     }
    3305              : 
    3306       284432 :   if (gfc_current_state () == COMP_DERIVED
    3307        17933 :       && initializer && initializer->ts.type == BT_HOLLERITH)
    3308              :     {
    3309            1 :       gfc_error ("Initialization of structure component with a HOLLERITH "
    3310              :                  "constant at %L is not allowed", &initializer->where);
    3311            1 :       m = MATCH_ERROR;
    3312            1 :       goto cleanup;
    3313              :     }
    3314              : 
    3315       284431 :   if (gfc_current_state () == COMP_DERIVED
    3316        17932 :       && gfc_current_block ()->attr.pdt_template)
    3317              :     {
    3318         1434 :       gfc_symbol *param;
    3319         1434 :       gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
    3320              :                        0, &param);
    3321         1434 :       if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
    3322              :         {
    3323            1 :           gfc_error ("The component with KIND or LEN attribute at %C does not "
    3324              :                      "not appear in the type parameter list at %L",
    3325            1 :                      &gfc_current_block ()->declared_at);
    3326            1 :           m = MATCH_ERROR;
    3327            4 :           goto cleanup;
    3328              :         }
    3329         1433 :       else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
    3330              :         {
    3331            1 :           gfc_error ("The component at %C that appears in the type parameter "
    3332              :                      "list at %L has neither the KIND nor LEN attribute",
    3333            1 :                      &gfc_current_block ()->declared_at);
    3334            1 :           m = MATCH_ERROR;
    3335            1 :           goto cleanup;
    3336              :         }
    3337         1432 :       else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
    3338              :         {
    3339            1 :           gfc_error ("The component at %C which is a type parameter must be "
    3340              :                      "a scalar");
    3341            1 :           m = MATCH_ERROR;
    3342            1 :           goto cleanup;
    3343              :         }
    3344         1431 :       else if (param && initializer)
    3345              :         {
    3346          265 :           if (initializer->ts.type == BT_BOZ)
    3347              :             {
    3348            1 :               gfc_error ("BOZ literal constant at %L cannot appear as an "
    3349              :                          "initializer", &initializer->where);
    3350            1 :               m = MATCH_ERROR;
    3351            1 :               goto cleanup;
    3352              :             }
    3353          264 :           param->value = gfc_copy_expr (initializer);
    3354              :         }
    3355              :     }
    3356              : 
    3357              :   /* Before adding a possible initializer, do a simple check for compatibility
    3358              :      of lhs and rhs types.  Assigning a REAL value to a derived type is not a
    3359              :      good thing.  */
    3360        29117 :   if (current_ts.type == BT_DERIVED && initializer
    3361       285884 :       && (gfc_numeric_ts (&initializer->ts)
    3362         1455 :           || initializer->ts.type == BT_LOGICAL
    3363         1455 :           || initializer->ts.type == BT_CHARACTER))
    3364              :     {
    3365            2 :       gfc_error ("Incompatible initialization between a derived type "
    3366              :                  "entity and an entity with %qs type at %C",
    3367              :                   gfc_typename (initializer));
    3368            2 :       m = MATCH_ERROR;
    3369            2 :       goto cleanup;
    3370              :     }
    3371              : 
    3372              : 
    3373              :   /* Add the initializer.  Note that it is fine if initializer is
    3374              :      NULL here, because we sometimes also need to check if a
    3375              :      declaration *must* have an initialization expression.  */
    3376       284425 :   if (!gfc_comp_struct (gfc_current_state ()))
    3377       265479 :     t = add_init_expr_to_sym (name, &initializer, &var_locus,
    3378              :                               saved_cl_list);
    3379              :   else
    3380              :     {
    3381        18946 :       if (current_ts.type == BT_DERIVED
    3382         2669 :           && !current_attr.pointer && !initializer)
    3383         2110 :         initializer = gfc_default_initializer (&current_ts);
    3384        18946 :       t = build_struct (name, cl, &initializer, &as);
    3385              : 
    3386              :       /* If we match a nested structure definition we expect to see the
    3387              :        * body even if the variable declarations blow up, so we need to keep
    3388              :        * the structure declaration around.  */
    3389        18946 :       if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
    3390           34 :         gfc_commit_symbol (gfc_new_block);
    3391              :     }
    3392              : 
    3393       284425 :   m = (t) ? MATCH_YES : MATCH_ERROR;
    3394              : 
    3395       284869 : cleanup:
    3396              :   /* Free stuff up and return.  */
    3397       284869 :   gfc_seen_div0 = false;
    3398       284869 :   gfc_free_expr (initializer);
    3399       284869 :   gfc_free_array_spec (as);
    3400              : 
    3401       284869 :   return m;
    3402              : }
    3403              : 
    3404              : 
    3405              : /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
    3406              :    This assumes that the byte size is equal to the kind number for
    3407              :    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
    3408              : 
    3409              : static match
    3410       109246 : gfc_match_old_kind_spec (gfc_typespec *ts)
    3411              : {
    3412       109246 :   match m;
    3413       109246 :   int original_kind;
    3414              : 
    3415       109246 :   if (gfc_match_char ('*') != MATCH_YES)
    3416              :     return MATCH_NO;
    3417              : 
    3418         1150 :   m = gfc_match_small_literal_int (&ts->kind, NULL);
    3419         1150 :   if (m != MATCH_YES)
    3420              :     return MATCH_ERROR;
    3421              : 
    3422         1150 :   original_kind = ts->kind;
    3423              : 
    3424              :   /* Massage the kind numbers for complex types.  */
    3425         1150 :   if (ts->type == BT_COMPLEX)
    3426              :     {
    3427           79 :       if (ts->kind % 2)
    3428              :         {
    3429            0 :           gfc_error ("Old-style type declaration %s*%d not supported at %C",
    3430              :                      gfc_basic_typename (ts->type), original_kind);
    3431            0 :           return MATCH_ERROR;
    3432              :         }
    3433           79 :       ts->kind /= 2;
    3434              : 
    3435              :     }
    3436              : 
    3437         1150 :   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
    3438            0 :     ts->kind = 8;
    3439              : 
    3440         1150 :   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
    3441              :     {
    3442          858 :       if (ts->kind == 4)
    3443              :         {
    3444          224 :           if (flag_real4_kind == 8)
    3445           24 :             ts->kind =  8;
    3446          224 :           if (flag_real4_kind == 10)
    3447           24 :             ts->kind = 10;
    3448          224 :           if (flag_real4_kind == 16)
    3449           24 :             ts->kind = 16;
    3450              :         }
    3451          634 :       else if (ts->kind == 8)
    3452              :         {
    3453          629 :           if (flag_real8_kind == 4)
    3454           24 :             ts->kind = 4;
    3455          629 :           if (flag_real8_kind == 10)
    3456           24 :             ts->kind = 10;
    3457          629 :           if (flag_real8_kind == 16)
    3458           24 :             ts->kind = 16;
    3459              :         }
    3460              :     }
    3461              : 
    3462         1150 :   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
    3463              :     {
    3464            8 :       gfc_error ("Old-style type declaration %s*%d not supported at %C",
    3465              :                  gfc_basic_typename (ts->type), original_kind);
    3466            8 :       return MATCH_ERROR;
    3467              :     }
    3468              : 
    3469         1142 :   if (!gfc_notify_std (GFC_STD_GNU,
    3470              :                        "Nonstandard type declaration %s*%d at %C",
    3471              :                        gfc_basic_typename(ts->type), original_kind))
    3472            0 :     return MATCH_ERROR;
    3473              : 
    3474              :   return MATCH_YES;
    3475              : }
    3476              : 
    3477              : 
    3478              : /* Match a kind specification.  Since kinds are generally optional, we
    3479              :    usually return MATCH_NO if something goes wrong.  If a "kind="
    3480              :    string is found, then we know we have an error.  */
    3481              : 
    3482              : match
    3483       162673 : gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
    3484              : {
    3485       162673 :   locus where, loc;
    3486       162673 :   gfc_expr *e;
    3487       162673 :   match m, n;
    3488       162673 :   char c;
    3489              : 
    3490       162673 :   m = MATCH_NO;
    3491       162673 :   n = MATCH_YES;
    3492       162673 :   e = NULL;
    3493       162673 :   saved_kind_expr = NULL;
    3494              : 
    3495       162673 :   where = loc = gfc_current_locus;
    3496              : 
    3497       162673 :   if (kind_expr_only)
    3498            0 :     goto kind_expr;
    3499              : 
    3500       162673 :   if (gfc_match_char ('(') == MATCH_NO)
    3501              :     return MATCH_NO;
    3502              : 
    3503              :   /* Also gobbles optional text.  */
    3504        51871 :   if (gfc_match (" kind = ") == MATCH_YES)
    3505        51871 :     m = MATCH_ERROR;
    3506              : 
    3507        51871 :   loc = gfc_current_locus;
    3508              : 
    3509        51871 : kind_expr:
    3510              : 
    3511        51871 :   n = gfc_match_init_expr (&e);
    3512              : 
    3513        51871 :   if (gfc_derived_parameter_expr (e))
    3514              :     {
    3515          256 :       ts->kind = 0;
    3516          256 :       saved_kind_expr = gfc_copy_expr (e);
    3517          256 :       goto close_brackets;
    3518              :     }
    3519              : 
    3520        51615 :   if (n != MATCH_YES)
    3521              :     {
    3522          465 :       if (gfc_matching_function)
    3523              :         {
    3524              :           /* The function kind expression might include use associated or
    3525              :              imported parameters and try again after the specification
    3526              :              expressions.....  */
    3527          437 :           if (gfc_match_char (')') != MATCH_YES)
    3528              :             {
    3529            1 :               gfc_error ("Missing right parenthesis at %C");
    3530            1 :               m = MATCH_ERROR;
    3531            1 :               goto no_match;
    3532              :             }
    3533              : 
    3534          436 :           gfc_free_expr (e);
    3535          436 :           gfc_undo_symbols ();
    3536          436 :           return MATCH_YES;
    3537              :         }
    3538              :       else
    3539              :         {
    3540              :           /* ....or else, the match is real.  */
    3541           28 :           if (n == MATCH_NO)
    3542            0 :             gfc_error ("Expected initialization expression at %C");
    3543              :           if (n != MATCH_YES)
    3544              :             return MATCH_ERROR;
    3545              :         }
    3546              :     }
    3547              : 
    3548        51150 :   if (e->rank != 0)
    3549              :     {
    3550            0 :       gfc_error ("Expected scalar initialization expression at %C");
    3551            0 :       m = MATCH_ERROR;
    3552            0 :       goto no_match;
    3553              :     }
    3554              : 
    3555        51150 :   if (gfc_extract_int (e, &ts->kind, 1))
    3556              :     {
    3557            0 :       m = MATCH_ERROR;
    3558            0 :       goto no_match;
    3559              :     }
    3560              : 
    3561              :   /* Before throwing away the expression, let's see if we had a
    3562              :      C interoperable kind (and store the fact).  */
    3563        51150 :   if (e->ts.is_c_interop == 1)
    3564              :     {
    3565              :       /* Mark this as C interoperable if being declared with one
    3566              :          of the named constants from iso_c_binding.  */
    3567        18874 :       ts->is_c_interop = e->ts.is_iso_c;
    3568        18874 :       ts->f90_type = e->ts.f90_type;
    3569        18874 :       if (e->symtree)
    3570        18873 :         ts->interop_kind = e->symtree->n.sym;
    3571              :     }
    3572              : 
    3573        51150 :   gfc_free_expr (e);
    3574        51150 :   e = NULL;
    3575              : 
    3576              :   /* Ignore errors to this point, if we've gotten here.  This means
    3577              :      we ignore the m=MATCH_ERROR from above.  */
    3578        51150 :   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
    3579              :     {
    3580            7 :       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
    3581              :                  gfc_basic_typename (ts->type));
    3582            7 :       gfc_current_locus = where;
    3583            7 :       return MATCH_ERROR;
    3584              :     }
    3585              : 
    3586              :   /* Warn if, e.g., c_int is used for a REAL variable, but not
    3587              :      if, e.g., c_double is used for COMPLEX as the standard
    3588              :      explicitly says that the kind type parameter for complex and real
    3589              :      variable is the same, i.e. c_float == c_float_complex.  */
    3590        51143 :   if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
    3591           17 :       && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
    3592            1 :            || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
    3593           13 :     gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
    3594              :                      "is %s", gfc_basic_typename (ts->f90_type), &where,
    3595              :                      gfc_basic_typename (ts->type));
    3596              : 
    3597        51130 : close_brackets:
    3598              : 
    3599        51399 :   gfc_gobble_whitespace ();
    3600        51399 :   if ((c = gfc_next_ascii_char ()) != ')'
    3601        51399 :       && (ts->type != BT_CHARACTER || c != ','))
    3602              :     {
    3603            0 :       if (ts->type == BT_CHARACTER)
    3604            0 :         gfc_error ("Missing right parenthesis or comma at %C");
    3605              :       else
    3606            0 :         gfc_error ("Missing right parenthesis at %C");
    3607            0 :       m = MATCH_ERROR;
    3608            0 :       goto no_match;
    3609              :     }
    3610              :   else
    3611              :      /* All tests passed.  */
    3612        51399 :      m = MATCH_YES;
    3613              : 
    3614        51399 :   if(m == MATCH_ERROR)
    3615              :      gfc_current_locus = where;
    3616              : 
    3617        51399 :   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
    3618            0 :     ts->kind =  8;
    3619              : 
    3620        51399 :   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
    3621              :     {
    3622        14539 :       if (ts->kind == 4)
    3623              :         {
    3624         4617 :           if (flag_real4_kind == 8)
    3625           54 :             ts->kind =  8;
    3626         4617 :           if (flag_real4_kind == 10)
    3627           54 :             ts->kind = 10;
    3628         4617 :           if (flag_real4_kind == 16)
    3629           54 :             ts->kind = 16;
    3630              :         }
    3631         9922 :       else if (ts->kind == 8)
    3632              :         {
    3633         6730 :           if (flag_real8_kind == 4)
    3634           48 :             ts->kind = 4;
    3635         6730 :           if (flag_real8_kind == 10)
    3636           48 :             ts->kind = 10;
    3637         6730 :           if (flag_real8_kind == 16)
    3638           48 :             ts->kind = 16;
    3639              :         }
    3640              :     }
    3641              : 
    3642              :   /* Return what we know from the test(s).  */
    3643              :   return m;
    3644              : 
    3645            1 : no_match:
    3646            1 :   gfc_free_expr (e);
    3647            1 :   gfc_current_locus = where;
    3648            1 :   return m;
    3649              : }
    3650              : 
    3651              : 
    3652              : static match
    3653         5014 : match_char_kind (int * kind, int * is_iso_c)
    3654              : {
    3655         5014 :   locus where;
    3656         5014 :   gfc_expr *e;
    3657         5014 :   match m, n;
    3658         5014 :   bool fail;
    3659              : 
    3660         5014 :   m = MATCH_NO;
    3661         5014 :   e = NULL;
    3662         5014 :   where = gfc_current_locus;
    3663              : 
    3664         5014 :   n = gfc_match_init_expr (&e);
    3665              : 
    3666         5014 :   if (n != MATCH_YES && gfc_matching_function)
    3667              :     {
    3668              :       /* The expression might include use-associated or imported
    3669              :          parameters and try again after the specification
    3670              :          expressions.  */
    3671            7 :       gfc_free_expr (e);
    3672            7 :       gfc_undo_symbols ();
    3673            7 :       return MATCH_YES;
    3674              :     }
    3675              : 
    3676            7 :   if (n == MATCH_NO)
    3677            2 :     gfc_error ("Expected initialization expression at %C");
    3678         5007 :   if (n != MATCH_YES)
    3679              :     return MATCH_ERROR;
    3680              : 
    3681         5000 :   if (e->rank != 0)
    3682              :     {
    3683            0 :       gfc_error ("Expected scalar initialization expression at %C");
    3684            0 :       m = MATCH_ERROR;
    3685            0 :       goto no_match;
    3686              :     }
    3687              : 
    3688         5000 :   if (gfc_derived_parameter_expr (e))
    3689              :     {
    3690           80 :       saved_kind_expr = e;
    3691           80 :       *kind = 0;
    3692           80 :       return MATCH_YES;
    3693              :     }
    3694              : 
    3695         4920 :   fail = gfc_extract_int (e, kind, 1);
    3696         4920 :   *is_iso_c = e->ts.is_iso_c;
    3697         4920 :   if (fail)
    3698              :     {
    3699            0 :       m = MATCH_ERROR;
    3700            0 :       goto no_match;
    3701              :     }
    3702              : 
    3703         4920 :   gfc_free_expr (e);
    3704              : 
    3705              :   /* Ignore errors to this point, if we've gotten here.  This means
    3706              :      we ignore the m=MATCH_ERROR from above.  */
    3707         4920 :   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
    3708              :     {
    3709           14 :       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
    3710           14 :       m = MATCH_ERROR;
    3711              :     }
    3712              :   else
    3713              :      /* All tests passed.  */
    3714              :      m = MATCH_YES;
    3715              : 
    3716           14 :   if (m == MATCH_ERROR)
    3717           14 :      gfc_current_locus = where;
    3718              : 
    3719              :   /* Return what we know from the test(s).  */
    3720              :   return m;
    3721              : 
    3722            0 : no_match:
    3723            0 :   gfc_free_expr (e);
    3724            0 :   gfc_current_locus = where;
    3725            0 :   return m;
    3726              : }
    3727              : 
    3728              : 
    3729              : /* Match the various kind/length specifications in a CHARACTER
    3730              :    declaration.  We don't return MATCH_NO.  */
    3731              : 
    3732              : match
    3733        32381 : gfc_match_char_spec (gfc_typespec *ts)
    3734              : {
    3735        32381 :   int kind, seen_length, is_iso_c;
    3736        32381 :   gfc_charlen *cl;
    3737        32381 :   gfc_expr *len;
    3738        32381 :   match m;
    3739        32381 :   bool deferred;
    3740              : 
    3741        32381 :   len = NULL;
    3742        32381 :   seen_length = 0;
    3743        32381 :   kind = 0;
    3744        32381 :   is_iso_c = 0;
    3745        32381 :   deferred = false;
    3746              : 
    3747              :   /* Try the old-style specification first.  */
    3748        32381 :   old_char_selector = 0;
    3749              : 
    3750        32381 :   m = match_char_length (&len, &deferred, true);
    3751        32381 :   if (m != MATCH_NO)
    3752              :     {
    3753         2205 :       if (m == MATCH_YES)
    3754         2205 :         old_char_selector = 1;
    3755         2205 :       seen_length = 1;
    3756         2205 :       goto done;
    3757              :     }
    3758              : 
    3759        30176 :   m = gfc_match_char ('(');
    3760        30176 :   if (m != MATCH_YES)
    3761              :     {
    3762         1916 :       m = MATCH_YES;    /* Character without length is a single char.  */
    3763         1916 :       goto done;
    3764              :     }
    3765              : 
    3766              :   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
    3767        28260 :   if (gfc_match (" kind =") == MATCH_YES)
    3768              :     {
    3769         3529 :       m = match_char_kind (&kind, &is_iso_c);
    3770              : 
    3771         3529 :       if (m == MATCH_ERROR)
    3772           16 :         goto done;
    3773         3513 :       if (m == MATCH_NO)
    3774              :         goto syntax;
    3775              : 
    3776         3513 :       if (gfc_match (" , len =") == MATCH_NO)
    3777          518 :         goto rparen;
    3778              : 
    3779         2995 :       m = char_len_param_value (&len, &deferred);
    3780         2995 :       if (m == MATCH_NO)
    3781            0 :         goto syntax;
    3782         2995 :       if (m == MATCH_ERROR)
    3783            2 :         goto done;
    3784         2993 :       seen_length = 1;
    3785              : 
    3786         2993 :       goto rparen;
    3787              :     }
    3788              : 
    3789              :   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
    3790        24731 :   if (gfc_match (" len =") == MATCH_YES)
    3791              :     {
    3792        14108 :       m = char_len_param_value (&len, &deferred);
    3793        14108 :       if (m == MATCH_NO)
    3794            2 :         goto syntax;
    3795        14106 :       if (m == MATCH_ERROR)
    3796            8 :         goto done;
    3797        14098 :       seen_length = 1;
    3798              : 
    3799        14098 :       if (gfc_match_char (')') == MATCH_YES)
    3800        12793 :         goto done;
    3801              : 
    3802         1305 :       if (gfc_match (" , kind =") != MATCH_YES)
    3803            0 :         goto syntax;
    3804              : 
    3805         1305 :       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
    3806            2 :         goto done;
    3807              : 
    3808         1303 :       goto rparen;
    3809              :     }
    3810              : 
    3811              :   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
    3812        10623 :   m = char_len_param_value (&len, &deferred);
    3813        10623 :   if (m == MATCH_NO)
    3814            0 :     goto syntax;
    3815        10623 :   if (m == MATCH_ERROR)
    3816           44 :     goto done;
    3817        10579 :   seen_length = 1;
    3818              : 
    3819        10579 :   m = gfc_match_char (')');
    3820        10579 :   if (m == MATCH_YES)
    3821        10397 :     goto done;
    3822              : 
    3823          182 :   if (gfc_match_char (',') != MATCH_YES)
    3824            2 :     goto syntax;
    3825              : 
    3826          180 :   gfc_match (" kind =");      /* Gobble optional text.  */
    3827              : 
    3828          180 :   m = match_char_kind (&kind, &is_iso_c);
    3829          180 :   if (m == MATCH_ERROR)
    3830            3 :     goto done;
    3831              :   if (m == MATCH_NO)
    3832              :     goto syntax;
    3833              : 
    3834         4991 : rparen:
    3835              :   /* Require a right-paren at this point.  */
    3836         4991 :   m = gfc_match_char (')');
    3837         4991 :   if (m == MATCH_YES)
    3838         4991 :     goto done;
    3839              : 
    3840            0 : syntax:
    3841            4 :   gfc_error ("Syntax error in CHARACTER declaration at %C");
    3842            4 :   m = MATCH_ERROR;
    3843            4 :   gfc_free_expr (len);
    3844            4 :   return m;
    3845              : 
    3846        32377 : done:
    3847              :   /* Deal with character functions after USE and IMPORT statements.  */
    3848        32377 :   if (gfc_matching_function)
    3849              :     {
    3850         1431 :       gfc_free_expr (len);
    3851         1431 :       gfc_undo_symbols ();
    3852         1431 :       return MATCH_YES;
    3853              :     }
    3854              : 
    3855        30946 :   if (m != MATCH_YES)
    3856              :     {
    3857           65 :       gfc_free_expr (len);
    3858           65 :       return m;
    3859              :     }
    3860              : 
    3861              :   /* Do some final massaging of the length values.  */
    3862        30881 :   cl = gfc_new_charlen (gfc_current_ns, NULL);
    3863              : 
    3864        30881 :   if (seen_length == 0)
    3865         2382 :     cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    3866              :   else
    3867              :     {
    3868              :       /* If gfortran ends up here, then len may be reducible to a constant.
    3869              :          Try to do that here.  If it does not reduce, simply assign len to
    3870              :          charlen.  A complication occurs with user-defined generic functions,
    3871              :          which are not resolved.  Use a private namespace to deal with
    3872              :          generic functions.  */
    3873              : 
    3874        28499 :       if (len && len->expr_type != EXPR_CONSTANT)
    3875              :         {
    3876         3195 :           gfc_namespace *old_ns;
    3877         3195 :           gfc_expr *e;
    3878              : 
    3879         3195 :           old_ns = gfc_current_ns;
    3880         3195 :           gfc_current_ns = gfc_get_namespace (NULL, 0);
    3881              : 
    3882         3195 :           e = gfc_copy_expr (len);
    3883         3195 :           gfc_push_suppress_errors ();
    3884         3195 :           gfc_reduce_init_expr (e);
    3885         3195 :           gfc_pop_suppress_errors ();
    3886         3195 :           if (e->expr_type == EXPR_CONSTANT)
    3887              :             {
    3888          318 :               gfc_replace_expr (len, e);
    3889          318 :               if (mpz_cmp_si (len->value.integer, 0) < 0)
    3890            7 :                 mpz_set_ui (len->value.integer, 0);
    3891              :             }
    3892              :           else
    3893         2877 :             gfc_free_expr (e);
    3894              : 
    3895         3195 :           gfc_free_namespace (gfc_current_ns);
    3896         3195 :           gfc_current_ns = old_ns;
    3897              :         }
    3898              : 
    3899        28499 :       cl->length = len;
    3900              :     }
    3901              : 
    3902        30881 :   ts->u.cl = cl;
    3903        30881 :   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
    3904        30881 :   ts->deferred = deferred;
    3905              : 
    3906              :   /* We have to know if it was a C interoperable kind so we can
    3907              :      do accurate type checking of bind(c) procs, etc.  */
    3908        30881 :   if (kind != 0)
    3909              :     /* Mark this as C interoperable if being declared with one
    3910              :        of the named constants from iso_c_binding.  */
    3911         4831 :     ts->is_c_interop = is_iso_c;
    3912        26050 :   else if (len != NULL)
    3913              :     /* Here, we might have parsed something such as: character(c_char)
    3914              :        In this case, the parsing code above grabs the c_char when
    3915              :        looking for the length (line 1690, roughly).  it's the last
    3916              :        testcase for parsing the kind params of a character variable.
    3917              :        However, it's not actually the length.    this seems like it
    3918              :        could be an error.
    3919              :        To see if the user used a C interop kind, test the expr
    3920              :        of the so called length, and see if it's C interoperable.  */
    3921        16807 :     ts->is_c_interop = len->ts.is_iso_c;
    3922              : 
    3923              :   return MATCH_YES;
    3924              : }
    3925              : 
    3926              : 
    3927              : /* Matches a RECORD declaration. */
    3928              : 
    3929              : static match
    3930       979466 : match_record_decl (char *name)
    3931              : {
    3932       979466 :     locus old_loc;
    3933       979466 :     old_loc = gfc_current_locus;
    3934       979466 :     match m;
    3935              : 
    3936       979466 :     m = gfc_match (" record /");
    3937       979466 :     if (m == MATCH_YES)
    3938              :       {
    3939          353 :           if (!flag_dec_structure)
    3940              :             {
    3941            6 :                 gfc_current_locus = old_loc;
    3942            6 :                 gfc_error ("RECORD at %C is an extension, enable it with "
    3943              :                            "%<-fdec-structure%>");
    3944            6 :                 return MATCH_ERROR;
    3945              :             }
    3946          347 :           m = gfc_match (" %n/", name);
    3947          347 :           if (m == MATCH_YES)
    3948              :             return MATCH_YES;
    3949              :       }
    3950              : 
    3951       979116 :   gfc_current_locus = old_loc;
    3952       979116 :   if (flag_dec_structure
    3953       979116 :       && (gfc_match (" record% ") == MATCH_YES
    3954         8026 :           || gfc_match (" record%t") == MATCH_YES))
    3955            6 :     gfc_error ("Structure name expected after RECORD at %C");
    3956       979116 :   if (m == MATCH_NO)
    3957       979116 :     return MATCH_NO;
    3958              : 
    3959              :   return MATCH_ERROR;
    3960              : }
    3961              : 
    3962              : 
    3963              :   /* In parsing a PDT, it is possible that one of the type parameters has the
    3964              :      same name as a previously declared symbol that is not a type parameter.
    3965              :      Intercept this now by looking for the symtree in f2k_derived.  */
    3966              : 
    3967              : static bool
    3968         1102 : correct_parm_expr (gfc_expr* e, gfc_symbol* pdt, int* f ATTRIBUTE_UNUSED)
    3969              : {
    3970         1102 :   if (!e || (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION))
    3971              :     return false;
    3972              : 
    3973          885 :   if (!(e->symtree->n.sym->attr.pdt_len
    3974          242 :         || e->symtree->n.sym->attr.pdt_kind))
    3975              :     {
    3976          158 :       gfc_symtree *st;
    3977          158 :       st = gfc_find_symtree (pdt->f2k_derived->sym_root,
    3978              :                              e->symtree->n.sym->name);
    3979          158 :       if (st && st->n.sym
    3980           30 :           && (st->n.sym->attr.pdt_len || st->n.sym->attr.pdt_kind))
    3981              :         {
    3982           30 :           gfc_expr *new_expr;
    3983           30 :           gfc_set_sym_referenced (st->n.sym);
    3984           30 :           new_expr = gfc_get_expr ();
    3985           30 :           new_expr->ts = st->n.sym->ts;
    3986           30 :           new_expr->expr_type = EXPR_VARIABLE;
    3987           30 :           new_expr->symtree = st;
    3988           30 :           new_expr->where = e->where;
    3989           30 :           gfc_replace_expr (e, new_expr);
    3990              :         }
    3991              :     }
    3992              : 
    3993              :   return false;
    3994              : }
    3995              : 
    3996              : 
    3997              : void
    3998          918 : gfc_correct_parm_expr (gfc_symbol *pdt, gfc_expr **bound)
    3999              : {
    4000          918 :   if (!*bound || (*bound)->expr_type == EXPR_CONSTANT)
    4001              :     return;
    4002          731 :   gfc_traverse_expr (*bound, pdt, &correct_parm_expr, 0);
    4003              : }
    4004              : 
    4005              : /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
    4006              :    of expressions to substitute into the possibly parameterized expression
    4007              :    'e'. Using a list is inefficient but should not be too bad since the
    4008              :    number of type parameters is not likely to be large.  */
    4009              : static bool
    4010         4135 : insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    4011              :                         int* f)
    4012              : {
    4013         4135 :   gfc_actual_arglist *param;
    4014         4135 :   gfc_expr *copy;
    4015              : 
    4016         4135 :   if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
    4017              :     return false;
    4018              : 
    4019         1879 :   gcc_assert (e->symtree);
    4020         1879 :   if (e->symtree->n.sym->attr.pdt_kind
    4021         1236 :       || (*f != 0 && e->symtree->n.sym->attr.pdt_len)
    4022          669 :       || (e->expr_type == EXPR_FUNCTION && e->symtree->n.sym))
    4023              :     {
    4024         2122 :       for (param = type_param_spec_list; param; param = param->next)
    4025         1978 :         if (!strcmp (e->symtree->n.sym->name, param->name))
    4026              :           break;
    4027              : 
    4028         1353 :       if (param && param->expr)
    4029              :         {
    4030         1208 :           copy = gfc_copy_expr (param->expr);
    4031         1208 :           gfc_replace_expr (e, copy);
    4032              :           /* Catch variables declared without a value expression.  */
    4033         1208 :           if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_PROCEDURE)
    4034           21 :             e->ts = e->symtree->n.sym->ts;
    4035              :         }
    4036              :     }
    4037              : 
    4038              :   return false;
    4039              : }
    4040              : 
    4041              : 
    4042              : static bool
    4043         1187 : gfc_insert_kind_parameter_exprs (gfc_expr *e)
    4044              : {
    4045         1187 :   return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
    4046              : }
    4047              : 
    4048              : 
    4049              : bool
    4050         2151 : gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
    4051              : {
    4052         2151 :   gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
    4053         2151 :   type_param_spec_list = param_list;
    4054         2151 :   bool res = gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
    4055         2151 :   type_param_spec_list = old_param_spec_list;
    4056         2151 :   return res;
    4057              : }
    4058              : 
    4059              : /* Determines the instance of a parameterized derived type to be used by
    4060              :    matching determining the values of the kind parameters and using them
    4061              :    in the name of the instance. If the instance exists, it is used, otherwise
    4062              :    a new derived type is created.  */
    4063              : match
    4064         3035 : gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
    4065              :                       gfc_actual_arglist **ext_param_list)
    4066              : {
    4067              :   /* The PDT template symbol.  */
    4068         3035 :   gfc_symbol *pdt = *sym;
    4069              :   /* The symbol for the parameter in the template f2k_namespace.  */
    4070         3035 :   gfc_symbol *param;
    4071              :   /* The hoped for instance of the PDT.  */
    4072         3035 :   gfc_symbol *instance = NULL;
    4073              :   /* The list of parameters appearing in the PDT declaration.  */
    4074         3035 :   gfc_formal_arglist *type_param_name_list;
    4075              :   /* Used to store the parameter specification list during recursive calls.  */
    4076         3035 :   gfc_actual_arglist *old_param_spec_list;
    4077              :   /* Pointers to the parameter specification being used.  */
    4078         3035 :   gfc_actual_arglist *actual_param;
    4079         3035 :   gfc_actual_arglist *tail = NULL;
    4080              :   /* Used to build up the name of the PDT instance.  */
    4081         3035 :   char *name;
    4082         3035 :   bool name_seen = (param_list == NULL);
    4083         3035 :   bool assumed_seen = false;
    4084         3035 :   bool deferred_seen = false;
    4085         3035 :   bool spec_error = false;
    4086         3035 :   bool alloc_seen = false;
    4087         3035 :   bool ptr_seen = false;
    4088         3035 :   int i;
    4089         3035 :   gfc_expr *kind_expr;
    4090         3035 :   gfc_component *c1, *c2;
    4091         3035 :   match m;
    4092         3035 :   gfc_symtree *s = NULL;
    4093              : 
    4094         3035 :   type_param_spec_list = NULL;
    4095              : 
    4096         3035 :   type_param_name_list = pdt->formal;
    4097         3035 :   actual_param = param_list;
    4098              : 
    4099              :   /* Prevent a PDT component of the same type as the template from being
    4100              :      converted into an instance. Doing this results in the component being
    4101              :      lost.  */
    4102         3035 :   if (gfc_current_state () == COMP_DERIVED
    4103          113 :       && !(gfc_state_stack->previous
    4104          113 :            && gfc_state_stack->previous->state == COMP_DERIVED)
    4105          113 :       && gfc_current_block ()->attr.pdt_template)
    4106              :     {
    4107          100 :       if (ext_param_list)
    4108          100 :         *ext_param_list = gfc_copy_actual_arglist (param_list);
    4109              :       return MATCH_YES;
    4110              :     }
    4111              : 
    4112         2935 :   name = xasprintf ("%s%s", PDT_PREFIX, pdt->name);
    4113              : 
    4114              :   /* Run through the parameter name list and pick up the actual
    4115              :      parameter values or use the default values in the PDT declaration.  */
    4116         9872 :   for (; type_param_name_list;
    4117         4002 :        type_param_name_list = type_param_name_list->next)
    4118              :     {
    4119         4070 :       if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
    4120              :         {
    4121         3620 :           if (actual_param->spec_type == SPEC_ASSUMED)
    4122              :             spec_error = deferred_seen;
    4123              :           else
    4124         3620 :             spec_error = assumed_seen;
    4125              : 
    4126         3620 :           if (spec_error)
    4127              :             {
    4128              :               gfc_error ("The type parameter spec list at %C cannot contain "
    4129              :                          "both ASSUMED and DEFERRED parameters");
    4130              :               goto error_return;
    4131              :             }
    4132              :         }
    4133              : 
    4134         3620 :       if (actual_param && actual_param->name)
    4135         4070 :         name_seen = true;
    4136         4070 :       param = type_param_name_list->sym;
    4137              : 
    4138         4070 :       if (!param || !param->name)
    4139            2 :         continue;
    4140              : 
    4141         4068 :       c1 = gfc_find_component (pdt, param->name, false, true, NULL);
    4142              :       /* An error should already have been thrown in resolve.cc
    4143              :          (resolve_fl_derived0).  */
    4144         4068 :       if (!pdt->attr.use_assoc && !c1)
    4145            8 :         goto error_return;
    4146              : 
    4147              :       /* Resolution PDT class components of derived types are handled here.
    4148              :          They can arrive without a parameter list and no KIND parameters.  */
    4149         4060 :       if (!param_list && (!c1->attr.pdt_kind && !c1->initializer))
    4150           20 :         continue;
    4151              : 
    4152         4040 :       kind_expr = NULL;
    4153         4040 :       if (!name_seen)
    4154              :         {
    4155         2236 :           if (!actual_param && !(c1 && c1->initializer))
    4156              :             {
    4157            2 :               gfc_error ("The type parameter spec list at %C does not contain "
    4158              :                          "enough parameter expressions");
    4159            2 :               goto error_return;
    4160              :             }
    4161         2234 :           else if (!actual_param && c1 && c1->initializer)
    4162            5 :             kind_expr = gfc_copy_expr (c1->initializer);
    4163         2229 :           else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
    4164         1986 :             kind_expr = gfc_copy_expr (actual_param->expr);
    4165              :         }
    4166              :       else
    4167              :         {
    4168              :           actual_param = param_list;
    4169         2684 :           for (;actual_param; actual_param = actual_param->next)
    4170         2246 :             if (actual_param->name
    4171         2226 :                 && strcmp (actual_param->name, param->name) == 0)
    4172              :               break;
    4173         1804 :           if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
    4174         1199 :             kind_expr = gfc_copy_expr (actual_param->expr);
    4175              :           else
    4176              :             {
    4177          605 :               if (c1->initializer)
    4178          541 :                 kind_expr = gfc_copy_expr (c1->initializer);
    4179           64 :               else if (!(actual_param && param->attr.pdt_len))
    4180              :                 {
    4181            9 :                   gfc_error ("The derived parameter %qs at %C does not "
    4182              :                              "have a default value", param->name);
    4183            9 :                   goto error_return;
    4184              :                 }
    4185              :             }
    4186              :         }
    4187              : 
    4188         3731 :       if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
    4189          342 :           && kind_expr->ts.type != BT_INTEGER
    4190          136 :           && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
    4191              :         {
    4192           12 :           gfc_error ("The type parameter expression at %L must be of INTEGER "
    4193              :                      "type and not %s", &kind_expr->where,
    4194              :                      gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
    4195           12 :           goto error_return;
    4196              :         }
    4197              : 
    4198              :       /* Store the current parameter expressions in a temporary actual
    4199              :          arglist 'list' so that they can be substituted in the corresponding
    4200              :          expressions in the PDT instance.  */
    4201         4017 :       if (type_param_spec_list == NULL)
    4202              :         {
    4203         2892 :           type_param_spec_list = gfc_get_actual_arglist ();
    4204         2892 :           tail = type_param_spec_list;
    4205              :         }
    4206              :       else
    4207              :         {
    4208         1125 :           tail->next = gfc_get_actual_arglist ();
    4209         1125 :           tail = tail->next;
    4210              :         }
    4211         4017 :       tail->name = param->name;
    4212              : 
    4213         4017 :       if (kind_expr)
    4214              :         {
    4215              :           /* Try simplification even for LEN expressions.  */
    4216         3719 :           bool ok;
    4217         3719 :           gfc_resolve_expr (kind_expr);
    4218              : 
    4219         3719 :           if (c1->attr.pdt_kind
    4220         2018 :               && kind_expr->expr_type != EXPR_CONSTANT
    4221           28 :               && type_param_spec_list)
    4222           28 :           gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
    4223              : 
    4224         3719 :           ok = gfc_simplify_expr (kind_expr, 1);
    4225              :           /* Variable expressions default to BT_PROCEDURE in the absence of an
    4226              :              initializer so allow for this.  */
    4227         3719 :           if (kind_expr->ts.type != BT_INTEGER
    4228          153 :               && kind_expr->ts.type != BT_PROCEDURE)
    4229              :             {
    4230           29 :               gfc_error ("The parameter expression at %C must be of "
    4231              :                          "INTEGER type and not %s type",
    4232              :                          gfc_basic_typename (kind_expr->ts.type));
    4233           29 :               goto error_return;
    4234              :             }
    4235         3690 :           if (kind_expr->ts.type == BT_INTEGER && !ok)
    4236              :             {
    4237            4 :               gfc_error ("The parameter expression at %C does not "
    4238              :                          "simplify to an INTEGER constant");
    4239            4 :               goto error_return;
    4240              :             }
    4241              : 
    4242         3686 :           tail->expr = gfc_copy_expr (kind_expr);
    4243              :         }
    4244              : 
    4245         3984 :       if (actual_param)
    4246         3548 :         tail->spec_type = actual_param->spec_type;
    4247              : 
    4248         3984 :       if (!param->attr.pdt_kind)
    4249              :         {
    4250         1991 :           if (!name_seen && actual_param)
    4251         1222 :             actual_param = actual_param->next;
    4252         1991 :           if (kind_expr)
    4253              :             {
    4254         1695 :               gfc_free_expr (kind_expr);
    4255         1695 :               kind_expr = NULL;
    4256              :             }
    4257         1991 :           continue;
    4258              :         }
    4259              : 
    4260         1993 :       if (actual_param
    4261         1601 :           && (actual_param->spec_type == SPEC_ASSUMED
    4262         1601 :               || actual_param->spec_type == SPEC_DEFERRED))
    4263              :         {
    4264            2 :           gfc_error ("The KIND parameter %qs at %C cannot either be "
    4265              :                      "ASSUMED or DEFERRED", param->name);
    4266            2 :           goto error_return;
    4267              :         }
    4268              : 
    4269         1991 :       if (!kind_expr || !gfc_is_constant_expr (kind_expr))
    4270              :         {
    4271            2 :           gfc_error ("The value for the KIND parameter %qs at %C does not "
    4272              :                      "reduce to a constant expression", param->name);
    4273            2 :           goto error_return;
    4274              :         }
    4275              : 
    4276              :       /* This can come about during the parsing of nested pdt_templates. An
    4277              :          error arises because the KIND parameter expression has not been
    4278              :          provided. Use the template instead of an incorrect instance.  */
    4279         1989 :       if (kind_expr->expr_type != EXPR_CONSTANT
    4280         1989 :           || kind_expr->ts.type != BT_INTEGER)
    4281              :         {
    4282            0 :           gfc_free_actual_arglist (type_param_spec_list);
    4283            0 :           free (name);
    4284            0 :           return MATCH_YES;
    4285              :         }
    4286              : 
    4287         1989 :       char *kind_value = mpz_get_str (NULL, 10, kind_expr->value.integer);
    4288         1989 :       char *old_name = name;
    4289         1989 :       name = xasprintf ("%s_%s", old_name, kind_value);
    4290         1989 :       free (old_name);
    4291         1989 :       free (kind_value);
    4292              : 
    4293         1989 :       if (!name_seen && actual_param)
    4294          958 :         actual_param = actual_param->next;
    4295         1989 :       gfc_free_expr (kind_expr);
    4296              :     }
    4297              : 
    4298         2867 :   if (!name_seen && actual_param)
    4299              :     {
    4300            2 :       gfc_error ("The type parameter spec list at %C contains too many "
    4301              :                  "parameter expressions");
    4302            2 :       goto error_return;
    4303              :     }
    4304              : 
    4305              :   /* Now we search for the PDT instance 'name'. If it doesn't exist, we
    4306              :      build it, using 'pdt' as a template.  */
    4307         2865 :   if (gfc_get_symbol (name, pdt->ns, &instance))
    4308              :     {
    4309            0 :       gfc_error ("Parameterized derived type at %C is ambiguous");
    4310            0 :       goto error_return;
    4311              :     }
    4312              : 
    4313              :   /* If we are in an interface body, the instance will not have been imported.
    4314              :      Make sure that it is imported implicitly.  */
    4315         2865 :   s = gfc_find_symtree (gfc_current_ns->sym_root, pdt->name);
    4316         2865 :   if (gfc_current_ns->proc_name
    4317         2818 :       && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
    4318           93 :       && s && s->import_only && pdt->attr.imported)
    4319              :     {
    4320            2 :       s = gfc_find_symtree (gfc_current_ns->sym_root, instance->name);
    4321            2 :       if (!s)
    4322              :         {
    4323            1 :           gfc_get_sym_tree (instance->name, gfc_current_ns, &s, false,
    4324              :                             &gfc_current_locus);
    4325            1 :           s->n.sym = instance;
    4326              :         }
    4327            2 :       s->n.sym->attr.imported = 1;
    4328            2 :       s->import_only = 1;
    4329              :     }
    4330              : 
    4331         2865 :   m = MATCH_YES;
    4332              : 
    4333         2865 :   if (instance->attr.flavor == FL_DERIVED
    4334         2242 :       && instance->attr.pdt_type
    4335         2242 :       && instance->components)
    4336              :     {
    4337         2242 :       instance->refs++;
    4338         2242 :       if (ext_param_list)
    4339         1038 :         *ext_param_list = type_param_spec_list;
    4340         2242 :       *sym = instance;
    4341         2242 :       gfc_commit_symbols ();
    4342         2242 :       free (name);
    4343         2242 :       return m;
    4344              :     }
    4345              : 
    4346              :   /* Start building the new instance of the parameterized type.  */
    4347          623 :   gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
    4348          623 :   if (pdt->attr.use_assoc)
    4349          114 :     instance->module = pdt->module;
    4350          623 :   instance->attr.pdt_template = 0;
    4351          623 :   instance->attr.pdt_type = 1;
    4352          623 :   instance->declared_at = gfc_current_locus;
    4353              : 
    4354              :   /* In resolution, the finalizers are copied, according to the type of the
    4355              :      argument, to the instance finalizers. However, they are retained by the
    4356              :      template and procedures are freed there.  */
    4357          623 :   if (pdt->f2k_derived && pdt->f2k_derived->finalizers)
    4358              :     {
    4359           24 :       instance->f2k_derived = gfc_get_namespace (NULL, 0);
    4360           24 :       instance->template_sym = pdt;
    4361           24 :       *instance->f2k_derived = *pdt->f2k_derived;
    4362              :     }
    4363              : 
    4364              :   /* Add the components, replacing the parameters in all expressions
    4365              :      with the expressions for their values in 'type_param_spec_list'.  */
    4366          623 :   c1 = pdt->components;
    4367          623 :   tail = type_param_spec_list;
    4368         2398 :   for (; c1; c1 = c1->next)
    4369              :     {
    4370         1777 :       gfc_add_component (instance, c1->name, &c2);
    4371              : 
    4372         1777 :       c2->ts = c1->ts;
    4373         1777 :       c2->attr = c1->attr;
    4374         1777 :       if (c1->tb)
    4375              :         {
    4376            6 :           c2->tb = gfc_get_tbp ();
    4377            6 :           *c2->tb = *c1->tb;
    4378              :         }
    4379              : 
    4380              :       /* The order of declaration of the type_specs might not be the
    4381              :          same as that of the components.  */
    4382         1777 :       if (c1->attr.pdt_kind || c1->attr.pdt_len)
    4383              :         {
    4384         1262 :           for (tail = type_param_spec_list; tail; tail = tail->next)
    4385         1258 :             if (strcmp (c1->name, tail->name) == 0)
    4386              :               break;
    4387              :         }
    4388              : 
    4389              :       /* Deal with type extension by recursively calling this function
    4390              :          to obtain the instance of the extended type.  */
    4391         1777 :       if (gfc_current_state () != COMP_DERIVED
    4392         1763 :           && c1 == pdt->components
    4393          610 :           && c1->ts.type == BT_DERIVED
    4394           78 :           && c1->ts.u.derived
    4395         1855 :           && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
    4396              :         {
    4397           78 :           if (c1->ts.u.derived->attr.pdt_template)
    4398              :             {
    4399           71 :               gfc_formal_arglist *f;
    4400              : 
    4401           71 :               old_param_spec_list = type_param_spec_list;
    4402              : 
    4403              :               /* Obtain a spec list appropriate to the extended type..*/
    4404           71 :               actual_param = gfc_copy_actual_arglist (type_param_spec_list);
    4405           71 :               type_param_spec_list = actual_param;
    4406          139 :               for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
    4407           68 :                 actual_param = actual_param->next;
    4408           71 :               if (actual_param)
    4409              :                 {
    4410           71 :                   gfc_free_actual_arglist (actual_param->next);
    4411           71 :                   actual_param->next = NULL;
    4412              :                 }
    4413              : 
    4414              :               /* Now obtain the PDT instance for the extended type.  */
    4415           71 :               c2->param_list = type_param_spec_list;
    4416           71 :               m = gfc_get_pdt_instance (type_param_spec_list,
    4417              :                                         &c2->ts.u.derived,
    4418              :                                         &c2->param_list);
    4419           71 :               type_param_spec_list = old_param_spec_list;
    4420              :             }
    4421              :           else
    4422            7 :             c2->ts = c1->ts;
    4423              : 
    4424           78 :           c2->ts.u.derived->refs++;
    4425           78 :           gfc_set_sym_referenced (c2->ts.u.derived);
    4426              : 
    4427              :           /* If the component is allocatable or the parent has allocatable
    4428              :              components, make sure that the new instance also is marked as
    4429              :              having allocatable components.  */
    4430           78 :           if (c2->attr.allocatable || c2->ts.u.derived->attr.alloc_comp)
    4431            6 :             instance->attr.alloc_comp = 1;
    4432              : 
    4433              :           /* Set extension level.  */
    4434           78 :           if (c2->ts.u.derived->attr.extension == 255)
    4435              :             {
    4436              :               /* Since the extension field is 8 bit wide, we can only have
    4437              :                  up to 255 extension levels.  */
    4438            0 :               gfc_error ("Maximum extension level reached with type %qs at %L",
    4439              :                          c2->ts.u.derived->name,
    4440              :                          &c2->ts.u.derived->declared_at);
    4441            0 :               goto error_return;
    4442              :             }
    4443           78 :           instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
    4444              : 
    4445           78 :           continue;
    4446           78 :         }
    4447              : 
    4448              :       /* Addressing PR82943, this will fix the issue where a function or
    4449              :          subroutine is declared as not a member of the PDT instance.
    4450              :          The reason for this is because the PDT instance did not have access
    4451              :          to its template's f2k_derived namespace in order to find the
    4452              :          typebound procedures.
    4453              : 
    4454              :          The number of references to the PDT template's f2k_derived will
    4455              :          ensure that f2k_derived is properly freed later on.  */
    4456              : 
    4457         1699 :       if (!instance->f2k_derived && pdt->f2k_derived)
    4458              :         {
    4459          592 :           instance->f2k_derived = pdt->f2k_derived;
    4460          592 :           instance->f2k_derived->refs++;
    4461              :         }
    4462              : 
    4463              :       /* Set the component kind using the parameterized expression.  */
    4464         1699 :       if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
    4465          657 :            && c1->kind_expr != NULL)
    4466              :         {
    4467          446 :           gfc_expr *e = gfc_copy_expr (c1->kind_expr);
    4468          446 :           gfc_insert_kind_parameter_exprs (e);
    4469          446 :           gfc_simplify_expr (e, 1);
    4470          446 :           gfc_extract_int (e, &c2->ts.kind);
    4471          446 :           gfc_free_expr (e);
    4472          446 :           if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
    4473              :             {
    4474            2 :               gfc_error ("Kind %d not supported for type %s at %C",
    4475              :                          c2->ts.kind, gfc_basic_typename (c2->ts.type));
    4476            2 :               goto error_return;
    4477              :             }
    4478          444 :           if (c2->attr.proc_pointer && c2->attr.function
    4479            0 :               && c1->ts.interface && c1->ts.interface->ts.kind == 0)
    4480              :             {
    4481            0 :               c2->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    4482            0 :               c2->ts.interface->result = c2->ts.interface;
    4483            0 :               c2->ts.interface->ts = c2->ts;
    4484            0 :               c2->ts.interface->attr.flavor = FL_PROCEDURE;
    4485            0 :               c2->ts.interface->attr.function = 1;
    4486            0 :               c2->attr.function = 1;
    4487            0 :               c2->attr.if_source = IFSRC_UNKNOWN;
    4488              :             }
    4489              :         }
    4490              : 
    4491              :       /* Set up either the KIND/LEN initializer, if constant,
    4492              :          or the parameterized expression. Use the template
    4493              :          initializer if one is not already set in this instance.  */
    4494         1697 :       if (c2->attr.pdt_kind || c2->attr.pdt_len)
    4495              :         {
    4496          826 :           if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
    4497          674 :             c2->initializer = gfc_copy_expr (tail->expr);
    4498          152 :           else if (tail && tail->expr)
    4499              :             {
    4500           34 :               c2->param_list = gfc_get_actual_arglist ();
    4501           34 :               c2->param_list->name = tail->name;
    4502           34 :               c2->param_list->expr = gfc_copy_expr (tail->expr);
    4503           34 :               c2->param_list->next = NULL;
    4504              :             }
    4505              : 
    4506              :           /* Initializer expressions in PDT templates, such as character_kinds(1),
    4507              :              can end up being mutilated when use associated. Simplify now.  */
    4508          826 :           if (c1->initializer && c1->initializer->expr_type != EXPR_CONSTANT)
    4509          154 :             gfc_simplify_expr (c1->initializer, 1);
    4510              : 
    4511          826 :           if (!c2->initializer && c1->initializer)
    4512           24 :             c2->initializer = gfc_copy_expr (c1->initializer);
    4513              : 
    4514          826 :           if (c2->initializer)
    4515          698 :             gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4516              :         }
    4517              : 
    4518              :       /* Copy the array spec.  */
    4519         1697 :       c2->as = gfc_copy_array_spec (c1->as);
    4520         1697 :       if (c1->ts.type == BT_CLASS)
    4521            0 :         CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
    4522              : 
    4523         1697 :       if (c1->attr.allocatable)
    4524           82 :         alloc_seen = true;
    4525              : 
    4526         1697 :       if (c1->attr.pointer)
    4527           20 :         ptr_seen = true;
    4528              : 
    4529              :       /* Determine if an array spec is parameterized. If so, substitute
    4530              :          in the parameter expressions for the bounds and set the pdt_array
    4531              :          attribute. Notice that this attribute must be unconditionally set
    4532              :          if this is an array of parameterized character length.  */
    4533         1697 :       if (c1->as && c1->as->type == AS_EXPLICIT)
    4534              :         {
    4535              :           bool pdt_array = false;
    4536          658 :           bool all_constant = true;
    4537              : 
    4538              :           /* Are the bounds of the array parameterized?  */
    4539          658 :           for (i = 0; i < c1->as->rank; i++)
    4540              :             {
    4541          377 :               if (gfc_derived_parameter_expr (c1->as->lower[i]))
    4542            6 :                 pdt_array = true;
    4543          377 :               if (gfc_derived_parameter_expr (c1->as->upper[i]))
    4544          297 :                 pdt_array = true;
    4545              :             }
    4546              : 
    4547              :           /* If they are, free the expressions for the bounds and
    4548              :              replace them with the template expressions with substitute
    4549              :              values.  */
    4550          578 :           for (i = 0; pdt_array && i < c1->as->rank; i++)
    4551              :             {
    4552          297 :               gfc_expr *e;
    4553          297 :               e = gfc_copy_expr (c1->as->lower[i]);
    4554          297 :               gfc_insert_kind_parameter_exprs (e);
    4555          297 :               if (gfc_simplify_expr (e, 1))
    4556          297 :                 gfc_replace_expr (c2->as->lower[i], e);
    4557              :               else
    4558            0 :                 gfc_free_expr (e);
    4559          297 :               if (c2->as->lower[i]->expr_type != EXPR_CONSTANT)
    4560            6 :                 all_constant = false;
    4561          297 :               e = gfc_copy_expr (c1->as->upper[i]);
    4562          297 :               gfc_insert_kind_parameter_exprs (e);
    4563          297 :               if (gfc_simplify_expr (e, 1))
    4564          297 :                 gfc_replace_expr (c2->as->upper[i], e);
    4565              :               else
    4566            0 :                 gfc_free_expr (e);
    4567          297 :               if (c2->as->upper[i]->expr_type != EXPR_CONSTANT)
    4568          295 :                 all_constant = false;
    4569              :             }
    4570              : 
    4571          281 :           c2->attr.pdt_array = all_constant ? 0 : 1;
    4572          281 :           if (c1->initializer)
    4573              :             {
    4574            7 :               c2->initializer = gfc_copy_expr (c1->initializer);
    4575            7 :               gfc_insert_kind_parameter_exprs (c2->initializer);
    4576            7 :               gfc_simplify_expr (c2->initializer, 1);
    4577              :             }
    4578              :         }
    4579              : 
    4580              :       /* Similarly, set the string length if parameterized.  */
    4581         1697 :       if (c1->ts.type == BT_CHARACTER
    4582          177 :           && c1->ts.u.cl->length
    4583         1873 :           && gfc_derived_parameter_expr (c1->ts.u.cl->length))
    4584              :         {
    4585          140 :           gfc_expr *e;
    4586          140 :           e = gfc_copy_expr (c1->ts.u.cl->length);
    4587          140 :           gfc_insert_kind_parameter_exprs (e);
    4588          140 :           if (gfc_simplify_expr (e, 1))
    4589          140 :             gfc_replace_expr (c2->ts.u.cl->length, e);
    4590              :           else
    4591            0 :             gfc_free_expr (e);
    4592          140 :           if (c2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    4593          137 :             c2->attr.pdt_string = 1;
    4594          140 :           if (c1->as && c1->as->type == AS_EXPLICIT)
    4595           48 :             c2->attr.pdt_array = 1;
    4596           92 :           else if (c1->attr.allocatable)
    4597            6 :             c2->ts.deferred = 1;
    4598              :         }
    4599              : 
    4600              :       /* Recurse into this function for PDT components.  */
    4601         1697 :       if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
    4602          131 :           && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
    4603              :         {
    4604          123 :           gfc_actual_arglist *params;
    4605              :           /* The component in the template has a list of specification
    4606              :              expressions derived from its declaration.  */
    4607          123 :           params = gfc_copy_actual_arglist (c1->param_list);
    4608          123 :           actual_param = params;
    4609              :           /* Substitute the template parameters with the expressions
    4610              :              from the specification list.  */
    4611          384 :           for (;actual_param; actual_param = actual_param->next)
    4612              :             {
    4613          138 :               gfc_correct_parm_expr (pdt, &actual_param->expr);
    4614          138 :               gfc_insert_parameter_exprs (actual_param->expr,
    4615              :                                           type_param_spec_list);
    4616              :             }
    4617              : 
    4618              :           /* Now obtain the PDT instance for the component.  */
    4619          123 :           old_param_spec_list = type_param_spec_list;
    4620          246 :           m = gfc_get_pdt_instance (params, &c2->ts.u.derived,
    4621          123 :                                     &c2->param_list);
    4622          123 :           type_param_spec_list = old_param_spec_list;
    4623              : 
    4624          123 :           if (!(c2->attr.pointer || c2->attr.allocatable))
    4625              :             {
    4626           83 :               if (!c1->initializer
    4627           58 :                   || c1->initializer->expr_type != EXPR_FUNCTION)
    4628           82 :                 c2->initializer = gfc_default_initializer (&c2->ts);
    4629              :               else
    4630              :                 {
    4631            1 :                   gfc_symtree *s;
    4632            1 :                   c2->initializer = gfc_copy_expr (c1->initializer);
    4633            1 :                   s = gfc_find_symtree (pdt->ns->sym_root,
    4634            1 :                                 gfc_dt_lower_string (c2->ts.u.derived->name));
    4635            1 :                   if (s)
    4636            0 :                     c2->initializer->symtree = s;
    4637            1 :                   c2->initializer->ts = c2->ts;
    4638            1 :                   if (!s)
    4639            1 :                     gfc_insert_parameter_exprs (c2->initializer,
    4640              :                                                 type_param_spec_list);
    4641            1 :                   gfc_simplify_expr (c2->initializer, 1);
    4642              :                 }
    4643              :             }
    4644              : 
    4645          123 :           if (c2->attr.allocatable
    4646           91 :               || (c2->ts.type == BT_DERIVED && c2->ts.u.derived
    4647           91 :                   && c2->ts.u.derived->attr.alloc_comp && !c2->attr.pointer))
    4648           61 :             instance->attr.alloc_comp = 1;
    4649              :         }
    4650         1574 :       else if (!(c2->attr.pdt_kind || c2->attr.pdt_len || c2->attr.pdt_string
    4651          611 :                  || c2->attr.pdt_array) && c1->initializer)
    4652              :         {
    4653           44 :           c2->initializer = gfc_copy_expr (c1->initializer);
    4654           44 :           if (c2->initializer->ts.type == BT_UNKNOWN)
    4655           12 :             c2->initializer->ts = c2->ts;
    4656           44 :           gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4657              :           /* The template initializers are parsed using gfc_match_expr rather
    4658              :              than gfc_match_init_expr. Apply the missing reduction to the
    4659              :              PDT instance initializers.  */
    4660           44 :           if (!gfc_reduce_init_expr (c2->initializer))
    4661              :             {
    4662            0 :               gfc_free_expr (c2->initializer);
    4663            0 :               goto error_return;
    4664              :             }
    4665           44 :           gfc_simplify_expr (c2->initializer, 1);
    4666              :         }
    4667              : 
    4668              :       /* Pick up any remaining initializers that could be simplified.  */
    4669         1697 :       if (c1->initializer)
    4670              :         {
    4671          451 :           if (!c2->initializer)
    4672           25 :             c2->initializer = gfc_copy_expr (c1->initializer);
    4673          451 :           if (gfc_derived_parameter_expr (c2->initializer))
    4674            0 :             gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
    4675          451 :           c2->initializer->ts = c2->ts;
    4676          451 :           if (!!gfc_is_constant_expr (c2->initializer))
    4677          432 :             gfc_simplify_expr (c2->initializer, 1);
    4678              :         }
    4679              :     }
    4680              : 
    4681          621 :   if (alloc_seen)
    4682           79 :     instance->attr.alloc_comp = 1;
    4683          621 :   if (ptr_seen)
    4684           20 :     instance->attr.pointer_comp = 1;
    4685              : 
    4686              : 
    4687          621 :   gfc_commit_symbol (instance);
    4688          621 :   if (ext_param_list)
    4689          402 :     *ext_param_list = type_param_spec_list;
    4690          621 :   *sym = instance;
    4691          621 :   free (name);
    4692          621 :   return m;
    4693              : 
    4694           72 : error_return:
    4695           72 :   gfc_free_actual_arglist (type_param_spec_list);
    4696           72 :   free (name);
    4697           72 :   return MATCH_ERROR;
    4698              : }
    4699              : 
    4700              : 
    4701              : /* Match a legacy nonstandard BYTE type-spec.  */
    4702              : 
    4703              : static match
    4704      1204913 : match_byte_typespec (gfc_typespec *ts)
    4705              : {
    4706      1204913 :   if (gfc_match (" byte") == MATCH_YES)
    4707              :     {
    4708           33 :       if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
    4709              :         return MATCH_ERROR;
    4710              : 
    4711           31 :       if (gfc_current_form == FORM_FREE)
    4712              :         {
    4713           19 :           char c = gfc_peek_ascii_char ();
    4714           19 :           if (!gfc_is_whitespace (c) && c != ',')
    4715              :             return MATCH_NO;
    4716              :         }
    4717              : 
    4718           29 :       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
    4719              :         {
    4720            0 :           gfc_error ("BYTE type used at %C "
    4721              :                      "is not available on the target machine");
    4722            0 :           return MATCH_ERROR;
    4723              :         }
    4724              : 
    4725           29 :       ts->type = BT_INTEGER;
    4726           29 :       ts->kind = 1;
    4727           29 :       return MATCH_YES;
    4728              :     }
    4729              :   return MATCH_NO;
    4730              : }
    4731              : 
    4732              : 
    4733              : /* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
    4734              :    structure to the matched specification.  This is necessary for FUNCTION and
    4735              :    IMPLICIT statements.
    4736              : 
    4737              :    If implicit_flag is nonzero, then we don't check for the optional
    4738              :    kind specification.  Not doing so is needed for matching an IMPLICIT
    4739              :    statement correctly.  */
    4740              : 
    4741              : match
    4742      1204913 : gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
    4743              : {
    4744              :   /* Provide sufficient space to hold "pdtsymbol".  */
    4745      1204913 :   char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
    4746      1204913 :   gfc_symbol *sym, *dt_sym;
    4747      1204913 :   match m;
    4748      1204913 :   char c;
    4749      1204913 :   bool seen_deferred_kind, matched_type;
    4750      1204913 :   const char *dt_name;
    4751              : 
    4752      1204913 :   decl_type_param_list = NULL;
    4753              : 
    4754              :   /* A belt and braces check that the typespec is correctly being treated
    4755              :      as a deferred characteristic association.  */
    4756      2409826 :   seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
    4757        84836 :                           && (gfc_current_block ()->result->ts.kind == -1)
    4758      1216873 :                           && (ts->kind == -1);
    4759      1204913 :   gfc_clear_ts (ts);
    4760      1204913 :   if (seen_deferred_kind)
    4761         9725 :     ts->kind = -1;
    4762              : 
    4763              :   /* Clear the current binding label, in case one is given.  */
    4764      1204913 :   curr_binding_label = NULL;
    4765              : 
    4766              :   /* Match BYTE type-spec.  */
    4767      1204913 :   m = match_byte_typespec (ts);
    4768      1204913 :   if (m != MATCH_NO)
    4769              :     return m;
    4770              : 
    4771      1204882 :   m = gfc_match (" type (");
    4772      1204882 :   matched_type = (m == MATCH_YES);
    4773      1204882 :   if (matched_type)
    4774              :     {
    4775        32085 :       gfc_gobble_whitespace ();
    4776        32085 :       if (gfc_peek_ascii_char () == '*')
    4777              :         {
    4778         5617 :           if ((m = gfc_match ("* ) ")) != MATCH_YES)
    4779              :             return m;
    4780         5617 :           if (gfc_comp_struct (gfc_current_state ()))
    4781              :             {
    4782            2 :               gfc_error ("Assumed type at %C is not allowed for components");
    4783            2 :               return MATCH_ERROR;
    4784              :             }
    4785         5615 :           if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
    4786              :             return MATCH_ERROR;
    4787         5613 :           ts->type = BT_ASSUMED;
    4788         5613 :           return MATCH_YES;
    4789              :         }
    4790              : 
    4791        26468 :       m = gfc_match ("%n", name);
    4792        26468 :       matched_type = (m == MATCH_YES);
    4793              :     }
    4794              : 
    4795        26468 :   if ((matched_type && strcmp ("integer", name) == 0)
    4796      1199265 :       || (!matched_type && gfc_match (" integer") == MATCH_YES))
    4797              :     {
    4798       113745 :       ts->type = BT_INTEGER;
    4799       113745 :       ts->kind = gfc_default_integer_kind;
    4800       113745 :       goto get_kind;
    4801              :     }
    4802              : 
    4803      1085520 :   if (flag_unsigned)
    4804              :     {
    4805            0 :       if ((matched_type && strcmp ("unsigned", name) == 0)
    4806        22489 :           || (!matched_type && gfc_match (" unsigned") == MATCH_YES))
    4807              :         {
    4808         1036 :           ts->type = BT_UNSIGNED;
    4809         1036 :           ts->kind = gfc_default_integer_kind;
    4810         1036 :           goto get_kind;
    4811              :         }
    4812              :     }
    4813              : 
    4814        26462 :   if ((matched_type && strcmp ("character", name) == 0)
    4815      1084484 :       || (!matched_type && gfc_match (" character") == MATCH_YES))
    4816              :     {
    4817        29395 :       if (matched_type
    4818        29395 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4819              :                               "intrinsic-type-spec at %C"))
    4820              :         return MATCH_ERROR;
    4821              : 
    4822        29394 :       ts->type = BT_CHARACTER;
    4823        29394 :       if (implicit_flag == 0)
    4824        29288 :         m = gfc_match_char_spec (ts);
    4825              :       else
    4826              :         m = MATCH_YES;
    4827              : 
    4828        29394 :       if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
    4829              :         {
    4830            1 :           gfc_error ("Malformed type-spec at %C");
    4831            1 :           return MATCH_ERROR;
    4832              :         }
    4833              : 
    4834              :       return m;
    4835              :     }
    4836              : 
    4837        26458 :   if ((matched_type && strcmp ("real", name) == 0)
    4838      1055089 :       || (!matched_type && gfc_match (" real") == MATCH_YES))
    4839              :     {
    4840        30567 :       ts->type = BT_REAL;
    4841        30567 :       ts->kind = gfc_default_real_kind;
    4842        30567 :       goto get_kind;
    4843              :     }
    4844              : 
    4845      1024522 :   if ((matched_type
    4846        26455 :        && (strcmp ("doubleprecision", name) == 0
    4847        26454 :            || (strcmp ("double", name) == 0
    4848            5 :                && gfc_match (" precision") == MATCH_YES)))
    4849      1024522 :       || (!matched_type && gfc_match (" double precision") == MATCH_YES))
    4850              :     {
    4851         2614 :       if (matched_type
    4852         2614 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4853              :                               "intrinsic-type-spec at %C"))
    4854              :         return MATCH_ERROR;
    4855              : 
    4856         2613 :       if (matched_type && gfc_match_char (')') != MATCH_YES)
    4857              :         {
    4858            2 :           gfc_error ("Malformed type-spec at %C");
    4859            2 :           return MATCH_ERROR;
    4860              :         }
    4861              : 
    4862         2611 :       ts->type = BT_REAL;
    4863         2611 :       ts->kind = gfc_default_double_kind;
    4864         2611 :       return MATCH_YES;
    4865              :     }
    4866              : 
    4867        26451 :   if ((matched_type && strcmp ("complex", name) == 0)
    4868      1021908 :       || (!matched_type && gfc_match (" complex") == MATCH_YES))
    4869              :     {
    4870         4153 :       ts->type = BT_COMPLEX;
    4871         4153 :       ts->kind = gfc_default_complex_kind;
    4872         4153 :       goto get_kind;
    4873              :     }
    4874              : 
    4875      1017755 :   if ((matched_type
    4876        26451 :        && (strcmp ("doublecomplex", name) == 0
    4877        26450 :            || (strcmp ("double", name) == 0
    4878            2 :                && gfc_match (" complex") == MATCH_YES)))
    4879      1017755 :       || (!matched_type && gfc_match (" double complex") == MATCH_YES))
    4880              :     {
    4881          204 :       if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
    4882              :         return MATCH_ERROR;
    4883              : 
    4884          203 :       if (matched_type
    4885          203 :           && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    4886              :                               "intrinsic-type-spec at %C"))
    4887              :         return MATCH_ERROR;
    4888              : 
    4889          203 :       if (matched_type && gfc_match_char (')') != MATCH_YES)
    4890              :         {
    4891            2 :           gfc_error ("Malformed type-spec at %C");
    4892            2 :           return MATCH_ERROR;
    4893              :         }
    4894              : 
    4895          201 :       ts->type = BT_COMPLEX;
    4896          201 :       ts->kind = gfc_default_double_kind;
    4897          201 :       return MATCH_YES;
    4898              :     }
    4899              : 
    4900        26448 :   if ((matched_type && strcmp ("logical", name) == 0)
    4901      1017551 :       || (!matched_type && gfc_match (" logical") == MATCH_YES))
    4902              :     {
    4903        11640 :       ts->type = BT_LOGICAL;
    4904        11640 :       ts->kind = gfc_default_logical_kind;
    4905        11640 :       goto get_kind;
    4906              :     }
    4907              : 
    4908      1005911 :   if (matched_type)
    4909              :     {
    4910        26445 :       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
    4911        26445 :       if (m == MATCH_ERROR)
    4912              :         return m;
    4913              : 
    4914        26445 :       gfc_gobble_whitespace ();
    4915        26445 :       if (gfc_peek_ascii_char () != ')')
    4916              :         {
    4917            1 :           gfc_error ("Malformed type-spec at %C");
    4918            1 :           return MATCH_ERROR;
    4919              :         }
    4920        26444 :       m = gfc_match_char (')'); /* Burn closing ')'.  */
    4921              :     }
    4922              : 
    4923      1005910 :   if (m != MATCH_YES)
    4924       979466 :     m = match_record_decl (name);
    4925              : 
    4926      1005910 :   if (matched_type || m == MATCH_YES)
    4927              :     {
    4928        26788 :       ts->type = BT_DERIVED;
    4929              :       /* We accept record/s/ or type(s) where s is a structure, but we
    4930              :        * don't need all the extra derived-type stuff for structures.  */
    4931        26788 :       if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
    4932              :         {
    4933            1 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    4934            1 :           return MATCH_ERROR;
    4935              :         }
    4936              : 
    4937        26787 :       if (sym && sym->attr.flavor == FL_DERIVED
    4938        25637 :           && sym->attr.pdt_template
    4939         1120 :           && gfc_current_state () != COMP_DERIVED)
    4940              :         {
    4941         1005 :           m = gfc_get_pdt_instance (decl_type_param_list, &sym,  NULL);
    4942         1005 :           if (m != MATCH_YES)
    4943              :             return m;
    4944          990 :           gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
    4945          990 :           ts->u.derived = sym;
    4946          990 :           const char* lower = gfc_dt_lower_string (sym->name);
    4947          990 :           size_t len = strlen (lower);
    4948              :           /* Reallocate with sufficient size.  */
    4949          990 :           if (len > GFC_MAX_SYMBOL_LEN)
    4950            2 :             name = XALLOCAVEC (char, len + 1);
    4951          990 :           memcpy (name, lower, len);
    4952          990 :           name[len] = '\0';
    4953              :         }
    4954              : 
    4955        26772 :       if (sym && sym->attr.flavor == FL_STRUCT)
    4956              :         {
    4957          361 :           ts->u.derived = sym;
    4958          361 :           return MATCH_YES;
    4959              :         }
    4960              :       /* Actually a derived type.  */
    4961              :     }
    4962              : 
    4963              :   else
    4964              :     {
    4965              :       /* Match nested STRUCTURE declarations; only valid within another
    4966              :          structure declaration.  */
    4967       979122 :       if (flag_dec_structure
    4968         8032 :           && (gfc_current_state () == COMP_STRUCTURE
    4969         7570 :               || gfc_current_state () == COMP_MAP))
    4970              :         {
    4971          732 :           m = gfc_match (" structure");
    4972          732 :           if (m == MATCH_YES)
    4973              :             {
    4974           27 :               m = gfc_match_structure_decl ();
    4975           27 :               if (m == MATCH_YES)
    4976              :                 {
    4977              :                   /* gfc_new_block is updated by match_structure_decl.  */
    4978           26 :                   ts->type = BT_DERIVED;
    4979           26 :                   ts->u.derived = gfc_new_block;
    4980           26 :                   return MATCH_YES;
    4981              :                 }
    4982              :             }
    4983          706 :           if (m == MATCH_ERROR)
    4984              :             return MATCH_ERROR;
    4985              :         }
    4986              : 
    4987              :       /* Match CLASS declarations.  */
    4988       979095 :       m = gfc_match (" class ( * )");
    4989       979095 :       if (m == MATCH_ERROR)
    4990              :         return MATCH_ERROR;
    4991       979095 :       else if (m == MATCH_YES)
    4992              :         {
    4993         2021 :           gfc_symbol *upe;
    4994         2021 :           gfc_symtree *st;
    4995         2021 :           ts->type = BT_CLASS;
    4996         2021 :           gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
    4997         2021 :           if (upe == NULL)
    4998              :             {
    4999         1219 :               upe = gfc_new_symbol ("STAR", gfc_current_ns);
    5000         1219 :               st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
    5001         1219 :               st->n.sym = upe;
    5002         1219 :               gfc_set_sym_referenced (upe);
    5003         1219 :               upe->refs++;
    5004         1219 :               upe->ts.type = BT_VOID;
    5005         1219 :               upe->attr.unlimited_polymorphic = 1;
    5006              :               /* This is essential to force the construction of
    5007              :                  unlimited polymorphic component class containers.  */
    5008         1219 :               upe->attr.zero_comp = 1;
    5009         1219 :               if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
    5010              :                                    &gfc_current_locus))
    5011              :               return MATCH_ERROR;
    5012              :             }
    5013              :           else
    5014              :             {
    5015          802 :               st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
    5016          802 :               st->n.sym = upe;
    5017          802 :               upe->refs++;
    5018              :             }
    5019         2021 :           ts->u.derived = upe;
    5020         2021 :           return m;
    5021              :         }
    5022              : 
    5023       977074 :       m = gfc_match (" class (");
    5024              : 
    5025       977074 :       if (m == MATCH_YES)
    5026         9247 :         m = gfc_match ("%n", name);
    5027              :       else
    5028              :         return m;
    5029              : 
    5030         9247 :       if (m != MATCH_YES)
    5031              :         return m;
    5032         9247 :       ts->type = BT_CLASS;
    5033              : 
    5034         9247 :       if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
    5035              :         return MATCH_ERROR;
    5036              : 
    5037         9246 :       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
    5038         9246 :       if (m == MATCH_ERROR)
    5039              :         return m;
    5040              : 
    5041         9246 :       m = gfc_match_char (')');
    5042         9246 :       if (m != MATCH_YES)
    5043              :         return m;
    5044              :     }
    5045              : 
    5046              :   /* This picks up function declarations with a PDT typespec. Since a
    5047              :      pdt_type has been generated, there is no more to do. Within the
    5048              :      function body, this type must be used for the typespec so that
    5049              :      the "being used before it is defined warning" does not arise.  */
    5050        35657 :   if (ts->type == BT_DERIVED
    5051        26411 :       && sym && sym->attr.pdt_type
    5052        36647 :       && (gfc_current_state () == COMP_CONTAINS
    5053          974 :           || (gfc_current_state () == COMP_FUNCTION
    5054          286 :               && gfc_current_block ()->ts.type == BT_DERIVED
    5055           60 :               && gfc_current_block ()->ts.u.derived == sym
    5056           30 :               && !gfc_find_symtree (gfc_current_ns->sym_root,
    5057              :                                     sym->name))))
    5058              :     {
    5059           42 :       if (gfc_current_state () == COMP_FUNCTION)
    5060              :         {
    5061           26 :           gfc_symtree *pdt_st;
    5062           26 :           pdt_st = gfc_new_symtree (&gfc_current_ns->sym_root,
    5063              :                                     sym->name);
    5064           26 :           pdt_st->n.sym = sym;
    5065           26 :           sym->refs++;
    5066              :         }
    5067           42 :       ts->u.derived = sym;
    5068           42 :       return MATCH_YES;
    5069              :     }
    5070              : 
    5071              :   /* Defer association of the derived type until the end of the
    5072              :      specification block.  However, if the derived type can be
    5073              :      found, add it to the typespec.  */
    5074        35615 :   if (gfc_matching_function)
    5075              :     {
    5076         1044 :       ts->u.derived = NULL;
    5077         1044 :       if (gfc_current_state () != COMP_INTERFACE
    5078         1044 :             && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
    5079              :         {
    5080          513 :           sym = gfc_find_dt_in_generic (sym);
    5081          513 :           ts->u.derived = sym;
    5082              :         }
    5083              :       return MATCH_YES;
    5084              :     }
    5085              : 
    5086              :   /* Search for the name but allow the components to be defined later.  If
    5087              :      type = -1, this typespec has been seen in a function declaration but
    5088              :      the type could not be accessed at that point.  The actual derived type is
    5089              :      stored in a symtree with the first letter of the name capitalized; the
    5090              :      symtree with the all lower-case name contains the associated
    5091              :      generic function.  */
    5092        34571 :   dt_name = gfc_dt_upper_string (name);
    5093        34571 :   sym = NULL;
    5094        34571 :   dt_sym = NULL;
    5095        34571 :   if (ts->kind != -1)
    5096              :     {
    5097        33358 :       gfc_get_ha_symbol (name, &sym);
    5098        33358 :       if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
    5099              :         {
    5100            0 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    5101            0 :           return MATCH_ERROR;
    5102              :         }
    5103        33358 :       if (sym->generic && !dt_sym)
    5104        14620 :         dt_sym = gfc_find_dt_in_generic (sym);
    5105              : 
    5106              :       /* Host associated PDTs can get confused with their constructors
    5107              :          because they are instantiated in the template's namespace.  */
    5108        33358 :       if (!dt_sym)
    5109              :         {
    5110         1052 :           if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
    5111              :             {
    5112            0 :               gfc_error ("Type name %qs at %C is ambiguous", name);
    5113            0 :               return MATCH_ERROR;
    5114              :             }
    5115         1052 :           if (dt_sym && !dt_sym->attr.pdt_type)
    5116            0 :             dt_sym = NULL;
    5117              :         }
    5118              :     }
    5119         1213 :   else if (ts->kind == -1)
    5120              :     {
    5121         2426 :       int iface = gfc_state_stack->previous->state != COMP_INTERFACE
    5122         1213 :                     || gfc_current_ns->has_import_set;
    5123         1213 :       gfc_find_symbol (name, NULL, iface, &sym);
    5124         1213 :       if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
    5125              :         {
    5126            0 :           gfc_error ("Type name %qs at %C is ambiguous", name);
    5127            0 :           return MATCH_ERROR;
    5128              :         }
    5129         1213 :       if (sym && sym->generic && !dt_sym)
    5130            2 :         dt_sym = gfc_find_dt_in_generic (sym);
    5131              : 
    5132         1213 :       ts->kind = 0;
    5133         1213 :       if (sym == NULL)
    5134              :         return MATCH_NO;
    5135              :     }
    5136              : 
    5137        34554 :   if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
    5138        33736 :        && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
    5139        34552 :       || sym->attr.subroutine)
    5140              :     {
    5141            2 :       gfc_error ("Type name %qs at %C conflicts with previously declared "
    5142              :                  "entity at %L, which has the same name", name,
    5143              :                  &sym->declared_at);
    5144            2 :       return MATCH_ERROR;
    5145              :     }
    5146              : 
    5147        34552 :   if (dt_sym && decl_type_param_list
    5148         1012 :       && dt_sym->attr.flavor == FL_DERIVED
    5149         1012 :       && !dt_sym->attr.pdt_type
    5150          250 :       && !dt_sym->attr.pdt_template)
    5151              :     {
    5152            1 :       gfc_error ("Type %qs is not parameterized and so the type parameter spec "
    5153              :                  "list at %C may not appear", dt_sym->name);
    5154            1 :       return MATCH_ERROR;
    5155              :     }
    5156              : 
    5157        34551 :   if (sym && sym->attr.flavor == FL_DERIVED
    5158              :       && sym->attr.pdt_template
    5159              :       && gfc_current_state () != COMP_DERIVED)
    5160              :     {
    5161              :       m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
    5162              :       if (m != MATCH_YES)
    5163              :         return m;
    5164              :       gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
    5165              :       ts->u.derived = sym;
    5166              :       strcpy (name, gfc_dt_lower_string (sym->name));
    5167              :     }
    5168              : 
    5169        34551 :   gfc_save_symbol_data (sym);
    5170        34551 :   gfc_set_sym_referenced (sym);
    5171        34551 :   if (!sym->attr.generic
    5172        34551 :       && !gfc_add_generic (&sym->attr, sym->name, NULL))
    5173              :     return MATCH_ERROR;
    5174              : 
    5175        34551 :   if (!sym->attr.function
    5176        34551 :       && !gfc_add_function (&sym->attr, sym->name, NULL))
    5177              :     return MATCH_ERROR;
    5178              : 
    5179        34551 :   if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
    5180        34419 :       && dt_sym->attr.pdt_template
    5181          260 :       && gfc_current_state () != COMP_DERIVED)
    5182              :     {
    5183          133 :       m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
    5184          133 :       if (m != MATCH_YES)
    5185              :         return m;
    5186          133 :       gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
    5187              :     }
    5188              : 
    5189        34551 :   if (!dt_sym)
    5190              :     {
    5191          132 :       gfc_interface *intr, *head;
    5192              : 
    5193              :       /* Use upper case to save the actual derived-type symbol.  */
    5194          132 :       gfc_get_symbol (dt_name, NULL, &dt_sym);
    5195          132 :       dt_sym->name = gfc_get_string ("%s", sym->name);
    5196          132 :       head = sym->generic;
    5197          132 :       intr = gfc_get_interface ();
    5198          132 :       intr->sym = dt_sym;
    5199          132 :       intr->where = gfc_current_locus;
    5200          132 :       intr->next = head;
    5201          132 :       sym->generic = intr;
    5202          132 :       sym->attr.if_source = IFSRC_DECL;
    5203              :     }
    5204              :   else
    5205        34419 :     gfc_save_symbol_data (dt_sym);
    5206              : 
    5207        34551 :   gfc_set_sym_referenced (dt_sym);
    5208              : 
    5209          132 :   if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
    5210        34683 :       && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
    5211              :     return MATCH_ERROR;
    5212              : 
    5213        34551 :   ts->u.derived = dt_sym;
    5214              : 
    5215        34551 :   return MATCH_YES;
    5216              : 
    5217       161141 : get_kind:
    5218       161141 :   if (matched_type
    5219       161141 :       && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
    5220              :                           "intrinsic-type-spec at %C"))
    5221              :     return MATCH_ERROR;
    5222              : 
    5223              :   /* For all types except double, derived and character, look for an
    5224              :      optional kind specifier.  MATCH_NO is actually OK at this point.  */
    5225       161138 :   if (implicit_flag == 1)
    5226              :     {
    5227          223 :         if (matched_type && gfc_match_char (')') != MATCH_YES)
    5228              :           return MATCH_ERROR;
    5229              : 
    5230              :         return MATCH_YES;
    5231              :     }
    5232              : 
    5233       160915 :   if (gfc_current_form == FORM_FREE)
    5234              :     {
    5235       145700 :       c = gfc_peek_ascii_char ();
    5236       145700 :       if (!gfc_is_whitespace (c) && c != '*' && c != '('
    5237        72037 :           && c != ':' && c != ',')
    5238              :         {
    5239          167 :           if (matched_type && c == ')')
    5240              :             {
    5241            3 :               gfc_next_ascii_char ();
    5242            3 :               return MATCH_YES;
    5243              :             }
    5244          164 :           gfc_error ("Malformed type-spec at %C");
    5245          164 :           return MATCH_NO;
    5246              :         }
    5247              :     }
    5248              : 
    5249       160748 :   m = gfc_match_kind_spec (ts, false);
    5250       160748 :   if (m == MATCH_ERROR)
    5251              :     return MATCH_ERROR;
    5252              : 
    5253       160712 :   if (m == MATCH_NO && ts->type != BT_CHARACTER)
    5254              :     {
    5255       109206 :       m = gfc_match_old_kind_spec (ts);
    5256       109206 :       if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
    5257              :          return MATCH_ERROR;
    5258              :     }
    5259              : 
    5260       160704 :   if (matched_type && gfc_match_char (')') != MATCH_YES)
    5261              :     {
    5262            0 :       gfc_error ("Malformed type-spec at %C");
    5263            0 :       return MATCH_ERROR;
    5264              :     }
    5265              : 
    5266              :   /* Defer association of the KIND expression of function results
    5267              :      until after USE and IMPORT statements.  */
    5268         4450 :   if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
    5269       165127 :          || gfc_matching_function)
    5270              :     return MATCH_YES;
    5271              : 
    5272       153414 :   if (m == MATCH_NO)
    5273       154495 :     m = MATCH_YES;              /* No kind specifier found.  */
    5274              : 
    5275              :   return m;
    5276              : }
    5277              : 
    5278              : 
    5279              : /* Match an IMPLICIT NONE statement.  Actually, this statement is
    5280              :    already matched in parse.cc, or we would not end up here in the
    5281              :    first place.  So the only thing we need to check, is if there is
    5282              :    trailing garbage.  If not, the match is successful.  */
    5283              : 
    5284              : match
    5285        24488 : gfc_match_implicit_none (void)
    5286              : {
    5287        24488 :   char c;
    5288        24488 :   match m;
    5289        24488 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    5290        24488 :   bool type = false;
    5291        24488 :   bool external = false;
    5292        24488 :   locus cur_loc = gfc_current_locus;
    5293              : 
    5294        24488 :   if (gfc_current_ns->seen_implicit_none
    5295        24486 :       || gfc_current_ns->has_implicit_none_export)
    5296              :     {
    5297            4 :       gfc_error ("Duplicate IMPLICIT NONE statement at %C");
    5298            4 :       return MATCH_ERROR;
    5299              :     }
    5300              : 
    5301        24484 :   gfc_gobble_whitespace ();
    5302        24484 :   c = gfc_peek_ascii_char ();
    5303        24484 :   if (c == '(')
    5304              :     {
    5305         1109 :       (void) gfc_next_ascii_char ();
    5306         1109 :       if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
    5307              :         return MATCH_ERROR;
    5308              : 
    5309         1108 :       gfc_gobble_whitespace ();
    5310         1108 :       if (gfc_peek_ascii_char () == ')')
    5311              :         {
    5312            1 :           (void) gfc_next_ascii_char ();
    5313            1 :           type = true;
    5314              :         }
    5315              :       else
    5316         3297 :         for(;;)
    5317              :           {
    5318         2202 :             m = gfc_match (" %n", name);
    5319         2202 :             if (m != MATCH_YES)
    5320              :               return MATCH_ERROR;
    5321              : 
    5322         2202 :             if (strcmp (name, "type") == 0)
    5323              :               type = true;
    5324         1107 :             else if (strcmp (name, "external") == 0)
    5325              :               external = true;
    5326              :             else
    5327              :               return MATCH_ERROR;
    5328              : 
    5329         2202 :             gfc_gobble_whitespace ();
    5330         2202 :             c = gfc_next_ascii_char ();
    5331         2202 :             if (c == ',')
    5332         1095 :               continue;
    5333         1107 :             if (c == ')')
    5334              :               break;
    5335              :             return MATCH_ERROR;
    5336              :           }
    5337              :     }
    5338              :   else
    5339              :     type = true;
    5340              : 
    5341        24483 :   if (gfc_match_eos () != MATCH_YES)
    5342              :     return MATCH_ERROR;
    5343              : 
    5344        24483 :   gfc_set_implicit_none (type, external, &cur_loc);
    5345              : 
    5346        24483 :   return MATCH_YES;
    5347              : }
    5348              : 
    5349              : 
    5350              : /* Match the letter range(s) of an IMPLICIT statement.  */
    5351              : 
    5352              : static match
    5353          600 : match_implicit_range (void)
    5354              : {
    5355          600 :   char c, c1, c2;
    5356          600 :   int inner;
    5357          600 :   locus cur_loc;
    5358              : 
    5359          600 :   cur_loc = gfc_current_locus;
    5360              : 
    5361          600 :   gfc_gobble_whitespace ();
    5362          600 :   c = gfc_next_ascii_char ();
    5363          600 :   if (c != '(')
    5364              :     {
    5365           59 :       gfc_error ("Missing character range in IMPLICIT at %C");
    5366           59 :       goto bad;
    5367              :     }
    5368              : 
    5369              :   inner = 1;
    5370         1195 :   while (inner)
    5371              :     {
    5372          722 :       gfc_gobble_whitespace ();
    5373          722 :       c1 = gfc_next_ascii_char ();
    5374          722 :       if (!ISALPHA (c1))
    5375           33 :         goto bad;
    5376              : 
    5377          689 :       gfc_gobble_whitespace ();
    5378          689 :       c = gfc_next_ascii_char ();
    5379              : 
    5380          689 :       switch (c)
    5381              :         {
    5382          201 :         case ')':
    5383          201 :           inner = 0;            /* Fall through.  */
    5384              : 
    5385              :         case ',':
    5386              :           c2 = c1;
    5387              :           break;
    5388              : 
    5389          439 :         case '-':
    5390          439 :           gfc_gobble_whitespace ();
    5391          439 :           c2 = gfc_next_ascii_char ();
    5392          439 :           if (!ISALPHA (c2))
    5393            0 :             goto bad;
    5394              : 
    5395          439 :           gfc_gobble_whitespace ();
    5396          439 :           c = gfc_next_ascii_char ();
    5397              : 
    5398          439 :           if ((c != ',') && (c != ')'))
    5399            0 :             goto bad;
    5400          439 :           if (c == ')')
    5401          272 :             inner = 0;
    5402              : 
    5403              :           break;
    5404              : 
    5405           35 :         default:
    5406           35 :           goto bad;
    5407              :         }
    5408              : 
    5409          654 :       if (c1 > c2)
    5410              :         {
    5411            0 :           gfc_error ("Letters must be in alphabetic order in "
    5412              :                      "IMPLICIT statement at %C");
    5413            0 :           goto bad;
    5414              :         }
    5415              : 
    5416              :       /* See if we can add the newly matched range to the pending
    5417              :          implicits from this IMPLICIT statement.  We do not check for
    5418              :          conflicts with whatever earlier IMPLICIT statements may have
    5419              :          set.  This is done when we've successfully finished matching
    5420              :          the current one.  */
    5421          654 :       if (!gfc_add_new_implicit_range (c1, c2))
    5422            0 :         goto bad;
    5423              :     }
    5424              : 
    5425              :   return MATCH_YES;
    5426              : 
    5427          127 : bad:
    5428          127 :   gfc_syntax_error (ST_IMPLICIT);
    5429              : 
    5430          127 :   gfc_current_locus = cur_loc;
    5431          127 :   return MATCH_ERROR;
    5432              : }
    5433              : 
    5434              : 
    5435              : /* Match an IMPLICIT statement, storing the types for
    5436              :    gfc_set_implicit() if the statement is accepted by the parser.
    5437              :    There is a strange looking, but legal syntactic construction
    5438              :    possible.  It looks like:
    5439              : 
    5440              :      IMPLICIT INTEGER (a-b) (c-d)
    5441              : 
    5442              :    This is legal if "a-b" is a constant expression that happens to
    5443              :    equal one of the legal kinds for integers.  The real problem
    5444              :    happens with an implicit specification that looks like:
    5445              : 
    5446              :      IMPLICIT INTEGER (a-b)
    5447              : 
    5448              :    In this case, a typespec matcher that is "greedy" (as most of the
    5449              :    matchers are) gobbles the character range as a kindspec, leaving
    5450              :    nothing left.  We therefore have to go a bit more slowly in the
    5451              :    matching process by inhibiting the kindspec checking during
    5452              :    typespec matching and checking for a kind later.  */
    5453              : 
    5454              : match
    5455        24914 : gfc_match_implicit (void)
    5456              : {
    5457        24914 :   gfc_typespec ts;
    5458        24914 :   locus cur_loc;
    5459        24914 :   char c;
    5460        24914 :   match m;
    5461              : 
    5462        24914 :   if (gfc_current_ns->seen_implicit_none)
    5463              :     {
    5464            4 :       gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
    5465              :                  "statement");
    5466            4 :       return MATCH_ERROR;
    5467              :     }
    5468              : 
    5469        24910 :   gfc_clear_ts (&ts);
    5470              : 
    5471              :   /* We don't allow empty implicit statements.  */
    5472        24910 :   if (gfc_match_eos () == MATCH_YES)
    5473              :     {
    5474            0 :       gfc_error ("Empty IMPLICIT statement at %C");
    5475            0 :       return MATCH_ERROR;
    5476              :     }
    5477              : 
    5478        24939 :   do
    5479              :     {
    5480              :       /* First cleanup.  */
    5481        24939 :       gfc_clear_new_implicit ();
    5482              : 
    5483              :       /* A basic type is mandatory here.  */
    5484        24939 :       m = gfc_match_decl_type_spec (&ts, 1);
    5485        24939 :       if (m == MATCH_ERROR)
    5486            0 :         goto error;
    5487        24939 :       if (m == MATCH_NO)
    5488        24486 :         goto syntax;
    5489              : 
    5490          453 :       cur_loc = gfc_current_locus;
    5491          453 :       m = match_implicit_range ();
    5492              : 
    5493          453 :       if (m == MATCH_YES)
    5494              :         {
    5495              :           /* We may have <TYPE> (<RANGE>).  */
    5496          326 :           gfc_gobble_whitespace ();
    5497          326 :           c = gfc_peek_ascii_char ();
    5498          326 :           if (c == ',' || c == '\n' || c == ';' || c == '!')
    5499              :             {
    5500              :               /* Check for CHARACTER with no length parameter.  */
    5501          299 :               if (ts.type == BT_CHARACTER && !ts.u.cl)
    5502              :                 {
    5503           32 :                   ts.kind = gfc_default_character_kind;
    5504           32 :                   ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5505           32 :                   ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    5506              :                                                       NULL, 1);
    5507              :                 }
    5508              : 
    5509              :               /* Record the Successful match.  */
    5510          299 :               if (!gfc_merge_new_implicit (&ts))
    5511              :                 return MATCH_ERROR;
    5512          297 :               if (c == ',')
    5513           28 :                 c = gfc_next_ascii_char ();
    5514          269 :               else if (gfc_match_eos () == MATCH_ERROR)
    5515            0 :                 goto error;
    5516          297 :               continue;
    5517              :             }
    5518              : 
    5519           27 :           gfc_current_locus = cur_loc;
    5520              :         }
    5521              : 
    5522              :       /* Discard the (incorrectly) matched range.  */
    5523          154 :       gfc_clear_new_implicit ();
    5524              : 
    5525              :       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
    5526          154 :       if (ts.type == BT_CHARACTER)
    5527           74 :         m = gfc_match_char_spec (&ts);
    5528           80 :       else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
    5529              :         {
    5530           76 :           m = gfc_match_kind_spec (&ts, false);
    5531           76 :           if (m == MATCH_NO)
    5532              :             {
    5533           40 :               m = gfc_match_old_kind_spec (&ts);
    5534           40 :               if (m == MATCH_ERROR)
    5535            0 :                 goto error;
    5536           40 :               if (m == MATCH_NO)
    5537            0 :                 goto syntax;
    5538              :             }
    5539              :         }
    5540          154 :       if (m == MATCH_ERROR)
    5541            7 :         goto error;
    5542              : 
    5543          147 :       m = match_implicit_range ();
    5544          147 :       if (m == MATCH_ERROR)
    5545            0 :         goto error;
    5546          147 :       if (m == MATCH_NO)
    5547              :         goto syntax;
    5548              : 
    5549          147 :       gfc_gobble_whitespace ();
    5550          147 :       c = gfc_next_ascii_char ();
    5551          147 :       if (c != ',' && gfc_match_eos () != MATCH_YES)
    5552            0 :         goto syntax;
    5553              : 
    5554          147 :       if (!gfc_merge_new_implicit (&ts))
    5555              :         return MATCH_ERROR;
    5556              :     }
    5557          444 :   while (c == ',');
    5558              : 
    5559              :   return MATCH_YES;
    5560              : 
    5561        24486 : syntax:
    5562        24486 :   gfc_syntax_error (ST_IMPLICIT);
    5563              : 
    5564        24914 : error:
    5565              :   return MATCH_ERROR;
    5566              : }
    5567              : 
    5568              : 
    5569              : /* Match the IMPORT statement.  IMPORT was added to F2003 as
    5570              : 
    5571              :    R1209 import-stmt  is IMPORT [[ :: ] import-name-list ]
    5572              : 
    5573              :    C1210 (R1209) The IMPORT statement is allowed only in an interface-body.
    5574              : 
    5575              :    C1211 (R1209) Each import-name shall be the name of an entity in the
    5576              :                  host scoping unit.
    5577              : 
    5578              :    under the description of an interface block. Under F2008, IMPORT was
    5579              :    split out of the interface block description to 12.4.3.3 and C1210
    5580              :    became
    5581              : 
    5582              :    C1210 (R1209) The IMPORT statement is allowed only in an interface-body
    5583              :                  that is not a module procedure interface body.
    5584              : 
    5585              :    Finally, F2018, section 8.8, has changed the IMPORT statement to
    5586              : 
    5587              :    R867 import-stmt  is IMPORT [[ :: ] import-name-list ]
    5588              :                      or IMPORT, ONLY : import-name-list
    5589              :                      or IMPORT, NONE
    5590              :                      or IMPORT, ALL
    5591              : 
    5592              :    C896 (R867) An IMPORT statement shall not appear in the scoping unit of
    5593              :                 a main-program, external-subprogram, module, or block-data.
    5594              : 
    5595              :    C897 (R867) Each import-name shall be the name of an entity in the host
    5596              :                 scoping unit.
    5597              : 
    5598              :    C898  If any IMPORT statement in a scoping unit has an ONLY specifier,
    5599              :          all IMPORT statements in that scoping unit shall have an ONLY
    5600              :          specifier.
    5601              : 
    5602              :    C899  IMPORT, NONE shall not appear in the scoping unit of a submodule.
    5603              : 
    5604              :    C8100 If an IMPORT, NONE or IMPORT, ALL statement appears in a scoping
    5605              :          unit, no other IMPORT statement shall appear in that scoping unit.
    5606              : 
    5607              :    C8101 Within an interface body, an entity that is accessed by host
    5608              :          association shall be accessible by host or use association within
    5609              :          the host scoping unit, or explicitly declared prior to the interface
    5610              :          body.
    5611              : 
    5612              :    C8102 An entity whose name appears as an import-name or which is made
    5613              :          accessible by an IMPORT, ALL statement shall not appear in any
    5614              :          context described in 19.5.1.4 that would cause the host entity
    5615              :          of that name to be inaccessible.  */
    5616              : 
    5617              : match
    5618         4032 : gfc_match_import (void)
    5619              : {
    5620         4032 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    5621         4032 :   match m;
    5622         4032 :   gfc_symbol *sym;
    5623         4032 :   gfc_symtree *st;
    5624         4032 :   bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;;
    5625         4032 :   importstate current_import_state = gfc_current_ns->import_state;
    5626              : 
    5627         4032 :   if (!f2018_allowed
    5628           13 :       && (gfc_current_ns->proc_name == NULL
    5629           12 :           || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY))
    5630              :     {
    5631            3 :       gfc_error ("IMPORT statement at %C only permitted in "
    5632              :                  "an INTERFACE body");
    5633            3 :       return MATCH_ERROR;
    5634              :     }
    5635              :   else if (f2018_allowed
    5636         4019 :            && (!gfc_current_ns->parent || gfc_current_ns->is_block_data))
    5637            4 :     goto C897;
    5638              : 
    5639         4015 :   if (f2018_allowed
    5640         4015 :       && (current_import_state == IMPORT_ALL
    5641         4015 :           || current_import_state == IMPORT_NONE))
    5642            2 :     goto C8100;
    5643              : 
    5644         4023 :   if (gfc_current_ns->proc_name
    5645         4022 :       && gfc_current_ns->proc_name->attr.module_procedure)
    5646              :     {
    5647            1 :       gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
    5648              :                  "in a module procedure interface body");
    5649            1 :       return MATCH_ERROR;
    5650              :     }
    5651              : 
    5652         4022 :   if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
    5653              :     return MATCH_ERROR;
    5654              : 
    5655         4018 :   gfc_current_ns->import_state = IMPORT_NOT_SET;
    5656         4018 :   if (f2018_allowed)
    5657              :     {
    5658         4012 :       if (gfc_match (" , none") == MATCH_YES)
    5659              :         {
    5660            8 :           if (current_import_state == IMPORT_ONLY)
    5661            0 :             goto C898;
    5662            8 :           if (gfc_current_state () == COMP_SUBMODULE)
    5663            0 :             goto C899;
    5664            8 :           gfc_current_ns->import_state = IMPORT_NONE;
    5665              :         }
    5666         4004 :       else if (gfc_match (" , only :") == MATCH_YES)
    5667              :         {
    5668           19 :           if (current_import_state != IMPORT_NOT_SET
    5669           19 :               && current_import_state != IMPORT_ONLY)
    5670            0 :             goto C898;
    5671           19 :           gfc_current_ns->import_state = IMPORT_ONLY;
    5672              :         }
    5673         3985 :       else if (gfc_match (" , all") == MATCH_YES)
    5674              :         {
    5675            1 :           if (current_import_state == IMPORT_ONLY)
    5676            0 :             goto C898;
    5677            1 :           gfc_current_ns->import_state = IMPORT_ALL;
    5678              :         }
    5679              : 
    5680         4012 :       if (current_import_state != IMPORT_NOT_SET
    5681            6 :           && (gfc_current_ns->import_state == IMPORT_NONE
    5682            6 :               || gfc_current_ns->import_state == IMPORT_ALL))
    5683            0 :         goto C8100;
    5684              :     }
    5685              : 
    5686              :   /* F2008 IMPORT<eos> is distinct from F2018 IMPORT, ALL.  */
    5687         4018 :   if (gfc_match_eos () == MATCH_YES)
    5688              :     {
    5689              :       /* This is the F2008 variant.  */
    5690          340 :       if (gfc_current_ns->import_state == IMPORT_NOT_SET)
    5691              :         {
    5692          331 :           if (current_import_state == IMPORT_ONLY)
    5693            0 :             goto C898;
    5694          331 :           gfc_current_ns->import_state = IMPORT_F2008;
    5695              :         }
    5696              : 
    5697              :       /* Host variables should be imported.  */
    5698          340 :       if (gfc_current_ns->import_state != IMPORT_NONE)
    5699          332 :         gfc_current_ns->has_import_set = 1;
    5700              :       return MATCH_YES;
    5701              :     }
    5702              : 
    5703         3678 :   if (gfc_match (" ::") == MATCH_YES
    5704         3678 :       && gfc_current_ns->import_state != IMPORT_ONLY)
    5705              :     {
    5706         1170 :       if (gfc_match_eos () == MATCH_YES)
    5707            1 :         goto expecting_list;
    5708         1169 :       gfc_current_ns->import_state = IMPORT_F2008;
    5709              :     }
    5710         2508 :   else if (gfc_current_ns->import_state == IMPORT_ONLY)
    5711              :     {
    5712           19 :       if (gfc_match_eos () == MATCH_YES)
    5713            0 :         goto expecting_list;
    5714              :     }
    5715              : 
    5716         4366 :   for(;;)
    5717              :     {
    5718         4366 :       sym = NULL;
    5719         4366 :       m = gfc_match (" %n", name);
    5720         4366 :       switch (m)
    5721              :         {
    5722         4366 :         case MATCH_YES:
    5723              :           /* Before checking if the symbol is available from host
    5724              :              association into a SUBROUTINE or FUNCTION within an
    5725              :              INTERFACE, check if it is already in local scope.  */
    5726         4366 :           gfc_find_symbol (name, gfc_current_ns, 1, &sym);
    5727         4366 :           if (sym
    5728           25 :               && gfc_state_stack->previous
    5729           25 :               && gfc_state_stack->previous->state == COMP_INTERFACE)
    5730              :             {
    5731            2 :                gfc_error ("import-name %qs at %C is in the "
    5732              :                           "local scope", name);
    5733            2 :                return MATCH_ERROR;
    5734              :             }
    5735              : 
    5736         4364 :           if (gfc_current_ns->parent != NULL
    5737         4364 :               && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
    5738              :             {
    5739            0 :                gfc_error ("Type name %qs at %C is ambiguous", name);
    5740            0 :                return MATCH_ERROR;
    5741              :             }
    5742         4364 :           else if (!sym
    5743            5 :                    && gfc_current_ns->proc_name
    5744            4 :                    && gfc_current_ns->proc_name->ns->parent
    5745         4365 :                    && gfc_find_symbol (name,
    5746              :                                        gfc_current_ns->proc_name->ns->parent,
    5747              :                                        1, &sym))
    5748              :             {
    5749            0 :                gfc_error ("Type name %qs at %C is ambiguous", name);
    5750            0 :                return MATCH_ERROR;
    5751              :             }
    5752              : 
    5753         4364 :           if (sym == NULL)
    5754              :             {
    5755            5 :               if (gfc_current_ns->proc_name
    5756            4 :                   && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
    5757              :                 {
    5758            1 :                   gfc_error ("Cannot IMPORT %qs from host scoping unit "
    5759              :                              "at %C - does not exist.", name);
    5760            1 :                   return MATCH_ERROR;
    5761              :                 }
    5762              :               else
    5763              :                 {
    5764              :                   /* This might be a procedure that has not yet been parsed. If
    5765              :                      so gfc_fixup_sibling_symbols will replace this symbol with
    5766              :                      that of the procedure.  */
    5767            4 :                   gfc_get_sym_tree (name, gfc_current_ns, &st, false,
    5768              :                                     &gfc_current_locus);
    5769            4 :                   st->n.sym->refs++;
    5770            4 :                   st->n.sym->attr.imported = 1;
    5771            4 :                   st->import_only = 1;
    5772            4 :                   goto next_item;
    5773              :                 }
    5774              :             }
    5775              : 
    5776         4359 :           st = gfc_find_symtree (gfc_current_ns->sym_root, name);
    5777         4359 :           if (st && st->n.sym && st->n.sym->attr.imported)
    5778              :             {
    5779            0 :               gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
    5780              :                            "at %C", name);
    5781            0 :               goto next_item;
    5782              :             }
    5783              : 
    5784         4359 :           st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
    5785         4359 :           st->n.sym = sym;
    5786         4359 :           sym->refs++;
    5787         4359 :           sym->attr.imported = 1;
    5788         4359 :           st->import_only = 1;
    5789              : 
    5790         4359 :           if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
    5791              :             {
    5792              :               /* The actual derived type is stored in a symtree with the first
    5793              :                  letter of the name capitalized; the symtree with the all
    5794              :                  lower-case name contains the associated generic function.  */
    5795          599 :               st = gfc_new_symtree (&gfc_current_ns->sym_root,
    5796              :                                     gfc_dt_upper_string (name));
    5797          599 :               st->n.sym = sym;
    5798          599 :               sym->refs++;
    5799          599 :               sym->attr.imported = 1;
    5800          599 :               st->import_only = 1;
    5801              :             }
    5802              : 
    5803         4359 :           goto next_item;
    5804              : 
    5805              :         case MATCH_NO:
    5806              :           break;
    5807              : 
    5808              :         case MATCH_ERROR:
    5809              :           return MATCH_ERROR;
    5810              :         }
    5811              : 
    5812         4363 :     next_item:
    5813         4363 :       if (gfc_match_eos () == MATCH_YES)
    5814              :         break;
    5815          689 :       if (gfc_match_char (',') != MATCH_YES)
    5816            0 :         goto syntax;
    5817              :     }
    5818              : 
    5819              :   return MATCH_YES;
    5820              : 
    5821            0 : syntax:
    5822            0 :   gfc_error ("Syntax error in IMPORT statement at %C");
    5823            0 :   return MATCH_ERROR;
    5824              : 
    5825            4 : C897:
    5826            4 :   gfc_error ("F2018: C897 IMPORT statement at %C cannot appear in a main "
    5827              :              "program, an external subprogram, a module or block data");
    5828            4 :   return MATCH_ERROR;
    5829              : 
    5830            0 : C898:
    5831            0 :   gfc_error ("F2018: C898 IMPORT statement at %C is not permitted because "
    5832              :              "a scoping unit has an ONLY specifier, can only have IMPORT "
    5833              :              "with an ONLY specifier");
    5834            0 :   return MATCH_ERROR;
    5835              : 
    5836            0 : C899:
    5837            0 :   gfc_error ("F2018: C899 IMPORT, NONE shall not appear in the scoping unit"
    5838              :              " of a submodule as at %C");
    5839            0 :   return MATCH_ERROR;
    5840              : 
    5841            2 : C8100:
    5842            4 :   gfc_error ("F2018: C8100 IMPORT statement at %C is not permitted because "
    5843              :              "%s has already been declared, which must be unique in the "
    5844              :              "scoping unit",
    5845            2 :              gfc_current_ns->import_state == IMPORT_ALL ? "IMPORT, ALL" :
    5846              :                                                           "IMPORT, NONE");
    5847            2 :   return MATCH_ERROR;
    5848              : 
    5849            1 : expecting_list:
    5850            1 :   gfc_error ("Expecting list of named entities at %C");
    5851            1 :   return MATCH_ERROR;
    5852              : }
    5853              : 
    5854              : 
    5855              : /* A minimal implementation of gfc_match without whitespace, escape
    5856              :    characters or variable arguments.  Returns true if the next
    5857              :    characters match the TARGET template exactly.  */
    5858              : 
    5859              : static bool
    5860       149308 : match_string_p (const char *target)
    5861              : {
    5862       149308 :   const char *p;
    5863              : 
    5864       936753 :   for (p = target; *p; p++)
    5865       787446 :     if ((char) gfc_next_ascii_char () != *p)
    5866              :       return false;
    5867              :   return true;
    5868              : }
    5869              : 
    5870              : /* Matches an attribute specification including array specs.  If
    5871              :    successful, leaves the variables current_attr and current_as
    5872              :    holding the specification.  Also sets the colon_seen variable for
    5873              :    later use by matchers associated with initializations.
    5874              : 
    5875              :    This subroutine is a little tricky in the sense that we don't know
    5876              :    if we really have an attr-spec until we hit the double colon.
    5877              :    Until that time, we can only return MATCH_NO.  This forces us to
    5878              :    check for duplicate specification at this level.  */
    5879              : 
    5880              : static match
    5881       220610 : match_attr_spec (void)
    5882              : {
    5883              :   /* Modifiers that can exist in a type statement.  */
    5884       220610 :   enum
    5885              :   { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
    5886              :     DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
    5887              :     DECL_DIMENSION, DECL_EXTERNAL,
    5888              :     DECL_INTRINSIC, DECL_OPTIONAL,
    5889              :     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
    5890              :     DECL_STATIC, DECL_AUTOMATIC,
    5891              :     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
    5892              :     DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
    5893              :     DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
    5894              :   };
    5895              : 
    5896              : /* GFC_DECL_END is the sentinel, index starts at 0.  */
    5897              : #define NUM_DECL GFC_DECL_END
    5898              : 
    5899              :   /* Make sure that values from sym_intent are safe to be used here.  */
    5900       220610 :   gcc_assert (INTENT_IN > 0);
    5901              : 
    5902       220610 :   locus start, seen_at[NUM_DECL];
    5903       220610 :   int seen[NUM_DECL];
    5904       220610 :   unsigned int d;
    5905       220610 :   const char *attr;
    5906       220610 :   match m;
    5907       220610 :   bool t;
    5908              : 
    5909       220610 :   gfc_clear_attr (&current_attr);
    5910       220610 :   start = gfc_current_locus;
    5911              : 
    5912       220610 :   current_as = NULL;
    5913       220610 :   colon_seen = 0;
    5914       220610 :   attr_seen = 0;
    5915              : 
    5916              :   /* See if we get all of the keywords up to the final double colon.  */
    5917      5956470 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    5918      5735860 :     seen[d] = 0;
    5919              : 
    5920       341599 :   for (;;)
    5921              :     {
    5922       341599 :       char ch;
    5923              : 
    5924       341599 :       d = DECL_NONE;
    5925       341599 :       gfc_gobble_whitespace ();
    5926              : 
    5927       341599 :       ch = gfc_next_ascii_char ();
    5928       341599 :       if (ch == ':')
    5929              :         {
    5930              :           /* This is the successful exit condition for the loop.  */
    5931       186755 :           if (gfc_next_ascii_char () == ':')
    5932              :             break;
    5933              :         }
    5934       154844 :       else if (ch == ',')
    5935              :         {
    5936       121001 :           gfc_gobble_whitespace ();
    5937       121001 :           switch (gfc_peek_ascii_char ())
    5938              :             {
    5939        18837 :             case 'a':
    5940        18837 :               gfc_next_ascii_char ();
    5941        18837 :               switch (gfc_next_ascii_char ())
    5942              :                 {
    5943        18771 :                 case 'l':
    5944        18771 :                   if (match_string_p ("locatable"))
    5945              :                     {
    5946              :                       /* Matched "allocatable".  */
    5947              :                       d = DECL_ALLOCATABLE;
    5948              :                     }
    5949              :                   break;
    5950              : 
    5951           25 :                 case 's':
    5952           25 :                   if (match_string_p ("ynchronous"))
    5953              :                     {
    5954              :                       /* Matched "asynchronous".  */
    5955              :                       d = DECL_ASYNCHRONOUS;
    5956              :                     }
    5957              :                   break;
    5958              : 
    5959           41 :                 case 'u':
    5960           41 :                   if (match_string_p ("tomatic"))
    5961              :                     {
    5962              :                       /* Matched "automatic".  */
    5963              :                       d = DECL_AUTOMATIC;
    5964              :                     }
    5965              :                   break;
    5966              :                 }
    5967              :               break;
    5968              : 
    5969          164 :             case 'b':
    5970              :               /* Try and match the bind(c).  */
    5971          164 :               m = gfc_match_bind_c (NULL, true);
    5972          164 :               if (m == MATCH_YES)
    5973              :                 d = DECL_IS_BIND_C;
    5974            0 :               else if (m == MATCH_ERROR)
    5975            0 :                 goto cleanup;
    5976              :               break;
    5977              : 
    5978         2164 :             case 'c':
    5979         2164 :               gfc_next_ascii_char ();
    5980         2164 :               if ('o' != gfc_next_ascii_char ())
    5981              :                 break;
    5982         2163 :               switch (gfc_next_ascii_char ())
    5983              :                 {
    5984           68 :                 case 'd':
    5985           68 :                   if (match_string_p ("imension"))
    5986              :                     {
    5987              :                       d = DECL_CODIMENSION;
    5988              :                       break;
    5989              :                     }
    5990              :                   /* FALLTHRU */
    5991         2095 :                 case 'n':
    5992         2095 :                   if (match_string_p ("tiguous"))
    5993              :                     {
    5994              :                       d = DECL_CONTIGUOUS;
    5995              :                       break;
    5996              :                     }
    5997              :                 }
    5998              :               break;
    5999              : 
    6000        19760 :             case 'd':
    6001        19760 :               if (match_string_p ("dimension"))
    6002              :                 d = DECL_DIMENSION;
    6003              :               break;
    6004              : 
    6005          177 :             case 'e':
    6006          177 :               if (match_string_p ("external"))
    6007              :                 d = DECL_EXTERNAL;
    6008              :               break;
    6009              : 
    6010        28472 :             case 'i':
    6011        28472 :               if (match_string_p ("int"))
    6012              :                 {
    6013        28472 :                   ch = gfc_next_ascii_char ();
    6014        28472 :                   if (ch == 'e')
    6015              :                     {
    6016        28466 :                       if (match_string_p ("nt"))
    6017              :                         {
    6018              :                           /* Matched "intent".  */
    6019        28465 :                           d = match_intent_spec ();
    6020        28465 :                           if (d == INTENT_UNKNOWN)
    6021              :                             {
    6022            2 :                               m = MATCH_ERROR;
    6023            2 :                               goto cleanup;
    6024              :                             }
    6025              :                         }
    6026              :                     }
    6027            6 :                   else if (ch == 'r')
    6028              :                     {
    6029            6 :                       if (match_string_p ("insic"))
    6030              :                         {
    6031              :                           /* Matched "intrinsic".  */
    6032              :                           d = DECL_INTRINSIC;
    6033              :                         }
    6034              :                     }
    6035              :                 }
    6036              :               break;
    6037              : 
    6038          353 :             case 'k':
    6039          353 :               if (match_string_p ("kind"))
    6040              :                 d = DECL_KIND;
    6041              :               break;
    6042              : 
    6043          331 :             case 'l':
    6044          331 :               if (match_string_p ("len"))
    6045              :                 d = DECL_LEN;
    6046              :               break;
    6047              : 
    6048         5205 :             case 'o':
    6049         5205 :               if (match_string_p ("optional"))
    6050              :                 d = DECL_OPTIONAL;
    6051              :               break;
    6052              : 
    6053        27374 :             case 'p':
    6054        27374 :               gfc_next_ascii_char ();
    6055        27374 :               switch (gfc_next_ascii_char ())
    6056              :                 {
    6057        14440 :                 case 'a':
    6058        14440 :                   if (match_string_p ("rameter"))
    6059              :                     {
    6060              :                       /* Matched "parameter".  */
    6061              :                       d = DECL_PARAMETER;
    6062              :                     }
    6063              :                   break;
    6064              : 
    6065        12413 :                 case 'o':
    6066        12413 :                   if (match_string_p ("inter"))
    6067              :                     {
    6068              :                       /* Matched "pointer".  */
    6069              :                       d = DECL_POINTER;
    6070              :                     }
    6071              :                   break;
    6072              : 
    6073          268 :                 case 'r':
    6074          268 :                   ch = gfc_next_ascii_char ();
    6075          268 :                   if (ch == 'i')
    6076              :                     {
    6077          217 :                       if (match_string_p ("vate"))
    6078              :                         {
    6079              :                           /* Matched "private".  */
    6080              :                           d = DECL_PRIVATE;
    6081              :                         }
    6082              :                     }
    6083           51 :                   else if (ch == 'o')
    6084              :                     {
    6085           51 :                       if (match_string_p ("tected"))
    6086              :                         {
    6087              :                           /* Matched "protected".  */
    6088              :                           d = DECL_PROTECTED;
    6089              :                         }
    6090              :                     }
    6091              :                   break;
    6092              : 
    6093          253 :                 case 'u':
    6094          253 :                   if (match_string_p ("blic"))
    6095              :                     {
    6096              :                       /* Matched "public".  */
    6097              :                       d = DECL_PUBLIC;
    6098              :                     }
    6099              :                   break;
    6100              :                 }
    6101              :               break;
    6102              : 
    6103         1223 :             case 's':
    6104         1223 :               gfc_next_ascii_char ();
    6105         1223 :               switch (gfc_next_ascii_char ())
    6106              :                 {
    6107         1210 :                   case 'a':
    6108         1210 :                     if (match_string_p ("ve"))
    6109              :                       {
    6110              :                         /* Matched "save".  */
    6111              :                         d = DECL_SAVE;
    6112              :                       }
    6113              :                     break;
    6114              : 
    6115           13 :                   case 't':
    6116           13 :                     if (match_string_p ("atic"))
    6117              :                       {
    6118              :                         /* Matched "static".  */
    6119              :                         d = DECL_STATIC;
    6120              :                       }
    6121              :                     break;
    6122              :                 }
    6123              :               break;
    6124              : 
    6125         5636 :             case 't':
    6126         5636 :               if (match_string_p ("target"))
    6127              :                 d = DECL_TARGET;
    6128              :               break;
    6129              : 
    6130        11305 :             case 'v':
    6131        11305 :               gfc_next_ascii_char ();
    6132        11305 :               ch = gfc_next_ascii_char ();
    6133        11305 :               if (ch == 'a')
    6134              :                 {
    6135        10789 :                   if (match_string_p ("lue"))
    6136              :                     {
    6137              :                       /* Matched "value".  */
    6138              :                       d = DECL_VALUE;
    6139              :                     }
    6140              :                 }
    6141          516 :               else if (ch == 'o')
    6142              :                 {
    6143          516 :                   if (match_string_p ("latile"))
    6144              :                     {
    6145              :                       /* Matched "volatile".  */
    6146              :                       d = DECL_VOLATILE;
    6147              :                     }
    6148              :                 }
    6149              :               break;
    6150              :             }
    6151              :         }
    6152              : 
    6153              :       /* No double colon and no recognizable decl_type, so assume that
    6154              :          we've been looking at something else the whole time.  */
    6155              :       if (d == DECL_NONE)
    6156              :         {
    6157        33846 :           m = MATCH_NO;
    6158        33846 :           goto cleanup;
    6159              :         }
    6160              : 
    6161              :       /* Check to make sure any parens are paired up correctly.  */
    6162       120997 :       if (gfc_match_parens () == MATCH_ERROR)
    6163              :         {
    6164            1 :           m = MATCH_ERROR;
    6165            1 :           goto cleanup;
    6166              :         }
    6167              : 
    6168       120996 :       seen[d]++;
    6169       120996 :       seen_at[d] = gfc_current_locus;
    6170              : 
    6171       120996 :       if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
    6172              :         {
    6173        19827 :           gfc_array_spec *as = NULL;
    6174              : 
    6175        19827 :           m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
    6176              :                                     d == DECL_CODIMENSION);
    6177              : 
    6178        19827 :           if (current_as == NULL)
    6179        19802 :             current_as = as;
    6180           25 :           else if (m == MATCH_YES)
    6181              :             {
    6182           25 :               if (!merge_array_spec (as, current_as, false))
    6183            2 :                 m = MATCH_ERROR;
    6184           25 :               free (as);
    6185              :             }
    6186              : 
    6187        19827 :           if (m == MATCH_NO)
    6188              :             {
    6189            0 :               if (d == DECL_CODIMENSION)
    6190            0 :                 gfc_error ("Missing codimension specification at %C");
    6191              :               else
    6192            0 :                 gfc_error ("Missing dimension specification at %C");
    6193              :               m = MATCH_ERROR;
    6194              :             }
    6195              : 
    6196        19827 :           if (m == MATCH_ERROR)
    6197            7 :             goto cleanup;
    6198              :         }
    6199              :     }
    6200              : 
    6201              :   /* Since we've seen a double colon, we have to be looking at an
    6202              :      attr-spec.  This means that we can now issue errors.  */
    6203      5042337 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    6204      4855585 :     if (seen[d] > 1)
    6205              :       {
    6206            2 :         switch (d)
    6207              :           {
    6208              :           case DECL_ALLOCATABLE:
    6209              :             attr = "ALLOCATABLE";
    6210              :             break;
    6211            0 :           case DECL_ASYNCHRONOUS:
    6212            0 :             attr = "ASYNCHRONOUS";
    6213            0 :             break;
    6214            0 :           case DECL_CODIMENSION:
    6215            0 :             attr = "CODIMENSION";
    6216            0 :             break;
    6217            0 :           case DECL_CONTIGUOUS:
    6218            0 :             attr = "CONTIGUOUS";
    6219            0 :             break;
    6220            0 :           case DECL_DIMENSION:
    6221            0 :             attr = "DIMENSION";
    6222            0 :             break;
    6223            0 :           case DECL_EXTERNAL:
    6224            0 :             attr = "EXTERNAL";
    6225            0 :             break;
    6226            0 :           case DECL_IN:
    6227            0 :             attr = "INTENT (IN)";
    6228            0 :             break;
    6229            0 :           case DECL_OUT:
    6230            0 :             attr = "INTENT (OUT)";
    6231            0 :             break;
    6232            0 :           case DECL_INOUT:
    6233            0 :             attr = "INTENT (IN OUT)";
    6234            0 :             break;
    6235            0 :           case DECL_INTRINSIC:
    6236            0 :             attr = "INTRINSIC";
    6237            0 :             break;
    6238            0 :           case DECL_OPTIONAL:
    6239            0 :             attr = "OPTIONAL";
    6240            0 :             break;
    6241            0 :           case DECL_KIND:
    6242            0 :             attr = "KIND";
    6243            0 :             break;
    6244            0 :           case DECL_LEN:
    6245            0 :             attr = "LEN";
    6246            0 :             break;
    6247            0 :           case DECL_PARAMETER:
    6248            0 :             attr = "PARAMETER";
    6249            0 :             break;
    6250            0 :           case DECL_POINTER:
    6251            0 :             attr = "POINTER";
    6252            0 :             break;
    6253            0 :           case DECL_PROTECTED:
    6254            0 :             attr = "PROTECTED";
    6255            0 :             break;
    6256            0 :           case DECL_PRIVATE:
    6257            0 :             attr = "PRIVATE";
    6258            0 :             break;
    6259            0 :           case DECL_PUBLIC:
    6260            0 :             attr = "PUBLIC";
    6261            0 :             break;
    6262            0 :           case DECL_SAVE:
    6263            0 :             attr = "SAVE";
    6264            0 :             break;
    6265            0 :           case DECL_STATIC:
    6266            0 :             attr = "STATIC";
    6267            0 :             break;
    6268            1 :           case DECL_AUTOMATIC:
    6269            1 :             attr = "AUTOMATIC";
    6270            1 :             break;
    6271            0 :           case DECL_TARGET:
    6272            0 :             attr = "TARGET";
    6273            0 :             break;
    6274            0 :           case DECL_IS_BIND_C:
    6275            0 :             attr = "IS_BIND_C";
    6276            0 :             break;
    6277            0 :           case DECL_VALUE:
    6278            0 :             attr = "VALUE";
    6279            0 :             break;
    6280            1 :           case DECL_VOLATILE:
    6281            1 :             attr = "VOLATILE";
    6282            1 :             break;
    6283            0 :           default:
    6284            0 :             attr = NULL;        /* This shouldn't happen.  */
    6285              :           }
    6286              : 
    6287            2 :         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
    6288            2 :         m = MATCH_ERROR;
    6289            2 :         goto cleanup;
    6290              :       }
    6291              : 
    6292              :   /* Now that we've dealt with duplicate attributes, add the attributes
    6293              :      to the current attribute.  */
    6294      5041517 :   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
    6295              :     {
    6296      4854838 :       if (seen[d] == 0)
    6297      4733858 :         continue;
    6298              :       else
    6299       120980 :         attr_seen = 1;
    6300              : 
    6301       120980 :       if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
    6302           52 :           && !flag_dec_static)
    6303              :         {
    6304            3 :           gfc_error ("%s at %L is a DEC extension, enable with "
    6305              :                      "%<-fdec-static%>",
    6306              :                      d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
    6307            2 :           m = MATCH_ERROR;
    6308            2 :           goto cleanup;
    6309              :         }
    6310              :       /* Allow SAVE with STATIC, but don't complain.  */
    6311           50 :       if (d == DECL_STATIC && seen[DECL_SAVE])
    6312            0 :         continue;
    6313              : 
    6314       120978 :       if (gfc_comp_struct (gfc_current_state ())
    6315         7085 :           && d != DECL_DIMENSION && d != DECL_CODIMENSION
    6316         6121 :           && d != DECL_POINTER   && d != DECL_PRIVATE
    6317         4431 :           && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
    6318              :         {
    6319         4374 :           bool is_derived = gfc_current_state () == COMP_DERIVED;
    6320         4374 :           if (d == DECL_ALLOCATABLE)
    6321              :             {
    6322         3677 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6323              :                                    ? G_("ALLOCATABLE attribute at %C in a "
    6324              :                                         "TYPE definition")
    6325              :                                    : G_("ALLOCATABLE attribute at %C in a "
    6326              :                                         "STRUCTURE definition")))
    6327              :                 {
    6328            2 :                   m = MATCH_ERROR;
    6329            2 :                   goto cleanup;
    6330              :                 }
    6331              :             }
    6332          697 :           else if (d == DECL_KIND)
    6333              :             {
    6334          351 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6335              :                                    ? G_("KIND attribute at %C in a "
    6336              :                                         "TYPE definition")
    6337              :                                    : G_("KIND attribute at %C in a "
    6338              :                                         "STRUCTURE definition")))
    6339              :                 {
    6340            1 :                   m = MATCH_ERROR;
    6341            1 :                   goto cleanup;
    6342              :                 }
    6343          350 :               if (current_ts.type != BT_INTEGER)
    6344              :                 {
    6345            2 :                   gfc_error ("Component with KIND attribute at %C must be "
    6346              :                              "INTEGER");
    6347            2 :                   m = MATCH_ERROR;
    6348            2 :                   goto cleanup;
    6349              :                 }
    6350              :             }
    6351          346 :           else if (d == DECL_LEN)
    6352              :             {
    6353          330 :               if (!gfc_notify_std (GFC_STD_F2003, is_derived
    6354              :                                    ? G_("LEN attribute at %C in a "
    6355              :                                         "TYPE definition")
    6356              :                                    : G_("LEN attribute at %C in a "
    6357              :                                         "STRUCTURE definition")))
    6358              :                 {
    6359            0 :                   m = MATCH_ERROR;
    6360            0 :                   goto cleanup;
    6361              :                 }
    6362          330 :               if (current_ts.type != BT_INTEGER)
    6363              :                 {
    6364            1 :                   gfc_error ("Component with LEN attribute at %C must be "
    6365              :                              "INTEGER");
    6366            1 :                   m = MATCH_ERROR;
    6367            1 :                   goto cleanup;
    6368              :                 }
    6369              :             }
    6370              :           else
    6371              :             {
    6372           32 :               gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
    6373              :                                          "TYPE definition")
    6374              :                                     : G_("Attribute at %L is not allowed in a "
    6375              :                                          "STRUCTURE definition"), &seen_at[d]);
    6376           16 :               m = MATCH_ERROR;
    6377           16 :               goto cleanup;
    6378              :             }
    6379              :         }
    6380              : 
    6381       120956 :       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
    6382          470 :           && gfc_current_state () != COMP_MODULE)
    6383              :         {
    6384          147 :           if (d == DECL_PRIVATE)
    6385              :             attr = "PRIVATE";
    6386              :           else
    6387           43 :             attr = "PUBLIC";
    6388          147 :           if (gfc_current_state () == COMP_DERIVED
    6389          141 :               && gfc_state_stack->previous
    6390          141 :               && gfc_state_stack->previous->state == COMP_MODULE)
    6391              :             {
    6392          138 :               if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
    6393              :                                    "at %L in a TYPE definition", attr,
    6394              :                                    &seen_at[d]))
    6395              :                 {
    6396            2 :                   m = MATCH_ERROR;
    6397            2 :                   goto cleanup;
    6398              :                 }
    6399              :             }
    6400              :           else
    6401              :             {
    6402            9 :               gfc_error ("%s attribute at %L is not allowed outside of the "
    6403              :                          "specification part of a module", attr, &seen_at[d]);
    6404            9 :               m = MATCH_ERROR;
    6405            9 :               goto cleanup;
    6406              :             }
    6407              :         }
    6408              : 
    6409       120945 :       if (gfc_current_state () != COMP_DERIVED
    6410       113891 :           && (d == DECL_KIND || d == DECL_LEN))
    6411              :         {
    6412            3 :           gfc_error ("Attribute at %L is not allowed outside a TYPE "
    6413              :                      "definition", &seen_at[d]);
    6414            3 :           m = MATCH_ERROR;
    6415            3 :           goto cleanup;
    6416              :         }
    6417              : 
    6418       120942 :       switch (d)
    6419              :         {
    6420        18769 :         case DECL_ALLOCATABLE:
    6421        18769 :           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
    6422        18769 :           break;
    6423              : 
    6424           24 :         case DECL_ASYNCHRONOUS:
    6425           24 :           if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
    6426              :             t = false;
    6427              :           else
    6428           24 :             t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
    6429              :           break;
    6430              : 
    6431           66 :         case DECL_CODIMENSION:
    6432           66 :           t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
    6433           66 :           break;
    6434              : 
    6435         2095 :         case DECL_CONTIGUOUS:
    6436         2095 :           if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
    6437              :             t = false;
    6438              :           else
    6439         2094 :             t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
    6440              :           break;
    6441              : 
    6442        19752 :         case DECL_DIMENSION:
    6443        19752 :           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
    6444        19752 :           break;
    6445              : 
    6446          176 :         case DECL_EXTERNAL:
    6447          176 :           t = gfc_add_external (&current_attr, &seen_at[d]);
    6448          176 :           break;
    6449              : 
    6450        21534 :         case DECL_IN:
    6451        21534 :           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
    6452        21534 :           break;
    6453              : 
    6454         3748 :         case DECL_OUT:
    6455         3748 :           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
    6456         3748 :           break;
    6457              : 
    6458         3177 :         case DECL_INOUT:
    6459         3177 :           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
    6460         3177 :           break;
    6461              : 
    6462            5 :         case DECL_INTRINSIC:
    6463            5 :           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
    6464            5 :           break;
    6465              : 
    6466         5204 :         case DECL_OPTIONAL:
    6467         5204 :           t = gfc_add_optional (&current_attr, &seen_at[d]);
    6468         5204 :           break;
    6469              : 
    6470          348 :         case DECL_KIND:
    6471          348 :           t = gfc_add_kind (&current_attr, &seen_at[d]);
    6472          348 :           break;
    6473              : 
    6474          329 :         case DECL_LEN:
    6475          329 :           t = gfc_add_len (&current_attr, &seen_at[d]);
    6476          329 :           break;
    6477              : 
    6478        14439 :         case DECL_PARAMETER:
    6479        14439 :           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
    6480        14439 :           break;
    6481              : 
    6482        12412 :         case DECL_POINTER:
    6483        12412 :           t = gfc_add_pointer (&current_attr, &seen_at[d]);
    6484        12412 :           break;
    6485              : 
    6486           50 :         case DECL_PROTECTED:
    6487           50 :           if (gfc_current_state () != COMP_MODULE
    6488           48 :               || (gfc_current_ns->proc_name
    6489           48 :                   && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
    6490              :             {
    6491            2 :                gfc_error ("PROTECTED at %C only allowed in specification "
    6492              :                           "part of a module");
    6493            2 :                t = false;
    6494            2 :                break;
    6495              :             }
    6496              : 
    6497           48 :           if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
    6498              :             t = false;
    6499              :           else
    6500           44 :             t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
    6501              :           break;
    6502              : 
    6503          214 :         case DECL_PRIVATE:
    6504          214 :           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
    6505              :                               &seen_at[d]);
    6506          214 :           break;
    6507              : 
    6508          245 :         case DECL_PUBLIC:
    6509          245 :           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
    6510              :                               &seen_at[d]);
    6511          245 :           break;
    6512              : 
    6513         1220 :         case DECL_STATIC:
    6514         1220 :         case DECL_SAVE:
    6515         1220 :           t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
    6516         1220 :           break;
    6517              : 
    6518           37 :         case DECL_AUTOMATIC:
    6519           37 :           t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
    6520           37 :           break;
    6521              : 
    6522         5634 :         case DECL_TARGET:
    6523         5634 :           t = gfc_add_target (&current_attr, &seen_at[d]);
    6524         5634 :           break;
    6525              : 
    6526          163 :         case DECL_IS_BIND_C:
    6527          163 :            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
    6528          163 :            break;
    6529              : 
    6530        10788 :         case DECL_VALUE:
    6531        10788 :           if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
    6532              :             t = false;
    6533              :           else
    6534        10788 :             t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
    6535              :           break;
    6536              : 
    6537          513 :         case DECL_VOLATILE:
    6538          513 :           if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
    6539              :             t = false;
    6540              :           else
    6541          512 :             t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
    6542              :           break;
    6543              : 
    6544            0 :         default:
    6545            0 :           gfc_internal_error ("match_attr_spec(): Bad attribute");
    6546              :         }
    6547              : 
    6548       120936 :       if (!t)
    6549              :         {
    6550           35 :           m = MATCH_ERROR;
    6551           35 :           goto cleanup;
    6552              :         }
    6553              :     }
    6554              : 
    6555              :   /* Since Fortran 2008 module variables implicitly have the SAVE attribute.  */
    6556       186679 :   if ((gfc_current_state () == COMP_MODULE
    6557       186679 :        || gfc_current_state () == COMP_SUBMODULE)
    6558         5988 :       && !current_attr.save
    6559         5806 :       && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    6560         5714 :     current_attr.save = SAVE_IMPLICIT;
    6561              : 
    6562       186679 :   colon_seen = 1;
    6563       186679 :   return MATCH_YES;
    6564              : 
    6565        33931 : cleanup:
    6566        33931 :   gfc_current_locus = start;
    6567        33931 :   gfc_free_array_spec (current_as);
    6568        33931 :   current_as = NULL;
    6569        33931 :   attr_seen = 0;
    6570        33931 :   return m;
    6571              : }
    6572              : 
    6573              : 
    6574              : /* Set the binding label, dest_label, either with the binding label
    6575              :    stored in the given gfc_typespec, ts, or if none was provided, it
    6576              :    will be the symbol name in all lower case, as required by the draft
    6577              :    (J3/04-007, section 15.4.1).  If a binding label was given and
    6578              :    there is more than one argument (num_idents), it is an error.  */
    6579              : 
    6580              : static bool
    6581          347 : set_binding_label (const char **dest_label, const char *sym_name,
    6582              :                    int num_idents)
    6583              : {
    6584          347 :   if (num_idents > 1 && has_name_equals)
    6585              :     {
    6586            4 :       gfc_error ("Multiple identifiers provided with "
    6587              :                  "single NAME= specifier at %C");
    6588            4 :       return false;
    6589              :     }
    6590              : 
    6591          343 :   if (curr_binding_label)
    6592              :     /* Binding label given; store in temp holder till have sym.  */
    6593          108 :     *dest_label = curr_binding_label;
    6594              :   else
    6595              :     {
    6596              :       /* No binding label given, and the NAME= specifier did not exist,
    6597              :          which means there was no NAME="".  */
    6598          235 :       if (sym_name != NULL && has_name_equals == 0)
    6599          205 :         *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
    6600              :     }
    6601              : 
    6602              :   return true;
    6603              : }
    6604              : 
    6605              : 
    6606              : /* Set the status of the given common block as being BIND(C) or not,
    6607              :    depending on the given parameter, is_bind_c.  */
    6608              : 
    6609              : static void
    6610           76 : set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
    6611              : {
    6612           76 :   com_block->is_bind_c = is_bind_c;
    6613           76 :   return;
    6614              : }
    6615              : 
    6616              : 
    6617              : /* Verify that the given gfc_typespec is for a C interoperable type.  */
    6618              : 
    6619              : bool
    6620        21421 : gfc_verify_c_interop (gfc_typespec *ts)
    6621              : {
    6622        21421 :   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
    6623         4320 :     return ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c;
    6624        17101 :   else if (ts->type == BT_CLASS)
    6625              :     return false;
    6626        17093 :   else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
    6627         3983 :     return false;
    6628              : 
    6629              :   return true;
    6630              : }
    6631              : 
    6632              : 
    6633              : /* Verify that the variables of a given common block, which has been
    6634              :    defined with the attribute specifier bind(c), to be of a C
    6635              :    interoperable type.  Errors will be reported here, if
    6636              :    encountered.  */
    6637              : 
    6638              : bool
    6639            1 : verify_com_block_vars_c_interop (gfc_common_head *com_block)
    6640              : {
    6641            1 :   gfc_symbol *curr_sym = NULL;
    6642            1 :   bool retval = true;
    6643              : 
    6644            1 :   curr_sym = com_block->head;
    6645              : 
    6646              :   /* Make sure we have at least one symbol.  */
    6647            1 :   if (curr_sym == NULL)
    6648              :     return retval;
    6649              : 
    6650              :   /* Here we know we have a symbol, so we'll execute this loop
    6651              :      at least once.  */
    6652            1 :   do
    6653              :     {
    6654              :       /* The second to last param, 1, says this is in a common block.  */
    6655            1 :       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
    6656            1 :       curr_sym = curr_sym->common_next;
    6657            1 :     } while (curr_sym != NULL);
    6658              : 
    6659              :   return retval;
    6660              : }
    6661              : 
    6662              : 
    6663              : /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
    6664              :    an appropriate error message is reported.  */
    6665              : 
    6666              : bool
    6667         7401 : verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
    6668              :                    int is_in_common, gfc_common_head *com_block)
    6669              : {
    6670         7401 :   bool bind_c_function = false;
    6671         7401 :   bool retval = true;
    6672              : 
    6673         7401 :   if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
    6674         7401 :     bind_c_function = true;
    6675              : 
    6676         7401 :   if (tmp_sym->attr.function && tmp_sym->result != NULL)
    6677              :     {
    6678         3150 :       tmp_sym = tmp_sym->result;
    6679              :       /* Make sure it wasn't an implicitly typed result.  */
    6680         3150 :       if (tmp_sym->attr.implicit_type && warn_c_binding_type)
    6681              :         {
    6682            1 :           gfc_warning (OPT_Wc_binding_type,
    6683              :                        "Implicitly declared BIND(C) function %qs at "
    6684              :                        "%L may not be C interoperable", tmp_sym->name,
    6685              :                        &tmp_sym->declared_at);
    6686            1 :           tmp_sym->ts.f90_type = tmp_sym->ts.type;
    6687              :           /* Mark it as C interoperable to prevent duplicate warnings.  */
    6688            1 :           tmp_sym->ts.is_c_interop = 1;
    6689            1 :           tmp_sym->attr.is_c_interop = 1;
    6690              :         }
    6691              :     }
    6692              : 
    6693              :   /* Here, we know we have the bind(c) attribute, so if we have
    6694              :      enough type info, then verify that it's a C interop kind.
    6695              :      The info could be in the symbol already, or possibly still in
    6696              :      the given ts (current_ts), so look in both.  */
    6697         7401 :   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
    6698              :     {
    6699         3309 :       if (!gfc_verify_c_interop (&(tmp_sym->ts)))
    6700              :         {
    6701              :           /* See if we're dealing with a sym in a common block or not.  */
    6702          237 :           if (is_in_common == 1 && warn_c_binding_type)
    6703              :             {
    6704            0 :               gfc_warning (OPT_Wc_binding_type,
    6705              :                            "Variable %qs in common block %qs at %L "
    6706              :                            "may not be a C interoperable "
    6707              :                            "kind though common block %qs is BIND(C)",
    6708              :                            tmp_sym->name, com_block->name,
    6709            0 :                            &(tmp_sym->declared_at), com_block->name);
    6710              :             }
    6711              :           else
    6712              :             {
    6713          237 :               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
    6714          235 :                   || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
    6715              :                 {
    6716            3 :                   gfc_error ("Type declaration %qs at %L is not C "
    6717              :                              "interoperable but it is BIND(C)",
    6718              :                              tmp_sym->name, &(tmp_sym->declared_at));
    6719            3 :                   retval = false;
    6720              :                 }
    6721          234 :               else if (warn_c_binding_type)
    6722            3 :                 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
    6723              :                              "may not be a C interoperable "
    6724              :                              "kind but it is BIND(C)",
    6725              :                              tmp_sym->name, &(tmp_sym->declared_at));
    6726              :             }
    6727              :         }
    6728              : 
    6729              :       /* Variables declared w/in a common block can't be bind(c)
    6730              :          since there's no way for C to see these variables, so there's
    6731              :          semantically no reason for the attribute.  */
    6732         3309 :       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
    6733              :         {
    6734            1 :           gfc_error ("Variable %qs in common block %qs at "
    6735              :                      "%L cannot be declared with BIND(C) "
    6736              :                      "since it is not a global",
    6737            1 :                      tmp_sym->name, com_block->name,
    6738              :                      &(tmp_sym->declared_at));
    6739            1 :           retval = false;
    6740              :         }
    6741              : 
    6742              :       /* Scalar variables that are bind(c) cannot have the pointer
    6743              :          or allocatable attributes.  */
    6744         3309 :       if (tmp_sym->attr.is_bind_c == 1)
    6745              :         {
    6746         2771 :           if (tmp_sym->attr.pointer == 1)
    6747              :             {
    6748            1 :               gfc_error ("Variable %qs at %L cannot have both the "
    6749              :                          "POINTER and BIND(C) attributes",
    6750              :                          tmp_sym->name, &(tmp_sym->declared_at));
    6751            1 :               retval = false;
    6752              :             }
    6753              : 
    6754         2771 :           if (tmp_sym->attr.allocatable == 1)
    6755              :             {
    6756            0 :               gfc_error ("Variable %qs at %L cannot have both the "
    6757              :                          "ALLOCATABLE and BIND(C) attributes",
    6758              :                          tmp_sym->name, &(tmp_sym->declared_at));
    6759            0 :               retval = false;
    6760              :             }
    6761              : 
    6762              :         }
    6763              : 
    6764              :       /* If it is a BIND(C) function, make sure the return value is a
    6765              :          scalar value.  The previous tests in this function made sure
    6766              :          the type is interoperable.  */
    6767         3309 :       if (bind_c_function && tmp_sym->as != NULL)
    6768            2 :         gfc_error ("Return type of BIND(C) function %qs at %L cannot "
    6769              :                    "be an array", tmp_sym->name, &(tmp_sym->declared_at));
    6770              : 
    6771              :       /* BIND(C) functions cannot return a character string.  */
    6772         3150 :       if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
    6773          116 :         if (!gfc_length_one_character_type_p (&tmp_sym->ts))
    6774            4 :           gfc_error ("Return type of BIND(C) function %qs of character "
    6775              :                      "type at %L must have length 1", tmp_sym->name,
    6776              :                          &(tmp_sym->declared_at));
    6777              :     }
    6778              : 
    6779              :   /* See if the symbol has been marked as private.  If it has, warn if
    6780              :      there is a binding label with default binding name.  */
    6781         7401 :   if (tmp_sym->attr.access == ACCESS_PRIVATE
    6782           11 :       && tmp_sym->binding_label
    6783            8 :       && strcmp (tmp_sym->name, tmp_sym->binding_label) == 0
    6784            5 :       && (tmp_sym->attr.flavor == FL_VARIABLE
    6785            4 :           || tmp_sym->attr.if_source == IFSRC_DECL))
    6786            4 :     gfc_warning (OPT_Wsurprising,
    6787              :                  "Symbol %qs at %L is marked PRIVATE but is accessible "
    6788              :                  "via its default binding name %qs", tmp_sym->name,
    6789              :                  &(tmp_sym->declared_at), tmp_sym->binding_label);
    6790              : 
    6791         7401 :   return retval;
    6792              : }
    6793              : 
    6794              : 
    6795              : /* Set the appropriate fields for a symbol that's been declared as
    6796              :    BIND(C) (the is_bind_c flag and the binding label), and verify that
    6797              :    the type is C interoperable.  Errors are reported by the functions
    6798              :    used to set/test these fields.  */
    6799              : 
    6800              : static bool
    6801           47 : set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
    6802              : {
    6803           47 :   bool retval = true;
    6804              : 
    6805              :   /* TODO: Do we need to make sure the vars aren't marked private?  */
    6806              : 
    6807              :   /* Set the is_bind_c bit in symbol_attribute.  */
    6808           47 :   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
    6809              : 
    6810           47 :   if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
    6811              :     return false;
    6812              : 
    6813              :   return retval;
    6814              : }
    6815              : 
    6816              : 
    6817              : /* Set the fields marking the given common block as BIND(C), including
    6818              :    a binding label, and report any errors encountered.  */
    6819              : 
    6820              : static bool
    6821           76 : set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
    6822              : {
    6823           76 :   bool retval = true;
    6824              : 
    6825              :   /* destLabel, common name, typespec (which may have binding label).  */
    6826           76 :   if (!set_binding_label (&com_block->binding_label, com_block->name,
    6827              :                           num_idents))
    6828              :     return false;
    6829              : 
    6830              :   /* Set the given common block (com_block) to being bind(c) (1).  */
    6831           76 :   set_com_block_bind_c (com_block, 1);
    6832              : 
    6833           76 :   return retval;
    6834              : }
    6835              : 
    6836              : 
    6837              : /* Retrieve the list of one or more identifiers that the given bind(c)
    6838              :    attribute applies to.  */
    6839              : 
    6840              : static bool
    6841          102 : get_bind_c_idents (void)
    6842              : {
    6843          102 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    6844          102 :   int num_idents = 0;
    6845          102 :   gfc_symbol *tmp_sym = NULL;
    6846          102 :   match found_id;
    6847          102 :   gfc_common_head *com_block = NULL;
    6848              : 
    6849          102 :   if (gfc_match_name (name) == MATCH_YES)
    6850              :     {
    6851           38 :       found_id = MATCH_YES;
    6852           38 :       gfc_get_ha_symbol (name, &tmp_sym);
    6853              :     }
    6854           64 :   else if (gfc_match_common_name (name) == MATCH_YES)
    6855              :     {
    6856           64 :       found_id = MATCH_YES;
    6857           64 :       com_block = gfc_get_common (name, 0);
    6858              :     }
    6859              :   else
    6860              :     {
    6861            0 :       gfc_error ("Need either entity or common block name for "
    6862              :                  "attribute specification statement at %C");
    6863            0 :       return false;
    6864              :     }
    6865              : 
    6866              :   /* Save the current identifier and look for more.  */
    6867          123 :   do
    6868              :     {
    6869              :       /* Increment the number of identifiers found for this spec stmt.  */
    6870          123 :       num_idents++;
    6871              : 
    6872              :       /* Make sure we have a sym or com block, and verify that it can
    6873              :          be bind(c).  Set the appropriate field(s) and look for more
    6874              :          identifiers.  */
    6875          123 :       if (tmp_sym != NULL || com_block != NULL)
    6876              :         {
    6877          123 :           if (tmp_sym != NULL)
    6878              :             {
    6879           47 :               if (!set_verify_bind_c_sym (tmp_sym, num_idents))
    6880              :                 return false;
    6881              :             }
    6882              :           else
    6883              :             {
    6884           76 :               if (!set_verify_bind_c_com_block (com_block, num_idents))
    6885              :                 return false;
    6886              :             }
    6887              : 
    6888              :           /* Look to see if we have another identifier.  */
    6889          122 :           tmp_sym = NULL;
    6890          122 :           if (gfc_match_eos () == MATCH_YES)
    6891              :             found_id = MATCH_NO;
    6892           21 :           else if (gfc_match_char (',') != MATCH_YES)
    6893              :             found_id = MATCH_NO;
    6894           21 :           else if (gfc_match_name (name) == MATCH_YES)
    6895              :             {
    6896            9 :               found_id = MATCH_YES;
    6897            9 :               gfc_get_ha_symbol (name, &tmp_sym);
    6898              :             }
    6899           12 :           else if (gfc_match_common_name (name) == MATCH_YES)
    6900              :             {
    6901           12 :               found_id = MATCH_YES;
    6902           12 :               com_block = gfc_get_common (name, 0);
    6903              :             }
    6904              :           else
    6905              :             {
    6906            0 :               gfc_error ("Missing entity or common block name for "
    6907              :                          "attribute specification statement at %C");
    6908            0 :               return false;
    6909              :             }
    6910              :         }
    6911              :       else
    6912              :         {
    6913            0 :           gfc_internal_error ("Missing symbol");
    6914              :         }
    6915          122 :     } while (found_id == MATCH_YES);
    6916              : 
    6917              :   /* if we get here we were successful */
    6918              :   return true;
    6919              : }
    6920              : 
    6921              : 
    6922              : /* Try and match a BIND(C) attribute specification statement.  */
    6923              : 
    6924              : match
    6925          140 : gfc_match_bind_c_stmt (void)
    6926              : {
    6927          140 :   match found_match = MATCH_NO;
    6928          140 :   gfc_typespec *ts;
    6929              : 
    6930          140 :   ts = &current_ts;
    6931              : 
    6932              :   /* This may not be necessary.  */
    6933          140 :   gfc_clear_ts (ts);
    6934              :   /* Clear the temporary binding label holder.  */
    6935          140 :   curr_binding_label = NULL;
    6936              : 
    6937              :   /* Look for the bind(c).  */
    6938          140 :   found_match = gfc_match_bind_c (NULL, true);
    6939              : 
    6940          140 :   if (found_match == MATCH_YES)
    6941              :     {
    6942          103 :       if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
    6943              :         return MATCH_ERROR;
    6944              : 
    6945              :       /* Look for the :: now, but it is not required.  */
    6946          102 :       gfc_match (" :: ");
    6947              : 
    6948              :       /* Get the identifier(s) that needs to be updated.  This may need to
    6949              :          change to hand the flag(s) for the attr specified so all identifiers
    6950              :          found can have all appropriate parts updated (assuming that the same
    6951              :          spec stmt can have multiple attrs, such as both bind(c) and
    6952              :          allocatable...).  */
    6953          102 :       if (!get_bind_c_idents ())
    6954              :         /* Error message should have printed already.  */
    6955            1 :         return MATCH_ERROR;
    6956              :     }
    6957              : 
    6958              :   return found_match;
    6959              : }
    6960              : 
    6961              : 
    6962              : /* Match a data declaration statement.  */
    6963              : 
    6964              : match
    6965      1040301 : gfc_match_data_decl (void)
    6966              : {
    6967      1040301 :   gfc_symbol *sym;
    6968      1040301 :   match m;
    6969      1040301 :   int elem;
    6970      1040301 :   gfc_component *comp_tail = NULL;
    6971              : 
    6972      1040301 :   type_param_spec_list = NULL;
    6973      1040301 :   decl_type_param_list = NULL;
    6974              : 
    6975      1040301 :   num_idents_on_line = 0;
    6976              : 
    6977              :   /* Record the last component before we start, so that we can roll back
    6978              :      any components added during this statement on error.  PR106946.
    6979              :      Must be set before any 'goto cleanup' with m == MATCH_ERROR.  */
    6980      1040301 :   if (gfc_comp_struct (gfc_current_state ()))
    6981              :     {
    6982        32847 :       gfc_symbol *block = gfc_current_block ();
    6983        32847 :       if (block)
    6984              :         {
    6985        32847 :           comp_tail = block->components;
    6986        32847 :           if (comp_tail)
    6987        34739 :             while (comp_tail->next)
    6988              :               comp_tail = comp_tail->next;
    6989              :         }
    6990              :     }
    6991              : 
    6992      1040301 :   m = gfc_match_decl_type_spec (&current_ts, 0);
    6993      1040301 :   if (m != MATCH_YES)
    6994              :     return m;
    6995              : 
    6996       219435 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    6997        35886 :         && !gfc_comp_struct (gfc_current_state ()))
    6998              :     {
    6999        32410 :       sym = gfc_use_derived (current_ts.u.derived);
    7000              : 
    7001        32410 :       if (sym == NULL)
    7002              :         {
    7003           22 :           m = MATCH_ERROR;
    7004           22 :           goto cleanup;
    7005              :         }
    7006              : 
    7007        32388 :       current_ts.u.derived = sym;
    7008              :     }
    7009              : 
    7010       219413 :   m = match_attr_spec ();
    7011       219413 :   if (m == MATCH_ERROR)
    7012              :     {
    7013           84 :       m = MATCH_NO;
    7014           84 :       goto cleanup;
    7015              :     }
    7016              : 
    7017              :   /* F2018:C708.  */
    7018       219329 :   if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
    7019              :     {
    7020            6 :       gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
    7021            6 :       m = MATCH_ERROR;
    7022            6 :       goto cleanup;
    7023              :     }
    7024              : 
    7025       219323 :   if (current_ts.type == BT_CLASS
    7026        11194 :         && current_ts.u.derived->attr.unlimited_polymorphic)
    7027         1993 :     goto ok;
    7028              : 
    7029       217330 :   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
    7030        33864 :       && current_ts.u.derived->components == NULL
    7031         2873 :       && !current_ts.u.derived->attr.zero_comp)
    7032              :     {
    7033              : 
    7034          210 :       if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
    7035          136 :         goto ok;
    7036              : 
    7037           74 :       if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
    7038           47 :         goto ok;
    7039              : 
    7040           27 :       gfc_find_symbol (current_ts.u.derived->name,
    7041           27 :                        current_ts.u.derived->ns, 1, &sym);
    7042              : 
    7043              :       /* Any symbol that we find had better be a type definition
    7044              :          which has its components defined, or be a structure definition
    7045              :          actively being parsed.  */
    7046           27 :       if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
    7047           26 :           && (current_ts.u.derived->components != NULL
    7048           26 :               || current_ts.u.derived->attr.zero_comp
    7049           26 :               || current_ts.u.derived == gfc_new_block))
    7050           26 :         goto ok;
    7051              : 
    7052            1 :       gfc_error ("Derived type at %C has not been previously defined "
    7053              :                  "and so cannot appear in a derived type definition");
    7054            1 :       m = MATCH_ERROR;
    7055            1 :       goto cleanup;
    7056              :     }
    7057              : 
    7058       217120 : ok:
    7059              :   /* If we have an old-style character declaration, and no new-style
    7060              :      attribute specifications, then there a comma is optional between
    7061              :      the type specification and the variable list.  */
    7062       219322 :   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
    7063         1407 :     gfc_match_char (',');
    7064              : 
    7065              :   /* Give the types/attributes to symbols that follow. Give the element
    7066              :      a number so that repeat character length expressions can be copied.  */
    7067       219322 :   elem = 1;
    7068       284942 :   for (;;)
    7069              :     {
    7070       284942 :       num_idents_on_line++;
    7071       284942 :       m = variable_decl (elem++);
    7072       284940 :       if (m == MATCH_ERROR)
    7073          413 :         goto cleanup;
    7074       284527 :       if (m == MATCH_NO)
    7075              :         break;
    7076              : 
    7077       284516 :       if (gfc_match_eos () == MATCH_YES)
    7078       218872 :         goto cleanup;
    7079        65644 :       if (gfc_match_char (',') != MATCH_YES)
    7080              :         break;
    7081              :     }
    7082              : 
    7083           35 :   if (!gfc_error_flag_test ())
    7084              :     {
    7085              :       /* An anonymous structure declaration is unambiguous; if we matched one
    7086              :          according to gfc_match_structure_decl, we need to return MATCH_YES
    7087              :          here to avoid confusing the remaining matchers, even if there was an
    7088              :          error during variable_decl.  We must flush any such errors.  Note this
    7089              :          causes the parser to gracefully continue parsing the remaining input
    7090              :          as a structure body, which likely follows.  */
    7091           11 :       if (current_ts.type == BT_DERIVED && current_ts.u.derived
    7092            1 :           && gfc_fl_struct (current_ts.u.derived->attr.flavor))
    7093              :         {
    7094            1 :           gfc_error_now ("Syntax error in anonymous structure declaration"
    7095              :                          " at %C");
    7096              :           /* Skip the bad variable_decl and line up for the start of the
    7097              :              structure body.  */
    7098            1 :           gfc_error_recovery ();
    7099            1 :           m = MATCH_YES;
    7100            1 :           goto cleanup;
    7101              :         }
    7102              : 
    7103           10 :       gfc_error ("Syntax error in data declaration at %C");
    7104              :     }
    7105              : 
    7106           34 :   m = MATCH_ERROR;
    7107              : 
    7108           34 :   gfc_free_data_all (gfc_current_ns);
    7109              : 
    7110       219433 : cleanup:
    7111              :   /* If we failed inside a derived type definition, remove any CLASS
    7112              :      components that were added during this failed statement.  For CLASS
    7113              :      components, gfc_build_class_symbol creates an extra container symbol in
    7114              :      the namespace outside the normal undo machinery.  When reject_statement
    7115              :      later calls gfc_undo_symbols, the declaration state is rolled back but
    7116              :      that helper symbol survives and leaves the component dangling.  Ordinary
    7117              :      components do not create that extra helper symbol, so leave them in
    7118              :      place for the usual follow-up diagnostics.  PR106946.
    7119              : 
    7120              :      CLASS containers are shared between components of the same class type
    7121              :      and attributes (gfc_build_class_symbol reuses existing containers).
    7122              :      We must not free a container that is still referenced by a previously
    7123              :      committed component.  Unlink and free the components first, then clean
    7124              :      up only orphaned containers.  PR124482.  */
    7125       219433 :   if (m == MATCH_ERROR && gfc_comp_struct (gfc_current_state ()))
    7126              :     {
    7127           86 :       gfc_symbol *block = gfc_current_block ();
    7128           86 :       if (block)
    7129              :         {
    7130           86 :           gfc_component **prev;
    7131           86 :           if (comp_tail)
    7132           43 :             prev = &comp_tail->next;
    7133              :           else
    7134           43 :             prev = &block->components;
    7135              : 
    7136              :           /* Record the CLASS container from the removed components.
    7137              :              Normally all components in one declaration share a single
    7138              :              container, but per-variable array specs can produce
    7139              :              additional ones; any beyond the first are harmlessly
    7140              :              leaked until namespace destruction.  */
    7141           86 :           gfc_symbol *fclass_container = NULL;
    7142              : 
    7143          120 :           while (*prev)
    7144              :             {
    7145           34 :               gfc_component *c = *prev;
    7146           34 :               if (c->ts.type == BT_CLASS && c->ts.u.derived
    7147            6 :                   && c->ts.u.derived->attr.is_class)
    7148              :                 {
    7149            3 :                   *prev = c->next;
    7150            3 :                   if (!fclass_container)
    7151            3 :                     fclass_container = c->ts.u.derived;
    7152            3 :                   c->ts.u.derived = NULL;
    7153            3 :                   gfc_free_component (c);
    7154              :                 }
    7155              :               else
    7156           31 :                 prev = &c->next;
    7157              :             }
    7158              : 
    7159              :           /* Free the container only if no remaining component still
    7160              :              references it.  CLASS containers are shared between
    7161              :              components of the same class type and attributes
    7162              :              (gfc_build_class_symbol reuses existing ones).  */
    7163           86 :           if (fclass_container)
    7164              :             {
    7165            3 :               bool shared = false;
    7166            3 :               for (gfc_component *q = block->components; q; q = q->next)
    7167            1 :                 if (q->ts.type == BT_CLASS
    7168            1 :                     && q->ts.u.derived == fclass_container)
    7169              :                   {
    7170              :                     shared = true;
    7171              :                     break;
    7172              :                   }
    7173            3 :               if (!shared)
    7174              :                 {
    7175            2 :                   if (gfc_find_symtree (fclass_container->ns->sym_root,
    7176              :                                         fclass_container->name))
    7177            2 :                     gfc_delete_symtree (&fclass_container->ns->sym_root,
    7178              :                                         fclass_container->name);
    7179            2 :                   gfc_release_symbol (fclass_container);
    7180              :                 }
    7181              :             }
    7182              :         }
    7183              :     }
    7184              : 
    7185       219433 :   if (saved_kind_expr)
    7186          336 :     gfc_free_expr (saved_kind_expr);
    7187       219433 :   if (type_param_spec_list)
    7188         1069 :     gfc_free_actual_arglist (type_param_spec_list);
    7189       219433 :   if (decl_type_param_list)
    7190         1014 :     gfc_free_actual_arglist (decl_type_param_list);
    7191       219433 :   saved_kind_expr = NULL;
    7192       219433 :   gfc_free_array_spec (current_as);
    7193       219433 :   current_as = NULL;
    7194       219433 :   return m;
    7195              : }
    7196              : 
    7197              : static bool
    7198        24967 : in_module_or_interface(void)
    7199              : {
    7200        24967 :   if (gfc_current_state () == COMP_MODULE
    7201        24967 :       || gfc_current_state () == COMP_SUBMODULE
    7202        24967 :       || gfc_current_state () == COMP_INTERFACE)
    7203              :     return true;
    7204              : 
    7205        20948 :   if (gfc_state_stack->state == COMP_CONTAINS
    7206        20066 :       || gfc_state_stack->state == COMP_FUNCTION
    7207        19960 :       || gfc_state_stack->state == COMP_SUBROUTINE)
    7208              :     {
    7209          988 :       gfc_state_data *p;
    7210         1032 :       for (p = gfc_state_stack->previous; p ; p = p->previous)
    7211              :         {
    7212         1028 :           if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
    7213          118 :               || p->state == COMP_INTERFACE)
    7214              :             return true;
    7215              :         }
    7216              :     }
    7217              :     return false;
    7218              : }
    7219              : 
    7220              : /* Match a prefix associated with a function or subroutine
    7221              :    declaration.  If the typespec pointer is nonnull, then a typespec
    7222              :    can be matched.  Note that if nothing matches, MATCH_YES is
    7223              :    returned (the null string was matched).  */
    7224              : 
    7225              : match
    7226       246595 : gfc_match_prefix (gfc_typespec *ts)
    7227              : {
    7228       246595 :   bool seen_type;
    7229       246595 :   bool seen_impure;
    7230       246595 :   bool found_prefix;
    7231              : 
    7232       246595 :   gfc_clear_attr (&current_attr);
    7233       246595 :   seen_type = false;
    7234       246595 :   seen_impure = false;
    7235              : 
    7236       246595 :   gcc_assert (!gfc_matching_prefix);
    7237       246595 :   gfc_matching_prefix = true;
    7238              : 
    7239       256278 :   do
    7240              :     {
    7241       276542 :       found_prefix = false;
    7242              : 
    7243              :       /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
    7244              :          corresponding attribute seems natural and distinguishes these
    7245              :          procedures from procedure types of PROC_MODULE, which these are
    7246              :          as well.  */
    7247       276542 :       if (gfc_match ("module% ") == MATCH_YES)
    7248              :         {
    7249        25242 :           if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
    7250          275 :             goto error;
    7251              : 
    7252        24967 :           if (!in_module_or_interface ())
    7253              :             {
    7254        19964 :               gfc_error ("MODULE prefix at %C found outside of a module, "
    7255              :                          "submodule, or interface");
    7256        19964 :               goto error;
    7257              :             }
    7258              : 
    7259         5003 :           current_attr.module_procedure = 1;
    7260         5003 :           found_prefix = true;
    7261              :         }
    7262              : 
    7263       256303 :       if (!seen_type && ts != NULL)
    7264              :         {
    7265       138038 :           match m;
    7266       138038 :           m = gfc_match_decl_type_spec (ts, 0);
    7267       138038 :           if (m == MATCH_ERROR)
    7268           15 :             goto error;
    7269       138023 :           if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
    7270              :             {
    7271              :               seen_type = true;
    7272              :               found_prefix = true;
    7273              :             }
    7274              :         }
    7275              : 
    7276       256288 :       if (gfc_match ("elemental% ") == MATCH_YES)
    7277              :         {
    7278         5383 :           if (!gfc_add_elemental (&current_attr, NULL))
    7279            2 :             goto error;
    7280              : 
    7281              :           found_prefix = true;
    7282              :         }
    7283              : 
    7284       256286 :       if (gfc_match ("pure% ") == MATCH_YES)
    7285              :         {
    7286         2490 :           if (!gfc_add_pure (&current_attr, NULL))
    7287            2 :             goto error;
    7288              : 
    7289              :           found_prefix = true;
    7290              :         }
    7291              : 
    7292       256284 :       if (gfc_match ("recursive% ") == MATCH_YES)
    7293              :         {
    7294          469 :           if (!gfc_add_recursive (&current_attr, NULL))
    7295            2 :             goto error;
    7296              : 
    7297              :           found_prefix = true;
    7298              :         }
    7299              : 
    7300              :       /* IMPURE is a somewhat special case, as it needs not set an actual
    7301              :          attribute but rather only prevents ELEMENTAL routines from being
    7302              :          automatically PURE.  */
    7303       256282 :       if (gfc_match ("impure% ") == MATCH_YES)
    7304              :         {
    7305          729 :           if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
    7306            4 :             goto error;
    7307              : 
    7308              :           seen_impure = true;
    7309              :           found_prefix = true;
    7310              :         }
    7311              :     }
    7312              :   while (found_prefix);
    7313              : 
    7314              :   /* IMPURE and PURE must not both appear, of course.  */
    7315       226331 :   if (seen_impure && current_attr.pure)
    7316              :     {
    7317            4 :       gfc_error ("PURE and IMPURE must not appear both at %C");
    7318            4 :       goto error;
    7319              :     }
    7320              : 
    7321              :   /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE.  */
    7322       225606 :   if (!seen_impure && current_attr.elemental && !current_attr.pure)
    7323              :     {
    7324         4688 :       if (!gfc_add_pure (&current_attr, NULL))
    7325            0 :         goto error;
    7326              :     }
    7327              : 
    7328              :   /* At this point, the next item is not a prefix.  */
    7329       226327 :   gcc_assert (gfc_matching_prefix);
    7330              : 
    7331       226327 :   gfc_matching_prefix = false;
    7332       226327 :   return MATCH_YES;
    7333              : 
    7334        20268 : error:
    7335        20268 :   gcc_assert (gfc_matching_prefix);
    7336        20268 :   gfc_matching_prefix = false;
    7337        20268 :   return MATCH_ERROR;
    7338              : }
    7339              : 
    7340              : 
    7341              : /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
    7342              : 
    7343              : static bool
    7344        64205 : copy_prefix (symbol_attribute *dest, locus *where)
    7345              : {
    7346        64205 :   if (dest->module_procedure)
    7347              :     {
    7348          732 :       if (current_attr.elemental)
    7349           13 :         dest->elemental = 1;
    7350              : 
    7351          732 :       if (current_attr.pure)
    7352           61 :         dest->pure = 1;
    7353              : 
    7354          732 :       if (current_attr.recursive)
    7355            8 :         dest->recursive = 1;
    7356              : 
    7357              :       /* Module procedures are unusual in that the 'dest' is copied from
    7358              :          the interface declaration. However, this is an opportunity to
    7359              :          check that the submodule declaration is compliant with the
    7360              :          interface.  */
    7361          732 :       if (dest->elemental && !current_attr.elemental)
    7362              :         {
    7363            1 :           gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
    7364              :                      "missing at %L", where);
    7365            1 :           return false;
    7366              :         }
    7367              : 
    7368          731 :       if (dest->pure && !current_attr.pure)
    7369              :         {
    7370            1 :           gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
    7371              :                      "missing at %L", where);
    7372            1 :           return false;
    7373              :         }
    7374              : 
    7375          730 :       if (dest->recursive && !current_attr.recursive)
    7376              :         {
    7377            1 :           gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
    7378              :                      "missing at %L", where);
    7379            1 :           return false;
    7380              :         }
    7381              : 
    7382              :       return true;
    7383              :     }
    7384              : 
    7385        63473 :   if (current_attr.elemental && !gfc_add_elemental (dest, where))
    7386              :     return false;
    7387              : 
    7388        63471 :   if (current_attr.pure && !gfc_add_pure (dest, where))
    7389              :     return false;
    7390              : 
    7391        63471 :   if (current_attr.recursive && !gfc_add_recursive (dest, where))
    7392              :     return false;
    7393              : 
    7394              :   return true;
    7395              : }
    7396              : 
    7397              : 
    7398              : /* Match a formal argument list or, if typeparam is true, a
    7399              :    type_param_name_list.  */
    7400              : 
    7401              : match
    7402       495389 : gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
    7403              :                           int null_flag, bool typeparam)
    7404              : {
    7405       495389 :   gfc_formal_arglist *head, *tail, *p, *q;
    7406       495389 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7407       495389 :   gfc_symbol *sym;
    7408       495389 :   match m;
    7409       495389 :   gfc_formal_arglist *formal = NULL;
    7410              : 
    7411       495389 :   head = tail = NULL;
    7412              : 
    7413              :   /* Keep the interface formal argument list and null it so that the
    7414              :      matching for the new declaration can be done.  The numbers and
    7415              :      names of the arguments are checked here. The interface formal
    7416              :      arguments are retained in formal_arglist and the characteristics
    7417              :      are compared in resolve.cc(resolve_fl_procedure).  See the remark
    7418              :      in get_proc_name about the eventual need to copy the formal_arglist
    7419              :      and populate the formal namespace of the interface symbol.  */
    7420       495389 :   if (progname->attr.module_procedure
    7421          736 :       && progname->attr.host_assoc)
    7422              :     {
    7423          196 :       formal = progname->formal;
    7424          196 :       progname->formal = NULL;
    7425              :     }
    7426              : 
    7427       495389 :   if (gfc_match_char ('(') != MATCH_YES)
    7428              :     {
    7429       292817 :       if (null_flag)
    7430         6799 :         goto ok;
    7431              :       return MATCH_NO;
    7432              :     }
    7433              : 
    7434       202572 :   if (gfc_match_char (')') == MATCH_YES)
    7435              :   {
    7436        10560 :     if (typeparam)
    7437              :       {
    7438            1 :         gfc_error_now ("A type parameter list is required at %C");
    7439            1 :         m = MATCH_ERROR;
    7440            1 :         goto cleanup;
    7441              :       }
    7442              :     else
    7443        10559 :       goto ok;
    7444              :   }
    7445              : 
    7446       254766 :   for (;;)
    7447              :     {
    7448       254766 :       gfc_gobble_whitespace ();
    7449       254766 :       if (gfc_match_char ('*') == MATCH_YES)
    7450              :         {
    7451        10437 :           sym = NULL;
    7452        10437 :           if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
    7453              :                              "Alternate-return argument at %C"))
    7454              :             {
    7455            1 :               m = MATCH_ERROR;
    7456            1 :               goto cleanup;
    7457              :             }
    7458        10436 :           else if (typeparam)
    7459            2 :             gfc_error_now ("A parameter name is required at %C");
    7460              :         }
    7461              :       else
    7462              :         {
    7463       244329 :           locus loc = gfc_current_locus;
    7464       244329 :           m = gfc_match_name (name);
    7465       244329 :           if (m != MATCH_YES)
    7466              :             {
    7467        16796 :               if(typeparam)
    7468            1 :                 gfc_error_now ("A parameter name is required at %C");
    7469        16812 :               goto cleanup;
    7470              :             }
    7471       227533 :           loc = gfc_get_location_range (NULL, 0, &loc, 1, &gfc_current_locus);
    7472              : 
    7473       227533 :           if (!typeparam && gfc_get_symbol (name, NULL, &sym, &loc))
    7474           16 :             goto cleanup;
    7475       227517 :           else if (typeparam
    7476       227517 :                    && gfc_get_symbol (name, progname->f2k_derived, &sym, &loc))
    7477            0 :             goto cleanup;
    7478              :         }
    7479              : 
    7480       237953 :       p = gfc_get_formal_arglist ();
    7481              : 
    7482       237953 :       if (head == NULL)
    7483              :         head = tail = p;
    7484              :       else
    7485              :         {
    7486        62051 :           tail->next = p;
    7487        62051 :           tail = p;
    7488              :         }
    7489              : 
    7490       237953 :       tail->sym = sym;
    7491              : 
    7492              :       /* We don't add the VARIABLE flavor because the name could be a
    7493              :          dummy procedure.  We don't apply these attributes to formal
    7494              :          arguments of statement functions.  */
    7495       227517 :       if (sym != NULL && !st_flag
    7496       340213 :           && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
    7497       102260 :               || !gfc_missing_attr (&sym->attr, NULL)))
    7498              :         {
    7499            0 :           m = MATCH_ERROR;
    7500            0 :           goto cleanup;
    7501              :         }
    7502              : 
    7503              :       /* The name of a program unit can be in a different namespace,
    7504              :          so check for it explicitly.  After the statement is accepted,
    7505              :          the name is checked for especially in gfc_get_symbol().  */
    7506       237953 :       if (gfc_new_block != NULL && sym != NULL && !typeparam
    7507       100900 :           && strcmp (sym->name, gfc_new_block->name) == 0)
    7508              :         {
    7509            0 :           gfc_error ("Name %qs at %C is the name of the procedure",
    7510              :                      sym->name);
    7511            0 :           m = MATCH_ERROR;
    7512            0 :           goto cleanup;
    7513              :         }
    7514              : 
    7515       237953 :       if (gfc_match_char (')') == MATCH_YES)
    7516       126259 :         goto ok;
    7517              : 
    7518       111694 :       m = gfc_match_char (',');
    7519       111694 :       if (m != MATCH_YES)
    7520              :         {
    7521        48940 :           if (typeparam)
    7522            1 :             gfc_error_now ("Expected parameter list in type declaration "
    7523              :                            "at %C");
    7524              :           else
    7525        48939 :             gfc_error ("Unexpected junk in formal argument list at %C");
    7526        48940 :           goto cleanup;
    7527              :         }
    7528              :     }
    7529              : 
    7530       143617 : ok:
    7531              :   /* Check for duplicate symbols in the formal argument list.  */
    7532       143617 :   if (head != NULL)
    7533              :     {
    7534       186669 :       for (p = head; p->next; p = p->next)
    7535              :         {
    7536        60458 :           if (p->sym == NULL)
    7537          338 :             continue;
    7538              : 
    7539       237961 :           for (q = p->next; q; q = q->next)
    7540       177889 :             if (p->sym == q->sym)
    7541              :               {
    7542           48 :                 if (typeparam)
    7543            1 :                   gfc_error_now ("Duplicate name %qs in parameter "
    7544              :                                  "list at %C", p->sym->name);
    7545              :                 else
    7546           47 :                   gfc_error ("Duplicate symbol %qs in formal argument "
    7547              :                              "list at %C", p->sym->name);
    7548              : 
    7549           48 :                 m = MATCH_ERROR;
    7550           48 :                 goto cleanup;
    7551              :               }
    7552              :         }
    7553              :     }
    7554              : 
    7555       143569 :   if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
    7556              :     {
    7557            0 :       m = MATCH_ERROR;
    7558            0 :       goto cleanup;
    7559              :     }
    7560              : 
    7561              :   /* gfc_error_now used in following and return with MATCH_YES because
    7562              :      doing otherwise results in a cascade of extraneous errors and in
    7563              :      some cases an ICE in symbol.cc(gfc_release_symbol).  */
    7564       143569 :   if (progname->attr.module_procedure && progname->attr.host_assoc)
    7565              :     {
    7566          195 :       bool arg_count_mismatch = false;
    7567              : 
    7568          195 :       if (!formal && head)
    7569              :         arg_count_mismatch = true;
    7570              : 
    7571              :       /* Abbreviated module procedure declaration is not meant to have any
    7572              :          formal arguments!  */
    7573          195 :       if (!progname->abr_modproc_decl && formal && !head)
    7574          195 :         arg_count_mismatch = true;
    7575              : 
    7576          377 :       for (p = formal, q = head; p && q; p = p->next, q = q->next)
    7577              :         {
    7578          182 :           if ((p->next != NULL && q->next == NULL)
    7579          181 :               || (p->next == NULL && q->next != NULL))
    7580              :             arg_count_mismatch = true;
    7581          180 :           else if ((p->sym == NULL && q->sym == NULL)
    7582          180 :                     || (p->sym && q->sym
    7583          178 :                         && strcmp (p->sym->name, q->sym->name) == 0))
    7584          176 :             continue;
    7585              :           else
    7586              :             {
    7587            4 :               if (q->sym == NULL)
    7588            1 :                 gfc_error_now ("MODULE PROCEDURE formal argument %qs "
    7589              :                                "conflicts with alternate return at %C",
    7590              :                                p->sym->name);
    7591            3 :               else if (p->sym == NULL)
    7592            1 :                 gfc_error_now ("MODULE PROCEDURE formal argument is "
    7593              :                                "alternate return and conflicts with "
    7594              :                                "%qs in the separate declaration at %C",
    7595              :                                q->sym->name);
    7596              :               else
    7597            2 :                 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
    7598              :                                "argument names (%s/%s) at %C",
    7599              :                                p->sym->name, q->sym->name);
    7600              :             }
    7601              :         }
    7602              : 
    7603          195 :       if (arg_count_mismatch)
    7604            4 :         gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
    7605              :                        "formal arguments at %C");
    7606              :     }
    7607              : 
    7608              :   return MATCH_YES;
    7609              : 
    7610        65802 : cleanup:
    7611        65802 :   gfc_free_formal_arglist (head);
    7612        65802 :   return m;
    7613              : }
    7614              : 
    7615              : 
    7616              : /* Match a RESULT specification following a function declaration or
    7617              :    ENTRY statement.  Also matches the end-of-statement.  */
    7618              : 
    7619              : static match
    7620         8698 : match_result (gfc_symbol *function, gfc_symbol **result)
    7621              : {
    7622         8698 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7623         8698 :   gfc_symbol *r;
    7624         8698 :   match m;
    7625              : 
    7626         8698 :   if (gfc_match (" result (") != MATCH_YES)
    7627              :     return MATCH_NO;
    7628              : 
    7629         6142 :   m = gfc_match_name (name);
    7630         6142 :   if (m != MATCH_YES)
    7631              :     return m;
    7632              : 
    7633              :   /* Get the right paren, and that's it because there could be the
    7634              :      bind(c) attribute after the result clause.  */
    7635         6142 :   if (gfc_match_char (')') != MATCH_YES)
    7636              :     {
    7637              :      /* TODO: should report the missing right paren here.  */
    7638              :       return MATCH_ERROR;
    7639              :     }
    7640              : 
    7641         6142 :   if (strcmp (function->name, name) == 0)
    7642              :     {
    7643            1 :       gfc_error ("RESULT variable at %C must be different than function name");
    7644            1 :       return MATCH_ERROR;
    7645              :     }
    7646              : 
    7647         6141 :   if (gfc_get_symbol (name, NULL, &r))
    7648              :     return MATCH_ERROR;
    7649              : 
    7650         6141 :   if (!gfc_add_result (&r->attr, r->name, NULL))
    7651              :     return MATCH_ERROR;
    7652              : 
    7653         6141 :   *result = r;
    7654              : 
    7655         6141 :   return MATCH_YES;
    7656              : }
    7657              : 
    7658              : 
    7659              : /* Match a function suffix, which could be a combination of a result
    7660              :    clause and BIND(C), either one, or neither.  The draft does not
    7661              :    require them to come in a specific order.  */
    7662              : 
    7663              : static match
    7664         8702 : gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
    7665              : {
    7666         8702 :   match is_bind_c;   /* Found bind(c).  */
    7667         8702 :   match is_result;   /* Found result clause.  */
    7668         8702 :   match found_match; /* Status of whether we've found a good match.  */
    7669         8702 :   char peek_char;    /* Character we're going to peek at.  */
    7670         8702 :   bool allow_binding_name;
    7671              : 
    7672              :   /* Initialize to having found nothing.  */
    7673         8702 :   found_match = MATCH_NO;
    7674         8702 :   is_bind_c = MATCH_NO;
    7675         8702 :   is_result = MATCH_NO;
    7676              : 
    7677              :   /* Get the next char to narrow between result and bind(c).  */
    7678         8702 :   gfc_gobble_whitespace ();
    7679         8702 :   peek_char = gfc_peek_ascii_char ();
    7680              : 
    7681              :   /* C binding names are not allowed for internal procedures.  */
    7682         8702 :   if (gfc_current_state () == COMP_CONTAINS
    7683         4869 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    7684              :     allow_binding_name = false;
    7685              :   else
    7686         6998 :     allow_binding_name = true;
    7687              : 
    7688         8702 :   switch (peek_char)
    7689              :     {
    7690         5771 :     case 'r':
    7691              :       /* Look for result clause.  */
    7692         5771 :       is_result = match_result (sym, result);
    7693         5771 :       if (is_result == MATCH_YES)
    7694              :         {
    7695              :           /* Now see if there is a bind(c) after it.  */
    7696         5770 :           is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    7697              :           /* We've found the result clause and possibly bind(c).  */
    7698         5770 :           found_match = MATCH_YES;
    7699              :         }
    7700              :       else
    7701              :         /* This should only be MATCH_ERROR.  */
    7702              :         found_match = is_result;
    7703              :       break;
    7704         2931 :     case 'b':
    7705              :       /* Look for bind(c) first.  */
    7706         2931 :       is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    7707         2931 :       if (is_bind_c == MATCH_YES)
    7708              :         {
    7709              :           /* Now see if a result clause followed it.  */
    7710         2927 :           is_result = match_result (sym, result);
    7711         2927 :           found_match = MATCH_YES;
    7712              :         }
    7713              :       else
    7714              :         {
    7715              :           /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
    7716              :           found_match = MATCH_ERROR;
    7717              :         }
    7718              :       break;
    7719            0 :     default:
    7720            0 :       gfc_error ("Unexpected junk after function declaration at %C");
    7721            0 :       found_match = MATCH_ERROR;
    7722            0 :       break;
    7723              :     }
    7724              : 
    7725         8697 :   if (is_bind_c == MATCH_YES)
    7726              :     {
    7727              :       /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
    7728         3094 :       if (gfc_current_state () == COMP_CONTAINS
    7729          423 :           && sym->ns->proc_name->attr.flavor != FL_MODULE
    7730         3112 :           && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
    7731              :                               "at %L may not be specified for an internal "
    7732              :                               "procedure", &gfc_current_locus))
    7733              :         return MATCH_ERROR;
    7734              : 
    7735         3091 :       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
    7736            0 :         return MATCH_ERROR;
    7737              :     }
    7738              : 
    7739              :   return found_match;
    7740              : }
    7741              : 
    7742              : 
    7743              : /* Procedure pointer return value without RESULT statement:
    7744              :    Add "hidden" result variable named "ppr@".  */
    7745              : 
    7746              : static bool
    7747        75884 : add_hidden_procptr_result (gfc_symbol *sym)
    7748              : {
    7749        75884 :   bool case1,case2;
    7750              : 
    7751        75884 :   if (gfc_notification_std (GFC_STD_F2003) == ERROR)
    7752              :     return false;
    7753              : 
    7754              :   /* First usage case: PROCEDURE and EXTERNAL statements.  */
    7755         1539 :   case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
    7756         1539 :           && strcmp (gfc_current_block ()->name, sym->name) == 0
    7757        76283 :           && sym->attr.external;
    7758              :   /* Second usage case: INTERFACE statements.  */
    7759        14892 :   case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
    7760        14892 :           && gfc_state_stack->previous->state == COMP_FUNCTION
    7761        75931 :           && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
    7762              : 
    7763        75700 :   if (case1 || case2)
    7764              :     {
    7765          125 :       gfc_symtree *stree;
    7766          125 :       if (case1)
    7767           95 :         gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
    7768              :       else
    7769              :         {
    7770           30 :           gfc_symtree *st2;
    7771           30 :           gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
    7772           30 :           st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
    7773           30 :           st2->n.sym = stree->n.sym;
    7774           30 :           stree->n.sym->refs++;
    7775              :         }
    7776          125 :       sym->result = stree->n.sym;
    7777              : 
    7778          125 :       sym->result->attr.proc_pointer = sym->attr.proc_pointer;
    7779          125 :       sym->result->attr.pointer = sym->attr.pointer;
    7780          125 :       sym->result->attr.external = sym->attr.external;
    7781          125 :       sym->result->attr.referenced = sym->attr.referenced;
    7782          125 :       sym->result->ts = sym->ts;
    7783          125 :       sym->attr.proc_pointer = 0;
    7784          125 :       sym->attr.pointer = 0;
    7785          125 :       sym->attr.external = 0;
    7786          125 :       if (sym->result->attr.external && sym->result->attr.pointer)
    7787              :         {
    7788            4 :           sym->result->attr.pointer = 0;
    7789            4 :           sym->result->attr.proc_pointer = 1;
    7790              :         }
    7791              : 
    7792          125 :       return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
    7793              :     }
    7794              :   /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
    7795        75605 :   else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
    7796          411 :            && sym->result && sym->result != sym && sym->result->attr.external
    7797           28 :            && sym == gfc_current_ns->proc_name
    7798           28 :            && sym == sym->result->ns->proc_name
    7799           28 :            && strcmp ("ppr@", sym->result->name) == 0)
    7800              :     {
    7801           28 :       sym->result->attr.proc_pointer = 1;
    7802           28 :       sym->attr.pointer = 0;
    7803           28 :       return true;
    7804              :     }
    7805              :   else
    7806              :     return false;
    7807              : }
    7808              : 
    7809              : 
    7810              : /* Match the interface for a PROCEDURE declaration,
    7811              :    including brackets (R1212).  */
    7812              : 
    7813              : static match
    7814         1636 : match_procedure_interface (gfc_symbol **proc_if)
    7815              : {
    7816         1636 :   match m;
    7817         1636 :   gfc_symtree *st;
    7818         1636 :   locus old_loc, entry_loc;
    7819         1636 :   gfc_namespace *old_ns = gfc_current_ns;
    7820         1636 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    7821              : 
    7822         1636 :   old_loc = entry_loc = gfc_current_locus;
    7823         1636 :   gfc_clear_ts (&current_ts);
    7824              : 
    7825         1636 :   if (gfc_match (" (") != MATCH_YES)
    7826              :     {
    7827            1 :       gfc_current_locus = entry_loc;
    7828            1 :       return MATCH_NO;
    7829              :     }
    7830              : 
    7831              :   /* Get the type spec. for the procedure interface.  */
    7832         1635 :   old_loc = gfc_current_locus;
    7833         1635 :   m = gfc_match_decl_type_spec (&current_ts, 0);
    7834         1635 :   gfc_gobble_whitespace ();
    7835         1635 :   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
    7836          395 :     goto got_ts;
    7837              : 
    7838         1240 :   if (m == MATCH_ERROR)
    7839              :     return m;
    7840              : 
    7841              :   /* Procedure interface is itself a procedure.  */
    7842         1240 :   gfc_current_locus = old_loc;
    7843         1240 :   m = gfc_match_name (name);
    7844              : 
    7845              :   /* First look to see if it is already accessible in the current
    7846              :      namespace because it is use associated or contained.  */
    7847         1240 :   st = NULL;
    7848         1240 :   if (gfc_find_sym_tree (name, NULL, 0, &st))
    7849              :     return MATCH_ERROR;
    7850              : 
    7851              :   /* If it is still not found, then try the parent namespace, if it
    7852              :      exists and create the symbol there if it is still not found.  */
    7853         1240 :   if (gfc_current_ns->parent)
    7854          435 :     gfc_current_ns = gfc_current_ns->parent;
    7855         1240 :   if (st == NULL && gfc_get_ha_sym_tree (name, &st))
    7856              :     return MATCH_ERROR;
    7857              : 
    7858         1240 :   gfc_current_ns = old_ns;
    7859         1240 :   *proc_if = st->n.sym;
    7860              : 
    7861         1240 :   if (*proc_if)
    7862              :     {
    7863         1240 :       (*proc_if)->refs++;
    7864              :       /* Resolve interface if possible. That way, attr.procedure is only set
    7865              :          if it is declared by a later procedure-declaration-stmt, which is
    7866              :          invalid per F08:C1216 (cf. resolve_procedure_interface).  */
    7867         1240 :       while ((*proc_if)->ts.interface
    7868         1247 :              && *proc_if != (*proc_if)->ts.interface)
    7869            7 :         *proc_if = (*proc_if)->ts.interface;
    7870              : 
    7871         1240 :       if ((*proc_if)->attr.flavor == FL_UNKNOWN
    7872          389 :           && (*proc_if)->ts.type == BT_UNKNOWN
    7873         1629 :           && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
    7874              :                               (*proc_if)->name, NULL))
    7875              :         return MATCH_ERROR;
    7876              :     }
    7877              : 
    7878            0 : got_ts:
    7879         1635 :   if (gfc_match (" )") != MATCH_YES)
    7880              :     {
    7881            0 :       gfc_current_locus = entry_loc;
    7882            0 :       return MATCH_NO;
    7883              :     }
    7884              : 
    7885              :   return MATCH_YES;
    7886              : }
    7887              : 
    7888              : 
    7889              : /* Match a PROCEDURE declaration (R1211).  */
    7890              : 
    7891              : static match
    7892         1197 : match_procedure_decl (void)
    7893              : {
    7894         1197 :   match m;
    7895         1197 :   gfc_symbol *sym, *proc_if = NULL;
    7896         1197 :   int num;
    7897         1197 :   gfc_expr *initializer = NULL;
    7898              : 
    7899              :   /* Parse interface (with brackets).  */
    7900         1197 :   m = match_procedure_interface (&proc_if);
    7901         1197 :   if (m != MATCH_YES)
    7902              :     return m;
    7903              : 
    7904              :   /* Parse attributes (with colons).  */
    7905         1197 :   m = match_attr_spec();
    7906         1197 :   if (m == MATCH_ERROR)
    7907              :     return MATCH_ERROR;
    7908              : 
    7909         1196 :   if (current_attr.allocatable)
    7910              :     {
    7911            2 :       current_attr.procedure = 1;
    7912            2 :       gfc_check_conflict (&current_attr, NULL, &gfc_current_locus);
    7913            2 :       return MATCH_ERROR;
    7914              :     }
    7915              : 
    7916         1194 :   if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
    7917              :     {
    7918           53 :       current_attr.is_bind_c = 1;
    7919           53 :       has_name_equals = 0;
    7920           53 :       curr_binding_label = NULL;
    7921              :     }
    7922              : 
    7923              :   /* Get procedure symbols.  */
    7924         1194 :   for(num=1;;num++)
    7925              :     {
    7926         1273 :       m = gfc_match_symbol (&sym, 0);
    7927         1273 :       if (m == MATCH_NO)
    7928            1 :         goto syntax;
    7929         1272 :       else if (m == MATCH_ERROR)
    7930              :         return m;
    7931              : 
    7932              :       /* Add current_attr to the symbol attributes.  */
    7933         1272 :       if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
    7934              :         return MATCH_ERROR;
    7935              : 
    7936         1270 :       if (sym->attr.is_bind_c)
    7937              :         {
    7938              :           /* Check for C1218.  */
    7939           90 :           if (!proc_if || !proc_if->attr.is_bind_c)
    7940              :             {
    7941            1 :               gfc_error ("BIND(C) attribute at %C requires "
    7942              :                         "an interface with BIND(C)");
    7943            1 :               return MATCH_ERROR;
    7944              :             }
    7945              :           /* Check for C1217.  */
    7946           89 :           if (has_name_equals && sym->attr.pointer)
    7947              :             {
    7948            1 :               gfc_error ("BIND(C) procedure with NAME may not have "
    7949              :                         "POINTER attribute at %C");
    7950            1 :               return MATCH_ERROR;
    7951              :             }
    7952           88 :           if (has_name_equals && sym->attr.dummy)
    7953              :             {
    7954            1 :               gfc_error ("Dummy procedure at %C may not have "
    7955              :                         "BIND(C) attribute with NAME");
    7956            1 :               return MATCH_ERROR;
    7957              :             }
    7958              :           /* Set binding label for BIND(C).  */
    7959           87 :           if (!set_binding_label (&sym->binding_label, sym->name, num))
    7960              :             return MATCH_ERROR;
    7961              :         }
    7962              : 
    7963         1266 :       if (!gfc_add_external (&sym->attr, NULL))
    7964              :         return MATCH_ERROR;
    7965              : 
    7966         1262 :       if (add_hidden_procptr_result (sym))
    7967           68 :         sym = sym->result;
    7968              : 
    7969         1262 :       if (!gfc_add_proc (&sym->attr, sym->name, NULL))
    7970              :         return MATCH_ERROR;
    7971              : 
    7972              :       /* Set interface.  */
    7973         1262 :       if (proc_if != NULL)
    7974              :         {
    7975          919 :           if (sym->ts.type != BT_UNKNOWN)
    7976              :             {
    7977            1 :               gfc_error ("Procedure %qs at %L already has basic type of %s",
    7978              :                          sym->name, &gfc_current_locus,
    7979              :                          gfc_basic_typename (sym->ts.type));
    7980            1 :               return MATCH_ERROR;
    7981              :             }
    7982          918 :           sym->ts.interface = proc_if;
    7983          918 :           sym->attr.untyped = 1;
    7984          918 :           sym->attr.if_source = IFSRC_IFBODY;
    7985              :         }
    7986          343 :       else if (current_ts.type != BT_UNKNOWN)
    7987              :         {
    7988          199 :           if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
    7989              :             return MATCH_ERROR;
    7990          198 :           sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    7991          198 :           sym->ts.interface->ts = current_ts;
    7992          198 :           sym->ts.interface->attr.flavor = FL_PROCEDURE;
    7993          198 :           sym->ts.interface->attr.function = 1;
    7994          198 :           sym->attr.function = 1;
    7995          198 :           sym->attr.if_source = IFSRC_UNKNOWN;
    7996              :         }
    7997              : 
    7998         1260 :       if (gfc_match (" =>") == MATCH_YES)
    7999              :         {
    8000          110 :           if (!current_attr.pointer)
    8001              :             {
    8002            0 :               gfc_error ("Initialization at %C isn't for a pointer variable");
    8003            0 :               m = MATCH_ERROR;
    8004            0 :               goto cleanup;
    8005              :             }
    8006              : 
    8007          110 :           m = match_pointer_init (&initializer, 1);
    8008          110 :           if (m != MATCH_YES)
    8009            1 :             goto cleanup;
    8010              : 
    8011          109 :           if (!add_init_expr_to_sym (sym->name, &initializer,
    8012              :                                      &gfc_current_locus,
    8013              :                                      gfc_current_ns->cl_list))
    8014            0 :             goto cleanup;
    8015              : 
    8016              :         }
    8017              : 
    8018         1259 :       if (gfc_match_eos () == MATCH_YES)
    8019              :         return MATCH_YES;
    8020           79 :       if (gfc_match_char (',') != MATCH_YES)
    8021            0 :         goto syntax;
    8022              :     }
    8023              : 
    8024            1 : syntax:
    8025            1 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
    8026            1 :   return MATCH_ERROR;
    8027              : 
    8028            1 : cleanup:
    8029              :   /* Free stuff up and return.  */
    8030            1 :   gfc_free_expr (initializer);
    8031            1 :   return m;
    8032              : }
    8033              : 
    8034              : 
    8035              : static match
    8036              : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
    8037              : 
    8038              : 
    8039              : /* Match a procedure pointer component declaration (R445).  */
    8040              : 
    8041              : static match
    8042          439 : match_ppc_decl (void)
    8043              : {
    8044          439 :   match m;
    8045          439 :   gfc_symbol *proc_if = NULL;
    8046          439 :   gfc_typespec ts;
    8047          439 :   int num;
    8048          439 :   gfc_component *c;
    8049          439 :   gfc_expr *initializer = NULL;
    8050          439 :   gfc_typebound_proc* tb;
    8051          439 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8052              : 
    8053              :   /* Parse interface (with brackets).  */
    8054          439 :   m = match_procedure_interface (&proc_if);
    8055          439 :   if (m != MATCH_YES)
    8056            1 :     goto syntax;
    8057              : 
    8058              :   /* Parse attributes.  */
    8059          438 :   tb = XCNEW (gfc_typebound_proc);
    8060          438 :   tb->where = gfc_current_locus;
    8061          438 :   m = match_binding_attributes (tb, false, true);
    8062          438 :   if (m == MATCH_ERROR)
    8063              :     return m;
    8064              : 
    8065          435 :   gfc_clear_attr (&current_attr);
    8066          435 :   current_attr.procedure = 1;
    8067          435 :   current_attr.proc_pointer = 1;
    8068          435 :   current_attr.access = tb->access;
    8069          435 :   current_attr.flavor = FL_PROCEDURE;
    8070              : 
    8071              :   /* Match the colons (required).  */
    8072          435 :   if (gfc_match (" ::") != MATCH_YES)
    8073              :     {
    8074            1 :       gfc_error ("Expected %<::%> after binding-attributes at %C");
    8075            1 :       return MATCH_ERROR;
    8076              :     }
    8077              : 
    8078              :   /* Check for C450.  */
    8079          434 :   if (!tb->nopass && proc_if == NULL)
    8080              :     {
    8081            2 :       gfc_error("NOPASS or explicit interface required at %C");
    8082            2 :       return MATCH_ERROR;
    8083              :     }
    8084              : 
    8085          432 :   if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
    8086              :     return MATCH_ERROR;
    8087              : 
    8088              :   /* Match PPC names.  */
    8089          431 :   ts = current_ts;
    8090          431 :   for(num=1;;num++)
    8091              :     {
    8092          432 :       m = gfc_match_name (name);
    8093          432 :       if (m == MATCH_NO)
    8094            0 :         goto syntax;
    8095          432 :       else if (m == MATCH_ERROR)
    8096              :         return m;
    8097              : 
    8098          432 :       if (!gfc_add_component (gfc_current_block(), name, &c))
    8099              :         return MATCH_ERROR;
    8100              : 
    8101              :       /* Add current_attr to the symbol attributes.  */
    8102          432 :       if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
    8103              :         return MATCH_ERROR;
    8104              : 
    8105          432 :       if (!gfc_add_external (&c->attr, NULL))
    8106              :         return MATCH_ERROR;
    8107              : 
    8108          432 :       if (!gfc_add_proc (&c->attr, name, NULL))
    8109              :         return MATCH_ERROR;
    8110              : 
    8111          432 :       if (num == 1)
    8112          431 :         c->tb = tb;
    8113              :       else
    8114              :         {
    8115            1 :           c->tb = XCNEW (gfc_typebound_proc);
    8116            1 :           c->tb->where = gfc_current_locus;
    8117            1 :           *c->tb = *tb;
    8118              :         }
    8119              : 
    8120          432 :       if (saved_kind_expr)
    8121            0 :         c->kind_expr = gfc_copy_expr (saved_kind_expr);
    8122              : 
    8123              :       /* Set interface.  */
    8124          432 :       if (proc_if != NULL)
    8125              :         {
    8126          365 :           c->ts.interface = proc_if;
    8127          365 :           c->attr.untyped = 1;
    8128          365 :           c->attr.if_source = IFSRC_IFBODY;
    8129              :         }
    8130           67 :       else if (ts.type != BT_UNKNOWN)
    8131              :         {
    8132           29 :           c->ts = ts;
    8133           29 :           c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
    8134           29 :           c->ts.interface->result = c->ts.interface;
    8135           29 :           c->ts.interface->ts = ts;
    8136           29 :           c->ts.interface->attr.flavor = FL_PROCEDURE;
    8137           29 :           c->ts.interface->attr.function = 1;
    8138           29 :           c->attr.function = 1;
    8139           29 :           c->attr.if_source = IFSRC_UNKNOWN;
    8140              :         }
    8141              : 
    8142          432 :       if (gfc_match (" =>") == MATCH_YES)
    8143              :         {
    8144           79 :           m = match_pointer_init (&initializer, 1);
    8145           79 :           if (m != MATCH_YES)
    8146              :             {
    8147            0 :               gfc_free_expr (initializer);
    8148            0 :               return m;
    8149              :             }
    8150           79 :           c->initializer = initializer;
    8151              :         }
    8152              : 
    8153          432 :       if (gfc_match_eos () == MATCH_YES)
    8154              :         return MATCH_YES;
    8155            1 :       if (gfc_match_char (',') != MATCH_YES)
    8156            0 :         goto syntax;
    8157              :     }
    8158              : 
    8159            1 : syntax:
    8160            1 :   gfc_error ("Syntax error in procedure pointer component at %C");
    8161            1 :   return MATCH_ERROR;
    8162              : }
    8163              : 
    8164              : 
    8165              : /* Match a PROCEDURE declaration inside an interface (R1206).  */
    8166              : 
    8167              : static match
    8168         1561 : match_procedure_in_interface (void)
    8169              : {
    8170         1561 :   match m;
    8171         1561 :   gfc_symbol *sym;
    8172         1561 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8173         1561 :   locus old_locus;
    8174              : 
    8175         1561 :   if (current_interface.type == INTERFACE_NAMELESS
    8176         1561 :       || current_interface.type == INTERFACE_ABSTRACT)
    8177              :     {
    8178            1 :       gfc_error ("PROCEDURE at %C must be in a generic interface");
    8179            1 :       return MATCH_ERROR;
    8180              :     }
    8181              : 
    8182              :   /* Check if the F2008 optional double colon appears.  */
    8183         1560 :   gfc_gobble_whitespace ();
    8184         1560 :   old_locus = gfc_current_locus;
    8185         1560 :   if (gfc_match ("::") == MATCH_YES)
    8186              :     {
    8187          875 :       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
    8188              :                            "MODULE PROCEDURE statement at %L", &old_locus))
    8189              :         return MATCH_ERROR;
    8190              :     }
    8191              :   else
    8192          685 :     gfc_current_locus = old_locus;
    8193              : 
    8194         2214 :   for(;;)
    8195              :     {
    8196         2214 :       m = gfc_match_name (name);
    8197         2214 :       if (m == MATCH_NO)
    8198            0 :         goto syntax;
    8199         2214 :       else if (m == MATCH_ERROR)
    8200              :         return m;
    8201         2214 :       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
    8202              :         return MATCH_ERROR;
    8203              : 
    8204         2214 :       if (!gfc_add_interface (sym))
    8205              :         return MATCH_ERROR;
    8206              : 
    8207         2213 :       if (gfc_match_eos () == MATCH_YES)
    8208              :         break;
    8209          655 :       if (gfc_match_char (',') != MATCH_YES)
    8210            0 :         goto syntax;
    8211              :     }
    8212              : 
    8213              :   return MATCH_YES;
    8214              : 
    8215            0 : syntax:
    8216            0 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
    8217            0 :   return MATCH_ERROR;
    8218              : }
    8219              : 
    8220              : 
    8221              : /* General matcher for PROCEDURE declarations.  */
    8222              : 
    8223              : static match match_procedure_in_type (void);
    8224              : 
    8225              : match
    8226         6451 : gfc_match_procedure (void)
    8227              : {
    8228         6451 :   match m;
    8229              : 
    8230         6451 :   switch (gfc_current_state ())
    8231              :     {
    8232         1197 :     case COMP_NONE:
    8233         1197 :     case COMP_PROGRAM:
    8234         1197 :     case COMP_MODULE:
    8235         1197 :     case COMP_SUBMODULE:
    8236         1197 :     case COMP_SUBROUTINE:
    8237         1197 :     case COMP_FUNCTION:
    8238         1197 :     case COMP_BLOCK:
    8239         1197 :       m = match_procedure_decl ();
    8240         1197 :       break;
    8241         1561 :     case COMP_INTERFACE:
    8242         1561 :       m = match_procedure_in_interface ();
    8243         1561 :       break;
    8244          439 :     case COMP_DERIVED:
    8245          439 :       m = match_ppc_decl ();
    8246          439 :       break;
    8247         3254 :     case COMP_DERIVED_CONTAINS:
    8248         3254 :       m = match_procedure_in_type ();
    8249         3254 :       break;
    8250              :     default:
    8251              :       return MATCH_NO;
    8252              :     }
    8253              : 
    8254         6451 :   if (m != MATCH_YES)
    8255              :     return m;
    8256              : 
    8257         6394 :   if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
    8258            4 :     return MATCH_ERROR;
    8259              : 
    8260              :   return m;
    8261              : }
    8262              : 
    8263              : 
    8264              : /* Warn if a matched procedure has the same name as an intrinsic; this is
    8265              :    simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
    8266              :    parser-state-stack to find out whether we're in a module.  */
    8267              : 
    8268              : static void
    8269        64202 : do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
    8270              : {
    8271        64202 :   bool in_module;
    8272              : 
    8273       128404 :   in_module = (gfc_state_stack->previous
    8274        64202 :                && (gfc_state_stack->previous->state == COMP_MODULE
    8275        52404 :                    || gfc_state_stack->previous->state == COMP_SUBMODULE));
    8276              : 
    8277        64202 :   gfc_warn_intrinsic_shadow (sym, in_module, func);
    8278        64202 : }
    8279              : 
    8280              : 
    8281              : /* Match a function declaration.  */
    8282              : 
    8283              : match
    8284       131382 : gfc_match_function_decl (void)
    8285              : {
    8286       131382 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8287       131382 :   gfc_symbol *sym, *result;
    8288       131382 :   locus old_loc;
    8289       131382 :   match m;
    8290       131382 :   match suffix_match;
    8291       131382 :   match found_match; /* Status returned by match func.  */
    8292              : 
    8293       131382 :   if (gfc_current_state () != COMP_NONE
    8294        82937 :       && gfc_current_state () != COMP_INTERFACE
    8295        53473 :       && gfc_current_state () != COMP_CONTAINS)
    8296              :     return MATCH_NO;
    8297              : 
    8298       131382 :   gfc_clear_ts (&current_ts);
    8299              : 
    8300       131382 :   old_loc = gfc_current_locus;
    8301              : 
    8302       131382 :   m = gfc_match_prefix (&current_ts);
    8303       131382 :   if (m != MATCH_YES)
    8304              :     {
    8305        10136 :       gfc_current_locus = old_loc;
    8306        10136 :       return m;
    8307              :     }
    8308              : 
    8309       121246 :   if (gfc_match ("function% %n", name) != MATCH_YES)
    8310              :     {
    8311       100995 :       gfc_current_locus = old_loc;
    8312       100995 :       return MATCH_NO;
    8313              :     }
    8314              : 
    8315        20251 :   if (get_proc_name (name, &sym, false))
    8316              :     return MATCH_ERROR;
    8317              : 
    8318        20246 :   if (add_hidden_procptr_result (sym))
    8319           20 :     sym = sym->result;
    8320              : 
    8321        20246 :   if (current_attr.module_procedure)
    8322              :     {
    8323          304 :       sym->attr.module_procedure = 1;
    8324          304 :       if (gfc_current_state () == COMP_INTERFACE)
    8325          215 :         gfc_current_ns->has_import_set = 1;
    8326              :     }
    8327              : 
    8328        20246 :   gfc_new_block = sym;
    8329              : 
    8330        20246 :   m = gfc_match_formal_arglist (sym, 0, 0);
    8331        20246 :   if (m == MATCH_NO)
    8332              :     {
    8333            6 :       gfc_error ("Expected formal argument list in function "
    8334              :                  "definition at %C");
    8335            6 :       m = MATCH_ERROR;
    8336            6 :       goto cleanup;
    8337              :     }
    8338        20240 :   else if (m == MATCH_ERROR)
    8339            0 :     goto cleanup;
    8340              : 
    8341        20240 :   result = NULL;
    8342              : 
    8343              :   /* According to the draft, the bind(c) and result clause can
    8344              :      come in either order after the formal_arg_list (i.e., either
    8345              :      can be first, both can exist together or by themselves or neither
    8346              :      one).  Therefore, the match_result can't match the end of the
    8347              :      string, and check for the bind(c) or result clause in either order.  */
    8348        20240 :   found_match = gfc_match_eos ();
    8349              : 
    8350              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8351              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8352              :      not allowed for procedures.  */
    8353        20240 :   if (sym->attr.is_bind_c == 1)
    8354              :     {
    8355            3 :       sym->attr.is_bind_c = 0;
    8356              : 
    8357            3 :       if (gfc_state_stack->previous
    8358            3 :           && gfc_state_stack->previous->state != COMP_SUBMODULE)
    8359              :         {
    8360            1 :           locus loc;
    8361            1 :           loc = sym->old_symbol != NULL
    8362            1 :             ? sym->old_symbol->declared_at : gfc_current_locus;
    8363            1 :           gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8364              :                          "variables or common blocks", &loc);
    8365              :         }
    8366              :     }
    8367              : 
    8368        20240 :   if (found_match != MATCH_YES)
    8369              :     {
    8370              :       /* If we haven't found the end-of-statement, look for a suffix.  */
    8371         8453 :       suffix_match = gfc_match_suffix (sym, &result);
    8372         8453 :       if (suffix_match == MATCH_YES)
    8373              :         /* Need to get the eos now.  */
    8374         8445 :         found_match = gfc_match_eos ();
    8375              :       else
    8376              :         found_match = suffix_match;
    8377              :     }
    8378              : 
    8379              :   /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
    8380              :      subprogram and a binding label is specified, it shall be the
    8381              :      same as the binding label specified in the corresponding module
    8382              :      procedure interface body.  */
    8383        20240 :     if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
    8384            3 :         && strcmp (sym->name, sym->old_symbol->name) == 0
    8385            3 :         && sym->binding_label && sym->old_symbol->binding_label
    8386            2 :         && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
    8387              :       {
    8388            1 :           const char *null = "NULL", *s1, *s2;
    8389            1 :           s1 = sym->binding_label;
    8390            1 :           if (!s1) s1 = null;
    8391            1 :           s2 = sym->old_symbol->binding_label;
    8392            1 :           if (!s2) s2 = null;
    8393            1 :           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
    8394            1 :           sym->refs++;       /* Needed to avoid an ICE in gfc_release_symbol */
    8395            1 :           return MATCH_ERROR;
    8396              :       }
    8397              : 
    8398        20239 :   if(found_match != MATCH_YES)
    8399              :     m = MATCH_ERROR;
    8400              :   else
    8401              :     {
    8402              :       /* Make changes to the symbol.  */
    8403        20231 :       m = MATCH_ERROR;
    8404              : 
    8405        20231 :       if (!gfc_add_function (&sym->attr, sym->name, NULL))
    8406            0 :         goto cleanup;
    8407              : 
    8408        20231 :       if (!gfc_missing_attr (&sym->attr, NULL))
    8409            0 :         goto cleanup;
    8410              : 
    8411        20231 :       if (!copy_prefix (&sym->attr, &sym->declared_at))
    8412              :         {
    8413            1 :           if(!sym->attr.module_procedure)
    8414            1 :         goto cleanup;
    8415              :           else
    8416            0 :             gfc_error_check ();
    8417              :         }
    8418              : 
    8419              :       /* Delay matching the function characteristics until after the
    8420              :          specification block by signalling kind=-1.  */
    8421        20230 :       sym->declared_at = old_loc;
    8422        20230 :       if (current_ts.type != BT_UNKNOWN)
    8423              :         current_ts.kind = -1;
    8424              :       else
    8425        13238 :         current_ts.kind = 0;
    8426              : 
    8427        20230 :       if (result == NULL)
    8428              :         {
    8429        14301 :           if (current_ts.type != BT_UNKNOWN
    8430        14301 :               && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
    8431            1 :             goto cleanup;
    8432        14300 :           sym->result = sym;
    8433              :         }
    8434              :       else
    8435              :         {
    8436         5929 :           if (current_ts.type != BT_UNKNOWN
    8437         5929 :               && !gfc_add_type (result, &current_ts, &gfc_current_locus))
    8438            0 :             goto cleanup;
    8439         5929 :           sym->result = result;
    8440              :         }
    8441              : 
    8442              :       /* Warn if this procedure has the same name as an intrinsic.  */
    8443        20229 :       do_warn_intrinsic_shadow (sym, true);
    8444              : 
    8445        20229 :       return MATCH_YES;
    8446              :     }
    8447              : 
    8448           16 : cleanup:
    8449           16 :   gfc_current_locus = old_loc;
    8450           16 :   return m;
    8451              : }
    8452              : 
    8453              : 
    8454              : /* This is mostly a copy of parse.cc(add_global_procedure) but modified to
    8455              :    pass the name of the entry, rather than the gfc_current_block name, and
    8456              :    to return false upon finding an existing global entry.  */
    8457              : 
    8458              : static bool
    8459          539 : add_global_entry (const char *name, const char *binding_label, bool sub,
    8460              :                   locus *where)
    8461              : {
    8462          539 :   gfc_gsymbol *s;
    8463          539 :   enum gfc_symbol_type type;
    8464              : 
    8465          539 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    8466              : 
    8467              :   /* Only in Fortran 2003: For procedures with a binding label also the Fortran
    8468              :      name is a global identifier.  */
    8469          539 :   if (!binding_label || gfc_notification_std (GFC_STD_F2008))
    8470              :     {
    8471          516 :       s = gfc_get_gsymbol (name, false);
    8472              : 
    8473          516 :       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
    8474              :         {
    8475            2 :           gfc_global_used (s, where);
    8476            2 :           return false;
    8477              :         }
    8478              :       else
    8479              :         {
    8480          514 :           s->type = type;
    8481          514 :           s->sym_name = name;
    8482          514 :           s->where = *where;
    8483          514 :           s->defined = 1;
    8484          514 :           s->ns = gfc_current_ns;
    8485              :         }
    8486              :     }
    8487              : 
    8488              :   /* Don't add the symbol multiple times.  */
    8489          537 :   if (binding_label
    8490          537 :       && (!gfc_notification_std (GFC_STD_F2008)
    8491            0 :           || strcmp (name, binding_label) != 0))
    8492              :     {
    8493           23 :       s = gfc_get_gsymbol (binding_label, true);
    8494              : 
    8495           23 :       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
    8496              :         {
    8497            1 :           gfc_global_used (s, where);
    8498            1 :           return false;
    8499              :         }
    8500              :       else
    8501              :         {
    8502           22 :           s->type = type;
    8503           22 :           s->sym_name = gfc_get_string ("%s", name);
    8504           22 :           s->binding_label = binding_label;
    8505           22 :           s->where = *where;
    8506           22 :           s->defined = 1;
    8507           22 :           s->ns = gfc_current_ns;
    8508              :         }
    8509              :     }
    8510              : 
    8511              :   return true;
    8512              : }
    8513              : 
    8514              : 
    8515              : /* Match an ENTRY statement.  */
    8516              : 
    8517              : match
    8518          805 : gfc_match_entry (void)
    8519              : {
    8520          805 :   gfc_symbol *proc;
    8521          805 :   gfc_symbol *result;
    8522          805 :   gfc_symbol *entry;
    8523          805 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8524          805 :   gfc_compile_state state;
    8525          805 :   match m;
    8526          805 :   gfc_entry_list *el;
    8527          805 :   locus old_loc;
    8528          805 :   bool module_procedure;
    8529          805 :   char peek_char;
    8530          805 :   match is_bind_c;
    8531              : 
    8532          805 :   m = gfc_match_name (name);
    8533          805 :   if (m != MATCH_YES)
    8534              :     return m;
    8535              : 
    8536          805 :   if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
    8537              :     return MATCH_ERROR;
    8538              : 
    8539          805 :   state = gfc_current_state ();
    8540          805 :   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
    8541              :     {
    8542            3 :       switch (state)
    8543              :         {
    8544            0 :           case COMP_PROGRAM:
    8545            0 :             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
    8546            0 :             break;
    8547            0 :           case COMP_MODULE:
    8548            0 :             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
    8549            0 :             break;
    8550            0 :           case COMP_SUBMODULE:
    8551            0 :             gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
    8552            0 :             break;
    8553            0 :           case COMP_BLOCK_DATA:
    8554            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8555              :                        "a BLOCK DATA");
    8556            0 :             break;
    8557            0 :           case COMP_INTERFACE:
    8558            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8559              :                        "an INTERFACE");
    8560            0 :             break;
    8561            1 :           case COMP_STRUCTURE:
    8562            1 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8563              :                        "a STRUCTURE block");
    8564            1 :             break;
    8565            0 :           case COMP_DERIVED:
    8566            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8567              :                        "a DERIVED TYPE block");
    8568            0 :             break;
    8569            0 :           case COMP_IF:
    8570            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8571              :                        "an IF-THEN block");
    8572            0 :             break;
    8573            0 :           case COMP_DO:
    8574            0 :           case COMP_DO_CONCURRENT:
    8575            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8576              :                        "a DO block");
    8577            0 :             break;
    8578            0 :           case COMP_SELECT:
    8579            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8580              :                        "a SELECT block");
    8581            0 :             break;
    8582            0 :           case COMP_FORALL:
    8583            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8584              :                        "a FORALL block");
    8585            0 :             break;
    8586            0 :           case COMP_WHERE:
    8587            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8588              :                        "a WHERE block");
    8589            0 :             break;
    8590            0 :           case COMP_CONTAINS:
    8591            0 :             gfc_error ("ENTRY statement at %C cannot appear within "
    8592              :                        "a contained subprogram");
    8593            0 :             break;
    8594            2 :           default:
    8595            2 :             gfc_error ("Unexpected ENTRY statement at %C");
    8596              :         }
    8597              :       return MATCH_ERROR;
    8598              :     }
    8599              : 
    8600          802 :   if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
    8601          802 :       && gfc_state_stack->previous->state == COMP_INTERFACE)
    8602              :     {
    8603            1 :       gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
    8604            1 :       return MATCH_ERROR;
    8605              :     }
    8606              : 
    8607         1602 :   module_procedure = gfc_current_ns->parent != NULL
    8608          260 :                    && gfc_current_ns->parent->proc_name
    8609          801 :                    && gfc_current_ns->parent->proc_name->attr.flavor
    8610          260 :                       == FL_MODULE;
    8611              : 
    8612          801 :   if (gfc_current_ns->parent != NULL
    8613          260 :       && gfc_current_ns->parent->proc_name
    8614          260 :       && !module_procedure)
    8615              :     {
    8616            0 :       gfc_error("ENTRY statement at %C cannot appear in a "
    8617              :                 "contained procedure");
    8618            0 :       return MATCH_ERROR;
    8619              :     }
    8620              : 
    8621              :   /* Module function entries need special care in get_proc_name
    8622              :      because previous references within the function will have
    8623              :      created symbols attached to the current namespace.  */
    8624         1342 :   if (get_proc_name (name, &entry,
    8625              :                      gfc_current_ns->parent != NULL
    8626              :                      && module_procedure))
    8627              :     return MATCH_ERROR;
    8628              : 
    8629          799 :   proc = gfc_current_block ();
    8630              : 
    8631              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8632              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8633              :      not allowed for procedures.  */
    8634          799 :   if (entry->attr.is_bind_c == 1)
    8635              :     {
    8636            0 :       locus loc;
    8637              : 
    8638            0 :       entry->attr.is_bind_c = 0;
    8639              : 
    8640            0 :       loc = entry->old_symbol != NULL
    8641            0 :         ? entry->old_symbol->declared_at : gfc_current_locus;
    8642            0 :       gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8643              :                      "variables or common blocks", &loc);
    8644              :      }
    8645              : 
    8646              :   /* Check what next non-whitespace character is so we can tell if there
    8647              :      is the required parens if we have a BIND(C).  */
    8648          799 :   old_loc = gfc_current_locus;
    8649          799 :   gfc_gobble_whitespace ();
    8650          799 :   peek_char = gfc_peek_ascii_char ();
    8651              : 
    8652          799 :   if (state == COMP_SUBROUTINE)
    8653              :     {
    8654          138 :       m = gfc_match_formal_arglist (entry, 0, 1);
    8655          138 :       if (m != MATCH_YES)
    8656              :         return MATCH_ERROR;
    8657              : 
    8658              :       /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
    8659              :          never be an internal procedure.  */
    8660          138 :       is_bind_c = gfc_match_bind_c (entry, true);
    8661          138 :       if (is_bind_c == MATCH_ERROR)
    8662              :         return MATCH_ERROR;
    8663          138 :       if (is_bind_c == MATCH_YES)
    8664              :         {
    8665           22 :           if (peek_char != '(')
    8666              :             {
    8667            0 :               gfc_error ("Missing required parentheses before BIND(C) at %C");
    8668            0 :               return MATCH_ERROR;
    8669              :             }
    8670              : 
    8671           22 :           if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
    8672           22 :                                   &(entry->declared_at), 1))
    8673              :             return MATCH_ERROR;
    8674              : 
    8675              :         }
    8676              : 
    8677          138 :       if (!gfc_current_ns->parent
    8678          138 :           && !add_global_entry (name, entry->binding_label, true,
    8679              :                                 &old_loc))
    8680              :         return MATCH_ERROR;
    8681              : 
    8682              :       /* An entry in a subroutine.  */
    8683          135 :       if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8684          135 :           || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
    8685              :         return MATCH_ERROR;
    8686              :     }
    8687              :   else
    8688              :     {
    8689              :       /* An entry in a function.
    8690              :          We need to take special care because writing
    8691              :             ENTRY f()
    8692              :          as
    8693              :             ENTRY f
    8694              :          is allowed, whereas
    8695              :             ENTRY f() RESULT (r)
    8696              :          can't be written as
    8697              :             ENTRY f RESULT (r).  */
    8698          661 :       if (gfc_match_eos () == MATCH_YES)
    8699              :         {
    8700           24 :           gfc_current_locus = old_loc;
    8701              :           /* Match the empty argument list, and add the interface to
    8702              :              the symbol.  */
    8703           24 :           m = gfc_match_formal_arglist (entry, 0, 1);
    8704              :         }
    8705              :       else
    8706          637 :         m = gfc_match_formal_arglist (entry, 0, 0);
    8707              : 
    8708          661 :       if (m != MATCH_YES)
    8709              :         return MATCH_ERROR;
    8710              : 
    8711          660 :       result = NULL;
    8712              : 
    8713          660 :       if (gfc_match_eos () == MATCH_YES)
    8714              :         {
    8715          411 :           if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8716          411 :               || !gfc_add_function (&entry->attr, entry->name, NULL))
    8717              :             return MATCH_ERROR;
    8718              : 
    8719          409 :           entry->result = entry;
    8720              :         }
    8721              :       else
    8722              :         {
    8723          249 :           m = gfc_match_suffix (entry, &result);
    8724          249 :           if (m == MATCH_NO)
    8725            0 :             gfc_syntax_error (ST_ENTRY);
    8726          249 :           if (m != MATCH_YES)
    8727              :             return MATCH_ERROR;
    8728              : 
    8729          249 :           if (result)
    8730              :             {
    8731          212 :               if (!gfc_add_result (&result->attr, result->name, NULL)
    8732          212 :                   || !gfc_add_entry (&entry->attr, result->name, NULL)
    8733          424 :                   || !gfc_add_function (&entry->attr, result->name, NULL))
    8734              :                 return MATCH_ERROR;
    8735          212 :               entry->result = result;
    8736              :             }
    8737              :           else
    8738              :             {
    8739           37 :               if (!gfc_add_entry (&entry->attr, entry->name, NULL)
    8740           37 :                   || !gfc_add_function (&entry->attr, entry->name, NULL))
    8741              :                 return MATCH_ERROR;
    8742           37 :               entry->result = entry;
    8743              :             }
    8744              :         }
    8745              : 
    8746          658 :       if (!gfc_current_ns->parent
    8747          658 :           && !add_global_entry (name, entry->binding_label, false,
    8748              :                                 &old_loc))
    8749              :         return MATCH_ERROR;
    8750              :     }
    8751              : 
    8752          790 :   if (gfc_match_eos () != MATCH_YES)
    8753              :     {
    8754            0 :       gfc_syntax_error (ST_ENTRY);
    8755            0 :       return MATCH_ERROR;
    8756              :     }
    8757              : 
    8758              :   /* F2018:C1546 An elemental procedure shall not have the BIND attribute.  */
    8759          790 :   if (proc->attr.elemental && entry->attr.is_bind_c)
    8760              :     {
    8761            2 :       gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
    8762              :                  "elemental procedure", &entry->declared_at);
    8763            2 :       return MATCH_ERROR;
    8764              :     }
    8765              : 
    8766          788 :   entry->attr.recursive = proc->attr.recursive;
    8767          788 :   entry->attr.elemental = proc->attr.elemental;
    8768          788 :   entry->attr.pure = proc->attr.pure;
    8769              : 
    8770          788 :   el = gfc_get_entry_list ();
    8771          788 :   el->sym = entry;
    8772          788 :   el->next = gfc_current_ns->entries;
    8773          788 :   gfc_current_ns->entries = el;
    8774          788 :   if (el->next)
    8775           85 :     el->id = el->next->id + 1;
    8776              :   else
    8777              :     el->id = 1;
    8778              : 
    8779          788 :   new_st.op = EXEC_ENTRY;
    8780          788 :   new_st.ext.entry = el;
    8781              : 
    8782          788 :   return MATCH_YES;
    8783              : }
    8784              : 
    8785              : 
    8786              : /* Match a subroutine statement, including optional prefixes.  */
    8787              : 
    8788              : match
    8789       819890 : gfc_match_subroutine (void)
    8790              : {
    8791       819890 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    8792       819890 :   gfc_symbol *sym;
    8793       819890 :   match m;
    8794       819890 :   match is_bind_c;
    8795       819890 :   char peek_char;
    8796       819890 :   bool allow_binding_name;
    8797       819890 :   locus loc;
    8798              : 
    8799       819890 :   if (gfc_current_state () != COMP_NONE
    8800       777353 :       && gfc_current_state () != COMP_INTERFACE
    8801       754487 :       && gfc_current_state () != COMP_CONTAINS)
    8802              :     return MATCH_NO;
    8803              : 
    8804       108223 :   m = gfc_match_prefix (NULL);
    8805       108223 :   if (m != MATCH_YES)
    8806              :     return m;
    8807              : 
    8808        98097 :   loc = gfc_current_locus;
    8809        98097 :   m = gfc_match ("subroutine% %n", name);
    8810        98097 :   if (m != MATCH_YES)
    8811              :     return m;
    8812              : 
    8813        44009 :   if (get_proc_name (name, &sym, false))
    8814              :     return MATCH_ERROR;
    8815              : 
    8816              :   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
    8817              :      the symbol existed before.  */
    8818        43998 :   sym->declared_at = gfc_get_location_range (NULL, 0, &loc, 1,
    8819              :                                              &gfc_current_locus);
    8820              : 
    8821        43998 :   if (current_attr.module_procedure)
    8822              :     {
    8823          429 :       sym->attr.module_procedure = 1;
    8824          429 :       if (gfc_current_state () == COMP_INTERFACE)
    8825          302 :         gfc_current_ns->has_import_set = 1;
    8826              :     }
    8827              : 
    8828        43998 :   if (add_hidden_procptr_result (sym))
    8829            9 :     sym = sym->result;
    8830              : 
    8831        43998 :   gfc_new_block = sym;
    8832              : 
    8833              :   /* Check what next non-whitespace character is so we can tell if there
    8834              :      is the required parens if we have a BIND(C).  */
    8835        43998 :   gfc_gobble_whitespace ();
    8836        43998 :   peek_char = gfc_peek_ascii_char ();
    8837              : 
    8838        43998 :   if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
    8839              :     return MATCH_ERROR;
    8840              : 
    8841        43995 :   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
    8842              :     return MATCH_ERROR;
    8843              : 
    8844              :   /* Make sure that it isn't already declared as BIND(C).  If it is, it
    8845              :      must have been marked BIND(C) with a BIND(C) attribute and that is
    8846              :      not allowed for procedures.  */
    8847        43995 :   if (sym->attr.is_bind_c == 1)
    8848              :     {
    8849            4 :       sym->attr.is_bind_c = 0;
    8850              : 
    8851            4 :       if (gfc_state_stack->previous
    8852            4 :           && gfc_state_stack->previous->state != COMP_SUBMODULE)
    8853              :         {
    8854            2 :           locus loc;
    8855            2 :           loc = sym->old_symbol != NULL
    8856            2 :             ? sym->old_symbol->declared_at : gfc_current_locus;
    8857            2 :           gfc_error_now ("BIND(C) attribute at %L can only be used for "
    8858              :                          "variables or common blocks", &loc);
    8859              :         }
    8860              :     }
    8861              : 
    8862              :   /* C binding names are not allowed for internal procedures.  */
    8863        43995 :   if (gfc_current_state () == COMP_CONTAINS
    8864        26886 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    8865              :     allow_binding_name = false;
    8866              :   else
    8867        28657 :     allow_binding_name = true;
    8868              : 
    8869              :   /* Here, we are just checking if it has the bind(c) attribute, and if
    8870              :      so, then we need to make sure it's all correct.  If it doesn't,
    8871              :      we still need to continue matching the rest of the subroutine line.  */
    8872        43995 :   gfc_gobble_whitespace ();
    8873        43995 :   loc = gfc_current_locus;
    8874        43995 :   is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
    8875        43995 :   if (is_bind_c == MATCH_ERROR)
    8876              :     {
    8877              :       /* There was an attempt at the bind(c), but it was wrong.  An
    8878              :          error message should have been printed w/in the gfc_match_bind_c
    8879              :          so here we'll just return the MATCH_ERROR.  */
    8880              :       return MATCH_ERROR;
    8881              :     }
    8882              : 
    8883        43982 :   if (is_bind_c == MATCH_YES)
    8884              :     {
    8885         4055 :       gfc_formal_arglist *arg;
    8886              : 
    8887              :       /* The following is allowed in the Fortran 2008 draft.  */
    8888         4055 :       if (gfc_current_state () == COMP_CONTAINS
    8889         1301 :           && sym->ns->proc_name->attr.flavor != FL_MODULE
    8890         4466 :           && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
    8891              :                               "at %L may not be specified for an internal "
    8892              :                               "procedure", &gfc_current_locus))
    8893              :         return MATCH_ERROR;
    8894              : 
    8895         4052 :       if (peek_char != '(')
    8896              :         {
    8897            1 :           gfc_error ("Missing required parentheses before BIND(C) at %C");
    8898            1 :           return MATCH_ERROR;
    8899              :         }
    8900              : 
    8901              :       /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
    8902              :          subprogram and a binding label is specified, it shall be the
    8903              :          same as the binding label specified in the corresponding module
    8904              :          procedure interface body.  */
    8905         4051 :       if (sym->attr.module_procedure && sym->old_symbol
    8906            3 :           && strcmp (sym->name, sym->old_symbol->name) == 0
    8907            3 :           && sym->binding_label && sym->old_symbol->binding_label
    8908            2 :           && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
    8909              :         {
    8910            1 :           const char *null = "NULL", *s1, *s2;
    8911            1 :           s1 = sym->binding_label;
    8912            1 :           if (!s1) s1 = null;
    8913            1 :           s2 = sym->old_symbol->binding_label;
    8914            1 :           if (!s2) s2 = null;
    8915            1 :           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
    8916            1 :           sym->refs++;       /* Needed to avoid an ICE in gfc_release_symbol */
    8917            1 :           return MATCH_ERROR;
    8918              :         }
    8919              : 
    8920              :       /* Scan the dummy arguments for an alternate return.  */
    8921        12539 :       for (arg = sym->formal; arg; arg = arg->next)
    8922         8490 :         if (!arg->sym)
    8923              :           {
    8924            1 :             gfc_error ("Alternate return dummy argument cannot appear in a "
    8925              :                        "SUBROUTINE with the BIND(C) attribute at %L", &loc);
    8926            1 :             return MATCH_ERROR;
    8927              :           }
    8928              : 
    8929         4049 :       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
    8930              :         return MATCH_ERROR;
    8931              :     }
    8932              : 
    8933        43975 :   if (gfc_match_eos () != MATCH_YES)
    8934              :     {
    8935            1 :       gfc_syntax_error (ST_SUBROUTINE);
    8936            1 :       return MATCH_ERROR;
    8937              :     }
    8938              : 
    8939        43974 :   if (!copy_prefix (&sym->attr, &sym->declared_at))
    8940              :     {
    8941            4 :       if(!sym->attr.module_procedure)
    8942              :         return MATCH_ERROR;
    8943              :       else
    8944            3 :         gfc_error_check ();
    8945              :     }
    8946              : 
    8947              :   /* Warn if it has the same name as an intrinsic.  */
    8948        43973 :   do_warn_intrinsic_shadow (sym, false);
    8949              : 
    8950        43973 :   return MATCH_YES;
    8951              : }
    8952              : 
    8953              : 
    8954              : /* Check that the NAME identifier in a BIND attribute or statement
    8955              :    is conform to C identifier rules.  */
    8956              : 
    8957              : match
    8958         1187 : check_bind_name_identifier (char **name)
    8959              : {
    8960         1187 :   char *n = *name, *p;
    8961              : 
    8962              :   /* Remove leading spaces.  */
    8963         1213 :   while (*n == ' ')
    8964           26 :     n++;
    8965              : 
    8966              :   /* On an empty string, free memory and set name to NULL.  */
    8967         1187 :   if (*n == '\0')
    8968              :     {
    8969           42 :       free (*name);
    8970           42 :       *name = NULL;
    8971           42 :       return MATCH_YES;
    8972              :     }
    8973              : 
    8974              :   /* Remove trailing spaces.  */
    8975         1145 :   p = n + strlen(n) - 1;
    8976         1161 :   while (*p == ' ')
    8977           16 :     *(p--) = '\0';
    8978              : 
    8979              :   /* Insert the identifier into the symbol table.  */
    8980         1145 :   p = xstrdup (n);
    8981         1145 :   free (*name);
    8982         1145 :   *name = p;
    8983              : 
    8984              :   /* Now check that identifier is valid under C rules.  */
    8985         1145 :   if (ISDIGIT (*p))
    8986              :     {
    8987            2 :       gfc_error ("Invalid C identifier in NAME= specifier at %C");
    8988            2 :       return MATCH_ERROR;
    8989              :     }
    8990              : 
    8991        12512 :   for (; *p; p++)
    8992        11372 :     if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
    8993              :       {
    8994            3 :         gfc_error ("Invalid C identifier in NAME= specifier at %C");
    8995            3 :         return MATCH_ERROR;
    8996              :       }
    8997              : 
    8998              :   return MATCH_YES;
    8999              : }
    9000              : 
    9001              : 
    9002              : /* Match a BIND(C) specifier, with the optional 'name=' specifier if
    9003              :    given, and set the binding label in either the given symbol (if not
    9004              :    NULL), or in the current_ts.  The symbol may be NULL because we may
    9005              :    encounter the BIND(C) before the declaration itself.  Return
    9006              :    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
    9007              :    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
    9008              :    or MATCH_YES if the specifier was correct and the binding label and
    9009              :    bind(c) fields were set correctly for the given symbol or the
    9010              :    current_ts. If allow_binding_name is false, no binding name may be
    9011              :    given.  */
    9012              : 
    9013              : match
    9014        53138 : gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
    9015              : {
    9016        53138 :   char *binding_label = NULL;
    9017        53138 :   gfc_expr *e = NULL;
    9018              : 
    9019              :   /* Initialize the flag that specifies whether we encountered a NAME=
    9020              :      specifier or not.  */
    9021        53138 :   has_name_equals = 0;
    9022              : 
    9023              :   /* This much we have to be able to match, in this order, if
    9024              :      there is a bind(c) label.  */
    9025        53138 :   if (gfc_match (" bind ( c ") != MATCH_YES)
    9026              :     return MATCH_NO;
    9027              : 
    9028              :   /* Now see if there is a binding label, or if we've reached the
    9029              :      end of the bind(c) attribute without one.  */
    9030         7460 :   if (gfc_match_char (',') == MATCH_YES)
    9031              :     {
    9032         1194 :       if (gfc_match (" name = ") != MATCH_YES)
    9033              :         {
    9034            1 :           gfc_error ("Syntax error in NAME= specifier for binding label "
    9035              :                      "at %C");
    9036              :           /* should give an error message here */
    9037            1 :           return MATCH_ERROR;
    9038              :         }
    9039              : 
    9040         1193 :       has_name_equals = 1;
    9041              : 
    9042         1193 :       if (gfc_match_init_expr (&e) != MATCH_YES)
    9043              :         {
    9044            2 :           gfc_free_expr (e);
    9045            2 :           return MATCH_ERROR;
    9046              :         }
    9047              : 
    9048         1191 :       if (!gfc_simplify_expr(e, 0))
    9049              :         {
    9050            0 :           gfc_error ("NAME= specifier at %C should be a constant expression");
    9051            0 :           gfc_free_expr (e);
    9052            0 :           return MATCH_ERROR;
    9053              :         }
    9054              : 
    9055         1191 :       if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
    9056         1188 :           || e->ts.kind != gfc_default_character_kind || e->rank != 0)
    9057              :         {
    9058            4 :           gfc_error ("NAME= specifier at %C should be a scalar of "
    9059              :                      "default character kind");
    9060            4 :           gfc_free_expr(e);
    9061            4 :           return MATCH_ERROR;
    9062              :         }
    9063              : 
    9064              :       // Get a C string from the Fortran string constant
    9065         2374 :       binding_label = gfc_widechar_to_char (e->value.character.string,
    9066         1187 :                                             e->value.character.length);
    9067         1187 :       gfc_free_expr(e);
    9068              : 
    9069              :       // Check that it is valid (old gfc_match_name_C)
    9070         1187 :       if (check_bind_name_identifier (&binding_label) != MATCH_YES)
    9071              :         return MATCH_ERROR;
    9072              :     }
    9073              : 
    9074              :   /* Get the required right paren.  */
    9075         7448 :   if (gfc_match_char (')') != MATCH_YES)
    9076              :     {
    9077            1 :       gfc_error ("Missing closing paren for binding label at %C");
    9078            1 :       return MATCH_ERROR;
    9079              :     }
    9080              : 
    9081         7447 :   if (has_name_equals && !allow_binding_name)
    9082              :     {
    9083            6 :       gfc_error ("No binding name is allowed in BIND(C) at %C");
    9084            6 :       return MATCH_ERROR;
    9085              :     }
    9086              : 
    9087         7441 :   if (has_name_equals && sym != NULL && sym->attr.dummy)
    9088              :     {
    9089            2 :       gfc_error ("For dummy procedure %s, no binding name is "
    9090              :                  "allowed in BIND(C) at %C", sym->name);
    9091            2 :       return MATCH_ERROR;
    9092              :     }
    9093              : 
    9094              : 
    9095              :   /* Save the binding label to the symbol.  If sym is null, we're
    9096              :      probably matching the typespec attributes of a declaration and
    9097              :      haven't gotten the name yet, and therefore, no symbol yet.  */
    9098         7439 :   if (binding_label)
    9099              :     {
    9100         1133 :       if (sym != NULL)
    9101         1023 :         sym->binding_label = binding_label;
    9102              :       else
    9103          110 :         curr_binding_label = binding_label;
    9104              :     }
    9105         6306 :   else if (allow_binding_name)
    9106              :     {
    9107              :       /* No binding label, but if symbol isn't null, we
    9108              :          can set the label for it here.
    9109              :          If name="" or allow_binding_name is false, no C binding name is
    9110              :          created.  */
    9111         5877 :       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
    9112         5710 :         sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
    9113              :     }
    9114              : 
    9115         7439 :   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
    9116          741 :       && current_interface.type == INTERFACE_ABSTRACT)
    9117              :     {
    9118            1 :       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
    9119            1 :       return MATCH_ERROR;
    9120              :     }
    9121              : 
    9122              :   return MATCH_YES;
    9123              : }
    9124              : 
    9125              : 
    9126              : /* Return nonzero if we're currently compiling a contained procedure.  */
    9127              : 
    9128              : static int
    9129        64529 : contained_procedure (void)
    9130              : {
    9131        64529 :   gfc_state_data *s = gfc_state_stack;
    9132              : 
    9133        64529 :   if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
    9134        63542 :       && s->previous != NULL && s->previous->state == COMP_CONTAINS)
    9135        37447 :     return 1;
    9136              : 
    9137              :   return 0;
    9138              : }
    9139              : 
    9140              : /* Set the kind of each enumerator.  The kind is selected such that it is
    9141              :    interoperable with the corresponding C enumeration type, making
    9142              :    sure that -fshort-enums is honored.  */
    9143              : 
    9144              : static void
    9145          158 : set_enum_kind(void)
    9146              : {
    9147          158 :   enumerator_history *current_history = NULL;
    9148          158 :   int kind;
    9149          158 :   int i;
    9150              : 
    9151          158 :   if (max_enum == NULL || enum_history == NULL)
    9152              :     return;
    9153              : 
    9154          150 :   if (!flag_short_enums)
    9155              :     return;
    9156              : 
    9157              :   i = 0;
    9158           48 :   do
    9159              :     {
    9160           48 :       kind = gfc_integer_kinds[i++].kind;
    9161              :     }
    9162           48 :   while (kind < gfc_c_int_kind
    9163           72 :          && gfc_check_integer_range (max_enum->initializer->value.integer,
    9164              :                                      kind) != ARITH_OK);
    9165              : 
    9166           24 :   current_history = enum_history;
    9167           96 :   while (current_history != NULL)
    9168              :     {
    9169           72 :       current_history->sym->ts.kind = kind;
    9170           72 :       current_history = current_history->next;
    9171              :     }
    9172              : }
    9173              : 
    9174              : 
    9175              : /* Match any of the various end-block statements.  Returns the type of
    9176              :    END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
    9177              :    and END BLOCK statements cannot be replaced by a single END statement.  */
    9178              : 
    9179              : match
    9180       189341 : gfc_match_end (gfc_statement *st)
    9181              : {
    9182       189341 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    9183       189341 :   gfc_compile_state state;
    9184       189341 :   locus old_loc;
    9185       189341 :   const char *block_name;
    9186       189341 :   const char *target;
    9187       189341 :   int eos_ok;
    9188       189341 :   match m;
    9189       189341 :   gfc_namespace *parent_ns, *ns, *prev_ns;
    9190       189341 :   gfc_namespace **nsp;
    9191       189341 :   bool abbreviated_modproc_decl = false;
    9192       189341 :   bool got_matching_end = false;
    9193              : 
    9194       189341 :   old_loc = gfc_current_locus;
    9195       189341 :   if (gfc_match ("end") != MATCH_YES)
    9196              :     return MATCH_NO;
    9197              : 
    9198       184195 :   state = gfc_current_state ();
    9199       101091 :   block_name = gfc_current_block () == NULL
    9200       184195 :              ? NULL : gfc_current_block ()->name;
    9201              : 
    9202       184195 :   switch (state)
    9203              :     {
    9204         3268 :     case COMP_ASSOCIATE:
    9205         3268 :     case COMP_BLOCK:
    9206         3268 :     case COMP_CHANGE_TEAM:
    9207         3268 :       if (startswith (block_name, "block@"))
    9208              :         block_name = NULL;
    9209              :       break;
    9210              : 
    9211        17973 :     case COMP_CONTAINS:
    9212        17973 :     case COMP_DERIVED_CONTAINS:
    9213        17973 :     case COMP_OMP_BEGIN_METADIRECTIVE:
    9214        17973 :       state = gfc_state_stack->previous->state;
    9215        16407 :       block_name = gfc_state_stack->previous->sym == NULL
    9216        17973 :                  ? NULL : gfc_state_stack->previous->sym->name;
    9217        17973 :       abbreviated_modproc_decl = gfc_state_stack->previous->sym
    9218        17973 :                 && gfc_state_stack->previous->sym->abr_modproc_decl;
    9219              :       break;
    9220              : 
    9221              :     case COMP_OMP_METADIRECTIVE:
    9222              :       {
    9223              :         /* Metadirectives can be nested, so we need to drill down to the
    9224              :            first state that is not COMP_OMP_METADIRECTIVE.  */
    9225              :         gfc_state_data *state_data = gfc_state_stack;
    9226              : 
    9227           93 :         do
    9228              :           {
    9229           93 :             state_data = state_data->previous;
    9230           93 :             state = state_data->state;
    9231           81 :             block_name = (state_data->sym == NULL
    9232           93 :                           ? NULL : state_data->sym->name);
    9233          186 :             abbreviated_modproc_decl = (state_data->sym
    9234           93 :                                         && state_data->sym->abr_modproc_decl);
    9235              :           }
    9236           93 :         while (state == COMP_OMP_METADIRECTIVE);
    9237              : 
    9238           87 :         if (block_name && startswith (block_name, "block@"))
    9239              :           block_name = NULL;
    9240              :       }
    9241              :       break;
    9242              : 
    9243              :     default:
    9244              :       break;
    9245              :     }
    9246              : 
    9247           87 :   if (!abbreviated_modproc_decl)
    9248       184194 :     abbreviated_modproc_decl = gfc_current_block ()
    9249       184194 :                               && gfc_current_block ()->abr_modproc_decl;
    9250              : 
    9251       184195 :   switch (state)
    9252              :     {
    9253        28428 :     case COMP_NONE:
    9254        28428 :     case COMP_PROGRAM:
    9255        28428 :       *st = ST_END_PROGRAM;
    9256        28428 :       target = " program";
    9257        28428 :       eos_ok = 1;
    9258        28428 :       break;
    9259              : 
    9260        44166 :     case COMP_SUBROUTINE:
    9261        44166 :       *st = ST_END_SUBROUTINE;
    9262        44166 :       if (!abbreviated_modproc_decl)
    9263              :         target = " subroutine";
    9264              :       else
    9265          148 :         target = " procedure";
    9266        44166 :       eos_ok = !contained_procedure ();
    9267        44166 :       break;
    9268              : 
    9269        20363 :     case COMP_FUNCTION:
    9270        20363 :       *st = ST_END_FUNCTION;
    9271        20363 :       if (!abbreviated_modproc_decl)
    9272              :         target = " function";
    9273              :       else
    9274          117 :         target = " procedure";
    9275        20363 :       eos_ok = !contained_procedure ();
    9276        20363 :       break;
    9277              : 
    9278           87 :     case COMP_BLOCK_DATA:
    9279           87 :       *st = ST_END_BLOCK_DATA;
    9280           87 :       target = " block data";
    9281           87 :       eos_ok = 1;
    9282           87 :       break;
    9283              : 
    9284        10118 :     case COMP_MODULE:
    9285        10118 :       *st = ST_END_MODULE;
    9286        10118 :       target = " module";
    9287        10118 :       eos_ok = 1;
    9288        10118 :       break;
    9289              : 
    9290          268 :     case COMP_SUBMODULE:
    9291          268 :       *st = ST_END_SUBMODULE;
    9292          268 :       target = " submodule";
    9293          268 :       eos_ok = 1;
    9294          268 :       break;
    9295              : 
    9296        11372 :     case COMP_INTERFACE:
    9297        11372 :       *st = ST_END_INTERFACE;
    9298        11372 :       target = " interface";
    9299        11372 :       eos_ok = 0;
    9300        11372 :       break;
    9301              : 
    9302          257 :     case COMP_MAP:
    9303          257 :       *st = ST_END_MAP;
    9304          257 :       target = " map";
    9305          257 :       eos_ok = 0;
    9306          257 :       break;
    9307              : 
    9308          132 :     case COMP_UNION:
    9309          132 :       *st = ST_END_UNION;
    9310          132 :       target = " union";
    9311          132 :       eos_ok = 0;
    9312          132 :       break;
    9313              : 
    9314          313 :     case COMP_STRUCTURE:
    9315          313 :       *st = ST_END_STRUCTURE;
    9316          313 :       target = " structure";
    9317          313 :       eos_ok = 0;
    9318          313 :       break;
    9319              : 
    9320        13451 :     case COMP_DERIVED:
    9321        13451 :     case COMP_DERIVED_CONTAINS:
    9322        13451 :       *st = ST_END_TYPE;
    9323        13451 :       target = " type";
    9324        13451 :       eos_ok = 0;
    9325        13451 :       break;
    9326              : 
    9327         1663 :     case COMP_ASSOCIATE:
    9328         1663 :       *st = ST_END_ASSOCIATE;
    9329         1663 :       target = " associate";
    9330         1663 :       eos_ok = 0;
    9331         1663 :       break;
    9332              : 
    9333         1527 :     case COMP_BLOCK:
    9334         1527 :     case COMP_OMP_STRICTLY_STRUCTURED_BLOCK:
    9335         1527 :       *st = ST_END_BLOCK;
    9336         1527 :       target = " block";
    9337         1527 :       eos_ok = 0;
    9338         1527 :       break;
    9339              : 
    9340        15025 :     case COMP_IF:
    9341        15025 :       *st = ST_ENDIF;
    9342        15025 :       target = " if";
    9343        15025 :       eos_ok = 0;
    9344        15025 :       break;
    9345              : 
    9346        31088 :     case COMP_DO:
    9347        31088 :     case COMP_DO_CONCURRENT:
    9348        31088 :       *st = ST_ENDDO;
    9349        31088 :       target = " do";
    9350        31088 :       eos_ok = 0;
    9351        31088 :       break;
    9352              : 
    9353           54 :     case COMP_CRITICAL:
    9354           54 :       *st = ST_END_CRITICAL;
    9355           54 :       target = " critical";
    9356           54 :       eos_ok = 0;
    9357           54 :       break;
    9358              : 
    9359         4726 :     case COMP_SELECT:
    9360         4726 :     case COMP_SELECT_TYPE:
    9361         4726 :     case COMP_SELECT_RANK:
    9362         4726 :       *st = ST_END_SELECT;
    9363         4726 :       target = " select";
    9364         4726 :       eos_ok = 0;
    9365         4726 :       break;
    9366              : 
    9367          509 :     case COMP_FORALL:
    9368          509 :       *st = ST_END_FORALL;
    9369          509 :       target = " forall";
    9370          509 :       eos_ok = 0;
    9371          509 :       break;
    9372              : 
    9373          373 :     case COMP_WHERE:
    9374          373 :       *st = ST_END_WHERE;
    9375          373 :       target = " where";
    9376          373 :       eos_ok = 0;
    9377          373 :       break;
    9378              : 
    9379          158 :     case COMP_ENUM:
    9380          158 :       *st = ST_END_ENUM;
    9381          158 :       target = " enum";
    9382          158 :       eos_ok = 0;
    9383          158 :       last_initializer = NULL;
    9384          158 :       set_enum_kind ();
    9385          158 :       gfc_free_enum_history ();
    9386          158 :       break;
    9387              : 
    9388            0 :     case COMP_OMP_BEGIN_METADIRECTIVE:
    9389            0 :       *st = ST_OMP_END_METADIRECTIVE;
    9390            0 :       target = " metadirective";
    9391            0 :       eos_ok = 0;
    9392            0 :       break;
    9393              : 
    9394          108 :     case COMP_CHANGE_TEAM:
    9395          108 :       *st = ST_END_TEAM;
    9396          108 :       target = " team";
    9397          108 :       eos_ok = 0;
    9398          108 :       break;
    9399              : 
    9400            9 :     default:
    9401            9 :       gfc_error ("Unexpected END statement at %C");
    9402            9 :       goto cleanup;
    9403              :     }
    9404              : 
    9405       184186 :   old_loc = gfc_current_locus;
    9406       184186 :   if (gfc_match_eos () == MATCH_YES)
    9407              :     {
    9408        20894 :       if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
    9409              :         {
    9410         8253 :           if (!gfc_notify_std (GFC_STD_F2008, "END statement "
    9411              :                                "instead of %s statement at %L",
    9412              :                                abbreviated_modproc_decl ? "END PROCEDURE"
    9413         4114 :                                : gfc_ascii_statement(*st), &old_loc))
    9414            4 :             goto cleanup;
    9415              :         }
    9416            9 :       else if (!eos_ok)
    9417              :         {
    9418              :           /* We would have required END [something].  */
    9419            9 :           gfc_error ("%s statement expected at %L",
    9420              :                      gfc_ascii_statement (*st), &old_loc);
    9421            9 :           goto cleanup;
    9422              :         }
    9423              : 
    9424              :       return MATCH_YES;
    9425              :     }
    9426              : 
    9427              :   /* Verify that we've got the sort of end-block that we're expecting.  */
    9428       163292 :   if (gfc_match (target) != MATCH_YES)
    9429              :     {
    9430          331 :       gfc_error ("Expecting %s statement at %L", abbreviated_modproc_decl
    9431          165 :                  ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
    9432          166 :       goto cleanup;
    9433              :     }
    9434              :   else
    9435       163126 :     got_matching_end = true;
    9436              : 
    9437       163126 :   if (*st == ST_END_TEAM && gfc_match_end_team () == MATCH_ERROR)
    9438              :     /* Emit errors of stat and errmsg parsing now to finish the block and
    9439              :        continue analysis of compilation unit.  */
    9440            2 :     gfc_error_check ();
    9441              : 
    9442       163126 :   old_loc = gfc_current_locus;
    9443              :   /* If we're at the end, make sure a block name wasn't required.  */
    9444       163126 :   if (gfc_match_eos () == MATCH_YES)
    9445              :     {
    9446       107867 :       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
    9447              :           && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
    9448              :           && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL
    9449              :           && *st != ST_END_TEAM)
    9450              :         return MATCH_YES;
    9451              : 
    9452        54574 :       if (!block_name)
    9453              :         return MATCH_YES;
    9454              : 
    9455            8 :       gfc_error ("Expected block name of %qs in %s statement at %L",
    9456              :                  block_name, gfc_ascii_statement (*st), &old_loc);
    9457              : 
    9458            8 :       return MATCH_ERROR;
    9459              :     }
    9460              : 
    9461              :   /* END INTERFACE has a special handler for its several possible endings.  */
    9462        55259 :   if (*st == ST_END_INTERFACE)
    9463          696 :     return gfc_match_end_interface ();
    9464              : 
    9465              :   /* We haven't hit the end of statement, so what is left must be an
    9466              :      end-name.  */
    9467        54563 :   m = gfc_match_space ();
    9468        54563 :   if (m == MATCH_YES)
    9469        54563 :     m = gfc_match_name (name);
    9470              : 
    9471        54563 :   if (m == MATCH_NO)
    9472            0 :     gfc_error ("Expected terminating name at %C");
    9473        54563 :   if (m != MATCH_YES)
    9474            0 :     goto cleanup;
    9475              : 
    9476        54563 :   if (block_name == NULL)
    9477           15 :     goto syntax;
    9478              : 
    9479              :   /* We have to pick out the declared submodule name from the composite
    9480              :      required by F2008:11.2.3 para 2, which ends in the declared name.  */
    9481        54548 :   if (state == COMP_SUBMODULE)
    9482          137 :     block_name = strchr (block_name, '.') + 1;
    9483              : 
    9484        54548 :   if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
    9485              :     {
    9486            8 :       gfc_error ("Expected label %qs for %s statement at %C", block_name,
    9487              :                  gfc_ascii_statement (*st));
    9488            8 :       goto cleanup;
    9489              :     }
    9490              :   /* Procedure pointer as function result.  */
    9491        54540 :   else if (strcmp (block_name, "ppr@") == 0
    9492           21 :            && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
    9493              :     {
    9494            0 :       gfc_error ("Expected label %qs for %s statement at %C",
    9495            0 :                  gfc_current_block ()->ns->proc_name->name,
    9496              :                  gfc_ascii_statement (*st));
    9497            0 :       goto cleanup;
    9498              :     }
    9499              : 
    9500        54540 :   if (gfc_match_eos () == MATCH_YES)
    9501              :     return MATCH_YES;
    9502              : 
    9503            0 : syntax:
    9504           15 :   gfc_syntax_error (*st);
    9505              : 
    9506          211 : cleanup:
    9507          211 :   gfc_current_locus = old_loc;
    9508              : 
    9509              :   /* If we are missing an END BLOCK, we created a half-ready namespace.
    9510              :      Remove it from the parent namespace's sibling list.  */
    9511              : 
    9512          211 :   if (state == COMP_BLOCK && !got_matching_end)
    9513              :     {
    9514            7 :       parent_ns = gfc_current_ns->parent;
    9515              : 
    9516            7 :       nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
    9517              : 
    9518            7 :       prev_ns = NULL;
    9519            7 :       ns = *nsp;
    9520           14 :       while (ns)
    9521              :         {
    9522            7 :           if (ns == gfc_current_ns)
    9523              :             {
    9524            7 :               if (prev_ns == NULL)
    9525            7 :                 *nsp = NULL;
    9526              :               else
    9527            0 :                 prev_ns->sibling = ns->sibling;
    9528              :             }
    9529            7 :           prev_ns = ns;
    9530            7 :           ns = ns->sibling;
    9531              :         }
    9532              : 
    9533              :       /* The namespace can still be referenced by parser state and code nodes;
    9534              :          let normal block unwinding/freeing own its lifetime.  */
    9535            7 :       gfc_current_ns = parent_ns;
    9536            7 :       gfc_state_stack = gfc_state_stack->previous;
    9537            7 :       state = gfc_current_state ();
    9538              :     }
    9539              : 
    9540              :   return MATCH_ERROR;
    9541              : }
    9542              : 
    9543              : 
    9544              : 
    9545              : /***************** Attribute declaration statements ****************/
    9546              : 
    9547              : /* Set the attribute of a single variable.  */
    9548              : 
    9549              : static match
    9550        10427 : attr_decl1 (void)
    9551              : {
    9552        10427 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    9553        10427 :   gfc_array_spec *as;
    9554              : 
    9555              :   /* Workaround -Wmaybe-uninitialized false positive during
    9556              :      profiledbootstrap by initializing them.  */
    9557        10427 :   gfc_symbol *sym = NULL;
    9558        10427 :   locus var_locus;
    9559        10427 :   match m;
    9560              : 
    9561        10427 :   as = NULL;
    9562              : 
    9563        10427 :   m = gfc_match_name (name);
    9564        10427 :   if (m != MATCH_YES)
    9565            0 :     goto cleanup;
    9566              : 
    9567        10427 :   if (find_special (name, &sym, false))
    9568              :     return MATCH_ERROR;
    9569              : 
    9570        10427 :   if (!check_function_name (name))
    9571              :     {
    9572            7 :       m = MATCH_ERROR;
    9573            7 :       goto cleanup;
    9574              :     }
    9575              : 
    9576        10420 :   var_locus = gfc_current_locus;
    9577              : 
    9578              :   /* Deal with possible array specification for certain attributes.  */
    9579        10420 :   if (current_attr.dimension
    9580         8841 :       || current_attr.codimension
    9581         8819 :       || current_attr.allocatable
    9582         8395 :       || current_attr.pointer
    9583         7672 :       || current_attr.target)
    9584              :     {
    9585         6174 :       m = gfc_match_array_spec (&as, !current_attr.codimension,
    9586              :                                 !current_attr.dimension
    9587         1395 :                                 && !current_attr.pointer
    9588              :                                 && !current_attr.target);
    9589         2974 :       if (m == MATCH_ERROR)
    9590            2 :         goto cleanup;
    9591              : 
    9592         2972 :       if (current_attr.dimension && m == MATCH_NO)
    9593              :         {
    9594            0 :           gfc_error ("Missing array specification at %L in DIMENSION "
    9595              :                      "statement", &var_locus);
    9596            0 :           m = MATCH_ERROR;
    9597            0 :           goto cleanup;
    9598              :         }
    9599              : 
    9600         2972 :       if (current_attr.dimension && sym->value)
    9601              :         {
    9602            1 :           gfc_error ("Dimensions specified for %s at %L after its "
    9603              :                      "initialization", sym->name, &var_locus);
    9604            1 :           m = MATCH_ERROR;
    9605            1 :           goto cleanup;
    9606              :         }
    9607              : 
    9608         2971 :       if (current_attr.codimension && m == MATCH_NO)
    9609              :         {
    9610            0 :           gfc_error ("Missing array specification at %L in CODIMENSION "
    9611              :                      "statement", &var_locus);
    9612            0 :           m = MATCH_ERROR;
    9613            0 :           goto cleanup;
    9614              :         }
    9615              : 
    9616         2971 :       if ((current_attr.allocatable || current_attr.pointer)
    9617         1147 :           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
    9618              :         {
    9619            0 :           gfc_error ("Array specification must be deferred at %L", &var_locus);
    9620            0 :           m = MATCH_ERROR;
    9621            0 :           goto cleanup;
    9622              :         }
    9623              :     }
    9624              : 
    9625        10417 :   if (sym->ts.type == BT_CLASS
    9626          200 :       && sym->ts.u.derived
    9627          200 :       && sym->ts.u.derived->attr.is_class)
    9628              :     {
    9629          177 :       sym->attr.pointer = CLASS_DATA(sym)->attr.class_pointer;
    9630          177 :       sym->attr.allocatable = CLASS_DATA(sym)->attr.allocatable;
    9631          177 :       sym->attr.dimension = CLASS_DATA(sym)->attr.dimension;
    9632          177 :       sym->attr.codimension = CLASS_DATA(sym)->attr.codimension;
    9633          177 :       if (CLASS_DATA (sym)->as)
    9634          123 :         sym->as = gfc_copy_array_spec (CLASS_DATA (sym)->as);
    9635              :     }
    9636         8840 :   if (current_attr.dimension == 0 && current_attr.codimension == 0
    9637        19236 :       && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
    9638              :     {
    9639           22 :       m = MATCH_ERROR;
    9640           22 :       goto cleanup;
    9641              :     }
    9642        10395 :   if (!gfc_set_array_spec (sym, as, &var_locus))
    9643              :     {
    9644           17 :       m = MATCH_ERROR;
    9645           17 :       goto cleanup;
    9646              :     }
    9647              : 
    9648        10378 :   if (sym->attr.cray_pointee && sym->as != NULL)
    9649              :     {
    9650              :       /* Fix the array spec.  */
    9651            2 :       m = gfc_mod_pointee_as (sym->as);
    9652            2 :       if (m == MATCH_ERROR)
    9653            0 :         goto cleanup;
    9654              :     }
    9655              : 
    9656        10378 :   if (!gfc_add_attribute (&sym->attr, &var_locus))
    9657              :     {
    9658            0 :       m = MATCH_ERROR;
    9659            0 :       goto cleanup;
    9660              :     }
    9661              : 
    9662         5741 :   if ((current_attr.external || current_attr.intrinsic)
    9663         6289 :       && sym->attr.flavor != FL_PROCEDURE
    9664        16635 :       && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
    9665              :     {
    9666            0 :       m = MATCH_ERROR;
    9667            0 :       goto cleanup;
    9668              :     }
    9669              : 
    9670        10378 :   if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class
    9671          169 :       && !as && !current_attr.pointer && !current_attr.allocatable
    9672          136 :       && !current_attr.external)
    9673              :     {
    9674          136 :       sym->attr.pointer = 0;
    9675          136 :       sym->attr.allocatable = 0;
    9676          136 :       sym->attr.dimension = 0;
    9677          136 :       sym->attr.codimension = 0;
    9678          136 :       gfc_free_array_spec (sym->as);
    9679          136 :       sym->as = NULL;
    9680              :     }
    9681        10242 :   else if (sym->ts.type == BT_CLASS
    9682        10242 :       && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
    9683              :     {
    9684            0 :       m = MATCH_ERROR;
    9685            0 :       goto cleanup;
    9686              :     }
    9687              : 
    9688        10378 :   add_hidden_procptr_result (sym);
    9689              : 
    9690        10378 :   return MATCH_YES;
    9691              : 
    9692           49 : cleanup:
    9693           49 :   gfc_free_array_spec (as);
    9694           49 :   return m;
    9695              : }
    9696              : 
    9697              : 
    9698              : /* Generic attribute declaration subroutine.  Used for attributes that
    9699              :    just have a list of names.  */
    9700              : 
    9701              : static match
    9702         6712 : attr_decl (void)
    9703              : {
    9704         6712 :   match m;
    9705              : 
    9706              :   /* Gobble the optional double colon, by simply ignoring the result
    9707              :      of gfc_match().  */
    9708         6712 :   gfc_match (" ::");
    9709              : 
    9710        10427 :   for (;;)
    9711              :     {
    9712        10427 :       m = attr_decl1 ();
    9713        10427 :       if (m != MATCH_YES)
    9714              :         break;
    9715              : 
    9716        10378 :       if (gfc_match_eos () == MATCH_YES)
    9717              :         {
    9718              :           m = MATCH_YES;
    9719              :           break;
    9720              :         }
    9721              : 
    9722         3715 :       if (gfc_match_char (',') != MATCH_YES)
    9723              :         {
    9724            0 :           gfc_error ("Unexpected character in variable list at %C");
    9725            0 :           m = MATCH_ERROR;
    9726            0 :           break;
    9727              :         }
    9728              :     }
    9729              : 
    9730         6712 :   return m;
    9731              : }
    9732              : 
    9733              : 
    9734              : /* This routine matches Cray Pointer declarations of the form:
    9735              :    pointer ( <pointer>, <pointee> )
    9736              :    or
    9737              :    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
    9738              :    The pointer, if already declared, should be an integer.  Otherwise, we
    9739              :    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
    9740              :    be either a scalar, or an array declaration.  No space is allocated for
    9741              :    the pointee.  For the statement
    9742              :    pointer (ipt, ar(10))
    9743              :    any subsequent uses of ar will be translated (in C-notation) as
    9744              :    ar(i) => ((<type> *) ipt)(i)
    9745              :    After gimplification, pointee variable will disappear in the code.  */
    9746              : 
    9747              : static match
    9748          334 : cray_pointer_decl (void)
    9749              : {
    9750          334 :   match m;
    9751          334 :   gfc_array_spec *as = NULL;
    9752          334 :   gfc_symbol *cptr; /* Pointer symbol.  */
    9753          334 :   gfc_symbol *cpte; /* Pointee symbol.  */
    9754          334 :   locus var_locus;
    9755          334 :   bool done = false;
    9756              : 
    9757          334 :   while (!done)
    9758              :     {
    9759          347 :       if (gfc_match_char ('(') != MATCH_YES)
    9760              :         {
    9761            1 :           gfc_error ("Expected %<(%> at %C");
    9762            1 :           return MATCH_ERROR;
    9763              :         }
    9764              : 
    9765              :       /* Match pointer.  */
    9766          346 :       var_locus = gfc_current_locus;
    9767          346 :       gfc_clear_attr (&current_attr);
    9768          346 :       gfc_add_cray_pointer (&current_attr, &var_locus);
    9769          346 :       current_ts.type = BT_INTEGER;
    9770          346 :       current_ts.kind = gfc_index_integer_kind;
    9771              : 
    9772          346 :       m = gfc_match_symbol (&cptr, 0);
    9773          346 :       if (m != MATCH_YES)
    9774              :         {
    9775            2 :           gfc_error ("Expected variable name at %C");
    9776            2 :           return m;
    9777              :         }
    9778              : 
    9779          344 :       if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
    9780              :         return MATCH_ERROR;
    9781              : 
    9782          341 :       gfc_set_sym_referenced (cptr);
    9783              : 
    9784          341 :       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
    9785              :         {
    9786          327 :           cptr->ts.type = BT_INTEGER;
    9787          327 :           cptr->ts.kind = gfc_index_integer_kind;
    9788              :         }
    9789           14 :       else if (cptr->ts.type != BT_INTEGER)
    9790              :         {
    9791            1 :           gfc_error ("Cray pointer at %C must be an integer");
    9792            1 :           return MATCH_ERROR;
    9793              :         }
    9794           13 :       else if (cptr->ts.kind < gfc_index_integer_kind)
    9795            0 :         gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
    9796              :                      " memory addresses require %d bytes",
    9797              :                      cptr->ts.kind, gfc_index_integer_kind);
    9798              : 
    9799          340 :       if (gfc_match_char (',') != MATCH_YES)
    9800              :         {
    9801            2 :           gfc_error ("Expected \",\" at %C");
    9802            2 :           return MATCH_ERROR;
    9803              :         }
    9804              : 
    9805              :       /* Match Pointee.  */
    9806          338 :       var_locus = gfc_current_locus;
    9807          338 :       gfc_clear_attr (&current_attr);
    9808          338 :       gfc_add_cray_pointee (&current_attr, &var_locus);
    9809          338 :       current_ts.type = BT_UNKNOWN;
    9810          338 :       current_ts.kind = 0;
    9811              : 
    9812          338 :       m = gfc_match_symbol (&cpte, 0);
    9813          338 :       if (m != MATCH_YES)
    9814              :         {
    9815            2 :           gfc_error ("Expected variable name at %C");
    9816            2 :           return m;
    9817              :         }
    9818              : 
    9819              :       /* Check for an optional array spec.  */
    9820          336 :       m = gfc_match_array_spec (&as, true, false);
    9821          336 :       if (m == MATCH_ERROR)
    9822              :         {
    9823            0 :           gfc_free_array_spec (as);
    9824            0 :           return m;
    9825              :         }
    9826          336 :       else if (m == MATCH_NO)
    9827              :         {
    9828          226 :           gfc_free_array_spec (as);
    9829          226 :           as = NULL;
    9830              :         }
    9831              : 
    9832          336 :       if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
    9833              :         return MATCH_ERROR;
    9834              : 
    9835          329 :       gfc_set_sym_referenced (cpte);
    9836              : 
    9837          329 :       if (cpte->as == NULL)
    9838              :         {
    9839          247 :           if (!gfc_set_array_spec (cpte, as, &var_locus))
    9840            0 :             gfc_internal_error ("Cannot set Cray pointee array spec.");
    9841              :         }
    9842           82 :       else if (as != NULL)
    9843              :         {
    9844            1 :           gfc_error ("Duplicate array spec for Cray pointee at %C");
    9845            1 :           gfc_free_array_spec (as);
    9846            1 :           return MATCH_ERROR;
    9847              :         }
    9848              : 
    9849          328 :       as = NULL;
    9850              : 
    9851          328 :       if (cpte->as != NULL)
    9852              :         {
    9853              :           /* Fix array spec.  */
    9854          190 :           m = gfc_mod_pointee_as (cpte->as);
    9855          190 :           if (m == MATCH_ERROR)
    9856              :             return m;
    9857              :         }
    9858              : 
    9859              :       /* Point the Pointee at the Pointer.  */
    9860          328 :       cpte->cp_pointer = cptr;
    9861              : 
    9862          328 :       if (gfc_match_char (')') != MATCH_YES)
    9863              :         {
    9864            2 :           gfc_error ("Expected \")\" at %C");
    9865            2 :           return MATCH_ERROR;
    9866              :         }
    9867          326 :       m = gfc_match_char (',');
    9868          326 :       if (m != MATCH_YES)
    9869              :         done = true; /* Stop searching for more declarations.  */
    9870              : 
    9871              :     }
    9872              : 
    9873          313 :   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
    9874          313 :       || gfc_match_eos () != MATCH_YES)
    9875              :     {
    9876            0 :       gfc_error ("Expected %<,%> or end of statement at %C");
    9877            0 :       return MATCH_ERROR;
    9878              :     }
    9879              :   return MATCH_YES;
    9880              : }
    9881              : 
    9882              : 
    9883              : match
    9884         3215 : gfc_match_external (void)
    9885              : {
    9886              : 
    9887         3215 :   gfc_clear_attr (&current_attr);
    9888         3215 :   current_attr.external = 1;
    9889              : 
    9890         3215 :   return attr_decl ();
    9891              : }
    9892              : 
    9893              : 
    9894              : match
    9895          208 : gfc_match_intent (void)
    9896              : {
    9897          208 :   sym_intent intent;
    9898              : 
    9899              :   /* This is not allowed within a BLOCK construct!  */
    9900          208 :   if (gfc_current_state () == COMP_BLOCK)
    9901              :     {
    9902            2 :       gfc_error ("INTENT is not allowed inside of BLOCK at %C");
    9903            2 :       return MATCH_ERROR;
    9904              :     }
    9905              : 
    9906          206 :   intent = match_intent_spec ();
    9907          206 :   if (intent == INTENT_UNKNOWN)
    9908              :     return MATCH_ERROR;
    9909              : 
    9910          206 :   gfc_clear_attr (&current_attr);
    9911          206 :   current_attr.intent = intent;
    9912              : 
    9913          206 :   return attr_decl ();
    9914              : }
    9915              : 
    9916              : 
    9917              : match
    9918         1482 : gfc_match_intrinsic (void)
    9919              : {
    9920              : 
    9921         1482 :   gfc_clear_attr (&current_attr);
    9922         1482 :   current_attr.intrinsic = 1;
    9923              : 
    9924         1482 :   return attr_decl ();
    9925              : }
    9926              : 
    9927              : 
    9928              : match
    9929          220 : gfc_match_optional (void)
    9930              : {
    9931              :   /* This is not allowed within a BLOCK construct!  */
    9932          220 :   if (gfc_current_state () == COMP_BLOCK)
    9933              :     {
    9934            2 :       gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
    9935            2 :       return MATCH_ERROR;
    9936              :     }
    9937              : 
    9938          218 :   gfc_clear_attr (&current_attr);
    9939          218 :   current_attr.optional = 1;
    9940              : 
    9941          218 :   return attr_decl ();
    9942              : }
    9943              : 
    9944              : 
    9945              : match
    9946          915 : gfc_match_pointer (void)
    9947              : {
    9948          915 :   gfc_gobble_whitespace ();
    9949          915 :   if (gfc_peek_ascii_char () == '(')
    9950              :     {
    9951          335 :       if (!flag_cray_pointer)
    9952              :         {
    9953            1 :           gfc_error ("Cray pointer declaration at %C requires "
    9954              :                      "%<-fcray-pointer%> flag");
    9955            1 :           return MATCH_ERROR;
    9956              :         }
    9957          334 :       return cray_pointer_decl ();
    9958              :     }
    9959              :   else
    9960              :     {
    9961          580 :       gfc_clear_attr (&current_attr);
    9962          580 :       current_attr.pointer = 1;
    9963              : 
    9964          580 :       return attr_decl ();
    9965              :     }
    9966              : }
    9967              : 
    9968              : 
    9969              : match
    9970          162 : gfc_match_allocatable (void)
    9971              : {
    9972          162 :   gfc_clear_attr (&current_attr);
    9973          162 :   current_attr.allocatable = 1;
    9974              : 
    9975          162 :   return attr_decl ();
    9976              : }
    9977              : 
    9978              : 
    9979              : match
    9980           23 : gfc_match_codimension (void)
    9981              : {
    9982           23 :   gfc_clear_attr (&current_attr);
    9983           23 :   current_attr.codimension = 1;
    9984              : 
    9985           23 :   return attr_decl ();
    9986              : }
    9987              : 
    9988              : 
    9989              : match
    9990           80 : gfc_match_contiguous (void)
    9991              : {
    9992           80 :   if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
    9993              :     return MATCH_ERROR;
    9994              : 
    9995           79 :   gfc_clear_attr (&current_attr);
    9996           79 :   current_attr.contiguous = 1;
    9997              : 
    9998           79 :   return attr_decl ();
    9999              : }
   10000              : 
   10001              : 
   10002              : match
   10003          648 : gfc_match_dimension (void)
   10004              : {
   10005          648 :   gfc_clear_attr (&current_attr);
   10006          648 :   current_attr.dimension = 1;
   10007              : 
   10008          648 :   return attr_decl ();
   10009              : }
   10010              : 
   10011              : 
   10012              : match
   10013           99 : gfc_match_target (void)
   10014              : {
   10015           99 :   gfc_clear_attr (&current_attr);
   10016           99 :   current_attr.target = 1;
   10017              : 
   10018           99 :   return attr_decl ();
   10019              : }
   10020              : 
   10021              : 
   10022              : /* Match the list of entities being specified in a PUBLIC or PRIVATE
   10023              :    statement.  */
   10024              : 
   10025              : static match
   10026         1766 : access_attr_decl (gfc_statement st)
   10027              : {
   10028         1766 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10029         1766 :   interface_type type;
   10030         1766 :   gfc_user_op *uop;
   10031         1766 :   gfc_symbol *sym, *dt_sym;
   10032         1766 :   gfc_intrinsic_op op;
   10033         1766 :   match m;
   10034         1766 :   gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
   10035              : 
   10036         1766 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10037            0 :     goto done;
   10038              : 
   10039         2916 :   for (;;)
   10040              :     {
   10041         2916 :       m = gfc_match_generic_spec (&type, name, &op);
   10042         2916 :       if (m == MATCH_NO)
   10043            0 :         goto syntax;
   10044         2916 :       if (m == MATCH_ERROR)
   10045            0 :         goto done;
   10046              : 
   10047         2916 :       switch (type)
   10048              :         {
   10049            0 :         case INTERFACE_NAMELESS:
   10050            0 :         case INTERFACE_ABSTRACT:
   10051            0 :           goto syntax;
   10052              : 
   10053         2839 :         case INTERFACE_GENERIC:
   10054         2839 :         case INTERFACE_DTIO:
   10055              : 
   10056         2839 :           if (gfc_get_symbol (name, NULL, &sym))
   10057            0 :             goto done;
   10058              : 
   10059         2839 :           if (type == INTERFACE_DTIO
   10060           26 :               && gfc_current_ns->proc_name
   10061           26 :               && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
   10062           26 :               && sym->attr.flavor == FL_UNKNOWN)
   10063            2 :             sym->attr.flavor = FL_PROCEDURE;
   10064              : 
   10065         2839 :           if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
   10066            4 :             goto done;
   10067              : 
   10068          330 :           if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
   10069         2892 :               && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
   10070            0 :             goto done;
   10071              : 
   10072              :           break;
   10073              : 
   10074           72 :         case INTERFACE_INTRINSIC_OP:
   10075           72 :           if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
   10076              :             {
   10077           72 :               gfc_intrinsic_op other_op;
   10078              : 
   10079           72 :               gfc_current_ns->operator_access[op] = access;
   10080              : 
   10081              :               /* Handle the case if there is another op with the same
   10082              :                  function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on.  */
   10083           72 :               other_op = gfc_equivalent_op (op);
   10084              : 
   10085           72 :               if (other_op != INTRINSIC_NONE)
   10086           21 :                 gfc_current_ns->operator_access[other_op] = access;
   10087              :             }
   10088              :           else
   10089              :             {
   10090            0 :               gfc_error ("Access specification of the %s operator at %C has "
   10091              :                          "already been specified", gfc_op2string (op));
   10092            0 :               goto done;
   10093              :             }
   10094              : 
   10095              :           break;
   10096              : 
   10097            5 :         case INTERFACE_USER_OP:
   10098            5 :           uop = gfc_get_uop (name);
   10099              : 
   10100            5 :           if (uop->access == ACCESS_UNKNOWN)
   10101              :             {
   10102            4 :               uop->access = access;
   10103              :             }
   10104              :           else
   10105              :             {
   10106            1 :               gfc_error ("Access specification of the .%s. operator at %C "
   10107              :                          "has already been specified", uop->name);
   10108            1 :               goto done;
   10109              :             }
   10110              : 
   10111            4 :           break;
   10112              :         }
   10113              : 
   10114         2911 :       if (gfc_match_char (',') == MATCH_NO)
   10115              :         break;
   10116              :     }
   10117              : 
   10118         1761 :   if (gfc_match_eos () != MATCH_YES)
   10119            0 :     goto syntax;
   10120              :   return MATCH_YES;
   10121              : 
   10122            0 : syntax:
   10123            0 :   gfc_syntax_error (st);
   10124              : 
   10125         1766 : done:
   10126              :   return MATCH_ERROR;
   10127              : }
   10128              : 
   10129              : 
   10130              : match
   10131           23 : gfc_match_protected (void)
   10132              : {
   10133           23 :   gfc_symbol *sym;
   10134           23 :   match m;
   10135           23 :   char c;
   10136              : 
   10137              :   /* PROTECTED has already been seen, but must be followed by whitespace
   10138              :      or ::.  */
   10139           23 :   c = gfc_peek_ascii_char ();
   10140           23 :   if (!gfc_is_whitespace (c) && c != ':')
   10141              :     return MATCH_NO;
   10142              : 
   10143           22 :   if (!gfc_current_ns->proc_name
   10144           20 :       || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
   10145              :     {
   10146            3 :        gfc_error ("PROTECTED at %C only allowed in specification "
   10147              :                   "part of a module");
   10148            3 :        return MATCH_ERROR;
   10149              : 
   10150              :     }
   10151              : 
   10152           19 :   gfc_match (" ::");
   10153              : 
   10154           19 :   if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
   10155              :     return MATCH_ERROR;
   10156              : 
   10157              :   /* PROTECTED has an entity-list.  */
   10158           18 :   if (gfc_match_eos () == MATCH_YES)
   10159            0 :     goto syntax;
   10160              : 
   10161           26 :   for(;;)
   10162              :     {
   10163           26 :       m = gfc_match_symbol (&sym, 0);
   10164           26 :       switch (m)
   10165              :         {
   10166           26 :         case MATCH_YES:
   10167           26 :           if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
   10168              :             return MATCH_ERROR;
   10169           25 :           goto next_item;
   10170              : 
   10171              :         case MATCH_NO:
   10172              :           break;
   10173              : 
   10174              :         case MATCH_ERROR:
   10175              :           return MATCH_ERROR;
   10176              :         }
   10177              : 
   10178           25 :     next_item:
   10179           25 :       if (gfc_match_eos () == MATCH_YES)
   10180              :         break;
   10181            8 :       if (gfc_match_char (',') != MATCH_YES)
   10182            0 :         goto syntax;
   10183              :     }
   10184              : 
   10185              :   return MATCH_YES;
   10186              : 
   10187            0 : syntax:
   10188            0 :   gfc_error ("Syntax error in PROTECTED statement at %C");
   10189            0 :   return MATCH_ERROR;
   10190              : }
   10191              : 
   10192              : 
   10193              : /* The PRIVATE statement is a bit weird in that it can be an attribute
   10194              :    declaration, but also works as a standalone statement inside of a
   10195              :    type declaration or a module.  */
   10196              : 
   10197              : match
   10198        29567 : gfc_match_private (gfc_statement *st)
   10199              : {
   10200        29567 :   gfc_state_data *prev;
   10201              : 
   10202        29567 :   if (gfc_match ("private") != MATCH_YES)
   10203              :     return MATCH_NO;
   10204              : 
   10205              :   /* Try matching PRIVATE without an access-list.  */
   10206         1635 :   if (gfc_match_eos () == MATCH_YES)
   10207              :     {
   10208         1348 :       prev = gfc_state_stack->previous;
   10209         1348 :       if (gfc_current_state () != COMP_MODULE
   10210          367 :           && !(gfc_current_state () == COMP_DERIVED
   10211          334 :                 && prev && prev->state == COMP_MODULE)
   10212           34 :           && !(gfc_current_state () == COMP_DERIVED_CONTAINS
   10213           32 :                 && prev->previous && prev->previous->state == COMP_MODULE))
   10214              :         {
   10215            2 :           gfc_error ("PRIVATE statement at %C is only allowed in the "
   10216              :                      "specification part of a module");
   10217            2 :           return MATCH_ERROR;
   10218              :         }
   10219              : 
   10220         1346 :       *st = ST_PRIVATE;
   10221         1346 :       return MATCH_YES;
   10222              :     }
   10223              : 
   10224              :   /* At this point in free-form source code, PRIVATE must be followed
   10225              :      by whitespace or ::.  */
   10226          287 :   if (gfc_current_form == FORM_FREE)
   10227              :     {
   10228          285 :       char c = gfc_peek_ascii_char ();
   10229          285 :       if (!gfc_is_whitespace (c) && c != ':')
   10230              :         return MATCH_NO;
   10231              :     }
   10232              : 
   10233          286 :   prev = gfc_state_stack->previous;
   10234          286 :   if (gfc_current_state () != COMP_MODULE
   10235            1 :       && !(gfc_current_state () == COMP_DERIVED
   10236            0 :            && prev && prev->state == COMP_MODULE)
   10237            1 :       && !(gfc_current_state () == COMP_DERIVED_CONTAINS
   10238            0 :            && prev->previous && prev->previous->state == COMP_MODULE))
   10239              :     {
   10240            1 :       gfc_error ("PRIVATE statement at %C is only allowed in the "
   10241              :                  "specification part of a module");
   10242            1 :       return MATCH_ERROR;
   10243              :     }
   10244              : 
   10245          285 :   *st = ST_ATTR_DECL;
   10246          285 :   return access_attr_decl (ST_PRIVATE);
   10247              : }
   10248              : 
   10249              : 
   10250              : match
   10251         1880 : gfc_match_public (gfc_statement *st)
   10252              : {
   10253         1880 :   if (gfc_match ("public") != MATCH_YES)
   10254              :     return MATCH_NO;
   10255              : 
   10256              :   /* Try matching PUBLIC without an access-list.  */
   10257         1528 :   if (gfc_match_eos () == MATCH_YES)
   10258              :     {
   10259           45 :       if (gfc_current_state () != COMP_MODULE)
   10260              :         {
   10261            2 :           gfc_error ("PUBLIC statement at %C is only allowed in the "
   10262              :                      "specification part of a module");
   10263            2 :           return MATCH_ERROR;
   10264              :         }
   10265              : 
   10266           43 :       *st = ST_PUBLIC;
   10267           43 :       return MATCH_YES;
   10268              :     }
   10269              : 
   10270              :   /* At this point in free-form source code, PUBLIC must be followed
   10271              :      by whitespace or ::.  */
   10272         1483 :   if (gfc_current_form == FORM_FREE)
   10273              :     {
   10274         1481 :       char c = gfc_peek_ascii_char ();
   10275         1481 :       if (!gfc_is_whitespace (c) && c != ':')
   10276              :         return MATCH_NO;
   10277              :     }
   10278              : 
   10279         1482 :   if (gfc_current_state () != COMP_MODULE)
   10280              :     {
   10281            1 :       gfc_error ("PUBLIC statement at %C is only allowed in the "
   10282              :                  "specification part of a module");
   10283            1 :       return MATCH_ERROR;
   10284              :     }
   10285              : 
   10286         1481 :   *st = ST_ATTR_DECL;
   10287         1481 :   return access_attr_decl (ST_PUBLIC);
   10288              : }
   10289              : 
   10290              : 
   10291              : /* Workhorse for gfc_match_parameter.  */
   10292              : 
   10293              : static match
   10294         8533 : do_parm (void)
   10295              : {
   10296         8533 :   gfc_symbol *sym;
   10297         8533 :   gfc_expr *init;
   10298         8533 :   gfc_charlen *saved_cl_list;
   10299         8533 :   match m;
   10300         8533 :   bool t;
   10301              : 
   10302         8533 :   saved_cl_list = gfc_current_ns->cl_list;
   10303              : 
   10304         8533 :   m = gfc_match_symbol (&sym, 0);
   10305         8533 :   if (m == MATCH_NO)
   10306            0 :     gfc_error ("Expected variable name at %C in PARAMETER statement");
   10307              : 
   10308         8533 :   if (m != MATCH_YES)
   10309              :     return m;
   10310              : 
   10311         8533 :   if (gfc_match_char ('=') == MATCH_NO)
   10312              :     {
   10313            0 :       gfc_error ("Expected = sign in PARAMETER statement at %C");
   10314            0 :       return MATCH_ERROR;
   10315              :     }
   10316              : 
   10317         8533 :   m = gfc_match_init_expr (&init);
   10318         8533 :   if (m == MATCH_NO)
   10319            0 :     gfc_error ("Expected expression at %C in PARAMETER statement");
   10320         8533 :   if (m != MATCH_YES)
   10321              :     return m;
   10322              : 
   10323         8532 :   if (sym->ts.type == BT_UNKNOWN
   10324         8532 :       && !gfc_set_default_type (sym, 1, NULL))
   10325              :     {
   10326            1 :       m = MATCH_ERROR;
   10327            1 :       goto cleanup;
   10328              :     }
   10329              : 
   10330         8531 :   if (!gfc_check_assign_symbol (sym, NULL, init)
   10331         8531 :       || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
   10332              :     {
   10333            1 :       m = MATCH_ERROR;
   10334            1 :       goto cleanup;
   10335              :     }
   10336              : 
   10337         8530 :   if (sym->value)
   10338              :     {
   10339            1 :       gfc_error ("Initializing already initialized variable at %C");
   10340            1 :       m = MATCH_ERROR;
   10341            1 :       goto cleanup;
   10342              :     }
   10343              : 
   10344         8529 :   t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus,
   10345              :                             saved_cl_list);
   10346         8529 :   return (t) ? MATCH_YES : MATCH_ERROR;
   10347              : 
   10348            3 : cleanup:
   10349            3 :   gfc_free_expr (init);
   10350            3 :   return m;
   10351              : }
   10352              : 
   10353              : 
   10354              : /* Match a parameter statement, with the weird syntax that these have.  */
   10355              : 
   10356              : match
   10357         7820 : gfc_match_parameter (void)
   10358              : {
   10359         7820 :   const char *term = " )%t";
   10360         7820 :   match m;
   10361              : 
   10362         7820 :   if (gfc_match_char ('(') == MATCH_NO)
   10363              :     {
   10364              :       /* With legacy PARAMETER statements, don't expect a terminating ')'.  */
   10365           28 :       if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
   10366              :         return MATCH_NO;
   10367         7819 :       term = " %t";
   10368              :     }
   10369              : 
   10370         8533 :   for (;;)
   10371              :     {
   10372         8533 :       m = do_parm ();
   10373         8533 :       if (m != MATCH_YES)
   10374              :         break;
   10375              : 
   10376         8529 :       if (gfc_match (term) == MATCH_YES)
   10377              :         break;
   10378              : 
   10379          714 :       if (gfc_match_char (',') != MATCH_YES)
   10380              :         {
   10381            0 :           gfc_error ("Unexpected characters in PARAMETER statement at %C");
   10382            0 :           m = MATCH_ERROR;
   10383            0 :           break;
   10384              :         }
   10385              :     }
   10386              : 
   10387              :   return m;
   10388              : }
   10389              : 
   10390              : 
   10391              : match
   10392            8 : gfc_match_automatic (void)
   10393              : {
   10394            8 :   gfc_symbol *sym;
   10395            8 :   match m;
   10396            8 :   bool seen_symbol = false;
   10397              : 
   10398            8 :   if (!flag_dec_static)
   10399              :     {
   10400            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
   10401              :                  "%<-fdec-static%>",
   10402              :                  "AUTOMATIC"
   10403              :                  );
   10404            2 :       return MATCH_ERROR;
   10405              :     }
   10406              : 
   10407            6 :   gfc_match (" ::");
   10408              : 
   10409            6 :   for (;;)
   10410              :     {
   10411            6 :       m = gfc_match_symbol (&sym, 0);
   10412            6 :       switch (m)
   10413              :       {
   10414              :       case MATCH_NO:
   10415              :         break;
   10416              : 
   10417              :       case MATCH_ERROR:
   10418              :         return MATCH_ERROR;
   10419              : 
   10420            4 :       case MATCH_YES:
   10421            4 :         if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
   10422              :           return MATCH_ERROR;
   10423              :         seen_symbol = true;
   10424              :         break;
   10425              :       }
   10426              : 
   10427            4 :       if (gfc_match_eos () == MATCH_YES)
   10428              :         break;
   10429            0 :       if (gfc_match_char (',') != MATCH_YES)
   10430            0 :         goto syntax;
   10431              :     }
   10432              : 
   10433            4 :   if (!seen_symbol)
   10434              :     {
   10435            2 :       gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
   10436            2 :       return MATCH_ERROR;
   10437              :     }
   10438              : 
   10439              :   return MATCH_YES;
   10440              : 
   10441            0 : syntax:
   10442            0 :   gfc_error ("Syntax error in AUTOMATIC statement at %C");
   10443            0 :   return MATCH_ERROR;
   10444              : }
   10445              : 
   10446              : 
   10447              : match
   10448            7 : gfc_match_static (void)
   10449              : {
   10450            7 :   gfc_symbol *sym;
   10451            7 :   match m;
   10452            7 :   bool seen_symbol = false;
   10453              : 
   10454            7 :   if (!flag_dec_static)
   10455              :     {
   10456            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
   10457              :                  "%<-fdec-static%>",
   10458              :                  "STATIC");
   10459            2 :       return MATCH_ERROR;
   10460              :     }
   10461              : 
   10462            5 :   gfc_match (" ::");
   10463              : 
   10464            5 :   for (;;)
   10465              :     {
   10466            5 :       m = gfc_match_symbol (&sym, 0);
   10467            5 :       switch (m)
   10468              :       {
   10469              :       case MATCH_NO:
   10470              :         break;
   10471              : 
   10472              :       case MATCH_ERROR:
   10473              :         return MATCH_ERROR;
   10474              : 
   10475            3 :       case MATCH_YES:
   10476            3 :         if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
   10477              :                           &gfc_current_locus))
   10478              :           return MATCH_ERROR;
   10479              :         seen_symbol = true;
   10480              :         break;
   10481              :       }
   10482              : 
   10483            3 :       if (gfc_match_eos () == MATCH_YES)
   10484              :         break;
   10485            0 :       if (gfc_match_char (',') != MATCH_YES)
   10486            0 :         goto syntax;
   10487              :     }
   10488              : 
   10489            3 :   if (!seen_symbol)
   10490              :     {
   10491            2 :       gfc_error ("Expected entity-list in STATIC statement at %C");
   10492            2 :       return MATCH_ERROR;
   10493              :     }
   10494              : 
   10495              :   return MATCH_YES;
   10496              : 
   10497            0 : syntax:
   10498            0 :   gfc_error ("Syntax error in STATIC statement at %C");
   10499            0 :   return MATCH_ERROR;
   10500              : }
   10501              : 
   10502              : 
   10503              : /* Save statements have a special syntax.  */
   10504              : 
   10505              : match
   10506          272 : gfc_match_save (void)
   10507              : {
   10508          272 :   char n[GFC_MAX_SYMBOL_LEN+1];
   10509          272 :   gfc_common_head *c;
   10510          272 :   gfc_symbol *sym;
   10511          272 :   match m;
   10512              : 
   10513          272 :   if (gfc_match_eos () == MATCH_YES)
   10514              :     {
   10515          150 :       if (gfc_current_ns->seen_save)
   10516              :         {
   10517            7 :           if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
   10518              :                                "follows previous SAVE statement"))
   10519              :             return MATCH_ERROR;
   10520              :         }
   10521              : 
   10522          149 :       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
   10523          149 :       return MATCH_YES;
   10524              :     }
   10525              : 
   10526          122 :   if (gfc_current_ns->save_all)
   10527              :     {
   10528            7 :       if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
   10529              :                            "blanket SAVE statement"))
   10530              :         return MATCH_ERROR;
   10531              :     }
   10532              : 
   10533          121 :   gfc_match (" ::");
   10534              : 
   10535          183 :   for (;;)
   10536              :     {
   10537          183 :       m = gfc_match_symbol (&sym, 0);
   10538          183 :       switch (m)
   10539              :         {
   10540          181 :         case MATCH_YES:
   10541          181 :           if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
   10542              :                              &gfc_current_locus))
   10543              :             return MATCH_ERROR;
   10544          179 :           goto next_item;
   10545              : 
   10546              :         case MATCH_NO:
   10547              :           break;
   10548              : 
   10549              :         case MATCH_ERROR:
   10550              :           return MATCH_ERROR;
   10551              :         }
   10552              : 
   10553            2 :       m = gfc_match (" / %n /", &n);
   10554            2 :       if (m == MATCH_ERROR)
   10555              :         return MATCH_ERROR;
   10556            2 :       if (m == MATCH_NO)
   10557            0 :         goto syntax;
   10558              : 
   10559              :       /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
   10560              :          saved-entity-list that does not specify a common-block-name.  */
   10561            2 :       if (gfc_current_state () == COMP_BLOCK)
   10562              :         {
   10563            1 :           gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
   10564              :                      "in a BLOCK construct", n);
   10565            1 :           return MATCH_ERROR;
   10566              :         }
   10567              : 
   10568            1 :       c = gfc_get_common (n, 0);
   10569            1 :       c->saved = 1;
   10570              : 
   10571            1 :       gfc_current_ns->seen_save = 1;
   10572              : 
   10573          180 :     next_item:
   10574          180 :       if (gfc_match_eos () == MATCH_YES)
   10575              :         break;
   10576           62 :       if (gfc_match_char (',') != MATCH_YES)
   10577            0 :         goto syntax;
   10578              :     }
   10579              : 
   10580              :   return MATCH_YES;
   10581              : 
   10582            0 : syntax:
   10583            0 :   if (gfc_current_ns->seen_save)
   10584              :     {
   10585            0 :       gfc_error ("Syntax error in SAVE statement at %C");
   10586            0 :       return MATCH_ERROR;
   10587              :     }
   10588              :   else
   10589              :       return MATCH_NO;
   10590              : }
   10591              : 
   10592              : 
   10593              : match
   10594           93 : gfc_match_value (void)
   10595              : {
   10596           93 :   gfc_symbol *sym;
   10597           93 :   match m;
   10598              : 
   10599              :   /* This is not allowed within a BLOCK construct!  */
   10600           93 :   if (gfc_current_state () == COMP_BLOCK)
   10601              :     {
   10602            2 :       gfc_error ("VALUE is not allowed inside of BLOCK at %C");
   10603            2 :       return MATCH_ERROR;
   10604              :     }
   10605              : 
   10606           91 :   if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
   10607              :     return MATCH_ERROR;
   10608              : 
   10609           90 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10610              :     {
   10611              :       return MATCH_ERROR;
   10612              :     }
   10613              : 
   10614           90 :   if (gfc_match_eos () == MATCH_YES)
   10615            0 :     goto syntax;
   10616              : 
   10617          116 :   for(;;)
   10618              :     {
   10619          116 :       m = gfc_match_symbol (&sym, 0);
   10620          116 :       switch (m)
   10621              :         {
   10622          116 :         case MATCH_YES:
   10623          116 :           if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
   10624              :             return MATCH_ERROR;
   10625          110 :           goto next_item;
   10626              : 
   10627              :         case MATCH_NO:
   10628              :           break;
   10629              : 
   10630              :         case MATCH_ERROR:
   10631              :           return MATCH_ERROR;
   10632              :         }
   10633              : 
   10634          110 :     next_item:
   10635          110 :       if (gfc_match_eos () == MATCH_YES)
   10636              :         break;
   10637           26 :       if (gfc_match_char (',') != MATCH_YES)
   10638            0 :         goto syntax;
   10639              :     }
   10640              : 
   10641              :   return MATCH_YES;
   10642              : 
   10643            0 : syntax:
   10644            0 :   gfc_error ("Syntax error in VALUE statement at %C");
   10645            0 :   return MATCH_ERROR;
   10646              : }
   10647              : 
   10648              : 
   10649              : match
   10650           45 : gfc_match_volatile (void)
   10651              : {
   10652           45 :   gfc_symbol *sym;
   10653           45 :   char *name;
   10654           45 :   match m;
   10655              : 
   10656           45 :   if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
   10657              :     return MATCH_ERROR;
   10658              : 
   10659           44 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10660              :     {
   10661              :       return MATCH_ERROR;
   10662              :     }
   10663              : 
   10664           44 :   if (gfc_match_eos () == MATCH_YES)
   10665            1 :     goto syntax;
   10666              : 
   10667           48 :   for(;;)
   10668              :     {
   10669              :       /* VOLATILE is special because it can be added to host-associated
   10670              :          symbols locally.  Except for coarrays.  */
   10671           48 :       m = gfc_match_symbol (&sym, 1);
   10672           48 :       switch (m)
   10673              :         {
   10674           48 :         case MATCH_YES:
   10675           48 :           name = XALLOCAVAR (char, strlen (sym->name) + 1);
   10676           48 :           strcpy (name, sym->name);
   10677           48 :           if (!check_function_name (name))
   10678              :             return MATCH_ERROR;
   10679              :           /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
   10680              :              for variable in a BLOCK which is defined outside of the BLOCK.  */
   10681           47 :           if (sym->ns != gfc_current_ns && sym->attr.codimension)
   10682              :             {
   10683            2 :               gfc_error ("Specifying VOLATILE for coarray variable %qs at "
   10684              :                          "%C, which is use-/host-associated", sym->name);
   10685            2 :               return MATCH_ERROR;
   10686              :             }
   10687           45 :           if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
   10688              :             return MATCH_ERROR;
   10689           42 :           goto next_item;
   10690              : 
   10691              :         case MATCH_NO:
   10692              :           break;
   10693              : 
   10694              :         case MATCH_ERROR:
   10695              :           return MATCH_ERROR;
   10696              :         }
   10697              : 
   10698           42 :     next_item:
   10699           42 :       if (gfc_match_eos () == MATCH_YES)
   10700              :         break;
   10701            5 :       if (gfc_match_char (',') != MATCH_YES)
   10702            0 :         goto syntax;
   10703              :     }
   10704              : 
   10705              :   return MATCH_YES;
   10706              : 
   10707            1 : syntax:
   10708            1 :   gfc_error ("Syntax error in VOLATILE statement at %C");
   10709            1 :   return MATCH_ERROR;
   10710              : }
   10711              : 
   10712              : 
   10713              : match
   10714           11 : gfc_match_asynchronous (void)
   10715              : {
   10716           11 :   gfc_symbol *sym;
   10717           11 :   char *name;
   10718           11 :   match m;
   10719              : 
   10720           11 :   if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
   10721              :     return MATCH_ERROR;
   10722              : 
   10723           10 :   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
   10724              :     {
   10725              :       return MATCH_ERROR;
   10726              :     }
   10727              : 
   10728           10 :   if (gfc_match_eos () == MATCH_YES)
   10729            0 :     goto syntax;
   10730              : 
   10731           10 :   for(;;)
   10732              :     {
   10733              :       /* ASYNCHRONOUS is special because it can be added to host-associated
   10734              :          symbols locally.  */
   10735           10 :       m = gfc_match_symbol (&sym, 1);
   10736           10 :       switch (m)
   10737              :         {
   10738           10 :         case MATCH_YES:
   10739           10 :           name = XALLOCAVAR (char, strlen (sym->name) + 1);
   10740           10 :           strcpy (name, sym->name);
   10741           10 :           if (!check_function_name (name))
   10742              :             return MATCH_ERROR;
   10743            9 :           if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
   10744              :             return MATCH_ERROR;
   10745            7 :           goto next_item;
   10746              : 
   10747              :         case MATCH_NO:
   10748              :           break;
   10749              : 
   10750              :         case MATCH_ERROR:
   10751              :           return MATCH_ERROR;
   10752              :         }
   10753              : 
   10754            7 :     next_item:
   10755            7 :       if (gfc_match_eos () == MATCH_YES)
   10756              :         break;
   10757            0 :       if (gfc_match_char (',') != MATCH_YES)
   10758            0 :         goto syntax;
   10759              :     }
   10760              : 
   10761              :   return MATCH_YES;
   10762              : 
   10763            0 : syntax:
   10764            0 :   gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
   10765            0 :   return MATCH_ERROR;
   10766              : }
   10767              : 
   10768              : 
   10769              : /* Match a module procedure statement in a submodule.  */
   10770              : 
   10771              : match
   10772       775917 : gfc_match_submod_proc (void)
   10773              : {
   10774       775917 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10775       775917 :   gfc_symbol *sym, *fsym;
   10776       775917 :   match m;
   10777       775917 :   gfc_formal_arglist *formal, *head, *tail;
   10778              : 
   10779       775917 :   if (gfc_current_state () != COMP_CONTAINS
   10780        15949 :       || !(gfc_state_stack->previous
   10781        15949 :            && (gfc_state_stack->previous->state == COMP_SUBMODULE
   10782        15949 :                || gfc_state_stack->previous->state == COMP_MODULE)))
   10783              :     return MATCH_NO;
   10784              : 
   10785         7951 :   m = gfc_match (" module% procedure% %n", name);
   10786         7951 :   if (m != MATCH_YES)
   10787              :     return m;
   10788              : 
   10789          267 :   if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
   10790              :                                       "at %C"))
   10791              :     return MATCH_ERROR;
   10792              : 
   10793          267 :   if (get_proc_name (name, &sym, false))
   10794              :     return MATCH_ERROR;
   10795              : 
   10796              :   /* Make sure that the result field is appropriately filled.  */
   10797          267 :   if (sym->tlink && sym->tlink->attr.function)
   10798              :     {
   10799          117 :       if (sym->tlink->result && sym->tlink->result != sym->tlink)
   10800              :         {
   10801           67 :           sym->result = sym->tlink->result;
   10802           67 :           if (!sym->result->attr.use_assoc)
   10803              :             {
   10804           20 :               gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
   10805              :                                                  sym->result->name);
   10806           20 :               st->n.sym = sym->result;
   10807           20 :               sym->result->refs++;
   10808              :             }
   10809              :         }
   10810              :       else
   10811           50 :         sym->result = sym;
   10812              :     }
   10813              : 
   10814              :   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
   10815              :      the symbol existed before.  */
   10816          267 :   sym->declared_at = gfc_current_locus;
   10817              : 
   10818          267 :   if (!sym->attr.module_procedure)
   10819              :     return MATCH_ERROR;
   10820              : 
   10821              :   /* Signal match_end to expect "end procedure".  */
   10822          265 :   sym->abr_modproc_decl = 1;
   10823              : 
   10824              :   /* Change from IFSRC_IFBODY coming from the interface declaration.  */
   10825          265 :   sym->attr.if_source = IFSRC_DECL;
   10826              : 
   10827          265 :   gfc_new_block = sym;
   10828              : 
   10829              :   /* Make a new formal arglist with the symbols in the procedure
   10830              :       namespace.  */
   10831          265 :   head = tail = NULL;
   10832          600 :   for (formal = sym->formal; formal && formal->sym; formal = formal->next)
   10833              :     {
   10834          335 :       if (formal == sym->formal)
   10835          238 :         head = tail = gfc_get_formal_arglist ();
   10836              :       else
   10837              :         {
   10838           97 :           tail->next = gfc_get_formal_arglist ();
   10839           97 :           tail = tail->next;
   10840              :         }
   10841              : 
   10842          335 :       if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
   10843            0 :         goto cleanup;
   10844              : 
   10845          335 :       tail->sym = fsym;
   10846          335 :       gfc_set_sym_referenced (fsym);
   10847              :     }
   10848              : 
   10849              :   /* The dummy symbols get cleaned up, when the formal_namespace of the
   10850              :      interface declaration is cleared.  This allows us to add the
   10851              :      explicit interface as is done for other type of procedure.  */
   10852          265 :   if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
   10853              :                                    &gfc_current_locus))
   10854              :     return MATCH_ERROR;
   10855              : 
   10856          265 :   if (gfc_match_eos () != MATCH_YES)
   10857              :     {
   10858              :       /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
   10859              :          undone, such that the st->n.sym->formal points to the original symbol;
   10860              :          if now this namespace is finalized, the formal namespace is freed,
   10861              :          but it might be still needed in the parent namespace.  */
   10862            1 :       gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
   10863            1 :       st->n.sym = NULL;
   10864            1 :       gfc_free_symbol (sym->tlink);
   10865            1 :       sym->tlink = NULL;
   10866            1 :       sym->refs--;
   10867            1 :       gfc_syntax_error (ST_MODULE_PROC);
   10868            1 :       return MATCH_ERROR;
   10869              :     }
   10870              : 
   10871              :   return MATCH_YES;
   10872              : 
   10873            0 : cleanup:
   10874            0 :   gfc_free_formal_arglist (head);
   10875            0 :   return MATCH_ERROR;
   10876              : }
   10877              : 
   10878              : 
   10879              : /* Match a module procedure statement.  Note that we have to modify
   10880              :    symbols in the parent's namespace because the current one was there
   10881              :    to receive symbols that are in an interface's formal argument list.  */
   10882              : 
   10883              : match
   10884         1626 : gfc_match_modproc (void)
   10885              : {
   10886         1626 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   10887         1626 :   gfc_symbol *sym;
   10888         1626 :   match m;
   10889         1626 :   locus old_locus;
   10890         1626 :   gfc_namespace *module_ns;
   10891         1626 :   gfc_interface *old_interface_head, *interface;
   10892              : 
   10893         1626 :   if (gfc_state_stack->previous == NULL
   10894         1624 :       || (gfc_state_stack->state != COMP_INTERFACE
   10895            5 :           && (gfc_state_stack->state != COMP_CONTAINS
   10896            4 :               || gfc_state_stack->previous->state != COMP_INTERFACE))
   10897         1619 :       || current_interface.type == INTERFACE_NAMELESS
   10898         1619 :       || current_interface.type == INTERFACE_ABSTRACT)
   10899              :     {
   10900            8 :       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
   10901              :                  "interface");
   10902            8 :       return MATCH_ERROR;
   10903              :     }
   10904              : 
   10905         1618 :   module_ns = gfc_current_ns->parent;
   10906         1624 :   for (; module_ns; module_ns = module_ns->parent)
   10907         1624 :     if (module_ns->proc_name->attr.flavor == FL_MODULE
   10908           29 :         || module_ns->proc_name->attr.flavor == FL_PROGRAM
   10909           12 :         || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
   10910           12 :             && !module_ns->proc_name->attr.contained))
   10911              :       break;
   10912              : 
   10913         1618 :   if (module_ns == NULL)
   10914              :     return MATCH_ERROR;
   10915              : 
   10916              :   /* Store the current state of the interface. We will need it if we
   10917              :      end up with a syntax error and need to recover.  */
   10918         1618 :   old_interface_head = gfc_current_interface_head ();
   10919              : 
   10920              :   /* Check if the F2008 optional double colon appears.  */
   10921         1618 :   gfc_gobble_whitespace ();
   10922         1618 :   old_locus = gfc_current_locus;
   10923         1618 :   if (gfc_match ("::") == MATCH_YES)
   10924              :     {
   10925           31 :       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
   10926              :                            "MODULE PROCEDURE statement at %L", &old_locus))
   10927              :         return MATCH_ERROR;
   10928              :     }
   10929              :   else
   10930         1587 :     gfc_current_locus = old_locus;
   10931              : 
   10932         1973 :   for (;;)
   10933              :     {
   10934         1973 :       bool last = false;
   10935         1973 :       old_locus = gfc_current_locus;
   10936              : 
   10937         1973 :       m = gfc_match_name (name);
   10938         1973 :       if (m == MATCH_NO)
   10939            1 :         goto syntax;
   10940         1972 :       if (m != MATCH_YES)
   10941              :         return MATCH_ERROR;
   10942              : 
   10943              :       /* Check for syntax error before starting to add symbols to the
   10944              :          current namespace.  */
   10945         1972 :       if (gfc_match_eos () == MATCH_YES)
   10946              :         last = true;
   10947              : 
   10948          360 :       if (!last && gfc_match_char (',') != MATCH_YES)
   10949            2 :         goto syntax;
   10950              : 
   10951              :       /* Now we're sure the syntax is valid, we process this item
   10952              :          further.  */
   10953         1970 :       if (gfc_get_symbol (name, module_ns, &sym))
   10954              :         return MATCH_ERROR;
   10955              : 
   10956         1970 :       if (sym->attr.intrinsic)
   10957              :         {
   10958            1 :           gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
   10959              :                      "PROCEDURE", &old_locus);
   10960            1 :           return MATCH_ERROR;
   10961              :         }
   10962              : 
   10963         1969 :       if (sym->attr.proc != PROC_MODULE
   10964         1969 :           && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
   10965              :         return MATCH_ERROR;
   10966              : 
   10967         1966 :       if (!gfc_add_interface (sym))
   10968              :         return MATCH_ERROR;
   10969              : 
   10970         1963 :       sym->attr.mod_proc = 1;
   10971         1963 :       sym->declared_at = old_locus;
   10972              : 
   10973         1963 :       if (last)
   10974              :         break;
   10975              :     }
   10976              : 
   10977              :   return MATCH_YES;
   10978              : 
   10979            3 : syntax:
   10980              :   /* Restore the previous state of the interface.  */
   10981            3 :   interface = gfc_current_interface_head ();
   10982            3 :   gfc_set_current_interface_head (old_interface_head);
   10983              : 
   10984              :   /* Free the new interfaces.  */
   10985           10 :   while (interface != old_interface_head)
   10986              :   {
   10987            4 :     gfc_interface *i = interface->next;
   10988            4 :     free (interface);
   10989            4 :     interface = i;
   10990              :   }
   10991              : 
   10992              :   /* And issue a syntax error.  */
   10993            3 :   gfc_syntax_error (ST_MODULE_PROC);
   10994            3 :   return MATCH_ERROR;
   10995              : }
   10996              : 
   10997              : 
   10998              : /* Check a derived type that is being extended.  */
   10999              : 
   11000              : static gfc_symbol*
   11001         1581 : check_extended_derived_type (char *name)
   11002              : {
   11003         1581 :   gfc_symbol *extended;
   11004              : 
   11005         1581 :   if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
   11006              :     {
   11007            0 :       gfc_error ("Ambiguous symbol in TYPE definition at %C");
   11008            0 :       return NULL;
   11009              :     }
   11010              : 
   11011         1581 :   extended = gfc_find_dt_in_generic (extended);
   11012              : 
   11013              :   /* F08:C428.  */
   11014         1581 :   if (!extended)
   11015              :     {
   11016            2 :       gfc_error ("Symbol %qs at %C has not been previously defined", name);
   11017            2 :       return NULL;
   11018              :     }
   11019              : 
   11020         1579 :   if (extended->attr.flavor != FL_DERIVED)
   11021              :     {
   11022            0 :       gfc_error ("%qs in EXTENDS expression at %C is not a "
   11023              :                  "derived type", name);
   11024            0 :       return NULL;
   11025              :     }
   11026              : 
   11027         1579 :   if (extended->attr.is_bind_c)
   11028              :     {
   11029            1 :       gfc_error ("%qs cannot be extended at %C because it "
   11030              :                  "is BIND(C)", extended->name);
   11031            1 :       return NULL;
   11032              :     }
   11033              : 
   11034         1578 :   if (extended->attr.sequence)
   11035              :     {
   11036            1 :       gfc_error ("%qs cannot be extended at %C because it "
   11037              :                  "is a SEQUENCE type", extended->name);
   11038            1 :       return NULL;
   11039              :     }
   11040              : 
   11041              :   return extended;
   11042              : }
   11043              : 
   11044              : 
   11045              : /* Match the optional attribute specifiers for a type declaration.
   11046              :    Return MATCH_ERROR if an error is encountered in one of the handled
   11047              :    attributes (public, private, bind(c)), MATCH_NO if what's found is
   11048              :    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
   11049              :    checking on attribute conflicts needs to be done.  */
   11050              : 
   11051              : static match
   11052        20041 : gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
   11053              : {
   11054              :   /* See if the derived type is marked as private.  */
   11055        20041 :   if (gfc_match (" , private") == MATCH_YES)
   11056              :     {
   11057           15 :       if (gfc_current_state () != COMP_MODULE)
   11058              :         {
   11059            1 :           gfc_error ("Derived type at %C can only be PRIVATE in the "
   11060              :                      "specification part of a module");
   11061            1 :           return MATCH_ERROR;
   11062              :         }
   11063              : 
   11064           14 :       if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
   11065            0 :         return MATCH_ERROR;
   11066              :     }
   11067        20026 :   else if (gfc_match (" , public") == MATCH_YES)
   11068              :     {
   11069          558 :       if (gfc_current_state () != COMP_MODULE)
   11070              :         {
   11071            0 :           gfc_error ("Derived type at %C can only be PUBLIC in the "
   11072              :                      "specification part of a module");
   11073            0 :           return MATCH_ERROR;
   11074              :         }
   11075              : 
   11076          558 :       if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
   11077            0 :         return MATCH_ERROR;
   11078              :     }
   11079        19468 :   else if (gfc_match (" , bind ( c )") == MATCH_YES)
   11080              :     {
   11081              :       /* If the type is defined to be bind(c) it then needs to make
   11082              :          sure that all fields are interoperable.  This will
   11083              :          need to be a semantic check on the finished derived type.
   11084              :          See 15.2.3 (lines 9-12) of F2003 draft.  */
   11085          407 :       if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
   11086            0 :         return MATCH_ERROR;
   11087              : 
   11088              :       /* TODO: attr conflicts need to be checked, probably in symbol.cc.  */
   11089              :     }
   11090        19061 :   else if (gfc_match (" , abstract") == MATCH_YES)
   11091              :     {
   11092          349 :       if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
   11093              :         return MATCH_ERROR;
   11094              : 
   11095          348 :       if (!gfc_add_abstract (attr, &gfc_current_locus))
   11096            1 :         return MATCH_ERROR;
   11097              :     }
   11098        18712 :   else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
   11099              :     {
   11100         1582 :       if (!gfc_add_extension (attr, &gfc_current_locus))
   11101            0 :         return MATCH_ERROR;
   11102              :     }
   11103              :   else
   11104              :     return MATCH_NO;
   11105              : 
   11106              :   /* If we get here, something matched.  */
   11107              :   return MATCH_YES;
   11108              : }
   11109              : 
   11110              : 
   11111              : /* Common function for type declaration blocks similar to derived types, such
   11112              :    as STRUCTURES and MAPs. Unlike derived types, a structure type
   11113              :    does NOT have a generic symbol matching the name given by the user.
   11114              :    STRUCTUREs can share names with variables and PARAMETERs so we must allow
   11115              :    for the creation of an independent symbol.
   11116              :    Other parameters are a message to prefix errors with, the name of the new
   11117              :    type to be created, and the flavor to add to the resulting symbol. */
   11118              : 
   11119              : static bool
   11120          717 : get_struct_decl (const char *name, sym_flavor fl, locus *decl,
   11121              :                  gfc_symbol **result)
   11122              : {
   11123          717 :   gfc_symbol *sym;
   11124          717 :   locus where;
   11125              : 
   11126          717 :   gcc_assert (name[0] == (char) TOUPPER (name[0]));
   11127              : 
   11128          717 :   if (decl)
   11129          717 :     where = *decl;
   11130              :   else
   11131            0 :     where = gfc_current_locus;
   11132              : 
   11133          717 :   if (gfc_get_symbol (name, NULL, &sym))
   11134              :     return false;
   11135              : 
   11136          717 :   if (!sym)
   11137              :     {
   11138            0 :       gfc_internal_error ("Failed to create structure type '%s' at %C", name);
   11139              :       return false;
   11140              :     }
   11141              : 
   11142          717 :   if (sym->components != NULL || sym->attr.zero_comp)
   11143              :     {
   11144            3 :       gfc_error ("Type definition of %qs at %C was already defined at %L",
   11145              :                  sym->name, &sym->declared_at);
   11146            3 :       return false;
   11147              :     }
   11148              : 
   11149          714 :   sym->declared_at = where;
   11150              : 
   11151          714 :   if (sym->attr.flavor != fl
   11152          714 :       && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
   11153              :     return false;
   11154              : 
   11155          714 :   if (!sym->hash_value)
   11156              :       /* Set the hash for the compound name for this type.  */
   11157          713 :     sym->hash_value = gfc_hash_value (sym);
   11158              : 
   11159              :   /* Normally the type is expected to have been completely parsed by the time
   11160              :      a field declaration with this type is seen. For unions, maps, and nested
   11161              :      structure declarations, we need to indicate that it is okay that we
   11162              :      haven't seen any components yet. This will be updated after the structure
   11163              :      is fully parsed. */
   11164          714 :   sym->attr.zero_comp = 0;
   11165              : 
   11166              :   /* Structures always act like derived-types with the SEQUENCE attribute */
   11167          714 :   gfc_add_sequence (&sym->attr, sym->name, NULL);
   11168              : 
   11169          714 :   if (result) *result = sym;
   11170              : 
   11171              :   return true;
   11172              : }
   11173              : 
   11174              : 
   11175              : /* Match the opening of a MAP block. Like a struct within a union in C;
   11176              :    behaves identical to STRUCTURE blocks.  */
   11177              : 
   11178              : match
   11179          259 : gfc_match_map (void)
   11180              : {
   11181              :   /* Counter used to give unique internal names to map structures. */
   11182          259 :   static unsigned int gfc_map_id = 0;
   11183          259 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11184          259 :   gfc_symbol *sym;
   11185          259 :   locus old_loc;
   11186              : 
   11187          259 :   old_loc = gfc_current_locus;
   11188              : 
   11189          259 :   if (gfc_match_eos () != MATCH_YES)
   11190              :     {
   11191            1 :         gfc_error ("Junk after MAP statement at %C");
   11192            1 :         gfc_current_locus = old_loc;
   11193            1 :         return MATCH_ERROR;
   11194              :     }
   11195              : 
   11196              :   /* Map blocks are anonymous so we make up unique names for the symbol table
   11197              :      which are invalid Fortran identifiers.  */
   11198          258 :   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
   11199              : 
   11200          258 :   if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
   11201              :     return MATCH_ERROR;
   11202              : 
   11203          258 :   gfc_new_block = sym;
   11204              : 
   11205          258 :   return MATCH_YES;
   11206              : }
   11207              : 
   11208              : 
   11209              : /* Match the opening of a UNION block.  */
   11210              : 
   11211              : match
   11212          133 : gfc_match_union (void)
   11213              : {
   11214              :   /* Counter used to give unique internal names to union types. */
   11215          133 :   static unsigned int gfc_union_id = 0;
   11216          133 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11217          133 :   gfc_symbol *sym;
   11218          133 :   locus old_loc;
   11219              : 
   11220          133 :   old_loc = gfc_current_locus;
   11221              : 
   11222          133 :   if (gfc_match_eos () != MATCH_YES)
   11223              :     {
   11224            1 :         gfc_error ("Junk after UNION statement at %C");
   11225            1 :         gfc_current_locus = old_loc;
   11226            1 :         return MATCH_ERROR;
   11227              :     }
   11228              : 
   11229              :   /* Unions are anonymous so we make up unique names for the symbol table
   11230              :      which are invalid Fortran identifiers.  */
   11231          132 :   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
   11232              : 
   11233          132 :   if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
   11234              :     return MATCH_ERROR;
   11235              : 
   11236          132 :   gfc_new_block = sym;
   11237              : 
   11238          132 :   return MATCH_YES;
   11239              : }
   11240              : 
   11241              : 
   11242              : /* Match the beginning of a STRUCTURE declaration. This is similar to
   11243              :    matching the beginning of a derived type declaration with a few
   11244              :    twists. The resulting type symbol has no access control or other
   11245              :    interesting attributes.  */
   11246              : 
   11247              : match
   11248          336 : gfc_match_structure_decl (void)
   11249              : {
   11250              :   /* Counter used to give unique internal names to anonymous structures.  */
   11251          336 :   static unsigned int gfc_structure_id = 0;
   11252          336 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11253          336 :   gfc_symbol *sym;
   11254          336 :   match m;
   11255          336 :   locus where;
   11256              : 
   11257          336 :   if (!flag_dec_structure)
   11258              :     {
   11259            3 :       gfc_error ("%s at %C is a DEC extension, enable with "
   11260              :                  "%<-fdec-structure%>",
   11261              :                  "STRUCTURE");
   11262            3 :       return MATCH_ERROR;
   11263              :     }
   11264              : 
   11265          333 :   name[0] = '\0';
   11266              : 
   11267          333 :   m = gfc_match (" /%n/", name);
   11268          333 :   if (m != MATCH_YES)
   11269              :     {
   11270              :       /* Non-nested structure declarations require a structure name.  */
   11271           24 :       if (!gfc_comp_struct (gfc_current_state ()))
   11272              :         {
   11273            4 :             gfc_error ("Structure name expected in non-nested structure "
   11274              :                        "declaration at %C");
   11275            4 :             return MATCH_ERROR;
   11276              :         }
   11277              :       /* This is an anonymous structure; make up a unique name for it
   11278              :          (upper-case letters never make it to symbol names from the source).
   11279              :          The important thing is initializing the type variable
   11280              :          and setting gfc_new_symbol, which is immediately used by
   11281              :          parse_structure () and variable_decl () to add components of
   11282              :          this type.  */
   11283           20 :       snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
   11284              :     }
   11285              : 
   11286          329 :   where = gfc_current_locus;
   11287              :   /* No field list allowed after non-nested structure declaration.  */
   11288          329 :   if (!gfc_comp_struct (gfc_current_state ())
   11289          296 :       && gfc_match_eos () != MATCH_YES)
   11290              :     {
   11291            1 :       gfc_error ("Junk after non-nested STRUCTURE statement at %C");
   11292            1 :       return MATCH_ERROR;
   11293              :     }
   11294              : 
   11295              :   /* Make sure the name is not the name of an intrinsic type.  */
   11296          328 :   if (gfc_is_intrinsic_typename (name))
   11297              :     {
   11298            1 :       gfc_error ("Structure name %qs at %C cannot be the same as an"
   11299              :                  " intrinsic type", name);
   11300            1 :       return MATCH_ERROR;
   11301              :     }
   11302              : 
   11303              :   /* Store the actual type symbol for the structure with an upper-case first
   11304              :      letter (an invalid Fortran identifier).  */
   11305              : 
   11306          327 :   if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
   11307              :     return MATCH_ERROR;
   11308              : 
   11309          324 :   gfc_new_block = sym;
   11310          324 :   return MATCH_YES;
   11311              : }
   11312              : 
   11313              : 
   11314              : /* This function does some work to determine which matcher should be used to
   11315              :  * match a statement beginning with "TYPE".  This is used to disambiguate TYPE
   11316              :  * as an alias for PRINT from derived type declarations, TYPE IS statements,
   11317              :  * and [parameterized] derived type declarations.  */
   11318              : 
   11319              : match
   11320       538440 : gfc_match_type (gfc_statement *st)
   11321              : {
   11322       538440 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11323       538440 :   match m;
   11324       538440 :   locus old_loc;
   11325              : 
   11326              :   /* Requires -fdec.  */
   11327       538440 :   if (!flag_dec)
   11328              :     return MATCH_NO;
   11329              : 
   11330         2483 :   m = gfc_match ("type");
   11331         2483 :   if (m != MATCH_YES)
   11332              :     return m;
   11333              :   /* If we already have an error in the buffer, it is probably from failing to
   11334              :    * match a derived type data declaration. Let it happen.  */
   11335           20 :   else if (gfc_error_flag_test ())
   11336              :     return MATCH_NO;
   11337              : 
   11338           20 :   old_loc = gfc_current_locus;
   11339           20 :   *st = ST_NONE;
   11340              : 
   11341              :   /* If we see an attribute list before anything else it's definitely a derived
   11342              :    * type declaration.  */
   11343           20 :   if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
   11344            8 :     goto derived;
   11345              : 
   11346              :   /* By now "TYPE" has already been matched. If we do not see a name, this may
   11347              :    * be something like "TYPE *" or "TYPE <fmt>".  */
   11348           12 :   m = gfc_match_name (name);
   11349           12 :   if (m != MATCH_YES)
   11350              :     {
   11351              :       /* Let print match if it can, otherwise throw an error from
   11352              :        * gfc_match_derived_decl.  */
   11353            7 :       gfc_current_locus = old_loc;
   11354            7 :       if (gfc_match_print () == MATCH_YES)
   11355              :         {
   11356            7 :           *st = ST_WRITE;
   11357            7 :           return MATCH_YES;
   11358              :         }
   11359            0 :       goto derived;
   11360              :     }
   11361              : 
   11362              :   /* Check for EOS.  */
   11363            5 :   if (gfc_match_eos () == MATCH_YES)
   11364              :     {
   11365              :       /* By now we have "TYPE <name> <EOS>". Check first if the name is an
   11366              :        * intrinsic typename - if so let gfc_match_derived_decl dump an error.
   11367              :        * Otherwise if gfc_match_derived_decl fails it's probably an existing
   11368              :        * symbol which can be printed.  */
   11369            3 :       gfc_current_locus = old_loc;
   11370            3 :       m = gfc_match_derived_decl ();
   11371            3 :       if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
   11372              :         {
   11373            2 :           *st = ST_DERIVED_DECL;
   11374            2 :           return m;
   11375              :         }
   11376              :     }
   11377              :   else
   11378              :     {
   11379              :       /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
   11380              :          like <type name(parameter)>.  */
   11381            2 :       gfc_gobble_whitespace ();
   11382            2 :       bool paren = gfc_peek_ascii_char () == '(';
   11383            2 :       if (paren)
   11384              :         {
   11385            1 :           if (strcmp ("is", name) == 0)
   11386            1 :             goto typeis;
   11387              :           else
   11388            0 :             goto derived;
   11389              :         }
   11390              :     }
   11391              : 
   11392              :   /* Treat TYPE... like PRINT...  */
   11393            2 :   gfc_current_locus = old_loc;
   11394            2 :   *st = ST_WRITE;
   11395            2 :   return gfc_match_print ();
   11396              : 
   11397            8 : derived:
   11398            8 :   gfc_current_locus = old_loc;
   11399            8 :   *st = ST_DERIVED_DECL;
   11400            8 :   return gfc_match_derived_decl ();
   11401              : 
   11402            1 : typeis:
   11403            1 :   gfc_current_locus = old_loc;
   11404            1 :   *st = ST_TYPE_IS;
   11405            1 :   return gfc_match_type_is ();
   11406              : }
   11407              : 
   11408              : 
   11409              : /* Match the beginning of a derived type declaration.  If a type name
   11410              :    was the result of a function, then it is possible to have a symbol
   11411              :    already to be known as a derived type yet have no components.  */
   11412              : 
   11413              : match
   11414        17137 : gfc_match_derived_decl (void)
   11415              : {
   11416        17137 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11417        17137 :   char parent[GFC_MAX_SYMBOL_LEN + 1];
   11418        17137 :   symbol_attribute attr;
   11419        17137 :   gfc_symbol *sym, *gensym;
   11420        17137 :   gfc_symbol *extended;
   11421        17137 :   match m;
   11422        17137 :   match is_type_attr_spec = MATCH_NO;
   11423        17137 :   bool seen_attr = false;
   11424        17137 :   gfc_interface *intr = NULL, *head;
   11425        17137 :   bool parameterized_type = false;
   11426        17137 :   bool seen_colons = false;
   11427              : 
   11428        17137 :   if (gfc_comp_struct (gfc_current_state ()))
   11429              :     return MATCH_NO;
   11430              : 
   11431        17133 :   name[0] = '\0';
   11432        17133 :   parent[0] = '\0';
   11433        17133 :   gfc_clear_attr (&attr);
   11434        17133 :   extended = NULL;
   11435              : 
   11436        20041 :   do
   11437              :     {
   11438        20041 :       is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
   11439        20041 :       if (is_type_attr_spec == MATCH_ERROR)
   11440              :         return MATCH_ERROR;
   11441        20038 :       if (is_type_attr_spec == MATCH_YES)
   11442         2908 :         seen_attr = true;
   11443        20038 :     } while (is_type_attr_spec == MATCH_YES);
   11444              : 
   11445              :   /* Deal with derived type extensions.  The extension attribute has
   11446              :      been added to 'attr' but now the parent type must be found and
   11447              :      checked.  */
   11448        17130 :   if (parent[0])
   11449         1581 :     extended = check_extended_derived_type (parent);
   11450              : 
   11451        17130 :   if (parent[0] && !extended)
   11452              :     return MATCH_ERROR;
   11453              : 
   11454        17126 :   m = gfc_match (" ::");
   11455        17126 :   if (m == MATCH_YES)
   11456              :     {
   11457              :       seen_colons = true;
   11458              :     }
   11459        10656 :   else if (seen_attr)
   11460              :     {
   11461            5 :       gfc_error ("Expected :: in TYPE definition at %C");
   11462            5 :       return MATCH_ERROR;
   11463              :     }
   11464              : 
   11465              :   /*  In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
   11466              :       But, we need to simply return for TYPE(.  */
   11467        10651 :   if (m == MATCH_NO && gfc_current_form == FORM_FREE)
   11468              :     {
   11469        10602 :       char c = gfc_peek_ascii_char ();
   11470        10602 :       if (c == '(')
   11471              :         return m;
   11472        10521 :       if (!gfc_is_whitespace (c))
   11473              :         {
   11474            4 :           gfc_error ("Mangled derived type definition at %C");
   11475            4 :           return MATCH_NO;
   11476              :         }
   11477              :     }
   11478              : 
   11479        17036 :   m = gfc_match (" %n ", name);
   11480        17036 :   if (m != MATCH_YES)
   11481              :     return m;
   11482              : 
   11483              :   /* Make sure that we don't identify TYPE IS (...) as a parameterized
   11484              :      derived type named 'is'.
   11485              :      TODO Expand the check, when 'name' = "is" by matching " (tname) "
   11486              :      and checking if this is a(n intrinsic) typename.  This picks up
   11487              :      misplaced TYPE IS statements such as in select_type_1.f03.  */
   11488        17024 :   if (gfc_peek_ascii_char () == '(')
   11489              :     {
   11490         4067 :       if (gfc_current_state () == COMP_SELECT_TYPE
   11491          531 :           || (!seen_colons && !strcmp (name, "is")))
   11492              :         return MATCH_NO;
   11493              :       parameterized_type = true;
   11494              :     }
   11495              : 
   11496        13486 :   m = gfc_match_eos ();
   11497        13486 :   if (m != MATCH_YES && !parameterized_type)
   11498              :     return m;
   11499              : 
   11500              :   /* Make sure the name is not the name of an intrinsic type.  */
   11501        13483 :   if (gfc_is_intrinsic_typename (name))
   11502              :     {
   11503           18 :       gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
   11504              :                  "type", name);
   11505           18 :       return MATCH_ERROR;
   11506              :     }
   11507              : 
   11508        13465 :   if (gfc_get_symbol (name, NULL, &gensym))
   11509              :     return MATCH_ERROR;
   11510              : 
   11511        13465 :   if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
   11512              :     {
   11513            5 :       if (gensym->ts.u.derived)
   11514            0 :         gfc_error ("Derived type name %qs at %C already has a basic type "
   11515              :                    "of %s", gensym->name, gfc_typename (&gensym->ts));
   11516              :       else
   11517            5 :         gfc_error ("Derived type name %qs at %C already has a basic type",
   11518              :                    gensym->name);
   11519              :       return MATCH_ERROR;
   11520              :     }
   11521              : 
   11522        13460 :   if (!gensym->attr.generic
   11523        13460 :       && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
   11524              :     return MATCH_ERROR;
   11525              : 
   11526        13456 :   if (!gensym->attr.function
   11527        13456 :       && !gfc_add_function (&gensym->attr, gensym->name, NULL))
   11528              :     return MATCH_ERROR;
   11529              : 
   11530        13455 :   if (gensym->attr.dummy)
   11531              :     {
   11532            1 :       gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
   11533              :                  name, &gensym->declared_at);
   11534            1 :       return MATCH_ERROR;
   11535              :     }
   11536              : 
   11537        13454 :   sym = gfc_find_dt_in_generic (gensym);
   11538              : 
   11539        13454 :   if (sym && (sym->components != NULL || sym->attr.zero_comp))
   11540              :     {
   11541            1 :       gfc_error ("Derived type definition of %qs at %C has already been "
   11542              :                  "defined", sym->name);
   11543            1 :       return MATCH_ERROR;
   11544              :     }
   11545              : 
   11546        13453 :   if (!sym)
   11547              :     {
   11548              :       /* Use upper case to save the actual derived-type symbol.  */
   11549        13363 :       gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
   11550        13363 :       sym->name = gfc_get_string ("%s", gensym->name);
   11551        13363 :       head = gensym->generic;
   11552        13363 :       intr = gfc_get_interface ();
   11553        13363 :       intr->sym = sym;
   11554        13363 :       intr->where = gfc_current_locus;
   11555        13363 :       intr->sym->declared_at = gfc_current_locus;
   11556        13363 :       intr->next = head;
   11557        13363 :       gensym->generic = intr;
   11558        13363 :       gensym->attr.if_source = IFSRC_DECL;
   11559              :     }
   11560              : 
   11561              :   /* The symbol may already have the derived attribute without the
   11562              :      components.  The ways this can happen is via a function
   11563              :      definition, an INTRINSIC statement or a subtype in another
   11564              :      derived type that is a pointer.  The first part of the AND clause
   11565              :      is true if the symbol is not the return value of a function.  */
   11566        13453 :   if (sym->attr.flavor != FL_DERIVED
   11567        13453 :       && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
   11568              :     return MATCH_ERROR;
   11569              : 
   11570        13453 :   if (attr.access != ACCESS_UNKNOWN
   11571        13453 :       && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
   11572              :     return MATCH_ERROR;
   11573        13453 :   else if (sym->attr.access == ACCESS_UNKNOWN
   11574        12885 :            && gensym->attr.access != ACCESS_UNKNOWN
   11575        13801 :            && !gfc_add_access (&sym->attr, gensym->attr.access,
   11576              :                                sym->name, NULL))
   11577              :     return MATCH_ERROR;
   11578              : 
   11579        13453 :   if (sym->attr.access != ACCESS_UNKNOWN
   11580          916 :       && gensym->attr.access == ACCESS_UNKNOWN)
   11581          568 :     gensym->attr.access = sym->attr.access;
   11582              : 
   11583              :   /* See if the derived type was labeled as bind(c).  */
   11584        13453 :   if (attr.is_bind_c != 0)
   11585          404 :     sym->attr.is_bind_c = attr.is_bind_c;
   11586              : 
   11587              :   /* Construct the f2k_derived namespace if it is not yet there.  */
   11588        13453 :   if (!sym->f2k_derived)
   11589        13453 :     sym->f2k_derived = gfc_get_namespace (NULL, 0);
   11590              : 
   11591        13453 :   if (parameterized_type)
   11592              :     {
   11593              :       /* Ignore error or mismatches by going to the end of the statement
   11594              :          in order to avoid the component declarations causing problems.  */
   11595          529 :       m = gfc_match_formal_arglist (sym, 0, 0, true);
   11596          529 :       if (m != MATCH_YES)
   11597            4 :         gfc_error_recovery ();
   11598              :       else
   11599          525 :         sym->attr.pdt_template = 1;
   11600          529 :       m = gfc_match_eos ();
   11601          529 :       if (m != MATCH_YES)
   11602              :         {
   11603            1 :           gfc_error_recovery ();
   11604            1 :           gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
   11605              :         }
   11606              :     }
   11607              : 
   11608        13453 :   if (extended && !sym->components)
   11609              :     {
   11610         1577 :       gfc_component *p;
   11611         1577 :       gfc_formal_arglist *f, *g, *h;
   11612              : 
   11613              :       /* Add the extended derived type as the first component.  */
   11614         1577 :       gfc_add_component (sym, parent, &p);
   11615         1577 :       extended->refs++;
   11616         1577 :       gfc_set_sym_referenced (extended);
   11617              : 
   11618         1577 :       p->ts.type = BT_DERIVED;
   11619         1577 :       p->ts.u.derived = extended;
   11620         1577 :       p->initializer = gfc_default_initializer (&p->ts);
   11621              : 
   11622              :       /* Set extension level.  */
   11623         1577 :       if (extended->attr.extension == 255)
   11624              :         {
   11625              :           /* Since the extension field is 8 bit wide, we can only have
   11626              :              up to 255 extension levels.  */
   11627            0 :           gfc_error ("Maximum extension level reached with type %qs at %L",
   11628              :                      extended->name, &extended->declared_at);
   11629            0 :           return MATCH_ERROR;
   11630              :         }
   11631         1577 :       sym->attr.extension = extended->attr.extension + 1;
   11632              : 
   11633              :       /* Provide the links between the extended type and its extension.  */
   11634         1577 :       if (!extended->f2k_derived)
   11635            1 :         extended->f2k_derived = gfc_get_namespace (NULL, 0);
   11636              : 
   11637              :       /* Copy the extended type-param-name-list from the extended type,
   11638              :          append those of the extension and add the whole lot to the
   11639              :          extension.  */
   11640         1577 :       if (extended->attr.pdt_template)
   11641              :         {
   11642           64 :           g = h = NULL;
   11643           64 :           sym->attr.pdt_template = 1;
   11644          195 :           for (f = extended->formal; f; f = f->next)
   11645              :             {
   11646          131 :               if (f == extended->formal)
   11647              :                 {
   11648           64 :                   g = gfc_get_formal_arglist ();
   11649           64 :                   h = g;
   11650              :                 }
   11651              :               else
   11652              :                 {
   11653           67 :                   g->next = gfc_get_formal_arglist ();
   11654           67 :                   g = g->next;
   11655              :                 }
   11656          131 :               g->sym = f->sym;
   11657              :             }
   11658           64 :           g->next = sym->formal;
   11659           64 :           sym->formal = h;
   11660              :         }
   11661              :     }
   11662              : 
   11663        13453 :   if (!sym->hash_value)
   11664              :     /* Set the hash for the compound name for this type.  */
   11665        13453 :     sym->hash_value = gfc_hash_value (sym);
   11666              : 
   11667              :   /* Take over the ABSTRACT attribute.  */
   11668        13453 :   sym->attr.abstract = attr.abstract;
   11669              : 
   11670        13453 :   gfc_new_block = sym;
   11671              : 
   11672        13453 :   return MATCH_YES;
   11673              : }
   11674              : 
   11675              : 
   11676              : /* Cray Pointees can be declared as:
   11677              :       pointer (ipt, a (n,m,...,*))  */
   11678              : 
   11679              : match
   11680          240 : gfc_mod_pointee_as (gfc_array_spec *as)
   11681              : {
   11682          240 :   as->cray_pointee = true; /* This will be useful to know later.  */
   11683          240 :   if (as->type == AS_ASSUMED_SIZE)
   11684           72 :     as->cp_was_assumed = true;
   11685          168 :   else if (as->type == AS_ASSUMED_SHAPE)
   11686              :     {
   11687            0 :       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
   11688            0 :       return MATCH_ERROR;
   11689              :     }
   11690              :   return MATCH_YES;
   11691              : }
   11692              : 
   11693              : 
   11694              : /* Match the enum definition statement, here we are trying to match
   11695              :    the first line of enum definition statement.
   11696              :    Returns MATCH_YES if match is found.  */
   11697              : 
   11698              : match
   11699          158 : gfc_match_enum (void)
   11700              : {
   11701          158 :   match m;
   11702              : 
   11703          158 :   m = gfc_match_eos ();
   11704          158 :   if (m != MATCH_YES)
   11705              :     return m;
   11706              : 
   11707          158 :   if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
   11708            0 :     return MATCH_ERROR;
   11709              : 
   11710              :   return MATCH_YES;
   11711              : }
   11712              : 
   11713              : 
   11714              : /* Returns an initializer whose value is one higher than the value of the
   11715              :    LAST_INITIALIZER argument.  If the argument is NULL, the
   11716              :    initializers value will be set to zero.  The initializer's kind
   11717              :    will be set to gfc_c_int_kind.
   11718              : 
   11719              :    If -fshort-enums is given, the appropriate kind will be selected
   11720              :    later after all enumerators have been parsed.  A warning is issued
   11721              :    here if an initializer exceeds gfc_c_int_kind.  */
   11722              : 
   11723              : static gfc_expr *
   11724          377 : enum_initializer (gfc_expr *last_initializer, locus where)
   11725              : {
   11726          377 :   gfc_expr *result;
   11727          377 :   result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
   11728              : 
   11729          377 :   mpz_init (result->value.integer);
   11730              : 
   11731          377 :   if (last_initializer != NULL)
   11732              :     {
   11733          266 :       mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
   11734          266 :       result->where = last_initializer->where;
   11735              : 
   11736          266 :       if (gfc_check_integer_range (result->value.integer,
   11737              :              gfc_c_int_kind) != ARITH_OK)
   11738              :         {
   11739            0 :           gfc_error ("Enumerator exceeds the C integer type at %C");
   11740            0 :           return NULL;
   11741              :         }
   11742              :     }
   11743              :   else
   11744              :     {
   11745              :       /* Control comes here, if it's the very first enumerator and no
   11746              :          initializer has been given.  It will be initialized to zero.  */
   11747          111 :       mpz_set_si (result->value.integer, 0);
   11748              :     }
   11749              : 
   11750              :   return result;
   11751              : }
   11752              : 
   11753              : 
   11754              : /* Match a variable name with an optional initializer.  When this
   11755              :    subroutine is called, a variable is expected to be parsed next.
   11756              :    Depending on what is happening at the moment, updates either the
   11757              :    symbol table or the current interface.  */
   11758              : 
   11759              : static match
   11760          549 : enumerator_decl (void)
   11761              : {
   11762          549 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   11763          549 :   gfc_expr *initializer;
   11764          549 :   gfc_array_spec *as = NULL;
   11765          549 :   gfc_charlen *saved_cl_list;
   11766          549 :   gfc_symbol *sym;
   11767          549 :   locus var_locus;
   11768          549 :   match m;
   11769          549 :   bool t;
   11770          549 :   locus old_locus;
   11771              : 
   11772          549 :   initializer = NULL;
   11773          549 :   saved_cl_list = gfc_current_ns->cl_list;
   11774          549 :   old_locus = gfc_current_locus;
   11775              : 
   11776              :   /* When we get here, we've just matched a list of attributes and
   11777              :      maybe a type and a double colon.  The next thing we expect to see
   11778              :      is the name of the symbol.  */
   11779          549 :   m = gfc_match_name (name);
   11780          549 :   if (m != MATCH_YES)
   11781            1 :     goto cleanup;
   11782              : 
   11783          548 :   var_locus = gfc_current_locus;
   11784              : 
   11785              :   /* OK, we've successfully matched the declaration.  Now put the
   11786              :      symbol in the current namespace. If we fail to create the symbol,
   11787              :      bail out.  */
   11788          548 :   if (!build_sym (name, 1, NULL, false, &as, &var_locus))
   11789              :     {
   11790            1 :       m = MATCH_ERROR;
   11791            1 :       goto cleanup;
   11792              :     }
   11793              : 
   11794              :   /* The double colon must be present in order to have initializers.
   11795              :      Otherwise the statement is ambiguous with an assignment statement.  */
   11796          547 :   if (colon_seen)
   11797              :     {
   11798          471 :       if (gfc_match_char ('=') == MATCH_YES)
   11799              :         {
   11800          170 :           m = gfc_match_init_expr (&initializer);
   11801          170 :           if (m == MATCH_NO)
   11802              :             {
   11803            0 :               gfc_error ("Expected an initialization expression at %C");
   11804            0 :               m = MATCH_ERROR;
   11805              :             }
   11806              : 
   11807          170 :           if (m != MATCH_YES)
   11808            2 :             goto cleanup;
   11809              :         }
   11810              :     }
   11811              : 
   11812              :   /* If we do not have an initializer, the initialization value of the
   11813              :      previous enumerator (stored in last_initializer) is incremented
   11814              :      by 1 and is used to initialize the current enumerator.  */
   11815          545 :   if (initializer == NULL)
   11816          377 :     initializer = enum_initializer (last_initializer, old_locus);
   11817              : 
   11818          545 :   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
   11819              :     {
   11820            2 :       gfc_error ("ENUMERATOR %L not initialized with integer expression",
   11821              :                  &var_locus);
   11822            2 :       m = MATCH_ERROR;
   11823            2 :       goto cleanup;
   11824              :     }
   11825              : 
   11826              :   /* Store this current initializer, for the next enumerator variable
   11827              :      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
   11828              :      use last_initializer below.  */
   11829          543 :   last_initializer = initializer;
   11830          543 :   t = add_init_expr_to_sym (name, &initializer, &var_locus,
   11831              :                             saved_cl_list);
   11832              : 
   11833              :   /* Maintain enumerator history.  */
   11834          543 :   gfc_find_symbol (name, NULL, 0, &sym);
   11835          543 :   create_enum_history (sym, last_initializer);
   11836              : 
   11837          543 :   return (t) ? MATCH_YES : MATCH_ERROR;
   11838              : 
   11839            6 : cleanup:
   11840              :   /* Free stuff up and return.  */
   11841            6 :   gfc_free_expr (initializer);
   11842              : 
   11843            6 :   return m;
   11844              : }
   11845              : 
   11846              : 
   11847              : /* Match the enumerator definition statement.  */
   11848              : 
   11849              : match
   11850       821426 : gfc_match_enumerator_def (void)
   11851              : {
   11852       821426 :   match m;
   11853       821426 :   bool t;
   11854              : 
   11855       821426 :   gfc_clear_ts (&current_ts);
   11856              : 
   11857       821426 :   m = gfc_match (" enumerator");
   11858       821426 :   if (m != MATCH_YES)
   11859              :     return m;
   11860              : 
   11861          269 :   m = gfc_match (" :: ");
   11862          269 :   if (m == MATCH_ERROR)
   11863              :     return m;
   11864              : 
   11865          269 :   colon_seen = (m == MATCH_YES);
   11866              : 
   11867          269 :   if (gfc_current_state () != COMP_ENUM)
   11868              :     {
   11869            4 :       gfc_error ("ENUM definition statement expected before %C");
   11870            4 :       gfc_free_enum_history ();
   11871            4 :       return MATCH_ERROR;
   11872              :     }
   11873              : 
   11874          265 :   (&current_ts)->type = BT_INTEGER;
   11875          265 :   (&current_ts)->kind = gfc_c_int_kind;
   11876              : 
   11877          265 :   gfc_clear_attr (&current_attr);
   11878          265 :   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
   11879          265 :   if (!t)
   11880              :     {
   11881            0 :       m = MATCH_ERROR;
   11882            0 :       goto cleanup;
   11883              :     }
   11884              : 
   11885          549 :   for (;;)
   11886              :     {
   11887          549 :       m = enumerator_decl ();
   11888          549 :       if (m == MATCH_ERROR)
   11889              :         {
   11890            6 :           gfc_free_enum_history ();
   11891            6 :           goto cleanup;
   11892              :         }
   11893          543 :       if (m == MATCH_NO)
   11894              :         break;
   11895              : 
   11896          542 :       if (gfc_match_eos () == MATCH_YES)
   11897          256 :         goto cleanup;
   11898          286 :       if (gfc_match_char (',') != MATCH_YES)
   11899              :         break;
   11900              :     }
   11901              : 
   11902            3 :   if (gfc_current_state () == COMP_ENUM)
   11903              :     {
   11904            3 :       gfc_free_enum_history ();
   11905            3 :       gfc_error ("Syntax error in ENUMERATOR definition at %C");
   11906            3 :       m = MATCH_ERROR;
   11907              :     }
   11908              : 
   11909            0 : cleanup:
   11910          265 :   gfc_free_array_spec (current_as);
   11911          265 :   current_as = NULL;
   11912          265 :   return m;
   11913              : 
   11914              : }
   11915              : 
   11916              : 
   11917              : /* Match binding attributes.  */
   11918              : 
   11919              : static match
   11920         4744 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
   11921              : {
   11922         4744 :   bool found_passing = false;
   11923         4744 :   bool seen_ptr = false;
   11924         4744 :   match m = MATCH_YES;
   11925              : 
   11926              :   /* Initialize to defaults.  Do so even before the MATCH_NO check so that in
   11927              :      this case the defaults are in there.  */
   11928         4744 :   ba->access = ACCESS_UNKNOWN;
   11929         4744 :   ba->pass_arg = NULL;
   11930         4744 :   ba->pass_arg_num = 0;
   11931         4744 :   ba->nopass = 0;
   11932         4744 :   ba->non_overridable = 0;
   11933         4744 :   ba->deferred = 0;
   11934         4744 :   ba->ppc = ppc;
   11935              : 
   11936              :   /* If we find a comma, we believe there are binding attributes.  */
   11937         4744 :   m = gfc_match_char (',');
   11938         4744 :   if (m == MATCH_NO)
   11939         2482 :     goto done;
   11940              : 
   11941         2817 :   do
   11942              :     {
   11943              :       /* Access specifier.  */
   11944              : 
   11945         2817 :       m = gfc_match (" public");
   11946         2817 :       if (m == MATCH_ERROR)
   11947            0 :         goto error;
   11948         2817 :       if (m == MATCH_YES)
   11949              :         {
   11950          250 :           if (ba->access != ACCESS_UNKNOWN)
   11951              :             {
   11952            0 :               gfc_error ("Duplicate access-specifier at %C");
   11953            0 :               goto error;
   11954              :             }
   11955              : 
   11956          250 :           ba->access = ACCESS_PUBLIC;
   11957          250 :           continue;
   11958              :         }
   11959              : 
   11960         2567 :       m = gfc_match (" private");
   11961         2567 :       if (m == MATCH_ERROR)
   11962            0 :         goto error;
   11963         2567 :       if (m == MATCH_YES)
   11964              :         {
   11965          181 :           if (ba->access != ACCESS_UNKNOWN)
   11966              :             {
   11967            1 :               gfc_error ("Duplicate access-specifier at %C");
   11968            1 :               goto error;
   11969              :             }
   11970              : 
   11971          180 :           ba->access = ACCESS_PRIVATE;
   11972          180 :           continue;
   11973              :         }
   11974              : 
   11975              :       /* If inside GENERIC, the following is not allowed.  */
   11976         2386 :       if (!generic)
   11977              :         {
   11978              : 
   11979              :           /* NOPASS flag.  */
   11980         2385 :           m = gfc_match (" nopass");
   11981         2385 :           if (m == MATCH_ERROR)
   11982            0 :             goto error;
   11983         2385 :           if (m == MATCH_YES)
   11984              :             {
   11985          725 :               if (found_passing)
   11986              :                 {
   11987            1 :                   gfc_error ("Binding attributes already specify passing,"
   11988              :                              " illegal NOPASS at %C");
   11989            1 :                   goto error;
   11990              :                 }
   11991              : 
   11992          724 :               found_passing = true;
   11993          724 :               ba->nopass = 1;
   11994          724 :               continue;
   11995              :             }
   11996              : 
   11997              :           /* PASS possibly including argument.  */
   11998         1660 :           m = gfc_match (" pass");
   11999         1660 :           if (m == MATCH_ERROR)
   12000            0 :             goto error;
   12001         1660 :           if (m == MATCH_YES)
   12002              :             {
   12003          901 :               char arg[GFC_MAX_SYMBOL_LEN + 1];
   12004              : 
   12005          901 :               if (found_passing)
   12006              :                 {
   12007            2 :                   gfc_error ("Binding attributes already specify passing,"
   12008              :                              " illegal PASS at %C");
   12009            2 :                   goto error;
   12010              :                 }
   12011              : 
   12012          899 :               m = gfc_match (" ( %n )", arg);
   12013          899 :               if (m == MATCH_ERROR)
   12014            0 :                 goto error;
   12015          899 :               if (m == MATCH_YES)
   12016          490 :                 ba->pass_arg = gfc_get_string ("%s", arg);
   12017          899 :               gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
   12018              : 
   12019          899 :               found_passing = true;
   12020          899 :               ba->nopass = 0;
   12021          899 :               continue;
   12022          899 :             }
   12023              : 
   12024          759 :           if (ppc)
   12025              :             {
   12026              :               /* POINTER flag.  */
   12027          437 :               m = gfc_match (" pointer");
   12028          437 :               if (m == MATCH_ERROR)
   12029            0 :                 goto error;
   12030          437 :               if (m == MATCH_YES)
   12031              :                 {
   12032          437 :                   if (seen_ptr)
   12033              :                     {
   12034            1 :                       gfc_error ("Duplicate POINTER attribute at %C");
   12035            1 :                       goto error;
   12036              :                     }
   12037              : 
   12038          436 :                   seen_ptr = true;
   12039          436 :                   continue;
   12040              :                 }
   12041              :             }
   12042              :           else
   12043              :             {
   12044              :               /* NON_OVERRIDABLE flag.  */
   12045          322 :               m = gfc_match (" non_overridable");
   12046          322 :               if (m == MATCH_ERROR)
   12047            0 :                 goto error;
   12048          322 :               if (m == MATCH_YES)
   12049              :                 {
   12050           62 :                   if (ba->non_overridable)
   12051              :                     {
   12052            1 :                       gfc_error ("Duplicate NON_OVERRIDABLE at %C");
   12053            1 :                       goto error;
   12054              :                     }
   12055              : 
   12056           61 :                   ba->non_overridable = 1;
   12057           61 :                   continue;
   12058              :                 }
   12059              : 
   12060              :               /* DEFERRED flag.  */
   12061          260 :               m = gfc_match (" deferred");
   12062          260 :               if (m == MATCH_ERROR)
   12063            0 :                 goto error;
   12064          260 :               if (m == MATCH_YES)
   12065              :                 {
   12066          260 :                   if (ba->deferred)
   12067              :                     {
   12068            1 :                       gfc_error ("Duplicate DEFERRED at %C");
   12069            1 :                       goto error;
   12070              :                     }
   12071              : 
   12072          259 :                   ba->deferred = 1;
   12073          259 :                   continue;
   12074              :                 }
   12075              :             }
   12076              : 
   12077              :         }
   12078              : 
   12079              :       /* Nothing matching found.  */
   12080            1 :       if (generic)
   12081            1 :         gfc_error ("Expected access-specifier at %C");
   12082              :       else
   12083            0 :         gfc_error ("Expected binding attribute at %C");
   12084            1 :       goto error;
   12085              :     }
   12086         2809 :   while (gfc_match_char (',') == MATCH_YES);
   12087              : 
   12088              :   /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
   12089         2254 :   if (ba->non_overridable && ba->deferred)
   12090              :     {
   12091            1 :       gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
   12092            1 :       goto error;
   12093              :     }
   12094              : 
   12095              :   m = MATCH_YES;
   12096              : 
   12097         4735 : done:
   12098         4735 :   if (ba->access == ACCESS_UNKNOWN)
   12099         4306 :     ba->access = ppc ? gfc_current_block()->component_access
   12100              :                      : gfc_typebound_default_access;
   12101              : 
   12102         4735 :   if (ppc && !seen_ptr)
   12103              :     {
   12104            2 :       gfc_error ("POINTER attribute is required for procedure pointer component"
   12105              :                  " at %C");
   12106            2 :       goto error;
   12107              :     }
   12108              : 
   12109              :   return m;
   12110              : 
   12111         4744 : error:
   12112              :   return MATCH_ERROR;
   12113              : }
   12114              : 
   12115              : 
   12116              : /* Match a PROCEDURE specific binding inside a derived type.  */
   12117              : 
   12118              : static match
   12119         3254 : match_procedure_in_type (void)
   12120              : {
   12121         3254 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12122         3254 :   char target_buf[GFC_MAX_SYMBOL_LEN + 1];
   12123         3254 :   char* target = NULL, *ifc = NULL;
   12124         3254 :   gfc_typebound_proc tb;
   12125         3254 :   bool seen_colons;
   12126         3254 :   bool seen_attrs;
   12127         3254 :   match m;
   12128         3254 :   gfc_symtree* stree;
   12129         3254 :   gfc_namespace* ns;
   12130         3254 :   gfc_symbol* block;
   12131         3254 :   int num;
   12132              : 
   12133              :   /* Check current state.  */
   12134         3254 :   gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
   12135         3254 :   block = gfc_state_stack->previous->sym;
   12136         3254 :   gcc_assert (block);
   12137              : 
   12138              :   /* Try to match PROCEDURE(interface).  */
   12139         3254 :   if (gfc_match (" (") == MATCH_YES)
   12140              :     {
   12141          261 :       m = gfc_match_name (target_buf);
   12142          261 :       if (m == MATCH_ERROR)
   12143              :         return m;
   12144          261 :       if (m != MATCH_YES)
   12145              :         {
   12146            1 :           gfc_error ("Interface-name expected after %<(%> at %C");
   12147            1 :           return MATCH_ERROR;
   12148              :         }
   12149              : 
   12150          260 :       if (gfc_match (" )") != MATCH_YES)
   12151              :         {
   12152            1 :           gfc_error ("%<)%> expected at %C");
   12153            1 :           return MATCH_ERROR;
   12154              :         }
   12155              : 
   12156              :       ifc = target_buf;
   12157              :     }
   12158              : 
   12159              :   /* Construct the data structure.  */
   12160         3252 :   memset (&tb, 0, sizeof (tb));
   12161         3252 :   tb.where = gfc_current_locus;
   12162              : 
   12163              :   /* Match binding attributes.  */
   12164         3252 :   m = match_binding_attributes (&tb, false, false);
   12165         3252 :   if (m == MATCH_ERROR)
   12166              :     return m;
   12167         3245 :   seen_attrs = (m == MATCH_YES);
   12168              : 
   12169              :   /* Check that attribute DEFERRED is given if an interface is specified.  */
   12170         3245 :   if (tb.deferred && !ifc)
   12171              :     {
   12172            1 :       gfc_error ("Interface must be specified for DEFERRED binding at %C");
   12173            1 :       return MATCH_ERROR;
   12174              :     }
   12175         3244 :   if (ifc && !tb.deferred)
   12176              :     {
   12177            1 :       gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
   12178            1 :       return MATCH_ERROR;
   12179              :     }
   12180              : 
   12181              :   /* Match the colons.  */
   12182         3243 :   m = gfc_match (" ::");
   12183         3243 :   if (m == MATCH_ERROR)
   12184              :     return m;
   12185         3243 :   seen_colons = (m == MATCH_YES);
   12186         3243 :   if (seen_attrs && !seen_colons)
   12187              :     {
   12188            4 :       gfc_error ("Expected %<::%> after binding-attributes at %C");
   12189            4 :       return MATCH_ERROR;
   12190              :     }
   12191              : 
   12192              :   /* Match the binding names.  */
   12193           19 :   for(num=1;;num++)
   12194              :     {
   12195         3258 :       m = gfc_match_name (name);
   12196         3258 :       if (m == MATCH_ERROR)
   12197              :         return m;
   12198         3258 :       if (m == MATCH_NO)
   12199              :         {
   12200            5 :           gfc_error ("Expected binding name at %C");
   12201            5 :           return MATCH_ERROR;
   12202              :         }
   12203              : 
   12204         3253 :       if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
   12205              :         return MATCH_ERROR;
   12206              : 
   12207              :       /* Try to match the '=> target', if it's there.  */
   12208         3252 :       target = ifc;
   12209         3252 :       m = gfc_match (" =>");
   12210         3252 :       if (m == MATCH_ERROR)
   12211              :         return m;
   12212         3252 :       if (m == MATCH_YES)
   12213              :         {
   12214         1250 :           if (tb.deferred)
   12215              :             {
   12216            1 :               gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
   12217            1 :               return MATCH_ERROR;
   12218              :             }
   12219              : 
   12220         1249 :           if (!seen_colons)
   12221              :             {
   12222            1 :               gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
   12223              :                          " at %C");
   12224            1 :               return MATCH_ERROR;
   12225              :             }
   12226              : 
   12227         1248 :           m = gfc_match_name (target_buf);
   12228         1248 :           if (m == MATCH_ERROR)
   12229              :             return m;
   12230         1248 :           if (m == MATCH_NO)
   12231              :             {
   12232            2 :               gfc_error ("Expected binding target after %<=>%> at %C");
   12233            2 :               return MATCH_ERROR;
   12234              :             }
   12235              :           target = target_buf;
   12236              :         }
   12237              : 
   12238              :       /* If no target was found, it has the same name as the binding.  */
   12239         2002 :       if (!target)
   12240         1747 :         target = name;
   12241              : 
   12242              :       /* Get the namespace to insert the symbols into.  */
   12243         3248 :       ns = block->f2k_derived;
   12244         3248 :       gcc_assert (ns);
   12245              : 
   12246              :       /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
   12247         3248 :       if (tb.deferred && !block->attr.abstract)
   12248              :         {
   12249            1 :           gfc_error ("Type %qs containing DEFERRED binding at %C "
   12250              :                      "is not ABSTRACT", block->name);
   12251            1 :           return MATCH_ERROR;
   12252              :         }
   12253              : 
   12254              :       /* See if we already have a binding with this name in the symtree which
   12255              :          would be an error.  If a GENERIC already targeted this binding, it may
   12256              :          be already there but then typebound is still NULL.  */
   12257         3247 :       stree = gfc_find_symtree (ns->tb_sym_root, name);
   12258         3247 :       if (stree && stree->n.tb)
   12259              :         {
   12260            2 :           gfc_error ("There is already a procedure with binding name %qs for "
   12261              :                      "the derived type %qs at %C", name, block->name);
   12262            2 :           return MATCH_ERROR;
   12263              :         }
   12264              : 
   12265              :       /* Insert it and set attributes.  */
   12266              : 
   12267         3126 :       if (!stree)
   12268              :         {
   12269         3126 :           stree = gfc_new_symtree (&ns->tb_sym_root, name);
   12270         3126 :           gcc_assert (stree);
   12271              :         }
   12272         3245 :       stree->n.tb = gfc_get_typebound_proc (&tb);
   12273              : 
   12274         3245 :       if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
   12275              :                             false))
   12276              :         return MATCH_ERROR;
   12277         3245 :       gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
   12278         3245 :       gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
   12279         3245 :                      target, &stree->n.tb->u.specific->n.sym->declared_at);
   12280              : 
   12281         3245 :       if (gfc_match_eos () == MATCH_YES)
   12282              :         return MATCH_YES;
   12283           20 :       if (gfc_match_char (',') != MATCH_YES)
   12284            1 :         goto syntax;
   12285              :     }
   12286              : 
   12287            1 : syntax:
   12288            1 :   gfc_error ("Syntax error in PROCEDURE statement at %C");
   12289            1 :   return MATCH_ERROR;
   12290              : }
   12291              : 
   12292              : 
   12293              : /* Match a GENERIC statement.
   12294              : F2018 15.4.3.3 GENERIC statement
   12295              : 
   12296              : A GENERIC statement specifies a generic identifier for one or more specific
   12297              : procedures, in the same way as a generic interface block that does not contain
   12298              : interface bodies.
   12299              : 
   12300              : R1510 generic-stmt is:
   12301              : GENERIC [ , access-spec ] :: generic-spec => specific-procedure-list
   12302              : 
   12303              : C1510 (R1510) A specific-procedure in a GENERIC statement shall not specify a
   12304              : procedure that was specified previously in any accessible interface with the
   12305              : same generic identifier.
   12306              : 
   12307              : If access-spec appears, it specifies the accessibility (8.5.2) of generic-spec.
   12308              : 
   12309              : For GENERIC statements outside of a derived type, use is made of the existing,
   12310              : typebound matching functions to obtain access-spec and generic-spec.  After
   12311              : this the standard INTERFACE machinery is used. */
   12312              : 
   12313              : static match
   12314          100 : match_generic_stmt (void)
   12315              : {
   12316          100 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12317              :   /* Allow space for OPERATOR(...).  */
   12318          100 :   char generic_spec_name[GFC_MAX_SYMBOL_LEN + 16];
   12319              :   /* Generics other than uops  */
   12320          100 :   gfc_symbol* generic_spec = NULL;
   12321              :   /* Generic uops  */
   12322          100 :   gfc_user_op *generic_uop = NULL;
   12323              :   /* For the matching calls  */
   12324          100 :   gfc_typebound_proc tbattr;
   12325          100 :   gfc_namespace* ns = gfc_current_ns;
   12326          100 :   interface_type op_type;
   12327          100 :   gfc_intrinsic_op op;
   12328          100 :   match m;
   12329          100 :   gfc_symtree* st;
   12330              :   /* The specific-procedure-list  */
   12331          100 :   gfc_interface *generic = NULL;
   12332              :   /* The head of the specific-procedure-list  */
   12333          100 :   gfc_interface **generic_tail = NULL;
   12334              : 
   12335          100 :   memset (&tbattr, 0, sizeof (tbattr));
   12336          100 :   tbattr.where = gfc_current_locus;
   12337              : 
   12338              :   /* See if we get an access-specifier.  */
   12339          100 :   m = match_binding_attributes (&tbattr, true, false);
   12340          100 :   tbattr.where = gfc_current_locus;
   12341          100 :   if (m == MATCH_ERROR)
   12342            0 :     goto error;
   12343              : 
   12344              :   /* Now the colons, those are required.  */
   12345          100 :   if (gfc_match (" ::") != MATCH_YES)
   12346              :     {
   12347            0 :       gfc_error ("Expected %<::%> at %C");
   12348            0 :       goto error;
   12349              :     }
   12350              : 
   12351              :   /* Match the generic-spec name; depending on type (operator / generic) format
   12352              :      it for future error messages in 'generic_spec_name'.  */
   12353          100 :   m = gfc_match_generic_spec (&op_type, name, &op);
   12354          100 :   if (m == MATCH_ERROR)
   12355              :     return MATCH_ERROR;
   12356          100 :   if (m == MATCH_NO)
   12357              :     {
   12358            0 :       gfc_error ("Expected generic name or operator descriptor at %C");
   12359            0 :       goto error;
   12360              :     }
   12361              : 
   12362          100 :   switch (op_type)
   12363              :     {
   12364           63 :     case INTERFACE_GENERIC:
   12365           63 :     case INTERFACE_DTIO:
   12366           63 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "%s", name);
   12367           63 :       break;
   12368              : 
   12369           22 :     case INTERFACE_USER_OP:
   12370           22 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(.%s.)", name);
   12371           22 :       break;
   12372              : 
   12373           13 :     case INTERFACE_INTRINSIC_OP:
   12374           13 :       snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(%s)",
   12375              :                 gfc_op2string (op));
   12376           13 :       break;
   12377              : 
   12378            2 :     case INTERFACE_NAMELESS:
   12379            2 :       gfc_error ("Malformed GENERIC statement at %C");
   12380            2 :       goto error;
   12381            0 :       break;
   12382              : 
   12383            0 :     default:
   12384            0 :       gcc_unreachable ();
   12385              :     }
   12386              : 
   12387              :   /* Match the required =>.  */
   12388           98 :   if (gfc_match (" =>") != MATCH_YES)
   12389              :     {
   12390            1 :       gfc_error ("Expected %<=>%> at %C");
   12391            1 :       goto error;
   12392              :     }
   12393              : 
   12394              : 
   12395           97 :   if (gfc_current_state () != COMP_MODULE && tbattr.access != ACCESS_UNKNOWN)
   12396              :     {
   12397            1 :       gfc_error ("The access specification at %L not in a module",
   12398              :                  &tbattr.where);
   12399            1 :       goto error;
   12400              :     }
   12401              : 
   12402              :   /* Try to find existing generic-spec with this name for this operator;
   12403              :      if there is something, check that it is another generic-spec and then
   12404              :      extend it rather than building a new symbol. Otherwise, create a new
   12405              :      one with the right attributes.  */
   12406              : 
   12407           96 :   switch (op_type)
   12408              :     {
   12409           61 :     case INTERFACE_DTIO:
   12410           61 :     case INTERFACE_GENERIC:
   12411           61 :       st = gfc_find_symtree (ns->sym_root, name);
   12412           61 :       generic_spec = st ? st->n.sym : NULL;
   12413           61 :       if (generic_spec)
   12414              :         {
   12415           25 :           if (generic_spec->attr.flavor != FL_PROCEDURE
   12416           11 :                && generic_spec->attr.flavor != FL_UNKNOWN)
   12417              :             {
   12418            1 :               gfc_error ("The generic-spec name %qs at %C clashes with the "
   12419              :                          "name of an entity declared at %L that is not a "
   12420              :                          "procedure", name, &generic_spec->declared_at);
   12421            1 :               goto error;
   12422              :             }
   12423              : 
   12424           24 :           if (op_type == INTERFACE_GENERIC && !generic_spec->attr.generic
   12425           10 :                && generic_spec->attr.flavor != FL_UNKNOWN)
   12426              :             {
   12427            0 :               gfc_error ("There's already a non-generic procedure with "
   12428              :                          "name %qs at %C", generic_spec->name);
   12429            0 :               goto error;
   12430              :             }
   12431              : 
   12432           24 :           if (tbattr.access != ACCESS_UNKNOWN)
   12433              :             {
   12434            2 :               if (generic_spec->attr.access != tbattr.access)
   12435              :                 {
   12436            1 :                   gfc_error ("The access specification at %L conflicts with "
   12437              :                              "that already given to %qs", &tbattr.where,
   12438              :                              generic_spec->name);
   12439            1 :                   goto error;
   12440              :                 }
   12441              :               else
   12442              :                 {
   12443            1 :                   gfc_error ("The access specification at %L repeats that "
   12444              :                              "already given to %qs", &tbattr.where,
   12445              :                              generic_spec->name);
   12446            1 :                   goto error;
   12447              :                 }
   12448              :             }
   12449              : 
   12450           22 :           if (generic_spec->ts.type != BT_UNKNOWN)
   12451              :             {
   12452            1 :               gfc_error ("The generic-spec in the generic statement at %C "
   12453              :                          "has a type from the declaration at %L",
   12454              :                          &generic_spec->declared_at);
   12455            1 :               goto error;
   12456              :             }
   12457              :         }
   12458              : 
   12459              :       /* Now create the generic_spec if it doesn't already exist and provide
   12460              :          is with the appropriate attributes.  */
   12461           57 :       if (!generic_spec || generic_spec->attr.flavor != FL_PROCEDURE)
   12462              :         {
   12463           45 :           if (!generic_spec)
   12464              :             {
   12465           36 :               gfc_get_symbol (name, ns, &generic_spec, &gfc_current_locus);
   12466           36 :               gfc_set_sym_referenced (generic_spec);
   12467           36 :               generic_spec->attr.access = tbattr.access;
   12468              :             }
   12469            9 :           else if (generic_spec->attr.access == ACCESS_UNKNOWN)
   12470            0 :             generic_spec->attr.access = tbattr.access;
   12471           45 :           generic_spec->refs++;
   12472           45 :           generic_spec->attr.generic = 1;
   12473           45 :           generic_spec->attr.flavor = FL_PROCEDURE;
   12474              : 
   12475           45 :           generic_spec->declared_at = gfc_current_locus;
   12476              :         }
   12477              : 
   12478              :       /* Prepare to add the specific procedures.  */
   12479           57 :       generic = generic_spec->generic;
   12480           57 :       generic_tail = &generic_spec->generic;
   12481           57 :       break;
   12482              : 
   12483           22 :     case INTERFACE_USER_OP:
   12484           22 :       st = gfc_find_symtree (ns->uop_root, name);
   12485           22 :       generic_uop = st ? st->n.uop : NULL;
   12486            2 :       if (generic_uop)
   12487              :         {
   12488            2 :           if (generic_uop->access != ACCESS_UNKNOWN
   12489            2 :               && tbattr.access != ACCESS_UNKNOWN)
   12490              :             {
   12491            2 :               if (generic_uop->access != tbattr.access)
   12492              :                 {
   12493            1 :                   gfc_error ("The user operator at %L must have the same "
   12494              :                              "access specification as already defined user "
   12495              :                              "operator %qs", &tbattr.where, generic_spec_name);
   12496            1 :                   goto error;
   12497              :                 }
   12498              :               else
   12499              :                 {
   12500            1 :                   gfc_error ("The user operator at %L repeats the access "
   12501              :                              "specification of already defined user operator "                                   "%qs", &tbattr.where, generic_spec_name);
   12502            1 :                   goto error;
   12503              :                 }
   12504              :             }
   12505            0 :           else if (generic_uop->access == ACCESS_UNKNOWN)
   12506            0 :             generic_uop->access = tbattr.access;
   12507              :         }
   12508              :       else
   12509              :         {
   12510           20 :           generic_uop = gfc_get_uop (name);
   12511           20 :           generic_uop->access = tbattr.access;
   12512              :         }
   12513              : 
   12514              :       /* Prepare to add the specific procedures.  */
   12515           20 :       generic = generic_uop->op;
   12516           20 :       generic_tail = &generic_uop->op;
   12517           20 :       break;
   12518              : 
   12519           13 :     case INTERFACE_INTRINSIC_OP:
   12520           13 :       generic = ns->op[op];
   12521           13 :       generic_tail = &ns->op[op];
   12522           13 :       break;
   12523              : 
   12524            0 :     default:
   12525            0 :       gcc_unreachable ();
   12526              :     }
   12527              : 
   12528              :   /* Now, match all following names in the specific-procedure-list.  */
   12529          154 :   do
   12530              :     {
   12531          154 :       m = gfc_match_name (name);
   12532          154 :       if (m == MATCH_ERROR)
   12533            0 :         goto error;
   12534          154 :       if (m == MATCH_NO)
   12535              :         {
   12536            0 :           gfc_error ("Expected specific procedure name at %C");
   12537            0 :           goto error;
   12538              :         }
   12539              : 
   12540          154 :       if (op_type == INTERFACE_GENERIC
   12541           95 :           && !strcmp (generic_spec->name, name))
   12542              :         {
   12543            2 :           gfc_error ("The name %qs of the specific procedure at %C conflicts "
   12544              :                      "with that of the generic-spec", name);
   12545            2 :           goto error;
   12546              :         }
   12547              : 
   12548          152 :       generic = *generic_tail;
   12549          242 :       for (; generic; generic = generic->next)
   12550              :         {
   12551           90 :           if (!strcmp (generic->sym->name, name))
   12552              :             {
   12553            0 :               gfc_error ("%qs already defined as a specific procedure for the"
   12554              :                          " generic %qs at %C", name, generic_spec->name);
   12555            0 :               goto error;
   12556              :             }
   12557              :         }
   12558              : 
   12559          152 :       gfc_find_sym_tree (name, ns, 1, &st);
   12560          152 :       if (!st)
   12561              :         {
   12562              :           /* This might be a procedure that has not yet been parsed. If
   12563              :              so gfc_fixup_sibling_symbols will replace this symbol with
   12564              :              that of the procedure.  */
   12565           75 :           gfc_get_sym_tree (name, ns, &st, false);
   12566           75 :           st->n.sym->refs++;
   12567              :         }
   12568              : 
   12569          152 :       generic = gfc_get_interface();
   12570          152 :       generic->next = *generic_tail;
   12571          152 :       *generic_tail = generic;
   12572          152 :       generic->where = gfc_current_locus;
   12573          152 :       generic->sym = st->n.sym;
   12574              :     }
   12575          152 :   while (gfc_match (" ,") == MATCH_YES);
   12576              : 
   12577           88 :   if (gfc_match_eos () != MATCH_YES)
   12578              :     {
   12579            0 :       gfc_error ("Junk after GENERIC statement at %C");
   12580            0 :       goto error;
   12581              :     }
   12582              : 
   12583           88 :   gfc_commit_symbols ();
   12584           88 :   return MATCH_YES;
   12585              : 
   12586          100 : error:
   12587              :   return MATCH_ERROR;
   12588              : }
   12589              : 
   12590              : 
   12591              : /* Match a GENERIC procedure binding inside a derived type.  */
   12592              : 
   12593              : static match
   12594          954 : match_typebound_generic (void)
   12595              : {
   12596          954 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12597          954 :   char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
   12598          954 :   gfc_symbol* block;
   12599          954 :   gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
   12600          954 :   gfc_typebound_proc* tb;
   12601          954 :   gfc_namespace* ns;
   12602          954 :   interface_type op_type;
   12603          954 :   gfc_intrinsic_op op;
   12604          954 :   match m;
   12605              : 
   12606              :   /* Check current state.  */
   12607          954 :   if (gfc_current_state () == COMP_DERIVED)
   12608              :     {
   12609            0 :       gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
   12610            0 :       return MATCH_ERROR;
   12611              :     }
   12612          954 :   if (gfc_current_state () != COMP_DERIVED_CONTAINS)
   12613              :     return MATCH_NO;
   12614          954 :   block = gfc_state_stack->previous->sym;
   12615          954 :   ns = block->f2k_derived;
   12616          954 :   gcc_assert (block && ns);
   12617              : 
   12618          954 :   memset (&tbattr, 0, sizeof (tbattr));
   12619          954 :   tbattr.where = gfc_current_locus;
   12620              : 
   12621              :   /* See if we get an access-specifier.  */
   12622          954 :   m = match_binding_attributes (&tbattr, true, false);
   12623          954 :   if (m == MATCH_ERROR)
   12624            1 :     goto error;
   12625              : 
   12626              :   /* Now the colons, those are required.  */
   12627          953 :   if (gfc_match (" ::") != MATCH_YES)
   12628              :     {
   12629            0 :       gfc_error ("Expected %<::%> at %C");
   12630            0 :       goto error;
   12631              :     }
   12632              : 
   12633              :   /* Match the binding name; depending on type (operator / generic) format
   12634              :      it for future error messages into bind_name.  */
   12635              : 
   12636          953 :   m = gfc_match_generic_spec (&op_type, name, &op);
   12637          953 :   if (m == MATCH_ERROR)
   12638              :     return MATCH_ERROR;
   12639          953 :   if (m == MATCH_NO)
   12640              :     {
   12641            0 :       gfc_error ("Expected generic name or operator descriptor at %C");
   12642            0 :       goto error;
   12643              :     }
   12644              : 
   12645          953 :   switch (op_type)
   12646              :     {
   12647          470 :     case INTERFACE_GENERIC:
   12648          470 :     case INTERFACE_DTIO:
   12649          470 :       snprintf (bind_name, sizeof (bind_name), "%s", name);
   12650          470 :       break;
   12651              : 
   12652           47 :     case INTERFACE_USER_OP:
   12653           47 :       snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
   12654           47 :       break;
   12655              : 
   12656          435 :     case INTERFACE_INTRINSIC_OP:
   12657          435 :       snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
   12658              :                 gfc_op2string (op));
   12659          435 :       break;
   12660              : 
   12661            1 :     case INTERFACE_NAMELESS:
   12662            1 :       gfc_error ("Malformed GENERIC statement at %C");
   12663            1 :       goto error;
   12664            0 :       break;
   12665              : 
   12666            0 :     default:
   12667            0 :       gcc_unreachable ();
   12668              :     }
   12669              : 
   12670              :   /* Match the required =>.  */
   12671          952 :   if (gfc_match (" =>") != MATCH_YES)
   12672              :     {
   12673            0 :       gfc_error ("Expected %<=>%> at %C");
   12674            0 :       goto error;
   12675              :     }
   12676              : 
   12677              :   /* Try to find existing GENERIC binding with this name / for this operator;
   12678              :      if there is something, check that it is another GENERIC and then extend
   12679              :      it rather than building a new node.  Otherwise, create it and put it
   12680              :      at the right position.  */
   12681              : 
   12682          952 :   switch (op_type)
   12683              :     {
   12684          517 :     case INTERFACE_DTIO:
   12685          517 :     case INTERFACE_USER_OP:
   12686          517 :     case INTERFACE_GENERIC:
   12687          517 :       {
   12688          517 :         const bool is_op = (op_type == INTERFACE_USER_OP);
   12689          517 :         gfc_symtree* st;
   12690              : 
   12691          517 :         st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
   12692          517 :         tb = st ? st->n.tb : NULL;
   12693              :         break;
   12694              :       }
   12695              : 
   12696          435 :     case INTERFACE_INTRINSIC_OP:
   12697          435 :       tb = ns->tb_op[op];
   12698          435 :       break;
   12699              : 
   12700            0 :     default:
   12701            0 :       gcc_unreachable ();
   12702              :     }
   12703              : 
   12704          446 :   if (tb)
   12705              :     {
   12706            9 :       if (!tb->is_generic)
   12707              :         {
   12708            1 :           gcc_assert (op_type == INTERFACE_GENERIC);
   12709            1 :           gfc_error ("There's already a non-generic procedure with binding name"
   12710              :                      " %qs for the derived type %qs at %C",
   12711              :                      bind_name, block->name);
   12712            1 :           goto error;
   12713              :         }
   12714              : 
   12715            8 :       if (tb->access != tbattr.access)
   12716              :         {
   12717            2 :           gfc_error ("Binding at %C must have the same access as already"
   12718              :                      " defined binding %qs", bind_name);
   12719            2 :           goto error;
   12720              :         }
   12721              :     }
   12722              :   else
   12723              :     {
   12724          943 :       tb = gfc_get_typebound_proc (NULL);
   12725          943 :       tb->where = gfc_current_locus;
   12726          943 :       tb->access = tbattr.access;
   12727          943 :       tb->is_generic = 1;
   12728          943 :       tb->u.generic = NULL;
   12729              : 
   12730          943 :       switch (op_type)
   12731              :         {
   12732          508 :         case INTERFACE_DTIO:
   12733          508 :         case INTERFACE_GENERIC:
   12734          508 :         case INTERFACE_USER_OP:
   12735          508 :           {
   12736          508 :             const bool is_op = (op_type == INTERFACE_USER_OP);
   12737          508 :             gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
   12738              :                                                    &ns->tb_sym_root, name);
   12739          508 :             gcc_assert (st);
   12740          508 :             st->n.tb = tb;
   12741              : 
   12742          508 :             break;
   12743              :           }
   12744              : 
   12745          435 :         case INTERFACE_INTRINSIC_OP:
   12746          435 :           ns->tb_op[op] = tb;
   12747          435 :           break;
   12748              : 
   12749            0 :         default:
   12750            0 :           gcc_unreachable ();
   12751              :         }
   12752              :     }
   12753              : 
   12754              :   /* Now, match all following names as specific targets.  */
   12755         1106 :   do
   12756              :     {
   12757         1106 :       gfc_symtree* target_st;
   12758         1106 :       gfc_tbp_generic* target;
   12759              : 
   12760         1106 :       m = gfc_match_name (name);
   12761         1106 :       if (m == MATCH_ERROR)
   12762            0 :         goto error;
   12763         1106 :       if (m == MATCH_NO)
   12764              :         {
   12765            1 :           gfc_error ("Expected specific binding name at %C");
   12766            1 :           goto error;
   12767              :         }
   12768              : 
   12769         1105 :       target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
   12770              : 
   12771              :       /* See if this is a duplicate specification.  */
   12772         1340 :       for (target = tb->u.generic; target; target = target->next)
   12773          236 :         if (target_st == target->specific_st)
   12774              :           {
   12775            1 :             gfc_error ("%qs already defined as specific binding for the"
   12776              :                        " generic %qs at %C", name, bind_name);
   12777            1 :             goto error;
   12778              :           }
   12779              : 
   12780         1104 :       target = gfc_get_tbp_generic ();
   12781         1104 :       target->specific_st = target_st;
   12782         1104 :       target->specific = NULL;
   12783         1104 :       target->next = tb->u.generic;
   12784         1104 :       target->is_operator = ((op_type == INTERFACE_USER_OP)
   12785         1104 :                              || (op_type == INTERFACE_INTRINSIC_OP));
   12786         1104 :       tb->u.generic = target;
   12787              :     }
   12788         1104 :   while (gfc_match (" ,") == MATCH_YES);
   12789              : 
   12790              :   /* Here should be the end.  */
   12791          947 :   if (gfc_match_eos () != MATCH_YES)
   12792              :     {
   12793            1 :       gfc_error ("Junk after GENERIC binding at %C");
   12794            1 :       goto error;
   12795              :     }
   12796              : 
   12797              :   return MATCH_YES;
   12798              : 
   12799          954 : error:
   12800              :   return MATCH_ERROR;
   12801              : }
   12802              : 
   12803              : 
   12804              : match
   12805         1054 : gfc_match_generic ()
   12806              : {
   12807         1054 :   if (gfc_option.allow_std & ~GFC_STD_OPT_F08
   12808         1052 :       && gfc_current_state () != COMP_DERIVED_CONTAINS)
   12809          100 :     return match_generic_stmt ();
   12810              :   else
   12811          954 :     return match_typebound_generic ();
   12812              : }
   12813              : 
   12814              : 
   12815              : /* Match a FINAL declaration inside a derived type.  */
   12816              : 
   12817              : match
   12818          484 : gfc_match_final_decl (void)
   12819              : {
   12820          484 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12821          484 :   gfc_symbol* sym;
   12822          484 :   match m;
   12823          484 :   gfc_namespace* module_ns;
   12824          484 :   bool first, last;
   12825          484 :   gfc_symbol* block;
   12826              : 
   12827          484 :   if (gfc_current_form == FORM_FREE)
   12828              :     {
   12829          484 :       char c = gfc_peek_ascii_char ();
   12830          484 :       if (!gfc_is_whitespace (c) && c != ':')
   12831              :         return MATCH_NO;
   12832              :     }
   12833              : 
   12834          483 :   if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
   12835              :     {
   12836            1 :       if (gfc_current_form == FORM_FIXED)
   12837              :         return MATCH_NO;
   12838              : 
   12839            1 :       gfc_error ("FINAL declaration at %C must be inside a derived type "
   12840              :                  "CONTAINS section");
   12841            1 :       return MATCH_ERROR;
   12842              :     }
   12843              : 
   12844          482 :   block = gfc_state_stack->previous->sym;
   12845          482 :   gcc_assert (block);
   12846              : 
   12847          482 :   if (gfc_state_stack->previous->previous
   12848          482 :       && gfc_state_stack->previous->previous->state != COMP_MODULE
   12849            6 :       && gfc_state_stack->previous->previous->state != COMP_SUBMODULE)
   12850              :     {
   12851            0 :       gfc_error ("Derived type declaration with FINAL at %C must be in the"
   12852              :                  " specification part of a MODULE");
   12853            0 :       return MATCH_ERROR;
   12854              :     }
   12855              : 
   12856          482 :   module_ns = gfc_current_ns;
   12857          482 :   gcc_assert (module_ns);
   12858          482 :   gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
   12859              : 
   12860              :   /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
   12861          482 :   if (gfc_match (" ::") == MATCH_ERROR)
   12862              :     return MATCH_ERROR;
   12863              : 
   12864              :   /* Match the sequence of procedure names.  */
   12865              :   first = true;
   12866              :   last = false;
   12867          574 :   do
   12868              :     {
   12869          574 :       gfc_finalizer* f;
   12870              : 
   12871          574 :       if (first && gfc_match_eos () == MATCH_YES)
   12872              :         {
   12873            2 :           gfc_error ("Empty FINAL at %C");
   12874            2 :           return MATCH_ERROR;
   12875              :         }
   12876              : 
   12877          572 :       m = gfc_match_name (name);
   12878          572 :       if (m == MATCH_NO)
   12879              :         {
   12880            1 :           gfc_error ("Expected module procedure name at %C");
   12881            1 :           return MATCH_ERROR;
   12882              :         }
   12883          571 :       else if (m != MATCH_YES)
   12884              :         return MATCH_ERROR;
   12885              : 
   12886          571 :       if (gfc_match_eos () == MATCH_YES)
   12887              :         last = true;
   12888           93 :       if (!last && gfc_match_char (',') != MATCH_YES)
   12889              :         {
   12890            1 :           gfc_error ("Expected %<,%> at %C");
   12891            1 :           return MATCH_ERROR;
   12892              :         }
   12893              : 
   12894          570 :       if (gfc_get_symbol (name, module_ns, &sym))
   12895              :         {
   12896            0 :           gfc_error ("Unknown procedure name %qs at %C", name);
   12897            0 :           return MATCH_ERROR;
   12898              :         }
   12899              : 
   12900              :       /* Mark the symbol as module procedure.  */
   12901          570 :       if (sym->attr.proc != PROC_MODULE
   12902          570 :           && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
   12903              :         return MATCH_ERROR;
   12904              : 
   12905              :       /* Check if we already have this symbol in the list, this is an error.  */
   12906          769 :       for (f = block->f2k_derived->finalizers; f; f = f->next)
   12907          200 :         if (f->proc_sym == sym)
   12908              :           {
   12909            1 :             gfc_error ("%qs at %C is already defined as FINAL procedure",
   12910              :                        name);
   12911            1 :             return MATCH_ERROR;
   12912              :           }
   12913              : 
   12914              :       /* Add this symbol to the list of finalizers.  */
   12915          569 :       gcc_assert (block->f2k_derived);
   12916          569 :       sym->refs++;
   12917          569 :       f = XCNEW (gfc_finalizer);
   12918          569 :       f->proc_sym = sym;
   12919          569 :       f->proc_tree = NULL;
   12920          569 :       f->where = gfc_current_locus;
   12921          569 :       f->next = block->f2k_derived->finalizers;
   12922          569 :       block->f2k_derived->finalizers = f;
   12923              : 
   12924          569 :       first = false;
   12925              :     }
   12926          569 :   while (!last);
   12927              : 
   12928              :   return MATCH_YES;
   12929              : }
   12930              : 
   12931              : 
   12932              : const ext_attr_t ext_attr_list[] = {
   12933              :   { "dllimport",    EXT_ATTR_DLLIMPORT,    "dllimport" },
   12934              :   { "dllexport",    EXT_ATTR_DLLEXPORT,    "dllexport" },
   12935              :   { "cdecl",        EXT_ATTR_CDECL,        "cdecl"     },
   12936              :   { "stdcall",      EXT_ATTR_STDCALL,      "stdcall"   },
   12937              :   { "fastcall",     EXT_ATTR_FASTCALL,     "fastcall"  },
   12938              :   { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL              },
   12939              :   { "deprecated",   EXT_ATTR_DEPRECATED,   NULL              },
   12940              :   { "noinline",     EXT_ATTR_NOINLINE,     NULL              },
   12941              :   { "noreturn",     EXT_ATTR_NORETURN,     NULL              },
   12942              :   { "weak",       EXT_ATTR_WEAK,         NULL        },
   12943              :   { "inline",       EXT_ATTR_INLINE,       NULL              },
   12944              :   { "always_inline",EXT_ATTR_ALWAYS_INLINE,NULL              },
   12945              :   { NULL,           EXT_ATTR_LAST,         NULL        }
   12946              : };
   12947              : 
   12948              : /* Match a !GCC$ ATTRIBUTES statement of the form:
   12949              :       !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
   12950              :    When we come here, we have already matched the !GCC$ ATTRIBUTES string.
   12951              : 
   12952              :    TODO: We should support all GCC attributes using the same syntax for
   12953              :    the attribute list, i.e. the list in C
   12954              :       __attributes(( attribute-list ))
   12955              :    matches then
   12956              :       !GCC$ ATTRIBUTES attribute-list ::
   12957              :    Cf. c-parser.cc's c_parser_attributes; the data can then directly be
   12958              :    saved into a TREE.
   12959              : 
   12960              :    As there is absolutely no risk of confusion, we should never return
   12961              :    MATCH_NO.  */
   12962              : match
   12963         2984 : gfc_match_gcc_attributes (void)
   12964              : {
   12965         2984 :   symbol_attribute attr;
   12966         2984 :   char name[GFC_MAX_SYMBOL_LEN + 1];
   12967         2984 :   unsigned id;
   12968         2984 :   gfc_symbol *sym;
   12969         2984 :   match m;
   12970              : 
   12971         2984 :   gfc_clear_attr (&attr);
   12972         2988 :   for(;;)
   12973              :     {
   12974         2986 :       char ch;
   12975              : 
   12976         2986 :       if (gfc_match_name (name) != MATCH_YES)
   12977              :         return MATCH_ERROR;
   12978              : 
   12979        18042 :       for (id = 0; id < EXT_ATTR_LAST; id++)
   12980        18042 :         if (strcmp (name, ext_attr_list[id].name) == 0)
   12981              :           break;
   12982              : 
   12983         2986 :       if (id == EXT_ATTR_LAST)
   12984              :         {
   12985            0 :           gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
   12986            0 :           return MATCH_ERROR;
   12987              :         }
   12988              : 
   12989         2986 :       if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
   12990              :         return MATCH_ERROR;
   12991              : 
   12992         2986 :       gfc_gobble_whitespace ();
   12993         2986 :       ch = gfc_next_ascii_char ();
   12994         2986 :       if (ch == ':')
   12995              :         {
   12996              :           /* This is the successful exit condition for the loop.  */
   12997         2984 :           if (gfc_next_ascii_char () == ':')
   12998              :             break;
   12999              :         }
   13000              : 
   13001            2 :       if (ch == ',')
   13002            2 :         continue;
   13003              : 
   13004            0 :       goto syntax;
   13005            2 :     }
   13006              : 
   13007         2984 :   if (gfc_match_eos () == MATCH_YES)
   13008            0 :     goto syntax;
   13009              : 
   13010         2999 :   for(;;)
   13011              :     {
   13012         2999 :       m = gfc_match_name (name);
   13013         2999 :       if (m != MATCH_YES)
   13014              :         return m;
   13015              : 
   13016         2999 :       if (find_special (name, &sym, true))
   13017              :         return MATCH_ERROR;
   13018              : 
   13019         2999 :       sym->attr.ext_attr |= attr.ext_attr;
   13020              : 
   13021              :       /* INLINE and ALWAYS_INLINE are incompatible with NOINLINE.  In the
   13022              :          middle-end the DECL_UNINLINABLE flag set by NOINLINE always wins, so
   13023              :          the inline request would be silently ignored.  Warn and drop it.  */
   13024         2999 :       if (sym->attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
   13025              :         {
   13026            5 :           if (sym->attr.ext_attr & (1 << EXT_ATTR_ALWAYS_INLINE))
   13027              :             {
   13028            2 :               gfc_warning (0, "Attribute %<ALWAYS_INLINE%> at %C is "
   13029              :                            "incompatible with %<NOINLINE%> for %qs and will "
   13030              :                            "be ignored", sym->name);
   13031            2 :               sym->attr.ext_attr &= ~(1 << EXT_ATTR_ALWAYS_INLINE);
   13032              :             }
   13033            5 :           if (sym->attr.ext_attr & (1 << EXT_ATTR_INLINE))
   13034              :             {
   13035            2 :               gfc_warning (0, "Attribute %<INLINE%> at %C is incompatible "
   13036              :                            "with %<NOINLINE%> for %qs and will be ignored",
   13037              :                            sym->name);
   13038            2 :               sym->attr.ext_attr &= ~(1 << EXT_ATTR_INLINE);
   13039              :             }
   13040              :         }
   13041              : 
   13042         2999 :       if (gfc_match_eos () == MATCH_YES)
   13043              :         break;
   13044              : 
   13045           15 :       if (gfc_match_char (',') != MATCH_YES)
   13046            0 :         goto syntax;
   13047              :     }
   13048              : 
   13049              :   return MATCH_YES;
   13050              : 
   13051            0 : syntax:
   13052            0 :   gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
   13053            0 :   return MATCH_ERROR;
   13054              : }
   13055              : 
   13056              : 
   13057              : /* Match a !GCC$ UNROLL statement of the form:
   13058              :       !GCC$ UNROLL n
   13059              : 
   13060              :    The parameter n is the number of times we are supposed to unroll.
   13061              : 
   13062              :    When we come here, we have already matched the !GCC$ UNROLL string.  */
   13063              : match
   13064           19 : gfc_match_gcc_unroll (void)
   13065              : {
   13066           19 :   int value;
   13067              : 
   13068              :   /* FIXME: use gfc_match_small_literal_int instead, delete small_int  */
   13069           19 :   if (gfc_match_small_int (&value) == MATCH_YES)
   13070              :     {
   13071           19 :       if (value < 0 || value > USHRT_MAX)
   13072              :         {
   13073            2 :           gfc_error ("%<GCC unroll%> directive requires a"
   13074              :               " non-negative integral constant"
   13075              :               " less than or equal to %u at %C",
   13076              :               USHRT_MAX
   13077              :           );
   13078            2 :           return MATCH_ERROR;
   13079              :         }
   13080           17 :       if (gfc_match_eos () == MATCH_YES)
   13081              :         {
   13082           17 :           directive_unroll = value == 0 ? 1 : value;
   13083           17 :           return MATCH_YES;
   13084              :         }
   13085              :     }
   13086              : 
   13087            0 :   gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
   13088            0 :   return MATCH_ERROR;
   13089              : }
   13090              : 
   13091              : /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
   13092              : 
   13093              :    The parameter b is name of a middle-end built-in.
   13094              :    FLAGS is optional and must be one of:
   13095              :      - (inbranch)
   13096              :      - (notinbranch)
   13097              : 
   13098              :    IF('target') is optional and TARGET is a name of a multilib ABI.
   13099              : 
   13100              :    When we come here, we have already matched the !GCC$ builtin string.  */
   13101              : 
   13102              : match
   13103      3487245 : gfc_match_gcc_builtin (void)
   13104              : {
   13105      3487245 :   char builtin[GFC_MAX_SYMBOL_LEN + 1];
   13106      3487245 :   char target[GFC_MAX_SYMBOL_LEN + 1];
   13107              : 
   13108      3487245 :   if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
   13109              :     return MATCH_ERROR;
   13110              : 
   13111      3487245 :   gfc_simd_clause clause = SIMD_NONE;
   13112      3487245 :   if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
   13113              :     clause = SIMD_NOTINBRANCH;
   13114           21 :   else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
   13115           15 :     clause = SIMD_INBRANCH;
   13116              : 
   13117      3487245 :   if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
   13118              :     {
   13119      3487215 :       if (strcmp (target, "fastmath") == 0)
   13120              :         {
   13121            0 :           if (!fast_math_flags_set_p (&global_options))
   13122              :             return MATCH_YES;
   13123              :         }
   13124              :       else
   13125              :         {
   13126      3487215 :           const char *abi = targetm.get_multilib_abi_name ();
   13127      3487215 :           if (abi == NULL || strcmp (abi, target) != 0)
   13128              :             return MATCH_YES;
   13129              :         }
   13130              :     }
   13131              : 
   13132      1721552 :   if (gfc_vectorized_builtins == NULL)
   13133        31886 :     gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
   13134              : 
   13135      1721552 :   char *r = XNEWVEC (char, strlen (builtin) + 32);
   13136      1721552 :   sprintf (r, "__builtin_%s", builtin);
   13137              : 
   13138      1721552 :   bool existed;
   13139      1721552 :   int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
   13140      1721552 :   value |= clause;
   13141      1721552 :   if (existed)
   13142           23 :     free (r);
   13143              : 
   13144              :   return MATCH_YES;
   13145              : }
   13146              : 
   13147              : /* Match an !GCC$ IVDEP statement.
   13148              :    When we come here, we have already matched the !GCC$ IVDEP string.  */
   13149              : 
   13150              : match
   13151            3 : gfc_match_gcc_ivdep (void)
   13152              : {
   13153            3 :   if (gfc_match_eos () == MATCH_YES)
   13154              :     {
   13155            3 :       directive_ivdep = true;
   13156            3 :       return MATCH_YES;
   13157              :     }
   13158              : 
   13159            0 :   gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
   13160            0 :   return MATCH_ERROR;
   13161              : }
   13162              : 
   13163              : /* Match an !GCC$ VECTOR statement.
   13164              :    When we come here, we have already matched the !GCC$ VECTOR string.  */
   13165              : 
   13166              : match
   13167            3 : gfc_match_gcc_vector (void)
   13168              : {
   13169            3 :   if (gfc_match_eos () == MATCH_YES)
   13170              :     {
   13171            3 :       directive_vector = true;
   13172            3 :       directive_novector = false;
   13173            3 :       return MATCH_YES;
   13174              :     }
   13175              : 
   13176            0 :   gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
   13177            0 :   return MATCH_ERROR;
   13178              : }
   13179              : 
   13180              : /* Match an !GCC$ NOVECTOR statement.
   13181              :    When we come here, we have already matched the !GCC$ NOVECTOR string.  */
   13182              : 
   13183              : match
   13184            3 : gfc_match_gcc_novector (void)
   13185              : {
   13186            3 :   if (gfc_match_eos () == MATCH_YES)
   13187              :     {
   13188            3 :       directive_novector = true;
   13189            3 :       directive_vector = false;
   13190            3 :       return MATCH_YES;
   13191              :     }
   13192              : 
   13193            0 :   gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
   13194            0 :   return MATCH_ERROR;
   13195              : }
        

Generated by: LCOV version 2.4-beta

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