re PR fortran/40955 (STDCALL attributes are not saved in the .MOD files)
[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 return m;
2373
2374 ts->type = BT_DERIVED;
2375
2376 /* Defer association of the derived type until the end of the
2377 specification block. However, if the derived type can be
2378 found, add it to the typespec. */
2379 if (gfc_matching_function)
2380 {
2381 ts->derived = NULL;
2382 if (gfc_current_state () != COMP_INTERFACE
2383 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2384 ts->derived = sym;
2385 return MATCH_YES;
2386 }
2387
2388 /* Search for the name but allow the components to be defined later. If
2389 type = -1, this typespec has been seen in a function declaration but
2390 the type could not be accessed at that point. */
2391 sym = NULL;
2392 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2393 {
2394 gfc_error ("Type name '%s' at %C is ambiguous", name);
2395 return MATCH_ERROR;
2396 }
2397 else if (ts->kind == -1)
2398 {
2399 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2400 || gfc_current_ns->has_import_set;
2401 if (gfc_find_symbol (name, NULL, iface, &sym))
2402 {
2403 gfc_error ("Type name '%s' at %C is ambiguous", name);
2404 return MATCH_ERROR;
2405 }
2406
2407 ts->kind = 0;
2408 if (sym == NULL)
2409 return MATCH_NO;
2410 }
2411
2412 if (sym->attr.flavor != FL_DERIVED
2413 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2414 return MATCH_ERROR;
2415
2416 gfc_set_sym_referenced (sym);
2417 ts->derived = sym;
2418
2419 return MATCH_YES;
2420
2421 get_kind:
2422 /* For all types except double, derived and character, look for an
2423 optional kind specifier. MATCH_NO is actually OK at this point. */
2424 if (implicit_flag == 1)
2425 return MATCH_YES;
2426
2427 if (gfc_current_form == FORM_FREE)
2428 {
2429 c = gfc_peek_ascii_char();
2430 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2431 && c != ':' && c != ',')
2432 return MATCH_NO;
2433 }
2434
2435 m = gfc_match_kind_spec (ts, false);
2436 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2437 m = gfc_match_old_kind_spec (ts);
2438
2439 /* Defer association of the KIND expression of function results
2440 until after USE and IMPORT statements. */
2441 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2442 || gfc_matching_function)
2443 return MATCH_YES;
2444
2445 if (m == MATCH_NO)
2446 m = MATCH_YES; /* No kind specifier found. */
2447
2448 return m;
2449 }
2450
2451
2452 /* Match an IMPLICIT NONE statement. Actually, this statement is
2453 already matched in parse.c, or we would not end up here in the
2454 first place. So the only thing we need to check, is if there is
2455 trailing garbage. If not, the match is successful. */
2456
2457 match
2458 gfc_match_implicit_none (void)
2459 {
2460 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2461 }
2462
2463
2464 /* Match the letter range(s) of an IMPLICIT statement. */
2465
2466 static match
2467 match_implicit_range (void)
2468 {
2469 char c, c1, c2;
2470 int inner;
2471 locus cur_loc;
2472
2473 cur_loc = gfc_current_locus;
2474
2475 gfc_gobble_whitespace ();
2476 c = gfc_next_ascii_char ();
2477 if (c != '(')
2478 {
2479 gfc_error ("Missing character range in IMPLICIT at %C");
2480 goto bad;
2481 }
2482
2483 inner = 1;
2484 while (inner)
2485 {
2486 gfc_gobble_whitespace ();
2487 c1 = gfc_next_ascii_char ();
2488 if (!ISALPHA (c1))
2489 goto bad;
2490
2491 gfc_gobble_whitespace ();
2492 c = gfc_next_ascii_char ();
2493
2494 switch (c)
2495 {
2496 case ')':
2497 inner = 0; /* Fall through. */
2498
2499 case ',':
2500 c2 = c1;
2501 break;
2502
2503 case '-':
2504 gfc_gobble_whitespace ();
2505 c2 = gfc_next_ascii_char ();
2506 if (!ISALPHA (c2))
2507 goto bad;
2508
2509 gfc_gobble_whitespace ();
2510 c = gfc_next_ascii_char ();
2511
2512 if ((c != ',') && (c != ')'))
2513 goto bad;
2514 if (c == ')')
2515 inner = 0;
2516
2517 break;
2518
2519 default:
2520 goto bad;
2521 }
2522
2523 if (c1 > c2)
2524 {
2525 gfc_error ("Letters must be in alphabetic order in "
2526 "IMPLICIT statement at %C");
2527 goto bad;
2528 }
2529
2530 /* See if we can add the newly matched range to the pending
2531 implicits from this IMPLICIT statement. We do not check for
2532 conflicts with whatever earlier IMPLICIT statements may have
2533 set. This is done when we've successfully finished matching
2534 the current one. */
2535 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2536 goto bad;
2537 }
2538
2539 return MATCH_YES;
2540
2541 bad:
2542 gfc_syntax_error (ST_IMPLICIT);
2543
2544 gfc_current_locus = cur_loc;
2545 return MATCH_ERROR;
2546 }
2547
2548
2549 /* Match an IMPLICIT statement, storing the types for
2550 gfc_set_implicit() if the statement is accepted by the parser.
2551 There is a strange looking, but legal syntactic construction
2552 possible. It looks like:
2553
2554 IMPLICIT INTEGER (a-b) (c-d)
2555
2556 This is legal if "a-b" is a constant expression that happens to
2557 equal one of the legal kinds for integers. The real problem
2558 happens with an implicit specification that looks like:
2559
2560 IMPLICIT INTEGER (a-b)
2561
2562 In this case, a typespec matcher that is "greedy" (as most of the
2563 matchers are) gobbles the character range as a kindspec, leaving
2564 nothing left. We therefore have to go a bit more slowly in the
2565 matching process by inhibiting the kindspec checking during
2566 typespec matching and checking for a kind later. */
2567
2568 match
2569 gfc_match_implicit (void)
2570 {
2571 gfc_typespec ts;
2572 locus cur_loc;
2573 char c;
2574 match m;
2575
2576 gfc_clear_ts (&ts);
2577
2578 /* We don't allow empty implicit statements. */
2579 if (gfc_match_eos () == MATCH_YES)
2580 {
2581 gfc_error ("Empty IMPLICIT statement at %C");
2582 return MATCH_ERROR;
2583 }
2584
2585 do
2586 {
2587 /* First cleanup. */
2588 gfc_clear_new_implicit ();
2589
2590 /* A basic type is mandatory here. */
2591 m = gfc_match_type_spec (&ts, 1);
2592 if (m == MATCH_ERROR)
2593 goto error;
2594 if (m == MATCH_NO)
2595 goto syntax;
2596
2597 cur_loc = gfc_current_locus;
2598 m = match_implicit_range ();
2599
2600 if (m == MATCH_YES)
2601 {
2602 /* We may have <TYPE> (<RANGE>). */
2603 gfc_gobble_whitespace ();
2604 c = gfc_next_ascii_char ();
2605 if ((c == '\n') || (c == ','))
2606 {
2607 /* Check for CHARACTER with no length parameter. */
2608 if (ts.type == BT_CHARACTER && !ts.cl)
2609 {
2610 ts.kind = gfc_default_character_kind;
2611 ts.cl = gfc_new_charlen (gfc_current_ns);
2612 ts.cl->length = gfc_int_expr (1);
2613 }
2614
2615 /* Record the Successful match. */
2616 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2617 return MATCH_ERROR;
2618 continue;
2619 }
2620
2621 gfc_current_locus = cur_loc;
2622 }
2623
2624 /* Discard the (incorrectly) matched range. */
2625 gfc_clear_new_implicit ();
2626
2627 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2628 if (ts.type == BT_CHARACTER)
2629 m = match_char_spec (&ts);
2630 else
2631 {
2632 m = gfc_match_kind_spec (&ts, false);
2633 if (m == MATCH_NO)
2634 {
2635 m = gfc_match_old_kind_spec (&ts);
2636 if (m == MATCH_ERROR)
2637 goto error;
2638 if (m == MATCH_NO)
2639 goto syntax;
2640 }
2641 }
2642 if (m == MATCH_ERROR)
2643 goto error;
2644
2645 m = match_implicit_range ();
2646 if (m == MATCH_ERROR)
2647 goto error;
2648 if (m == MATCH_NO)
2649 goto syntax;
2650
2651 gfc_gobble_whitespace ();
2652 c = gfc_next_ascii_char ();
2653 if ((c != '\n') && (c != ','))
2654 goto syntax;
2655
2656 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2657 return MATCH_ERROR;
2658 }
2659 while (c == ',');
2660
2661 return MATCH_YES;
2662
2663 syntax:
2664 gfc_syntax_error (ST_IMPLICIT);
2665
2666 error:
2667 return MATCH_ERROR;
2668 }
2669
2670
2671 match
2672 gfc_match_import (void)
2673 {
2674 char name[GFC_MAX_SYMBOL_LEN + 1];
2675 match m;
2676 gfc_symbol *sym;
2677 gfc_symtree *st;
2678
2679 if (gfc_current_ns->proc_name == NULL
2680 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2681 {
2682 gfc_error ("IMPORT statement at %C only permitted in "
2683 "an INTERFACE body");
2684 return MATCH_ERROR;
2685 }
2686
2687 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2688 == FAILURE)
2689 return MATCH_ERROR;
2690
2691 if (gfc_match_eos () == MATCH_YES)
2692 {
2693 /* All host variables should be imported. */
2694 gfc_current_ns->has_import_set = 1;
2695 return MATCH_YES;
2696 }
2697
2698 if (gfc_match (" ::") == MATCH_YES)
2699 {
2700 if (gfc_match_eos () == MATCH_YES)
2701 {
2702 gfc_error ("Expecting list of named entities at %C");
2703 return MATCH_ERROR;
2704 }
2705 }
2706
2707 for(;;)
2708 {
2709 m = gfc_match (" %n", name);
2710 switch (m)
2711 {
2712 case MATCH_YES:
2713 if (gfc_current_ns->parent != NULL
2714 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2715 {
2716 gfc_error ("Type name '%s' at %C is ambiguous", name);
2717 return MATCH_ERROR;
2718 }
2719 else if (gfc_current_ns->proc_name->ns->parent != NULL
2720 && gfc_find_symbol (name,
2721 gfc_current_ns->proc_name->ns->parent,
2722 1, &sym))
2723 {
2724 gfc_error ("Type name '%s' at %C is ambiguous", name);
2725 return MATCH_ERROR;
2726 }
2727
2728 if (sym == NULL)
2729 {
2730 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2731 "at %C - does not exist.", name);
2732 return MATCH_ERROR;
2733 }
2734
2735 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2736 {
2737 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2738 "at %C.", name);
2739 goto next_item;
2740 }
2741
2742 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
2743 st->n.sym = sym;
2744 sym->refs++;
2745 sym->attr.imported = 1;
2746
2747 goto next_item;
2748
2749 case MATCH_NO:
2750 break;
2751
2752 case MATCH_ERROR:
2753 return MATCH_ERROR;
2754 }
2755
2756 next_item:
2757 if (gfc_match_eos () == MATCH_YES)
2758 break;
2759 if (gfc_match_char (',') != MATCH_YES)
2760 goto syntax;
2761 }
2762
2763 return MATCH_YES;
2764
2765 syntax:
2766 gfc_error ("Syntax error in IMPORT statement at %C");
2767 return MATCH_ERROR;
2768 }
2769
2770
2771 /* A minimal implementation of gfc_match without whitespace, escape
2772 characters or variable arguments. Returns true if the next
2773 characters match the TARGET template exactly. */
2774
2775 static bool
2776 match_string_p (const char *target)
2777 {
2778 const char *p;
2779
2780 for (p = target; *p; p++)
2781 if ((char) gfc_next_ascii_char () != *p)
2782 return false;
2783 return true;
2784 }
2785
2786 /* Matches an attribute specification including array specs. If
2787 successful, leaves the variables current_attr and current_as
2788 holding the specification. Also sets the colon_seen variable for
2789 later use by matchers associated with initializations.
2790
2791 This subroutine is a little tricky in the sense that we don't know
2792 if we really have an attr-spec until we hit the double colon.
2793 Until that time, we can only return MATCH_NO. This forces us to
2794 check for duplicate specification at this level. */
2795
2796 static match
2797 match_attr_spec (void)
2798 {
2799 /* Modifiers that can exist in a type statement. */
2800 typedef enum
2801 { GFC_DECL_BEGIN = 0,
2802 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2803 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2804 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2805 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2806 DECL_IS_BIND_C, DECL_NONE,
2807 GFC_DECL_END /* Sentinel */
2808 }
2809 decl_types;
2810
2811 /* GFC_DECL_END is the sentinel, index starts at 0. */
2812 #define NUM_DECL GFC_DECL_END
2813
2814 locus start, seen_at[NUM_DECL];
2815 int seen[NUM_DECL];
2816 unsigned int d;
2817 const char *attr;
2818 match m;
2819 gfc_try t;
2820
2821 gfc_clear_attr (&current_attr);
2822 start = gfc_current_locus;
2823
2824 current_as = NULL;
2825 colon_seen = 0;
2826
2827 /* See if we get all of the keywords up to the final double colon. */
2828 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2829 seen[d] = 0;
2830
2831 for (;;)
2832 {
2833 char ch;
2834
2835 d = DECL_NONE;
2836 gfc_gobble_whitespace ();
2837
2838 ch = gfc_next_ascii_char ();
2839 if (ch == ':')
2840 {
2841 /* This is the successful exit condition for the loop. */
2842 if (gfc_next_ascii_char () == ':')
2843 break;
2844 }
2845 else if (ch == ',')
2846 {
2847 gfc_gobble_whitespace ();
2848 switch (gfc_peek_ascii_char ())
2849 {
2850 case 'a':
2851 if (match_string_p ("allocatable"))
2852 d = DECL_ALLOCATABLE;
2853 break;
2854
2855 case 'b':
2856 /* Try and match the bind(c). */
2857 m = gfc_match_bind_c (NULL, true);
2858 if (m == MATCH_YES)
2859 d = DECL_IS_BIND_C;
2860 else if (m == MATCH_ERROR)
2861 goto cleanup;
2862 break;
2863
2864 case 'd':
2865 if (match_string_p ("dimension"))
2866 d = DECL_DIMENSION;
2867 break;
2868
2869 case 'e':
2870 if (match_string_p ("external"))
2871 d = DECL_EXTERNAL;
2872 break;
2873
2874 case 'i':
2875 if (match_string_p ("int"))
2876 {
2877 ch = gfc_next_ascii_char ();
2878 if (ch == 'e')
2879 {
2880 if (match_string_p ("nt"))
2881 {
2882 /* Matched "intent". */
2883 /* TODO: Call match_intent_spec from here. */
2884 if (gfc_match (" ( in out )") == MATCH_YES)
2885 d = DECL_INOUT;
2886 else if (gfc_match (" ( in )") == MATCH_YES)
2887 d = DECL_IN;
2888 else if (gfc_match (" ( out )") == MATCH_YES)
2889 d = DECL_OUT;
2890 }
2891 }
2892 else if (ch == 'r')
2893 {
2894 if (match_string_p ("insic"))
2895 {
2896 /* Matched "intrinsic". */
2897 d = DECL_INTRINSIC;
2898 }
2899 }
2900 }
2901 break;
2902
2903 case 'o':
2904 if (match_string_p ("optional"))
2905 d = DECL_OPTIONAL;
2906 break;
2907
2908 case 'p':
2909 gfc_next_ascii_char ();
2910 switch (gfc_next_ascii_char ())
2911 {
2912 case 'a':
2913 if (match_string_p ("rameter"))
2914 {
2915 /* Matched "parameter". */
2916 d = DECL_PARAMETER;
2917 }
2918 break;
2919
2920 case 'o':
2921 if (match_string_p ("inter"))
2922 {
2923 /* Matched "pointer". */
2924 d = DECL_POINTER;
2925 }
2926 break;
2927
2928 case 'r':
2929 ch = gfc_next_ascii_char ();
2930 if (ch == 'i')
2931 {
2932 if (match_string_p ("vate"))
2933 {
2934 /* Matched "private". */
2935 d = DECL_PRIVATE;
2936 }
2937 }
2938 else if (ch == 'o')
2939 {
2940 if (match_string_p ("tected"))
2941 {
2942 /* Matched "protected". */
2943 d = DECL_PROTECTED;
2944 }
2945 }
2946 break;
2947
2948 case 'u':
2949 if (match_string_p ("blic"))
2950 {
2951 /* Matched "public". */
2952 d = DECL_PUBLIC;
2953 }
2954 break;
2955 }
2956 break;
2957
2958 case 's':
2959 if (match_string_p ("save"))
2960 d = DECL_SAVE;
2961 break;
2962
2963 case 't':
2964 if (match_string_p ("target"))
2965 d = DECL_TARGET;
2966 break;
2967
2968 case 'v':
2969 gfc_next_ascii_char ();
2970 ch = gfc_next_ascii_char ();
2971 if (ch == 'a')
2972 {
2973 if (match_string_p ("lue"))
2974 {
2975 /* Matched "value". */
2976 d = DECL_VALUE;
2977 }
2978 }
2979 else if (ch == 'o')
2980 {
2981 if (match_string_p ("latile"))
2982 {
2983 /* Matched "volatile". */
2984 d = DECL_VOLATILE;
2985 }
2986 }
2987 break;
2988 }
2989 }
2990
2991 /* No double colon and no recognizable decl_type, so assume that
2992 we've been looking at something else the whole time. */
2993 if (d == DECL_NONE)
2994 {
2995 m = MATCH_NO;
2996 goto cleanup;
2997 }
2998
2999 /* Check to make sure any parens are paired up correctly. */
3000 if (gfc_match_parens () == MATCH_ERROR)
3001 {
3002 m = MATCH_ERROR;
3003 goto cleanup;
3004 }
3005
3006 seen[d]++;
3007 seen_at[d] = gfc_current_locus;
3008
3009 if (d == DECL_DIMENSION)
3010 {
3011 m = gfc_match_array_spec (&current_as);
3012
3013 if (m == MATCH_NO)
3014 {
3015 gfc_error ("Missing dimension specification at %C");
3016 m = MATCH_ERROR;
3017 }
3018
3019 if (m == MATCH_ERROR)
3020 goto cleanup;
3021 }
3022 }
3023
3024 /* Since we've seen a double colon, we have to be looking at an
3025 attr-spec. This means that we can now issue errors. */
3026 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3027 if (seen[d] > 1)
3028 {
3029 switch (d)
3030 {
3031 case DECL_ALLOCATABLE:
3032 attr = "ALLOCATABLE";
3033 break;
3034 case DECL_DIMENSION:
3035 attr = "DIMENSION";
3036 break;
3037 case DECL_EXTERNAL:
3038 attr = "EXTERNAL";
3039 break;
3040 case DECL_IN:
3041 attr = "INTENT (IN)";
3042 break;
3043 case DECL_OUT:
3044 attr = "INTENT (OUT)";
3045 break;
3046 case DECL_INOUT:
3047 attr = "INTENT (IN OUT)";
3048 break;
3049 case DECL_INTRINSIC:
3050 attr = "INTRINSIC";
3051 break;
3052 case DECL_OPTIONAL:
3053 attr = "OPTIONAL";
3054 break;
3055 case DECL_PARAMETER:
3056 attr = "PARAMETER";
3057 break;
3058 case DECL_POINTER:
3059 attr = "POINTER";
3060 break;
3061 case DECL_PROTECTED:
3062 attr = "PROTECTED";
3063 break;
3064 case DECL_PRIVATE:
3065 attr = "PRIVATE";
3066 break;
3067 case DECL_PUBLIC:
3068 attr = "PUBLIC";
3069 break;
3070 case DECL_SAVE:
3071 attr = "SAVE";
3072 break;
3073 case DECL_TARGET:
3074 attr = "TARGET";
3075 break;
3076 case DECL_IS_BIND_C:
3077 attr = "IS_BIND_C";
3078 break;
3079 case DECL_VALUE:
3080 attr = "VALUE";
3081 break;
3082 case DECL_VOLATILE:
3083 attr = "VOLATILE";
3084 break;
3085 default:
3086 attr = NULL; /* This shouldn't happen. */
3087 }
3088
3089 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3090 m = MATCH_ERROR;
3091 goto cleanup;
3092 }
3093
3094 /* Now that we've dealt with duplicate attributes, add the attributes
3095 to the current attribute. */
3096 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3097 {
3098 if (seen[d] == 0)
3099 continue;
3100
3101 if (gfc_current_state () == COMP_DERIVED
3102 && d != DECL_DIMENSION && d != DECL_POINTER
3103 && d != DECL_PRIVATE && d != DECL_PUBLIC
3104 && d != DECL_NONE)
3105 {
3106 if (d == DECL_ALLOCATABLE)
3107 {
3108 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3109 "attribute at %C in a TYPE definition")
3110 == FAILURE)
3111 {
3112 m = MATCH_ERROR;
3113 goto cleanup;
3114 }
3115 }
3116 else
3117 {
3118 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3119 &seen_at[d]);
3120 m = MATCH_ERROR;
3121 goto cleanup;
3122 }
3123 }
3124
3125 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3126 && gfc_current_state () != COMP_MODULE)
3127 {
3128 if (d == DECL_PRIVATE)
3129 attr = "PRIVATE";
3130 else
3131 attr = "PUBLIC";
3132 if (gfc_current_state () == COMP_DERIVED
3133 && gfc_state_stack->previous
3134 && gfc_state_stack->previous->state == COMP_MODULE)
3135 {
3136 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3137 "at %L in a TYPE definition", attr,
3138 &seen_at[d])
3139 == FAILURE)
3140 {
3141 m = MATCH_ERROR;
3142 goto cleanup;
3143 }
3144 }
3145 else
3146 {
3147 gfc_error ("%s attribute at %L is not allowed outside of the "
3148 "specification part of a module", attr, &seen_at[d]);
3149 m = MATCH_ERROR;
3150 goto cleanup;
3151 }
3152 }
3153
3154 switch (d)
3155 {
3156 case DECL_ALLOCATABLE:
3157 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3158 break;
3159
3160 case DECL_DIMENSION:
3161 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3162 break;
3163
3164 case DECL_EXTERNAL:
3165 t = gfc_add_external (&current_attr, &seen_at[d]);
3166 break;
3167
3168 case DECL_IN:
3169 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3170 break;
3171
3172 case DECL_OUT:
3173 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3174 break;
3175
3176 case DECL_INOUT:
3177 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3178 break;
3179
3180 case DECL_INTRINSIC:
3181 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3182 break;
3183
3184 case DECL_OPTIONAL:
3185 t = gfc_add_optional (&current_attr, &seen_at[d]);
3186 break;
3187
3188 case DECL_PARAMETER:
3189 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3190 break;
3191
3192 case DECL_POINTER:
3193 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3194 break;
3195
3196 case DECL_PROTECTED:
3197 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3198 {
3199 gfc_error ("PROTECTED at %C only allowed in specification "
3200 "part of a module");
3201 t = FAILURE;
3202 break;
3203 }
3204
3205 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3206 "attribute at %C")
3207 == FAILURE)
3208 t = FAILURE;
3209 else
3210 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3211 break;
3212
3213 case DECL_PRIVATE:
3214 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3215 &seen_at[d]);
3216 break;
3217
3218 case DECL_PUBLIC:
3219 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3220 &seen_at[d]);
3221 break;
3222
3223 case DECL_SAVE:
3224 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
3225 break;
3226
3227 case DECL_TARGET:
3228 t = gfc_add_target (&current_attr, &seen_at[d]);
3229 break;
3230
3231 case DECL_IS_BIND_C:
3232 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3233 break;
3234
3235 case DECL_VALUE:
3236 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3237 "at %C")
3238 == FAILURE)
3239 t = FAILURE;
3240 else
3241 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3242 break;
3243
3244 case DECL_VOLATILE:
3245 if (gfc_notify_std (GFC_STD_F2003,
3246 "Fortran 2003: VOLATILE attribute at %C")
3247 == FAILURE)
3248 t = FAILURE;
3249 else
3250 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3251 break;
3252
3253 default:
3254 gfc_internal_error ("match_attr_spec(): Bad attribute");
3255 }
3256
3257 if (t == FAILURE)
3258 {
3259 m = MATCH_ERROR;
3260 goto cleanup;
3261 }
3262 }
3263
3264 colon_seen = 1;
3265 return MATCH_YES;
3266
3267 cleanup:
3268 gfc_current_locus = start;
3269 gfc_free_array_spec (current_as);
3270 current_as = NULL;
3271 return m;
3272 }
3273
3274
3275 /* Set the binding label, dest_label, either with the binding label
3276 stored in the given gfc_typespec, ts, or if none was provided, it
3277 will be the symbol name in all lower case, as required by the draft
3278 (J3/04-007, section 15.4.1). If a binding label was given and
3279 there is more than one argument (num_idents), it is an error. */
3280
3281 gfc_try
3282 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3283 {
3284 if (num_idents > 1 && has_name_equals)
3285 {
3286 gfc_error ("Multiple identifiers provided with "
3287 "single NAME= specifier at %C");
3288 return FAILURE;
3289 }
3290
3291 if (curr_binding_label[0] != '\0')
3292 {
3293 /* Binding label given; store in temp holder til have sym. */
3294 strcpy (dest_label, curr_binding_label);
3295 }
3296 else
3297 {
3298 /* No binding label given, and the NAME= specifier did not exist,
3299 which means there was no NAME="". */
3300 if (sym_name != NULL && has_name_equals == 0)
3301 strcpy (dest_label, sym_name);
3302 }
3303
3304 return SUCCESS;
3305 }
3306
3307
3308 /* Set the status of the given common block as being BIND(C) or not,
3309 depending on the given parameter, is_bind_c. */
3310
3311 void
3312 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3313 {
3314 com_block->is_bind_c = is_bind_c;
3315 return;
3316 }
3317
3318
3319 /* Verify that the given gfc_typespec is for a C interoperable type. */
3320
3321 gfc_try
3322 verify_c_interop (gfc_typespec *ts)
3323 {
3324 if (ts->type == BT_DERIVED && ts->derived != NULL)
3325 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3326 else if (ts->is_c_interop != 1)
3327 return FAILURE;
3328
3329 return SUCCESS;
3330 }
3331
3332
3333 /* Verify that the variables of a given common block, which has been
3334 defined with the attribute specifier bind(c), to be of a C
3335 interoperable type. Errors will be reported here, if
3336 encountered. */
3337
3338 gfc_try
3339 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3340 {
3341 gfc_symbol *curr_sym = NULL;
3342 gfc_try retval = SUCCESS;
3343
3344 curr_sym = com_block->head;
3345
3346 /* Make sure we have at least one symbol. */
3347 if (curr_sym == NULL)
3348 return retval;
3349
3350 /* Here we know we have a symbol, so we'll execute this loop
3351 at least once. */
3352 do
3353 {
3354 /* The second to last param, 1, says this is in a common block. */
3355 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3356 curr_sym = curr_sym->common_next;
3357 } while (curr_sym != NULL);
3358
3359 return retval;
3360 }
3361
3362
3363 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3364 an appropriate error message is reported. */
3365
3366 gfc_try
3367 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3368 int is_in_common, gfc_common_head *com_block)
3369 {
3370 bool bind_c_function = false;
3371 gfc_try retval = SUCCESS;
3372
3373 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3374 bind_c_function = true;
3375
3376 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3377 {
3378 tmp_sym = tmp_sym->result;
3379 /* Make sure it wasn't an implicitly typed result. */
3380 if (tmp_sym->attr.implicit_type)
3381 {
3382 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3383 "%L may not be C interoperable", tmp_sym->name,
3384 &tmp_sym->declared_at);
3385 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3386 /* Mark it as C interoperable to prevent duplicate warnings. */
3387 tmp_sym->ts.is_c_interop = 1;
3388 tmp_sym->attr.is_c_interop = 1;
3389 }
3390 }
3391
3392 /* Here, we know we have the bind(c) attribute, so if we have
3393 enough type info, then verify that it's a C interop kind.
3394 The info could be in the symbol already, or possibly still in
3395 the given ts (current_ts), so look in both. */
3396 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3397 {
3398 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3399 {
3400 /* See if we're dealing with a sym in a common block or not. */
3401 if (is_in_common == 1)
3402 {
3403 gfc_warning ("Variable '%s' in common block '%s' at %L "
3404 "may not be a C interoperable "
3405 "kind though common block '%s' is BIND(C)",
3406 tmp_sym->name, com_block->name,
3407 &(tmp_sym->declared_at), com_block->name);
3408 }
3409 else
3410 {
3411 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3412 gfc_error ("Type declaration '%s' at %L is not C "
3413 "interoperable but it is BIND(C)",
3414 tmp_sym->name, &(tmp_sym->declared_at));
3415 else
3416 gfc_warning ("Variable '%s' at %L "
3417 "may not be a C interoperable "
3418 "kind but it is bind(c)",
3419 tmp_sym->name, &(tmp_sym->declared_at));
3420 }
3421 }
3422
3423 /* Variables declared w/in a common block can't be bind(c)
3424 since there's no way for C to see these variables, so there's
3425 semantically no reason for the attribute. */
3426 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3427 {
3428 gfc_error ("Variable '%s' in common block '%s' at "
3429 "%L cannot be declared with BIND(C) "
3430 "since it is not a global",
3431 tmp_sym->name, com_block->name,
3432 &(tmp_sym->declared_at));
3433 retval = FAILURE;
3434 }
3435
3436 /* Scalar variables that are bind(c) can not have the pointer
3437 or allocatable attributes. */
3438 if (tmp_sym->attr.is_bind_c == 1)
3439 {
3440 if (tmp_sym->attr.pointer == 1)
3441 {
3442 gfc_error ("Variable '%s' at %L cannot have both the "
3443 "POINTER and BIND(C) attributes",
3444 tmp_sym->name, &(tmp_sym->declared_at));
3445 retval = FAILURE;
3446 }
3447
3448 if (tmp_sym->attr.allocatable == 1)
3449 {
3450 gfc_error ("Variable '%s' at %L cannot have both the "
3451 "ALLOCATABLE and BIND(C) attributes",
3452 tmp_sym->name, &(tmp_sym->declared_at));
3453 retval = FAILURE;
3454 }
3455
3456 }
3457
3458 /* If it is a BIND(C) function, make sure the return value is a
3459 scalar value. The previous tests in this function made sure
3460 the type is interoperable. */
3461 if (bind_c_function && tmp_sym->as != NULL)
3462 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3463 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3464
3465 /* BIND(C) functions can not return a character string. */
3466 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3467 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3468 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3469 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3470 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3471 "be a character string", tmp_sym->name,
3472 &(tmp_sym->declared_at));
3473 }
3474
3475 /* See if the symbol has been marked as private. If it has, make sure
3476 there is no binding label and warn the user if there is one. */
3477 if (tmp_sym->attr.access == ACCESS_PRIVATE
3478 && tmp_sym->binding_label[0] != '\0')
3479 /* Use gfc_warning_now because we won't say that the symbol fails
3480 just because of this. */
3481 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3482 "given the binding label '%s'", tmp_sym->name,
3483 &(tmp_sym->declared_at), tmp_sym->binding_label);
3484
3485 return retval;
3486 }
3487
3488
3489 /* Set the appropriate fields for a symbol that's been declared as
3490 BIND(C) (the is_bind_c flag and the binding label), and verify that
3491 the type is C interoperable. Errors are reported by the functions
3492 used to set/test these fields. */
3493
3494 gfc_try
3495 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3496 {
3497 gfc_try retval = SUCCESS;
3498
3499 /* TODO: Do we need to make sure the vars aren't marked private? */
3500
3501 /* Set the is_bind_c bit in symbol_attribute. */
3502 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3503
3504 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3505 num_idents) != SUCCESS)
3506 return FAILURE;
3507
3508 return retval;
3509 }
3510
3511
3512 /* Set the fields marking the given common block as BIND(C), including
3513 a binding label, and report any errors encountered. */
3514
3515 gfc_try
3516 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3517 {
3518 gfc_try retval = SUCCESS;
3519
3520 /* destLabel, common name, typespec (which may have binding label). */
3521 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3522 != SUCCESS)
3523 return FAILURE;
3524
3525 /* Set the given common block (com_block) to being bind(c) (1). */
3526 set_com_block_bind_c (com_block, 1);
3527
3528 return retval;
3529 }
3530
3531
3532 /* Retrieve the list of one or more identifiers that the given bind(c)
3533 attribute applies to. */
3534
3535 gfc_try
3536 get_bind_c_idents (void)
3537 {
3538 char name[GFC_MAX_SYMBOL_LEN + 1];
3539 int num_idents = 0;
3540 gfc_symbol *tmp_sym = NULL;
3541 match found_id;
3542 gfc_common_head *com_block = NULL;
3543
3544 if (gfc_match_name (name) == MATCH_YES)
3545 {
3546 found_id = MATCH_YES;
3547 gfc_get_ha_symbol (name, &tmp_sym);
3548 }
3549 else if (match_common_name (name) == MATCH_YES)
3550 {
3551 found_id = MATCH_YES;
3552 com_block = gfc_get_common (name, 0);
3553 }
3554 else
3555 {
3556 gfc_error ("Need either entity or common block name for "
3557 "attribute specification statement at %C");
3558 return FAILURE;
3559 }
3560
3561 /* Save the current identifier and look for more. */
3562 do
3563 {
3564 /* Increment the number of identifiers found for this spec stmt. */
3565 num_idents++;
3566
3567 /* Make sure we have a sym or com block, and verify that it can
3568 be bind(c). Set the appropriate field(s) and look for more
3569 identifiers. */
3570 if (tmp_sym != NULL || com_block != NULL)
3571 {
3572 if (tmp_sym != NULL)
3573 {
3574 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3575 != SUCCESS)
3576 return FAILURE;
3577 }
3578 else
3579 {
3580 if (set_verify_bind_c_com_block(com_block, num_idents)
3581 != SUCCESS)
3582 return FAILURE;
3583 }
3584
3585 /* Look to see if we have another identifier. */
3586 tmp_sym = NULL;
3587 if (gfc_match_eos () == MATCH_YES)
3588 found_id = MATCH_NO;
3589 else if (gfc_match_char (',') != MATCH_YES)
3590 found_id = MATCH_NO;
3591 else if (gfc_match_name (name) == MATCH_YES)
3592 {
3593 found_id = MATCH_YES;
3594 gfc_get_ha_symbol (name, &tmp_sym);
3595 }
3596 else if (match_common_name (name) == MATCH_YES)
3597 {
3598 found_id = MATCH_YES;
3599 com_block = gfc_get_common (name, 0);
3600 }
3601 else
3602 {
3603 gfc_error ("Missing entity or common block name for "
3604 "attribute specification statement at %C");
3605 return FAILURE;
3606 }
3607 }
3608 else
3609 {
3610 gfc_internal_error ("Missing symbol");
3611 }
3612 } while (found_id == MATCH_YES);
3613
3614 /* if we get here we were successful */
3615 return SUCCESS;
3616 }
3617
3618
3619 /* Try and match a BIND(C) attribute specification statement. */
3620
3621 match
3622 gfc_match_bind_c_stmt (void)
3623 {
3624 match found_match = MATCH_NO;
3625 gfc_typespec *ts;
3626
3627 ts = &current_ts;
3628
3629 /* This may not be necessary. */
3630 gfc_clear_ts (ts);
3631 /* Clear the temporary binding label holder. */
3632 curr_binding_label[0] = '\0';
3633
3634 /* Look for the bind(c). */
3635 found_match = gfc_match_bind_c (NULL, true);
3636
3637 if (found_match == MATCH_YES)
3638 {
3639 /* Look for the :: now, but it is not required. */
3640 gfc_match (" :: ");
3641
3642 /* Get the identifier(s) that needs to be updated. This may need to
3643 change to hand the flag(s) for the attr specified so all identifiers
3644 found can have all appropriate parts updated (assuming that the same
3645 spec stmt can have multiple attrs, such as both bind(c) and
3646 allocatable...). */
3647 if (get_bind_c_idents () != SUCCESS)
3648 /* Error message should have printed already. */
3649 return MATCH_ERROR;
3650 }
3651
3652 return found_match;
3653 }
3654
3655
3656 /* Match a data declaration statement. */
3657
3658 match
3659 gfc_match_data_decl (void)
3660 {
3661 gfc_symbol *sym;
3662 match m;
3663 int elem;
3664
3665 num_idents_on_line = 0;
3666
3667 m = gfc_match_type_spec (&current_ts, 0);
3668 if (m != MATCH_YES)
3669 return m;
3670
3671 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3672 {
3673 sym = gfc_use_derived (current_ts.derived);
3674
3675 if (sym == NULL)
3676 {
3677 m = MATCH_ERROR;
3678 goto cleanup;
3679 }
3680
3681 current_ts.derived = sym;
3682 }
3683
3684 m = match_attr_spec ();
3685 if (m == MATCH_ERROR)
3686 {
3687 m = MATCH_NO;
3688 goto cleanup;
3689 }
3690
3691 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3692 && !current_ts.derived->attr.zero_comp)
3693 {
3694
3695 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3696 goto ok;
3697
3698 gfc_find_symbol (current_ts.derived->name,
3699 current_ts.derived->ns->parent, 1, &sym);
3700
3701 /* Any symbol that we find had better be a type definition
3702 which has its components defined. */
3703 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3704 && (current_ts.derived->components != NULL
3705 || current_ts.derived->attr.zero_comp))
3706 goto ok;
3707
3708 /* Now we have an error, which we signal, and then fix up
3709 because the knock-on is plain and simple confusing. */
3710 gfc_error_now ("Derived type at %C has not been previously defined "
3711 "and so cannot appear in a derived type definition");
3712 current_attr.pointer = 1;
3713 goto ok;
3714 }
3715
3716 ok:
3717 /* If we have an old-style character declaration, and no new-style
3718 attribute specifications, then there a comma is optional between
3719 the type specification and the variable list. */
3720 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3721 gfc_match_char (',');
3722
3723 /* Give the types/attributes to symbols that follow. Give the element
3724 a number so that repeat character length expressions can be copied. */
3725 elem = 1;
3726 for (;;)
3727 {
3728 num_idents_on_line++;
3729 m = variable_decl (elem++);
3730 if (m == MATCH_ERROR)
3731 goto cleanup;
3732 if (m == MATCH_NO)
3733 break;
3734
3735 if (gfc_match_eos () == MATCH_YES)
3736 goto cleanup;
3737 if (gfc_match_char (',') != MATCH_YES)
3738 break;
3739 }
3740
3741 if (gfc_error_flag_test () == 0)
3742 gfc_error ("Syntax error in data declaration at %C");
3743 m = MATCH_ERROR;
3744
3745 gfc_free_data_all (gfc_current_ns);
3746
3747 cleanup:
3748 gfc_free_array_spec (current_as);
3749 current_as = NULL;
3750 return m;
3751 }
3752
3753
3754 /* Match a prefix associated with a function or subroutine
3755 declaration. If the typespec pointer is nonnull, then a typespec
3756 can be matched. Note that if nothing matches, MATCH_YES is
3757 returned (the null string was matched). */
3758
3759 match
3760 gfc_match_prefix (gfc_typespec *ts)
3761 {
3762 bool seen_type;
3763
3764 gfc_clear_attr (&current_attr);
3765 seen_type = 0;
3766
3767 gcc_assert (!gfc_matching_prefix);
3768 gfc_matching_prefix = true;
3769
3770 loop:
3771 if (!seen_type && ts != NULL
3772 && gfc_match_type_spec (ts, 0) == MATCH_YES
3773 && gfc_match_space () == MATCH_YES)
3774 {
3775
3776 seen_type = 1;
3777 goto loop;
3778 }
3779
3780 if (gfc_match ("elemental% ") == MATCH_YES)
3781 {
3782 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3783 goto error;
3784
3785 goto loop;
3786 }
3787
3788 if (gfc_match ("pure% ") == MATCH_YES)
3789 {
3790 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3791 goto error;
3792
3793 goto loop;
3794 }
3795
3796 if (gfc_match ("recursive% ") == MATCH_YES)
3797 {
3798 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3799 goto error;
3800
3801 goto loop;
3802 }
3803
3804 /* At this point, the next item is not a prefix. */
3805 gcc_assert (gfc_matching_prefix);
3806 gfc_matching_prefix = false;
3807 return MATCH_YES;
3808
3809 error:
3810 gcc_assert (gfc_matching_prefix);
3811 gfc_matching_prefix = false;
3812 return MATCH_ERROR;
3813 }
3814
3815
3816 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3817
3818 static gfc_try
3819 copy_prefix (symbol_attribute *dest, locus *where)
3820 {
3821 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3822 return FAILURE;
3823
3824 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3825 return FAILURE;
3826
3827 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3828 return FAILURE;
3829
3830 return SUCCESS;
3831 }
3832
3833
3834 /* Match a formal argument list. */
3835
3836 match
3837 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3838 {
3839 gfc_formal_arglist *head, *tail, *p, *q;
3840 char name[GFC_MAX_SYMBOL_LEN + 1];
3841 gfc_symbol *sym;
3842 match m;
3843
3844 head = tail = NULL;
3845
3846 if (gfc_match_char ('(') != MATCH_YES)
3847 {
3848 if (null_flag)
3849 goto ok;
3850 return MATCH_NO;
3851 }
3852
3853 if (gfc_match_char (')') == MATCH_YES)
3854 goto ok;
3855
3856 for (;;)
3857 {
3858 if (gfc_match_char ('*') == MATCH_YES)
3859 sym = NULL;
3860 else
3861 {
3862 m = gfc_match_name (name);
3863 if (m != MATCH_YES)
3864 goto cleanup;
3865
3866 if (gfc_get_symbol (name, NULL, &sym))
3867 goto cleanup;
3868 }
3869
3870 p = gfc_get_formal_arglist ();
3871
3872 if (head == NULL)
3873 head = tail = p;
3874 else
3875 {
3876 tail->next = p;
3877 tail = p;
3878 }
3879
3880 tail->sym = sym;
3881
3882 /* We don't add the VARIABLE flavor because the name could be a
3883 dummy procedure. We don't apply these attributes to formal
3884 arguments of statement functions. */
3885 if (sym != NULL && !st_flag
3886 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3887 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3888 {
3889 m = MATCH_ERROR;
3890 goto cleanup;
3891 }
3892
3893 /* The name of a program unit can be in a different namespace,
3894 so check for it explicitly. After the statement is accepted,
3895 the name is checked for especially in gfc_get_symbol(). */
3896 if (gfc_new_block != NULL && sym != NULL
3897 && strcmp (sym->name, gfc_new_block->name) == 0)
3898 {
3899 gfc_error ("Name '%s' at %C is the name of the procedure",
3900 sym->name);
3901 m = MATCH_ERROR;
3902 goto cleanup;
3903 }
3904
3905 if (gfc_match_char (')') == MATCH_YES)
3906 goto ok;
3907
3908 m = gfc_match_char (',');
3909 if (m != MATCH_YES)
3910 {
3911 gfc_error ("Unexpected junk in formal argument list at %C");
3912 goto cleanup;
3913 }
3914 }
3915
3916 ok:
3917 /* Check for duplicate symbols in the formal argument list. */
3918 if (head != NULL)
3919 {
3920 for (p = head; p->next; p = p->next)
3921 {
3922 if (p->sym == NULL)
3923 continue;
3924
3925 for (q = p->next; q; q = q->next)
3926 if (p->sym == q->sym)
3927 {
3928 gfc_error ("Duplicate symbol '%s' in formal argument list "
3929 "at %C", p->sym->name);
3930
3931 m = MATCH_ERROR;
3932 goto cleanup;
3933 }
3934 }
3935 }
3936
3937 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3938 == FAILURE)
3939 {
3940 m = MATCH_ERROR;
3941 goto cleanup;
3942 }
3943
3944 return MATCH_YES;
3945
3946 cleanup:
3947 gfc_free_formal_arglist (head);
3948 return m;
3949 }
3950
3951
3952 /* Match a RESULT specification following a function declaration or
3953 ENTRY statement. Also matches the end-of-statement. */
3954
3955 static match
3956 match_result (gfc_symbol *function, gfc_symbol **result)
3957 {
3958 char name[GFC_MAX_SYMBOL_LEN + 1];
3959 gfc_symbol *r;
3960 match m;
3961
3962 if (gfc_match (" result (") != MATCH_YES)
3963 return MATCH_NO;
3964
3965 m = gfc_match_name (name);
3966 if (m != MATCH_YES)
3967 return m;
3968
3969 /* Get the right paren, and that's it because there could be the
3970 bind(c) attribute after the result clause. */
3971 if (gfc_match_char(')') != MATCH_YES)
3972 {
3973 /* TODO: should report the missing right paren here. */
3974 return MATCH_ERROR;
3975 }
3976
3977 if (strcmp (function->name, name) == 0)
3978 {
3979 gfc_error ("RESULT variable at %C must be different than function name");
3980 return MATCH_ERROR;
3981 }
3982
3983 if (gfc_get_symbol (name, NULL, &r))
3984 return MATCH_ERROR;
3985
3986 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3987 return MATCH_ERROR;
3988
3989 *result = r;
3990
3991 return MATCH_YES;
3992 }
3993
3994
3995 /* Match a function suffix, which could be a combination of a result
3996 clause and BIND(C), either one, or neither. The draft does not
3997 require them to come in a specific order. */
3998
3999 match
4000 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4001 {
4002 match is_bind_c; /* Found bind(c). */
4003 match is_result; /* Found result clause. */
4004 match found_match; /* Status of whether we've found a good match. */
4005 char peek_char; /* Character we're going to peek at. */
4006 bool allow_binding_name;
4007
4008 /* Initialize to having found nothing. */
4009 found_match = MATCH_NO;
4010 is_bind_c = MATCH_NO;
4011 is_result = MATCH_NO;
4012
4013 /* Get the next char to narrow between result and bind(c). */
4014 gfc_gobble_whitespace ();
4015 peek_char = gfc_peek_ascii_char ();
4016
4017 /* C binding names are not allowed for internal procedures. */
4018 if (gfc_current_state () == COMP_CONTAINS
4019 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4020 allow_binding_name = false;
4021 else
4022 allow_binding_name = true;
4023
4024 switch (peek_char)
4025 {
4026 case 'r':
4027 /* Look for result clause. */
4028 is_result = match_result (sym, result);
4029 if (is_result == MATCH_YES)
4030 {
4031 /* Now see if there is a bind(c) after it. */
4032 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4033 /* We've found the result clause and possibly bind(c). */
4034 found_match = MATCH_YES;
4035 }
4036 else
4037 /* This should only be MATCH_ERROR. */
4038 found_match = is_result;
4039 break;
4040 case 'b':
4041 /* Look for bind(c) first. */
4042 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4043 if (is_bind_c == MATCH_YES)
4044 {
4045 /* Now see if a result clause followed it. */
4046 is_result = match_result (sym, result);
4047 found_match = MATCH_YES;
4048 }
4049 else
4050 {
4051 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4052 found_match = MATCH_ERROR;
4053 }
4054 break;
4055 default:
4056 gfc_error ("Unexpected junk after function declaration at %C");
4057 found_match = MATCH_ERROR;
4058 break;
4059 }
4060
4061 if (is_bind_c == MATCH_YES)
4062 {
4063 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4064 if (gfc_current_state () == COMP_CONTAINS
4065 && sym->ns->proc_name->attr.flavor != FL_MODULE
4066 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4067 "at %L may not be specified for an internal "
4068 "procedure", &gfc_current_locus)
4069 == FAILURE)
4070 return MATCH_ERROR;
4071
4072 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4073 == FAILURE)
4074 return MATCH_ERROR;
4075 }
4076
4077 return found_match;
4078 }
4079
4080
4081 /* Procedure pointer return value without RESULT statement:
4082 Add "hidden" result variable named "ppr@". */
4083
4084 static gfc_try
4085 add_hidden_procptr_result (gfc_symbol *sym)
4086 {
4087 bool case1,case2;
4088
4089 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4090 return FAILURE;
4091
4092 /* First usage case: PROCEDURE and EXTERNAL statements. */
4093 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4094 && strcmp (gfc_current_block ()->name, sym->name) == 0
4095 && sym->attr.external;
4096 /* Second usage case: INTERFACE statements. */
4097 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4098 && gfc_state_stack->previous->state == COMP_FUNCTION
4099 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4100
4101 if (case1 || case2)
4102 {
4103 gfc_symtree *stree;
4104 if (case1)
4105 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4106 else if (case2)
4107 {
4108 gfc_symtree *st2;
4109 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4110 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4111 st2->n.sym = stree->n.sym;
4112 }
4113 sym->result = stree->n.sym;
4114
4115 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4116 sym->result->attr.pointer = sym->attr.pointer;
4117 sym->result->attr.external = sym->attr.external;
4118 sym->result->attr.referenced = sym->attr.referenced;
4119 sym->result->ts = sym->ts;
4120 sym->attr.proc_pointer = 0;
4121 sym->attr.pointer = 0;
4122 sym->attr.external = 0;
4123 if (sym->result->attr.external && sym->result->attr.pointer)
4124 {
4125 sym->result->attr.pointer = 0;
4126 sym->result->attr.proc_pointer = 1;
4127 }
4128
4129 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4130 }
4131 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4132 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4133 && sym->result && sym->result != sym && sym->result->attr.external
4134 && sym == gfc_current_ns->proc_name
4135 && sym == sym->result->ns->proc_name
4136 && strcmp ("ppr@", sym->result->name) == 0)
4137 {
4138 sym->result->attr.proc_pointer = 1;
4139 sym->attr.pointer = 0;
4140 return SUCCESS;
4141 }
4142 else
4143 return FAILURE;
4144 }
4145
4146
4147 /* Match the interface for a PROCEDURE declaration,
4148 including brackets (R1212). */
4149
4150 static match
4151 match_procedure_interface (gfc_symbol **proc_if)
4152 {
4153 match m;
4154 gfc_symtree *st;
4155 locus old_loc, entry_loc;
4156 gfc_namespace *old_ns = gfc_current_ns;
4157 char name[GFC_MAX_SYMBOL_LEN + 1];
4158
4159 old_loc = entry_loc = gfc_current_locus;
4160 gfc_clear_ts (&current_ts);
4161
4162 if (gfc_match (" (") != MATCH_YES)
4163 {
4164 gfc_current_locus = entry_loc;
4165 return MATCH_NO;
4166 }
4167
4168 /* Get the type spec. for the procedure interface. */
4169 old_loc = gfc_current_locus;
4170 m = gfc_match_type_spec (&current_ts, 0);
4171 gfc_gobble_whitespace ();
4172 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4173 goto got_ts;
4174
4175 if (m == MATCH_ERROR)
4176 return m;
4177
4178 /* Procedure interface is itself a procedure. */
4179 gfc_current_locus = old_loc;
4180 m = gfc_match_name (name);
4181
4182 /* First look to see if it is already accessible in the current
4183 namespace because it is use associated or contained. */
4184 st = NULL;
4185 if (gfc_find_sym_tree (name, NULL, 0, &st))
4186 return MATCH_ERROR;
4187
4188 /* If it is still not found, then try the parent namespace, if it
4189 exists and create the symbol there if it is still not found. */
4190 if (gfc_current_ns->parent)
4191 gfc_current_ns = gfc_current_ns->parent;
4192 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4193 return MATCH_ERROR;
4194
4195 gfc_current_ns = old_ns;
4196 *proc_if = st->n.sym;
4197
4198 /* Various interface checks. */
4199 if (*proc_if)
4200 {
4201 (*proc_if)->refs++;
4202 /* Resolve interface if possible. That way, attr.procedure is only set
4203 if it is declared by a later procedure-declaration-stmt, which is
4204 invalid per C1212. */
4205 while ((*proc_if)->ts.interface)
4206 *proc_if = (*proc_if)->ts.interface;
4207
4208 if ((*proc_if)->generic)
4209 {
4210 gfc_error ("Interface '%s' at %C may not be generic",
4211 (*proc_if)->name);
4212 return MATCH_ERROR;
4213 }
4214 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4215 {
4216 gfc_error ("Interface '%s' at %C may not be a statement function",
4217 (*proc_if)->name);
4218 return MATCH_ERROR;
4219 }
4220 /* Handle intrinsic procedures. */
4221 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4222 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4223 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4224 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4225 (*proc_if)->attr.intrinsic = 1;
4226 if ((*proc_if)->attr.intrinsic
4227 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4228 {
4229 gfc_error ("Intrinsic procedure '%s' not allowed "
4230 "in PROCEDURE statement at %C", (*proc_if)->name);
4231 return MATCH_ERROR;
4232 }
4233 }
4234
4235 got_ts:
4236 if (gfc_match (" )") != MATCH_YES)
4237 {
4238 gfc_current_locus = entry_loc;
4239 return MATCH_NO;
4240 }
4241
4242 return MATCH_YES;
4243 }
4244
4245
4246 /* Match a PROCEDURE declaration (R1211). */
4247
4248 static match
4249 match_procedure_decl (void)
4250 {
4251 match m;
4252 gfc_symbol *sym, *proc_if = NULL;
4253 int num;
4254 gfc_expr *initializer = NULL;
4255
4256 /* Parse interface (with brackets). */
4257 m = match_procedure_interface (&proc_if);
4258 if (m != MATCH_YES)
4259 return m;
4260
4261 /* Parse attributes (with colons). */
4262 m = match_attr_spec();
4263 if (m == MATCH_ERROR)
4264 return MATCH_ERROR;
4265
4266 /* Get procedure symbols. */
4267 for(num=1;;num++)
4268 {
4269 m = gfc_match_symbol (&sym, 0);
4270 if (m == MATCH_NO)
4271 goto syntax;
4272 else if (m == MATCH_ERROR)
4273 return m;
4274
4275 /* Add current_attr to the symbol attributes. */
4276 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4277 return MATCH_ERROR;
4278
4279 if (sym->attr.is_bind_c)
4280 {
4281 /* Check for C1218. */
4282 if (!proc_if || !proc_if->attr.is_bind_c)
4283 {
4284 gfc_error ("BIND(C) attribute at %C requires "
4285 "an interface with BIND(C)");
4286 return MATCH_ERROR;
4287 }
4288 /* Check for C1217. */
4289 if (has_name_equals && sym->attr.pointer)
4290 {
4291 gfc_error ("BIND(C) procedure with NAME may not have "
4292 "POINTER attribute at %C");
4293 return MATCH_ERROR;
4294 }
4295 if (has_name_equals && sym->attr.dummy)
4296 {
4297 gfc_error ("Dummy procedure at %C may not have "
4298 "BIND(C) attribute with NAME");
4299 return MATCH_ERROR;
4300 }
4301 /* Set binding label for BIND(C). */
4302 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4303 return MATCH_ERROR;
4304 }
4305
4306 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4307 return MATCH_ERROR;
4308
4309 if (add_hidden_procptr_result (sym) == SUCCESS)
4310 sym = sym->result;
4311
4312 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4313 return MATCH_ERROR;
4314
4315 /* Set interface. */
4316 if (proc_if != NULL)
4317 {
4318 if (sym->ts.type != BT_UNKNOWN)
4319 {
4320 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4321 sym->name, &gfc_current_locus,
4322 gfc_basic_typename (sym->ts.type));
4323 return MATCH_ERROR;
4324 }
4325 sym->ts.interface = proc_if;
4326 sym->attr.untyped = 1;
4327 sym->attr.if_source = IFSRC_IFBODY;
4328 }
4329 else if (current_ts.type != BT_UNKNOWN)
4330 {
4331 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4332 return MATCH_ERROR;
4333 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4334 sym->ts.interface->ts = current_ts;
4335 sym->ts.interface->attr.function = 1;
4336 sym->attr.function = sym->ts.interface->attr.function;
4337 sym->attr.if_source = IFSRC_UNKNOWN;
4338 }
4339
4340 if (gfc_match (" =>") == MATCH_YES)
4341 {
4342 if (!current_attr.pointer)
4343 {
4344 gfc_error ("Initialization at %C isn't for a pointer variable");
4345 m = MATCH_ERROR;
4346 goto cleanup;
4347 }
4348
4349 m = gfc_match_null (&initializer);
4350 if (m == MATCH_NO)
4351 {
4352 gfc_error ("Pointer initialization requires a NULL() at %C");
4353 m = MATCH_ERROR;
4354 }
4355
4356 if (gfc_pure (NULL))
4357 {
4358 gfc_error ("Initialization of pointer at %C is not allowed in "
4359 "a PURE procedure");
4360 m = MATCH_ERROR;
4361 }
4362
4363 if (m != MATCH_YES)
4364 goto cleanup;
4365
4366 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4367 != SUCCESS)
4368 goto cleanup;
4369
4370 }
4371
4372 gfc_set_sym_referenced (sym);
4373
4374 if (gfc_match_eos () == MATCH_YES)
4375 return MATCH_YES;
4376 if (gfc_match_char (',') != MATCH_YES)
4377 goto syntax;
4378 }
4379
4380 syntax:
4381 gfc_error ("Syntax error in PROCEDURE statement at %C");
4382 return MATCH_ERROR;
4383
4384 cleanup:
4385 /* Free stuff up and return. */
4386 gfc_free_expr (initializer);
4387 return m;
4388 }
4389
4390
4391 static match
4392 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4393
4394
4395 /* Match a procedure pointer component declaration (R445). */
4396
4397 static match
4398 match_ppc_decl (void)
4399 {
4400 match m;
4401 gfc_symbol *proc_if = NULL;
4402 gfc_typespec ts;
4403 int num;
4404 gfc_component *c;
4405 gfc_expr *initializer = NULL;
4406 gfc_typebound_proc* tb;
4407 char name[GFC_MAX_SYMBOL_LEN + 1];
4408
4409 /* Parse interface (with brackets). */
4410 m = match_procedure_interface (&proc_if);
4411 if (m != MATCH_YES)
4412 goto syntax;
4413
4414 /* Parse attributes. */
4415 tb = XCNEW (gfc_typebound_proc);
4416 tb->where = gfc_current_locus;
4417 m = match_binding_attributes (tb, false, true);
4418 if (m == MATCH_ERROR)
4419 return m;
4420
4421 gfc_clear_attr (&current_attr);
4422 current_attr.procedure = 1;
4423 current_attr.proc_pointer = 1;
4424 current_attr.access = tb->access;
4425 current_attr.flavor = FL_PROCEDURE;
4426
4427 /* Match the colons (required). */
4428 if (gfc_match (" ::") != MATCH_YES)
4429 {
4430 gfc_error ("Expected '::' after binding-attributes at %C");
4431 return MATCH_ERROR;
4432 }
4433
4434 /* Check for C450. */
4435 if (!tb->nopass && proc_if == NULL)
4436 {
4437 gfc_error("NOPASS or explicit interface required at %C");
4438 return MATCH_ERROR;
4439 }
4440
4441 /* Match PPC names. */
4442 ts = current_ts;
4443 for(num=1;;num++)
4444 {
4445 m = gfc_match_name (name);
4446 if (m == MATCH_NO)
4447 goto syntax;
4448 else if (m == MATCH_ERROR)
4449 return m;
4450
4451 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4452 return MATCH_ERROR;
4453
4454 /* Add current_attr to the symbol attributes. */
4455 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4456 return MATCH_ERROR;
4457
4458 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4459 return MATCH_ERROR;
4460
4461 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4462 return MATCH_ERROR;
4463
4464 c->tb = tb;
4465
4466 /* Set interface. */
4467 if (proc_if != NULL)
4468 {
4469 c->ts.interface = proc_if;
4470 c->attr.untyped = 1;
4471 c->attr.if_source = IFSRC_IFBODY;
4472 }
4473 else if (ts.type != BT_UNKNOWN)
4474 {
4475 c->ts = ts;
4476 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4477 c->ts.interface->ts = ts;
4478 c->ts.interface->attr.function = 1;
4479 c->attr.function = c->ts.interface->attr.function;
4480 c->attr.if_source = IFSRC_UNKNOWN;
4481 }
4482
4483 if (gfc_match (" =>") == MATCH_YES)
4484 {
4485 m = gfc_match_null (&initializer);
4486 if (m == MATCH_NO)
4487 {
4488 gfc_error ("Pointer initialization requires a NULL() at %C");
4489 m = MATCH_ERROR;
4490 }
4491 if (gfc_pure (NULL))
4492 {
4493 gfc_error ("Initialization of pointer at %C is not allowed in "
4494 "a PURE procedure");
4495 m = MATCH_ERROR;
4496 }
4497 if (m != MATCH_YES)
4498 {
4499 gfc_free_expr (initializer);
4500 return m;
4501 }
4502 c->initializer = initializer;
4503 }
4504
4505 if (gfc_match_eos () == MATCH_YES)
4506 return MATCH_YES;
4507 if (gfc_match_char (',') != MATCH_YES)
4508 goto syntax;
4509 }
4510
4511 syntax:
4512 gfc_error ("Syntax error in procedure pointer component at %C");
4513 return MATCH_ERROR;
4514 }
4515
4516
4517 /* Match a PROCEDURE declaration inside an interface (R1206). */
4518
4519 static match
4520 match_procedure_in_interface (void)
4521 {
4522 match m;
4523 gfc_symbol *sym;
4524 char name[GFC_MAX_SYMBOL_LEN + 1];
4525
4526 if (current_interface.type == INTERFACE_NAMELESS
4527 || current_interface.type == INTERFACE_ABSTRACT)
4528 {
4529 gfc_error ("PROCEDURE at %C must be in a generic interface");
4530 return MATCH_ERROR;
4531 }
4532
4533 for(;;)
4534 {
4535 m = gfc_match_name (name);
4536 if (m == MATCH_NO)
4537 goto syntax;
4538 else if (m == MATCH_ERROR)
4539 return m;
4540 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4541 return MATCH_ERROR;
4542
4543 if (gfc_add_interface (sym) == FAILURE)
4544 return MATCH_ERROR;
4545
4546 if (gfc_match_eos () == MATCH_YES)
4547 break;
4548 if (gfc_match_char (',') != MATCH_YES)
4549 goto syntax;
4550 }
4551
4552 return MATCH_YES;
4553
4554 syntax:
4555 gfc_error ("Syntax error in PROCEDURE statement at %C");
4556 return MATCH_ERROR;
4557 }
4558
4559
4560 /* General matcher for PROCEDURE declarations. */
4561
4562 static match match_procedure_in_type (void);
4563
4564 match
4565 gfc_match_procedure (void)
4566 {
4567 match m;
4568
4569 switch (gfc_current_state ())
4570 {
4571 case COMP_NONE:
4572 case COMP_PROGRAM:
4573 case COMP_MODULE:
4574 case COMP_SUBROUTINE:
4575 case COMP_FUNCTION:
4576 m = match_procedure_decl ();
4577 break;
4578 case COMP_INTERFACE:
4579 m = match_procedure_in_interface ();
4580 break;
4581 case COMP_DERIVED:
4582 m = match_ppc_decl ();
4583 break;
4584 case COMP_DERIVED_CONTAINS:
4585 m = match_procedure_in_type ();
4586 break;
4587 default:
4588 return MATCH_NO;
4589 }
4590
4591 if (m != MATCH_YES)
4592 return m;
4593
4594 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4595 == FAILURE)
4596 return MATCH_ERROR;
4597
4598 return m;
4599 }
4600
4601
4602 /* Warn if a matched procedure has the same name as an intrinsic; this is
4603 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4604 parser-state-stack to find out whether we're in a module. */
4605
4606 static void
4607 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4608 {
4609 bool in_module;
4610
4611 in_module = (gfc_state_stack->previous
4612 && gfc_state_stack->previous->state == COMP_MODULE);
4613
4614 gfc_warn_intrinsic_shadow (sym, in_module, func);
4615 }
4616
4617
4618 /* Match a function declaration. */
4619
4620 match
4621 gfc_match_function_decl (void)
4622 {
4623 char name[GFC_MAX_SYMBOL_LEN + 1];
4624 gfc_symbol *sym, *result;
4625 locus old_loc;
4626 match m;
4627 match suffix_match;
4628 match found_match; /* Status returned by match func. */
4629
4630 if (gfc_current_state () != COMP_NONE
4631 && gfc_current_state () != COMP_INTERFACE
4632 && gfc_current_state () != COMP_CONTAINS)
4633 return MATCH_NO;
4634
4635 gfc_clear_ts (&current_ts);
4636
4637 old_loc = gfc_current_locus;
4638
4639 m = gfc_match_prefix (&current_ts);
4640 if (m != MATCH_YES)
4641 {
4642 gfc_current_locus = old_loc;
4643 return m;
4644 }
4645
4646 if (gfc_match ("function% %n", name) != MATCH_YES)
4647 {
4648 gfc_current_locus = old_loc;
4649 return MATCH_NO;
4650 }
4651 if (get_proc_name (name, &sym, false))
4652 return MATCH_ERROR;
4653
4654 if (add_hidden_procptr_result (sym) == SUCCESS)
4655 sym = sym->result;
4656
4657 gfc_new_block = sym;
4658
4659 m = gfc_match_formal_arglist (sym, 0, 0);
4660 if (m == MATCH_NO)
4661 {
4662 gfc_error ("Expected formal argument list in function "
4663 "definition at %C");
4664 m = MATCH_ERROR;
4665 goto cleanup;
4666 }
4667 else if (m == MATCH_ERROR)
4668 goto cleanup;
4669
4670 result = NULL;
4671
4672 /* According to the draft, the bind(c) and result clause can
4673 come in either order after the formal_arg_list (i.e., either
4674 can be first, both can exist together or by themselves or neither
4675 one). Therefore, the match_result can't match the end of the
4676 string, and check for the bind(c) or result clause in either order. */
4677 found_match = gfc_match_eos ();
4678
4679 /* Make sure that it isn't already declared as BIND(C). If it is, it
4680 must have been marked BIND(C) with a BIND(C) attribute and that is
4681 not allowed for procedures. */
4682 if (sym->attr.is_bind_c == 1)
4683 {
4684 sym->attr.is_bind_c = 0;
4685 if (sym->old_symbol != NULL)
4686 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4687 "variables or common blocks",
4688 &(sym->old_symbol->declared_at));
4689 else
4690 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4691 "variables or common blocks", &gfc_current_locus);
4692 }
4693
4694 if (found_match != MATCH_YES)
4695 {
4696 /* If we haven't found the end-of-statement, look for a suffix. */
4697 suffix_match = gfc_match_suffix (sym, &result);
4698 if (suffix_match == MATCH_YES)
4699 /* Need to get the eos now. */
4700 found_match = gfc_match_eos ();
4701 else
4702 found_match = suffix_match;
4703 }
4704
4705 if(found_match != MATCH_YES)
4706 m = MATCH_ERROR;
4707 else
4708 {
4709 /* Make changes to the symbol. */
4710 m = MATCH_ERROR;
4711
4712 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4713 goto cleanup;
4714
4715 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4716 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4717 goto cleanup;
4718
4719 /* Delay matching the function characteristics until after the
4720 specification block by signalling kind=-1. */
4721 sym->declared_at = old_loc;
4722 if (current_ts.type != BT_UNKNOWN)
4723 current_ts.kind = -1;
4724 else
4725 current_ts.kind = 0;
4726
4727 if (result == NULL)
4728 {
4729 if (current_ts.type != BT_UNKNOWN
4730 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4731 goto cleanup;
4732 sym->result = sym;
4733 }
4734 else
4735 {
4736 if (current_ts.type != BT_UNKNOWN
4737 && gfc_add_type (result, &current_ts, &gfc_current_locus)
4738 == FAILURE)
4739 goto cleanup;
4740 sym->result = result;
4741 }
4742
4743 /* Warn if this procedure has the same name as an intrinsic. */
4744 warn_intrinsic_shadow (sym, true);
4745
4746 return MATCH_YES;
4747 }
4748
4749 cleanup:
4750 gfc_current_locus = old_loc;
4751 return m;
4752 }
4753
4754
4755 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4756 pass the name of the entry, rather than the gfc_current_block name, and
4757 to return false upon finding an existing global entry. */
4758
4759 static bool
4760 add_global_entry (const char *name, int sub)
4761 {
4762 gfc_gsymbol *s;
4763 enum gfc_symbol_type type;
4764
4765 s = gfc_get_gsymbol(name);
4766 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4767
4768 if (s->defined
4769 || (s->type != GSYM_UNKNOWN
4770 && s->type != type))
4771 gfc_global_used(s, NULL);
4772 else
4773 {
4774 s->type = type;
4775 s->where = gfc_current_locus;
4776 s->defined = 1;
4777 s->ns = gfc_current_ns;
4778 return true;
4779 }
4780 return false;
4781 }
4782
4783
4784 /* Match an ENTRY statement. */
4785
4786 match
4787 gfc_match_entry (void)
4788 {
4789 gfc_symbol *proc;
4790 gfc_symbol *result;
4791 gfc_symbol *entry;
4792 char name[GFC_MAX_SYMBOL_LEN + 1];
4793 gfc_compile_state state;
4794 match m;
4795 gfc_entry_list *el;
4796 locus old_loc;
4797 bool module_procedure;
4798 char peek_char;
4799 match is_bind_c;
4800
4801 m = gfc_match_name (name);
4802 if (m != MATCH_YES)
4803 return m;
4804
4805 state = gfc_current_state ();
4806 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4807 {
4808 switch (state)
4809 {
4810 case COMP_PROGRAM:
4811 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4812 break;
4813 case COMP_MODULE:
4814 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4815 break;
4816 case COMP_BLOCK_DATA:
4817 gfc_error ("ENTRY statement at %C cannot appear within "
4818 "a BLOCK DATA");
4819 break;
4820 case COMP_INTERFACE:
4821 gfc_error ("ENTRY statement at %C cannot appear within "
4822 "an INTERFACE");
4823 break;
4824 case COMP_DERIVED:
4825 gfc_error ("ENTRY statement at %C cannot appear within "
4826 "a DERIVED TYPE block");
4827 break;
4828 case COMP_IF:
4829 gfc_error ("ENTRY statement at %C cannot appear within "
4830 "an IF-THEN block");
4831 break;
4832 case COMP_DO:
4833 gfc_error ("ENTRY statement at %C cannot appear within "
4834 "a DO block");
4835 break;
4836 case COMP_SELECT:
4837 gfc_error ("ENTRY statement at %C cannot appear within "
4838 "a SELECT block");
4839 break;
4840 case COMP_FORALL:
4841 gfc_error ("ENTRY statement at %C cannot appear within "
4842 "a FORALL block");
4843 break;
4844 case COMP_WHERE:
4845 gfc_error ("ENTRY statement at %C cannot appear within "
4846 "a WHERE block");
4847 break;
4848 case COMP_CONTAINS:
4849 gfc_error ("ENTRY statement at %C cannot appear within "
4850 "a contained subprogram");
4851 break;
4852 default:
4853 gfc_internal_error ("gfc_match_entry(): Bad state");
4854 }
4855 return MATCH_ERROR;
4856 }
4857
4858 module_procedure = gfc_current_ns->parent != NULL
4859 && gfc_current_ns->parent->proc_name
4860 && gfc_current_ns->parent->proc_name->attr.flavor
4861 == FL_MODULE;
4862
4863 if (gfc_current_ns->parent != NULL
4864 && gfc_current_ns->parent->proc_name
4865 && !module_procedure)
4866 {
4867 gfc_error("ENTRY statement at %C cannot appear in a "
4868 "contained procedure");
4869 return MATCH_ERROR;
4870 }
4871
4872 /* Module function entries need special care in get_proc_name
4873 because previous references within the function will have
4874 created symbols attached to the current namespace. */
4875 if (get_proc_name (name, &entry,
4876 gfc_current_ns->parent != NULL
4877 && module_procedure))
4878 return MATCH_ERROR;
4879
4880 proc = gfc_current_block ();
4881
4882 /* Make sure that it isn't already declared as BIND(C). If it is, it
4883 must have been marked BIND(C) with a BIND(C) attribute and that is
4884 not allowed for procedures. */
4885 if (entry->attr.is_bind_c == 1)
4886 {
4887 entry->attr.is_bind_c = 0;
4888 if (entry->old_symbol != NULL)
4889 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4890 "variables or common blocks",
4891 &(entry->old_symbol->declared_at));
4892 else
4893 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4894 "variables or common blocks", &gfc_current_locus);
4895 }
4896
4897 /* Check what next non-whitespace character is so we can tell if there
4898 is the required parens if we have a BIND(C). */
4899 gfc_gobble_whitespace ();
4900 peek_char = gfc_peek_ascii_char ();
4901
4902 if (state == COMP_SUBROUTINE)
4903 {
4904 /* An entry in a subroutine. */
4905 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4906 return MATCH_ERROR;
4907
4908 m = gfc_match_formal_arglist (entry, 0, 1);
4909 if (m != MATCH_YES)
4910 return MATCH_ERROR;
4911
4912 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4913 never be an internal procedure. */
4914 is_bind_c = gfc_match_bind_c (entry, true);
4915 if (is_bind_c == MATCH_ERROR)
4916 return MATCH_ERROR;
4917 if (is_bind_c == MATCH_YES)
4918 {
4919 if (peek_char != '(')
4920 {
4921 gfc_error ("Missing required parentheses before BIND(C) at %C");
4922 return MATCH_ERROR;
4923 }
4924 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4925 == FAILURE)
4926 return MATCH_ERROR;
4927 }
4928
4929 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4930 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4931 return MATCH_ERROR;
4932 }
4933 else
4934 {
4935 /* An entry in a function.
4936 We need to take special care because writing
4937 ENTRY f()
4938 as
4939 ENTRY f
4940 is allowed, whereas
4941 ENTRY f() RESULT (r)
4942 can't be written as
4943 ENTRY f RESULT (r). */
4944 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4945 return MATCH_ERROR;
4946
4947 old_loc = gfc_current_locus;
4948 if (gfc_match_eos () == MATCH_YES)
4949 {
4950 gfc_current_locus = old_loc;
4951 /* Match the empty argument list, and add the interface to
4952 the symbol. */
4953 m = gfc_match_formal_arglist (entry, 0, 1);
4954 }
4955 else
4956 m = gfc_match_formal_arglist (entry, 0, 0);
4957
4958 if (m != MATCH_YES)
4959 return MATCH_ERROR;
4960
4961 result = NULL;
4962
4963 if (gfc_match_eos () == MATCH_YES)
4964 {
4965 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4966 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4967 return MATCH_ERROR;
4968
4969 entry->result = entry;
4970 }
4971 else
4972 {
4973 m = gfc_match_suffix (entry, &result);
4974 if (m == MATCH_NO)
4975 gfc_syntax_error (ST_ENTRY);
4976 if (m != MATCH_YES)
4977 return MATCH_ERROR;
4978
4979 if (result)
4980 {
4981 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4982 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4983 || gfc_add_function (&entry->attr, result->name, NULL)
4984 == FAILURE)
4985 return MATCH_ERROR;
4986 entry->result = result;
4987 }
4988 else
4989 {
4990 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4991 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4992 return MATCH_ERROR;
4993 entry->result = entry;
4994 }
4995 }
4996 }
4997
4998 if (gfc_match_eos () != MATCH_YES)
4999 {
5000 gfc_syntax_error (ST_ENTRY);
5001 return MATCH_ERROR;
5002 }
5003
5004 entry->attr.recursive = proc->attr.recursive;
5005 entry->attr.elemental = proc->attr.elemental;
5006 entry->attr.pure = proc->attr.pure;
5007
5008 el = gfc_get_entry_list ();
5009 el->sym = entry;
5010 el->next = gfc_current_ns->entries;
5011 gfc_current_ns->entries = el;
5012 if (el->next)
5013 el->id = el->next->id + 1;
5014 else
5015 el->id = 1;
5016
5017 new_st.op = EXEC_ENTRY;
5018 new_st.ext.entry = el;
5019
5020 return MATCH_YES;
5021 }
5022
5023
5024 /* Match a subroutine statement, including optional prefixes. */
5025
5026 match
5027 gfc_match_subroutine (void)
5028 {
5029 char name[GFC_MAX_SYMBOL_LEN + 1];
5030 gfc_symbol *sym;
5031 match m;
5032 match is_bind_c;
5033 char peek_char;
5034 bool allow_binding_name;
5035
5036 if (gfc_current_state () != COMP_NONE
5037 && gfc_current_state () != COMP_INTERFACE
5038 && gfc_current_state () != COMP_CONTAINS)
5039 return MATCH_NO;
5040
5041 m = gfc_match_prefix (NULL);
5042 if (m != MATCH_YES)
5043 return m;
5044
5045 m = gfc_match ("subroutine% %n", name);
5046 if (m != MATCH_YES)
5047 return m;
5048
5049 if (get_proc_name (name, &sym, false))
5050 return MATCH_ERROR;
5051
5052 if (add_hidden_procptr_result (sym) == SUCCESS)
5053 sym = sym->result;
5054
5055 gfc_new_block = sym;
5056
5057 /* Check what next non-whitespace character is so we can tell if there
5058 is the required parens if we have a BIND(C). */
5059 gfc_gobble_whitespace ();
5060 peek_char = gfc_peek_ascii_char ();
5061
5062 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5063 return MATCH_ERROR;
5064
5065 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5066 return MATCH_ERROR;
5067
5068 /* Make sure that it isn't already declared as BIND(C). If it is, it
5069 must have been marked BIND(C) with a BIND(C) attribute and that is
5070 not allowed for procedures. */
5071 if (sym->attr.is_bind_c == 1)
5072 {
5073 sym->attr.is_bind_c = 0;
5074 if (sym->old_symbol != NULL)
5075 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5076 "variables or common blocks",
5077 &(sym->old_symbol->declared_at));
5078 else
5079 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5080 "variables or common blocks", &gfc_current_locus);
5081 }
5082
5083 /* C binding names are not allowed for internal procedures. */
5084 if (gfc_current_state () == COMP_CONTAINS
5085 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5086 allow_binding_name = false;
5087 else
5088 allow_binding_name = true;
5089
5090 /* Here, we are just checking if it has the bind(c) attribute, and if
5091 so, then we need to make sure it's all correct. If it doesn't,
5092 we still need to continue matching the rest of the subroutine line. */
5093 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5094 if (is_bind_c == MATCH_ERROR)
5095 {
5096 /* There was an attempt at the bind(c), but it was wrong. An
5097 error message should have been printed w/in the gfc_match_bind_c
5098 so here we'll just return the MATCH_ERROR. */
5099 return MATCH_ERROR;
5100 }
5101
5102 if (is_bind_c == MATCH_YES)
5103 {
5104 /* The following is allowed in the Fortran 2008 draft. */
5105 if (gfc_current_state () == COMP_CONTAINS
5106 && sym->ns->proc_name->attr.flavor != FL_MODULE
5107 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5108 "at %L may not be specified for an internal "
5109 "procedure", &gfc_current_locus)
5110 == FAILURE)
5111 return MATCH_ERROR;
5112
5113 if (peek_char != '(')
5114 {
5115 gfc_error ("Missing required parentheses before BIND(C) at %C");
5116 return MATCH_ERROR;
5117 }
5118 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5119 == FAILURE)
5120 return MATCH_ERROR;
5121 }
5122
5123 if (gfc_match_eos () != MATCH_YES)
5124 {
5125 gfc_syntax_error (ST_SUBROUTINE);
5126 return MATCH_ERROR;
5127 }
5128
5129 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5130 return MATCH_ERROR;
5131
5132 /* Warn if it has the same name as an intrinsic. */
5133 warn_intrinsic_shadow (sym, false);
5134
5135 return MATCH_YES;
5136 }
5137
5138
5139 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5140 given, and set the binding label in either the given symbol (if not
5141 NULL), or in the current_ts. The symbol may be NULL because we may
5142 encounter the BIND(C) before the declaration itself. Return
5143 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5144 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5145 or MATCH_YES if the specifier was correct and the binding label and
5146 bind(c) fields were set correctly for the given symbol or the
5147 current_ts. If allow_binding_name is false, no binding name may be
5148 given. */
5149
5150 match
5151 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5152 {
5153 /* binding label, if exists */
5154 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5155 match double_quote;
5156 match single_quote;
5157
5158 /* Initialize the flag that specifies whether we encountered a NAME=
5159 specifier or not. */
5160 has_name_equals = 0;
5161
5162 /* Init the first char to nil so we can catch if we don't have
5163 the label (name attr) or the symbol name yet. */
5164 binding_label[0] = '\0';
5165
5166 /* This much we have to be able to match, in this order, if
5167 there is a bind(c) label. */
5168 if (gfc_match (" bind ( c ") != MATCH_YES)
5169 return MATCH_NO;
5170
5171 /* Now see if there is a binding label, or if we've reached the
5172 end of the bind(c) attribute without one. */
5173 if (gfc_match_char (',') == MATCH_YES)
5174 {
5175 if (gfc_match (" name = ") != MATCH_YES)
5176 {
5177 gfc_error ("Syntax error in NAME= specifier for binding label "
5178 "at %C");
5179 /* should give an error message here */
5180 return MATCH_ERROR;
5181 }
5182
5183 has_name_equals = 1;
5184
5185 /* Get the opening quote. */
5186 double_quote = MATCH_YES;
5187 single_quote = MATCH_YES;
5188 double_quote = gfc_match_char ('"');
5189 if (double_quote != MATCH_YES)
5190 single_quote = gfc_match_char ('\'');
5191 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5192 {
5193 gfc_error ("Syntax error in NAME= specifier for binding label "
5194 "at %C");
5195 return MATCH_ERROR;
5196 }
5197
5198 /* Grab the binding label, using functions that will not lower
5199 case the names automatically. */
5200 if (gfc_match_name_C (binding_label) != MATCH_YES)
5201 return MATCH_ERROR;
5202
5203 /* Get the closing quotation. */
5204 if (double_quote == MATCH_YES)
5205 {
5206 if (gfc_match_char ('"') != MATCH_YES)
5207 {
5208 gfc_error ("Missing closing quote '\"' for binding label at %C");
5209 /* User started string with '"' so looked to match it. */
5210 return MATCH_ERROR;
5211 }
5212 }
5213 else
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 "'" char. */
5219 return MATCH_ERROR;
5220 }
5221 }
5222 }
5223
5224 /* Get the required right paren. */
5225 if (gfc_match_char (')') != MATCH_YES)
5226 {
5227 gfc_error ("Missing closing paren for binding label at %C");
5228 return MATCH_ERROR;
5229 }
5230
5231 if (has_name_equals && !allow_binding_name)
5232 {
5233 gfc_error ("No binding name is allowed in BIND(C) at %C");
5234 return MATCH_ERROR;
5235 }
5236
5237 if (has_name_equals && sym != NULL && sym->attr.dummy)
5238 {
5239 gfc_error ("For dummy procedure %s, no binding name is "
5240 "allowed in BIND(C) at %C", sym->name);
5241 return MATCH_ERROR;
5242 }
5243
5244
5245 /* Save the binding label to the symbol. If sym is null, we're
5246 probably matching the typespec attributes of a declaration and
5247 haven't gotten the name yet, and therefore, no symbol yet. */
5248 if (binding_label[0] != '\0')
5249 {
5250 if (sym != NULL)
5251 {
5252 strcpy (sym->binding_label, binding_label);
5253 }
5254 else
5255 strcpy (curr_binding_label, binding_label);
5256 }
5257 else if (allow_binding_name)
5258 {
5259 /* No binding label, but if symbol isn't null, we
5260 can set the label for it here.
5261 If name="" or allow_binding_name is false, no C binding name is
5262 created. */
5263 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5264 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5265 }
5266
5267 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5268 && current_interface.type == INTERFACE_ABSTRACT)
5269 {
5270 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5271 return MATCH_ERROR;
5272 }
5273
5274 return MATCH_YES;
5275 }
5276
5277
5278 /* Return nonzero if we're currently compiling a contained procedure. */
5279
5280 static int
5281 contained_procedure (void)
5282 {
5283 gfc_state_data *s = gfc_state_stack;
5284
5285 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5286 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5287 return 1;
5288
5289 return 0;
5290 }
5291
5292 /* Set the kind of each enumerator. The kind is selected such that it is
5293 interoperable with the corresponding C enumeration type, making
5294 sure that -fshort-enums is honored. */
5295
5296 static void
5297 set_enum_kind(void)
5298 {
5299 enumerator_history *current_history = NULL;
5300 int kind;
5301 int i;
5302
5303 if (max_enum == NULL || enum_history == NULL)
5304 return;
5305
5306 if (!flag_short_enums)
5307 return;
5308
5309 i = 0;
5310 do
5311 {
5312 kind = gfc_integer_kinds[i++].kind;
5313 }
5314 while (kind < gfc_c_int_kind
5315 && gfc_check_integer_range (max_enum->initializer->value.integer,
5316 kind) != ARITH_OK);
5317
5318 current_history = enum_history;
5319 while (current_history != NULL)
5320 {
5321 current_history->sym->ts.kind = kind;
5322 current_history = current_history->next;
5323 }
5324 }
5325
5326
5327 /* Match any of the various end-block statements. Returns the type of
5328 END to the caller. The END INTERFACE, END IF, END DO and END
5329 SELECT statements cannot be replaced by a single END statement. */
5330
5331 match
5332 gfc_match_end (gfc_statement *st)
5333 {
5334 char name[GFC_MAX_SYMBOL_LEN + 1];
5335 gfc_compile_state state;
5336 locus old_loc;
5337 const char *block_name;
5338 const char *target;
5339 int eos_ok;
5340 match m;
5341
5342 old_loc = gfc_current_locus;
5343 if (gfc_match ("end") != MATCH_YES)
5344 return MATCH_NO;
5345
5346 state = gfc_current_state ();
5347 block_name = gfc_current_block () == NULL
5348 ? NULL : gfc_current_block ()->name;
5349
5350 if (state == COMP_CONTAINS || state == COMP_DERIVED_CONTAINS)
5351 {
5352 state = gfc_state_stack->previous->state;
5353 block_name = gfc_state_stack->previous->sym == NULL
5354 ? NULL : gfc_state_stack->previous->sym->name;
5355 }
5356
5357 switch (state)
5358 {
5359 case COMP_NONE:
5360 case COMP_PROGRAM:
5361 *st = ST_END_PROGRAM;
5362 target = " program";
5363 eos_ok = 1;
5364 break;
5365
5366 case COMP_SUBROUTINE:
5367 *st = ST_END_SUBROUTINE;
5368 target = " subroutine";
5369 eos_ok = !contained_procedure ();
5370 break;
5371
5372 case COMP_FUNCTION:
5373 *st = ST_END_FUNCTION;
5374 target = " function";
5375 eos_ok = !contained_procedure ();
5376 break;
5377
5378 case COMP_BLOCK_DATA:
5379 *st = ST_END_BLOCK_DATA;
5380 target = " block data";
5381 eos_ok = 1;
5382 break;
5383
5384 case COMP_MODULE:
5385 *st = ST_END_MODULE;
5386 target = " module";
5387 eos_ok = 1;
5388 break;
5389
5390 case COMP_INTERFACE:
5391 *st = ST_END_INTERFACE;
5392 target = " interface";
5393 eos_ok = 0;
5394 break;
5395
5396 case COMP_DERIVED:
5397 case COMP_DERIVED_CONTAINS:
5398 *st = ST_END_TYPE;
5399 target = " type";
5400 eos_ok = 0;
5401 break;
5402
5403 case COMP_IF:
5404 *st = ST_ENDIF;
5405 target = " if";
5406 eos_ok = 0;
5407 break;
5408
5409 case COMP_DO:
5410 *st = ST_ENDDO;
5411 target = " do";
5412 eos_ok = 0;
5413 break;
5414
5415 case COMP_SELECT:
5416 *st = ST_END_SELECT;
5417 target = " select";
5418 eos_ok = 0;
5419 break;
5420
5421 case COMP_FORALL:
5422 *st = ST_END_FORALL;
5423 target = " forall";
5424 eos_ok = 0;
5425 break;
5426
5427 case COMP_WHERE:
5428 *st = ST_END_WHERE;
5429 target = " where";
5430 eos_ok = 0;
5431 break;
5432
5433 case COMP_ENUM:
5434 *st = ST_END_ENUM;
5435 target = " enum";
5436 eos_ok = 0;
5437 last_initializer = NULL;
5438 set_enum_kind ();
5439 gfc_free_enum_history ();
5440 break;
5441
5442 default:
5443 gfc_error ("Unexpected END statement at %C");
5444 goto cleanup;
5445 }
5446
5447 if (gfc_match_eos () == MATCH_YES)
5448 {
5449 if (!eos_ok)
5450 {
5451 /* We would have required END [something]. */
5452 gfc_error ("%s statement expected at %L",
5453 gfc_ascii_statement (*st), &old_loc);
5454 goto cleanup;
5455 }
5456
5457 return MATCH_YES;
5458 }
5459
5460 /* Verify that we've got the sort of end-block that we're expecting. */
5461 if (gfc_match (target) != MATCH_YES)
5462 {
5463 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5464 goto cleanup;
5465 }
5466
5467 /* If we're at the end, make sure a block name wasn't required. */
5468 if (gfc_match_eos () == MATCH_YES)
5469 {
5470
5471 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5472 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5473 return MATCH_YES;
5474
5475 if (gfc_current_block () == NULL)
5476 return MATCH_YES;
5477
5478 gfc_error ("Expected block name of '%s' in %s statement at %C",
5479 block_name, gfc_ascii_statement (*st));
5480
5481 return MATCH_ERROR;
5482 }
5483
5484 /* END INTERFACE has a special handler for its several possible endings. */
5485 if (*st == ST_END_INTERFACE)
5486 return gfc_match_end_interface ();
5487
5488 /* We haven't hit the end of statement, so what is left must be an
5489 end-name. */
5490 m = gfc_match_space ();
5491 if (m == MATCH_YES)
5492 m = gfc_match_name (name);
5493
5494 if (m == MATCH_NO)
5495 gfc_error ("Expected terminating name at %C");
5496 if (m != MATCH_YES)
5497 goto cleanup;
5498
5499 if (block_name == NULL)
5500 goto syntax;
5501
5502 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5503 {
5504 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5505 gfc_ascii_statement (*st));
5506 goto cleanup;
5507 }
5508 /* Procedure pointer as function result. */
5509 else if (strcmp (block_name, "ppr@") == 0
5510 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5511 {
5512 gfc_error ("Expected label '%s' for %s statement at %C",
5513 gfc_current_block ()->ns->proc_name->name,
5514 gfc_ascii_statement (*st));
5515 goto cleanup;
5516 }
5517
5518 if (gfc_match_eos () == MATCH_YES)
5519 return MATCH_YES;
5520
5521 syntax:
5522 gfc_syntax_error (*st);
5523
5524 cleanup:
5525 gfc_current_locus = old_loc;
5526 return MATCH_ERROR;
5527 }
5528
5529
5530
5531 /***************** Attribute declaration statements ****************/
5532
5533 /* Set the attribute of a single variable. */
5534
5535 static match
5536 attr_decl1 (void)
5537 {
5538 char name[GFC_MAX_SYMBOL_LEN + 1];
5539 gfc_array_spec *as;
5540 gfc_symbol *sym;
5541 locus var_locus;
5542 match m;
5543
5544 as = NULL;
5545
5546 m = gfc_match_name (name);
5547 if (m != MATCH_YES)
5548 goto cleanup;
5549
5550 if (find_special (name, &sym, false))
5551 return MATCH_ERROR;
5552
5553 var_locus = gfc_current_locus;
5554
5555 /* Deal with possible array specification for certain attributes. */
5556 if (current_attr.dimension
5557 || current_attr.allocatable
5558 || current_attr.pointer
5559 || current_attr.target)
5560 {
5561 m = gfc_match_array_spec (&as);
5562 if (m == MATCH_ERROR)
5563 goto cleanup;
5564
5565 if (current_attr.dimension && m == MATCH_NO)
5566 {
5567 gfc_error ("Missing array specification at %L in DIMENSION "
5568 "statement", &var_locus);
5569 m = MATCH_ERROR;
5570 goto cleanup;
5571 }
5572
5573 if (current_attr.dimension && sym->value)
5574 {
5575 gfc_error ("Dimensions specified for %s at %L after its "
5576 "initialisation", sym->name, &var_locus);
5577 m = MATCH_ERROR;
5578 goto cleanup;
5579 }
5580
5581 if ((current_attr.allocatable || current_attr.pointer)
5582 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5583 {
5584 gfc_error ("Array specification must be deferred at %L", &var_locus);
5585 m = MATCH_ERROR;
5586 goto cleanup;
5587 }
5588 }
5589
5590 /* Update symbol table. DIMENSION attribute is set
5591 in gfc_set_array_spec(). */
5592 if (current_attr.dimension == 0
5593 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
5594 {
5595 m = MATCH_ERROR;
5596 goto cleanup;
5597 }
5598
5599 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5600 {
5601 m = MATCH_ERROR;
5602 goto cleanup;
5603 }
5604
5605 if (sym->attr.cray_pointee && sym->as != NULL)
5606 {
5607 /* Fix the array spec. */
5608 m = gfc_mod_pointee_as (sym->as);
5609 if (m == MATCH_ERROR)
5610 goto cleanup;
5611 }
5612
5613 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5614 {
5615 m = MATCH_ERROR;
5616 goto cleanup;
5617 }
5618
5619 if ((current_attr.external || current_attr.intrinsic)
5620 && sym->attr.flavor != FL_PROCEDURE
5621 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5622 {
5623 m = MATCH_ERROR;
5624 goto cleanup;
5625 }
5626
5627 add_hidden_procptr_result (sym);
5628
5629 return MATCH_YES;
5630
5631 cleanup:
5632 gfc_free_array_spec (as);
5633 return m;
5634 }
5635
5636
5637 /* Generic attribute declaration subroutine. Used for attributes that
5638 just have a list of names. */
5639
5640 static match
5641 attr_decl (void)
5642 {
5643 match m;
5644
5645 /* Gobble the optional double colon, by simply ignoring the result
5646 of gfc_match(). */
5647 gfc_match (" ::");
5648
5649 for (;;)
5650 {
5651 m = attr_decl1 ();
5652 if (m != MATCH_YES)
5653 break;
5654
5655 if (gfc_match_eos () == MATCH_YES)
5656 {
5657 m = MATCH_YES;
5658 break;
5659 }
5660
5661 if (gfc_match_char (',') != MATCH_YES)
5662 {
5663 gfc_error ("Unexpected character in variable list at %C");
5664 m = MATCH_ERROR;
5665 break;
5666 }
5667 }
5668
5669 return m;
5670 }
5671
5672
5673 /* This routine matches Cray Pointer declarations of the form:
5674 pointer ( <pointer>, <pointee> )
5675 or
5676 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5677 The pointer, if already declared, should be an integer. Otherwise, we
5678 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5679 be either a scalar, or an array declaration. No space is allocated for
5680 the pointee. For the statement
5681 pointer (ipt, ar(10))
5682 any subsequent uses of ar will be translated (in C-notation) as
5683 ar(i) => ((<type> *) ipt)(i)
5684 After gimplification, pointee variable will disappear in the code. */
5685
5686 static match
5687 cray_pointer_decl (void)
5688 {
5689 match m;
5690 gfc_array_spec *as;
5691 gfc_symbol *cptr; /* Pointer symbol. */
5692 gfc_symbol *cpte; /* Pointee symbol. */
5693 locus var_locus;
5694 bool done = false;
5695
5696 while (!done)
5697 {
5698 if (gfc_match_char ('(') != MATCH_YES)
5699 {
5700 gfc_error ("Expected '(' at %C");
5701 return MATCH_ERROR;
5702 }
5703
5704 /* Match pointer. */
5705 var_locus = gfc_current_locus;
5706 gfc_clear_attr (&current_attr);
5707 gfc_add_cray_pointer (&current_attr, &var_locus);
5708 current_ts.type = BT_INTEGER;
5709 current_ts.kind = gfc_index_integer_kind;
5710
5711 m = gfc_match_symbol (&cptr, 0);
5712 if (m != MATCH_YES)
5713 {
5714 gfc_error ("Expected variable name at %C");
5715 return m;
5716 }
5717
5718 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5719 return MATCH_ERROR;
5720
5721 gfc_set_sym_referenced (cptr);
5722
5723 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5724 {
5725 cptr->ts.type = BT_INTEGER;
5726 cptr->ts.kind = gfc_index_integer_kind;
5727 }
5728 else if (cptr->ts.type != BT_INTEGER)
5729 {
5730 gfc_error ("Cray pointer at %C must be an integer");
5731 return MATCH_ERROR;
5732 }
5733 else if (cptr->ts.kind < gfc_index_integer_kind)
5734 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5735 " memory addresses require %d bytes",
5736 cptr->ts.kind, gfc_index_integer_kind);
5737
5738 if (gfc_match_char (',') != MATCH_YES)
5739 {
5740 gfc_error ("Expected \",\" at %C");
5741 return MATCH_ERROR;
5742 }
5743
5744 /* Match Pointee. */
5745 var_locus = gfc_current_locus;
5746 gfc_clear_attr (&current_attr);
5747 gfc_add_cray_pointee (&current_attr, &var_locus);
5748 current_ts.type = BT_UNKNOWN;
5749 current_ts.kind = 0;
5750
5751 m = gfc_match_symbol (&cpte, 0);
5752 if (m != MATCH_YES)
5753 {
5754 gfc_error ("Expected variable name at %C");
5755 return m;
5756 }
5757
5758 /* Check for an optional array spec. */
5759 m = gfc_match_array_spec (&as);
5760 if (m == MATCH_ERROR)
5761 {
5762 gfc_free_array_spec (as);
5763 return m;
5764 }
5765 else if (m == MATCH_NO)
5766 {
5767 gfc_free_array_spec (as);
5768 as = NULL;
5769 }
5770
5771 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5772 return MATCH_ERROR;
5773
5774 gfc_set_sym_referenced (cpte);
5775
5776 if (cpte->as == NULL)
5777 {
5778 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5779 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5780 }
5781 else if (as != NULL)
5782 {
5783 gfc_error ("Duplicate array spec for Cray pointee at %C");
5784 gfc_free_array_spec (as);
5785 return MATCH_ERROR;
5786 }
5787
5788 as = NULL;
5789
5790 if (cpte->as != NULL)
5791 {
5792 /* Fix array spec. */
5793 m = gfc_mod_pointee_as (cpte->as);
5794 if (m == MATCH_ERROR)
5795 return m;
5796 }
5797
5798 /* Point the Pointee at the Pointer. */
5799 cpte->cp_pointer = cptr;
5800
5801 if (gfc_match_char (')') != MATCH_YES)
5802 {
5803 gfc_error ("Expected \")\" at %C");
5804 return MATCH_ERROR;
5805 }
5806 m = gfc_match_char (',');
5807 if (m != MATCH_YES)
5808 done = true; /* Stop searching for more declarations. */
5809
5810 }
5811
5812 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5813 || gfc_match_eos () != MATCH_YES)
5814 {
5815 gfc_error ("Expected \",\" or end of statement at %C");
5816 return MATCH_ERROR;
5817 }
5818 return MATCH_YES;
5819 }
5820
5821
5822 match
5823 gfc_match_external (void)
5824 {
5825
5826 gfc_clear_attr (&current_attr);
5827 current_attr.external = 1;
5828
5829 return attr_decl ();
5830 }
5831
5832
5833 match
5834 gfc_match_intent (void)
5835 {
5836 sym_intent intent;
5837
5838 intent = match_intent_spec ();
5839 if (intent == INTENT_UNKNOWN)
5840 return MATCH_ERROR;
5841
5842 gfc_clear_attr (&current_attr);
5843 current_attr.intent = intent;
5844
5845 return attr_decl ();
5846 }
5847
5848
5849 match
5850 gfc_match_intrinsic (void)
5851 {
5852
5853 gfc_clear_attr (&current_attr);
5854 current_attr.intrinsic = 1;
5855
5856 return attr_decl ();
5857 }
5858
5859
5860 match
5861 gfc_match_optional (void)
5862 {
5863
5864 gfc_clear_attr (&current_attr);
5865 current_attr.optional = 1;
5866
5867 return attr_decl ();
5868 }
5869
5870
5871 match
5872 gfc_match_pointer (void)
5873 {
5874 gfc_gobble_whitespace ();
5875 if (gfc_peek_ascii_char () == '(')
5876 {
5877 if (!gfc_option.flag_cray_pointer)
5878 {
5879 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5880 "flag");
5881 return MATCH_ERROR;
5882 }
5883 return cray_pointer_decl ();
5884 }
5885 else
5886 {
5887 gfc_clear_attr (&current_attr);
5888 current_attr.pointer = 1;
5889
5890 return attr_decl ();
5891 }
5892 }
5893
5894
5895 match
5896 gfc_match_allocatable (void)
5897 {
5898 gfc_clear_attr (&current_attr);
5899 current_attr.allocatable = 1;
5900
5901 return attr_decl ();
5902 }
5903
5904
5905 match
5906 gfc_match_dimension (void)
5907 {
5908 gfc_clear_attr (&current_attr);
5909 current_attr.dimension = 1;
5910
5911 return attr_decl ();
5912 }
5913
5914
5915 match
5916 gfc_match_target (void)
5917 {
5918 gfc_clear_attr (&current_attr);
5919 current_attr.target = 1;
5920
5921 return attr_decl ();
5922 }
5923
5924
5925 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5926 statement. */
5927
5928 static match
5929 access_attr_decl (gfc_statement st)
5930 {
5931 char name[GFC_MAX_SYMBOL_LEN + 1];
5932 interface_type type;
5933 gfc_user_op *uop;
5934 gfc_symbol *sym;
5935 gfc_intrinsic_op op;
5936 match m;
5937
5938 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5939 goto done;
5940
5941 for (;;)
5942 {
5943 m = gfc_match_generic_spec (&type, name, &op);
5944 if (m == MATCH_NO)
5945 goto syntax;
5946 if (m == MATCH_ERROR)
5947 return MATCH_ERROR;
5948
5949 switch (type)
5950 {
5951 case INTERFACE_NAMELESS:
5952 case INTERFACE_ABSTRACT:
5953 goto syntax;
5954
5955 case INTERFACE_GENERIC:
5956 if (gfc_get_symbol (name, NULL, &sym))
5957 goto done;
5958
5959 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5960 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5961 sym->name, NULL) == FAILURE)
5962 return MATCH_ERROR;
5963
5964 break;
5965
5966 case INTERFACE_INTRINSIC_OP:
5967 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
5968 {
5969 gfc_current_ns->operator_access[op] =
5970 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5971 }
5972 else
5973 {
5974 gfc_error ("Access specification of the %s operator at %C has "
5975 "already been specified", gfc_op2string (op));
5976 goto done;
5977 }
5978
5979 break;
5980
5981 case INTERFACE_USER_OP:
5982 uop = gfc_get_uop (name);
5983
5984 if (uop->access == ACCESS_UNKNOWN)
5985 {
5986 uop->access = (st == ST_PUBLIC)
5987 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5988 }
5989 else
5990 {
5991 gfc_error ("Access specification of the .%s. operator at %C "
5992 "has already been specified", sym->name);
5993 goto done;
5994 }
5995
5996 break;
5997 }
5998
5999 if (gfc_match_char (',') == MATCH_NO)
6000 break;
6001 }
6002
6003 if (gfc_match_eos () != MATCH_YES)
6004 goto syntax;
6005 return MATCH_YES;
6006
6007 syntax:
6008 gfc_syntax_error (st);
6009
6010 done:
6011 return MATCH_ERROR;
6012 }
6013
6014
6015 match
6016 gfc_match_protected (void)
6017 {
6018 gfc_symbol *sym;
6019 match m;
6020
6021 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6022 {
6023 gfc_error ("PROTECTED at %C only allowed in specification "
6024 "part of a module");
6025 return MATCH_ERROR;
6026
6027 }
6028
6029 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6030 == FAILURE)
6031 return MATCH_ERROR;
6032
6033 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6034 {
6035 return MATCH_ERROR;
6036 }
6037
6038 if (gfc_match_eos () == MATCH_YES)
6039 goto syntax;
6040
6041 for(;;)
6042 {
6043 m = gfc_match_symbol (&sym, 0);
6044 switch (m)
6045 {
6046 case MATCH_YES:
6047 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6048 == FAILURE)
6049 return MATCH_ERROR;
6050 goto next_item;
6051
6052 case MATCH_NO:
6053 break;
6054
6055 case MATCH_ERROR:
6056 return MATCH_ERROR;
6057 }
6058
6059 next_item:
6060 if (gfc_match_eos () == MATCH_YES)
6061 break;
6062 if (gfc_match_char (',') != MATCH_YES)
6063 goto syntax;
6064 }
6065
6066 return MATCH_YES;
6067
6068 syntax:
6069 gfc_error ("Syntax error in PROTECTED statement at %C");
6070 return MATCH_ERROR;
6071 }
6072
6073
6074 /* The PRIVATE statement is a bit weird in that it can be an attribute
6075 declaration, but also works as a standalone statement inside of a
6076 type declaration or a module. */
6077
6078 match
6079 gfc_match_private (gfc_statement *st)
6080 {
6081
6082 if (gfc_match ("private") != MATCH_YES)
6083 return MATCH_NO;
6084
6085 if (gfc_current_state () != COMP_MODULE
6086 && !(gfc_current_state () == COMP_DERIVED
6087 && gfc_state_stack->previous
6088 && gfc_state_stack->previous->state == COMP_MODULE)
6089 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6090 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6091 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6092 {
6093 gfc_error ("PRIVATE statement at %C is only allowed in the "
6094 "specification part of a module");
6095 return MATCH_ERROR;
6096 }
6097
6098 if (gfc_current_state () == COMP_DERIVED)
6099 {
6100 if (gfc_match_eos () == MATCH_YES)
6101 {
6102 *st = ST_PRIVATE;
6103 return MATCH_YES;
6104 }
6105
6106 gfc_syntax_error (ST_PRIVATE);
6107 return MATCH_ERROR;
6108 }
6109
6110 if (gfc_match_eos () == MATCH_YES)
6111 {
6112 *st = ST_PRIVATE;
6113 return MATCH_YES;
6114 }
6115
6116 *st = ST_ATTR_DECL;
6117 return access_attr_decl (ST_PRIVATE);
6118 }
6119
6120
6121 match
6122 gfc_match_public (gfc_statement *st)
6123 {
6124
6125 if (gfc_match ("public") != MATCH_YES)
6126 return MATCH_NO;
6127
6128 if (gfc_current_state () != COMP_MODULE)
6129 {
6130 gfc_error ("PUBLIC statement at %C is only allowed in the "
6131 "specification part of a module");
6132 return MATCH_ERROR;
6133 }
6134
6135 if (gfc_match_eos () == MATCH_YES)
6136 {
6137 *st = ST_PUBLIC;
6138 return MATCH_YES;
6139 }
6140
6141 *st = ST_ATTR_DECL;
6142 return access_attr_decl (ST_PUBLIC);
6143 }
6144
6145
6146 /* Workhorse for gfc_match_parameter. */
6147
6148 static match
6149 do_parm (void)
6150 {
6151 gfc_symbol *sym;
6152 gfc_expr *init;
6153 match m;
6154
6155 m = gfc_match_symbol (&sym, 0);
6156 if (m == MATCH_NO)
6157 gfc_error ("Expected variable name at %C in PARAMETER statement");
6158
6159 if (m != MATCH_YES)
6160 return m;
6161
6162 if (gfc_match_char ('=') == MATCH_NO)
6163 {
6164 gfc_error ("Expected = sign in PARAMETER statement at %C");
6165 return MATCH_ERROR;
6166 }
6167
6168 m = gfc_match_init_expr (&init);
6169 if (m == MATCH_NO)
6170 gfc_error ("Expected expression at %C in PARAMETER statement");
6171 if (m != MATCH_YES)
6172 return m;
6173
6174 if (sym->ts.type == BT_UNKNOWN
6175 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6176 {
6177 m = MATCH_ERROR;
6178 goto cleanup;
6179 }
6180
6181 if (gfc_check_assign_symbol (sym, init) == FAILURE
6182 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6183 {
6184 m = MATCH_ERROR;
6185 goto cleanup;
6186 }
6187
6188 if (sym->value)
6189 {
6190 gfc_error ("Initializing already initialized variable at %C");
6191 m = MATCH_ERROR;
6192 goto cleanup;
6193 }
6194
6195 if (sym->ts.type == BT_CHARACTER
6196 && sym->ts.cl != NULL
6197 && sym->ts.cl->length != NULL
6198 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
6199 && init->expr_type == EXPR_CONSTANT
6200 && init->ts.type == BT_CHARACTER)
6201 gfc_set_constant_character_len (
6202 mpz_get_si (sym->ts.cl->length->value.integer), init, -1);
6203 else if (sym->ts.type == BT_CHARACTER && sym->ts.cl != NULL
6204 && sym->ts.cl->length == NULL)
6205 {
6206 int clen;
6207 if (init->expr_type == EXPR_CONSTANT)
6208 {
6209 clen = init->value.character.length;
6210 sym->ts.cl->length = gfc_int_expr (clen);
6211 }
6212 else if (init->expr_type == EXPR_ARRAY)
6213 {
6214 gfc_expr *p = init->value.constructor->expr;
6215 clen = p->value.character.length;
6216 sym->ts.cl->length = gfc_int_expr (clen);
6217 }
6218 else if (init->ts.cl && init->ts.cl->length)
6219 sym->ts.cl->length = gfc_copy_expr (sym->value->ts.cl->length);
6220 }
6221
6222 sym->value = init;
6223 return MATCH_YES;
6224
6225 cleanup:
6226 gfc_free_expr (init);
6227 return m;
6228 }
6229
6230
6231 /* Match a parameter statement, with the weird syntax that these have. */
6232
6233 match
6234 gfc_match_parameter (void)
6235 {
6236 match m;
6237
6238 if (gfc_match_char ('(') == MATCH_NO)
6239 return MATCH_NO;
6240
6241 for (;;)
6242 {
6243 m = do_parm ();
6244 if (m != MATCH_YES)
6245 break;
6246
6247 if (gfc_match (" )%t") == MATCH_YES)
6248 break;
6249
6250 if (gfc_match_char (',') != MATCH_YES)
6251 {
6252 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6253 m = MATCH_ERROR;
6254 break;
6255 }
6256 }
6257
6258 return m;
6259 }
6260
6261
6262 /* Save statements have a special syntax. */
6263
6264 match
6265 gfc_match_save (void)
6266 {
6267 char n[GFC_MAX_SYMBOL_LEN+1];
6268 gfc_common_head *c;
6269 gfc_symbol *sym;
6270 match m;
6271
6272 if (gfc_match_eos () == MATCH_YES)
6273 {
6274 if (gfc_current_ns->seen_save)
6275 {
6276 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6277 "follows previous SAVE statement")
6278 == FAILURE)
6279 return MATCH_ERROR;
6280 }
6281
6282 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6283 return MATCH_YES;
6284 }
6285
6286 if (gfc_current_ns->save_all)
6287 {
6288 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6289 "blanket SAVE statement")
6290 == FAILURE)
6291 return MATCH_ERROR;
6292 }
6293
6294 gfc_match (" ::");
6295
6296 for (;;)
6297 {
6298 m = gfc_match_symbol (&sym, 0);
6299 switch (m)
6300 {
6301 case MATCH_YES:
6302 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6303 == FAILURE)
6304 return MATCH_ERROR;
6305 goto next_item;
6306
6307 case MATCH_NO:
6308 break;
6309
6310 case MATCH_ERROR:
6311 return MATCH_ERROR;
6312 }
6313
6314 m = gfc_match (" / %n /", &n);
6315 if (m == MATCH_ERROR)
6316 return MATCH_ERROR;
6317 if (m == MATCH_NO)
6318 goto syntax;
6319
6320 c = gfc_get_common (n, 0);
6321 c->saved = 1;
6322
6323 gfc_current_ns->seen_save = 1;
6324
6325 next_item:
6326 if (gfc_match_eos () == MATCH_YES)
6327 break;
6328 if (gfc_match_char (',') != MATCH_YES)
6329 goto syntax;
6330 }
6331
6332 return MATCH_YES;
6333
6334 syntax:
6335 gfc_error ("Syntax error in SAVE statement at %C");
6336 return MATCH_ERROR;
6337 }
6338
6339
6340 match
6341 gfc_match_value (void)
6342 {
6343 gfc_symbol *sym;
6344 match m;
6345
6346 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6347 == FAILURE)
6348 return MATCH_ERROR;
6349
6350 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6351 {
6352 return MATCH_ERROR;
6353 }
6354
6355 if (gfc_match_eos () == MATCH_YES)
6356 goto syntax;
6357
6358 for(;;)
6359 {
6360 m = gfc_match_symbol (&sym, 0);
6361 switch (m)
6362 {
6363 case MATCH_YES:
6364 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6365 == FAILURE)
6366 return MATCH_ERROR;
6367 goto next_item;
6368
6369 case MATCH_NO:
6370 break;
6371
6372 case MATCH_ERROR:
6373 return MATCH_ERROR;
6374 }
6375
6376 next_item:
6377 if (gfc_match_eos () == MATCH_YES)
6378 break;
6379 if (gfc_match_char (',') != MATCH_YES)
6380 goto syntax;
6381 }
6382
6383 return MATCH_YES;
6384
6385 syntax:
6386 gfc_error ("Syntax error in VALUE statement at %C");
6387 return MATCH_ERROR;
6388 }
6389
6390
6391 match
6392 gfc_match_volatile (void)
6393 {
6394 gfc_symbol *sym;
6395 match m;
6396
6397 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6398 == FAILURE)
6399 return MATCH_ERROR;
6400
6401 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6402 {
6403 return MATCH_ERROR;
6404 }
6405
6406 if (gfc_match_eos () == MATCH_YES)
6407 goto syntax;
6408
6409 for(;;)
6410 {
6411 /* VOLATILE is special because it can be added to host-associated
6412 symbols locally. */
6413 m = gfc_match_symbol (&sym, 1);
6414 switch (m)
6415 {
6416 case MATCH_YES:
6417 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6418 == FAILURE)
6419 return MATCH_ERROR;
6420 goto next_item;
6421
6422 case MATCH_NO:
6423 break;
6424
6425 case MATCH_ERROR:
6426 return MATCH_ERROR;
6427 }
6428
6429 next_item:
6430 if (gfc_match_eos () == MATCH_YES)
6431 break;
6432 if (gfc_match_char (',') != MATCH_YES)
6433 goto syntax;
6434 }
6435
6436 return MATCH_YES;
6437
6438 syntax:
6439 gfc_error ("Syntax error in VOLATILE statement at %C");
6440 return MATCH_ERROR;
6441 }
6442
6443
6444 /* Match a module procedure statement. Note that we have to modify
6445 symbols in the parent's namespace because the current one was there
6446 to receive symbols that are in an interface's formal argument list. */
6447
6448 match
6449 gfc_match_modproc (void)
6450 {
6451 char name[GFC_MAX_SYMBOL_LEN + 1];
6452 gfc_symbol *sym;
6453 match m;
6454 gfc_namespace *module_ns;
6455 gfc_interface *old_interface_head, *interface;
6456
6457 if (gfc_state_stack->state != COMP_INTERFACE
6458 || gfc_state_stack->previous == NULL
6459 || current_interface.type == INTERFACE_NAMELESS
6460 || current_interface.type == INTERFACE_ABSTRACT)
6461 {
6462 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6463 "interface");
6464 return MATCH_ERROR;
6465 }
6466
6467 module_ns = gfc_current_ns->parent;
6468 for (; module_ns; module_ns = module_ns->parent)
6469 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6470 break;
6471
6472 if (module_ns == NULL)
6473 return MATCH_ERROR;
6474
6475 /* Store the current state of the interface. We will need it if we
6476 end up with a syntax error and need to recover. */
6477 old_interface_head = gfc_current_interface_head ();
6478
6479 for (;;)
6480 {
6481 bool last = false;
6482
6483 m = gfc_match_name (name);
6484 if (m == MATCH_NO)
6485 goto syntax;
6486 if (m != MATCH_YES)
6487 return MATCH_ERROR;
6488
6489 /* Check for syntax error before starting to add symbols to the
6490 current namespace. */
6491 if (gfc_match_eos () == MATCH_YES)
6492 last = true;
6493 if (!last && gfc_match_char (',') != MATCH_YES)
6494 goto syntax;
6495
6496 /* Now we're sure the syntax is valid, we process this item
6497 further. */
6498 if (gfc_get_symbol (name, module_ns, &sym))
6499 return MATCH_ERROR;
6500
6501 if (sym->attr.proc != PROC_MODULE
6502 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6503 sym->name, NULL) == FAILURE)
6504 return MATCH_ERROR;
6505
6506 if (gfc_add_interface (sym) == FAILURE)
6507 return MATCH_ERROR;
6508
6509 sym->attr.mod_proc = 1;
6510
6511 if (last)
6512 break;
6513 }
6514
6515 return MATCH_YES;
6516
6517 syntax:
6518 /* Restore the previous state of the interface. */
6519 interface = gfc_current_interface_head ();
6520 gfc_set_current_interface_head (old_interface_head);
6521
6522 /* Free the new interfaces. */
6523 while (interface != old_interface_head)
6524 {
6525 gfc_interface *i = interface->next;
6526 gfc_free (interface);
6527 interface = i;
6528 }
6529
6530 /* And issue a syntax error. */
6531 gfc_syntax_error (ST_MODULE_PROC);
6532 return MATCH_ERROR;
6533 }
6534
6535
6536 /* Check a derived type that is being extended. */
6537 static gfc_symbol*
6538 check_extended_derived_type (char *name)
6539 {
6540 gfc_symbol *extended;
6541
6542 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6543 {
6544 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6545 return NULL;
6546 }
6547
6548 if (!extended)
6549 {
6550 gfc_error ("No such symbol in TYPE definition at %C");
6551 return NULL;
6552 }
6553
6554 if (extended->attr.flavor != FL_DERIVED)
6555 {
6556 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6557 "derived type", name);
6558 return NULL;
6559 }
6560
6561 if (extended->attr.is_bind_c)
6562 {
6563 gfc_error ("'%s' cannot be extended at %C because it "
6564 "is BIND(C)", extended->name);
6565 return NULL;
6566 }
6567
6568 if (extended->attr.sequence)
6569 {
6570 gfc_error ("'%s' cannot be extended at %C because it "
6571 "is a SEQUENCE type", extended->name);
6572 return NULL;
6573 }
6574
6575 return extended;
6576 }
6577
6578
6579 /* Match the optional attribute specifiers for a type declaration.
6580 Return MATCH_ERROR if an error is encountered in one of the handled
6581 attributes (public, private, bind(c)), MATCH_NO if what's found is
6582 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6583 checking on attribute conflicts needs to be done. */
6584
6585 match
6586 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6587 {
6588 /* See if the derived type is marked as private. */
6589 if (gfc_match (" , private") == MATCH_YES)
6590 {
6591 if (gfc_current_state () != COMP_MODULE)
6592 {
6593 gfc_error ("Derived type at %C can only be PRIVATE in the "
6594 "specification part of a module");
6595 return MATCH_ERROR;
6596 }
6597
6598 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6599 return MATCH_ERROR;
6600 }
6601 else if (gfc_match (" , public") == MATCH_YES)
6602 {
6603 if (gfc_current_state () != COMP_MODULE)
6604 {
6605 gfc_error ("Derived type at %C can only be PUBLIC in the "
6606 "specification part of a module");
6607 return MATCH_ERROR;
6608 }
6609
6610 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6611 return MATCH_ERROR;
6612 }
6613 else if (gfc_match (" , bind ( c )") == MATCH_YES)
6614 {
6615 /* If the type is defined to be bind(c) it then needs to make
6616 sure that all fields are interoperable. This will
6617 need to be a semantic check on the finished derived type.
6618 See 15.2.3 (lines 9-12) of F2003 draft. */
6619 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6620 return MATCH_ERROR;
6621
6622 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6623 }
6624 else if (gfc_match (" , abstract") == MATCH_YES)
6625 {
6626 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
6627 == FAILURE)
6628 return MATCH_ERROR;
6629
6630 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
6631 return MATCH_ERROR;
6632 }
6633 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6634 {
6635 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
6636 return MATCH_ERROR;
6637 }
6638 else
6639 return MATCH_NO;
6640
6641 /* If we get here, something matched. */
6642 return MATCH_YES;
6643 }
6644
6645
6646 /* Match the beginning of a derived type declaration. If a type name
6647 was the result of a function, then it is possible to have a symbol
6648 already to be known as a derived type yet have no components. */
6649
6650 match
6651 gfc_match_derived_decl (void)
6652 {
6653 char name[GFC_MAX_SYMBOL_LEN + 1];
6654 char parent[GFC_MAX_SYMBOL_LEN + 1];
6655 symbol_attribute attr;
6656 gfc_symbol *sym;
6657 gfc_symbol *extended;
6658 match m;
6659 match is_type_attr_spec = MATCH_NO;
6660 bool seen_attr = false;
6661
6662 if (gfc_current_state () == COMP_DERIVED)
6663 return MATCH_NO;
6664
6665 name[0] = '\0';
6666 parent[0] = '\0';
6667 gfc_clear_attr (&attr);
6668 extended = NULL;
6669
6670 do
6671 {
6672 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
6673 if (is_type_attr_spec == MATCH_ERROR)
6674 return MATCH_ERROR;
6675 if (is_type_attr_spec == MATCH_YES)
6676 seen_attr = true;
6677 } while (is_type_attr_spec == MATCH_YES);
6678
6679 /* Deal with derived type extensions. The extension attribute has
6680 been added to 'attr' but now the parent type must be found and
6681 checked. */
6682 if (parent[0])
6683 extended = check_extended_derived_type (parent);
6684
6685 if (parent[0] && !extended)
6686 return MATCH_ERROR;
6687
6688 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6689 {
6690 gfc_error ("Expected :: in TYPE definition at %C");
6691 return MATCH_ERROR;
6692 }
6693
6694 m = gfc_match (" %n%t", name);
6695 if (m != MATCH_YES)
6696 return m;
6697
6698 /* Make sure the name is not the name of an intrinsic type. */
6699 if (gfc_is_intrinsic_typename (name))
6700 {
6701 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6702 "type", name);
6703 return MATCH_ERROR;
6704 }
6705
6706 if (gfc_get_symbol (name, NULL, &sym))
6707 return MATCH_ERROR;
6708
6709 if (sym->ts.type != BT_UNKNOWN)
6710 {
6711 gfc_error ("Derived type name '%s' at %C already has a basic type "
6712 "of %s", sym->name, gfc_typename (&sym->ts));
6713 return MATCH_ERROR;
6714 }
6715
6716 /* The symbol may already have the derived attribute without the
6717 components. The ways this can happen is via a function
6718 definition, an INTRINSIC statement or a subtype in another
6719 derived type that is a pointer. The first part of the AND clause
6720 is true if the symbol is not the return value of a function. */
6721 if (sym->attr.flavor != FL_DERIVED
6722 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6723 return MATCH_ERROR;
6724
6725 if (sym->components != NULL || sym->attr.zero_comp)
6726 {
6727 gfc_error ("Derived type definition of '%s' at %C has already been "
6728 "defined", sym->name);
6729 return MATCH_ERROR;
6730 }
6731
6732 if (attr.access != ACCESS_UNKNOWN
6733 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6734 return MATCH_ERROR;
6735
6736 /* See if the derived type was labeled as bind(c). */
6737 if (attr.is_bind_c != 0)
6738 sym->attr.is_bind_c = attr.is_bind_c;
6739
6740 /* Construct the f2k_derived namespace if it is not yet there. */
6741 if (!sym->f2k_derived)
6742 sym->f2k_derived = gfc_get_namespace (NULL, 0);
6743
6744 if (extended && !sym->components)
6745 {
6746 gfc_component *p;
6747 gfc_symtree *st;
6748
6749 /* Add the extended derived type as the first component. */
6750 gfc_add_component (sym, parent, &p);
6751 sym->attr.extension = attr.extension;
6752 extended->refs++;
6753 gfc_set_sym_referenced (extended);
6754
6755 p->ts.type = BT_DERIVED;
6756 p->ts.derived = extended;
6757 p->initializer = gfc_default_initializer (&p->ts);
6758
6759 /* Provide the links between the extended type and its extension. */
6760 if (!extended->f2k_derived)
6761 extended->f2k_derived = gfc_get_namespace (NULL, 0);
6762 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6763 st->n.sym = sym;
6764 }
6765
6766 /* Take over the ABSTRACT attribute. */
6767 sym->attr.abstract = attr.abstract;
6768
6769 gfc_new_block = sym;
6770
6771 return MATCH_YES;
6772 }
6773
6774
6775 /* Cray Pointees can be declared as:
6776 pointer (ipt, a (n,m,...,*))
6777 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6778 cheat and set a constant bound of 1 for the last dimension, if this
6779 is the case. Since there is no bounds-checking for Cray Pointees,
6780 this will be okay. */
6781
6782 match
6783 gfc_mod_pointee_as (gfc_array_spec *as)
6784 {
6785 as->cray_pointee = true; /* This will be useful to know later. */
6786 if (as->type == AS_ASSUMED_SIZE)
6787 {
6788 as->type = AS_EXPLICIT;
6789 as->upper[as->rank - 1] = gfc_int_expr (1);
6790 as->cp_was_assumed = true;
6791 }
6792 else if (as->type == AS_ASSUMED_SHAPE)
6793 {
6794 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6795 return MATCH_ERROR;
6796 }
6797 return MATCH_YES;
6798 }
6799
6800
6801 /* Match the enum definition statement, here we are trying to match
6802 the first line of enum definition statement.
6803 Returns MATCH_YES if match is found. */
6804
6805 match
6806 gfc_match_enum (void)
6807 {
6808 match m;
6809
6810 m = gfc_match_eos ();
6811 if (m != MATCH_YES)
6812 return m;
6813
6814 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6815 == FAILURE)
6816 return MATCH_ERROR;
6817
6818 return MATCH_YES;
6819 }
6820
6821
6822 /* Returns an initializer whose value is one higher than the value of the
6823 LAST_INITIALIZER argument. If the argument is NULL, the
6824 initializers value will be set to zero. The initializer's kind
6825 will be set to gfc_c_int_kind.
6826
6827 If -fshort-enums is given, the appropriate kind will be selected
6828 later after all enumerators have been parsed. A warning is issued
6829 here if an initializer exceeds gfc_c_int_kind. */
6830
6831 static gfc_expr *
6832 enum_initializer (gfc_expr *last_initializer, locus where)
6833 {
6834 gfc_expr *result;
6835
6836 result = gfc_get_expr ();
6837 result->expr_type = EXPR_CONSTANT;
6838 result->ts.type = BT_INTEGER;
6839 result->ts.kind = gfc_c_int_kind;
6840 result->where = where;
6841
6842 mpz_init (result->value.integer);
6843
6844 if (last_initializer != NULL)
6845 {
6846 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
6847 result->where = last_initializer->where;
6848
6849 if (gfc_check_integer_range (result->value.integer,
6850 gfc_c_int_kind) != ARITH_OK)
6851 {
6852 gfc_error ("Enumerator exceeds the C integer type at %C");
6853 return NULL;
6854 }
6855 }
6856 else
6857 {
6858 /* Control comes here, if it's the very first enumerator and no
6859 initializer has been given. It will be initialized to zero. */
6860 mpz_set_si (result->value.integer, 0);
6861 }
6862
6863 return result;
6864 }
6865
6866
6867 /* Match a variable name with an optional initializer. When this
6868 subroutine is called, a variable is expected to be parsed next.
6869 Depending on what is happening at the moment, updates either the
6870 symbol table or the current interface. */
6871
6872 static match
6873 enumerator_decl (void)
6874 {
6875 char name[GFC_MAX_SYMBOL_LEN + 1];
6876 gfc_expr *initializer;
6877 gfc_array_spec *as = NULL;
6878 gfc_symbol *sym;
6879 locus var_locus;
6880 match m;
6881 gfc_try t;
6882 locus old_locus;
6883
6884 initializer = NULL;
6885 old_locus = gfc_current_locus;
6886
6887 /* When we get here, we've just matched a list of attributes and
6888 maybe a type and a double colon. The next thing we expect to see
6889 is the name of the symbol. */
6890 m = gfc_match_name (name);
6891 if (m != MATCH_YES)
6892 goto cleanup;
6893
6894 var_locus = gfc_current_locus;
6895
6896 /* OK, we've successfully matched the declaration. Now put the
6897 symbol in the current namespace. If we fail to create the symbol,
6898 bail out. */
6899 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6900 {
6901 m = MATCH_ERROR;
6902 goto cleanup;
6903 }
6904
6905 /* The double colon must be present in order to have initializers.
6906 Otherwise the statement is ambiguous with an assignment statement. */
6907 if (colon_seen)
6908 {
6909 if (gfc_match_char ('=') == MATCH_YES)
6910 {
6911 m = gfc_match_init_expr (&initializer);
6912 if (m == MATCH_NO)
6913 {
6914 gfc_error ("Expected an initialization expression at %C");
6915 m = MATCH_ERROR;
6916 }
6917
6918 if (m != MATCH_YES)
6919 goto cleanup;
6920 }
6921 }
6922
6923 /* If we do not have an initializer, the initialization value of the
6924 previous enumerator (stored in last_initializer) is incremented
6925 by 1 and is used to initialize the current enumerator. */
6926 if (initializer == NULL)
6927 initializer = enum_initializer (last_initializer, old_locus);
6928
6929 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6930 {
6931 gfc_error("ENUMERATOR %L not initialized with integer expression",
6932 &var_locus);
6933 m = MATCH_ERROR;
6934 gfc_free_enum_history ();
6935 goto cleanup;
6936 }
6937
6938 /* Store this current initializer, for the next enumerator variable
6939 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6940 use last_initializer below. */
6941 last_initializer = initializer;
6942 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6943
6944 /* Maintain enumerator history. */
6945 gfc_find_symbol (name, NULL, 0, &sym);
6946 create_enum_history (sym, last_initializer);
6947
6948 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6949
6950 cleanup:
6951 /* Free stuff up and return. */
6952 gfc_free_expr (initializer);
6953
6954 return m;
6955 }
6956
6957
6958 /* Match the enumerator definition statement. */
6959
6960 match
6961 gfc_match_enumerator_def (void)
6962 {
6963 match m;
6964 gfc_try t;
6965
6966 gfc_clear_ts (&current_ts);
6967
6968 m = gfc_match (" enumerator");
6969 if (m != MATCH_YES)
6970 return m;
6971
6972 m = gfc_match (" :: ");
6973 if (m == MATCH_ERROR)
6974 return m;
6975
6976 colon_seen = (m == MATCH_YES);
6977
6978 if (gfc_current_state () != COMP_ENUM)
6979 {
6980 gfc_error ("ENUM definition statement expected before %C");
6981 gfc_free_enum_history ();
6982 return MATCH_ERROR;
6983 }
6984
6985 (&current_ts)->type = BT_INTEGER;
6986 (&current_ts)->kind = gfc_c_int_kind;
6987
6988 gfc_clear_attr (&current_attr);
6989 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6990 if (t == FAILURE)
6991 {
6992 m = MATCH_ERROR;
6993 goto cleanup;
6994 }
6995
6996 for (;;)
6997 {
6998 m = enumerator_decl ();
6999 if (m == MATCH_ERROR)
7000 goto cleanup;
7001 if (m == MATCH_NO)
7002 break;
7003
7004 if (gfc_match_eos () == MATCH_YES)
7005 goto cleanup;
7006 if (gfc_match_char (',') != MATCH_YES)
7007 break;
7008 }
7009
7010 if (gfc_current_state () == COMP_ENUM)
7011 {
7012 gfc_free_enum_history ();
7013 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7014 m = MATCH_ERROR;
7015 }
7016
7017 cleanup:
7018 gfc_free_array_spec (current_as);
7019 current_as = NULL;
7020 return m;
7021
7022 }
7023
7024
7025 /* Match binding attributes. */
7026
7027 static match
7028 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7029 {
7030 bool found_passing = false;
7031 bool seen_ptr = false;
7032 match m = MATCH_YES;
7033
7034 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7035 this case the defaults are in there. */
7036 ba->access = ACCESS_UNKNOWN;
7037 ba->pass_arg = NULL;
7038 ba->pass_arg_num = 0;
7039 ba->nopass = 0;
7040 ba->non_overridable = 0;
7041 ba->deferred = 0;
7042 ba->ppc = ppc;
7043
7044 /* If we find a comma, we believe there are binding attributes. */
7045 m = gfc_match_char (',');
7046 if (m == MATCH_NO)
7047 goto done;
7048
7049 do
7050 {
7051 /* Access specifier. */
7052
7053 m = gfc_match (" public");
7054 if (m == MATCH_ERROR)
7055 goto error;
7056 if (m == MATCH_YES)
7057 {
7058 if (ba->access != ACCESS_UNKNOWN)
7059 {
7060 gfc_error ("Duplicate access-specifier at %C");
7061 goto error;
7062 }
7063
7064 ba->access = ACCESS_PUBLIC;
7065 continue;
7066 }
7067
7068 m = gfc_match (" private");
7069 if (m == MATCH_ERROR)
7070 goto error;
7071 if (m == MATCH_YES)
7072 {
7073 if (ba->access != ACCESS_UNKNOWN)
7074 {
7075 gfc_error ("Duplicate access-specifier at %C");
7076 goto error;
7077 }
7078
7079 ba->access = ACCESS_PRIVATE;
7080 continue;
7081 }
7082
7083 /* If inside GENERIC, the following is not allowed. */
7084 if (!generic)
7085 {
7086
7087 /* NOPASS flag. */
7088 m = gfc_match (" nopass");
7089 if (m == MATCH_ERROR)
7090 goto error;
7091 if (m == MATCH_YES)
7092 {
7093 if (found_passing)
7094 {
7095 gfc_error ("Binding attributes already specify passing,"
7096 " illegal NOPASS at %C");
7097 goto error;
7098 }
7099
7100 found_passing = true;
7101 ba->nopass = 1;
7102 continue;
7103 }
7104
7105 /* PASS possibly including argument. */
7106 m = gfc_match (" pass");
7107 if (m == MATCH_ERROR)
7108 goto error;
7109 if (m == MATCH_YES)
7110 {
7111 char arg[GFC_MAX_SYMBOL_LEN + 1];
7112
7113 if (found_passing)
7114 {
7115 gfc_error ("Binding attributes already specify passing,"
7116 " illegal PASS at %C");
7117 goto error;
7118 }
7119
7120 m = gfc_match (" ( %n )", arg);
7121 if (m == MATCH_ERROR)
7122 goto error;
7123 if (m == MATCH_YES)
7124 ba->pass_arg = gfc_get_string (arg);
7125 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7126
7127 found_passing = true;
7128 ba->nopass = 0;
7129 continue;
7130 }
7131
7132 if (ppc)
7133 {
7134 /* POINTER flag. */
7135 m = gfc_match (" pointer");
7136 if (m == MATCH_ERROR)
7137 goto error;
7138 if (m == MATCH_YES)
7139 {
7140 if (seen_ptr)
7141 {
7142 gfc_error ("Duplicate POINTER attribute at %C");
7143 goto error;
7144 }
7145
7146 seen_ptr = true;
7147 continue;
7148 }
7149 }
7150 else
7151 {
7152 /* NON_OVERRIDABLE flag. */
7153 m = gfc_match (" non_overridable");
7154 if (m == MATCH_ERROR)
7155 goto error;
7156 if (m == MATCH_YES)
7157 {
7158 if (ba->non_overridable)
7159 {
7160 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7161 goto error;
7162 }
7163
7164 ba->non_overridable = 1;
7165 continue;
7166 }
7167
7168 /* DEFERRED flag. */
7169 m = gfc_match (" deferred");
7170 if (m == MATCH_ERROR)
7171 goto error;
7172 if (m == MATCH_YES)
7173 {
7174 if (ba->deferred)
7175 {
7176 gfc_error ("Duplicate DEFERRED at %C");
7177 goto error;
7178 }
7179
7180 ba->deferred = 1;
7181 continue;
7182 }
7183 }
7184
7185 }
7186
7187 /* Nothing matching found. */
7188 if (generic)
7189 gfc_error ("Expected access-specifier at %C");
7190 else
7191 gfc_error ("Expected binding attribute at %C");
7192 goto error;
7193 }
7194 while (gfc_match_char (',') == MATCH_YES);
7195
7196 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7197 if (ba->non_overridable && ba->deferred)
7198 {
7199 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7200 goto error;
7201 }
7202
7203 m = MATCH_YES;
7204
7205 done:
7206 if (ba->access == ACCESS_UNKNOWN)
7207 ba->access = gfc_typebound_default_access;
7208
7209 if (ppc && !seen_ptr)
7210 {
7211 gfc_error ("POINTER attribute is required for procedure pointer component"
7212 " at %C");
7213 goto error;
7214 }
7215
7216 return m;
7217
7218 error:
7219 return MATCH_ERROR;
7220 }
7221
7222
7223 /* Match a PROCEDURE specific binding inside a derived type. */
7224
7225 static match
7226 match_procedure_in_type (void)
7227 {
7228 char name[GFC_MAX_SYMBOL_LEN + 1];
7229 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7230 char* target = NULL;
7231 gfc_typebound_proc* tb;
7232 bool seen_colons;
7233 bool seen_attrs;
7234 match m;
7235 gfc_symtree* stree;
7236 gfc_namespace* ns;
7237 gfc_symbol* block;
7238
7239 /* Check current state. */
7240 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7241 block = gfc_state_stack->previous->sym;
7242 gcc_assert (block);
7243
7244 /* Try to match PROCEDURE(interface). */
7245 if (gfc_match (" (") == MATCH_YES)
7246 {
7247 m = gfc_match_name (target_buf);
7248 if (m == MATCH_ERROR)
7249 return m;
7250 if (m != MATCH_YES)
7251 {
7252 gfc_error ("Interface-name expected after '(' at %C");
7253 return MATCH_ERROR;
7254 }
7255
7256 if (gfc_match (" )") != MATCH_YES)
7257 {
7258 gfc_error ("')' expected at %C");
7259 return MATCH_ERROR;
7260 }
7261
7262 target = target_buf;
7263 }
7264
7265 /* Construct the data structure. */
7266 tb = gfc_get_typebound_proc ();
7267 tb->where = gfc_current_locus;
7268 tb->is_generic = 0;
7269
7270 /* Match binding attributes. */
7271 m = match_binding_attributes (tb, false, false);
7272 if (m == MATCH_ERROR)
7273 return m;
7274 seen_attrs = (m == MATCH_YES);
7275
7276 /* Check that attribute DEFERRED is given iff an interface is specified, which
7277 means target != NULL. */
7278 if (tb->deferred && !target)
7279 {
7280 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7281 return MATCH_ERROR;
7282 }
7283 if (target && !tb->deferred)
7284 {
7285 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7286 return MATCH_ERROR;
7287 }
7288
7289 /* Match the colons. */
7290 m = gfc_match (" ::");
7291 if (m == MATCH_ERROR)
7292 return m;
7293 seen_colons = (m == MATCH_YES);
7294 if (seen_attrs && !seen_colons)
7295 {
7296 gfc_error ("Expected '::' after binding-attributes at %C");
7297 return MATCH_ERROR;
7298 }
7299
7300 /* Match the binding name. */
7301 m = gfc_match_name (name);
7302 if (m == MATCH_ERROR)
7303 return m;
7304 if (m == MATCH_NO)
7305 {
7306 gfc_error ("Expected binding name at %C");
7307 return MATCH_ERROR;
7308 }
7309
7310 /* Try to match the '=> target', if it's there. */
7311 m = gfc_match (" =>");
7312 if (m == MATCH_ERROR)
7313 return m;
7314 if (m == MATCH_YES)
7315 {
7316 if (tb->deferred)
7317 {
7318 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7319 return MATCH_ERROR;
7320 }
7321
7322 if (!seen_colons)
7323 {
7324 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7325 " at %C");
7326 return MATCH_ERROR;
7327 }
7328
7329 m = gfc_match_name (target_buf);
7330 if (m == MATCH_ERROR)
7331 return m;
7332 if (m == MATCH_NO)
7333 {
7334 gfc_error ("Expected binding target after '=>' at %C");
7335 return MATCH_ERROR;
7336 }
7337 target = target_buf;
7338 }
7339
7340 /* Now we should have the end. */
7341 m = gfc_match_eos ();
7342 if (m == MATCH_ERROR)
7343 return m;
7344 if (m == MATCH_NO)
7345 {
7346 gfc_error ("Junk after PROCEDURE declaration at %C");
7347 return MATCH_ERROR;
7348 }
7349
7350 /* If no target was found, it has the same name as the binding. */
7351 if (!target)
7352 target = name;
7353
7354 /* Get the namespace to insert the symbols into. */
7355 ns = block->f2k_derived;
7356 gcc_assert (ns);
7357
7358 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7359 if (tb->deferred && !block->attr.abstract)
7360 {
7361 gfc_error ("Type '%s' containing DEFERRED binding at %C is not ABSTRACT",
7362 block->name);
7363 return MATCH_ERROR;
7364 }
7365
7366 /* See if we already have a binding with this name in the symtree which would
7367 be an error. If a GENERIC already targetted this binding, it may be
7368 already there but then typebound is still NULL. */
7369 stree = gfc_find_symtree (ns->tb_sym_root, name);
7370 if (stree && stree->n.tb)
7371 {
7372 gfc_error ("There's already a procedure with binding name '%s' for the"
7373 " derived type '%s' at %C", name, block->name);
7374 return MATCH_ERROR;
7375 }
7376
7377 /* Insert it and set attributes. */
7378
7379 if (!stree)
7380 {
7381 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7382 gcc_assert (stree);
7383 }
7384 stree->n.tb = tb;
7385
7386 if (gfc_get_sym_tree (target, gfc_current_ns, &tb->u.specific, false))
7387 return MATCH_ERROR;
7388 gfc_set_sym_referenced (tb->u.specific->n.sym);
7389
7390 return MATCH_YES;
7391 }
7392
7393
7394 /* Match a GENERIC procedure binding inside a derived type. */
7395
7396 match
7397 gfc_match_generic (void)
7398 {
7399 char name[GFC_MAX_SYMBOL_LEN + 1];
7400 gfc_symbol* block;
7401 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7402 gfc_typebound_proc* tb;
7403 gfc_symtree* st;
7404 gfc_namespace* ns;
7405 match m;
7406
7407 /* Check current state. */
7408 if (gfc_current_state () == COMP_DERIVED)
7409 {
7410 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7411 return MATCH_ERROR;
7412 }
7413 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7414 return MATCH_NO;
7415 block = gfc_state_stack->previous->sym;
7416 ns = block->f2k_derived;
7417 gcc_assert (block && ns);
7418
7419 /* See if we get an access-specifier. */
7420 m = match_binding_attributes (&tbattr, true, false);
7421 if (m == MATCH_ERROR)
7422 goto error;
7423
7424 /* Now the colons, those are required. */
7425 if (gfc_match (" ::") != MATCH_YES)
7426 {
7427 gfc_error ("Expected '::' at %C");
7428 goto error;
7429 }
7430
7431 /* The binding name and =>. */
7432 m = gfc_match (" %n =>", name);
7433 if (m == MATCH_ERROR)
7434 return MATCH_ERROR;
7435 if (m == MATCH_NO)
7436 {
7437 gfc_error ("Expected generic name at %C");
7438 goto error;
7439 }
7440
7441 /* If there's already something with this name, check that it is another
7442 GENERIC and then extend that rather than build a new node. */
7443 st = gfc_find_symtree (ns->tb_sym_root, name);
7444 if (st)
7445 {
7446 gcc_assert (st->n.tb);
7447 tb = st->n.tb;
7448
7449 if (!tb->is_generic)
7450 {
7451 gfc_error ("There's already a non-generic procedure with binding name"
7452 " '%s' for the derived type '%s' at %C",
7453 name, block->name);
7454 goto error;
7455 }
7456
7457 if (tb->access != tbattr.access)
7458 {
7459 gfc_error ("Binding at %C must have the same access as already"
7460 " defined binding '%s'", name);
7461 goto error;
7462 }
7463 }
7464 else
7465 {
7466 st = gfc_new_symtree (&ns->tb_sym_root, name);
7467 gcc_assert (st);
7468
7469 st->n.tb = tb = gfc_get_typebound_proc ();
7470 tb->where = gfc_current_locus;
7471 tb->access = tbattr.access;
7472 tb->is_generic = 1;
7473 tb->u.generic = NULL;
7474 }
7475
7476 /* Now, match all following names as specific targets. */
7477 do
7478 {
7479 gfc_symtree* target_st;
7480 gfc_tbp_generic* target;
7481
7482 m = gfc_match_name (name);
7483 if (m == MATCH_ERROR)
7484 goto error;
7485 if (m == MATCH_NO)
7486 {
7487 gfc_error ("Expected specific binding name at %C");
7488 goto error;
7489 }
7490
7491 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
7492
7493 /* See if this is a duplicate specification. */
7494 for (target = tb->u.generic; target; target = target->next)
7495 if (target_st == target->specific_st)
7496 {
7497 gfc_error ("'%s' already defined as specific binding for the"
7498 " generic '%s' at %C", name, st->name);
7499 goto error;
7500 }
7501
7502 target = gfc_get_tbp_generic ();
7503 target->specific_st = target_st;
7504 target->specific = NULL;
7505 target->next = tb->u.generic;
7506 tb->u.generic = target;
7507 }
7508 while (gfc_match (" ,") == MATCH_YES);
7509
7510 /* Here should be the end. */
7511 if (gfc_match_eos () != MATCH_YES)
7512 {
7513 gfc_error ("Junk after GENERIC binding at %C");
7514 goto error;
7515 }
7516
7517 return MATCH_YES;
7518
7519 error:
7520 return MATCH_ERROR;
7521 }
7522
7523
7524 /* Match a FINAL declaration inside a derived type. */
7525
7526 match
7527 gfc_match_final_decl (void)
7528 {
7529 char name[GFC_MAX_SYMBOL_LEN + 1];
7530 gfc_symbol* sym;
7531 match m;
7532 gfc_namespace* module_ns;
7533 bool first, last;
7534 gfc_symbol* block;
7535
7536 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
7537 {
7538 gfc_error ("FINAL declaration at %C must be inside a derived type "
7539 "CONTAINS section");
7540 return MATCH_ERROR;
7541 }
7542
7543 block = gfc_state_stack->previous->sym;
7544 gcc_assert (block);
7545
7546 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
7547 || gfc_state_stack->previous->previous->state != COMP_MODULE)
7548 {
7549 gfc_error ("Derived type declaration with FINAL at %C must be in the"
7550 " specification part of a MODULE");
7551 return MATCH_ERROR;
7552 }
7553
7554 module_ns = gfc_current_ns;
7555 gcc_assert (module_ns);
7556 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
7557
7558 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
7559 if (gfc_match (" ::") == MATCH_ERROR)
7560 return MATCH_ERROR;
7561
7562 /* Match the sequence of procedure names. */
7563 first = true;
7564 last = false;
7565 do
7566 {
7567 gfc_finalizer* f;
7568
7569 if (first && gfc_match_eos () == MATCH_YES)
7570 {
7571 gfc_error ("Empty FINAL at %C");
7572 return MATCH_ERROR;
7573 }
7574
7575 m = gfc_match_name (name);
7576 if (m == MATCH_NO)
7577 {
7578 gfc_error ("Expected module procedure name at %C");
7579 return MATCH_ERROR;
7580 }
7581 else if (m != MATCH_YES)
7582 return MATCH_ERROR;
7583
7584 if (gfc_match_eos () == MATCH_YES)
7585 last = true;
7586 if (!last && gfc_match_char (',') != MATCH_YES)
7587 {
7588 gfc_error ("Expected ',' at %C");
7589 return MATCH_ERROR;
7590 }
7591
7592 if (gfc_get_symbol (name, module_ns, &sym))
7593 {
7594 gfc_error ("Unknown procedure name \"%s\" at %C", name);
7595 return MATCH_ERROR;
7596 }
7597
7598 /* Mark the symbol as module procedure. */
7599 if (sym->attr.proc != PROC_MODULE
7600 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7601 sym->name, NULL) == FAILURE)
7602 return MATCH_ERROR;
7603
7604 /* Check if we already have this symbol in the list, this is an error. */
7605 for (f = block->f2k_derived->finalizers; f; f = f->next)
7606 if (f->proc_sym == sym)
7607 {
7608 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7609 name);
7610 return MATCH_ERROR;
7611 }
7612
7613 /* Add this symbol to the list of finalizers. */
7614 gcc_assert (block->f2k_derived);
7615 ++sym->refs;
7616 f = XCNEW (gfc_finalizer);
7617 f->proc_sym = sym;
7618 f->proc_tree = NULL;
7619 f->where = gfc_current_locus;
7620 f->next = block->f2k_derived->finalizers;
7621 block->f2k_derived->finalizers = f;
7622
7623 first = false;
7624 }
7625 while (!last);
7626
7627 return MATCH_YES;
7628 }
7629
7630
7631 const ext_attr_t ext_attr_list[] = {
7632 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
7633 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
7634 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
7635 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
7636 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
7637 { NULL, EXT_ATTR_LAST, NULL }
7638 };
7639
7640 /* Match a !GCC$ ATTRIBUTES statement of the form:
7641 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
7642 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
7643
7644 TODO: We should support all GCC attributes using the same syntax for
7645 the attribute list, i.e. the list in C
7646 __attributes(( attribute-list ))
7647 matches then
7648 !GCC$ ATTRIBUTES attribute-list ::
7649 Cf. c-parser.c's c_parser_attributes; the data can then directly be
7650 saved into a TREE.
7651
7652 As there is absolutely no risk of confusion, we should never return
7653 MATCH_NO. */
7654 match
7655 gfc_match_gcc_attributes (void)
7656 {
7657 symbol_attribute attr;
7658 char name[GFC_MAX_SYMBOL_LEN + 1];
7659 unsigned id;
7660 gfc_symbol *sym;
7661 match m;
7662
7663 gfc_clear_attr (&attr);
7664 for(;;)
7665 {
7666 char ch;
7667
7668 if (gfc_match_name (name) != MATCH_YES)
7669 return MATCH_ERROR;
7670
7671 for (id = 0; id < EXT_ATTR_LAST; id++)
7672 if (strcmp (name, ext_attr_list[id].name) == 0)
7673 break;
7674
7675 if (id == EXT_ATTR_LAST)
7676 {
7677 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
7678 return MATCH_ERROR;
7679 }
7680
7681 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
7682 == FAILURE)
7683 return MATCH_ERROR;
7684
7685 gfc_gobble_whitespace ();
7686 ch = gfc_next_ascii_char ();
7687 if (ch == ':')
7688 {
7689 /* This is the successful exit condition for the loop. */
7690 if (gfc_next_ascii_char () == ':')
7691 break;
7692 }
7693
7694 if (ch == ',')
7695 continue;
7696
7697 goto syntax;
7698 }
7699
7700 if (gfc_match_eos () == MATCH_YES)
7701 goto syntax;
7702
7703 for(;;)
7704 {
7705 m = gfc_match_name (name);
7706 if (m != MATCH_YES)
7707 return m;
7708
7709 if (find_special (name, &sym, true))
7710 return MATCH_ERROR;
7711
7712 sym->attr.ext_attr |= attr.ext_attr;
7713
7714 if (gfc_match_eos () == MATCH_YES)
7715 break;
7716
7717 if (gfc_match_char (',') != MATCH_YES)
7718 goto syntax;
7719 }
7720
7721 return MATCH_YES;
7722
7723 syntax:
7724 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
7725 return MATCH_ERROR;
7726 }