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