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