2015-12-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / decl.c
blobb03dadf6e72bd7ccd62de860279ed414afc85ab9
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 "options.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "stringpool.h"
28 #include "match.h"
29 #include "parse.h"
30 #include "constructor.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts;
54 static symbol_attribute current_attr;
55 static gfc_array_spec *current_as;
56 static int colon_seen;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals = 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr *last_initializer;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
77 gfc_symbol *sym;
78 gfc_expr *initializer;
79 struct enumerator_history *next;
81 enumerator_history;
83 /* Header of enum history chain. */
85 static enumerator_history *enum_history = NULL;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history *max_enum = NULL;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol *gfc_new_block;
95 bool gfc_matching_function;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data = false;
102 bool
103 gfc_in_match_data (void)
105 return in_match_data;
108 static void
109 set_in_match_data (bool set_value)
111 in_match_data = set_value;
114 /* Free a gfc_data_variable structure and everything beneath it. */
116 static void
117 free_variable (gfc_data_variable *p)
119 gfc_data_variable *q;
121 for (; p; p = q)
123 q = p->next;
124 gfc_free_expr (p->expr);
125 gfc_free_iterator (&p->iter, 0);
126 free_variable (p->list);
127 free (p);
132 /* Free a gfc_data_value structure and everything beneath it. */
134 static void
135 free_value (gfc_data_value *p)
137 gfc_data_value *q;
139 for (; p; p = q)
141 q = p->next;
142 mpz_clear (p->repeat);
143 gfc_free_expr (p->expr);
144 free (p);
149 /* Free a list of gfc_data structures. */
151 void
152 gfc_free_data (gfc_data *p)
154 gfc_data *q;
156 for (; p; p = q)
158 q = p->next;
159 free_variable (p->var);
160 free_value (p->value);
161 free (p);
166 /* Free all data in a namespace. */
168 static void
169 gfc_free_data_all (gfc_namespace *ns)
171 gfc_data *d;
173 for (;ns->data;)
175 d = ns->data->next;
176 free (ns->data);
177 ns->data = d;
181 /* Reject data parsed since the last restore point was marked. */
183 void
184 gfc_reject_data (gfc_namespace *ns)
186 gfc_data *d;
188 while (ns->data && ns->data != ns->old_data)
190 d = ns->data->next;
191 free (ns->data);
192 ns->data = d;
196 static match var_element (gfc_data_variable *);
198 /* Match a list of variables terminated by an iterator and a right
199 parenthesis. */
201 static match
202 var_list (gfc_data_variable *parent)
204 gfc_data_variable *tail, var;
205 match m;
207 m = var_element (&var);
208 if (m == MATCH_ERROR)
209 return MATCH_ERROR;
210 if (m == MATCH_NO)
211 goto syntax;
213 tail = gfc_get_data_variable ();
214 *tail = var;
216 parent->list = tail;
218 for (;;)
220 if (gfc_match_char (',') != MATCH_YES)
221 goto syntax;
223 m = gfc_match_iterator (&parent->iter, 1);
224 if (m == MATCH_YES)
225 break;
226 if (m == MATCH_ERROR)
227 return MATCH_ERROR;
229 m = var_element (&var);
230 if (m == MATCH_ERROR)
231 return MATCH_ERROR;
232 if (m == MATCH_NO)
233 goto syntax;
235 tail->next = gfc_get_data_variable ();
236 tail = tail->next;
238 *tail = var;
241 if (gfc_match_char (')') != MATCH_YES)
242 goto syntax;
243 return MATCH_YES;
245 syntax:
246 gfc_syntax_error (ST_DATA);
247 return MATCH_ERROR;
251 /* Match a single element in a data variable list, which can be a
252 variable-iterator list. */
254 static match
255 var_element (gfc_data_variable *new_var)
257 match m;
258 gfc_symbol *sym;
260 memset (new_var, 0, sizeof (gfc_data_variable));
262 if (gfc_match_char ('(') == MATCH_YES)
263 return var_list (new_var);
265 m = gfc_match_variable (&new_var->expr, 0);
266 if (m != MATCH_YES)
267 return m;
269 sym = new_var->expr->symtree->n.sym;
271 /* Symbol should already have an associated type. */
272 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
273 return MATCH_ERROR;
275 if (!sym->attr.function && gfc_current_ns->parent
276 && gfc_current_ns->parent == sym->ns)
278 gfc_error ("Host associated variable %qs may not be in the DATA "
279 "statement at %C", sym->name);
280 return MATCH_ERROR;
283 if (gfc_current_state () != COMP_BLOCK_DATA
284 && sym->attr.in_common
285 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
286 "common block variable %qs in DATA statement at %C",
287 sym->name))
288 return MATCH_ERROR;
290 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
291 return MATCH_ERROR;
293 return MATCH_YES;
297 /* Match the top-level list of data variables. */
299 static match
300 top_var_list (gfc_data *d)
302 gfc_data_variable var, *tail, *new_var;
303 match m;
305 tail = NULL;
307 for (;;)
309 m = var_element (&var);
310 if (m == MATCH_NO)
311 goto syntax;
312 if (m == MATCH_ERROR)
313 return MATCH_ERROR;
315 new_var = gfc_get_data_variable ();
316 *new_var = var;
318 if (tail == NULL)
319 d->var = new_var;
320 else
321 tail->next = new_var;
323 tail = new_var;
325 if (gfc_match_char ('/') == MATCH_YES)
326 break;
327 if (gfc_match_char (',') != MATCH_YES)
328 goto syntax;
331 return MATCH_YES;
333 syntax:
334 gfc_syntax_error (ST_DATA);
335 gfc_free_data_all (gfc_current_ns);
336 return MATCH_ERROR;
340 static match
341 match_data_constant (gfc_expr **result)
343 char name[GFC_MAX_SYMBOL_LEN + 1];
344 gfc_symbol *sym, *dt_sym = NULL;
345 gfc_expr *expr;
346 match m;
347 locus old_loc;
349 m = gfc_match_literal_constant (&expr, 1);
350 if (m == MATCH_YES)
352 *result = expr;
353 return MATCH_YES;
356 if (m == MATCH_ERROR)
357 return MATCH_ERROR;
359 m = gfc_match_null (result);
360 if (m != MATCH_NO)
361 return m;
363 old_loc = gfc_current_locus;
365 /* Should this be a structure component, try to match it
366 before matching a name. */
367 m = gfc_match_rvalue (result);
368 if (m == MATCH_ERROR)
369 return m;
371 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
373 if (!gfc_simplify_expr (*result, 0))
374 m = MATCH_ERROR;
375 return m;
377 else if (m == MATCH_YES)
378 gfc_free_expr (*result);
380 gfc_current_locus = old_loc;
382 m = gfc_match_name (name);
383 if (m != MATCH_YES)
384 return m;
386 if (gfc_find_symbol (name, NULL, 1, &sym))
387 return MATCH_ERROR;
389 if (sym && sym->attr.generic)
390 dt_sym = gfc_find_dt_in_generic (sym);
392 if (sym == NULL
393 || (sym->attr.flavor != FL_PARAMETER
394 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
396 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
397 name);
398 return MATCH_ERROR;
400 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
401 return gfc_match_structure_constructor (dt_sym, result);
403 /* Check to see if the value is an initialization array expression. */
404 if (sym->value->expr_type == EXPR_ARRAY)
406 gfc_current_locus = old_loc;
408 m = gfc_match_init_expr (result);
409 if (m == MATCH_ERROR)
410 return m;
412 if (m == MATCH_YES)
414 if (!gfc_simplify_expr (*result, 0))
415 m = MATCH_ERROR;
417 if ((*result)->expr_type == EXPR_CONSTANT)
418 return m;
419 else
421 gfc_error ("Invalid initializer %s in Data statement at %C", name);
422 return MATCH_ERROR;
427 *result = gfc_copy_expr (sym->value);
428 return MATCH_YES;
432 /* Match a list of values in a DATA statement. The leading '/' has
433 already been seen at this point. */
435 static match
436 top_val_list (gfc_data *data)
438 gfc_data_value *new_val, *tail;
439 gfc_expr *expr;
440 match m;
442 tail = NULL;
444 for (;;)
446 m = match_data_constant (&expr);
447 if (m == MATCH_NO)
448 goto syntax;
449 if (m == MATCH_ERROR)
450 return MATCH_ERROR;
452 new_val = gfc_get_data_value ();
453 mpz_init (new_val->repeat);
455 if (tail == NULL)
456 data->value = new_val;
457 else
458 tail->next = new_val;
460 tail = new_val;
462 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
464 tail->expr = expr;
465 mpz_set_ui (tail->repeat, 1);
467 else
469 mpz_set (tail->repeat, expr->value.integer);
470 gfc_free_expr (expr);
472 m = match_data_constant (&tail->expr);
473 if (m == MATCH_NO)
474 goto syntax;
475 if (m == MATCH_ERROR)
476 return MATCH_ERROR;
479 if (gfc_match_char ('/') == MATCH_YES)
480 break;
481 if (gfc_match_char (',') == MATCH_NO)
482 goto syntax;
485 return MATCH_YES;
487 syntax:
488 gfc_syntax_error (ST_DATA);
489 gfc_free_data_all (gfc_current_ns);
490 return MATCH_ERROR;
494 /* Matches an old style initialization. */
496 static match
497 match_old_style_init (const char *name)
499 match m;
500 gfc_symtree *st;
501 gfc_symbol *sym;
502 gfc_data *newdata;
504 /* Set up data structure to hold initializers. */
505 gfc_find_sym_tree (name, NULL, 0, &st);
506 sym = st->n.sym;
508 newdata = gfc_get_data ();
509 newdata->var = gfc_get_data_variable ();
510 newdata->var->expr = gfc_get_variable_expr (st);
511 newdata->where = gfc_current_locus;
513 /* Match initial value list. This also eats the terminal '/'. */
514 m = top_val_list (newdata);
515 if (m != MATCH_YES)
517 free (newdata);
518 return m;
521 if (gfc_pure (NULL))
523 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
524 free (newdata);
525 return MATCH_ERROR;
527 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
529 /* Mark the variable as having appeared in a data statement. */
530 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
532 free (newdata);
533 return MATCH_ERROR;
536 /* Chain in namespace list of DATA initializers. */
537 newdata->next = gfc_current_ns->data;
538 gfc_current_ns->data = newdata;
540 return m;
544 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
545 we are matching a DATA statement and are therefore issuing an error
546 if we encounter something unexpected, if not, we're trying to match
547 an old-style initialization expression of the form INTEGER I /2/. */
549 match
550 gfc_match_data (void)
552 gfc_data *new_data;
553 match m;
555 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
556 if ((gfc_current_state () == COMP_FUNCTION
557 || gfc_current_state () == COMP_SUBROUTINE)
558 && gfc_state_stack->previous->state == COMP_INTERFACE)
560 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
561 return MATCH_ERROR;
564 set_in_match_data (true);
566 for (;;)
568 new_data = gfc_get_data ();
569 new_data->where = gfc_current_locus;
571 m = top_var_list (new_data);
572 if (m != MATCH_YES)
573 goto cleanup;
575 m = top_val_list (new_data);
576 if (m != MATCH_YES)
577 goto cleanup;
579 new_data->next = gfc_current_ns->data;
580 gfc_current_ns->data = new_data;
582 if (gfc_match_eos () == MATCH_YES)
583 break;
585 gfc_match_char (','); /* Optional comma */
588 set_in_match_data (false);
590 if (gfc_pure (NULL))
592 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
593 return MATCH_ERROR;
595 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
597 return MATCH_YES;
599 cleanup:
600 set_in_match_data (false);
601 gfc_free_data (new_data);
602 return MATCH_ERROR;
606 /************************ Declaration statements *********************/
609 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
611 static bool
612 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
614 int i;
616 if ((from->type == AS_ASSUMED_RANK && to->corank)
617 || (to->type == AS_ASSUMED_RANK && from->corank))
619 gfc_error ("The assumed-rank array at %C shall not have a codimension");
620 return false;
623 if (to->rank == 0 && from->rank > 0)
625 to->rank = from->rank;
626 to->type = from->type;
627 to->cray_pointee = from->cray_pointee;
628 to->cp_was_assumed = from->cp_was_assumed;
630 for (i = 0; i < to->corank; i++)
632 to->lower[from->rank + i] = to->lower[i];
633 to->upper[from->rank + i] = to->upper[i];
635 for (i = 0; i < from->rank; i++)
637 if (copy)
639 to->lower[i] = gfc_copy_expr (from->lower[i]);
640 to->upper[i] = gfc_copy_expr (from->upper[i]);
642 else
644 to->lower[i] = from->lower[i];
645 to->upper[i] = from->upper[i];
649 else if (to->corank == 0 && from->corank > 0)
651 to->corank = from->corank;
652 to->cotype = from->cotype;
654 for (i = 0; i < from->corank; i++)
656 if (copy)
658 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
659 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
661 else
663 to->lower[to->rank + i] = from->lower[i];
664 to->upper[to->rank + i] = from->upper[i];
669 return true;
673 /* Match an intent specification. Since this can only happen after an
674 INTENT word, a legal intent-spec must follow. */
676 static sym_intent
677 match_intent_spec (void)
680 if (gfc_match (" ( in out )") == MATCH_YES)
681 return INTENT_INOUT;
682 if (gfc_match (" ( in )") == MATCH_YES)
683 return INTENT_IN;
684 if (gfc_match (" ( out )") == MATCH_YES)
685 return INTENT_OUT;
687 gfc_error ("Bad INTENT specification at %C");
688 return INTENT_UNKNOWN;
692 /* Matches a character length specification, which is either a
693 specification expression, '*', or ':'. */
695 static match
696 char_len_param_value (gfc_expr **expr, bool *deferred)
698 match m;
700 *expr = NULL;
701 *deferred = false;
703 if (gfc_match_char ('*') == MATCH_YES)
704 return MATCH_YES;
706 if (gfc_match_char (':') == MATCH_YES)
708 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
709 return MATCH_ERROR;
711 *deferred = true;
713 return MATCH_YES;
716 m = gfc_match_expr (expr);
718 if (m == MATCH_NO || m == MATCH_ERROR)
719 return m;
721 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
722 return MATCH_ERROR;
724 if ((*expr)->expr_type == EXPR_FUNCTION)
726 if ((*expr)->ts.type == BT_INTEGER
727 || ((*expr)->ts.type == BT_UNKNOWN
728 && strcmp((*expr)->symtree->name, "null") != 0))
729 return MATCH_YES;
731 goto syntax;
733 else if ((*expr)->expr_type == EXPR_CONSTANT)
735 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
736 processor dependent and its value is greater than or equal to zero.
737 F2008, 4.4.3.2: If the character length parameter value evaluates
738 to a negative value, the length of character entities declared
739 is zero. */
741 if ((*expr)->ts.type == BT_INTEGER)
743 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
744 mpz_set_si ((*expr)->value.integer, 0);
746 else
747 goto syntax;
749 else if ((*expr)->expr_type == EXPR_ARRAY)
750 goto syntax;
751 else if ((*expr)->expr_type == EXPR_VARIABLE)
753 gfc_expr *e;
755 e = gfc_copy_expr (*expr);
757 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
758 which causes an ICE if gfc_reduce_init_expr() is called. */
759 if (e->ref && e->ref->type == REF_ARRAY
760 && e->ref->u.ar.type == AR_UNKNOWN
761 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
762 goto syntax;
764 gfc_reduce_init_expr (e);
766 if ((e->ref && e->ref->type == REF_ARRAY
767 && e->ref->u.ar.type != AR_ELEMENT)
768 || (!e->ref && e->expr_type == EXPR_ARRAY))
770 gfc_free_expr (e);
771 goto syntax;
774 gfc_free_expr (e);
777 return m;
779 syntax:
780 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
781 return MATCH_ERROR;
785 /* A character length is a '*' followed by a literal integer or a
786 char_len_param_value in parenthesis. */
788 static match
789 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
791 int length;
792 match m;
794 *deferred = false;
795 m = gfc_match_char ('*');
796 if (m != MATCH_YES)
797 return m;
799 m = gfc_match_small_literal_int (&length, NULL);
800 if (m == MATCH_ERROR)
801 return m;
803 if (m == MATCH_YES)
805 if (obsolescent_check
806 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
807 return MATCH_ERROR;
808 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
809 return m;
812 if (gfc_match_char ('(') == MATCH_NO)
813 goto syntax;
815 m = char_len_param_value (expr, deferred);
816 if (m != MATCH_YES && gfc_matching_function)
818 gfc_undo_symbols ();
819 m = MATCH_YES;
822 if (m == MATCH_ERROR)
823 return m;
824 if (m == MATCH_NO)
825 goto syntax;
827 if (gfc_match_char (')') == MATCH_NO)
829 gfc_free_expr (*expr);
830 *expr = NULL;
831 goto syntax;
834 return MATCH_YES;
836 syntax:
837 gfc_error ("Syntax error in character length specification at %C");
838 return MATCH_ERROR;
842 /* Special subroutine for finding a symbol. Check if the name is found
843 in the current name space. If not, and we're compiling a function or
844 subroutine and the parent compilation unit is an interface, then check
845 to see if the name we've been given is the name of the interface
846 (located in another namespace). */
848 static int
849 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
851 gfc_state_data *s;
852 gfc_symtree *st;
853 int i;
855 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
856 if (i == 0)
858 *result = st ? st->n.sym : NULL;
859 goto end;
862 if (gfc_current_state () != COMP_SUBROUTINE
863 && gfc_current_state () != COMP_FUNCTION)
864 goto end;
866 s = gfc_state_stack->previous;
867 if (s == NULL)
868 goto end;
870 if (s->state != COMP_INTERFACE)
871 goto end;
872 if (s->sym == NULL)
873 goto end; /* Nameless interface. */
875 if (strcmp (name, s->sym->name) == 0)
877 *result = s->sym;
878 return 0;
881 end:
882 return i;
886 /* Special subroutine for getting a symbol node associated with a
887 procedure name, used in SUBROUTINE and FUNCTION statements. The
888 symbol is created in the parent using with symtree node in the
889 child unit pointing to the symbol. If the current namespace has no
890 parent, then the symbol is just created in the current unit. */
892 static int
893 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
895 gfc_symtree *st;
896 gfc_symbol *sym;
897 int rc = 0;
899 /* Module functions have to be left in their own namespace because
900 they have potentially (almost certainly!) already been referenced.
901 In this sense, they are rather like external functions. This is
902 fixed up in resolve.c(resolve_entries), where the symbol name-
903 space is set to point to the master function, so that the fake
904 result mechanism can work. */
905 if (module_fcn_entry)
907 /* Present if entry is declared to be a module procedure. */
908 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
910 if (*result == NULL)
911 rc = gfc_get_symbol (name, NULL, result);
912 else if (!gfc_get_symbol (name, NULL, &sym) && sym
913 && (*result)->ts.type == BT_UNKNOWN
914 && sym->attr.flavor == FL_UNKNOWN)
915 /* Pick up the typespec for the entry, if declared in the function
916 body. Note that this symbol is FL_UNKNOWN because it will
917 only have appeared in a type declaration. The local symtree
918 is set to point to the module symbol and a unique symtree
919 to the local version. This latter ensures a correct clearing
920 of the symbols. */
922 /* If the ENTRY proceeds its specification, we need to ensure
923 that this does not raise a "has no IMPLICIT type" error. */
924 if (sym->ts.type == BT_UNKNOWN)
925 sym->attr.untyped = 1;
927 (*result)->ts = sym->ts;
929 /* Put the symbol in the procedure namespace so that, should
930 the ENTRY precede its specification, the specification
931 can be applied. */
932 (*result)->ns = gfc_current_ns;
934 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
935 st->n.sym = *result;
936 st = gfc_get_unique_symtree (gfc_current_ns);
937 sym->refs++;
938 st->n.sym = sym;
941 else
942 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
944 if (rc)
945 return rc;
947 sym = *result;
948 if (sym->attr.proc == PROC_ST_FUNCTION)
949 return rc;
951 if (sym->attr.module_procedure
952 && sym->attr.if_source == IFSRC_IFBODY)
954 /* Create a partially populated interface symbol to carry the
955 characteristics of the procedure and the result. */
956 sym->ts.interface = gfc_new_symbol (name, sym->ns);
957 gfc_add_type (sym->ts.interface, &(sym->ts),
958 &gfc_current_locus);
959 gfc_copy_attr (&sym->ts.interface->attr, &sym->attr, NULL);
960 if (sym->attr.dimension)
961 sym->ts.interface->as = gfc_copy_array_spec (sym->as);
963 /* Ideally, at this point, a copy would be made of the formal
964 arguments and their namespace. However, this does not appear
965 to be necessary, albeit at the expense of not being able to
966 use gfc_compare_interfaces directly. */
968 if (sym->result && sym->result != sym)
970 sym->ts.interface->result = sym->result;
971 sym->result = NULL;
973 else if (sym->result)
975 sym->ts.interface->result = sym->ts.interface;
978 else if (sym && !sym->gfc_new
979 && gfc_current_state () != COMP_INTERFACE)
981 /* Trap another encompassed procedure with the same name. All
982 these conditions are necessary to avoid picking up an entry
983 whose name clashes with that of the encompassing procedure;
984 this is handled using gsymbols to register unique, globally
985 accessible names. */
986 if (sym->attr.flavor != 0
987 && sym->attr.proc != 0
988 && (sym->attr.subroutine || sym->attr.function)
989 && sym->attr.if_source != IFSRC_UNKNOWN)
990 gfc_error_now ("Procedure %qs at %C is already defined at %L",
991 name, &sym->declared_at);
993 /* Trap a procedure with a name the same as interface in the
994 encompassing scope. */
995 if (sym->attr.generic != 0
996 && (sym->attr.subroutine || sym->attr.function)
997 && !sym->attr.mod_proc)
998 gfc_error_now ("Name %qs at %C is already defined"
999 " as a generic interface at %L",
1000 name, &sym->declared_at);
1002 /* Trap declarations of attributes in encompassing scope. The
1003 signature for this is that ts.kind is set. Legitimate
1004 references only set ts.type. */
1005 if (sym->ts.kind != 0
1006 && !sym->attr.implicit_type
1007 && sym->attr.proc == 0
1008 && gfc_current_ns->parent != NULL
1009 && sym->attr.access == 0
1010 && !module_fcn_entry)
1011 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1012 "and must not have attributes declared at %L",
1013 name, &sym->declared_at);
1016 if (gfc_current_ns->parent == NULL || *result == NULL)
1017 return rc;
1019 /* Module function entries will already have a symtree in
1020 the current namespace but will need one at module level. */
1021 if (module_fcn_entry)
1023 /* Present if entry is declared to be a module procedure. */
1024 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1025 if (st == NULL)
1026 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1028 else
1029 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1031 st->n.sym = sym;
1032 sym->refs++;
1034 /* See if the procedure should be a module procedure. */
1036 if (((sym->ns->proc_name != NULL
1037 && sym->ns->proc_name->attr.flavor == FL_MODULE
1038 && sym->attr.proc != PROC_MODULE)
1039 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1040 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1041 rc = 2;
1043 return rc;
1047 /* Verify that the given symbol representing a parameter is C
1048 interoperable, by checking to see if it was marked as such after
1049 its declaration. If the given symbol is not interoperable, a
1050 warning is reported, thus removing the need to return the status to
1051 the calling function. The standard does not require the user use
1052 one of the iso_c_binding named constants to declare an
1053 interoperable parameter, but we can't be sure if the param is C
1054 interop or not if the user doesn't. For example, integer(4) may be
1055 legal Fortran, but doesn't have meaning in C. It may interop with
1056 a number of the C types, which causes a problem because the
1057 compiler can't know which one. This code is almost certainly not
1058 portable, and the user will get what they deserve if the C type
1059 across platforms isn't always interoperable with integer(4). If
1060 the user had used something like integer(c_int) or integer(c_long),
1061 the compiler could have automatically handled the varying sizes
1062 across platforms. */
1064 bool
1065 gfc_verify_c_interop_param (gfc_symbol *sym)
1067 int is_c_interop = 0;
1068 bool retval = true;
1070 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1071 Don't repeat the checks here. */
1072 if (sym->attr.implicit_type)
1073 return true;
1075 /* For subroutines or functions that are passed to a BIND(C) procedure,
1076 they're interoperable if they're BIND(C) and their params are all
1077 interoperable. */
1078 if (sym->attr.flavor == FL_PROCEDURE)
1080 if (sym->attr.is_bind_c == 0)
1082 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1083 "attribute to be C interoperable", sym->name,
1084 &(sym->declared_at));
1085 return false;
1087 else
1089 if (sym->attr.is_c_interop == 1)
1090 /* We've already checked this procedure; don't check it again. */
1091 return true;
1092 else
1093 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1094 sym->common_block);
1098 /* See if we've stored a reference to a procedure that owns sym. */
1099 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1101 if (sym->ns->proc_name->attr.is_bind_c == 1)
1103 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1105 if (is_c_interop != 1)
1107 /* Make personalized messages to give better feedback. */
1108 if (sym->ts.type == BT_DERIVED)
1109 gfc_error ("Variable %qs at %L is a dummy argument to the "
1110 "BIND(C) procedure %qs but is not C interoperable "
1111 "because derived type %qs is not C interoperable",
1112 sym->name, &(sym->declared_at),
1113 sym->ns->proc_name->name,
1114 sym->ts.u.derived->name);
1115 else if (sym->ts.type == BT_CLASS)
1116 gfc_error ("Variable %qs at %L is a dummy argument to the "
1117 "BIND(C) procedure %qs but is not C interoperable "
1118 "because it is polymorphic",
1119 sym->name, &(sym->declared_at),
1120 sym->ns->proc_name->name);
1121 else if (warn_c_binding_type)
1122 gfc_warning (OPT_Wc_binding_type,
1123 "Variable %qs at %L is a dummy argument of the "
1124 "BIND(C) procedure %qs but may not be C "
1125 "interoperable",
1126 sym->name, &(sym->declared_at),
1127 sym->ns->proc_name->name);
1130 /* Character strings are only C interoperable if they have a
1131 length of 1. */
1132 if (sym->ts.type == BT_CHARACTER)
1134 gfc_charlen *cl = sym->ts.u.cl;
1135 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1136 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1138 gfc_error ("Character argument %qs at %L "
1139 "must be length 1 because "
1140 "procedure %qs is BIND(C)",
1141 sym->name, &sym->declared_at,
1142 sym->ns->proc_name->name);
1143 retval = false;
1147 /* We have to make sure that any param to a bind(c) routine does
1148 not have the allocatable, pointer, or optional attributes,
1149 according to J3/04-007, section 5.1. */
1150 if (sym->attr.allocatable == 1
1151 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1152 "ALLOCATABLE attribute in procedure %qs "
1153 "with BIND(C)", sym->name,
1154 &(sym->declared_at),
1155 sym->ns->proc_name->name))
1156 retval = false;
1158 if (sym->attr.pointer == 1
1159 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1160 "POINTER attribute in procedure %qs "
1161 "with BIND(C)", sym->name,
1162 &(sym->declared_at),
1163 sym->ns->proc_name->name))
1164 retval = false;
1166 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1168 gfc_error ("Scalar variable %qs at %L with POINTER or "
1169 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1170 " supported", sym->name, &(sym->declared_at),
1171 sym->ns->proc_name->name);
1172 retval = false;
1175 if (sym->attr.optional == 1 && sym->attr.value)
1177 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1178 "and the VALUE attribute because procedure %qs "
1179 "is BIND(C)", sym->name, &(sym->declared_at),
1180 sym->ns->proc_name->name);
1181 retval = false;
1183 else if (sym->attr.optional == 1
1184 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1185 "at %L with OPTIONAL attribute in "
1186 "procedure %qs which is BIND(C)",
1187 sym->name, &(sym->declared_at),
1188 sym->ns->proc_name->name))
1189 retval = false;
1191 /* Make sure that if it has the dimension attribute, that it is
1192 either assumed size or explicit shape. Deferred shape is already
1193 covered by the pointer/allocatable attribute. */
1194 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1195 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1196 "at %L as dummy argument to the BIND(C) "
1197 "procedure %qs at %L", sym->name,
1198 &(sym->declared_at),
1199 sym->ns->proc_name->name,
1200 &(sym->ns->proc_name->declared_at)))
1201 retval = false;
1205 return retval;
1210 /* Function called by variable_decl() that adds a name to the symbol table. */
1212 static bool
1213 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1214 gfc_array_spec **as, locus *var_locus)
1216 symbol_attribute attr;
1217 gfc_symbol *sym;
1219 if (gfc_get_symbol (name, NULL, &sym))
1220 return false;
1222 /* Start updating the symbol table. Add basic type attribute if present. */
1223 if (current_ts.type != BT_UNKNOWN
1224 && (sym->attr.implicit_type == 0
1225 || !gfc_compare_types (&sym->ts, &current_ts))
1226 && !gfc_add_type (sym, &current_ts, var_locus))
1227 return false;
1229 if (sym->ts.type == BT_CHARACTER)
1231 sym->ts.u.cl = cl;
1232 sym->ts.deferred = cl_deferred;
1235 /* Add dimension attribute if present. */
1236 if (!gfc_set_array_spec (sym, *as, var_locus))
1237 return false;
1238 *as = NULL;
1240 /* Add attribute to symbol. The copy is so that we can reset the
1241 dimension attribute. */
1242 attr = current_attr;
1243 attr.dimension = 0;
1244 attr.codimension = 0;
1246 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1247 return false;
1249 /* Finish any work that may need to be done for the binding label,
1250 if it's a bind(c). The bind(c) attr is found before the symbol
1251 is made, and before the symbol name (for data decls), so the
1252 current_ts is holding the binding label, or nothing if the
1253 name= attr wasn't given. Therefore, test here if we're dealing
1254 with a bind(c) and make sure the binding label is set correctly. */
1255 if (sym->attr.is_bind_c == 1)
1257 if (!sym->binding_label)
1259 /* Set the binding label and verify that if a NAME= was specified
1260 then only one identifier was in the entity-decl-list. */
1261 if (!set_binding_label (&sym->binding_label, sym->name,
1262 num_idents_on_line))
1263 return false;
1267 /* See if we know we're in a common block, and if it's a bind(c)
1268 common then we need to make sure we're an interoperable type. */
1269 if (sym->attr.in_common == 1)
1271 /* Test the common block object. */
1272 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1273 && sym->ts.is_c_interop != 1)
1275 gfc_error_now ("Variable %qs in common block %qs at %C "
1276 "must be declared with a C interoperable "
1277 "kind since common block %qs is BIND(C)",
1278 sym->name, sym->common_block->name,
1279 sym->common_block->name);
1280 gfc_clear_error ();
1284 sym->attr.implied_index = 0;
1286 if (sym->ts.type == BT_CLASS)
1287 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1289 return true;
1293 /* Set character constant to the given length. The constant will be padded or
1294 truncated. If we're inside an array constructor without a typespec, we
1295 additionally check that all elements have the same length; check_len -1
1296 means no checking. */
1298 void
1299 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1301 gfc_char_t *s;
1302 int slen;
1304 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1306 if (expr->ts.type != BT_CHARACTER)
1307 return;
1309 slen = expr->value.character.length;
1310 if (len != slen)
1312 s = gfc_get_wide_string (len + 1);
1313 memcpy (s, expr->value.character.string,
1314 MIN (len, slen) * sizeof (gfc_char_t));
1315 if (len > slen)
1316 gfc_wide_memset (&s[slen], ' ', len - slen);
1318 if (warn_character_truncation && slen > len)
1319 gfc_warning_now (OPT_Wcharacter_truncation,
1320 "CHARACTER expression at %L is being truncated "
1321 "(%d/%d)", &expr->where, slen, len);
1323 /* Apply the standard by 'hand' otherwise it gets cleared for
1324 initializers. */
1325 if (check_len != -1 && slen != check_len
1326 && !(gfc_option.allow_std & GFC_STD_GNU))
1327 gfc_error_now ("The CHARACTER elements of the array constructor "
1328 "at %L must have the same length (%d/%d)",
1329 &expr->where, slen, check_len);
1331 s[len] = '\0';
1332 free (expr->value.character.string);
1333 expr->value.character.string = s;
1334 expr->value.character.length = len;
1339 /* Function to create and update the enumerator history
1340 using the information passed as arguments.
1341 Pointer "max_enum" is also updated, to point to
1342 enum history node containing largest initializer.
1344 SYM points to the symbol node of enumerator.
1345 INIT points to its enumerator value. */
1347 static void
1348 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1350 enumerator_history *new_enum_history;
1351 gcc_assert (sym != NULL && init != NULL);
1353 new_enum_history = XCNEW (enumerator_history);
1355 new_enum_history->sym = sym;
1356 new_enum_history->initializer = init;
1357 new_enum_history->next = NULL;
1359 if (enum_history == NULL)
1361 enum_history = new_enum_history;
1362 max_enum = enum_history;
1364 else
1366 new_enum_history->next = enum_history;
1367 enum_history = new_enum_history;
1369 if (mpz_cmp (max_enum->initializer->value.integer,
1370 new_enum_history->initializer->value.integer) < 0)
1371 max_enum = new_enum_history;
1376 /* Function to free enum kind history. */
1378 void
1379 gfc_free_enum_history (void)
1381 enumerator_history *current = enum_history;
1382 enumerator_history *next;
1384 while (current != NULL)
1386 next = current->next;
1387 free (current);
1388 current = next;
1390 max_enum = NULL;
1391 enum_history = NULL;
1395 /* Function called by variable_decl() that adds an initialization
1396 expression to a symbol. */
1398 static bool
1399 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1401 symbol_attribute attr;
1402 gfc_symbol *sym;
1403 gfc_expr *init;
1405 init = *initp;
1406 if (find_special (name, &sym, false))
1407 return false;
1409 attr = sym->attr;
1411 /* If this symbol is confirming an implicit parameter type,
1412 then an initialization expression is not allowed. */
1413 if (attr.flavor == FL_PARAMETER
1414 && sym->value != NULL
1415 && *initp != NULL)
1417 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1418 sym->name);
1419 return false;
1422 if (init == NULL)
1424 /* An initializer is required for PARAMETER declarations. */
1425 if (attr.flavor == FL_PARAMETER)
1427 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1428 return false;
1431 else
1433 /* If a variable appears in a DATA block, it cannot have an
1434 initializer. */
1435 if (sym->attr.data)
1437 gfc_error ("Variable %qs at %C with an initializer already "
1438 "appears in a DATA statement", sym->name);
1439 return false;
1442 /* Check if the assignment can happen. This has to be put off
1443 until later for derived type variables and procedure pointers. */
1444 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1445 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1446 && !sym->attr.proc_pointer
1447 && !gfc_check_assign_symbol (sym, NULL, init))
1448 return false;
1450 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1451 && init->ts.type == BT_CHARACTER)
1453 /* Update symbol character length according initializer. */
1454 if (!gfc_check_assign_symbol (sym, NULL, init))
1455 return false;
1457 if (sym->ts.u.cl->length == NULL)
1459 int clen;
1460 /* If there are multiple CHARACTER variables declared on the
1461 same line, we don't want them to share the same length. */
1462 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1464 if (sym->attr.flavor == FL_PARAMETER)
1466 if (init->expr_type == EXPR_CONSTANT)
1468 clen = init->value.character.length;
1469 sym->ts.u.cl->length
1470 = gfc_get_int_expr (gfc_default_integer_kind,
1471 NULL, clen);
1473 else if (init->expr_type == EXPR_ARRAY)
1475 if (init->ts.u.cl)
1476 clen = mpz_get_si (init->ts.u.cl->length->value.integer);
1477 else if (init->value.constructor)
1479 gfc_constructor *c;
1480 c = gfc_constructor_first (init->value.constructor);
1481 clen = c->expr->value.character.length;
1483 else
1484 gcc_unreachable ();
1485 sym->ts.u.cl->length
1486 = gfc_get_int_expr (gfc_default_integer_kind,
1487 NULL, clen);
1489 else if (init->ts.u.cl && init->ts.u.cl->length)
1490 sym->ts.u.cl->length =
1491 gfc_copy_expr (sym->value->ts.u.cl->length);
1494 /* Update initializer character length according symbol. */
1495 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1497 int len;
1499 if (!gfc_specification_expr (sym->ts.u.cl->length))
1500 return false;
1502 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1504 if (init->expr_type == EXPR_CONSTANT)
1505 gfc_set_constant_character_len (len, init, -1);
1506 else if (init->expr_type == EXPR_ARRAY)
1508 gfc_constructor *c;
1510 /* Build a new charlen to prevent simplification from
1511 deleting the length before it is resolved. */
1512 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1513 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1515 for (c = gfc_constructor_first (init->value.constructor);
1516 c; c = gfc_constructor_next (c))
1517 gfc_set_constant_character_len (len, c->expr, -1);
1522 /* If sym is implied-shape, set its upper bounds from init. */
1523 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1524 && sym->as->type == AS_IMPLIED_SHAPE)
1526 int dim;
1528 if (init->rank == 0)
1530 gfc_error ("Can't initialize implied-shape array at %L"
1531 " with scalar", &sym->declared_at);
1532 return false;
1535 /* Shape should be present, we get an initialization expression. */
1536 gcc_assert (init->shape);
1538 for (dim = 0; dim < sym->as->rank; ++dim)
1540 int k;
1541 gfc_expr *e, *lower;
1543 lower = sym->as->lower[dim];
1545 /* If the lower bound is an array element from another
1546 parameterized array, then it is marked with EXPR_VARIABLE and
1547 is an initialization expression. Try to reduce it. */
1548 if (lower->expr_type == EXPR_VARIABLE)
1549 gfc_reduce_init_expr (lower);
1551 if (lower->expr_type == EXPR_CONSTANT)
1553 /* All dimensions must be without upper bound. */
1554 gcc_assert (!sym->as->upper[dim]);
1556 k = lower->ts.kind;
1557 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1558 mpz_add (e->value.integer, lower->value.integer,
1559 init->shape[dim]);
1560 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1561 sym->as->upper[dim] = e;
1563 else
1565 gfc_error ("Non-constant lower bound in implied-shape"
1566 " declaration at %L", &lower->where);
1567 return false;
1571 sym->as->type = AS_EXPLICIT;
1574 /* Need to check if the expression we initialized this
1575 to was one of the iso_c_binding named constants. If so,
1576 and we're a parameter (constant), let it be iso_c.
1577 For example:
1578 integer(c_int), parameter :: my_int = c_int
1579 integer(my_int) :: my_int_2
1580 If we mark my_int as iso_c (since we can see it's value
1581 is equal to one of the named constants), then my_int_2
1582 will be considered C interoperable. */
1583 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1585 sym->ts.is_iso_c |= init->ts.is_iso_c;
1586 sym->ts.is_c_interop |= init->ts.is_c_interop;
1587 /* attr bits needed for module files. */
1588 sym->attr.is_iso_c |= init->ts.is_iso_c;
1589 sym->attr.is_c_interop |= init->ts.is_c_interop;
1590 if (init->ts.is_iso_c)
1591 sym->ts.f90_type = init->ts.f90_type;
1594 /* Add initializer. Make sure we keep the ranks sane. */
1595 if (sym->attr.dimension && init->rank == 0)
1597 mpz_t size;
1598 gfc_expr *array;
1599 int n;
1600 if (sym->attr.flavor == FL_PARAMETER
1601 && init->expr_type == EXPR_CONSTANT
1602 && spec_size (sym->as, &size)
1603 && mpz_cmp_si (size, 0) > 0)
1605 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1606 &init->where);
1607 for (n = 0; n < (int)mpz_get_si (size); n++)
1608 gfc_constructor_append_expr (&array->value.constructor,
1609 n == 0
1610 ? init
1611 : gfc_copy_expr (init),
1612 &init->where);
1614 array->shape = gfc_get_shape (sym->as->rank);
1615 for (n = 0; n < sym->as->rank; n++)
1616 spec_dimen_size (sym->as, n, &array->shape[n]);
1618 init = array;
1619 mpz_clear (size);
1621 init->rank = sym->as->rank;
1624 sym->value = init;
1625 if (sym->attr.save == SAVE_NONE)
1626 sym->attr.save = SAVE_IMPLICIT;
1627 *initp = NULL;
1630 return true;
1634 /* Function called by variable_decl() that adds a name to a structure
1635 being built. */
1637 static bool
1638 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1639 gfc_array_spec **as)
1641 gfc_component *c;
1642 bool t = true;
1644 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1645 constructing, it must have the pointer attribute. */
1646 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1647 && current_ts.u.derived == gfc_current_block ()
1648 && current_attr.pointer == 0)
1650 gfc_error ("Component at %C must have the POINTER attribute");
1651 return false;
1654 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1656 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1658 gfc_error ("Array component of structure at %C must have explicit "
1659 "or deferred shape");
1660 return false;
1664 if (!gfc_add_component (gfc_current_block(), name, &c))
1665 return false;
1667 c->ts = current_ts;
1668 if (c->ts.type == BT_CHARACTER)
1669 c->ts.u.cl = cl;
1670 c->attr = current_attr;
1672 c->initializer = *init;
1673 *init = NULL;
1675 c->as = *as;
1676 if (c->as != NULL)
1678 if (c->as->corank)
1679 c->attr.codimension = 1;
1680 if (c->as->rank)
1681 c->attr.dimension = 1;
1683 *as = NULL;
1685 /* Should this ever get more complicated, combine with similar section
1686 in add_init_expr_to_sym into a separate function. */
1687 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1688 && c->ts.u.cl
1689 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1691 int len;
1693 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1694 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1695 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1697 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1699 if (c->initializer->expr_type == EXPR_CONSTANT)
1700 gfc_set_constant_character_len (len, c->initializer, -1);
1701 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1702 c->initializer->ts.u.cl->length->value.integer))
1704 gfc_constructor *ctor;
1705 ctor = gfc_constructor_first (c->initializer->value.constructor);
1707 if (ctor)
1709 int first_len;
1710 bool has_ts = (c->initializer->ts.u.cl
1711 && c->initializer->ts.u.cl->length_from_typespec);
1713 /* Remember the length of the first element for checking
1714 that all elements *in the constructor* have the same
1715 length. This need not be the length of the LHS! */
1716 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1717 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1718 first_len = ctor->expr->value.character.length;
1720 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1721 if (ctor->expr->expr_type == EXPR_CONSTANT)
1723 gfc_set_constant_character_len (len, ctor->expr,
1724 has_ts ? -1 : first_len);
1725 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1731 /* Check array components. */
1732 if (!c->attr.dimension)
1733 goto scalar;
1735 if (c->attr.pointer)
1737 if (c->as->type != AS_DEFERRED)
1739 gfc_error ("Pointer array component of structure at %C must have a "
1740 "deferred shape");
1741 t = false;
1744 else if (c->attr.allocatable)
1746 if (c->as->type != AS_DEFERRED)
1748 gfc_error ("Allocatable component of structure at %C must have a "
1749 "deferred shape");
1750 t = false;
1753 else
1755 if (c->as->type != AS_EXPLICIT)
1757 gfc_error ("Array component of structure at %C must have an "
1758 "explicit shape");
1759 t = false;
1763 scalar:
1764 if (c->ts.type == BT_CLASS)
1766 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1768 if (t)
1769 t = t2;
1772 return t;
1776 /* Match a 'NULL()', and possibly take care of some side effects. */
1778 match
1779 gfc_match_null (gfc_expr **result)
1781 gfc_symbol *sym;
1782 match m, m2 = MATCH_NO;
1784 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1785 return MATCH_ERROR;
1787 if (m == MATCH_NO)
1789 locus old_loc;
1790 char name[GFC_MAX_SYMBOL_LEN + 1];
1792 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1793 return m2;
1795 old_loc = gfc_current_locus;
1796 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1797 return MATCH_ERROR;
1798 if (m2 != MATCH_YES
1799 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1800 return MATCH_ERROR;
1801 if (m2 == MATCH_NO)
1803 gfc_current_locus = old_loc;
1804 return MATCH_NO;
1808 /* The NULL symbol now has to be/become an intrinsic function. */
1809 if (gfc_get_symbol ("null", NULL, &sym))
1811 gfc_error ("NULL() initialization at %C is ambiguous");
1812 return MATCH_ERROR;
1815 gfc_intrinsic_symbol (sym);
1817 if (sym->attr.proc != PROC_INTRINSIC
1818 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1819 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1820 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1821 return MATCH_ERROR;
1823 *result = gfc_get_null_expr (&gfc_current_locus);
1825 /* Invalid per F2008, C512. */
1826 if (m2 == MATCH_YES)
1828 gfc_error ("NULL() initialization at %C may not have MOLD");
1829 return MATCH_ERROR;
1832 return MATCH_YES;
1836 /* Match the initialization expr for a data pointer or procedure pointer. */
1838 static match
1839 match_pointer_init (gfc_expr **init, int procptr)
1841 match m;
1843 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1845 gfc_error ("Initialization of pointer at %C is not allowed in "
1846 "a PURE procedure");
1847 return MATCH_ERROR;
1849 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1851 /* Match NULL() initialization. */
1852 m = gfc_match_null (init);
1853 if (m != MATCH_NO)
1854 return m;
1856 /* Match non-NULL initialization. */
1857 gfc_matching_ptr_assignment = !procptr;
1858 gfc_matching_procptr_assignment = procptr;
1859 m = gfc_match_rvalue (init);
1860 gfc_matching_ptr_assignment = 0;
1861 gfc_matching_procptr_assignment = 0;
1862 if (m == MATCH_ERROR)
1863 return MATCH_ERROR;
1864 else if (m == MATCH_NO)
1866 gfc_error ("Error in pointer initialization at %C");
1867 return MATCH_ERROR;
1870 if (!procptr && !gfc_resolve_expr (*init))
1871 return MATCH_ERROR;
1873 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1874 "initialization at %C"))
1875 return MATCH_ERROR;
1877 return MATCH_YES;
1881 static bool
1882 check_function_name (char *name)
1884 /* In functions that have a RESULT variable defined, the function name always
1885 refers to function calls. Therefore, the name is not allowed to appear in
1886 specification statements. When checking this, be careful about
1887 'hidden' procedure pointer results ('ppr@'). */
1889 if (gfc_current_state () == COMP_FUNCTION)
1891 gfc_symbol *block = gfc_current_block ();
1892 if (block && block->result && block->result != block
1893 && strcmp (block->result->name, "ppr@") != 0
1894 && strcmp (block->name, name) == 0)
1896 gfc_error ("Function name %qs not allowed at %C", name);
1897 return false;
1901 return true;
1905 /* Match a variable name with an optional initializer. When this
1906 subroutine is called, a variable is expected to be parsed next.
1907 Depending on what is happening at the moment, updates either the
1908 symbol table or the current interface. */
1910 static match
1911 variable_decl (int elem)
1913 char name[GFC_MAX_SYMBOL_LEN + 1];
1914 gfc_expr *initializer, *char_len;
1915 gfc_array_spec *as;
1916 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1917 gfc_charlen *cl;
1918 bool cl_deferred;
1919 locus var_locus;
1920 match m;
1921 bool t;
1922 gfc_symbol *sym;
1924 initializer = NULL;
1925 as = NULL;
1926 cp_as = NULL;
1928 /* When we get here, we've just matched a list of attributes and
1929 maybe a type and a double colon. The next thing we expect to see
1930 is the name of the symbol. */
1931 m = gfc_match_name (name);
1932 if (m != MATCH_YES)
1933 goto cleanup;
1935 var_locus = gfc_current_locus;
1937 /* Now we could see the optional array spec. or character length. */
1938 m = gfc_match_array_spec (&as, true, true);
1939 if (m == MATCH_ERROR)
1940 goto cleanup;
1942 if (m == MATCH_NO)
1943 as = gfc_copy_array_spec (current_as);
1944 else if (current_as
1945 && !merge_array_spec (current_as, as, true))
1947 m = MATCH_ERROR;
1948 goto cleanup;
1951 if (flag_cray_pointer)
1952 cp_as = gfc_copy_array_spec (as);
1954 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1955 determine (and check) whether it can be implied-shape. If it
1956 was parsed as assumed-size, change it because PARAMETERs can not
1957 be assumed-size. */
1958 if (as)
1960 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1962 m = MATCH_ERROR;
1963 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1964 name, &var_locus);
1965 goto cleanup;
1968 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1969 && current_attr.flavor == FL_PARAMETER)
1970 as->type = AS_IMPLIED_SHAPE;
1972 if (as->type == AS_IMPLIED_SHAPE
1973 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1974 &var_locus))
1976 m = MATCH_ERROR;
1977 goto cleanup;
1981 char_len = NULL;
1982 cl = NULL;
1983 cl_deferred = false;
1985 if (current_ts.type == BT_CHARACTER)
1987 switch (match_char_length (&char_len, &cl_deferred, false))
1989 case MATCH_YES:
1990 cl = gfc_new_charlen (gfc_current_ns, NULL);
1992 cl->length = char_len;
1993 break;
1995 /* Non-constant lengths need to be copied after the first
1996 element. Also copy assumed lengths. */
1997 case MATCH_NO:
1998 if (elem > 1
1999 && (current_ts.u.cl->length == NULL
2000 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2002 cl = gfc_new_charlen (gfc_current_ns, NULL);
2003 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2005 else
2006 cl = current_ts.u.cl;
2008 cl_deferred = current_ts.deferred;
2010 break;
2012 case MATCH_ERROR:
2013 goto cleanup;
2017 /* The dummy arguments and result of the abreviated form of MODULE
2018 PROCEDUREs, used in SUBMODULES should not be redefined. */
2019 if (gfc_current_ns->proc_name
2020 && gfc_current_ns->proc_name->abr_modproc_decl)
2022 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2023 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2025 m = MATCH_ERROR;
2026 gfc_error ("%qs at %C is a redefinition of the declaration "
2027 "in the corresponding interface for MODULE "
2028 "PROCEDURE %qs", sym->name,
2029 gfc_current_ns->proc_name->name);
2030 goto cleanup;
2034 /* If this symbol has already shown up in a Cray Pointer declaration,
2035 and this is not a component declaration,
2036 then we want to set the type & bail out. */
2037 if (flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
2039 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2040 if (sym != NULL && sym->attr.cray_pointee)
2042 sym->ts.type = current_ts.type;
2043 sym->ts.kind = current_ts.kind;
2044 sym->ts.u.cl = cl;
2045 sym->ts.u.derived = current_ts.u.derived;
2046 sym->ts.is_c_interop = current_ts.is_c_interop;
2047 sym->ts.is_iso_c = current_ts.is_iso_c;
2048 m = MATCH_YES;
2050 /* Check to see if we have an array specification. */
2051 if (cp_as != NULL)
2053 if (sym->as != NULL)
2055 gfc_error ("Duplicate array spec for Cray pointee at %C");
2056 gfc_free_array_spec (cp_as);
2057 m = MATCH_ERROR;
2058 goto cleanup;
2060 else
2062 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2063 gfc_internal_error ("Couldn't set pointee array spec.");
2065 /* Fix the array spec. */
2066 m = gfc_mod_pointee_as (sym->as);
2067 if (m == MATCH_ERROR)
2068 goto cleanup;
2071 goto cleanup;
2073 else
2075 gfc_free_array_spec (cp_as);
2079 /* Procedure pointer as function result. */
2080 if (gfc_current_state () == COMP_FUNCTION
2081 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2082 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2083 strcpy (name, "ppr@");
2085 if (gfc_current_state () == COMP_FUNCTION
2086 && strcmp (name, gfc_current_block ()->name) == 0
2087 && gfc_current_block ()->result
2088 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2089 strcpy (name, "ppr@");
2091 /* OK, we've successfully matched the declaration. Now put the
2092 symbol in the current namespace, because it might be used in the
2093 optional initialization expression for this symbol, e.g. this is
2094 perfectly legal:
2096 integer, parameter :: i = huge(i)
2098 This is only true for parameters or variables of a basic type.
2099 For components of derived types, it is not true, so we don't
2100 create a symbol for those yet. If we fail to create the symbol,
2101 bail out. */
2102 if (gfc_current_state () != COMP_DERIVED
2103 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2105 m = MATCH_ERROR;
2106 goto cleanup;
2109 if (!check_function_name (name))
2111 m = MATCH_ERROR;
2112 goto cleanup;
2115 /* We allow old-style initializations of the form
2116 integer i /2/, j(4) /3*3, 1/
2117 (if no colon has been seen). These are different from data
2118 statements in that initializers are only allowed to apply to the
2119 variable immediately preceding, i.e.
2120 integer i, j /1, 2/
2121 is not allowed. Therefore we have to do some work manually, that
2122 could otherwise be left to the matchers for DATA statements. */
2124 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2126 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2127 "initialization at %C"))
2128 return MATCH_ERROR;
2129 else if (gfc_current_state () == COMP_DERIVED)
2131 gfc_error ("Invalid old style initialization for derived type "
2132 "component at %C");
2133 m = MATCH_ERROR;
2134 goto cleanup;
2137 return match_old_style_init (name);
2140 /* The double colon must be present in order to have initializers.
2141 Otherwise the statement is ambiguous with an assignment statement. */
2142 if (colon_seen)
2144 if (gfc_match (" =>") == MATCH_YES)
2146 if (!current_attr.pointer)
2148 gfc_error ("Initialization at %C isn't for a pointer variable");
2149 m = MATCH_ERROR;
2150 goto cleanup;
2153 m = match_pointer_init (&initializer, 0);
2154 if (m != MATCH_YES)
2155 goto cleanup;
2157 else if (gfc_match_char ('=') == MATCH_YES)
2159 if (current_attr.pointer)
2161 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2162 "not %<=%>");
2163 m = MATCH_ERROR;
2164 goto cleanup;
2167 m = gfc_match_init_expr (&initializer);
2168 if (m == MATCH_NO)
2170 gfc_error ("Expected an initialization expression at %C");
2171 m = MATCH_ERROR;
2174 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2175 && gfc_state_stack->state != COMP_DERIVED)
2177 gfc_error ("Initialization of variable at %C is not allowed in "
2178 "a PURE procedure");
2179 m = MATCH_ERROR;
2182 if (current_attr.flavor != FL_PARAMETER
2183 && gfc_state_stack->state != COMP_DERIVED)
2184 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2186 if (m != MATCH_YES)
2187 goto cleanup;
2191 if (initializer != NULL && current_attr.allocatable
2192 && gfc_current_state () == COMP_DERIVED)
2194 gfc_error ("Initialization of allocatable component at %C is not "
2195 "allowed");
2196 m = MATCH_ERROR;
2197 goto cleanup;
2200 /* Add the initializer. Note that it is fine if initializer is
2201 NULL here, because we sometimes also need to check if a
2202 declaration *must* have an initialization expression. */
2203 if (gfc_current_state () != COMP_DERIVED)
2204 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2205 else
2207 if (current_ts.type == BT_DERIVED
2208 && !current_attr.pointer && !initializer)
2209 initializer = gfc_default_initializer (&current_ts);
2210 t = build_struct (name, cl, &initializer, &as);
2213 m = (t) ? MATCH_YES : MATCH_ERROR;
2215 cleanup:
2216 /* Free stuff up and return. */
2217 gfc_free_expr (initializer);
2218 gfc_free_array_spec (as);
2220 return m;
2224 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2225 This assumes that the byte size is equal to the kind number for
2226 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2228 match
2229 gfc_match_old_kind_spec (gfc_typespec *ts)
2231 match m;
2232 int original_kind;
2234 if (gfc_match_char ('*') != MATCH_YES)
2235 return MATCH_NO;
2237 m = gfc_match_small_literal_int (&ts->kind, NULL);
2238 if (m != MATCH_YES)
2239 return MATCH_ERROR;
2241 original_kind = ts->kind;
2243 /* Massage the kind numbers for complex types. */
2244 if (ts->type == BT_COMPLEX)
2246 if (ts->kind % 2)
2248 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2249 gfc_basic_typename (ts->type), original_kind);
2250 return MATCH_ERROR;
2252 ts->kind /= 2;
2256 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2257 ts->kind = 8;
2259 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2261 if (ts->kind == 4)
2263 if (flag_real4_kind == 8)
2264 ts->kind = 8;
2265 if (flag_real4_kind == 10)
2266 ts->kind = 10;
2267 if (flag_real4_kind == 16)
2268 ts->kind = 16;
2271 if (ts->kind == 8)
2273 if (flag_real8_kind == 4)
2274 ts->kind = 4;
2275 if (flag_real8_kind == 10)
2276 ts->kind = 10;
2277 if (flag_real8_kind == 16)
2278 ts->kind = 16;
2282 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2284 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2285 gfc_basic_typename (ts->type), original_kind);
2286 return MATCH_ERROR;
2289 if (!gfc_notify_std (GFC_STD_GNU,
2290 "Nonstandard type declaration %s*%d at %C",
2291 gfc_basic_typename(ts->type), original_kind))
2292 return MATCH_ERROR;
2294 return MATCH_YES;
2298 /* Match a kind specification. Since kinds are generally optional, we
2299 usually return MATCH_NO if something goes wrong. If a "kind="
2300 string is found, then we know we have an error. */
2302 match
2303 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2305 locus where, loc;
2306 gfc_expr *e;
2307 match m, n;
2308 char c;
2309 const char *msg;
2311 m = MATCH_NO;
2312 n = MATCH_YES;
2313 e = NULL;
2315 where = loc = gfc_current_locus;
2317 if (kind_expr_only)
2318 goto kind_expr;
2320 if (gfc_match_char ('(') == MATCH_NO)
2321 return MATCH_NO;
2323 /* Also gobbles optional text. */
2324 if (gfc_match (" kind = ") == MATCH_YES)
2325 m = MATCH_ERROR;
2327 loc = gfc_current_locus;
2329 kind_expr:
2330 n = gfc_match_init_expr (&e);
2332 if (n != MATCH_YES)
2334 if (gfc_matching_function)
2336 /* The function kind expression might include use associated or
2337 imported parameters and try again after the specification
2338 expressions..... */
2339 if (gfc_match_char (')') != MATCH_YES)
2341 gfc_error ("Missing right parenthesis at %C");
2342 m = MATCH_ERROR;
2343 goto no_match;
2346 gfc_free_expr (e);
2347 gfc_undo_symbols ();
2348 return MATCH_YES;
2350 else
2352 /* ....or else, the match is real. */
2353 if (n == MATCH_NO)
2354 gfc_error ("Expected initialization expression at %C");
2355 if (n != MATCH_YES)
2356 return MATCH_ERROR;
2360 if (e->rank != 0)
2362 gfc_error ("Expected scalar initialization expression at %C");
2363 m = MATCH_ERROR;
2364 goto no_match;
2367 msg = gfc_extract_int (e, &ts->kind);
2369 if (msg != NULL)
2371 gfc_error (msg);
2372 m = MATCH_ERROR;
2373 goto no_match;
2376 /* Before throwing away the expression, let's see if we had a
2377 C interoperable kind (and store the fact). */
2378 if (e->ts.is_c_interop == 1)
2380 /* Mark this as C interoperable if being declared with one
2381 of the named constants from iso_c_binding. */
2382 ts->is_c_interop = e->ts.is_iso_c;
2383 ts->f90_type = e->ts.f90_type;
2386 gfc_free_expr (e);
2387 e = NULL;
2389 /* Ignore errors to this point, if we've gotten here. This means
2390 we ignore the m=MATCH_ERROR from above. */
2391 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2393 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2394 gfc_basic_typename (ts->type));
2395 gfc_current_locus = where;
2396 return MATCH_ERROR;
2399 /* Warn if, e.g., c_int is used for a REAL variable, but not
2400 if, e.g., c_double is used for COMPLEX as the standard
2401 explicitly says that the kind type parameter for complex and real
2402 variable is the same, i.e. c_float == c_float_complex. */
2403 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2404 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2405 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2406 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2407 "is %s", gfc_basic_typename (ts->f90_type), &where,
2408 gfc_basic_typename (ts->type));
2410 gfc_gobble_whitespace ();
2411 if ((c = gfc_next_ascii_char ()) != ')'
2412 && (ts->type != BT_CHARACTER || c != ','))
2414 if (ts->type == BT_CHARACTER)
2415 gfc_error ("Missing right parenthesis or comma at %C");
2416 else
2417 gfc_error ("Missing right parenthesis at %C");
2418 m = MATCH_ERROR;
2420 else
2421 /* All tests passed. */
2422 m = MATCH_YES;
2424 if(m == MATCH_ERROR)
2425 gfc_current_locus = where;
2427 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2428 ts->kind = 8;
2430 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2432 if (ts->kind == 4)
2434 if (flag_real4_kind == 8)
2435 ts->kind = 8;
2436 if (flag_real4_kind == 10)
2437 ts->kind = 10;
2438 if (flag_real4_kind == 16)
2439 ts->kind = 16;
2442 if (ts->kind == 8)
2444 if (flag_real8_kind == 4)
2445 ts->kind = 4;
2446 if (flag_real8_kind == 10)
2447 ts->kind = 10;
2448 if (flag_real8_kind == 16)
2449 ts->kind = 16;
2453 /* Return what we know from the test(s). */
2454 return m;
2456 no_match:
2457 gfc_free_expr (e);
2458 gfc_current_locus = where;
2459 return m;
2463 static match
2464 match_char_kind (int * kind, int * is_iso_c)
2466 locus where;
2467 gfc_expr *e;
2468 match m, n;
2469 const char *msg;
2471 m = MATCH_NO;
2472 e = NULL;
2473 where = gfc_current_locus;
2475 n = gfc_match_init_expr (&e);
2477 if (n != MATCH_YES && gfc_matching_function)
2479 /* The expression might include use-associated or imported
2480 parameters and try again after the specification
2481 expressions. */
2482 gfc_free_expr (e);
2483 gfc_undo_symbols ();
2484 return MATCH_YES;
2487 if (n == MATCH_NO)
2488 gfc_error ("Expected initialization expression at %C");
2489 if (n != MATCH_YES)
2490 return MATCH_ERROR;
2492 if (e->rank != 0)
2494 gfc_error ("Expected scalar initialization expression at %C");
2495 m = MATCH_ERROR;
2496 goto no_match;
2499 msg = gfc_extract_int (e, kind);
2500 *is_iso_c = e->ts.is_iso_c;
2501 if (msg != NULL)
2503 gfc_error (msg);
2504 m = MATCH_ERROR;
2505 goto no_match;
2508 gfc_free_expr (e);
2510 /* Ignore errors to this point, if we've gotten here. This means
2511 we ignore the m=MATCH_ERROR from above. */
2512 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2514 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2515 m = MATCH_ERROR;
2517 else
2518 /* All tests passed. */
2519 m = MATCH_YES;
2521 if (m == MATCH_ERROR)
2522 gfc_current_locus = where;
2524 /* Return what we know from the test(s). */
2525 return m;
2527 no_match:
2528 gfc_free_expr (e);
2529 gfc_current_locus = where;
2530 return m;
2534 /* Match the various kind/length specifications in a CHARACTER
2535 declaration. We don't return MATCH_NO. */
2537 match
2538 gfc_match_char_spec (gfc_typespec *ts)
2540 int kind, seen_length, is_iso_c;
2541 gfc_charlen *cl;
2542 gfc_expr *len;
2543 match m;
2544 bool deferred;
2546 len = NULL;
2547 seen_length = 0;
2548 kind = 0;
2549 is_iso_c = 0;
2550 deferred = false;
2552 /* Try the old-style specification first. */
2553 old_char_selector = 0;
2555 m = match_char_length (&len, &deferred, true);
2556 if (m != MATCH_NO)
2558 if (m == MATCH_YES)
2559 old_char_selector = 1;
2560 seen_length = 1;
2561 goto done;
2564 m = gfc_match_char ('(');
2565 if (m != MATCH_YES)
2567 m = MATCH_YES; /* Character without length is a single char. */
2568 goto done;
2571 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2572 if (gfc_match (" kind =") == MATCH_YES)
2574 m = match_char_kind (&kind, &is_iso_c);
2576 if (m == MATCH_ERROR)
2577 goto done;
2578 if (m == MATCH_NO)
2579 goto syntax;
2581 if (gfc_match (" , len =") == MATCH_NO)
2582 goto rparen;
2584 m = char_len_param_value (&len, &deferred);
2585 if (m == MATCH_NO)
2586 goto syntax;
2587 if (m == MATCH_ERROR)
2588 goto done;
2589 seen_length = 1;
2591 goto rparen;
2594 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2595 if (gfc_match (" len =") == MATCH_YES)
2597 m = char_len_param_value (&len, &deferred);
2598 if (m == MATCH_NO)
2599 goto syntax;
2600 if (m == MATCH_ERROR)
2601 goto done;
2602 seen_length = 1;
2604 if (gfc_match_char (')') == MATCH_YES)
2605 goto done;
2607 if (gfc_match (" , kind =") != MATCH_YES)
2608 goto syntax;
2610 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2611 goto done;
2613 goto rparen;
2616 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2617 m = char_len_param_value (&len, &deferred);
2618 if (m == MATCH_NO)
2619 goto syntax;
2620 if (m == MATCH_ERROR)
2621 goto done;
2622 seen_length = 1;
2624 m = gfc_match_char (')');
2625 if (m == MATCH_YES)
2626 goto done;
2628 if (gfc_match_char (',') != MATCH_YES)
2629 goto syntax;
2631 gfc_match (" kind ="); /* Gobble optional text. */
2633 m = match_char_kind (&kind, &is_iso_c);
2634 if (m == MATCH_ERROR)
2635 goto done;
2636 if (m == MATCH_NO)
2637 goto syntax;
2639 rparen:
2640 /* Require a right-paren at this point. */
2641 m = gfc_match_char (')');
2642 if (m == MATCH_YES)
2643 goto done;
2645 syntax:
2646 gfc_error ("Syntax error in CHARACTER declaration at %C");
2647 m = MATCH_ERROR;
2648 gfc_free_expr (len);
2649 return m;
2651 done:
2652 /* Deal with character functions after USE and IMPORT statements. */
2653 if (gfc_matching_function)
2655 gfc_free_expr (len);
2656 gfc_undo_symbols ();
2657 return MATCH_YES;
2660 if (m != MATCH_YES)
2662 gfc_free_expr (len);
2663 return m;
2666 /* Do some final massaging of the length values. */
2667 cl = gfc_new_charlen (gfc_current_ns, NULL);
2669 if (seen_length == 0)
2670 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2671 else
2672 cl->length = len;
2674 ts->u.cl = cl;
2675 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2676 ts->deferred = deferred;
2678 /* We have to know if it was a C interoperable kind so we can
2679 do accurate type checking of bind(c) procs, etc. */
2680 if (kind != 0)
2681 /* Mark this as C interoperable if being declared with one
2682 of the named constants from iso_c_binding. */
2683 ts->is_c_interop = is_iso_c;
2684 else if (len != NULL)
2685 /* Here, we might have parsed something such as: character(c_char)
2686 In this case, the parsing code above grabs the c_char when
2687 looking for the length (line 1690, roughly). it's the last
2688 testcase for parsing the kind params of a character variable.
2689 However, it's not actually the length. this seems like it
2690 could be an error.
2691 To see if the user used a C interop kind, test the expr
2692 of the so called length, and see if it's C interoperable. */
2693 ts->is_c_interop = len->ts.is_iso_c;
2695 return MATCH_YES;
2699 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2700 structure to the matched specification. This is necessary for FUNCTION and
2701 IMPLICIT statements.
2703 If implicit_flag is nonzero, then we don't check for the optional
2704 kind specification. Not doing so is needed for matching an IMPLICIT
2705 statement correctly. */
2707 match
2708 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2710 char name[GFC_MAX_SYMBOL_LEN + 1];
2711 gfc_symbol *sym, *dt_sym;
2712 match m;
2713 char c;
2714 bool seen_deferred_kind, matched_type;
2715 const char *dt_name;
2717 /* A belt and braces check that the typespec is correctly being treated
2718 as a deferred characteristic association. */
2719 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2720 && (gfc_current_block ()->result->ts.kind == -1)
2721 && (ts->kind == -1);
2722 gfc_clear_ts (ts);
2723 if (seen_deferred_kind)
2724 ts->kind = -1;
2726 /* Clear the current binding label, in case one is given. */
2727 curr_binding_label = NULL;
2729 if (gfc_match (" byte") == MATCH_YES)
2731 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2732 return MATCH_ERROR;
2734 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2736 gfc_error ("BYTE type used at %C "
2737 "is not available on the target machine");
2738 return MATCH_ERROR;
2741 ts->type = BT_INTEGER;
2742 ts->kind = 1;
2743 return MATCH_YES;
2747 m = gfc_match (" type (");
2748 matched_type = (m == MATCH_YES);
2749 if (matched_type)
2751 gfc_gobble_whitespace ();
2752 if (gfc_peek_ascii_char () == '*')
2754 if ((m = gfc_match ("*)")) != MATCH_YES)
2755 return m;
2756 if (gfc_current_state () == COMP_DERIVED)
2758 gfc_error ("Assumed type at %C is not allowed for components");
2759 return MATCH_ERROR;
2761 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2762 "at %C"))
2763 return MATCH_ERROR;
2764 ts->type = BT_ASSUMED;
2765 return MATCH_YES;
2768 m = gfc_match ("%n", name);
2769 matched_type = (m == MATCH_YES);
2772 if ((matched_type && strcmp ("integer", name) == 0)
2773 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2775 ts->type = BT_INTEGER;
2776 ts->kind = gfc_default_integer_kind;
2777 goto get_kind;
2780 if ((matched_type && strcmp ("character", name) == 0)
2781 || (!matched_type && gfc_match (" character") == MATCH_YES))
2783 if (matched_type
2784 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2785 "intrinsic-type-spec at %C"))
2786 return MATCH_ERROR;
2788 ts->type = BT_CHARACTER;
2789 if (implicit_flag == 0)
2790 m = gfc_match_char_spec (ts);
2791 else
2792 m = MATCH_YES;
2794 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2795 m = MATCH_ERROR;
2797 return m;
2800 if ((matched_type && strcmp ("real", name) == 0)
2801 || (!matched_type && gfc_match (" real") == MATCH_YES))
2803 ts->type = BT_REAL;
2804 ts->kind = gfc_default_real_kind;
2805 goto get_kind;
2808 if ((matched_type
2809 && (strcmp ("doubleprecision", name) == 0
2810 || (strcmp ("double", name) == 0
2811 && gfc_match (" precision") == MATCH_YES)))
2812 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2814 if (matched_type
2815 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2816 "intrinsic-type-spec at %C"))
2817 return MATCH_ERROR;
2818 if (matched_type && gfc_match_char (')') != MATCH_YES)
2819 return MATCH_ERROR;
2821 ts->type = BT_REAL;
2822 ts->kind = gfc_default_double_kind;
2823 return MATCH_YES;
2826 if ((matched_type && strcmp ("complex", name) == 0)
2827 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2829 ts->type = BT_COMPLEX;
2830 ts->kind = gfc_default_complex_kind;
2831 goto get_kind;
2834 if ((matched_type
2835 && (strcmp ("doublecomplex", name) == 0
2836 || (strcmp ("double", name) == 0
2837 && gfc_match (" complex") == MATCH_YES)))
2838 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2840 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2841 return MATCH_ERROR;
2843 if (matched_type
2844 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2845 "intrinsic-type-spec at %C"))
2846 return MATCH_ERROR;
2848 if (matched_type && gfc_match_char (')') != MATCH_YES)
2849 return MATCH_ERROR;
2851 ts->type = BT_COMPLEX;
2852 ts->kind = gfc_default_double_kind;
2853 return MATCH_YES;
2856 if ((matched_type && strcmp ("logical", name) == 0)
2857 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2859 ts->type = BT_LOGICAL;
2860 ts->kind = gfc_default_logical_kind;
2861 goto get_kind;
2864 if (matched_type)
2865 m = gfc_match_char (')');
2867 if (m == MATCH_YES)
2868 ts->type = BT_DERIVED;
2869 else
2871 /* Match CLASS declarations. */
2872 m = gfc_match (" class ( * )");
2873 if (m == MATCH_ERROR)
2874 return MATCH_ERROR;
2875 else if (m == MATCH_YES)
2877 gfc_symbol *upe;
2878 gfc_symtree *st;
2879 ts->type = BT_CLASS;
2880 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2881 if (upe == NULL)
2883 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2884 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2885 st->n.sym = upe;
2886 gfc_set_sym_referenced (upe);
2887 upe->refs++;
2888 upe->ts.type = BT_VOID;
2889 upe->attr.unlimited_polymorphic = 1;
2890 /* This is essential to force the construction of
2891 unlimited polymorphic component class containers. */
2892 upe->attr.zero_comp = 1;
2893 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2894 &gfc_current_locus))
2895 return MATCH_ERROR;
2897 else
2899 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2900 if (st == NULL)
2901 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2902 st->n.sym = upe;
2903 upe->refs++;
2905 ts->u.derived = upe;
2906 return m;
2909 m = gfc_match (" class ( %n )", name);
2910 if (m != MATCH_YES)
2911 return m;
2912 ts->type = BT_CLASS;
2914 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2915 return MATCH_ERROR;
2918 /* Defer association of the derived type until the end of the
2919 specification block. However, if the derived type can be
2920 found, add it to the typespec. */
2921 if (gfc_matching_function)
2923 ts->u.derived = NULL;
2924 if (gfc_current_state () != COMP_INTERFACE
2925 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2927 sym = gfc_find_dt_in_generic (sym);
2928 ts->u.derived = sym;
2930 return MATCH_YES;
2933 /* Search for the name but allow the components to be defined later. If
2934 type = -1, this typespec has been seen in a function declaration but
2935 the type could not be accessed at that point. The actual derived type is
2936 stored in a symtree with the first letter of the name capitalized; the
2937 symtree with the all lower-case name contains the associated
2938 generic function. */
2939 dt_name = gfc_get_string ("%c%s",
2940 (char) TOUPPER ((unsigned char) name[0]),
2941 (const char*)&name[1]);
2942 sym = NULL;
2943 dt_sym = NULL;
2944 if (ts->kind != -1)
2946 gfc_get_ha_symbol (name, &sym);
2947 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2949 gfc_error ("Type name %qs at %C is ambiguous", name);
2950 return MATCH_ERROR;
2952 if (sym->generic && !dt_sym)
2953 dt_sym = gfc_find_dt_in_generic (sym);
2955 else if (ts->kind == -1)
2957 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2958 || gfc_current_ns->has_import_set;
2959 gfc_find_symbol (name, NULL, iface, &sym);
2960 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2962 gfc_error ("Type name %qs at %C is ambiguous", name);
2963 return MATCH_ERROR;
2965 if (sym && sym->generic && !dt_sym)
2966 dt_sym = gfc_find_dt_in_generic (sym);
2968 ts->kind = 0;
2969 if (sym == NULL)
2970 return MATCH_NO;
2973 if ((sym->attr.flavor != FL_UNKNOWN
2974 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2975 || sym->attr.subroutine)
2977 gfc_error ("Type name %qs at %C conflicts with previously declared "
2978 "entity at %L, which has the same name", name,
2979 &sym->declared_at);
2980 return MATCH_ERROR;
2983 gfc_save_symbol_data (sym);
2984 gfc_set_sym_referenced (sym);
2985 if (!sym->attr.generic
2986 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2987 return MATCH_ERROR;
2989 if (!sym->attr.function
2990 && !gfc_add_function (&sym->attr, sym->name, NULL))
2991 return MATCH_ERROR;
2993 if (!dt_sym)
2995 gfc_interface *intr, *head;
2997 /* Use upper case to save the actual derived-type symbol. */
2998 gfc_get_symbol (dt_name, NULL, &dt_sym);
2999 dt_sym->name = gfc_get_string (sym->name);
3000 head = sym->generic;
3001 intr = gfc_get_interface ();
3002 intr->sym = dt_sym;
3003 intr->where = gfc_current_locus;
3004 intr->next = head;
3005 sym->generic = intr;
3006 sym->attr.if_source = IFSRC_DECL;
3008 else
3009 gfc_save_symbol_data (dt_sym);
3011 gfc_set_sym_referenced (dt_sym);
3013 if (dt_sym->attr.flavor != FL_DERIVED
3014 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
3015 return MATCH_ERROR;
3017 ts->u.derived = dt_sym;
3019 return MATCH_YES;
3021 get_kind:
3022 if (matched_type
3023 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3024 "intrinsic-type-spec at %C"))
3025 return MATCH_ERROR;
3027 /* For all types except double, derived and character, look for an
3028 optional kind specifier. MATCH_NO is actually OK at this point. */
3029 if (implicit_flag == 1)
3031 if (matched_type && gfc_match_char (')') != MATCH_YES)
3032 return MATCH_ERROR;
3034 return MATCH_YES;
3037 if (gfc_current_form == FORM_FREE)
3039 c = gfc_peek_ascii_char ();
3040 if (!gfc_is_whitespace (c) && c != '*' && c != '('
3041 && c != ':' && c != ',')
3043 if (matched_type && c == ')')
3045 gfc_next_ascii_char ();
3046 return MATCH_YES;
3048 return MATCH_NO;
3052 m = gfc_match_kind_spec (ts, false);
3053 if (m == MATCH_NO && ts->type != BT_CHARACTER)
3055 m = gfc_match_old_kind_spec (ts);
3056 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
3057 return MATCH_ERROR;
3060 if (matched_type && gfc_match_char (')') != MATCH_YES)
3061 return MATCH_ERROR;
3063 /* Defer association of the KIND expression of function results
3064 until after USE and IMPORT statements. */
3065 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
3066 || gfc_matching_function)
3067 return MATCH_YES;
3069 if (m == MATCH_NO)
3070 m = MATCH_YES; /* No kind specifier found. */
3072 return m;
3076 /* Match an IMPLICIT NONE statement. Actually, this statement is
3077 already matched in parse.c, or we would not end up here in the
3078 first place. So the only thing we need to check, is if there is
3079 trailing garbage. If not, the match is successful. */
3081 match
3082 gfc_match_implicit_none (void)
3084 char c;
3085 match m;
3086 char name[GFC_MAX_SYMBOL_LEN + 1];
3087 bool type = false;
3088 bool external = false;
3089 locus cur_loc = gfc_current_locus;
3091 if (gfc_current_ns->seen_implicit_none
3092 || gfc_current_ns->has_implicit_none_export)
3094 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3095 return MATCH_ERROR;
3098 gfc_gobble_whitespace ();
3099 c = gfc_peek_ascii_char ();
3100 if (c == '(')
3102 (void) gfc_next_ascii_char ();
3103 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3104 return MATCH_ERROR;
3106 gfc_gobble_whitespace ();
3107 if (gfc_peek_ascii_char () == ')')
3109 (void) gfc_next_ascii_char ();
3110 type = true;
3112 else
3113 for(;;)
3115 m = gfc_match (" %n", name);
3116 if (m != MATCH_YES)
3117 return MATCH_ERROR;
3119 if (strcmp (name, "type") == 0)
3120 type = true;
3121 else if (strcmp (name, "external") == 0)
3122 external = true;
3123 else
3124 return MATCH_ERROR;
3126 gfc_gobble_whitespace ();
3127 c = gfc_next_ascii_char ();
3128 if (c == ',')
3129 continue;
3130 if (c == ')')
3131 break;
3132 return MATCH_ERROR;
3135 else
3136 type = true;
3138 if (gfc_match_eos () != MATCH_YES)
3139 return MATCH_ERROR;
3141 gfc_set_implicit_none (type, external, &cur_loc);
3143 return MATCH_YES;
3147 /* Match the letter range(s) of an IMPLICIT statement. */
3149 static match
3150 match_implicit_range (void)
3152 char c, c1, c2;
3153 int inner;
3154 locus cur_loc;
3156 cur_loc = gfc_current_locus;
3158 gfc_gobble_whitespace ();
3159 c = gfc_next_ascii_char ();
3160 if (c != '(')
3162 gfc_error ("Missing character range in IMPLICIT at %C");
3163 goto bad;
3166 inner = 1;
3167 while (inner)
3169 gfc_gobble_whitespace ();
3170 c1 = gfc_next_ascii_char ();
3171 if (!ISALPHA (c1))
3172 goto bad;
3174 gfc_gobble_whitespace ();
3175 c = gfc_next_ascii_char ();
3177 switch (c)
3179 case ')':
3180 inner = 0; /* Fall through. */
3182 case ',':
3183 c2 = c1;
3184 break;
3186 case '-':
3187 gfc_gobble_whitespace ();
3188 c2 = gfc_next_ascii_char ();
3189 if (!ISALPHA (c2))
3190 goto bad;
3192 gfc_gobble_whitespace ();
3193 c = gfc_next_ascii_char ();
3195 if ((c != ',') && (c != ')'))
3196 goto bad;
3197 if (c == ')')
3198 inner = 0;
3200 break;
3202 default:
3203 goto bad;
3206 if (c1 > c2)
3208 gfc_error ("Letters must be in alphabetic order in "
3209 "IMPLICIT statement at %C");
3210 goto bad;
3213 /* See if we can add the newly matched range to the pending
3214 implicits from this IMPLICIT statement. We do not check for
3215 conflicts with whatever earlier IMPLICIT statements may have
3216 set. This is done when we've successfully finished matching
3217 the current one. */
3218 if (!gfc_add_new_implicit_range (c1, c2))
3219 goto bad;
3222 return MATCH_YES;
3224 bad:
3225 gfc_syntax_error (ST_IMPLICIT);
3227 gfc_current_locus = cur_loc;
3228 return MATCH_ERROR;
3232 /* Match an IMPLICIT statement, storing the types for
3233 gfc_set_implicit() if the statement is accepted by the parser.
3234 There is a strange looking, but legal syntactic construction
3235 possible. It looks like:
3237 IMPLICIT INTEGER (a-b) (c-d)
3239 This is legal if "a-b" is a constant expression that happens to
3240 equal one of the legal kinds for integers. The real problem
3241 happens with an implicit specification that looks like:
3243 IMPLICIT INTEGER (a-b)
3245 In this case, a typespec matcher that is "greedy" (as most of the
3246 matchers are) gobbles the character range as a kindspec, leaving
3247 nothing left. We therefore have to go a bit more slowly in the
3248 matching process by inhibiting the kindspec checking during
3249 typespec matching and checking for a kind later. */
3251 match
3252 gfc_match_implicit (void)
3254 gfc_typespec ts;
3255 locus cur_loc;
3256 char c;
3257 match m;
3259 if (gfc_current_ns->seen_implicit_none)
3261 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3262 "statement");
3263 return MATCH_ERROR;
3266 gfc_clear_ts (&ts);
3268 /* We don't allow empty implicit statements. */
3269 if (gfc_match_eos () == MATCH_YES)
3271 gfc_error ("Empty IMPLICIT statement at %C");
3272 return MATCH_ERROR;
3277 /* First cleanup. */
3278 gfc_clear_new_implicit ();
3280 /* A basic type is mandatory here. */
3281 m = gfc_match_decl_type_spec (&ts, 1);
3282 if (m == MATCH_ERROR)
3283 goto error;
3284 if (m == MATCH_NO)
3285 goto syntax;
3287 cur_loc = gfc_current_locus;
3288 m = match_implicit_range ();
3290 if (m == MATCH_YES)
3292 /* We may have <TYPE> (<RANGE>). */
3293 gfc_gobble_whitespace ();
3294 c = gfc_peek_ascii_char ();
3295 if (c == ',' || c == '\n' || c == ';' || c == '!')
3297 /* Check for CHARACTER with no length parameter. */
3298 if (ts.type == BT_CHARACTER && !ts.u.cl)
3300 ts.kind = gfc_default_character_kind;
3301 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3302 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3303 NULL, 1);
3306 /* Record the Successful match. */
3307 if (!gfc_merge_new_implicit (&ts))
3308 return MATCH_ERROR;
3309 if (c == ',')
3310 c = gfc_next_ascii_char ();
3311 else if (gfc_match_eos () == MATCH_ERROR)
3312 goto error;
3313 continue;
3316 gfc_current_locus = cur_loc;
3319 /* Discard the (incorrectly) matched range. */
3320 gfc_clear_new_implicit ();
3322 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3323 if (ts.type == BT_CHARACTER)
3324 m = gfc_match_char_spec (&ts);
3325 else
3327 m = gfc_match_kind_spec (&ts, false);
3328 if (m == MATCH_NO)
3330 m = gfc_match_old_kind_spec (&ts);
3331 if (m == MATCH_ERROR)
3332 goto error;
3333 if (m == MATCH_NO)
3334 goto syntax;
3337 if (m == MATCH_ERROR)
3338 goto error;
3340 m = match_implicit_range ();
3341 if (m == MATCH_ERROR)
3342 goto error;
3343 if (m == MATCH_NO)
3344 goto syntax;
3346 gfc_gobble_whitespace ();
3347 c = gfc_next_ascii_char ();
3348 if (c != ',' && gfc_match_eos () != MATCH_YES)
3349 goto syntax;
3351 if (!gfc_merge_new_implicit (&ts))
3352 return MATCH_ERROR;
3354 while (c == ',');
3356 return MATCH_YES;
3358 syntax:
3359 gfc_syntax_error (ST_IMPLICIT);
3361 error:
3362 return MATCH_ERROR;
3366 match
3367 gfc_match_import (void)
3369 char name[GFC_MAX_SYMBOL_LEN + 1];
3370 match m;
3371 gfc_symbol *sym;
3372 gfc_symtree *st;
3374 if (gfc_current_ns->proc_name == NULL
3375 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3377 gfc_error ("IMPORT statement at %C only permitted in "
3378 "an INTERFACE body");
3379 return MATCH_ERROR;
3382 if (gfc_current_ns->proc_name->attr.module_procedure)
3384 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3385 "in a module procedure interface body");
3386 return MATCH_ERROR;
3389 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3390 return MATCH_ERROR;
3392 if (gfc_match_eos () == MATCH_YES)
3394 /* All host variables should be imported. */
3395 gfc_current_ns->has_import_set = 1;
3396 return MATCH_YES;
3399 if (gfc_match (" ::") == MATCH_YES)
3401 if (gfc_match_eos () == MATCH_YES)
3403 gfc_error ("Expecting list of named entities at %C");
3404 return MATCH_ERROR;
3408 for(;;)
3410 sym = NULL;
3411 m = gfc_match (" %n", name);
3412 switch (m)
3414 case MATCH_YES:
3415 if (gfc_current_ns->parent != NULL
3416 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3418 gfc_error ("Type name %qs at %C is ambiguous", name);
3419 return MATCH_ERROR;
3421 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3422 && gfc_find_symbol (name,
3423 gfc_current_ns->proc_name->ns->parent,
3424 1, &sym))
3426 gfc_error ("Type name %qs at %C is ambiguous", name);
3427 return MATCH_ERROR;
3430 if (sym == NULL)
3432 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3433 "at %C - does not exist.", name);
3434 return MATCH_ERROR;
3437 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3439 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3440 "at %C", name);
3441 goto next_item;
3444 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3445 st->n.sym = sym;
3446 sym->refs++;
3447 sym->attr.imported = 1;
3449 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3451 /* The actual derived type is stored in a symtree with the first
3452 letter of the name capitalized; the symtree with the all
3453 lower-case name contains the associated generic function. */
3454 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3455 gfc_get_string ("%c%s",
3456 (char) TOUPPER ((unsigned char) name[0]),
3457 &name[1]));
3458 st->n.sym = sym;
3459 sym->refs++;
3460 sym->attr.imported = 1;
3463 goto next_item;
3465 case MATCH_NO:
3466 break;
3468 case MATCH_ERROR:
3469 return MATCH_ERROR;
3472 next_item:
3473 if (gfc_match_eos () == MATCH_YES)
3474 break;
3475 if (gfc_match_char (',') != MATCH_YES)
3476 goto syntax;
3479 return MATCH_YES;
3481 syntax:
3482 gfc_error ("Syntax error in IMPORT statement at %C");
3483 return MATCH_ERROR;
3487 /* A minimal implementation of gfc_match without whitespace, escape
3488 characters or variable arguments. Returns true if the next
3489 characters match the TARGET template exactly. */
3491 static bool
3492 match_string_p (const char *target)
3494 const char *p;
3496 for (p = target; *p; p++)
3497 if ((char) gfc_next_ascii_char () != *p)
3498 return false;
3499 return true;
3502 /* Matches an attribute specification including array specs. If
3503 successful, leaves the variables current_attr and current_as
3504 holding the specification. Also sets the colon_seen variable for
3505 later use by matchers associated with initializations.
3507 This subroutine is a little tricky in the sense that we don't know
3508 if we really have an attr-spec until we hit the double colon.
3509 Until that time, we can only return MATCH_NO. This forces us to
3510 check for duplicate specification at this level. */
3512 static match
3513 match_attr_spec (void)
3515 /* Modifiers that can exist in a type statement. */
3516 enum
3517 { GFC_DECL_BEGIN = 0,
3518 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3519 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3520 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3521 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3522 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3523 DECL_NONE, GFC_DECL_END /* Sentinel */
3526 /* GFC_DECL_END is the sentinel, index starts at 0. */
3527 #define NUM_DECL GFC_DECL_END
3529 locus start, seen_at[NUM_DECL];
3530 int seen[NUM_DECL];
3531 unsigned int d;
3532 const char *attr;
3533 match m;
3534 bool t;
3536 gfc_clear_attr (&current_attr);
3537 start = gfc_current_locus;
3539 current_as = NULL;
3540 colon_seen = 0;
3542 /* See if we get all of the keywords up to the final double colon. */
3543 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3544 seen[d] = 0;
3546 for (;;)
3548 char ch;
3550 d = DECL_NONE;
3551 gfc_gobble_whitespace ();
3553 ch = gfc_next_ascii_char ();
3554 if (ch == ':')
3556 /* This is the successful exit condition for the loop. */
3557 if (gfc_next_ascii_char () == ':')
3558 break;
3560 else if (ch == ',')
3562 gfc_gobble_whitespace ();
3563 switch (gfc_peek_ascii_char ())
3565 case 'a':
3566 gfc_next_ascii_char ();
3567 switch (gfc_next_ascii_char ())
3569 case 'l':
3570 if (match_string_p ("locatable"))
3572 /* Matched "allocatable". */
3573 d = DECL_ALLOCATABLE;
3575 break;
3577 case 's':
3578 if (match_string_p ("ynchronous"))
3580 /* Matched "asynchronous". */
3581 d = DECL_ASYNCHRONOUS;
3583 break;
3585 break;
3587 case 'b':
3588 /* Try and match the bind(c). */
3589 m = gfc_match_bind_c (NULL, true);
3590 if (m == MATCH_YES)
3591 d = DECL_IS_BIND_C;
3592 else if (m == MATCH_ERROR)
3593 goto cleanup;
3594 break;
3596 case 'c':
3597 gfc_next_ascii_char ();
3598 if ('o' != gfc_next_ascii_char ())
3599 break;
3600 switch (gfc_next_ascii_char ())
3602 case 'd':
3603 if (match_string_p ("imension"))
3605 d = DECL_CODIMENSION;
3606 break;
3608 case 'n':
3609 if (match_string_p ("tiguous"))
3611 d = DECL_CONTIGUOUS;
3612 break;
3615 break;
3617 case 'd':
3618 if (match_string_p ("dimension"))
3619 d = DECL_DIMENSION;
3620 break;
3622 case 'e':
3623 if (match_string_p ("external"))
3624 d = DECL_EXTERNAL;
3625 break;
3627 case 'i':
3628 if (match_string_p ("int"))
3630 ch = gfc_next_ascii_char ();
3631 if (ch == 'e')
3633 if (match_string_p ("nt"))
3635 /* Matched "intent". */
3636 /* TODO: Call match_intent_spec from here. */
3637 if (gfc_match (" ( in out )") == MATCH_YES)
3638 d = DECL_INOUT;
3639 else if (gfc_match (" ( in )") == MATCH_YES)
3640 d = DECL_IN;
3641 else if (gfc_match (" ( out )") == MATCH_YES)
3642 d = DECL_OUT;
3645 else if (ch == 'r')
3647 if (match_string_p ("insic"))
3649 /* Matched "intrinsic". */
3650 d = DECL_INTRINSIC;
3654 break;
3656 case 'o':
3657 if (match_string_p ("optional"))
3658 d = DECL_OPTIONAL;
3659 break;
3661 case 'p':
3662 gfc_next_ascii_char ();
3663 switch (gfc_next_ascii_char ())
3665 case 'a':
3666 if (match_string_p ("rameter"))
3668 /* Matched "parameter". */
3669 d = DECL_PARAMETER;
3671 break;
3673 case 'o':
3674 if (match_string_p ("inter"))
3676 /* Matched "pointer". */
3677 d = DECL_POINTER;
3679 break;
3681 case 'r':
3682 ch = gfc_next_ascii_char ();
3683 if (ch == 'i')
3685 if (match_string_p ("vate"))
3687 /* Matched "private". */
3688 d = DECL_PRIVATE;
3691 else if (ch == 'o')
3693 if (match_string_p ("tected"))
3695 /* Matched "protected". */
3696 d = DECL_PROTECTED;
3699 break;
3701 case 'u':
3702 if (match_string_p ("blic"))
3704 /* Matched "public". */
3705 d = DECL_PUBLIC;
3707 break;
3709 break;
3711 case 's':
3712 if (match_string_p ("save"))
3713 d = DECL_SAVE;
3714 break;
3716 case 't':
3717 if (match_string_p ("target"))
3718 d = DECL_TARGET;
3719 break;
3721 case 'v':
3722 gfc_next_ascii_char ();
3723 ch = gfc_next_ascii_char ();
3724 if (ch == 'a')
3726 if (match_string_p ("lue"))
3728 /* Matched "value". */
3729 d = DECL_VALUE;
3732 else if (ch == 'o')
3734 if (match_string_p ("latile"))
3736 /* Matched "volatile". */
3737 d = DECL_VOLATILE;
3740 break;
3744 /* No double colon and no recognizable decl_type, so assume that
3745 we've been looking at something else the whole time. */
3746 if (d == DECL_NONE)
3748 m = MATCH_NO;
3749 goto cleanup;
3752 /* Check to make sure any parens are paired up correctly. */
3753 if (gfc_match_parens () == MATCH_ERROR)
3755 m = MATCH_ERROR;
3756 goto cleanup;
3759 seen[d]++;
3760 seen_at[d] = gfc_current_locus;
3762 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3764 gfc_array_spec *as = NULL;
3766 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3767 d == DECL_CODIMENSION);
3769 if (current_as == NULL)
3770 current_as = as;
3771 else if (m == MATCH_YES)
3773 if (!merge_array_spec (as, current_as, false))
3774 m = MATCH_ERROR;
3775 free (as);
3778 if (m == MATCH_NO)
3780 if (d == DECL_CODIMENSION)
3781 gfc_error ("Missing codimension specification at %C");
3782 else
3783 gfc_error ("Missing dimension specification at %C");
3784 m = MATCH_ERROR;
3787 if (m == MATCH_ERROR)
3788 goto cleanup;
3792 /* Since we've seen a double colon, we have to be looking at an
3793 attr-spec. This means that we can now issue errors. */
3794 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3795 if (seen[d] > 1)
3797 switch (d)
3799 case DECL_ALLOCATABLE:
3800 attr = "ALLOCATABLE";
3801 break;
3802 case DECL_ASYNCHRONOUS:
3803 attr = "ASYNCHRONOUS";
3804 break;
3805 case DECL_CODIMENSION:
3806 attr = "CODIMENSION";
3807 break;
3808 case DECL_CONTIGUOUS:
3809 attr = "CONTIGUOUS";
3810 break;
3811 case DECL_DIMENSION:
3812 attr = "DIMENSION";
3813 break;
3814 case DECL_EXTERNAL:
3815 attr = "EXTERNAL";
3816 break;
3817 case DECL_IN:
3818 attr = "INTENT (IN)";
3819 break;
3820 case DECL_OUT:
3821 attr = "INTENT (OUT)";
3822 break;
3823 case DECL_INOUT:
3824 attr = "INTENT (IN OUT)";
3825 break;
3826 case DECL_INTRINSIC:
3827 attr = "INTRINSIC";
3828 break;
3829 case DECL_OPTIONAL:
3830 attr = "OPTIONAL";
3831 break;
3832 case DECL_PARAMETER:
3833 attr = "PARAMETER";
3834 break;
3835 case DECL_POINTER:
3836 attr = "POINTER";
3837 break;
3838 case DECL_PROTECTED:
3839 attr = "PROTECTED";
3840 break;
3841 case DECL_PRIVATE:
3842 attr = "PRIVATE";
3843 break;
3844 case DECL_PUBLIC:
3845 attr = "PUBLIC";
3846 break;
3847 case DECL_SAVE:
3848 attr = "SAVE";
3849 break;
3850 case DECL_TARGET:
3851 attr = "TARGET";
3852 break;
3853 case DECL_IS_BIND_C:
3854 attr = "IS_BIND_C";
3855 break;
3856 case DECL_VALUE:
3857 attr = "VALUE";
3858 break;
3859 case DECL_VOLATILE:
3860 attr = "VOLATILE";
3861 break;
3862 default:
3863 attr = NULL; /* This shouldn't happen. */
3866 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3867 m = MATCH_ERROR;
3868 goto cleanup;
3871 /* Now that we've dealt with duplicate attributes, add the attributes
3872 to the current attribute. */
3873 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3875 if (seen[d] == 0)
3876 continue;
3878 if (gfc_current_state () == COMP_DERIVED
3879 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3880 && d != DECL_POINTER && d != DECL_PRIVATE
3881 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3883 if (d == DECL_ALLOCATABLE)
3885 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3886 "attribute at %C in a TYPE definition"))
3888 m = MATCH_ERROR;
3889 goto cleanup;
3892 else
3894 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3895 &seen_at[d]);
3896 m = MATCH_ERROR;
3897 goto cleanup;
3901 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3902 && gfc_current_state () != COMP_MODULE)
3904 if (d == DECL_PRIVATE)
3905 attr = "PRIVATE";
3906 else
3907 attr = "PUBLIC";
3908 if (gfc_current_state () == COMP_DERIVED
3909 && gfc_state_stack->previous
3910 && gfc_state_stack->previous->state == COMP_MODULE)
3912 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3913 "at %L in a TYPE definition", attr,
3914 &seen_at[d]))
3916 m = MATCH_ERROR;
3917 goto cleanup;
3920 else
3922 gfc_error ("%s attribute at %L is not allowed outside of the "
3923 "specification part of a module", attr, &seen_at[d]);
3924 m = MATCH_ERROR;
3925 goto cleanup;
3929 switch (d)
3931 case DECL_ALLOCATABLE:
3932 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3933 break;
3935 case DECL_ASYNCHRONOUS:
3936 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3937 t = false;
3938 else
3939 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3940 break;
3942 case DECL_CODIMENSION:
3943 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3944 break;
3946 case DECL_CONTIGUOUS:
3947 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3948 t = false;
3949 else
3950 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3951 break;
3953 case DECL_DIMENSION:
3954 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3955 break;
3957 case DECL_EXTERNAL:
3958 t = gfc_add_external (&current_attr, &seen_at[d]);
3959 break;
3961 case DECL_IN:
3962 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3963 break;
3965 case DECL_OUT:
3966 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3967 break;
3969 case DECL_INOUT:
3970 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3971 break;
3973 case DECL_INTRINSIC:
3974 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3975 break;
3977 case DECL_OPTIONAL:
3978 t = gfc_add_optional (&current_attr, &seen_at[d]);
3979 break;
3981 case DECL_PARAMETER:
3982 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3983 break;
3985 case DECL_POINTER:
3986 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3987 break;
3989 case DECL_PROTECTED:
3990 if (gfc_current_state () != COMP_MODULE
3991 || (gfc_current_ns->proc_name
3992 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
3994 gfc_error ("PROTECTED at %C only allowed in specification "
3995 "part of a module");
3996 t = false;
3997 break;
4000 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
4001 t = false;
4002 else
4003 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
4004 break;
4006 case DECL_PRIVATE:
4007 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
4008 &seen_at[d]);
4009 break;
4011 case DECL_PUBLIC:
4012 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
4013 &seen_at[d]);
4014 break;
4016 case DECL_SAVE:
4017 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
4018 break;
4020 case DECL_TARGET:
4021 t = gfc_add_target (&current_attr, &seen_at[d]);
4022 break;
4024 case DECL_IS_BIND_C:
4025 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
4026 break;
4028 case DECL_VALUE:
4029 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
4030 t = false;
4031 else
4032 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
4033 break;
4035 case DECL_VOLATILE:
4036 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
4037 t = false;
4038 else
4039 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
4040 break;
4042 default:
4043 gfc_internal_error ("match_attr_spec(): Bad attribute");
4046 if (!t)
4048 m = MATCH_ERROR;
4049 goto cleanup;
4053 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4054 if ((gfc_current_state () == COMP_MODULE
4055 || gfc_current_state () == COMP_SUBMODULE)
4056 && !current_attr.save
4057 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4058 current_attr.save = SAVE_IMPLICIT;
4060 colon_seen = 1;
4061 return MATCH_YES;
4063 cleanup:
4064 gfc_current_locus = start;
4065 gfc_free_array_spec (current_as);
4066 current_as = NULL;
4067 return m;
4071 /* Set the binding label, dest_label, either with the binding label
4072 stored in the given gfc_typespec, ts, or if none was provided, it
4073 will be the symbol name in all lower case, as required by the draft
4074 (J3/04-007, section 15.4.1). If a binding label was given and
4075 there is more than one argument (num_idents), it is an error. */
4077 static bool
4078 set_binding_label (const char **dest_label, const char *sym_name,
4079 int num_idents)
4081 if (num_idents > 1 && has_name_equals)
4083 gfc_error ("Multiple identifiers provided with "
4084 "single NAME= specifier at %C");
4085 return false;
4088 if (curr_binding_label)
4089 /* Binding label given; store in temp holder till have sym. */
4090 *dest_label = curr_binding_label;
4091 else
4093 /* No binding label given, and the NAME= specifier did not exist,
4094 which means there was no NAME="". */
4095 if (sym_name != NULL && has_name_equals == 0)
4096 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4099 return true;
4103 /* Set the status of the given common block as being BIND(C) or not,
4104 depending on the given parameter, is_bind_c. */
4106 void
4107 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4109 com_block->is_bind_c = is_bind_c;
4110 return;
4114 /* Verify that the given gfc_typespec is for a C interoperable type. */
4116 bool
4117 gfc_verify_c_interop (gfc_typespec *ts)
4119 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4120 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4121 ? true : false;
4122 else if (ts->type == BT_CLASS)
4123 return false;
4124 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4125 return false;
4127 return true;
4131 /* Verify that the variables of a given common block, which has been
4132 defined with the attribute specifier bind(c), to be of a C
4133 interoperable type. Errors will be reported here, if
4134 encountered. */
4136 bool
4137 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4139 gfc_symbol *curr_sym = NULL;
4140 bool retval = true;
4142 curr_sym = com_block->head;
4144 /* Make sure we have at least one symbol. */
4145 if (curr_sym == NULL)
4146 return retval;
4148 /* Here we know we have a symbol, so we'll execute this loop
4149 at least once. */
4152 /* The second to last param, 1, says this is in a common block. */
4153 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4154 curr_sym = curr_sym->common_next;
4155 } while (curr_sym != NULL);
4157 return retval;
4161 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4162 an appropriate error message is reported. */
4164 bool
4165 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4166 int is_in_common, gfc_common_head *com_block)
4168 bool bind_c_function = false;
4169 bool retval = true;
4171 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4172 bind_c_function = true;
4174 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4176 tmp_sym = tmp_sym->result;
4177 /* Make sure it wasn't an implicitly typed result. */
4178 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4180 gfc_warning (OPT_Wc_binding_type,
4181 "Implicitly declared BIND(C) function %qs at "
4182 "%L may not be C interoperable", tmp_sym->name,
4183 &tmp_sym->declared_at);
4184 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4185 /* Mark it as C interoperable to prevent duplicate warnings. */
4186 tmp_sym->ts.is_c_interop = 1;
4187 tmp_sym->attr.is_c_interop = 1;
4191 /* Here, we know we have the bind(c) attribute, so if we have
4192 enough type info, then verify that it's a C interop kind.
4193 The info could be in the symbol already, or possibly still in
4194 the given ts (current_ts), so look in both. */
4195 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4197 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4199 /* See if we're dealing with a sym in a common block or not. */
4200 if (is_in_common == 1 && warn_c_binding_type)
4202 gfc_warning (OPT_Wc_binding_type,
4203 "Variable %qs in common block %qs at %L "
4204 "may not be a C interoperable "
4205 "kind though common block %qs is BIND(C)",
4206 tmp_sym->name, com_block->name,
4207 &(tmp_sym->declared_at), com_block->name);
4209 else
4211 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4212 gfc_error ("Type declaration %qs at %L is not C "
4213 "interoperable but it is BIND(C)",
4214 tmp_sym->name, &(tmp_sym->declared_at));
4215 else if (warn_c_binding_type)
4216 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4217 "may not be a C interoperable "
4218 "kind but it is BIND(C)",
4219 tmp_sym->name, &(tmp_sym->declared_at));
4223 /* Variables declared w/in a common block can't be bind(c)
4224 since there's no way for C to see these variables, so there's
4225 semantically no reason for the attribute. */
4226 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4228 gfc_error ("Variable %qs in common block %qs at "
4229 "%L cannot be declared with BIND(C) "
4230 "since it is not a global",
4231 tmp_sym->name, com_block->name,
4232 &(tmp_sym->declared_at));
4233 retval = false;
4236 /* Scalar variables that are bind(c) can not have the pointer
4237 or allocatable attributes. */
4238 if (tmp_sym->attr.is_bind_c == 1)
4240 if (tmp_sym->attr.pointer == 1)
4242 gfc_error ("Variable %qs at %L cannot have both the "
4243 "POINTER and BIND(C) attributes",
4244 tmp_sym->name, &(tmp_sym->declared_at));
4245 retval = false;
4248 if (tmp_sym->attr.allocatable == 1)
4250 gfc_error ("Variable %qs at %L cannot have both the "
4251 "ALLOCATABLE and BIND(C) attributes",
4252 tmp_sym->name, &(tmp_sym->declared_at));
4253 retval = false;
4258 /* If it is a BIND(C) function, make sure the return value is a
4259 scalar value. The previous tests in this function made sure
4260 the type is interoperable. */
4261 if (bind_c_function && tmp_sym->as != NULL)
4262 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4263 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4265 /* BIND(C) functions can not return a character string. */
4266 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4267 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4268 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4269 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4270 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4271 "be a character string", tmp_sym->name,
4272 &(tmp_sym->declared_at));
4275 /* See if the symbol has been marked as private. If it has, make sure
4276 there is no binding label and warn the user if there is one. */
4277 if (tmp_sym->attr.access == ACCESS_PRIVATE
4278 && tmp_sym->binding_label)
4279 /* Use gfc_warning_now because we won't say that the symbol fails
4280 just because of this. */
4281 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4282 "given the binding label %qs", tmp_sym->name,
4283 &(tmp_sym->declared_at), tmp_sym->binding_label);
4285 return retval;
4289 /* Set the appropriate fields for a symbol that's been declared as
4290 BIND(C) (the is_bind_c flag and the binding label), and verify that
4291 the type is C interoperable. Errors are reported by the functions
4292 used to set/test these fields. */
4294 bool
4295 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4297 bool retval = true;
4299 /* TODO: Do we need to make sure the vars aren't marked private? */
4301 /* Set the is_bind_c bit in symbol_attribute. */
4302 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4304 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4305 return false;
4307 return retval;
4311 /* Set the fields marking the given common block as BIND(C), including
4312 a binding label, and report any errors encountered. */
4314 bool
4315 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4317 bool retval = true;
4319 /* destLabel, common name, typespec (which may have binding label). */
4320 if (!set_binding_label (&com_block->binding_label, com_block->name,
4321 num_idents))
4322 return false;
4324 /* Set the given common block (com_block) to being bind(c) (1). */
4325 set_com_block_bind_c (com_block, 1);
4327 return retval;
4331 /* Retrieve the list of one or more identifiers that the given bind(c)
4332 attribute applies to. */
4334 bool
4335 get_bind_c_idents (void)
4337 char name[GFC_MAX_SYMBOL_LEN + 1];
4338 int num_idents = 0;
4339 gfc_symbol *tmp_sym = NULL;
4340 match found_id;
4341 gfc_common_head *com_block = NULL;
4343 if (gfc_match_name (name) == MATCH_YES)
4345 found_id = MATCH_YES;
4346 gfc_get_ha_symbol (name, &tmp_sym);
4348 else if (match_common_name (name) == MATCH_YES)
4350 found_id = MATCH_YES;
4351 com_block = gfc_get_common (name, 0);
4353 else
4355 gfc_error ("Need either entity or common block name for "
4356 "attribute specification statement at %C");
4357 return false;
4360 /* Save the current identifier and look for more. */
4363 /* Increment the number of identifiers found for this spec stmt. */
4364 num_idents++;
4366 /* Make sure we have a sym or com block, and verify that it can
4367 be bind(c). Set the appropriate field(s) and look for more
4368 identifiers. */
4369 if (tmp_sym != NULL || com_block != NULL)
4371 if (tmp_sym != NULL)
4373 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4374 return false;
4376 else
4378 if (!set_verify_bind_c_com_block (com_block, num_idents))
4379 return false;
4382 /* Look to see if we have another identifier. */
4383 tmp_sym = NULL;
4384 if (gfc_match_eos () == MATCH_YES)
4385 found_id = MATCH_NO;
4386 else if (gfc_match_char (',') != MATCH_YES)
4387 found_id = MATCH_NO;
4388 else if (gfc_match_name (name) == MATCH_YES)
4390 found_id = MATCH_YES;
4391 gfc_get_ha_symbol (name, &tmp_sym);
4393 else if (match_common_name (name) == MATCH_YES)
4395 found_id = MATCH_YES;
4396 com_block = gfc_get_common (name, 0);
4398 else
4400 gfc_error ("Missing entity or common block name for "
4401 "attribute specification statement at %C");
4402 return false;
4405 else
4407 gfc_internal_error ("Missing symbol");
4409 } while (found_id == MATCH_YES);
4411 /* if we get here we were successful */
4412 return true;
4416 /* Try and match a BIND(C) attribute specification statement. */
4418 match
4419 gfc_match_bind_c_stmt (void)
4421 match found_match = MATCH_NO;
4422 gfc_typespec *ts;
4424 ts = &current_ts;
4426 /* This may not be necessary. */
4427 gfc_clear_ts (ts);
4428 /* Clear the temporary binding label holder. */
4429 curr_binding_label = NULL;
4431 /* Look for the bind(c). */
4432 found_match = gfc_match_bind_c (NULL, true);
4434 if (found_match == MATCH_YES)
4436 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4437 return MATCH_ERROR;
4439 /* Look for the :: now, but it is not required. */
4440 gfc_match (" :: ");
4442 /* Get the identifier(s) that needs to be updated. This may need to
4443 change to hand the flag(s) for the attr specified so all identifiers
4444 found can have all appropriate parts updated (assuming that the same
4445 spec stmt can have multiple attrs, such as both bind(c) and
4446 allocatable...). */
4447 if (!get_bind_c_idents ())
4448 /* Error message should have printed already. */
4449 return MATCH_ERROR;
4452 return found_match;
4456 /* Match a data declaration statement. */
4458 match
4459 gfc_match_data_decl (void)
4461 gfc_symbol *sym;
4462 match m;
4463 int elem;
4465 num_idents_on_line = 0;
4467 m = gfc_match_decl_type_spec (&current_ts, 0);
4468 if (m != MATCH_YES)
4469 return m;
4471 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4472 && gfc_current_state () != COMP_DERIVED)
4474 sym = gfc_use_derived (current_ts.u.derived);
4476 if (sym == NULL)
4478 m = MATCH_ERROR;
4479 goto cleanup;
4482 current_ts.u.derived = sym;
4485 m = match_attr_spec ();
4486 if (m == MATCH_ERROR)
4488 m = MATCH_NO;
4489 goto cleanup;
4492 if (current_ts.type == BT_CLASS
4493 && current_ts.u.derived->attr.unlimited_polymorphic)
4494 goto ok;
4496 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4497 && current_ts.u.derived->components == NULL
4498 && !current_ts.u.derived->attr.zero_comp)
4501 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4502 goto ok;
4504 gfc_find_symbol (current_ts.u.derived->name,
4505 current_ts.u.derived->ns, 1, &sym);
4507 /* Any symbol that we find had better be a type definition
4508 which has its components defined. */
4509 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4510 && (current_ts.u.derived->components != NULL
4511 || current_ts.u.derived->attr.zero_comp))
4512 goto ok;
4514 gfc_error ("Derived type at %C has not been previously defined "
4515 "and so cannot appear in a derived type definition");
4516 m = MATCH_ERROR;
4517 goto cleanup;
4521 /* If we have an old-style character declaration, and no new-style
4522 attribute specifications, then there a comma is optional between
4523 the type specification and the variable list. */
4524 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4525 gfc_match_char (',');
4527 /* Give the types/attributes to symbols that follow. Give the element
4528 a number so that repeat character length expressions can be copied. */
4529 elem = 1;
4530 for (;;)
4532 num_idents_on_line++;
4533 m = variable_decl (elem++);
4534 if (m == MATCH_ERROR)
4535 goto cleanup;
4536 if (m == MATCH_NO)
4537 break;
4539 if (gfc_match_eos () == MATCH_YES)
4540 goto cleanup;
4541 if (gfc_match_char (',') != MATCH_YES)
4542 break;
4545 if (!gfc_error_flag_test ())
4546 gfc_error ("Syntax error in data declaration at %C");
4547 m = MATCH_ERROR;
4549 gfc_free_data_all (gfc_current_ns);
4551 cleanup:
4552 gfc_free_array_spec (current_as);
4553 current_as = NULL;
4554 return m;
4558 /* Match a prefix associated with a function or subroutine
4559 declaration. If the typespec pointer is nonnull, then a typespec
4560 can be matched. Note that if nothing matches, MATCH_YES is
4561 returned (the null string was matched). */
4563 match
4564 gfc_match_prefix (gfc_typespec *ts)
4566 bool seen_type;
4567 bool seen_impure;
4568 bool found_prefix;
4570 gfc_clear_attr (&current_attr);
4571 seen_type = false;
4572 seen_impure = false;
4574 gcc_assert (!gfc_matching_prefix);
4575 gfc_matching_prefix = true;
4579 found_prefix = false;
4581 if (!seen_type && ts != NULL
4582 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4583 && gfc_match_space () == MATCH_YES)
4586 seen_type = true;
4587 found_prefix = true;
4590 if (gfc_match ("elemental% ") == MATCH_YES)
4592 if (!gfc_add_elemental (&current_attr, NULL))
4593 goto error;
4595 found_prefix = true;
4598 if (gfc_match ("pure% ") == MATCH_YES)
4600 if (!gfc_add_pure (&current_attr, NULL))
4601 goto error;
4603 found_prefix = true;
4606 if (gfc_match ("recursive% ") == MATCH_YES)
4608 if (!gfc_add_recursive (&current_attr, NULL))
4609 goto error;
4611 found_prefix = true;
4614 /* IMPURE is a somewhat special case, as it needs not set an actual
4615 attribute but rather only prevents ELEMENTAL routines from being
4616 automatically PURE. */
4617 if (gfc_match ("impure% ") == MATCH_YES)
4619 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4620 goto error;
4622 seen_impure = true;
4623 found_prefix = true;
4626 while (found_prefix);
4628 /* IMPURE and PURE must not both appear, of course. */
4629 if (seen_impure && current_attr.pure)
4631 gfc_error ("PURE and IMPURE must not appear both at %C");
4632 goto error;
4635 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4636 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4638 if (!gfc_add_pure (&current_attr, NULL))
4639 goto error;
4642 /* At this point, the next item is not a prefix. */
4643 gcc_assert (gfc_matching_prefix);
4645 /* MODULE should be the last prefix before FUNCTION or SUBROUTINE.
4646 Since this is a prefix like PURE, ELEMENTAL, etc., having a
4647 corresponding attribute seems natural and distinguishes these
4648 procedures from procedure types of PROC_MODULE, which these are
4649 as well. */
4650 if ((gfc_current_state () == COMP_INTERFACE
4651 || gfc_current_state () == COMP_CONTAINS)
4652 && gfc_match ("module% ") == MATCH_YES)
4654 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4655 goto error;
4656 else
4657 current_attr.module_procedure = 1;
4660 gfc_matching_prefix = false;
4661 return MATCH_YES;
4663 error:
4664 gcc_assert (gfc_matching_prefix);
4665 gfc_matching_prefix = false;
4666 return MATCH_ERROR;
4670 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4672 static bool
4673 copy_prefix (symbol_attribute *dest, locus *where)
4675 if (current_attr.pure && !gfc_add_pure (dest, where))
4676 return false;
4678 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4679 return false;
4681 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4682 return false;
4684 return true;
4688 /* Match a formal argument list. */
4690 match
4691 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4693 gfc_formal_arglist *head, *tail, *p, *q;
4694 char name[GFC_MAX_SYMBOL_LEN + 1];
4695 gfc_symbol *sym;
4696 match m;
4697 gfc_formal_arglist *formal = NULL;
4699 head = tail = NULL;
4701 /* Keep the interface formal argument list and null it so that the
4702 matching for the new declaration can be done. The numbers and
4703 names of the arguments are checked here. The interface formal
4704 arguments are retained in formal_arglist and the characteristics
4705 are compared in resolve.c(resolve_fl_procedure). See the remark
4706 in get_proc_name about the eventual need to copy the formal_arglist
4707 and populate the formal namespace of the interface symbol. */
4708 if (progname->attr.module_procedure
4709 && progname->attr.host_assoc)
4711 formal = progname->formal;
4712 progname->formal = NULL;
4715 if (gfc_match_char ('(') != MATCH_YES)
4717 if (null_flag)
4718 goto ok;
4719 return MATCH_NO;
4722 if (gfc_match_char (')') == MATCH_YES)
4723 goto ok;
4725 for (;;)
4727 if (gfc_match_char ('*') == MATCH_YES)
4729 sym = NULL;
4730 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4731 "at %C"))
4733 m = MATCH_ERROR;
4734 goto cleanup;
4737 else
4739 m = gfc_match_name (name);
4740 if (m != MATCH_YES)
4741 goto cleanup;
4743 if (gfc_get_symbol (name, NULL, &sym))
4744 goto cleanup;
4747 p = gfc_get_formal_arglist ();
4749 if (head == NULL)
4750 head = tail = p;
4751 else
4753 tail->next = p;
4754 tail = p;
4757 tail->sym = sym;
4759 /* We don't add the VARIABLE flavor because the name could be a
4760 dummy procedure. We don't apply these attributes to formal
4761 arguments of statement functions. */
4762 if (sym != NULL && !st_flag
4763 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4764 || !gfc_missing_attr (&sym->attr, NULL)))
4766 m = MATCH_ERROR;
4767 goto cleanup;
4770 /* The name of a program unit can be in a different namespace,
4771 so check for it explicitly. After the statement is accepted,
4772 the name is checked for especially in gfc_get_symbol(). */
4773 if (gfc_new_block != NULL && sym != NULL
4774 && strcmp (sym->name, gfc_new_block->name) == 0)
4776 gfc_error ("Name %qs at %C is the name of the procedure",
4777 sym->name);
4778 m = MATCH_ERROR;
4779 goto cleanup;
4782 if (gfc_match_char (')') == MATCH_YES)
4783 goto ok;
4785 m = gfc_match_char (',');
4786 if (m != MATCH_YES)
4788 gfc_error ("Unexpected junk in formal argument list at %C");
4789 goto cleanup;
4794 /* Check for duplicate symbols in the formal argument list. */
4795 if (head != NULL)
4797 for (p = head; p->next; p = p->next)
4799 if (p->sym == NULL)
4800 continue;
4802 for (q = p->next; q; q = q->next)
4803 if (p->sym == q->sym)
4805 gfc_error ("Duplicate symbol %qs in formal argument list "
4806 "at %C", p->sym->name);
4808 m = MATCH_ERROR;
4809 goto cleanup;
4814 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4816 m = MATCH_ERROR;
4817 goto cleanup;
4820 /* gfc_error_now used in following and return with MATCH_YES because
4821 doing otherwise results in a cascade of extraneous errors and in
4822 some cases an ICE in symbol.c(gfc_release_symbol). */
4823 if (progname->attr.module_procedure && progname->attr.host_assoc)
4825 bool arg_count_mismatch = false;
4827 if (!formal && head)
4828 arg_count_mismatch = true;
4830 /* Abbreviated module procedure declaration is not meant to have any
4831 formal arguments! */
4832 if (!progname->abr_modproc_decl && formal && !head)
4833 arg_count_mismatch = true;
4835 for (p = formal, q = head; p && q; p = p->next, q = q->next)
4837 if ((p->next != NULL && q->next == NULL)
4838 || (p->next == NULL && q->next != NULL))
4839 arg_count_mismatch = true;
4840 else if ((p->sym == NULL && q->sym == NULL)
4841 || strcmp (p->sym->name, q->sym->name) == 0)
4842 continue;
4843 else
4844 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4845 "argument names (%s/%s) at %C",
4846 p->sym->name, q->sym->name);
4849 if (arg_count_mismatch)
4850 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4851 "formal arguments at %C");
4854 return MATCH_YES;
4856 cleanup:
4857 gfc_free_formal_arglist (head);
4858 return m;
4862 /* Match a RESULT specification following a function declaration or
4863 ENTRY statement. Also matches the end-of-statement. */
4865 static match
4866 match_result (gfc_symbol *function, gfc_symbol **result)
4868 char name[GFC_MAX_SYMBOL_LEN + 1];
4869 gfc_symbol *r;
4870 match m;
4872 if (gfc_match (" result (") != MATCH_YES)
4873 return MATCH_NO;
4875 m = gfc_match_name (name);
4876 if (m != MATCH_YES)
4877 return m;
4879 /* Get the right paren, and that's it because there could be the
4880 bind(c) attribute after the result clause. */
4881 if (gfc_match_char (')') != MATCH_YES)
4883 /* TODO: should report the missing right paren here. */
4884 return MATCH_ERROR;
4887 if (strcmp (function->name, name) == 0)
4889 gfc_error ("RESULT variable at %C must be different than function name");
4890 return MATCH_ERROR;
4893 if (gfc_get_symbol (name, NULL, &r))
4894 return MATCH_ERROR;
4896 if (!gfc_add_result (&r->attr, r->name, NULL))
4897 return MATCH_ERROR;
4899 *result = r;
4901 return MATCH_YES;
4905 /* Match a function suffix, which could be a combination of a result
4906 clause and BIND(C), either one, or neither. The draft does not
4907 require them to come in a specific order. */
4909 match
4910 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4912 match is_bind_c; /* Found bind(c). */
4913 match is_result; /* Found result clause. */
4914 match found_match; /* Status of whether we've found a good match. */
4915 char peek_char; /* Character we're going to peek at. */
4916 bool allow_binding_name;
4918 /* Initialize to having found nothing. */
4919 found_match = MATCH_NO;
4920 is_bind_c = MATCH_NO;
4921 is_result = MATCH_NO;
4923 /* Get the next char to narrow between result and bind(c). */
4924 gfc_gobble_whitespace ();
4925 peek_char = gfc_peek_ascii_char ();
4927 /* C binding names are not allowed for internal procedures. */
4928 if (gfc_current_state () == COMP_CONTAINS
4929 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4930 allow_binding_name = false;
4931 else
4932 allow_binding_name = true;
4934 switch (peek_char)
4936 case 'r':
4937 /* Look for result clause. */
4938 is_result = match_result (sym, result);
4939 if (is_result == MATCH_YES)
4941 /* Now see if there is a bind(c) after it. */
4942 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4943 /* We've found the result clause and possibly bind(c). */
4944 found_match = MATCH_YES;
4946 else
4947 /* This should only be MATCH_ERROR. */
4948 found_match = is_result;
4949 break;
4950 case 'b':
4951 /* Look for bind(c) first. */
4952 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4953 if (is_bind_c == MATCH_YES)
4955 /* Now see if a result clause followed it. */
4956 is_result = match_result (sym, result);
4957 found_match = MATCH_YES;
4959 else
4961 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4962 found_match = MATCH_ERROR;
4964 break;
4965 default:
4966 gfc_error ("Unexpected junk after function declaration at %C");
4967 found_match = MATCH_ERROR;
4968 break;
4971 if (is_bind_c == MATCH_YES)
4973 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4974 if (gfc_current_state () == COMP_CONTAINS
4975 && sym->ns->proc_name->attr.flavor != FL_MODULE
4976 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4977 "at %L may not be specified for an internal "
4978 "procedure", &gfc_current_locus))
4979 return MATCH_ERROR;
4981 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4982 return MATCH_ERROR;
4985 return found_match;
4989 /* Procedure pointer return value without RESULT statement:
4990 Add "hidden" result variable named "ppr@". */
4992 static bool
4993 add_hidden_procptr_result (gfc_symbol *sym)
4995 bool case1,case2;
4997 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4998 return false;
5000 /* First usage case: PROCEDURE and EXTERNAL statements. */
5001 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
5002 && strcmp (gfc_current_block ()->name, sym->name) == 0
5003 && sym->attr.external;
5004 /* Second usage case: INTERFACE statements. */
5005 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
5006 && gfc_state_stack->previous->state == COMP_FUNCTION
5007 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
5009 if (case1 || case2)
5011 gfc_symtree *stree;
5012 if (case1)
5013 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
5014 else if (case2)
5016 gfc_symtree *st2;
5017 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
5018 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
5019 st2->n.sym = stree->n.sym;
5021 sym->result = stree->n.sym;
5023 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
5024 sym->result->attr.pointer = sym->attr.pointer;
5025 sym->result->attr.external = sym->attr.external;
5026 sym->result->attr.referenced = sym->attr.referenced;
5027 sym->result->ts = sym->ts;
5028 sym->attr.proc_pointer = 0;
5029 sym->attr.pointer = 0;
5030 sym->attr.external = 0;
5031 if (sym->result->attr.external && sym->result->attr.pointer)
5033 sym->result->attr.pointer = 0;
5034 sym->result->attr.proc_pointer = 1;
5037 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
5039 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5040 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
5041 && sym->result && sym->result != sym && sym->result->attr.external
5042 && sym == gfc_current_ns->proc_name
5043 && sym == sym->result->ns->proc_name
5044 && strcmp ("ppr@", sym->result->name) == 0)
5046 sym->result->attr.proc_pointer = 1;
5047 sym->attr.pointer = 0;
5048 return true;
5050 else
5051 return false;
5055 /* Match the interface for a PROCEDURE declaration,
5056 including brackets (R1212). */
5058 static match
5059 match_procedure_interface (gfc_symbol **proc_if)
5061 match m;
5062 gfc_symtree *st;
5063 locus old_loc, entry_loc;
5064 gfc_namespace *old_ns = gfc_current_ns;
5065 char name[GFC_MAX_SYMBOL_LEN + 1];
5067 old_loc = entry_loc = gfc_current_locus;
5068 gfc_clear_ts (&current_ts);
5070 if (gfc_match (" (") != MATCH_YES)
5072 gfc_current_locus = entry_loc;
5073 return MATCH_NO;
5076 /* Get the type spec. for the procedure interface. */
5077 old_loc = gfc_current_locus;
5078 m = gfc_match_decl_type_spec (&current_ts, 0);
5079 gfc_gobble_whitespace ();
5080 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
5081 goto got_ts;
5083 if (m == MATCH_ERROR)
5084 return m;
5086 /* Procedure interface is itself a procedure. */
5087 gfc_current_locus = old_loc;
5088 m = gfc_match_name (name);
5090 /* First look to see if it is already accessible in the current
5091 namespace because it is use associated or contained. */
5092 st = NULL;
5093 if (gfc_find_sym_tree (name, NULL, 0, &st))
5094 return MATCH_ERROR;
5096 /* If it is still not found, then try the parent namespace, if it
5097 exists and create the symbol there if it is still not found. */
5098 if (gfc_current_ns->parent)
5099 gfc_current_ns = gfc_current_ns->parent;
5100 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5101 return MATCH_ERROR;
5103 gfc_current_ns = old_ns;
5104 *proc_if = st->n.sym;
5106 if (*proc_if)
5108 (*proc_if)->refs++;
5109 /* Resolve interface if possible. That way, attr.procedure is only set
5110 if it is declared by a later procedure-declaration-stmt, which is
5111 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5112 while ((*proc_if)->ts.interface)
5113 *proc_if = (*proc_if)->ts.interface;
5115 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5116 && (*proc_if)->ts.type == BT_UNKNOWN
5117 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5118 (*proc_if)->name, NULL))
5119 return MATCH_ERROR;
5122 got_ts:
5123 if (gfc_match (" )") != MATCH_YES)
5125 gfc_current_locus = entry_loc;
5126 return MATCH_NO;
5129 return MATCH_YES;
5133 /* Match a PROCEDURE declaration (R1211). */
5135 static match
5136 match_procedure_decl (void)
5138 match m;
5139 gfc_symbol *sym, *proc_if = NULL;
5140 int num;
5141 gfc_expr *initializer = NULL;
5143 /* Parse interface (with brackets). */
5144 m = match_procedure_interface (&proc_if);
5145 if (m != MATCH_YES)
5146 return m;
5148 /* Parse attributes (with colons). */
5149 m = match_attr_spec();
5150 if (m == MATCH_ERROR)
5151 return MATCH_ERROR;
5153 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5155 current_attr.is_bind_c = 1;
5156 has_name_equals = 0;
5157 curr_binding_label = NULL;
5160 /* Get procedure symbols. */
5161 for(num=1;;num++)
5163 m = gfc_match_symbol (&sym, 0);
5164 if (m == MATCH_NO)
5165 goto syntax;
5166 else if (m == MATCH_ERROR)
5167 return m;
5169 /* Add current_attr to the symbol attributes. */
5170 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5171 return MATCH_ERROR;
5173 if (sym->attr.is_bind_c)
5175 /* Check for C1218. */
5176 if (!proc_if || !proc_if->attr.is_bind_c)
5178 gfc_error ("BIND(C) attribute at %C requires "
5179 "an interface with BIND(C)");
5180 return MATCH_ERROR;
5182 /* Check for C1217. */
5183 if (has_name_equals && sym->attr.pointer)
5185 gfc_error ("BIND(C) procedure with NAME may not have "
5186 "POINTER attribute at %C");
5187 return MATCH_ERROR;
5189 if (has_name_equals && sym->attr.dummy)
5191 gfc_error ("Dummy procedure at %C may not have "
5192 "BIND(C) attribute with NAME");
5193 return MATCH_ERROR;
5195 /* Set binding label for BIND(C). */
5196 if (!set_binding_label (&sym->binding_label, sym->name, num))
5197 return MATCH_ERROR;
5200 if (!gfc_add_external (&sym->attr, NULL))
5201 return MATCH_ERROR;
5203 if (add_hidden_procptr_result (sym))
5204 sym = sym->result;
5206 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5207 return MATCH_ERROR;
5209 /* Set interface. */
5210 if (proc_if != NULL)
5212 if (sym->ts.type != BT_UNKNOWN)
5214 gfc_error ("Procedure %qs at %L already has basic type of %s",
5215 sym->name, &gfc_current_locus,
5216 gfc_basic_typename (sym->ts.type));
5217 return MATCH_ERROR;
5219 sym->ts.interface = proc_if;
5220 sym->attr.untyped = 1;
5221 sym->attr.if_source = IFSRC_IFBODY;
5223 else if (current_ts.type != BT_UNKNOWN)
5225 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5226 return MATCH_ERROR;
5227 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5228 sym->ts.interface->ts = current_ts;
5229 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5230 sym->ts.interface->attr.function = 1;
5231 sym->attr.function = 1;
5232 sym->attr.if_source = IFSRC_UNKNOWN;
5235 if (gfc_match (" =>") == MATCH_YES)
5237 if (!current_attr.pointer)
5239 gfc_error ("Initialization at %C isn't for a pointer variable");
5240 m = MATCH_ERROR;
5241 goto cleanup;
5244 m = match_pointer_init (&initializer, 1);
5245 if (m != MATCH_YES)
5246 goto cleanup;
5248 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5249 goto cleanup;
5253 if (gfc_match_eos () == MATCH_YES)
5254 return MATCH_YES;
5255 if (gfc_match_char (',') != MATCH_YES)
5256 goto syntax;
5259 syntax:
5260 gfc_error ("Syntax error in PROCEDURE statement at %C");
5261 return MATCH_ERROR;
5263 cleanup:
5264 /* Free stuff up and return. */
5265 gfc_free_expr (initializer);
5266 return m;
5270 static match
5271 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5274 /* Match a procedure pointer component declaration (R445). */
5276 static match
5277 match_ppc_decl (void)
5279 match m;
5280 gfc_symbol *proc_if = NULL;
5281 gfc_typespec ts;
5282 int num;
5283 gfc_component *c;
5284 gfc_expr *initializer = NULL;
5285 gfc_typebound_proc* tb;
5286 char name[GFC_MAX_SYMBOL_LEN + 1];
5288 /* Parse interface (with brackets). */
5289 m = match_procedure_interface (&proc_if);
5290 if (m != MATCH_YES)
5291 goto syntax;
5293 /* Parse attributes. */
5294 tb = XCNEW (gfc_typebound_proc);
5295 tb->where = gfc_current_locus;
5296 m = match_binding_attributes (tb, false, true);
5297 if (m == MATCH_ERROR)
5298 return m;
5300 gfc_clear_attr (&current_attr);
5301 current_attr.procedure = 1;
5302 current_attr.proc_pointer = 1;
5303 current_attr.access = tb->access;
5304 current_attr.flavor = FL_PROCEDURE;
5306 /* Match the colons (required). */
5307 if (gfc_match (" ::") != MATCH_YES)
5309 gfc_error ("Expected %<::%> after binding-attributes at %C");
5310 return MATCH_ERROR;
5313 /* Check for C450. */
5314 if (!tb->nopass && proc_if == NULL)
5316 gfc_error("NOPASS or explicit interface required at %C");
5317 return MATCH_ERROR;
5320 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5321 return MATCH_ERROR;
5323 /* Match PPC names. */
5324 ts = current_ts;
5325 for(num=1;;num++)
5327 m = gfc_match_name (name);
5328 if (m == MATCH_NO)
5329 goto syntax;
5330 else if (m == MATCH_ERROR)
5331 return m;
5333 if (!gfc_add_component (gfc_current_block(), name, &c))
5334 return MATCH_ERROR;
5336 /* Add current_attr to the symbol attributes. */
5337 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5338 return MATCH_ERROR;
5340 if (!gfc_add_external (&c->attr, NULL))
5341 return MATCH_ERROR;
5343 if (!gfc_add_proc (&c->attr, name, NULL))
5344 return MATCH_ERROR;
5346 if (num == 1)
5347 c->tb = tb;
5348 else
5350 c->tb = XCNEW (gfc_typebound_proc);
5351 c->tb->where = gfc_current_locus;
5352 *c->tb = *tb;
5355 /* Set interface. */
5356 if (proc_if != NULL)
5358 c->ts.interface = proc_if;
5359 c->attr.untyped = 1;
5360 c->attr.if_source = IFSRC_IFBODY;
5362 else if (ts.type != BT_UNKNOWN)
5364 c->ts = ts;
5365 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5366 c->ts.interface->result = c->ts.interface;
5367 c->ts.interface->ts = ts;
5368 c->ts.interface->attr.flavor = FL_PROCEDURE;
5369 c->ts.interface->attr.function = 1;
5370 c->attr.function = 1;
5371 c->attr.if_source = IFSRC_UNKNOWN;
5374 if (gfc_match (" =>") == MATCH_YES)
5376 m = match_pointer_init (&initializer, 1);
5377 if (m != MATCH_YES)
5379 gfc_free_expr (initializer);
5380 return m;
5382 c->initializer = initializer;
5385 if (gfc_match_eos () == MATCH_YES)
5386 return MATCH_YES;
5387 if (gfc_match_char (',') != MATCH_YES)
5388 goto syntax;
5391 syntax:
5392 gfc_error ("Syntax error in procedure pointer component at %C");
5393 return MATCH_ERROR;
5397 /* Match a PROCEDURE declaration inside an interface (R1206). */
5399 static match
5400 match_procedure_in_interface (void)
5402 match m;
5403 gfc_symbol *sym;
5404 char name[GFC_MAX_SYMBOL_LEN + 1];
5405 locus old_locus;
5407 if (current_interface.type == INTERFACE_NAMELESS
5408 || current_interface.type == INTERFACE_ABSTRACT)
5410 gfc_error ("PROCEDURE at %C must be in a generic interface");
5411 return MATCH_ERROR;
5414 /* Check if the F2008 optional double colon appears. */
5415 gfc_gobble_whitespace ();
5416 old_locus = gfc_current_locus;
5417 if (gfc_match ("::") == MATCH_YES)
5419 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5420 "MODULE PROCEDURE statement at %L", &old_locus))
5421 return MATCH_ERROR;
5423 else
5424 gfc_current_locus = old_locus;
5426 for(;;)
5428 m = gfc_match_name (name);
5429 if (m == MATCH_NO)
5430 goto syntax;
5431 else if (m == MATCH_ERROR)
5432 return m;
5433 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5434 return MATCH_ERROR;
5436 if (!gfc_add_interface (sym))
5437 return MATCH_ERROR;
5439 if (gfc_match_eos () == MATCH_YES)
5440 break;
5441 if (gfc_match_char (',') != MATCH_YES)
5442 goto syntax;
5445 return MATCH_YES;
5447 syntax:
5448 gfc_error ("Syntax error in PROCEDURE statement at %C");
5449 return MATCH_ERROR;
5453 /* General matcher for PROCEDURE declarations. */
5455 static match match_procedure_in_type (void);
5457 match
5458 gfc_match_procedure (void)
5460 match m;
5462 switch (gfc_current_state ())
5464 case COMP_NONE:
5465 case COMP_PROGRAM:
5466 case COMP_MODULE:
5467 case COMP_SUBMODULE:
5468 case COMP_SUBROUTINE:
5469 case COMP_FUNCTION:
5470 case COMP_BLOCK:
5471 m = match_procedure_decl ();
5472 break;
5473 case COMP_INTERFACE:
5474 m = match_procedure_in_interface ();
5475 break;
5476 case COMP_DERIVED:
5477 m = match_ppc_decl ();
5478 break;
5479 case COMP_DERIVED_CONTAINS:
5480 m = match_procedure_in_type ();
5481 break;
5482 default:
5483 return MATCH_NO;
5486 if (m != MATCH_YES)
5487 return m;
5489 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5490 return MATCH_ERROR;
5492 return m;
5496 /* Warn if a matched procedure has the same name as an intrinsic; this is
5497 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5498 parser-state-stack to find out whether we're in a module. */
5500 static void
5501 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5503 bool in_module;
5505 in_module = (gfc_state_stack->previous
5506 && (gfc_state_stack->previous->state == COMP_MODULE
5507 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5509 gfc_warn_intrinsic_shadow (sym, in_module, func);
5513 /* Match a function declaration. */
5515 match
5516 gfc_match_function_decl (void)
5518 char name[GFC_MAX_SYMBOL_LEN + 1];
5519 gfc_symbol *sym, *result;
5520 locus old_loc;
5521 match m;
5522 match suffix_match;
5523 match found_match; /* Status returned by match func. */
5525 if (gfc_current_state () != COMP_NONE
5526 && gfc_current_state () != COMP_INTERFACE
5527 && gfc_current_state () != COMP_CONTAINS)
5528 return MATCH_NO;
5530 gfc_clear_ts (&current_ts);
5532 old_loc = gfc_current_locus;
5534 m = gfc_match_prefix (&current_ts);
5535 if (m != MATCH_YES)
5537 gfc_current_locus = old_loc;
5538 return m;
5541 if (gfc_match ("function% %n", name) != MATCH_YES)
5543 gfc_current_locus = old_loc;
5544 return MATCH_NO;
5547 if (get_proc_name (name, &sym, false))
5548 return MATCH_ERROR;
5550 if (add_hidden_procptr_result (sym))
5551 sym = sym->result;
5553 if (current_attr.module_procedure)
5554 sym->attr.module_procedure = 1;
5556 gfc_new_block = sym;
5558 m = gfc_match_formal_arglist (sym, 0, 0);
5559 if (m == MATCH_NO)
5561 gfc_error ("Expected formal argument list in function "
5562 "definition at %C");
5563 m = MATCH_ERROR;
5564 goto cleanup;
5566 else if (m == MATCH_ERROR)
5567 goto cleanup;
5569 result = NULL;
5571 /* According to the draft, the bind(c) and result clause can
5572 come in either order after the formal_arg_list (i.e., either
5573 can be first, both can exist together or by themselves or neither
5574 one). Therefore, the match_result can't match the end of the
5575 string, and check for the bind(c) or result clause in either order. */
5576 found_match = gfc_match_eos ();
5578 /* Make sure that it isn't already declared as BIND(C). If it is, it
5579 must have been marked BIND(C) with a BIND(C) attribute and that is
5580 not allowed for procedures. */
5581 if (sym->attr.is_bind_c == 1)
5583 sym->attr.is_bind_c = 0;
5584 if (sym->old_symbol != NULL)
5585 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5586 "variables or common blocks",
5587 &(sym->old_symbol->declared_at));
5588 else
5589 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5590 "variables or common blocks", &gfc_current_locus);
5593 if (found_match != MATCH_YES)
5595 /* If we haven't found the end-of-statement, look for a suffix. */
5596 suffix_match = gfc_match_suffix (sym, &result);
5597 if (suffix_match == MATCH_YES)
5598 /* Need to get the eos now. */
5599 found_match = gfc_match_eos ();
5600 else
5601 found_match = suffix_match;
5604 if(found_match != MATCH_YES)
5605 m = MATCH_ERROR;
5606 else
5608 /* Make changes to the symbol. */
5609 m = MATCH_ERROR;
5611 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5612 goto cleanup;
5614 if (!gfc_missing_attr (&sym->attr, NULL)
5615 || !copy_prefix (&sym->attr, &sym->declared_at))
5616 goto cleanup;
5618 /* Delay matching the function characteristics until after the
5619 specification block by signalling kind=-1. */
5620 sym->declared_at = old_loc;
5621 if (current_ts.type != BT_UNKNOWN)
5622 current_ts.kind = -1;
5623 else
5624 current_ts.kind = 0;
5626 if (result == NULL)
5628 if (current_ts.type != BT_UNKNOWN
5629 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5630 goto cleanup;
5631 sym->result = sym;
5633 else
5635 if (current_ts.type != BT_UNKNOWN
5636 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5637 goto cleanup;
5638 sym->result = result;
5641 /* Warn if this procedure has the same name as an intrinsic. */
5642 do_warn_intrinsic_shadow (sym, true);
5644 return MATCH_YES;
5647 cleanup:
5648 gfc_current_locus = old_loc;
5649 return m;
5653 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5654 pass the name of the entry, rather than the gfc_current_block name, and
5655 to return false upon finding an existing global entry. */
5657 static bool
5658 add_global_entry (const char *name, const char *binding_label, bool sub,
5659 locus *where)
5661 gfc_gsymbol *s;
5662 enum gfc_symbol_type type;
5664 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5666 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5667 name is a global identifier. */
5668 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5670 s = gfc_get_gsymbol (name);
5672 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5674 gfc_global_used (s, where);
5675 return false;
5677 else
5679 s->type = type;
5680 s->sym_name = name;
5681 s->where = *where;
5682 s->defined = 1;
5683 s->ns = gfc_current_ns;
5687 /* Don't add the symbol multiple times. */
5688 if (binding_label
5689 && (!gfc_notification_std (GFC_STD_F2008)
5690 || strcmp (name, binding_label) != 0))
5692 s = gfc_get_gsymbol (binding_label);
5694 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5696 gfc_global_used (s, where);
5697 return false;
5699 else
5701 s->type = type;
5702 s->sym_name = name;
5703 s->binding_label = binding_label;
5704 s->where = *where;
5705 s->defined = 1;
5706 s->ns = gfc_current_ns;
5710 return true;
5714 /* Match an ENTRY statement. */
5716 match
5717 gfc_match_entry (void)
5719 gfc_symbol *proc;
5720 gfc_symbol *result;
5721 gfc_symbol *entry;
5722 char name[GFC_MAX_SYMBOL_LEN + 1];
5723 gfc_compile_state state;
5724 match m;
5725 gfc_entry_list *el;
5726 locus old_loc;
5727 bool module_procedure;
5728 char peek_char;
5729 match is_bind_c;
5731 m = gfc_match_name (name);
5732 if (m != MATCH_YES)
5733 return m;
5735 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5736 return MATCH_ERROR;
5738 state = gfc_current_state ();
5739 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5741 switch (state)
5743 case COMP_PROGRAM:
5744 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5745 break;
5746 case COMP_MODULE:
5747 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5748 break;
5749 case COMP_SUBMODULE:
5750 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5751 break;
5752 case COMP_BLOCK_DATA:
5753 gfc_error ("ENTRY statement at %C cannot appear within "
5754 "a BLOCK DATA");
5755 break;
5756 case COMP_INTERFACE:
5757 gfc_error ("ENTRY statement at %C cannot appear within "
5758 "an INTERFACE");
5759 break;
5760 case COMP_DERIVED:
5761 gfc_error ("ENTRY statement at %C cannot appear within "
5762 "a DERIVED TYPE block");
5763 break;
5764 case COMP_IF:
5765 gfc_error ("ENTRY statement at %C cannot appear within "
5766 "an IF-THEN block");
5767 break;
5768 case COMP_DO:
5769 case COMP_DO_CONCURRENT:
5770 gfc_error ("ENTRY statement at %C cannot appear within "
5771 "a DO block");
5772 break;
5773 case COMP_SELECT:
5774 gfc_error ("ENTRY statement at %C cannot appear within "
5775 "a SELECT block");
5776 break;
5777 case COMP_FORALL:
5778 gfc_error ("ENTRY statement at %C cannot appear within "
5779 "a FORALL block");
5780 break;
5781 case COMP_WHERE:
5782 gfc_error ("ENTRY statement at %C cannot appear within "
5783 "a WHERE block");
5784 break;
5785 case COMP_CONTAINS:
5786 gfc_error ("ENTRY statement at %C cannot appear within "
5787 "a contained subprogram");
5788 break;
5789 default:
5790 gfc_error ("Unexpected ENTRY statement at %C");
5792 return MATCH_ERROR;
5795 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
5796 && gfc_state_stack->previous->state == COMP_INTERFACE)
5798 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
5799 return MATCH_ERROR;
5802 module_procedure = gfc_current_ns->parent != NULL
5803 && gfc_current_ns->parent->proc_name
5804 && gfc_current_ns->parent->proc_name->attr.flavor
5805 == FL_MODULE;
5807 if (gfc_current_ns->parent != NULL
5808 && gfc_current_ns->parent->proc_name
5809 && !module_procedure)
5811 gfc_error("ENTRY statement at %C cannot appear in a "
5812 "contained procedure");
5813 return MATCH_ERROR;
5816 /* Module function entries need special care in get_proc_name
5817 because previous references within the function will have
5818 created symbols attached to the current namespace. */
5819 if (get_proc_name (name, &entry,
5820 gfc_current_ns->parent != NULL
5821 && module_procedure))
5822 return MATCH_ERROR;
5824 proc = gfc_current_block ();
5826 /* Make sure that it isn't already declared as BIND(C). If it is, it
5827 must have been marked BIND(C) with a BIND(C) attribute and that is
5828 not allowed for procedures. */
5829 if (entry->attr.is_bind_c == 1)
5831 entry->attr.is_bind_c = 0;
5832 if (entry->old_symbol != NULL)
5833 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5834 "variables or common blocks",
5835 &(entry->old_symbol->declared_at));
5836 else
5837 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5838 "variables or common blocks", &gfc_current_locus);
5841 /* Check what next non-whitespace character is so we can tell if there
5842 is the required parens if we have a BIND(C). */
5843 old_loc = gfc_current_locus;
5844 gfc_gobble_whitespace ();
5845 peek_char = gfc_peek_ascii_char ();
5847 if (state == COMP_SUBROUTINE)
5849 m = gfc_match_formal_arglist (entry, 0, 1);
5850 if (m != MATCH_YES)
5851 return MATCH_ERROR;
5853 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5854 never be an internal procedure. */
5855 is_bind_c = gfc_match_bind_c (entry, true);
5856 if (is_bind_c == MATCH_ERROR)
5857 return MATCH_ERROR;
5858 if (is_bind_c == MATCH_YES)
5860 if (peek_char != '(')
5862 gfc_error ("Missing required parentheses before BIND(C) at %C");
5863 return MATCH_ERROR;
5865 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5866 &(entry->declared_at), 1))
5867 return MATCH_ERROR;
5870 if (!gfc_current_ns->parent
5871 && !add_global_entry (name, entry->binding_label, true,
5872 &old_loc))
5873 return MATCH_ERROR;
5875 /* An entry in a subroutine. */
5876 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5877 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5878 return MATCH_ERROR;
5880 else
5882 /* An entry in a function.
5883 We need to take special care because writing
5884 ENTRY f()
5886 ENTRY f
5887 is allowed, whereas
5888 ENTRY f() RESULT (r)
5889 can't be written as
5890 ENTRY f RESULT (r). */
5891 if (gfc_match_eos () == MATCH_YES)
5893 gfc_current_locus = old_loc;
5894 /* Match the empty argument list, and add the interface to
5895 the symbol. */
5896 m = gfc_match_formal_arglist (entry, 0, 1);
5898 else
5899 m = gfc_match_formal_arglist (entry, 0, 0);
5901 if (m != MATCH_YES)
5902 return MATCH_ERROR;
5904 result = NULL;
5906 if (gfc_match_eos () == MATCH_YES)
5908 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5909 || !gfc_add_function (&entry->attr, entry->name, NULL))
5910 return MATCH_ERROR;
5912 entry->result = entry;
5914 else
5916 m = gfc_match_suffix (entry, &result);
5917 if (m == MATCH_NO)
5918 gfc_syntax_error (ST_ENTRY);
5919 if (m != MATCH_YES)
5920 return MATCH_ERROR;
5922 if (result)
5924 if (!gfc_add_result (&result->attr, result->name, NULL)
5925 || !gfc_add_entry (&entry->attr, result->name, NULL)
5926 || !gfc_add_function (&entry->attr, result->name, NULL))
5927 return MATCH_ERROR;
5928 entry->result = result;
5930 else
5932 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5933 || !gfc_add_function (&entry->attr, entry->name, NULL))
5934 return MATCH_ERROR;
5935 entry->result = entry;
5939 if (!gfc_current_ns->parent
5940 && !add_global_entry (name, entry->binding_label, false,
5941 &old_loc))
5942 return MATCH_ERROR;
5945 if (gfc_match_eos () != MATCH_YES)
5947 gfc_syntax_error (ST_ENTRY);
5948 return MATCH_ERROR;
5951 entry->attr.recursive = proc->attr.recursive;
5952 entry->attr.elemental = proc->attr.elemental;
5953 entry->attr.pure = proc->attr.pure;
5955 el = gfc_get_entry_list ();
5956 el->sym = entry;
5957 el->next = gfc_current_ns->entries;
5958 gfc_current_ns->entries = el;
5959 if (el->next)
5960 el->id = el->next->id + 1;
5961 else
5962 el->id = 1;
5964 new_st.op = EXEC_ENTRY;
5965 new_st.ext.entry = el;
5967 return MATCH_YES;
5971 /* Match a subroutine statement, including optional prefixes. */
5973 match
5974 gfc_match_subroutine (void)
5976 char name[GFC_MAX_SYMBOL_LEN + 1];
5977 gfc_symbol *sym;
5978 match m;
5979 match is_bind_c;
5980 char peek_char;
5981 bool allow_binding_name;
5983 if (gfc_current_state () != COMP_NONE
5984 && gfc_current_state () != COMP_INTERFACE
5985 && gfc_current_state () != COMP_CONTAINS)
5986 return MATCH_NO;
5988 m = gfc_match_prefix (NULL);
5989 if (m != MATCH_YES)
5990 return m;
5992 m = gfc_match ("subroutine% %n", name);
5993 if (m != MATCH_YES)
5994 return m;
5996 if (get_proc_name (name, &sym, false))
5997 return MATCH_ERROR;
5999 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
6000 the symbol existed before. */
6001 sym->declared_at = gfc_current_locus;
6003 if (current_attr.module_procedure)
6004 sym->attr.module_procedure = 1;
6006 if (add_hidden_procptr_result (sym))
6007 sym = sym->result;
6009 gfc_new_block = sym;
6011 /* Check what next non-whitespace character is so we can tell if there
6012 is the required parens if we have a BIND(C). */
6013 gfc_gobble_whitespace ();
6014 peek_char = gfc_peek_ascii_char ();
6016 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
6017 return MATCH_ERROR;
6019 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
6020 return MATCH_ERROR;
6022 /* Make sure that it isn't already declared as BIND(C). If it is, it
6023 must have been marked BIND(C) with a BIND(C) attribute and that is
6024 not allowed for procedures. */
6025 if (sym->attr.is_bind_c == 1)
6027 sym->attr.is_bind_c = 0;
6028 if (sym->old_symbol != NULL)
6029 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6030 "variables or common blocks",
6031 &(sym->old_symbol->declared_at));
6032 else
6033 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6034 "variables or common blocks", &gfc_current_locus);
6037 /* C binding names are not allowed for internal procedures. */
6038 if (gfc_current_state () == COMP_CONTAINS
6039 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6040 allow_binding_name = false;
6041 else
6042 allow_binding_name = true;
6044 /* Here, we are just checking if it has the bind(c) attribute, and if
6045 so, then we need to make sure it's all correct. If it doesn't,
6046 we still need to continue matching the rest of the subroutine line. */
6047 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6048 if (is_bind_c == MATCH_ERROR)
6050 /* There was an attempt at the bind(c), but it was wrong. An
6051 error message should have been printed w/in the gfc_match_bind_c
6052 so here we'll just return the MATCH_ERROR. */
6053 return MATCH_ERROR;
6056 if (is_bind_c == MATCH_YES)
6058 /* The following is allowed in the Fortran 2008 draft. */
6059 if (gfc_current_state () == COMP_CONTAINS
6060 && sym->ns->proc_name->attr.flavor != FL_MODULE
6061 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6062 "at %L may not be specified for an internal "
6063 "procedure", &gfc_current_locus))
6064 return MATCH_ERROR;
6066 if (peek_char != '(')
6068 gfc_error ("Missing required parentheses before BIND(C) at %C");
6069 return MATCH_ERROR;
6071 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
6072 &(sym->declared_at), 1))
6073 return MATCH_ERROR;
6076 if (gfc_match_eos () != MATCH_YES)
6078 gfc_syntax_error (ST_SUBROUTINE);
6079 return MATCH_ERROR;
6082 if (!copy_prefix (&sym->attr, &sym->declared_at))
6083 return MATCH_ERROR;
6085 /* Warn if it has the same name as an intrinsic. */
6086 do_warn_intrinsic_shadow (sym, false);
6088 return MATCH_YES;
6092 /* Check that the NAME identifier in a BIND attribute or statement
6093 is conform to C identifier rules. */
6095 match
6096 check_bind_name_identifier (char **name)
6098 char *n = *name, *p;
6100 /* Remove leading spaces. */
6101 while (*n == ' ')
6102 n++;
6104 /* On an empty string, free memory and set name to NULL. */
6105 if (*n == '\0')
6107 free (*name);
6108 *name = NULL;
6109 return MATCH_YES;
6112 /* Remove trailing spaces. */
6113 p = n + strlen(n) - 1;
6114 while (*p == ' ')
6115 *(p--) = '\0';
6117 /* Insert the identifier into the symbol table. */
6118 p = xstrdup (n);
6119 free (*name);
6120 *name = p;
6122 /* Now check that identifier is valid under C rules. */
6123 if (ISDIGIT (*p))
6125 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6126 return MATCH_ERROR;
6129 for (; *p; p++)
6130 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6132 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6133 return MATCH_ERROR;
6136 return MATCH_YES;
6140 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6141 given, and set the binding label in either the given symbol (if not
6142 NULL), or in the current_ts. The symbol may be NULL because we may
6143 encounter the BIND(C) before the declaration itself. Return
6144 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6145 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6146 or MATCH_YES if the specifier was correct and the binding label and
6147 bind(c) fields were set correctly for the given symbol or the
6148 current_ts. If allow_binding_name is false, no binding name may be
6149 given. */
6151 match
6152 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6154 char *binding_label = NULL;
6155 gfc_expr *e = NULL;
6157 /* Initialize the flag that specifies whether we encountered a NAME=
6158 specifier or not. */
6159 has_name_equals = 0;
6161 /* This much we have to be able to match, in this order, if
6162 there is a bind(c) label. */
6163 if (gfc_match (" bind ( c ") != MATCH_YES)
6164 return MATCH_NO;
6166 /* Now see if there is a binding label, or if we've reached the
6167 end of the bind(c) attribute without one. */
6168 if (gfc_match_char (',') == MATCH_YES)
6170 if (gfc_match (" name = ") != MATCH_YES)
6172 gfc_error ("Syntax error in NAME= specifier for binding label "
6173 "at %C");
6174 /* should give an error message here */
6175 return MATCH_ERROR;
6178 has_name_equals = 1;
6180 if (gfc_match_init_expr (&e) != MATCH_YES)
6182 gfc_free_expr (e);
6183 return MATCH_ERROR;
6186 if (!gfc_simplify_expr(e, 0))
6188 gfc_error ("NAME= specifier at %C should be a constant expression");
6189 gfc_free_expr (e);
6190 return MATCH_ERROR;
6193 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6194 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6196 gfc_error ("NAME= specifier at %C should be a scalar of "
6197 "default character kind");
6198 gfc_free_expr(e);
6199 return MATCH_ERROR;
6202 // Get a C string from the Fortran string constant
6203 binding_label = gfc_widechar_to_char (e->value.character.string,
6204 e->value.character.length);
6205 gfc_free_expr(e);
6207 // Check that it is valid (old gfc_match_name_C)
6208 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6209 return MATCH_ERROR;
6212 /* Get the required right paren. */
6213 if (gfc_match_char (')') != MATCH_YES)
6215 gfc_error ("Missing closing paren for binding label at %C");
6216 return MATCH_ERROR;
6219 if (has_name_equals && !allow_binding_name)
6221 gfc_error ("No binding name is allowed in BIND(C) at %C");
6222 return MATCH_ERROR;
6225 if (has_name_equals && sym != NULL && sym->attr.dummy)
6227 gfc_error ("For dummy procedure %s, no binding name is "
6228 "allowed in BIND(C) at %C", sym->name);
6229 return MATCH_ERROR;
6233 /* Save the binding label to the symbol. If sym is null, we're
6234 probably matching the typespec attributes of a declaration and
6235 haven't gotten the name yet, and therefore, no symbol yet. */
6236 if (binding_label)
6238 if (sym != NULL)
6239 sym->binding_label = binding_label;
6240 else
6241 curr_binding_label = binding_label;
6243 else if (allow_binding_name)
6245 /* No binding label, but if symbol isn't null, we
6246 can set the label for it here.
6247 If name="" or allow_binding_name is false, no C binding name is
6248 created. */
6249 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6250 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6253 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6254 && current_interface.type == INTERFACE_ABSTRACT)
6256 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6257 return MATCH_ERROR;
6260 return MATCH_YES;
6264 /* Return nonzero if we're currently compiling a contained procedure. */
6266 static int
6267 contained_procedure (void)
6269 gfc_state_data *s = gfc_state_stack;
6271 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6272 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6273 return 1;
6275 return 0;
6278 /* Set the kind of each enumerator. The kind is selected such that it is
6279 interoperable with the corresponding C enumeration type, making
6280 sure that -fshort-enums is honored. */
6282 static void
6283 set_enum_kind(void)
6285 enumerator_history *current_history = NULL;
6286 int kind;
6287 int i;
6289 if (max_enum == NULL || enum_history == NULL)
6290 return;
6292 if (!flag_short_enums)
6293 return;
6295 i = 0;
6298 kind = gfc_integer_kinds[i++].kind;
6300 while (kind < gfc_c_int_kind
6301 && gfc_check_integer_range (max_enum->initializer->value.integer,
6302 kind) != ARITH_OK);
6304 current_history = enum_history;
6305 while (current_history != NULL)
6307 current_history->sym->ts.kind = kind;
6308 current_history = current_history->next;
6313 /* Match any of the various end-block statements. Returns the type of
6314 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6315 and END BLOCK statements cannot be replaced by a single END statement. */
6317 match
6318 gfc_match_end (gfc_statement *st)
6320 char name[GFC_MAX_SYMBOL_LEN + 1];
6321 gfc_compile_state state;
6322 locus old_loc;
6323 const char *block_name;
6324 const char *target;
6325 int eos_ok;
6326 match m;
6327 gfc_namespace *parent_ns, *ns, *prev_ns;
6328 gfc_namespace **nsp;
6329 bool abreviated_modproc_decl;
6331 old_loc = gfc_current_locus;
6332 if (gfc_match ("end") != MATCH_YES)
6333 return MATCH_NO;
6335 state = gfc_current_state ();
6336 block_name = gfc_current_block () == NULL
6337 ? NULL : gfc_current_block ()->name;
6339 switch (state)
6341 case COMP_ASSOCIATE:
6342 case COMP_BLOCK:
6343 if (!strncmp (block_name, "block@", strlen("block@")))
6344 block_name = NULL;
6345 break;
6347 case COMP_CONTAINS:
6348 case COMP_DERIVED_CONTAINS:
6349 state = gfc_state_stack->previous->state;
6350 block_name = gfc_state_stack->previous->sym == NULL
6351 ? NULL : gfc_state_stack->previous->sym->name;
6352 break;
6354 default:
6355 break;
6358 abreviated_modproc_decl
6359 = gfc_current_block ()
6360 && gfc_current_block ()->abr_modproc_decl;
6362 switch (state)
6364 case COMP_NONE:
6365 case COMP_PROGRAM:
6366 *st = ST_END_PROGRAM;
6367 target = " program";
6368 eos_ok = 1;
6369 break;
6371 case COMP_SUBROUTINE:
6372 *st = ST_END_SUBROUTINE;
6373 if (!abreviated_modproc_decl)
6374 target = " subroutine";
6375 else
6376 target = " procedure";
6377 eos_ok = !contained_procedure ();
6378 break;
6380 case COMP_FUNCTION:
6381 *st = ST_END_FUNCTION;
6382 if (!abreviated_modproc_decl)
6383 target = " function";
6384 else
6385 target = " procedure";
6386 eos_ok = !contained_procedure ();
6387 break;
6389 case COMP_BLOCK_DATA:
6390 *st = ST_END_BLOCK_DATA;
6391 target = " block data";
6392 eos_ok = 1;
6393 break;
6395 case COMP_MODULE:
6396 *st = ST_END_MODULE;
6397 target = " module";
6398 eos_ok = 1;
6399 break;
6401 case COMP_SUBMODULE:
6402 *st = ST_END_SUBMODULE;
6403 target = " submodule";
6404 eos_ok = 1;
6405 break;
6407 case COMP_INTERFACE:
6408 *st = ST_END_INTERFACE;
6409 target = " interface";
6410 eos_ok = 0;
6411 break;
6413 case COMP_DERIVED:
6414 case COMP_DERIVED_CONTAINS:
6415 *st = ST_END_TYPE;
6416 target = " type";
6417 eos_ok = 0;
6418 break;
6420 case COMP_ASSOCIATE:
6421 *st = ST_END_ASSOCIATE;
6422 target = " associate";
6423 eos_ok = 0;
6424 break;
6426 case COMP_BLOCK:
6427 *st = ST_END_BLOCK;
6428 target = " block";
6429 eos_ok = 0;
6430 break;
6432 case COMP_IF:
6433 *st = ST_ENDIF;
6434 target = " if";
6435 eos_ok = 0;
6436 break;
6438 case COMP_DO:
6439 case COMP_DO_CONCURRENT:
6440 *st = ST_ENDDO;
6441 target = " do";
6442 eos_ok = 0;
6443 break;
6445 case COMP_CRITICAL:
6446 *st = ST_END_CRITICAL;
6447 target = " critical";
6448 eos_ok = 0;
6449 break;
6451 case COMP_SELECT:
6452 case COMP_SELECT_TYPE:
6453 *st = ST_END_SELECT;
6454 target = " select";
6455 eos_ok = 0;
6456 break;
6458 case COMP_FORALL:
6459 *st = ST_END_FORALL;
6460 target = " forall";
6461 eos_ok = 0;
6462 break;
6464 case COMP_WHERE:
6465 *st = ST_END_WHERE;
6466 target = " where";
6467 eos_ok = 0;
6468 break;
6470 case COMP_ENUM:
6471 *st = ST_END_ENUM;
6472 target = " enum";
6473 eos_ok = 0;
6474 last_initializer = NULL;
6475 set_enum_kind ();
6476 gfc_free_enum_history ();
6477 break;
6479 default:
6480 gfc_error ("Unexpected END statement at %C");
6481 goto cleanup;
6484 old_loc = gfc_current_locus;
6485 if (gfc_match_eos () == MATCH_YES)
6487 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6489 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6490 "instead of %s statement at %L",
6491 abreviated_modproc_decl ? "END PROCEDURE"
6492 : gfc_ascii_statement(*st), &old_loc))
6493 goto cleanup;
6495 else if (!eos_ok)
6497 /* We would have required END [something]. */
6498 gfc_error ("%s statement expected at %L",
6499 gfc_ascii_statement (*st), &old_loc);
6500 goto cleanup;
6503 return MATCH_YES;
6506 /* Verify that we've got the sort of end-block that we're expecting. */
6507 if (gfc_match (target) != MATCH_YES)
6509 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6510 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6511 goto cleanup;
6514 old_loc = gfc_current_locus;
6515 /* If we're at the end, make sure a block name wasn't required. */
6516 if (gfc_match_eos () == MATCH_YES)
6519 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6520 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6521 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6522 return MATCH_YES;
6524 if (!block_name)
6525 return MATCH_YES;
6527 gfc_error ("Expected block name of %qs in %s statement at %L",
6528 block_name, gfc_ascii_statement (*st), &old_loc);
6530 return MATCH_ERROR;
6533 /* END INTERFACE has a special handler for its several possible endings. */
6534 if (*st == ST_END_INTERFACE)
6535 return gfc_match_end_interface ();
6537 /* We haven't hit the end of statement, so what is left must be an
6538 end-name. */
6539 m = gfc_match_space ();
6540 if (m == MATCH_YES)
6541 m = gfc_match_name (name);
6543 if (m == MATCH_NO)
6544 gfc_error ("Expected terminating name at %C");
6545 if (m != MATCH_YES)
6546 goto cleanup;
6548 if (block_name == NULL)
6549 goto syntax;
6551 /* We have to pick out the declared submodule name from the composite
6552 required by F2008:11.2.3 para 2, which ends in the declared name. */
6553 if (state == COMP_SUBMODULE)
6554 block_name = strchr (block_name, '.') + 1;
6556 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6558 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6559 gfc_ascii_statement (*st));
6560 goto cleanup;
6562 /* Procedure pointer as function result. */
6563 else if (strcmp (block_name, "ppr@") == 0
6564 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6566 gfc_error ("Expected label %qs for %s statement at %C",
6567 gfc_current_block ()->ns->proc_name->name,
6568 gfc_ascii_statement (*st));
6569 goto cleanup;
6572 if (gfc_match_eos () == MATCH_YES)
6573 return MATCH_YES;
6575 syntax:
6576 gfc_syntax_error (*st);
6578 cleanup:
6579 gfc_current_locus = old_loc;
6581 /* If we are missing an END BLOCK, we created a half-ready namespace.
6582 Remove it from the parent namespace's sibling list. */
6584 while (state == COMP_BLOCK)
6586 parent_ns = gfc_current_ns->parent;
6588 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6590 prev_ns = NULL;
6591 ns = *nsp;
6592 while (ns)
6594 if (ns == gfc_current_ns)
6596 if (prev_ns == NULL)
6597 *nsp = NULL;
6598 else
6599 prev_ns->sibling = ns->sibling;
6601 prev_ns = ns;
6602 ns = ns->sibling;
6605 gfc_free_namespace (gfc_current_ns);
6606 gfc_current_ns = parent_ns;
6607 gfc_state_stack = gfc_state_stack->previous;
6608 state = gfc_current_state ();
6611 return MATCH_ERROR;
6616 /***************** Attribute declaration statements ****************/
6618 /* Set the attribute of a single variable. */
6620 static match
6621 attr_decl1 (void)
6623 char name[GFC_MAX_SYMBOL_LEN + 1];
6624 gfc_array_spec *as;
6626 /* Workaround -Wmaybe-uninitialized false positive during
6627 profiledbootstrap by initializing them. */
6628 gfc_symbol *sym = NULL;
6629 locus var_locus;
6630 match m;
6632 as = NULL;
6634 m = gfc_match_name (name);
6635 if (m != MATCH_YES)
6636 goto cleanup;
6638 if (find_special (name, &sym, false))
6639 return MATCH_ERROR;
6641 if (!check_function_name (name))
6643 m = MATCH_ERROR;
6644 goto cleanup;
6647 var_locus = gfc_current_locus;
6649 /* Deal with possible array specification for certain attributes. */
6650 if (current_attr.dimension
6651 || current_attr.codimension
6652 || current_attr.allocatable
6653 || current_attr.pointer
6654 || current_attr.target)
6656 m = gfc_match_array_spec (&as, !current_attr.codimension,
6657 !current_attr.dimension
6658 && !current_attr.pointer
6659 && !current_attr.target);
6660 if (m == MATCH_ERROR)
6661 goto cleanup;
6663 if (current_attr.dimension && m == MATCH_NO)
6665 gfc_error ("Missing array specification at %L in DIMENSION "
6666 "statement", &var_locus);
6667 m = MATCH_ERROR;
6668 goto cleanup;
6671 if (current_attr.dimension && sym->value)
6673 gfc_error ("Dimensions specified for %s at %L after its "
6674 "initialisation", sym->name, &var_locus);
6675 m = MATCH_ERROR;
6676 goto cleanup;
6679 if (current_attr.codimension && m == MATCH_NO)
6681 gfc_error ("Missing array specification at %L in CODIMENSION "
6682 "statement", &var_locus);
6683 m = MATCH_ERROR;
6684 goto cleanup;
6687 if ((current_attr.allocatable || current_attr.pointer)
6688 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6690 gfc_error ("Array specification must be deferred at %L", &var_locus);
6691 m = MATCH_ERROR;
6692 goto cleanup;
6696 /* Update symbol table. DIMENSION attribute is set in
6697 gfc_set_array_spec(). For CLASS variables, this must be applied
6698 to the first component, or '_data' field. */
6699 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6701 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6703 m = MATCH_ERROR;
6704 goto cleanup;
6707 else
6709 if (current_attr.dimension == 0 && current_attr.codimension == 0
6710 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6712 m = MATCH_ERROR;
6713 goto cleanup;
6717 if (sym->ts.type == BT_CLASS
6718 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6720 m = MATCH_ERROR;
6721 goto cleanup;
6724 if (!gfc_set_array_spec (sym, as, &var_locus))
6726 m = MATCH_ERROR;
6727 goto cleanup;
6730 if (sym->attr.cray_pointee && sym->as != NULL)
6732 /* Fix the array spec. */
6733 m = gfc_mod_pointee_as (sym->as);
6734 if (m == MATCH_ERROR)
6735 goto cleanup;
6738 if (!gfc_add_attribute (&sym->attr, &var_locus))
6740 m = MATCH_ERROR;
6741 goto cleanup;
6744 if ((current_attr.external || current_attr.intrinsic)
6745 && sym->attr.flavor != FL_PROCEDURE
6746 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6748 m = MATCH_ERROR;
6749 goto cleanup;
6752 add_hidden_procptr_result (sym);
6754 return MATCH_YES;
6756 cleanup:
6757 gfc_free_array_spec (as);
6758 return m;
6762 /* Generic attribute declaration subroutine. Used for attributes that
6763 just have a list of names. */
6765 static match
6766 attr_decl (void)
6768 match m;
6770 /* Gobble the optional double colon, by simply ignoring the result
6771 of gfc_match(). */
6772 gfc_match (" ::");
6774 for (;;)
6776 m = attr_decl1 ();
6777 if (m != MATCH_YES)
6778 break;
6780 if (gfc_match_eos () == MATCH_YES)
6782 m = MATCH_YES;
6783 break;
6786 if (gfc_match_char (',') != MATCH_YES)
6788 gfc_error ("Unexpected character in variable list at %C");
6789 m = MATCH_ERROR;
6790 break;
6794 return m;
6798 /* This routine matches Cray Pointer declarations of the form:
6799 pointer ( <pointer>, <pointee> )
6801 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6802 The pointer, if already declared, should be an integer. Otherwise, we
6803 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6804 be either a scalar, or an array declaration. No space is allocated for
6805 the pointee. For the statement
6806 pointer (ipt, ar(10))
6807 any subsequent uses of ar will be translated (in C-notation) as
6808 ar(i) => ((<type> *) ipt)(i)
6809 After gimplification, pointee variable will disappear in the code. */
6811 static match
6812 cray_pointer_decl (void)
6814 match m;
6815 gfc_array_spec *as = NULL;
6816 gfc_symbol *cptr; /* Pointer symbol. */
6817 gfc_symbol *cpte; /* Pointee symbol. */
6818 locus var_locus;
6819 bool done = false;
6821 while (!done)
6823 if (gfc_match_char ('(') != MATCH_YES)
6825 gfc_error ("Expected %<(%> at %C");
6826 return MATCH_ERROR;
6829 /* Match pointer. */
6830 var_locus = gfc_current_locus;
6831 gfc_clear_attr (&current_attr);
6832 gfc_add_cray_pointer (&current_attr, &var_locus);
6833 current_ts.type = BT_INTEGER;
6834 current_ts.kind = gfc_index_integer_kind;
6836 m = gfc_match_symbol (&cptr, 0);
6837 if (m != MATCH_YES)
6839 gfc_error ("Expected variable name at %C");
6840 return m;
6843 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6844 return MATCH_ERROR;
6846 gfc_set_sym_referenced (cptr);
6848 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6850 cptr->ts.type = BT_INTEGER;
6851 cptr->ts.kind = gfc_index_integer_kind;
6853 else if (cptr->ts.type != BT_INTEGER)
6855 gfc_error ("Cray pointer at %C must be an integer");
6856 return MATCH_ERROR;
6858 else if (cptr->ts.kind < gfc_index_integer_kind)
6859 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6860 " memory addresses require %d bytes",
6861 cptr->ts.kind, gfc_index_integer_kind);
6863 if (gfc_match_char (',') != MATCH_YES)
6865 gfc_error ("Expected \",\" at %C");
6866 return MATCH_ERROR;
6869 /* Match Pointee. */
6870 var_locus = gfc_current_locus;
6871 gfc_clear_attr (&current_attr);
6872 gfc_add_cray_pointee (&current_attr, &var_locus);
6873 current_ts.type = BT_UNKNOWN;
6874 current_ts.kind = 0;
6876 m = gfc_match_symbol (&cpte, 0);
6877 if (m != MATCH_YES)
6879 gfc_error ("Expected variable name at %C");
6880 return m;
6883 /* Check for an optional array spec. */
6884 m = gfc_match_array_spec (&as, true, false);
6885 if (m == MATCH_ERROR)
6887 gfc_free_array_spec (as);
6888 return m;
6890 else if (m == MATCH_NO)
6892 gfc_free_array_spec (as);
6893 as = NULL;
6896 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6897 return MATCH_ERROR;
6899 gfc_set_sym_referenced (cpte);
6901 if (cpte->as == NULL)
6903 if (!gfc_set_array_spec (cpte, as, &var_locus))
6904 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6906 else if (as != NULL)
6908 gfc_error ("Duplicate array spec for Cray pointee at %C");
6909 gfc_free_array_spec (as);
6910 return MATCH_ERROR;
6913 as = NULL;
6915 if (cpte->as != NULL)
6917 /* Fix array spec. */
6918 m = gfc_mod_pointee_as (cpte->as);
6919 if (m == MATCH_ERROR)
6920 return m;
6923 /* Point the Pointee at the Pointer. */
6924 cpte->cp_pointer = cptr;
6926 if (gfc_match_char (')') != MATCH_YES)
6928 gfc_error ("Expected \")\" at %C");
6929 return MATCH_ERROR;
6931 m = gfc_match_char (',');
6932 if (m != MATCH_YES)
6933 done = true; /* Stop searching for more declarations. */
6937 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6938 || gfc_match_eos () != MATCH_YES)
6940 gfc_error ("Expected %<,%> or end of statement at %C");
6941 return MATCH_ERROR;
6943 return MATCH_YES;
6947 match
6948 gfc_match_external (void)
6951 gfc_clear_attr (&current_attr);
6952 current_attr.external = 1;
6954 return attr_decl ();
6958 match
6959 gfc_match_intent (void)
6961 sym_intent intent;
6963 /* This is not allowed within a BLOCK construct! */
6964 if (gfc_current_state () == COMP_BLOCK)
6966 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6967 return MATCH_ERROR;
6970 intent = match_intent_spec ();
6971 if (intent == INTENT_UNKNOWN)
6972 return MATCH_ERROR;
6974 gfc_clear_attr (&current_attr);
6975 current_attr.intent = intent;
6977 return attr_decl ();
6981 match
6982 gfc_match_intrinsic (void)
6985 gfc_clear_attr (&current_attr);
6986 current_attr.intrinsic = 1;
6988 return attr_decl ();
6992 match
6993 gfc_match_optional (void)
6995 /* This is not allowed within a BLOCK construct! */
6996 if (gfc_current_state () == COMP_BLOCK)
6998 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6999 return MATCH_ERROR;
7002 gfc_clear_attr (&current_attr);
7003 current_attr.optional = 1;
7005 return attr_decl ();
7009 match
7010 gfc_match_pointer (void)
7012 gfc_gobble_whitespace ();
7013 if (gfc_peek_ascii_char () == '(')
7015 if (!flag_cray_pointer)
7017 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7018 "flag");
7019 return MATCH_ERROR;
7021 return cray_pointer_decl ();
7023 else
7025 gfc_clear_attr (&current_attr);
7026 current_attr.pointer = 1;
7028 return attr_decl ();
7033 match
7034 gfc_match_allocatable (void)
7036 gfc_clear_attr (&current_attr);
7037 current_attr.allocatable = 1;
7039 return attr_decl ();
7043 match
7044 gfc_match_codimension (void)
7046 gfc_clear_attr (&current_attr);
7047 current_attr.codimension = 1;
7049 return attr_decl ();
7053 match
7054 gfc_match_contiguous (void)
7056 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
7057 return MATCH_ERROR;
7059 gfc_clear_attr (&current_attr);
7060 current_attr.contiguous = 1;
7062 return attr_decl ();
7066 match
7067 gfc_match_dimension (void)
7069 gfc_clear_attr (&current_attr);
7070 current_attr.dimension = 1;
7072 return attr_decl ();
7076 match
7077 gfc_match_target (void)
7079 gfc_clear_attr (&current_attr);
7080 current_attr.target = 1;
7082 return attr_decl ();
7086 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7087 statement. */
7089 static match
7090 access_attr_decl (gfc_statement st)
7092 char name[GFC_MAX_SYMBOL_LEN + 1];
7093 interface_type type;
7094 gfc_user_op *uop;
7095 gfc_symbol *sym, *dt_sym;
7096 gfc_intrinsic_op op;
7097 match m;
7099 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7100 goto done;
7102 for (;;)
7104 m = gfc_match_generic_spec (&type, name, &op);
7105 if (m == MATCH_NO)
7106 goto syntax;
7107 if (m == MATCH_ERROR)
7108 return MATCH_ERROR;
7110 switch (type)
7112 case INTERFACE_NAMELESS:
7113 case INTERFACE_ABSTRACT:
7114 goto syntax;
7116 case INTERFACE_GENERIC:
7117 if (gfc_get_symbol (name, NULL, &sym))
7118 goto done;
7120 if (!gfc_add_access (&sym->attr,
7121 (st == ST_PUBLIC)
7122 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7123 sym->name, NULL))
7124 return MATCH_ERROR;
7126 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7127 && !gfc_add_access (&dt_sym->attr,
7128 (st == ST_PUBLIC)
7129 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7130 sym->name, NULL))
7131 return MATCH_ERROR;
7133 break;
7135 case INTERFACE_INTRINSIC_OP:
7136 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7138 gfc_intrinsic_op other_op;
7140 gfc_current_ns->operator_access[op] =
7141 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7143 /* Handle the case if there is another op with the same
7144 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7145 other_op = gfc_equivalent_op (op);
7147 if (other_op != INTRINSIC_NONE)
7148 gfc_current_ns->operator_access[other_op] =
7149 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7152 else
7154 gfc_error ("Access specification of the %s operator at %C has "
7155 "already been specified", gfc_op2string (op));
7156 goto done;
7159 break;
7161 case INTERFACE_USER_OP:
7162 uop = gfc_get_uop (name);
7164 if (uop->access == ACCESS_UNKNOWN)
7166 uop->access = (st == ST_PUBLIC)
7167 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7169 else
7171 gfc_error ("Access specification of the .%s. operator at %C "
7172 "has already been specified", sym->name);
7173 goto done;
7176 break;
7179 if (gfc_match_char (',') == MATCH_NO)
7180 break;
7183 if (gfc_match_eos () != MATCH_YES)
7184 goto syntax;
7185 return MATCH_YES;
7187 syntax:
7188 gfc_syntax_error (st);
7190 done:
7191 return MATCH_ERROR;
7195 match
7196 gfc_match_protected (void)
7198 gfc_symbol *sym;
7199 match m;
7201 if (!gfc_current_ns->proc_name
7202 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7204 gfc_error ("PROTECTED at %C only allowed in specification "
7205 "part of a module");
7206 return MATCH_ERROR;
7210 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7211 return MATCH_ERROR;
7213 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7215 return MATCH_ERROR;
7218 if (gfc_match_eos () == MATCH_YES)
7219 goto syntax;
7221 for(;;)
7223 m = gfc_match_symbol (&sym, 0);
7224 switch (m)
7226 case MATCH_YES:
7227 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7228 return MATCH_ERROR;
7229 goto next_item;
7231 case MATCH_NO:
7232 break;
7234 case MATCH_ERROR:
7235 return MATCH_ERROR;
7238 next_item:
7239 if (gfc_match_eos () == MATCH_YES)
7240 break;
7241 if (gfc_match_char (',') != MATCH_YES)
7242 goto syntax;
7245 return MATCH_YES;
7247 syntax:
7248 gfc_error ("Syntax error in PROTECTED statement at %C");
7249 return MATCH_ERROR;
7253 /* The PRIVATE statement is a bit weird in that it can be an attribute
7254 declaration, but also works as a standalone statement inside of a
7255 type declaration or a module. */
7257 match
7258 gfc_match_private (gfc_statement *st)
7261 if (gfc_match ("private") != MATCH_YES)
7262 return MATCH_NO;
7264 if (gfc_current_state () != COMP_MODULE
7265 && !(gfc_current_state () == COMP_DERIVED
7266 && gfc_state_stack->previous
7267 && gfc_state_stack->previous->state == COMP_MODULE)
7268 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7269 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7270 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7272 gfc_error ("PRIVATE statement at %C is only allowed in the "
7273 "specification part of a module");
7274 return MATCH_ERROR;
7277 if (gfc_current_state () == COMP_DERIVED)
7279 if (gfc_match_eos () == MATCH_YES)
7281 *st = ST_PRIVATE;
7282 return MATCH_YES;
7285 gfc_syntax_error (ST_PRIVATE);
7286 return MATCH_ERROR;
7289 if (gfc_match_eos () == MATCH_YES)
7291 *st = ST_PRIVATE;
7292 return MATCH_YES;
7295 *st = ST_ATTR_DECL;
7296 return access_attr_decl (ST_PRIVATE);
7300 match
7301 gfc_match_public (gfc_statement *st)
7304 if (gfc_match ("public") != MATCH_YES)
7305 return MATCH_NO;
7307 if (gfc_current_state () != COMP_MODULE)
7309 gfc_error ("PUBLIC statement at %C is only allowed in the "
7310 "specification part of a module");
7311 return MATCH_ERROR;
7314 if (gfc_match_eos () == MATCH_YES)
7316 *st = ST_PUBLIC;
7317 return MATCH_YES;
7320 *st = ST_ATTR_DECL;
7321 return access_attr_decl (ST_PUBLIC);
7325 /* Workhorse for gfc_match_parameter. */
7327 static match
7328 do_parm (void)
7330 gfc_symbol *sym;
7331 gfc_expr *init;
7332 match m;
7333 bool t;
7335 m = gfc_match_symbol (&sym, 0);
7336 if (m == MATCH_NO)
7337 gfc_error ("Expected variable name at %C in PARAMETER statement");
7339 if (m != MATCH_YES)
7340 return m;
7342 if (gfc_match_char ('=') == MATCH_NO)
7344 gfc_error ("Expected = sign in PARAMETER statement at %C");
7345 return MATCH_ERROR;
7348 m = gfc_match_init_expr (&init);
7349 if (m == MATCH_NO)
7350 gfc_error ("Expected expression at %C in PARAMETER statement");
7351 if (m != MATCH_YES)
7352 return m;
7354 if (sym->ts.type == BT_UNKNOWN
7355 && !gfc_set_default_type (sym, 1, NULL))
7357 m = MATCH_ERROR;
7358 goto cleanup;
7361 if (!gfc_check_assign_symbol (sym, NULL, init)
7362 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7364 m = MATCH_ERROR;
7365 goto cleanup;
7368 if (sym->value)
7370 gfc_error ("Initializing already initialized variable at %C");
7371 m = MATCH_ERROR;
7372 goto cleanup;
7375 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7376 return (t) ? MATCH_YES : MATCH_ERROR;
7378 cleanup:
7379 gfc_free_expr (init);
7380 return m;
7384 /* Match a parameter statement, with the weird syntax that these have. */
7386 match
7387 gfc_match_parameter (void)
7389 match m;
7391 if (gfc_match_char ('(') == MATCH_NO)
7392 return MATCH_NO;
7394 for (;;)
7396 m = do_parm ();
7397 if (m != MATCH_YES)
7398 break;
7400 if (gfc_match (" )%t") == MATCH_YES)
7401 break;
7403 if (gfc_match_char (',') != MATCH_YES)
7405 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7406 m = MATCH_ERROR;
7407 break;
7411 return m;
7415 /* Save statements have a special syntax. */
7417 match
7418 gfc_match_save (void)
7420 char n[GFC_MAX_SYMBOL_LEN+1];
7421 gfc_common_head *c;
7422 gfc_symbol *sym;
7423 match m;
7425 if (gfc_match_eos () == MATCH_YES)
7427 if (gfc_current_ns->seen_save)
7429 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7430 "follows previous SAVE statement"))
7431 return MATCH_ERROR;
7434 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7435 return MATCH_YES;
7438 if (gfc_current_ns->save_all)
7440 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7441 "blanket SAVE statement"))
7442 return MATCH_ERROR;
7445 gfc_match (" ::");
7447 for (;;)
7449 m = gfc_match_symbol (&sym, 0);
7450 switch (m)
7452 case MATCH_YES:
7453 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7454 &gfc_current_locus))
7455 return MATCH_ERROR;
7456 goto next_item;
7458 case MATCH_NO:
7459 break;
7461 case MATCH_ERROR:
7462 return MATCH_ERROR;
7465 m = gfc_match (" / %n /", &n);
7466 if (m == MATCH_ERROR)
7467 return MATCH_ERROR;
7468 if (m == MATCH_NO)
7469 goto syntax;
7471 c = gfc_get_common (n, 0);
7472 c->saved = 1;
7474 gfc_current_ns->seen_save = 1;
7476 next_item:
7477 if (gfc_match_eos () == MATCH_YES)
7478 break;
7479 if (gfc_match_char (',') != MATCH_YES)
7480 goto syntax;
7483 return MATCH_YES;
7485 syntax:
7486 gfc_error ("Syntax error in SAVE statement at %C");
7487 return MATCH_ERROR;
7491 match
7492 gfc_match_value (void)
7494 gfc_symbol *sym;
7495 match m;
7497 /* This is not allowed within a BLOCK construct! */
7498 if (gfc_current_state () == COMP_BLOCK)
7500 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7501 return MATCH_ERROR;
7504 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7505 return MATCH_ERROR;
7507 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7509 return MATCH_ERROR;
7512 if (gfc_match_eos () == MATCH_YES)
7513 goto syntax;
7515 for(;;)
7517 m = gfc_match_symbol (&sym, 0);
7518 switch (m)
7520 case MATCH_YES:
7521 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7522 return MATCH_ERROR;
7523 goto next_item;
7525 case MATCH_NO:
7526 break;
7528 case MATCH_ERROR:
7529 return MATCH_ERROR;
7532 next_item:
7533 if (gfc_match_eos () == MATCH_YES)
7534 break;
7535 if (gfc_match_char (',') != MATCH_YES)
7536 goto syntax;
7539 return MATCH_YES;
7541 syntax:
7542 gfc_error ("Syntax error in VALUE statement at %C");
7543 return MATCH_ERROR;
7547 match
7548 gfc_match_volatile (void)
7550 gfc_symbol *sym;
7551 match m;
7553 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7554 return MATCH_ERROR;
7556 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7558 return MATCH_ERROR;
7561 if (gfc_match_eos () == MATCH_YES)
7562 goto syntax;
7564 for(;;)
7566 /* VOLATILE is special because it can be added to host-associated
7567 symbols locally. Except for coarrays. */
7568 m = gfc_match_symbol (&sym, 1);
7569 switch (m)
7571 case MATCH_YES:
7572 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7573 for variable in a BLOCK which is defined outside of the BLOCK. */
7574 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7576 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7577 "%C, which is use-/host-associated", sym->name);
7578 return MATCH_ERROR;
7580 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7581 return MATCH_ERROR;
7582 goto next_item;
7584 case MATCH_NO:
7585 break;
7587 case MATCH_ERROR:
7588 return MATCH_ERROR;
7591 next_item:
7592 if (gfc_match_eos () == MATCH_YES)
7593 break;
7594 if (gfc_match_char (',') != MATCH_YES)
7595 goto syntax;
7598 return MATCH_YES;
7600 syntax:
7601 gfc_error ("Syntax error in VOLATILE statement at %C");
7602 return MATCH_ERROR;
7606 match
7607 gfc_match_asynchronous (void)
7609 gfc_symbol *sym;
7610 match m;
7612 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7613 return MATCH_ERROR;
7615 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7617 return MATCH_ERROR;
7620 if (gfc_match_eos () == MATCH_YES)
7621 goto syntax;
7623 for(;;)
7625 /* ASYNCHRONOUS is special because it can be added to host-associated
7626 symbols locally. */
7627 m = gfc_match_symbol (&sym, 1);
7628 switch (m)
7630 case MATCH_YES:
7631 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7632 return MATCH_ERROR;
7633 goto next_item;
7635 case MATCH_NO:
7636 break;
7638 case MATCH_ERROR:
7639 return MATCH_ERROR;
7642 next_item:
7643 if (gfc_match_eos () == MATCH_YES)
7644 break;
7645 if (gfc_match_char (',') != MATCH_YES)
7646 goto syntax;
7649 return MATCH_YES;
7651 syntax:
7652 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7653 return MATCH_ERROR;
7657 /* Match a module procedure statement in a submodule. */
7659 match
7660 gfc_match_submod_proc (void)
7662 char name[GFC_MAX_SYMBOL_LEN + 1];
7663 gfc_symbol *sym, *fsym;
7664 match m;
7665 gfc_formal_arglist *formal, *head, *tail;
7667 if (gfc_current_state () != COMP_CONTAINS
7668 || !(gfc_state_stack->previous
7669 && gfc_state_stack->previous->state == COMP_SUBMODULE))
7670 return MATCH_NO;
7672 m = gfc_match (" module% procedure% %n", name);
7673 if (m != MATCH_YES)
7674 return m;
7676 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
7677 "at %C"))
7678 return MATCH_ERROR;
7680 if (get_proc_name (name, &sym, false))
7681 return MATCH_ERROR;
7683 /* Make sure that the result field is appropriately filled, even though
7684 the result symbol will be replaced later on. */
7685 if (sym->ts.interface && sym->ts.interface->attr.function)
7687 if (sym->ts.interface->result
7688 && sym->ts.interface->result != sym->ts.interface)
7689 sym->result= sym->ts.interface->result;
7690 else
7691 sym->result = sym;
7694 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7695 the symbol existed before. */
7696 sym->declared_at = gfc_current_locus;
7698 if (!sym->attr.module_procedure)
7699 return MATCH_ERROR;
7701 /* Signal match_end to expect "end procedure". */
7702 sym->abr_modproc_decl = 1;
7704 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7705 sym->attr.if_source = IFSRC_DECL;
7707 gfc_new_block = sym;
7709 /* Make a new formal arglist with the symbols in the procedure
7710 namespace. */
7711 head = tail = NULL;
7712 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
7714 if (formal == sym->formal)
7715 head = tail = gfc_get_formal_arglist ();
7716 else
7718 tail->next = gfc_get_formal_arglist ();
7719 tail = tail->next;
7722 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
7723 goto cleanup;
7725 tail->sym = fsym;
7726 gfc_set_sym_referenced (fsym);
7729 /* The dummy symbols get cleaned up, when the formal_namespace of the
7730 interface declaration is cleared. This allows us to add the
7731 explicit interface as is done for other type of procedure. */
7732 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
7733 &gfc_current_locus))
7734 return MATCH_ERROR;
7736 if (gfc_match_eos () != MATCH_YES)
7738 gfc_syntax_error (ST_MODULE_PROC);
7739 return MATCH_ERROR;
7742 return MATCH_YES;
7744 cleanup:
7745 gfc_free_formal_arglist (head);
7746 return MATCH_ERROR;
7750 /* Match a module procedure statement. Note that we have to modify
7751 symbols in the parent's namespace because the current one was there
7752 to receive symbols that are in an interface's formal argument list. */
7754 match
7755 gfc_match_modproc (void)
7757 char name[GFC_MAX_SYMBOL_LEN + 1];
7758 gfc_symbol *sym;
7759 match m;
7760 locus old_locus;
7761 gfc_namespace *module_ns;
7762 gfc_interface *old_interface_head, *interface;
7764 if (gfc_state_stack->state != COMP_INTERFACE
7765 || gfc_state_stack->previous == NULL
7766 || current_interface.type == INTERFACE_NAMELESS
7767 || current_interface.type == INTERFACE_ABSTRACT)
7769 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7770 "interface");
7771 return MATCH_ERROR;
7774 module_ns = gfc_current_ns->parent;
7775 for (; module_ns; module_ns = module_ns->parent)
7776 if (module_ns->proc_name->attr.flavor == FL_MODULE
7777 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7778 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7779 && !module_ns->proc_name->attr.contained))
7780 break;
7782 if (module_ns == NULL)
7783 return MATCH_ERROR;
7785 /* Store the current state of the interface. We will need it if we
7786 end up with a syntax error and need to recover. */
7787 old_interface_head = gfc_current_interface_head ();
7789 /* Check if the F2008 optional double colon appears. */
7790 gfc_gobble_whitespace ();
7791 old_locus = gfc_current_locus;
7792 if (gfc_match ("::") == MATCH_YES)
7794 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7795 "MODULE PROCEDURE statement at %L", &old_locus))
7796 return MATCH_ERROR;
7798 else
7799 gfc_current_locus = old_locus;
7801 for (;;)
7803 bool last = false;
7804 old_locus = gfc_current_locus;
7806 m = gfc_match_name (name);
7807 if (m == MATCH_NO)
7808 goto syntax;
7809 if (m != MATCH_YES)
7810 return MATCH_ERROR;
7812 /* Check for syntax error before starting to add symbols to the
7813 current namespace. */
7814 if (gfc_match_eos () == MATCH_YES)
7815 last = true;
7817 if (!last && gfc_match_char (',') != MATCH_YES)
7818 goto syntax;
7820 /* Now we're sure the syntax is valid, we process this item
7821 further. */
7822 if (gfc_get_symbol (name, module_ns, &sym))
7823 return MATCH_ERROR;
7825 if (sym->attr.intrinsic)
7827 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7828 "PROCEDURE", &old_locus);
7829 return MATCH_ERROR;
7832 if (sym->attr.proc != PROC_MODULE
7833 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7834 return MATCH_ERROR;
7836 if (!gfc_add_interface (sym))
7837 return MATCH_ERROR;
7839 sym->attr.mod_proc = 1;
7840 sym->declared_at = old_locus;
7842 if (last)
7843 break;
7846 return MATCH_YES;
7848 syntax:
7849 /* Restore the previous state of the interface. */
7850 interface = gfc_current_interface_head ();
7851 gfc_set_current_interface_head (old_interface_head);
7853 /* Free the new interfaces. */
7854 while (interface != old_interface_head)
7856 gfc_interface *i = interface->next;
7857 free (interface);
7858 interface = i;
7861 /* And issue a syntax error. */
7862 gfc_syntax_error (ST_MODULE_PROC);
7863 return MATCH_ERROR;
7867 /* Check a derived type that is being extended. */
7869 static gfc_symbol*
7870 check_extended_derived_type (char *name)
7872 gfc_symbol *extended;
7874 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7876 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7877 return NULL;
7880 extended = gfc_find_dt_in_generic (extended);
7882 /* F08:C428. */
7883 if (!extended)
7885 gfc_error ("Symbol %qs at %C has not been previously defined", name);
7886 return NULL;
7889 if (extended->attr.flavor != FL_DERIVED)
7891 gfc_error ("%qs in EXTENDS expression at %C is not a "
7892 "derived type", name);
7893 return NULL;
7896 if (extended->attr.is_bind_c)
7898 gfc_error ("%qs cannot be extended at %C because it "
7899 "is BIND(C)", extended->name);
7900 return NULL;
7903 if (extended->attr.sequence)
7905 gfc_error ("%qs cannot be extended at %C because it "
7906 "is a SEQUENCE type", extended->name);
7907 return NULL;
7910 return extended;
7914 /* Match the optional attribute specifiers for a type declaration.
7915 Return MATCH_ERROR if an error is encountered in one of the handled
7916 attributes (public, private, bind(c)), MATCH_NO if what's found is
7917 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7918 checking on attribute conflicts needs to be done. */
7920 match
7921 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7923 /* See if the derived type is marked as private. */
7924 if (gfc_match (" , private") == MATCH_YES)
7926 if (gfc_current_state () != COMP_MODULE)
7928 gfc_error ("Derived type at %C can only be PRIVATE in the "
7929 "specification part of a module");
7930 return MATCH_ERROR;
7933 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7934 return MATCH_ERROR;
7936 else if (gfc_match (" , public") == MATCH_YES)
7938 if (gfc_current_state () != COMP_MODULE)
7940 gfc_error ("Derived type at %C can only be PUBLIC in the "
7941 "specification part of a module");
7942 return MATCH_ERROR;
7945 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7946 return MATCH_ERROR;
7948 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7950 /* If the type is defined to be bind(c) it then needs to make
7951 sure that all fields are interoperable. This will
7952 need to be a semantic check on the finished derived type.
7953 See 15.2.3 (lines 9-12) of F2003 draft. */
7954 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7955 return MATCH_ERROR;
7957 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7959 else if (gfc_match (" , abstract") == MATCH_YES)
7961 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7962 return MATCH_ERROR;
7964 if (!gfc_add_abstract (attr, &gfc_current_locus))
7965 return MATCH_ERROR;
7967 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7969 if (!gfc_add_extension (attr, &gfc_current_locus))
7970 return MATCH_ERROR;
7972 else
7973 return MATCH_NO;
7975 /* If we get here, something matched. */
7976 return MATCH_YES;
7980 /* Match the beginning of a derived type declaration. If a type name
7981 was the result of a function, then it is possible to have a symbol
7982 already to be known as a derived type yet have no components. */
7984 match
7985 gfc_match_derived_decl (void)
7987 char name[GFC_MAX_SYMBOL_LEN + 1];
7988 char parent[GFC_MAX_SYMBOL_LEN + 1];
7989 symbol_attribute attr;
7990 gfc_symbol *sym, *gensym;
7991 gfc_symbol *extended;
7992 match m;
7993 match is_type_attr_spec = MATCH_NO;
7994 bool seen_attr = false;
7995 gfc_interface *intr = NULL, *head;
7997 if (gfc_current_state () == COMP_DERIVED)
7998 return MATCH_NO;
8000 name[0] = '\0';
8001 parent[0] = '\0';
8002 gfc_clear_attr (&attr);
8003 extended = NULL;
8007 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
8008 if (is_type_attr_spec == MATCH_ERROR)
8009 return MATCH_ERROR;
8010 if (is_type_attr_spec == MATCH_YES)
8011 seen_attr = true;
8012 } while (is_type_attr_spec == MATCH_YES);
8014 /* Deal with derived type extensions. The extension attribute has
8015 been added to 'attr' but now the parent type must be found and
8016 checked. */
8017 if (parent[0])
8018 extended = check_extended_derived_type (parent);
8020 if (parent[0] && !extended)
8021 return MATCH_ERROR;
8023 if (gfc_match (" ::") != MATCH_YES && seen_attr)
8025 gfc_error ("Expected :: in TYPE definition at %C");
8026 return MATCH_ERROR;
8029 m = gfc_match (" %n%t", name);
8030 if (m != MATCH_YES)
8031 return m;
8033 /* Make sure the name is not the name of an intrinsic type. */
8034 if (gfc_is_intrinsic_typename (name))
8036 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
8037 "type", name);
8038 return MATCH_ERROR;
8041 if (gfc_get_symbol (name, NULL, &gensym))
8042 return MATCH_ERROR;
8044 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
8046 gfc_error ("Derived type name %qs at %C already has a basic type "
8047 "of %s", gensym->name, gfc_typename (&gensym->ts));
8048 return MATCH_ERROR;
8051 if (!gensym->attr.generic
8052 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
8053 return MATCH_ERROR;
8055 if (!gensym->attr.function
8056 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
8057 return MATCH_ERROR;
8059 sym = gfc_find_dt_in_generic (gensym);
8061 if (sym && (sym->components != NULL || sym->attr.zero_comp))
8063 gfc_error ("Derived type definition of %qs at %C has already been "
8064 "defined", sym->name);
8065 return MATCH_ERROR;
8068 if (!sym)
8070 /* Use upper case to save the actual derived-type symbol. */
8071 gfc_get_symbol (gfc_get_string ("%c%s",
8072 (char) TOUPPER ((unsigned char) gensym->name[0]),
8073 &gensym->name[1]), NULL, &sym);
8074 sym->name = gfc_get_string (gensym->name);
8075 head = gensym->generic;
8076 intr = gfc_get_interface ();
8077 intr->sym = sym;
8078 intr->where = gfc_current_locus;
8079 intr->sym->declared_at = gfc_current_locus;
8080 intr->next = head;
8081 gensym->generic = intr;
8082 gensym->attr.if_source = IFSRC_DECL;
8085 /* The symbol may already have the derived attribute without the
8086 components. The ways this can happen is via a function
8087 definition, an INTRINSIC statement or a subtype in another
8088 derived type that is a pointer. The first part of the AND clause
8089 is true if the symbol is not the return value of a function. */
8090 if (sym->attr.flavor != FL_DERIVED
8091 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
8092 return MATCH_ERROR;
8094 if (attr.access != ACCESS_UNKNOWN
8095 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
8096 return MATCH_ERROR;
8097 else if (sym->attr.access == ACCESS_UNKNOWN
8098 && gensym->attr.access != ACCESS_UNKNOWN
8099 && !gfc_add_access (&sym->attr, gensym->attr.access,
8100 sym->name, NULL))
8101 return MATCH_ERROR;
8103 if (sym->attr.access != ACCESS_UNKNOWN
8104 && gensym->attr.access == ACCESS_UNKNOWN)
8105 gensym->attr.access = sym->attr.access;
8107 /* See if the derived type was labeled as bind(c). */
8108 if (attr.is_bind_c != 0)
8109 sym->attr.is_bind_c = attr.is_bind_c;
8111 /* Construct the f2k_derived namespace if it is not yet there. */
8112 if (!sym->f2k_derived)
8113 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8115 if (extended && !sym->components)
8117 gfc_component *p;
8119 /* Add the extended derived type as the first component. */
8120 gfc_add_component (sym, parent, &p);
8121 extended->refs++;
8122 gfc_set_sym_referenced (extended);
8124 p->ts.type = BT_DERIVED;
8125 p->ts.u.derived = extended;
8126 p->initializer = gfc_default_initializer (&p->ts);
8128 /* Set extension level. */
8129 if (extended->attr.extension == 255)
8131 /* Since the extension field is 8 bit wide, we can only have
8132 up to 255 extension levels. */
8133 gfc_error ("Maximum extension level reached with type %qs at %L",
8134 extended->name, &extended->declared_at);
8135 return MATCH_ERROR;
8137 sym->attr.extension = extended->attr.extension + 1;
8139 /* Provide the links between the extended type and its extension. */
8140 if (!extended->f2k_derived)
8141 extended->f2k_derived = gfc_get_namespace (NULL, 0);
8144 if (!sym->hash_value)
8145 /* Set the hash for the compound name for this type. */
8146 sym->hash_value = gfc_hash_value (sym);
8148 /* Take over the ABSTRACT attribute. */
8149 sym->attr.abstract = attr.abstract;
8151 gfc_new_block = sym;
8153 return MATCH_YES;
8157 /* Cray Pointees can be declared as:
8158 pointer (ipt, a (n,m,...,*)) */
8160 match
8161 gfc_mod_pointee_as (gfc_array_spec *as)
8163 as->cray_pointee = true; /* This will be useful to know later. */
8164 if (as->type == AS_ASSUMED_SIZE)
8165 as->cp_was_assumed = true;
8166 else if (as->type == AS_ASSUMED_SHAPE)
8168 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8169 return MATCH_ERROR;
8171 return MATCH_YES;
8175 /* Match the enum definition statement, here we are trying to match
8176 the first line of enum definition statement.
8177 Returns MATCH_YES if match is found. */
8179 match
8180 gfc_match_enum (void)
8182 match m;
8184 m = gfc_match_eos ();
8185 if (m != MATCH_YES)
8186 return m;
8188 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
8189 return MATCH_ERROR;
8191 return MATCH_YES;
8195 /* Returns an initializer whose value is one higher than the value of the
8196 LAST_INITIALIZER argument. If the argument is NULL, the
8197 initializers value will be set to zero. The initializer's kind
8198 will be set to gfc_c_int_kind.
8200 If -fshort-enums is given, the appropriate kind will be selected
8201 later after all enumerators have been parsed. A warning is issued
8202 here if an initializer exceeds gfc_c_int_kind. */
8204 static gfc_expr *
8205 enum_initializer (gfc_expr *last_initializer, locus where)
8207 gfc_expr *result;
8208 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
8210 mpz_init (result->value.integer);
8212 if (last_initializer != NULL)
8214 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8215 result->where = last_initializer->where;
8217 if (gfc_check_integer_range (result->value.integer,
8218 gfc_c_int_kind) != ARITH_OK)
8220 gfc_error ("Enumerator exceeds the C integer type at %C");
8221 return NULL;
8224 else
8226 /* Control comes here, if it's the very first enumerator and no
8227 initializer has been given. It will be initialized to zero. */
8228 mpz_set_si (result->value.integer, 0);
8231 return result;
8235 /* Match a variable name with an optional initializer. When this
8236 subroutine is called, a variable is expected to be parsed next.
8237 Depending on what is happening at the moment, updates either the
8238 symbol table or the current interface. */
8240 static match
8241 enumerator_decl (void)
8243 char name[GFC_MAX_SYMBOL_LEN + 1];
8244 gfc_expr *initializer;
8245 gfc_array_spec *as = NULL;
8246 gfc_symbol *sym;
8247 locus var_locus;
8248 match m;
8249 bool t;
8250 locus old_locus;
8252 initializer = NULL;
8253 old_locus = gfc_current_locus;
8255 /* When we get here, we've just matched a list of attributes and
8256 maybe a type and a double colon. The next thing we expect to see
8257 is the name of the symbol. */
8258 m = gfc_match_name (name);
8259 if (m != MATCH_YES)
8260 goto cleanup;
8262 var_locus = gfc_current_locus;
8264 /* OK, we've successfully matched the declaration. Now put the
8265 symbol in the current namespace. If we fail to create the symbol,
8266 bail out. */
8267 if (!build_sym (name, NULL, false, &as, &var_locus))
8269 m = MATCH_ERROR;
8270 goto cleanup;
8273 /* The double colon must be present in order to have initializers.
8274 Otherwise the statement is ambiguous with an assignment statement. */
8275 if (colon_seen)
8277 if (gfc_match_char ('=') == MATCH_YES)
8279 m = gfc_match_init_expr (&initializer);
8280 if (m == MATCH_NO)
8282 gfc_error ("Expected an initialization expression at %C");
8283 m = MATCH_ERROR;
8286 if (m != MATCH_YES)
8287 goto cleanup;
8291 /* If we do not have an initializer, the initialization value of the
8292 previous enumerator (stored in last_initializer) is incremented
8293 by 1 and is used to initialize the current enumerator. */
8294 if (initializer == NULL)
8295 initializer = enum_initializer (last_initializer, old_locus);
8297 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8299 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8300 &var_locus);
8301 m = MATCH_ERROR;
8302 goto cleanup;
8305 /* Store this current initializer, for the next enumerator variable
8306 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8307 use last_initializer below. */
8308 last_initializer = initializer;
8309 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8311 /* Maintain enumerator history. */
8312 gfc_find_symbol (name, NULL, 0, &sym);
8313 create_enum_history (sym, last_initializer);
8315 return (t) ? MATCH_YES : MATCH_ERROR;
8317 cleanup:
8318 /* Free stuff up and return. */
8319 gfc_free_expr (initializer);
8321 return m;
8325 /* Match the enumerator definition statement. */
8327 match
8328 gfc_match_enumerator_def (void)
8330 match m;
8331 bool t;
8333 gfc_clear_ts (&current_ts);
8335 m = gfc_match (" enumerator");
8336 if (m != MATCH_YES)
8337 return m;
8339 m = gfc_match (" :: ");
8340 if (m == MATCH_ERROR)
8341 return m;
8343 colon_seen = (m == MATCH_YES);
8345 if (gfc_current_state () != COMP_ENUM)
8347 gfc_error ("ENUM definition statement expected before %C");
8348 gfc_free_enum_history ();
8349 return MATCH_ERROR;
8352 (&current_ts)->type = BT_INTEGER;
8353 (&current_ts)->kind = gfc_c_int_kind;
8355 gfc_clear_attr (&current_attr);
8356 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
8357 if (!t)
8359 m = MATCH_ERROR;
8360 goto cleanup;
8363 for (;;)
8365 m = enumerator_decl ();
8366 if (m == MATCH_ERROR)
8368 gfc_free_enum_history ();
8369 goto cleanup;
8371 if (m == MATCH_NO)
8372 break;
8374 if (gfc_match_eos () == MATCH_YES)
8375 goto cleanup;
8376 if (gfc_match_char (',') != MATCH_YES)
8377 break;
8380 if (gfc_current_state () == COMP_ENUM)
8382 gfc_free_enum_history ();
8383 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8384 m = MATCH_ERROR;
8387 cleanup:
8388 gfc_free_array_spec (current_as);
8389 current_as = NULL;
8390 return m;
8395 /* Match binding attributes. */
8397 static match
8398 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
8400 bool found_passing = false;
8401 bool seen_ptr = false;
8402 match m = MATCH_YES;
8404 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8405 this case the defaults are in there. */
8406 ba->access = ACCESS_UNKNOWN;
8407 ba->pass_arg = NULL;
8408 ba->pass_arg_num = 0;
8409 ba->nopass = 0;
8410 ba->non_overridable = 0;
8411 ba->deferred = 0;
8412 ba->ppc = ppc;
8414 /* If we find a comma, we believe there are binding attributes. */
8415 m = gfc_match_char (',');
8416 if (m == MATCH_NO)
8417 goto done;
8421 /* Access specifier. */
8423 m = gfc_match (" public");
8424 if (m == MATCH_ERROR)
8425 goto error;
8426 if (m == MATCH_YES)
8428 if (ba->access != ACCESS_UNKNOWN)
8430 gfc_error ("Duplicate access-specifier at %C");
8431 goto error;
8434 ba->access = ACCESS_PUBLIC;
8435 continue;
8438 m = gfc_match (" private");
8439 if (m == MATCH_ERROR)
8440 goto error;
8441 if (m == MATCH_YES)
8443 if (ba->access != ACCESS_UNKNOWN)
8445 gfc_error ("Duplicate access-specifier at %C");
8446 goto error;
8449 ba->access = ACCESS_PRIVATE;
8450 continue;
8453 /* If inside GENERIC, the following is not allowed. */
8454 if (!generic)
8457 /* NOPASS flag. */
8458 m = gfc_match (" nopass");
8459 if (m == MATCH_ERROR)
8460 goto error;
8461 if (m == MATCH_YES)
8463 if (found_passing)
8465 gfc_error ("Binding attributes already specify passing,"
8466 " illegal NOPASS at %C");
8467 goto error;
8470 found_passing = true;
8471 ba->nopass = 1;
8472 continue;
8475 /* PASS possibly including argument. */
8476 m = gfc_match (" pass");
8477 if (m == MATCH_ERROR)
8478 goto error;
8479 if (m == MATCH_YES)
8481 char arg[GFC_MAX_SYMBOL_LEN + 1];
8483 if (found_passing)
8485 gfc_error ("Binding attributes already specify passing,"
8486 " illegal PASS at %C");
8487 goto error;
8490 m = gfc_match (" ( %n )", arg);
8491 if (m == MATCH_ERROR)
8492 goto error;
8493 if (m == MATCH_YES)
8494 ba->pass_arg = gfc_get_string (arg);
8495 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8497 found_passing = true;
8498 ba->nopass = 0;
8499 continue;
8502 if (ppc)
8504 /* POINTER flag. */
8505 m = gfc_match (" pointer");
8506 if (m == MATCH_ERROR)
8507 goto error;
8508 if (m == MATCH_YES)
8510 if (seen_ptr)
8512 gfc_error ("Duplicate POINTER attribute at %C");
8513 goto error;
8516 seen_ptr = true;
8517 continue;
8520 else
8522 /* NON_OVERRIDABLE flag. */
8523 m = gfc_match (" non_overridable");
8524 if (m == MATCH_ERROR)
8525 goto error;
8526 if (m == MATCH_YES)
8528 if (ba->non_overridable)
8530 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8531 goto error;
8534 ba->non_overridable = 1;
8535 continue;
8538 /* DEFERRED flag. */
8539 m = gfc_match (" deferred");
8540 if (m == MATCH_ERROR)
8541 goto error;
8542 if (m == MATCH_YES)
8544 if (ba->deferred)
8546 gfc_error ("Duplicate DEFERRED at %C");
8547 goto error;
8550 ba->deferred = 1;
8551 continue;
8557 /* Nothing matching found. */
8558 if (generic)
8559 gfc_error ("Expected access-specifier at %C");
8560 else
8561 gfc_error ("Expected binding attribute at %C");
8562 goto error;
8564 while (gfc_match_char (',') == MATCH_YES);
8566 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8567 if (ba->non_overridable && ba->deferred)
8569 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8570 goto error;
8573 m = MATCH_YES;
8575 done:
8576 if (ba->access == ACCESS_UNKNOWN)
8577 ba->access = gfc_typebound_default_access;
8579 if (ppc && !seen_ptr)
8581 gfc_error ("POINTER attribute is required for procedure pointer component"
8582 " at %C");
8583 goto error;
8586 return m;
8588 error:
8589 return MATCH_ERROR;
8593 /* Match a PROCEDURE specific binding inside a derived type. */
8595 static match
8596 match_procedure_in_type (void)
8598 char name[GFC_MAX_SYMBOL_LEN + 1];
8599 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8600 char* target = NULL, *ifc = NULL;
8601 gfc_typebound_proc tb;
8602 bool seen_colons;
8603 bool seen_attrs;
8604 match m;
8605 gfc_symtree* stree;
8606 gfc_namespace* ns;
8607 gfc_symbol* block;
8608 int num;
8610 /* Check current state. */
8611 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8612 block = gfc_state_stack->previous->sym;
8613 gcc_assert (block);
8615 /* Try to match PROCEDURE(interface). */
8616 if (gfc_match (" (") == MATCH_YES)
8618 m = gfc_match_name (target_buf);
8619 if (m == MATCH_ERROR)
8620 return m;
8621 if (m != MATCH_YES)
8623 gfc_error ("Interface-name expected after %<(%> at %C");
8624 return MATCH_ERROR;
8627 if (gfc_match (" )") != MATCH_YES)
8629 gfc_error ("%<)%> expected at %C");
8630 return MATCH_ERROR;
8633 ifc = target_buf;
8636 /* Construct the data structure. */
8637 memset (&tb, 0, sizeof (tb));
8638 tb.where = gfc_current_locus;
8640 /* Match binding attributes. */
8641 m = match_binding_attributes (&tb, false, false);
8642 if (m == MATCH_ERROR)
8643 return m;
8644 seen_attrs = (m == MATCH_YES);
8646 /* Check that attribute DEFERRED is given if an interface is specified. */
8647 if (tb.deferred && !ifc)
8649 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8650 return MATCH_ERROR;
8652 if (ifc && !tb.deferred)
8654 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8655 return MATCH_ERROR;
8658 /* Match the colons. */
8659 m = gfc_match (" ::");
8660 if (m == MATCH_ERROR)
8661 return m;
8662 seen_colons = (m == MATCH_YES);
8663 if (seen_attrs && !seen_colons)
8665 gfc_error ("Expected %<::%> after binding-attributes at %C");
8666 return MATCH_ERROR;
8669 /* Match the binding names. */
8670 for(num=1;;num++)
8672 m = gfc_match_name (name);
8673 if (m == MATCH_ERROR)
8674 return m;
8675 if (m == MATCH_NO)
8677 gfc_error ("Expected binding name at %C");
8678 return MATCH_ERROR;
8681 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8682 return MATCH_ERROR;
8684 /* Try to match the '=> target', if it's there. */
8685 target = ifc;
8686 m = gfc_match (" =>");
8687 if (m == MATCH_ERROR)
8688 return m;
8689 if (m == MATCH_YES)
8691 if (tb.deferred)
8693 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8694 return MATCH_ERROR;
8697 if (!seen_colons)
8699 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8700 " at %C");
8701 return MATCH_ERROR;
8704 m = gfc_match_name (target_buf);
8705 if (m == MATCH_ERROR)
8706 return m;
8707 if (m == MATCH_NO)
8709 gfc_error ("Expected binding target after %<=>%> at %C");
8710 return MATCH_ERROR;
8712 target = target_buf;
8715 /* If no target was found, it has the same name as the binding. */
8716 if (!target)
8717 target = name;
8719 /* Get the namespace to insert the symbols into. */
8720 ns = block->f2k_derived;
8721 gcc_assert (ns);
8723 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8724 if (tb.deferred && !block->attr.abstract)
8726 gfc_error ("Type %qs containing DEFERRED binding at %C "
8727 "is not ABSTRACT", block->name);
8728 return MATCH_ERROR;
8731 /* See if we already have a binding with this name in the symtree which
8732 would be an error. If a GENERIC already targeted this binding, it may
8733 be already there but then typebound is still NULL. */
8734 stree = gfc_find_symtree (ns->tb_sym_root, name);
8735 if (stree && stree->n.tb)
8737 gfc_error ("There is already a procedure with binding name %qs for "
8738 "the derived type %qs at %C", name, block->name);
8739 return MATCH_ERROR;
8742 /* Insert it and set attributes. */
8744 if (!stree)
8746 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8747 gcc_assert (stree);
8749 stree->n.tb = gfc_get_typebound_proc (&tb);
8751 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8752 false))
8753 return MATCH_ERROR;
8754 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8756 if (gfc_match_eos () == MATCH_YES)
8757 return MATCH_YES;
8758 if (gfc_match_char (',') != MATCH_YES)
8759 goto syntax;
8762 syntax:
8763 gfc_error ("Syntax error in PROCEDURE statement at %C");
8764 return MATCH_ERROR;
8768 /* Match a GENERIC procedure binding inside a derived type. */
8770 match
8771 gfc_match_generic (void)
8773 char name[GFC_MAX_SYMBOL_LEN + 1];
8774 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8775 gfc_symbol* block;
8776 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8777 gfc_typebound_proc* tb;
8778 gfc_namespace* ns;
8779 interface_type op_type;
8780 gfc_intrinsic_op op;
8781 match m;
8783 /* Check current state. */
8784 if (gfc_current_state () == COMP_DERIVED)
8786 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8787 return MATCH_ERROR;
8789 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8790 return MATCH_NO;
8791 block = gfc_state_stack->previous->sym;
8792 ns = block->f2k_derived;
8793 gcc_assert (block && ns);
8795 memset (&tbattr, 0, sizeof (tbattr));
8796 tbattr.where = gfc_current_locus;
8798 /* See if we get an access-specifier. */
8799 m = match_binding_attributes (&tbattr, true, false);
8800 if (m == MATCH_ERROR)
8801 goto error;
8803 /* Now the colons, those are required. */
8804 if (gfc_match (" ::") != MATCH_YES)
8806 gfc_error ("Expected %<::%> at %C");
8807 goto error;
8810 /* Match the binding name; depending on type (operator / generic) format
8811 it for future error messages into bind_name. */
8813 m = gfc_match_generic_spec (&op_type, name, &op);
8814 if (m == MATCH_ERROR)
8815 return MATCH_ERROR;
8816 if (m == MATCH_NO)
8818 gfc_error ("Expected generic name or operator descriptor at %C");
8819 goto error;
8822 switch (op_type)
8824 case INTERFACE_GENERIC:
8825 snprintf (bind_name, sizeof (bind_name), "%s", name);
8826 break;
8828 case INTERFACE_USER_OP:
8829 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8830 break;
8832 case INTERFACE_INTRINSIC_OP:
8833 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8834 gfc_op2string (op));
8835 break;
8837 case INTERFACE_NAMELESS:
8838 gfc_error ("Malformed GENERIC statement at %C");
8839 goto error;
8840 break;
8842 default:
8843 gcc_unreachable ();
8846 /* Match the required =>. */
8847 if (gfc_match (" =>") != MATCH_YES)
8849 gfc_error ("Expected %<=>%> at %C");
8850 goto error;
8853 /* Try to find existing GENERIC binding with this name / for this operator;
8854 if there is something, check that it is another GENERIC and then extend
8855 it rather than building a new node. Otherwise, create it and put it
8856 at the right position. */
8858 switch (op_type)
8860 case INTERFACE_USER_OP:
8861 case INTERFACE_GENERIC:
8863 const bool is_op = (op_type == INTERFACE_USER_OP);
8864 gfc_symtree* st;
8866 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8867 if (st)
8869 tb = st->n.tb;
8870 gcc_assert (tb);
8872 else
8873 tb = NULL;
8875 break;
8878 case INTERFACE_INTRINSIC_OP:
8879 tb = ns->tb_op[op];
8880 break;
8882 default:
8883 gcc_unreachable ();
8886 if (tb)
8888 if (!tb->is_generic)
8890 gcc_assert (op_type == INTERFACE_GENERIC);
8891 gfc_error ("There's already a non-generic procedure with binding name"
8892 " %qs for the derived type %qs at %C",
8893 bind_name, block->name);
8894 goto error;
8897 if (tb->access != tbattr.access)
8899 gfc_error ("Binding at %C must have the same access as already"
8900 " defined binding %qs", bind_name);
8901 goto error;
8904 else
8906 tb = gfc_get_typebound_proc (NULL);
8907 tb->where = gfc_current_locus;
8908 tb->access = tbattr.access;
8909 tb->is_generic = 1;
8910 tb->u.generic = NULL;
8912 switch (op_type)
8914 case INTERFACE_GENERIC:
8915 case INTERFACE_USER_OP:
8917 const bool is_op = (op_type == INTERFACE_USER_OP);
8918 gfc_symtree* st;
8920 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8921 name);
8922 gcc_assert (st);
8923 st->n.tb = tb;
8925 break;
8928 case INTERFACE_INTRINSIC_OP:
8929 ns->tb_op[op] = tb;
8930 break;
8932 default:
8933 gcc_unreachable ();
8937 /* Now, match all following names as specific targets. */
8940 gfc_symtree* target_st;
8941 gfc_tbp_generic* target;
8943 m = gfc_match_name (name);
8944 if (m == MATCH_ERROR)
8945 goto error;
8946 if (m == MATCH_NO)
8948 gfc_error ("Expected specific binding name at %C");
8949 goto error;
8952 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8954 /* See if this is a duplicate specification. */
8955 for (target = tb->u.generic; target; target = target->next)
8956 if (target_st == target->specific_st)
8958 gfc_error ("%qs already defined as specific binding for the"
8959 " generic %qs at %C", name, bind_name);
8960 goto error;
8963 target = gfc_get_tbp_generic ();
8964 target->specific_st = target_st;
8965 target->specific = NULL;
8966 target->next = tb->u.generic;
8967 target->is_operator = ((op_type == INTERFACE_USER_OP)
8968 || (op_type == INTERFACE_INTRINSIC_OP));
8969 tb->u.generic = target;
8971 while (gfc_match (" ,") == MATCH_YES);
8973 /* Here should be the end. */
8974 if (gfc_match_eos () != MATCH_YES)
8976 gfc_error ("Junk after GENERIC binding at %C");
8977 goto error;
8980 return MATCH_YES;
8982 error:
8983 return MATCH_ERROR;
8987 /* Match a FINAL declaration inside a derived type. */
8989 match
8990 gfc_match_final_decl (void)
8992 char name[GFC_MAX_SYMBOL_LEN + 1];
8993 gfc_symbol* sym;
8994 match m;
8995 gfc_namespace* module_ns;
8996 bool first, last;
8997 gfc_symbol* block;
8999 if (gfc_current_form == FORM_FREE)
9001 char c = gfc_peek_ascii_char ();
9002 if (!gfc_is_whitespace (c) && c != ':')
9003 return MATCH_NO;
9006 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
9008 if (gfc_current_form == FORM_FIXED)
9009 return MATCH_NO;
9011 gfc_error ("FINAL declaration at %C must be inside a derived type "
9012 "CONTAINS section");
9013 return MATCH_ERROR;
9016 block = gfc_state_stack->previous->sym;
9017 gcc_assert (block);
9019 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
9020 || gfc_state_stack->previous->previous->state != COMP_MODULE)
9022 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9023 " specification part of a MODULE");
9024 return MATCH_ERROR;
9027 module_ns = gfc_current_ns;
9028 gcc_assert (module_ns);
9029 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
9031 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9032 if (gfc_match (" ::") == MATCH_ERROR)
9033 return MATCH_ERROR;
9035 /* Match the sequence of procedure names. */
9036 first = true;
9037 last = false;
9040 gfc_finalizer* f;
9042 if (first && gfc_match_eos () == MATCH_YES)
9044 gfc_error ("Empty FINAL at %C");
9045 return MATCH_ERROR;
9048 m = gfc_match_name (name);
9049 if (m == MATCH_NO)
9051 gfc_error ("Expected module procedure name at %C");
9052 return MATCH_ERROR;
9054 else if (m != MATCH_YES)
9055 return MATCH_ERROR;
9057 if (gfc_match_eos () == MATCH_YES)
9058 last = true;
9059 if (!last && gfc_match_char (',') != MATCH_YES)
9061 gfc_error ("Expected %<,%> at %C");
9062 return MATCH_ERROR;
9065 if (gfc_get_symbol (name, module_ns, &sym))
9067 gfc_error ("Unknown procedure name %qs at %C", name);
9068 return MATCH_ERROR;
9071 /* Mark the symbol as module procedure. */
9072 if (sym->attr.proc != PROC_MODULE
9073 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9074 return MATCH_ERROR;
9076 /* Check if we already have this symbol in the list, this is an error. */
9077 for (f = block->f2k_derived->finalizers; f; f = f->next)
9078 if (f->proc_sym == sym)
9080 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9081 name);
9082 return MATCH_ERROR;
9085 /* Add this symbol to the list of finalizers. */
9086 gcc_assert (block->f2k_derived);
9087 sym->refs++;
9088 f = XCNEW (gfc_finalizer);
9089 f->proc_sym = sym;
9090 f->proc_tree = NULL;
9091 f->where = gfc_current_locus;
9092 f->next = block->f2k_derived->finalizers;
9093 block->f2k_derived->finalizers = f;
9095 first = false;
9097 while (!last);
9099 return MATCH_YES;
9103 const ext_attr_t ext_attr_list[] = {
9104 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9105 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9106 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9107 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9108 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9109 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9110 { NULL, EXT_ATTR_LAST, NULL }
9113 /* Match a !GCC$ ATTRIBUTES statement of the form:
9114 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9115 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9117 TODO: We should support all GCC attributes using the same syntax for
9118 the attribute list, i.e. the list in C
9119 __attributes(( attribute-list ))
9120 matches then
9121 !GCC$ ATTRIBUTES attribute-list ::
9122 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9123 saved into a TREE.
9125 As there is absolutely no risk of confusion, we should never return
9126 MATCH_NO. */
9127 match
9128 gfc_match_gcc_attributes (void)
9130 symbol_attribute attr;
9131 char name[GFC_MAX_SYMBOL_LEN + 1];
9132 unsigned id;
9133 gfc_symbol *sym;
9134 match m;
9136 gfc_clear_attr (&attr);
9137 for(;;)
9139 char ch;
9141 if (gfc_match_name (name) != MATCH_YES)
9142 return MATCH_ERROR;
9144 for (id = 0; id < EXT_ATTR_LAST; id++)
9145 if (strcmp (name, ext_attr_list[id].name) == 0)
9146 break;
9148 if (id == EXT_ATTR_LAST)
9150 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9151 return MATCH_ERROR;
9154 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
9155 return MATCH_ERROR;
9157 gfc_gobble_whitespace ();
9158 ch = gfc_next_ascii_char ();
9159 if (ch == ':')
9161 /* This is the successful exit condition for the loop. */
9162 if (gfc_next_ascii_char () == ':')
9163 break;
9166 if (ch == ',')
9167 continue;
9169 goto syntax;
9172 if (gfc_match_eos () == MATCH_YES)
9173 goto syntax;
9175 for(;;)
9177 m = gfc_match_name (name);
9178 if (m != MATCH_YES)
9179 return m;
9181 if (find_special (name, &sym, true))
9182 return MATCH_ERROR;
9184 sym->attr.ext_attr |= attr.ext_attr;
9186 if (gfc_match_eos () == MATCH_YES)
9187 break;
9189 if (gfc_match_char (',') != MATCH_YES)
9190 goto syntax;
9193 return MATCH_YES;
9195 syntax:
9196 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9197 return MATCH_ERROR;