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