Debian Codegraph @

gcc/fortran/decl.cc

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