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