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