2015-07-17 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / decl.c
blobebc88eaa5dd86fbdf5ca06c93e456092759fb01c
1 /* Declaration statement matcher
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "options.h"
28 #include "constructor.h"
29 #include "alias.h"
30 #include "tree.h"
31 #include "stringpool.h"
33 /* Macros to access allocate memory for gfc_data_variable,
34 gfc_data_value and gfc_data. */
35 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
36 #define gfc_get_data_value() XCNEW (gfc_data_value)
37 #define gfc_get_data() XCNEW (gfc_data)
40 static bool set_binding_label (const char **, const char *, int);
43 /* This flag is set if an old-style length selector is matched
44 during a type-declaration statement. */
46 static int old_char_selector;
48 /* When variables acquire types and attributes from a declaration
49 statement, they get them from the following static variables. The
50 first part of a declaration sets these variables and the second
51 part copies these into symbol structures. */
53 static gfc_typespec current_ts;
55 static symbol_attribute current_attr;
56 static gfc_array_spec *current_as;
57 static int colon_seen;
59 /* The current binding label (if any). */
60 static const char* curr_binding_label;
61 /* Need to know how many identifiers are on the current data declaration
62 line in case we're given the BIND(C) attribute with a NAME= specifier. */
63 static int num_idents_on_line;
64 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
65 can supply a name if the curr_binding_label is nil and NAME= was not. */
66 static int has_name_equals = 0;
68 /* Initializer of the previous enumerator. */
70 static gfc_expr *last_initializer;
72 /* History of all the enumerators is maintained, so that
73 kind values of all the enumerators could be updated depending
74 upon the maximum initialized value. */
76 typedef struct enumerator_history
78 gfc_symbol *sym;
79 gfc_expr *initializer;
80 struct enumerator_history *next;
82 enumerator_history;
84 /* Header of enum history chain. */
86 static enumerator_history *enum_history = NULL;
88 /* Pointer of enum history node containing largest initializer. */
90 static enumerator_history *max_enum = NULL;
92 /* gfc_new_block points to the symbol of a newly matched block. */
94 gfc_symbol *gfc_new_block;
96 bool gfc_matching_function;
99 /********************* DATA statement subroutines *********************/
101 static bool in_match_data = false;
103 bool
104 gfc_in_match_data (void)
106 return in_match_data;
109 static void
110 set_in_match_data (bool set_value)
112 in_match_data = set_value;
115 /* Free a gfc_data_variable structure and everything beneath it. */
117 static void
118 free_variable (gfc_data_variable *p)
120 gfc_data_variable *q;
122 for (; p; p = q)
124 q = p->next;
125 gfc_free_expr (p->expr);
126 gfc_free_iterator (&p->iter, 0);
127 free_variable (p->list);
128 free (p);
133 /* Free a gfc_data_value structure and everything beneath it. */
135 static void
136 free_value (gfc_data_value *p)
138 gfc_data_value *q;
140 for (; p; p = q)
142 q = p->next;
143 mpz_clear (p->repeat);
144 gfc_free_expr (p->expr);
145 free (p);
150 /* Free a list of gfc_data structures. */
152 void
153 gfc_free_data (gfc_data *p)
155 gfc_data *q;
157 for (; p; p = q)
159 q = p->next;
160 free_variable (p->var);
161 free_value (p->value);
162 free (p);
167 /* Free all data in a namespace. */
169 static void
170 gfc_free_data_all (gfc_namespace *ns)
172 gfc_data *d;
174 for (;ns->data;)
176 d = ns->data->next;
177 free (ns->data);
178 ns->data = d;
182 /* Reject data parsed since the last restore point was marked. */
184 void
185 gfc_reject_data (gfc_namespace *ns)
187 gfc_data *d;
189 while (ns->data && ns->data != ns->old_data)
191 d = ns->data->next;
192 free (ns->data);
193 ns->data = d;
197 static match var_element (gfc_data_variable *);
199 /* Match a list of variables terminated by an iterator and a right
200 parenthesis. */
202 static match
203 var_list (gfc_data_variable *parent)
205 gfc_data_variable *tail, var;
206 match m;
208 m = var_element (&var);
209 if (m == MATCH_ERROR)
210 return MATCH_ERROR;
211 if (m == MATCH_NO)
212 goto syntax;
214 tail = gfc_get_data_variable ();
215 *tail = var;
217 parent->list = tail;
219 for (;;)
221 if (gfc_match_char (',') != MATCH_YES)
222 goto syntax;
224 m = gfc_match_iterator (&parent->iter, 1);
225 if (m == MATCH_YES)
226 break;
227 if (m == MATCH_ERROR)
228 return MATCH_ERROR;
230 m = var_element (&var);
231 if (m == MATCH_ERROR)
232 return MATCH_ERROR;
233 if (m == MATCH_NO)
234 goto syntax;
236 tail->next = gfc_get_data_variable ();
237 tail = tail->next;
239 *tail = var;
242 if (gfc_match_char (')') != MATCH_YES)
243 goto syntax;
244 return MATCH_YES;
246 syntax:
247 gfc_syntax_error (ST_DATA);
248 return MATCH_ERROR;
252 /* Match a single element in a data variable list, which can be a
253 variable-iterator list. */
255 static match
256 var_element (gfc_data_variable *new_var)
258 match m;
259 gfc_symbol *sym;
261 memset (new_var, 0, sizeof (gfc_data_variable));
263 if (gfc_match_char ('(') == MATCH_YES)
264 return var_list (new_var);
266 m = gfc_match_variable (&new_var->expr, 0);
267 if (m != MATCH_YES)
268 return m;
270 sym = new_var->expr->symtree->n.sym;
272 /* Symbol should already have an associated type. */
273 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
274 return MATCH_ERROR;
276 if (!sym->attr.function && gfc_current_ns->parent
277 && gfc_current_ns->parent == sym->ns)
279 gfc_error ("Host associated variable %qs may not be in the DATA "
280 "statement at %C", sym->name);
281 return MATCH_ERROR;
284 if (gfc_current_state () != COMP_BLOCK_DATA
285 && sym->attr.in_common
286 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
287 "common block variable %qs in DATA statement at %C",
288 sym->name))
289 return MATCH_ERROR;
291 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
292 return MATCH_ERROR;
294 return MATCH_YES;
298 /* Match the top-level list of data variables. */
300 static match
301 top_var_list (gfc_data *d)
303 gfc_data_variable var, *tail, *new_var;
304 match m;
306 tail = NULL;
308 for (;;)
310 m = var_element (&var);
311 if (m == MATCH_NO)
312 goto syntax;
313 if (m == MATCH_ERROR)
314 return MATCH_ERROR;
316 new_var = gfc_get_data_variable ();
317 *new_var = var;
319 if (tail == NULL)
320 d->var = new_var;
321 else
322 tail->next = new_var;
324 tail = new_var;
326 if (gfc_match_char ('/') == MATCH_YES)
327 break;
328 if (gfc_match_char (',') != MATCH_YES)
329 goto syntax;
332 return MATCH_YES;
334 syntax:
335 gfc_syntax_error (ST_DATA);
336 gfc_free_data_all (gfc_current_ns);
337 return MATCH_ERROR;
341 static match
342 match_data_constant (gfc_expr **result)
344 char name[GFC_MAX_SYMBOL_LEN + 1];
345 gfc_symbol *sym, *dt_sym = NULL;
346 gfc_expr *expr;
347 match m;
348 locus old_loc;
350 m = gfc_match_literal_constant (&expr, 1);
351 if (m == MATCH_YES)
353 *result = expr;
354 return MATCH_YES;
357 if (m == MATCH_ERROR)
358 return MATCH_ERROR;
360 m = gfc_match_null (result);
361 if (m != MATCH_NO)
362 return m;
364 old_loc = gfc_current_locus;
366 /* Should this be a structure component, try to match it
367 before matching a name. */
368 m = gfc_match_rvalue (result);
369 if (m == MATCH_ERROR)
370 return m;
372 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
374 if (!gfc_simplify_expr (*result, 0))
375 m = MATCH_ERROR;
376 return m;
378 else if (m == MATCH_YES)
379 gfc_free_expr (*result);
381 gfc_current_locus = old_loc;
383 m = gfc_match_name (name);
384 if (m != MATCH_YES)
385 return m;
387 if (gfc_find_symbol (name, NULL, 1, &sym))
388 return MATCH_ERROR;
390 if (sym && sym->attr.generic)
391 dt_sym = gfc_find_dt_in_generic (sym);
393 if (sym == NULL
394 || (sym->attr.flavor != FL_PARAMETER
395 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
397 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
398 name);
399 return MATCH_ERROR;
401 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
402 return gfc_match_structure_constructor (dt_sym, result);
404 /* Check to see if the value is an initialization array expression. */
405 if (sym->value->expr_type == EXPR_ARRAY)
407 gfc_current_locus = old_loc;
409 m = gfc_match_init_expr (result);
410 if (m == MATCH_ERROR)
411 return m;
413 if (m == MATCH_YES)
415 if (!gfc_simplify_expr (*result, 0))
416 m = MATCH_ERROR;
418 if ((*result)->expr_type == EXPR_CONSTANT)
419 return m;
420 else
422 gfc_error ("Invalid initializer %s in Data statement at %C", name);
423 return MATCH_ERROR;
428 *result = gfc_copy_expr (sym->value);
429 return MATCH_YES;
433 /* Match a list of values in a DATA statement. The leading '/' has
434 already been seen at this point. */
436 static match
437 top_val_list (gfc_data *data)
439 gfc_data_value *new_val, *tail;
440 gfc_expr *expr;
441 match m;
443 tail = NULL;
445 for (;;)
447 m = match_data_constant (&expr);
448 if (m == MATCH_NO)
449 goto syntax;
450 if (m == MATCH_ERROR)
451 return MATCH_ERROR;
453 new_val = gfc_get_data_value ();
454 mpz_init (new_val->repeat);
456 if (tail == NULL)
457 data->value = new_val;
458 else
459 tail->next = new_val;
461 tail = new_val;
463 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
465 tail->expr = expr;
466 mpz_set_ui (tail->repeat, 1);
468 else
470 mpz_set (tail->repeat, expr->value.integer);
471 gfc_free_expr (expr);
473 m = match_data_constant (&tail->expr);
474 if (m == MATCH_NO)
475 goto syntax;
476 if (m == MATCH_ERROR)
477 return MATCH_ERROR;
480 if (gfc_match_char ('/') == MATCH_YES)
481 break;
482 if (gfc_match_char (',') == MATCH_NO)
483 goto syntax;
486 return MATCH_YES;
488 syntax:
489 gfc_syntax_error (ST_DATA);
490 gfc_free_data_all (gfc_current_ns);
491 return MATCH_ERROR;
495 /* Matches an old style initialization. */
497 static match
498 match_old_style_init (const char *name)
500 match m;
501 gfc_symtree *st;
502 gfc_symbol *sym;
503 gfc_data *newdata;
505 /* Set up data structure to hold initializers. */
506 gfc_find_sym_tree (name, NULL, 0, &st);
507 sym = st->n.sym;
509 newdata = gfc_get_data ();
510 newdata->var = gfc_get_data_variable ();
511 newdata->var->expr = gfc_get_variable_expr (st);
512 newdata->where = gfc_current_locus;
514 /* Match initial value list. This also eats the terminal '/'. */
515 m = top_val_list (newdata);
516 if (m != MATCH_YES)
518 free (newdata);
519 return m;
522 if (gfc_pure (NULL))
524 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
525 free (newdata);
526 return MATCH_ERROR;
528 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
530 /* Mark the variable as having appeared in a data statement. */
531 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
533 free (newdata);
534 return MATCH_ERROR;
537 /* Chain in namespace list of DATA initializers. */
538 newdata->next = gfc_current_ns->data;
539 gfc_current_ns->data = newdata;
541 return m;
545 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
546 we are matching a DATA statement and are therefore issuing an error
547 if we encounter something unexpected, if not, we're trying to match
548 an old-style initialization expression of the form INTEGER I /2/. */
550 match
551 gfc_match_data (void)
553 gfc_data *new_data;
554 match m;
556 set_in_match_data (true);
558 for (;;)
560 new_data = gfc_get_data ();
561 new_data->where = gfc_current_locus;
563 m = top_var_list (new_data);
564 if (m != MATCH_YES)
565 goto cleanup;
567 m = top_val_list (new_data);
568 if (m != MATCH_YES)
569 goto cleanup;
571 new_data->next = gfc_current_ns->data;
572 gfc_current_ns->data = new_data;
574 if (gfc_match_eos () == MATCH_YES)
575 break;
577 gfc_match_char (','); /* Optional comma */
580 set_in_match_data (false);
582 if (gfc_pure (NULL))
584 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
585 return MATCH_ERROR;
587 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
589 return MATCH_YES;
591 cleanup:
592 set_in_match_data (false);
593 gfc_free_data (new_data);
594 return MATCH_ERROR;
598 /************************ Declaration statements *********************/
601 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
603 static bool
604 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
606 int i;
608 if ((from->type == AS_ASSUMED_RANK && to->corank)
609 || (to->type == AS_ASSUMED_RANK && from->corank))
611 gfc_error ("The assumed-rank array at %C shall not have a codimension");
612 return false;
615 if (to->rank == 0 && from->rank > 0)
617 to->rank = from->rank;
618 to->type = from->type;
619 to->cray_pointee = from->cray_pointee;
620 to->cp_was_assumed = from->cp_was_assumed;
622 for (i = 0; i < to->corank; i++)
624 to->lower[from->rank + i] = to->lower[i];
625 to->upper[from->rank + i] = to->upper[i];
627 for (i = 0; i < from->rank; i++)
629 if (copy)
631 to->lower[i] = gfc_copy_expr (from->lower[i]);
632 to->upper[i] = gfc_copy_expr (from->upper[i]);
634 else
636 to->lower[i] = from->lower[i];
637 to->upper[i] = from->upper[i];
641 else if (to->corank == 0 && from->corank > 0)
643 to->corank = from->corank;
644 to->cotype = from->cotype;
646 for (i = 0; i < from->corank; i++)
648 if (copy)
650 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
651 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
653 else
655 to->lower[to->rank + i] = from->lower[i];
656 to->upper[to->rank + i] = from->upper[i];
661 return true;
665 /* Match an intent specification. Since this can only happen after an
666 INTENT word, a legal intent-spec must follow. */
668 static sym_intent
669 match_intent_spec (void)
672 if (gfc_match (" ( in out )") == MATCH_YES)
673 return INTENT_INOUT;
674 if (gfc_match (" ( in )") == MATCH_YES)
675 return INTENT_IN;
676 if (gfc_match (" ( out )") == MATCH_YES)
677 return INTENT_OUT;
679 gfc_error ("Bad INTENT specification at %C");
680 return INTENT_UNKNOWN;
684 /* Matches a character length specification, which is either a
685 specification expression, '*', or ':'. */
687 static match
688 char_len_param_value (gfc_expr **expr, bool *deferred)
690 match m;
692 *expr = NULL;
693 *deferred = false;
695 if (gfc_match_char ('*') == MATCH_YES)
696 return MATCH_YES;
698 if (gfc_match_char (':') == MATCH_YES)
700 if (!gfc_notify_std (GFC_STD_F2003, "deferred type "
701 "parameter at %C"))
702 return MATCH_ERROR;
704 *deferred = true;
706 return MATCH_YES;
709 m = gfc_match_expr (expr);
711 if (m == MATCH_YES
712 && !gfc_expr_check_typed (*expr, gfc_current_ns, false))
713 return MATCH_ERROR;
715 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
717 if ((*expr)->value.function.actual
718 && (*expr)->value.function.actual->expr->symtree)
720 gfc_expr *e;
721 e = (*expr)->value.function.actual->expr;
722 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
723 && e->expr_type == EXPR_VARIABLE)
725 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
726 goto syntax;
727 if (e->symtree->n.sym->ts.type == BT_CHARACTER
728 && e->symtree->n.sym->ts.u.cl
729 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
730 goto syntax;
734 return m;
736 syntax:
737 gfc_error ("Conflict in attributes of function argument at %C");
738 return MATCH_ERROR;
742 /* A character length is a '*' followed by a literal integer or a
743 char_len_param_value in parenthesis. */
745 static match
746 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
748 int length;
749 match m;
751 *deferred = false;
752 m = gfc_match_char ('*');
753 if (m != MATCH_YES)
754 return m;
756 m = gfc_match_small_literal_int (&length, NULL);
757 if (m == MATCH_ERROR)
758 return m;
760 if (m == MATCH_YES)
762 if (obsolescent_check
763 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
764 return MATCH_ERROR;
765 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
766 return m;
769 if (gfc_match_char ('(') == MATCH_NO)
770 goto syntax;
772 m = char_len_param_value (expr, deferred);
773 if (m != MATCH_YES && gfc_matching_function)
775 gfc_undo_symbols ();
776 m = MATCH_YES;
779 if (m == MATCH_ERROR)
780 return m;
781 if (m == MATCH_NO)
782 goto syntax;
784 if (gfc_match_char (')') == MATCH_NO)
786 gfc_free_expr (*expr);
787 *expr = NULL;
788 goto syntax;
791 return MATCH_YES;
793 syntax:
794 gfc_error ("Syntax error in character length specification at %C");
795 return MATCH_ERROR;
799 /* Special subroutine for finding a symbol. Check if the name is found
800 in the current name space. If not, and we're compiling a function or
801 subroutine and the parent compilation unit is an interface, then check
802 to see if the name we've been given is the name of the interface
803 (located in another namespace). */
805 static int
806 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
808 gfc_state_data *s;
809 gfc_symtree *st;
810 int i;
812 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
813 if (i == 0)
815 *result = st ? st->n.sym : NULL;
816 goto end;
819 if (gfc_current_state () != COMP_SUBROUTINE
820 && gfc_current_state () != COMP_FUNCTION)
821 goto end;
823 s = gfc_state_stack->previous;
824 if (s == NULL)
825 goto end;
827 if (s->state != COMP_INTERFACE)
828 goto end;
829 if (s->sym == NULL)
830 goto end; /* Nameless interface. */
832 if (strcmp (name, s->sym->name) == 0)
834 *result = s->sym;
835 return 0;
838 end:
839 return i;
843 /* Special subroutine for getting a symbol node associated with a
844 procedure name, used in SUBROUTINE and FUNCTION statements. The
845 symbol is created in the parent using with symtree node in the
846 child unit pointing to the symbol. If the current namespace has no
847 parent, then the symbol is just created in the current unit. */
849 static int
850 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
852 gfc_symtree *st;
853 gfc_symbol *sym;
854 int rc = 0;
856 /* Module functions have to be left in their own namespace because
857 they have potentially (almost certainly!) already been referenced.
858 In this sense, they are rather like external functions. This is
859 fixed up in resolve.c(resolve_entries), where the symbol name-
860 space is set to point to the master function, so that the fake
861 result mechanism can work. */
862 if (module_fcn_entry)
864 /* Present if entry is declared to be a module procedure. */
865 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
867 if (*result == NULL)
868 rc = gfc_get_symbol (name, NULL, result);
869 else if (!gfc_get_symbol (name, NULL, &sym) && sym
870 && (*result)->ts.type == BT_UNKNOWN
871 && sym->attr.flavor == FL_UNKNOWN)
872 /* Pick up the typespec for the entry, if declared in the function
873 body. Note that this symbol is FL_UNKNOWN because it will
874 only have appeared in a type declaration. The local symtree
875 is set to point to the module symbol and a unique symtree
876 to the local version. This latter ensures a correct clearing
877 of the symbols. */
879 /* If the ENTRY proceeds its specification, we need to ensure
880 that this does not raise a "has no IMPLICIT type" error. */
881 if (sym->ts.type == BT_UNKNOWN)
882 sym->attr.untyped = 1;
884 (*result)->ts = sym->ts;
886 /* Put the symbol in the procedure namespace so that, should
887 the ENTRY precede its specification, the specification
888 can be applied. */
889 (*result)->ns = gfc_current_ns;
891 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
892 st->n.sym = *result;
893 st = gfc_get_unique_symtree (gfc_current_ns);
894 st->n.sym = sym;
897 else
898 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
900 if (rc)
901 return rc;
903 sym = *result;
905 if (sym->attr.module_procedure
906 && sym->attr.if_source == IFSRC_IFBODY)
908 /* Create a partially populated interface symbol to carry the
909 characteristics of the procedure and the result. */
910 sym->ts.interface = gfc_new_symbol (name, sym->ns);
911 gfc_add_type (sym->ts.interface, &(sym->ts),
912 &gfc_current_locus);
913 gfc_copy_attr (&sym->ts.interface->attr, &sym->attr, NULL);
914 if (sym->attr.dimension)
915 sym->ts.interface->as = gfc_copy_array_spec (sym->as);
917 /* Ideally, at this point, a copy would be made of the formal
918 arguments and their namespace. However, this does not appear
919 to be necessary, albeit at the expense of not being able to
920 use gfc_compare_interfaces directly. */
922 if (sym->result && sym->result != sym)
924 sym->ts.interface->result = sym->result;
925 sym->result = NULL;
927 else if (sym->result)
929 sym->ts.interface->result = sym->ts.interface;
932 else if (sym && !sym->gfc_new
933 && gfc_current_state () != COMP_INTERFACE)
935 /* Trap another encompassed procedure with the same name. All
936 these conditions are necessary to avoid picking up an entry
937 whose name clashes with that of the encompassing procedure;
938 this is handled using gsymbols to register unique,globally
939 accessible names. */
940 if (sym->attr.flavor != 0
941 && sym->attr.proc != 0
942 && (sym->attr.subroutine || sym->attr.function)
943 && sym->attr.if_source != IFSRC_UNKNOWN)
944 gfc_error_now ("Procedure %qs at %C is already defined at %L",
945 name, &sym->declared_at);
947 /* Trap a procedure with a name the same as interface in the
948 encompassing scope. */
949 if (sym->attr.generic != 0
950 && (sym->attr.subroutine || sym->attr.function)
951 && !sym->attr.mod_proc)
952 gfc_error_now ("Name %qs at %C is already defined"
953 " as a generic interface at %L",
954 name, &sym->declared_at);
956 /* Trap declarations of attributes in encompassing scope. The
957 signature for this is that ts.kind is set. Legitimate
958 references only set ts.type. */
959 if (sym->ts.kind != 0
960 && !sym->attr.implicit_type
961 && sym->attr.proc == 0
962 && gfc_current_ns->parent != NULL
963 && sym->attr.access == 0
964 && !module_fcn_entry)
965 gfc_error_now ("Procedure %qs at %C has an explicit interface "
966 "and must not have attributes declared at %L",
967 name, &sym->declared_at);
970 if (gfc_current_ns->parent == NULL || *result == NULL)
971 return rc;
973 /* Module function entries will already have a symtree in
974 the current namespace but will need one at module level. */
975 if (module_fcn_entry)
977 /* Present if entry is declared to be a module procedure. */
978 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
979 if (st == NULL)
980 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
982 else
983 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
985 st->n.sym = sym;
986 sym->refs++;
988 /* See if the procedure should be a module procedure. */
990 if (((sym->ns->proc_name != NULL
991 && sym->ns->proc_name->attr.flavor == FL_MODULE
992 && sym->attr.proc != PROC_MODULE)
993 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
994 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
995 rc = 2;
997 return rc;
1001 /* Verify that the given symbol representing a parameter is C
1002 interoperable, by checking to see if it was marked as such after
1003 its declaration. If the given symbol is not interoperable, a
1004 warning is reported, thus removing the need to return the status to
1005 the calling function. The standard does not require the user use
1006 one of the iso_c_binding named constants to declare an
1007 interoperable parameter, but we can't be sure if the param is C
1008 interop or not if the user doesn't. For example, integer(4) may be
1009 legal Fortran, but doesn't have meaning in C. It may interop with
1010 a number of the C types, which causes a problem because the
1011 compiler can't know which one. This code is almost certainly not
1012 portable, and the user will get what they deserve if the C type
1013 across platforms isn't always interoperable with integer(4). If
1014 the user had used something like integer(c_int) or integer(c_long),
1015 the compiler could have automatically handled the varying sizes
1016 across platforms. */
1018 bool
1019 gfc_verify_c_interop_param (gfc_symbol *sym)
1021 int is_c_interop = 0;
1022 bool retval = true;
1024 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1025 Don't repeat the checks here. */
1026 if (sym->attr.implicit_type)
1027 return true;
1029 /* For subroutines or functions that are passed to a BIND(C) procedure,
1030 they're interoperable if they're BIND(C) and their params are all
1031 interoperable. */
1032 if (sym->attr.flavor == FL_PROCEDURE)
1034 if (sym->attr.is_bind_c == 0)
1036 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1037 "attribute to be C interoperable", sym->name,
1038 &(sym->declared_at));
1039 return false;
1041 else
1043 if (sym->attr.is_c_interop == 1)
1044 /* We've already checked this procedure; don't check it again. */
1045 return true;
1046 else
1047 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1048 sym->common_block);
1052 /* See if we've stored a reference to a procedure that owns sym. */
1053 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1055 if (sym->ns->proc_name->attr.is_bind_c == 1)
1057 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1059 if (is_c_interop != 1)
1061 /* Make personalized messages to give better feedback. */
1062 if (sym->ts.type == BT_DERIVED)
1063 gfc_error ("Variable %qs at %L is a dummy argument to the "
1064 "BIND(C) procedure %qs but is not C interoperable "
1065 "because derived type %qs is not C interoperable",
1066 sym->name, &(sym->declared_at),
1067 sym->ns->proc_name->name,
1068 sym->ts.u.derived->name);
1069 else if (sym->ts.type == BT_CLASS)
1070 gfc_error ("Variable %qs at %L is a dummy argument to the "
1071 "BIND(C) procedure %qs but is not C interoperable "
1072 "because it is polymorphic",
1073 sym->name, &(sym->declared_at),
1074 sym->ns->proc_name->name);
1075 else if (warn_c_binding_type)
1076 gfc_warning (OPT_Wc_binding_type,
1077 "Variable %qs at %L is a dummy argument of the "
1078 "BIND(C) procedure %qs but may not be C "
1079 "interoperable",
1080 sym->name, &(sym->declared_at),
1081 sym->ns->proc_name->name);
1084 /* Character strings are only C interoperable if they have a
1085 length of 1. */
1086 if (sym->ts.type == BT_CHARACTER)
1088 gfc_charlen *cl = sym->ts.u.cl;
1089 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1090 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1092 gfc_error ("Character argument %qs at %L "
1093 "must be length 1 because "
1094 "procedure %qs is BIND(C)",
1095 sym->name, &sym->declared_at,
1096 sym->ns->proc_name->name);
1097 retval = false;
1101 /* We have to make sure that any param to a bind(c) routine does
1102 not have the allocatable, pointer, or optional attributes,
1103 according to J3/04-007, section 5.1. */
1104 if (sym->attr.allocatable == 1
1105 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1106 "ALLOCATABLE attribute in procedure %qs "
1107 "with BIND(C)", sym->name,
1108 &(sym->declared_at),
1109 sym->ns->proc_name->name))
1110 retval = false;
1112 if (sym->attr.pointer == 1
1113 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1114 "POINTER attribute in procedure %qs "
1115 "with BIND(C)", sym->name,
1116 &(sym->declared_at),
1117 sym->ns->proc_name->name))
1118 retval = false;
1120 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1122 gfc_error ("Scalar variable %qs at %L with POINTER or "
1123 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1124 " supported", sym->name, &(sym->declared_at),
1125 sym->ns->proc_name->name);
1126 retval = false;
1129 if (sym->attr.optional == 1 && sym->attr.value)
1131 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1132 "and the VALUE attribute because procedure %qs "
1133 "is BIND(C)", sym->name, &(sym->declared_at),
1134 sym->ns->proc_name->name);
1135 retval = false;
1137 else if (sym->attr.optional == 1
1138 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1139 "at %L with OPTIONAL attribute in "
1140 "procedure %qs which is BIND(C)",
1141 sym->name, &(sym->declared_at),
1142 sym->ns->proc_name->name))
1143 retval = false;
1145 /* Make sure that if it has the dimension attribute, that it is
1146 either assumed size or explicit shape. Deferred shape is already
1147 covered by the pointer/allocatable attribute. */
1148 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1149 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1150 "at %L as dummy argument to the BIND(C) "
1151 "procedure '%s' at %L", sym->name,
1152 &(sym->declared_at),
1153 sym->ns->proc_name->name,
1154 &(sym->ns->proc_name->declared_at)))
1155 retval = false;
1159 return retval;
1164 /* Function called by variable_decl() that adds a name to the symbol table. */
1166 static bool
1167 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1168 gfc_array_spec **as, locus *var_locus)
1170 symbol_attribute attr;
1171 gfc_symbol *sym;
1173 if (gfc_get_symbol (name, NULL, &sym))
1174 return false;
1176 /* Start updating the symbol table. Add basic type attribute if present. */
1177 if (current_ts.type != BT_UNKNOWN
1178 && (sym->attr.implicit_type == 0
1179 || !gfc_compare_types (&sym->ts, &current_ts))
1180 && !gfc_add_type (sym, &current_ts, var_locus))
1181 return false;
1183 if (sym->ts.type == BT_CHARACTER)
1185 sym->ts.u.cl = cl;
1186 sym->ts.deferred = cl_deferred;
1189 /* Add dimension attribute if present. */
1190 if (!gfc_set_array_spec (sym, *as, var_locus))
1191 return false;
1192 *as = NULL;
1194 /* Add attribute to symbol. The copy is so that we can reset the
1195 dimension attribute. */
1196 attr = current_attr;
1197 attr.dimension = 0;
1198 attr.codimension = 0;
1200 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1201 return false;
1203 /* Finish any work that may need to be done for the binding label,
1204 if it's a bind(c). The bind(c) attr is found before the symbol
1205 is made, and before the symbol name (for data decls), so the
1206 current_ts is holding the binding label, or nothing if the
1207 name= attr wasn't given. Therefore, test here if we're dealing
1208 with a bind(c) and make sure the binding label is set correctly. */
1209 if (sym->attr.is_bind_c == 1)
1211 if (!sym->binding_label)
1213 /* Set the binding label and verify that if a NAME= was specified
1214 then only one identifier was in the entity-decl-list. */
1215 if (!set_binding_label (&sym->binding_label, sym->name,
1216 num_idents_on_line))
1217 return false;
1221 /* See if we know we're in a common block, and if it's a bind(c)
1222 common then we need to make sure we're an interoperable type. */
1223 if (sym->attr.in_common == 1)
1225 /* Test the common block object. */
1226 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1227 && sym->ts.is_c_interop != 1)
1229 gfc_error_now ("Variable %qs in common block %qs at %C "
1230 "must be declared with a C interoperable "
1231 "kind since common block %qs is BIND(C)",
1232 sym->name, sym->common_block->name,
1233 sym->common_block->name);
1234 gfc_clear_error ();
1238 sym->attr.implied_index = 0;
1240 if (sym->ts.type == BT_CLASS)
1241 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1243 return true;
1247 /* Set character constant to the given length. The constant will be padded or
1248 truncated. If we're inside an array constructor without a typespec, we
1249 additionally check that all elements have the same length; check_len -1
1250 means no checking. */
1252 void
1253 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1255 gfc_char_t *s;
1256 int slen;
1258 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1259 gcc_assert (expr->ts.type == BT_CHARACTER);
1261 slen = expr->value.character.length;
1262 if (len != slen)
1264 s = gfc_get_wide_string (len + 1);
1265 memcpy (s, expr->value.character.string,
1266 MIN (len, slen) * sizeof (gfc_char_t));
1267 if (len > slen)
1268 gfc_wide_memset (&s[slen], ' ', len - slen);
1270 if (warn_character_truncation && slen > len)
1271 gfc_warning_now (OPT_Wcharacter_truncation,
1272 "CHARACTER expression at %L is being truncated "
1273 "(%d/%d)", &expr->where, slen, len);
1275 /* Apply the standard by 'hand' otherwise it gets cleared for
1276 initializers. */
1277 if (check_len != -1 && slen != check_len
1278 && !(gfc_option.allow_std & GFC_STD_GNU))
1279 gfc_error_now ("The CHARACTER elements of the array constructor "
1280 "at %L must have the same length (%d/%d)",
1281 &expr->where, slen, check_len);
1283 s[len] = '\0';
1284 free (expr->value.character.string);
1285 expr->value.character.string = s;
1286 expr->value.character.length = len;
1291 /* Function to create and update the enumerator history
1292 using the information passed as arguments.
1293 Pointer "max_enum" is also updated, to point to
1294 enum history node containing largest initializer.
1296 SYM points to the symbol node of enumerator.
1297 INIT points to its enumerator value. */
1299 static void
1300 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1302 enumerator_history *new_enum_history;
1303 gcc_assert (sym != NULL && init != NULL);
1305 new_enum_history = XCNEW (enumerator_history);
1307 new_enum_history->sym = sym;
1308 new_enum_history->initializer = init;
1309 new_enum_history->next = NULL;
1311 if (enum_history == NULL)
1313 enum_history = new_enum_history;
1314 max_enum = enum_history;
1316 else
1318 new_enum_history->next = enum_history;
1319 enum_history = new_enum_history;
1321 if (mpz_cmp (max_enum->initializer->value.integer,
1322 new_enum_history->initializer->value.integer) < 0)
1323 max_enum = new_enum_history;
1328 /* Function to free enum kind history. */
1330 void
1331 gfc_free_enum_history (void)
1333 enumerator_history *current = enum_history;
1334 enumerator_history *next;
1336 while (current != NULL)
1338 next = current->next;
1339 free (current);
1340 current = next;
1342 max_enum = NULL;
1343 enum_history = NULL;
1347 /* Function called by variable_decl() that adds an initialization
1348 expression to a symbol. */
1350 static bool
1351 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1353 symbol_attribute attr;
1354 gfc_symbol *sym;
1355 gfc_expr *init;
1357 init = *initp;
1358 if (find_special (name, &sym, false))
1359 return false;
1361 attr = sym->attr;
1363 /* If this symbol is confirming an implicit parameter type,
1364 then an initialization expression is not allowed. */
1365 if (attr.flavor == FL_PARAMETER
1366 && sym->value != NULL
1367 && *initp != NULL)
1369 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1370 sym->name);
1371 return false;
1374 if (init == NULL)
1376 /* An initializer is required for PARAMETER declarations. */
1377 if (attr.flavor == FL_PARAMETER)
1379 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1380 return false;
1383 else
1385 /* If a variable appears in a DATA block, it cannot have an
1386 initializer. */
1387 if (sym->attr.data)
1389 gfc_error ("Variable %qs at %C with an initializer already "
1390 "appears in a DATA statement", sym->name);
1391 return false;
1394 /* Check if the assignment can happen. This has to be put off
1395 until later for derived type variables and procedure pointers. */
1396 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1397 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1398 && !sym->attr.proc_pointer
1399 && !gfc_check_assign_symbol (sym, NULL, init))
1400 return false;
1402 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1403 && init->ts.type == BT_CHARACTER)
1405 /* Update symbol character length according initializer. */
1406 if (!gfc_check_assign_symbol (sym, NULL, init))
1407 return false;
1409 if (sym->ts.u.cl->length == NULL)
1411 int clen;
1412 /* If there are multiple CHARACTER variables declared on the
1413 same line, we don't want them to share the same length. */
1414 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1416 if (sym->attr.flavor == FL_PARAMETER)
1418 if (init->expr_type == EXPR_CONSTANT)
1420 clen = init->value.character.length;
1421 sym->ts.u.cl->length
1422 = gfc_get_int_expr (gfc_default_integer_kind,
1423 NULL, clen);
1425 else if (init->expr_type == EXPR_ARRAY)
1427 clen = mpz_get_si (init->ts.u.cl->length->value.integer);
1428 sym->ts.u.cl->length
1429 = gfc_get_int_expr (gfc_default_integer_kind,
1430 NULL, clen);
1432 else if (init->ts.u.cl && init->ts.u.cl->length)
1433 sym->ts.u.cl->length =
1434 gfc_copy_expr (sym->value->ts.u.cl->length);
1437 /* Update initializer character length according symbol. */
1438 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1440 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1442 if (init->expr_type == EXPR_CONSTANT)
1443 gfc_set_constant_character_len (len, init, -1);
1444 else if (init->expr_type == EXPR_ARRAY)
1446 gfc_constructor *c;
1448 /* Build a new charlen to prevent simplification from
1449 deleting the length before it is resolved. */
1450 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1451 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1453 for (c = gfc_constructor_first (init->value.constructor);
1454 c; c = gfc_constructor_next (c))
1455 gfc_set_constant_character_len (len, c->expr, -1);
1460 /* If sym is implied-shape, set its upper bounds from init. */
1461 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1462 && sym->as->type == AS_IMPLIED_SHAPE)
1464 int dim;
1466 if (init->rank == 0)
1468 gfc_error ("Can't initialize implied-shape array at %L"
1469 " with scalar", &sym->declared_at);
1470 return false;
1472 gcc_assert (sym->as->rank == init->rank);
1474 /* Shape should be present, we get an initialization expression. */
1475 gcc_assert (init->shape);
1477 for (dim = 0; dim < sym->as->rank; ++dim)
1479 int k;
1480 gfc_expr* lower;
1481 gfc_expr* e;
1483 lower = sym->as->lower[dim];
1484 if (lower->expr_type != EXPR_CONSTANT)
1486 gfc_error ("Non-constant lower bound in implied-shape"
1487 " declaration at %L", &lower->where);
1488 return false;
1491 /* All dimensions must be without upper bound. */
1492 gcc_assert (!sym->as->upper[dim]);
1494 k = lower->ts.kind;
1495 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1496 mpz_add (e->value.integer,
1497 lower->value.integer, init->shape[dim]);
1498 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1499 sym->as->upper[dim] = e;
1502 sym->as->type = AS_EXPLICIT;
1505 /* Need to check if the expression we initialized this
1506 to was one of the iso_c_binding named constants. If so,
1507 and we're a parameter (constant), let it be iso_c.
1508 For example:
1509 integer(c_int), parameter :: my_int = c_int
1510 integer(my_int) :: my_int_2
1511 If we mark my_int as iso_c (since we can see it's value
1512 is equal to one of the named constants), then my_int_2
1513 will be considered C interoperable. */
1514 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1516 sym->ts.is_iso_c |= init->ts.is_iso_c;
1517 sym->ts.is_c_interop |= init->ts.is_c_interop;
1518 /* attr bits needed for module files. */
1519 sym->attr.is_iso_c |= init->ts.is_iso_c;
1520 sym->attr.is_c_interop |= init->ts.is_c_interop;
1521 if (init->ts.is_iso_c)
1522 sym->ts.f90_type = init->ts.f90_type;
1525 /* Add initializer. Make sure we keep the ranks sane. */
1526 if (sym->attr.dimension && init->rank == 0)
1528 mpz_t size;
1529 gfc_expr *array;
1530 int n;
1531 if (sym->attr.flavor == FL_PARAMETER
1532 && init->expr_type == EXPR_CONSTANT
1533 && spec_size (sym->as, &size)
1534 && mpz_cmp_si (size, 0) > 0)
1536 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1537 &init->where);
1538 for (n = 0; n < (int)mpz_get_si (size); n++)
1539 gfc_constructor_append_expr (&array->value.constructor,
1540 n == 0
1541 ? init
1542 : gfc_copy_expr (init),
1543 &init->where);
1545 array->shape = gfc_get_shape (sym->as->rank);
1546 for (n = 0; n < sym->as->rank; n++)
1547 spec_dimen_size (sym->as, n, &array->shape[n]);
1549 init = array;
1550 mpz_clear (size);
1552 init->rank = sym->as->rank;
1555 sym->value = init;
1556 if (sym->attr.save == SAVE_NONE)
1557 sym->attr.save = SAVE_IMPLICIT;
1558 *initp = NULL;
1561 return true;
1565 /* Function called by variable_decl() that adds a name to a structure
1566 being built. */
1568 static bool
1569 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1570 gfc_array_spec **as)
1572 gfc_component *c;
1573 bool t = true;
1575 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1576 constructing, it must have the pointer attribute. */
1577 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1578 && current_ts.u.derived == gfc_current_block ()
1579 && current_attr.pointer == 0)
1581 gfc_error ("Component at %C must have the POINTER attribute");
1582 return false;
1585 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1587 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1589 gfc_error ("Array component of structure at %C must have explicit "
1590 "or deferred shape");
1591 return false;
1595 if (!gfc_add_component (gfc_current_block(), name, &c))
1596 return false;
1598 c->ts = current_ts;
1599 if (c->ts.type == BT_CHARACTER)
1600 c->ts.u.cl = cl;
1601 c->attr = current_attr;
1603 c->initializer = *init;
1604 *init = NULL;
1606 c->as = *as;
1607 if (c->as != NULL)
1609 if (c->as->corank)
1610 c->attr.codimension = 1;
1611 if (c->as->rank)
1612 c->attr.dimension = 1;
1614 *as = NULL;
1616 /* Should this ever get more complicated, combine with similar section
1617 in add_init_expr_to_sym into a separate function. */
1618 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1619 && c->ts.u.cl
1620 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1622 int len;
1624 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1625 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1626 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1628 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1630 if (c->initializer->expr_type == EXPR_CONSTANT)
1631 gfc_set_constant_character_len (len, c->initializer, -1);
1632 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1633 c->initializer->ts.u.cl->length->value.integer))
1635 gfc_constructor *ctor;
1636 ctor = gfc_constructor_first (c->initializer->value.constructor);
1638 if (ctor)
1640 int first_len;
1641 bool has_ts = (c->initializer->ts.u.cl
1642 && c->initializer->ts.u.cl->length_from_typespec);
1644 /* Remember the length of the first element for checking
1645 that all elements *in the constructor* have the same
1646 length. This need not be the length of the LHS! */
1647 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1648 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1649 first_len = ctor->expr->value.character.length;
1651 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1652 if (ctor->expr->expr_type == EXPR_CONSTANT)
1654 gfc_set_constant_character_len (len, ctor->expr,
1655 has_ts ? -1 : first_len);
1656 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1662 /* Check array components. */
1663 if (!c->attr.dimension)
1664 goto scalar;
1666 if (c->attr.pointer)
1668 if (c->as->type != AS_DEFERRED)
1670 gfc_error ("Pointer array component of structure at %C must have a "
1671 "deferred shape");
1672 t = false;
1675 else if (c->attr.allocatable)
1677 if (c->as->type != AS_DEFERRED)
1679 gfc_error ("Allocatable component of structure at %C must have a "
1680 "deferred shape");
1681 t = false;
1684 else
1686 if (c->as->type != AS_EXPLICIT)
1688 gfc_error ("Array component of structure at %C must have an "
1689 "explicit shape");
1690 t = false;
1694 scalar:
1695 if (c->ts.type == BT_CLASS)
1697 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1699 if (t)
1700 t = t2;
1703 return t;
1707 /* Match a 'NULL()', and possibly take care of some side effects. */
1709 match
1710 gfc_match_null (gfc_expr **result)
1712 gfc_symbol *sym;
1713 match m, m2 = MATCH_NO;
1715 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1716 return MATCH_ERROR;
1718 if (m == MATCH_NO)
1720 locus old_loc;
1721 char name[GFC_MAX_SYMBOL_LEN + 1];
1723 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1724 return m2;
1726 old_loc = gfc_current_locus;
1727 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1728 return MATCH_ERROR;
1729 if (m2 != MATCH_YES
1730 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1731 return MATCH_ERROR;
1732 if (m2 == MATCH_NO)
1734 gfc_current_locus = old_loc;
1735 return MATCH_NO;
1739 /* The NULL symbol now has to be/become an intrinsic function. */
1740 if (gfc_get_symbol ("null", NULL, &sym))
1742 gfc_error ("NULL() initialization at %C is ambiguous");
1743 return MATCH_ERROR;
1746 gfc_intrinsic_symbol (sym);
1748 if (sym->attr.proc != PROC_INTRINSIC
1749 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1750 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1751 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1752 return MATCH_ERROR;
1754 *result = gfc_get_null_expr (&gfc_current_locus);
1756 /* Invalid per F2008, C512. */
1757 if (m2 == MATCH_YES)
1759 gfc_error ("NULL() initialization at %C may not have MOLD");
1760 return MATCH_ERROR;
1763 return MATCH_YES;
1767 /* Match the initialization expr for a data pointer or procedure pointer. */
1769 static match
1770 match_pointer_init (gfc_expr **init, int procptr)
1772 match m;
1774 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1776 gfc_error ("Initialization of pointer at %C is not allowed in "
1777 "a PURE procedure");
1778 return MATCH_ERROR;
1780 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1782 /* Match NULL() initialization. */
1783 m = gfc_match_null (init);
1784 if (m != MATCH_NO)
1785 return m;
1787 /* Match non-NULL initialization. */
1788 gfc_matching_ptr_assignment = !procptr;
1789 gfc_matching_procptr_assignment = procptr;
1790 m = gfc_match_rvalue (init);
1791 gfc_matching_ptr_assignment = 0;
1792 gfc_matching_procptr_assignment = 0;
1793 if (m == MATCH_ERROR)
1794 return MATCH_ERROR;
1795 else if (m == MATCH_NO)
1797 gfc_error ("Error in pointer initialization at %C");
1798 return MATCH_ERROR;
1801 if (!procptr && !gfc_resolve_expr (*init))
1802 return MATCH_ERROR;
1804 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1805 "initialization at %C"))
1806 return MATCH_ERROR;
1808 return MATCH_YES;
1812 static bool
1813 check_function_name (char *name)
1815 /* In functions that have a RESULT variable defined, the function name always
1816 refers to function calls. Therefore, the name is not allowed to appear in
1817 specification statements. When checking this, be careful about
1818 'hidden' procedure pointer results ('ppr@'). */
1820 if (gfc_current_state () == COMP_FUNCTION)
1822 gfc_symbol *block = gfc_current_block ();
1823 if (block && block->result && block->result != block
1824 && strcmp (block->result->name, "ppr@") != 0
1825 && strcmp (block->name, name) == 0)
1827 gfc_error ("Function name %qs not allowed at %C", name);
1828 return false;
1832 return true;
1836 /* Match a variable name with an optional initializer. When this
1837 subroutine is called, a variable is expected to be parsed next.
1838 Depending on what is happening at the moment, updates either the
1839 symbol table or the current interface. */
1841 static match
1842 variable_decl (int elem)
1844 char name[GFC_MAX_SYMBOL_LEN + 1];
1845 gfc_expr *initializer, *char_len;
1846 gfc_array_spec *as;
1847 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1848 gfc_charlen *cl;
1849 bool cl_deferred;
1850 locus var_locus;
1851 match m;
1852 bool t;
1853 gfc_symbol *sym;
1855 initializer = NULL;
1856 as = NULL;
1857 cp_as = NULL;
1859 /* When we get here, we've just matched a list of attributes and
1860 maybe a type and a double colon. The next thing we expect to see
1861 is the name of the symbol. */
1862 m = gfc_match_name (name);
1863 if (m != MATCH_YES)
1864 goto cleanup;
1866 var_locus = gfc_current_locus;
1868 /* Now we could see the optional array spec. or character length. */
1869 m = gfc_match_array_spec (&as, true, true);
1870 if (m == MATCH_ERROR)
1871 goto cleanup;
1873 if (m == MATCH_NO)
1874 as = gfc_copy_array_spec (current_as);
1875 else if (current_as
1876 && !merge_array_spec (current_as, as, true))
1878 m = MATCH_ERROR;
1879 goto cleanup;
1882 if (flag_cray_pointer)
1883 cp_as = gfc_copy_array_spec (as);
1885 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1886 determine (and check) whether it can be implied-shape. If it
1887 was parsed as assumed-size, change it because PARAMETERs can not
1888 be assumed-size. */
1889 if (as)
1891 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1893 m = MATCH_ERROR;
1894 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1895 name, &var_locus);
1896 goto cleanup;
1899 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1900 && current_attr.flavor == FL_PARAMETER)
1901 as->type = AS_IMPLIED_SHAPE;
1903 if (as->type == AS_IMPLIED_SHAPE
1904 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1905 &var_locus))
1907 m = MATCH_ERROR;
1908 goto cleanup;
1912 char_len = NULL;
1913 cl = NULL;
1914 cl_deferred = false;
1916 if (current_ts.type == BT_CHARACTER)
1918 switch (match_char_length (&char_len, &cl_deferred, false))
1920 case MATCH_YES:
1921 cl = gfc_new_charlen (gfc_current_ns, NULL);
1923 cl->length = char_len;
1924 break;
1926 /* Non-constant lengths need to be copied after the first
1927 element. Also copy assumed lengths. */
1928 case MATCH_NO:
1929 if (elem > 1
1930 && (current_ts.u.cl->length == NULL
1931 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1933 cl = gfc_new_charlen (gfc_current_ns, NULL);
1934 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1936 else
1937 cl = current_ts.u.cl;
1939 cl_deferred = current_ts.deferred;
1941 break;
1943 case MATCH_ERROR:
1944 goto cleanup;
1948 /* The dummy arguments and result of the abreviated form of MODULE
1949 PROCEDUREs, used in SUBMODULES should not be redefined. */
1950 if (gfc_current_ns->proc_name
1951 && gfc_current_ns->proc_name->abr_modproc_decl)
1953 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1954 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
1956 m = MATCH_ERROR;
1957 gfc_error ("'%s' at %C is a redefinition of the declaration "
1958 "in the corresponding interface for MODULE "
1959 "PROCEDURE '%s'", sym->name,
1960 gfc_current_ns->proc_name->name);
1961 goto cleanup;
1965 /* If this symbol has already shown up in a Cray Pointer declaration,
1966 and this is not a component declaration,
1967 then we want to set the type & bail out. */
1968 if (flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
1970 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1971 if (sym != NULL && sym->attr.cray_pointee)
1973 sym->ts.type = current_ts.type;
1974 sym->ts.kind = current_ts.kind;
1975 sym->ts.u.cl = cl;
1976 sym->ts.u.derived = current_ts.u.derived;
1977 sym->ts.is_c_interop = current_ts.is_c_interop;
1978 sym->ts.is_iso_c = current_ts.is_iso_c;
1979 m = MATCH_YES;
1981 /* Check to see if we have an array specification. */
1982 if (cp_as != NULL)
1984 if (sym->as != NULL)
1986 gfc_error ("Duplicate array spec for Cray pointee at %C");
1987 gfc_free_array_spec (cp_as);
1988 m = MATCH_ERROR;
1989 goto cleanup;
1991 else
1993 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
1994 gfc_internal_error ("Couldn't set pointee array spec.");
1996 /* Fix the array spec. */
1997 m = gfc_mod_pointee_as (sym->as);
1998 if (m == MATCH_ERROR)
1999 goto cleanup;
2002 goto cleanup;
2004 else
2006 gfc_free_array_spec (cp_as);
2010 /* Procedure pointer as function result. */
2011 if (gfc_current_state () == COMP_FUNCTION
2012 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2013 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2014 strcpy (name, "ppr@");
2016 if (gfc_current_state () == COMP_FUNCTION
2017 && strcmp (name, gfc_current_block ()->name) == 0
2018 && gfc_current_block ()->result
2019 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2020 strcpy (name, "ppr@");
2022 /* OK, we've successfully matched the declaration. Now put the
2023 symbol in the current namespace, because it might be used in the
2024 optional initialization expression for this symbol, e.g. this is
2025 perfectly legal:
2027 integer, parameter :: i = huge(i)
2029 This is only true for parameters or variables of a basic type.
2030 For components of derived types, it is not true, so we don't
2031 create a symbol for those yet. If we fail to create the symbol,
2032 bail out. */
2033 if (gfc_current_state () != COMP_DERIVED
2034 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2036 m = MATCH_ERROR;
2037 goto cleanup;
2040 if (!check_function_name (name))
2042 m = MATCH_ERROR;
2043 goto cleanup;
2046 /* We allow old-style initializations of the form
2047 integer i /2/, j(4) /3*3, 1/
2048 (if no colon has been seen). These are different from data
2049 statements in that initializers are only allowed to apply to the
2050 variable immediately preceding, i.e.
2051 integer i, j /1, 2/
2052 is not allowed. Therefore we have to do some work manually, that
2053 could otherwise be left to the matchers for DATA statements. */
2055 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2057 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2058 "initialization at %C"))
2059 return MATCH_ERROR;
2060 else if (gfc_current_state () == COMP_DERIVED)
2062 gfc_error ("Invalid old style initialization for derived type "
2063 "component at %C");
2064 m = MATCH_ERROR;
2065 goto cleanup;
2068 return match_old_style_init (name);
2071 /* The double colon must be present in order to have initializers.
2072 Otherwise the statement is ambiguous with an assignment statement. */
2073 if (colon_seen)
2075 if (gfc_match (" =>") == MATCH_YES)
2077 if (!current_attr.pointer)
2079 gfc_error ("Initialization at %C isn't for a pointer variable");
2080 m = MATCH_ERROR;
2081 goto cleanup;
2084 m = match_pointer_init (&initializer, 0);
2085 if (m != MATCH_YES)
2086 goto cleanup;
2088 else if (gfc_match_char ('=') == MATCH_YES)
2090 if (current_attr.pointer)
2092 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2093 "not %<=%>");
2094 m = MATCH_ERROR;
2095 goto cleanup;
2098 m = gfc_match_init_expr (&initializer);
2099 if (m == MATCH_NO)
2101 gfc_error ("Expected an initialization expression at %C");
2102 m = MATCH_ERROR;
2105 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2106 && gfc_state_stack->state != COMP_DERIVED)
2108 gfc_error ("Initialization of variable at %C is not allowed in "
2109 "a PURE procedure");
2110 m = MATCH_ERROR;
2113 if (current_attr.flavor != FL_PARAMETER
2114 && gfc_state_stack->state != COMP_DERIVED)
2115 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2117 if (m != MATCH_YES)
2118 goto cleanup;
2122 if (initializer != NULL && current_attr.allocatable
2123 && gfc_current_state () == COMP_DERIVED)
2125 gfc_error ("Initialization of allocatable component at %C is not "
2126 "allowed");
2127 m = MATCH_ERROR;
2128 goto cleanup;
2131 /* Add the initializer. Note that it is fine if initializer is
2132 NULL here, because we sometimes also need to check if a
2133 declaration *must* have an initialization expression. */
2134 if (gfc_current_state () != COMP_DERIVED)
2135 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2136 else
2138 if (current_ts.type == BT_DERIVED
2139 && !current_attr.pointer && !initializer)
2140 initializer = gfc_default_initializer (&current_ts);
2141 t = build_struct (name, cl, &initializer, &as);
2144 m = (t) ? MATCH_YES : MATCH_ERROR;
2146 cleanup:
2147 /* Free stuff up and return. */
2148 gfc_free_expr (initializer);
2149 gfc_free_array_spec (as);
2151 return m;
2155 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2156 This assumes that the byte size is equal to the kind number for
2157 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2159 match
2160 gfc_match_old_kind_spec (gfc_typespec *ts)
2162 match m;
2163 int original_kind;
2165 if (gfc_match_char ('*') != MATCH_YES)
2166 return MATCH_NO;
2168 m = gfc_match_small_literal_int (&ts->kind, NULL);
2169 if (m != MATCH_YES)
2170 return MATCH_ERROR;
2172 original_kind = ts->kind;
2174 /* Massage the kind numbers for complex types. */
2175 if (ts->type == BT_COMPLEX)
2177 if (ts->kind % 2)
2179 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2180 gfc_basic_typename (ts->type), original_kind);
2181 return MATCH_ERROR;
2183 ts->kind /= 2;
2187 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2188 ts->kind = 8;
2190 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2192 if (ts->kind == 4)
2194 if (flag_real4_kind == 8)
2195 ts->kind = 8;
2196 if (flag_real4_kind == 10)
2197 ts->kind = 10;
2198 if (flag_real4_kind == 16)
2199 ts->kind = 16;
2202 if (ts->kind == 8)
2204 if (flag_real8_kind == 4)
2205 ts->kind = 4;
2206 if (flag_real8_kind == 10)
2207 ts->kind = 10;
2208 if (flag_real8_kind == 16)
2209 ts->kind = 16;
2213 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2215 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2216 gfc_basic_typename (ts->type), original_kind);
2217 return MATCH_ERROR;
2220 if (!gfc_notify_std (GFC_STD_GNU,
2221 "Nonstandard type declaration %s*%d at %C",
2222 gfc_basic_typename(ts->type), original_kind))
2223 return MATCH_ERROR;
2225 return MATCH_YES;
2229 /* Match a kind specification. Since kinds are generally optional, we
2230 usually return MATCH_NO if something goes wrong. If a "kind="
2231 string is found, then we know we have an error. */
2233 match
2234 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2236 locus where, loc;
2237 gfc_expr *e;
2238 match m, n;
2239 char c;
2240 const char *msg;
2242 m = MATCH_NO;
2243 n = MATCH_YES;
2244 e = NULL;
2246 where = loc = gfc_current_locus;
2248 if (kind_expr_only)
2249 goto kind_expr;
2251 if (gfc_match_char ('(') == MATCH_NO)
2252 return MATCH_NO;
2254 /* Also gobbles optional text. */
2255 if (gfc_match (" kind = ") == MATCH_YES)
2256 m = MATCH_ERROR;
2258 loc = gfc_current_locus;
2260 kind_expr:
2261 n = gfc_match_init_expr (&e);
2263 if (n != MATCH_YES)
2265 if (gfc_matching_function)
2267 /* The function kind expression might include use associated or
2268 imported parameters and try again after the specification
2269 expressions..... */
2270 if (gfc_match_char (')') != MATCH_YES)
2272 gfc_error ("Missing right parenthesis at %C");
2273 m = MATCH_ERROR;
2274 goto no_match;
2277 gfc_free_expr (e);
2278 gfc_undo_symbols ();
2279 return MATCH_YES;
2281 else
2283 /* ....or else, the match is real. */
2284 if (n == MATCH_NO)
2285 gfc_error ("Expected initialization expression at %C");
2286 if (n != MATCH_YES)
2287 return MATCH_ERROR;
2291 if (e->rank != 0)
2293 gfc_error ("Expected scalar initialization expression at %C");
2294 m = MATCH_ERROR;
2295 goto no_match;
2298 msg = gfc_extract_int (e, &ts->kind);
2300 if (msg != NULL)
2302 gfc_error (msg);
2303 m = MATCH_ERROR;
2304 goto no_match;
2307 /* Before throwing away the expression, let's see if we had a
2308 C interoperable kind (and store the fact). */
2309 if (e->ts.is_c_interop == 1)
2311 /* Mark this as C interoperable if being declared with one
2312 of the named constants from iso_c_binding. */
2313 ts->is_c_interop = e->ts.is_iso_c;
2314 ts->f90_type = e->ts.f90_type;
2317 gfc_free_expr (e);
2318 e = NULL;
2320 /* Ignore errors to this point, if we've gotten here. This means
2321 we ignore the m=MATCH_ERROR from above. */
2322 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2324 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2325 gfc_basic_typename (ts->type));
2326 gfc_current_locus = where;
2327 return MATCH_ERROR;
2330 /* Warn if, e.g., c_int is used for a REAL variable, but not
2331 if, e.g., c_double is used for COMPLEX as the standard
2332 explicitly says that the kind type parameter for complex and real
2333 variable is the same, i.e. c_float == c_float_complex. */
2334 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2335 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2336 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2337 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2338 "is %s", gfc_basic_typename (ts->f90_type), &where,
2339 gfc_basic_typename (ts->type));
2341 gfc_gobble_whitespace ();
2342 if ((c = gfc_next_ascii_char ()) != ')'
2343 && (ts->type != BT_CHARACTER || c != ','))
2345 if (ts->type == BT_CHARACTER)
2346 gfc_error ("Missing right parenthesis or comma at %C");
2347 else
2348 gfc_error ("Missing right parenthesis at %C");
2349 m = MATCH_ERROR;
2351 else
2352 /* All tests passed. */
2353 m = MATCH_YES;
2355 if(m == MATCH_ERROR)
2356 gfc_current_locus = where;
2358 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2359 ts->kind = 8;
2361 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2363 if (ts->kind == 4)
2365 if (flag_real4_kind == 8)
2366 ts->kind = 8;
2367 if (flag_real4_kind == 10)
2368 ts->kind = 10;
2369 if (flag_real4_kind == 16)
2370 ts->kind = 16;
2373 if (ts->kind == 8)
2375 if (flag_real8_kind == 4)
2376 ts->kind = 4;
2377 if (flag_real8_kind == 10)
2378 ts->kind = 10;
2379 if (flag_real8_kind == 16)
2380 ts->kind = 16;
2384 /* Return what we know from the test(s). */
2385 return m;
2387 no_match:
2388 gfc_free_expr (e);
2389 gfc_current_locus = where;
2390 return m;
2394 static match
2395 match_char_kind (int * kind, int * is_iso_c)
2397 locus where;
2398 gfc_expr *e;
2399 match m, n;
2400 const char *msg;
2402 m = MATCH_NO;
2403 e = NULL;
2404 where = gfc_current_locus;
2406 n = gfc_match_init_expr (&e);
2408 if (n != MATCH_YES && gfc_matching_function)
2410 /* The expression might include use-associated or imported
2411 parameters and try again after the specification
2412 expressions. */
2413 gfc_free_expr (e);
2414 gfc_undo_symbols ();
2415 return MATCH_YES;
2418 if (n == MATCH_NO)
2419 gfc_error ("Expected initialization expression at %C");
2420 if (n != MATCH_YES)
2421 return MATCH_ERROR;
2423 if (e->rank != 0)
2425 gfc_error ("Expected scalar initialization expression at %C");
2426 m = MATCH_ERROR;
2427 goto no_match;
2430 msg = gfc_extract_int (e, kind);
2431 *is_iso_c = e->ts.is_iso_c;
2432 if (msg != NULL)
2434 gfc_error (msg);
2435 m = MATCH_ERROR;
2436 goto no_match;
2439 gfc_free_expr (e);
2441 /* Ignore errors to this point, if we've gotten here. This means
2442 we ignore the m=MATCH_ERROR from above. */
2443 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2445 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2446 m = MATCH_ERROR;
2448 else
2449 /* All tests passed. */
2450 m = MATCH_YES;
2452 if (m == MATCH_ERROR)
2453 gfc_current_locus = where;
2455 /* Return what we know from the test(s). */
2456 return m;
2458 no_match:
2459 gfc_free_expr (e);
2460 gfc_current_locus = where;
2461 return m;
2465 /* Match the various kind/length specifications in a CHARACTER
2466 declaration. We don't return MATCH_NO. */
2468 match
2469 gfc_match_char_spec (gfc_typespec *ts)
2471 int kind, seen_length, is_iso_c;
2472 gfc_charlen *cl;
2473 gfc_expr *len;
2474 match m;
2475 bool deferred;
2477 len = NULL;
2478 seen_length = 0;
2479 kind = 0;
2480 is_iso_c = 0;
2481 deferred = false;
2483 /* Try the old-style specification first. */
2484 old_char_selector = 0;
2486 m = match_char_length (&len, &deferred, true);
2487 if (m != MATCH_NO)
2489 if (m == MATCH_YES)
2490 old_char_selector = 1;
2491 seen_length = 1;
2492 goto done;
2495 m = gfc_match_char ('(');
2496 if (m != MATCH_YES)
2498 m = MATCH_YES; /* Character without length is a single char. */
2499 goto done;
2502 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2503 if (gfc_match (" kind =") == MATCH_YES)
2505 m = match_char_kind (&kind, &is_iso_c);
2507 if (m == MATCH_ERROR)
2508 goto done;
2509 if (m == MATCH_NO)
2510 goto syntax;
2512 if (gfc_match (" , len =") == MATCH_NO)
2513 goto rparen;
2515 m = char_len_param_value (&len, &deferred);
2516 if (m == MATCH_NO)
2517 goto syntax;
2518 if (m == MATCH_ERROR)
2519 goto done;
2520 seen_length = 1;
2522 goto rparen;
2525 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2526 if (gfc_match (" len =") == MATCH_YES)
2528 m = char_len_param_value (&len, &deferred);
2529 if (m == MATCH_NO)
2530 goto syntax;
2531 if (m == MATCH_ERROR)
2532 goto done;
2533 seen_length = 1;
2535 if (gfc_match_char (')') == MATCH_YES)
2536 goto done;
2538 if (gfc_match (" , kind =") != MATCH_YES)
2539 goto syntax;
2541 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2542 goto done;
2544 goto rparen;
2547 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2548 m = char_len_param_value (&len, &deferred);
2549 if (m == MATCH_NO)
2550 goto syntax;
2551 if (m == MATCH_ERROR)
2552 goto done;
2553 seen_length = 1;
2555 m = gfc_match_char (')');
2556 if (m == MATCH_YES)
2557 goto done;
2559 if (gfc_match_char (',') != MATCH_YES)
2560 goto syntax;
2562 gfc_match (" kind ="); /* Gobble optional text. */
2564 m = match_char_kind (&kind, &is_iso_c);
2565 if (m == MATCH_ERROR)
2566 goto done;
2567 if (m == MATCH_NO)
2568 goto syntax;
2570 rparen:
2571 /* Require a right-paren at this point. */
2572 m = gfc_match_char (')');
2573 if (m == MATCH_YES)
2574 goto done;
2576 syntax:
2577 gfc_error ("Syntax error in CHARACTER declaration at %C");
2578 m = MATCH_ERROR;
2579 gfc_free_expr (len);
2580 return m;
2582 done:
2583 /* Deal with character functions after USE and IMPORT statements. */
2584 if (gfc_matching_function)
2586 gfc_free_expr (len);
2587 gfc_undo_symbols ();
2588 return MATCH_YES;
2591 if (m != MATCH_YES)
2593 gfc_free_expr (len);
2594 return m;
2597 /* Do some final massaging of the length values. */
2598 cl = gfc_new_charlen (gfc_current_ns, NULL);
2600 if (seen_length == 0)
2601 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2602 else
2603 cl->length = len;
2605 ts->u.cl = cl;
2606 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2607 ts->deferred = deferred;
2609 /* We have to know if it was a C interoperable kind so we can
2610 do accurate type checking of bind(c) procs, etc. */
2611 if (kind != 0)
2612 /* Mark this as C interoperable if being declared with one
2613 of the named constants from iso_c_binding. */
2614 ts->is_c_interop = is_iso_c;
2615 else if (len != NULL)
2616 /* Here, we might have parsed something such as: character(c_char)
2617 In this case, the parsing code above grabs the c_char when
2618 looking for the length (line 1690, roughly). it's the last
2619 testcase for parsing the kind params of a character variable.
2620 However, it's not actually the length. this seems like it
2621 could be an error.
2622 To see if the user used a C interop kind, test the expr
2623 of the so called length, and see if it's C interoperable. */
2624 ts->is_c_interop = len->ts.is_iso_c;
2626 return MATCH_YES;
2630 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2631 structure to the matched specification. This is necessary for FUNCTION and
2632 IMPLICIT statements.
2634 If implicit_flag is nonzero, then we don't check for the optional
2635 kind specification. Not doing so is needed for matching an IMPLICIT
2636 statement correctly. */
2638 match
2639 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2641 char name[GFC_MAX_SYMBOL_LEN + 1];
2642 gfc_symbol *sym, *dt_sym;
2643 match m;
2644 char c;
2645 bool seen_deferred_kind, matched_type;
2646 const char *dt_name;
2648 /* A belt and braces check that the typespec is correctly being treated
2649 as a deferred characteristic association. */
2650 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2651 && (gfc_current_block ()->result->ts.kind == -1)
2652 && (ts->kind == -1);
2653 gfc_clear_ts (ts);
2654 if (seen_deferred_kind)
2655 ts->kind = -1;
2657 /* Clear the current binding label, in case one is given. */
2658 curr_binding_label = NULL;
2660 if (gfc_match (" byte") == MATCH_YES)
2662 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2663 return MATCH_ERROR;
2665 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2667 gfc_error ("BYTE type used at %C "
2668 "is not available on the target machine");
2669 return MATCH_ERROR;
2672 ts->type = BT_INTEGER;
2673 ts->kind = 1;
2674 return MATCH_YES;
2678 m = gfc_match (" type (");
2679 matched_type = (m == MATCH_YES);
2680 if (matched_type)
2682 gfc_gobble_whitespace ();
2683 if (gfc_peek_ascii_char () == '*')
2685 if ((m = gfc_match ("*)")) != MATCH_YES)
2686 return m;
2687 if (gfc_current_state () == COMP_DERIVED)
2689 gfc_error ("Assumed type at %C is not allowed for components");
2690 return MATCH_ERROR;
2692 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2693 "at %C"))
2694 return MATCH_ERROR;
2695 ts->type = BT_ASSUMED;
2696 return MATCH_YES;
2699 m = gfc_match ("%n", name);
2700 matched_type = (m == MATCH_YES);
2703 if ((matched_type && strcmp ("integer", name) == 0)
2704 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2706 ts->type = BT_INTEGER;
2707 ts->kind = gfc_default_integer_kind;
2708 goto get_kind;
2711 if ((matched_type && strcmp ("character", name) == 0)
2712 || (!matched_type && gfc_match (" character") == MATCH_YES))
2714 if (matched_type
2715 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2716 "intrinsic-type-spec at %C"))
2717 return MATCH_ERROR;
2719 ts->type = BT_CHARACTER;
2720 if (implicit_flag == 0)
2721 m = gfc_match_char_spec (ts);
2722 else
2723 m = MATCH_YES;
2725 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2726 m = MATCH_ERROR;
2728 return m;
2731 if ((matched_type && strcmp ("real", name) == 0)
2732 || (!matched_type && gfc_match (" real") == MATCH_YES))
2734 ts->type = BT_REAL;
2735 ts->kind = gfc_default_real_kind;
2736 goto get_kind;
2739 if ((matched_type
2740 && (strcmp ("doubleprecision", name) == 0
2741 || (strcmp ("double", name) == 0
2742 && gfc_match (" precision") == MATCH_YES)))
2743 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2745 if (matched_type
2746 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2747 "intrinsic-type-spec at %C"))
2748 return MATCH_ERROR;
2749 if (matched_type && gfc_match_char (')') != MATCH_YES)
2750 return MATCH_ERROR;
2752 ts->type = BT_REAL;
2753 ts->kind = gfc_default_double_kind;
2754 return MATCH_YES;
2757 if ((matched_type && strcmp ("complex", name) == 0)
2758 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2760 ts->type = BT_COMPLEX;
2761 ts->kind = gfc_default_complex_kind;
2762 goto get_kind;
2765 if ((matched_type
2766 && (strcmp ("doublecomplex", name) == 0
2767 || (strcmp ("double", name) == 0
2768 && gfc_match (" complex") == MATCH_YES)))
2769 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2771 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2772 return MATCH_ERROR;
2774 if (matched_type
2775 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2776 "intrinsic-type-spec at %C"))
2777 return MATCH_ERROR;
2779 if (matched_type && gfc_match_char (')') != MATCH_YES)
2780 return MATCH_ERROR;
2782 ts->type = BT_COMPLEX;
2783 ts->kind = gfc_default_double_kind;
2784 return MATCH_YES;
2787 if ((matched_type && strcmp ("logical", name) == 0)
2788 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2790 ts->type = BT_LOGICAL;
2791 ts->kind = gfc_default_logical_kind;
2792 goto get_kind;
2795 if (matched_type)
2796 m = gfc_match_char (')');
2798 if (m == MATCH_YES)
2799 ts->type = BT_DERIVED;
2800 else
2802 /* Match CLASS declarations. */
2803 m = gfc_match (" class ( * )");
2804 if (m == MATCH_ERROR)
2805 return MATCH_ERROR;
2806 else if (m == MATCH_YES)
2808 gfc_symbol *upe;
2809 gfc_symtree *st;
2810 ts->type = BT_CLASS;
2811 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2812 if (upe == NULL)
2814 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2815 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2816 st->n.sym = upe;
2817 gfc_set_sym_referenced (upe);
2818 upe->refs++;
2819 upe->ts.type = BT_VOID;
2820 upe->attr.unlimited_polymorphic = 1;
2821 /* This is essential to force the construction of
2822 unlimited polymorphic component class containers. */
2823 upe->attr.zero_comp = 1;
2824 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2825 &gfc_current_locus))
2826 return MATCH_ERROR;
2828 else
2830 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2831 if (st == NULL)
2832 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2833 st->n.sym = upe;
2834 upe->refs++;
2836 ts->u.derived = upe;
2837 return m;
2840 m = gfc_match (" class ( %n )", name);
2841 if (m != MATCH_YES)
2842 return m;
2843 ts->type = BT_CLASS;
2845 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2846 return MATCH_ERROR;
2849 /* Defer association of the derived type until the end of the
2850 specification block. However, if the derived type can be
2851 found, add it to the typespec. */
2852 if (gfc_matching_function)
2854 ts->u.derived = NULL;
2855 if (gfc_current_state () != COMP_INTERFACE
2856 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2858 sym = gfc_find_dt_in_generic (sym);
2859 ts->u.derived = sym;
2861 return MATCH_YES;
2864 /* Search for the name but allow the components to be defined later. If
2865 type = -1, this typespec has been seen in a function declaration but
2866 the type could not be accessed at that point. The actual derived type is
2867 stored in a symtree with the first letter of the name capitalized; the
2868 symtree with the all lower-case name contains the associated
2869 generic function. */
2870 dt_name = gfc_get_string ("%c%s",
2871 (char) TOUPPER ((unsigned char) name[0]),
2872 (const char*)&name[1]);
2873 sym = NULL;
2874 dt_sym = NULL;
2875 if (ts->kind != -1)
2877 gfc_get_ha_symbol (name, &sym);
2878 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2880 gfc_error ("Type name %qs at %C is ambiguous", name);
2881 return MATCH_ERROR;
2883 if (sym->generic && !dt_sym)
2884 dt_sym = gfc_find_dt_in_generic (sym);
2886 else if (ts->kind == -1)
2888 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2889 || gfc_current_ns->has_import_set;
2890 gfc_find_symbol (name, NULL, iface, &sym);
2891 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2893 gfc_error ("Type name %qs at %C is ambiguous", name);
2894 return MATCH_ERROR;
2896 if (sym && sym->generic && !dt_sym)
2897 dt_sym = gfc_find_dt_in_generic (sym);
2899 ts->kind = 0;
2900 if (sym == NULL)
2901 return MATCH_NO;
2904 if ((sym->attr.flavor != FL_UNKNOWN
2905 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2906 || sym->attr.subroutine)
2908 gfc_error ("Type name %qs at %C conflicts with previously declared "
2909 "entity at %L, which has the same name", name,
2910 &sym->declared_at);
2911 return MATCH_ERROR;
2914 gfc_save_symbol_data (sym);
2915 gfc_set_sym_referenced (sym);
2916 if (!sym->attr.generic
2917 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2918 return MATCH_ERROR;
2920 if (!sym->attr.function
2921 && !gfc_add_function (&sym->attr, sym->name, NULL))
2922 return MATCH_ERROR;
2924 if (!dt_sym)
2926 gfc_interface *intr, *head;
2928 /* Use upper case to save the actual derived-type symbol. */
2929 gfc_get_symbol (dt_name, NULL, &dt_sym);
2930 dt_sym->name = gfc_get_string (sym->name);
2931 head = sym->generic;
2932 intr = gfc_get_interface ();
2933 intr->sym = dt_sym;
2934 intr->where = gfc_current_locus;
2935 intr->next = head;
2936 sym->generic = intr;
2937 sym->attr.if_source = IFSRC_DECL;
2939 else
2940 gfc_save_symbol_data (dt_sym);
2942 gfc_set_sym_referenced (dt_sym);
2944 if (dt_sym->attr.flavor != FL_DERIVED
2945 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2946 return MATCH_ERROR;
2948 ts->u.derived = dt_sym;
2950 return MATCH_YES;
2952 get_kind:
2953 if (matched_type
2954 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2955 "intrinsic-type-spec at %C"))
2956 return MATCH_ERROR;
2958 /* For all types except double, derived and character, look for an
2959 optional kind specifier. MATCH_NO is actually OK at this point. */
2960 if (implicit_flag == 1)
2962 if (matched_type && gfc_match_char (')') != MATCH_YES)
2963 return MATCH_ERROR;
2965 return MATCH_YES;
2968 if (gfc_current_form == FORM_FREE)
2970 c = gfc_peek_ascii_char ();
2971 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2972 && c != ':' && c != ',')
2974 if (matched_type && c == ')')
2976 gfc_next_ascii_char ();
2977 return MATCH_YES;
2979 return MATCH_NO;
2983 m = gfc_match_kind_spec (ts, false);
2984 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2985 m = gfc_match_old_kind_spec (ts);
2987 if (matched_type && gfc_match_char (')') != MATCH_YES)
2988 return MATCH_ERROR;
2990 /* Defer association of the KIND expression of function results
2991 until after USE and IMPORT statements. */
2992 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2993 || gfc_matching_function)
2994 return MATCH_YES;
2996 if (m == MATCH_NO)
2997 m = MATCH_YES; /* No kind specifier found. */
2999 return m;
3003 /* Match an IMPLICIT NONE statement. Actually, this statement is
3004 already matched in parse.c, or we would not end up here in the
3005 first place. So the only thing we need to check, is if there is
3006 trailing garbage. If not, the match is successful. */
3008 match
3009 gfc_match_implicit_none (void)
3011 char c;
3012 match m;
3013 char name[GFC_MAX_SYMBOL_LEN + 1];
3014 bool type = false;
3015 bool external = false;
3016 locus cur_loc = gfc_current_locus;
3018 if (gfc_current_ns->seen_implicit_none
3019 || gfc_current_ns->has_implicit_none_export)
3021 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3022 return MATCH_ERROR;
3025 gfc_gobble_whitespace ();
3026 c = gfc_peek_ascii_char ();
3027 if (c == '(')
3029 (void) gfc_next_ascii_char ();
3030 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3031 return MATCH_ERROR;
3033 gfc_gobble_whitespace ();
3034 if (gfc_peek_ascii_char () == ')')
3036 (void) gfc_next_ascii_char ();
3037 type = true;
3039 else
3040 for(;;)
3042 m = gfc_match (" %n", name);
3043 if (m != MATCH_YES)
3044 return MATCH_ERROR;
3046 if (strcmp (name, "type") == 0)
3047 type = true;
3048 else if (strcmp (name, "external") == 0)
3049 external = true;
3050 else
3051 return MATCH_ERROR;
3053 gfc_gobble_whitespace ();
3054 c = gfc_next_ascii_char ();
3055 if (c == ',')
3056 continue;
3057 if (c == ')')
3058 break;
3059 return MATCH_ERROR;
3062 else
3063 type = true;
3065 if (gfc_match_eos () != MATCH_YES)
3066 return MATCH_ERROR;
3068 gfc_set_implicit_none (type, external, &cur_loc);
3070 return MATCH_YES;
3074 /* Match the letter range(s) of an IMPLICIT statement. */
3076 static match
3077 match_implicit_range (void)
3079 char c, c1, c2;
3080 int inner;
3081 locus cur_loc;
3083 cur_loc = gfc_current_locus;
3085 gfc_gobble_whitespace ();
3086 c = gfc_next_ascii_char ();
3087 if (c != '(')
3089 gfc_error ("Missing character range in IMPLICIT at %C");
3090 goto bad;
3093 inner = 1;
3094 while (inner)
3096 gfc_gobble_whitespace ();
3097 c1 = gfc_next_ascii_char ();
3098 if (!ISALPHA (c1))
3099 goto bad;
3101 gfc_gobble_whitespace ();
3102 c = gfc_next_ascii_char ();
3104 switch (c)
3106 case ')':
3107 inner = 0; /* Fall through. */
3109 case ',':
3110 c2 = c1;
3111 break;
3113 case '-':
3114 gfc_gobble_whitespace ();
3115 c2 = gfc_next_ascii_char ();
3116 if (!ISALPHA (c2))
3117 goto bad;
3119 gfc_gobble_whitespace ();
3120 c = gfc_next_ascii_char ();
3122 if ((c != ',') && (c != ')'))
3123 goto bad;
3124 if (c == ')')
3125 inner = 0;
3127 break;
3129 default:
3130 goto bad;
3133 if (c1 > c2)
3135 gfc_error ("Letters must be in alphabetic order in "
3136 "IMPLICIT statement at %C");
3137 goto bad;
3140 /* See if we can add the newly matched range to the pending
3141 implicits from this IMPLICIT statement. We do not check for
3142 conflicts with whatever earlier IMPLICIT statements may have
3143 set. This is done when we've successfully finished matching
3144 the current one. */
3145 if (!gfc_add_new_implicit_range (c1, c2))
3146 goto bad;
3149 return MATCH_YES;
3151 bad:
3152 gfc_syntax_error (ST_IMPLICIT);
3154 gfc_current_locus = cur_loc;
3155 return MATCH_ERROR;
3159 /* Match an IMPLICIT statement, storing the types for
3160 gfc_set_implicit() if the statement is accepted by the parser.
3161 There is a strange looking, but legal syntactic construction
3162 possible. It looks like:
3164 IMPLICIT INTEGER (a-b) (c-d)
3166 This is legal if "a-b" is a constant expression that happens to
3167 equal one of the legal kinds for integers. The real problem
3168 happens with an implicit specification that looks like:
3170 IMPLICIT INTEGER (a-b)
3172 In this case, a typespec matcher that is "greedy" (as most of the
3173 matchers are) gobbles the character range as a kindspec, leaving
3174 nothing left. We therefore have to go a bit more slowly in the
3175 matching process by inhibiting the kindspec checking during
3176 typespec matching and checking for a kind later. */
3178 match
3179 gfc_match_implicit (void)
3181 gfc_typespec ts;
3182 locus cur_loc;
3183 char c;
3184 match m;
3186 if (gfc_current_ns->seen_implicit_none)
3188 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3189 "statement");
3190 return MATCH_ERROR;
3193 gfc_clear_ts (&ts);
3195 /* We don't allow empty implicit statements. */
3196 if (gfc_match_eos () == MATCH_YES)
3198 gfc_error ("Empty IMPLICIT statement at %C");
3199 return MATCH_ERROR;
3204 /* First cleanup. */
3205 gfc_clear_new_implicit ();
3207 /* A basic type is mandatory here. */
3208 m = gfc_match_decl_type_spec (&ts, 1);
3209 if (m == MATCH_ERROR)
3210 goto error;
3211 if (m == MATCH_NO)
3212 goto syntax;
3214 cur_loc = gfc_current_locus;
3215 m = match_implicit_range ();
3217 if (m == MATCH_YES)
3219 /* We may have <TYPE> (<RANGE>). */
3220 gfc_gobble_whitespace ();
3221 c = gfc_peek_ascii_char ();
3222 if (c == ',' || c == '\n' || c == ';' || c == '!')
3224 /* Check for CHARACTER with no length parameter. */
3225 if (ts.type == BT_CHARACTER && !ts.u.cl)
3227 ts.kind = gfc_default_character_kind;
3228 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3229 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3230 NULL, 1);
3233 /* Record the Successful match. */
3234 if (!gfc_merge_new_implicit (&ts))
3235 return MATCH_ERROR;
3236 if (c == ',')
3237 c = gfc_next_ascii_char ();
3238 else if (gfc_match_eos () == MATCH_ERROR)
3239 goto error;
3240 continue;
3243 gfc_current_locus = cur_loc;
3246 /* Discard the (incorrectly) matched range. */
3247 gfc_clear_new_implicit ();
3249 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3250 if (ts.type == BT_CHARACTER)
3251 m = gfc_match_char_spec (&ts);
3252 else
3254 m = gfc_match_kind_spec (&ts, false);
3255 if (m == MATCH_NO)
3257 m = gfc_match_old_kind_spec (&ts);
3258 if (m == MATCH_ERROR)
3259 goto error;
3260 if (m == MATCH_NO)
3261 goto syntax;
3264 if (m == MATCH_ERROR)
3265 goto error;
3267 m = match_implicit_range ();
3268 if (m == MATCH_ERROR)
3269 goto error;
3270 if (m == MATCH_NO)
3271 goto syntax;
3273 gfc_gobble_whitespace ();
3274 c = gfc_next_ascii_char ();
3275 if (c != ',' && gfc_match_eos () != MATCH_YES)
3276 goto syntax;
3278 if (!gfc_merge_new_implicit (&ts))
3279 return MATCH_ERROR;
3281 while (c == ',');
3283 return MATCH_YES;
3285 syntax:
3286 gfc_syntax_error (ST_IMPLICIT);
3288 error:
3289 return MATCH_ERROR;
3293 match
3294 gfc_match_import (void)
3296 char name[GFC_MAX_SYMBOL_LEN + 1];
3297 match m;
3298 gfc_symbol *sym;
3299 gfc_symtree *st;
3301 if (gfc_current_ns->proc_name == NULL
3302 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3304 gfc_error ("IMPORT statement at %C only permitted in "
3305 "an INTERFACE body");
3306 return MATCH_ERROR;
3309 if (gfc_current_ns->proc_name->attr.module_procedure)
3311 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3312 "in a module procedure interface body");
3313 return MATCH_ERROR;
3316 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3317 return MATCH_ERROR;
3319 if (gfc_match_eos () == MATCH_YES)
3321 /* All host variables should be imported. */
3322 gfc_current_ns->has_import_set = 1;
3323 return MATCH_YES;
3326 if (gfc_match (" ::") == MATCH_YES)
3328 if (gfc_match_eos () == MATCH_YES)
3330 gfc_error ("Expecting list of named entities at %C");
3331 return MATCH_ERROR;
3335 for(;;)
3337 sym = NULL;
3338 m = gfc_match (" %n", name);
3339 switch (m)
3341 case MATCH_YES:
3342 if (gfc_current_ns->parent != NULL
3343 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3345 gfc_error ("Type name %qs at %C is ambiguous", name);
3346 return MATCH_ERROR;
3348 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3349 && gfc_find_symbol (name,
3350 gfc_current_ns->proc_name->ns->parent,
3351 1, &sym))
3353 gfc_error ("Type name %qs at %C is ambiguous", name);
3354 return MATCH_ERROR;
3357 if (sym == NULL)
3359 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3360 "at %C - does not exist.", name);
3361 return MATCH_ERROR;
3364 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3366 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3367 "at %C", name);
3368 goto next_item;
3371 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3372 st->n.sym = sym;
3373 sym->refs++;
3374 sym->attr.imported = 1;
3376 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3378 /* The actual derived type is stored in a symtree with the first
3379 letter of the name capitalized; the symtree with the all
3380 lower-case name contains the associated generic function. */
3381 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3382 gfc_get_string ("%c%s",
3383 (char) TOUPPER ((unsigned char) name[0]),
3384 &name[1]));
3385 st->n.sym = sym;
3386 sym->refs++;
3387 sym->attr.imported = 1;
3390 goto next_item;
3392 case MATCH_NO:
3393 break;
3395 case MATCH_ERROR:
3396 return MATCH_ERROR;
3399 next_item:
3400 if (gfc_match_eos () == MATCH_YES)
3401 break;
3402 if (gfc_match_char (',') != MATCH_YES)
3403 goto syntax;
3406 return MATCH_YES;
3408 syntax:
3409 gfc_error ("Syntax error in IMPORT statement at %C");
3410 return MATCH_ERROR;
3414 /* A minimal implementation of gfc_match without whitespace, escape
3415 characters or variable arguments. Returns true if the next
3416 characters match the TARGET template exactly. */
3418 static bool
3419 match_string_p (const char *target)
3421 const char *p;
3423 for (p = target; *p; p++)
3424 if ((char) gfc_next_ascii_char () != *p)
3425 return false;
3426 return true;
3429 /* Matches an attribute specification including array specs. If
3430 successful, leaves the variables current_attr and current_as
3431 holding the specification. Also sets the colon_seen variable for
3432 later use by matchers associated with initializations.
3434 This subroutine is a little tricky in the sense that we don't know
3435 if we really have an attr-spec until we hit the double colon.
3436 Until that time, we can only return MATCH_NO. This forces us to
3437 check for duplicate specification at this level. */
3439 static match
3440 match_attr_spec (void)
3442 /* Modifiers that can exist in a type statement. */
3443 enum
3444 { GFC_DECL_BEGIN = 0,
3445 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3446 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3447 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3448 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3449 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3450 DECL_NONE, GFC_DECL_END /* Sentinel */
3453 /* GFC_DECL_END is the sentinel, index starts at 0. */
3454 #define NUM_DECL GFC_DECL_END
3456 locus start, seen_at[NUM_DECL];
3457 int seen[NUM_DECL];
3458 unsigned int d;
3459 const char *attr;
3460 match m;
3461 bool t;
3463 gfc_clear_attr (&current_attr);
3464 start = gfc_current_locus;
3466 current_as = NULL;
3467 colon_seen = 0;
3469 /* See if we get all of the keywords up to the final double colon. */
3470 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3471 seen[d] = 0;
3473 for (;;)
3475 char ch;
3477 d = DECL_NONE;
3478 gfc_gobble_whitespace ();
3480 ch = gfc_next_ascii_char ();
3481 if (ch == ':')
3483 /* This is the successful exit condition for the loop. */
3484 if (gfc_next_ascii_char () == ':')
3485 break;
3487 else if (ch == ',')
3489 gfc_gobble_whitespace ();
3490 switch (gfc_peek_ascii_char ())
3492 case 'a':
3493 gfc_next_ascii_char ();
3494 switch (gfc_next_ascii_char ())
3496 case 'l':
3497 if (match_string_p ("locatable"))
3499 /* Matched "allocatable". */
3500 d = DECL_ALLOCATABLE;
3502 break;
3504 case 's':
3505 if (match_string_p ("ynchronous"))
3507 /* Matched "asynchronous". */
3508 d = DECL_ASYNCHRONOUS;
3510 break;
3512 break;
3514 case 'b':
3515 /* Try and match the bind(c). */
3516 m = gfc_match_bind_c (NULL, true);
3517 if (m == MATCH_YES)
3518 d = DECL_IS_BIND_C;
3519 else if (m == MATCH_ERROR)
3520 goto cleanup;
3521 break;
3523 case 'c':
3524 gfc_next_ascii_char ();
3525 if ('o' != gfc_next_ascii_char ())
3526 break;
3527 switch (gfc_next_ascii_char ())
3529 case 'd':
3530 if (match_string_p ("imension"))
3532 d = DECL_CODIMENSION;
3533 break;
3535 case 'n':
3536 if (match_string_p ("tiguous"))
3538 d = DECL_CONTIGUOUS;
3539 break;
3542 break;
3544 case 'd':
3545 if (match_string_p ("dimension"))
3546 d = DECL_DIMENSION;
3547 break;
3549 case 'e':
3550 if (match_string_p ("external"))
3551 d = DECL_EXTERNAL;
3552 break;
3554 case 'i':
3555 if (match_string_p ("int"))
3557 ch = gfc_next_ascii_char ();
3558 if (ch == 'e')
3560 if (match_string_p ("nt"))
3562 /* Matched "intent". */
3563 /* TODO: Call match_intent_spec from here. */
3564 if (gfc_match (" ( in out )") == MATCH_YES)
3565 d = DECL_INOUT;
3566 else if (gfc_match (" ( in )") == MATCH_YES)
3567 d = DECL_IN;
3568 else if (gfc_match (" ( out )") == MATCH_YES)
3569 d = DECL_OUT;
3572 else if (ch == 'r')
3574 if (match_string_p ("insic"))
3576 /* Matched "intrinsic". */
3577 d = DECL_INTRINSIC;
3581 break;
3583 case 'o':
3584 if (match_string_p ("optional"))
3585 d = DECL_OPTIONAL;
3586 break;
3588 case 'p':
3589 gfc_next_ascii_char ();
3590 switch (gfc_next_ascii_char ())
3592 case 'a':
3593 if (match_string_p ("rameter"))
3595 /* Matched "parameter". */
3596 d = DECL_PARAMETER;
3598 break;
3600 case 'o':
3601 if (match_string_p ("inter"))
3603 /* Matched "pointer". */
3604 d = DECL_POINTER;
3606 break;
3608 case 'r':
3609 ch = gfc_next_ascii_char ();
3610 if (ch == 'i')
3612 if (match_string_p ("vate"))
3614 /* Matched "private". */
3615 d = DECL_PRIVATE;
3618 else if (ch == 'o')
3620 if (match_string_p ("tected"))
3622 /* Matched "protected". */
3623 d = DECL_PROTECTED;
3626 break;
3628 case 'u':
3629 if (match_string_p ("blic"))
3631 /* Matched "public". */
3632 d = DECL_PUBLIC;
3634 break;
3636 break;
3638 case 's':
3639 if (match_string_p ("save"))
3640 d = DECL_SAVE;
3641 break;
3643 case 't':
3644 if (match_string_p ("target"))
3645 d = DECL_TARGET;
3646 break;
3648 case 'v':
3649 gfc_next_ascii_char ();
3650 ch = gfc_next_ascii_char ();
3651 if (ch == 'a')
3653 if (match_string_p ("lue"))
3655 /* Matched "value". */
3656 d = DECL_VALUE;
3659 else if (ch == 'o')
3661 if (match_string_p ("latile"))
3663 /* Matched "volatile". */
3664 d = DECL_VOLATILE;
3667 break;
3671 /* No double colon and no recognizable decl_type, so assume that
3672 we've been looking at something else the whole time. */
3673 if (d == DECL_NONE)
3675 m = MATCH_NO;
3676 goto cleanup;
3679 /* Check to make sure any parens are paired up correctly. */
3680 if (gfc_match_parens () == MATCH_ERROR)
3682 m = MATCH_ERROR;
3683 goto cleanup;
3686 seen[d]++;
3687 seen_at[d] = gfc_current_locus;
3689 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3691 gfc_array_spec *as = NULL;
3693 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3694 d == DECL_CODIMENSION);
3696 if (current_as == NULL)
3697 current_as = as;
3698 else if (m == MATCH_YES)
3700 if (!merge_array_spec (as, current_as, false))
3701 m = MATCH_ERROR;
3702 free (as);
3705 if (m == MATCH_NO)
3707 if (d == DECL_CODIMENSION)
3708 gfc_error ("Missing codimension specification at %C");
3709 else
3710 gfc_error ("Missing dimension specification at %C");
3711 m = MATCH_ERROR;
3714 if (m == MATCH_ERROR)
3715 goto cleanup;
3719 /* Since we've seen a double colon, we have to be looking at an
3720 attr-spec. This means that we can now issue errors. */
3721 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3722 if (seen[d] > 1)
3724 switch (d)
3726 case DECL_ALLOCATABLE:
3727 attr = "ALLOCATABLE";
3728 break;
3729 case DECL_ASYNCHRONOUS:
3730 attr = "ASYNCHRONOUS";
3731 break;
3732 case DECL_CODIMENSION:
3733 attr = "CODIMENSION";
3734 break;
3735 case DECL_CONTIGUOUS:
3736 attr = "CONTIGUOUS";
3737 break;
3738 case DECL_DIMENSION:
3739 attr = "DIMENSION";
3740 break;
3741 case DECL_EXTERNAL:
3742 attr = "EXTERNAL";
3743 break;
3744 case DECL_IN:
3745 attr = "INTENT (IN)";
3746 break;
3747 case DECL_OUT:
3748 attr = "INTENT (OUT)";
3749 break;
3750 case DECL_INOUT:
3751 attr = "INTENT (IN OUT)";
3752 break;
3753 case DECL_INTRINSIC:
3754 attr = "INTRINSIC";
3755 break;
3756 case DECL_OPTIONAL:
3757 attr = "OPTIONAL";
3758 break;
3759 case DECL_PARAMETER:
3760 attr = "PARAMETER";
3761 break;
3762 case DECL_POINTER:
3763 attr = "POINTER";
3764 break;
3765 case DECL_PROTECTED:
3766 attr = "PROTECTED";
3767 break;
3768 case DECL_PRIVATE:
3769 attr = "PRIVATE";
3770 break;
3771 case DECL_PUBLIC:
3772 attr = "PUBLIC";
3773 break;
3774 case DECL_SAVE:
3775 attr = "SAVE";
3776 break;
3777 case DECL_TARGET:
3778 attr = "TARGET";
3779 break;
3780 case DECL_IS_BIND_C:
3781 attr = "IS_BIND_C";
3782 break;
3783 case DECL_VALUE:
3784 attr = "VALUE";
3785 break;
3786 case DECL_VOLATILE:
3787 attr = "VOLATILE";
3788 break;
3789 default:
3790 attr = NULL; /* This shouldn't happen. */
3793 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3794 m = MATCH_ERROR;
3795 goto cleanup;
3798 /* Now that we've dealt with duplicate attributes, add the attributes
3799 to the current attribute. */
3800 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3802 if (seen[d] == 0)
3803 continue;
3805 if (gfc_current_state () == COMP_DERIVED
3806 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3807 && d != DECL_POINTER && d != DECL_PRIVATE
3808 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3810 if (d == DECL_ALLOCATABLE)
3812 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3813 "attribute at %C in a TYPE definition"))
3815 m = MATCH_ERROR;
3816 goto cleanup;
3819 else
3821 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3822 &seen_at[d]);
3823 m = MATCH_ERROR;
3824 goto cleanup;
3828 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3829 && gfc_current_state () != COMP_MODULE)
3831 if (d == DECL_PRIVATE)
3832 attr = "PRIVATE";
3833 else
3834 attr = "PUBLIC";
3835 if (gfc_current_state () == COMP_DERIVED
3836 && gfc_state_stack->previous
3837 && gfc_state_stack->previous->state == COMP_MODULE)
3839 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3840 "at %L in a TYPE definition", attr,
3841 &seen_at[d]))
3843 m = MATCH_ERROR;
3844 goto cleanup;
3847 else
3849 gfc_error ("%s attribute at %L is not allowed outside of the "
3850 "specification part of a module", attr, &seen_at[d]);
3851 m = MATCH_ERROR;
3852 goto cleanup;
3856 switch (d)
3858 case DECL_ALLOCATABLE:
3859 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3860 break;
3862 case DECL_ASYNCHRONOUS:
3863 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3864 t = false;
3865 else
3866 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3867 break;
3869 case DECL_CODIMENSION:
3870 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3871 break;
3873 case DECL_CONTIGUOUS:
3874 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3875 t = false;
3876 else
3877 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3878 break;
3880 case DECL_DIMENSION:
3881 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3882 break;
3884 case DECL_EXTERNAL:
3885 t = gfc_add_external (&current_attr, &seen_at[d]);
3886 break;
3888 case DECL_IN:
3889 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3890 break;
3892 case DECL_OUT:
3893 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3894 break;
3896 case DECL_INOUT:
3897 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3898 break;
3900 case DECL_INTRINSIC:
3901 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3902 break;
3904 case DECL_OPTIONAL:
3905 t = gfc_add_optional (&current_attr, &seen_at[d]);
3906 break;
3908 case DECL_PARAMETER:
3909 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3910 break;
3912 case DECL_POINTER:
3913 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3914 break;
3916 case DECL_PROTECTED:
3917 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3919 gfc_error ("PROTECTED at %C only allowed in specification "
3920 "part of a module");
3921 t = false;
3922 break;
3925 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3926 t = false;
3927 else
3928 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3929 break;
3931 case DECL_PRIVATE:
3932 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3933 &seen_at[d]);
3934 break;
3936 case DECL_PUBLIC:
3937 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3938 &seen_at[d]);
3939 break;
3941 case DECL_SAVE:
3942 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3943 break;
3945 case DECL_TARGET:
3946 t = gfc_add_target (&current_attr, &seen_at[d]);
3947 break;
3949 case DECL_IS_BIND_C:
3950 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3951 break;
3953 case DECL_VALUE:
3954 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
3955 t = false;
3956 else
3957 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3958 break;
3960 case DECL_VOLATILE:
3961 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
3962 t = false;
3963 else
3964 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3965 break;
3967 default:
3968 gfc_internal_error ("match_attr_spec(): Bad attribute");
3971 if (!t)
3973 m = MATCH_ERROR;
3974 goto cleanup;
3978 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3979 if ((gfc_current_state () == COMP_MODULE
3980 || gfc_current_state () == COMP_SUBMODULE)
3981 && !current_attr.save
3982 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3983 current_attr.save = SAVE_IMPLICIT;
3985 colon_seen = 1;
3986 return MATCH_YES;
3988 cleanup:
3989 gfc_current_locus = start;
3990 gfc_free_array_spec (current_as);
3991 current_as = NULL;
3992 return m;
3996 /* Set the binding label, dest_label, either with the binding label
3997 stored in the given gfc_typespec, ts, or if none was provided, it
3998 will be the symbol name in all lower case, as required by the draft
3999 (J3/04-007, section 15.4.1). If a binding label was given and
4000 there is more than one argument (num_idents), it is an error. */
4002 static bool
4003 set_binding_label (const char **dest_label, const char *sym_name,
4004 int num_idents)
4006 if (num_idents > 1 && has_name_equals)
4008 gfc_error ("Multiple identifiers provided with "
4009 "single NAME= specifier at %C");
4010 return false;
4013 if (curr_binding_label)
4014 /* Binding label given; store in temp holder till have sym. */
4015 *dest_label = curr_binding_label;
4016 else
4018 /* No binding label given, and the NAME= specifier did not exist,
4019 which means there was no NAME="". */
4020 if (sym_name != NULL && has_name_equals == 0)
4021 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4024 return true;
4028 /* Set the status of the given common block as being BIND(C) or not,
4029 depending on the given parameter, is_bind_c. */
4031 void
4032 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4034 com_block->is_bind_c = is_bind_c;
4035 return;
4039 /* Verify that the given gfc_typespec is for a C interoperable type. */
4041 bool
4042 gfc_verify_c_interop (gfc_typespec *ts)
4044 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4045 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4046 ? true : false;
4047 else if (ts->type == BT_CLASS)
4048 return false;
4049 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4050 return false;
4052 return true;
4056 /* Verify that the variables of a given common block, which has been
4057 defined with the attribute specifier bind(c), to be of a C
4058 interoperable type. Errors will be reported here, if
4059 encountered. */
4061 bool
4062 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4064 gfc_symbol *curr_sym = NULL;
4065 bool retval = true;
4067 curr_sym = com_block->head;
4069 /* Make sure we have at least one symbol. */
4070 if (curr_sym == NULL)
4071 return retval;
4073 /* Here we know we have a symbol, so we'll execute this loop
4074 at least once. */
4077 /* The second to last param, 1, says this is in a common block. */
4078 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4079 curr_sym = curr_sym->common_next;
4080 } while (curr_sym != NULL);
4082 return retval;
4086 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4087 an appropriate error message is reported. */
4089 bool
4090 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4091 int is_in_common, gfc_common_head *com_block)
4093 bool bind_c_function = false;
4094 bool retval = true;
4096 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4097 bind_c_function = true;
4099 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4101 tmp_sym = tmp_sym->result;
4102 /* Make sure it wasn't an implicitly typed result. */
4103 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4105 gfc_warning (OPT_Wc_binding_type,
4106 "Implicitly declared BIND(C) function %qs at "
4107 "%L may not be C interoperable", tmp_sym->name,
4108 &tmp_sym->declared_at);
4109 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4110 /* Mark it as C interoperable to prevent duplicate warnings. */
4111 tmp_sym->ts.is_c_interop = 1;
4112 tmp_sym->attr.is_c_interop = 1;
4116 /* Here, we know we have the bind(c) attribute, so if we have
4117 enough type info, then verify that it's a C interop kind.
4118 The info could be in the symbol already, or possibly still in
4119 the given ts (current_ts), so look in both. */
4120 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4122 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4124 /* See if we're dealing with a sym in a common block or not. */
4125 if (is_in_common == 1 && warn_c_binding_type)
4127 gfc_warning (OPT_Wc_binding_type,
4128 "Variable %qs in common block %qs at %L "
4129 "may not be a C interoperable "
4130 "kind though common block %qs is BIND(C)",
4131 tmp_sym->name, com_block->name,
4132 &(tmp_sym->declared_at), com_block->name);
4134 else
4136 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4137 gfc_error ("Type declaration %qs at %L is not C "
4138 "interoperable but it is BIND(C)",
4139 tmp_sym->name, &(tmp_sym->declared_at));
4140 else if (warn_c_binding_type)
4141 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4142 "may not be a C interoperable "
4143 "kind but it is BIND(C)",
4144 tmp_sym->name, &(tmp_sym->declared_at));
4148 /* Variables declared w/in a common block can't be bind(c)
4149 since there's no way for C to see these variables, so there's
4150 semantically no reason for the attribute. */
4151 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4153 gfc_error ("Variable %qs in common block %qs at "
4154 "%L cannot be declared with BIND(C) "
4155 "since it is not a global",
4156 tmp_sym->name, com_block->name,
4157 &(tmp_sym->declared_at));
4158 retval = false;
4161 /* Scalar variables that are bind(c) can not have the pointer
4162 or allocatable attributes. */
4163 if (tmp_sym->attr.is_bind_c == 1)
4165 if (tmp_sym->attr.pointer == 1)
4167 gfc_error ("Variable %qs at %L cannot have both the "
4168 "POINTER and BIND(C) attributes",
4169 tmp_sym->name, &(tmp_sym->declared_at));
4170 retval = false;
4173 if (tmp_sym->attr.allocatable == 1)
4175 gfc_error ("Variable %qs at %L cannot have both the "
4176 "ALLOCATABLE and BIND(C) attributes",
4177 tmp_sym->name, &(tmp_sym->declared_at));
4178 retval = false;
4183 /* If it is a BIND(C) function, make sure the return value is a
4184 scalar value. The previous tests in this function made sure
4185 the type is interoperable. */
4186 if (bind_c_function && tmp_sym->as != NULL)
4187 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4188 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4190 /* BIND(C) functions can not return a character string. */
4191 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4192 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4193 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4194 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4195 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4196 "be a character string", tmp_sym->name,
4197 &(tmp_sym->declared_at));
4200 /* See if the symbol has been marked as private. If it has, make sure
4201 there is no binding label and warn the user if there is one. */
4202 if (tmp_sym->attr.access == ACCESS_PRIVATE
4203 && tmp_sym->binding_label)
4204 /* Use gfc_warning_now because we won't say that the symbol fails
4205 just because of this. */
4206 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4207 "given the binding label %qs", tmp_sym->name,
4208 &(tmp_sym->declared_at), tmp_sym->binding_label);
4210 return retval;
4214 /* Set the appropriate fields for a symbol that's been declared as
4215 BIND(C) (the is_bind_c flag and the binding label), and verify that
4216 the type is C interoperable. Errors are reported by the functions
4217 used to set/test these fields. */
4219 bool
4220 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4222 bool retval = true;
4224 /* TODO: Do we need to make sure the vars aren't marked private? */
4226 /* Set the is_bind_c bit in symbol_attribute. */
4227 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4229 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4230 return false;
4232 return retval;
4236 /* Set the fields marking the given common block as BIND(C), including
4237 a binding label, and report any errors encountered. */
4239 bool
4240 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4242 bool retval = true;
4244 /* destLabel, common name, typespec (which may have binding label). */
4245 if (!set_binding_label (&com_block->binding_label, com_block->name,
4246 num_idents))
4247 return false;
4249 /* Set the given common block (com_block) to being bind(c) (1). */
4250 set_com_block_bind_c (com_block, 1);
4252 return retval;
4256 /* Retrieve the list of one or more identifiers that the given bind(c)
4257 attribute applies to. */
4259 bool
4260 get_bind_c_idents (void)
4262 char name[GFC_MAX_SYMBOL_LEN + 1];
4263 int num_idents = 0;
4264 gfc_symbol *tmp_sym = NULL;
4265 match found_id;
4266 gfc_common_head *com_block = NULL;
4268 if (gfc_match_name (name) == MATCH_YES)
4270 found_id = MATCH_YES;
4271 gfc_get_ha_symbol (name, &tmp_sym);
4273 else if (match_common_name (name) == MATCH_YES)
4275 found_id = MATCH_YES;
4276 com_block = gfc_get_common (name, 0);
4278 else
4280 gfc_error ("Need either entity or common block name for "
4281 "attribute specification statement at %C");
4282 return false;
4285 /* Save the current identifier and look for more. */
4288 /* Increment the number of identifiers found for this spec stmt. */
4289 num_idents++;
4291 /* Make sure we have a sym or com block, and verify that it can
4292 be bind(c). Set the appropriate field(s) and look for more
4293 identifiers. */
4294 if (tmp_sym != NULL || com_block != NULL)
4296 if (tmp_sym != NULL)
4298 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4299 return false;
4301 else
4303 if (!set_verify_bind_c_com_block (com_block, num_idents))
4304 return false;
4307 /* Look to see if we have another identifier. */
4308 tmp_sym = NULL;
4309 if (gfc_match_eos () == MATCH_YES)
4310 found_id = MATCH_NO;
4311 else if (gfc_match_char (',') != MATCH_YES)
4312 found_id = MATCH_NO;
4313 else if (gfc_match_name (name) == MATCH_YES)
4315 found_id = MATCH_YES;
4316 gfc_get_ha_symbol (name, &tmp_sym);
4318 else if (match_common_name (name) == MATCH_YES)
4320 found_id = MATCH_YES;
4321 com_block = gfc_get_common (name, 0);
4323 else
4325 gfc_error ("Missing entity or common block name for "
4326 "attribute specification statement at %C");
4327 return false;
4330 else
4332 gfc_internal_error ("Missing symbol");
4334 } while (found_id == MATCH_YES);
4336 /* if we get here we were successful */
4337 return true;
4341 /* Try and match a BIND(C) attribute specification statement. */
4343 match
4344 gfc_match_bind_c_stmt (void)
4346 match found_match = MATCH_NO;
4347 gfc_typespec *ts;
4349 ts = &current_ts;
4351 /* This may not be necessary. */
4352 gfc_clear_ts (ts);
4353 /* Clear the temporary binding label holder. */
4354 curr_binding_label = NULL;
4356 /* Look for the bind(c). */
4357 found_match = gfc_match_bind_c (NULL, true);
4359 if (found_match == MATCH_YES)
4361 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4362 return MATCH_ERROR;
4364 /* Look for the :: now, but it is not required. */
4365 gfc_match (" :: ");
4367 /* Get the identifier(s) that needs to be updated. This may need to
4368 change to hand the flag(s) for the attr specified so all identifiers
4369 found can have all appropriate parts updated (assuming that the same
4370 spec stmt can have multiple attrs, such as both bind(c) and
4371 allocatable...). */
4372 if (!get_bind_c_idents ())
4373 /* Error message should have printed already. */
4374 return MATCH_ERROR;
4377 return found_match;
4381 /* Match a data declaration statement. */
4383 match
4384 gfc_match_data_decl (void)
4386 gfc_symbol *sym;
4387 match m;
4388 int elem;
4390 num_idents_on_line = 0;
4392 m = gfc_match_decl_type_spec (&current_ts, 0);
4393 if (m != MATCH_YES)
4394 return m;
4396 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4397 && gfc_current_state () != COMP_DERIVED)
4399 sym = gfc_use_derived (current_ts.u.derived);
4401 if (sym == NULL)
4403 m = MATCH_ERROR;
4404 goto cleanup;
4407 current_ts.u.derived = sym;
4410 m = match_attr_spec ();
4411 if (m == MATCH_ERROR)
4413 m = MATCH_NO;
4414 goto cleanup;
4417 if (current_ts.type == BT_CLASS
4418 && current_ts.u.derived->attr.unlimited_polymorphic)
4419 goto ok;
4421 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4422 && current_ts.u.derived->components == NULL
4423 && !current_ts.u.derived->attr.zero_comp)
4426 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4427 goto ok;
4429 gfc_find_symbol (current_ts.u.derived->name,
4430 current_ts.u.derived->ns, 1, &sym);
4432 /* Any symbol that we find had better be a type definition
4433 which has its components defined. */
4434 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4435 && (current_ts.u.derived->components != NULL
4436 || current_ts.u.derived->attr.zero_comp))
4437 goto ok;
4439 gfc_error ("Derived type at %C has not been previously defined "
4440 "and so cannot appear in a derived type definition");
4441 m = MATCH_ERROR;
4442 goto cleanup;
4446 /* If we have an old-style character declaration, and no new-style
4447 attribute specifications, then there a comma is optional between
4448 the type specification and the variable list. */
4449 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4450 gfc_match_char (',');
4452 /* Give the types/attributes to symbols that follow. Give the element
4453 a number so that repeat character length expressions can be copied. */
4454 elem = 1;
4455 for (;;)
4457 num_idents_on_line++;
4458 m = variable_decl (elem++);
4459 if (m == MATCH_ERROR)
4460 goto cleanup;
4461 if (m == MATCH_NO)
4462 break;
4464 if (gfc_match_eos () == MATCH_YES)
4465 goto cleanup;
4466 if (gfc_match_char (',') != MATCH_YES)
4467 break;
4470 if (!gfc_error_flag_test ())
4471 gfc_error ("Syntax error in data declaration at %C");
4472 m = MATCH_ERROR;
4474 gfc_free_data_all (gfc_current_ns);
4476 cleanup:
4477 gfc_free_array_spec (current_as);
4478 current_as = NULL;
4479 return m;
4483 /* Match a prefix associated with a function or subroutine
4484 declaration. If the typespec pointer is nonnull, then a typespec
4485 can be matched. Note that if nothing matches, MATCH_YES is
4486 returned (the null string was matched). */
4488 match
4489 gfc_match_prefix (gfc_typespec *ts)
4491 bool seen_type;
4492 bool seen_impure;
4493 bool found_prefix;
4495 gfc_clear_attr (&current_attr);
4496 seen_type = false;
4497 seen_impure = false;
4499 gcc_assert (!gfc_matching_prefix);
4500 gfc_matching_prefix = true;
4504 found_prefix = false;
4506 if (!seen_type && ts != NULL
4507 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4508 && gfc_match_space () == MATCH_YES)
4511 seen_type = true;
4512 found_prefix = true;
4515 if (gfc_match ("elemental% ") == MATCH_YES)
4517 if (!gfc_add_elemental (&current_attr, NULL))
4518 goto error;
4520 found_prefix = true;
4523 if (gfc_match ("pure% ") == MATCH_YES)
4525 if (!gfc_add_pure (&current_attr, NULL))
4526 goto error;
4528 found_prefix = true;
4531 if (gfc_match ("recursive% ") == MATCH_YES)
4533 if (!gfc_add_recursive (&current_attr, NULL))
4534 goto error;
4536 found_prefix = true;
4539 /* IMPURE is a somewhat special case, as it needs not set an actual
4540 attribute but rather only prevents ELEMENTAL routines from being
4541 automatically PURE. */
4542 if (gfc_match ("impure% ") == MATCH_YES)
4544 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4545 goto error;
4547 seen_impure = true;
4548 found_prefix = true;
4551 while (found_prefix);
4553 /* IMPURE and PURE must not both appear, of course. */
4554 if (seen_impure && current_attr.pure)
4556 gfc_error ("PURE and IMPURE must not appear both at %C");
4557 goto error;
4560 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4561 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4563 if (!gfc_add_pure (&current_attr, NULL))
4564 goto error;
4567 /* At this point, the next item is not a prefix. */
4568 gcc_assert (gfc_matching_prefix);
4570 /* MODULE should be the last prefix before FUNCTION or SUBROUTINE.
4571 Since this is a prefix like PURE, ELEMENTAL, etc., having a
4572 corresponding attribute seems natural and distinguishes these
4573 procedures from procedure types of PROC_MODULE, which these are
4574 as well. */
4575 if ((gfc_current_state () == COMP_INTERFACE
4576 || gfc_current_state () == COMP_CONTAINS)
4577 && gfc_match ("module% ") == MATCH_YES)
4579 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4580 goto error;
4581 else
4582 current_attr.module_procedure = 1;
4585 gfc_matching_prefix = false;
4586 return MATCH_YES;
4588 error:
4589 gcc_assert (gfc_matching_prefix);
4590 gfc_matching_prefix = false;
4591 return MATCH_ERROR;
4595 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4597 static bool
4598 copy_prefix (symbol_attribute *dest, locus *where)
4600 if (current_attr.pure && !gfc_add_pure (dest, where))
4601 return false;
4603 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4604 return false;
4606 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4607 return false;
4609 return true;
4613 /* Match a formal argument list. */
4615 match
4616 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4618 gfc_formal_arglist *head, *tail, *p, *q;
4619 char name[GFC_MAX_SYMBOL_LEN + 1];
4620 gfc_symbol *sym;
4621 match m;
4622 gfc_formal_arglist *formal = NULL;
4624 head = tail = NULL;
4626 /* Keep the interface formal argument list and null it so that the
4627 matching for the new declaration can be done. The numbers and
4628 names of the arguments are checked here. The interface formal
4629 arguments are retained in formal_arglist and the characteristics
4630 are compared in resolve.c(resolve_fl_procedure). See the remark
4631 in get_proc_name about the eventual need to copy the formal_arglist
4632 and populate the formal namespace of the interface symbol. */
4633 if (progname->attr.module_procedure
4634 && progname->attr.host_assoc)
4636 formal = progname->formal;
4637 progname->formal = NULL;
4640 if (gfc_match_char ('(') != MATCH_YES)
4642 if (null_flag)
4643 goto ok;
4644 return MATCH_NO;
4647 if (gfc_match_char (')') == MATCH_YES)
4648 goto ok;
4650 for (;;)
4652 if (gfc_match_char ('*') == MATCH_YES)
4654 sym = NULL;
4655 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4656 "at %C"))
4658 m = MATCH_ERROR;
4659 goto cleanup;
4662 else
4664 m = gfc_match_name (name);
4665 if (m != MATCH_YES)
4666 goto cleanup;
4668 if (gfc_get_symbol (name, NULL, &sym))
4669 goto cleanup;
4672 p = gfc_get_formal_arglist ();
4674 if (head == NULL)
4675 head = tail = p;
4676 else
4678 tail->next = p;
4679 tail = p;
4682 tail->sym = sym;
4684 /* We don't add the VARIABLE flavor because the name could be a
4685 dummy procedure. We don't apply these attributes to formal
4686 arguments of statement functions. */
4687 if (sym != NULL && !st_flag
4688 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4689 || !gfc_missing_attr (&sym->attr, NULL)))
4691 m = MATCH_ERROR;
4692 goto cleanup;
4695 /* The name of a program unit can be in a different namespace,
4696 so check for it explicitly. After the statement is accepted,
4697 the name is checked for especially in gfc_get_symbol(). */
4698 if (gfc_new_block != NULL && sym != NULL
4699 && strcmp (sym->name, gfc_new_block->name) == 0)
4701 gfc_error ("Name %qs at %C is the name of the procedure",
4702 sym->name);
4703 m = MATCH_ERROR;
4704 goto cleanup;
4707 if (gfc_match_char (')') == MATCH_YES)
4708 goto ok;
4710 m = gfc_match_char (',');
4711 if (m != MATCH_YES)
4713 gfc_error ("Unexpected junk in formal argument list at %C");
4714 goto cleanup;
4719 /* Check for duplicate symbols in the formal argument list. */
4720 if (head != NULL)
4722 for (p = head; p->next; p = p->next)
4724 if (p->sym == NULL)
4725 continue;
4727 for (q = p->next; q; q = q->next)
4728 if (p->sym == q->sym)
4730 gfc_error ("Duplicate symbol %qs in formal argument list "
4731 "at %C", p->sym->name);
4733 m = MATCH_ERROR;
4734 goto cleanup;
4739 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4741 m = MATCH_ERROR;
4742 goto cleanup;
4745 if (formal)
4747 for (p = formal, q = head; p && q; p = p->next, q = q->next)
4749 if ((p->next != NULL && q->next == NULL)
4750 || (p->next == NULL && q->next != NULL))
4751 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4752 "formal arguments at %C");
4753 else if ((p->sym == NULL && q->sym == NULL)
4754 || strcmp (p->sym->name, q->sym->name) == 0)
4755 continue;
4756 else
4757 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4758 "argument names (%s/%s) at %C",
4759 p->sym->name, q->sym->name);
4763 return MATCH_YES;
4765 cleanup:
4766 gfc_free_formal_arglist (head);
4767 return m;
4771 /* Match a RESULT specification following a function declaration or
4772 ENTRY statement. Also matches the end-of-statement. */
4774 static match
4775 match_result (gfc_symbol *function, gfc_symbol **result)
4777 char name[GFC_MAX_SYMBOL_LEN + 1];
4778 gfc_symbol *r;
4779 match m;
4781 if (gfc_match (" result (") != MATCH_YES)
4782 return MATCH_NO;
4784 m = gfc_match_name (name);
4785 if (m != MATCH_YES)
4786 return m;
4788 /* Get the right paren, and that's it because there could be the
4789 bind(c) attribute after the result clause. */
4790 if (gfc_match_char (')') != MATCH_YES)
4792 /* TODO: should report the missing right paren here. */
4793 return MATCH_ERROR;
4796 if (strcmp (function->name, name) == 0)
4798 gfc_error ("RESULT variable at %C must be different than function name");
4799 return MATCH_ERROR;
4802 if (gfc_get_symbol (name, NULL, &r))
4803 return MATCH_ERROR;
4805 if (!gfc_add_result (&r->attr, r->name, NULL))
4806 return MATCH_ERROR;
4808 *result = r;
4810 return MATCH_YES;
4814 /* Match a function suffix, which could be a combination of a result
4815 clause and BIND(C), either one, or neither. The draft does not
4816 require them to come in a specific order. */
4818 match
4819 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4821 match is_bind_c; /* Found bind(c). */
4822 match is_result; /* Found result clause. */
4823 match found_match; /* Status of whether we've found a good match. */
4824 char peek_char; /* Character we're going to peek at. */
4825 bool allow_binding_name;
4827 /* Initialize to having found nothing. */
4828 found_match = MATCH_NO;
4829 is_bind_c = MATCH_NO;
4830 is_result = MATCH_NO;
4832 /* Get the next char to narrow between result and bind(c). */
4833 gfc_gobble_whitespace ();
4834 peek_char = gfc_peek_ascii_char ();
4836 /* C binding names are not allowed for internal procedures. */
4837 if (gfc_current_state () == COMP_CONTAINS
4838 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4839 allow_binding_name = false;
4840 else
4841 allow_binding_name = true;
4843 switch (peek_char)
4845 case 'r':
4846 /* Look for result clause. */
4847 is_result = match_result (sym, result);
4848 if (is_result == MATCH_YES)
4850 /* Now see if there is a bind(c) after it. */
4851 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4852 /* We've found the result clause and possibly bind(c). */
4853 found_match = MATCH_YES;
4855 else
4856 /* This should only be MATCH_ERROR. */
4857 found_match = is_result;
4858 break;
4859 case 'b':
4860 /* Look for bind(c) first. */
4861 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4862 if (is_bind_c == MATCH_YES)
4864 /* Now see if a result clause followed it. */
4865 is_result = match_result (sym, result);
4866 found_match = MATCH_YES;
4868 else
4870 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4871 found_match = MATCH_ERROR;
4873 break;
4874 default:
4875 gfc_error ("Unexpected junk after function declaration at %C");
4876 found_match = MATCH_ERROR;
4877 break;
4880 if (is_bind_c == MATCH_YES)
4882 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4883 if (gfc_current_state () == COMP_CONTAINS
4884 && sym->ns->proc_name->attr.flavor != FL_MODULE
4885 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4886 "at %L may not be specified for an internal "
4887 "procedure", &gfc_current_locus))
4888 return MATCH_ERROR;
4890 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4891 return MATCH_ERROR;
4894 return found_match;
4898 /* Procedure pointer return value without RESULT statement:
4899 Add "hidden" result variable named "ppr@". */
4901 static bool
4902 add_hidden_procptr_result (gfc_symbol *sym)
4904 bool case1,case2;
4906 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4907 return false;
4909 /* First usage case: PROCEDURE and EXTERNAL statements. */
4910 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4911 && strcmp (gfc_current_block ()->name, sym->name) == 0
4912 && sym->attr.external;
4913 /* Second usage case: INTERFACE statements. */
4914 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4915 && gfc_state_stack->previous->state == COMP_FUNCTION
4916 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4918 if (case1 || case2)
4920 gfc_symtree *stree;
4921 if (case1)
4922 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4923 else if (case2)
4925 gfc_symtree *st2;
4926 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4927 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4928 st2->n.sym = stree->n.sym;
4930 sym->result = stree->n.sym;
4932 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4933 sym->result->attr.pointer = sym->attr.pointer;
4934 sym->result->attr.external = sym->attr.external;
4935 sym->result->attr.referenced = sym->attr.referenced;
4936 sym->result->ts = sym->ts;
4937 sym->attr.proc_pointer = 0;
4938 sym->attr.pointer = 0;
4939 sym->attr.external = 0;
4940 if (sym->result->attr.external && sym->result->attr.pointer)
4942 sym->result->attr.pointer = 0;
4943 sym->result->attr.proc_pointer = 1;
4946 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4948 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4949 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4950 && sym->result && sym->result != sym && sym->result->attr.external
4951 && sym == gfc_current_ns->proc_name
4952 && sym == sym->result->ns->proc_name
4953 && strcmp ("ppr@", sym->result->name) == 0)
4955 sym->result->attr.proc_pointer = 1;
4956 sym->attr.pointer = 0;
4957 return true;
4959 else
4960 return false;
4964 /* Match the interface for a PROCEDURE declaration,
4965 including brackets (R1212). */
4967 static match
4968 match_procedure_interface (gfc_symbol **proc_if)
4970 match m;
4971 gfc_symtree *st;
4972 locus old_loc, entry_loc;
4973 gfc_namespace *old_ns = gfc_current_ns;
4974 char name[GFC_MAX_SYMBOL_LEN + 1];
4976 old_loc = entry_loc = gfc_current_locus;
4977 gfc_clear_ts (&current_ts);
4979 if (gfc_match (" (") != MATCH_YES)
4981 gfc_current_locus = entry_loc;
4982 return MATCH_NO;
4985 /* Get the type spec. for the procedure interface. */
4986 old_loc = gfc_current_locus;
4987 m = gfc_match_decl_type_spec (&current_ts, 0);
4988 gfc_gobble_whitespace ();
4989 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4990 goto got_ts;
4992 if (m == MATCH_ERROR)
4993 return m;
4995 /* Procedure interface is itself a procedure. */
4996 gfc_current_locus = old_loc;
4997 m = gfc_match_name (name);
4999 /* First look to see if it is already accessible in the current
5000 namespace because it is use associated or contained. */
5001 st = NULL;
5002 if (gfc_find_sym_tree (name, NULL, 0, &st))
5003 return MATCH_ERROR;
5005 /* If it is still not found, then try the parent namespace, if it
5006 exists and create the symbol there if it is still not found. */
5007 if (gfc_current_ns->parent)
5008 gfc_current_ns = gfc_current_ns->parent;
5009 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5010 return MATCH_ERROR;
5012 gfc_current_ns = old_ns;
5013 *proc_if = st->n.sym;
5015 if (*proc_if)
5017 (*proc_if)->refs++;
5018 /* Resolve interface if possible. That way, attr.procedure is only set
5019 if it is declared by a later procedure-declaration-stmt, which is
5020 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5021 while ((*proc_if)->ts.interface)
5022 *proc_if = (*proc_if)->ts.interface;
5024 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5025 && (*proc_if)->ts.type == BT_UNKNOWN
5026 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5027 (*proc_if)->name, NULL))
5028 return MATCH_ERROR;
5031 got_ts:
5032 if (gfc_match (" )") != MATCH_YES)
5034 gfc_current_locus = entry_loc;
5035 return MATCH_NO;
5038 return MATCH_YES;
5042 /* Match a PROCEDURE declaration (R1211). */
5044 static match
5045 match_procedure_decl (void)
5047 match m;
5048 gfc_symbol *sym, *proc_if = NULL;
5049 int num;
5050 gfc_expr *initializer = NULL;
5052 /* Parse interface (with brackets). */
5053 m = match_procedure_interface (&proc_if);
5054 if (m != MATCH_YES)
5055 return m;
5057 /* Parse attributes (with colons). */
5058 m = match_attr_spec();
5059 if (m == MATCH_ERROR)
5060 return MATCH_ERROR;
5062 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5064 current_attr.is_bind_c = 1;
5065 has_name_equals = 0;
5066 curr_binding_label = NULL;
5069 /* Get procedure symbols. */
5070 for(num=1;;num++)
5072 m = gfc_match_symbol (&sym, 0);
5073 if (m == MATCH_NO)
5074 goto syntax;
5075 else if (m == MATCH_ERROR)
5076 return m;
5078 /* Add current_attr to the symbol attributes. */
5079 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5080 return MATCH_ERROR;
5082 if (sym->attr.is_bind_c)
5084 /* Check for C1218. */
5085 if (!proc_if || !proc_if->attr.is_bind_c)
5087 gfc_error ("BIND(C) attribute at %C requires "
5088 "an interface with BIND(C)");
5089 return MATCH_ERROR;
5091 /* Check for C1217. */
5092 if (has_name_equals && sym->attr.pointer)
5094 gfc_error ("BIND(C) procedure with NAME may not have "
5095 "POINTER attribute at %C");
5096 return MATCH_ERROR;
5098 if (has_name_equals && sym->attr.dummy)
5100 gfc_error ("Dummy procedure at %C may not have "
5101 "BIND(C) attribute with NAME");
5102 return MATCH_ERROR;
5104 /* Set binding label for BIND(C). */
5105 if (!set_binding_label (&sym->binding_label, sym->name, num))
5106 return MATCH_ERROR;
5109 if (!gfc_add_external (&sym->attr, NULL))
5110 return MATCH_ERROR;
5112 if (add_hidden_procptr_result (sym))
5113 sym = sym->result;
5115 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5116 return MATCH_ERROR;
5118 /* Set interface. */
5119 if (proc_if != NULL)
5121 if (sym->ts.type != BT_UNKNOWN)
5123 gfc_error ("Procedure %qs at %L already has basic type of %s",
5124 sym->name, &gfc_current_locus,
5125 gfc_basic_typename (sym->ts.type));
5126 return MATCH_ERROR;
5128 sym->ts.interface = proc_if;
5129 sym->attr.untyped = 1;
5130 sym->attr.if_source = IFSRC_IFBODY;
5132 else if (current_ts.type != BT_UNKNOWN)
5134 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5135 return MATCH_ERROR;
5136 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5137 sym->ts.interface->ts = current_ts;
5138 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5139 sym->ts.interface->attr.function = 1;
5140 sym->attr.function = 1;
5141 sym->attr.if_source = IFSRC_UNKNOWN;
5144 if (gfc_match (" =>") == MATCH_YES)
5146 if (!current_attr.pointer)
5148 gfc_error ("Initialization at %C isn't for a pointer variable");
5149 m = MATCH_ERROR;
5150 goto cleanup;
5153 m = match_pointer_init (&initializer, 1);
5154 if (m != MATCH_YES)
5155 goto cleanup;
5157 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5158 goto cleanup;
5162 if (gfc_match_eos () == MATCH_YES)
5163 return MATCH_YES;
5164 if (gfc_match_char (',') != MATCH_YES)
5165 goto syntax;
5168 syntax:
5169 gfc_error ("Syntax error in PROCEDURE statement at %C");
5170 return MATCH_ERROR;
5172 cleanup:
5173 /* Free stuff up and return. */
5174 gfc_free_expr (initializer);
5175 return m;
5179 static match
5180 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5183 /* Match a procedure pointer component declaration (R445). */
5185 static match
5186 match_ppc_decl (void)
5188 match m;
5189 gfc_symbol *proc_if = NULL;
5190 gfc_typespec ts;
5191 int num;
5192 gfc_component *c;
5193 gfc_expr *initializer = NULL;
5194 gfc_typebound_proc* tb;
5195 char name[GFC_MAX_SYMBOL_LEN + 1];
5197 /* Parse interface (with brackets). */
5198 m = match_procedure_interface (&proc_if);
5199 if (m != MATCH_YES)
5200 goto syntax;
5202 /* Parse attributes. */
5203 tb = XCNEW (gfc_typebound_proc);
5204 tb->where = gfc_current_locus;
5205 m = match_binding_attributes (tb, false, true);
5206 if (m == MATCH_ERROR)
5207 return m;
5209 gfc_clear_attr (&current_attr);
5210 current_attr.procedure = 1;
5211 current_attr.proc_pointer = 1;
5212 current_attr.access = tb->access;
5213 current_attr.flavor = FL_PROCEDURE;
5215 /* Match the colons (required). */
5216 if (gfc_match (" ::") != MATCH_YES)
5218 gfc_error ("Expected %<::%> after binding-attributes at %C");
5219 return MATCH_ERROR;
5222 /* Check for C450. */
5223 if (!tb->nopass && proc_if == NULL)
5225 gfc_error("NOPASS or explicit interface required at %C");
5226 return MATCH_ERROR;
5229 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5230 return MATCH_ERROR;
5232 /* Match PPC names. */
5233 ts = current_ts;
5234 for(num=1;;num++)
5236 m = gfc_match_name (name);
5237 if (m == MATCH_NO)
5238 goto syntax;
5239 else if (m == MATCH_ERROR)
5240 return m;
5242 if (!gfc_add_component (gfc_current_block(), name, &c))
5243 return MATCH_ERROR;
5245 /* Add current_attr to the symbol attributes. */
5246 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5247 return MATCH_ERROR;
5249 if (!gfc_add_external (&c->attr, NULL))
5250 return MATCH_ERROR;
5252 if (!gfc_add_proc (&c->attr, name, NULL))
5253 return MATCH_ERROR;
5255 if (num == 1)
5256 c->tb = tb;
5257 else
5259 c->tb = XCNEW (gfc_typebound_proc);
5260 c->tb->where = gfc_current_locus;
5261 *c->tb = *tb;
5264 /* Set interface. */
5265 if (proc_if != NULL)
5267 c->ts.interface = proc_if;
5268 c->attr.untyped = 1;
5269 c->attr.if_source = IFSRC_IFBODY;
5271 else if (ts.type != BT_UNKNOWN)
5273 c->ts = ts;
5274 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5275 c->ts.interface->result = c->ts.interface;
5276 c->ts.interface->ts = ts;
5277 c->ts.interface->attr.flavor = FL_PROCEDURE;
5278 c->ts.interface->attr.function = 1;
5279 c->attr.function = 1;
5280 c->attr.if_source = IFSRC_UNKNOWN;
5283 if (gfc_match (" =>") == MATCH_YES)
5285 m = match_pointer_init (&initializer, 1);
5286 if (m != MATCH_YES)
5288 gfc_free_expr (initializer);
5289 return m;
5291 c->initializer = initializer;
5294 if (gfc_match_eos () == MATCH_YES)
5295 return MATCH_YES;
5296 if (gfc_match_char (',') != MATCH_YES)
5297 goto syntax;
5300 syntax:
5301 gfc_error ("Syntax error in procedure pointer component at %C");
5302 return MATCH_ERROR;
5306 /* Match a PROCEDURE declaration inside an interface (R1206). */
5308 static match
5309 match_procedure_in_interface (void)
5311 match m;
5312 gfc_symbol *sym;
5313 char name[GFC_MAX_SYMBOL_LEN + 1];
5314 locus old_locus;
5316 if (current_interface.type == INTERFACE_NAMELESS
5317 || current_interface.type == INTERFACE_ABSTRACT)
5319 gfc_error ("PROCEDURE at %C must be in a generic interface");
5320 return MATCH_ERROR;
5323 /* Check if the F2008 optional double colon appears. */
5324 gfc_gobble_whitespace ();
5325 old_locus = gfc_current_locus;
5326 if (gfc_match ("::") == MATCH_YES)
5328 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5329 "MODULE PROCEDURE statement at %L", &old_locus))
5330 return MATCH_ERROR;
5332 else
5333 gfc_current_locus = old_locus;
5335 for(;;)
5337 m = gfc_match_name (name);
5338 if (m == MATCH_NO)
5339 goto syntax;
5340 else if (m == MATCH_ERROR)
5341 return m;
5342 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5343 return MATCH_ERROR;
5345 if (!gfc_add_interface (sym))
5346 return MATCH_ERROR;
5348 if (gfc_match_eos () == MATCH_YES)
5349 break;
5350 if (gfc_match_char (',') != MATCH_YES)
5351 goto syntax;
5354 return MATCH_YES;
5356 syntax:
5357 gfc_error ("Syntax error in PROCEDURE statement at %C");
5358 return MATCH_ERROR;
5362 /* General matcher for PROCEDURE declarations. */
5364 static match match_procedure_in_type (void);
5366 match
5367 gfc_match_procedure (void)
5369 match m;
5371 switch (gfc_current_state ())
5373 case COMP_NONE:
5374 case COMP_PROGRAM:
5375 case COMP_MODULE:
5376 case COMP_SUBMODULE:
5377 case COMP_SUBROUTINE:
5378 case COMP_FUNCTION:
5379 case COMP_BLOCK:
5380 m = match_procedure_decl ();
5381 break;
5382 case COMP_INTERFACE:
5383 m = match_procedure_in_interface ();
5384 break;
5385 case COMP_DERIVED:
5386 m = match_ppc_decl ();
5387 break;
5388 case COMP_DERIVED_CONTAINS:
5389 m = match_procedure_in_type ();
5390 break;
5391 default:
5392 return MATCH_NO;
5395 if (m != MATCH_YES)
5396 return m;
5398 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5399 return MATCH_ERROR;
5401 return m;
5405 /* Warn if a matched procedure has the same name as an intrinsic; this is
5406 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5407 parser-state-stack to find out whether we're in a module. */
5409 static void
5410 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5412 bool in_module;
5414 in_module = (gfc_state_stack->previous
5415 && (gfc_state_stack->previous->state == COMP_MODULE
5416 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5418 gfc_warn_intrinsic_shadow (sym, in_module, func);
5422 /* Match a function declaration. */
5424 match
5425 gfc_match_function_decl (void)
5427 char name[GFC_MAX_SYMBOL_LEN + 1];
5428 gfc_symbol *sym, *result;
5429 locus old_loc;
5430 match m;
5431 match suffix_match;
5432 match found_match; /* Status returned by match func. */
5434 if (gfc_current_state () != COMP_NONE
5435 && gfc_current_state () != COMP_INTERFACE
5436 && gfc_current_state () != COMP_CONTAINS)
5437 return MATCH_NO;
5439 gfc_clear_ts (&current_ts);
5441 old_loc = gfc_current_locus;
5443 m = gfc_match_prefix (&current_ts);
5444 if (m != MATCH_YES)
5446 gfc_current_locus = old_loc;
5447 return m;
5450 if (gfc_match ("function% %n", name) != MATCH_YES)
5452 gfc_current_locus = old_loc;
5453 return MATCH_NO;
5456 if (get_proc_name (name, &sym, false))
5457 return MATCH_ERROR;
5459 if (add_hidden_procptr_result (sym))
5460 sym = sym->result;
5462 if (current_attr.module_procedure)
5463 sym->attr.module_procedure = 1;
5465 gfc_new_block = sym;
5467 m = gfc_match_formal_arglist (sym, 0, 0);
5468 if (m == MATCH_NO)
5470 gfc_error ("Expected formal argument list in function "
5471 "definition at %C");
5472 m = MATCH_ERROR;
5473 goto cleanup;
5475 else if (m == MATCH_ERROR)
5476 goto cleanup;
5478 result = NULL;
5480 /* According to the draft, the bind(c) and result clause can
5481 come in either order after the formal_arg_list (i.e., either
5482 can be first, both can exist together or by themselves or neither
5483 one). Therefore, the match_result can't match the end of the
5484 string, and check for the bind(c) or result clause in either order. */
5485 found_match = gfc_match_eos ();
5487 /* Make sure that it isn't already declared as BIND(C). If it is, it
5488 must have been marked BIND(C) with a BIND(C) attribute and that is
5489 not allowed for procedures. */
5490 if (sym->attr.is_bind_c == 1)
5492 sym->attr.is_bind_c = 0;
5493 if (sym->old_symbol != NULL)
5494 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5495 "variables or common blocks",
5496 &(sym->old_symbol->declared_at));
5497 else
5498 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5499 "variables or common blocks", &gfc_current_locus);
5502 if (found_match != MATCH_YES)
5504 /* If we haven't found the end-of-statement, look for a suffix. */
5505 suffix_match = gfc_match_suffix (sym, &result);
5506 if (suffix_match == MATCH_YES)
5507 /* Need to get the eos now. */
5508 found_match = gfc_match_eos ();
5509 else
5510 found_match = suffix_match;
5513 if(found_match != MATCH_YES)
5514 m = MATCH_ERROR;
5515 else
5517 /* Make changes to the symbol. */
5518 m = MATCH_ERROR;
5520 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5521 goto cleanup;
5523 if (!gfc_missing_attr (&sym->attr, NULL)
5524 || !copy_prefix (&sym->attr, &sym->declared_at))
5525 goto cleanup;
5527 /* Delay matching the function characteristics until after the
5528 specification block by signalling kind=-1. */
5529 sym->declared_at = old_loc;
5530 if (current_ts.type != BT_UNKNOWN)
5531 current_ts.kind = -1;
5532 else
5533 current_ts.kind = 0;
5535 if (result == NULL)
5537 if (current_ts.type != BT_UNKNOWN
5538 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5539 goto cleanup;
5540 sym->result = sym;
5542 else
5544 if (current_ts.type != BT_UNKNOWN
5545 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5546 goto cleanup;
5547 sym->result = result;
5550 /* Warn if this procedure has the same name as an intrinsic. */
5551 do_warn_intrinsic_shadow (sym, true);
5553 return MATCH_YES;
5556 cleanup:
5557 gfc_current_locus = old_loc;
5558 return m;
5562 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5563 pass the name of the entry, rather than the gfc_current_block name, and
5564 to return false upon finding an existing global entry. */
5566 static bool
5567 add_global_entry (const char *name, const char *binding_label, bool sub,
5568 locus *where)
5570 gfc_gsymbol *s;
5571 enum gfc_symbol_type type;
5573 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5575 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5576 name is a global identifier. */
5577 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5579 s = gfc_get_gsymbol (name);
5581 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5583 gfc_global_used (s, where);
5584 return false;
5586 else
5588 s->type = type;
5589 s->sym_name = name;
5590 s->where = *where;
5591 s->defined = 1;
5592 s->ns = gfc_current_ns;
5596 /* Don't add the symbol multiple times. */
5597 if (binding_label
5598 && (!gfc_notification_std (GFC_STD_F2008)
5599 || strcmp (name, binding_label) != 0))
5601 s = gfc_get_gsymbol (binding_label);
5603 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5605 gfc_global_used (s, where);
5606 return false;
5608 else
5610 s->type = type;
5611 s->sym_name = name;
5612 s->binding_label = binding_label;
5613 s->where = *where;
5614 s->defined = 1;
5615 s->ns = gfc_current_ns;
5619 return true;
5623 /* Match an ENTRY statement. */
5625 match
5626 gfc_match_entry (void)
5628 gfc_symbol *proc;
5629 gfc_symbol *result;
5630 gfc_symbol *entry;
5631 char name[GFC_MAX_SYMBOL_LEN + 1];
5632 gfc_compile_state state;
5633 match m;
5634 gfc_entry_list *el;
5635 locus old_loc;
5636 bool module_procedure;
5637 char peek_char;
5638 match is_bind_c;
5640 m = gfc_match_name (name);
5641 if (m != MATCH_YES)
5642 return m;
5644 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5645 return MATCH_ERROR;
5647 state = gfc_current_state ();
5648 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5650 switch (state)
5652 case COMP_PROGRAM:
5653 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5654 break;
5655 case COMP_MODULE:
5656 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5657 break;
5658 case COMP_SUBMODULE:
5659 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5660 break;
5661 case COMP_BLOCK_DATA:
5662 gfc_error ("ENTRY statement at %C cannot appear within "
5663 "a BLOCK DATA");
5664 break;
5665 case COMP_INTERFACE:
5666 gfc_error ("ENTRY statement at %C cannot appear within "
5667 "an INTERFACE");
5668 break;
5669 case COMP_DERIVED:
5670 gfc_error ("ENTRY statement at %C cannot appear within "
5671 "a DERIVED TYPE block");
5672 break;
5673 case COMP_IF:
5674 gfc_error ("ENTRY statement at %C cannot appear within "
5675 "an IF-THEN block");
5676 break;
5677 case COMP_DO:
5678 case COMP_DO_CONCURRENT:
5679 gfc_error ("ENTRY statement at %C cannot appear within "
5680 "a DO block");
5681 break;
5682 case COMP_SELECT:
5683 gfc_error ("ENTRY statement at %C cannot appear within "
5684 "a SELECT block");
5685 break;
5686 case COMP_FORALL:
5687 gfc_error ("ENTRY statement at %C cannot appear within "
5688 "a FORALL block");
5689 break;
5690 case COMP_WHERE:
5691 gfc_error ("ENTRY statement at %C cannot appear within "
5692 "a WHERE block");
5693 break;
5694 case COMP_CONTAINS:
5695 gfc_error ("ENTRY statement at %C cannot appear within "
5696 "a contained subprogram");
5697 break;
5698 default:
5699 gfc_error ("Unexpected ENTRY statement at %C");
5701 return MATCH_ERROR;
5704 module_procedure = gfc_current_ns->parent != NULL
5705 && gfc_current_ns->parent->proc_name
5706 && gfc_current_ns->parent->proc_name->attr.flavor
5707 == FL_MODULE;
5709 if (gfc_current_ns->parent != NULL
5710 && gfc_current_ns->parent->proc_name
5711 && !module_procedure)
5713 gfc_error("ENTRY statement at %C cannot appear in a "
5714 "contained procedure");
5715 return MATCH_ERROR;
5718 /* Module function entries need special care in get_proc_name
5719 because previous references within the function will have
5720 created symbols attached to the current namespace. */
5721 if (get_proc_name (name, &entry,
5722 gfc_current_ns->parent != NULL
5723 && module_procedure))
5724 return MATCH_ERROR;
5726 proc = gfc_current_block ();
5728 /* Make sure that it isn't already declared as BIND(C). If it is, it
5729 must have been marked BIND(C) with a BIND(C) attribute and that is
5730 not allowed for procedures. */
5731 if (entry->attr.is_bind_c == 1)
5733 entry->attr.is_bind_c = 0;
5734 if (entry->old_symbol != NULL)
5735 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5736 "variables or common blocks",
5737 &(entry->old_symbol->declared_at));
5738 else
5739 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5740 "variables or common blocks", &gfc_current_locus);
5743 /* Check what next non-whitespace character is so we can tell if there
5744 is the required parens if we have a BIND(C). */
5745 old_loc = gfc_current_locus;
5746 gfc_gobble_whitespace ();
5747 peek_char = gfc_peek_ascii_char ();
5749 if (state == COMP_SUBROUTINE)
5751 m = gfc_match_formal_arglist (entry, 0, 1);
5752 if (m != MATCH_YES)
5753 return MATCH_ERROR;
5755 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5756 never be an internal procedure. */
5757 is_bind_c = gfc_match_bind_c (entry, true);
5758 if (is_bind_c == MATCH_ERROR)
5759 return MATCH_ERROR;
5760 if (is_bind_c == MATCH_YES)
5762 if (peek_char != '(')
5764 gfc_error ("Missing required parentheses before BIND(C) at %C");
5765 return MATCH_ERROR;
5767 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5768 &(entry->declared_at), 1))
5769 return MATCH_ERROR;
5772 if (!gfc_current_ns->parent
5773 && !add_global_entry (name, entry->binding_label, true,
5774 &old_loc))
5775 return MATCH_ERROR;
5777 /* An entry in a subroutine. */
5778 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5779 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5780 return MATCH_ERROR;
5782 else
5784 /* An entry in a function.
5785 We need to take special care because writing
5786 ENTRY f()
5788 ENTRY f
5789 is allowed, whereas
5790 ENTRY f() RESULT (r)
5791 can't be written as
5792 ENTRY f RESULT (r). */
5793 if (gfc_match_eos () == MATCH_YES)
5795 gfc_current_locus = old_loc;
5796 /* Match the empty argument list, and add the interface to
5797 the symbol. */
5798 m = gfc_match_formal_arglist (entry, 0, 1);
5800 else
5801 m = gfc_match_formal_arglist (entry, 0, 0);
5803 if (m != MATCH_YES)
5804 return MATCH_ERROR;
5806 result = NULL;
5808 if (gfc_match_eos () == MATCH_YES)
5810 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5811 || !gfc_add_function (&entry->attr, entry->name, NULL))
5812 return MATCH_ERROR;
5814 entry->result = entry;
5816 else
5818 m = gfc_match_suffix (entry, &result);
5819 if (m == MATCH_NO)
5820 gfc_syntax_error (ST_ENTRY);
5821 if (m != MATCH_YES)
5822 return MATCH_ERROR;
5824 if (result)
5826 if (!gfc_add_result (&result->attr, result->name, NULL)
5827 || !gfc_add_entry (&entry->attr, result->name, NULL)
5828 || !gfc_add_function (&entry->attr, result->name, NULL))
5829 return MATCH_ERROR;
5830 entry->result = result;
5832 else
5834 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5835 || !gfc_add_function (&entry->attr, entry->name, NULL))
5836 return MATCH_ERROR;
5837 entry->result = entry;
5841 if (!gfc_current_ns->parent
5842 && !add_global_entry (name, entry->binding_label, false,
5843 &old_loc))
5844 return MATCH_ERROR;
5847 if (gfc_match_eos () != MATCH_YES)
5849 gfc_syntax_error (ST_ENTRY);
5850 return MATCH_ERROR;
5853 entry->attr.recursive = proc->attr.recursive;
5854 entry->attr.elemental = proc->attr.elemental;
5855 entry->attr.pure = proc->attr.pure;
5857 el = gfc_get_entry_list ();
5858 el->sym = entry;
5859 el->next = gfc_current_ns->entries;
5860 gfc_current_ns->entries = el;
5861 if (el->next)
5862 el->id = el->next->id + 1;
5863 else
5864 el->id = 1;
5866 new_st.op = EXEC_ENTRY;
5867 new_st.ext.entry = el;
5869 return MATCH_YES;
5873 /* Match a subroutine statement, including optional prefixes. */
5875 match
5876 gfc_match_subroutine (void)
5878 char name[GFC_MAX_SYMBOL_LEN + 1];
5879 gfc_symbol *sym;
5880 match m;
5881 match is_bind_c;
5882 char peek_char;
5883 bool allow_binding_name;
5885 if (gfc_current_state () != COMP_NONE
5886 && gfc_current_state () != COMP_INTERFACE
5887 && gfc_current_state () != COMP_CONTAINS)
5888 return MATCH_NO;
5890 m = gfc_match_prefix (NULL);
5891 if (m != MATCH_YES)
5892 return m;
5894 m = gfc_match ("subroutine% %n", name);
5895 if (m != MATCH_YES)
5896 return m;
5898 if (get_proc_name (name, &sym, false))
5899 return MATCH_ERROR;
5901 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5902 the symbol existed before. */
5903 sym->declared_at = gfc_current_locus;
5905 if (current_attr.module_procedure)
5906 sym->attr.module_procedure = 1;
5908 if (add_hidden_procptr_result (sym))
5909 sym = sym->result;
5911 gfc_new_block = sym;
5913 /* Check what next non-whitespace character is so we can tell if there
5914 is the required parens if we have a BIND(C). */
5915 gfc_gobble_whitespace ();
5916 peek_char = gfc_peek_ascii_char ();
5918 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5919 return MATCH_ERROR;
5921 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5922 return MATCH_ERROR;
5924 /* Make sure that it isn't already declared as BIND(C). If it is, it
5925 must have been marked BIND(C) with a BIND(C) attribute and that is
5926 not allowed for procedures. */
5927 if (sym->attr.is_bind_c == 1)
5929 sym->attr.is_bind_c = 0;
5930 if (sym->old_symbol != NULL)
5931 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5932 "variables or common blocks",
5933 &(sym->old_symbol->declared_at));
5934 else
5935 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5936 "variables or common blocks", &gfc_current_locus);
5939 /* C binding names are not allowed for internal procedures. */
5940 if (gfc_current_state () == COMP_CONTAINS
5941 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5942 allow_binding_name = false;
5943 else
5944 allow_binding_name = true;
5946 /* Here, we are just checking if it has the bind(c) attribute, and if
5947 so, then we need to make sure it's all correct. If it doesn't,
5948 we still need to continue matching the rest of the subroutine line. */
5949 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5950 if (is_bind_c == MATCH_ERROR)
5952 /* There was an attempt at the bind(c), but it was wrong. An
5953 error message should have been printed w/in the gfc_match_bind_c
5954 so here we'll just return the MATCH_ERROR. */
5955 return MATCH_ERROR;
5958 if (is_bind_c == MATCH_YES)
5960 /* The following is allowed in the Fortran 2008 draft. */
5961 if (gfc_current_state () == COMP_CONTAINS
5962 && sym->ns->proc_name->attr.flavor != FL_MODULE
5963 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5964 "at %L may not be specified for an internal "
5965 "procedure", &gfc_current_locus))
5966 return MATCH_ERROR;
5968 if (peek_char != '(')
5970 gfc_error ("Missing required parentheses before BIND(C) at %C");
5971 return MATCH_ERROR;
5973 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5974 &(sym->declared_at), 1))
5975 return MATCH_ERROR;
5978 if (gfc_match_eos () != MATCH_YES)
5980 gfc_syntax_error (ST_SUBROUTINE);
5981 return MATCH_ERROR;
5984 if (!copy_prefix (&sym->attr, &sym->declared_at))
5985 return MATCH_ERROR;
5987 /* Warn if it has the same name as an intrinsic. */
5988 do_warn_intrinsic_shadow (sym, false);
5990 return MATCH_YES;
5994 /* Check that the NAME identifier in a BIND attribute or statement
5995 is conform to C identifier rules. */
5997 match
5998 check_bind_name_identifier (char **name)
6000 char *n = *name, *p;
6002 /* Remove leading spaces. */
6003 while (*n == ' ')
6004 n++;
6006 /* On an empty string, free memory and set name to NULL. */
6007 if (*n == '\0')
6009 free (*name);
6010 *name = NULL;
6011 return MATCH_YES;
6014 /* Remove trailing spaces. */
6015 p = n + strlen(n) - 1;
6016 while (*p == ' ')
6017 *(p--) = '\0';
6019 /* Insert the identifier into the symbol table. */
6020 p = xstrdup (n);
6021 free (*name);
6022 *name = p;
6024 /* Now check that identifier is valid under C rules. */
6025 if (ISDIGIT (*p))
6027 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6028 return MATCH_ERROR;
6031 for (; *p; p++)
6032 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6034 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6035 return MATCH_ERROR;
6038 return MATCH_YES;
6042 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6043 given, and set the binding label in either the given symbol (if not
6044 NULL), or in the current_ts. The symbol may be NULL because we may
6045 encounter the BIND(C) before the declaration itself. Return
6046 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6047 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6048 or MATCH_YES if the specifier was correct and the binding label and
6049 bind(c) fields were set correctly for the given symbol or the
6050 current_ts. If allow_binding_name is false, no binding name may be
6051 given. */
6053 match
6054 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6056 char *binding_label = NULL;
6057 gfc_expr *e = NULL;
6059 /* Initialize the flag that specifies whether we encountered a NAME=
6060 specifier or not. */
6061 has_name_equals = 0;
6063 /* This much we have to be able to match, in this order, if
6064 there is a bind(c) label. */
6065 if (gfc_match (" bind ( c ") != MATCH_YES)
6066 return MATCH_NO;
6068 /* Now see if there is a binding label, or if we've reached the
6069 end of the bind(c) attribute without one. */
6070 if (gfc_match_char (',') == MATCH_YES)
6072 if (gfc_match (" name = ") != MATCH_YES)
6074 gfc_error ("Syntax error in NAME= specifier for binding label "
6075 "at %C");
6076 /* should give an error message here */
6077 return MATCH_ERROR;
6080 has_name_equals = 1;
6082 if (gfc_match_init_expr (&e) != MATCH_YES)
6084 gfc_free_expr (e);
6085 return MATCH_ERROR;
6088 if (!gfc_simplify_expr(e, 0))
6090 gfc_error ("NAME= specifier at %C should be a constant expression");
6091 gfc_free_expr (e);
6092 return MATCH_ERROR;
6095 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6096 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6098 gfc_error ("NAME= specifier at %C should be a scalar of "
6099 "default character kind");
6100 gfc_free_expr(e);
6101 return MATCH_ERROR;
6104 // Get a C string from the Fortran string constant
6105 binding_label = gfc_widechar_to_char (e->value.character.string,
6106 e->value.character.length);
6107 gfc_free_expr(e);
6109 // Check that it is valid (old gfc_match_name_C)
6110 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6111 return MATCH_ERROR;
6114 /* Get the required right paren. */
6115 if (gfc_match_char (')') != MATCH_YES)
6117 gfc_error ("Missing closing paren for binding label at %C");
6118 return MATCH_ERROR;
6121 if (has_name_equals && !allow_binding_name)
6123 gfc_error ("No binding name is allowed in BIND(C) at %C");
6124 return MATCH_ERROR;
6127 if (has_name_equals && sym != NULL && sym->attr.dummy)
6129 gfc_error ("For dummy procedure %s, no binding name is "
6130 "allowed in BIND(C) at %C", sym->name);
6131 return MATCH_ERROR;
6135 /* Save the binding label to the symbol. If sym is null, we're
6136 probably matching the typespec attributes of a declaration and
6137 haven't gotten the name yet, and therefore, no symbol yet. */
6138 if (binding_label)
6140 if (sym != NULL)
6141 sym->binding_label = binding_label;
6142 else
6143 curr_binding_label = binding_label;
6145 else if (allow_binding_name)
6147 /* No binding label, but if symbol isn't null, we
6148 can set the label for it here.
6149 If name="" or allow_binding_name is false, no C binding name is
6150 created. */
6151 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6152 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6155 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6156 && current_interface.type == INTERFACE_ABSTRACT)
6158 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6159 return MATCH_ERROR;
6162 return MATCH_YES;
6166 /* Return nonzero if we're currently compiling a contained procedure. */
6168 static int
6169 contained_procedure (void)
6171 gfc_state_data *s = gfc_state_stack;
6173 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6174 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6175 return 1;
6177 return 0;
6180 /* Set the kind of each enumerator. The kind is selected such that it is
6181 interoperable with the corresponding C enumeration type, making
6182 sure that -fshort-enums is honored. */
6184 static void
6185 set_enum_kind(void)
6187 enumerator_history *current_history = NULL;
6188 int kind;
6189 int i;
6191 if (max_enum == NULL || enum_history == NULL)
6192 return;
6194 if (!flag_short_enums)
6195 return;
6197 i = 0;
6200 kind = gfc_integer_kinds[i++].kind;
6202 while (kind < gfc_c_int_kind
6203 && gfc_check_integer_range (max_enum->initializer->value.integer,
6204 kind) != ARITH_OK);
6206 current_history = enum_history;
6207 while (current_history != NULL)
6209 current_history->sym->ts.kind = kind;
6210 current_history = current_history->next;
6215 /* Match any of the various end-block statements. Returns the type of
6216 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6217 and END BLOCK statements cannot be replaced by a single END statement. */
6219 match
6220 gfc_match_end (gfc_statement *st)
6222 char name[GFC_MAX_SYMBOL_LEN + 1];
6223 gfc_compile_state state;
6224 locus old_loc;
6225 const char *block_name;
6226 const char *target;
6227 int eos_ok;
6228 match m;
6229 gfc_namespace *parent_ns, *ns, *prev_ns;
6230 gfc_namespace **nsp;
6231 bool abreviated_modproc_decl;
6233 old_loc = gfc_current_locus;
6234 if (gfc_match ("end") != MATCH_YES)
6235 return MATCH_NO;
6237 state = gfc_current_state ();
6238 block_name = gfc_current_block () == NULL
6239 ? NULL : gfc_current_block ()->name;
6241 switch (state)
6243 case COMP_ASSOCIATE:
6244 case COMP_BLOCK:
6245 if (!strncmp (block_name, "block@", strlen("block@")))
6246 block_name = NULL;
6247 break;
6249 case COMP_CONTAINS:
6250 case COMP_DERIVED_CONTAINS:
6251 state = gfc_state_stack->previous->state;
6252 block_name = gfc_state_stack->previous->sym == NULL
6253 ? NULL : gfc_state_stack->previous->sym->name;
6254 break;
6256 default:
6257 break;
6260 abreviated_modproc_decl
6261 = gfc_current_block ()
6262 && gfc_current_block ()->abr_modproc_decl;
6264 switch (state)
6266 case COMP_NONE:
6267 case COMP_PROGRAM:
6268 *st = ST_END_PROGRAM;
6269 target = " program";
6270 eos_ok = 1;
6271 break;
6273 case COMP_SUBROUTINE:
6274 *st = ST_END_SUBROUTINE;
6275 if (!abreviated_modproc_decl)
6276 target = " subroutine";
6277 else
6278 target = " procedure";
6279 eos_ok = !contained_procedure ();
6280 break;
6282 case COMP_FUNCTION:
6283 *st = ST_END_FUNCTION;
6284 if (!abreviated_modproc_decl)
6285 target = " function";
6286 else
6287 target = " procedure";
6288 eos_ok = !contained_procedure ();
6289 break;
6291 case COMP_BLOCK_DATA:
6292 *st = ST_END_BLOCK_DATA;
6293 target = " block data";
6294 eos_ok = 1;
6295 break;
6297 case COMP_MODULE:
6298 *st = ST_END_MODULE;
6299 target = " module";
6300 eos_ok = 1;
6301 break;
6303 case COMP_SUBMODULE:
6304 *st = ST_END_SUBMODULE;
6305 target = " submodule";
6306 eos_ok = 1;
6307 break;
6309 case COMP_INTERFACE:
6310 *st = ST_END_INTERFACE;
6311 target = " interface";
6312 eos_ok = 0;
6313 break;
6315 case COMP_DERIVED:
6316 case COMP_DERIVED_CONTAINS:
6317 *st = ST_END_TYPE;
6318 target = " type";
6319 eos_ok = 0;
6320 break;
6322 case COMP_ASSOCIATE:
6323 *st = ST_END_ASSOCIATE;
6324 target = " associate";
6325 eos_ok = 0;
6326 break;
6328 case COMP_BLOCK:
6329 *st = ST_END_BLOCK;
6330 target = " block";
6331 eos_ok = 0;
6332 break;
6334 case COMP_IF:
6335 *st = ST_ENDIF;
6336 target = " if";
6337 eos_ok = 0;
6338 break;
6340 case COMP_DO:
6341 case COMP_DO_CONCURRENT:
6342 *st = ST_ENDDO;
6343 target = " do";
6344 eos_ok = 0;
6345 break;
6347 case COMP_CRITICAL:
6348 *st = ST_END_CRITICAL;
6349 target = " critical";
6350 eos_ok = 0;
6351 break;
6353 case COMP_SELECT:
6354 case COMP_SELECT_TYPE:
6355 *st = ST_END_SELECT;
6356 target = " select";
6357 eos_ok = 0;
6358 break;
6360 case COMP_FORALL:
6361 *st = ST_END_FORALL;
6362 target = " forall";
6363 eos_ok = 0;
6364 break;
6366 case COMP_WHERE:
6367 *st = ST_END_WHERE;
6368 target = " where";
6369 eos_ok = 0;
6370 break;
6372 case COMP_ENUM:
6373 *st = ST_END_ENUM;
6374 target = " enum";
6375 eos_ok = 0;
6376 last_initializer = NULL;
6377 set_enum_kind ();
6378 gfc_free_enum_history ();
6379 break;
6381 default:
6382 gfc_error ("Unexpected END statement at %C");
6383 goto cleanup;
6386 old_loc = gfc_current_locus;
6387 if (gfc_match_eos () == MATCH_YES)
6389 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6391 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6392 "instead of %s statement at %L",
6393 abreviated_modproc_decl ? "END PROCEDURE"
6394 : gfc_ascii_statement(*st), &old_loc))
6395 goto cleanup;
6397 else if (!eos_ok)
6399 /* We would have required END [something]. */
6400 gfc_error ("%s statement expected at %L",
6401 gfc_ascii_statement (*st), &old_loc);
6402 goto cleanup;
6405 return MATCH_YES;
6408 /* Verify that we've got the sort of end-block that we're expecting. */
6409 if (gfc_match (target) != MATCH_YES)
6411 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6412 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6413 goto cleanup;
6416 old_loc = gfc_current_locus;
6417 /* If we're at the end, make sure a block name wasn't required. */
6418 if (gfc_match_eos () == MATCH_YES)
6421 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6422 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6423 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6424 return MATCH_YES;
6426 if (!block_name)
6427 return MATCH_YES;
6429 gfc_error ("Expected block name of %qs in %s statement at %L",
6430 block_name, gfc_ascii_statement (*st), &old_loc);
6432 return MATCH_ERROR;
6435 /* END INTERFACE has a special handler for its several possible endings. */
6436 if (*st == ST_END_INTERFACE)
6437 return gfc_match_end_interface ();
6439 /* We haven't hit the end of statement, so what is left must be an
6440 end-name. */
6441 m = gfc_match_space ();
6442 if (m == MATCH_YES)
6443 m = gfc_match_name (name);
6445 if (m == MATCH_NO)
6446 gfc_error ("Expected terminating name at %C");
6447 if (m != MATCH_YES)
6448 goto cleanup;
6450 if (block_name == NULL)
6451 goto syntax;
6453 /* We have to pick out the declared submodule name from the composite
6454 required by F2008:11.2.3 para 2, which ends in the declared name. */
6455 if (state == COMP_SUBMODULE)
6456 block_name = strchr (block_name, '.') + 1;
6458 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6460 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6461 gfc_ascii_statement (*st));
6462 goto cleanup;
6464 /* Procedure pointer as function result. */
6465 else if (strcmp (block_name, "ppr@") == 0
6466 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6468 gfc_error ("Expected label %qs for %s statement at %C",
6469 gfc_current_block ()->ns->proc_name->name,
6470 gfc_ascii_statement (*st));
6471 goto cleanup;
6474 if (gfc_match_eos () == MATCH_YES)
6475 return MATCH_YES;
6477 syntax:
6478 gfc_syntax_error (*st);
6480 cleanup:
6481 gfc_current_locus = old_loc;
6483 /* If we are missing an END BLOCK, we created a half-ready namespace.
6484 Remove it from the parent namespace's sibling list. */
6486 if (state == COMP_BLOCK)
6488 parent_ns = gfc_current_ns->parent;
6490 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6492 prev_ns = NULL;
6493 ns = *nsp;
6494 while (ns)
6496 if (ns == gfc_current_ns)
6498 if (prev_ns == NULL)
6499 *nsp = NULL;
6500 else
6501 prev_ns->sibling = ns->sibling;
6503 prev_ns = ns;
6504 ns = ns->sibling;
6507 gfc_free_namespace (gfc_current_ns);
6508 gfc_current_ns = parent_ns;
6511 return MATCH_ERROR;
6516 /***************** Attribute declaration statements ****************/
6518 /* Set the attribute of a single variable. */
6520 static match
6521 attr_decl1 (void)
6523 char name[GFC_MAX_SYMBOL_LEN + 1];
6524 gfc_array_spec *as;
6526 /* Workaround -Wmaybe-uninitialized false positive during
6527 profiledbootstrap by initializing them. */
6528 gfc_symbol *sym = NULL;
6529 locus var_locus;
6530 match m;
6532 as = NULL;
6534 m = gfc_match_name (name);
6535 if (m != MATCH_YES)
6536 goto cleanup;
6538 if (find_special (name, &sym, false))
6539 return MATCH_ERROR;
6541 if (!check_function_name (name))
6543 m = MATCH_ERROR;
6544 goto cleanup;
6547 var_locus = gfc_current_locus;
6549 /* Deal with possible array specification for certain attributes. */
6550 if (current_attr.dimension
6551 || current_attr.codimension
6552 || current_attr.allocatable
6553 || current_attr.pointer
6554 || current_attr.target)
6556 m = gfc_match_array_spec (&as, !current_attr.codimension,
6557 !current_attr.dimension
6558 && !current_attr.pointer
6559 && !current_attr.target);
6560 if (m == MATCH_ERROR)
6561 goto cleanup;
6563 if (current_attr.dimension && m == MATCH_NO)
6565 gfc_error ("Missing array specification at %L in DIMENSION "
6566 "statement", &var_locus);
6567 m = MATCH_ERROR;
6568 goto cleanup;
6571 if (current_attr.dimension && sym->value)
6573 gfc_error ("Dimensions specified for %s at %L after its "
6574 "initialisation", sym->name, &var_locus);
6575 m = MATCH_ERROR;
6576 goto cleanup;
6579 if (current_attr.codimension && m == MATCH_NO)
6581 gfc_error ("Missing array specification at %L in CODIMENSION "
6582 "statement", &var_locus);
6583 m = MATCH_ERROR;
6584 goto cleanup;
6587 if ((current_attr.allocatable || current_attr.pointer)
6588 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6590 gfc_error ("Array specification must be deferred at %L", &var_locus);
6591 m = MATCH_ERROR;
6592 goto cleanup;
6596 /* Update symbol table. DIMENSION attribute is set in
6597 gfc_set_array_spec(). For CLASS variables, this must be applied
6598 to the first component, or '_data' field. */
6599 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6601 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6603 m = MATCH_ERROR;
6604 goto cleanup;
6607 else
6609 if (current_attr.dimension == 0 && current_attr.codimension == 0
6610 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6612 m = MATCH_ERROR;
6613 goto cleanup;
6617 if (sym->ts.type == BT_CLASS
6618 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6620 m = MATCH_ERROR;
6621 goto cleanup;
6624 if (!gfc_set_array_spec (sym, as, &var_locus))
6626 m = MATCH_ERROR;
6627 goto cleanup;
6630 if (sym->attr.cray_pointee && sym->as != NULL)
6632 /* Fix the array spec. */
6633 m = gfc_mod_pointee_as (sym->as);
6634 if (m == MATCH_ERROR)
6635 goto cleanup;
6638 if (!gfc_add_attribute (&sym->attr, &var_locus))
6640 m = MATCH_ERROR;
6641 goto cleanup;
6644 if ((current_attr.external || current_attr.intrinsic)
6645 && sym->attr.flavor != FL_PROCEDURE
6646 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6648 m = MATCH_ERROR;
6649 goto cleanup;
6652 add_hidden_procptr_result (sym);
6654 return MATCH_YES;
6656 cleanup:
6657 gfc_free_array_spec (as);
6658 return m;
6662 /* Generic attribute declaration subroutine. Used for attributes that
6663 just have a list of names. */
6665 static match
6666 attr_decl (void)
6668 match m;
6670 /* Gobble the optional double colon, by simply ignoring the result
6671 of gfc_match(). */
6672 gfc_match (" ::");
6674 for (;;)
6676 m = attr_decl1 ();
6677 if (m != MATCH_YES)
6678 break;
6680 if (gfc_match_eos () == MATCH_YES)
6682 m = MATCH_YES;
6683 break;
6686 if (gfc_match_char (',') != MATCH_YES)
6688 gfc_error ("Unexpected character in variable list at %C");
6689 m = MATCH_ERROR;
6690 break;
6694 return m;
6698 /* This routine matches Cray Pointer declarations of the form:
6699 pointer ( <pointer>, <pointee> )
6701 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6702 The pointer, if already declared, should be an integer. Otherwise, we
6703 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6704 be either a scalar, or an array declaration. No space is allocated for
6705 the pointee. For the statement
6706 pointer (ipt, ar(10))
6707 any subsequent uses of ar will be translated (in C-notation) as
6708 ar(i) => ((<type> *) ipt)(i)
6709 After gimplification, pointee variable will disappear in the code. */
6711 static match
6712 cray_pointer_decl (void)
6714 match m;
6715 gfc_array_spec *as = NULL;
6716 gfc_symbol *cptr; /* Pointer symbol. */
6717 gfc_symbol *cpte; /* Pointee symbol. */
6718 locus var_locus;
6719 bool done = false;
6721 while (!done)
6723 if (gfc_match_char ('(') != MATCH_YES)
6725 gfc_error ("Expected %<(%> at %C");
6726 return MATCH_ERROR;
6729 /* Match pointer. */
6730 var_locus = gfc_current_locus;
6731 gfc_clear_attr (&current_attr);
6732 gfc_add_cray_pointer (&current_attr, &var_locus);
6733 current_ts.type = BT_INTEGER;
6734 current_ts.kind = gfc_index_integer_kind;
6736 m = gfc_match_symbol (&cptr, 0);
6737 if (m != MATCH_YES)
6739 gfc_error ("Expected variable name at %C");
6740 return m;
6743 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6744 return MATCH_ERROR;
6746 gfc_set_sym_referenced (cptr);
6748 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6750 cptr->ts.type = BT_INTEGER;
6751 cptr->ts.kind = gfc_index_integer_kind;
6753 else if (cptr->ts.type != BT_INTEGER)
6755 gfc_error ("Cray pointer at %C must be an integer");
6756 return MATCH_ERROR;
6758 else if (cptr->ts.kind < gfc_index_integer_kind)
6759 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6760 " memory addresses require %d bytes",
6761 cptr->ts.kind, gfc_index_integer_kind);
6763 if (gfc_match_char (',') != MATCH_YES)
6765 gfc_error ("Expected \",\" at %C");
6766 return MATCH_ERROR;
6769 /* Match Pointee. */
6770 var_locus = gfc_current_locus;
6771 gfc_clear_attr (&current_attr);
6772 gfc_add_cray_pointee (&current_attr, &var_locus);
6773 current_ts.type = BT_UNKNOWN;
6774 current_ts.kind = 0;
6776 m = gfc_match_symbol (&cpte, 0);
6777 if (m != MATCH_YES)
6779 gfc_error ("Expected variable name at %C");
6780 return m;
6783 /* Check for an optional array spec. */
6784 m = gfc_match_array_spec (&as, true, false);
6785 if (m == MATCH_ERROR)
6787 gfc_free_array_spec (as);
6788 return m;
6790 else if (m == MATCH_NO)
6792 gfc_free_array_spec (as);
6793 as = NULL;
6796 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6797 return MATCH_ERROR;
6799 gfc_set_sym_referenced (cpte);
6801 if (cpte->as == NULL)
6803 if (!gfc_set_array_spec (cpte, as, &var_locus))
6804 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6806 else if (as != NULL)
6808 gfc_error ("Duplicate array spec for Cray pointee at %C");
6809 gfc_free_array_spec (as);
6810 return MATCH_ERROR;
6813 as = NULL;
6815 if (cpte->as != NULL)
6817 /* Fix array spec. */
6818 m = gfc_mod_pointee_as (cpte->as);
6819 if (m == MATCH_ERROR)
6820 return m;
6823 /* Point the Pointee at the Pointer. */
6824 cpte->cp_pointer = cptr;
6826 if (gfc_match_char (')') != MATCH_YES)
6828 gfc_error ("Expected \")\" at %C");
6829 return MATCH_ERROR;
6831 m = gfc_match_char (',');
6832 if (m != MATCH_YES)
6833 done = true; /* Stop searching for more declarations. */
6837 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6838 || gfc_match_eos () != MATCH_YES)
6840 gfc_error ("Expected %<,%> or end of statement at %C");
6841 return MATCH_ERROR;
6843 return MATCH_YES;
6847 match
6848 gfc_match_external (void)
6851 gfc_clear_attr (&current_attr);
6852 current_attr.external = 1;
6854 return attr_decl ();
6858 match
6859 gfc_match_intent (void)
6861 sym_intent intent;
6863 /* This is not allowed within a BLOCK construct! */
6864 if (gfc_current_state () == COMP_BLOCK)
6866 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6867 return MATCH_ERROR;
6870 intent = match_intent_spec ();
6871 if (intent == INTENT_UNKNOWN)
6872 return MATCH_ERROR;
6874 gfc_clear_attr (&current_attr);
6875 current_attr.intent = intent;
6877 return attr_decl ();
6881 match
6882 gfc_match_intrinsic (void)
6885 gfc_clear_attr (&current_attr);
6886 current_attr.intrinsic = 1;
6888 return attr_decl ();
6892 match
6893 gfc_match_optional (void)
6895 /* This is not allowed within a BLOCK construct! */
6896 if (gfc_current_state () == COMP_BLOCK)
6898 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6899 return MATCH_ERROR;
6902 gfc_clear_attr (&current_attr);
6903 current_attr.optional = 1;
6905 return attr_decl ();
6909 match
6910 gfc_match_pointer (void)
6912 gfc_gobble_whitespace ();
6913 if (gfc_peek_ascii_char () == '(')
6915 if (!flag_cray_pointer)
6917 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6918 "flag");
6919 return MATCH_ERROR;
6921 return cray_pointer_decl ();
6923 else
6925 gfc_clear_attr (&current_attr);
6926 current_attr.pointer = 1;
6928 return attr_decl ();
6933 match
6934 gfc_match_allocatable (void)
6936 gfc_clear_attr (&current_attr);
6937 current_attr.allocatable = 1;
6939 return attr_decl ();
6943 match
6944 gfc_match_codimension (void)
6946 gfc_clear_attr (&current_attr);
6947 current_attr.codimension = 1;
6949 return attr_decl ();
6953 match
6954 gfc_match_contiguous (void)
6956 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
6957 return MATCH_ERROR;
6959 gfc_clear_attr (&current_attr);
6960 current_attr.contiguous = 1;
6962 return attr_decl ();
6966 match
6967 gfc_match_dimension (void)
6969 gfc_clear_attr (&current_attr);
6970 current_attr.dimension = 1;
6972 return attr_decl ();
6976 match
6977 gfc_match_target (void)
6979 gfc_clear_attr (&current_attr);
6980 current_attr.target = 1;
6982 return attr_decl ();
6986 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6987 statement. */
6989 static match
6990 access_attr_decl (gfc_statement st)
6992 char name[GFC_MAX_SYMBOL_LEN + 1];
6993 interface_type type;
6994 gfc_user_op *uop;
6995 gfc_symbol *sym, *dt_sym;
6996 gfc_intrinsic_op op;
6997 match m;
6999 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7000 goto done;
7002 for (;;)
7004 m = gfc_match_generic_spec (&type, name, &op);
7005 if (m == MATCH_NO)
7006 goto syntax;
7007 if (m == MATCH_ERROR)
7008 return MATCH_ERROR;
7010 switch (type)
7012 case INTERFACE_NAMELESS:
7013 case INTERFACE_ABSTRACT:
7014 goto syntax;
7016 case INTERFACE_GENERIC:
7017 if (gfc_get_symbol (name, NULL, &sym))
7018 goto done;
7020 if (!gfc_add_access (&sym->attr,
7021 (st == ST_PUBLIC)
7022 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7023 sym->name, NULL))
7024 return MATCH_ERROR;
7026 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7027 && !gfc_add_access (&dt_sym->attr,
7028 (st == ST_PUBLIC)
7029 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7030 sym->name, NULL))
7031 return MATCH_ERROR;
7033 break;
7035 case INTERFACE_INTRINSIC_OP:
7036 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7038 gfc_intrinsic_op other_op;
7040 gfc_current_ns->operator_access[op] =
7041 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7043 /* Handle the case if there is another op with the same
7044 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7045 other_op = gfc_equivalent_op (op);
7047 if (other_op != INTRINSIC_NONE)
7048 gfc_current_ns->operator_access[other_op] =
7049 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7052 else
7054 gfc_error ("Access specification of the %s operator at %C has "
7055 "already been specified", gfc_op2string (op));
7056 goto done;
7059 break;
7061 case INTERFACE_USER_OP:
7062 uop = gfc_get_uop (name);
7064 if (uop->access == ACCESS_UNKNOWN)
7066 uop->access = (st == ST_PUBLIC)
7067 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7069 else
7071 gfc_error ("Access specification of the .%s. operator at %C "
7072 "has already been specified", sym->name);
7073 goto done;
7076 break;
7079 if (gfc_match_char (',') == MATCH_NO)
7080 break;
7083 if (gfc_match_eos () != MATCH_YES)
7084 goto syntax;
7085 return MATCH_YES;
7087 syntax:
7088 gfc_syntax_error (st);
7090 done:
7091 return MATCH_ERROR;
7095 match
7096 gfc_match_protected (void)
7098 gfc_symbol *sym;
7099 match m;
7101 if (!gfc_current_ns->proc_name
7102 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7104 gfc_error ("PROTECTED at %C only allowed in specification "
7105 "part of a module");
7106 return MATCH_ERROR;
7110 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7111 return MATCH_ERROR;
7113 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7115 return MATCH_ERROR;
7118 if (gfc_match_eos () == MATCH_YES)
7119 goto syntax;
7121 for(;;)
7123 m = gfc_match_symbol (&sym, 0);
7124 switch (m)
7126 case MATCH_YES:
7127 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7128 return MATCH_ERROR;
7129 goto next_item;
7131 case MATCH_NO:
7132 break;
7134 case MATCH_ERROR:
7135 return MATCH_ERROR;
7138 next_item:
7139 if (gfc_match_eos () == MATCH_YES)
7140 break;
7141 if (gfc_match_char (',') != MATCH_YES)
7142 goto syntax;
7145 return MATCH_YES;
7147 syntax:
7148 gfc_error ("Syntax error in PROTECTED statement at %C");
7149 return MATCH_ERROR;
7153 /* The PRIVATE statement is a bit weird in that it can be an attribute
7154 declaration, but also works as a standalone statement inside of a
7155 type declaration or a module. */
7157 match
7158 gfc_match_private (gfc_statement *st)
7161 if (gfc_match ("private") != MATCH_YES)
7162 return MATCH_NO;
7164 if (gfc_current_state () != COMP_MODULE
7165 && !(gfc_current_state () == COMP_DERIVED
7166 && gfc_state_stack->previous
7167 && gfc_state_stack->previous->state == COMP_MODULE)
7168 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7169 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7170 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7172 gfc_error ("PRIVATE statement at %C is only allowed in the "
7173 "specification part of a module");
7174 return MATCH_ERROR;
7177 if (gfc_current_state () == COMP_DERIVED)
7179 if (gfc_match_eos () == MATCH_YES)
7181 *st = ST_PRIVATE;
7182 return MATCH_YES;
7185 gfc_syntax_error (ST_PRIVATE);
7186 return MATCH_ERROR;
7189 if (gfc_match_eos () == MATCH_YES)
7191 *st = ST_PRIVATE;
7192 return MATCH_YES;
7195 *st = ST_ATTR_DECL;
7196 return access_attr_decl (ST_PRIVATE);
7200 match
7201 gfc_match_public (gfc_statement *st)
7204 if (gfc_match ("public") != MATCH_YES)
7205 return MATCH_NO;
7207 if (gfc_current_state () != COMP_MODULE)
7209 gfc_error ("PUBLIC statement at %C is only allowed in the "
7210 "specification part of a module");
7211 return MATCH_ERROR;
7214 if (gfc_match_eos () == MATCH_YES)
7216 *st = ST_PUBLIC;
7217 return MATCH_YES;
7220 *st = ST_ATTR_DECL;
7221 return access_attr_decl (ST_PUBLIC);
7225 /* Workhorse for gfc_match_parameter. */
7227 static match
7228 do_parm (void)
7230 gfc_symbol *sym;
7231 gfc_expr *init;
7232 match m;
7233 bool t;
7235 m = gfc_match_symbol (&sym, 0);
7236 if (m == MATCH_NO)
7237 gfc_error ("Expected variable name at %C in PARAMETER statement");
7239 if (m != MATCH_YES)
7240 return m;
7242 if (gfc_match_char ('=') == MATCH_NO)
7244 gfc_error ("Expected = sign in PARAMETER statement at %C");
7245 return MATCH_ERROR;
7248 m = gfc_match_init_expr (&init);
7249 if (m == MATCH_NO)
7250 gfc_error ("Expected expression at %C in PARAMETER statement");
7251 if (m != MATCH_YES)
7252 return m;
7254 if (sym->ts.type == BT_UNKNOWN
7255 && !gfc_set_default_type (sym, 1, NULL))
7257 m = MATCH_ERROR;
7258 goto cleanup;
7261 if (!gfc_check_assign_symbol (sym, NULL, init)
7262 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7264 m = MATCH_ERROR;
7265 goto cleanup;
7268 if (sym->value)
7270 gfc_error ("Initializing already initialized variable at %C");
7271 m = MATCH_ERROR;
7272 goto cleanup;
7275 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7276 return (t) ? MATCH_YES : MATCH_ERROR;
7278 cleanup:
7279 gfc_free_expr (init);
7280 return m;
7284 /* Match a parameter statement, with the weird syntax that these have. */
7286 match
7287 gfc_match_parameter (void)
7289 match m;
7291 if (gfc_match_char ('(') == MATCH_NO)
7292 return MATCH_NO;
7294 for (;;)
7296 m = do_parm ();
7297 if (m != MATCH_YES)
7298 break;
7300 if (gfc_match (" )%t") == MATCH_YES)
7301 break;
7303 if (gfc_match_char (',') != MATCH_YES)
7305 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7306 m = MATCH_ERROR;
7307 break;
7311 return m;
7315 /* Save statements have a special syntax. */
7317 match
7318 gfc_match_save (void)
7320 char n[GFC_MAX_SYMBOL_LEN+1];
7321 gfc_common_head *c;
7322 gfc_symbol *sym;
7323 match m;
7325 if (gfc_match_eos () == MATCH_YES)
7327 if (gfc_current_ns->seen_save)
7329 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7330 "follows previous SAVE statement"))
7331 return MATCH_ERROR;
7334 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7335 return MATCH_YES;
7338 if (gfc_current_ns->save_all)
7340 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7341 "blanket SAVE statement"))
7342 return MATCH_ERROR;
7345 gfc_match (" ::");
7347 for (;;)
7349 m = gfc_match_symbol (&sym, 0);
7350 switch (m)
7352 case MATCH_YES:
7353 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7354 &gfc_current_locus))
7355 return MATCH_ERROR;
7356 goto next_item;
7358 case MATCH_NO:
7359 break;
7361 case MATCH_ERROR:
7362 return MATCH_ERROR;
7365 m = gfc_match (" / %n /", &n);
7366 if (m == MATCH_ERROR)
7367 return MATCH_ERROR;
7368 if (m == MATCH_NO)
7369 goto syntax;
7371 c = gfc_get_common (n, 0);
7372 c->saved = 1;
7374 gfc_current_ns->seen_save = 1;
7376 next_item:
7377 if (gfc_match_eos () == MATCH_YES)
7378 break;
7379 if (gfc_match_char (',') != MATCH_YES)
7380 goto syntax;
7383 return MATCH_YES;
7385 syntax:
7386 gfc_error ("Syntax error in SAVE statement at %C");
7387 return MATCH_ERROR;
7391 match
7392 gfc_match_value (void)
7394 gfc_symbol *sym;
7395 match m;
7397 /* This is not allowed within a BLOCK construct! */
7398 if (gfc_current_state () == COMP_BLOCK)
7400 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7401 return MATCH_ERROR;
7404 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7405 return MATCH_ERROR;
7407 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7409 return MATCH_ERROR;
7412 if (gfc_match_eos () == MATCH_YES)
7413 goto syntax;
7415 for(;;)
7417 m = gfc_match_symbol (&sym, 0);
7418 switch (m)
7420 case MATCH_YES:
7421 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7422 return MATCH_ERROR;
7423 goto next_item;
7425 case MATCH_NO:
7426 break;
7428 case MATCH_ERROR:
7429 return MATCH_ERROR;
7432 next_item:
7433 if (gfc_match_eos () == MATCH_YES)
7434 break;
7435 if (gfc_match_char (',') != MATCH_YES)
7436 goto syntax;
7439 return MATCH_YES;
7441 syntax:
7442 gfc_error ("Syntax error in VALUE statement at %C");
7443 return MATCH_ERROR;
7447 match
7448 gfc_match_volatile (void)
7450 gfc_symbol *sym;
7451 match m;
7453 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7454 return MATCH_ERROR;
7456 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7458 return MATCH_ERROR;
7461 if (gfc_match_eos () == MATCH_YES)
7462 goto syntax;
7464 for(;;)
7466 /* VOLATILE is special because it can be added to host-associated
7467 symbols locally. Except for coarrays. */
7468 m = gfc_match_symbol (&sym, 1);
7469 switch (m)
7471 case MATCH_YES:
7472 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7473 for variable in a BLOCK which is defined outside of the BLOCK. */
7474 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7476 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7477 "%C, which is use-/host-associated", sym->name);
7478 return MATCH_ERROR;
7480 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7481 return MATCH_ERROR;
7482 goto next_item;
7484 case MATCH_NO:
7485 break;
7487 case MATCH_ERROR:
7488 return MATCH_ERROR;
7491 next_item:
7492 if (gfc_match_eos () == MATCH_YES)
7493 break;
7494 if (gfc_match_char (',') != MATCH_YES)
7495 goto syntax;
7498 return MATCH_YES;
7500 syntax:
7501 gfc_error ("Syntax error in VOLATILE statement at %C");
7502 return MATCH_ERROR;
7506 match
7507 gfc_match_asynchronous (void)
7509 gfc_symbol *sym;
7510 match m;
7512 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7513 return MATCH_ERROR;
7515 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7517 return MATCH_ERROR;
7520 if (gfc_match_eos () == MATCH_YES)
7521 goto syntax;
7523 for(;;)
7525 /* ASYNCHRONOUS is special because it can be added to host-associated
7526 symbols locally. */
7527 m = gfc_match_symbol (&sym, 1);
7528 switch (m)
7530 case MATCH_YES:
7531 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7532 return MATCH_ERROR;
7533 goto next_item;
7535 case MATCH_NO:
7536 break;
7538 case MATCH_ERROR:
7539 return MATCH_ERROR;
7542 next_item:
7543 if (gfc_match_eos () == MATCH_YES)
7544 break;
7545 if (gfc_match_char (',') != MATCH_YES)
7546 goto syntax;
7549 return MATCH_YES;
7551 syntax:
7552 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7553 return MATCH_ERROR;
7557 /* Match a module procedure statement in a submodule. */
7559 match
7560 gfc_match_submod_proc (void)
7562 char name[GFC_MAX_SYMBOL_LEN + 1];
7563 gfc_symbol *sym, *fsym;
7564 match m;
7565 gfc_formal_arglist *formal, *head, *tail;
7567 if (gfc_current_state () != COMP_CONTAINS
7568 || !(gfc_state_stack->previous
7569 && gfc_state_stack->previous->state == COMP_SUBMODULE))
7570 return MATCH_NO;
7572 m = gfc_match (" module% procedure% %n", name);
7573 if (m != MATCH_YES)
7574 return m;
7576 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
7577 "at %C"))
7578 return MATCH_ERROR;
7580 if (get_proc_name (name, &sym, false))
7581 return MATCH_ERROR;
7583 /* Make sure that the result field is appropriately filled, even though
7584 the result symbol will be replaced later on. */
7585 if (sym->ts.interface->attr.function)
7587 if (sym->ts.interface->result
7588 && sym->ts.interface->result != sym->ts.interface)
7589 sym->result= sym->ts.interface->result;
7590 else
7591 sym->result = sym;
7594 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7595 the symbol existed before. */
7596 sym->declared_at = gfc_current_locus;
7598 if (!sym->attr.module_procedure)
7599 return MATCH_ERROR;
7601 /* Signal match_end to expect "end procedure". */
7602 sym->abr_modproc_decl = 1;
7604 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7605 sym->attr.if_source = IFSRC_DECL;
7607 gfc_new_block = sym;
7609 /* Make a new formal arglist with the symbols in the procedure
7610 namespace. */
7611 head = tail = NULL;
7612 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
7614 if (formal == sym->formal)
7615 head = tail = gfc_get_formal_arglist ();
7616 else
7618 tail->next = gfc_get_formal_arglist ();
7619 tail = tail->next;
7622 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
7623 goto cleanup;
7625 tail->sym = fsym;
7626 gfc_set_sym_referenced (fsym);
7629 /* The dummy symbols get cleaned up, when the formal_namespace of the
7630 interface declaration is cleared. This allows us to add the
7631 explicit interface as is done for other type of procedure. */
7632 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
7633 &gfc_current_locus))
7634 return MATCH_ERROR;
7636 if (gfc_match_eos () != MATCH_YES)
7638 gfc_syntax_error (ST_MODULE_PROC);
7639 return MATCH_ERROR;
7642 return MATCH_YES;
7644 cleanup:
7645 gfc_free_formal_arglist (head);
7646 return MATCH_ERROR;
7650 /* Match a module procedure statement. Note that we have to modify
7651 symbols in the parent's namespace because the current one was there
7652 to receive symbols that are in an interface's formal argument list. */
7654 match
7655 gfc_match_modproc (void)
7657 char name[GFC_MAX_SYMBOL_LEN + 1];
7658 gfc_symbol *sym;
7659 match m;
7660 locus old_locus;
7661 gfc_namespace *module_ns;
7662 gfc_interface *old_interface_head, *interface;
7664 if (gfc_state_stack->state != COMP_INTERFACE
7665 || gfc_state_stack->previous == NULL
7666 || current_interface.type == INTERFACE_NAMELESS
7667 || current_interface.type == INTERFACE_ABSTRACT)
7669 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7670 "interface");
7671 return MATCH_ERROR;
7674 module_ns = gfc_current_ns->parent;
7675 for (; module_ns; module_ns = module_ns->parent)
7676 if (module_ns->proc_name->attr.flavor == FL_MODULE
7677 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7678 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7679 && !module_ns->proc_name->attr.contained))
7680 break;
7682 if (module_ns == NULL)
7683 return MATCH_ERROR;
7685 /* Store the current state of the interface. We will need it if we
7686 end up with a syntax error and need to recover. */
7687 old_interface_head = gfc_current_interface_head ();
7689 /* Check if the F2008 optional double colon appears. */
7690 gfc_gobble_whitespace ();
7691 old_locus = gfc_current_locus;
7692 if (gfc_match ("::") == MATCH_YES)
7694 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7695 "MODULE PROCEDURE statement at %L", &old_locus))
7696 return MATCH_ERROR;
7698 else
7699 gfc_current_locus = old_locus;
7701 for (;;)
7703 bool last = false;
7704 old_locus = gfc_current_locus;
7706 m = gfc_match_name (name);
7707 if (m == MATCH_NO)
7708 goto syntax;
7709 if (m != MATCH_YES)
7710 return MATCH_ERROR;
7712 /* Check for syntax error before starting to add symbols to the
7713 current namespace. */
7714 if (gfc_match_eos () == MATCH_YES)
7715 last = true;
7717 if (!last && gfc_match_char (',') != MATCH_YES)
7718 goto syntax;
7720 /* Now we're sure the syntax is valid, we process this item
7721 further. */
7722 if (gfc_get_symbol (name, module_ns, &sym))
7723 return MATCH_ERROR;
7725 if (sym->attr.intrinsic)
7727 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7728 "PROCEDURE", &old_locus);
7729 return MATCH_ERROR;
7732 if (sym->attr.proc != PROC_MODULE
7733 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7734 return MATCH_ERROR;
7736 if (!gfc_add_interface (sym))
7737 return MATCH_ERROR;
7739 sym->attr.mod_proc = 1;
7740 sym->declared_at = old_locus;
7742 if (last)
7743 break;
7746 return MATCH_YES;
7748 syntax:
7749 /* Restore the previous state of the interface. */
7750 interface = gfc_current_interface_head ();
7751 gfc_set_current_interface_head (old_interface_head);
7753 /* Free the new interfaces. */
7754 while (interface != old_interface_head)
7756 gfc_interface *i = interface->next;
7757 free (interface);
7758 interface = i;
7761 /* And issue a syntax error. */
7762 gfc_syntax_error (ST_MODULE_PROC);
7763 return MATCH_ERROR;
7767 /* Check a derived type that is being extended. */
7769 static gfc_symbol*
7770 check_extended_derived_type (char *name)
7772 gfc_symbol *extended;
7774 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7776 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7777 return NULL;
7780 extended = gfc_find_dt_in_generic (extended);
7782 /* F08:C428. */
7783 if (!extended)
7785 gfc_error ("Symbol %qs at %C has not been previously defined", name);
7786 return NULL;
7789 if (extended->attr.flavor != FL_DERIVED)
7791 gfc_error ("%qs in EXTENDS expression at %C is not a "
7792 "derived type", name);
7793 return NULL;
7796 if (extended->attr.is_bind_c)
7798 gfc_error ("%qs cannot be extended at %C because it "
7799 "is BIND(C)", extended->name);
7800 return NULL;
7803 if (extended->attr.sequence)
7805 gfc_error ("%qs cannot be extended at %C because it "
7806 "is a SEQUENCE type", extended->name);
7807 return NULL;
7810 return extended;
7814 /* Match the optional attribute specifiers for a type declaration.
7815 Return MATCH_ERROR if an error is encountered in one of the handled
7816 attributes (public, private, bind(c)), MATCH_NO if what's found is
7817 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7818 checking on attribute conflicts needs to be done. */
7820 match
7821 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7823 /* See if the derived type is marked as private. */
7824 if (gfc_match (" , private") == MATCH_YES)
7826 if (gfc_current_state () != COMP_MODULE)
7828 gfc_error ("Derived type at %C can only be PRIVATE in the "
7829 "specification part of a module");
7830 return MATCH_ERROR;
7833 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7834 return MATCH_ERROR;
7836 else if (gfc_match (" , public") == MATCH_YES)
7838 if (gfc_current_state () != COMP_MODULE)
7840 gfc_error ("Derived type at %C can only be PUBLIC in the "
7841 "specification part of a module");
7842 return MATCH_ERROR;
7845 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7846 return MATCH_ERROR;
7848 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7850 /* If the type is defined to be bind(c) it then needs to make
7851 sure that all fields are interoperable. This will
7852 need to be a semantic check on the finished derived type.
7853 See 15.2.3 (lines 9-12) of F2003 draft. */
7854 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7855 return MATCH_ERROR;
7857 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7859 else if (gfc_match (" , abstract") == MATCH_YES)
7861 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7862 return MATCH_ERROR;
7864 if (!gfc_add_abstract (attr, &gfc_current_locus))
7865 return MATCH_ERROR;
7867 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7869 if (!gfc_add_extension (attr, &gfc_current_locus))
7870 return MATCH_ERROR;
7872 else
7873 return MATCH_NO;
7875 /* If we get here, something matched. */
7876 return MATCH_YES;
7880 /* Match the beginning of a derived type declaration. If a type name
7881 was the result of a function, then it is possible to have a symbol
7882 already to be known as a derived type yet have no components. */
7884 match
7885 gfc_match_derived_decl (void)
7887 char name[GFC_MAX_SYMBOL_LEN + 1];
7888 char parent[GFC_MAX_SYMBOL_LEN + 1];
7889 symbol_attribute attr;
7890 gfc_symbol *sym, *gensym;
7891 gfc_symbol *extended;
7892 match m;
7893 match is_type_attr_spec = MATCH_NO;
7894 bool seen_attr = false;
7895 gfc_interface *intr = NULL, *head;
7897 if (gfc_current_state () == COMP_DERIVED)
7898 return MATCH_NO;
7900 name[0] = '\0';
7901 parent[0] = '\0';
7902 gfc_clear_attr (&attr);
7903 extended = NULL;
7907 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7908 if (is_type_attr_spec == MATCH_ERROR)
7909 return MATCH_ERROR;
7910 if (is_type_attr_spec == MATCH_YES)
7911 seen_attr = true;
7912 } while (is_type_attr_spec == MATCH_YES);
7914 /* Deal with derived type extensions. The extension attribute has
7915 been added to 'attr' but now the parent type must be found and
7916 checked. */
7917 if (parent[0])
7918 extended = check_extended_derived_type (parent);
7920 if (parent[0] && !extended)
7921 return MATCH_ERROR;
7923 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7925 gfc_error ("Expected :: in TYPE definition at %C");
7926 return MATCH_ERROR;
7929 m = gfc_match (" %n%t", name);
7930 if (m != MATCH_YES)
7931 return m;
7933 /* Make sure the name is not the name of an intrinsic type. */
7934 if (gfc_is_intrinsic_typename (name))
7936 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
7937 "type", name);
7938 return MATCH_ERROR;
7941 if (gfc_get_symbol (name, NULL, &gensym))
7942 return MATCH_ERROR;
7944 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7946 gfc_error ("Derived type name %qs at %C already has a basic type "
7947 "of %s", gensym->name, gfc_typename (&gensym->ts));
7948 return MATCH_ERROR;
7951 if (!gensym->attr.generic
7952 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
7953 return MATCH_ERROR;
7955 if (!gensym->attr.function
7956 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
7957 return MATCH_ERROR;
7959 sym = gfc_find_dt_in_generic (gensym);
7961 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7963 gfc_error ("Derived type definition of %qs at %C has already been "
7964 "defined", sym->name);
7965 return MATCH_ERROR;
7968 if (!sym)
7970 /* Use upper case to save the actual derived-type symbol. */
7971 gfc_get_symbol (gfc_get_string ("%c%s",
7972 (char) TOUPPER ((unsigned char) gensym->name[0]),
7973 &gensym->name[1]), NULL, &sym);
7974 sym->name = gfc_get_string (gensym->name);
7975 head = gensym->generic;
7976 intr = gfc_get_interface ();
7977 intr->sym = sym;
7978 intr->where = gfc_current_locus;
7979 intr->sym->declared_at = gfc_current_locus;
7980 intr->next = head;
7981 gensym->generic = intr;
7982 gensym->attr.if_source = IFSRC_DECL;
7985 /* The symbol may already have the derived attribute without the
7986 components. The ways this can happen is via a function
7987 definition, an INTRINSIC statement or a subtype in another
7988 derived type that is a pointer. The first part of the AND clause
7989 is true if the symbol is not the return value of a function. */
7990 if (sym->attr.flavor != FL_DERIVED
7991 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
7992 return MATCH_ERROR;
7994 if (attr.access != ACCESS_UNKNOWN
7995 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
7996 return MATCH_ERROR;
7997 else if (sym->attr.access == ACCESS_UNKNOWN
7998 && gensym->attr.access != ACCESS_UNKNOWN
7999 && !gfc_add_access (&sym->attr, gensym->attr.access,
8000 sym->name, NULL))
8001 return MATCH_ERROR;
8003 if (sym->attr.access != ACCESS_UNKNOWN
8004 && gensym->attr.access == ACCESS_UNKNOWN)
8005 gensym->attr.access = sym->attr.access;
8007 /* See if the derived type was labeled as bind(c). */
8008 if (attr.is_bind_c != 0)
8009 sym->attr.is_bind_c = attr.is_bind_c;
8011 /* Construct the f2k_derived namespace if it is not yet there. */
8012 if (!sym->f2k_derived)
8013 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8015 if (extended && !sym->components)
8017 gfc_component *p;
8019 /* Add the extended derived type as the first component. */
8020 gfc_add_component (sym, parent, &p);
8021 extended->refs++;
8022 gfc_set_sym_referenced (extended);
8024 p->ts.type = BT_DERIVED;
8025 p->ts.u.derived = extended;
8026 p->initializer = gfc_default_initializer (&p->ts);
8028 /* Set extension level. */
8029 if (extended->attr.extension == 255)
8031 /* Since the extension field is 8 bit wide, we can only have
8032 up to 255 extension levels. */
8033 gfc_error ("Maximum extension level reached with type %qs at %L",
8034 extended->name, &extended->declared_at);
8035 return MATCH_ERROR;
8037 sym->attr.extension = extended->attr.extension + 1;
8039 /* Provide the links between the extended type and its extension. */
8040 if (!extended->f2k_derived)
8041 extended->f2k_derived = gfc_get_namespace (NULL, 0);
8044 if (!sym->hash_value)
8045 /* Set the hash for the compound name for this type. */
8046 sym->hash_value = gfc_hash_value (sym);
8048 /* Take over the ABSTRACT attribute. */
8049 sym->attr.abstract = attr.abstract;
8051 gfc_new_block = sym;
8053 return MATCH_YES;
8057 /* Cray Pointees can be declared as:
8058 pointer (ipt, a (n,m,...,*)) */
8060 match
8061 gfc_mod_pointee_as (gfc_array_spec *as)
8063 as->cray_pointee = true; /* This will be useful to know later. */
8064 if (as->type == AS_ASSUMED_SIZE)
8065 as->cp_was_assumed = true;
8066 else if (as->type == AS_ASSUMED_SHAPE)
8068 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8069 return MATCH_ERROR;
8071 return MATCH_YES;
8075 /* Match the enum definition statement, here we are trying to match
8076 the first line of enum definition statement.
8077 Returns MATCH_YES if match is found. */
8079 match
8080 gfc_match_enum (void)
8082 match m;
8084 m = gfc_match_eos ();
8085 if (m != MATCH_YES)
8086 return m;
8088 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
8089 return MATCH_ERROR;
8091 return MATCH_YES;
8095 /* Returns an initializer whose value is one higher than the value of the
8096 LAST_INITIALIZER argument. If the argument is NULL, the
8097 initializers value will be set to zero. The initializer's kind
8098 will be set to gfc_c_int_kind.
8100 If -fshort-enums is given, the appropriate kind will be selected
8101 later after all enumerators have been parsed. A warning is issued
8102 here if an initializer exceeds gfc_c_int_kind. */
8104 static gfc_expr *
8105 enum_initializer (gfc_expr *last_initializer, locus where)
8107 gfc_expr *result;
8108 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
8110 mpz_init (result->value.integer);
8112 if (last_initializer != NULL)
8114 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8115 result->where = last_initializer->where;
8117 if (gfc_check_integer_range (result->value.integer,
8118 gfc_c_int_kind) != ARITH_OK)
8120 gfc_error ("Enumerator exceeds the C integer type at %C");
8121 return NULL;
8124 else
8126 /* Control comes here, if it's the very first enumerator and no
8127 initializer has been given. It will be initialized to zero. */
8128 mpz_set_si (result->value.integer, 0);
8131 return result;
8135 /* Match a variable name with an optional initializer. When this
8136 subroutine is called, a variable is expected to be parsed next.
8137 Depending on what is happening at the moment, updates either the
8138 symbol table or the current interface. */
8140 static match
8141 enumerator_decl (void)
8143 char name[GFC_MAX_SYMBOL_LEN + 1];
8144 gfc_expr *initializer;
8145 gfc_array_spec *as = NULL;
8146 gfc_symbol *sym;
8147 locus var_locus;
8148 match m;
8149 bool t;
8150 locus old_locus;
8152 initializer = NULL;
8153 old_locus = gfc_current_locus;
8155 /* When we get here, we've just matched a list of attributes and
8156 maybe a type and a double colon. The next thing we expect to see
8157 is the name of the symbol. */
8158 m = gfc_match_name (name);
8159 if (m != MATCH_YES)
8160 goto cleanup;
8162 var_locus = gfc_current_locus;
8164 /* OK, we've successfully matched the declaration. Now put the
8165 symbol in the current namespace. If we fail to create the symbol,
8166 bail out. */
8167 if (!build_sym (name, NULL, false, &as, &var_locus))
8169 m = MATCH_ERROR;
8170 goto cleanup;
8173 /* The double colon must be present in order to have initializers.
8174 Otherwise the statement is ambiguous with an assignment statement. */
8175 if (colon_seen)
8177 if (gfc_match_char ('=') == MATCH_YES)
8179 m = gfc_match_init_expr (&initializer);
8180 if (m == MATCH_NO)
8182 gfc_error ("Expected an initialization expression at %C");
8183 m = MATCH_ERROR;
8186 if (m != MATCH_YES)
8187 goto cleanup;
8191 /* If we do not have an initializer, the initialization value of the
8192 previous enumerator (stored in last_initializer) is incremented
8193 by 1 and is used to initialize the current enumerator. */
8194 if (initializer == NULL)
8195 initializer = enum_initializer (last_initializer, old_locus);
8197 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8199 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8200 &var_locus);
8201 m = MATCH_ERROR;
8202 goto cleanup;
8205 /* Store this current initializer, for the next enumerator variable
8206 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8207 use last_initializer below. */
8208 last_initializer = initializer;
8209 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8211 /* Maintain enumerator history. */
8212 gfc_find_symbol (name, NULL, 0, &sym);
8213 create_enum_history (sym, last_initializer);
8215 return (t) ? MATCH_YES : MATCH_ERROR;
8217 cleanup:
8218 /* Free stuff up and return. */
8219 gfc_free_expr (initializer);
8221 return m;
8225 /* Match the enumerator definition statement. */
8227 match
8228 gfc_match_enumerator_def (void)
8230 match m;
8231 bool t;
8233 gfc_clear_ts (&current_ts);
8235 m = gfc_match (" enumerator");
8236 if (m != MATCH_YES)
8237 return m;
8239 m = gfc_match (" :: ");
8240 if (m == MATCH_ERROR)
8241 return m;
8243 colon_seen = (m == MATCH_YES);
8245 if (gfc_current_state () != COMP_ENUM)
8247 gfc_error ("ENUM definition statement expected before %C");
8248 gfc_free_enum_history ();
8249 return MATCH_ERROR;
8252 (&current_ts)->type = BT_INTEGER;
8253 (&current_ts)->kind = gfc_c_int_kind;
8255 gfc_clear_attr (&current_attr);
8256 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
8257 if (!t)
8259 m = MATCH_ERROR;
8260 goto cleanup;
8263 for (;;)
8265 m = enumerator_decl ();
8266 if (m == MATCH_ERROR)
8268 gfc_free_enum_history ();
8269 goto cleanup;
8271 if (m == MATCH_NO)
8272 break;
8274 if (gfc_match_eos () == MATCH_YES)
8275 goto cleanup;
8276 if (gfc_match_char (',') != MATCH_YES)
8277 break;
8280 if (gfc_current_state () == COMP_ENUM)
8282 gfc_free_enum_history ();
8283 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8284 m = MATCH_ERROR;
8287 cleanup:
8288 gfc_free_array_spec (current_as);
8289 current_as = NULL;
8290 return m;
8295 /* Match binding attributes. */
8297 static match
8298 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
8300 bool found_passing = false;
8301 bool seen_ptr = false;
8302 match m = MATCH_YES;
8304 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8305 this case the defaults are in there. */
8306 ba->access = ACCESS_UNKNOWN;
8307 ba->pass_arg = NULL;
8308 ba->pass_arg_num = 0;
8309 ba->nopass = 0;
8310 ba->non_overridable = 0;
8311 ba->deferred = 0;
8312 ba->ppc = ppc;
8314 /* If we find a comma, we believe there are binding attributes. */
8315 m = gfc_match_char (',');
8316 if (m == MATCH_NO)
8317 goto done;
8321 /* Access specifier. */
8323 m = gfc_match (" public");
8324 if (m == MATCH_ERROR)
8325 goto error;
8326 if (m == MATCH_YES)
8328 if (ba->access != ACCESS_UNKNOWN)
8330 gfc_error ("Duplicate access-specifier at %C");
8331 goto error;
8334 ba->access = ACCESS_PUBLIC;
8335 continue;
8338 m = gfc_match (" private");
8339 if (m == MATCH_ERROR)
8340 goto error;
8341 if (m == MATCH_YES)
8343 if (ba->access != ACCESS_UNKNOWN)
8345 gfc_error ("Duplicate access-specifier at %C");
8346 goto error;
8349 ba->access = ACCESS_PRIVATE;
8350 continue;
8353 /* If inside GENERIC, the following is not allowed. */
8354 if (!generic)
8357 /* NOPASS flag. */
8358 m = gfc_match (" nopass");
8359 if (m == MATCH_ERROR)
8360 goto error;
8361 if (m == MATCH_YES)
8363 if (found_passing)
8365 gfc_error ("Binding attributes already specify passing,"
8366 " illegal NOPASS at %C");
8367 goto error;
8370 found_passing = true;
8371 ba->nopass = 1;
8372 continue;
8375 /* PASS possibly including argument. */
8376 m = gfc_match (" pass");
8377 if (m == MATCH_ERROR)
8378 goto error;
8379 if (m == MATCH_YES)
8381 char arg[GFC_MAX_SYMBOL_LEN + 1];
8383 if (found_passing)
8385 gfc_error ("Binding attributes already specify passing,"
8386 " illegal PASS at %C");
8387 goto error;
8390 m = gfc_match (" ( %n )", arg);
8391 if (m == MATCH_ERROR)
8392 goto error;
8393 if (m == MATCH_YES)
8394 ba->pass_arg = gfc_get_string (arg);
8395 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8397 found_passing = true;
8398 ba->nopass = 0;
8399 continue;
8402 if (ppc)
8404 /* POINTER flag. */
8405 m = gfc_match (" pointer");
8406 if (m == MATCH_ERROR)
8407 goto error;
8408 if (m == MATCH_YES)
8410 if (seen_ptr)
8412 gfc_error ("Duplicate POINTER attribute at %C");
8413 goto error;
8416 seen_ptr = true;
8417 continue;
8420 else
8422 /* NON_OVERRIDABLE flag. */
8423 m = gfc_match (" non_overridable");
8424 if (m == MATCH_ERROR)
8425 goto error;
8426 if (m == MATCH_YES)
8428 if (ba->non_overridable)
8430 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8431 goto error;
8434 ba->non_overridable = 1;
8435 continue;
8438 /* DEFERRED flag. */
8439 m = gfc_match (" deferred");
8440 if (m == MATCH_ERROR)
8441 goto error;
8442 if (m == MATCH_YES)
8444 if (ba->deferred)
8446 gfc_error ("Duplicate DEFERRED at %C");
8447 goto error;
8450 ba->deferred = 1;
8451 continue;
8457 /* Nothing matching found. */
8458 if (generic)
8459 gfc_error ("Expected access-specifier at %C");
8460 else
8461 gfc_error ("Expected binding attribute at %C");
8462 goto error;
8464 while (gfc_match_char (',') == MATCH_YES);
8466 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8467 if (ba->non_overridable && ba->deferred)
8469 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8470 goto error;
8473 m = MATCH_YES;
8475 done:
8476 if (ba->access == ACCESS_UNKNOWN)
8477 ba->access = gfc_typebound_default_access;
8479 if (ppc && !seen_ptr)
8481 gfc_error ("POINTER attribute is required for procedure pointer component"
8482 " at %C");
8483 goto error;
8486 return m;
8488 error:
8489 return MATCH_ERROR;
8493 /* Match a PROCEDURE specific binding inside a derived type. */
8495 static match
8496 match_procedure_in_type (void)
8498 char name[GFC_MAX_SYMBOL_LEN + 1];
8499 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8500 char* target = NULL, *ifc = NULL;
8501 gfc_typebound_proc tb;
8502 bool seen_colons;
8503 bool seen_attrs;
8504 match m;
8505 gfc_symtree* stree;
8506 gfc_namespace* ns;
8507 gfc_symbol* block;
8508 int num;
8510 /* Check current state. */
8511 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8512 block = gfc_state_stack->previous->sym;
8513 gcc_assert (block);
8515 /* Try to match PROCEDURE(interface). */
8516 if (gfc_match (" (") == MATCH_YES)
8518 m = gfc_match_name (target_buf);
8519 if (m == MATCH_ERROR)
8520 return m;
8521 if (m != MATCH_YES)
8523 gfc_error ("Interface-name expected after %<(%> at %C");
8524 return MATCH_ERROR;
8527 if (gfc_match (" )") != MATCH_YES)
8529 gfc_error ("%<)%> expected at %C");
8530 return MATCH_ERROR;
8533 ifc = target_buf;
8536 /* Construct the data structure. */
8537 memset (&tb, 0, sizeof (tb));
8538 tb.where = gfc_current_locus;
8540 /* Match binding attributes. */
8541 m = match_binding_attributes (&tb, false, false);
8542 if (m == MATCH_ERROR)
8543 return m;
8544 seen_attrs = (m == MATCH_YES);
8546 /* Check that attribute DEFERRED is given if an interface is specified. */
8547 if (tb.deferred && !ifc)
8549 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8550 return MATCH_ERROR;
8552 if (ifc && !tb.deferred)
8554 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8555 return MATCH_ERROR;
8558 /* Match the colons. */
8559 m = gfc_match (" ::");
8560 if (m == MATCH_ERROR)
8561 return m;
8562 seen_colons = (m == MATCH_YES);
8563 if (seen_attrs && !seen_colons)
8565 gfc_error ("Expected %<::%> after binding-attributes at %C");
8566 return MATCH_ERROR;
8569 /* Match the binding names. */
8570 for(num=1;;num++)
8572 m = gfc_match_name (name);
8573 if (m == MATCH_ERROR)
8574 return m;
8575 if (m == MATCH_NO)
8577 gfc_error ("Expected binding name at %C");
8578 return MATCH_ERROR;
8581 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8582 return MATCH_ERROR;
8584 /* Try to match the '=> target', if it's there. */
8585 target = ifc;
8586 m = gfc_match (" =>");
8587 if (m == MATCH_ERROR)
8588 return m;
8589 if (m == MATCH_YES)
8591 if (tb.deferred)
8593 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8594 return MATCH_ERROR;
8597 if (!seen_colons)
8599 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8600 " at %C");
8601 return MATCH_ERROR;
8604 m = gfc_match_name (target_buf);
8605 if (m == MATCH_ERROR)
8606 return m;
8607 if (m == MATCH_NO)
8609 gfc_error ("Expected binding target after %<=>%> at %C");
8610 return MATCH_ERROR;
8612 target = target_buf;
8615 /* If no target was found, it has the same name as the binding. */
8616 if (!target)
8617 target = name;
8619 /* Get the namespace to insert the symbols into. */
8620 ns = block->f2k_derived;
8621 gcc_assert (ns);
8623 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8624 if (tb.deferred && !block->attr.abstract)
8626 gfc_error ("Type %qs containing DEFERRED binding at %C "
8627 "is not ABSTRACT", block->name);
8628 return MATCH_ERROR;
8631 /* See if we already have a binding with this name in the symtree which
8632 would be an error. If a GENERIC already targeted this binding, it may
8633 be already there but then typebound is still NULL. */
8634 stree = gfc_find_symtree (ns->tb_sym_root, name);
8635 if (stree && stree->n.tb)
8637 gfc_error ("There is already a procedure with binding name %qs for "
8638 "the derived type %qs at %C", name, block->name);
8639 return MATCH_ERROR;
8642 /* Insert it and set attributes. */
8644 if (!stree)
8646 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8647 gcc_assert (stree);
8649 stree->n.tb = gfc_get_typebound_proc (&tb);
8651 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8652 false))
8653 return MATCH_ERROR;
8654 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8656 if (gfc_match_eos () == MATCH_YES)
8657 return MATCH_YES;
8658 if (gfc_match_char (',') != MATCH_YES)
8659 goto syntax;
8662 syntax:
8663 gfc_error ("Syntax error in PROCEDURE statement at %C");
8664 return MATCH_ERROR;
8668 /* Match a GENERIC procedure binding inside a derived type. */
8670 match
8671 gfc_match_generic (void)
8673 char name[GFC_MAX_SYMBOL_LEN + 1];
8674 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8675 gfc_symbol* block;
8676 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8677 gfc_typebound_proc* tb;
8678 gfc_namespace* ns;
8679 interface_type op_type;
8680 gfc_intrinsic_op op;
8681 match m;
8683 /* Check current state. */
8684 if (gfc_current_state () == COMP_DERIVED)
8686 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8687 return MATCH_ERROR;
8689 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8690 return MATCH_NO;
8691 block = gfc_state_stack->previous->sym;
8692 ns = block->f2k_derived;
8693 gcc_assert (block && ns);
8695 memset (&tbattr, 0, sizeof (tbattr));
8696 tbattr.where = gfc_current_locus;
8698 /* See if we get an access-specifier. */
8699 m = match_binding_attributes (&tbattr, true, false);
8700 if (m == MATCH_ERROR)
8701 goto error;
8703 /* Now the colons, those are required. */
8704 if (gfc_match (" ::") != MATCH_YES)
8706 gfc_error ("Expected %<::%> at %C");
8707 goto error;
8710 /* Match the binding name; depending on type (operator / generic) format
8711 it for future error messages into bind_name. */
8713 m = gfc_match_generic_spec (&op_type, name, &op);
8714 if (m == MATCH_ERROR)
8715 return MATCH_ERROR;
8716 if (m == MATCH_NO)
8718 gfc_error ("Expected generic name or operator descriptor at %C");
8719 goto error;
8722 switch (op_type)
8724 case INTERFACE_GENERIC:
8725 snprintf (bind_name, sizeof (bind_name), "%s", name);
8726 break;
8728 case INTERFACE_USER_OP:
8729 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8730 break;
8732 case INTERFACE_INTRINSIC_OP:
8733 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8734 gfc_op2string (op));
8735 break;
8737 case INTERFACE_NAMELESS:
8738 gfc_error ("Malformed GENERIC statement at %C");
8739 goto error;
8740 break;
8742 default:
8743 gcc_unreachable ();
8746 /* Match the required =>. */
8747 if (gfc_match (" =>") != MATCH_YES)
8749 gfc_error ("Expected %<=>%> at %C");
8750 goto error;
8753 /* Try to find existing GENERIC binding with this name / for this operator;
8754 if there is something, check that it is another GENERIC and then extend
8755 it rather than building a new node. Otherwise, create it and put it
8756 at the right position. */
8758 switch (op_type)
8760 case INTERFACE_USER_OP:
8761 case INTERFACE_GENERIC:
8763 const bool is_op = (op_type == INTERFACE_USER_OP);
8764 gfc_symtree* st;
8766 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8767 if (st)
8769 tb = st->n.tb;
8770 gcc_assert (tb);
8772 else
8773 tb = NULL;
8775 break;
8778 case INTERFACE_INTRINSIC_OP:
8779 tb = ns->tb_op[op];
8780 break;
8782 default:
8783 gcc_unreachable ();
8786 if (tb)
8788 if (!tb->is_generic)
8790 gcc_assert (op_type == INTERFACE_GENERIC);
8791 gfc_error ("There's already a non-generic procedure with binding name"
8792 " %qs for the derived type %qs at %C",
8793 bind_name, block->name);
8794 goto error;
8797 if (tb->access != tbattr.access)
8799 gfc_error ("Binding at %C must have the same access as already"
8800 " defined binding %qs", bind_name);
8801 goto error;
8804 else
8806 tb = gfc_get_typebound_proc (NULL);
8807 tb->where = gfc_current_locus;
8808 tb->access = tbattr.access;
8809 tb->is_generic = 1;
8810 tb->u.generic = NULL;
8812 switch (op_type)
8814 case INTERFACE_GENERIC:
8815 case INTERFACE_USER_OP:
8817 const bool is_op = (op_type == INTERFACE_USER_OP);
8818 gfc_symtree* st;
8820 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8821 name);
8822 gcc_assert (st);
8823 st->n.tb = tb;
8825 break;
8828 case INTERFACE_INTRINSIC_OP:
8829 ns->tb_op[op] = tb;
8830 break;
8832 default:
8833 gcc_unreachable ();
8837 /* Now, match all following names as specific targets. */
8840 gfc_symtree* target_st;
8841 gfc_tbp_generic* target;
8843 m = gfc_match_name (name);
8844 if (m == MATCH_ERROR)
8845 goto error;
8846 if (m == MATCH_NO)
8848 gfc_error ("Expected specific binding name at %C");
8849 goto error;
8852 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8854 /* See if this is a duplicate specification. */
8855 for (target = tb->u.generic; target; target = target->next)
8856 if (target_st == target->specific_st)
8858 gfc_error ("%qs already defined as specific binding for the"
8859 " generic %qs at %C", name, bind_name);
8860 goto error;
8863 target = gfc_get_tbp_generic ();
8864 target->specific_st = target_st;
8865 target->specific = NULL;
8866 target->next = tb->u.generic;
8867 target->is_operator = ((op_type == INTERFACE_USER_OP)
8868 || (op_type == INTERFACE_INTRINSIC_OP));
8869 tb->u.generic = target;
8871 while (gfc_match (" ,") == MATCH_YES);
8873 /* Here should be the end. */
8874 if (gfc_match_eos () != MATCH_YES)
8876 gfc_error ("Junk after GENERIC binding at %C");
8877 goto error;
8880 return MATCH_YES;
8882 error:
8883 return MATCH_ERROR;
8887 /* Match a FINAL declaration inside a derived type. */
8889 match
8890 gfc_match_final_decl (void)
8892 char name[GFC_MAX_SYMBOL_LEN + 1];
8893 gfc_symbol* sym;
8894 match m;
8895 gfc_namespace* module_ns;
8896 bool first, last;
8897 gfc_symbol* block;
8899 if (gfc_current_form == FORM_FREE)
8901 char c = gfc_peek_ascii_char ();
8902 if (!gfc_is_whitespace (c) && c != ':')
8903 return MATCH_NO;
8906 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8908 if (gfc_current_form == FORM_FIXED)
8909 return MATCH_NO;
8911 gfc_error ("FINAL declaration at %C must be inside a derived type "
8912 "CONTAINS section");
8913 return MATCH_ERROR;
8916 block = gfc_state_stack->previous->sym;
8917 gcc_assert (block);
8919 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8920 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8922 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8923 " specification part of a MODULE");
8924 return MATCH_ERROR;
8927 module_ns = gfc_current_ns;
8928 gcc_assert (module_ns);
8929 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8931 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8932 if (gfc_match (" ::") == MATCH_ERROR)
8933 return MATCH_ERROR;
8935 /* Match the sequence of procedure names. */
8936 first = true;
8937 last = false;
8940 gfc_finalizer* f;
8942 if (first && gfc_match_eos () == MATCH_YES)
8944 gfc_error ("Empty FINAL at %C");
8945 return MATCH_ERROR;
8948 m = gfc_match_name (name);
8949 if (m == MATCH_NO)
8951 gfc_error ("Expected module procedure name at %C");
8952 return MATCH_ERROR;
8954 else if (m != MATCH_YES)
8955 return MATCH_ERROR;
8957 if (gfc_match_eos () == MATCH_YES)
8958 last = true;
8959 if (!last && gfc_match_char (',') != MATCH_YES)
8961 gfc_error ("Expected %<,%> at %C");
8962 return MATCH_ERROR;
8965 if (gfc_get_symbol (name, module_ns, &sym))
8967 gfc_error ("Unknown procedure name %qs at %C", name);
8968 return MATCH_ERROR;
8971 /* Mark the symbol as module procedure. */
8972 if (sym->attr.proc != PROC_MODULE
8973 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8974 return MATCH_ERROR;
8976 /* Check if we already have this symbol in the list, this is an error. */
8977 for (f = block->f2k_derived->finalizers; f; f = f->next)
8978 if (f->proc_sym == sym)
8980 gfc_error ("%qs at %C is already defined as FINAL procedure!",
8981 name);
8982 return MATCH_ERROR;
8985 /* Add this symbol to the list of finalizers. */
8986 gcc_assert (block->f2k_derived);
8987 ++sym->refs;
8988 f = XCNEW (gfc_finalizer);
8989 f->proc_sym = sym;
8990 f->proc_tree = NULL;
8991 f->where = gfc_current_locus;
8992 f->next = block->f2k_derived->finalizers;
8993 block->f2k_derived->finalizers = f;
8995 first = false;
8997 while (!last);
8999 return MATCH_YES;
9003 const ext_attr_t ext_attr_list[] = {
9004 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9005 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9006 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9007 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9008 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9009 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9010 { NULL, EXT_ATTR_LAST, NULL }
9013 /* Match a !GCC$ ATTRIBUTES statement of the form:
9014 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9015 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9017 TODO: We should support all GCC attributes using the same syntax for
9018 the attribute list, i.e. the list in C
9019 __attributes(( attribute-list ))
9020 matches then
9021 !GCC$ ATTRIBUTES attribute-list ::
9022 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9023 saved into a TREE.
9025 As there is absolutely no risk of confusion, we should never return
9026 MATCH_NO. */
9027 match
9028 gfc_match_gcc_attributes (void)
9030 symbol_attribute attr;
9031 char name[GFC_MAX_SYMBOL_LEN + 1];
9032 unsigned id;
9033 gfc_symbol *sym;
9034 match m;
9036 gfc_clear_attr (&attr);
9037 for(;;)
9039 char ch;
9041 if (gfc_match_name (name) != MATCH_YES)
9042 return MATCH_ERROR;
9044 for (id = 0; id < EXT_ATTR_LAST; id++)
9045 if (strcmp (name, ext_attr_list[id].name) == 0)
9046 break;
9048 if (id == EXT_ATTR_LAST)
9050 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9051 return MATCH_ERROR;
9054 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
9055 return MATCH_ERROR;
9057 gfc_gobble_whitespace ();
9058 ch = gfc_next_ascii_char ();
9059 if (ch == ':')
9061 /* This is the successful exit condition for the loop. */
9062 if (gfc_next_ascii_char () == ':')
9063 break;
9066 if (ch == ',')
9067 continue;
9069 goto syntax;
9072 if (gfc_match_eos () == MATCH_YES)
9073 goto syntax;
9075 for(;;)
9077 m = gfc_match_name (name);
9078 if (m != MATCH_YES)
9079 return m;
9081 if (find_special (name, &sym, true))
9082 return MATCH_ERROR;
9084 sym->attr.ext_attr |= attr.ext_attr;
9086 if (gfc_match_eos () == MATCH_YES)
9087 break;
9089 if (gfc_match_char (',') != MATCH_YES)
9090 goto syntax;
9093 return MATCH_YES;
9095 syntax:
9096 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9097 return MATCH_ERROR;