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