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