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