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