xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/fortran/decl.c (revision 4ac76180e904e771b9d522c7e57296d371f06499)
1 /* Declaration statement matcher
2    Copyright (C) 2002-2020 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 
33 /* Macros to access allocate memory for gfc_data_variable,
34    gfc_data_value and gfc_data.  */
35 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
36 #define gfc_get_data_value() XCNEW (gfc_data_value)
37 #define gfc_get_data() XCNEW (gfc_data)
38 
39 
40 static bool set_binding_label (const char **, const char *, int);
41 
42 
43 /* This flag is set if an old-style length selector is matched
44    during a type-declaration statement.  */
45 
46 static int old_char_selector;
47 
48 /* When variables acquire types and attributes from a declaration
49    statement, they get them from the following static variables.  The
50    first part of a declaration sets these variables and the second
51    part copies these into symbol structures.  */
52 
53 static gfc_typespec current_ts;
54 
55 static symbol_attribute current_attr;
56 static gfc_array_spec *current_as;
57 static int colon_seen;
58 static int attr_seen;
59 
60 /* The current binding label (if any).  */
61 static const char* curr_binding_label;
62 /* Need to know how many identifiers are on the current data declaration
63    line in case we're given the BIND(C) attribute with a NAME= specifier.  */
64 static int num_idents_on_line;
65 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
66    can supply a name if the curr_binding_label is nil and NAME= was not.  */
67 static int has_name_equals = 0;
68 
69 /* Initializer of the previous enumerator.  */
70 
71 static gfc_expr *last_initializer;
72 
73 /* History of all the enumerators is maintained, so that
74    kind values of all the enumerators could be updated depending
75    upon the maximum initialized value.  */
76 
77 typedef struct enumerator_history
78 {
79   gfc_symbol *sym;
80   gfc_expr *initializer;
81   struct enumerator_history *next;
82 }
83 enumerator_history;
84 
85 /* Header of enum history chain.  */
86 
87 static enumerator_history *enum_history = NULL;
88 
89 /* Pointer of enum history node containing largest initializer.  */
90 
91 static enumerator_history *max_enum = NULL;
92 
93 /* gfc_new_block points to the symbol of a newly matched block.  */
94 
95 gfc_symbol *gfc_new_block;
96 
97 bool gfc_matching_function;
98 
99 /* Set upon parsing a !GCC$ unroll n directive for use in the next loop.  */
100 int directive_unroll = -1;
101 
102 /* Set upon parsing supported !GCC$ pragmas for use in the next loop.  */
103 bool directive_ivdep = false;
104 bool directive_vector = false;
105 bool directive_novector = false;
106 
107 /* Map of middle-end built-ins that should be vectorized.  */
108 hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
109 
110 /* If a kind expression of a component of a parameterized derived type is
111    parameterized, temporarily store the expression here.  */
112 static gfc_expr *saved_kind_expr = NULL;
113 
114 /* Used to store the parameter list arising in a PDT declaration and
115    in the typespec of a PDT variable or component.  */
116 static gfc_actual_arglist *decl_type_param_list;
117 static gfc_actual_arglist *type_param_spec_list;
118 
119 /********************* DATA statement subroutines *********************/
120 
121 static bool in_match_data = false;
122 
123 bool
gfc_in_match_data(void)124 gfc_in_match_data (void)
125 {
126   return in_match_data;
127 }
128 
129 static void
set_in_match_data(bool set_value)130 set_in_match_data (bool set_value)
131 {
132   in_match_data = set_value;
133 }
134 
135 /* Free a gfc_data_variable structure and everything beneath it.  */
136 
137 static void
free_variable(gfc_data_variable * p)138 free_variable (gfc_data_variable *p)
139 {
140   gfc_data_variable *q;
141 
142   for (; p; p = q)
143     {
144       q = p->next;
145       gfc_free_expr (p->expr);
146       gfc_free_iterator (&p->iter, 0);
147       free_variable (p->list);
148       free (p);
149     }
150 }
151 
152 
153 /* Free a gfc_data_value structure and everything beneath it.  */
154 
155 static void
free_value(gfc_data_value * p)156 free_value (gfc_data_value *p)
157 {
158   gfc_data_value *q;
159 
160   for (; p; p = q)
161     {
162       q = p->next;
163       mpz_clear (p->repeat);
164       gfc_free_expr (p->expr);
165       free (p);
166     }
167 }
168 
169 
170 /* Free a list of gfc_data structures.  */
171 
172 void
gfc_free_data(gfc_data * p)173 gfc_free_data (gfc_data *p)
174 {
175   gfc_data *q;
176 
177   for (; p; p = q)
178     {
179       q = p->next;
180       free_variable (p->var);
181       free_value (p->value);
182       free (p);
183     }
184 }
185 
186 
187 /* Free all data in a namespace.  */
188 
189 static void
gfc_free_data_all(gfc_namespace * ns)190 gfc_free_data_all (gfc_namespace *ns)
191 {
192   gfc_data *d;
193 
194   for (;ns->data;)
195     {
196       d = ns->data->next;
197       free (ns->data);
198       ns->data = d;
199     }
200 }
201 
202 /* Reject data parsed since the last restore point was marked.  */
203 
204 void
gfc_reject_data(gfc_namespace * ns)205 gfc_reject_data (gfc_namespace *ns)
206 {
207   gfc_data *d;
208 
209   while (ns->data && ns->data != ns->old_data)
210     {
211       d = ns->data->next;
212       free (ns->data);
213       ns->data = d;
214     }
215 }
216 
217 static match var_element (gfc_data_variable *);
218 
219 /* Match a list of variables terminated by an iterator and a right
220    parenthesis.  */
221 
222 static match
var_list(gfc_data_variable * parent)223 var_list (gfc_data_variable *parent)
224 {
225   gfc_data_variable *tail, var;
226   match m;
227 
228   m = var_element (&var);
229   if (m == MATCH_ERROR)
230     return MATCH_ERROR;
231   if (m == MATCH_NO)
232     goto syntax;
233 
234   tail = gfc_get_data_variable ();
235   *tail = var;
236 
237   parent->list = tail;
238 
239   for (;;)
240     {
241       if (gfc_match_char (',') != MATCH_YES)
242 	goto syntax;
243 
244       m = gfc_match_iterator (&parent->iter, 1);
245       if (m == MATCH_YES)
246 	break;
247       if (m == MATCH_ERROR)
248 	return MATCH_ERROR;
249 
250       m = var_element (&var);
251       if (m == MATCH_ERROR)
252 	return MATCH_ERROR;
253       if (m == MATCH_NO)
254 	goto syntax;
255 
256       tail->next = gfc_get_data_variable ();
257       tail = tail->next;
258 
259       *tail = var;
260     }
261 
262   if (gfc_match_char (')') != MATCH_YES)
263     goto syntax;
264   return MATCH_YES;
265 
266 syntax:
267   gfc_syntax_error (ST_DATA);
268   return MATCH_ERROR;
269 }
270 
271 
272 /* Match a single element in a data variable list, which can be a
273    variable-iterator list.  */
274 
275 static match
var_element(gfc_data_variable * new_var)276 var_element (gfc_data_variable *new_var)
277 {
278   match m;
279   gfc_symbol *sym;
280 
281   memset (new_var, 0, sizeof (gfc_data_variable));
282 
283   if (gfc_match_char ('(') == MATCH_YES)
284     return var_list (new_var);
285 
286   m = gfc_match_variable (&new_var->expr, 0);
287   if (m != MATCH_YES)
288     return m;
289 
290   if (new_var->expr->expr_type == EXPR_CONSTANT
291       && new_var->expr->symtree == NULL)
292     {
293       gfc_error ("Inquiry parameter cannot appear in a "
294 		 "data-stmt-object-list at %C");
295       return MATCH_ERROR;
296     }
297 
298   sym = new_var->expr->symtree->n.sym;
299 
300   /* Symbol should already have an associated type.  */
301   if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
302     return MATCH_ERROR;
303 
304   if (!sym->attr.function && gfc_current_ns->parent
305       && gfc_current_ns->parent == sym->ns)
306     {
307       gfc_error ("Host associated variable %qs may not be in the DATA "
308 		 "statement at %C", sym->name);
309       return MATCH_ERROR;
310     }
311 
312   if (gfc_current_state () != COMP_BLOCK_DATA
313       && sym->attr.in_common
314       && !gfc_notify_std (GFC_STD_GNU, "initialization of "
315 			  "common block variable %qs in DATA statement at %C",
316 			  sym->name))
317     return MATCH_ERROR;
318 
319   if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
320     return MATCH_ERROR;
321 
322   return MATCH_YES;
323 }
324 
325 
326 /* Match the top-level list of data variables.  */
327 
328 static match
top_var_list(gfc_data * d)329 top_var_list (gfc_data *d)
330 {
331   gfc_data_variable var, *tail, *new_var;
332   match m;
333 
334   tail = NULL;
335 
336   for (;;)
337     {
338       m = var_element (&var);
339       if (m == MATCH_NO)
340 	goto syntax;
341       if (m == MATCH_ERROR)
342 	return MATCH_ERROR;
343 
344       new_var = gfc_get_data_variable ();
345       *new_var = var;
346       if (new_var->expr)
347 	new_var->expr->where = gfc_current_locus;
348 
349       if (tail == NULL)
350 	d->var = new_var;
351       else
352 	tail->next = new_var;
353 
354       tail = new_var;
355 
356       if (gfc_match_char ('/') == MATCH_YES)
357 	break;
358       if (gfc_match_char (',') != MATCH_YES)
359 	goto syntax;
360     }
361 
362   return MATCH_YES;
363 
364 syntax:
365   gfc_syntax_error (ST_DATA);
366   gfc_free_data_all (gfc_current_ns);
367   return MATCH_ERROR;
368 }
369 
370 
371 static match
match_data_constant(gfc_expr ** result)372 match_data_constant (gfc_expr **result)
373 {
374   char name[GFC_MAX_SYMBOL_LEN + 1];
375   gfc_symbol *sym, *dt_sym = NULL;
376   gfc_expr *expr;
377   match m;
378   locus old_loc;
379 
380   m = gfc_match_literal_constant (&expr, 1);
381   if (m == MATCH_YES)
382     {
383       *result = expr;
384       return MATCH_YES;
385     }
386 
387   if (m == MATCH_ERROR)
388     return MATCH_ERROR;
389 
390   m = gfc_match_null (result);
391   if (m != MATCH_NO)
392     return m;
393 
394   old_loc = gfc_current_locus;
395 
396   /* Should this be a structure component, try to match it
397      before matching a name.  */
398   m = gfc_match_rvalue (result);
399   if (m == MATCH_ERROR)
400     return m;
401 
402   if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
403     {
404       if (!gfc_simplify_expr (*result, 0))
405 	m = MATCH_ERROR;
406       return m;
407     }
408   else if (m == MATCH_YES)
409     {
410       /* If a parameter inquiry ends up here, symtree is NULL but **result
411 	 contains the right constant expression.  Check here.  */
412       if ((*result)->symtree == NULL
413 	  && (*result)->expr_type == EXPR_CONSTANT
414 	  && ((*result)->ts.type == BT_INTEGER
415 	      || (*result)->ts.type == BT_REAL))
416 	return m;
417 
418       /* F2018:R845 data-stmt-constant is initial-data-target.
419 	 A data-stmt-constant shall be ... initial-data-target if and
420 	 only if the corresponding data-stmt-object has the POINTER
421 	 attribute. ...  If data-stmt-constant is initial-data-target
422 	 the corresponding data statement object shall be
423 	 data-pointer-initialization compatible (7.5.4.6) with the initial
424 	 data target; the data statement object is initially associated
425 	 with the target.  */
426       if ((*result)->symtree->n.sym->attr.save
427 	  && (*result)->symtree->n.sym->attr.target)
428 	return m;
429       gfc_free_expr (*result);
430     }
431 
432   gfc_current_locus = old_loc;
433 
434   m = gfc_match_name (name);
435   if (m != MATCH_YES)
436     return m;
437 
438   if (gfc_find_symbol (name, NULL, 1, &sym))
439     return MATCH_ERROR;
440 
441   if (sym && sym->attr.generic)
442     dt_sym = gfc_find_dt_in_generic (sym);
443 
444   if (sym == NULL
445       || (sym->attr.flavor != FL_PARAMETER
446 	  && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
447     {
448       gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
449 		 name);
450       *result = NULL;
451       return MATCH_ERROR;
452     }
453   else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
454     return gfc_match_structure_constructor (dt_sym, result);
455 
456   /* Check to see if the value is an initialization array expression.  */
457   if (sym->value->expr_type == EXPR_ARRAY)
458     {
459       gfc_current_locus = old_loc;
460 
461       m = gfc_match_init_expr (result);
462       if (m == MATCH_ERROR)
463 	return m;
464 
465       if (m == MATCH_YES)
466 	{
467 	  if (!gfc_simplify_expr (*result, 0))
468 	    m = MATCH_ERROR;
469 
470 	  if ((*result)->expr_type == EXPR_CONSTANT)
471 	    return m;
472           else
473 	    {
474 	      gfc_error ("Invalid initializer %s in Data statement at %C", name);
475 	      return MATCH_ERROR;
476 	    }
477 	}
478     }
479 
480   *result = gfc_copy_expr (sym->value);
481   return MATCH_YES;
482 }
483 
484 
485 /* Match a list of values in a DATA statement.  The leading '/' has
486    already been seen at this point.  */
487 
488 static match
top_val_list(gfc_data * data)489 top_val_list (gfc_data *data)
490 {
491   gfc_data_value *new_val, *tail;
492   gfc_expr *expr;
493   match m;
494 
495   tail = NULL;
496 
497   for (;;)
498     {
499       m = match_data_constant (&expr);
500       if (m == MATCH_NO)
501 	goto syntax;
502       if (m == MATCH_ERROR)
503 	return MATCH_ERROR;
504 
505       new_val = gfc_get_data_value ();
506       mpz_init (new_val->repeat);
507 
508       if (tail == NULL)
509 	data->value = new_val;
510       else
511 	tail->next = new_val;
512 
513       tail = new_val;
514 
515       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
516 	{
517 	  tail->expr = expr;
518 	  mpz_set_ui (tail->repeat, 1);
519 	}
520       else
521 	{
522 	  mpz_set (tail->repeat, expr->value.integer);
523 	  gfc_free_expr (expr);
524 
525 	  m = match_data_constant (&tail->expr);
526 	  if (m == MATCH_NO)
527 	    goto syntax;
528 	  if (m == MATCH_ERROR)
529 	    return MATCH_ERROR;
530 	}
531 
532       if (gfc_match_char ('/') == MATCH_YES)
533 	break;
534       if (gfc_match_char (',') == MATCH_NO)
535 	goto syntax;
536     }
537 
538   return MATCH_YES;
539 
540 syntax:
541   gfc_syntax_error (ST_DATA);
542   gfc_free_data_all (gfc_current_ns);
543   return MATCH_ERROR;
544 }
545 
546 
547 /* Matches an old style initialization.  */
548 
549 static match
match_old_style_init(const char * name)550 match_old_style_init (const char *name)
551 {
552   match m;
553   gfc_symtree *st;
554   gfc_symbol *sym;
555   gfc_data *newdata, *nd;
556 
557   /* Set up data structure to hold initializers.  */
558   gfc_find_sym_tree (name, NULL, 0, &st);
559   sym = st->n.sym;
560 
561   newdata = gfc_get_data ();
562   newdata->var = gfc_get_data_variable ();
563   newdata->var->expr = gfc_get_variable_expr (st);
564   newdata->var->expr->where = sym->declared_at;
565   newdata->where = gfc_current_locus;
566 
567   /* Match initial value list. This also eats the terminal '/'.  */
568   m = top_val_list (newdata);
569   if (m != MATCH_YES)
570     {
571       free (newdata);
572       return m;
573     }
574 
575   /* Check that a BOZ did not creep into an old-style initialization.  */
576   for (nd = newdata; nd; nd = nd->next)
577     {
578       if (nd->value->expr->ts.type == BT_BOZ
579 	  && gfc_invalid_boz ("BOZ at %L cannot appear in an old-style "
580 			      "initialization", &nd->value->expr->where))
581 	return MATCH_ERROR;
582 
583       if (nd->var->expr->ts.type != BT_INTEGER
584 	  && nd->var->expr->ts.type != BT_REAL
585 	  && nd->value->expr->ts.type == BT_BOZ)
586 	{
587 	  gfc_error ("BOZ literal constant near %L cannot be assigned to "
588 		     "a %qs variable in an old-style initialization",
589 		     &nd->value->expr->where,
590 		     gfc_typename (&nd->value->expr->ts));
591 	  return MATCH_ERROR;
592 	}
593     }
594 
595   if (gfc_pure (NULL))
596     {
597       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
598       free (newdata);
599       return MATCH_ERROR;
600     }
601   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
602 
603   /* Mark the variable as having appeared in a data statement.  */
604   if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
605     {
606       free (newdata);
607       return MATCH_ERROR;
608     }
609 
610   /* Chain in namespace list of DATA initializers.  */
611   newdata->next = gfc_current_ns->data;
612   gfc_current_ns->data = newdata;
613 
614   return m;
615 }
616 
617 
618 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
619    we are matching a DATA statement and are therefore issuing an error
620    if we encounter something unexpected, if not, we're trying to match
621    an old-style initialization expression of the form INTEGER I /2/.  */
622 
623 match
gfc_match_data(void)624 gfc_match_data (void)
625 {
626   gfc_data *new_data;
627   gfc_expr *e;
628   gfc_ref *ref;
629   match m;
630   char c;
631 
632   /* DATA has been matched.  In free form source code, the next character
633      needs to be whitespace or '(' from an implied do-loop.  Check that
634      here.  */
635   c = gfc_peek_ascii_char ();
636   if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
637     return MATCH_NO;
638 
639   /* Before parsing the rest of a DATA statement, check F2008:c1206.  */
640   if ((gfc_current_state () == COMP_FUNCTION
641        || gfc_current_state () == COMP_SUBROUTINE)
642       && gfc_state_stack->previous->state == COMP_INTERFACE)
643     {
644       gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
645       return MATCH_ERROR;
646     }
647 
648   set_in_match_data (true);
649 
650   for (;;)
651     {
652       new_data = gfc_get_data ();
653       new_data->where = gfc_current_locus;
654 
655       m = top_var_list (new_data);
656       if (m != MATCH_YES)
657 	goto cleanup;
658 
659       if (new_data->var->iter.var
660 	  && new_data->var->iter.var->ts.type == BT_INTEGER
661 	  && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
662 	  && new_data->var->list
663 	  && new_data->var->list->expr
664 	  && new_data->var->list->expr->ts.type == BT_CHARACTER
665 	  && new_data->var->list->expr->ref
666 	  && new_data->var->list->expr->ref->type == REF_SUBSTRING)
667 	{
668 	  gfc_error ("Invalid substring in data-implied-do at %L in DATA "
669 		     "statement", &new_data->var->list->expr->where);
670 	  goto cleanup;
671 	}
672 
673       /* Check for an entity with an allocatable component, which is not
674 	 allowed.  */
675       e = new_data->var->expr;
676       if (e)
677 	{
678 	  bool invalid;
679 
680 	  invalid = false;
681 	  for (ref = e->ref; ref; ref = ref->next)
682 	    if ((ref->type == REF_COMPONENT
683 		 && ref->u.c.component->attr.allocatable)
684 		|| (ref->type == REF_ARRAY
685 		    && e->symtree->n.sym->attr.pointer != 1
686 		    && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
687 	      invalid = true;
688 
689 	  if (invalid)
690 	    {
691 	      gfc_error ("Allocatable component or deferred-shaped array "
692 			 "near %C in DATA statement");
693 	      goto cleanup;
694 	    }
695 
696 	  /* F2008:C567 (R536) A data-i-do-object or a variable that appears
697 	     as a data-stmt-object shall not be an object designator in which
698 	     a pointer appears other than as the entire rightmost part-ref.  */
699 	  if (!e->ref && e->ts.type == BT_DERIVED
700 	      && e->symtree->n.sym->attr.pointer)
701 	    goto partref;
702 
703 	  ref = e->ref;
704 	  if (e->symtree->n.sym->ts.type == BT_DERIVED
705 	      && e->symtree->n.sym->attr.pointer
706 	      && ref->type == REF_COMPONENT)
707 	    goto partref;
708 
709 	  for (; ref; ref = ref->next)
710 	    if (ref->type == REF_COMPONENT
711 		&& ref->u.c.component->attr.pointer
712 		&& ref->next)
713 	      goto partref;
714 	}
715 
716       m = top_val_list (new_data);
717       if (m != MATCH_YES)
718 	goto cleanup;
719 
720       new_data->next = gfc_current_ns->data;
721       gfc_current_ns->data = new_data;
722 
723       /* A BOZ literal constant cannot appear in a structure constructor.
724 	 Check for that here for a data statement value.  */
725       if (new_data->value->expr->ts.type == BT_DERIVED
726 	  && new_data->value->expr->value.constructor)
727 	{
728 	  gfc_constructor *c;
729 	  c = gfc_constructor_first (new_data->value->expr->value.constructor);
730 	  for (; c; c = gfc_constructor_next (c))
731 	    if (c->expr && c->expr->ts.type == BT_BOZ)
732 	      {
733 		gfc_error ("BOZ literal constant at %L cannot appear in a "
734 			   "structure constructor", &c->expr->where);
735 		return MATCH_ERROR;
736 	      }
737 	}
738 
739       if (gfc_match_eos () == MATCH_YES)
740 	break;
741 
742       gfc_match_char (',');	/* Optional comma */
743     }
744 
745   set_in_match_data (false);
746 
747   if (gfc_pure (NULL))
748     {
749       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
750       return MATCH_ERROR;
751     }
752   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
753 
754   return MATCH_YES;
755 
756 partref:
757 
758   gfc_error ("part-ref with pointer attribute near %L is not "
759 	     "rightmost part-ref of data-stmt-object",
760 	     &e->where);
761 
762 cleanup:
763   set_in_match_data (false);
764   gfc_free_data (new_data);
765   return MATCH_ERROR;
766 }
767 
768 
769 /************************ Declaration statements *********************/
770 
771 
772 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
773    list). The difference here is the expression is a list of constants
774    and is surrounded by '/'.
775    The typespec ts must match the typespec of the variable which the
776    clist is initializing.
777    The arrayspec tells whether this should match a list of constants
778    corresponding to array elements or a scalar (as == NULL).  */
779 
780 static match
match_clist_expr(gfc_expr ** result,gfc_typespec * ts,gfc_array_spec * as)781 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
782 {
783   gfc_constructor_base array_head = NULL;
784   gfc_expr *expr = NULL;
785   match m = MATCH_ERROR;
786   locus where;
787   mpz_t repeat, cons_size, as_size;
788   bool scalar;
789   int cmp;
790 
791   gcc_assert (ts);
792 
793   /* We have already matched '/' - now look for a constant list, as with
794      top_val_list from decl.c, but append the result to an array.  */
795   if (gfc_match ("/") == MATCH_YES)
796     {
797       gfc_error ("Empty old style initializer list at %C");
798       return MATCH_ERROR;
799     }
800 
801   where = gfc_current_locus;
802   scalar = !as || !as->rank;
803 
804   if (!scalar && !spec_size (as, &as_size))
805     {
806       gfc_error ("Array in initializer list at %L must have an explicit shape",
807 		 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
808       /* Nothing to cleanup yet.  */
809       return MATCH_ERROR;
810     }
811 
812   mpz_init_set_ui (repeat, 0);
813 
814   for (;;)
815     {
816       m = match_data_constant (&expr);
817       if (m != MATCH_YES)
818         expr = NULL; /* match_data_constant may set expr to garbage */
819       if (m == MATCH_NO)
820         goto syntax;
821       if (m == MATCH_ERROR)
822         goto cleanup;
823 
824       /* Found r in repeat spec r*c; look for the constant to repeat.  */
825       if ( gfc_match_char ('*') == MATCH_YES)
826         {
827           if (scalar)
828             {
829               gfc_error ("Repeat spec invalid in scalar initializer at %C");
830               goto cleanup;
831             }
832           if (expr->ts.type != BT_INTEGER)
833             {
834               gfc_error ("Repeat spec must be an integer at %C");
835               goto cleanup;
836             }
837           mpz_set (repeat, expr->value.integer);
838           gfc_free_expr (expr);
839           expr = NULL;
840 
841           m = match_data_constant (&expr);
842           if (m == MATCH_NO)
843 	    {
844 	      m = MATCH_ERROR;
845 	      gfc_error ("Expected data constant after repeat spec at %C");
846 	    }
847           if (m != MATCH_YES)
848             goto cleanup;
849         }
850       /* No repeat spec, we matched the data constant itself. */
851       else
852         mpz_set_ui (repeat, 1);
853 
854       if (!scalar)
855         {
856           /* Add the constant initializer as many times as repeated. */
857           for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
858             {
859               /* Make sure types of elements match */
860               if(ts && !gfc_compare_types (&expr->ts, ts)
861                     && !gfc_convert_type (expr, ts, 1))
862                 goto cleanup;
863 
864               gfc_constructor_append_expr (&array_head,
865                   gfc_copy_expr (expr), &gfc_current_locus);
866             }
867 
868           gfc_free_expr (expr);
869           expr = NULL;
870         }
871 
872       /* For scalar initializers quit after one element.  */
873       else
874         {
875           if(gfc_match_char ('/') != MATCH_YES)
876             {
877               gfc_error ("End of scalar initializer expected at %C");
878               goto cleanup;
879             }
880           break;
881         }
882 
883       if (gfc_match_char ('/') == MATCH_YES)
884         break;
885       if (gfc_match_char (',') == MATCH_NO)
886         goto syntax;
887     }
888 
889   /* If we break early from here out, we encountered an error.  */
890   m = MATCH_ERROR;
891 
892   /* Set up expr as an array constructor. */
893   if (!scalar)
894     {
895       expr = gfc_get_array_expr (ts->type, ts->kind, &where);
896       expr->ts = *ts;
897       expr->value.constructor = array_head;
898 
899       /* Validate sizes.  We built expr ourselves, so cons_size will be
900 	 constant (we fail above for non-constant expressions).
901 	 We still need to verify that the sizes match.  */
902       gcc_assert (gfc_array_size (expr, &cons_size));
903       cmp = mpz_cmp (cons_size, as_size);
904       if (cmp < 0)
905 	gfc_error ("Not enough elements in array initializer at %C");
906       else if (cmp > 0)
907 	gfc_error ("Too many elements in array initializer at %C");
908       mpz_clear (cons_size);
909       if (cmp)
910 	goto cleanup;
911 
912       /* Set the rank/shape to match the LHS as auto-reshape is implied. */
913       expr->rank = as->rank;
914       expr->shape = gfc_get_shape (as->rank);
915       for (int i = 0; i < as->rank; ++i)
916 	spec_dimen_size (as, i, &expr->shape[i]);
917     }
918 
919   /* Make sure scalar types match. */
920   else if (!gfc_compare_types (&expr->ts, ts)
921            && !gfc_convert_type (expr, ts, 1))
922     goto cleanup;
923 
924   if (expr->ts.u.cl)
925     expr->ts.u.cl->length_from_typespec = 1;
926 
927   *result = expr;
928   m = MATCH_YES;
929   goto done;
930 
931 syntax:
932   m = MATCH_ERROR;
933   gfc_error ("Syntax error in old style initializer list at %C");
934 
935 cleanup:
936   if (expr)
937     expr->value.constructor = NULL;
938   gfc_free_expr (expr);
939   gfc_constructor_free (array_head);
940 
941 done:
942   mpz_clear (repeat);
943   if (!scalar)
944     mpz_clear (as_size);
945   return m;
946 }
947 
948 
949 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs.  */
950 
951 static bool
merge_array_spec(gfc_array_spec * from,gfc_array_spec * to,bool copy)952 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
953 {
954   if ((from->type == AS_ASSUMED_RANK && to->corank)
955       || (to->type == AS_ASSUMED_RANK && from->corank))
956     {
957       gfc_error ("The assumed-rank array at %C shall not have a codimension");
958       return false;
959     }
960 
961   if (to->rank == 0 && from->rank > 0)
962     {
963       to->rank = from->rank;
964       to->type = from->type;
965       to->cray_pointee = from->cray_pointee;
966       to->cp_was_assumed = from->cp_was_assumed;
967 
968       for (int i = to->corank - 1; i >= 0; i--)
969 	{
970 	  /* Do not exceed the limits on lower[] and upper[].  gfortran
971 	     cleans up elsewhere.  */
972 	  int j = from->rank + i;
973 	  if (j >= GFC_MAX_DIMENSIONS)
974 	    break;
975 
976 	  to->lower[j] = to->lower[i];
977 	  to->upper[j] = to->upper[i];
978 	}
979       for (int i = 0; i < from->rank; i++)
980 	{
981 	  if (copy)
982 	    {
983 	      to->lower[i] = gfc_copy_expr (from->lower[i]);
984 	      to->upper[i] = gfc_copy_expr (from->upper[i]);
985 	    }
986 	  else
987 	    {
988 	      to->lower[i] = from->lower[i];
989 	      to->upper[i] = from->upper[i];
990 	    }
991 	}
992     }
993   else if (to->corank == 0 && from->corank > 0)
994     {
995       to->corank = from->corank;
996       to->cotype = from->cotype;
997 
998       for (int i = 0; i < from->corank; i++)
999 	{
1000 	  /* Do not exceed the limits on lower[] and upper[].  gfortran
1001 	     cleans up elsewhere.  */
1002 	  int k = from->rank + i;
1003 	  int j = to->rank + i;
1004 	  if (j >= GFC_MAX_DIMENSIONS)
1005 	    break;
1006 
1007 	  if (copy)
1008 	    {
1009 	      to->lower[j] = gfc_copy_expr (from->lower[k]);
1010 	      to->upper[j] = gfc_copy_expr (from->upper[k]);
1011 	    }
1012 	  else
1013 	    {
1014 	      to->lower[j] = from->lower[k];
1015 	      to->upper[j] = from->upper[k];
1016 	    }
1017 	}
1018     }
1019 
1020   if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
1021     {
1022       gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
1023 		 "allowed dimensions of %d",
1024 		 to->rank, to->corank, GFC_MAX_DIMENSIONS);
1025       to->corank = GFC_MAX_DIMENSIONS - to->rank;
1026       return false;
1027     }
1028   return true;
1029 }
1030 
1031 
1032 /* Match an intent specification.  Since this can only happen after an
1033    INTENT word, a legal intent-spec must follow.  */
1034 
1035 static sym_intent
match_intent_spec(void)1036 match_intent_spec (void)
1037 {
1038 
1039   if (gfc_match (" ( in out )") == MATCH_YES)
1040     return INTENT_INOUT;
1041   if (gfc_match (" ( in )") == MATCH_YES)
1042     return INTENT_IN;
1043   if (gfc_match (" ( out )") == MATCH_YES)
1044     return INTENT_OUT;
1045 
1046   gfc_error ("Bad INTENT specification at %C");
1047   return INTENT_UNKNOWN;
1048 }
1049 
1050 
1051 /* Matches a character length specification, which is either a
1052    specification expression, '*', or ':'.  */
1053 
1054 static match
char_len_param_value(gfc_expr ** expr,bool * deferred)1055 char_len_param_value (gfc_expr **expr, bool *deferred)
1056 {
1057   match m;
1058 
1059   *expr = NULL;
1060   *deferred = false;
1061 
1062   if (gfc_match_char ('*') == MATCH_YES)
1063     return MATCH_YES;
1064 
1065   if (gfc_match_char (':') == MATCH_YES)
1066     {
1067       if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1068 	return MATCH_ERROR;
1069 
1070       *deferred = true;
1071 
1072       return MATCH_YES;
1073     }
1074 
1075   m = gfc_match_expr (expr);
1076 
1077   if (m == MATCH_NO || m == MATCH_ERROR)
1078     return m;
1079 
1080   if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1081     return MATCH_ERROR;
1082 
1083   /* If gfortran gets an EXPR_OP, try to simplifiy it.  This catches things
1084      like CHARACTER(([1])).   */
1085   if ((*expr)->expr_type == EXPR_OP)
1086     gfc_simplify_expr (*expr, 1);
1087 
1088   if ((*expr)->expr_type == EXPR_FUNCTION)
1089     {
1090       if ((*expr)->ts.type == BT_INTEGER
1091 	  || ((*expr)->ts.type == BT_UNKNOWN
1092 	      && strcmp((*expr)->symtree->name, "null") != 0))
1093 	return MATCH_YES;
1094 
1095       goto syntax;
1096     }
1097   else if ((*expr)->expr_type == EXPR_CONSTANT)
1098     {
1099       /* F2008, 4.4.3.1:  The length is a type parameter; its kind is
1100 	 processor dependent and its value is greater than or equal to zero.
1101 	 F2008, 4.4.3.2:  If the character length parameter value evaluates
1102 	 to a negative value, the length of character entities declared
1103 	 is zero.  */
1104 
1105       if ((*expr)->ts.type == BT_INTEGER)
1106 	{
1107 	  if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1108 	    mpz_set_si ((*expr)->value.integer, 0);
1109 	}
1110       else
1111 	goto syntax;
1112     }
1113   else if ((*expr)->expr_type == EXPR_ARRAY)
1114     goto syntax;
1115   else if ((*expr)->expr_type == EXPR_VARIABLE)
1116     {
1117       bool t;
1118       gfc_expr *e;
1119 
1120       e = gfc_copy_expr (*expr);
1121 
1122       /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1123 	 which causes an ICE if gfc_reduce_init_expr() is called.  */
1124       if (e->ref && e->ref->type == REF_ARRAY
1125 	  && e->ref->u.ar.type == AR_UNKNOWN
1126 	  && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1127 	goto syntax;
1128 
1129       t = gfc_reduce_init_expr (e);
1130 
1131       if (!t && e->ts.type == BT_UNKNOWN
1132 	  && e->symtree->n.sym->attr.untyped == 1
1133 	  && (flag_implicit_none
1134 	      || e->symtree->n.sym->ns->seen_implicit_none == 1
1135 	      || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1136 	{
1137 	  gfc_free_expr (e);
1138 	  goto syntax;
1139 	}
1140 
1141       if ((e->ref && e->ref->type == REF_ARRAY
1142 	   && e->ref->u.ar.type != AR_ELEMENT)
1143 	  || (!e->ref && e->expr_type == EXPR_ARRAY))
1144 	{
1145 	  gfc_free_expr (e);
1146 	  goto syntax;
1147 	}
1148 
1149       gfc_free_expr (e);
1150     }
1151 
1152   if (gfc_seen_div0)
1153     m = MATCH_ERROR;
1154 
1155   return m;
1156 
1157 syntax:
1158   gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1159   return MATCH_ERROR;
1160 }
1161 
1162 
1163 /* A character length is a '*' followed by a literal integer or a
1164    char_len_param_value in parenthesis.  */
1165 
1166 static match
match_char_length(gfc_expr ** expr,bool * deferred,bool obsolescent_check)1167 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1168 {
1169   int length;
1170   match m;
1171 
1172   *deferred = false;
1173   m = gfc_match_char ('*');
1174   if (m != MATCH_YES)
1175     return m;
1176 
1177   m = gfc_match_small_literal_int (&length, NULL);
1178   if (m == MATCH_ERROR)
1179     return m;
1180 
1181   if (m == MATCH_YES)
1182     {
1183       if (obsolescent_check
1184 	  && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1185 	return MATCH_ERROR;
1186       *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1187       return m;
1188     }
1189 
1190   if (gfc_match_char ('(') == MATCH_NO)
1191     goto syntax;
1192 
1193   m = char_len_param_value (expr, deferred);
1194   if (m != MATCH_YES && gfc_matching_function)
1195     {
1196       gfc_undo_symbols ();
1197       m = MATCH_YES;
1198     }
1199 
1200   if (m == MATCH_ERROR)
1201     return m;
1202   if (m == MATCH_NO)
1203     goto syntax;
1204 
1205   if (gfc_match_char (')') == MATCH_NO)
1206     {
1207       gfc_free_expr (*expr);
1208       *expr = NULL;
1209       goto syntax;
1210     }
1211 
1212   return MATCH_YES;
1213 
1214 syntax:
1215   gfc_error ("Syntax error in character length specification at %C");
1216   return MATCH_ERROR;
1217 }
1218 
1219 
1220 /* Special subroutine for finding a symbol.  Check if the name is found
1221    in the current name space.  If not, and we're compiling a function or
1222    subroutine and the parent compilation unit is an interface, then check
1223    to see if the name we've been given is the name of the interface
1224    (located in another namespace).  */
1225 
1226 static int
find_special(const char * name,gfc_symbol ** result,bool allow_subroutine)1227 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1228 {
1229   gfc_state_data *s;
1230   gfc_symtree *st;
1231   int i;
1232 
1233   i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1234   if (i == 0)
1235     {
1236       *result = st ? st->n.sym : NULL;
1237       goto end;
1238     }
1239 
1240   if (gfc_current_state () != COMP_SUBROUTINE
1241       && gfc_current_state () != COMP_FUNCTION)
1242     goto end;
1243 
1244   s = gfc_state_stack->previous;
1245   if (s == NULL)
1246     goto end;
1247 
1248   if (s->state != COMP_INTERFACE)
1249     goto end;
1250   if (s->sym == NULL)
1251     goto end;		  /* Nameless interface.  */
1252 
1253   if (strcmp (name, s->sym->name) == 0)
1254     {
1255       *result = s->sym;
1256       return 0;
1257     }
1258 
1259 end:
1260   return i;
1261 }
1262 
1263 
1264 /* Special subroutine for getting a symbol node associated with a
1265    procedure name, used in SUBROUTINE and FUNCTION statements.  The
1266    symbol is created in the parent using with symtree node in the
1267    child unit pointing to the symbol.  If the current namespace has no
1268    parent, then the symbol is just created in the current unit.  */
1269 
1270 static int
get_proc_name(const char * name,gfc_symbol ** result,bool module_fcn_entry)1271 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1272 {
1273   gfc_symtree *st;
1274   gfc_symbol *sym;
1275   int rc = 0;
1276 
1277   /* Module functions have to be left in their own namespace because
1278      they have potentially (almost certainly!) already been referenced.
1279      In this sense, they are rather like external functions.  This is
1280      fixed up in resolve.c(resolve_entries), where the symbol name-
1281      space is set to point to the master function, so that the fake
1282      result mechanism can work.  */
1283   if (module_fcn_entry)
1284     {
1285       /* Present if entry is declared to be a module procedure.  */
1286       rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1287 
1288       if (*result == NULL)
1289 	rc = gfc_get_symbol (name, NULL, result);
1290       else if (!gfc_get_symbol (name, NULL, &sym) && sym
1291 		 && (*result)->ts.type == BT_UNKNOWN
1292 		 && sym->attr.flavor == FL_UNKNOWN)
1293 	/* Pick up the typespec for the entry, if declared in the function
1294 	   body.  Note that this symbol is FL_UNKNOWN because it will
1295 	   only have appeared in a type declaration.  The local symtree
1296 	   is set to point to the module symbol and a unique symtree
1297 	   to the local version.  This latter ensures a correct clearing
1298 	   of the symbols.  */
1299 	{
1300 	  /* If the ENTRY proceeds its specification, we need to ensure
1301 	     that this does not raise a "has no IMPLICIT type" error.  */
1302 	  if (sym->ts.type == BT_UNKNOWN)
1303 	    sym->attr.untyped = 1;
1304 
1305 	  (*result)->ts = sym->ts;
1306 
1307 	  /* Put the symbol in the procedure namespace so that, should
1308 	     the ENTRY precede its specification, the specification
1309 	     can be applied.  */
1310 	  (*result)->ns = gfc_current_ns;
1311 
1312 	  gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1313 	  st->n.sym = *result;
1314 	  st = gfc_get_unique_symtree (gfc_current_ns);
1315 	  sym->refs++;
1316 	  st->n.sym = sym;
1317 	}
1318     }
1319   else
1320     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1321 
1322   if (rc)
1323     return rc;
1324 
1325   sym = *result;
1326   if (sym->attr.proc == PROC_ST_FUNCTION)
1327     return rc;
1328 
1329   if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1330     {
1331       /* Create a partially populated interface symbol to carry the
1332 	 characteristics of the procedure and the result.  */
1333       sym->tlink = gfc_new_symbol (name, sym->ns);
1334       gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1335       gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1336       if (sym->attr.dimension)
1337 	sym->tlink->as = gfc_copy_array_spec (sym->as);
1338 
1339       /* Ideally, at this point, a copy would be made of the formal
1340 	 arguments and their namespace. However, this does not appear
1341 	 to be necessary, albeit at the expense of not being able to
1342 	 use gfc_compare_interfaces directly.  */
1343 
1344       if (sym->result && sym->result != sym)
1345 	{
1346 	  sym->tlink->result = sym->result;
1347 	  sym->result = NULL;
1348 	}
1349       else if (sym->result)
1350 	{
1351 	  sym->tlink->result = sym->tlink;
1352 	}
1353     }
1354   else if (sym && !sym->gfc_new
1355 	   && gfc_current_state () != COMP_INTERFACE)
1356     {
1357       /* Trap another encompassed procedure with the same name.  All
1358 	 these conditions are necessary to avoid picking up an entry
1359 	 whose name clashes with that of the encompassing procedure;
1360 	 this is handled using gsymbols to register unique, globally
1361 	 accessible names.  */
1362       if (sym->attr.flavor != 0
1363 	  && sym->attr.proc != 0
1364 	  && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1365 	  && sym->attr.if_source != IFSRC_UNKNOWN)
1366 	{
1367 	  gfc_error_now ("Procedure %qs at %C is already defined at %L",
1368 			 name, &sym->declared_at);
1369 	  return true;
1370 	}
1371       if (sym->attr.flavor != 0
1372 	  && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1373 	{
1374 	  gfc_error_now ("Procedure %qs at %C is already defined at %L",
1375 			 name, &sym->declared_at);
1376 	  return true;
1377 	}
1378 
1379       if (sym->attr.external && sym->attr.procedure
1380 	  && gfc_current_state () == COMP_CONTAINS)
1381 	{
1382 	  gfc_error_now ("Contained procedure %qs at %C clashes with "
1383 			 "procedure defined at %L",
1384 			 name, &sym->declared_at);
1385 	  return true;
1386 	}
1387 
1388       /* Trap a procedure with a name the same as interface in the
1389 	 encompassing scope.  */
1390       if (sym->attr.generic != 0
1391 	  && (sym->attr.subroutine || sym->attr.function)
1392 	  && !sym->attr.mod_proc)
1393 	{
1394 	  gfc_error_now ("Name %qs at %C is already defined"
1395 			 " as a generic interface at %L",
1396 			 name, &sym->declared_at);
1397 	  return true;
1398 	}
1399 
1400       /* Trap declarations of attributes in encompassing scope.  The
1401 	 signature for this is that ts.kind is nonzero for no-CLASS
1402 	 entity.  For a CLASS entity, ts.kind is zero.  */
1403       if ((sym->ts.kind != 0 || sym->ts.type == BT_CLASS)
1404 	  && !sym->attr.implicit_type
1405 	  && sym->attr.proc == 0
1406 	  && gfc_current_ns->parent != NULL
1407 	  && sym->attr.access == 0
1408 	  && !module_fcn_entry)
1409 	{
1410 	  gfc_error_now ("Procedure %qs at %C has an explicit interface "
1411 		       "from a previous declaration",  name);
1412 	  return true;
1413 	}
1414     }
1415 
1416   /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1417      subroutine-stmt of a module subprogram or of a nonabstract interface
1418      body that is declared in the scoping unit of a module or submodule.  */
1419   if (sym->attr.external
1420       && (sym->attr.subroutine || sym->attr.function)
1421       && sym->attr.if_source == IFSRC_IFBODY
1422       && !current_attr.module_procedure
1423       && sym->attr.proc == PROC_MODULE
1424       && gfc_state_stack->state == COMP_CONTAINS)
1425     {
1426       gfc_error_now ("Procedure %qs defined in interface body at %L "
1427 		     "clashes with internal procedure defined at %C",
1428 		     name, &sym->declared_at);
1429       return true;
1430     }
1431 
1432   if (sym && !sym->gfc_new
1433       && sym->attr.flavor != FL_UNKNOWN
1434       && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1435       && gfc_state_stack->state == COMP_CONTAINS
1436       && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1437     {
1438       gfc_error_now ("Procedure %qs at %C is already defined at %L",
1439 		     name, &sym->declared_at);
1440       return true;
1441     }
1442 
1443   if (gfc_current_ns->parent == NULL || *result == NULL)
1444     return rc;
1445 
1446   /* Module function entries will already have a symtree in
1447      the current namespace but will need one at module level.  */
1448   if (module_fcn_entry)
1449     {
1450       /* Present if entry is declared to be a module procedure.  */
1451       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1452       if (st == NULL)
1453 	st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1454     }
1455   else
1456     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1457 
1458   st->n.sym = sym;
1459   sym->refs++;
1460 
1461   /* See if the procedure should be a module procedure.  */
1462 
1463   if (((sym->ns->proc_name != NULL
1464 	&& sym->ns->proc_name->attr.flavor == FL_MODULE
1465 	&& sym->attr.proc != PROC_MODULE)
1466        || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1467       && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1468     rc = 2;
1469 
1470   return rc;
1471 }
1472 
1473 
1474 /* Verify that the given symbol representing a parameter is C
1475    interoperable, by checking to see if it was marked as such after
1476    its declaration.  If the given symbol is not interoperable, a
1477    warning is reported, thus removing the need to return the status to
1478    the calling function.  The standard does not require the user use
1479    one of the iso_c_binding named constants to declare an
1480    interoperable parameter, but we can't be sure if the param is C
1481    interop or not if the user doesn't.  For example, integer(4) may be
1482    legal Fortran, but doesn't have meaning in C.  It may interop with
1483    a number of the C types, which causes a problem because the
1484    compiler can't know which one.  This code is almost certainly not
1485    portable, and the user will get what they deserve if the C type
1486    across platforms isn't always interoperable with integer(4).  If
1487    the user had used something like integer(c_int) or integer(c_long),
1488    the compiler could have automatically handled the varying sizes
1489    across platforms.  */
1490 
1491 bool
gfc_verify_c_interop_param(gfc_symbol * sym)1492 gfc_verify_c_interop_param (gfc_symbol *sym)
1493 {
1494   int is_c_interop = 0;
1495   bool retval = true;
1496 
1497   /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1498      Don't repeat the checks here.  */
1499   if (sym->attr.implicit_type)
1500     return true;
1501 
1502   /* For subroutines or functions that are passed to a BIND(C) procedure,
1503      they're interoperable if they're BIND(C) and their params are all
1504      interoperable.  */
1505   if (sym->attr.flavor == FL_PROCEDURE)
1506     {
1507       if (sym->attr.is_bind_c == 0)
1508         {
1509           gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1510 			 "attribute to be C interoperable", sym->name,
1511 			 &(sym->declared_at));
1512           return false;
1513         }
1514       else
1515         {
1516           if (sym->attr.is_c_interop == 1)
1517             /* We've already checked this procedure; don't check it again.  */
1518             return true;
1519           else
1520             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1521                                       sym->common_block);
1522         }
1523     }
1524 
1525   /* See if we've stored a reference to a procedure that owns sym.  */
1526   if (sym->ns != NULL && sym->ns->proc_name != NULL)
1527     {
1528       if (sym->ns->proc_name->attr.is_bind_c == 1)
1529 	{
1530 	  is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1531 
1532 	  if (is_c_interop != 1)
1533 	    {
1534 	      /* Make personalized messages to give better feedback.  */
1535 	      if (sym->ts.type == BT_DERIVED)
1536 		gfc_error ("Variable %qs at %L is a dummy argument to the "
1537 			   "BIND(C) procedure %qs but is not C interoperable "
1538 			   "because derived type %qs is not C interoperable",
1539 			   sym->name, &(sym->declared_at),
1540 			   sym->ns->proc_name->name,
1541 			   sym->ts.u.derived->name);
1542 	      else if (sym->ts.type == BT_CLASS)
1543 		gfc_error ("Variable %qs at %L is a dummy argument to the "
1544 			   "BIND(C) procedure %qs but is not C interoperable "
1545 			   "because it is polymorphic",
1546 			   sym->name, &(sym->declared_at),
1547 			   sym->ns->proc_name->name);
1548 	      else if (warn_c_binding_type)
1549 		gfc_warning (OPT_Wc_binding_type,
1550 			     "Variable %qs at %L is a dummy argument of the "
1551 			     "BIND(C) procedure %qs but may not be C "
1552 			     "interoperable",
1553 			     sym->name, &(sym->declared_at),
1554 			     sym->ns->proc_name->name);
1555 	    }
1556 
1557           /* Character strings are only C interoperable if they have a
1558              length of 1.  */
1559           if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension)
1560 	    {
1561 	      gfc_charlen *cl = sym->ts.u.cl;
1562 	      if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1563                   || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1564 		{
1565 		  gfc_error ("Character argument %qs at %L "
1566 			     "must be length 1 because "
1567 			     "procedure %qs is BIND(C)",
1568 			     sym->name, &sym->declared_at,
1569 			     sym->ns->proc_name->name);
1570 		  retval = false;
1571 		}
1572 	    }
1573 
1574 	  /* We have to make sure that any param to a bind(c) routine does
1575 	     not have the allocatable, pointer, or optional attributes,
1576 	     according to J3/04-007, section 5.1.  */
1577 	  if (sym->attr.allocatable == 1
1578 	      && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1579 				  "ALLOCATABLE attribute in procedure %qs "
1580 				  "with BIND(C)", sym->name,
1581 				  &(sym->declared_at),
1582 				  sym->ns->proc_name->name))
1583 	    retval = false;
1584 
1585 	  if (sym->attr.pointer == 1
1586 	      && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1587 				  "POINTER attribute in procedure %qs "
1588 				  "with BIND(C)", sym->name,
1589 				  &(sym->declared_at),
1590 				  sym->ns->proc_name->name))
1591 	    retval = false;
1592 
1593 	  if (sym->attr.optional == 1 && sym->attr.value)
1594 	    {
1595 	      gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1596 			 "and the VALUE attribute because procedure %qs "
1597 			 "is BIND(C)", sym->name, &(sym->declared_at),
1598 			 sym->ns->proc_name->name);
1599 	      retval = false;
1600 	    }
1601 	  else if (sym->attr.optional == 1
1602 		   && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1603 				       "at %L with OPTIONAL attribute in "
1604 				       "procedure %qs which is BIND(C)",
1605 				       sym->name, &(sym->declared_at),
1606 				       sym->ns->proc_name->name))
1607 	    retval = false;
1608 
1609           /* Make sure that if it has the dimension attribute, that it is
1610 	     either assumed size or explicit shape. Deferred shape is already
1611 	     covered by the pointer/allocatable attribute.  */
1612 	  if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1613 	      && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1614 				  "at %L as dummy argument to the BIND(C) "
1615 				  "procedure %qs at %L", sym->name,
1616 				  &(sym->declared_at),
1617 				  sym->ns->proc_name->name,
1618 				  &(sym->ns->proc_name->declared_at)))
1619 	    retval = false;
1620 	}
1621     }
1622 
1623   return retval;
1624 }
1625 
1626 
1627 
1628 /* Function called by variable_decl() that adds a name to the symbol table.  */
1629 
1630 static bool
build_sym(const char * name,gfc_charlen * cl,bool cl_deferred,gfc_array_spec ** as,locus * var_locus)1631 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1632 	   gfc_array_spec **as, locus *var_locus)
1633 {
1634   symbol_attribute attr;
1635   gfc_symbol *sym;
1636   int upper;
1637   gfc_symtree *st;
1638 
1639   /* Symbols in a submodule are host associated from the parent module or
1640      submodules. Therefore, they can be overridden by declarations in the
1641      submodule scope. Deal with this by attaching the existing symbol to
1642      a new symtree and recycling the old symtree with a new symbol...  */
1643   st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1644   if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1645       && st->n.sym != NULL
1646       && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1647     {
1648       gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1649       s->n.sym = st->n.sym;
1650       sym = gfc_new_symbol (name, gfc_current_ns);
1651 
1652 
1653       st->n.sym = sym;
1654       sym->refs++;
1655       gfc_set_sym_referenced (sym);
1656     }
1657   /* ...Otherwise generate a new symtree and new symbol.  */
1658   else if (gfc_get_symbol (name, NULL, &sym))
1659     return false;
1660 
1661   /* Check if the name has already been defined as a type.  The
1662      first letter of the symtree will be in upper case then.  Of
1663      course, this is only necessary if the upper case letter is
1664      actually different.  */
1665 
1666   upper = TOUPPER(name[0]);
1667   if (upper != name[0])
1668     {
1669       char u_name[GFC_MAX_SYMBOL_LEN + 1];
1670       gfc_symtree *st;
1671 
1672       gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1673       strcpy (u_name, name);
1674       u_name[0] = upper;
1675 
1676       st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1677 
1678       /* STRUCTURE types can alias symbol names */
1679       if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1680 	{
1681 	  gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1682 		     &st->n.sym->declared_at);
1683 	  return false;
1684 	}
1685     }
1686 
1687   /* Start updating the symbol table.  Add basic type attribute if present.  */
1688   if (current_ts.type != BT_UNKNOWN
1689       && (sym->attr.implicit_type == 0
1690 	  || !gfc_compare_types (&sym->ts, &current_ts))
1691       && !gfc_add_type (sym, &current_ts, var_locus))
1692     return false;
1693 
1694   if (sym->ts.type == BT_CHARACTER)
1695     {
1696       sym->ts.u.cl = cl;
1697       sym->ts.deferred = cl_deferred;
1698     }
1699 
1700   /* Add dimension attribute if present.  */
1701   if (!gfc_set_array_spec (sym, *as, var_locus))
1702     return false;
1703   *as = NULL;
1704 
1705   /* Add attribute to symbol.  The copy is so that we can reset the
1706      dimension attribute.  */
1707   attr = current_attr;
1708   attr.dimension = 0;
1709   attr.codimension = 0;
1710 
1711   if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1712     return false;
1713 
1714   /* Finish any work that may need to be done for the binding label,
1715      if it's a bind(c).  The bind(c) attr is found before the symbol
1716      is made, and before the symbol name (for data decls), so the
1717      current_ts is holding the binding label, or nothing if the
1718      name= attr wasn't given.  Therefore, test here if we're dealing
1719      with a bind(c) and make sure the binding label is set correctly.  */
1720   if (sym->attr.is_bind_c == 1)
1721     {
1722       if (!sym->binding_label)
1723         {
1724 	  /* Set the binding label and verify that if a NAME= was specified
1725 	     then only one identifier was in the entity-decl-list.  */
1726 	  if (!set_binding_label (&sym->binding_label, sym->name,
1727 				  num_idents_on_line))
1728             return false;
1729         }
1730     }
1731 
1732   /* See if we know we're in a common block, and if it's a bind(c)
1733      common then we need to make sure we're an interoperable type.  */
1734   if (sym->attr.in_common == 1)
1735     {
1736       /* Test the common block object.  */
1737       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1738           && sym->ts.is_c_interop != 1)
1739         {
1740           gfc_error_now ("Variable %qs in common block %qs at %C "
1741                          "must be declared with a C interoperable "
1742                          "kind since common block %qs is BIND(C)",
1743                          sym->name, sym->common_block->name,
1744                          sym->common_block->name);
1745           gfc_clear_error ();
1746         }
1747     }
1748 
1749   sym->attr.implied_index = 0;
1750 
1751   /* Use the parameter expressions for a parameterized derived type.  */
1752   if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1753       && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1754     sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1755 
1756   if (sym->ts.type == BT_CLASS)
1757     return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1758 
1759   return true;
1760 }
1761 
1762 
1763 /* Set character constant to the given length. The constant will be padded or
1764    truncated.  If we're inside an array constructor without a typespec, we
1765    additionally check that all elements have the same length; check_len -1
1766    means no checking.  */
1767 
1768 void
gfc_set_constant_character_len(gfc_charlen_t len,gfc_expr * expr,gfc_charlen_t check_len)1769 gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1770 				gfc_charlen_t check_len)
1771 {
1772   gfc_char_t *s;
1773   gfc_charlen_t slen;
1774 
1775   if (expr->ts.type != BT_CHARACTER)
1776     return;
1777 
1778   if (expr->expr_type != EXPR_CONSTANT)
1779     {
1780       gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1781       return;
1782     }
1783 
1784   slen = expr->value.character.length;
1785   if (len != slen)
1786     {
1787       s = gfc_get_wide_string (len + 1);
1788       memcpy (s, expr->value.character.string,
1789 	      MIN (len, slen) * sizeof (gfc_char_t));
1790       if (len > slen)
1791 	gfc_wide_memset (&s[slen], ' ', len - slen);
1792 
1793       if (warn_character_truncation && slen > len)
1794 	gfc_warning_now (OPT_Wcharacter_truncation,
1795 			 "CHARACTER expression at %L is being truncated "
1796 			 "(%ld/%ld)", &expr->where,
1797 			 (long) slen, (long) len);
1798 
1799       /* Apply the standard by 'hand' otherwise it gets cleared for
1800 	 initializers.  */
1801       if (check_len != -1 && slen != check_len
1802           && !(gfc_option.allow_std & GFC_STD_GNU))
1803 	gfc_error_now ("The CHARACTER elements of the array constructor "
1804 		       "at %L must have the same length (%ld/%ld)",
1805 		       &expr->where, (long) slen,
1806 		       (long) check_len);
1807 
1808       s[len] = '\0';
1809       free (expr->value.character.string);
1810       expr->value.character.string = s;
1811       expr->value.character.length = len;
1812       /* If explicit representation was given, clear it
1813 	 as it is no longer needed after padding.  */
1814       if (expr->representation.length)
1815 	{
1816 	  expr->representation.length = 0;
1817 	  free (expr->representation.string);
1818 	  expr->representation.string = NULL;
1819 	}
1820     }
1821 }
1822 
1823 
1824 /* Function to create and update the enumerator history
1825    using the information passed as arguments.
1826    Pointer "max_enum" is also updated, to point to
1827    enum history node containing largest initializer.
1828 
1829    SYM points to the symbol node of enumerator.
1830    INIT points to its enumerator value.  */
1831 
1832 static void
create_enum_history(gfc_symbol * sym,gfc_expr * init)1833 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1834 {
1835   enumerator_history *new_enum_history;
1836   gcc_assert (sym != NULL && init != NULL);
1837 
1838   new_enum_history = XCNEW (enumerator_history);
1839 
1840   new_enum_history->sym = sym;
1841   new_enum_history->initializer = init;
1842   new_enum_history->next = NULL;
1843 
1844   if (enum_history == NULL)
1845     {
1846       enum_history = new_enum_history;
1847       max_enum = enum_history;
1848     }
1849   else
1850     {
1851       new_enum_history->next = enum_history;
1852       enum_history = new_enum_history;
1853 
1854       if (mpz_cmp (max_enum->initializer->value.integer,
1855 		   new_enum_history->initializer->value.integer) < 0)
1856 	max_enum = new_enum_history;
1857     }
1858 }
1859 
1860 
1861 /* Function to free enum kind history.  */
1862 
1863 void
gfc_free_enum_history(void)1864 gfc_free_enum_history (void)
1865 {
1866   enumerator_history *current = enum_history;
1867   enumerator_history *next;
1868 
1869   while (current != NULL)
1870     {
1871       next = current->next;
1872       free (current);
1873       current = next;
1874     }
1875   max_enum = NULL;
1876   enum_history = NULL;
1877 }
1878 
1879 
1880 /* Function called by variable_decl() that adds an initialization
1881    expression to a symbol.  */
1882 
1883 static bool
add_init_expr_to_sym(const char * name,gfc_expr ** initp,locus * var_locus)1884 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1885 {
1886   symbol_attribute attr;
1887   gfc_symbol *sym;
1888   gfc_expr *init;
1889 
1890   init = *initp;
1891   if (find_special (name, &sym, false))
1892     return false;
1893 
1894   attr = sym->attr;
1895 
1896   /* If this symbol is confirming an implicit parameter type,
1897      then an initialization expression is not allowed.  */
1898   if (attr.flavor == FL_PARAMETER
1899       && sym->value != NULL
1900       && *initp != NULL)
1901     {
1902       gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1903 		 sym->name);
1904       return false;
1905     }
1906 
1907   if (init == NULL)
1908     {
1909       /* An initializer is required for PARAMETER declarations.  */
1910       if (attr.flavor == FL_PARAMETER)
1911 	{
1912 	  gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1913 	  return false;
1914 	}
1915     }
1916   else
1917     {
1918       /* If a variable appears in a DATA block, it cannot have an
1919 	 initializer.  */
1920       if (sym->attr.data)
1921 	{
1922 	  gfc_error ("Variable %qs at %C with an initializer already "
1923 		     "appears in a DATA statement", sym->name);
1924 	  return false;
1925 	}
1926 
1927       /* Check if the assignment can happen. This has to be put off
1928 	 until later for derived type variables and procedure pointers.  */
1929       if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1930 	  && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1931 	  && !sym->attr.proc_pointer
1932 	  && !gfc_check_assign_symbol (sym, NULL, init))
1933 	return false;
1934 
1935       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1936 	    && init->ts.type == BT_CHARACTER)
1937 	{
1938 	  /* Update symbol character length according initializer.  */
1939 	  if (!gfc_check_assign_symbol (sym, NULL, init))
1940 	    return false;
1941 
1942 	  if (sym->ts.u.cl->length == NULL)
1943 	    {
1944 	      gfc_charlen_t clen;
1945 	      /* If there are multiple CHARACTER variables declared on the
1946 		 same line, we don't want them to share the same length.  */
1947 	      sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1948 
1949 	      if (sym->attr.flavor == FL_PARAMETER)
1950 		{
1951 		  if (init->expr_type == EXPR_CONSTANT)
1952 		    {
1953 		      clen = init->value.character.length;
1954 		      sym->ts.u.cl->length
1955 				= gfc_get_int_expr (gfc_charlen_int_kind,
1956 						    NULL, clen);
1957 		    }
1958 		  else if (init->expr_type == EXPR_ARRAY)
1959 		    {
1960 		      if (init->ts.u.cl && init->ts.u.cl->length)
1961 			{
1962 			  const gfc_expr *length = init->ts.u.cl->length;
1963 			  if (length->expr_type != EXPR_CONSTANT)
1964 			    {
1965 			      gfc_error ("Cannot initialize parameter array "
1966 					 "at %L "
1967 					 "with variable length elements",
1968 					 &sym->declared_at);
1969 			      return false;
1970 			    }
1971 			  clen = mpz_get_si (length->value.integer);
1972 			}
1973 		      else if (init->value.constructor)
1974 			{
1975 			  gfc_constructor *c;
1976 	                  c = gfc_constructor_first (init->value.constructor);
1977 	                  clen = c->expr->value.character.length;
1978 			}
1979 		      else
1980 			  gcc_unreachable ();
1981 		      sym->ts.u.cl->length
1982 				= gfc_get_int_expr (gfc_charlen_int_kind,
1983 						    NULL, clen);
1984 		    }
1985 		  else if (init->ts.u.cl && init->ts.u.cl->length)
1986 		    sym->ts.u.cl->length =
1987 				gfc_copy_expr (init->ts.u.cl->length);
1988 		}
1989 	    }
1990 	  /* Update initializer character length according symbol.  */
1991 	  else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1992 	    {
1993 	      if (!gfc_specification_expr (sym->ts.u.cl->length))
1994 		return false;
1995 
1996 	      int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
1997 					 false);
1998 	      /* resolve_charlen will complain later on if the length
1999 		 is too large.  Just skeep the initialization in that case.  */
2000 	      if (mpz_cmp (sym->ts.u.cl->length->value.integer,
2001 			   gfc_integer_kinds[k].huge) <= 0)
2002 		{
2003 		  HOST_WIDE_INT len
2004 		    = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
2005 
2006 		  if (init->expr_type == EXPR_CONSTANT)
2007 		    gfc_set_constant_character_len (len, init, -1);
2008 		  else if (init->expr_type == EXPR_ARRAY)
2009 		    {
2010 		      gfc_constructor *c;
2011 
2012 		      /* Build a new charlen to prevent simplification from
2013 			 deleting the length before it is resolved.  */
2014 		      init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2015 		      init->ts.u.cl->length
2016 			= gfc_copy_expr (sym->ts.u.cl->length);
2017 
2018 		      for (c = gfc_constructor_first (init->value.constructor);
2019 			   c; c = gfc_constructor_next (c))
2020 			gfc_set_constant_character_len (len, c->expr, -1);
2021 		    }
2022 		}
2023 	    }
2024 	}
2025 
2026       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
2027 	  && sym->as->rank && init->rank && init->rank != sym->as->rank)
2028 	{
2029 	  gfc_error ("Rank mismatch of array at %L and its initializer "
2030 		     "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
2031 	  return false;
2032 	}
2033 
2034       /* If sym is implied-shape, set its upper bounds from init.  */
2035       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2036 	  && sym->as->type == AS_IMPLIED_SHAPE)
2037 	{
2038 	  int dim;
2039 
2040 	  if (init->rank == 0)
2041 	    {
2042 	      gfc_error ("Cannot initialize implied-shape array at %L"
2043 			 " with scalar", &sym->declared_at);
2044 	      return false;
2045 	    }
2046 
2047 	  /* The shape may be NULL for EXPR_ARRAY, set it.  */
2048 	  if (init->shape == NULL)
2049 	    {
2050 	      if (init->expr_type != EXPR_ARRAY)
2051 		{
2052 		  gfc_error ("Bad shape of initializer at %L", &init->where);
2053 		  return false;
2054 		}
2055 
2056 	      init->shape = gfc_get_shape (1);
2057 	      if (!gfc_array_size (init, &init->shape[0]))
2058 		{
2059 		  gfc_error ("Cannot determine shape of initializer at %L",
2060 			     &init->where);
2061 		  free (init->shape);
2062 		  init->shape = NULL;
2063 		  return false;
2064 		}
2065 	    }
2066 
2067 	  for (dim = 0; dim < sym->as->rank; ++dim)
2068 	    {
2069 	      int k;
2070 	      gfc_expr *e, *lower;
2071 
2072 	      lower = sym->as->lower[dim];
2073 
2074 	      /* If the lower bound is an array element from another
2075 		 parameterized array, then it is marked with EXPR_VARIABLE and
2076 		 is an initialization expression.  Try to reduce it.  */
2077 	      if (lower->expr_type == EXPR_VARIABLE)
2078 		gfc_reduce_init_expr (lower);
2079 
2080 	      if (lower->expr_type == EXPR_CONSTANT)
2081 		{
2082 		  /* All dimensions must be without upper bound.  */
2083 		  gcc_assert (!sym->as->upper[dim]);
2084 
2085 		  k = lower->ts.kind;
2086 		  e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2087 		  mpz_add (e->value.integer, lower->value.integer,
2088 			   init->shape[dim]);
2089 		  mpz_sub_ui (e->value.integer, e->value.integer, 1);
2090 		  sym->as->upper[dim] = e;
2091 		}
2092 	      else
2093 		{
2094 		  gfc_error ("Non-constant lower bound in implied-shape"
2095 			     " declaration at %L", &lower->where);
2096 		  return false;
2097 		}
2098 	    }
2099 
2100 	  sym->as->type = AS_EXPLICIT;
2101 	}
2102 
2103       /* Ensure that explicit bounds are simplified.  */
2104       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2105 	  && sym->as->type == AS_EXPLICIT)
2106 	{
2107 	  for (int dim = 0; dim < sym->as->rank; ++dim)
2108 	    {
2109 	      gfc_expr *e;
2110 
2111 	      e = sym->as->lower[dim];
2112 	      if (e->expr_type != EXPR_CONSTANT)
2113 		gfc_reduce_init_expr (e);
2114 
2115 	      e = sym->as->upper[dim];
2116 	      if (e->expr_type != EXPR_CONSTANT)
2117 		gfc_reduce_init_expr (e);
2118 	    }
2119 	}
2120 
2121       /* Need to check if the expression we initialized this
2122 	 to was one of the iso_c_binding named constants.  If so,
2123 	 and we're a parameter (constant), let it be iso_c.
2124 	 For example:
2125 	 integer(c_int), parameter :: my_int = c_int
2126 	 integer(my_int) :: my_int_2
2127 	 If we mark my_int as iso_c (since we can see it's value
2128 	 is equal to one of the named constants), then my_int_2
2129 	 will be considered C interoperable.  */
2130       if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2131 	{
2132 	  sym->ts.is_iso_c |= init->ts.is_iso_c;
2133 	  sym->ts.is_c_interop |= init->ts.is_c_interop;
2134 	  /* attr bits needed for module files.  */
2135 	  sym->attr.is_iso_c |= init->ts.is_iso_c;
2136 	  sym->attr.is_c_interop |= init->ts.is_c_interop;
2137 	  if (init->ts.is_iso_c)
2138 	    sym->ts.f90_type = init->ts.f90_type;
2139 	}
2140 
2141       /* Add initializer.  Make sure we keep the ranks sane.  */
2142       if (sym->attr.dimension && init->rank == 0)
2143 	{
2144 	  mpz_t size;
2145 	  gfc_expr *array;
2146 	  int n;
2147 	  if (sym->attr.flavor == FL_PARAMETER
2148 	      && gfc_is_constant_expr (init)
2149 	      && (init->expr_type == EXPR_CONSTANT
2150 		  || init->expr_type == EXPR_STRUCTURE)
2151 	      && spec_size (sym->as, &size)
2152 	      && mpz_cmp_si (size, 0) > 0)
2153 	    {
2154 	      array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2155 					  &init->where);
2156 	      if (init->ts.type == BT_DERIVED)
2157 		array->ts.u.derived = init->ts.u.derived;
2158 	      for (n = 0; n < (int)mpz_get_si (size); n++)
2159 		gfc_constructor_append_expr (&array->value.constructor,
2160 					     n == 0
2161 						? init
2162 						: gfc_copy_expr (init),
2163 					     &init->where);
2164 
2165 	      array->shape = gfc_get_shape (sym->as->rank);
2166 	      for (n = 0; n < sym->as->rank; n++)
2167 		spec_dimen_size (sym->as, n, &array->shape[n]);
2168 
2169 	      init = array;
2170 	      mpz_clear (size);
2171 	    }
2172 	  init->rank = sym->as->rank;
2173 	}
2174 
2175       sym->value = init;
2176       if (sym->attr.save == SAVE_NONE)
2177 	sym->attr.save = SAVE_IMPLICIT;
2178       *initp = NULL;
2179     }
2180 
2181   return true;
2182 }
2183 
2184 
2185 /* Function called by variable_decl() that adds a name to a structure
2186    being built.  */
2187 
2188 static bool
build_struct(const char * name,gfc_charlen * cl,gfc_expr ** init,gfc_array_spec ** as)2189 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2190 	      gfc_array_spec **as)
2191 {
2192   gfc_state_data *s;
2193   gfc_component *c;
2194 
2195   /* F03:C438/C439. If the current symbol is of the same derived type that we're
2196      constructing, it must have the pointer attribute.  */
2197   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2198       && current_ts.u.derived == gfc_current_block ()
2199       && current_attr.pointer == 0)
2200     {
2201       if (current_attr.allocatable
2202 	  && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2203 			     "must have the POINTER attribute"))
2204 	{
2205 	  return false;
2206 	}
2207       else if (current_attr.allocatable == 0)
2208 	{
2209 	  gfc_error ("Component at %C must have the POINTER attribute");
2210 	  return false;
2211 	}
2212     }
2213 
2214   /* F03:C437.  */
2215   if (current_ts.type == BT_CLASS
2216       && !(current_attr.pointer || current_attr.allocatable))
2217     {
2218       gfc_error ("Component %qs with CLASS at %C must be allocatable "
2219                  "or pointer", name);
2220       return false;
2221     }
2222 
2223   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2224     {
2225       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2226 	{
2227 	  gfc_error ("Array component of structure at %C must have explicit "
2228 		     "or deferred shape");
2229 	  return false;
2230 	}
2231     }
2232 
2233   /* If we are in a nested union/map definition, gfc_add_component will not
2234      properly find repeated components because:
2235        (i) gfc_add_component does a flat search, where components of unions
2236            and maps are implicity chained so nested components may conflict.
2237       (ii) Unions and maps are not linked as components of their parent
2238            structures until after they are parsed.
2239      For (i) we use gfc_find_component which searches recursively, and for (ii)
2240      we search each block directly from the parse stack until we find the top
2241      level structure.  */
2242 
2243   s = gfc_state_stack;
2244   if (s->state == COMP_UNION || s->state == COMP_MAP)
2245     {
2246       while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2247         {
2248           c = gfc_find_component (s->sym, name, true, true, NULL);
2249           if (c != NULL)
2250             {
2251               gfc_error_now ("Component %qs at %C already declared at %L",
2252                              name, &c->loc);
2253               return false;
2254             }
2255           /* Break after we've searched the entire chain.  */
2256           if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2257             break;
2258           s = s->previous;
2259         }
2260     }
2261 
2262   if (!gfc_add_component (gfc_current_block(), name, &c))
2263     return false;
2264 
2265   c->ts = current_ts;
2266   if (c->ts.type == BT_CHARACTER)
2267     c->ts.u.cl = cl;
2268 
2269   if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2270       && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2271       && saved_kind_expr != NULL)
2272     c->kind_expr = gfc_copy_expr (saved_kind_expr);
2273 
2274   c->attr = current_attr;
2275 
2276   c->initializer = *init;
2277   *init = NULL;
2278 
2279   c->as = *as;
2280   if (c->as != NULL)
2281     {
2282       if (c->as->corank)
2283 	c->attr.codimension = 1;
2284       if (c->as->rank)
2285 	c->attr.dimension = 1;
2286     }
2287   *as = NULL;
2288 
2289   gfc_apply_init (&c->ts, &c->attr, c->initializer);
2290 
2291   /* Check array components.  */
2292   if (!c->attr.dimension)
2293     goto scalar;
2294 
2295   if (c->attr.pointer)
2296     {
2297       if (c->as->type != AS_DEFERRED)
2298 	{
2299 	  gfc_error ("Pointer array component of structure at %C must have a "
2300 		     "deferred shape");
2301 	  return false;
2302 	}
2303     }
2304   else if (c->attr.allocatable)
2305     {
2306       if (c->as->type != AS_DEFERRED)
2307 	{
2308 	  gfc_error ("Allocatable component of structure at %C must have a "
2309 		     "deferred shape");
2310 	  return false;
2311 	}
2312     }
2313   else
2314     {
2315       if (c->as->type != AS_EXPLICIT)
2316 	{
2317 	  gfc_error ("Array component of structure at %C must have an "
2318 		     "explicit shape");
2319 	  return false;
2320 	}
2321     }
2322 
2323 scalar:
2324   if (c->ts.type == BT_CLASS)
2325     return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2326 
2327   if (c->attr.pdt_kind || c->attr.pdt_len)
2328     {
2329       gfc_symbol *sym;
2330       gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2331 		       0, &sym);
2332       if (sym == NULL)
2333 	{
2334 	  gfc_error ("Type parameter %qs at %C has no corresponding entry "
2335 		     "in the type parameter name list at %L",
2336 		     c->name, &gfc_current_block ()->declared_at);
2337 	  return false;
2338 	}
2339       sym->ts = c->ts;
2340       sym->attr.pdt_kind = c->attr.pdt_kind;
2341       sym->attr.pdt_len = c->attr.pdt_len;
2342       if (c->initializer)
2343 	sym->value = gfc_copy_expr (c->initializer);
2344       sym->attr.flavor = FL_VARIABLE;
2345     }
2346 
2347   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2348       && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2349       && decl_type_param_list)
2350     c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2351 
2352   return true;
2353 }
2354 
2355 
2356 /* Match a 'NULL()', and possibly take care of some side effects.  */
2357 
2358 match
gfc_match_null(gfc_expr ** result)2359 gfc_match_null (gfc_expr **result)
2360 {
2361   gfc_symbol *sym;
2362   match m, m2 = MATCH_NO;
2363 
2364   if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2365     return MATCH_ERROR;
2366 
2367   if (m == MATCH_NO)
2368     {
2369       locus old_loc;
2370       char name[GFC_MAX_SYMBOL_LEN + 1];
2371 
2372       if ((m2 = gfc_match (" null (")) != MATCH_YES)
2373 	return m2;
2374 
2375       old_loc = gfc_current_locus;
2376       if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2377 	return MATCH_ERROR;
2378       if (m2 != MATCH_YES
2379 	  && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2380 	return MATCH_ERROR;
2381       if (m2 == MATCH_NO)
2382 	{
2383 	  gfc_current_locus = old_loc;
2384 	  return MATCH_NO;
2385 	}
2386     }
2387 
2388   /* The NULL symbol now has to be/become an intrinsic function.  */
2389   if (gfc_get_symbol ("null", NULL, &sym))
2390     {
2391       gfc_error ("NULL() initialization at %C is ambiguous");
2392       return MATCH_ERROR;
2393     }
2394 
2395   gfc_intrinsic_symbol (sym);
2396 
2397   if (sym->attr.proc != PROC_INTRINSIC
2398       && !(sym->attr.use_assoc && sym->attr.intrinsic)
2399       && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2400 	  || !gfc_add_function (&sym->attr, sym->name, NULL)))
2401     return MATCH_ERROR;
2402 
2403   *result = gfc_get_null_expr (&gfc_current_locus);
2404 
2405   /* Invalid per F2008, C512.  */
2406   if (m2 == MATCH_YES)
2407     {
2408       gfc_error ("NULL() initialization at %C may not have MOLD");
2409       return MATCH_ERROR;
2410     }
2411 
2412   return MATCH_YES;
2413 }
2414 
2415 
2416 /* Match the initialization expr for a data pointer or procedure pointer.  */
2417 
2418 static match
match_pointer_init(gfc_expr ** init,int procptr)2419 match_pointer_init (gfc_expr **init, int procptr)
2420 {
2421   match m;
2422 
2423   if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2424     {
2425       gfc_error ("Initialization of pointer at %C is not allowed in "
2426 		 "a PURE procedure");
2427       return MATCH_ERROR;
2428     }
2429   gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2430 
2431   /* Match NULL() initialization.  */
2432   m = gfc_match_null (init);
2433   if (m != MATCH_NO)
2434     return m;
2435 
2436   /* Match non-NULL initialization.  */
2437   gfc_matching_ptr_assignment = !procptr;
2438   gfc_matching_procptr_assignment = procptr;
2439   m = gfc_match_rvalue (init);
2440   gfc_matching_ptr_assignment = 0;
2441   gfc_matching_procptr_assignment = 0;
2442   if (m == MATCH_ERROR)
2443     return MATCH_ERROR;
2444   else if (m == MATCH_NO)
2445     {
2446       gfc_error ("Error in pointer initialization at %C");
2447       return MATCH_ERROR;
2448     }
2449 
2450   if (!procptr && !gfc_resolve_expr (*init))
2451     return MATCH_ERROR;
2452 
2453   if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2454 		       "initialization at %C"))
2455     return MATCH_ERROR;
2456 
2457   return MATCH_YES;
2458 }
2459 
2460 
2461 static bool
check_function_name(char * name)2462 check_function_name (char *name)
2463 {
2464   /* In functions that have a RESULT variable defined, the function name always
2465      refers to function calls.  Therefore, the name is not allowed to appear in
2466      specification statements. When checking this, be careful about
2467      'hidden' procedure pointer results ('ppr@').  */
2468 
2469   if (gfc_current_state () == COMP_FUNCTION)
2470     {
2471       gfc_symbol *block = gfc_current_block ();
2472       if (block && block->result && block->result != block
2473 	  && strcmp (block->result->name, "ppr@") != 0
2474 	  && strcmp (block->name, name) == 0)
2475 	{
2476 	  gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2477 		     "from appearing in a specification statement",
2478 		     block->result->name, &block->result->declared_at, name);
2479 	  return false;
2480 	}
2481     }
2482 
2483   return true;
2484 }
2485 
2486 
2487 /* Match a variable name with an optional initializer.  When this
2488    subroutine is called, a variable is expected to be parsed next.
2489    Depending on what is happening at the moment, updates either the
2490    symbol table or the current interface.  */
2491 
2492 static match
variable_decl(int elem)2493 variable_decl (int elem)
2494 {
2495   char name[GFC_MAX_SYMBOL_LEN + 1];
2496   static unsigned int fill_id = 0;
2497   gfc_expr *initializer, *char_len;
2498   gfc_array_spec *as;
2499   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
2500   gfc_charlen *cl;
2501   bool cl_deferred;
2502   locus var_locus;
2503   match m;
2504   bool t;
2505   gfc_symbol *sym;
2506   char c;
2507 
2508   initializer = NULL;
2509   as = NULL;
2510   cp_as = NULL;
2511 
2512   /* When we get here, we've just matched a list of attributes and
2513      maybe a type and a double colon.  The next thing we expect to see
2514      is the name of the symbol.  */
2515 
2516   /* If we are parsing a structure with legacy support, we allow the symbol
2517      name to be '%FILL' which gives it an anonymous (inaccessible) name.  */
2518   m = MATCH_NO;
2519   gfc_gobble_whitespace ();
2520   c = gfc_peek_ascii_char ();
2521   if (c == '%')
2522     {
2523       gfc_next_ascii_char ();	/* Burn % character.  */
2524       m = gfc_match ("fill");
2525       if (m == MATCH_YES)
2526 	{
2527 	  if (gfc_current_state () != COMP_STRUCTURE)
2528 	    {
2529 	      if (flag_dec_structure)
2530 		gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2531 	      else
2532 		gfc_error ("%qs at %C is a DEC extension, enable with "
2533 		       "%<-fdec-structure%>", "%FILL");
2534 	      m = MATCH_ERROR;
2535 	      goto cleanup;
2536 	    }
2537 
2538 	  if (attr_seen)
2539 	    {
2540 	      gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2541 	      m = MATCH_ERROR;
2542 	      goto cleanup;
2543 	    }
2544 
2545 	  /* %FILL components are given invalid fortran names.  */
2546 	  snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2547 	}
2548       else
2549 	{
2550 	  gfc_error ("Invalid character %qc in variable name at %C", c);
2551 	  return MATCH_ERROR;
2552 	}
2553     }
2554   else
2555     {
2556       m = gfc_match_name (name);
2557       if (m != MATCH_YES)
2558 	goto cleanup;
2559     }
2560 
2561   var_locus = gfc_current_locus;
2562 
2563   /* Now we could see the optional array spec. or character length.  */
2564   m = gfc_match_array_spec (&as, true, true);
2565   if (m == MATCH_ERROR)
2566     goto cleanup;
2567 
2568   if (m == MATCH_NO)
2569     as = gfc_copy_array_spec (current_as);
2570   else if (current_as
2571 	   && !merge_array_spec (current_as, as, true))
2572     {
2573       m = MATCH_ERROR;
2574       goto cleanup;
2575     }
2576 
2577   if (flag_cray_pointer)
2578     cp_as = gfc_copy_array_spec (as);
2579 
2580   /* At this point, we know for sure if the symbol is PARAMETER and can thus
2581      determine (and check) whether it can be implied-shape.  If it
2582      was parsed as assumed-size, change it because PARAMETERs cannot
2583      be assumed-size.
2584 
2585      An explicit-shape-array cannot appear under several conditions.
2586      That check is done here as well.  */
2587   if (as)
2588     {
2589       if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2590 	{
2591 	  m = MATCH_ERROR;
2592 	  gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
2593 		     name, &var_locus);
2594 	  goto cleanup;
2595 	}
2596 
2597       if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2598 	  && current_attr.flavor == FL_PARAMETER)
2599 	as->type = AS_IMPLIED_SHAPE;
2600 
2601       if (as->type == AS_IMPLIED_SHAPE
2602 	  && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2603 			      &var_locus))
2604 	{
2605 	  m = MATCH_ERROR;
2606 	  goto cleanup;
2607 	}
2608 
2609       gfc_seen_div0 = false;
2610 
2611       /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2612 	 constant expressions shall appear only in a subprogram, derived
2613 	 type definition, BLOCK construct, or interface body.  */
2614       if (as->type == AS_EXPLICIT
2615 	  && gfc_current_state () != COMP_BLOCK
2616 	  && gfc_current_state () != COMP_DERIVED
2617 	  && gfc_current_state () != COMP_FUNCTION
2618 	  && gfc_current_state () != COMP_INTERFACE
2619 	  && gfc_current_state () != COMP_SUBROUTINE)
2620 	{
2621 	  gfc_expr *e;
2622 	  bool not_constant = false;
2623 
2624 	  for (int i = 0; i < as->rank; i++)
2625 	    {
2626 	      e = gfc_copy_expr (as->lower[i]);
2627 	      if (!gfc_resolve_expr (e) && gfc_seen_div0)
2628 		{
2629 		  m = MATCH_ERROR;
2630 		  goto cleanup;
2631 		}
2632 
2633 	      gfc_simplify_expr (e, 0);
2634 	      if (e && (e->expr_type != EXPR_CONSTANT))
2635 		{
2636 		  not_constant = true;
2637 		  break;
2638 		}
2639 	      gfc_free_expr (e);
2640 
2641 	      e = gfc_copy_expr (as->upper[i]);
2642 	      if (!gfc_resolve_expr (e)  && gfc_seen_div0)
2643 		{
2644 		  m = MATCH_ERROR;
2645 		  goto cleanup;
2646 		}
2647 
2648 	      gfc_simplify_expr (e, 0);
2649 	      if (e && (e->expr_type != EXPR_CONSTANT))
2650 		{
2651 		  not_constant = true;
2652 		  break;
2653 		}
2654 	      gfc_free_expr (e);
2655 	    }
2656 
2657 	  if (not_constant)
2658 	    {
2659 	      gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2660 	      m = MATCH_ERROR;
2661 	      goto cleanup;
2662 	    }
2663 	}
2664       if (as->type == AS_EXPLICIT)
2665 	{
2666 	  for (int i = 0; i < as->rank; i++)
2667 	    {
2668 	      gfc_expr *e, *n;
2669 	      e = as->lower[i];
2670 	      if (e->expr_type != EXPR_CONSTANT)
2671 		{
2672 		  n = gfc_copy_expr (e);
2673 		  if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
2674 		    {
2675 		      m = MATCH_ERROR;
2676 		      goto cleanup;
2677 		    }
2678 
2679 		  if (n->expr_type == EXPR_CONSTANT)
2680 		    gfc_replace_expr (e, n);
2681 		  else
2682 		    gfc_free_expr (n);
2683 		}
2684 	      e = as->upper[i];
2685 	      if (e->expr_type != EXPR_CONSTANT)
2686 		{
2687 		  n = gfc_copy_expr (e);
2688 		  if (!gfc_simplify_expr (n, 1)  && gfc_seen_div0)
2689 		    {
2690 		      m = MATCH_ERROR;
2691 		      goto cleanup;
2692 		    }
2693 
2694 		  if (n->expr_type == EXPR_CONSTANT)
2695 		    gfc_replace_expr (e, n);
2696 		  else
2697 		    gfc_free_expr (n);
2698 		}
2699 	      /* For an explicit-shape spec with constant bounds, ensure
2700 		 that the effective upper bound is not lower than the
2701 		 respective lower bound minus one.  Otherwise adjust it so
2702 		 that the extent is trivially derived to be zero.  */
2703 	      if (as->lower[i]->expr_type == EXPR_CONSTANT
2704 		  && as->upper[i]->expr_type == EXPR_CONSTANT
2705 		  && as->lower[i]->ts.type == BT_INTEGER
2706 		  && as->upper[i]->ts.type == BT_INTEGER
2707 		  && mpz_cmp (as->upper[i]->value.integer,
2708 			      as->lower[i]->value.integer) < 0)
2709 		mpz_sub_ui (as->upper[i]->value.integer,
2710 			    as->lower[i]->value.integer, 1);
2711 	    }
2712 	}
2713     }
2714 
2715   char_len = NULL;
2716   cl = NULL;
2717   cl_deferred = false;
2718 
2719   if (current_ts.type == BT_CHARACTER)
2720     {
2721       switch (match_char_length (&char_len, &cl_deferred, false))
2722 	{
2723 	case MATCH_YES:
2724 	  cl = gfc_new_charlen (gfc_current_ns, NULL);
2725 
2726 	  cl->length = char_len;
2727 	  break;
2728 
2729 	/* Non-constant lengths need to be copied after the first
2730 	   element.  Also copy assumed lengths.  */
2731 	case MATCH_NO:
2732 	  if (elem > 1
2733 	      && (current_ts.u.cl->length == NULL
2734 		  || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2735 	    {
2736 	      cl = gfc_new_charlen (gfc_current_ns, NULL);
2737 	      cl->length = gfc_copy_expr (current_ts.u.cl->length);
2738 	    }
2739 	  else
2740 	    cl = current_ts.u.cl;
2741 
2742 	  cl_deferred = current_ts.deferred;
2743 
2744 	  break;
2745 
2746 	case MATCH_ERROR:
2747 	  goto cleanup;
2748 	}
2749     }
2750 
2751   /* The dummy arguments and result of the abreviated form of MODULE
2752      PROCEDUREs, used in SUBMODULES should not be redefined.  */
2753   if (gfc_current_ns->proc_name
2754       && gfc_current_ns->proc_name->abr_modproc_decl)
2755     {
2756       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2757       if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2758 	{
2759 	  m = MATCH_ERROR;
2760 	  gfc_error ("%qs at %C is a redefinition of the declaration "
2761 		     "in the corresponding interface for MODULE "
2762 		     "PROCEDURE %qs", sym->name,
2763 		     gfc_current_ns->proc_name->name);
2764 	  goto cleanup;
2765 	}
2766     }
2767 
2768   /* %FILL components may not have initializers.  */
2769   if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
2770     {
2771       gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2772       m = MATCH_ERROR;
2773       goto cleanup;
2774     }
2775 
2776   /*  If this symbol has already shown up in a Cray Pointer declaration,
2777       and this is not a component declaration,
2778       then we want to set the type & bail out.  */
2779   if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2780     {
2781       gfc_find_symbol (name, gfc_current_ns, 0, &sym);
2782       if (sym != NULL && sym->attr.cray_pointee)
2783 	{
2784 	  m = MATCH_YES;
2785 	  if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
2786 	    {
2787 	      m = MATCH_ERROR;
2788 	      goto cleanup;
2789 	    }
2790 
2791 	  /* Check to see if we have an array specification.  */
2792 	  if (cp_as != NULL)
2793 	    {
2794 	      if (sym->as != NULL)
2795 		{
2796 		  gfc_error ("Duplicate array spec for Cray pointee at %C");
2797 		  gfc_free_array_spec (cp_as);
2798 		  m = MATCH_ERROR;
2799 		  goto cleanup;
2800 		}
2801 	      else
2802 		{
2803 		  if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2804 		    gfc_internal_error ("Cannot set pointee array spec.");
2805 
2806 		  /* Fix the array spec.  */
2807 		  m = gfc_mod_pointee_as (sym->as);
2808 		  if (m == MATCH_ERROR)
2809 		    goto cleanup;
2810 		}
2811 	    }
2812 	  goto cleanup;
2813 	}
2814       else
2815 	{
2816 	  gfc_free_array_spec (cp_as);
2817 	}
2818     }
2819 
2820   /* Procedure pointer as function result.  */
2821   if (gfc_current_state () == COMP_FUNCTION
2822       && strcmp ("ppr@", gfc_current_block ()->name) == 0
2823       && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2824     strcpy (name, "ppr@");
2825 
2826   if (gfc_current_state () == COMP_FUNCTION
2827       && strcmp (name, gfc_current_block ()->name) == 0
2828       && gfc_current_block ()->result
2829       && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2830     strcpy (name, "ppr@");
2831 
2832   /* OK, we've successfully matched the declaration.  Now put the
2833      symbol in the current namespace, because it might be used in the
2834      optional initialization expression for this symbol, e.g. this is
2835      perfectly legal:
2836 
2837      integer, parameter :: i = huge(i)
2838 
2839      This is only true for parameters or variables of a basic type.
2840      For components of derived types, it is not true, so we don't
2841      create a symbol for those yet.  If we fail to create the symbol,
2842      bail out.  */
2843   if (!gfc_comp_struct (gfc_current_state ())
2844       && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2845     {
2846       m = MATCH_ERROR;
2847       goto cleanup;
2848     }
2849 
2850   if (!check_function_name (name))
2851     {
2852       m = MATCH_ERROR;
2853       goto cleanup;
2854     }
2855 
2856   /* We allow old-style initializations of the form
2857        integer i /2/, j(4) /3*3, 1/
2858      (if no colon has been seen). These are different from data
2859      statements in that initializers are only allowed to apply to the
2860      variable immediately preceding, i.e.
2861        integer i, j /1, 2/
2862      is not allowed. Therefore we have to do some work manually, that
2863      could otherwise be left to the matchers for DATA statements.  */
2864 
2865   if (!colon_seen && gfc_match (" /") == MATCH_YES)
2866     {
2867       if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2868 			   "initialization at %C"))
2869 	return MATCH_ERROR;
2870 
2871       /* Allow old style initializations for components of STRUCTUREs and MAPs
2872          but not components of derived types.  */
2873       else if (gfc_current_state () == COMP_DERIVED)
2874 	{
2875 	  gfc_error ("Invalid old style initialization for derived type "
2876 		     "component at %C");
2877 	  m = MATCH_ERROR;
2878 	  goto cleanup;
2879 	}
2880 
2881       /* For structure components, read the initializer as a special
2882          expression and let the rest of this function apply the initializer
2883          as usual.  */
2884       else if (gfc_comp_struct (gfc_current_state ()))
2885         {
2886           m = match_clist_expr (&initializer, &current_ts, as);
2887           if (m == MATCH_NO)
2888             gfc_error ("Syntax error in old style initialization of %s at %C",
2889                        name);
2890           if (m != MATCH_YES)
2891             goto cleanup;
2892         }
2893 
2894       /* Otherwise we treat the old style initialization just like a
2895          DATA declaration for the current variable.  */
2896       else
2897         return match_old_style_init (name);
2898     }
2899 
2900   /* The double colon must be present in order to have initializers.
2901      Otherwise the statement is ambiguous with an assignment statement.  */
2902   if (colon_seen)
2903     {
2904       if (gfc_match (" =>") == MATCH_YES)
2905 	{
2906 	  if (!current_attr.pointer)
2907 	    {
2908 	      gfc_error ("Initialization at %C isn't for a pointer variable");
2909 	      m = MATCH_ERROR;
2910 	      goto cleanup;
2911 	    }
2912 
2913 	  m = match_pointer_init (&initializer, 0);
2914 	  if (m != MATCH_YES)
2915 	    goto cleanup;
2916 
2917 	  /* The target of a pointer initialization must have the SAVE
2918 	     attribute.  A variable in PROGRAM, MODULE, or SUBMODULE scope
2919 	     is implicit SAVEd.  Explicitly, set the SAVE_IMPLICIT value.  */
2920 	  if (initializer->expr_type == EXPR_VARIABLE
2921 	      && initializer->symtree->n.sym->attr.save == SAVE_NONE
2922 	      && (gfc_current_state () == COMP_PROGRAM
2923 		  || gfc_current_state () == COMP_MODULE
2924 		  || gfc_current_state () == COMP_SUBMODULE))
2925 	    initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
2926 	}
2927       else if (gfc_match_char ('=') == MATCH_YES)
2928 	{
2929 	  if (current_attr.pointer)
2930 	    {
2931 	      gfc_error ("Pointer initialization at %C requires %<=>%>, "
2932 			 "not %<=%>");
2933 	      m = MATCH_ERROR;
2934 	      goto cleanup;
2935 	    }
2936 
2937 	  m = gfc_match_init_expr (&initializer);
2938 	  if (m == MATCH_NO)
2939 	    {
2940 	      gfc_error ("Expected an initialization expression at %C");
2941 	      m = MATCH_ERROR;
2942 	    }
2943 
2944 	  if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2945 	      && !gfc_comp_struct (gfc_state_stack->state))
2946 	    {
2947 	      gfc_error ("Initialization of variable at %C is not allowed in "
2948 			 "a PURE procedure");
2949 	      m = MATCH_ERROR;
2950 	    }
2951 
2952 	  if (current_attr.flavor != FL_PARAMETER
2953 	      && !gfc_comp_struct (gfc_state_stack->state))
2954 	    gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2955 
2956 	  if (m != MATCH_YES)
2957 	    goto cleanup;
2958 	}
2959     }
2960 
2961   if (initializer != NULL && current_attr.allocatable
2962 	&& gfc_comp_struct (gfc_current_state ()))
2963     {
2964       gfc_error ("Initialization of allocatable component at %C is not "
2965 		 "allowed");
2966       m = MATCH_ERROR;
2967       goto cleanup;
2968     }
2969 
2970   if (gfc_current_state () == COMP_DERIVED
2971       && initializer && initializer->ts.type == BT_HOLLERITH)
2972     {
2973       gfc_error ("Initialization of structure component with a HOLLERITH "
2974 		 "constant at %L is not allowed", &initializer->where);
2975       m = MATCH_ERROR;
2976       goto cleanup;
2977     }
2978 
2979   if (gfc_current_state () == COMP_DERIVED
2980       && gfc_current_block ()->attr.pdt_template)
2981     {
2982       gfc_symbol *param;
2983       gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2984 		       0, &param);
2985       if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2986 	{
2987 	  gfc_error ("The component with KIND or LEN attribute at %C does not "
2988 		     "not appear in the type parameter list at %L",
2989 		     &gfc_current_block ()->declared_at);
2990 	  m = MATCH_ERROR;
2991 	  goto cleanup;
2992 	}
2993       else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2994 	{
2995 	  gfc_error ("The component at %C that appears in the type parameter "
2996 		     "list at %L has neither the KIND nor LEN attribute",
2997 		     &gfc_current_block ()->declared_at);
2998 	  m = MATCH_ERROR;
2999 	  goto cleanup;
3000 	}
3001       else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
3002 	{
3003 	  gfc_error ("The component at %C which is a type parameter must be "
3004 		     "a scalar");
3005 	  m = MATCH_ERROR;
3006 	  goto cleanup;
3007 	}
3008       else if (param && initializer)
3009 	{
3010 	  if (initializer->ts.type == BT_BOZ)
3011 	    {
3012 	      gfc_error ("BOZ literal constant at %L cannot appear as an "
3013 			 "initializer", &initializer->where);
3014 	      m = MATCH_ERROR;
3015       	      goto cleanup;
3016 	    }
3017 	  param->value = gfc_copy_expr (initializer);
3018 	}
3019     }
3020 
3021   /* Before adding a possible initilizer, do a simple check for compatibility
3022      of lhs and rhs types.  Assigning a REAL value to a derived type is not a
3023      good thing.  */
3024   if (current_ts.type == BT_DERIVED && initializer
3025       && (gfc_numeric_ts (&initializer->ts)
3026 	  || initializer->ts.type == BT_LOGICAL
3027 	  || initializer->ts.type == BT_CHARACTER))
3028     {
3029       gfc_error ("Incompatible initialization between a derived type "
3030 		 "entity and an entity with %qs type at %C",
3031 		  gfc_typename (initializer));
3032       m = MATCH_ERROR;
3033       goto cleanup;
3034     }
3035 
3036 
3037   /* Add the initializer.  Note that it is fine if initializer is
3038      NULL here, because we sometimes also need to check if a
3039      declaration *must* have an initialization expression.  */
3040   if (!gfc_comp_struct (gfc_current_state ()))
3041     t = add_init_expr_to_sym (name, &initializer, &var_locus);
3042   else
3043     {
3044       if (current_ts.type == BT_DERIVED
3045 	  && !current_attr.pointer && !initializer)
3046 	initializer = gfc_default_initializer (&current_ts);
3047       t = build_struct (name, cl, &initializer, &as);
3048 
3049       /* If we match a nested structure definition we expect to see the
3050        * body even if the variable declarations blow up, so we need to keep
3051        * the structure declaration around.  */
3052       if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
3053         gfc_commit_symbol (gfc_new_block);
3054     }
3055 
3056   m = (t) ? MATCH_YES : MATCH_ERROR;
3057 
3058 cleanup:
3059   /* Free stuff up and return.  */
3060   gfc_seen_div0 = false;
3061   gfc_free_expr (initializer);
3062   gfc_free_array_spec (as);
3063 
3064   return m;
3065 }
3066 
3067 
3068 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
3069    This assumes that the byte size is equal to the kind number for
3070    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
3071 
3072 match
gfc_match_old_kind_spec(gfc_typespec * ts)3073 gfc_match_old_kind_spec (gfc_typespec *ts)
3074 {
3075   match m;
3076   int original_kind;
3077 
3078   if (gfc_match_char ('*') != MATCH_YES)
3079     return MATCH_NO;
3080 
3081   m = gfc_match_small_literal_int (&ts->kind, NULL);
3082   if (m != MATCH_YES)
3083     return MATCH_ERROR;
3084 
3085   original_kind = ts->kind;
3086 
3087   /* Massage the kind numbers for complex types.  */
3088   if (ts->type == BT_COMPLEX)
3089     {
3090       if (ts->kind % 2)
3091 	{
3092 	  gfc_error ("Old-style type declaration %s*%d not supported at %C",
3093 		     gfc_basic_typename (ts->type), original_kind);
3094 	  return MATCH_ERROR;
3095 	}
3096       ts->kind /= 2;
3097 
3098     }
3099 
3100   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3101     ts->kind = 8;
3102 
3103   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3104     {
3105       if (ts->kind == 4)
3106 	{
3107 	  if (flag_real4_kind == 8)
3108 	    ts->kind =  8;
3109 	  if (flag_real4_kind == 10)
3110 	    ts->kind = 10;
3111 	  if (flag_real4_kind == 16)
3112 	    ts->kind = 16;
3113 	}
3114 
3115       if (ts->kind == 8)
3116 	{
3117 	  if (flag_real8_kind == 4)
3118 	    ts->kind = 4;
3119 	  if (flag_real8_kind == 10)
3120 	    ts->kind = 10;
3121 	  if (flag_real8_kind == 16)
3122 	    ts->kind = 16;
3123 	}
3124     }
3125 
3126   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3127     {
3128       gfc_error ("Old-style type declaration %s*%d not supported at %C",
3129 		 gfc_basic_typename (ts->type), original_kind);
3130       return MATCH_ERROR;
3131     }
3132 
3133   if (!gfc_notify_std (GFC_STD_GNU,
3134 		       "Nonstandard type declaration %s*%d at %C",
3135 		       gfc_basic_typename(ts->type), original_kind))
3136     return MATCH_ERROR;
3137 
3138   return MATCH_YES;
3139 }
3140 
3141 
3142 /* Match a kind specification.  Since kinds are generally optional, we
3143    usually return MATCH_NO if something goes wrong.  If a "kind="
3144    string is found, then we know we have an error.  */
3145 
3146 match
gfc_match_kind_spec(gfc_typespec * ts,bool kind_expr_only)3147 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
3148 {
3149   locus where, loc;
3150   gfc_expr *e;
3151   match m, n;
3152   char c;
3153 
3154   m = MATCH_NO;
3155   n = MATCH_YES;
3156   e = NULL;
3157   saved_kind_expr = NULL;
3158 
3159   where = loc = gfc_current_locus;
3160 
3161   if (kind_expr_only)
3162     goto kind_expr;
3163 
3164   if (gfc_match_char ('(') == MATCH_NO)
3165     return MATCH_NO;
3166 
3167   /* Also gobbles optional text.  */
3168   if (gfc_match (" kind = ") == MATCH_YES)
3169     m = MATCH_ERROR;
3170 
3171   loc = gfc_current_locus;
3172 
3173 kind_expr:
3174 
3175   n = gfc_match_init_expr (&e);
3176 
3177   if (gfc_derived_parameter_expr (e))
3178     {
3179       ts->kind = 0;
3180       saved_kind_expr = gfc_copy_expr (e);
3181       goto close_brackets;
3182     }
3183 
3184   if (n != MATCH_YES)
3185     {
3186       if (gfc_matching_function)
3187 	{
3188 	  /* The function kind expression might include use associated or
3189 	     imported parameters and try again after the specification
3190 	     expressions.....  */
3191 	  if (gfc_match_char (')') != MATCH_YES)
3192 	    {
3193 	      gfc_error ("Missing right parenthesis at %C");
3194 	      m = MATCH_ERROR;
3195 	      goto no_match;
3196 	    }
3197 
3198 	  gfc_free_expr (e);
3199 	  gfc_undo_symbols ();
3200 	  return MATCH_YES;
3201 	}
3202       else
3203 	{
3204 	  /* ....or else, the match is real.  */
3205 	  if (n == MATCH_NO)
3206 	    gfc_error ("Expected initialization expression at %C");
3207 	  if (n != MATCH_YES)
3208 	    return MATCH_ERROR;
3209 	}
3210     }
3211 
3212   if (e->rank != 0)
3213     {
3214       gfc_error ("Expected scalar initialization expression at %C");
3215       m = MATCH_ERROR;
3216       goto no_match;
3217     }
3218 
3219   if (gfc_extract_int (e, &ts->kind, 1))
3220     {
3221       m = MATCH_ERROR;
3222       goto no_match;
3223     }
3224 
3225   /* Before throwing away the expression, let's see if we had a
3226      C interoperable kind (and store the fact).	 */
3227   if (e->ts.is_c_interop == 1)
3228     {
3229       /* Mark this as C interoperable if being declared with one
3230 	 of the named constants from iso_c_binding.  */
3231       ts->is_c_interop = e->ts.is_iso_c;
3232       ts->f90_type = e->ts.f90_type;
3233       if (e->symtree)
3234 	ts->interop_kind = e->symtree->n.sym;
3235     }
3236 
3237   gfc_free_expr (e);
3238   e = NULL;
3239 
3240   /* Ignore errors to this point, if we've gotten here.  This means
3241      we ignore the m=MATCH_ERROR from above.  */
3242   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3243     {
3244       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3245 		 gfc_basic_typename (ts->type));
3246       gfc_current_locus = where;
3247       return MATCH_ERROR;
3248     }
3249 
3250   /* Warn if, e.g., c_int is used for a REAL variable, but not
3251      if, e.g., c_double is used for COMPLEX as the standard
3252      explicitly says that the kind type parameter for complex and real
3253      variable is the same, i.e. c_float == c_float_complex.  */
3254   if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3255       && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3256 	   || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3257     gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3258 		     "is %s", gfc_basic_typename (ts->f90_type), &where,
3259 		     gfc_basic_typename (ts->type));
3260 
3261 close_brackets:
3262 
3263   gfc_gobble_whitespace ();
3264   if ((c = gfc_next_ascii_char ()) != ')'
3265       && (ts->type != BT_CHARACTER || c != ','))
3266     {
3267       if (ts->type == BT_CHARACTER)
3268 	gfc_error ("Missing right parenthesis or comma at %C");
3269       else
3270 	gfc_error ("Missing right parenthesis at %C");
3271       m = MATCH_ERROR;
3272     }
3273   else
3274      /* All tests passed.  */
3275      m = MATCH_YES;
3276 
3277   if(m == MATCH_ERROR)
3278      gfc_current_locus = where;
3279 
3280   if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3281     ts->kind =  8;
3282 
3283   if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3284     {
3285       if (ts->kind == 4)
3286 	{
3287 	  if (flag_real4_kind == 8)
3288 	    ts->kind =  8;
3289 	  if (flag_real4_kind == 10)
3290 	    ts->kind = 10;
3291 	  if (flag_real4_kind == 16)
3292 	    ts->kind = 16;
3293 	}
3294 
3295       if (ts->kind == 8)
3296 	{
3297 	  if (flag_real8_kind == 4)
3298 	    ts->kind = 4;
3299 	  if (flag_real8_kind == 10)
3300 	    ts->kind = 10;
3301 	  if (flag_real8_kind == 16)
3302 	    ts->kind = 16;
3303 	}
3304     }
3305 
3306   /* Return what we know from the test(s).  */
3307   return m;
3308 
3309 no_match:
3310   gfc_free_expr (e);
3311   gfc_current_locus = where;
3312   return m;
3313 }
3314 
3315 
3316 static match
match_char_kind(int * kind,int * is_iso_c)3317 match_char_kind (int * kind, int * is_iso_c)
3318 {
3319   locus where;
3320   gfc_expr *e;
3321   match m, n;
3322   bool fail;
3323 
3324   m = MATCH_NO;
3325   e = NULL;
3326   where = gfc_current_locus;
3327 
3328   n = gfc_match_init_expr (&e);
3329 
3330   if (n != MATCH_YES && gfc_matching_function)
3331     {
3332       /* The expression might include use-associated or imported
3333 	 parameters and try again after the specification
3334 	 expressions.  */
3335       gfc_free_expr (e);
3336       gfc_undo_symbols ();
3337       return MATCH_YES;
3338     }
3339 
3340   if (n == MATCH_NO)
3341     gfc_error ("Expected initialization expression at %C");
3342   if (n != MATCH_YES)
3343     return MATCH_ERROR;
3344 
3345   if (e->rank != 0)
3346     {
3347       gfc_error ("Expected scalar initialization expression at %C");
3348       m = MATCH_ERROR;
3349       goto no_match;
3350     }
3351 
3352   if (gfc_derived_parameter_expr (e))
3353     {
3354       saved_kind_expr = e;
3355       *kind = 0;
3356       return MATCH_YES;
3357     }
3358 
3359   fail = gfc_extract_int (e, kind, 1);
3360   *is_iso_c = e->ts.is_iso_c;
3361   if (fail)
3362     {
3363       m = MATCH_ERROR;
3364       goto no_match;
3365     }
3366 
3367   gfc_free_expr (e);
3368 
3369   /* Ignore errors to this point, if we've gotten here.  This means
3370      we ignore the m=MATCH_ERROR from above.  */
3371   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3372     {
3373       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3374       m = MATCH_ERROR;
3375     }
3376   else
3377      /* All tests passed.  */
3378      m = MATCH_YES;
3379 
3380   if (m == MATCH_ERROR)
3381      gfc_current_locus = where;
3382 
3383   /* Return what we know from the test(s).  */
3384   return m;
3385 
3386 no_match:
3387   gfc_free_expr (e);
3388   gfc_current_locus = where;
3389   return m;
3390 }
3391 
3392 
3393 /* Match the various kind/length specifications in a CHARACTER
3394    declaration.  We don't return MATCH_NO.  */
3395 
3396 match
gfc_match_char_spec(gfc_typespec * ts)3397 gfc_match_char_spec (gfc_typespec *ts)
3398 {
3399   int kind, seen_length, is_iso_c;
3400   gfc_charlen *cl;
3401   gfc_expr *len;
3402   match m;
3403   bool deferred;
3404 
3405   len = NULL;
3406   seen_length = 0;
3407   kind = 0;
3408   is_iso_c = 0;
3409   deferred = false;
3410 
3411   /* Try the old-style specification first.  */
3412   old_char_selector = 0;
3413 
3414   m = match_char_length (&len, &deferred, true);
3415   if (m != MATCH_NO)
3416     {
3417       if (m == MATCH_YES)
3418 	old_char_selector = 1;
3419       seen_length = 1;
3420       goto done;
3421     }
3422 
3423   m = gfc_match_char ('(');
3424   if (m != MATCH_YES)
3425     {
3426       m = MATCH_YES;	/* Character without length is a single char.  */
3427       goto done;
3428     }
3429 
3430   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
3431   if (gfc_match (" kind =") == MATCH_YES)
3432     {
3433       m = match_char_kind (&kind, &is_iso_c);
3434 
3435       if (m == MATCH_ERROR)
3436 	goto done;
3437       if (m == MATCH_NO)
3438 	goto syntax;
3439 
3440       if (gfc_match (" , len =") == MATCH_NO)
3441 	goto rparen;
3442 
3443       m = char_len_param_value (&len, &deferred);
3444       if (m == MATCH_NO)
3445 	goto syntax;
3446       if (m == MATCH_ERROR)
3447 	goto done;
3448       seen_length = 1;
3449 
3450       goto rparen;
3451     }
3452 
3453   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
3454   if (gfc_match (" len =") == MATCH_YES)
3455     {
3456       m = char_len_param_value (&len, &deferred);
3457       if (m == MATCH_NO)
3458 	goto syntax;
3459       if (m == MATCH_ERROR)
3460 	goto done;
3461       seen_length = 1;
3462 
3463       if (gfc_match_char (')') == MATCH_YES)
3464 	goto done;
3465 
3466       if (gfc_match (" , kind =") != MATCH_YES)
3467 	goto syntax;
3468 
3469       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3470 	goto done;
3471 
3472       goto rparen;
3473     }
3474 
3475   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
3476   m = char_len_param_value (&len, &deferred);
3477   if (m == MATCH_NO)
3478     goto syntax;
3479   if (m == MATCH_ERROR)
3480     goto done;
3481   seen_length = 1;
3482 
3483   m = gfc_match_char (')');
3484   if (m == MATCH_YES)
3485     goto done;
3486 
3487   if (gfc_match_char (',') != MATCH_YES)
3488     goto syntax;
3489 
3490   gfc_match (" kind =");	/* Gobble optional text.  */
3491 
3492   m = match_char_kind (&kind, &is_iso_c);
3493   if (m == MATCH_ERROR)
3494     goto done;
3495   if (m == MATCH_NO)
3496     goto syntax;
3497 
3498 rparen:
3499   /* Require a right-paren at this point.  */
3500   m = gfc_match_char (')');
3501   if (m == MATCH_YES)
3502     goto done;
3503 
3504 syntax:
3505   gfc_error ("Syntax error in CHARACTER declaration at %C");
3506   m = MATCH_ERROR;
3507   gfc_free_expr (len);
3508   return m;
3509 
3510 done:
3511   /* Deal with character functions after USE and IMPORT statements.  */
3512   if (gfc_matching_function)
3513     {
3514       gfc_free_expr (len);
3515       gfc_undo_symbols ();
3516       return MATCH_YES;
3517     }
3518 
3519   if (m != MATCH_YES)
3520     {
3521       gfc_free_expr (len);
3522       return m;
3523     }
3524 
3525   /* Do some final massaging of the length values.  */
3526   cl = gfc_new_charlen (gfc_current_ns, NULL);
3527 
3528   if (seen_length == 0)
3529     cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3530   else
3531     {
3532       /* If gfortran ends up here, then len may be reducible to a constant.
3533 	 Try to do that here.  If it does not reduce, simply assign len to
3534 	 charlen.  A complication occurs with user-defined generic functions,
3535 	 which are not resolved.  Use a private namespace to deal with
3536 	 generic functions.  */
3537 
3538       if (len && len->expr_type != EXPR_CONSTANT)
3539 	{
3540 	  gfc_namespace *old_ns;
3541 	  gfc_expr *e;
3542 
3543 	  old_ns = gfc_current_ns;
3544 	  gfc_current_ns = gfc_get_namespace (NULL, 0);
3545 
3546 	  e = gfc_copy_expr (len);
3547 	  gfc_reduce_init_expr (e);
3548 	  if (e->expr_type == EXPR_CONSTANT)
3549 	    {
3550 	      gfc_replace_expr (len, e);
3551 	      if (mpz_cmp_si (len->value.integer, 0) < 0)
3552 		mpz_set_ui (len->value.integer, 0);
3553 	    }
3554 	  else
3555 	    gfc_free_expr (e);
3556 
3557 	  gfc_free_namespace (gfc_current_ns);
3558 	  gfc_current_ns = old_ns;
3559 	}
3560 
3561       cl->length = len;
3562     }
3563 
3564   ts->u.cl = cl;
3565   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3566   ts->deferred = deferred;
3567 
3568   /* We have to know if it was a C interoperable kind so we can
3569      do accurate type checking of bind(c) procs, etc.  */
3570   if (kind != 0)
3571     /* Mark this as C interoperable if being declared with one
3572        of the named constants from iso_c_binding.  */
3573     ts->is_c_interop = is_iso_c;
3574   else if (len != NULL)
3575     /* Here, we might have parsed something such as: character(c_char)
3576        In this case, the parsing code above grabs the c_char when
3577        looking for the length (line 1690, roughly).  it's the last
3578        testcase for parsing the kind params of a character variable.
3579        However, it's not actually the length.	 this seems like it
3580        could be an error.
3581        To see if the user used a C interop kind, test the expr
3582        of the so called length, and see if it's C interoperable.  */
3583     ts->is_c_interop = len->ts.is_iso_c;
3584 
3585   return MATCH_YES;
3586 }
3587 
3588 
3589 /* Matches a RECORD declaration. */
3590 
3591 static match
match_record_decl(char * name)3592 match_record_decl (char *name)
3593 {
3594     locus old_loc;
3595     old_loc = gfc_current_locus;
3596     match m;
3597 
3598     m = gfc_match (" record /");
3599     if (m == MATCH_YES)
3600       {
3601           if (!flag_dec_structure)
3602             {
3603                 gfc_current_locus = old_loc;
3604                 gfc_error ("RECORD at %C is an extension, enable it with "
3605 			   "%<-fdec-structure%>");
3606                 return MATCH_ERROR;
3607             }
3608           m = gfc_match (" %n/", name);
3609           if (m == MATCH_YES)
3610             return MATCH_YES;
3611       }
3612 
3613   gfc_current_locus = old_loc;
3614   if (flag_dec_structure
3615       && (gfc_match (" record% ") == MATCH_YES
3616           || gfc_match (" record%t") == MATCH_YES))
3617     gfc_error ("Structure name expected after RECORD at %C");
3618   if (m == MATCH_NO)
3619     return MATCH_NO;
3620 
3621   return MATCH_ERROR;
3622 }
3623 
3624 
3625 /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3626    of expressions to substitute into the possibly parameterized expression
3627    'e'. Using a list is inefficient but should not be too bad since the
3628    number of type parameters is not likely to be large.  */
3629 static bool
insert_parameter_exprs(gfc_expr * e,gfc_symbol * sym ATTRIBUTE_UNUSED,int * f)3630 insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3631 			int* f)
3632 {
3633   gfc_actual_arglist *param;
3634   gfc_expr *copy;
3635 
3636   if (e->expr_type != EXPR_VARIABLE)
3637     return false;
3638 
3639   gcc_assert (e->symtree);
3640   if (e->symtree->n.sym->attr.pdt_kind
3641       || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3642     {
3643       for (param = type_param_spec_list; param; param = param->next)
3644 	if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3645 	  break;
3646 
3647       if (param)
3648 	{
3649 	  copy = gfc_copy_expr (param->expr);
3650 	  *e = *copy;
3651 	  free (copy);
3652 	}
3653     }
3654 
3655   return false;
3656 }
3657 
3658 
3659 bool
gfc_insert_kind_parameter_exprs(gfc_expr * e)3660 gfc_insert_kind_parameter_exprs (gfc_expr *e)
3661 {
3662   return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3663 }
3664 
3665 
3666 bool
gfc_insert_parameter_exprs(gfc_expr * e,gfc_actual_arglist * param_list)3667 gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3668 {
3669   gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3670   type_param_spec_list = param_list;
3671   return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3672   type_param_spec_list = NULL;
3673   type_param_spec_list = old_param_spec_list;
3674 }
3675 
3676 /* Determines the instance of a parameterized derived type to be used by
3677    matching determining the values of the kind parameters and using them
3678    in the name of the instance. If the instance exists, it is used, otherwise
3679    a new derived type is created.  */
3680 match
gfc_get_pdt_instance(gfc_actual_arglist * param_list,gfc_symbol ** sym,gfc_actual_arglist ** ext_param_list)3681 gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3682 		      gfc_actual_arglist **ext_param_list)
3683 {
3684   /* The PDT template symbol.  */
3685   gfc_symbol *pdt = *sym;
3686   /* The symbol for the parameter in the template f2k_namespace.  */
3687   gfc_symbol *param;
3688   /* The hoped for instance of the PDT.  */
3689   gfc_symbol *instance;
3690   /* The list of parameters appearing in the PDT declaration.  */
3691   gfc_formal_arglist *type_param_name_list;
3692   /* Used to store the parameter specification list during recursive calls.  */
3693   gfc_actual_arglist *old_param_spec_list;
3694   /* Pointers to the parameter specification being used.  */
3695   gfc_actual_arglist *actual_param;
3696   gfc_actual_arglist *tail = NULL;
3697   /* Used to build up the name of the PDT instance. The prefix uses 4
3698      characters and each KIND parameter 2 more.  Allow 8 of the latter. */
3699   char name[GFC_MAX_SYMBOL_LEN + 21];
3700 
3701   bool name_seen = (param_list == NULL);
3702   bool assumed_seen = false;
3703   bool deferred_seen = false;
3704   bool spec_error = false;
3705   int kind_value, i;
3706   gfc_expr *kind_expr;
3707   gfc_component *c1, *c2;
3708   match m;
3709 
3710   type_param_spec_list = NULL;
3711 
3712   type_param_name_list = pdt->formal;
3713   actual_param = param_list;
3714   sprintf (name, "Pdt%s", pdt->name);
3715 
3716   /* Run through the parameter name list and pick up the actual
3717      parameter values or use the default values in the PDT declaration.  */
3718   for (; type_param_name_list;
3719        type_param_name_list = type_param_name_list->next)
3720     {
3721       if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3722 	{
3723 	  if (actual_param->spec_type == SPEC_ASSUMED)
3724 	    spec_error = deferred_seen;
3725 	  else
3726 	    spec_error = assumed_seen;
3727 
3728 	  if (spec_error)
3729 	    {
3730 	      gfc_error ("The type parameter spec list at %C cannot contain "
3731 			 "both ASSUMED and DEFERRED parameters");
3732 	      goto error_return;
3733 	    }
3734 	}
3735 
3736       if (actual_param && actual_param->name)
3737 	name_seen = true;
3738       param = type_param_name_list->sym;
3739 
3740       if (!param || !param->name)
3741 	continue;
3742 
3743       c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3744       /* An error should already have been thrown in resolve.c
3745 	 (resolve_fl_derived0).  */
3746       if (!pdt->attr.use_assoc && !c1)
3747 	goto error_return;
3748 
3749       kind_expr = NULL;
3750       if (!name_seen)
3751 	{
3752 	  if (!actual_param && !(c1 && c1->initializer))
3753 	    {
3754 	      gfc_error ("The type parameter spec list at %C does not contain "
3755 			 "enough parameter expressions");
3756 	      goto error_return;
3757 	    }
3758 	  else if (!actual_param && c1 && c1->initializer)
3759 	    kind_expr = gfc_copy_expr (c1->initializer);
3760 	  else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3761 	    kind_expr = gfc_copy_expr (actual_param->expr);
3762 	}
3763       else
3764 	{
3765 	  actual_param = param_list;
3766 	  for (;actual_param; actual_param = actual_param->next)
3767 	    if (actual_param->name
3768 	        && strcmp (actual_param->name, param->name) == 0)
3769 	      break;
3770 	  if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3771 	    kind_expr = gfc_copy_expr (actual_param->expr);
3772 	  else
3773 	    {
3774 	      if (c1->initializer)
3775 		kind_expr = gfc_copy_expr (c1->initializer);
3776 	      else if (!(actual_param && param->attr.pdt_len))
3777 		{
3778 		  gfc_error ("The derived parameter %qs at %C does not "
3779 			     "have a default value", param->name);
3780 		  goto error_return;
3781 		}
3782 	    }
3783 	}
3784 
3785       /* Store the current parameter expressions in a temporary actual
3786 	 arglist 'list' so that they can be substituted in the corresponding
3787 	 expressions in the PDT instance.  */
3788       if (type_param_spec_list == NULL)
3789 	{
3790 	  type_param_spec_list = gfc_get_actual_arglist ();
3791 	  tail = type_param_spec_list;
3792 	}
3793       else
3794 	{
3795 	  tail->next = gfc_get_actual_arglist ();
3796 	  tail = tail->next;
3797 	}
3798       tail->name = param->name;
3799 
3800       if (kind_expr)
3801 	{
3802 	  /* Try simplification even for LEN expressions.  */
3803 	  gfc_resolve_expr (kind_expr);
3804 	  gfc_simplify_expr (kind_expr, 1);
3805 	  /* Variable expressions seem to default to BT_PROCEDURE.
3806 	     TODO find out why this is and fix it.  */
3807 	  if (kind_expr->ts.type != BT_INTEGER
3808 	      && kind_expr->ts.type != BT_PROCEDURE)
3809 	    {
3810 	      gfc_error ("The parameter expression at %C must be of "
3811 		         "INTEGER type and not %s type",
3812 			 gfc_basic_typename (kind_expr->ts.type));
3813 	      goto error_return;
3814 	    }
3815 
3816 	  tail->expr = gfc_copy_expr (kind_expr);
3817 	}
3818 
3819       if (actual_param)
3820 	tail->spec_type = actual_param->spec_type;
3821 
3822       if (!param->attr.pdt_kind)
3823 	{
3824 	  if (!name_seen && actual_param)
3825 	    actual_param = actual_param->next;
3826 	  if (kind_expr)
3827 	    {
3828 	      gfc_free_expr (kind_expr);
3829 	      kind_expr = NULL;
3830 	    }
3831 	  continue;
3832 	}
3833 
3834       if (actual_param
3835 	  && (actual_param->spec_type == SPEC_ASSUMED
3836 	      || actual_param->spec_type == SPEC_DEFERRED))
3837 	{
3838 	  gfc_error ("The KIND parameter %qs at %C cannot either be "
3839 		     "ASSUMED or DEFERRED", param->name);
3840 	  goto error_return;
3841 	}
3842 
3843       if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3844 	{
3845 	  gfc_error ("The value for the KIND parameter %qs at %C does not "
3846 		     "reduce to a constant expression", param->name);
3847 	  goto error_return;
3848 	}
3849 
3850       gfc_extract_int (kind_expr, &kind_value);
3851       sprintf (name + strlen (name), "_%d", kind_value);
3852 
3853       if (!name_seen && actual_param)
3854 	actual_param = actual_param->next;
3855       gfc_free_expr (kind_expr);
3856     }
3857 
3858   if (!name_seen && actual_param)
3859     {
3860       gfc_error ("The type parameter spec list at %C contains too many "
3861 		 "parameter expressions");
3862       goto error_return;
3863     }
3864 
3865   /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3866      build it, using 'pdt' as a template.  */
3867   if (gfc_get_symbol (name, pdt->ns, &instance))
3868     {
3869       gfc_error ("Parameterized derived type at %C is ambiguous");
3870       goto error_return;
3871     }
3872 
3873   m = MATCH_YES;
3874 
3875   if (instance->attr.flavor == FL_DERIVED
3876       && instance->attr.pdt_type)
3877     {
3878       instance->refs++;
3879       if (ext_param_list)
3880         *ext_param_list = type_param_spec_list;
3881       *sym = instance;
3882       gfc_commit_symbols ();
3883       return m;
3884     }
3885 
3886   /* Start building the new instance of the parameterized type.  */
3887   gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3888   instance->attr.pdt_template = 0;
3889   instance->attr.pdt_type = 1;
3890   instance->declared_at = gfc_current_locus;
3891 
3892   /* Add the components, replacing the parameters in all expressions
3893      with the expressions for their values in 'type_param_spec_list'.  */
3894   c1 = pdt->components;
3895   tail = type_param_spec_list;
3896   for (; c1; c1 = c1->next)
3897     {
3898       gfc_add_component (instance, c1->name, &c2);
3899 
3900       c2->ts = c1->ts;
3901       c2->attr = c1->attr;
3902 
3903       /* The order of declaration of the type_specs might not be the
3904 	 same as that of the components.  */
3905       if (c1->attr.pdt_kind || c1->attr.pdt_len)
3906 	{
3907 	  for (tail = type_param_spec_list; tail; tail = tail->next)
3908 	    if (strcmp (c1->name, tail->name) == 0)
3909 	      break;
3910 	}
3911 
3912       /* Deal with type extension by recursively calling this function
3913 	 to obtain the instance of the extended type.  */
3914       if (gfc_current_state () != COMP_DERIVED
3915 	  && c1 == pdt->components
3916 	  && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3917 	  && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3918 	  && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3919 	{
3920 	  gfc_formal_arglist *f;
3921 
3922 	  old_param_spec_list = type_param_spec_list;
3923 
3924 	  /* Obtain a spec list appropriate to the extended type..*/
3925 	  actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3926 	  type_param_spec_list = actual_param;
3927 	  for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3928 	    actual_param = actual_param->next;
3929 	  if (actual_param)
3930 	    {
3931 	      gfc_free_actual_arglist (actual_param->next);
3932 	      actual_param->next = NULL;
3933 	    }
3934 
3935 	  /* Now obtain the PDT instance for the extended type.  */
3936 	  c2->param_list = type_param_spec_list;
3937 	  m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3938 				    NULL);
3939 	  type_param_spec_list = old_param_spec_list;
3940 
3941 	  c2->ts.u.derived->refs++;
3942 	  gfc_set_sym_referenced (c2->ts.u.derived);
3943 
3944 	  /* Set extension level.  */
3945 	  if (c2->ts.u.derived->attr.extension == 255)
3946 	    {
3947 	      /* Since the extension field is 8 bit wide, we can only have
3948 		 up to 255 extension levels.  */
3949 	      gfc_error ("Maximum extension level reached with type %qs at %L",
3950 			 c2->ts.u.derived->name,
3951 			 &c2->ts.u.derived->declared_at);
3952 	      goto error_return;
3953 	    }
3954 	  instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3955 
3956 	  continue;
3957 	}
3958 
3959       /* Set the component kind using the parameterized expression.  */
3960       if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3961 	   && c1->kind_expr != NULL)
3962 	{
3963 	  gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3964 	  gfc_insert_kind_parameter_exprs (e);
3965 	  gfc_simplify_expr (e, 1);
3966 	  gfc_extract_int (e, &c2->ts.kind);
3967 	  gfc_free_expr (e);
3968 	  if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3969 	    {
3970 	      gfc_error ("Kind %d not supported for type %s at %C",
3971 			 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3972 	      goto error_return;
3973 	    }
3974 	}
3975 
3976       /* Similarly, set the string length if parameterized.  */
3977       if (c1->ts.type == BT_CHARACTER
3978 	  && c1->ts.u.cl->length
3979 	  && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3980 	{
3981 	  gfc_expr *e;
3982 	  e = gfc_copy_expr (c1->ts.u.cl->length);
3983 	  gfc_insert_kind_parameter_exprs (e);
3984 	  gfc_simplify_expr (e, 1);
3985 	  c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3986 	  c2->ts.u.cl->length = e;
3987 	  c2->attr.pdt_string = 1;
3988 	}
3989 
3990       /* Set up either the KIND/LEN initializer, if constant,
3991 	 or the parameterized expression. Use the template
3992 	 initializer if one is not already set in this instance.  */
3993       if (c2->attr.pdt_kind || c2->attr.pdt_len)
3994 	{
3995 	  if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3996 	    c2->initializer = gfc_copy_expr (tail->expr);
3997 	  else if (tail && tail->expr)
3998 	    {
3999 	      c2->param_list = gfc_get_actual_arglist ();
4000 	      c2->param_list->name = tail->name;
4001 	      c2->param_list->expr = gfc_copy_expr (tail->expr);
4002 	      c2->param_list->next = NULL;
4003 	    }
4004 
4005 	  if (!c2->initializer && c1->initializer)
4006 	    c2->initializer = gfc_copy_expr (c1->initializer);
4007 	}
4008 
4009       /* Copy the array spec.  */
4010       c2->as = gfc_copy_array_spec (c1->as);
4011       if (c1->ts.type == BT_CLASS)
4012 	CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
4013 
4014       /* Determine if an array spec is parameterized. If so, substitute
4015 	 in the parameter expressions for the bounds and set the pdt_array
4016 	 attribute. Notice that this attribute must be unconditionally set
4017 	 if this is an array of parameterized character length.  */
4018       if (c1->as && c1->as->type == AS_EXPLICIT)
4019 	{
4020 	  bool pdt_array = false;
4021 
4022 	  /* Are the bounds of the array parameterized?  */
4023 	  for (i = 0; i < c1->as->rank; i++)
4024 	    {
4025 	      if (gfc_derived_parameter_expr (c1->as->lower[i]))
4026 		pdt_array = true;
4027 	      if (gfc_derived_parameter_expr (c1->as->upper[i]))
4028 		pdt_array = true;
4029 	    }
4030 
4031 	  /* If they are, free the expressions for the bounds and
4032 	     replace them with the template expressions with substitute
4033 	     values.  */
4034 	  for (i = 0; pdt_array && i < c1->as->rank; i++)
4035 	    {
4036 	      gfc_expr *e;
4037 	      e = gfc_copy_expr (c1->as->lower[i]);
4038 	      gfc_insert_kind_parameter_exprs (e);
4039 	      gfc_simplify_expr (e, 1);
4040 	      gfc_free_expr (c2->as->lower[i]);
4041 	      c2->as->lower[i] = e;
4042 	      e = gfc_copy_expr (c1->as->upper[i]);
4043 	      gfc_insert_kind_parameter_exprs (e);
4044 	      gfc_simplify_expr (e, 1);
4045 	      gfc_free_expr (c2->as->upper[i]);
4046 	      c2->as->upper[i] = e;
4047 	    }
4048 	  c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
4049 	  if (c1->initializer)
4050 	    {
4051 	      c2->initializer = gfc_copy_expr (c1->initializer);
4052 	      gfc_insert_kind_parameter_exprs (c2->initializer);
4053 	      gfc_simplify_expr (c2->initializer, 1);
4054 	    }
4055 	}
4056 
4057       /* Recurse into this function for PDT components.  */
4058       if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
4059 	  && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
4060 	{
4061 	  gfc_actual_arglist *params;
4062 	  /* The component in the template has a list of specification
4063 	     expressions derived from its declaration.  */
4064 	  params = gfc_copy_actual_arglist (c1->param_list);
4065 	  actual_param = params;
4066 	  /* Substitute the template parameters with the expressions
4067 	     from the specification list.  */
4068 	  for (;actual_param; actual_param = actual_param->next)
4069 	    gfc_insert_parameter_exprs (actual_param->expr,
4070 					type_param_spec_list);
4071 
4072 	  /* Now obtain the PDT instance for the component.  */
4073 	  old_param_spec_list = type_param_spec_list;
4074 	  m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
4075 	  type_param_spec_list = old_param_spec_list;
4076 
4077 	  c2->param_list = params;
4078 	  if (!(c2->attr.pointer || c2->attr.allocatable))
4079 	    c2->initializer = gfc_default_initializer (&c2->ts);
4080 
4081 	  if (c2->attr.allocatable)
4082 	    instance->attr.alloc_comp = 1;
4083 	}
4084     }
4085 
4086   gfc_commit_symbol (instance);
4087   if (ext_param_list)
4088     *ext_param_list = type_param_spec_list;
4089   *sym = instance;
4090   return m;
4091 
4092 error_return:
4093   gfc_free_actual_arglist (type_param_spec_list);
4094   return MATCH_ERROR;
4095 }
4096 
4097 
4098 /* Match a legacy nonstandard BYTE type-spec.  */
4099 
4100 static match
match_byte_typespec(gfc_typespec * ts)4101 match_byte_typespec (gfc_typespec *ts)
4102 {
4103   if (gfc_match (" byte") == MATCH_YES)
4104     {
4105       if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
4106 	return MATCH_ERROR;
4107 
4108       if (gfc_current_form == FORM_FREE)
4109 	{
4110 	  char c = gfc_peek_ascii_char ();
4111 	  if (!gfc_is_whitespace (c) && c != ',')
4112 	    return MATCH_NO;
4113 	}
4114 
4115       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4116 	{
4117 	  gfc_error ("BYTE type used at %C "
4118 		     "is not available on the target machine");
4119 	  return MATCH_ERROR;
4120 	}
4121 
4122       ts->type = BT_INTEGER;
4123       ts->kind = 1;
4124       return MATCH_YES;
4125     }
4126   return MATCH_NO;
4127 }
4128 
4129 
4130 /* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
4131    structure to the matched specification.  This is necessary for FUNCTION and
4132    IMPLICIT statements.
4133 
4134    If implicit_flag is nonzero, then we don't check for the optional
4135    kind specification.  Not doing so is needed for matching an IMPLICIT
4136    statement correctly.  */
4137 
4138 match
gfc_match_decl_type_spec(gfc_typespec * ts,int implicit_flag)4139 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4140 {
4141   /* Provide sufficient space to hold "pdtsymbol".  */
4142   char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
4143   gfc_symbol *sym, *dt_sym;
4144   match m;
4145   char c;
4146   bool seen_deferred_kind, matched_type;
4147   const char *dt_name;
4148 
4149   decl_type_param_list = NULL;
4150 
4151   /* A belt and braces check that the typespec is correctly being treated
4152      as a deferred characteristic association.  */
4153   seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
4154 			  && (gfc_current_block ()->result->ts.kind == -1)
4155 			  && (ts->kind == -1);
4156   gfc_clear_ts (ts);
4157   if (seen_deferred_kind)
4158     ts->kind = -1;
4159 
4160   /* Clear the current binding label, in case one is given.  */
4161   curr_binding_label = NULL;
4162 
4163   /* Match BYTE type-spec.  */
4164   m = match_byte_typespec (ts);
4165   if (m != MATCH_NO)
4166     return m;
4167 
4168   m = gfc_match (" type (");
4169   matched_type = (m == MATCH_YES);
4170   if (matched_type)
4171     {
4172       gfc_gobble_whitespace ();
4173       if (gfc_peek_ascii_char () == '*')
4174 	{
4175 	  if ((m = gfc_match ("* ) ")) != MATCH_YES)
4176 	    return m;
4177 	  if (gfc_comp_struct (gfc_current_state ()))
4178 	    {
4179 	      gfc_error ("Assumed type at %C is not allowed for components");
4180 	      return MATCH_ERROR;
4181 	    }
4182 	  if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4183 	    return MATCH_ERROR;
4184 	  ts->type = BT_ASSUMED;
4185 	  return MATCH_YES;
4186 	}
4187 
4188       m = gfc_match ("%n", name);
4189       matched_type = (m == MATCH_YES);
4190     }
4191 
4192   if ((matched_type && strcmp ("integer", name) == 0)
4193       || (!matched_type && gfc_match (" integer") == MATCH_YES))
4194     {
4195       ts->type = BT_INTEGER;
4196       ts->kind = gfc_default_integer_kind;
4197       goto get_kind;
4198     }
4199 
4200   if ((matched_type && strcmp ("character", name) == 0)
4201       || (!matched_type && gfc_match (" character") == MATCH_YES))
4202     {
4203       if (matched_type
4204 	  && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4205 			      "intrinsic-type-spec at %C"))
4206 	return MATCH_ERROR;
4207 
4208       ts->type = BT_CHARACTER;
4209       if (implicit_flag == 0)
4210 	m = gfc_match_char_spec (ts);
4211       else
4212 	m = MATCH_YES;
4213 
4214       if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4215 	{
4216 	  gfc_error ("Malformed type-spec at %C");
4217 	  return MATCH_ERROR;
4218 	}
4219 
4220       return m;
4221     }
4222 
4223   if ((matched_type && strcmp ("real", name) == 0)
4224       || (!matched_type && gfc_match (" real") == MATCH_YES))
4225     {
4226       ts->type = BT_REAL;
4227       ts->kind = gfc_default_real_kind;
4228       goto get_kind;
4229     }
4230 
4231   if ((matched_type
4232        && (strcmp ("doubleprecision", name) == 0
4233 	   || (strcmp ("double", name) == 0
4234 	       && gfc_match (" precision") == MATCH_YES)))
4235       || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4236     {
4237       if (matched_type
4238 	  && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4239 			      "intrinsic-type-spec at %C"))
4240 	return MATCH_ERROR;
4241 
4242       if (matched_type && gfc_match_char (')') != MATCH_YES)
4243 	{
4244 	  gfc_error ("Malformed type-spec at %C");
4245 	  return MATCH_ERROR;
4246 	}
4247 
4248       ts->type = BT_REAL;
4249       ts->kind = gfc_default_double_kind;
4250       return MATCH_YES;
4251     }
4252 
4253   if ((matched_type && strcmp ("complex", name) == 0)
4254       || (!matched_type && gfc_match (" complex") == MATCH_YES))
4255     {
4256       ts->type = BT_COMPLEX;
4257       ts->kind = gfc_default_complex_kind;
4258       goto get_kind;
4259     }
4260 
4261   if ((matched_type
4262        && (strcmp ("doublecomplex", name) == 0
4263 	   || (strcmp ("double", name) == 0
4264 	       && gfc_match (" complex") == MATCH_YES)))
4265       || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4266     {
4267       if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4268 	return MATCH_ERROR;
4269 
4270       if (matched_type
4271 	  && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4272 			      "intrinsic-type-spec at %C"))
4273 	return MATCH_ERROR;
4274 
4275       if (matched_type && gfc_match_char (')') != MATCH_YES)
4276 	{
4277 	  gfc_error ("Malformed type-spec at %C");
4278 	  return MATCH_ERROR;
4279 	}
4280 
4281       ts->type = BT_COMPLEX;
4282       ts->kind = gfc_default_double_kind;
4283       return MATCH_YES;
4284     }
4285 
4286   if ((matched_type && strcmp ("logical", name) == 0)
4287       || (!matched_type && gfc_match (" logical") == MATCH_YES))
4288     {
4289       ts->type = BT_LOGICAL;
4290       ts->kind = gfc_default_logical_kind;
4291       goto get_kind;
4292     }
4293 
4294   if (matched_type)
4295     {
4296       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4297       if (m == MATCH_ERROR)
4298 	return m;
4299 
4300       gfc_gobble_whitespace ();
4301       if (gfc_peek_ascii_char () != ')')
4302 	{
4303 	  gfc_error ("Malformed type-spec at %C");
4304 	  return MATCH_ERROR;
4305 	}
4306       m = gfc_match_char (')'); /* Burn closing ')'.  */
4307     }
4308 
4309   if (m != MATCH_YES)
4310     m = match_record_decl (name);
4311 
4312   if (matched_type || m == MATCH_YES)
4313     {
4314       ts->type = BT_DERIVED;
4315       /* We accept record/s/ or type(s) where s is a structure, but we
4316        * don't need all the extra derived-type stuff for structures.  */
4317       if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4318         {
4319           gfc_error ("Type name %qs at %C is ambiguous", name);
4320           return MATCH_ERROR;
4321         }
4322 
4323       if (sym && sym->attr.flavor == FL_DERIVED
4324 	  && sym->attr.pdt_template
4325 	  && gfc_current_state () != COMP_DERIVED)
4326 	{
4327 	  m = gfc_get_pdt_instance (decl_type_param_list, &sym,  NULL);
4328 	  if (m != MATCH_YES)
4329 	    return m;
4330 	  gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4331 	  ts->u.derived = sym;
4332 	  const char* lower = gfc_dt_lower_string (sym->name);
4333 	  size_t len = strlen (lower);
4334 	  /* Reallocate with sufficient size.  */
4335 	  if (len > GFC_MAX_SYMBOL_LEN)
4336 	    name = XALLOCAVEC (char, len + 1);
4337 	  memcpy (name, lower, len);
4338 	  name[len] = '\0';
4339 	}
4340 
4341       if (sym && sym->attr.flavor == FL_STRUCT)
4342         {
4343           ts->u.derived = sym;
4344           return MATCH_YES;
4345         }
4346       /* Actually a derived type.  */
4347     }
4348 
4349   else
4350     {
4351       /* Match nested STRUCTURE declarations; only valid within another
4352 	 structure declaration.  */
4353       if (flag_dec_structure
4354 	  && (gfc_current_state () == COMP_STRUCTURE
4355 	      || gfc_current_state () == COMP_MAP))
4356 	{
4357 	  m = gfc_match (" structure");
4358 	  if (m == MATCH_YES)
4359 	    {
4360 	      m = gfc_match_structure_decl ();
4361 	      if (m == MATCH_YES)
4362 		{
4363 		  /* gfc_new_block is updated by match_structure_decl.  */
4364 		  ts->type = BT_DERIVED;
4365 		  ts->u.derived = gfc_new_block;
4366 		  return MATCH_YES;
4367 		}
4368 	    }
4369 	  if (m == MATCH_ERROR)
4370 	    return MATCH_ERROR;
4371 	}
4372 
4373       /* Match CLASS declarations.  */
4374       m = gfc_match (" class ( * )");
4375       if (m == MATCH_ERROR)
4376 	return MATCH_ERROR;
4377       else if (m == MATCH_YES)
4378 	{
4379 	  gfc_symbol *upe;
4380 	  gfc_symtree *st;
4381 	  ts->type = BT_CLASS;
4382 	  gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4383 	  if (upe == NULL)
4384 	    {
4385 	      upe = gfc_new_symbol ("STAR", gfc_current_ns);
4386 	      st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4387 	      st->n.sym = upe;
4388 	      gfc_set_sym_referenced (upe);
4389 	      upe->refs++;
4390 	      upe->ts.type = BT_VOID;
4391 	      upe->attr.unlimited_polymorphic = 1;
4392 	      /* This is essential to force the construction of
4393 		 unlimited polymorphic component class containers.  */
4394 	      upe->attr.zero_comp = 1;
4395 	      if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4396 				   &gfc_current_locus))
4397 	      return MATCH_ERROR;
4398 	    }
4399 	  else
4400 	    {
4401 	      st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4402 	      st->n.sym = upe;
4403 	      upe->refs++;
4404 	    }
4405 	  ts->u.derived = upe;
4406 	  return m;
4407 	}
4408 
4409       m = gfc_match (" class (");
4410 
4411       if (m == MATCH_YES)
4412 	m = gfc_match ("%n", name);
4413       else
4414 	return m;
4415 
4416       if (m != MATCH_YES)
4417 	return m;
4418       ts->type = BT_CLASS;
4419 
4420       if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4421 	return MATCH_ERROR;
4422 
4423       m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4424       if (m == MATCH_ERROR)
4425 	return m;
4426 
4427       m = gfc_match_char (')');
4428       if (m != MATCH_YES)
4429 	return m;
4430     }
4431 
4432   /* Defer association of the derived type until the end of the
4433      specification block.  However, if the derived type can be
4434      found, add it to the typespec.  */
4435   if (gfc_matching_function)
4436     {
4437       ts->u.derived = NULL;
4438       if (gfc_current_state () != COMP_INTERFACE
4439 	    && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4440 	{
4441 	  sym = gfc_find_dt_in_generic (sym);
4442 	  ts->u.derived = sym;
4443 	}
4444       return MATCH_YES;
4445     }
4446 
4447   /* Search for the name but allow the components to be defined later.  If
4448      type = -1, this typespec has been seen in a function declaration but
4449      the type could not be accessed at that point.  The actual derived type is
4450      stored in a symtree with the first letter of the name capitalized; the
4451      symtree with the all lower-case name contains the associated
4452      generic function.  */
4453   dt_name = gfc_dt_upper_string (name);
4454   sym = NULL;
4455   dt_sym = NULL;
4456   if (ts->kind != -1)
4457     {
4458       gfc_get_ha_symbol (name, &sym);
4459       if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4460 	{
4461 	  gfc_error ("Type name %qs at %C is ambiguous", name);
4462 	  return MATCH_ERROR;
4463 	}
4464       if (sym->generic && !dt_sym)
4465 	dt_sym = gfc_find_dt_in_generic (sym);
4466 
4467       /* Host associated PDTs can get confused with their constructors
4468 	 because they ar instantiated in the template's namespace.  */
4469       if (!dt_sym)
4470 	{
4471 	  if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4472 	    {
4473 	      gfc_error ("Type name %qs at %C is ambiguous", name);
4474 	      return MATCH_ERROR;
4475 	    }
4476 	  if (dt_sym && !dt_sym->attr.pdt_type)
4477 	    dt_sym = NULL;
4478 	}
4479     }
4480   else if (ts->kind == -1)
4481     {
4482       int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4483 		    || gfc_current_ns->has_import_set;
4484       gfc_find_symbol (name, NULL, iface, &sym);
4485       if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4486 	{
4487 	  gfc_error ("Type name %qs at %C is ambiguous", name);
4488 	  return MATCH_ERROR;
4489 	}
4490       if (sym && sym->generic && !dt_sym)
4491 	dt_sym = gfc_find_dt_in_generic (sym);
4492 
4493       ts->kind = 0;
4494       if (sym == NULL)
4495 	return MATCH_NO;
4496     }
4497 
4498   if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4499        && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4500       || sym->attr.subroutine)
4501     {
4502       gfc_error ("Type name %qs at %C conflicts with previously declared "
4503 		 "entity at %L, which has the same name", name,
4504 		 &sym->declared_at);
4505       return MATCH_ERROR;
4506     }
4507 
4508   if (sym && sym->attr.flavor == FL_DERIVED
4509       && sym->attr.pdt_template
4510       && gfc_current_state () != COMP_DERIVED)
4511     {
4512       m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4513       if (m != MATCH_YES)
4514 	return m;
4515       gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4516       ts->u.derived = sym;
4517       strcpy (name, gfc_dt_lower_string (sym->name));
4518     }
4519 
4520   gfc_save_symbol_data (sym);
4521   gfc_set_sym_referenced (sym);
4522   if (!sym->attr.generic
4523       && !gfc_add_generic (&sym->attr, sym->name, NULL))
4524     return MATCH_ERROR;
4525 
4526   if (!sym->attr.function
4527       && !gfc_add_function (&sym->attr, sym->name, NULL))
4528     return MATCH_ERROR;
4529 
4530   if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4531       && dt_sym->attr.pdt_template
4532       && gfc_current_state () != COMP_DERIVED)
4533     {
4534       m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4535       if (m != MATCH_YES)
4536 	return m;
4537       gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4538     }
4539 
4540   if (!dt_sym)
4541     {
4542       gfc_interface *intr, *head;
4543 
4544       /* Use upper case to save the actual derived-type symbol.  */
4545       gfc_get_symbol (dt_name, NULL, &dt_sym);
4546       dt_sym->name = gfc_get_string ("%s", sym->name);
4547       head = sym->generic;
4548       intr = gfc_get_interface ();
4549       intr->sym = dt_sym;
4550       intr->where = gfc_current_locus;
4551       intr->next = head;
4552       sym->generic = intr;
4553       sym->attr.if_source = IFSRC_DECL;
4554     }
4555   else
4556     gfc_save_symbol_data (dt_sym);
4557 
4558   gfc_set_sym_referenced (dt_sym);
4559 
4560   if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4561       && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4562     return MATCH_ERROR;
4563 
4564   ts->u.derived = dt_sym;
4565 
4566   return MATCH_YES;
4567 
4568 get_kind:
4569   if (matched_type
4570       && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4571 			  "intrinsic-type-spec at %C"))
4572     return MATCH_ERROR;
4573 
4574   /* For all types except double, derived and character, look for an
4575      optional kind specifier.  MATCH_NO is actually OK at this point.  */
4576   if (implicit_flag == 1)
4577     {
4578 	if (matched_type && gfc_match_char (')') != MATCH_YES)
4579 	  return MATCH_ERROR;
4580 
4581 	return MATCH_YES;
4582     }
4583 
4584   if (gfc_current_form == FORM_FREE)
4585     {
4586       c = gfc_peek_ascii_char ();
4587       if (!gfc_is_whitespace (c) && c != '*' && c != '('
4588 	  && c != ':' && c != ',')
4589         {
4590 	  if (matched_type && c == ')')
4591 	    {
4592 	      gfc_next_ascii_char ();
4593 	      return MATCH_YES;
4594 	    }
4595 	  gfc_error ("Malformed type-spec at %C");
4596 	  return MATCH_NO;
4597 	}
4598     }
4599 
4600   m = gfc_match_kind_spec (ts, false);
4601   if (m == MATCH_NO && ts->type != BT_CHARACTER)
4602     {
4603       m = gfc_match_old_kind_spec (ts);
4604       if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4605          return MATCH_ERROR;
4606     }
4607 
4608   if (matched_type && gfc_match_char (')') != MATCH_YES)
4609     {
4610       gfc_error ("Malformed type-spec at %C");
4611       return MATCH_ERROR;
4612     }
4613 
4614   /* Defer association of the KIND expression of function results
4615      until after USE and IMPORT statements.  */
4616   if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4617 	 || gfc_matching_function)
4618     return MATCH_YES;
4619 
4620   if (m == MATCH_NO)
4621     m = MATCH_YES;		/* No kind specifier found.  */
4622 
4623   return m;
4624 }
4625 
4626 
4627 /* Match an IMPLICIT NONE statement.  Actually, this statement is
4628    already matched in parse.c, or we would not end up here in the
4629    first place.  So the only thing we need to check, is if there is
4630    trailing garbage.  If not, the match is successful.  */
4631 
4632 match
gfc_match_implicit_none(void)4633 gfc_match_implicit_none (void)
4634 {
4635   char c;
4636   match m;
4637   char name[GFC_MAX_SYMBOL_LEN + 1];
4638   bool type = false;
4639   bool external = false;
4640   locus cur_loc = gfc_current_locus;
4641 
4642   if (gfc_current_ns->seen_implicit_none
4643       || gfc_current_ns->has_implicit_none_export)
4644     {
4645       gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4646       return MATCH_ERROR;
4647     }
4648 
4649   gfc_gobble_whitespace ();
4650   c = gfc_peek_ascii_char ();
4651   if (c == '(')
4652     {
4653       (void) gfc_next_ascii_char ();
4654       if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
4655 	return MATCH_ERROR;
4656 
4657       gfc_gobble_whitespace ();
4658       if (gfc_peek_ascii_char () == ')')
4659 	{
4660 	  (void) gfc_next_ascii_char ();
4661 	  type = true;
4662 	}
4663       else
4664 	for(;;)
4665 	  {
4666 	    m = gfc_match (" %n", name);
4667 	    if (m != MATCH_YES)
4668 	      return MATCH_ERROR;
4669 
4670 	    if (strcmp (name, "type") == 0)
4671 	      type = true;
4672 	    else if (strcmp (name, "external") == 0)
4673 	      external = true;
4674 	    else
4675 	      return MATCH_ERROR;
4676 
4677 	    gfc_gobble_whitespace ();
4678 	    c = gfc_next_ascii_char ();
4679 	    if (c == ',')
4680 	      continue;
4681 	    if (c == ')')
4682 	      break;
4683 	    return MATCH_ERROR;
4684 	  }
4685     }
4686   else
4687     type = true;
4688 
4689   if (gfc_match_eos () != MATCH_YES)
4690     return MATCH_ERROR;
4691 
4692   gfc_set_implicit_none (type, external, &cur_loc);
4693 
4694   return MATCH_YES;
4695 }
4696 
4697 
4698 /* Match the letter range(s) of an IMPLICIT statement.  */
4699 
4700 static match
match_implicit_range(void)4701 match_implicit_range (void)
4702 {
4703   char c, c1, c2;
4704   int inner;
4705   locus cur_loc;
4706 
4707   cur_loc = gfc_current_locus;
4708 
4709   gfc_gobble_whitespace ();
4710   c = gfc_next_ascii_char ();
4711   if (c != '(')
4712     {
4713       gfc_error ("Missing character range in IMPLICIT at %C");
4714       goto bad;
4715     }
4716 
4717   inner = 1;
4718   while (inner)
4719     {
4720       gfc_gobble_whitespace ();
4721       c1 = gfc_next_ascii_char ();
4722       if (!ISALPHA (c1))
4723 	goto bad;
4724 
4725       gfc_gobble_whitespace ();
4726       c = gfc_next_ascii_char ();
4727 
4728       switch (c)
4729 	{
4730 	case ')':
4731 	  inner = 0;		/* Fall through.  */
4732 
4733 	case ',':
4734 	  c2 = c1;
4735 	  break;
4736 
4737 	case '-':
4738 	  gfc_gobble_whitespace ();
4739 	  c2 = gfc_next_ascii_char ();
4740 	  if (!ISALPHA (c2))
4741 	    goto bad;
4742 
4743 	  gfc_gobble_whitespace ();
4744 	  c = gfc_next_ascii_char ();
4745 
4746 	  if ((c != ',') && (c != ')'))
4747 	    goto bad;
4748 	  if (c == ')')
4749 	    inner = 0;
4750 
4751 	  break;
4752 
4753 	default:
4754 	  goto bad;
4755 	}
4756 
4757       if (c1 > c2)
4758 	{
4759 	  gfc_error ("Letters must be in alphabetic order in "
4760 		     "IMPLICIT statement at %C");
4761 	  goto bad;
4762 	}
4763 
4764       /* See if we can add the newly matched range to the pending
4765 	 implicits from this IMPLICIT statement.  We do not check for
4766 	 conflicts with whatever earlier IMPLICIT statements may have
4767 	 set.  This is done when we've successfully finished matching
4768 	 the current one.  */
4769       if (!gfc_add_new_implicit_range (c1, c2))
4770 	goto bad;
4771     }
4772 
4773   return MATCH_YES;
4774 
4775 bad:
4776   gfc_syntax_error (ST_IMPLICIT);
4777 
4778   gfc_current_locus = cur_loc;
4779   return MATCH_ERROR;
4780 }
4781 
4782 
4783 /* Match an IMPLICIT statement, storing the types for
4784    gfc_set_implicit() if the statement is accepted by the parser.
4785    There is a strange looking, but legal syntactic construction
4786    possible.  It looks like:
4787 
4788      IMPLICIT INTEGER (a-b) (c-d)
4789 
4790    This is legal if "a-b" is a constant expression that happens to
4791    equal one of the legal kinds for integers.  The real problem
4792    happens with an implicit specification that looks like:
4793 
4794      IMPLICIT INTEGER (a-b)
4795 
4796    In this case, a typespec matcher that is "greedy" (as most of the
4797    matchers are) gobbles the character range as a kindspec, leaving
4798    nothing left.  We therefore have to go a bit more slowly in the
4799    matching process by inhibiting the kindspec checking during
4800    typespec matching and checking for a kind later.  */
4801 
4802 match
gfc_match_implicit(void)4803 gfc_match_implicit (void)
4804 {
4805   gfc_typespec ts;
4806   locus cur_loc;
4807   char c;
4808   match m;
4809 
4810   if (gfc_current_ns->seen_implicit_none)
4811     {
4812       gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4813 		 "statement");
4814       return MATCH_ERROR;
4815     }
4816 
4817   gfc_clear_ts (&ts);
4818 
4819   /* We don't allow empty implicit statements.  */
4820   if (gfc_match_eos () == MATCH_YES)
4821     {
4822       gfc_error ("Empty IMPLICIT statement at %C");
4823       return MATCH_ERROR;
4824     }
4825 
4826   do
4827     {
4828       /* First cleanup.  */
4829       gfc_clear_new_implicit ();
4830 
4831       /* A basic type is mandatory here.  */
4832       m = gfc_match_decl_type_spec (&ts, 1);
4833       if (m == MATCH_ERROR)
4834 	goto error;
4835       if (m == MATCH_NO)
4836 	goto syntax;
4837 
4838       cur_loc = gfc_current_locus;
4839       m = match_implicit_range ();
4840 
4841       if (m == MATCH_YES)
4842 	{
4843 	  /* We may have <TYPE> (<RANGE>).  */
4844 	  gfc_gobble_whitespace ();
4845           c = gfc_peek_ascii_char ();
4846 	  if (c == ',' || c == '\n' || c == ';' || c == '!')
4847 	    {
4848 	      /* Check for CHARACTER with no length parameter.  */
4849 	      if (ts.type == BT_CHARACTER && !ts.u.cl)
4850 		{
4851 		  ts.kind = gfc_default_character_kind;
4852 		  ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4853 		  ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4854 						      NULL, 1);
4855 		}
4856 
4857 	      /* Record the Successful match.  */
4858 	      if (!gfc_merge_new_implicit (&ts))
4859 		return MATCH_ERROR;
4860 	      if (c == ',')
4861 		c = gfc_next_ascii_char ();
4862 	      else if (gfc_match_eos () == MATCH_ERROR)
4863 		goto error;
4864 	      continue;
4865 	    }
4866 
4867 	  gfc_current_locus = cur_loc;
4868 	}
4869 
4870       /* Discard the (incorrectly) matched range.  */
4871       gfc_clear_new_implicit ();
4872 
4873       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
4874       if (ts.type == BT_CHARACTER)
4875 	m = gfc_match_char_spec (&ts);
4876       else
4877 	{
4878 	  m = gfc_match_kind_spec (&ts, false);
4879 	  if (m == MATCH_NO)
4880 	    {
4881 	      m = gfc_match_old_kind_spec (&ts);
4882 	      if (m == MATCH_ERROR)
4883 		goto error;
4884 	      if (m == MATCH_NO)
4885 		goto syntax;
4886 	    }
4887 	}
4888       if (m == MATCH_ERROR)
4889 	goto error;
4890 
4891       m = match_implicit_range ();
4892       if (m == MATCH_ERROR)
4893 	goto error;
4894       if (m == MATCH_NO)
4895 	goto syntax;
4896 
4897       gfc_gobble_whitespace ();
4898       c = gfc_next_ascii_char ();
4899       if (c != ',' && gfc_match_eos () != MATCH_YES)
4900 	goto syntax;
4901 
4902       if (!gfc_merge_new_implicit (&ts))
4903 	return MATCH_ERROR;
4904     }
4905   while (c == ',');
4906 
4907   return MATCH_YES;
4908 
4909 syntax:
4910   gfc_syntax_error (ST_IMPLICIT);
4911 
4912 error:
4913   return MATCH_ERROR;
4914 }
4915 
4916 
4917 match
gfc_match_import(void)4918 gfc_match_import (void)
4919 {
4920   char name[GFC_MAX_SYMBOL_LEN + 1];
4921   match m;
4922   gfc_symbol *sym;
4923   gfc_symtree *st;
4924 
4925   if (gfc_current_ns->proc_name == NULL
4926       || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4927     {
4928       gfc_error ("IMPORT statement at %C only permitted in "
4929 		 "an INTERFACE body");
4930       return MATCH_ERROR;
4931     }
4932 
4933   if (gfc_current_ns->proc_name->attr.module_procedure)
4934     {
4935       gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4936 		 "in a module procedure interface body");
4937       return MATCH_ERROR;
4938     }
4939 
4940   if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4941     return MATCH_ERROR;
4942 
4943   if (gfc_match_eos () == MATCH_YES)
4944     {
4945       /* All host variables should be imported.  */
4946       gfc_current_ns->has_import_set = 1;
4947       return MATCH_YES;
4948     }
4949 
4950   if (gfc_match (" ::") == MATCH_YES)
4951     {
4952       if (gfc_match_eos () == MATCH_YES)
4953 	{
4954 	   gfc_error ("Expecting list of named entities at %C");
4955 	   return MATCH_ERROR;
4956 	}
4957     }
4958 
4959   for(;;)
4960     {
4961       sym = NULL;
4962       m = gfc_match (" %n", name);
4963       switch (m)
4964 	{
4965 	case MATCH_YES:
4966 	  if (gfc_current_ns->parent !=  NULL
4967 	      && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4968 	    {
4969 	       gfc_error ("Type name %qs at %C is ambiguous", name);
4970 	       return MATCH_ERROR;
4971 	    }
4972 	  else if (!sym && gfc_current_ns->proc_name->ns->parent !=  NULL
4973 		   && gfc_find_symbol (name,
4974 				       gfc_current_ns->proc_name->ns->parent,
4975 				       1, &sym))
4976 	    {
4977 	       gfc_error ("Type name %qs at %C is ambiguous", name);
4978 	       return MATCH_ERROR;
4979 	    }
4980 
4981 	  if (sym == NULL)
4982 	    {
4983 	      gfc_error ("Cannot IMPORT %qs from host scoping unit "
4984 			 "at %C - does not exist.", name);
4985 	      return MATCH_ERROR;
4986 	    }
4987 
4988 	  if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4989 	    {
4990 	      gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4991 			   "at %C", name);
4992 	      goto next_item;
4993 	    }
4994 
4995 	  st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4996 	  st->n.sym = sym;
4997 	  sym->refs++;
4998 	  sym->attr.imported = 1;
4999 
5000 	  if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
5001 	    {
5002 	      /* The actual derived type is stored in a symtree with the first
5003 		 letter of the name capitalized; the symtree with the all
5004 		 lower-case name contains the associated generic function.  */
5005 	      st = gfc_new_symtree (&gfc_current_ns->sym_root,
5006                                     gfc_dt_upper_string (name));
5007 	      st->n.sym = sym;
5008 	      sym->refs++;
5009 	      sym->attr.imported = 1;
5010 	    }
5011 
5012 	  goto next_item;
5013 
5014 	case MATCH_NO:
5015 	  break;
5016 
5017 	case MATCH_ERROR:
5018 	  return MATCH_ERROR;
5019 	}
5020 
5021     next_item:
5022       if (gfc_match_eos () == MATCH_YES)
5023 	break;
5024       if (gfc_match_char (',') != MATCH_YES)
5025 	goto syntax;
5026     }
5027 
5028   return MATCH_YES;
5029 
5030 syntax:
5031   gfc_error ("Syntax error in IMPORT statement at %C");
5032   return MATCH_ERROR;
5033 }
5034 
5035 
5036 /* A minimal implementation of gfc_match without whitespace, escape
5037    characters or variable arguments.  Returns true if the next
5038    characters match the TARGET template exactly.  */
5039 
5040 static bool
match_string_p(const char * target)5041 match_string_p (const char *target)
5042 {
5043   const char *p;
5044 
5045   for (p = target; *p; p++)
5046     if ((char) gfc_next_ascii_char () != *p)
5047       return false;
5048   return true;
5049 }
5050 
5051 /* Matches an attribute specification including array specs.  If
5052    successful, leaves the variables current_attr and current_as
5053    holding the specification.  Also sets the colon_seen variable for
5054    later use by matchers associated with initializations.
5055 
5056    This subroutine is a little tricky in the sense that we don't know
5057    if we really have an attr-spec until we hit the double colon.
5058    Until that time, we can only return MATCH_NO.  This forces us to
5059    check for duplicate specification at this level.  */
5060 
5061 static match
match_attr_spec(void)5062 match_attr_spec (void)
5063 {
5064   /* Modifiers that can exist in a type statement.  */
5065   enum
5066   { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
5067     DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
5068     DECL_DIMENSION, DECL_EXTERNAL,
5069     DECL_INTRINSIC, DECL_OPTIONAL,
5070     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
5071     DECL_STATIC, DECL_AUTOMATIC,
5072     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
5073     DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
5074     DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
5075   };
5076 
5077 /* GFC_DECL_END is the sentinel, index starts at 0.  */
5078 #define NUM_DECL GFC_DECL_END
5079 
5080   /* Make sure that values from sym_intent are safe to be used here.  */
5081   gcc_assert (INTENT_IN > 0);
5082 
5083   locus start, seen_at[NUM_DECL];
5084   int seen[NUM_DECL];
5085   unsigned int d;
5086   const char *attr;
5087   match m;
5088   bool t;
5089 
5090   gfc_clear_attr (&current_attr);
5091   start = gfc_current_locus;
5092 
5093   current_as = NULL;
5094   colon_seen = 0;
5095   attr_seen = 0;
5096 
5097   /* See if we get all of the keywords up to the final double colon.  */
5098   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5099     seen[d] = 0;
5100 
5101   for (;;)
5102     {
5103       char ch;
5104 
5105       d = DECL_NONE;
5106       gfc_gobble_whitespace ();
5107 
5108       ch = gfc_next_ascii_char ();
5109       if (ch == ':')
5110 	{
5111 	  /* This is the successful exit condition for the loop.  */
5112 	  if (gfc_next_ascii_char () == ':')
5113 	    break;
5114 	}
5115       else if (ch == ',')
5116 	{
5117 	  gfc_gobble_whitespace ();
5118 	  switch (gfc_peek_ascii_char ())
5119 	    {
5120 	    case 'a':
5121 	      gfc_next_ascii_char ();
5122 	      switch (gfc_next_ascii_char ())
5123 		{
5124 		case 'l':
5125 		  if (match_string_p ("locatable"))
5126 		    {
5127 		      /* Matched "allocatable".  */
5128 		      d = DECL_ALLOCATABLE;
5129 		    }
5130 		  break;
5131 
5132 		case 's':
5133 		  if (match_string_p ("ynchronous"))
5134 		    {
5135 		      /* Matched "asynchronous".  */
5136 		      d = DECL_ASYNCHRONOUS;
5137 		    }
5138 		  break;
5139 
5140 		case 'u':
5141 		  if (match_string_p ("tomatic"))
5142 		    {
5143 		      /* Matched "automatic".  */
5144 		      d = DECL_AUTOMATIC;
5145 		    }
5146 		  break;
5147 		}
5148 	      break;
5149 
5150 	    case 'b':
5151 	      /* Try and match the bind(c).  */
5152 	      m = gfc_match_bind_c (NULL, true);
5153 	      if (m == MATCH_YES)
5154 		d = DECL_IS_BIND_C;
5155 	      else if (m == MATCH_ERROR)
5156 		goto cleanup;
5157 	      break;
5158 
5159 	    case 'c':
5160 	      gfc_next_ascii_char ();
5161 	      if ('o' != gfc_next_ascii_char ())
5162 		break;
5163 	      switch (gfc_next_ascii_char ())
5164 		{
5165 		case 'd':
5166 		  if (match_string_p ("imension"))
5167 		    {
5168 		      d = DECL_CODIMENSION;
5169 		      break;
5170 		    }
5171 		  /* FALLTHRU */
5172 		case 'n':
5173 		  if (match_string_p ("tiguous"))
5174 		    {
5175 		      d = DECL_CONTIGUOUS;
5176 		      break;
5177 		    }
5178 		}
5179 	      break;
5180 
5181 	    case 'd':
5182 	      if (match_string_p ("dimension"))
5183 		d = DECL_DIMENSION;
5184 	      break;
5185 
5186 	    case 'e':
5187 	      if (match_string_p ("external"))
5188 		d = DECL_EXTERNAL;
5189 	      break;
5190 
5191 	    case 'i':
5192 	      if (match_string_p ("int"))
5193 		{
5194 		  ch = gfc_next_ascii_char ();
5195 		  if (ch == 'e')
5196 		    {
5197 		      if (match_string_p ("nt"))
5198 			{
5199 			  /* Matched "intent".  */
5200 			  d = match_intent_spec ();
5201 			  if (d == INTENT_UNKNOWN)
5202 			    {
5203 			      m = MATCH_ERROR;
5204 			      goto cleanup;
5205 			    }
5206 			}
5207 		    }
5208 		  else if (ch == 'r')
5209 		    {
5210 		      if (match_string_p ("insic"))
5211 			{
5212 			  /* Matched "intrinsic".  */
5213 			  d = DECL_INTRINSIC;
5214 			}
5215 		    }
5216 		}
5217 	      break;
5218 
5219 	    case 'k':
5220 	      if (match_string_p ("kind"))
5221 		d = DECL_KIND;
5222 	      break;
5223 
5224 	    case 'l':
5225 	      if (match_string_p ("len"))
5226 		d = DECL_LEN;
5227 	      break;
5228 
5229 	    case 'o':
5230 	      if (match_string_p ("optional"))
5231 		d = DECL_OPTIONAL;
5232 	      break;
5233 
5234 	    case 'p':
5235 	      gfc_next_ascii_char ();
5236 	      switch (gfc_next_ascii_char ())
5237 		{
5238 		case 'a':
5239 		  if (match_string_p ("rameter"))
5240 		    {
5241 		      /* Matched "parameter".  */
5242 		      d = DECL_PARAMETER;
5243 		    }
5244 		  break;
5245 
5246 		case 'o':
5247 		  if (match_string_p ("inter"))
5248 		    {
5249 		      /* Matched "pointer".  */
5250 		      d = DECL_POINTER;
5251 		    }
5252 		  break;
5253 
5254 		case 'r':
5255 		  ch = gfc_next_ascii_char ();
5256 		  if (ch == 'i')
5257 		    {
5258 		      if (match_string_p ("vate"))
5259 			{
5260 			  /* Matched "private".  */
5261 			  d = DECL_PRIVATE;
5262 			}
5263 		    }
5264 		  else if (ch == 'o')
5265 		    {
5266 		      if (match_string_p ("tected"))
5267 			{
5268 			  /* Matched "protected".  */
5269 			  d = DECL_PROTECTED;
5270 			}
5271 		    }
5272 		  break;
5273 
5274 		case 'u':
5275 		  if (match_string_p ("blic"))
5276 		    {
5277 		      /* Matched "public".  */
5278 		      d = DECL_PUBLIC;
5279 		    }
5280 		  break;
5281 		}
5282 	      break;
5283 
5284 	    case 's':
5285 	      gfc_next_ascii_char ();
5286 	      switch (gfc_next_ascii_char ())
5287 		{
5288 		  case 'a':
5289 		    if (match_string_p ("ve"))
5290 		      {
5291 			/* Matched "save".  */
5292 			d = DECL_SAVE;
5293 		      }
5294 		    break;
5295 
5296 		  case 't':
5297 		    if (match_string_p ("atic"))
5298 		      {
5299 			/* Matched "static".  */
5300 			d = DECL_STATIC;
5301 		      }
5302 		    break;
5303 		}
5304 	      break;
5305 
5306 	    case 't':
5307 	      if (match_string_p ("target"))
5308 		d = DECL_TARGET;
5309 	      break;
5310 
5311 	    case 'v':
5312 	      gfc_next_ascii_char ();
5313 	      ch = gfc_next_ascii_char ();
5314 	      if (ch == 'a')
5315 		{
5316 		  if (match_string_p ("lue"))
5317 		    {
5318 		      /* Matched "value".  */
5319 		      d = DECL_VALUE;
5320 		    }
5321 		}
5322 	      else if (ch == 'o')
5323 		{
5324 		  if (match_string_p ("latile"))
5325 		    {
5326 		      /* Matched "volatile".  */
5327 		      d = DECL_VOLATILE;
5328 		    }
5329 		}
5330 	      break;
5331 	    }
5332 	}
5333 
5334       /* No double colon and no recognizable decl_type, so assume that
5335 	 we've been looking at something else the whole time.  */
5336       if (d == DECL_NONE)
5337 	{
5338 	  m = MATCH_NO;
5339 	  goto cleanup;
5340 	}
5341 
5342       /* Check to make sure any parens are paired up correctly.  */
5343       if (gfc_match_parens () == MATCH_ERROR)
5344 	{
5345 	  m = MATCH_ERROR;
5346 	  goto cleanup;
5347 	}
5348 
5349       seen[d]++;
5350       seen_at[d] = gfc_current_locus;
5351 
5352       if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
5353 	{
5354 	  gfc_array_spec *as = NULL;
5355 
5356 	  m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5357 				    d == DECL_CODIMENSION);
5358 
5359 	  if (current_as == NULL)
5360 	    current_as = as;
5361 	  else if (m == MATCH_YES)
5362 	    {
5363 	      if (!merge_array_spec (as, current_as, false))
5364 		m = MATCH_ERROR;
5365 	      free (as);
5366 	    }
5367 
5368 	  if (m == MATCH_NO)
5369 	    {
5370 	      if (d == DECL_CODIMENSION)
5371 		gfc_error ("Missing codimension specification at %C");
5372 	      else
5373 		gfc_error ("Missing dimension specification at %C");
5374 	      m = MATCH_ERROR;
5375 	    }
5376 
5377 	  if (m == MATCH_ERROR)
5378 	    goto cleanup;
5379 	}
5380     }
5381 
5382   /* Since we've seen a double colon, we have to be looking at an
5383      attr-spec.  This means that we can now issue errors.  */
5384   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5385     if (seen[d] > 1)
5386       {
5387 	switch (d)
5388 	  {
5389 	  case DECL_ALLOCATABLE:
5390 	    attr = "ALLOCATABLE";
5391 	    break;
5392 	  case DECL_ASYNCHRONOUS:
5393 	    attr = "ASYNCHRONOUS";
5394 	    break;
5395 	  case DECL_CODIMENSION:
5396 	    attr = "CODIMENSION";
5397 	    break;
5398 	  case DECL_CONTIGUOUS:
5399 	    attr = "CONTIGUOUS";
5400 	    break;
5401 	  case DECL_DIMENSION:
5402 	    attr = "DIMENSION";
5403 	    break;
5404 	  case DECL_EXTERNAL:
5405 	    attr = "EXTERNAL";
5406 	    break;
5407 	  case DECL_IN:
5408 	    attr = "INTENT (IN)";
5409 	    break;
5410 	  case DECL_OUT:
5411 	    attr = "INTENT (OUT)";
5412 	    break;
5413 	  case DECL_INOUT:
5414 	    attr = "INTENT (IN OUT)";
5415 	    break;
5416 	  case DECL_INTRINSIC:
5417 	    attr = "INTRINSIC";
5418 	    break;
5419 	  case DECL_OPTIONAL:
5420 	    attr = "OPTIONAL";
5421 	    break;
5422 	  case DECL_KIND:
5423 	    attr = "KIND";
5424 	    break;
5425 	  case DECL_LEN:
5426 	    attr = "LEN";
5427 	    break;
5428 	  case DECL_PARAMETER:
5429 	    attr = "PARAMETER";
5430 	    break;
5431 	  case DECL_POINTER:
5432 	    attr = "POINTER";
5433 	    break;
5434 	  case DECL_PROTECTED:
5435 	    attr = "PROTECTED";
5436 	    break;
5437 	  case DECL_PRIVATE:
5438 	    attr = "PRIVATE";
5439 	    break;
5440 	  case DECL_PUBLIC:
5441 	    attr = "PUBLIC";
5442 	    break;
5443 	  case DECL_SAVE:
5444 	    attr = "SAVE";
5445 	    break;
5446 	  case DECL_STATIC:
5447 	    attr = "STATIC";
5448 	    break;
5449 	  case DECL_AUTOMATIC:
5450 	    attr = "AUTOMATIC";
5451 	    break;
5452 	  case DECL_TARGET:
5453 	    attr = "TARGET";
5454 	    break;
5455           case DECL_IS_BIND_C:
5456             attr = "IS_BIND_C";
5457             break;
5458           case DECL_VALUE:
5459             attr = "VALUE";
5460             break;
5461 	  case DECL_VOLATILE:
5462 	    attr = "VOLATILE";
5463 	    break;
5464 	  default:
5465 	    attr = NULL;	/* This shouldn't happen.  */
5466 	  }
5467 
5468 	gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5469 	m = MATCH_ERROR;
5470 	goto cleanup;
5471       }
5472 
5473   /* Now that we've dealt with duplicate attributes, add the attributes
5474      to the current attribute.  */
5475   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5476     {
5477       if (seen[d] == 0)
5478 	continue;
5479       else
5480         attr_seen = 1;
5481 
5482       if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5483 	  && !flag_dec_static)
5484 	{
5485 	  gfc_error ("%s at %L is a DEC extension, enable with "
5486 		     "%<-fdec-static%>",
5487 		     d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5488 	  m = MATCH_ERROR;
5489 	  goto cleanup;
5490 	}
5491       /* Allow SAVE with STATIC, but don't complain.  */
5492       if (d == DECL_STATIC && seen[DECL_SAVE])
5493 	continue;
5494 
5495       if (gfc_comp_struct (gfc_current_state ())
5496 	  && d != DECL_DIMENSION && d != DECL_CODIMENSION
5497 	  && d != DECL_POINTER   && d != DECL_PRIVATE
5498 	  && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
5499 	{
5500 	  bool is_derived = gfc_current_state () == COMP_DERIVED;
5501 	  if (d == DECL_ALLOCATABLE)
5502 	    {
5503 	      if (!gfc_notify_std (GFC_STD_F2003, is_derived
5504 				   ? G_("ALLOCATABLE attribute at %C in a "
5505 					"TYPE definition")
5506 				   : G_("ALLOCATABLE attribute at %C in a "
5507 					"STRUCTURE definition")))
5508 		{
5509 		  m = MATCH_ERROR;
5510 		  goto cleanup;
5511 		}
5512 	    }
5513 	  else if (d == DECL_KIND)
5514 	    {
5515 	      if (!gfc_notify_std (GFC_STD_F2003, is_derived
5516 				   ? G_("KIND attribute at %C in a "
5517 					"TYPE definition")
5518 				   : G_("KIND attribute at %C in a "
5519 					"STRUCTURE definition")))
5520 		{
5521 		  m = MATCH_ERROR;
5522 		  goto cleanup;
5523 		}
5524 	      if (current_ts.type != BT_INTEGER)
5525 		{
5526 		  gfc_error ("Component with KIND attribute at %C must be "
5527 			     "INTEGER");
5528 		  m = MATCH_ERROR;
5529 		  goto cleanup;
5530 		}
5531 	      if (current_ts.kind != gfc_default_integer_kind)
5532 		{
5533 		  gfc_error ("Component with KIND attribute at %C must be "
5534 			     "default integer kind (%d)",
5535 			      gfc_default_integer_kind);
5536 		  m = MATCH_ERROR;
5537 		  goto cleanup;
5538 		}
5539 	    }
5540 	  else if (d == DECL_LEN)
5541 	    {
5542 	      if (!gfc_notify_std (GFC_STD_F2003, is_derived
5543 				   ? G_("LEN attribute at %C in a "
5544 					"TYPE definition")
5545 				   : G_("LEN attribute at %C in a "
5546 					"STRUCTURE definition")))
5547 		{
5548 		  m = MATCH_ERROR;
5549 		  goto cleanup;
5550 		}
5551 	      if (current_ts.type != BT_INTEGER)
5552 		{
5553 		  gfc_error ("Component with LEN attribute at %C must be "
5554 			     "INTEGER");
5555 		  m = MATCH_ERROR;
5556 		  goto cleanup;
5557 		}
5558 	      if (current_ts.kind != gfc_default_integer_kind)
5559 		{
5560 		  gfc_error ("Component with LEN attribute at %C must be "
5561 			     "default integer kind (%d)",
5562 			      gfc_default_integer_kind);
5563 		  m = MATCH_ERROR;
5564 		  goto cleanup;
5565 		}
5566 	    }
5567 	  else
5568 	    {
5569 	      gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
5570 					 "TYPE definition")
5571 				    : G_("Attribute at %L is not allowed in a "
5572 					 "STRUCTURE definition"), &seen_at[d]);
5573 	      m = MATCH_ERROR;
5574 	      goto cleanup;
5575 	    }
5576 	}
5577 
5578       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5579 	  && gfc_current_state () != COMP_MODULE)
5580 	{
5581 	  if (d == DECL_PRIVATE)
5582 	    attr = "PRIVATE";
5583 	  else
5584 	    attr = "PUBLIC";
5585 	  if (gfc_current_state () == COMP_DERIVED
5586 	      && gfc_state_stack->previous
5587 	      && gfc_state_stack->previous->state == COMP_MODULE)
5588 	    {
5589 	      if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5590 				   "at %L in a TYPE definition", attr,
5591 				   &seen_at[d]))
5592 		{
5593 		  m = MATCH_ERROR;
5594 		  goto cleanup;
5595 		}
5596 	    }
5597 	  else
5598 	    {
5599 	      gfc_error ("%s attribute at %L is not allowed outside of the "
5600 			 "specification part of a module", attr, &seen_at[d]);
5601 	      m = MATCH_ERROR;
5602 	      goto cleanup;
5603 	    }
5604 	}
5605 
5606       if (gfc_current_state () != COMP_DERIVED
5607 	  && (d == DECL_KIND || d == DECL_LEN))
5608 	{
5609 	  gfc_error ("Attribute at %L is not allowed outside a TYPE "
5610 		     "definition", &seen_at[d]);
5611 	  m = MATCH_ERROR;
5612 	  goto cleanup;
5613 	}
5614 
5615       switch (d)
5616 	{
5617 	case DECL_ALLOCATABLE:
5618 	  t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5619 	  break;
5620 
5621 	case DECL_ASYNCHRONOUS:
5622 	  if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5623 	    t = false;
5624 	  else
5625 	    t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5626 	  break;
5627 
5628 	case DECL_CODIMENSION:
5629 	  t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5630 	  break;
5631 
5632 	case DECL_CONTIGUOUS:
5633 	  if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5634 	    t = false;
5635 	  else
5636 	    t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5637 	  break;
5638 
5639 	case DECL_DIMENSION:
5640 	  t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5641 	  break;
5642 
5643 	case DECL_EXTERNAL:
5644 	  t = gfc_add_external (&current_attr, &seen_at[d]);
5645 	  break;
5646 
5647 	case DECL_IN:
5648 	  t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5649 	  break;
5650 
5651 	case DECL_OUT:
5652 	  t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5653 	  break;
5654 
5655 	case DECL_INOUT:
5656 	  t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5657 	  break;
5658 
5659 	case DECL_INTRINSIC:
5660 	  t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5661 	  break;
5662 
5663 	case DECL_OPTIONAL:
5664 	  t = gfc_add_optional (&current_attr, &seen_at[d]);
5665 	  break;
5666 
5667 	case DECL_KIND:
5668 	  t = gfc_add_kind (&current_attr, &seen_at[d]);
5669 	  break;
5670 
5671 	case DECL_LEN:
5672 	  t = gfc_add_len (&current_attr, &seen_at[d]);
5673 	  break;
5674 
5675 	case DECL_PARAMETER:
5676 	  t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5677 	  break;
5678 
5679 	case DECL_POINTER:
5680 	  t = gfc_add_pointer (&current_attr, &seen_at[d]);
5681 	  break;
5682 
5683 	case DECL_PROTECTED:
5684 	  if (gfc_current_state () != COMP_MODULE
5685 	      || (gfc_current_ns->proc_name
5686 		  && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5687 	    {
5688 	       gfc_error ("PROTECTED at %C only allowed in specification "
5689 			  "part of a module");
5690 	       t = false;
5691 	       break;
5692 	    }
5693 
5694 	  if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5695 	    t = false;
5696 	  else
5697 	    t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5698 	  break;
5699 
5700 	case DECL_PRIVATE:
5701 	  t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5702 			      &seen_at[d]);
5703 	  break;
5704 
5705 	case DECL_PUBLIC:
5706 	  t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5707 			      &seen_at[d]);
5708 	  break;
5709 
5710 	case DECL_STATIC:
5711 	case DECL_SAVE:
5712 	  t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5713 	  break;
5714 
5715 	case DECL_AUTOMATIC:
5716 	  t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5717 	  break;
5718 
5719 	case DECL_TARGET:
5720 	  t = gfc_add_target (&current_attr, &seen_at[d]);
5721 	  break;
5722 
5723         case DECL_IS_BIND_C:
5724            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5725            break;
5726 
5727 	case DECL_VALUE:
5728 	  if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5729 	    t = false;
5730 	  else
5731 	    t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5732 	  break;
5733 
5734 	case DECL_VOLATILE:
5735 	  if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5736 	    t = false;
5737 	  else
5738 	    t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5739 	  break;
5740 
5741 	default:
5742 	  gfc_internal_error ("match_attr_spec(): Bad attribute");
5743 	}
5744 
5745       if (!t)
5746 	{
5747 	  m = MATCH_ERROR;
5748 	  goto cleanup;
5749 	}
5750     }
5751 
5752   /* Since Fortran 2008 module variables implicitly have the SAVE attribute.  */
5753   if ((gfc_current_state () == COMP_MODULE
5754        || gfc_current_state () == COMP_SUBMODULE)
5755       && !current_attr.save
5756       && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5757     current_attr.save = SAVE_IMPLICIT;
5758 
5759   colon_seen = 1;
5760   return MATCH_YES;
5761 
5762 cleanup:
5763   gfc_current_locus = start;
5764   gfc_free_array_spec (current_as);
5765   current_as = NULL;
5766   attr_seen = 0;
5767   return m;
5768 }
5769 
5770 
5771 /* Set the binding label, dest_label, either with the binding label
5772    stored in the given gfc_typespec, ts, or if none was provided, it
5773    will be the symbol name in all lower case, as required by the draft
5774    (J3/04-007, section 15.4.1).  If a binding label was given and
5775    there is more than one argument (num_idents), it is an error.  */
5776 
5777 static bool
set_binding_label(const char ** dest_label,const char * sym_name,int num_idents)5778 set_binding_label (const char **dest_label, const char *sym_name,
5779 		   int num_idents)
5780 {
5781   if (num_idents > 1 && has_name_equals)
5782     {
5783       gfc_error ("Multiple identifiers provided with "
5784 		 "single NAME= specifier at %C");
5785       return false;
5786     }
5787 
5788   if (curr_binding_label)
5789     /* Binding label given; store in temp holder till have sym.  */
5790     *dest_label = curr_binding_label;
5791   else
5792     {
5793       /* No binding label given, and the NAME= specifier did not exist,
5794          which means there was no NAME="".  */
5795       if (sym_name != NULL && has_name_equals == 0)
5796         *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5797     }
5798 
5799   return true;
5800 }
5801 
5802 
5803 /* Set the status of the given common block as being BIND(C) or not,
5804    depending on the given parameter, is_bind_c.  */
5805 
5806 void
set_com_block_bind_c(gfc_common_head * com_block,int is_bind_c)5807 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5808 {
5809   com_block->is_bind_c = is_bind_c;
5810   return;
5811 }
5812 
5813 
5814 /* Verify that the given gfc_typespec is for a C interoperable type.  */
5815 
5816 bool
gfc_verify_c_interop(gfc_typespec * ts)5817 gfc_verify_c_interop (gfc_typespec *ts)
5818 {
5819   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5820     return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5821 	   ? true : false;
5822   else if (ts->type == BT_CLASS)
5823     return false;
5824   else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5825     return false;
5826 
5827   return true;
5828 }
5829 
5830 
5831 /* Verify that the variables of a given common block, which has been
5832    defined with the attribute specifier bind(c), to be of a C
5833    interoperable type.  Errors will be reported here, if
5834    encountered.  */
5835 
5836 bool
verify_com_block_vars_c_interop(gfc_common_head * com_block)5837 verify_com_block_vars_c_interop (gfc_common_head *com_block)
5838 {
5839   gfc_symbol *curr_sym = NULL;
5840   bool retval = true;
5841 
5842   curr_sym = com_block->head;
5843 
5844   /* Make sure we have at least one symbol.  */
5845   if (curr_sym == NULL)
5846     return retval;
5847 
5848   /* Here we know we have a symbol, so we'll execute this loop
5849      at least once.  */
5850   do
5851     {
5852       /* The second to last param, 1, says this is in a common block.  */
5853       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5854       curr_sym = curr_sym->common_next;
5855     } while (curr_sym != NULL);
5856 
5857   return retval;
5858 }
5859 
5860 
5861 /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
5862    an appropriate error message is reported.  */
5863 
5864 bool
verify_bind_c_sym(gfc_symbol * tmp_sym,gfc_typespec * ts,int is_in_common,gfc_common_head * com_block)5865 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5866                    int is_in_common, gfc_common_head *com_block)
5867 {
5868   bool bind_c_function = false;
5869   bool retval = true;
5870 
5871   if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5872     bind_c_function = true;
5873 
5874   if (tmp_sym->attr.function && tmp_sym->result != NULL)
5875     {
5876       tmp_sym = tmp_sym->result;
5877       /* Make sure it wasn't an implicitly typed result.  */
5878       if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5879 	{
5880 	  gfc_warning (OPT_Wc_binding_type,
5881 		       "Implicitly declared BIND(C) function %qs at "
5882                        "%L may not be C interoperable", tmp_sym->name,
5883                        &tmp_sym->declared_at);
5884 	  tmp_sym->ts.f90_type = tmp_sym->ts.type;
5885 	  /* Mark it as C interoperable to prevent duplicate warnings.	*/
5886 	  tmp_sym->ts.is_c_interop = 1;
5887 	  tmp_sym->attr.is_c_interop = 1;
5888 	}
5889     }
5890 
5891   /* Here, we know we have the bind(c) attribute, so if we have
5892      enough type info, then verify that it's a C interop kind.
5893      The info could be in the symbol already, or possibly still in
5894      the given ts (current_ts), so look in both.  */
5895   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5896     {
5897       if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5898 	{
5899 	  /* See if we're dealing with a sym in a common block or not.	*/
5900 	  if (is_in_common == 1 && warn_c_binding_type)
5901 	    {
5902 	      gfc_warning (OPT_Wc_binding_type,
5903 			   "Variable %qs in common block %qs at %L "
5904                            "may not be a C interoperable "
5905                            "kind though common block %qs is BIND(C)",
5906                            tmp_sym->name, com_block->name,
5907                            &(tmp_sym->declared_at), com_block->name);
5908 	    }
5909 	  else
5910 	    {
5911               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5912                 gfc_error ("Type declaration %qs at %L is not C "
5913                            "interoperable but it is BIND(C)",
5914                            tmp_sym->name, &(tmp_sym->declared_at));
5915               else if (warn_c_binding_type)
5916                 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5917                              "may not be a C interoperable "
5918                              "kind but it is BIND(C)",
5919                              tmp_sym->name, &(tmp_sym->declared_at));
5920 	    }
5921 	}
5922 
5923       /* Variables declared w/in a common block can't be bind(c)
5924 	 since there's no way for C to see these variables, so there's
5925 	 semantically no reason for the attribute.  */
5926       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5927 	{
5928 	  gfc_error ("Variable %qs in common block %qs at "
5929 		     "%L cannot be declared with BIND(C) "
5930 		     "since it is not a global",
5931 		     tmp_sym->name, com_block->name,
5932 		     &(tmp_sym->declared_at));
5933 	  retval = false;
5934 	}
5935 
5936       /* Scalar variables that are bind(c) cannot have the pointer
5937 	 or allocatable attributes.  */
5938       if (tmp_sym->attr.is_bind_c == 1)
5939 	{
5940 	  if (tmp_sym->attr.pointer == 1)
5941 	    {
5942 	      gfc_error ("Variable %qs at %L cannot have both the "
5943 			 "POINTER and BIND(C) attributes",
5944 			 tmp_sym->name, &(tmp_sym->declared_at));
5945 	      retval = false;
5946 	    }
5947 
5948 	  if (tmp_sym->attr.allocatable == 1)
5949 	    {
5950 	      gfc_error ("Variable %qs at %L cannot have both the "
5951 			 "ALLOCATABLE and BIND(C) attributes",
5952 			 tmp_sym->name, &(tmp_sym->declared_at));
5953 	      retval = false;
5954 	    }
5955 
5956         }
5957 
5958       /* If it is a BIND(C) function, make sure the return value is a
5959 	 scalar value.  The previous tests in this function made sure
5960 	 the type is interoperable.  */
5961       if (bind_c_function && tmp_sym->as != NULL)
5962 	gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5963 		   "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5964 
5965       /* BIND(C) functions cannot return a character string.  */
5966       if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5967 	if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5968 	    || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5969 	    || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5970 	  gfc_error ("Return type of BIND(C) function %qs of character "
5971 		     "type at %L must have length 1", tmp_sym->name,
5972 			 &(tmp_sym->declared_at));
5973     }
5974 
5975   /* See if the symbol has been marked as private.  If it has, make sure
5976      there is no binding label and warn the user if there is one.  */
5977   if (tmp_sym->attr.access == ACCESS_PRIVATE
5978       && tmp_sym->binding_label)
5979       /* Use gfc_warning_now because we won't say that the symbol fails
5980 	 just because of this.	*/
5981       gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5982 		       "given the binding label %qs", tmp_sym->name,
5983 		       &(tmp_sym->declared_at), tmp_sym->binding_label);
5984 
5985   return retval;
5986 }
5987 
5988 
5989 /* Set the appropriate fields for a symbol that's been declared as
5990    BIND(C) (the is_bind_c flag and the binding label), and verify that
5991    the type is C interoperable.  Errors are reported by the functions
5992    used to set/test these fields.  */
5993 
5994 bool
set_verify_bind_c_sym(gfc_symbol * tmp_sym,int num_idents)5995 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5996 {
5997   bool retval = true;
5998 
5999   /* TODO: Do we need to make sure the vars aren't marked private?  */
6000 
6001   /* Set the is_bind_c bit in symbol_attribute.  */
6002   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
6003 
6004   if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
6005     return false;
6006 
6007   return retval;
6008 }
6009 
6010 
6011 /* Set the fields marking the given common block as BIND(C), including
6012    a binding label, and report any errors encountered.  */
6013 
6014 bool
set_verify_bind_c_com_block(gfc_common_head * com_block,int num_idents)6015 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
6016 {
6017   bool retval = true;
6018 
6019   /* destLabel, common name, typespec (which may have binding label).  */
6020   if (!set_binding_label (&com_block->binding_label, com_block->name,
6021 			  num_idents))
6022     return false;
6023 
6024   /* Set the given common block (com_block) to being bind(c) (1).  */
6025   set_com_block_bind_c (com_block, 1);
6026 
6027   return retval;
6028 }
6029 
6030 
6031 /* Retrieve the list of one or more identifiers that the given bind(c)
6032    attribute applies to.  */
6033 
6034 bool
get_bind_c_idents(void)6035 get_bind_c_idents (void)
6036 {
6037   char name[GFC_MAX_SYMBOL_LEN + 1];
6038   int num_idents = 0;
6039   gfc_symbol *tmp_sym = NULL;
6040   match found_id;
6041   gfc_common_head *com_block = NULL;
6042 
6043   if (gfc_match_name (name) == MATCH_YES)
6044     {
6045       found_id = MATCH_YES;
6046       gfc_get_ha_symbol (name, &tmp_sym);
6047     }
6048   else if (match_common_name (name) == MATCH_YES)
6049     {
6050       found_id = MATCH_YES;
6051       com_block = gfc_get_common (name, 0);
6052     }
6053   else
6054     {
6055       gfc_error ("Need either entity or common block name for "
6056 		 "attribute specification statement at %C");
6057       return false;
6058     }
6059 
6060   /* Save the current identifier and look for more.  */
6061   do
6062     {
6063       /* Increment the number of identifiers found for this spec stmt.  */
6064       num_idents++;
6065 
6066       /* Make sure we have a sym or com block, and verify that it can
6067 	 be bind(c).  Set the appropriate field(s) and look for more
6068 	 identifiers.  */
6069       if (tmp_sym != NULL || com_block != NULL)
6070         {
6071 	  if (tmp_sym != NULL)
6072 	    {
6073 	      if (!set_verify_bind_c_sym (tmp_sym, num_idents))
6074 		return false;
6075 	    }
6076 	  else
6077 	    {
6078 	      if (!set_verify_bind_c_com_block (com_block, num_idents))
6079 		return false;
6080 	    }
6081 
6082 	  /* Look to see if we have another identifier.  */
6083 	  tmp_sym = NULL;
6084 	  if (gfc_match_eos () == MATCH_YES)
6085 	    found_id = MATCH_NO;
6086 	  else if (gfc_match_char (',') != MATCH_YES)
6087 	    found_id = MATCH_NO;
6088 	  else if (gfc_match_name (name) == MATCH_YES)
6089 	    {
6090 	      found_id = MATCH_YES;
6091 	      gfc_get_ha_symbol (name, &tmp_sym);
6092 	    }
6093 	  else if (match_common_name (name) == MATCH_YES)
6094 	    {
6095 	      found_id = MATCH_YES;
6096 	      com_block = gfc_get_common (name, 0);
6097 	    }
6098 	  else
6099 	    {
6100 	      gfc_error ("Missing entity or common block name for "
6101 			 "attribute specification statement at %C");
6102 	      return false;
6103 	    }
6104 	}
6105       else
6106 	{
6107 	  gfc_internal_error ("Missing symbol");
6108 	}
6109     } while (found_id == MATCH_YES);
6110 
6111   /* if we get here we were successful */
6112   return true;
6113 }
6114 
6115 
6116 /* Try and match a BIND(C) attribute specification statement.  */
6117 
6118 match
gfc_match_bind_c_stmt(void)6119 gfc_match_bind_c_stmt (void)
6120 {
6121   match found_match = MATCH_NO;
6122   gfc_typespec *ts;
6123 
6124   ts = &current_ts;
6125 
6126   /* This may not be necessary.  */
6127   gfc_clear_ts (ts);
6128   /* Clear the temporary binding label holder.  */
6129   curr_binding_label = NULL;
6130 
6131   /* Look for the bind(c).  */
6132   found_match = gfc_match_bind_c (NULL, true);
6133 
6134   if (found_match == MATCH_YES)
6135     {
6136       if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
6137 	return MATCH_ERROR;
6138 
6139       /* Look for the :: now, but it is not required.  */
6140       gfc_match (" :: ");
6141 
6142       /* Get the identifier(s) that needs to be updated.  This may need to
6143 	 change to hand the flag(s) for the attr specified so all identifiers
6144 	 found can have all appropriate parts updated (assuming that the same
6145 	 spec stmt can have multiple attrs, such as both bind(c) and
6146 	 allocatable...).  */
6147       if (!get_bind_c_idents ())
6148 	/* Error message should have printed already.  */
6149 	return MATCH_ERROR;
6150     }
6151 
6152   return found_match;
6153 }
6154 
6155 
6156 /* Match a data declaration statement.  */
6157 
6158 match
gfc_match_data_decl(void)6159 gfc_match_data_decl (void)
6160 {
6161   gfc_symbol *sym;
6162   match m;
6163   int elem;
6164 
6165   type_param_spec_list = NULL;
6166   decl_type_param_list = NULL;
6167 
6168   num_idents_on_line = 0;
6169 
6170   m = gfc_match_decl_type_spec (&current_ts, 0);
6171   if (m != MATCH_YES)
6172     return m;
6173 
6174   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6175 	&& !gfc_comp_struct (gfc_current_state ()))
6176     {
6177       sym = gfc_use_derived (current_ts.u.derived);
6178 
6179       if (sym == NULL)
6180 	{
6181 	  m = MATCH_ERROR;
6182 	  goto cleanup;
6183 	}
6184 
6185       current_ts.u.derived = sym;
6186     }
6187 
6188   m = match_attr_spec ();
6189   if (m == MATCH_ERROR)
6190     {
6191       m = MATCH_NO;
6192       goto cleanup;
6193     }
6194 
6195   /* F2018:C708.  */
6196   if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
6197     {
6198       gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
6199       m = MATCH_ERROR;
6200       goto cleanup;
6201     }
6202 
6203   if (current_ts.type == BT_CLASS
6204 	&& current_ts.u.derived->attr.unlimited_polymorphic)
6205     goto ok;
6206 
6207   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6208       && current_ts.u.derived->components == NULL
6209       && !current_ts.u.derived->attr.zero_comp)
6210     {
6211 
6212       if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
6213 	goto ok;
6214 
6215       if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
6216 	goto ok;
6217 
6218       gfc_find_symbol (current_ts.u.derived->name,
6219 		       current_ts.u.derived->ns, 1, &sym);
6220 
6221       /* Any symbol that we find had better be a type definition
6222 	 which has its components defined, or be a structure definition
6223          actively being parsed.  */
6224       if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
6225 	  && (current_ts.u.derived->components != NULL
6226 	      || current_ts.u.derived->attr.zero_comp
6227 	      || current_ts.u.derived == gfc_new_block))
6228 	goto ok;
6229 
6230       gfc_error ("Derived type at %C has not been previously defined "
6231 		 "and so cannot appear in a derived type definition");
6232       m = MATCH_ERROR;
6233       goto cleanup;
6234     }
6235 
6236 ok:
6237   /* If we have an old-style character declaration, and no new-style
6238      attribute specifications, then there a comma is optional between
6239      the type specification and the variable list.  */
6240   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
6241     gfc_match_char (',');
6242 
6243   /* Give the types/attributes to symbols that follow. Give the element
6244      a number so that repeat character length expressions can be copied.  */
6245   elem = 1;
6246   for (;;)
6247     {
6248       num_idents_on_line++;
6249       m = variable_decl (elem++);
6250       if (m == MATCH_ERROR)
6251 	goto cleanup;
6252       if (m == MATCH_NO)
6253 	break;
6254 
6255       if (gfc_match_eos () == MATCH_YES)
6256 	goto cleanup;
6257       if (gfc_match_char (',') != MATCH_YES)
6258 	break;
6259     }
6260 
6261   if (!gfc_error_flag_test ())
6262     {
6263       /* An anonymous structure declaration is unambiguous; if we matched one
6264 	 according to gfc_match_structure_decl, we need to return MATCH_YES
6265 	 here to avoid confusing the remaining matchers, even if there was an
6266 	 error during variable_decl.  We must flush any such errors.  Note this
6267 	 causes the parser to gracefully continue parsing the remaining input
6268 	 as a structure body, which likely follows.  */
6269       if (current_ts.type == BT_DERIVED && current_ts.u.derived
6270 	  && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6271 	{
6272 	  gfc_error_now ("Syntax error in anonymous structure declaration"
6273 			 " at %C");
6274 	  /* Skip the bad variable_decl and line up for the start of the
6275 	     structure body.  */
6276 	  gfc_error_recovery ();
6277 	  m = MATCH_YES;
6278 	  goto cleanup;
6279 	}
6280 
6281       gfc_error ("Syntax error in data declaration at %C");
6282     }
6283 
6284   m = MATCH_ERROR;
6285 
6286   gfc_free_data_all (gfc_current_ns);
6287 
6288 cleanup:
6289   if (saved_kind_expr)
6290     gfc_free_expr (saved_kind_expr);
6291   if (type_param_spec_list)
6292     gfc_free_actual_arglist (type_param_spec_list);
6293   if (decl_type_param_list)
6294     gfc_free_actual_arglist (decl_type_param_list);
6295   saved_kind_expr = NULL;
6296   gfc_free_array_spec (current_as);
6297   current_as = NULL;
6298   return m;
6299 }
6300 
6301 static bool
in_module_or_interface(void)6302 in_module_or_interface(void)
6303 {
6304   if (gfc_current_state () == COMP_MODULE
6305       || gfc_current_state () == COMP_SUBMODULE
6306       || gfc_current_state () == COMP_INTERFACE)
6307     return true;
6308 
6309   if (gfc_state_stack->state == COMP_CONTAINS
6310       || gfc_state_stack->state == COMP_FUNCTION
6311       || gfc_state_stack->state == COMP_SUBROUTINE)
6312     {
6313       gfc_state_data *p;
6314       for (p = gfc_state_stack->previous; p ; p = p->previous)
6315 	{
6316 	  if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
6317 	      || p->state == COMP_INTERFACE)
6318 	    return true;
6319 	}
6320     }
6321     return false;
6322 }
6323 
6324 /* Match a prefix associated with a function or subroutine
6325    declaration.  If the typespec pointer is nonnull, then a typespec
6326    can be matched.  Note that if nothing matches, MATCH_YES is
6327    returned (the null string was matched).  */
6328 
6329 match
gfc_match_prefix(gfc_typespec * ts)6330 gfc_match_prefix (gfc_typespec *ts)
6331 {
6332   bool seen_type;
6333   bool seen_impure;
6334   bool found_prefix;
6335 
6336   gfc_clear_attr (&current_attr);
6337   seen_type = false;
6338   seen_impure = false;
6339 
6340   gcc_assert (!gfc_matching_prefix);
6341   gfc_matching_prefix = true;
6342 
6343   do
6344     {
6345       found_prefix = false;
6346 
6347       /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6348 	 corresponding attribute seems natural and distinguishes these
6349 	 procedures from procedure types of PROC_MODULE, which these are
6350 	 as well.  */
6351       if (gfc_match ("module% ") == MATCH_YES)
6352 	{
6353 	  if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6354 	    goto error;
6355 
6356 	  if (!in_module_or_interface ())
6357 	    {
6358 	      gfc_error ("MODULE prefix at %C found outside of a module, "
6359 			 "submodule, or interface");
6360 	      goto error;
6361 	    }
6362 
6363 	  current_attr.module_procedure = 1;
6364 	  found_prefix = true;
6365 	}
6366 
6367       if (!seen_type && ts != NULL)
6368 	{
6369 	  match m;
6370 	  m = gfc_match_decl_type_spec (ts, 0);
6371 	  if (m == MATCH_ERROR)
6372 	    goto error;
6373 	  if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
6374 	    {
6375 	      seen_type = true;
6376 	      found_prefix = true;
6377 	    }
6378 	}
6379 
6380       if (gfc_match ("elemental% ") == MATCH_YES)
6381 	{
6382 	  if (!gfc_add_elemental (&current_attr, NULL))
6383 	    goto error;
6384 
6385 	  found_prefix = true;
6386 	}
6387 
6388       if (gfc_match ("pure% ") == MATCH_YES)
6389 	{
6390 	  if (!gfc_add_pure (&current_attr, NULL))
6391 	    goto error;
6392 
6393 	  found_prefix = true;
6394 	}
6395 
6396       if (gfc_match ("recursive% ") == MATCH_YES)
6397 	{
6398 	  if (!gfc_add_recursive (&current_attr, NULL))
6399 	    goto error;
6400 
6401 	  found_prefix = true;
6402 	}
6403 
6404       /* IMPURE is a somewhat special case, as it needs not set an actual
6405 	 attribute but rather only prevents ELEMENTAL routines from being
6406 	 automatically PURE.  */
6407       if (gfc_match ("impure% ") == MATCH_YES)
6408 	{
6409 	  if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
6410 	    goto error;
6411 
6412 	  seen_impure = true;
6413 	  found_prefix = true;
6414 	}
6415     }
6416   while (found_prefix);
6417 
6418   /* IMPURE and PURE must not both appear, of course.  */
6419   if (seen_impure && current_attr.pure)
6420     {
6421       gfc_error ("PURE and IMPURE must not appear both at %C");
6422       goto error;
6423     }
6424 
6425   /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE.  */
6426   if (!seen_impure && current_attr.elemental && !current_attr.pure)
6427     {
6428       if (!gfc_add_pure (&current_attr, NULL))
6429 	goto error;
6430     }
6431 
6432   /* At this point, the next item is not a prefix.  */
6433   gcc_assert (gfc_matching_prefix);
6434 
6435   gfc_matching_prefix = false;
6436   return MATCH_YES;
6437 
6438 error:
6439   gcc_assert (gfc_matching_prefix);
6440   gfc_matching_prefix = false;
6441   return MATCH_ERROR;
6442 }
6443 
6444 
6445 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
6446 
6447 static bool
copy_prefix(symbol_attribute * dest,locus * where)6448 copy_prefix (symbol_attribute *dest, locus *where)
6449 {
6450   if (dest->module_procedure)
6451     {
6452       if (current_attr.elemental)
6453 	dest->elemental = 1;
6454 
6455       if (current_attr.pure)
6456 	dest->pure = 1;
6457 
6458       if (current_attr.recursive)
6459 	dest->recursive = 1;
6460 
6461       /* Module procedures are unusual in that the 'dest' is copied from
6462 	 the interface declaration. However, this is an oportunity to
6463 	 check that the submodule declaration is compliant with the
6464 	 interface.  */
6465       if (dest->elemental && !current_attr.elemental)
6466 	{
6467 	  gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6468 		     "missing at %L", where);
6469 	  return false;
6470 	}
6471 
6472       if (dest->pure && !current_attr.pure)
6473 	{
6474 	  gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6475 		     "missing at %L", where);
6476 	  return false;
6477 	}
6478 
6479       if (dest->recursive && !current_attr.recursive)
6480 	{
6481 	  gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6482 		     "missing at %L", where);
6483 	  return false;
6484 	}
6485 
6486       return true;
6487     }
6488 
6489   if (current_attr.elemental && !gfc_add_elemental (dest, where))
6490     return false;
6491 
6492   if (current_attr.pure && !gfc_add_pure (dest, where))
6493     return false;
6494 
6495   if (current_attr.recursive && !gfc_add_recursive (dest, where))
6496     return false;
6497 
6498   return true;
6499 }
6500 
6501 
6502 /* Match a formal argument list or, if typeparam is true, a
6503    type_param_name_list.  */
6504 
6505 match
gfc_match_formal_arglist(gfc_symbol * progname,int st_flag,int null_flag,bool typeparam)6506 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6507 			  int null_flag, bool typeparam)
6508 {
6509   gfc_formal_arglist *head, *tail, *p, *q;
6510   char name[GFC_MAX_SYMBOL_LEN + 1];
6511   gfc_symbol *sym;
6512   match m;
6513   gfc_formal_arglist *formal = NULL;
6514 
6515   head = tail = NULL;
6516 
6517   /* Keep the interface formal argument list and null it so that the
6518      matching for the new declaration can be done.  The numbers and
6519      names of the arguments are checked here. The interface formal
6520      arguments are retained in formal_arglist and the characteristics
6521      are compared in resolve.c(resolve_fl_procedure).  See the remark
6522      in get_proc_name about the eventual need to copy the formal_arglist
6523      and populate the formal namespace of the interface symbol.  */
6524   if (progname->attr.module_procedure
6525       && progname->attr.host_assoc)
6526     {
6527       formal = progname->formal;
6528       progname->formal = NULL;
6529     }
6530 
6531   if (gfc_match_char ('(') != MATCH_YES)
6532     {
6533       if (null_flag)
6534 	goto ok;
6535       return MATCH_NO;
6536     }
6537 
6538   if (gfc_match_char (')') == MATCH_YES)
6539   {
6540     if (typeparam)
6541       {
6542 	gfc_error_now ("A type parameter list is required at %C");
6543 	m = MATCH_ERROR;
6544 	goto cleanup;
6545       }
6546     else
6547       goto ok;
6548   }
6549 
6550   for (;;)
6551     {
6552       if (gfc_match_char ('*') == MATCH_YES)
6553 	{
6554 	  sym = NULL;
6555 	  if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6556 			     "Alternate-return argument at %C"))
6557 	    {
6558 	      m = MATCH_ERROR;
6559 	      goto cleanup;
6560 	    }
6561 	  else if (typeparam)
6562 	    gfc_error_now ("A parameter name is required at %C");
6563 	}
6564       else
6565 	{
6566 	  m = gfc_match_name (name);
6567 	  if (m != MATCH_YES)
6568 	    {
6569 	      if(typeparam)
6570 		gfc_error_now ("A parameter name is required at %C");
6571 	      goto cleanup;
6572 	    }
6573 
6574 	  if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6575 	    goto cleanup;
6576 	  else if (typeparam
6577 		   && gfc_get_symbol (name, progname->f2k_derived, &sym))
6578 	    goto cleanup;
6579 	}
6580 
6581       p = gfc_get_formal_arglist ();
6582 
6583       if (head == NULL)
6584 	head = tail = p;
6585       else
6586 	{
6587 	  tail->next = p;
6588 	  tail = p;
6589 	}
6590 
6591       tail->sym = sym;
6592 
6593       /* We don't add the VARIABLE flavor because the name could be a
6594 	 dummy procedure.  We don't apply these attributes to formal
6595 	 arguments of statement functions.  */
6596       if (sym != NULL && !st_flag
6597 	  && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6598 	      || !gfc_missing_attr (&sym->attr, NULL)))
6599 	{
6600 	  m = MATCH_ERROR;
6601 	  goto cleanup;
6602 	}
6603 
6604       /* The name of a program unit can be in a different namespace,
6605 	 so check for it explicitly.  After the statement is accepted,
6606 	 the name is checked for especially in gfc_get_symbol().  */
6607       if (gfc_new_block != NULL && sym != NULL && !typeparam
6608 	  && strcmp (sym->name, gfc_new_block->name) == 0)
6609 	{
6610 	  gfc_error ("Name %qs at %C is the name of the procedure",
6611 		     sym->name);
6612 	  m = MATCH_ERROR;
6613 	  goto cleanup;
6614 	}
6615 
6616       if (gfc_match_char (')') == MATCH_YES)
6617 	goto ok;
6618 
6619       m = gfc_match_char (',');
6620       if (m != MATCH_YES)
6621 	{
6622 	  if (typeparam)
6623 	    gfc_error_now ("Expected parameter list in type declaration "
6624 			   "at %C");
6625 	  else
6626 	    gfc_error ("Unexpected junk in formal argument list at %C");
6627 	  goto cleanup;
6628 	}
6629     }
6630 
6631 ok:
6632   /* Check for duplicate symbols in the formal argument list.  */
6633   if (head != NULL)
6634     {
6635       for (p = head; p->next; p = p->next)
6636 	{
6637 	  if (p->sym == NULL)
6638 	    continue;
6639 
6640 	  for (q = p->next; q; q = q->next)
6641 	    if (p->sym == q->sym)
6642 	      {
6643 		if (typeparam)
6644 		  gfc_error_now ("Duplicate name %qs in parameter "
6645 				 "list at %C", p->sym->name);
6646 		else
6647 		  gfc_error ("Duplicate symbol %qs in formal argument "
6648 			     "list at %C", p->sym->name);
6649 
6650 		m = MATCH_ERROR;
6651 		goto cleanup;
6652 	      }
6653 	}
6654     }
6655 
6656   if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6657     {
6658       m = MATCH_ERROR;
6659       goto cleanup;
6660     }
6661 
6662   /* gfc_error_now used in following and return with MATCH_YES because
6663      doing otherwise results in a cascade of extraneous errors and in
6664      some cases an ICE in symbol.c(gfc_release_symbol).  */
6665   if (progname->attr.module_procedure && progname->attr.host_assoc)
6666     {
6667       bool arg_count_mismatch = false;
6668 
6669       if (!formal && head)
6670 	arg_count_mismatch = true;
6671 
6672       /* Abbreviated module procedure declaration is not meant to have any
6673 	 formal arguments!  */
6674       if (!progname->abr_modproc_decl && formal && !head)
6675 	arg_count_mismatch = true;
6676 
6677       for (p = formal, q = head; p && q; p = p->next, q = q->next)
6678 	{
6679 	  if ((p->next != NULL && q->next == NULL)
6680 	      || (p->next == NULL && q->next != NULL))
6681 	    arg_count_mismatch = true;
6682 	  else if ((p->sym == NULL && q->sym == NULL)
6683 		    || strcmp (p->sym->name, q->sym->name) == 0)
6684 	    continue;
6685 	  else
6686 	    gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6687 			   "argument names (%s/%s) at %C",
6688 			   p->sym->name, q->sym->name);
6689 	}
6690 
6691       if (arg_count_mismatch)
6692 	gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6693 		       "formal arguments at %C");
6694     }
6695 
6696   return MATCH_YES;
6697 
6698 cleanup:
6699   gfc_free_formal_arglist (head);
6700   return m;
6701 }
6702 
6703 
6704 /* Match a RESULT specification following a function declaration or
6705    ENTRY statement.  Also matches the end-of-statement.  */
6706 
6707 static match
match_result(gfc_symbol * function,gfc_symbol ** result)6708 match_result (gfc_symbol *function, gfc_symbol **result)
6709 {
6710   char name[GFC_MAX_SYMBOL_LEN + 1];
6711   gfc_symbol *r;
6712   match m;
6713 
6714   if (gfc_match (" result (") != MATCH_YES)
6715     return MATCH_NO;
6716 
6717   m = gfc_match_name (name);
6718   if (m != MATCH_YES)
6719     return m;
6720 
6721   /* Get the right paren, and that's it because there could be the
6722      bind(c) attribute after the result clause.  */
6723   if (gfc_match_char (')') != MATCH_YES)
6724     {
6725      /* TODO: should report the missing right paren here.  */
6726       return MATCH_ERROR;
6727     }
6728 
6729   if (strcmp (function->name, name) == 0)
6730     {
6731       gfc_error ("RESULT variable at %C must be different than function name");
6732       return MATCH_ERROR;
6733     }
6734 
6735   if (gfc_get_symbol (name, NULL, &r))
6736     return MATCH_ERROR;
6737 
6738   if (!gfc_add_result (&r->attr, r->name, NULL))
6739     return MATCH_ERROR;
6740 
6741   *result = r;
6742 
6743   return MATCH_YES;
6744 }
6745 
6746 
6747 /* Match a function suffix, which could be a combination of a result
6748    clause and BIND(C), either one, or neither.  The draft does not
6749    require them to come in a specific order.  */
6750 
6751 match
gfc_match_suffix(gfc_symbol * sym,gfc_symbol ** result)6752 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6753 {
6754   match is_bind_c;   /* Found bind(c).  */
6755   match is_result;   /* Found result clause.  */
6756   match found_match; /* Status of whether we've found a good match.  */
6757   char peek_char;    /* Character we're going to peek at.  */
6758   bool allow_binding_name;
6759 
6760   /* Initialize to having found nothing.  */
6761   found_match = MATCH_NO;
6762   is_bind_c = MATCH_NO;
6763   is_result = MATCH_NO;
6764 
6765   /* Get the next char to narrow between result and bind(c).  */
6766   gfc_gobble_whitespace ();
6767   peek_char = gfc_peek_ascii_char ();
6768 
6769   /* C binding names are not allowed for internal procedures.  */
6770   if (gfc_current_state () == COMP_CONTAINS
6771       && sym->ns->proc_name->attr.flavor != FL_MODULE)
6772     allow_binding_name = false;
6773   else
6774     allow_binding_name = true;
6775 
6776   switch (peek_char)
6777     {
6778     case 'r':
6779       /* Look for result clause.  */
6780       is_result = match_result (sym, result);
6781       if (is_result == MATCH_YES)
6782 	{
6783 	  /* Now see if there is a bind(c) after it.  */
6784 	  is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6785 	  /* We've found the result clause and possibly bind(c).  */
6786 	  found_match = MATCH_YES;
6787 	}
6788       else
6789 	/* This should only be MATCH_ERROR.  */
6790 	found_match = is_result;
6791       break;
6792     case 'b':
6793       /* Look for bind(c) first.  */
6794       is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6795       if (is_bind_c == MATCH_YES)
6796 	{
6797 	  /* Now see if a result clause followed it.  */
6798 	  is_result = match_result (sym, result);
6799 	  found_match = MATCH_YES;
6800 	}
6801       else
6802 	{
6803 	  /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
6804 	  found_match = MATCH_ERROR;
6805 	}
6806       break;
6807     default:
6808       gfc_error ("Unexpected junk after function declaration at %C");
6809       found_match = MATCH_ERROR;
6810       break;
6811     }
6812 
6813   if (is_bind_c == MATCH_YES)
6814     {
6815       /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
6816       if (gfc_current_state () == COMP_CONTAINS
6817 	  && sym->ns->proc_name->attr.flavor != FL_MODULE
6818 	  && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6819 			      "at %L may not be specified for an internal "
6820 			      "procedure", &gfc_current_locus))
6821 	return MATCH_ERROR;
6822 
6823       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6824      	return MATCH_ERROR;
6825     }
6826 
6827   return found_match;
6828 }
6829 
6830 
6831 /* Procedure pointer return value without RESULT statement:
6832    Add "hidden" result variable named "ppr@".  */
6833 
6834 static bool
add_hidden_procptr_result(gfc_symbol * sym)6835 add_hidden_procptr_result (gfc_symbol *sym)
6836 {
6837   bool case1,case2;
6838 
6839   if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6840     return false;
6841 
6842   /* First usage case: PROCEDURE and EXTERNAL statements.  */
6843   case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6844 	  && strcmp (gfc_current_block ()->name, sym->name) == 0
6845 	  && sym->attr.external;
6846   /* Second usage case: INTERFACE statements.  */
6847   case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6848 	  && gfc_state_stack->previous->state == COMP_FUNCTION
6849 	  && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6850 
6851   if (case1 || case2)
6852     {
6853       gfc_symtree *stree;
6854       if (case1)
6855 	gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6856       else
6857 	{
6858 	  gfc_symtree *st2;
6859 	  gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6860 	  st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6861 	  st2->n.sym = stree->n.sym;
6862 	  stree->n.sym->refs++;
6863 	}
6864       sym->result = stree->n.sym;
6865 
6866       sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6867       sym->result->attr.pointer = sym->attr.pointer;
6868       sym->result->attr.external = sym->attr.external;
6869       sym->result->attr.referenced = sym->attr.referenced;
6870       sym->result->ts = sym->ts;
6871       sym->attr.proc_pointer = 0;
6872       sym->attr.pointer = 0;
6873       sym->attr.external = 0;
6874       if (sym->result->attr.external && sym->result->attr.pointer)
6875 	{
6876 	  sym->result->attr.pointer = 0;
6877 	  sym->result->attr.proc_pointer = 1;
6878 	}
6879 
6880       return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6881     }
6882   /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
6883   else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6884 	   && sym->result && sym->result != sym && sym->result->attr.external
6885 	   && sym == gfc_current_ns->proc_name
6886 	   && sym == sym->result->ns->proc_name
6887 	   && strcmp ("ppr@", sym->result->name) == 0)
6888     {
6889       sym->result->attr.proc_pointer = 1;
6890       sym->attr.pointer = 0;
6891       return true;
6892     }
6893   else
6894     return false;
6895 }
6896 
6897 
6898 /* Match the interface for a PROCEDURE declaration,
6899    including brackets (R1212).  */
6900 
6901 static match
match_procedure_interface(gfc_symbol ** proc_if)6902 match_procedure_interface (gfc_symbol **proc_if)
6903 {
6904   match m;
6905   gfc_symtree *st;
6906   locus old_loc, entry_loc;
6907   gfc_namespace *old_ns = gfc_current_ns;
6908   char name[GFC_MAX_SYMBOL_LEN + 1];
6909 
6910   old_loc = entry_loc = gfc_current_locus;
6911   gfc_clear_ts (&current_ts);
6912 
6913   if (gfc_match (" (") != MATCH_YES)
6914     {
6915       gfc_current_locus = entry_loc;
6916       return MATCH_NO;
6917     }
6918 
6919   /* Get the type spec. for the procedure interface.  */
6920   old_loc = gfc_current_locus;
6921   m = gfc_match_decl_type_spec (&current_ts, 0);
6922   gfc_gobble_whitespace ();
6923   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6924     goto got_ts;
6925 
6926   if (m == MATCH_ERROR)
6927     return m;
6928 
6929   /* Procedure interface is itself a procedure.  */
6930   gfc_current_locus = old_loc;
6931   m = gfc_match_name (name);
6932 
6933   /* First look to see if it is already accessible in the current
6934      namespace because it is use associated or contained.  */
6935   st = NULL;
6936   if (gfc_find_sym_tree (name, NULL, 0, &st))
6937     return MATCH_ERROR;
6938 
6939   /* If it is still not found, then try the parent namespace, if it
6940      exists and create the symbol there if it is still not found.  */
6941   if (gfc_current_ns->parent)
6942     gfc_current_ns = gfc_current_ns->parent;
6943   if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6944     return MATCH_ERROR;
6945 
6946   gfc_current_ns = old_ns;
6947   *proc_if = st->n.sym;
6948 
6949   if (*proc_if)
6950     {
6951       (*proc_if)->refs++;
6952       /* Resolve interface if possible. That way, attr.procedure is only set
6953 	 if it is declared by a later procedure-declaration-stmt, which is
6954 	 invalid per F08:C1216 (cf. resolve_procedure_interface).  */
6955       while ((*proc_if)->ts.interface
6956 	     && *proc_if != (*proc_if)->ts.interface)
6957 	*proc_if = (*proc_if)->ts.interface;
6958 
6959       if ((*proc_if)->attr.flavor == FL_UNKNOWN
6960 	  && (*proc_if)->ts.type == BT_UNKNOWN
6961 	  && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6962 			      (*proc_if)->name, NULL))
6963 	return MATCH_ERROR;
6964     }
6965 
6966 got_ts:
6967   if (gfc_match (" )") != MATCH_YES)
6968     {
6969       gfc_current_locus = entry_loc;
6970       return MATCH_NO;
6971     }
6972 
6973   return MATCH_YES;
6974 }
6975 
6976 
6977 /* Match a PROCEDURE declaration (R1211).  */
6978 
6979 static match
match_procedure_decl(void)6980 match_procedure_decl (void)
6981 {
6982   match m;
6983   gfc_symbol *sym, *proc_if = NULL;
6984   int num;
6985   gfc_expr *initializer = NULL;
6986 
6987   /* Parse interface (with brackets).  */
6988   m = match_procedure_interface (&proc_if);
6989   if (m != MATCH_YES)
6990     return m;
6991 
6992   /* Parse attributes (with colons).  */
6993   m = match_attr_spec();
6994   if (m == MATCH_ERROR)
6995     return MATCH_ERROR;
6996 
6997   if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6998     {
6999       current_attr.is_bind_c = 1;
7000       has_name_equals = 0;
7001       curr_binding_label = NULL;
7002     }
7003 
7004   /* Get procedure symbols.  */
7005   for(num=1;;num++)
7006     {
7007       m = gfc_match_symbol (&sym, 0);
7008       if (m == MATCH_NO)
7009 	goto syntax;
7010       else if (m == MATCH_ERROR)
7011 	return m;
7012 
7013       /* Add current_attr to the symbol attributes.  */
7014       if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
7015 	return MATCH_ERROR;
7016 
7017       if (sym->attr.is_bind_c)
7018 	{
7019 	  /* Check for C1218.  */
7020 	  if (!proc_if || !proc_if->attr.is_bind_c)
7021 	    {
7022 	      gfc_error ("BIND(C) attribute at %C requires "
7023 			"an interface with BIND(C)");
7024 	      return MATCH_ERROR;
7025 	    }
7026 	  /* Check for C1217.  */
7027 	  if (has_name_equals && sym->attr.pointer)
7028 	    {
7029 	      gfc_error ("BIND(C) procedure with NAME may not have "
7030 			"POINTER attribute at %C");
7031 	      return MATCH_ERROR;
7032 	    }
7033 	  if (has_name_equals && sym->attr.dummy)
7034 	    {
7035 	      gfc_error ("Dummy procedure at %C may not have "
7036 			"BIND(C) attribute with NAME");
7037 	      return MATCH_ERROR;
7038 	    }
7039 	  /* Set binding label for BIND(C).  */
7040 	  if (!set_binding_label (&sym->binding_label, sym->name, num))
7041 	    return MATCH_ERROR;
7042 	}
7043 
7044       if (!gfc_add_external (&sym->attr, NULL))
7045 	return MATCH_ERROR;
7046 
7047       if (add_hidden_procptr_result (sym))
7048 	sym = sym->result;
7049 
7050       if (!gfc_add_proc (&sym->attr, sym->name, NULL))
7051 	return MATCH_ERROR;
7052 
7053       /* Set interface.  */
7054       if (proc_if != NULL)
7055 	{
7056           if (sym->ts.type != BT_UNKNOWN)
7057 	    {
7058 	      gfc_error ("Procedure %qs at %L already has basic type of %s",
7059 			 sym->name, &gfc_current_locus,
7060 			 gfc_basic_typename (sym->ts.type));
7061 	      return MATCH_ERROR;
7062 	    }
7063 	  sym->ts.interface = proc_if;
7064 	  sym->attr.untyped = 1;
7065 	  sym->attr.if_source = IFSRC_IFBODY;
7066 	}
7067       else if (current_ts.type != BT_UNKNOWN)
7068 	{
7069 	  if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
7070 	    return MATCH_ERROR;
7071 	  sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7072 	  sym->ts.interface->ts = current_ts;
7073 	  sym->ts.interface->attr.flavor = FL_PROCEDURE;
7074 	  sym->ts.interface->attr.function = 1;
7075 	  sym->attr.function = 1;
7076 	  sym->attr.if_source = IFSRC_UNKNOWN;
7077 	}
7078 
7079       if (gfc_match (" =>") == MATCH_YES)
7080 	{
7081 	  if (!current_attr.pointer)
7082 	    {
7083 	      gfc_error ("Initialization at %C isn't for a pointer variable");
7084 	      m = MATCH_ERROR;
7085 	      goto cleanup;
7086 	    }
7087 
7088 	  m = match_pointer_init (&initializer, 1);
7089 	  if (m != MATCH_YES)
7090 	    goto cleanup;
7091 
7092 	  if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
7093 	    goto cleanup;
7094 
7095 	}
7096 
7097       if (gfc_match_eos () == MATCH_YES)
7098 	return MATCH_YES;
7099       if (gfc_match_char (',') != MATCH_YES)
7100 	goto syntax;
7101     }
7102 
7103 syntax:
7104   gfc_error ("Syntax error in PROCEDURE statement at %C");
7105   return MATCH_ERROR;
7106 
7107 cleanup:
7108   /* Free stuff up and return.  */
7109   gfc_free_expr (initializer);
7110   return m;
7111 }
7112 
7113 
7114 static match
7115 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
7116 
7117 
7118 /* Match a procedure pointer component declaration (R445).  */
7119 
7120 static match
match_ppc_decl(void)7121 match_ppc_decl (void)
7122 {
7123   match m;
7124   gfc_symbol *proc_if = NULL;
7125   gfc_typespec ts;
7126   int num;
7127   gfc_component *c;
7128   gfc_expr *initializer = NULL;
7129   gfc_typebound_proc* tb;
7130   char name[GFC_MAX_SYMBOL_LEN + 1];
7131 
7132   /* Parse interface (with brackets).  */
7133   m = match_procedure_interface (&proc_if);
7134   if (m != MATCH_YES)
7135     goto syntax;
7136 
7137   /* Parse attributes.  */
7138   tb = XCNEW (gfc_typebound_proc);
7139   tb->where = gfc_current_locus;
7140   m = match_binding_attributes (tb, false, true);
7141   if (m == MATCH_ERROR)
7142     return m;
7143 
7144   gfc_clear_attr (&current_attr);
7145   current_attr.procedure = 1;
7146   current_attr.proc_pointer = 1;
7147   current_attr.access = tb->access;
7148   current_attr.flavor = FL_PROCEDURE;
7149 
7150   /* Match the colons (required).  */
7151   if (gfc_match (" ::") != MATCH_YES)
7152     {
7153       gfc_error ("Expected %<::%> after binding-attributes at %C");
7154       return MATCH_ERROR;
7155     }
7156 
7157   /* Check for C450.  */
7158   if (!tb->nopass && proc_if == NULL)
7159     {
7160       gfc_error("NOPASS or explicit interface required at %C");
7161       return MATCH_ERROR;
7162     }
7163 
7164   if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
7165     return MATCH_ERROR;
7166 
7167   /* Match PPC names.  */
7168   ts = current_ts;
7169   for(num=1;;num++)
7170     {
7171       m = gfc_match_name (name);
7172       if (m == MATCH_NO)
7173 	goto syntax;
7174       else if (m == MATCH_ERROR)
7175 	return m;
7176 
7177       if (!gfc_add_component (gfc_current_block(), name, &c))
7178 	return MATCH_ERROR;
7179 
7180       /* Add current_attr to the symbol attributes.  */
7181       if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
7182 	return MATCH_ERROR;
7183 
7184       if (!gfc_add_external (&c->attr, NULL))
7185 	return MATCH_ERROR;
7186 
7187       if (!gfc_add_proc (&c->attr, name, NULL))
7188 	return MATCH_ERROR;
7189 
7190       if (num == 1)
7191 	c->tb = tb;
7192       else
7193 	{
7194 	  c->tb = XCNEW (gfc_typebound_proc);
7195 	  c->tb->where = gfc_current_locus;
7196 	  *c->tb = *tb;
7197 	}
7198 
7199       /* Set interface.  */
7200       if (proc_if != NULL)
7201 	{
7202 	  c->ts.interface = proc_if;
7203 	  c->attr.untyped = 1;
7204 	  c->attr.if_source = IFSRC_IFBODY;
7205 	}
7206       else if (ts.type != BT_UNKNOWN)
7207 	{
7208 	  c->ts = ts;
7209 	  c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7210 	  c->ts.interface->result = c->ts.interface;
7211 	  c->ts.interface->ts = ts;
7212 	  c->ts.interface->attr.flavor = FL_PROCEDURE;
7213 	  c->ts.interface->attr.function = 1;
7214 	  c->attr.function = 1;
7215 	  c->attr.if_source = IFSRC_UNKNOWN;
7216 	}
7217 
7218       if (gfc_match (" =>") == MATCH_YES)
7219 	{
7220 	  m = match_pointer_init (&initializer, 1);
7221 	  if (m != MATCH_YES)
7222 	    {
7223 	      gfc_free_expr (initializer);
7224 	      return m;
7225 	    }
7226 	  c->initializer = initializer;
7227 	}
7228 
7229       if (gfc_match_eos () == MATCH_YES)
7230 	return MATCH_YES;
7231       if (gfc_match_char (',') != MATCH_YES)
7232 	goto syntax;
7233     }
7234 
7235 syntax:
7236   gfc_error ("Syntax error in procedure pointer component at %C");
7237   return MATCH_ERROR;
7238 }
7239 
7240 
7241 /* Match a PROCEDURE declaration inside an interface (R1206).  */
7242 
7243 static match
match_procedure_in_interface(void)7244 match_procedure_in_interface (void)
7245 {
7246   match m;
7247   gfc_symbol *sym;
7248   char name[GFC_MAX_SYMBOL_LEN + 1];
7249   locus old_locus;
7250 
7251   if (current_interface.type == INTERFACE_NAMELESS
7252       || current_interface.type == INTERFACE_ABSTRACT)
7253     {
7254       gfc_error ("PROCEDURE at %C must be in a generic interface");
7255       return MATCH_ERROR;
7256     }
7257 
7258   /* Check if the F2008 optional double colon appears.  */
7259   gfc_gobble_whitespace ();
7260   old_locus = gfc_current_locus;
7261   if (gfc_match ("::") == MATCH_YES)
7262     {
7263       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7264 			   "MODULE PROCEDURE statement at %L", &old_locus))
7265 	return MATCH_ERROR;
7266     }
7267   else
7268     gfc_current_locus = old_locus;
7269 
7270   for(;;)
7271     {
7272       m = gfc_match_name (name);
7273       if (m == MATCH_NO)
7274 	goto syntax;
7275       else if (m == MATCH_ERROR)
7276 	return m;
7277       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
7278 	return MATCH_ERROR;
7279 
7280       if (!gfc_add_interface (sym))
7281 	return MATCH_ERROR;
7282 
7283       if (gfc_match_eos () == MATCH_YES)
7284 	break;
7285       if (gfc_match_char (',') != MATCH_YES)
7286 	goto syntax;
7287     }
7288 
7289   return MATCH_YES;
7290 
7291 syntax:
7292   gfc_error ("Syntax error in PROCEDURE statement at %C");
7293   return MATCH_ERROR;
7294 }
7295 
7296 
7297 /* General matcher for PROCEDURE declarations.  */
7298 
7299 static match match_procedure_in_type (void);
7300 
7301 match
gfc_match_procedure(void)7302 gfc_match_procedure (void)
7303 {
7304   match m;
7305 
7306   switch (gfc_current_state ())
7307     {
7308     case COMP_NONE:
7309     case COMP_PROGRAM:
7310     case COMP_MODULE:
7311     case COMP_SUBMODULE:
7312     case COMP_SUBROUTINE:
7313     case COMP_FUNCTION:
7314     case COMP_BLOCK:
7315       m = match_procedure_decl ();
7316       break;
7317     case COMP_INTERFACE:
7318       m = match_procedure_in_interface ();
7319       break;
7320     case COMP_DERIVED:
7321       m = match_ppc_decl ();
7322       break;
7323     case COMP_DERIVED_CONTAINS:
7324       m = match_procedure_in_type ();
7325       break;
7326     default:
7327       return MATCH_NO;
7328     }
7329 
7330   if (m != MATCH_YES)
7331     return m;
7332 
7333   if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
7334     return MATCH_ERROR;
7335 
7336   return m;
7337 }
7338 
7339 
7340 /* Warn if a matched procedure has the same name as an intrinsic; this is
7341    simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7342    parser-state-stack to find out whether we're in a module.  */
7343 
7344 static void
do_warn_intrinsic_shadow(const gfc_symbol * sym,bool func)7345 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
7346 {
7347   bool in_module;
7348 
7349   in_module = (gfc_state_stack->previous
7350 	       && (gfc_state_stack->previous->state == COMP_MODULE
7351 		   || gfc_state_stack->previous->state == COMP_SUBMODULE));
7352 
7353   gfc_warn_intrinsic_shadow (sym, in_module, func);
7354 }
7355 
7356 
7357 /* Match a function declaration.  */
7358 
7359 match
gfc_match_function_decl(void)7360 gfc_match_function_decl (void)
7361 {
7362   char name[GFC_MAX_SYMBOL_LEN + 1];
7363   gfc_symbol *sym, *result;
7364   locus old_loc;
7365   match m;
7366   match suffix_match;
7367   match found_match; /* Status returned by match func.  */
7368 
7369   if (gfc_current_state () != COMP_NONE
7370       && gfc_current_state () != COMP_INTERFACE
7371       && gfc_current_state () != COMP_CONTAINS)
7372     return MATCH_NO;
7373 
7374   gfc_clear_ts (&current_ts);
7375 
7376   old_loc = gfc_current_locus;
7377 
7378   m = gfc_match_prefix (&current_ts);
7379   if (m != MATCH_YES)
7380     {
7381       gfc_current_locus = old_loc;
7382       return m;
7383     }
7384 
7385   if (gfc_match ("function% %n", name) != MATCH_YES)
7386     {
7387       gfc_current_locus = old_loc;
7388       return MATCH_NO;
7389     }
7390 
7391   if (get_proc_name (name, &sym, false))
7392     return MATCH_ERROR;
7393 
7394   if (add_hidden_procptr_result (sym))
7395     sym = sym->result;
7396 
7397   if (current_attr.module_procedure)
7398     sym->attr.module_procedure = 1;
7399 
7400   gfc_new_block = sym;
7401 
7402   m = gfc_match_formal_arglist (sym, 0, 0);
7403   if (m == MATCH_NO)
7404     {
7405       gfc_error ("Expected formal argument list in function "
7406 		 "definition at %C");
7407       m = MATCH_ERROR;
7408       goto cleanup;
7409     }
7410   else if (m == MATCH_ERROR)
7411     goto cleanup;
7412 
7413   result = NULL;
7414 
7415   /* According to the draft, the bind(c) and result clause can
7416      come in either order after the formal_arg_list (i.e., either
7417      can be first, both can exist together or by themselves or neither
7418      one).  Therefore, the match_result can't match the end of the
7419      string, and check for the bind(c) or result clause in either order.  */
7420   found_match = gfc_match_eos ();
7421 
7422   /* Make sure that it isn't already declared as BIND(C).  If it is, it
7423      must have been marked BIND(C) with a BIND(C) attribute and that is
7424      not allowed for procedures.  */
7425   if (sym->attr.is_bind_c == 1)
7426     {
7427       sym->attr.is_bind_c = 0;
7428 
7429       if (gfc_state_stack->previous
7430 	  && gfc_state_stack->previous->state != COMP_SUBMODULE)
7431 	{
7432 	  locus loc;
7433 	  loc = sym->old_symbol != NULL
7434 	    ? sym->old_symbol->declared_at : gfc_current_locus;
7435 	  gfc_error_now ("BIND(C) attribute at %L can only be used for "
7436 			 "variables or common blocks", &loc);
7437 	}
7438     }
7439 
7440   if (found_match != MATCH_YES)
7441     {
7442       /* If we haven't found the end-of-statement, look for a suffix.  */
7443       suffix_match = gfc_match_suffix (sym, &result);
7444       if (suffix_match == MATCH_YES)
7445         /* Need to get the eos now.  */
7446         found_match = gfc_match_eos ();
7447       else
7448 	found_match = suffix_match;
7449     }
7450 
7451   /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
7452      subprogram and a binding label is specified, it shall be the
7453      same as the binding label specified in the corresponding module
7454      procedure interface body.  */
7455     if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
7456   	&& strcmp (sym->name, sym->old_symbol->name) == 0
7457 	&& sym->binding_label && sym->old_symbol->binding_label
7458 	&& strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
7459       {
7460 	  const char *null = "NULL", *s1, *s2;
7461 	  s1 = sym->binding_label;
7462 	  if (!s1) s1 = null;
7463 	  s2 = sym->old_symbol->binding_label;
7464 	  if (!s2) s2 = null;
7465           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
7466 	  sym->refs++;	/* Needed to avoid an ICE in gfc_release_symbol */
7467 	  return MATCH_ERROR;
7468       }
7469 
7470   if(found_match != MATCH_YES)
7471     m = MATCH_ERROR;
7472   else
7473     {
7474       /* Make changes to the symbol.  */
7475       m = MATCH_ERROR;
7476 
7477       if (!gfc_add_function (&sym->attr, sym->name, NULL))
7478 	goto cleanup;
7479 
7480       if (!gfc_missing_attr (&sym->attr, NULL))
7481 	goto cleanup;
7482 
7483       if (!copy_prefix (&sym->attr, &sym->declared_at))
7484 	{
7485 	  if(!sym->attr.module_procedure)
7486 	goto cleanup;
7487 	  else
7488 	    gfc_error_check ();
7489 	}
7490 
7491       /* Delay matching the function characteristics until after the
7492 	 specification block by signalling kind=-1.  */
7493       sym->declared_at = old_loc;
7494       if (current_ts.type != BT_UNKNOWN)
7495 	current_ts.kind = -1;
7496       else
7497 	current_ts.kind = 0;
7498 
7499       if (result == NULL)
7500 	{
7501           if (current_ts.type != BT_UNKNOWN
7502 	      && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
7503 	    goto cleanup;
7504 	  sym->result = sym;
7505 	}
7506       else
7507 	{
7508           if (current_ts.type != BT_UNKNOWN
7509 	      && !gfc_add_type (result, &current_ts, &gfc_current_locus))
7510 	    goto cleanup;
7511 	  sym->result = result;
7512 	}
7513 
7514       /* Warn if this procedure has the same name as an intrinsic.  */
7515       do_warn_intrinsic_shadow (sym, true);
7516 
7517       return MATCH_YES;
7518     }
7519 
7520 cleanup:
7521   gfc_current_locus = old_loc;
7522   return m;
7523 }
7524 
7525 
7526 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
7527    pass the name of the entry, rather than the gfc_current_block name, and
7528    to return false upon finding an existing global entry.  */
7529 
7530 static bool
add_global_entry(const char * name,const char * binding_label,bool sub,locus * where)7531 add_global_entry (const char *name, const char *binding_label, bool sub,
7532 		  locus *where)
7533 {
7534   gfc_gsymbol *s;
7535   enum gfc_symbol_type type;
7536 
7537   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7538 
7539   /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7540      name is a global identifier.  */
7541   if (!binding_label || gfc_notification_std (GFC_STD_F2008))
7542     {
7543       s = gfc_get_gsymbol (name, false);
7544 
7545       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7546 	{
7547 	  gfc_global_used (s, where);
7548 	  return false;
7549 	}
7550       else
7551 	{
7552 	  s->type = type;
7553 	  s->sym_name = name;
7554 	  s->where = *where;
7555 	  s->defined = 1;
7556 	  s->ns = gfc_current_ns;
7557 	}
7558     }
7559 
7560   /* Don't add the symbol multiple times.  */
7561   if (binding_label
7562       && (!gfc_notification_std (GFC_STD_F2008)
7563 	  || strcmp (name, binding_label) != 0))
7564     {
7565       s = gfc_get_gsymbol (binding_label, true);
7566 
7567       if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7568 	{
7569 	  gfc_global_used (s, where);
7570 	  return false;
7571 	}
7572       else
7573 	{
7574 	  s->type = type;
7575 	  s->sym_name = name;
7576 	  s->binding_label = binding_label;
7577 	  s->where = *where;
7578 	  s->defined = 1;
7579 	  s->ns = gfc_current_ns;
7580 	}
7581     }
7582 
7583   return true;
7584 }
7585 
7586 
7587 /* Match an ENTRY statement.  */
7588 
7589 match
gfc_match_entry(void)7590 gfc_match_entry (void)
7591 {
7592   gfc_symbol *proc;
7593   gfc_symbol *result;
7594   gfc_symbol *entry;
7595   char name[GFC_MAX_SYMBOL_LEN + 1];
7596   gfc_compile_state state;
7597   match m;
7598   gfc_entry_list *el;
7599   locus old_loc;
7600   bool module_procedure;
7601   char peek_char;
7602   match is_bind_c;
7603 
7604   m = gfc_match_name (name);
7605   if (m != MATCH_YES)
7606     return m;
7607 
7608   if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
7609     return MATCH_ERROR;
7610 
7611   state = gfc_current_state ();
7612   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
7613     {
7614       switch (state)
7615 	{
7616 	  case COMP_PROGRAM:
7617 	    gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7618 	    break;
7619 	  case COMP_MODULE:
7620 	    gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7621 	    break;
7622 	  case COMP_SUBMODULE:
7623 	    gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7624 	    break;
7625 	  case COMP_BLOCK_DATA:
7626 	    gfc_error ("ENTRY statement at %C cannot appear within "
7627 		       "a BLOCK DATA");
7628 	    break;
7629 	  case COMP_INTERFACE:
7630 	    gfc_error ("ENTRY statement at %C cannot appear within "
7631 		       "an INTERFACE");
7632 	    break;
7633           case COMP_STRUCTURE:
7634             gfc_error ("ENTRY statement at %C cannot appear within "
7635                        "a STRUCTURE block");
7636             break;
7637 	  case COMP_DERIVED:
7638 	    gfc_error ("ENTRY statement at %C cannot appear within "
7639 		       "a DERIVED TYPE block");
7640 	    break;
7641 	  case COMP_IF:
7642 	    gfc_error ("ENTRY statement at %C cannot appear within "
7643 		       "an IF-THEN block");
7644 	    break;
7645 	  case COMP_DO:
7646 	  case COMP_DO_CONCURRENT:
7647 	    gfc_error ("ENTRY statement at %C cannot appear within "
7648 		       "a DO block");
7649 	    break;
7650 	  case COMP_SELECT:
7651 	    gfc_error ("ENTRY statement at %C cannot appear within "
7652 		       "a SELECT block");
7653 	    break;
7654 	  case COMP_FORALL:
7655 	    gfc_error ("ENTRY statement at %C cannot appear within "
7656 		       "a FORALL block");
7657 	    break;
7658 	  case COMP_WHERE:
7659 	    gfc_error ("ENTRY statement at %C cannot appear within "
7660 		       "a WHERE block");
7661 	    break;
7662 	  case COMP_CONTAINS:
7663 	    gfc_error ("ENTRY statement at %C cannot appear within "
7664 		       "a contained subprogram");
7665 	    break;
7666 	  default:
7667 	    gfc_error ("Unexpected ENTRY statement at %C");
7668 	}
7669       return MATCH_ERROR;
7670     }
7671 
7672   if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7673       && gfc_state_stack->previous->state == COMP_INTERFACE)
7674     {
7675       gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7676       return MATCH_ERROR;
7677     }
7678 
7679   module_procedure = gfc_current_ns->parent != NULL
7680 		   && gfc_current_ns->parent->proc_name
7681 		   && gfc_current_ns->parent->proc_name->attr.flavor
7682 		      == FL_MODULE;
7683 
7684   if (gfc_current_ns->parent != NULL
7685       && gfc_current_ns->parent->proc_name
7686       && !module_procedure)
7687     {
7688       gfc_error("ENTRY statement at %C cannot appear in a "
7689 		"contained procedure");
7690       return MATCH_ERROR;
7691     }
7692 
7693   /* Module function entries need special care in get_proc_name
7694      because previous references within the function will have
7695      created symbols attached to the current namespace.  */
7696   if (get_proc_name (name, &entry,
7697 		     gfc_current_ns->parent != NULL
7698 		     && module_procedure))
7699     return MATCH_ERROR;
7700 
7701   proc = gfc_current_block ();
7702 
7703   /* Make sure that it isn't already declared as BIND(C).  If it is, it
7704      must have been marked BIND(C) with a BIND(C) attribute and that is
7705      not allowed for procedures.  */
7706   if (entry->attr.is_bind_c == 1)
7707     {
7708       locus loc;
7709 
7710       entry->attr.is_bind_c = 0;
7711 
7712       loc = entry->old_symbol != NULL
7713 	? entry->old_symbol->declared_at : gfc_current_locus;
7714       gfc_error_now ("BIND(C) attribute at %L can only be used for "
7715 		     "variables or common blocks", &loc);
7716      }
7717 
7718   /* Check what next non-whitespace character is so we can tell if there
7719      is the required parens if we have a BIND(C).  */
7720   old_loc = gfc_current_locus;
7721   gfc_gobble_whitespace ();
7722   peek_char = gfc_peek_ascii_char ();
7723 
7724   if (state == COMP_SUBROUTINE)
7725     {
7726       m = gfc_match_formal_arglist (entry, 0, 1);
7727       if (m != MATCH_YES)
7728 	return MATCH_ERROR;
7729 
7730       /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7731 	 never be an internal procedure.  */
7732       is_bind_c = gfc_match_bind_c (entry, true);
7733       if (is_bind_c == MATCH_ERROR)
7734 	return MATCH_ERROR;
7735       if (is_bind_c == MATCH_YES)
7736 	{
7737 	  if (peek_char != '(')
7738 	    {
7739 	      gfc_error ("Missing required parentheses before BIND(C) at %C");
7740 	      return MATCH_ERROR;
7741 	    }
7742 
7743 	  if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7744 				  &(entry->declared_at), 1))
7745 	    return MATCH_ERROR;
7746 
7747 	}
7748 
7749       if (!gfc_current_ns->parent
7750 	  && !add_global_entry (name, entry->binding_label, true,
7751 				&old_loc))
7752 	return MATCH_ERROR;
7753 
7754       /* An entry in a subroutine.  */
7755       if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7756 	  || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7757 	return MATCH_ERROR;
7758     }
7759   else
7760     {
7761       /* An entry in a function.
7762 	 We need to take special care because writing
7763 	    ENTRY f()
7764 	 as
7765 	    ENTRY f
7766 	 is allowed, whereas
7767 	    ENTRY f() RESULT (r)
7768 	 can't be written as
7769 	    ENTRY f RESULT (r).  */
7770       if (gfc_match_eos () == MATCH_YES)
7771 	{
7772 	  gfc_current_locus = old_loc;
7773 	  /* Match the empty argument list, and add the interface to
7774 	     the symbol.  */
7775 	  m = gfc_match_formal_arglist (entry, 0, 1);
7776 	}
7777       else
7778 	m = gfc_match_formal_arglist (entry, 0, 0);
7779 
7780       if (m != MATCH_YES)
7781 	return MATCH_ERROR;
7782 
7783       result = NULL;
7784 
7785       if (gfc_match_eos () == MATCH_YES)
7786 	{
7787 	  if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7788 	      || !gfc_add_function (&entry->attr, entry->name, NULL))
7789 	    return MATCH_ERROR;
7790 
7791 	  entry->result = entry;
7792 	}
7793       else
7794 	{
7795 	  m = gfc_match_suffix (entry, &result);
7796 	  if (m == MATCH_NO)
7797 	    gfc_syntax_error (ST_ENTRY);
7798 	  if (m != MATCH_YES)
7799 	    return MATCH_ERROR;
7800 
7801           if (result)
7802 	    {
7803 	      if (!gfc_add_result (&result->attr, result->name, NULL)
7804 		  || !gfc_add_entry (&entry->attr, result->name, NULL)
7805 		  || !gfc_add_function (&entry->attr, result->name, NULL))
7806 	        return MATCH_ERROR;
7807 	      entry->result = result;
7808 	    }
7809 	  else
7810 	    {
7811 	      if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7812 		  || !gfc_add_function (&entry->attr, entry->name, NULL))
7813 		return MATCH_ERROR;
7814 	      entry->result = entry;
7815 	    }
7816 	}
7817 
7818       if (!gfc_current_ns->parent
7819 	  && !add_global_entry (name, entry->binding_label, false,
7820 				&old_loc))
7821 	return MATCH_ERROR;
7822     }
7823 
7824   if (gfc_match_eos () != MATCH_YES)
7825     {
7826       gfc_syntax_error (ST_ENTRY);
7827       return MATCH_ERROR;
7828     }
7829 
7830   /* F2018:C1546 An elemental procedure shall not have the BIND attribute.  */
7831   if (proc->attr.elemental && entry->attr.is_bind_c)
7832     {
7833       gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
7834 		 "elemental procedure", &entry->declared_at);
7835       return MATCH_ERROR;
7836     }
7837 
7838   entry->attr.recursive = proc->attr.recursive;
7839   entry->attr.elemental = proc->attr.elemental;
7840   entry->attr.pure = proc->attr.pure;
7841 
7842   el = gfc_get_entry_list ();
7843   el->sym = entry;
7844   el->next = gfc_current_ns->entries;
7845   gfc_current_ns->entries = el;
7846   if (el->next)
7847     el->id = el->next->id + 1;
7848   else
7849     el->id = 1;
7850 
7851   new_st.op = EXEC_ENTRY;
7852   new_st.ext.entry = el;
7853 
7854   return MATCH_YES;
7855 }
7856 
7857 
7858 /* Match a subroutine statement, including optional prefixes.  */
7859 
7860 match
gfc_match_subroutine(void)7861 gfc_match_subroutine (void)
7862 {
7863   char name[GFC_MAX_SYMBOL_LEN + 1];
7864   gfc_symbol *sym;
7865   match m;
7866   match is_bind_c;
7867   char peek_char;
7868   bool allow_binding_name;
7869   locus loc;
7870 
7871   if (gfc_current_state () != COMP_NONE
7872       && gfc_current_state () != COMP_INTERFACE
7873       && gfc_current_state () != COMP_CONTAINS)
7874     return MATCH_NO;
7875 
7876   m = gfc_match_prefix (NULL);
7877   if (m != MATCH_YES)
7878     return m;
7879 
7880   m = gfc_match ("subroutine% %n", name);
7881   if (m != MATCH_YES)
7882     return m;
7883 
7884   if (get_proc_name (name, &sym, false))
7885     return MATCH_ERROR;
7886 
7887   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7888      the symbol existed before.  */
7889   sym->declared_at = gfc_current_locus;
7890 
7891   if (current_attr.module_procedure)
7892     sym->attr.module_procedure = 1;
7893 
7894   if (add_hidden_procptr_result (sym))
7895     sym = sym->result;
7896 
7897   gfc_new_block = sym;
7898 
7899   /* Check what next non-whitespace character is so we can tell if there
7900      is the required parens if we have a BIND(C).  */
7901   gfc_gobble_whitespace ();
7902   peek_char = gfc_peek_ascii_char ();
7903 
7904   if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7905     return MATCH_ERROR;
7906 
7907   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7908     return MATCH_ERROR;
7909 
7910   /* Make sure that it isn't already declared as BIND(C).  If it is, it
7911      must have been marked BIND(C) with a BIND(C) attribute and that is
7912      not allowed for procedures.  */
7913   if (sym->attr.is_bind_c == 1)
7914     {
7915       sym->attr.is_bind_c = 0;
7916 
7917       if (gfc_state_stack->previous
7918 	  && gfc_state_stack->previous->state != COMP_SUBMODULE)
7919 	{
7920 	  locus loc;
7921 	  loc = sym->old_symbol != NULL
7922 	    ? sym->old_symbol->declared_at : gfc_current_locus;
7923 	  gfc_error_now ("BIND(C) attribute at %L can only be used for "
7924 			 "variables or common blocks", &loc);
7925 	}
7926     }
7927 
7928   /* C binding names are not allowed for internal procedures.  */
7929   if (gfc_current_state () == COMP_CONTAINS
7930       && sym->ns->proc_name->attr.flavor != FL_MODULE)
7931     allow_binding_name = false;
7932   else
7933     allow_binding_name = true;
7934 
7935   /* Here, we are just checking if it has the bind(c) attribute, and if
7936      so, then we need to make sure it's all correct.  If it doesn't,
7937      we still need to continue matching the rest of the subroutine line.  */
7938   gfc_gobble_whitespace ();
7939   loc = gfc_current_locus;
7940   is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7941   if (is_bind_c == MATCH_ERROR)
7942     {
7943       /* There was an attempt at the bind(c), but it was wrong.	 An
7944 	 error message should have been printed w/in the gfc_match_bind_c
7945 	 so here we'll just return the MATCH_ERROR.  */
7946       return MATCH_ERROR;
7947     }
7948 
7949   if (is_bind_c == MATCH_YES)
7950     {
7951       gfc_formal_arglist *arg;
7952 
7953       /* The following is allowed in the Fortran 2008 draft.  */
7954       if (gfc_current_state () == COMP_CONTAINS
7955 	  && sym->ns->proc_name->attr.flavor != FL_MODULE
7956 	  && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7957 			      "at %L may not be specified for an internal "
7958 			      "procedure", &gfc_current_locus))
7959 	return MATCH_ERROR;
7960 
7961       if (peek_char != '(')
7962         {
7963           gfc_error ("Missing required parentheses before BIND(C) at %C");
7964           return MATCH_ERROR;
7965         }
7966 
7967       /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
7968 	 subprogram and a binding label is specified, it shall be the
7969 	 same as the binding label specified in the corresponding module
7970 	 procedure interface body.  */
7971       if (sym->attr.module_procedure && sym->old_symbol
7972   	  && strcmp (sym->name, sym->old_symbol->name) == 0
7973 	  && sym->binding_label && sym->old_symbol->binding_label
7974 	  && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
7975 	{
7976 	  const char *null = "NULL", *s1, *s2;
7977 	  s1 = sym->binding_label;
7978 	  if (!s1) s1 = null;
7979 	  s2 = sym->old_symbol->binding_label;
7980 	  if (!s2) s2 = null;
7981           gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
7982 	  sym->refs++;	/* Needed to avoid an ICE in gfc_release_symbol */
7983 	  return MATCH_ERROR;
7984 	}
7985 
7986       /* Scan the dummy arguments for an alternate return.  */
7987       for (arg = sym->formal; arg; arg = arg->next)
7988 	if (!arg->sym)
7989 	  {
7990 	    gfc_error ("Alternate return dummy argument cannot appear in a "
7991 		       "SUBROUTINE with the BIND(C) attribute at %L", &loc);
7992 	    return MATCH_ERROR;
7993 	  }
7994 
7995       if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
7996         return MATCH_ERROR;
7997     }
7998 
7999   if (gfc_match_eos () != MATCH_YES)
8000     {
8001       gfc_syntax_error (ST_SUBROUTINE);
8002       return MATCH_ERROR;
8003     }
8004 
8005   if (!copy_prefix (&sym->attr, &sym->declared_at))
8006     {
8007       if(!sym->attr.module_procedure)
8008 	return MATCH_ERROR;
8009       else
8010 	gfc_error_check ();
8011     }
8012 
8013   /* Warn if it has the same name as an intrinsic.  */
8014   do_warn_intrinsic_shadow (sym, false);
8015 
8016   return MATCH_YES;
8017 }
8018 
8019 
8020 /* Check that the NAME identifier in a BIND attribute or statement
8021    is conform to C identifier rules.  */
8022 
8023 match
check_bind_name_identifier(char ** name)8024 check_bind_name_identifier (char **name)
8025 {
8026   char *n = *name, *p;
8027 
8028   /* Remove leading spaces.  */
8029   while (*n == ' ')
8030     n++;
8031 
8032   /* On an empty string, free memory and set name to NULL.  */
8033   if (*n == '\0')
8034     {
8035       free (*name);
8036       *name = NULL;
8037       return MATCH_YES;
8038     }
8039 
8040   /* Remove trailing spaces.  */
8041   p = n + strlen(n) - 1;
8042   while (*p == ' ')
8043     *(p--) = '\0';
8044 
8045   /* Insert the identifier into the symbol table.  */
8046   p = xstrdup (n);
8047   free (*name);
8048   *name = p;
8049 
8050   /* Now check that identifier is valid under C rules.  */
8051   if (ISDIGIT (*p))
8052     {
8053       gfc_error ("Invalid C identifier in NAME= specifier at %C");
8054       return MATCH_ERROR;
8055     }
8056 
8057   for (; *p; p++)
8058     if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
8059       {
8060         gfc_error ("Invalid C identifier in NAME= specifier at %C");
8061 	return MATCH_ERROR;
8062       }
8063 
8064   return MATCH_YES;
8065 }
8066 
8067 
8068 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
8069    given, and set the binding label in either the given symbol (if not
8070    NULL), or in the current_ts.  The symbol may be NULL because we may
8071    encounter the BIND(C) before the declaration itself.  Return
8072    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
8073    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
8074    or MATCH_YES if the specifier was correct and the binding label and
8075    bind(c) fields were set correctly for the given symbol or the
8076    current_ts. If allow_binding_name is false, no binding name may be
8077    given.  */
8078 
8079 match
gfc_match_bind_c(gfc_symbol * sym,bool allow_binding_name)8080 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
8081 {
8082   char *binding_label = NULL;
8083   gfc_expr *e = NULL;
8084 
8085   /* Initialize the flag that specifies whether we encountered a NAME=
8086      specifier or not.  */
8087   has_name_equals = 0;
8088 
8089   /* This much we have to be able to match, in this order, if
8090      there is a bind(c) label.	*/
8091   if (gfc_match (" bind ( c ") != MATCH_YES)
8092     return MATCH_NO;
8093 
8094   /* Now see if there is a binding label, or if we've reached the
8095      end of the bind(c) attribute without one.	*/
8096   if (gfc_match_char (',') == MATCH_YES)
8097     {
8098       if (gfc_match (" name = ") != MATCH_YES)
8099         {
8100           gfc_error ("Syntax error in NAME= specifier for binding label "
8101                      "at %C");
8102           /* should give an error message here */
8103           return MATCH_ERROR;
8104         }
8105 
8106       has_name_equals = 1;
8107 
8108       if (gfc_match_init_expr (&e) != MATCH_YES)
8109 	{
8110 	  gfc_free_expr (e);
8111 	  return MATCH_ERROR;
8112 	}
8113 
8114       if (!gfc_simplify_expr(e, 0))
8115 	{
8116 	  gfc_error ("NAME= specifier at %C should be a constant expression");
8117 	  gfc_free_expr (e);
8118 	  return MATCH_ERROR;
8119 	}
8120 
8121       if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
8122 	  || e->ts.kind != gfc_default_character_kind || e->rank != 0)
8123 	{
8124 	  gfc_error ("NAME= specifier at %C should be a scalar of "
8125 	             "default character kind");
8126 	  gfc_free_expr(e);
8127 	  return MATCH_ERROR;
8128 	}
8129 
8130       // Get a C string from the Fortran string constant
8131       binding_label = gfc_widechar_to_char (e->value.character.string,
8132 					    e->value.character.length);
8133       gfc_free_expr(e);
8134 
8135       // Check that it is valid (old gfc_match_name_C)
8136       if (check_bind_name_identifier (&binding_label) != MATCH_YES)
8137 	return MATCH_ERROR;
8138     }
8139 
8140   /* Get the required right paren.  */
8141   if (gfc_match_char (')') != MATCH_YES)
8142     {
8143       gfc_error ("Missing closing paren for binding label at %C");
8144       return MATCH_ERROR;
8145     }
8146 
8147   if (has_name_equals && !allow_binding_name)
8148     {
8149       gfc_error ("No binding name is allowed in BIND(C) at %C");
8150       return MATCH_ERROR;
8151     }
8152 
8153   if (has_name_equals && sym != NULL && sym->attr.dummy)
8154     {
8155       gfc_error ("For dummy procedure %s, no binding name is "
8156 		 "allowed in BIND(C) at %C", sym->name);
8157       return MATCH_ERROR;
8158     }
8159 
8160 
8161   /* Save the binding label to the symbol.  If sym is null, we're
8162      probably matching the typespec attributes of a declaration and
8163      haven't gotten the name yet, and therefore, no symbol yet.	 */
8164   if (binding_label)
8165     {
8166       if (sym != NULL)
8167 	sym->binding_label = binding_label;
8168       else
8169 	curr_binding_label = binding_label;
8170     }
8171   else if (allow_binding_name)
8172     {
8173       /* No binding label, but if symbol isn't null, we
8174 	 can set the label for it here.
8175 	 If name="" or allow_binding_name is false, no C binding name is
8176 	 created.  */
8177       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
8178 	sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
8179     }
8180 
8181   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
8182       && current_interface.type == INTERFACE_ABSTRACT)
8183     {
8184       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
8185       return MATCH_ERROR;
8186     }
8187 
8188   return MATCH_YES;
8189 }
8190 
8191 
8192 /* Return nonzero if we're currently compiling a contained procedure.  */
8193 
8194 static int
contained_procedure(void)8195 contained_procedure (void)
8196 {
8197   gfc_state_data *s = gfc_state_stack;
8198 
8199   if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
8200       && s->previous != NULL && s->previous->state == COMP_CONTAINS)
8201     return 1;
8202 
8203   return 0;
8204 }
8205 
8206 /* Set the kind of each enumerator.  The kind is selected such that it is
8207    interoperable with the corresponding C enumeration type, making
8208    sure that -fshort-enums is honored.  */
8209 
8210 static void
set_enum_kind(void)8211 set_enum_kind(void)
8212 {
8213   enumerator_history *current_history = NULL;
8214   int kind;
8215   int i;
8216 
8217   if (max_enum == NULL || enum_history == NULL)
8218     return;
8219 
8220   if (!flag_short_enums)
8221     return;
8222 
8223   i = 0;
8224   do
8225     {
8226       kind = gfc_integer_kinds[i++].kind;
8227     }
8228   while (kind < gfc_c_int_kind
8229 	 && gfc_check_integer_range (max_enum->initializer->value.integer,
8230 				     kind) != ARITH_OK);
8231 
8232   current_history = enum_history;
8233   while (current_history != NULL)
8234     {
8235       current_history->sym->ts.kind = kind;
8236       current_history = current_history->next;
8237     }
8238 }
8239 
8240 
8241 /* Match any of the various end-block statements.  Returns the type of
8242    END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
8243    and END BLOCK statements cannot be replaced by a single END statement.  */
8244 
8245 match
gfc_match_end(gfc_statement * st)8246 gfc_match_end (gfc_statement *st)
8247 {
8248   char name[GFC_MAX_SYMBOL_LEN + 1];
8249   gfc_compile_state state;
8250   locus old_loc;
8251   const char *block_name;
8252   const char *target;
8253   int eos_ok;
8254   match m;
8255   gfc_namespace *parent_ns, *ns, *prev_ns;
8256   gfc_namespace **nsp;
8257   bool abreviated_modproc_decl = false;
8258   bool got_matching_end = false;
8259 
8260   old_loc = gfc_current_locus;
8261   if (gfc_match ("end") != MATCH_YES)
8262     return MATCH_NO;
8263 
8264   state = gfc_current_state ();
8265   block_name = gfc_current_block () == NULL
8266 	     ? NULL : gfc_current_block ()->name;
8267 
8268   switch (state)
8269     {
8270     case COMP_ASSOCIATE:
8271     case COMP_BLOCK:
8272       if (gfc_str_startswith (block_name, "block@"))
8273 	block_name = NULL;
8274       break;
8275 
8276     case COMP_CONTAINS:
8277     case COMP_DERIVED_CONTAINS:
8278       state = gfc_state_stack->previous->state;
8279       block_name = gfc_state_stack->previous->sym == NULL
8280 		 ? NULL : gfc_state_stack->previous->sym->name;
8281       abreviated_modproc_decl = gfc_state_stack->previous->sym
8282 		&& gfc_state_stack->previous->sym->abr_modproc_decl;
8283       break;
8284 
8285     default:
8286       break;
8287     }
8288 
8289   if (!abreviated_modproc_decl)
8290     abreviated_modproc_decl = gfc_current_block ()
8291 			      && gfc_current_block ()->abr_modproc_decl;
8292 
8293   switch (state)
8294     {
8295     case COMP_NONE:
8296     case COMP_PROGRAM:
8297       *st = ST_END_PROGRAM;
8298       target = " program";
8299       eos_ok = 1;
8300       break;
8301 
8302     case COMP_SUBROUTINE:
8303       *st = ST_END_SUBROUTINE;
8304       if (!abreviated_modproc_decl)
8305       target = " subroutine";
8306       else
8307 	target = " procedure";
8308       eos_ok = !contained_procedure ();
8309       break;
8310 
8311     case COMP_FUNCTION:
8312       *st = ST_END_FUNCTION;
8313       if (!abreviated_modproc_decl)
8314       target = " function";
8315       else
8316 	target = " procedure";
8317       eos_ok = !contained_procedure ();
8318       break;
8319 
8320     case COMP_BLOCK_DATA:
8321       *st = ST_END_BLOCK_DATA;
8322       target = " block data";
8323       eos_ok = 1;
8324       break;
8325 
8326     case COMP_MODULE:
8327       *st = ST_END_MODULE;
8328       target = " module";
8329       eos_ok = 1;
8330       break;
8331 
8332     case COMP_SUBMODULE:
8333       *st = ST_END_SUBMODULE;
8334       target = " submodule";
8335       eos_ok = 1;
8336       break;
8337 
8338     case COMP_INTERFACE:
8339       *st = ST_END_INTERFACE;
8340       target = " interface";
8341       eos_ok = 0;
8342       break;
8343 
8344     case COMP_MAP:
8345       *st = ST_END_MAP;
8346       target = " map";
8347       eos_ok = 0;
8348       break;
8349 
8350     case COMP_UNION:
8351       *st = ST_END_UNION;
8352       target = " union";
8353       eos_ok = 0;
8354       break;
8355 
8356     case COMP_STRUCTURE:
8357       *st = ST_END_STRUCTURE;
8358       target = " structure";
8359       eos_ok = 0;
8360       break;
8361 
8362     case COMP_DERIVED:
8363     case COMP_DERIVED_CONTAINS:
8364       *st = ST_END_TYPE;
8365       target = " type";
8366       eos_ok = 0;
8367       break;
8368 
8369     case COMP_ASSOCIATE:
8370       *st = ST_END_ASSOCIATE;
8371       target = " associate";
8372       eos_ok = 0;
8373       break;
8374 
8375     case COMP_BLOCK:
8376       *st = ST_END_BLOCK;
8377       target = " block";
8378       eos_ok = 0;
8379       break;
8380 
8381     case COMP_IF:
8382       *st = ST_ENDIF;
8383       target = " if";
8384       eos_ok = 0;
8385       break;
8386 
8387     case COMP_DO:
8388     case COMP_DO_CONCURRENT:
8389       *st = ST_ENDDO;
8390       target = " do";
8391       eos_ok = 0;
8392       break;
8393 
8394     case COMP_CRITICAL:
8395       *st = ST_END_CRITICAL;
8396       target = " critical";
8397       eos_ok = 0;
8398       break;
8399 
8400     case COMP_SELECT:
8401     case COMP_SELECT_TYPE:
8402     case COMP_SELECT_RANK:
8403       *st = ST_END_SELECT;
8404       target = " select";
8405       eos_ok = 0;
8406       break;
8407 
8408     case COMP_FORALL:
8409       *st = ST_END_FORALL;
8410       target = " forall";
8411       eos_ok = 0;
8412       break;
8413 
8414     case COMP_WHERE:
8415       *st = ST_END_WHERE;
8416       target = " where";
8417       eos_ok = 0;
8418       break;
8419 
8420     case COMP_ENUM:
8421       *st = ST_END_ENUM;
8422       target = " enum";
8423       eos_ok = 0;
8424       last_initializer = NULL;
8425       set_enum_kind ();
8426       gfc_free_enum_history ();
8427       break;
8428 
8429     default:
8430       gfc_error ("Unexpected END statement at %C");
8431       goto cleanup;
8432     }
8433 
8434   old_loc = gfc_current_locus;
8435   if (gfc_match_eos () == MATCH_YES)
8436     {
8437       if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
8438 	{
8439 	  if (!gfc_notify_std (GFC_STD_F2008, "END statement "
8440 			       "instead of %s statement at %L",
8441 			       abreviated_modproc_decl ? "END PROCEDURE"
8442 			       : gfc_ascii_statement(*st), &old_loc))
8443 	    goto cleanup;
8444 	}
8445       else if (!eos_ok)
8446 	{
8447 	  /* We would have required END [something].  */
8448 	  gfc_error ("%s statement expected at %L",
8449 		     gfc_ascii_statement (*st), &old_loc);
8450 	  goto cleanup;
8451 	}
8452 
8453       return MATCH_YES;
8454     }
8455 
8456   /* Verify that we've got the sort of end-block that we're expecting.  */
8457   if (gfc_match (target) != MATCH_YES)
8458     {
8459       gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
8460 		 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
8461       goto cleanup;
8462     }
8463   else
8464     got_matching_end = true;
8465 
8466   old_loc = gfc_current_locus;
8467   /* If we're at the end, make sure a block name wasn't required.  */
8468   if (gfc_match_eos () == MATCH_YES)
8469     {
8470 
8471       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
8472 	  && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
8473 	  && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
8474 	return MATCH_YES;
8475 
8476       if (!block_name)
8477 	return MATCH_YES;
8478 
8479       gfc_error ("Expected block name of %qs in %s statement at %L",
8480 		 block_name, gfc_ascii_statement (*st), &old_loc);
8481 
8482       return MATCH_ERROR;
8483     }
8484 
8485   /* END INTERFACE has a special handler for its several possible endings.  */
8486   if (*st == ST_END_INTERFACE)
8487     return gfc_match_end_interface ();
8488 
8489   /* We haven't hit the end of statement, so what is left must be an
8490      end-name.  */
8491   m = gfc_match_space ();
8492   if (m == MATCH_YES)
8493     m = gfc_match_name (name);
8494 
8495   if (m == MATCH_NO)
8496     gfc_error ("Expected terminating name at %C");
8497   if (m != MATCH_YES)
8498     goto cleanup;
8499 
8500   if (block_name == NULL)
8501     goto syntax;
8502 
8503   /* We have to pick out the declared submodule name from the composite
8504      required by F2008:11.2.3 para 2, which ends in the declared name.  */
8505   if (state == COMP_SUBMODULE)
8506     block_name = strchr (block_name, '.') + 1;
8507 
8508   if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
8509     {
8510       gfc_error ("Expected label %qs for %s statement at %C", block_name,
8511 		 gfc_ascii_statement (*st));
8512       goto cleanup;
8513     }
8514   /* Procedure pointer as function result.  */
8515   else if (strcmp (block_name, "ppr@") == 0
8516 	   && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8517     {
8518       gfc_error ("Expected label %qs for %s statement at %C",
8519 		 gfc_current_block ()->ns->proc_name->name,
8520 		 gfc_ascii_statement (*st));
8521       goto cleanup;
8522     }
8523 
8524   if (gfc_match_eos () == MATCH_YES)
8525     return MATCH_YES;
8526 
8527 syntax:
8528   gfc_syntax_error (*st);
8529 
8530 cleanup:
8531   gfc_current_locus = old_loc;
8532 
8533   /* If we are missing an END BLOCK, we created a half-ready namespace.
8534      Remove it from the parent namespace's sibling list.  */
8535 
8536   while (state == COMP_BLOCK && !got_matching_end)
8537     {
8538       parent_ns = gfc_current_ns->parent;
8539 
8540       nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8541 
8542       prev_ns = NULL;
8543       ns = *nsp;
8544       while (ns)
8545 	{
8546 	  if (ns == gfc_current_ns)
8547 	    {
8548 	      if (prev_ns == NULL)
8549 		*nsp = NULL;
8550 	      else
8551 		prev_ns->sibling = ns->sibling;
8552 	    }
8553 	  prev_ns = ns;
8554 	  ns = ns->sibling;
8555 	}
8556 
8557       gfc_free_namespace (gfc_current_ns);
8558       gfc_current_ns = parent_ns;
8559       gfc_state_stack = gfc_state_stack->previous;
8560       state = gfc_current_state ();
8561     }
8562 
8563   return MATCH_ERROR;
8564 }
8565 
8566 
8567 
8568 /***************** Attribute declaration statements ****************/
8569 
8570 /* Set the attribute of a single variable.  */
8571 
8572 static match
attr_decl1(void)8573 attr_decl1 (void)
8574 {
8575   char name[GFC_MAX_SYMBOL_LEN + 1];
8576   gfc_array_spec *as;
8577 
8578   /* Workaround -Wmaybe-uninitialized false positive during
8579      profiledbootstrap by initializing them.  */
8580   gfc_symbol *sym = NULL;
8581   locus var_locus;
8582   match m;
8583 
8584   as = NULL;
8585 
8586   m = gfc_match_name (name);
8587   if (m != MATCH_YES)
8588     goto cleanup;
8589 
8590   if (find_special (name, &sym, false))
8591     return MATCH_ERROR;
8592 
8593   if (!check_function_name (name))
8594     {
8595       m = MATCH_ERROR;
8596       goto cleanup;
8597     }
8598 
8599   var_locus = gfc_current_locus;
8600 
8601   /* Deal with possible array specification for certain attributes.  */
8602   if (current_attr.dimension
8603       || current_attr.codimension
8604       || current_attr.allocatable
8605       || current_attr.pointer
8606       || current_attr.target)
8607     {
8608       m = gfc_match_array_spec (&as, !current_attr.codimension,
8609 				!current_attr.dimension
8610 				&& !current_attr.pointer
8611 				&& !current_attr.target);
8612       if (m == MATCH_ERROR)
8613 	goto cleanup;
8614 
8615       if (current_attr.dimension && m == MATCH_NO)
8616 	{
8617 	  gfc_error ("Missing array specification at %L in DIMENSION "
8618 		     "statement", &var_locus);
8619 	  m = MATCH_ERROR;
8620 	  goto cleanup;
8621 	}
8622 
8623       if (current_attr.dimension && sym->value)
8624 	{
8625 	  gfc_error ("Dimensions specified for %s at %L after its "
8626 		     "initialization", sym->name, &var_locus);
8627 	  m = MATCH_ERROR;
8628 	  goto cleanup;
8629 	}
8630 
8631       if (current_attr.codimension && m == MATCH_NO)
8632 	{
8633 	  gfc_error ("Missing array specification at %L in CODIMENSION "
8634 		     "statement", &var_locus);
8635 	  m = MATCH_ERROR;
8636 	  goto cleanup;
8637 	}
8638 
8639       if ((current_attr.allocatable || current_attr.pointer)
8640 	  && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8641 	{
8642 	  gfc_error ("Array specification must be deferred at %L", &var_locus);
8643 	  m = MATCH_ERROR;
8644 	  goto cleanup;
8645 	}
8646     }
8647 
8648   /* Update symbol table.  DIMENSION attribute is set in
8649      gfc_set_array_spec().  For CLASS variables, this must be applied
8650      to the first component, or '_data' field.  */
8651   if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
8652     {
8653       /* gfc_set_array_spec sets sym->attr not CLASS_DATA(sym)->attr.  Check
8654 	 for duplicate attribute here.  */
8655       if (CLASS_DATA(sym)->attr.dimension == 1 && as)
8656 	{
8657 	  gfc_error ("Duplicate DIMENSION attribute at %C");
8658 	  m = MATCH_ERROR;
8659 	  goto cleanup;
8660 	}
8661 
8662       if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
8663 	{
8664 	  m = MATCH_ERROR;
8665 	  goto cleanup;
8666 	}
8667     }
8668   else
8669     {
8670       if (current_attr.dimension == 0 && current_attr.codimension == 0
8671 	  && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
8672 	{
8673 	  m = MATCH_ERROR;
8674 	  goto cleanup;
8675 	}
8676     }
8677 
8678   if (sym->ts.type == BT_CLASS
8679       && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
8680     {
8681       m = MATCH_ERROR;
8682       goto cleanup;
8683     }
8684 
8685   if (!gfc_set_array_spec (sym, as, &var_locus))
8686     {
8687       m = MATCH_ERROR;
8688       goto cleanup;
8689     }
8690 
8691   if (sym->attr.cray_pointee && sym->as != NULL)
8692     {
8693       /* Fix the array spec.  */
8694       m = gfc_mod_pointee_as (sym->as);
8695       if (m == MATCH_ERROR)
8696 	goto cleanup;
8697     }
8698 
8699   if (!gfc_add_attribute (&sym->attr, &var_locus))
8700     {
8701       m = MATCH_ERROR;
8702       goto cleanup;
8703     }
8704 
8705   if ((current_attr.external || current_attr.intrinsic)
8706       && sym->attr.flavor != FL_PROCEDURE
8707       && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8708     {
8709       m = MATCH_ERROR;
8710       goto cleanup;
8711     }
8712 
8713   add_hidden_procptr_result (sym);
8714 
8715   return MATCH_YES;
8716 
8717 cleanup:
8718   gfc_free_array_spec (as);
8719   return m;
8720 }
8721 
8722 
8723 /* Generic attribute declaration subroutine.  Used for attributes that
8724    just have a list of names.  */
8725 
8726 static match
attr_decl(void)8727 attr_decl (void)
8728 {
8729   match m;
8730 
8731   /* Gobble the optional double colon, by simply ignoring the result
8732      of gfc_match().  */
8733   gfc_match (" ::");
8734 
8735   for (;;)
8736     {
8737       m = attr_decl1 ();
8738       if (m != MATCH_YES)
8739 	break;
8740 
8741       if (gfc_match_eos () == MATCH_YES)
8742 	{
8743 	  m = MATCH_YES;
8744 	  break;
8745 	}
8746 
8747       if (gfc_match_char (',') != MATCH_YES)
8748 	{
8749 	  gfc_error ("Unexpected character in variable list at %C");
8750 	  m = MATCH_ERROR;
8751 	  break;
8752 	}
8753     }
8754 
8755   return m;
8756 }
8757 
8758 
8759 /* This routine matches Cray Pointer declarations of the form:
8760    pointer ( <pointer>, <pointee> )
8761    or
8762    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8763    The pointer, if already declared, should be an integer.  Otherwise, we
8764    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
8765    be either a scalar, or an array declaration.  No space is allocated for
8766    the pointee.  For the statement
8767    pointer (ipt, ar(10))
8768    any subsequent uses of ar will be translated (in C-notation) as
8769    ar(i) => ((<type> *) ipt)(i)
8770    After gimplification, pointee variable will disappear in the code.  */
8771 
8772 static match
cray_pointer_decl(void)8773 cray_pointer_decl (void)
8774 {
8775   match m;
8776   gfc_array_spec *as = NULL;
8777   gfc_symbol *cptr; /* Pointer symbol.  */
8778   gfc_symbol *cpte; /* Pointee symbol.  */
8779   locus var_locus;
8780   bool done = false;
8781 
8782   while (!done)
8783     {
8784       if (gfc_match_char ('(') != MATCH_YES)
8785 	{
8786 	  gfc_error ("Expected %<(%> at %C");
8787 	  return MATCH_ERROR;
8788 	}
8789 
8790       /* Match pointer.  */
8791       var_locus = gfc_current_locus;
8792       gfc_clear_attr (&current_attr);
8793       gfc_add_cray_pointer (&current_attr, &var_locus);
8794       current_ts.type = BT_INTEGER;
8795       current_ts.kind = gfc_index_integer_kind;
8796 
8797       m = gfc_match_symbol (&cptr, 0);
8798       if (m != MATCH_YES)
8799 	{
8800 	  gfc_error ("Expected variable name at %C");
8801 	  return m;
8802 	}
8803 
8804       if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8805 	return MATCH_ERROR;
8806 
8807       gfc_set_sym_referenced (cptr);
8808 
8809       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
8810 	{
8811 	  cptr->ts.type = BT_INTEGER;
8812 	  cptr->ts.kind = gfc_index_integer_kind;
8813 	}
8814       else if (cptr->ts.type != BT_INTEGER)
8815 	{
8816 	  gfc_error ("Cray pointer at %C must be an integer");
8817 	  return MATCH_ERROR;
8818 	}
8819       else if (cptr->ts.kind < gfc_index_integer_kind)
8820 	gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8821 		     " memory addresses require %d bytes",
8822 		     cptr->ts.kind, gfc_index_integer_kind);
8823 
8824       if (gfc_match_char (',') != MATCH_YES)
8825 	{
8826 	  gfc_error ("Expected \",\" at %C");
8827 	  return MATCH_ERROR;
8828 	}
8829 
8830       /* Match Pointee.  */
8831       var_locus = gfc_current_locus;
8832       gfc_clear_attr (&current_attr);
8833       gfc_add_cray_pointee (&current_attr, &var_locus);
8834       current_ts.type = BT_UNKNOWN;
8835       current_ts.kind = 0;
8836 
8837       m = gfc_match_symbol (&cpte, 0);
8838       if (m != MATCH_YES)
8839 	{
8840 	  gfc_error ("Expected variable name at %C");
8841 	  return m;
8842 	}
8843 
8844       /* Check for an optional array spec.  */
8845       m = gfc_match_array_spec (&as, true, false);
8846       if (m == MATCH_ERROR)
8847 	{
8848 	  gfc_free_array_spec (as);
8849 	  return m;
8850 	}
8851       else if (m == MATCH_NO)
8852 	{
8853 	  gfc_free_array_spec (as);
8854 	  as = NULL;
8855 	}
8856 
8857       if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8858 	return MATCH_ERROR;
8859 
8860       gfc_set_sym_referenced (cpte);
8861 
8862       if (cpte->as == NULL)
8863 	{
8864 	  if (!gfc_set_array_spec (cpte, as, &var_locus))
8865 	    gfc_internal_error ("Cannot set Cray pointee array spec.");
8866 	}
8867       else if (as != NULL)
8868 	{
8869 	  gfc_error ("Duplicate array spec for Cray pointee at %C");
8870 	  gfc_free_array_spec (as);
8871 	  return MATCH_ERROR;
8872 	}
8873 
8874       as = NULL;
8875 
8876       if (cpte->as != NULL)
8877 	{
8878 	  /* Fix array spec.  */
8879 	  m = gfc_mod_pointee_as (cpte->as);
8880 	  if (m == MATCH_ERROR)
8881 	    return m;
8882 	}
8883 
8884       /* Point the Pointee at the Pointer.  */
8885       cpte->cp_pointer = cptr;
8886 
8887       if (gfc_match_char (')') != MATCH_YES)
8888 	{
8889 	  gfc_error ("Expected \")\" at %C");
8890 	  return MATCH_ERROR;
8891 	}
8892       m = gfc_match_char (',');
8893       if (m != MATCH_YES)
8894 	done = true; /* Stop searching for more declarations.  */
8895 
8896     }
8897 
8898   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
8899       || gfc_match_eos () != MATCH_YES)
8900     {
8901       gfc_error ("Expected %<,%> or end of statement at %C");
8902       return MATCH_ERROR;
8903     }
8904   return MATCH_YES;
8905 }
8906 
8907 
8908 match
gfc_match_external(void)8909 gfc_match_external (void)
8910 {
8911 
8912   gfc_clear_attr (&current_attr);
8913   current_attr.external = 1;
8914 
8915   return attr_decl ();
8916 }
8917 
8918 
8919 match
gfc_match_intent(void)8920 gfc_match_intent (void)
8921 {
8922   sym_intent intent;
8923 
8924   /* This is not allowed within a BLOCK construct!  */
8925   if (gfc_current_state () == COMP_BLOCK)
8926     {
8927       gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8928       return MATCH_ERROR;
8929     }
8930 
8931   intent = match_intent_spec ();
8932   if (intent == INTENT_UNKNOWN)
8933     return MATCH_ERROR;
8934 
8935   gfc_clear_attr (&current_attr);
8936   current_attr.intent = intent;
8937 
8938   return attr_decl ();
8939 }
8940 
8941 
8942 match
gfc_match_intrinsic(void)8943 gfc_match_intrinsic (void)
8944 {
8945 
8946   gfc_clear_attr (&current_attr);
8947   current_attr.intrinsic = 1;
8948 
8949   return attr_decl ();
8950 }
8951 
8952 
8953 match
gfc_match_optional(void)8954 gfc_match_optional (void)
8955 {
8956   /* This is not allowed within a BLOCK construct!  */
8957   if (gfc_current_state () == COMP_BLOCK)
8958     {
8959       gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8960       return MATCH_ERROR;
8961     }
8962 
8963   gfc_clear_attr (&current_attr);
8964   current_attr.optional = 1;
8965 
8966   return attr_decl ();
8967 }
8968 
8969 
8970 match
gfc_match_pointer(void)8971 gfc_match_pointer (void)
8972 {
8973   gfc_gobble_whitespace ();
8974   if (gfc_peek_ascii_char () == '(')
8975     {
8976       if (!flag_cray_pointer)
8977 	{
8978 	  gfc_error ("Cray pointer declaration at %C requires "
8979 		     "%<-fcray-pointer%> flag");
8980 	  return MATCH_ERROR;
8981 	}
8982       return cray_pointer_decl ();
8983     }
8984   else
8985     {
8986       gfc_clear_attr (&current_attr);
8987       current_attr.pointer = 1;
8988 
8989       return attr_decl ();
8990     }
8991 }
8992 
8993 
8994 match
gfc_match_allocatable(void)8995 gfc_match_allocatable (void)
8996 {
8997   gfc_clear_attr (&current_attr);
8998   current_attr.allocatable = 1;
8999 
9000   return attr_decl ();
9001 }
9002 
9003 
9004 match
gfc_match_codimension(void)9005 gfc_match_codimension (void)
9006 {
9007   gfc_clear_attr (&current_attr);
9008   current_attr.codimension = 1;
9009 
9010   return attr_decl ();
9011 }
9012 
9013 
9014 match
gfc_match_contiguous(void)9015 gfc_match_contiguous (void)
9016 {
9017   if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
9018     return MATCH_ERROR;
9019 
9020   gfc_clear_attr (&current_attr);
9021   current_attr.contiguous = 1;
9022 
9023   return attr_decl ();
9024 }
9025 
9026 
9027 match
gfc_match_dimension(void)9028 gfc_match_dimension (void)
9029 {
9030   gfc_clear_attr (&current_attr);
9031   current_attr.dimension = 1;
9032 
9033   return attr_decl ();
9034 }
9035 
9036 
9037 match
gfc_match_target(void)9038 gfc_match_target (void)
9039 {
9040   gfc_clear_attr (&current_attr);
9041   current_attr.target = 1;
9042 
9043   return attr_decl ();
9044 }
9045 
9046 
9047 /* Match the list of entities being specified in a PUBLIC or PRIVATE
9048    statement.  */
9049 
9050 static match
access_attr_decl(gfc_statement st)9051 access_attr_decl (gfc_statement st)
9052 {
9053   char name[GFC_MAX_SYMBOL_LEN + 1];
9054   interface_type type;
9055   gfc_user_op *uop;
9056   gfc_symbol *sym, *dt_sym;
9057   gfc_intrinsic_op op;
9058   match m;
9059   gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
9060 
9061   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9062     goto done;
9063 
9064   for (;;)
9065     {
9066       m = gfc_match_generic_spec (&type, name, &op);
9067       if (m == MATCH_NO)
9068 	goto syntax;
9069       if (m == MATCH_ERROR)
9070 	goto done;
9071 
9072       switch (type)
9073 	{
9074 	case INTERFACE_NAMELESS:
9075 	case INTERFACE_ABSTRACT:
9076 	  goto syntax;
9077 
9078 	case INTERFACE_GENERIC:
9079 	case INTERFACE_DTIO:
9080 
9081 	  if (gfc_get_symbol (name, NULL, &sym))
9082 	    goto done;
9083 
9084 	  if (type == INTERFACE_DTIO
9085 	      && gfc_current_ns->proc_name
9086 	      && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
9087 	      && sym->attr.flavor == FL_UNKNOWN)
9088 	    sym->attr.flavor = FL_PROCEDURE;
9089 
9090 	  if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
9091 	    goto done;
9092 
9093 	  if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
9094 	      && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
9095 	    goto done;
9096 
9097 	  break;
9098 
9099 	case INTERFACE_INTRINSIC_OP:
9100 	  if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
9101 	    {
9102 	      gfc_intrinsic_op other_op;
9103 
9104 	      gfc_current_ns->operator_access[op] = access;
9105 
9106 	      /* Handle the case if there is another op with the same
9107 		 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on.  */
9108 	      other_op = gfc_equivalent_op (op);
9109 
9110 	      if (other_op != INTRINSIC_NONE)
9111 		gfc_current_ns->operator_access[other_op] = access;
9112 	    }
9113 	  else
9114 	    {
9115 	      gfc_error ("Access specification of the %s operator at %C has "
9116 			 "already been specified", gfc_op2string (op));
9117 	      goto done;
9118 	    }
9119 
9120 	  break;
9121 
9122 	case INTERFACE_USER_OP:
9123 	  uop = gfc_get_uop (name);
9124 
9125 	  if (uop->access == ACCESS_UNKNOWN)
9126 	    {
9127 	      uop->access = access;
9128 	    }
9129 	  else
9130 	    {
9131 	      gfc_error ("Access specification of the .%s. operator at %C "
9132 			 "has already been specified", uop->name);
9133 	      goto done;
9134 	    }
9135 
9136 	  break;
9137 	}
9138 
9139       if (gfc_match_char (',') == MATCH_NO)
9140 	break;
9141     }
9142 
9143   if (gfc_match_eos () != MATCH_YES)
9144     goto syntax;
9145   return MATCH_YES;
9146 
9147 syntax:
9148   gfc_syntax_error (st);
9149 
9150 done:
9151   return MATCH_ERROR;
9152 }
9153 
9154 
9155 match
gfc_match_protected(void)9156 gfc_match_protected (void)
9157 {
9158   gfc_symbol *sym;
9159   match m;
9160   char c;
9161 
9162   /* PROTECTED has already been seen, but must be followed by whitespace
9163      or ::.  */
9164   c = gfc_peek_ascii_char ();
9165   if (!gfc_is_whitespace (c) && c != ':')
9166     return MATCH_NO;
9167 
9168   if (!gfc_current_ns->proc_name
9169       || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
9170     {
9171        gfc_error ("PROTECTED at %C only allowed in specification "
9172 		  "part of a module");
9173        return MATCH_ERROR;
9174 
9175     }
9176 
9177   gfc_match (" ::");
9178 
9179   if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
9180     return MATCH_ERROR;
9181 
9182   /* PROTECTED has an entity-list.  */
9183   if (gfc_match_eos () == MATCH_YES)
9184     goto syntax;
9185 
9186   for(;;)
9187     {
9188       m = gfc_match_symbol (&sym, 0);
9189       switch (m)
9190 	{
9191 	case MATCH_YES:
9192 	  if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
9193 	    return MATCH_ERROR;
9194 	  goto next_item;
9195 
9196 	case MATCH_NO:
9197 	  break;
9198 
9199 	case MATCH_ERROR:
9200 	  return MATCH_ERROR;
9201 	}
9202 
9203     next_item:
9204       if (gfc_match_eos () == MATCH_YES)
9205 	break;
9206       if (gfc_match_char (',') != MATCH_YES)
9207 	goto syntax;
9208     }
9209 
9210   return MATCH_YES;
9211 
9212 syntax:
9213   gfc_error ("Syntax error in PROTECTED statement at %C");
9214   return MATCH_ERROR;
9215 }
9216 
9217 
9218 /* The PRIVATE statement is a bit weird in that it can be an attribute
9219    declaration, but also works as a standalone statement inside of a
9220    type declaration or a module.  */
9221 
9222 match
gfc_match_private(gfc_statement * st)9223 gfc_match_private (gfc_statement *st)
9224 {
9225   gfc_state_data *prev;
9226 
9227   if (gfc_match ("private") != MATCH_YES)
9228     return MATCH_NO;
9229 
9230   /* Try matching PRIVATE without an access-list.  */
9231   if (gfc_match_eos () == MATCH_YES)
9232     {
9233       prev = gfc_state_stack->previous;
9234       if (gfc_current_state () != COMP_MODULE
9235 	  && !(gfc_current_state () == COMP_DERIVED
9236 		&& prev && prev->state == COMP_MODULE)
9237 	  && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9238 		&& prev->previous && prev->previous->state == COMP_MODULE))
9239 	{
9240 	  gfc_error ("PRIVATE statement at %C is only allowed in the "
9241 		     "specification part of a module");
9242 	  return MATCH_ERROR;
9243 	}
9244 
9245       *st = ST_PRIVATE;
9246       return MATCH_YES;
9247     }
9248 
9249   /* At this point in free-form source code, PRIVATE must be followed
9250      by whitespace or ::.  */
9251   if (gfc_current_form == FORM_FREE)
9252     {
9253       char c = gfc_peek_ascii_char ();
9254       if (!gfc_is_whitespace (c) && c != ':')
9255 	return MATCH_NO;
9256     }
9257 
9258   prev = gfc_state_stack->previous;
9259   if (gfc_current_state () != COMP_MODULE
9260       && !(gfc_current_state () == COMP_DERIVED
9261 	   && prev && prev->state == COMP_MODULE)
9262       && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9263 	   && prev->previous && prev->previous->state == COMP_MODULE))
9264     {
9265       gfc_error ("PRIVATE statement at %C is only allowed in the "
9266 		 "specification part of a module");
9267       return MATCH_ERROR;
9268     }
9269 
9270   *st = ST_ATTR_DECL;
9271   return access_attr_decl (ST_PRIVATE);
9272 }
9273 
9274 
9275 match
gfc_match_public(gfc_statement * st)9276 gfc_match_public (gfc_statement *st)
9277 {
9278   if (gfc_match ("public") != MATCH_YES)
9279     return MATCH_NO;
9280 
9281   /* Try matching PUBLIC without an access-list.  */
9282   if (gfc_match_eos () == MATCH_YES)
9283     {
9284       if (gfc_current_state () != COMP_MODULE)
9285 	{
9286 	  gfc_error ("PUBLIC statement at %C is only allowed in the "
9287 		     "specification part of a module");
9288 	  return MATCH_ERROR;
9289 	}
9290 
9291       *st = ST_PUBLIC;
9292       return MATCH_YES;
9293     }
9294 
9295   /* At this point in free-form source code, PUBLIC must be followed
9296      by whitespace or ::.  */
9297   if (gfc_current_form == FORM_FREE)
9298     {
9299       char c = gfc_peek_ascii_char ();
9300       if (!gfc_is_whitespace (c) && c != ':')
9301 	return MATCH_NO;
9302     }
9303 
9304   if (gfc_current_state () != COMP_MODULE)
9305     {
9306       gfc_error ("PUBLIC statement at %C is only allowed in the "
9307 		 "specification part of a module");
9308       return MATCH_ERROR;
9309     }
9310 
9311   *st = ST_ATTR_DECL;
9312   return access_attr_decl (ST_PUBLIC);
9313 }
9314 
9315 
9316 /* Workhorse for gfc_match_parameter.  */
9317 
9318 static match
do_parm(void)9319 do_parm (void)
9320 {
9321   gfc_symbol *sym;
9322   gfc_expr *init;
9323   match m;
9324   bool t;
9325 
9326   m = gfc_match_symbol (&sym, 0);
9327   if (m == MATCH_NO)
9328     gfc_error ("Expected variable name at %C in PARAMETER statement");
9329 
9330   if (m != MATCH_YES)
9331     return m;
9332 
9333   if (gfc_match_char ('=') == MATCH_NO)
9334     {
9335       gfc_error ("Expected = sign in PARAMETER statement at %C");
9336       return MATCH_ERROR;
9337     }
9338 
9339   m = gfc_match_init_expr (&init);
9340   if (m == MATCH_NO)
9341     gfc_error ("Expected expression at %C in PARAMETER statement");
9342   if (m != MATCH_YES)
9343     return m;
9344 
9345   if (sym->ts.type == BT_UNKNOWN
9346       && !gfc_set_default_type (sym, 1, NULL))
9347     {
9348       m = MATCH_ERROR;
9349       goto cleanup;
9350     }
9351 
9352   if (!gfc_check_assign_symbol (sym, NULL, init)
9353       || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
9354     {
9355       m = MATCH_ERROR;
9356       goto cleanup;
9357     }
9358 
9359   if (sym->value)
9360     {
9361       gfc_error ("Initializing already initialized variable at %C");
9362       m = MATCH_ERROR;
9363       goto cleanup;
9364     }
9365 
9366   t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
9367   return (t) ? MATCH_YES : MATCH_ERROR;
9368 
9369 cleanup:
9370   gfc_free_expr (init);
9371   return m;
9372 }
9373 
9374 
9375 /* Match a parameter statement, with the weird syntax that these have.  */
9376 
9377 match
gfc_match_parameter(void)9378 gfc_match_parameter (void)
9379 {
9380   const char *term = " )%t";
9381   match m;
9382 
9383   if (gfc_match_char ('(') == MATCH_NO)
9384     {
9385       /* With legacy PARAMETER statements, don't expect a terminating ')'.  */
9386       if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
9387 	return MATCH_NO;
9388       term = " %t";
9389     }
9390 
9391   for (;;)
9392     {
9393       m = do_parm ();
9394       if (m != MATCH_YES)
9395 	break;
9396 
9397       if (gfc_match (term) == MATCH_YES)
9398 	break;
9399 
9400       if (gfc_match_char (',') != MATCH_YES)
9401 	{
9402 	  gfc_error ("Unexpected characters in PARAMETER statement at %C");
9403 	  m = MATCH_ERROR;
9404 	  break;
9405 	}
9406     }
9407 
9408   return m;
9409 }
9410 
9411 
9412 match
gfc_match_automatic(void)9413 gfc_match_automatic (void)
9414 {
9415   gfc_symbol *sym;
9416   match m;
9417   bool seen_symbol = false;
9418 
9419   if (!flag_dec_static)
9420     {
9421       gfc_error ("%s at %C is a DEC extension, enable with "
9422 		 "%<-fdec-static%>",
9423 		 "AUTOMATIC"
9424 		 );
9425       return MATCH_ERROR;
9426     }
9427 
9428   gfc_match (" ::");
9429 
9430   for (;;)
9431     {
9432       m = gfc_match_symbol (&sym, 0);
9433       switch (m)
9434       {
9435       case MATCH_NO:
9436         break;
9437 
9438       case MATCH_ERROR:
9439 	return MATCH_ERROR;
9440 
9441       case MATCH_YES:
9442 	if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
9443 	  return MATCH_ERROR;
9444 	seen_symbol = true;
9445 	break;
9446       }
9447 
9448       if (gfc_match_eos () == MATCH_YES)
9449 	break;
9450       if (gfc_match_char (',') != MATCH_YES)
9451 	goto syntax;
9452     }
9453 
9454   if (!seen_symbol)
9455     {
9456       gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
9457       return MATCH_ERROR;
9458     }
9459 
9460   return MATCH_YES;
9461 
9462 syntax:
9463   gfc_error ("Syntax error in AUTOMATIC statement at %C");
9464   return MATCH_ERROR;
9465 }
9466 
9467 
9468 match
gfc_match_static(void)9469 gfc_match_static (void)
9470 {
9471   gfc_symbol *sym;
9472   match m;
9473   bool seen_symbol = false;
9474 
9475   if (!flag_dec_static)
9476     {
9477       gfc_error ("%s at %C is a DEC extension, enable with "
9478 		 "%<-fdec-static%>",
9479 		 "STATIC");
9480       return MATCH_ERROR;
9481     }
9482 
9483   gfc_match (" ::");
9484 
9485   for (;;)
9486     {
9487       m = gfc_match_symbol (&sym, 0);
9488       switch (m)
9489       {
9490       case MATCH_NO:
9491         break;
9492 
9493       case MATCH_ERROR:
9494 	return MATCH_ERROR;
9495 
9496       case MATCH_YES:
9497 	if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9498 			  &gfc_current_locus))
9499 	  return MATCH_ERROR;
9500 	seen_symbol = true;
9501 	break;
9502       }
9503 
9504       if (gfc_match_eos () == MATCH_YES)
9505 	break;
9506       if (gfc_match_char (',') != MATCH_YES)
9507 	goto syntax;
9508     }
9509 
9510   if (!seen_symbol)
9511     {
9512       gfc_error ("Expected entity-list in STATIC statement at %C");
9513       return MATCH_ERROR;
9514     }
9515 
9516   return MATCH_YES;
9517 
9518 syntax:
9519   gfc_error ("Syntax error in STATIC statement at %C");
9520   return MATCH_ERROR;
9521 }
9522 
9523 
9524 /* Save statements have a special syntax.  */
9525 
9526 match
gfc_match_save(void)9527 gfc_match_save (void)
9528 {
9529   char n[GFC_MAX_SYMBOL_LEN+1];
9530   gfc_common_head *c;
9531   gfc_symbol *sym;
9532   match m;
9533 
9534   if (gfc_match_eos () == MATCH_YES)
9535     {
9536       if (gfc_current_ns->seen_save)
9537 	{
9538 	  if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9539 			       "follows previous SAVE statement"))
9540 	    return MATCH_ERROR;
9541 	}
9542 
9543       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9544       return MATCH_YES;
9545     }
9546 
9547   if (gfc_current_ns->save_all)
9548     {
9549       if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9550 			   "blanket SAVE statement"))
9551 	return MATCH_ERROR;
9552     }
9553 
9554   gfc_match (" ::");
9555 
9556   for (;;)
9557     {
9558       m = gfc_match_symbol (&sym, 0);
9559       switch (m)
9560 	{
9561 	case MATCH_YES:
9562 	  if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9563 			     &gfc_current_locus))
9564 	    return MATCH_ERROR;
9565 	  goto next_item;
9566 
9567 	case MATCH_NO:
9568 	  break;
9569 
9570 	case MATCH_ERROR:
9571 	  return MATCH_ERROR;
9572 	}
9573 
9574       m = gfc_match (" / %n /", &n);
9575       if (m == MATCH_ERROR)
9576 	return MATCH_ERROR;
9577       if (m == MATCH_NO)
9578 	goto syntax;
9579 
9580       c = gfc_get_common (n, 0);
9581       c->saved = 1;
9582 
9583       gfc_current_ns->seen_save = 1;
9584 
9585     next_item:
9586       if (gfc_match_eos () == MATCH_YES)
9587 	break;
9588       if (gfc_match_char (',') != MATCH_YES)
9589 	goto syntax;
9590     }
9591 
9592   return MATCH_YES;
9593 
9594 syntax:
9595   if (gfc_current_ns->seen_save)
9596     {
9597       gfc_error ("Syntax error in SAVE statement at %C");
9598       return MATCH_ERROR;
9599     }
9600   else
9601       return MATCH_NO;
9602 }
9603 
9604 
9605 match
gfc_match_value(void)9606 gfc_match_value (void)
9607 {
9608   gfc_symbol *sym;
9609   match m;
9610 
9611   /* This is not allowed within a BLOCK construct!  */
9612   if (gfc_current_state () == COMP_BLOCK)
9613     {
9614       gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9615       return MATCH_ERROR;
9616     }
9617 
9618   if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
9619     return MATCH_ERROR;
9620 
9621   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9622     {
9623       return MATCH_ERROR;
9624     }
9625 
9626   if (gfc_match_eos () == MATCH_YES)
9627     goto syntax;
9628 
9629   for(;;)
9630     {
9631       m = gfc_match_symbol (&sym, 0);
9632       switch (m)
9633 	{
9634 	case MATCH_YES:
9635 	  if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
9636 	    return MATCH_ERROR;
9637 	  goto next_item;
9638 
9639 	case MATCH_NO:
9640 	  break;
9641 
9642 	case MATCH_ERROR:
9643 	  return MATCH_ERROR;
9644 	}
9645 
9646     next_item:
9647       if (gfc_match_eos () == MATCH_YES)
9648 	break;
9649       if (gfc_match_char (',') != MATCH_YES)
9650 	goto syntax;
9651     }
9652 
9653   return MATCH_YES;
9654 
9655 syntax:
9656   gfc_error ("Syntax error in VALUE statement at %C");
9657   return MATCH_ERROR;
9658 }
9659 
9660 
9661 match
gfc_match_volatile(void)9662 gfc_match_volatile (void)
9663 {
9664   gfc_symbol *sym;
9665   char *name;
9666   match m;
9667 
9668   if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
9669     return MATCH_ERROR;
9670 
9671   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9672     {
9673       return MATCH_ERROR;
9674     }
9675 
9676   if (gfc_match_eos () == MATCH_YES)
9677     goto syntax;
9678 
9679   for(;;)
9680     {
9681       /* VOLATILE is special because it can be added to host-associated
9682 	 symbols locally.  Except for coarrays.  */
9683       m = gfc_match_symbol (&sym, 1);
9684       switch (m)
9685 	{
9686 	case MATCH_YES:
9687 	  name = XCNEWVAR (char, strlen (sym->name) + 1);
9688 	  strcpy (name, sym->name);
9689 	  if (!check_function_name (name))
9690 	    return MATCH_ERROR;
9691 	  /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9692 	     for variable in a BLOCK which is defined outside of the BLOCK.  */
9693 	  if (sym->ns != gfc_current_ns && sym->attr.codimension)
9694 	    {
9695 	      gfc_error ("Specifying VOLATILE for coarray variable %qs at "
9696 			 "%C, which is use-/host-associated", sym->name);
9697 	      return MATCH_ERROR;
9698 	    }
9699 	  if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
9700 	    return MATCH_ERROR;
9701 	  goto next_item;
9702 
9703 	case MATCH_NO:
9704 	  break;
9705 
9706 	case MATCH_ERROR:
9707 	  return MATCH_ERROR;
9708 	}
9709 
9710     next_item:
9711       if (gfc_match_eos () == MATCH_YES)
9712 	break;
9713       if (gfc_match_char (',') != MATCH_YES)
9714 	goto syntax;
9715     }
9716 
9717   return MATCH_YES;
9718 
9719 syntax:
9720   gfc_error ("Syntax error in VOLATILE statement at %C");
9721   return MATCH_ERROR;
9722 }
9723 
9724 
9725 match
gfc_match_asynchronous(void)9726 gfc_match_asynchronous (void)
9727 {
9728   gfc_symbol *sym;
9729   char *name;
9730   match m;
9731 
9732   if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
9733     return MATCH_ERROR;
9734 
9735   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9736     {
9737       return MATCH_ERROR;
9738     }
9739 
9740   if (gfc_match_eos () == MATCH_YES)
9741     goto syntax;
9742 
9743   for(;;)
9744     {
9745       /* ASYNCHRONOUS is special because it can be added to host-associated
9746 	 symbols locally.  */
9747       m = gfc_match_symbol (&sym, 1);
9748       switch (m)
9749 	{
9750 	case MATCH_YES:
9751 	  name = XCNEWVAR (char, strlen (sym->name) + 1);
9752 	  strcpy (name, sym->name);
9753 	  if (!check_function_name (name))
9754 	    return MATCH_ERROR;
9755 	  if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9756 	    return MATCH_ERROR;
9757 	  goto next_item;
9758 
9759 	case MATCH_NO:
9760 	  break;
9761 
9762 	case MATCH_ERROR:
9763 	  return MATCH_ERROR;
9764 	}
9765 
9766     next_item:
9767       if (gfc_match_eos () == MATCH_YES)
9768 	break;
9769       if (gfc_match_char (',') != MATCH_YES)
9770 	goto syntax;
9771     }
9772 
9773   return MATCH_YES;
9774 
9775 syntax:
9776   gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9777   return MATCH_ERROR;
9778 }
9779 
9780 
9781 /* Match a module procedure statement in a submodule.  */
9782 
9783 match
gfc_match_submod_proc(void)9784 gfc_match_submod_proc (void)
9785 {
9786   char name[GFC_MAX_SYMBOL_LEN + 1];
9787   gfc_symbol *sym, *fsym;
9788   match m;
9789   gfc_formal_arglist *formal, *head, *tail;
9790 
9791   if (gfc_current_state () != COMP_CONTAINS
9792       || !(gfc_state_stack->previous
9793 	   && (gfc_state_stack->previous->state == COMP_SUBMODULE
9794 	       || gfc_state_stack->previous->state == COMP_MODULE)))
9795     return MATCH_NO;
9796 
9797   m = gfc_match (" module% procedure% %n", name);
9798   if (m != MATCH_YES)
9799     return m;
9800 
9801   if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9802 				      "at %C"))
9803     return MATCH_ERROR;
9804 
9805   if (get_proc_name (name, &sym, false))
9806     return MATCH_ERROR;
9807 
9808   /* Make sure that the result field is appropriately filled.  */
9809   if (sym->tlink && sym->tlink->attr.function)
9810     {
9811       if (sym->tlink->result && sym->tlink->result != sym->tlink)
9812 	{
9813 	  sym->result = sym->tlink->result;
9814 	  if (!sym->result->attr.use_assoc)
9815 	    {
9816 	      gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
9817 						 sym->result->name);
9818 	      st->n.sym = sym->result;
9819 	      sym->result->refs++;
9820 	    }
9821 	}
9822       else
9823 	sym->result = sym;
9824     }
9825 
9826   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9827      the symbol existed before.  */
9828   sym->declared_at = gfc_current_locus;
9829 
9830   if (!sym->attr.module_procedure)
9831     return MATCH_ERROR;
9832 
9833   /* Signal match_end to expect "end procedure".  */
9834   sym->abr_modproc_decl = 1;
9835 
9836   /* Change from IFSRC_IFBODY coming from the interface declaration.  */
9837   sym->attr.if_source = IFSRC_DECL;
9838 
9839   gfc_new_block = sym;
9840 
9841   /* Make a new formal arglist with the symbols in the procedure
9842       namespace.  */
9843   head = tail = NULL;
9844   for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9845     {
9846       if (formal == sym->formal)
9847 	head = tail = gfc_get_formal_arglist ();
9848       else
9849 	{
9850 	  tail->next = gfc_get_formal_arglist ();
9851 	  tail = tail->next;
9852 	}
9853 
9854       if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9855 	goto cleanup;
9856 
9857       tail->sym = fsym;
9858       gfc_set_sym_referenced (fsym);
9859     }
9860 
9861   /* The dummy symbols get cleaned up, when the formal_namespace of the
9862      interface declaration is cleared.  This allows us to add the
9863      explicit interface as is done for other type of procedure.  */
9864   if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9865 				   &gfc_current_locus))
9866     return MATCH_ERROR;
9867 
9868   if (gfc_match_eos () != MATCH_YES)
9869     {
9870       /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
9871 	 undone, such that the st->n.sym->formal points to the original symbol;
9872 	 if now this namespace is finalized, the formal namespace is freed,
9873 	 but it might be still needed in the parent namespace.  */
9874       gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
9875       st->n.sym = NULL;
9876       gfc_free_symbol (sym->tlink);
9877       sym->tlink = NULL;
9878       sym->refs--;
9879       gfc_syntax_error (ST_MODULE_PROC);
9880       return MATCH_ERROR;
9881     }
9882 
9883   return MATCH_YES;
9884 
9885 cleanup:
9886   gfc_free_formal_arglist (head);
9887   return MATCH_ERROR;
9888 }
9889 
9890 
9891 /* Match a module procedure statement.  Note that we have to modify
9892    symbols in the parent's namespace because the current one was there
9893    to receive symbols that are in an interface's formal argument list.  */
9894 
9895 match
gfc_match_modproc(void)9896 gfc_match_modproc (void)
9897 {
9898   char name[GFC_MAX_SYMBOL_LEN + 1];
9899   gfc_symbol *sym;
9900   match m;
9901   locus old_locus;
9902   gfc_namespace *module_ns;
9903   gfc_interface *old_interface_head, *interface;
9904 
9905   if (gfc_state_stack->state != COMP_INTERFACE
9906       || gfc_state_stack->previous == NULL
9907       || current_interface.type == INTERFACE_NAMELESS
9908       || current_interface.type == INTERFACE_ABSTRACT)
9909     {
9910       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9911 		 "interface");
9912       return MATCH_ERROR;
9913     }
9914 
9915   module_ns = gfc_current_ns->parent;
9916   for (; module_ns; module_ns = module_ns->parent)
9917     if (module_ns->proc_name->attr.flavor == FL_MODULE
9918 	|| module_ns->proc_name->attr.flavor == FL_PROGRAM
9919 	|| (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9920 	    && !module_ns->proc_name->attr.contained))
9921       break;
9922 
9923   if (module_ns == NULL)
9924     return MATCH_ERROR;
9925 
9926   /* Store the current state of the interface. We will need it if we
9927      end up with a syntax error and need to recover.  */
9928   old_interface_head = gfc_current_interface_head ();
9929 
9930   /* Check if the F2008 optional double colon appears.  */
9931   gfc_gobble_whitespace ();
9932   old_locus = gfc_current_locus;
9933   if (gfc_match ("::") == MATCH_YES)
9934     {
9935       if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9936 			   "MODULE PROCEDURE statement at %L", &old_locus))
9937 	return MATCH_ERROR;
9938     }
9939   else
9940     gfc_current_locus = old_locus;
9941 
9942   for (;;)
9943     {
9944       bool last = false;
9945       old_locus = gfc_current_locus;
9946 
9947       m = gfc_match_name (name);
9948       if (m == MATCH_NO)
9949 	goto syntax;
9950       if (m != MATCH_YES)
9951 	return MATCH_ERROR;
9952 
9953       /* Check for syntax error before starting to add symbols to the
9954 	 current namespace.  */
9955       if (gfc_match_eos () == MATCH_YES)
9956 	last = true;
9957 
9958       if (!last && gfc_match_char (',') != MATCH_YES)
9959 	goto syntax;
9960 
9961       /* Now we're sure the syntax is valid, we process this item
9962 	 further.  */
9963       if (gfc_get_symbol (name, module_ns, &sym))
9964 	return MATCH_ERROR;
9965 
9966       if (sym->attr.intrinsic)
9967 	{
9968 	  gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9969 		     "PROCEDURE", &old_locus);
9970 	  return MATCH_ERROR;
9971 	}
9972 
9973       if (sym->attr.proc != PROC_MODULE
9974 	  && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9975 	return MATCH_ERROR;
9976 
9977       if (!gfc_add_interface (sym))
9978 	return MATCH_ERROR;
9979 
9980       sym->attr.mod_proc = 1;
9981       sym->declared_at = old_locus;
9982 
9983       if (last)
9984 	break;
9985     }
9986 
9987   return MATCH_YES;
9988 
9989 syntax:
9990   /* Restore the previous state of the interface.  */
9991   interface = gfc_current_interface_head ();
9992   gfc_set_current_interface_head (old_interface_head);
9993 
9994   /* Free the new interfaces.  */
9995   while (interface != old_interface_head)
9996   {
9997     gfc_interface *i = interface->next;
9998     free (interface);
9999     interface = i;
10000   }
10001 
10002   /* And issue a syntax error.  */
10003   gfc_syntax_error (ST_MODULE_PROC);
10004   return MATCH_ERROR;
10005 }
10006 
10007 
10008 /* Check a derived type that is being extended.  */
10009 
10010 static gfc_symbol*
check_extended_derived_type(char * name)10011 check_extended_derived_type (char *name)
10012 {
10013   gfc_symbol *extended;
10014 
10015   if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
10016     {
10017       gfc_error ("Ambiguous symbol in TYPE definition at %C");
10018       return NULL;
10019     }
10020 
10021   extended = gfc_find_dt_in_generic (extended);
10022 
10023   /* F08:C428.  */
10024   if (!extended)
10025     {
10026       gfc_error ("Symbol %qs at %C has not been previously defined", name);
10027       return NULL;
10028     }
10029 
10030   if (extended->attr.flavor != FL_DERIVED)
10031     {
10032       gfc_error ("%qs in EXTENDS expression at %C is not a "
10033 		 "derived type", name);
10034       return NULL;
10035     }
10036 
10037   if (extended->attr.is_bind_c)
10038     {
10039       gfc_error ("%qs cannot be extended at %C because it "
10040 		 "is BIND(C)", extended->name);
10041       return NULL;
10042     }
10043 
10044   if (extended->attr.sequence)
10045     {
10046       gfc_error ("%qs cannot be extended at %C because it "
10047 		 "is a SEQUENCE type", extended->name);
10048       return NULL;
10049     }
10050 
10051   return extended;
10052 }
10053 
10054 
10055 /* Match the optional attribute specifiers for a type declaration.
10056    Return MATCH_ERROR if an error is encountered in one of the handled
10057    attributes (public, private, bind(c)), MATCH_NO if what's found is
10058    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
10059    checking on attribute conflicts needs to be done.  */
10060 
10061 match
gfc_get_type_attr_spec(symbol_attribute * attr,char * name)10062 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
10063 {
10064   /* See if the derived type is marked as private.  */
10065   if (gfc_match (" , private") == MATCH_YES)
10066     {
10067       if (gfc_current_state () != COMP_MODULE)
10068 	{
10069 	  gfc_error ("Derived type at %C can only be PRIVATE in the "
10070 		     "specification part of a module");
10071 	  return MATCH_ERROR;
10072 	}
10073 
10074       if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
10075 	return MATCH_ERROR;
10076     }
10077   else if (gfc_match (" , public") == MATCH_YES)
10078     {
10079       if (gfc_current_state () != COMP_MODULE)
10080 	{
10081 	  gfc_error ("Derived type at %C can only be PUBLIC in the "
10082 		     "specification part of a module");
10083 	  return MATCH_ERROR;
10084 	}
10085 
10086       if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
10087 	return MATCH_ERROR;
10088     }
10089   else if (gfc_match (" , bind ( c )") == MATCH_YES)
10090     {
10091       /* If the type is defined to be bind(c) it then needs to make
10092 	 sure that all fields are interoperable.  This will
10093 	 need to be a semantic check on the finished derived type.
10094 	 See 15.2.3 (lines 9-12) of F2003 draft.  */
10095       if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
10096 	return MATCH_ERROR;
10097 
10098       /* TODO: attr conflicts need to be checked, probably in symbol.c.  */
10099     }
10100   else if (gfc_match (" , abstract") == MATCH_YES)
10101     {
10102       if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
10103 	return MATCH_ERROR;
10104 
10105       if (!gfc_add_abstract (attr, &gfc_current_locus))
10106 	return MATCH_ERROR;
10107     }
10108   else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
10109     {
10110       if (!gfc_add_extension (attr, &gfc_current_locus))
10111 	return MATCH_ERROR;
10112     }
10113   else
10114     return MATCH_NO;
10115 
10116   /* If we get here, something matched.  */
10117   return MATCH_YES;
10118 }
10119 
10120 
10121 /* Common function for type declaration blocks similar to derived types, such
10122    as STRUCTURES and MAPs. Unlike derived types, a structure type
10123    does NOT have a generic symbol matching the name given by the user.
10124    STRUCTUREs can share names with variables and PARAMETERs so we must allow
10125    for the creation of an independent symbol.
10126    Other parameters are a message to prefix errors with, the name of the new
10127    type to be created, and the flavor to add to the resulting symbol. */
10128 
10129 static bool
get_struct_decl(const char * name,sym_flavor fl,locus * decl,gfc_symbol ** result)10130 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
10131                  gfc_symbol **result)
10132 {
10133   gfc_symbol *sym;
10134   locus where;
10135 
10136   gcc_assert (name[0] == (char) TOUPPER (name[0]));
10137 
10138   if (decl)
10139     where = *decl;
10140   else
10141     where = gfc_current_locus;
10142 
10143   if (gfc_get_symbol (name, NULL, &sym))
10144     return false;
10145 
10146   if (!sym)
10147     {
10148       gfc_internal_error ("Failed to create structure type '%s' at %C", name);
10149       return false;
10150     }
10151 
10152   if (sym->components != NULL || sym->attr.zero_comp)
10153     {
10154       gfc_error ("Type definition of %qs at %C was already defined at %L",
10155                  sym->name, &sym->declared_at);
10156       return false;
10157     }
10158 
10159   sym->declared_at = where;
10160 
10161   if (sym->attr.flavor != fl
10162       && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
10163     return false;
10164 
10165   if (!sym->hash_value)
10166       /* Set the hash for the compound name for this type.  */
10167     sym->hash_value = gfc_hash_value (sym);
10168 
10169   /* Normally the type is expected to have been completely parsed by the time
10170      a field declaration with this type is seen. For unions, maps, and nested
10171      structure declarations, we need to indicate that it is okay that we
10172      haven't seen any components yet. This will be updated after the structure
10173      is fully parsed. */
10174   sym->attr.zero_comp = 0;
10175 
10176   /* Structures always act like derived-types with the SEQUENCE attribute */
10177   gfc_add_sequence (&sym->attr, sym->name, NULL);
10178 
10179   if (result) *result = sym;
10180 
10181   return true;
10182 }
10183 
10184 
10185 /* Match the opening of a MAP block. Like a struct within a union in C;
10186    behaves identical to STRUCTURE blocks.  */
10187 
10188 match
gfc_match_map(void)10189 gfc_match_map (void)
10190 {
10191   /* Counter used to give unique internal names to map structures. */
10192   static unsigned int gfc_map_id = 0;
10193   char name[GFC_MAX_SYMBOL_LEN + 1];
10194   gfc_symbol *sym;
10195   locus old_loc;
10196 
10197   old_loc = gfc_current_locus;
10198 
10199   if (gfc_match_eos () != MATCH_YES)
10200     {
10201 	gfc_error ("Junk after MAP statement at %C");
10202 	gfc_current_locus = old_loc;
10203 	return MATCH_ERROR;
10204     }
10205 
10206   /* Map blocks are anonymous so we make up unique names for the symbol table
10207      which are invalid Fortran identifiers.  */
10208   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
10209 
10210   if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
10211     return MATCH_ERROR;
10212 
10213   gfc_new_block = sym;
10214 
10215   return MATCH_YES;
10216 }
10217 
10218 
10219 /* Match the opening of a UNION block.  */
10220 
10221 match
gfc_match_union(void)10222 gfc_match_union (void)
10223 {
10224   /* Counter used to give unique internal names to union types. */
10225   static unsigned int gfc_union_id = 0;
10226   char name[GFC_MAX_SYMBOL_LEN + 1];
10227   gfc_symbol *sym;
10228   locus old_loc;
10229 
10230   old_loc = gfc_current_locus;
10231 
10232   if (gfc_match_eos () != MATCH_YES)
10233     {
10234 	gfc_error ("Junk after UNION statement at %C");
10235 	gfc_current_locus = old_loc;
10236 	return MATCH_ERROR;
10237     }
10238 
10239   /* Unions are anonymous so we make up unique names for the symbol table
10240      which are invalid Fortran identifiers.  */
10241   snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
10242 
10243   if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
10244     return MATCH_ERROR;
10245 
10246   gfc_new_block = sym;
10247 
10248   return MATCH_YES;
10249 }
10250 
10251 
10252 /* Match the beginning of a STRUCTURE declaration. This is similar to
10253    matching the beginning of a derived type declaration with a few
10254    twists. The resulting type symbol has no access control or other
10255    interesting attributes.  */
10256 
10257 match
gfc_match_structure_decl(void)10258 gfc_match_structure_decl (void)
10259 {
10260   /* Counter used to give unique internal names to anonymous structures.  */
10261   static unsigned int gfc_structure_id = 0;
10262   char name[GFC_MAX_SYMBOL_LEN + 1];
10263   gfc_symbol *sym;
10264   match m;
10265   locus where;
10266 
10267   if (!flag_dec_structure)
10268     {
10269       gfc_error ("%s at %C is a DEC extension, enable with "
10270 		 "%<-fdec-structure%>",
10271 		 "STRUCTURE");
10272       return MATCH_ERROR;
10273     }
10274 
10275   name[0] = '\0';
10276 
10277   m = gfc_match (" /%n/", name);
10278   if (m != MATCH_YES)
10279     {
10280       /* Non-nested structure declarations require a structure name.  */
10281       if (!gfc_comp_struct (gfc_current_state ()))
10282 	{
10283 	    gfc_error ("Structure name expected in non-nested structure "
10284 		       "declaration at %C");
10285 	    return MATCH_ERROR;
10286 	}
10287       /* This is an anonymous structure; make up a unique name for it
10288 	 (upper-case letters never make it to symbol names from the source).
10289 	 The important thing is initializing the type variable
10290 	 and setting gfc_new_symbol, which is immediately used by
10291 	 parse_structure () and variable_decl () to add components of
10292 	 this type.  */
10293       snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
10294     }
10295 
10296   where = gfc_current_locus;
10297   /* No field list allowed after non-nested structure declaration.  */
10298   if (!gfc_comp_struct (gfc_current_state ())
10299       && gfc_match_eos () != MATCH_YES)
10300     {
10301       gfc_error ("Junk after non-nested STRUCTURE statement at %C");
10302       return MATCH_ERROR;
10303     }
10304 
10305   /* Make sure the name is not the name of an intrinsic type.  */
10306   if (gfc_is_intrinsic_typename (name))
10307     {
10308       gfc_error ("Structure name %qs at %C cannot be the same as an"
10309 		 " intrinsic type", name);
10310       return MATCH_ERROR;
10311     }
10312 
10313   /* Store the actual type symbol for the structure with an upper-case first
10314      letter (an invalid Fortran identifier).  */
10315 
10316   if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
10317     return MATCH_ERROR;
10318 
10319   gfc_new_block = sym;
10320   return MATCH_YES;
10321 }
10322 
10323 
10324 /* This function does some work to determine which matcher should be used to
10325  * match a statement beginning with "TYPE".  This is used to disambiguate TYPE
10326  * as an alias for PRINT from derived type declarations, TYPE IS statements,
10327  * and [parameterized] derived type declarations.  */
10328 
10329 match
gfc_match_type(gfc_statement * st)10330 gfc_match_type (gfc_statement *st)
10331 {
10332   char name[GFC_MAX_SYMBOL_LEN + 1];
10333   match m;
10334   locus old_loc;
10335 
10336   /* Requires -fdec.  */
10337   if (!flag_dec)
10338     return MATCH_NO;
10339 
10340   m = gfc_match ("type");
10341   if (m != MATCH_YES)
10342     return m;
10343   /* If we already have an error in the buffer, it is probably from failing to
10344    * match a derived type data declaration. Let it happen.  */
10345   else if (gfc_error_flag_test ())
10346     return MATCH_NO;
10347 
10348   old_loc = gfc_current_locus;
10349   *st = ST_NONE;
10350 
10351   /* If we see an attribute list before anything else it's definitely a derived
10352    * type declaration.  */
10353   if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
10354     goto derived;
10355 
10356   /* By now "TYPE" has already been matched. If we do not see a name, this may
10357    * be something like "TYPE *" or "TYPE <fmt>".  */
10358   m = gfc_match_name (name);
10359   if (m != MATCH_YES)
10360     {
10361       /* Let print match if it can, otherwise throw an error from
10362        * gfc_match_derived_decl.  */
10363       gfc_current_locus = old_loc;
10364       if (gfc_match_print () == MATCH_YES)
10365 	{
10366 	  *st = ST_WRITE;
10367 	  return MATCH_YES;
10368 	}
10369       goto derived;
10370     }
10371 
10372   /* Check for EOS.  */
10373   if (gfc_match_eos () == MATCH_YES)
10374     {
10375       /* By now we have "TYPE <name> <EOS>". Check first if the name is an
10376        * intrinsic typename - if so let gfc_match_derived_decl dump an error.
10377        * Otherwise if gfc_match_derived_decl fails it's probably an existing
10378        * symbol which can be printed.  */
10379       gfc_current_locus = old_loc;
10380       m = gfc_match_derived_decl ();
10381       if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
10382 	{
10383 	  *st = ST_DERIVED_DECL;
10384 	  return m;
10385 	}
10386     }
10387   else
10388     {
10389       /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
10390 	 like <type name(parameter)>.  */
10391       gfc_gobble_whitespace ();
10392       bool paren = gfc_peek_ascii_char () == '(';
10393       if (paren)
10394 	{
10395 	  if (strcmp ("is", name) == 0)
10396 	    goto typeis;
10397 	  else
10398 	    goto derived;
10399 	}
10400     }
10401 
10402   /* Treat TYPE... like PRINT...  */
10403   gfc_current_locus = old_loc;
10404   *st = ST_WRITE;
10405   return gfc_match_print ();
10406 
10407 derived:
10408   gfc_current_locus = old_loc;
10409   *st = ST_DERIVED_DECL;
10410   return gfc_match_derived_decl ();
10411 
10412 typeis:
10413   gfc_current_locus = old_loc;
10414   *st = ST_TYPE_IS;
10415   return gfc_match_type_is ();
10416 }
10417 
10418 
10419 /* Match the beginning of a derived type declaration.  If a type name
10420    was the result of a function, then it is possible to have a symbol
10421    already to be known as a derived type yet have no components.  */
10422 
10423 match
gfc_match_derived_decl(void)10424 gfc_match_derived_decl (void)
10425 {
10426   char name[GFC_MAX_SYMBOL_LEN + 1];
10427   char parent[GFC_MAX_SYMBOL_LEN + 1];
10428   symbol_attribute attr;
10429   gfc_symbol *sym, *gensym;
10430   gfc_symbol *extended;
10431   match m;
10432   match is_type_attr_spec = MATCH_NO;
10433   bool seen_attr = false;
10434   gfc_interface *intr = NULL, *head;
10435   bool parameterized_type = false;
10436   bool seen_colons = false;
10437 
10438   if (gfc_comp_struct (gfc_current_state ()))
10439     return MATCH_NO;
10440 
10441   name[0] = '\0';
10442   parent[0] = '\0';
10443   gfc_clear_attr (&attr);
10444   extended = NULL;
10445 
10446   do
10447     {
10448       is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
10449       if (is_type_attr_spec == MATCH_ERROR)
10450 	return MATCH_ERROR;
10451       if (is_type_attr_spec == MATCH_YES)
10452 	seen_attr = true;
10453     } while (is_type_attr_spec == MATCH_YES);
10454 
10455   /* Deal with derived type extensions.  The extension attribute has
10456      been added to 'attr' but now the parent type must be found and
10457      checked.  */
10458   if (parent[0])
10459     extended = check_extended_derived_type (parent);
10460 
10461   if (parent[0] && !extended)
10462     return MATCH_ERROR;
10463 
10464   m = gfc_match (" ::");
10465   if (m == MATCH_YES)
10466     {
10467       seen_colons = true;
10468     }
10469   else if (seen_attr)
10470     {
10471       gfc_error ("Expected :: in TYPE definition at %C");
10472       return MATCH_ERROR;
10473     }
10474 
10475   /*  In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
10476       But, we need to simply return for TYPE(.  */
10477   if (m == MATCH_NO && gfc_current_form == FORM_FREE)
10478     {
10479       char c = gfc_peek_ascii_char ();
10480       if (c == '(')
10481 	return m;
10482       if (!gfc_is_whitespace (c))
10483 	{
10484 	  gfc_error ("Mangled derived type definition at %C");
10485 	  return MATCH_NO;
10486 	}
10487     }
10488 
10489   m = gfc_match (" %n ", name);
10490   if (m != MATCH_YES)
10491     return m;
10492 
10493   /* Make sure that we don't identify TYPE IS (...) as a parameterized
10494      derived type named 'is'.
10495      TODO Expand the check, when 'name' = "is" by matching " (tname) "
10496      and checking if this is a(n intrinsic) typename.  This picks up
10497      misplaced TYPE IS statements such as in select_type_1.f03.  */
10498   if (gfc_peek_ascii_char () == '(')
10499     {
10500       if (gfc_current_state () == COMP_SELECT_TYPE
10501 	  || (!seen_colons && !strcmp (name, "is")))
10502 	return MATCH_NO;
10503       parameterized_type = true;
10504     }
10505 
10506   m = gfc_match_eos ();
10507   if (m != MATCH_YES && !parameterized_type)
10508     return m;
10509 
10510   /* Make sure the name is not the name of an intrinsic type.  */
10511   if (gfc_is_intrinsic_typename (name))
10512     {
10513       gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
10514 		 "type", name);
10515       return MATCH_ERROR;
10516     }
10517 
10518   if (gfc_get_symbol (name, NULL, &gensym))
10519     return MATCH_ERROR;
10520 
10521   if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
10522     {
10523       if (gensym->ts.u.derived)
10524 	gfc_error ("Derived type name %qs at %C already has a basic type "
10525 		   "of %s", gensym->name, gfc_typename (&gensym->ts));
10526       else
10527 	gfc_error ("Derived type name %qs at %C already has a basic type",
10528 		   gensym->name);
10529       return MATCH_ERROR;
10530     }
10531 
10532   if (!gensym->attr.generic
10533       && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
10534     return MATCH_ERROR;
10535 
10536   if (!gensym->attr.function
10537       && !gfc_add_function (&gensym->attr, gensym->name, NULL))
10538     return MATCH_ERROR;
10539 
10540   if (gensym->attr.dummy)
10541     {
10542       gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
10543 		 name, &gensym->declared_at);
10544       return MATCH_ERROR;
10545     }
10546 
10547   sym = gfc_find_dt_in_generic (gensym);
10548 
10549   if (sym && (sym->components != NULL || sym->attr.zero_comp))
10550     {
10551       gfc_error ("Derived type definition of %qs at %C has already been "
10552                  "defined", sym->name);
10553       return MATCH_ERROR;
10554     }
10555 
10556   if (!sym)
10557     {
10558       /* Use upper case to save the actual derived-type symbol.  */
10559       gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
10560       sym->name = gfc_get_string ("%s", gensym->name);
10561       head = gensym->generic;
10562       intr = gfc_get_interface ();
10563       intr->sym = sym;
10564       intr->where = gfc_current_locus;
10565       intr->sym->declared_at = gfc_current_locus;
10566       intr->next = head;
10567       gensym->generic = intr;
10568       gensym->attr.if_source = IFSRC_DECL;
10569     }
10570 
10571   /* The symbol may already have the derived attribute without the
10572      components.  The ways this can happen is via a function
10573      definition, an INTRINSIC statement or a subtype in another
10574      derived type that is a pointer.  The first part of the AND clause
10575      is true if the symbol is not the return value of a function.  */
10576   if (sym->attr.flavor != FL_DERIVED
10577       && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
10578     return MATCH_ERROR;
10579 
10580   if (attr.access != ACCESS_UNKNOWN
10581       && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
10582     return MATCH_ERROR;
10583   else if (sym->attr.access == ACCESS_UNKNOWN
10584 	   && gensym->attr.access != ACCESS_UNKNOWN
10585 	   && !gfc_add_access (&sym->attr, gensym->attr.access,
10586 			       sym->name, NULL))
10587     return MATCH_ERROR;
10588 
10589   if (sym->attr.access != ACCESS_UNKNOWN
10590       && gensym->attr.access == ACCESS_UNKNOWN)
10591     gensym->attr.access = sym->attr.access;
10592 
10593   /* See if the derived type was labeled as bind(c).  */
10594   if (attr.is_bind_c != 0)
10595     sym->attr.is_bind_c = attr.is_bind_c;
10596 
10597   /* Construct the f2k_derived namespace if it is not yet there.  */
10598   if (!sym->f2k_derived)
10599     sym->f2k_derived = gfc_get_namespace (NULL, 0);
10600 
10601   if (parameterized_type)
10602     {
10603       /* Ignore error or mismatches by going to the end of the statement
10604 	 in order to avoid the component declarations causing problems.  */
10605       m = gfc_match_formal_arglist (sym, 0, 0, true);
10606       if (m != MATCH_YES)
10607 	gfc_error_recovery ();
10608       else
10609 	sym->attr.pdt_template = 1;
10610       m = gfc_match_eos ();
10611       if (m != MATCH_YES)
10612 	{
10613 	  gfc_error_recovery ();
10614 	  gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10615 	}
10616     }
10617 
10618   if (extended && !sym->components)
10619     {
10620       gfc_component *p;
10621       gfc_formal_arglist *f, *g, *h;
10622 
10623       /* Add the extended derived type as the first component.  */
10624       gfc_add_component (sym, parent, &p);
10625       extended->refs++;
10626       gfc_set_sym_referenced (extended);
10627 
10628       p->ts.type = BT_DERIVED;
10629       p->ts.u.derived = extended;
10630       p->initializer = gfc_default_initializer (&p->ts);
10631 
10632       /* Set extension level.  */
10633       if (extended->attr.extension == 255)
10634 	{
10635 	  /* Since the extension field is 8 bit wide, we can only have
10636 	     up to 255 extension levels.  */
10637 	  gfc_error ("Maximum extension level reached with type %qs at %L",
10638 		     extended->name, &extended->declared_at);
10639 	  return MATCH_ERROR;
10640 	}
10641       sym->attr.extension = extended->attr.extension + 1;
10642 
10643       /* Provide the links between the extended type and its extension.  */
10644       if (!extended->f2k_derived)
10645 	extended->f2k_derived = gfc_get_namespace (NULL, 0);
10646 
10647       /* Copy the extended type-param-name-list from the extended type,
10648 	 append those of the extension and add the whole lot to the
10649 	 extension.  */
10650       if (extended->attr.pdt_template)
10651 	{
10652 	  g = h = NULL;
10653 	  sym->attr.pdt_template = 1;
10654 	  for (f = extended->formal; f; f = f->next)
10655 	    {
10656 	      if (f == extended->formal)
10657 		{
10658 		  g = gfc_get_formal_arglist ();
10659 		  h = g;
10660 		}
10661 	      else
10662 		{
10663 		  g->next = gfc_get_formal_arglist ();
10664 		  g = g->next;
10665 		}
10666 	      g->sym = f->sym;
10667 	    }
10668 	  g->next = sym->formal;
10669 	  sym->formal = h;
10670 	}
10671     }
10672 
10673   if (!sym->hash_value)
10674     /* Set the hash for the compound name for this type.  */
10675     sym->hash_value = gfc_hash_value (sym);
10676 
10677   /* Take over the ABSTRACT attribute.  */
10678   sym->attr.abstract = attr.abstract;
10679 
10680   gfc_new_block = sym;
10681 
10682   return MATCH_YES;
10683 }
10684 
10685 
10686 /* Cray Pointees can be declared as:
10687       pointer (ipt, a (n,m,...,*))  */
10688 
10689 match
gfc_mod_pointee_as(gfc_array_spec * as)10690 gfc_mod_pointee_as (gfc_array_spec *as)
10691 {
10692   as->cray_pointee = true; /* This will be useful to know later.  */
10693   if (as->type == AS_ASSUMED_SIZE)
10694     as->cp_was_assumed = true;
10695   else if (as->type == AS_ASSUMED_SHAPE)
10696     {
10697       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10698       return MATCH_ERROR;
10699     }
10700   return MATCH_YES;
10701 }
10702 
10703 
10704 /* Match the enum definition statement, here we are trying to match
10705    the first line of enum definition statement.
10706    Returns MATCH_YES if match is found.  */
10707 
10708 match
gfc_match_enum(void)10709 gfc_match_enum (void)
10710 {
10711   match m;
10712 
10713   m = gfc_match_eos ();
10714   if (m != MATCH_YES)
10715     return m;
10716 
10717   if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
10718     return MATCH_ERROR;
10719 
10720   return MATCH_YES;
10721 }
10722 
10723 
10724 /* Returns an initializer whose value is one higher than the value of the
10725    LAST_INITIALIZER argument.  If the argument is NULL, the
10726    initializers value will be set to zero.  The initializer's kind
10727    will be set to gfc_c_int_kind.
10728 
10729    If -fshort-enums is given, the appropriate kind will be selected
10730    later after all enumerators have been parsed.  A warning is issued
10731    here if an initializer exceeds gfc_c_int_kind.  */
10732 
10733 static gfc_expr *
enum_initializer(gfc_expr * last_initializer,locus where)10734 enum_initializer (gfc_expr *last_initializer, locus where)
10735 {
10736   gfc_expr *result;
10737   result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
10738 
10739   mpz_init (result->value.integer);
10740 
10741   if (last_initializer != NULL)
10742     {
10743       mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10744       result->where = last_initializer->where;
10745 
10746       if (gfc_check_integer_range (result->value.integer,
10747 	     gfc_c_int_kind) != ARITH_OK)
10748 	{
10749 	  gfc_error ("Enumerator exceeds the C integer type at %C");
10750 	  return NULL;
10751 	}
10752     }
10753   else
10754     {
10755       /* Control comes here, if it's the very first enumerator and no
10756 	 initializer has been given.  It will be initialized to zero.  */
10757       mpz_set_si (result->value.integer, 0);
10758     }
10759 
10760   return result;
10761 }
10762 
10763 
10764 /* Match a variable name with an optional initializer.  When this
10765    subroutine is called, a variable is expected to be parsed next.
10766    Depending on what is happening at the moment, updates either the
10767    symbol table or the current interface.  */
10768 
10769 static match
enumerator_decl(void)10770 enumerator_decl (void)
10771 {
10772   char name[GFC_MAX_SYMBOL_LEN + 1];
10773   gfc_expr *initializer;
10774   gfc_array_spec *as = NULL;
10775   gfc_symbol *sym;
10776   locus var_locus;
10777   match m;
10778   bool t;
10779   locus old_locus;
10780 
10781   initializer = NULL;
10782   old_locus = gfc_current_locus;
10783 
10784   /* When we get here, we've just matched a list of attributes and
10785      maybe a type and a double colon.  The next thing we expect to see
10786      is the name of the symbol.  */
10787   m = gfc_match_name (name);
10788   if (m != MATCH_YES)
10789     goto cleanup;
10790 
10791   var_locus = gfc_current_locus;
10792 
10793   /* OK, we've successfully matched the declaration.  Now put the
10794      symbol in the current namespace. If we fail to create the symbol,
10795      bail out.  */
10796   if (!build_sym (name, NULL, false, &as, &var_locus))
10797     {
10798       m = MATCH_ERROR;
10799       goto cleanup;
10800     }
10801 
10802   /* The double colon must be present in order to have initializers.
10803      Otherwise the statement is ambiguous with an assignment statement.  */
10804   if (colon_seen)
10805     {
10806       if (gfc_match_char ('=') == MATCH_YES)
10807 	{
10808 	  m = gfc_match_init_expr (&initializer);
10809 	  if (m == MATCH_NO)
10810 	    {
10811 	      gfc_error ("Expected an initialization expression at %C");
10812 	      m = MATCH_ERROR;
10813 	    }
10814 
10815 	  if (m != MATCH_YES)
10816 	    goto cleanup;
10817 	}
10818     }
10819 
10820   /* If we do not have an initializer, the initialization value of the
10821      previous enumerator (stored in last_initializer) is incremented
10822      by 1 and is used to initialize the current enumerator.  */
10823   if (initializer == NULL)
10824     initializer = enum_initializer (last_initializer, old_locus);
10825 
10826   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10827     {
10828       gfc_error ("ENUMERATOR %L not initialized with integer expression",
10829 		 &var_locus);
10830       m = MATCH_ERROR;
10831       goto cleanup;
10832     }
10833 
10834   /* Store this current initializer, for the next enumerator variable
10835      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
10836      use last_initializer below.  */
10837   last_initializer = initializer;
10838   t = add_init_expr_to_sym (name, &initializer, &var_locus);
10839 
10840   /* Maintain enumerator history.  */
10841   gfc_find_symbol (name, NULL, 0, &sym);
10842   create_enum_history (sym, last_initializer);
10843 
10844   return (t) ? MATCH_YES : MATCH_ERROR;
10845 
10846 cleanup:
10847   /* Free stuff up and return.  */
10848   gfc_free_expr (initializer);
10849 
10850   return m;
10851 }
10852 
10853 
10854 /* Match the enumerator definition statement.  */
10855 
10856 match
gfc_match_enumerator_def(void)10857 gfc_match_enumerator_def (void)
10858 {
10859   match m;
10860   bool t;
10861 
10862   gfc_clear_ts (&current_ts);
10863 
10864   m = gfc_match (" enumerator");
10865   if (m != MATCH_YES)
10866     return m;
10867 
10868   m = gfc_match (" :: ");
10869   if (m == MATCH_ERROR)
10870     return m;
10871 
10872   colon_seen = (m == MATCH_YES);
10873 
10874   if (gfc_current_state () != COMP_ENUM)
10875     {
10876       gfc_error ("ENUM definition statement expected before %C");
10877       gfc_free_enum_history ();
10878       return MATCH_ERROR;
10879     }
10880 
10881   (&current_ts)->type = BT_INTEGER;
10882   (&current_ts)->kind = gfc_c_int_kind;
10883 
10884   gfc_clear_attr (&current_attr);
10885   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10886   if (!t)
10887     {
10888       m = MATCH_ERROR;
10889       goto cleanup;
10890     }
10891 
10892   for (;;)
10893     {
10894       m = enumerator_decl ();
10895       if (m == MATCH_ERROR)
10896 	{
10897 	  gfc_free_enum_history ();
10898 	  goto cleanup;
10899 	}
10900       if (m == MATCH_NO)
10901 	break;
10902 
10903       if (gfc_match_eos () == MATCH_YES)
10904 	goto cleanup;
10905       if (gfc_match_char (',') != MATCH_YES)
10906 	break;
10907     }
10908 
10909   if (gfc_current_state () == COMP_ENUM)
10910     {
10911       gfc_free_enum_history ();
10912       gfc_error ("Syntax error in ENUMERATOR definition at %C");
10913       m = MATCH_ERROR;
10914     }
10915 
10916 cleanup:
10917   gfc_free_array_spec (current_as);
10918   current_as = NULL;
10919   return m;
10920 
10921 }
10922 
10923 
10924 /* Match binding attributes.  */
10925 
10926 static match
match_binding_attributes(gfc_typebound_proc * ba,bool generic,bool ppc)10927 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10928 {
10929   bool found_passing = false;
10930   bool seen_ptr = false;
10931   match m = MATCH_YES;
10932 
10933   /* Initialize to defaults.  Do so even before the MATCH_NO check so that in
10934      this case the defaults are in there.  */
10935   ba->access = ACCESS_UNKNOWN;
10936   ba->pass_arg = NULL;
10937   ba->pass_arg_num = 0;
10938   ba->nopass = 0;
10939   ba->non_overridable = 0;
10940   ba->deferred = 0;
10941   ba->ppc = ppc;
10942 
10943   /* If we find a comma, we believe there are binding attributes.  */
10944   m = gfc_match_char (',');
10945   if (m == MATCH_NO)
10946     goto done;
10947 
10948   do
10949     {
10950       /* Access specifier.  */
10951 
10952       m = gfc_match (" public");
10953       if (m == MATCH_ERROR)
10954 	goto error;
10955       if (m == MATCH_YES)
10956 	{
10957 	  if (ba->access != ACCESS_UNKNOWN)
10958 	    {
10959 	      gfc_error ("Duplicate access-specifier at %C");
10960 	      goto error;
10961 	    }
10962 
10963 	  ba->access = ACCESS_PUBLIC;
10964 	  continue;
10965 	}
10966 
10967       m = gfc_match (" private");
10968       if (m == MATCH_ERROR)
10969 	goto error;
10970       if (m == MATCH_YES)
10971 	{
10972 	  if (ba->access != ACCESS_UNKNOWN)
10973 	    {
10974 	      gfc_error ("Duplicate access-specifier at %C");
10975 	      goto error;
10976 	    }
10977 
10978 	  ba->access = ACCESS_PRIVATE;
10979 	  continue;
10980 	}
10981 
10982       /* If inside GENERIC, the following is not allowed.  */
10983       if (!generic)
10984 	{
10985 
10986 	  /* NOPASS flag.  */
10987 	  m = gfc_match (" nopass");
10988 	  if (m == MATCH_ERROR)
10989 	    goto error;
10990 	  if (m == MATCH_YES)
10991 	    {
10992 	      if (found_passing)
10993 		{
10994 		  gfc_error ("Binding attributes already specify passing,"
10995 			     " illegal NOPASS at %C");
10996 		  goto error;
10997 		}
10998 
10999 	      found_passing = true;
11000 	      ba->nopass = 1;
11001 	      continue;
11002 	    }
11003 
11004 	  /* PASS possibly including argument.  */
11005 	  m = gfc_match (" pass");
11006 	  if (m == MATCH_ERROR)
11007 	    goto error;
11008 	  if (m == MATCH_YES)
11009 	    {
11010 	      char arg[GFC_MAX_SYMBOL_LEN + 1];
11011 
11012 	      if (found_passing)
11013 		{
11014 		  gfc_error ("Binding attributes already specify passing,"
11015 			     " illegal PASS at %C");
11016 		  goto error;
11017 		}
11018 
11019 	      m = gfc_match (" ( %n )", arg);
11020 	      if (m == MATCH_ERROR)
11021 		goto error;
11022 	      if (m == MATCH_YES)
11023 		ba->pass_arg = gfc_get_string ("%s", arg);
11024 	      gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
11025 
11026 	      found_passing = true;
11027 	      ba->nopass = 0;
11028 	      continue;
11029 	    }
11030 
11031 	  if (ppc)
11032 	    {
11033 	      /* POINTER flag.  */
11034 	      m = gfc_match (" pointer");
11035 	      if (m == MATCH_ERROR)
11036 		goto error;
11037 	      if (m == MATCH_YES)
11038 		{
11039 		  if (seen_ptr)
11040 		    {
11041 		      gfc_error ("Duplicate POINTER attribute at %C");
11042 		      goto error;
11043 		    }
11044 
11045 		  seen_ptr = true;
11046         	  continue;
11047 		}
11048 	    }
11049 	  else
11050 	    {
11051 	      /* NON_OVERRIDABLE flag.  */
11052 	      m = gfc_match (" non_overridable");
11053 	      if (m == MATCH_ERROR)
11054 		goto error;
11055 	      if (m == MATCH_YES)
11056 		{
11057 		  if (ba->non_overridable)
11058 		    {
11059 		      gfc_error ("Duplicate NON_OVERRIDABLE at %C");
11060 		      goto error;
11061 		    }
11062 
11063 		  ba->non_overridable = 1;
11064 		  continue;
11065 		}
11066 
11067 	      /* DEFERRED flag.  */
11068 	      m = gfc_match (" deferred");
11069 	      if (m == MATCH_ERROR)
11070 		goto error;
11071 	      if (m == MATCH_YES)
11072 		{
11073 		  if (ba->deferred)
11074 		    {
11075 		      gfc_error ("Duplicate DEFERRED at %C");
11076 		      goto error;
11077 		    }
11078 
11079 		  ba->deferred = 1;
11080 		  continue;
11081 		}
11082 	    }
11083 
11084 	}
11085 
11086       /* Nothing matching found.  */
11087       if (generic)
11088 	gfc_error ("Expected access-specifier at %C");
11089       else
11090 	gfc_error ("Expected binding attribute at %C");
11091       goto error;
11092     }
11093   while (gfc_match_char (',') == MATCH_YES);
11094 
11095   /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
11096   if (ba->non_overridable && ba->deferred)
11097     {
11098       gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
11099       goto error;
11100     }
11101 
11102   m = MATCH_YES;
11103 
11104 done:
11105   if (ba->access == ACCESS_UNKNOWN)
11106     ba->access = ppc ? gfc_current_block()->component_access
11107                      : gfc_typebound_default_access;
11108 
11109   if (ppc && !seen_ptr)
11110     {
11111       gfc_error ("POINTER attribute is required for procedure pointer component"
11112                  " at %C");
11113       goto error;
11114     }
11115 
11116   return m;
11117 
11118 error:
11119   return MATCH_ERROR;
11120 }
11121 
11122 
11123 /* Match a PROCEDURE specific binding inside a derived type.  */
11124 
11125 static match
match_procedure_in_type(void)11126 match_procedure_in_type (void)
11127 {
11128   char name[GFC_MAX_SYMBOL_LEN + 1];
11129   char target_buf[GFC_MAX_SYMBOL_LEN + 1];
11130   char* target = NULL, *ifc = NULL;
11131   gfc_typebound_proc tb;
11132   bool seen_colons;
11133   bool seen_attrs;
11134   match m;
11135   gfc_symtree* stree;
11136   gfc_namespace* ns;
11137   gfc_symbol* block;
11138   int num;
11139 
11140   /* Check current state.  */
11141   gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
11142   block = gfc_state_stack->previous->sym;
11143   gcc_assert (block);
11144 
11145   /* Try to match PROCEDURE(interface).  */
11146   if (gfc_match (" (") == MATCH_YES)
11147     {
11148       m = gfc_match_name (target_buf);
11149       if (m == MATCH_ERROR)
11150 	return m;
11151       if (m != MATCH_YES)
11152 	{
11153 	  gfc_error ("Interface-name expected after %<(%> at %C");
11154 	  return MATCH_ERROR;
11155 	}
11156 
11157       if (gfc_match (" )") != MATCH_YES)
11158 	{
11159 	  gfc_error ("%<)%> expected at %C");
11160 	  return MATCH_ERROR;
11161 	}
11162 
11163       ifc = target_buf;
11164     }
11165 
11166   /* Construct the data structure.  */
11167   memset (&tb, 0, sizeof (tb));
11168   tb.where = gfc_current_locus;
11169 
11170   /* Match binding attributes.  */
11171   m = match_binding_attributes (&tb, false, false);
11172   if (m == MATCH_ERROR)
11173     return m;
11174   seen_attrs = (m == MATCH_YES);
11175 
11176   /* Check that attribute DEFERRED is given if an interface is specified.  */
11177   if (tb.deferred && !ifc)
11178     {
11179       gfc_error ("Interface must be specified for DEFERRED binding at %C");
11180       return MATCH_ERROR;
11181     }
11182   if (ifc && !tb.deferred)
11183     {
11184       gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
11185       return MATCH_ERROR;
11186     }
11187 
11188   /* Match the colons.  */
11189   m = gfc_match (" ::");
11190   if (m == MATCH_ERROR)
11191     return m;
11192   seen_colons = (m == MATCH_YES);
11193   if (seen_attrs && !seen_colons)
11194     {
11195       gfc_error ("Expected %<::%> after binding-attributes at %C");
11196       return MATCH_ERROR;
11197     }
11198 
11199   /* Match the binding names.  */
11200   for(num=1;;num++)
11201     {
11202       m = gfc_match_name (name);
11203       if (m == MATCH_ERROR)
11204 	return m;
11205       if (m == MATCH_NO)
11206 	{
11207 	  gfc_error ("Expected binding name at %C");
11208 	  return MATCH_ERROR;
11209 	}
11210 
11211       if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
11212 	return MATCH_ERROR;
11213 
11214       /* Try to match the '=> target', if it's there.  */
11215       target = ifc;
11216       m = gfc_match (" =>");
11217       if (m == MATCH_ERROR)
11218 	return m;
11219       if (m == MATCH_YES)
11220 	{
11221 	  if (tb.deferred)
11222 	    {
11223 	      gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
11224 	      return MATCH_ERROR;
11225 	    }
11226 
11227 	  if (!seen_colons)
11228 	    {
11229 	      gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
11230 			 " at %C");
11231 	      return MATCH_ERROR;
11232 	    }
11233 
11234 	  m = gfc_match_name (target_buf);
11235 	  if (m == MATCH_ERROR)
11236 	    return m;
11237 	  if (m == MATCH_NO)
11238 	    {
11239 	      gfc_error ("Expected binding target after %<=>%> at %C");
11240 	      return MATCH_ERROR;
11241 	    }
11242 	  target = target_buf;
11243 	}
11244 
11245       /* If no target was found, it has the same name as the binding.  */
11246       if (!target)
11247 	target = name;
11248 
11249       /* Get the namespace to insert the symbols into.  */
11250       ns = block->f2k_derived;
11251       gcc_assert (ns);
11252 
11253       /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
11254       if (tb.deferred && !block->attr.abstract)
11255 	{
11256 	  gfc_error ("Type %qs containing DEFERRED binding at %C "
11257 		     "is not ABSTRACT", block->name);
11258 	  return MATCH_ERROR;
11259 	}
11260 
11261       /* See if we already have a binding with this name in the symtree which
11262 	 would be an error.  If a GENERIC already targeted this binding, it may
11263 	 be already there but then typebound is still NULL.  */
11264       stree = gfc_find_symtree (ns->tb_sym_root, name);
11265       if (stree && stree->n.tb)
11266 	{
11267 	  gfc_error ("There is already a procedure with binding name %qs for "
11268 		     "the derived type %qs at %C", name, block->name);
11269 	  return MATCH_ERROR;
11270 	}
11271 
11272       /* Insert it and set attributes.  */
11273 
11274       if (!stree)
11275 	{
11276 	  stree = gfc_new_symtree (&ns->tb_sym_root, name);
11277 	  gcc_assert (stree);
11278 	}
11279       stree->n.tb = gfc_get_typebound_proc (&tb);
11280 
11281       if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
11282 			    false))
11283 	return MATCH_ERROR;
11284       gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
11285       gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
11286 		     target, &stree->n.tb->u.specific->n.sym->declared_at);
11287 
11288       if (gfc_match_eos () == MATCH_YES)
11289 	return MATCH_YES;
11290       if (gfc_match_char (',') != MATCH_YES)
11291 	goto syntax;
11292     }
11293 
11294 syntax:
11295   gfc_error ("Syntax error in PROCEDURE statement at %C");
11296   return MATCH_ERROR;
11297 }
11298 
11299 
11300 /* Match a GENERIC procedure binding inside a derived type.  */
11301 
11302 match
gfc_match_generic(void)11303 gfc_match_generic (void)
11304 {
11305   char name[GFC_MAX_SYMBOL_LEN + 1];
11306   char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
11307   gfc_symbol* block;
11308   gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
11309   gfc_typebound_proc* tb;
11310   gfc_namespace* ns;
11311   interface_type op_type;
11312   gfc_intrinsic_op op;
11313   match m;
11314 
11315   /* Check current state.  */
11316   if (gfc_current_state () == COMP_DERIVED)
11317     {
11318       gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
11319       return MATCH_ERROR;
11320     }
11321   if (gfc_current_state () != COMP_DERIVED_CONTAINS)
11322     return MATCH_NO;
11323   block = gfc_state_stack->previous->sym;
11324   ns = block->f2k_derived;
11325   gcc_assert (block && ns);
11326 
11327   memset (&tbattr, 0, sizeof (tbattr));
11328   tbattr.where = gfc_current_locus;
11329 
11330   /* See if we get an access-specifier.  */
11331   m = match_binding_attributes (&tbattr, true, false);
11332   if (m == MATCH_ERROR)
11333     goto error;
11334 
11335   /* Now the colons, those are required.  */
11336   if (gfc_match (" ::") != MATCH_YES)
11337     {
11338       gfc_error ("Expected %<::%> at %C");
11339       goto error;
11340     }
11341 
11342   /* Match the binding name; depending on type (operator / generic) format
11343      it for future error messages into bind_name.  */
11344 
11345   m = gfc_match_generic_spec (&op_type, name, &op);
11346   if (m == MATCH_ERROR)
11347     return MATCH_ERROR;
11348   if (m == MATCH_NO)
11349     {
11350       gfc_error ("Expected generic name or operator descriptor at %C");
11351       goto error;
11352     }
11353 
11354   switch (op_type)
11355     {
11356     case INTERFACE_GENERIC:
11357     case INTERFACE_DTIO:
11358       snprintf (bind_name, sizeof (bind_name), "%s", name);
11359       break;
11360 
11361     case INTERFACE_USER_OP:
11362       snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
11363       break;
11364 
11365     case INTERFACE_INTRINSIC_OP:
11366       snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
11367 		gfc_op2string (op));
11368       break;
11369 
11370     case INTERFACE_NAMELESS:
11371       gfc_error ("Malformed GENERIC statement at %C");
11372       goto error;
11373       break;
11374 
11375     default:
11376       gcc_unreachable ();
11377     }
11378 
11379   /* Match the required =>.  */
11380   if (gfc_match (" =>") != MATCH_YES)
11381     {
11382       gfc_error ("Expected %<=>%> at %C");
11383       goto error;
11384     }
11385 
11386   /* Try to find existing GENERIC binding with this name / for this operator;
11387      if there is something, check that it is another GENERIC and then extend
11388      it rather than building a new node.  Otherwise, create it and put it
11389      at the right position.  */
11390 
11391   switch (op_type)
11392     {
11393     case INTERFACE_DTIO:
11394     case INTERFACE_USER_OP:
11395     case INTERFACE_GENERIC:
11396       {
11397 	const bool is_op = (op_type == INTERFACE_USER_OP);
11398 	gfc_symtree* st;
11399 
11400 	st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
11401 	tb = st ? st->n.tb : NULL;
11402 	break;
11403       }
11404 
11405     case INTERFACE_INTRINSIC_OP:
11406       tb = ns->tb_op[op];
11407       break;
11408 
11409     default:
11410       gcc_unreachable ();
11411     }
11412 
11413   if (tb)
11414     {
11415       if (!tb->is_generic)
11416 	{
11417 	  gcc_assert (op_type == INTERFACE_GENERIC);
11418 	  gfc_error ("There's already a non-generic procedure with binding name"
11419 		     " %qs for the derived type %qs at %C",
11420 		     bind_name, block->name);
11421 	  goto error;
11422 	}
11423 
11424       if (tb->access != tbattr.access)
11425 	{
11426 	  gfc_error ("Binding at %C must have the same access as already"
11427 		     " defined binding %qs", bind_name);
11428 	  goto error;
11429 	}
11430     }
11431   else
11432     {
11433       tb = gfc_get_typebound_proc (NULL);
11434       tb->where = gfc_current_locus;
11435       tb->access = tbattr.access;
11436       tb->is_generic = 1;
11437       tb->u.generic = NULL;
11438 
11439       switch (op_type)
11440 	{
11441 	case INTERFACE_DTIO:
11442 	case INTERFACE_GENERIC:
11443 	case INTERFACE_USER_OP:
11444 	  {
11445 	    const bool is_op = (op_type == INTERFACE_USER_OP);
11446 	    gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
11447 						   &ns->tb_sym_root, name);
11448 	    gcc_assert (st);
11449 	    st->n.tb = tb;
11450 
11451 	    break;
11452 	  }
11453 
11454 	case INTERFACE_INTRINSIC_OP:
11455 	  ns->tb_op[op] = tb;
11456 	  break;
11457 
11458 	default:
11459 	  gcc_unreachable ();
11460 	}
11461     }
11462 
11463   /* Now, match all following names as specific targets.  */
11464   do
11465     {
11466       gfc_symtree* target_st;
11467       gfc_tbp_generic* target;
11468 
11469       m = gfc_match_name (name);
11470       if (m == MATCH_ERROR)
11471 	goto error;
11472       if (m == MATCH_NO)
11473 	{
11474 	  gfc_error ("Expected specific binding name at %C");
11475 	  goto error;
11476 	}
11477 
11478       target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
11479 
11480       /* See if this is a duplicate specification.  */
11481       for (target = tb->u.generic; target; target = target->next)
11482 	if (target_st == target->specific_st)
11483 	  {
11484 	    gfc_error ("%qs already defined as specific binding for the"
11485 		       " generic %qs at %C", name, bind_name);
11486 	    goto error;
11487 	  }
11488 
11489       target = gfc_get_tbp_generic ();
11490       target->specific_st = target_st;
11491       target->specific = NULL;
11492       target->next = tb->u.generic;
11493       target->is_operator = ((op_type == INTERFACE_USER_OP)
11494 			     || (op_type == INTERFACE_INTRINSIC_OP));
11495       tb->u.generic = target;
11496     }
11497   while (gfc_match (" ,") == MATCH_YES);
11498 
11499   /* Here should be the end.  */
11500   if (gfc_match_eos () != MATCH_YES)
11501     {
11502       gfc_error ("Junk after GENERIC binding at %C");
11503       goto error;
11504     }
11505 
11506   return MATCH_YES;
11507 
11508 error:
11509   return MATCH_ERROR;
11510 }
11511 
11512 
11513 /* Match a FINAL declaration inside a derived type.  */
11514 
11515 match
gfc_match_final_decl(void)11516 gfc_match_final_decl (void)
11517 {
11518   char name[GFC_MAX_SYMBOL_LEN + 1];
11519   gfc_symbol* sym;
11520   match m;
11521   gfc_namespace* module_ns;
11522   bool first, last;
11523   gfc_symbol* block;
11524 
11525   if (gfc_current_form == FORM_FREE)
11526     {
11527       char c = gfc_peek_ascii_char ();
11528       if (!gfc_is_whitespace (c) && c != ':')
11529 	return MATCH_NO;
11530     }
11531 
11532   if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
11533     {
11534       if (gfc_current_form == FORM_FIXED)
11535 	return MATCH_NO;
11536 
11537       gfc_error ("FINAL declaration at %C must be inside a derived type "
11538 		 "CONTAINS section");
11539       return MATCH_ERROR;
11540     }
11541 
11542   block = gfc_state_stack->previous->sym;
11543   gcc_assert (block);
11544 
11545   if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
11546       || gfc_state_stack->previous->previous->state != COMP_MODULE)
11547     {
11548       gfc_error ("Derived type declaration with FINAL at %C must be in the"
11549 		 " specification part of a MODULE");
11550       return MATCH_ERROR;
11551     }
11552 
11553   module_ns = gfc_current_ns;
11554   gcc_assert (module_ns);
11555   gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
11556 
11557   /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
11558   if (gfc_match (" ::") == MATCH_ERROR)
11559     return MATCH_ERROR;
11560 
11561   /* Match the sequence of procedure names.  */
11562   first = true;
11563   last = false;
11564   do
11565     {
11566       gfc_finalizer* f;
11567 
11568       if (first && gfc_match_eos () == MATCH_YES)
11569 	{
11570 	  gfc_error ("Empty FINAL at %C");
11571 	  return MATCH_ERROR;
11572 	}
11573 
11574       m = gfc_match_name (name);
11575       if (m == MATCH_NO)
11576 	{
11577 	  gfc_error ("Expected module procedure name at %C");
11578 	  return MATCH_ERROR;
11579 	}
11580       else if (m != MATCH_YES)
11581 	return MATCH_ERROR;
11582 
11583       if (gfc_match_eos () == MATCH_YES)
11584 	last = true;
11585       if (!last && gfc_match_char (',') != MATCH_YES)
11586 	{
11587 	  gfc_error ("Expected %<,%> at %C");
11588 	  return MATCH_ERROR;
11589 	}
11590 
11591       if (gfc_get_symbol (name, module_ns, &sym))
11592 	{
11593 	  gfc_error ("Unknown procedure name %qs at %C", name);
11594 	  return MATCH_ERROR;
11595 	}
11596 
11597       /* Mark the symbol as module procedure.  */
11598       if (sym->attr.proc != PROC_MODULE
11599 	  && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
11600 	return MATCH_ERROR;
11601 
11602       /* Check if we already have this symbol in the list, this is an error.  */
11603       for (f = block->f2k_derived->finalizers; f; f = f->next)
11604 	if (f->proc_sym == sym)
11605 	  {
11606 	    gfc_error ("%qs at %C is already defined as FINAL procedure",
11607 		       name);
11608 	    return MATCH_ERROR;
11609 	  }
11610 
11611       /* Add this symbol to the list of finalizers.  */
11612       gcc_assert (block->f2k_derived);
11613       sym->refs++;
11614       f = XCNEW (gfc_finalizer);
11615       f->proc_sym = sym;
11616       f->proc_tree = NULL;
11617       f->where = gfc_current_locus;
11618       f->next = block->f2k_derived->finalizers;
11619       block->f2k_derived->finalizers = f;
11620 
11621       first = false;
11622     }
11623   while (!last);
11624 
11625   return MATCH_YES;
11626 }
11627 
11628 
11629 const ext_attr_t ext_attr_list[] = {
11630   { "dllimport",    EXT_ATTR_DLLIMPORT,    "dllimport" },
11631   { "dllexport",    EXT_ATTR_DLLEXPORT,    "dllexport" },
11632   { "cdecl",        EXT_ATTR_CDECL,        "cdecl"     },
11633   { "stdcall",      EXT_ATTR_STDCALL,      "stdcall"   },
11634   { "fastcall",     EXT_ATTR_FASTCALL,     "fastcall"  },
11635   { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL        },
11636   { NULL,           EXT_ATTR_LAST,         NULL        }
11637 };
11638 
11639 /* Match a !GCC$ ATTRIBUTES statement of the form:
11640       !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11641    When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11642 
11643    TODO: We should support all GCC attributes using the same syntax for
11644    the attribute list, i.e. the list in C
11645       __attributes(( attribute-list ))
11646    matches then
11647       !GCC$ ATTRIBUTES attribute-list ::
11648    Cf. c-parser.c's c_parser_attributes; the data can then directly be
11649    saved into a TREE.
11650 
11651    As there is absolutely no risk of confusion, we should never return
11652    MATCH_NO.  */
11653 match
gfc_match_gcc_attributes(void)11654 gfc_match_gcc_attributes (void)
11655 {
11656   symbol_attribute attr;
11657   char name[GFC_MAX_SYMBOL_LEN + 1];
11658   unsigned id;
11659   gfc_symbol *sym;
11660   match m;
11661 
11662   gfc_clear_attr (&attr);
11663   for(;;)
11664     {
11665       char ch;
11666 
11667       if (gfc_match_name (name) != MATCH_YES)
11668 	return MATCH_ERROR;
11669 
11670       for (id = 0; id < EXT_ATTR_LAST; id++)
11671 	if (strcmp (name, ext_attr_list[id].name) == 0)
11672 	  break;
11673 
11674       if (id == EXT_ATTR_LAST)
11675 	{
11676 	  gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11677 	  return MATCH_ERROR;
11678 	}
11679 
11680       if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
11681 	return MATCH_ERROR;
11682 
11683       gfc_gobble_whitespace ();
11684       ch = gfc_next_ascii_char ();
11685       if (ch == ':')
11686         {
11687           /* This is the successful exit condition for the loop.  */
11688           if (gfc_next_ascii_char () == ':')
11689             break;
11690         }
11691 
11692       if (ch == ',')
11693 	continue;
11694 
11695       goto syntax;
11696     }
11697 
11698   if (gfc_match_eos () == MATCH_YES)
11699     goto syntax;
11700 
11701   for(;;)
11702     {
11703       m = gfc_match_name (name);
11704       if (m != MATCH_YES)
11705 	return m;
11706 
11707       if (find_special (name, &sym, true))
11708 	return MATCH_ERROR;
11709 
11710       sym->attr.ext_attr |= attr.ext_attr;
11711 
11712       if (gfc_match_eos () == MATCH_YES)
11713 	break;
11714 
11715       if (gfc_match_char (',') != MATCH_YES)
11716 	goto syntax;
11717     }
11718 
11719   return MATCH_YES;
11720 
11721 syntax:
11722   gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11723   return MATCH_ERROR;
11724 }
11725 
11726 
11727 /* Match a !GCC$ UNROLL statement of the form:
11728       !GCC$ UNROLL n
11729 
11730    The parameter n is the number of times we are supposed to unroll.
11731 
11732    When we come here, we have already matched the !GCC$ UNROLL string.  */
11733 match
gfc_match_gcc_unroll(void)11734 gfc_match_gcc_unroll (void)
11735 {
11736   int value;
11737 
11738   if (gfc_match_small_int (&value) == MATCH_YES)
11739     {
11740       if (value < 0 || value > USHRT_MAX)
11741 	{
11742 	  gfc_error ("%<GCC unroll%> directive requires a"
11743 	      " non-negative integral constant"
11744 	      " less than or equal to %u at %C",
11745 	      USHRT_MAX
11746 	  );
11747 	  return MATCH_ERROR;
11748 	}
11749       if (gfc_match_eos () == MATCH_YES)
11750 	{
11751 	  directive_unroll = value == 0 ? 1 : value;
11752 	  return MATCH_YES;
11753 	}
11754     }
11755 
11756   gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11757   return MATCH_ERROR;
11758 }
11759 
11760 /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
11761 
11762    The parameter b is name of a middle-end built-in.
11763    FLAGS is optional and must be one of:
11764      - (inbranch)
11765      - (notinbranch)
11766 
11767    IF('target') is optional and TARGET is a name of a multilib ABI.
11768 
11769    When we come here, we have already matched the !GCC$ builtin string.  */
11770 
11771 match
gfc_match_gcc_builtin(void)11772 gfc_match_gcc_builtin (void)
11773 {
11774   char builtin[GFC_MAX_SYMBOL_LEN + 1];
11775   char target[GFC_MAX_SYMBOL_LEN + 1];
11776 
11777   if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
11778     return MATCH_ERROR;
11779 
11780   gfc_simd_clause clause = SIMD_NONE;
11781   if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
11782     clause = SIMD_NOTINBRANCH;
11783   else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
11784     clause = SIMD_INBRANCH;
11785 
11786   if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
11787     {
11788       const char *abi = targetm.get_multilib_abi_name ();
11789       if (abi == NULL || strcmp (abi, target) != 0)
11790 	return MATCH_YES;
11791     }
11792 
11793   if (gfc_vectorized_builtins == NULL)
11794     gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
11795 
11796   char *r = XNEWVEC (char, strlen (builtin) + 32);
11797   sprintf (r, "__builtin_%s", builtin);
11798 
11799   bool existed;
11800   int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
11801   value |= clause;
11802   if (existed)
11803     free (r);
11804 
11805   return MATCH_YES;
11806 }
11807 
11808 /* Match an !GCC$ IVDEP statement.
11809    When we come here, we have already matched the !GCC$ IVDEP string.  */
11810 
11811 match
gfc_match_gcc_ivdep(void)11812 gfc_match_gcc_ivdep (void)
11813 {
11814   if (gfc_match_eos () == MATCH_YES)
11815     {
11816       directive_ivdep = true;
11817       return MATCH_YES;
11818     }
11819 
11820   gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
11821   return MATCH_ERROR;
11822 }
11823 
11824 /* Match an !GCC$ VECTOR statement.
11825    When we come here, we have already matched the !GCC$ VECTOR string.  */
11826 
11827 match
gfc_match_gcc_vector(void)11828 gfc_match_gcc_vector (void)
11829 {
11830   if (gfc_match_eos () == MATCH_YES)
11831     {
11832       directive_vector = true;
11833       directive_novector = false;
11834       return MATCH_YES;
11835     }
11836 
11837   gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
11838   return MATCH_ERROR;
11839 }
11840 
11841 /* Match an !GCC$ NOVECTOR statement.
11842    When we come here, we have already matched the !GCC$ NOVECTOR string.  */
11843 
11844 match
gfc_match_gcc_novector(void)11845 gfc_match_gcc_novector (void)
11846 {
11847   if (gfc_match_eos () == MATCH_YES)
11848     {
11849       directive_novector = true;
11850       directive_vector = false;
11851       return MATCH_YES;
11852     }
11853 
11854   gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
11855   return MATCH_ERROR;
11856 }
11857