2016-03-12 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / decl.c
blob80ec39cb86b4869d1ef9da27306c0fd317544df8
1 /* Declaration statement matcher
2 Copyright (C) 2002-2016 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;
1218 int upper;
1220 if (gfc_get_symbol (name, NULL, &sym))
1221 return false;
1223 /* Check if the name has already been defined as a type. The
1224 first letter of the symtree will be in upper case then. Of
1225 course, this is only necessary if the upper case letter is
1226 actually different. */
1228 upper = TOUPPER(name[0]);
1229 if (upper != name[0])
1231 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1232 gfc_symtree *st;
1233 int nlen;
1235 nlen = strlen(name);
1236 gcc_assert (nlen <= GFC_MAX_SYMBOL_LEN);
1237 strncpy (u_name, name, nlen + 1);
1238 u_name[0] = upper;
1240 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1242 if (st != 0)
1244 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1245 &st->n.sym->declared_at);
1246 return false;
1250 /* Start updating the symbol table. Add basic type attribute if present. */
1251 if (current_ts.type != BT_UNKNOWN
1252 && (sym->attr.implicit_type == 0
1253 || !gfc_compare_types (&sym->ts, &current_ts))
1254 && !gfc_add_type (sym, &current_ts, var_locus))
1255 return false;
1257 if (sym->ts.type == BT_CHARACTER)
1259 sym->ts.u.cl = cl;
1260 sym->ts.deferred = cl_deferred;
1263 /* Add dimension attribute if present. */
1264 if (!gfc_set_array_spec (sym, *as, var_locus))
1265 return false;
1266 *as = NULL;
1268 /* Add attribute to symbol. The copy is so that we can reset the
1269 dimension attribute. */
1270 attr = current_attr;
1271 attr.dimension = 0;
1272 attr.codimension = 0;
1274 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1275 return false;
1277 /* Finish any work that may need to be done for the binding label,
1278 if it's a bind(c). The bind(c) attr is found before the symbol
1279 is made, and before the symbol name (for data decls), so the
1280 current_ts is holding the binding label, or nothing if the
1281 name= attr wasn't given. Therefore, test here if we're dealing
1282 with a bind(c) and make sure the binding label is set correctly. */
1283 if (sym->attr.is_bind_c == 1)
1285 if (!sym->binding_label)
1287 /* Set the binding label and verify that if a NAME= was specified
1288 then only one identifier was in the entity-decl-list. */
1289 if (!set_binding_label (&sym->binding_label, sym->name,
1290 num_idents_on_line))
1291 return false;
1295 /* See if we know we're in a common block, and if it's a bind(c)
1296 common then we need to make sure we're an interoperable type. */
1297 if (sym->attr.in_common == 1)
1299 /* Test the common block object. */
1300 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1301 && sym->ts.is_c_interop != 1)
1303 gfc_error_now ("Variable %qs in common block %qs at %C "
1304 "must be declared with a C interoperable "
1305 "kind since common block %qs is BIND(C)",
1306 sym->name, sym->common_block->name,
1307 sym->common_block->name);
1308 gfc_clear_error ();
1312 sym->attr.implied_index = 0;
1314 if (sym->ts.type == BT_CLASS)
1315 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1317 return true;
1321 /* Set character constant to the given length. The constant will be padded or
1322 truncated. If we're inside an array constructor without a typespec, we
1323 additionally check that all elements have the same length; check_len -1
1324 means no checking. */
1326 void
1327 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1329 gfc_char_t *s;
1330 int slen;
1332 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1334 if (expr->ts.type != BT_CHARACTER)
1335 return;
1337 slen = expr->value.character.length;
1338 if (len != slen)
1340 s = gfc_get_wide_string (len + 1);
1341 memcpy (s, expr->value.character.string,
1342 MIN (len, slen) * sizeof (gfc_char_t));
1343 if (len > slen)
1344 gfc_wide_memset (&s[slen], ' ', len - slen);
1346 if (warn_character_truncation && slen > len)
1347 gfc_warning_now (OPT_Wcharacter_truncation,
1348 "CHARACTER expression at %L is being truncated "
1349 "(%d/%d)", &expr->where, slen, len);
1351 /* Apply the standard by 'hand' otherwise it gets cleared for
1352 initializers. */
1353 if (check_len != -1 && slen != check_len
1354 && !(gfc_option.allow_std & GFC_STD_GNU))
1355 gfc_error_now ("The CHARACTER elements of the array constructor "
1356 "at %L must have the same length (%d/%d)",
1357 &expr->where, slen, check_len);
1359 s[len] = '\0';
1360 free (expr->value.character.string);
1361 expr->value.character.string = s;
1362 expr->value.character.length = len;
1367 /* Function to create and update the enumerator history
1368 using the information passed as arguments.
1369 Pointer "max_enum" is also updated, to point to
1370 enum history node containing largest initializer.
1372 SYM points to the symbol node of enumerator.
1373 INIT points to its enumerator value. */
1375 static void
1376 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1378 enumerator_history *new_enum_history;
1379 gcc_assert (sym != NULL && init != NULL);
1381 new_enum_history = XCNEW (enumerator_history);
1383 new_enum_history->sym = sym;
1384 new_enum_history->initializer = init;
1385 new_enum_history->next = NULL;
1387 if (enum_history == NULL)
1389 enum_history = new_enum_history;
1390 max_enum = enum_history;
1392 else
1394 new_enum_history->next = enum_history;
1395 enum_history = new_enum_history;
1397 if (mpz_cmp (max_enum->initializer->value.integer,
1398 new_enum_history->initializer->value.integer) < 0)
1399 max_enum = new_enum_history;
1404 /* Function to free enum kind history. */
1406 void
1407 gfc_free_enum_history (void)
1409 enumerator_history *current = enum_history;
1410 enumerator_history *next;
1412 while (current != NULL)
1414 next = current->next;
1415 free (current);
1416 current = next;
1418 max_enum = NULL;
1419 enum_history = NULL;
1423 /* Function called by variable_decl() that adds an initialization
1424 expression to a symbol. */
1426 static bool
1427 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1429 symbol_attribute attr;
1430 gfc_symbol *sym;
1431 gfc_expr *init;
1433 init = *initp;
1434 if (find_special (name, &sym, false))
1435 return false;
1437 attr = sym->attr;
1439 /* If this symbol is confirming an implicit parameter type,
1440 then an initialization expression is not allowed. */
1441 if (attr.flavor == FL_PARAMETER
1442 && sym->value != NULL
1443 && *initp != NULL)
1445 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1446 sym->name);
1447 return false;
1450 if (init == NULL)
1452 /* An initializer is required for PARAMETER declarations. */
1453 if (attr.flavor == FL_PARAMETER)
1455 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1456 return false;
1459 else
1461 /* If a variable appears in a DATA block, it cannot have an
1462 initializer. */
1463 if (sym->attr.data)
1465 gfc_error ("Variable %qs at %C with an initializer already "
1466 "appears in a DATA statement", sym->name);
1467 return false;
1470 /* Check if the assignment can happen. This has to be put off
1471 until later for derived type variables and procedure pointers. */
1472 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1473 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1474 && !sym->attr.proc_pointer
1475 && !gfc_check_assign_symbol (sym, NULL, init))
1476 return false;
1478 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1479 && init->ts.type == BT_CHARACTER)
1481 /* Update symbol character length according initializer. */
1482 if (!gfc_check_assign_symbol (sym, NULL, init))
1483 return false;
1485 if (sym->ts.u.cl->length == NULL)
1487 int clen;
1488 /* If there are multiple CHARACTER variables declared on the
1489 same line, we don't want them to share the same length. */
1490 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1492 if (sym->attr.flavor == FL_PARAMETER)
1494 if (init->expr_type == EXPR_CONSTANT)
1496 clen = init->value.character.length;
1497 sym->ts.u.cl->length
1498 = gfc_get_int_expr (gfc_default_integer_kind,
1499 NULL, clen);
1501 else if (init->expr_type == EXPR_ARRAY)
1503 if (init->ts.u.cl)
1504 clen = mpz_get_si (init->ts.u.cl->length->value.integer);
1505 else if (init->value.constructor)
1507 gfc_constructor *c;
1508 c = gfc_constructor_first (init->value.constructor);
1509 clen = c->expr->value.character.length;
1511 else
1512 gcc_unreachable ();
1513 sym->ts.u.cl->length
1514 = gfc_get_int_expr (gfc_default_integer_kind,
1515 NULL, clen);
1517 else if (init->ts.u.cl && init->ts.u.cl->length)
1518 sym->ts.u.cl->length =
1519 gfc_copy_expr (sym->value->ts.u.cl->length);
1522 /* Update initializer character length according symbol. */
1523 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1525 int len;
1527 if (!gfc_specification_expr (sym->ts.u.cl->length))
1528 return false;
1530 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1532 if (init->expr_type == EXPR_CONSTANT)
1533 gfc_set_constant_character_len (len, init, -1);
1534 else if (init->expr_type == EXPR_ARRAY)
1536 gfc_constructor *c;
1538 /* Build a new charlen to prevent simplification from
1539 deleting the length before it is resolved. */
1540 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1541 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1543 for (c = gfc_constructor_first (init->value.constructor);
1544 c; c = gfc_constructor_next (c))
1545 gfc_set_constant_character_len (len, c->expr, -1);
1550 /* If sym is implied-shape, set its upper bounds from init. */
1551 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1552 && sym->as->type == AS_IMPLIED_SHAPE)
1554 int dim;
1556 if (init->rank == 0)
1558 gfc_error ("Can't initialize implied-shape array at %L"
1559 " with scalar", &sym->declared_at);
1560 return false;
1563 /* Shape should be present, we get an initialization expression. */
1564 gcc_assert (init->shape);
1566 for (dim = 0; dim < sym->as->rank; ++dim)
1568 int k;
1569 gfc_expr *e, *lower;
1571 lower = sym->as->lower[dim];
1573 /* If the lower bound is an array element from another
1574 parameterized array, then it is marked with EXPR_VARIABLE and
1575 is an initialization expression. Try to reduce it. */
1576 if (lower->expr_type == EXPR_VARIABLE)
1577 gfc_reduce_init_expr (lower);
1579 if (lower->expr_type == EXPR_CONSTANT)
1581 /* All dimensions must be without upper bound. */
1582 gcc_assert (!sym->as->upper[dim]);
1584 k = lower->ts.kind;
1585 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1586 mpz_add (e->value.integer, lower->value.integer,
1587 init->shape[dim]);
1588 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1589 sym->as->upper[dim] = e;
1591 else
1593 gfc_error ("Non-constant lower bound in implied-shape"
1594 " declaration at %L", &lower->where);
1595 return false;
1599 sym->as->type = AS_EXPLICIT;
1602 /* Need to check if the expression we initialized this
1603 to was one of the iso_c_binding named constants. If so,
1604 and we're a parameter (constant), let it be iso_c.
1605 For example:
1606 integer(c_int), parameter :: my_int = c_int
1607 integer(my_int) :: my_int_2
1608 If we mark my_int as iso_c (since we can see it's value
1609 is equal to one of the named constants), then my_int_2
1610 will be considered C interoperable. */
1611 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1613 sym->ts.is_iso_c |= init->ts.is_iso_c;
1614 sym->ts.is_c_interop |= init->ts.is_c_interop;
1615 /* attr bits needed for module files. */
1616 sym->attr.is_iso_c |= init->ts.is_iso_c;
1617 sym->attr.is_c_interop |= init->ts.is_c_interop;
1618 if (init->ts.is_iso_c)
1619 sym->ts.f90_type = init->ts.f90_type;
1622 /* Add initializer. Make sure we keep the ranks sane. */
1623 if (sym->attr.dimension && init->rank == 0)
1625 mpz_t size;
1626 gfc_expr *array;
1627 int n;
1628 if (sym->attr.flavor == FL_PARAMETER
1629 && init->expr_type == EXPR_CONSTANT
1630 && spec_size (sym->as, &size)
1631 && mpz_cmp_si (size, 0) > 0)
1633 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1634 &init->where);
1635 for (n = 0; n < (int)mpz_get_si (size); n++)
1636 gfc_constructor_append_expr (&array->value.constructor,
1637 n == 0
1638 ? init
1639 : gfc_copy_expr (init),
1640 &init->where);
1642 array->shape = gfc_get_shape (sym->as->rank);
1643 for (n = 0; n < sym->as->rank; n++)
1644 spec_dimen_size (sym->as, n, &array->shape[n]);
1646 init = array;
1647 mpz_clear (size);
1649 init->rank = sym->as->rank;
1652 sym->value = init;
1653 if (sym->attr.save == SAVE_NONE)
1654 sym->attr.save = SAVE_IMPLICIT;
1655 *initp = NULL;
1658 return true;
1662 /* Function called by variable_decl() that adds a name to a structure
1663 being built. */
1665 static bool
1666 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1667 gfc_array_spec **as)
1669 gfc_component *c;
1670 bool t = true;
1672 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1673 constructing, it must have the pointer attribute. */
1674 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1675 && current_ts.u.derived == gfc_current_block ()
1676 && current_attr.pointer == 0)
1678 gfc_error ("Component at %C must have the POINTER attribute");
1679 return false;
1682 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1684 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1686 gfc_error ("Array component of structure at %C must have explicit "
1687 "or deferred shape");
1688 return false;
1692 if (!gfc_add_component (gfc_current_block(), name, &c))
1693 return false;
1695 c->ts = current_ts;
1696 if (c->ts.type == BT_CHARACTER)
1697 c->ts.u.cl = cl;
1698 c->attr = current_attr;
1700 c->initializer = *init;
1701 *init = NULL;
1703 c->as = *as;
1704 if (c->as != NULL)
1706 if (c->as->corank)
1707 c->attr.codimension = 1;
1708 if (c->as->rank)
1709 c->attr.dimension = 1;
1711 *as = NULL;
1713 /* Should this ever get more complicated, combine with similar section
1714 in add_init_expr_to_sym into a separate function. */
1715 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1716 && c->ts.u.cl
1717 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1719 int len;
1721 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1722 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1723 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1725 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1727 if (c->initializer->expr_type == EXPR_CONSTANT)
1728 gfc_set_constant_character_len (len, c->initializer, -1);
1729 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1730 c->initializer->ts.u.cl->length->value.integer))
1732 gfc_constructor *ctor;
1733 ctor = gfc_constructor_first (c->initializer->value.constructor);
1735 if (ctor)
1737 int first_len;
1738 bool has_ts = (c->initializer->ts.u.cl
1739 && c->initializer->ts.u.cl->length_from_typespec);
1741 /* Remember the length of the first element for checking
1742 that all elements *in the constructor* have the same
1743 length. This need not be the length of the LHS! */
1744 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1745 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1746 first_len = ctor->expr->value.character.length;
1748 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1749 if (ctor->expr->expr_type == EXPR_CONSTANT)
1751 gfc_set_constant_character_len (len, ctor->expr,
1752 has_ts ? -1 : first_len);
1753 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1759 /* Check array components. */
1760 if (!c->attr.dimension)
1761 goto scalar;
1763 if (c->attr.pointer)
1765 if (c->as->type != AS_DEFERRED)
1767 gfc_error ("Pointer array component of structure at %C must have a "
1768 "deferred shape");
1769 t = false;
1772 else if (c->attr.allocatable)
1774 if (c->as->type != AS_DEFERRED)
1776 gfc_error ("Allocatable component of structure at %C must have a "
1777 "deferred shape");
1778 t = false;
1781 else
1783 if (c->as->type != AS_EXPLICIT)
1785 gfc_error ("Array component of structure at %C must have an "
1786 "explicit shape");
1787 t = false;
1791 scalar:
1792 if (c->ts.type == BT_CLASS)
1794 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1796 if (t)
1797 t = t2;
1800 return t;
1804 /* Match a 'NULL()', and possibly take care of some side effects. */
1806 match
1807 gfc_match_null (gfc_expr **result)
1809 gfc_symbol *sym;
1810 match m, m2 = MATCH_NO;
1812 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1813 return MATCH_ERROR;
1815 if (m == MATCH_NO)
1817 locus old_loc;
1818 char name[GFC_MAX_SYMBOL_LEN + 1];
1820 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1821 return m2;
1823 old_loc = gfc_current_locus;
1824 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1825 return MATCH_ERROR;
1826 if (m2 != MATCH_YES
1827 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1828 return MATCH_ERROR;
1829 if (m2 == MATCH_NO)
1831 gfc_current_locus = old_loc;
1832 return MATCH_NO;
1836 /* The NULL symbol now has to be/become an intrinsic function. */
1837 if (gfc_get_symbol ("null", NULL, &sym))
1839 gfc_error ("NULL() initialization at %C is ambiguous");
1840 return MATCH_ERROR;
1843 gfc_intrinsic_symbol (sym);
1845 if (sym->attr.proc != PROC_INTRINSIC
1846 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1847 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1848 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1849 return MATCH_ERROR;
1851 *result = gfc_get_null_expr (&gfc_current_locus);
1853 /* Invalid per F2008, C512. */
1854 if (m2 == MATCH_YES)
1856 gfc_error ("NULL() initialization at %C may not have MOLD");
1857 return MATCH_ERROR;
1860 return MATCH_YES;
1864 /* Match the initialization expr for a data pointer or procedure pointer. */
1866 static match
1867 match_pointer_init (gfc_expr **init, int procptr)
1869 match m;
1871 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1873 gfc_error ("Initialization of pointer at %C is not allowed in "
1874 "a PURE procedure");
1875 return MATCH_ERROR;
1877 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1879 /* Match NULL() initialization. */
1880 m = gfc_match_null (init);
1881 if (m != MATCH_NO)
1882 return m;
1884 /* Match non-NULL initialization. */
1885 gfc_matching_ptr_assignment = !procptr;
1886 gfc_matching_procptr_assignment = procptr;
1887 m = gfc_match_rvalue (init);
1888 gfc_matching_ptr_assignment = 0;
1889 gfc_matching_procptr_assignment = 0;
1890 if (m == MATCH_ERROR)
1891 return MATCH_ERROR;
1892 else if (m == MATCH_NO)
1894 gfc_error ("Error in pointer initialization at %C");
1895 return MATCH_ERROR;
1898 if (!procptr && !gfc_resolve_expr (*init))
1899 return MATCH_ERROR;
1901 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1902 "initialization at %C"))
1903 return MATCH_ERROR;
1905 return MATCH_YES;
1909 static bool
1910 check_function_name (char *name)
1912 /* In functions that have a RESULT variable defined, the function name always
1913 refers to function calls. Therefore, the name is not allowed to appear in
1914 specification statements. When checking this, be careful about
1915 'hidden' procedure pointer results ('ppr@'). */
1917 if (gfc_current_state () == COMP_FUNCTION)
1919 gfc_symbol *block = gfc_current_block ();
1920 if (block && block->result && block->result != block
1921 && strcmp (block->result->name, "ppr@") != 0
1922 && strcmp (block->name, name) == 0)
1924 gfc_error ("Function name %qs not allowed at %C", name);
1925 return false;
1929 return true;
1933 /* Match a variable name with an optional initializer. When this
1934 subroutine is called, a variable is expected to be parsed next.
1935 Depending on what is happening at the moment, updates either the
1936 symbol table or the current interface. */
1938 static match
1939 variable_decl (int elem)
1941 char name[GFC_MAX_SYMBOL_LEN + 1];
1942 gfc_expr *initializer, *char_len;
1943 gfc_array_spec *as;
1944 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1945 gfc_charlen *cl;
1946 bool cl_deferred;
1947 locus var_locus;
1948 match m;
1949 bool t;
1950 gfc_symbol *sym;
1952 initializer = NULL;
1953 as = NULL;
1954 cp_as = NULL;
1956 /* When we get here, we've just matched a list of attributes and
1957 maybe a type and a double colon. The next thing we expect to see
1958 is the name of the symbol. */
1959 m = gfc_match_name (name);
1960 if (m != MATCH_YES)
1961 goto cleanup;
1963 var_locus = gfc_current_locus;
1965 /* Now we could see the optional array spec. or character length. */
1966 m = gfc_match_array_spec (&as, true, true);
1967 if (m == MATCH_ERROR)
1968 goto cleanup;
1970 if (m == MATCH_NO)
1971 as = gfc_copy_array_spec (current_as);
1972 else if (current_as
1973 && !merge_array_spec (current_as, as, true))
1975 m = MATCH_ERROR;
1976 goto cleanup;
1979 if (flag_cray_pointer)
1980 cp_as = gfc_copy_array_spec (as);
1982 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1983 determine (and check) whether it can be implied-shape. If it
1984 was parsed as assumed-size, change it because PARAMETERs can not
1985 be assumed-size. */
1986 if (as)
1988 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1990 m = MATCH_ERROR;
1991 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1992 name, &var_locus);
1993 goto cleanup;
1996 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1997 && current_attr.flavor == FL_PARAMETER)
1998 as->type = AS_IMPLIED_SHAPE;
2000 if (as->type == AS_IMPLIED_SHAPE
2001 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2002 &var_locus))
2004 m = MATCH_ERROR;
2005 goto cleanup;
2009 char_len = NULL;
2010 cl = NULL;
2011 cl_deferred = false;
2013 if (current_ts.type == BT_CHARACTER)
2015 switch (match_char_length (&char_len, &cl_deferred, false))
2017 case MATCH_YES:
2018 cl = gfc_new_charlen (gfc_current_ns, NULL);
2020 cl->length = char_len;
2021 break;
2023 /* Non-constant lengths need to be copied after the first
2024 element. Also copy assumed lengths. */
2025 case MATCH_NO:
2026 if (elem > 1
2027 && (current_ts.u.cl->length == NULL
2028 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2030 cl = gfc_new_charlen (gfc_current_ns, NULL);
2031 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2033 else
2034 cl = current_ts.u.cl;
2036 cl_deferred = current_ts.deferred;
2038 break;
2040 case MATCH_ERROR:
2041 goto cleanup;
2045 /* The dummy arguments and result of the abreviated form of MODULE
2046 PROCEDUREs, used in SUBMODULES should not be redefined. */
2047 if (gfc_current_ns->proc_name
2048 && gfc_current_ns->proc_name->abr_modproc_decl)
2050 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2051 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2053 m = MATCH_ERROR;
2054 gfc_error ("%qs at %C is a redefinition of the declaration "
2055 "in the corresponding interface for MODULE "
2056 "PROCEDURE %qs", sym->name,
2057 gfc_current_ns->proc_name->name);
2058 goto cleanup;
2062 /* If this symbol has already shown up in a Cray Pointer declaration,
2063 and this is not a component declaration,
2064 then we want to set the type & bail out. */
2065 if (flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
2067 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2068 if (sym != NULL && sym->attr.cray_pointee)
2070 sym->ts.type = current_ts.type;
2071 sym->ts.kind = current_ts.kind;
2072 sym->ts.u.cl = cl;
2073 sym->ts.u.derived = current_ts.u.derived;
2074 sym->ts.is_c_interop = current_ts.is_c_interop;
2075 sym->ts.is_iso_c = current_ts.is_iso_c;
2076 m = MATCH_YES;
2078 /* Check to see if we have an array specification. */
2079 if (cp_as != NULL)
2081 if (sym->as != NULL)
2083 gfc_error ("Duplicate array spec for Cray pointee at %C");
2084 gfc_free_array_spec (cp_as);
2085 m = MATCH_ERROR;
2086 goto cleanup;
2088 else
2090 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2091 gfc_internal_error ("Couldn't set pointee array spec.");
2093 /* Fix the array spec. */
2094 m = gfc_mod_pointee_as (sym->as);
2095 if (m == MATCH_ERROR)
2096 goto cleanup;
2099 goto cleanup;
2101 else
2103 gfc_free_array_spec (cp_as);
2107 /* Procedure pointer as function result. */
2108 if (gfc_current_state () == COMP_FUNCTION
2109 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2110 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2111 strcpy (name, "ppr@");
2113 if (gfc_current_state () == COMP_FUNCTION
2114 && strcmp (name, gfc_current_block ()->name) == 0
2115 && gfc_current_block ()->result
2116 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2117 strcpy (name, "ppr@");
2119 /* OK, we've successfully matched the declaration. Now put the
2120 symbol in the current namespace, because it might be used in the
2121 optional initialization expression for this symbol, e.g. this is
2122 perfectly legal:
2124 integer, parameter :: i = huge(i)
2126 This is only true for parameters or variables of a basic type.
2127 For components of derived types, it is not true, so we don't
2128 create a symbol for those yet. If we fail to create the symbol,
2129 bail out. */
2130 if (gfc_current_state () != COMP_DERIVED
2131 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2133 m = MATCH_ERROR;
2134 goto cleanup;
2137 if (!check_function_name (name))
2139 m = MATCH_ERROR;
2140 goto cleanup;
2143 /* We allow old-style initializations of the form
2144 integer i /2/, j(4) /3*3, 1/
2145 (if no colon has been seen). These are different from data
2146 statements in that initializers are only allowed to apply to the
2147 variable immediately preceding, i.e.
2148 integer i, j /1, 2/
2149 is not allowed. Therefore we have to do some work manually, that
2150 could otherwise be left to the matchers for DATA statements. */
2152 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2154 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2155 "initialization at %C"))
2156 return MATCH_ERROR;
2157 else if (gfc_current_state () == COMP_DERIVED)
2159 gfc_error ("Invalid old style initialization for derived type "
2160 "component at %C");
2161 m = MATCH_ERROR;
2162 goto cleanup;
2165 return match_old_style_init (name);
2168 /* The double colon must be present in order to have initializers.
2169 Otherwise the statement is ambiguous with an assignment statement. */
2170 if (colon_seen)
2172 if (gfc_match (" =>") == MATCH_YES)
2174 if (!current_attr.pointer)
2176 gfc_error ("Initialization at %C isn't for a pointer variable");
2177 m = MATCH_ERROR;
2178 goto cleanup;
2181 m = match_pointer_init (&initializer, 0);
2182 if (m != MATCH_YES)
2183 goto cleanup;
2185 else if (gfc_match_char ('=') == MATCH_YES)
2187 if (current_attr.pointer)
2189 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2190 "not %<=%>");
2191 m = MATCH_ERROR;
2192 goto cleanup;
2195 m = gfc_match_init_expr (&initializer);
2196 if (m == MATCH_NO)
2198 gfc_error ("Expected an initialization expression at %C");
2199 m = MATCH_ERROR;
2202 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2203 && gfc_state_stack->state != COMP_DERIVED)
2205 gfc_error ("Initialization of variable at %C is not allowed in "
2206 "a PURE procedure");
2207 m = MATCH_ERROR;
2210 if (current_attr.flavor != FL_PARAMETER
2211 && gfc_state_stack->state != COMP_DERIVED)
2212 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2214 if (m != MATCH_YES)
2215 goto cleanup;
2219 if (initializer != NULL && current_attr.allocatable
2220 && gfc_current_state () == COMP_DERIVED)
2222 gfc_error ("Initialization of allocatable component at %C is not "
2223 "allowed");
2224 m = MATCH_ERROR;
2225 goto cleanup;
2228 /* Add the initializer. Note that it is fine if initializer is
2229 NULL here, because we sometimes also need to check if a
2230 declaration *must* have an initialization expression. */
2231 if (gfc_current_state () != COMP_DERIVED)
2232 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2233 else
2235 if (current_ts.type == BT_DERIVED
2236 && !current_attr.pointer && !initializer)
2237 initializer = gfc_default_initializer (&current_ts);
2238 t = build_struct (name, cl, &initializer, &as);
2241 m = (t) ? MATCH_YES : MATCH_ERROR;
2243 cleanup:
2244 /* Free stuff up and return. */
2245 gfc_free_expr (initializer);
2246 gfc_free_array_spec (as);
2248 return m;
2252 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2253 This assumes that the byte size is equal to the kind number for
2254 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2256 match
2257 gfc_match_old_kind_spec (gfc_typespec *ts)
2259 match m;
2260 int original_kind;
2262 if (gfc_match_char ('*') != MATCH_YES)
2263 return MATCH_NO;
2265 m = gfc_match_small_literal_int (&ts->kind, NULL);
2266 if (m != MATCH_YES)
2267 return MATCH_ERROR;
2269 original_kind = ts->kind;
2271 /* Massage the kind numbers for complex types. */
2272 if (ts->type == BT_COMPLEX)
2274 if (ts->kind % 2)
2276 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2277 gfc_basic_typename (ts->type), original_kind);
2278 return MATCH_ERROR;
2280 ts->kind /= 2;
2284 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2285 ts->kind = 8;
2287 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2289 if (ts->kind == 4)
2291 if (flag_real4_kind == 8)
2292 ts->kind = 8;
2293 if (flag_real4_kind == 10)
2294 ts->kind = 10;
2295 if (flag_real4_kind == 16)
2296 ts->kind = 16;
2299 if (ts->kind == 8)
2301 if (flag_real8_kind == 4)
2302 ts->kind = 4;
2303 if (flag_real8_kind == 10)
2304 ts->kind = 10;
2305 if (flag_real8_kind == 16)
2306 ts->kind = 16;
2310 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2312 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2313 gfc_basic_typename (ts->type), original_kind);
2314 return MATCH_ERROR;
2317 if (!gfc_notify_std (GFC_STD_GNU,
2318 "Nonstandard type declaration %s*%d at %C",
2319 gfc_basic_typename(ts->type), original_kind))
2320 return MATCH_ERROR;
2322 return MATCH_YES;
2326 /* Match a kind specification. Since kinds are generally optional, we
2327 usually return MATCH_NO if something goes wrong. If a "kind="
2328 string is found, then we know we have an error. */
2330 match
2331 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2333 locus where, loc;
2334 gfc_expr *e;
2335 match m, n;
2336 char c;
2337 const char *msg;
2339 m = MATCH_NO;
2340 n = MATCH_YES;
2341 e = NULL;
2343 where = loc = gfc_current_locus;
2345 if (kind_expr_only)
2346 goto kind_expr;
2348 if (gfc_match_char ('(') == MATCH_NO)
2349 return MATCH_NO;
2351 /* Also gobbles optional text. */
2352 if (gfc_match (" kind = ") == MATCH_YES)
2353 m = MATCH_ERROR;
2355 loc = gfc_current_locus;
2357 kind_expr:
2358 n = gfc_match_init_expr (&e);
2360 if (n != MATCH_YES)
2362 if (gfc_matching_function)
2364 /* The function kind expression might include use associated or
2365 imported parameters and try again after the specification
2366 expressions..... */
2367 if (gfc_match_char (')') != MATCH_YES)
2369 gfc_error ("Missing right parenthesis at %C");
2370 m = MATCH_ERROR;
2371 goto no_match;
2374 gfc_free_expr (e);
2375 gfc_undo_symbols ();
2376 return MATCH_YES;
2378 else
2380 /* ....or else, the match is real. */
2381 if (n == MATCH_NO)
2382 gfc_error ("Expected initialization expression at %C");
2383 if (n != MATCH_YES)
2384 return MATCH_ERROR;
2388 if (e->rank != 0)
2390 gfc_error ("Expected scalar initialization expression at %C");
2391 m = MATCH_ERROR;
2392 goto no_match;
2395 msg = gfc_extract_int (e, &ts->kind);
2397 if (msg != NULL)
2399 gfc_error (msg);
2400 m = MATCH_ERROR;
2401 goto no_match;
2404 /* Before throwing away the expression, let's see if we had a
2405 C interoperable kind (and store the fact). */
2406 if (e->ts.is_c_interop == 1)
2408 /* Mark this as C interoperable if being declared with one
2409 of the named constants from iso_c_binding. */
2410 ts->is_c_interop = e->ts.is_iso_c;
2411 ts->f90_type = e->ts.f90_type;
2414 gfc_free_expr (e);
2415 e = NULL;
2417 /* Ignore errors to this point, if we've gotten here. This means
2418 we ignore the m=MATCH_ERROR from above. */
2419 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2421 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2422 gfc_basic_typename (ts->type));
2423 gfc_current_locus = where;
2424 return MATCH_ERROR;
2427 /* Warn if, e.g., c_int is used for a REAL variable, but not
2428 if, e.g., c_double is used for COMPLEX as the standard
2429 explicitly says that the kind type parameter for complex and real
2430 variable is the same, i.e. c_float == c_float_complex. */
2431 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2432 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2433 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2434 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2435 "is %s", gfc_basic_typename (ts->f90_type), &where,
2436 gfc_basic_typename (ts->type));
2438 gfc_gobble_whitespace ();
2439 if ((c = gfc_next_ascii_char ()) != ')'
2440 && (ts->type != BT_CHARACTER || c != ','))
2442 if (ts->type == BT_CHARACTER)
2443 gfc_error ("Missing right parenthesis or comma at %C");
2444 else
2445 gfc_error ("Missing right parenthesis at %C");
2446 m = MATCH_ERROR;
2448 else
2449 /* All tests passed. */
2450 m = MATCH_YES;
2452 if(m == MATCH_ERROR)
2453 gfc_current_locus = where;
2455 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2456 ts->kind = 8;
2458 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2460 if (ts->kind == 4)
2462 if (flag_real4_kind == 8)
2463 ts->kind = 8;
2464 if (flag_real4_kind == 10)
2465 ts->kind = 10;
2466 if (flag_real4_kind == 16)
2467 ts->kind = 16;
2470 if (ts->kind == 8)
2472 if (flag_real8_kind == 4)
2473 ts->kind = 4;
2474 if (flag_real8_kind == 10)
2475 ts->kind = 10;
2476 if (flag_real8_kind == 16)
2477 ts->kind = 16;
2481 /* Return what we know from the test(s). */
2482 return m;
2484 no_match:
2485 gfc_free_expr (e);
2486 gfc_current_locus = where;
2487 return m;
2491 static match
2492 match_char_kind (int * kind, int * is_iso_c)
2494 locus where;
2495 gfc_expr *e;
2496 match m, n;
2497 const char *msg;
2499 m = MATCH_NO;
2500 e = NULL;
2501 where = gfc_current_locus;
2503 n = gfc_match_init_expr (&e);
2505 if (n != MATCH_YES && gfc_matching_function)
2507 /* The expression might include use-associated or imported
2508 parameters and try again after the specification
2509 expressions. */
2510 gfc_free_expr (e);
2511 gfc_undo_symbols ();
2512 return MATCH_YES;
2515 if (n == MATCH_NO)
2516 gfc_error ("Expected initialization expression at %C");
2517 if (n != MATCH_YES)
2518 return MATCH_ERROR;
2520 if (e->rank != 0)
2522 gfc_error ("Expected scalar initialization expression at %C");
2523 m = MATCH_ERROR;
2524 goto no_match;
2527 msg = gfc_extract_int (e, kind);
2528 *is_iso_c = e->ts.is_iso_c;
2529 if (msg != NULL)
2531 gfc_error (msg);
2532 m = MATCH_ERROR;
2533 goto no_match;
2536 gfc_free_expr (e);
2538 /* Ignore errors to this point, if we've gotten here. This means
2539 we ignore the m=MATCH_ERROR from above. */
2540 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2542 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2543 m = MATCH_ERROR;
2545 else
2546 /* All tests passed. */
2547 m = MATCH_YES;
2549 if (m == MATCH_ERROR)
2550 gfc_current_locus = where;
2552 /* Return what we know from the test(s). */
2553 return m;
2555 no_match:
2556 gfc_free_expr (e);
2557 gfc_current_locus = where;
2558 return m;
2562 /* Match the various kind/length specifications in a CHARACTER
2563 declaration. We don't return MATCH_NO. */
2565 match
2566 gfc_match_char_spec (gfc_typespec *ts)
2568 int kind, seen_length, is_iso_c;
2569 gfc_charlen *cl;
2570 gfc_expr *len;
2571 match m;
2572 bool deferred;
2574 len = NULL;
2575 seen_length = 0;
2576 kind = 0;
2577 is_iso_c = 0;
2578 deferred = false;
2580 /* Try the old-style specification first. */
2581 old_char_selector = 0;
2583 m = match_char_length (&len, &deferred, true);
2584 if (m != MATCH_NO)
2586 if (m == MATCH_YES)
2587 old_char_selector = 1;
2588 seen_length = 1;
2589 goto done;
2592 m = gfc_match_char ('(');
2593 if (m != MATCH_YES)
2595 m = MATCH_YES; /* Character without length is a single char. */
2596 goto done;
2599 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2600 if (gfc_match (" kind =") == MATCH_YES)
2602 m = match_char_kind (&kind, &is_iso_c);
2604 if (m == MATCH_ERROR)
2605 goto done;
2606 if (m == MATCH_NO)
2607 goto syntax;
2609 if (gfc_match (" , len =") == MATCH_NO)
2610 goto rparen;
2612 m = char_len_param_value (&len, &deferred);
2613 if (m == MATCH_NO)
2614 goto syntax;
2615 if (m == MATCH_ERROR)
2616 goto done;
2617 seen_length = 1;
2619 goto rparen;
2622 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2623 if (gfc_match (" len =") == MATCH_YES)
2625 m = char_len_param_value (&len, &deferred);
2626 if (m == MATCH_NO)
2627 goto syntax;
2628 if (m == MATCH_ERROR)
2629 goto done;
2630 seen_length = 1;
2632 if (gfc_match_char (')') == MATCH_YES)
2633 goto done;
2635 if (gfc_match (" , kind =") != MATCH_YES)
2636 goto syntax;
2638 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2639 goto done;
2641 goto rparen;
2644 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2645 m = char_len_param_value (&len, &deferred);
2646 if (m == MATCH_NO)
2647 goto syntax;
2648 if (m == MATCH_ERROR)
2649 goto done;
2650 seen_length = 1;
2652 m = gfc_match_char (')');
2653 if (m == MATCH_YES)
2654 goto done;
2656 if (gfc_match_char (',') != MATCH_YES)
2657 goto syntax;
2659 gfc_match (" kind ="); /* Gobble optional text. */
2661 m = match_char_kind (&kind, &is_iso_c);
2662 if (m == MATCH_ERROR)
2663 goto done;
2664 if (m == MATCH_NO)
2665 goto syntax;
2667 rparen:
2668 /* Require a right-paren at this point. */
2669 m = gfc_match_char (')');
2670 if (m == MATCH_YES)
2671 goto done;
2673 syntax:
2674 gfc_error ("Syntax error in CHARACTER declaration at %C");
2675 m = MATCH_ERROR;
2676 gfc_free_expr (len);
2677 return m;
2679 done:
2680 /* Deal with character functions after USE and IMPORT statements. */
2681 if (gfc_matching_function)
2683 gfc_free_expr (len);
2684 gfc_undo_symbols ();
2685 return MATCH_YES;
2688 if (m != MATCH_YES)
2690 gfc_free_expr (len);
2691 return m;
2694 /* Do some final massaging of the length values. */
2695 cl = gfc_new_charlen (gfc_current_ns, NULL);
2697 if (seen_length == 0)
2698 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2699 else
2700 cl->length = len;
2702 ts->u.cl = cl;
2703 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2704 ts->deferred = deferred;
2706 /* We have to know if it was a C interoperable kind so we can
2707 do accurate type checking of bind(c) procs, etc. */
2708 if (kind != 0)
2709 /* Mark this as C interoperable if being declared with one
2710 of the named constants from iso_c_binding. */
2711 ts->is_c_interop = is_iso_c;
2712 else if (len != NULL)
2713 /* Here, we might have parsed something such as: character(c_char)
2714 In this case, the parsing code above grabs the c_char when
2715 looking for the length (line 1690, roughly). it's the last
2716 testcase for parsing the kind params of a character variable.
2717 However, it's not actually the length. this seems like it
2718 could be an error.
2719 To see if the user used a C interop kind, test the expr
2720 of the so called length, and see if it's C interoperable. */
2721 ts->is_c_interop = len->ts.is_iso_c;
2723 return MATCH_YES;
2727 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2728 structure to the matched specification. This is necessary for FUNCTION and
2729 IMPLICIT statements.
2731 If implicit_flag is nonzero, then we don't check for the optional
2732 kind specification. Not doing so is needed for matching an IMPLICIT
2733 statement correctly. */
2735 match
2736 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2738 char name[GFC_MAX_SYMBOL_LEN + 1];
2739 gfc_symbol *sym, *dt_sym;
2740 match m;
2741 char c;
2742 bool seen_deferred_kind, matched_type;
2743 const char *dt_name;
2745 /* A belt and braces check that the typespec is correctly being treated
2746 as a deferred characteristic association. */
2747 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2748 && (gfc_current_block ()->result->ts.kind == -1)
2749 && (ts->kind == -1);
2750 gfc_clear_ts (ts);
2751 if (seen_deferred_kind)
2752 ts->kind = -1;
2754 /* Clear the current binding label, in case one is given. */
2755 curr_binding_label = NULL;
2757 if (gfc_match (" byte") == MATCH_YES)
2759 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2760 return MATCH_ERROR;
2762 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2764 gfc_error ("BYTE type used at %C "
2765 "is not available on the target machine");
2766 return MATCH_ERROR;
2769 ts->type = BT_INTEGER;
2770 ts->kind = 1;
2771 return MATCH_YES;
2775 m = gfc_match (" type (");
2776 matched_type = (m == MATCH_YES);
2777 if (matched_type)
2779 gfc_gobble_whitespace ();
2780 if (gfc_peek_ascii_char () == '*')
2782 if ((m = gfc_match ("*)")) != MATCH_YES)
2783 return m;
2784 if (gfc_current_state () == COMP_DERIVED)
2786 gfc_error ("Assumed type at %C is not allowed for components");
2787 return MATCH_ERROR;
2789 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2790 "at %C"))
2791 return MATCH_ERROR;
2792 ts->type = BT_ASSUMED;
2793 return MATCH_YES;
2796 m = gfc_match ("%n", name);
2797 matched_type = (m == MATCH_YES);
2800 if ((matched_type && strcmp ("integer", name) == 0)
2801 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2803 ts->type = BT_INTEGER;
2804 ts->kind = gfc_default_integer_kind;
2805 goto get_kind;
2808 if ((matched_type && strcmp ("character", name) == 0)
2809 || (!matched_type && gfc_match (" character") == MATCH_YES))
2811 if (matched_type
2812 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2813 "intrinsic-type-spec at %C"))
2814 return MATCH_ERROR;
2816 ts->type = BT_CHARACTER;
2817 if (implicit_flag == 0)
2818 m = gfc_match_char_spec (ts);
2819 else
2820 m = MATCH_YES;
2822 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2823 m = MATCH_ERROR;
2825 return m;
2828 if ((matched_type && strcmp ("real", name) == 0)
2829 || (!matched_type && gfc_match (" real") == MATCH_YES))
2831 ts->type = BT_REAL;
2832 ts->kind = gfc_default_real_kind;
2833 goto get_kind;
2836 if ((matched_type
2837 && (strcmp ("doubleprecision", name) == 0
2838 || (strcmp ("double", name) == 0
2839 && gfc_match (" precision") == MATCH_YES)))
2840 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2842 if (matched_type
2843 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2844 "intrinsic-type-spec at %C"))
2845 return MATCH_ERROR;
2846 if (matched_type && gfc_match_char (')') != MATCH_YES)
2847 return MATCH_ERROR;
2849 ts->type = BT_REAL;
2850 ts->kind = gfc_default_double_kind;
2851 return MATCH_YES;
2854 if ((matched_type && strcmp ("complex", name) == 0)
2855 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2857 ts->type = BT_COMPLEX;
2858 ts->kind = gfc_default_complex_kind;
2859 goto get_kind;
2862 if ((matched_type
2863 && (strcmp ("doublecomplex", name) == 0
2864 || (strcmp ("double", name) == 0
2865 && gfc_match (" complex") == MATCH_YES)))
2866 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2868 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2869 return MATCH_ERROR;
2871 if (matched_type
2872 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2873 "intrinsic-type-spec at %C"))
2874 return MATCH_ERROR;
2876 if (matched_type && gfc_match_char (')') != MATCH_YES)
2877 return MATCH_ERROR;
2879 ts->type = BT_COMPLEX;
2880 ts->kind = gfc_default_double_kind;
2881 return MATCH_YES;
2884 if ((matched_type && strcmp ("logical", name) == 0)
2885 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2887 ts->type = BT_LOGICAL;
2888 ts->kind = gfc_default_logical_kind;
2889 goto get_kind;
2892 if (matched_type)
2893 m = gfc_match_char (')');
2895 if (m == MATCH_YES)
2896 ts->type = BT_DERIVED;
2897 else
2899 /* Match CLASS declarations. */
2900 m = gfc_match (" class ( * )");
2901 if (m == MATCH_ERROR)
2902 return MATCH_ERROR;
2903 else if (m == MATCH_YES)
2905 gfc_symbol *upe;
2906 gfc_symtree *st;
2907 ts->type = BT_CLASS;
2908 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2909 if (upe == NULL)
2911 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2912 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2913 st->n.sym = upe;
2914 gfc_set_sym_referenced (upe);
2915 upe->refs++;
2916 upe->ts.type = BT_VOID;
2917 upe->attr.unlimited_polymorphic = 1;
2918 /* This is essential to force the construction of
2919 unlimited polymorphic component class containers. */
2920 upe->attr.zero_comp = 1;
2921 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2922 &gfc_current_locus))
2923 return MATCH_ERROR;
2925 else
2927 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2928 if (st == NULL)
2929 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2930 st->n.sym = upe;
2931 upe->refs++;
2933 ts->u.derived = upe;
2934 return m;
2937 m = gfc_match (" class ( %n )", name);
2938 if (m != MATCH_YES)
2939 return m;
2940 ts->type = BT_CLASS;
2942 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2943 return MATCH_ERROR;
2946 /* Defer association of the derived type until the end of the
2947 specification block. However, if the derived type can be
2948 found, add it to the typespec. */
2949 if (gfc_matching_function)
2951 ts->u.derived = NULL;
2952 if (gfc_current_state () != COMP_INTERFACE
2953 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2955 sym = gfc_find_dt_in_generic (sym);
2956 ts->u.derived = sym;
2958 return MATCH_YES;
2961 /* Search for the name but allow the components to be defined later. If
2962 type = -1, this typespec has been seen in a function declaration but
2963 the type could not be accessed at that point. The actual derived type is
2964 stored in a symtree with the first letter of the name capitalized; the
2965 symtree with the all lower-case name contains the associated
2966 generic function. */
2967 dt_name = gfc_get_string ("%c%s",
2968 (char) TOUPPER ((unsigned char) name[0]),
2969 (const char*)&name[1]);
2970 sym = NULL;
2971 dt_sym = NULL;
2972 if (ts->kind != -1)
2974 gfc_get_ha_symbol (name, &sym);
2975 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2977 gfc_error ("Type name %qs at %C is ambiguous", name);
2978 return MATCH_ERROR;
2980 if (sym->generic && !dt_sym)
2981 dt_sym = gfc_find_dt_in_generic (sym);
2983 else if (ts->kind == -1)
2985 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2986 || gfc_current_ns->has_import_set;
2987 gfc_find_symbol (name, NULL, iface, &sym);
2988 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2990 gfc_error ("Type name %qs at %C is ambiguous", name);
2991 return MATCH_ERROR;
2993 if (sym && sym->generic && !dt_sym)
2994 dt_sym = gfc_find_dt_in_generic (sym);
2996 ts->kind = 0;
2997 if (sym == NULL)
2998 return MATCH_NO;
3001 if ((sym->attr.flavor != FL_UNKNOWN
3002 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
3003 || sym->attr.subroutine)
3005 gfc_error ("Type name %qs at %C conflicts with previously declared "
3006 "entity at %L, which has the same name", name,
3007 &sym->declared_at);
3008 return MATCH_ERROR;
3011 gfc_save_symbol_data (sym);
3012 gfc_set_sym_referenced (sym);
3013 if (!sym->attr.generic
3014 && !gfc_add_generic (&sym->attr, sym->name, NULL))
3015 return MATCH_ERROR;
3017 if (!sym->attr.function
3018 && !gfc_add_function (&sym->attr, sym->name, NULL))
3019 return MATCH_ERROR;
3021 if (!dt_sym)
3023 gfc_interface *intr, *head;
3025 /* Use upper case to save the actual derived-type symbol. */
3026 gfc_get_symbol (dt_name, NULL, &dt_sym);
3027 dt_sym->name = gfc_get_string (sym->name);
3028 head = sym->generic;
3029 intr = gfc_get_interface ();
3030 intr->sym = dt_sym;
3031 intr->where = gfc_current_locus;
3032 intr->next = head;
3033 sym->generic = intr;
3034 sym->attr.if_source = IFSRC_DECL;
3036 else
3037 gfc_save_symbol_data (dt_sym);
3039 gfc_set_sym_referenced (dt_sym);
3041 if (dt_sym->attr.flavor != FL_DERIVED
3042 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
3043 return MATCH_ERROR;
3045 ts->u.derived = dt_sym;
3047 return MATCH_YES;
3049 get_kind:
3050 if (matched_type
3051 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3052 "intrinsic-type-spec at %C"))
3053 return MATCH_ERROR;
3055 /* For all types except double, derived and character, look for an
3056 optional kind specifier. MATCH_NO is actually OK at this point. */
3057 if (implicit_flag == 1)
3059 if (matched_type && gfc_match_char (')') != MATCH_YES)
3060 return MATCH_ERROR;
3062 return MATCH_YES;
3065 if (gfc_current_form == FORM_FREE)
3067 c = gfc_peek_ascii_char ();
3068 if (!gfc_is_whitespace (c) && c != '*' && c != '('
3069 && c != ':' && c != ',')
3071 if (matched_type && c == ')')
3073 gfc_next_ascii_char ();
3074 return MATCH_YES;
3076 return MATCH_NO;
3080 m = gfc_match_kind_spec (ts, false);
3081 if (m == MATCH_NO && ts->type != BT_CHARACTER)
3083 m = gfc_match_old_kind_spec (ts);
3084 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
3085 return MATCH_ERROR;
3088 if (matched_type && gfc_match_char (')') != MATCH_YES)
3089 return MATCH_ERROR;
3091 /* Defer association of the KIND expression of function results
3092 until after USE and IMPORT statements. */
3093 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
3094 || gfc_matching_function)
3095 return MATCH_YES;
3097 if (m == MATCH_NO)
3098 m = MATCH_YES; /* No kind specifier found. */
3100 return m;
3104 /* Match an IMPLICIT NONE statement. Actually, this statement is
3105 already matched in parse.c, or we would not end up here in the
3106 first place. So the only thing we need to check, is if there is
3107 trailing garbage. If not, the match is successful. */
3109 match
3110 gfc_match_implicit_none (void)
3112 char c;
3113 match m;
3114 char name[GFC_MAX_SYMBOL_LEN + 1];
3115 bool type = false;
3116 bool external = false;
3117 locus cur_loc = gfc_current_locus;
3119 if (gfc_current_ns->seen_implicit_none
3120 || gfc_current_ns->has_implicit_none_export)
3122 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3123 return MATCH_ERROR;
3126 gfc_gobble_whitespace ();
3127 c = gfc_peek_ascii_char ();
3128 if (c == '(')
3130 (void) gfc_next_ascii_char ();
3131 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3132 return MATCH_ERROR;
3134 gfc_gobble_whitespace ();
3135 if (gfc_peek_ascii_char () == ')')
3137 (void) gfc_next_ascii_char ();
3138 type = true;
3140 else
3141 for(;;)
3143 m = gfc_match (" %n", name);
3144 if (m != MATCH_YES)
3145 return MATCH_ERROR;
3147 if (strcmp (name, "type") == 0)
3148 type = true;
3149 else if (strcmp (name, "external") == 0)
3150 external = true;
3151 else
3152 return MATCH_ERROR;
3154 gfc_gobble_whitespace ();
3155 c = gfc_next_ascii_char ();
3156 if (c == ',')
3157 continue;
3158 if (c == ')')
3159 break;
3160 return MATCH_ERROR;
3163 else
3164 type = true;
3166 if (gfc_match_eos () != MATCH_YES)
3167 return MATCH_ERROR;
3169 gfc_set_implicit_none (type, external, &cur_loc);
3171 return MATCH_YES;
3175 /* Match the letter range(s) of an IMPLICIT statement. */
3177 static match
3178 match_implicit_range (void)
3180 char c, c1, c2;
3181 int inner;
3182 locus cur_loc;
3184 cur_loc = gfc_current_locus;
3186 gfc_gobble_whitespace ();
3187 c = gfc_next_ascii_char ();
3188 if (c != '(')
3190 gfc_error ("Missing character range in IMPLICIT at %C");
3191 goto bad;
3194 inner = 1;
3195 while (inner)
3197 gfc_gobble_whitespace ();
3198 c1 = gfc_next_ascii_char ();
3199 if (!ISALPHA (c1))
3200 goto bad;
3202 gfc_gobble_whitespace ();
3203 c = gfc_next_ascii_char ();
3205 switch (c)
3207 case ')':
3208 inner = 0; /* Fall through. */
3210 case ',':
3211 c2 = c1;
3212 break;
3214 case '-':
3215 gfc_gobble_whitespace ();
3216 c2 = gfc_next_ascii_char ();
3217 if (!ISALPHA (c2))
3218 goto bad;
3220 gfc_gobble_whitespace ();
3221 c = gfc_next_ascii_char ();
3223 if ((c != ',') && (c != ')'))
3224 goto bad;
3225 if (c == ')')
3226 inner = 0;
3228 break;
3230 default:
3231 goto bad;
3234 if (c1 > c2)
3236 gfc_error ("Letters must be in alphabetic order in "
3237 "IMPLICIT statement at %C");
3238 goto bad;
3241 /* See if we can add the newly matched range to the pending
3242 implicits from this IMPLICIT statement. We do not check for
3243 conflicts with whatever earlier IMPLICIT statements may have
3244 set. This is done when we've successfully finished matching
3245 the current one. */
3246 if (!gfc_add_new_implicit_range (c1, c2))
3247 goto bad;
3250 return MATCH_YES;
3252 bad:
3253 gfc_syntax_error (ST_IMPLICIT);
3255 gfc_current_locus = cur_loc;
3256 return MATCH_ERROR;
3260 /* Match an IMPLICIT statement, storing the types for
3261 gfc_set_implicit() if the statement is accepted by the parser.
3262 There is a strange looking, but legal syntactic construction
3263 possible. It looks like:
3265 IMPLICIT INTEGER (a-b) (c-d)
3267 This is legal if "a-b" is a constant expression that happens to
3268 equal one of the legal kinds for integers. The real problem
3269 happens with an implicit specification that looks like:
3271 IMPLICIT INTEGER (a-b)
3273 In this case, a typespec matcher that is "greedy" (as most of the
3274 matchers are) gobbles the character range as a kindspec, leaving
3275 nothing left. We therefore have to go a bit more slowly in the
3276 matching process by inhibiting the kindspec checking during
3277 typespec matching and checking for a kind later. */
3279 match
3280 gfc_match_implicit (void)
3282 gfc_typespec ts;
3283 locus cur_loc;
3284 char c;
3285 match m;
3287 if (gfc_current_ns->seen_implicit_none)
3289 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3290 "statement");
3291 return MATCH_ERROR;
3294 gfc_clear_ts (&ts);
3296 /* We don't allow empty implicit statements. */
3297 if (gfc_match_eos () == MATCH_YES)
3299 gfc_error ("Empty IMPLICIT statement at %C");
3300 return MATCH_ERROR;
3305 /* First cleanup. */
3306 gfc_clear_new_implicit ();
3308 /* A basic type is mandatory here. */
3309 m = gfc_match_decl_type_spec (&ts, 1);
3310 if (m == MATCH_ERROR)
3311 goto error;
3312 if (m == MATCH_NO)
3313 goto syntax;
3315 cur_loc = gfc_current_locus;
3316 m = match_implicit_range ();
3318 if (m == MATCH_YES)
3320 /* We may have <TYPE> (<RANGE>). */
3321 gfc_gobble_whitespace ();
3322 c = gfc_peek_ascii_char ();
3323 if (c == ',' || c == '\n' || c == ';' || c == '!')
3325 /* Check for CHARACTER with no length parameter. */
3326 if (ts.type == BT_CHARACTER && !ts.u.cl)
3328 ts.kind = gfc_default_character_kind;
3329 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3330 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3331 NULL, 1);
3334 /* Record the Successful match. */
3335 if (!gfc_merge_new_implicit (&ts))
3336 return MATCH_ERROR;
3337 if (c == ',')
3338 c = gfc_next_ascii_char ();
3339 else if (gfc_match_eos () == MATCH_ERROR)
3340 goto error;
3341 continue;
3344 gfc_current_locus = cur_loc;
3347 /* Discard the (incorrectly) matched range. */
3348 gfc_clear_new_implicit ();
3350 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3351 if (ts.type == BT_CHARACTER)
3352 m = gfc_match_char_spec (&ts);
3353 else
3355 m = gfc_match_kind_spec (&ts, false);
3356 if (m == MATCH_NO)
3358 m = gfc_match_old_kind_spec (&ts);
3359 if (m == MATCH_ERROR)
3360 goto error;
3361 if (m == MATCH_NO)
3362 goto syntax;
3365 if (m == MATCH_ERROR)
3366 goto error;
3368 m = match_implicit_range ();
3369 if (m == MATCH_ERROR)
3370 goto error;
3371 if (m == MATCH_NO)
3372 goto syntax;
3374 gfc_gobble_whitespace ();
3375 c = gfc_next_ascii_char ();
3376 if (c != ',' && gfc_match_eos () != MATCH_YES)
3377 goto syntax;
3379 if (!gfc_merge_new_implicit (&ts))
3380 return MATCH_ERROR;
3382 while (c == ',');
3384 return MATCH_YES;
3386 syntax:
3387 gfc_syntax_error (ST_IMPLICIT);
3389 error:
3390 return MATCH_ERROR;
3394 match
3395 gfc_match_import (void)
3397 char name[GFC_MAX_SYMBOL_LEN + 1];
3398 match m;
3399 gfc_symbol *sym;
3400 gfc_symtree *st;
3402 if (gfc_current_ns->proc_name == NULL
3403 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3405 gfc_error ("IMPORT statement at %C only permitted in "
3406 "an INTERFACE body");
3407 return MATCH_ERROR;
3410 if (gfc_current_ns->proc_name->attr.module_procedure)
3412 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3413 "in a module procedure interface body");
3414 return MATCH_ERROR;
3417 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3418 return MATCH_ERROR;
3420 if (gfc_match_eos () == MATCH_YES)
3422 /* All host variables should be imported. */
3423 gfc_current_ns->has_import_set = 1;
3424 return MATCH_YES;
3427 if (gfc_match (" ::") == MATCH_YES)
3429 if (gfc_match_eos () == MATCH_YES)
3431 gfc_error ("Expecting list of named entities at %C");
3432 return MATCH_ERROR;
3436 for(;;)
3438 sym = NULL;
3439 m = gfc_match (" %n", name);
3440 switch (m)
3442 case MATCH_YES:
3443 if (gfc_current_ns->parent != NULL
3444 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3446 gfc_error ("Type name %qs at %C is ambiguous", name);
3447 return MATCH_ERROR;
3449 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3450 && gfc_find_symbol (name,
3451 gfc_current_ns->proc_name->ns->parent,
3452 1, &sym))
3454 gfc_error ("Type name %qs at %C is ambiguous", name);
3455 return MATCH_ERROR;
3458 if (sym == NULL)
3460 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3461 "at %C - does not exist.", name);
3462 return MATCH_ERROR;
3465 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3467 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3468 "at %C", name);
3469 goto next_item;
3472 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3473 st->n.sym = sym;
3474 sym->refs++;
3475 sym->attr.imported = 1;
3477 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3479 /* The actual derived type is stored in a symtree with the first
3480 letter of the name capitalized; the symtree with the all
3481 lower-case name contains the associated generic function. */
3482 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3483 gfc_get_string ("%c%s",
3484 (char) TOUPPER ((unsigned char) name[0]),
3485 &name[1]));
3486 st->n.sym = sym;
3487 sym->refs++;
3488 sym->attr.imported = 1;
3491 goto next_item;
3493 case MATCH_NO:
3494 break;
3496 case MATCH_ERROR:
3497 return MATCH_ERROR;
3500 next_item:
3501 if (gfc_match_eos () == MATCH_YES)
3502 break;
3503 if (gfc_match_char (',') != MATCH_YES)
3504 goto syntax;
3507 return MATCH_YES;
3509 syntax:
3510 gfc_error ("Syntax error in IMPORT statement at %C");
3511 return MATCH_ERROR;
3515 /* A minimal implementation of gfc_match without whitespace, escape
3516 characters or variable arguments. Returns true if the next
3517 characters match the TARGET template exactly. */
3519 static bool
3520 match_string_p (const char *target)
3522 const char *p;
3524 for (p = target; *p; p++)
3525 if ((char) gfc_next_ascii_char () != *p)
3526 return false;
3527 return true;
3530 /* Matches an attribute specification including array specs. If
3531 successful, leaves the variables current_attr and current_as
3532 holding the specification. Also sets the colon_seen variable for
3533 later use by matchers associated with initializations.
3535 This subroutine is a little tricky in the sense that we don't know
3536 if we really have an attr-spec until we hit the double colon.
3537 Until that time, we can only return MATCH_NO. This forces us to
3538 check for duplicate specification at this level. */
3540 static match
3541 match_attr_spec (void)
3543 /* Modifiers that can exist in a type statement. */
3544 enum
3545 { GFC_DECL_BEGIN = 0,
3546 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3547 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3548 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3549 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3550 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3551 DECL_NONE, GFC_DECL_END /* Sentinel */
3554 /* GFC_DECL_END is the sentinel, index starts at 0. */
3555 #define NUM_DECL GFC_DECL_END
3557 locus start, seen_at[NUM_DECL];
3558 int seen[NUM_DECL];
3559 unsigned int d;
3560 const char *attr;
3561 match m;
3562 bool t;
3564 gfc_clear_attr (&current_attr);
3565 start = gfc_current_locus;
3567 current_as = NULL;
3568 colon_seen = 0;
3570 /* See if we get all of the keywords up to the final double colon. */
3571 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3572 seen[d] = 0;
3574 for (;;)
3576 char ch;
3578 d = DECL_NONE;
3579 gfc_gobble_whitespace ();
3581 ch = gfc_next_ascii_char ();
3582 if (ch == ':')
3584 /* This is the successful exit condition for the loop. */
3585 if (gfc_next_ascii_char () == ':')
3586 break;
3588 else if (ch == ',')
3590 gfc_gobble_whitespace ();
3591 switch (gfc_peek_ascii_char ())
3593 case 'a':
3594 gfc_next_ascii_char ();
3595 switch (gfc_next_ascii_char ())
3597 case 'l':
3598 if (match_string_p ("locatable"))
3600 /* Matched "allocatable". */
3601 d = DECL_ALLOCATABLE;
3603 break;
3605 case 's':
3606 if (match_string_p ("ynchronous"))
3608 /* Matched "asynchronous". */
3609 d = DECL_ASYNCHRONOUS;
3611 break;
3613 break;
3615 case 'b':
3616 /* Try and match the bind(c). */
3617 m = gfc_match_bind_c (NULL, true);
3618 if (m == MATCH_YES)
3619 d = DECL_IS_BIND_C;
3620 else if (m == MATCH_ERROR)
3621 goto cleanup;
3622 break;
3624 case 'c':
3625 gfc_next_ascii_char ();
3626 if ('o' != gfc_next_ascii_char ())
3627 break;
3628 switch (gfc_next_ascii_char ())
3630 case 'd':
3631 if (match_string_p ("imension"))
3633 d = DECL_CODIMENSION;
3634 break;
3636 case 'n':
3637 if (match_string_p ("tiguous"))
3639 d = DECL_CONTIGUOUS;
3640 break;
3643 break;
3645 case 'd':
3646 if (match_string_p ("dimension"))
3647 d = DECL_DIMENSION;
3648 break;
3650 case 'e':
3651 if (match_string_p ("external"))
3652 d = DECL_EXTERNAL;
3653 break;
3655 case 'i':
3656 if (match_string_p ("int"))
3658 ch = gfc_next_ascii_char ();
3659 if (ch == 'e')
3661 if (match_string_p ("nt"))
3663 /* Matched "intent". */
3664 /* TODO: Call match_intent_spec from here. */
3665 if (gfc_match (" ( in out )") == MATCH_YES)
3666 d = DECL_INOUT;
3667 else if (gfc_match (" ( in )") == MATCH_YES)
3668 d = DECL_IN;
3669 else if (gfc_match (" ( out )") == MATCH_YES)
3670 d = DECL_OUT;
3673 else if (ch == 'r')
3675 if (match_string_p ("insic"))
3677 /* Matched "intrinsic". */
3678 d = DECL_INTRINSIC;
3682 break;
3684 case 'o':
3685 if (match_string_p ("optional"))
3686 d = DECL_OPTIONAL;
3687 break;
3689 case 'p':
3690 gfc_next_ascii_char ();
3691 switch (gfc_next_ascii_char ())
3693 case 'a':
3694 if (match_string_p ("rameter"))
3696 /* Matched "parameter". */
3697 d = DECL_PARAMETER;
3699 break;
3701 case 'o':
3702 if (match_string_p ("inter"))
3704 /* Matched "pointer". */
3705 d = DECL_POINTER;
3707 break;
3709 case 'r':
3710 ch = gfc_next_ascii_char ();
3711 if (ch == 'i')
3713 if (match_string_p ("vate"))
3715 /* Matched "private". */
3716 d = DECL_PRIVATE;
3719 else if (ch == 'o')
3721 if (match_string_p ("tected"))
3723 /* Matched "protected". */
3724 d = DECL_PROTECTED;
3727 break;
3729 case 'u':
3730 if (match_string_p ("blic"))
3732 /* Matched "public". */
3733 d = DECL_PUBLIC;
3735 break;
3737 break;
3739 case 's':
3740 if (match_string_p ("save"))
3741 d = DECL_SAVE;
3742 break;
3744 case 't':
3745 if (match_string_p ("target"))
3746 d = DECL_TARGET;
3747 break;
3749 case 'v':
3750 gfc_next_ascii_char ();
3751 ch = gfc_next_ascii_char ();
3752 if (ch == 'a')
3754 if (match_string_p ("lue"))
3756 /* Matched "value". */
3757 d = DECL_VALUE;
3760 else if (ch == 'o')
3762 if (match_string_p ("latile"))
3764 /* Matched "volatile". */
3765 d = DECL_VOLATILE;
3768 break;
3772 /* No double colon and no recognizable decl_type, so assume that
3773 we've been looking at something else the whole time. */
3774 if (d == DECL_NONE)
3776 m = MATCH_NO;
3777 goto cleanup;
3780 /* Check to make sure any parens are paired up correctly. */
3781 if (gfc_match_parens () == MATCH_ERROR)
3783 m = MATCH_ERROR;
3784 goto cleanup;
3787 seen[d]++;
3788 seen_at[d] = gfc_current_locus;
3790 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3792 gfc_array_spec *as = NULL;
3794 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3795 d == DECL_CODIMENSION);
3797 if (current_as == NULL)
3798 current_as = as;
3799 else if (m == MATCH_YES)
3801 if (!merge_array_spec (as, current_as, false))
3802 m = MATCH_ERROR;
3803 free (as);
3806 if (m == MATCH_NO)
3808 if (d == DECL_CODIMENSION)
3809 gfc_error ("Missing codimension specification at %C");
3810 else
3811 gfc_error ("Missing dimension specification at %C");
3812 m = MATCH_ERROR;
3815 if (m == MATCH_ERROR)
3816 goto cleanup;
3820 /* Since we've seen a double colon, we have to be looking at an
3821 attr-spec. This means that we can now issue errors. */
3822 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3823 if (seen[d] > 1)
3825 switch (d)
3827 case DECL_ALLOCATABLE:
3828 attr = "ALLOCATABLE";
3829 break;
3830 case DECL_ASYNCHRONOUS:
3831 attr = "ASYNCHRONOUS";
3832 break;
3833 case DECL_CODIMENSION:
3834 attr = "CODIMENSION";
3835 break;
3836 case DECL_CONTIGUOUS:
3837 attr = "CONTIGUOUS";
3838 break;
3839 case DECL_DIMENSION:
3840 attr = "DIMENSION";
3841 break;
3842 case DECL_EXTERNAL:
3843 attr = "EXTERNAL";
3844 break;
3845 case DECL_IN:
3846 attr = "INTENT (IN)";
3847 break;
3848 case DECL_OUT:
3849 attr = "INTENT (OUT)";
3850 break;
3851 case DECL_INOUT:
3852 attr = "INTENT (IN OUT)";
3853 break;
3854 case DECL_INTRINSIC:
3855 attr = "INTRINSIC";
3856 break;
3857 case DECL_OPTIONAL:
3858 attr = "OPTIONAL";
3859 break;
3860 case DECL_PARAMETER:
3861 attr = "PARAMETER";
3862 break;
3863 case DECL_POINTER:
3864 attr = "POINTER";
3865 break;
3866 case DECL_PROTECTED:
3867 attr = "PROTECTED";
3868 break;
3869 case DECL_PRIVATE:
3870 attr = "PRIVATE";
3871 break;
3872 case DECL_PUBLIC:
3873 attr = "PUBLIC";
3874 break;
3875 case DECL_SAVE:
3876 attr = "SAVE";
3877 break;
3878 case DECL_TARGET:
3879 attr = "TARGET";
3880 break;
3881 case DECL_IS_BIND_C:
3882 attr = "IS_BIND_C";
3883 break;
3884 case DECL_VALUE:
3885 attr = "VALUE";
3886 break;
3887 case DECL_VOLATILE:
3888 attr = "VOLATILE";
3889 break;
3890 default:
3891 attr = NULL; /* This shouldn't happen. */
3894 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3895 m = MATCH_ERROR;
3896 goto cleanup;
3899 /* Now that we've dealt with duplicate attributes, add the attributes
3900 to the current attribute. */
3901 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3903 if (seen[d] == 0)
3904 continue;
3906 if (gfc_current_state () == COMP_DERIVED
3907 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3908 && d != DECL_POINTER && d != DECL_PRIVATE
3909 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3911 if (d == DECL_ALLOCATABLE)
3913 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3914 "attribute at %C in a TYPE definition"))
3916 m = MATCH_ERROR;
3917 goto cleanup;
3920 else
3922 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3923 &seen_at[d]);
3924 m = MATCH_ERROR;
3925 goto cleanup;
3929 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3930 && gfc_current_state () != COMP_MODULE)
3932 if (d == DECL_PRIVATE)
3933 attr = "PRIVATE";
3934 else
3935 attr = "PUBLIC";
3936 if (gfc_current_state () == COMP_DERIVED
3937 && gfc_state_stack->previous
3938 && gfc_state_stack->previous->state == COMP_MODULE)
3940 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3941 "at %L in a TYPE definition", attr,
3942 &seen_at[d]))
3944 m = MATCH_ERROR;
3945 goto cleanup;
3948 else
3950 gfc_error ("%s attribute at %L is not allowed outside of the "
3951 "specification part of a module", attr, &seen_at[d]);
3952 m = MATCH_ERROR;
3953 goto cleanup;
3957 switch (d)
3959 case DECL_ALLOCATABLE:
3960 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3961 break;
3963 case DECL_ASYNCHRONOUS:
3964 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3965 t = false;
3966 else
3967 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3968 break;
3970 case DECL_CODIMENSION:
3971 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3972 break;
3974 case DECL_CONTIGUOUS:
3975 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3976 t = false;
3977 else
3978 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3979 break;
3981 case DECL_DIMENSION:
3982 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3983 break;
3985 case DECL_EXTERNAL:
3986 t = gfc_add_external (&current_attr, &seen_at[d]);
3987 break;
3989 case DECL_IN:
3990 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3991 break;
3993 case DECL_OUT:
3994 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3995 break;
3997 case DECL_INOUT:
3998 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3999 break;
4001 case DECL_INTRINSIC:
4002 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
4003 break;
4005 case DECL_OPTIONAL:
4006 t = gfc_add_optional (&current_attr, &seen_at[d]);
4007 break;
4009 case DECL_PARAMETER:
4010 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
4011 break;
4013 case DECL_POINTER:
4014 t = gfc_add_pointer (&current_attr, &seen_at[d]);
4015 break;
4017 case DECL_PROTECTED:
4018 if (gfc_current_state () != COMP_MODULE
4019 || (gfc_current_ns->proc_name
4020 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
4022 gfc_error ("PROTECTED at %C only allowed in specification "
4023 "part of a module");
4024 t = false;
4025 break;
4028 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
4029 t = false;
4030 else
4031 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
4032 break;
4034 case DECL_PRIVATE:
4035 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
4036 &seen_at[d]);
4037 break;
4039 case DECL_PUBLIC:
4040 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
4041 &seen_at[d]);
4042 break;
4044 case DECL_SAVE:
4045 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
4046 break;
4048 case DECL_TARGET:
4049 t = gfc_add_target (&current_attr, &seen_at[d]);
4050 break;
4052 case DECL_IS_BIND_C:
4053 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
4054 break;
4056 case DECL_VALUE:
4057 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
4058 t = false;
4059 else
4060 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
4061 break;
4063 case DECL_VOLATILE:
4064 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
4065 t = false;
4066 else
4067 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
4068 break;
4070 default:
4071 gfc_internal_error ("match_attr_spec(): Bad attribute");
4074 if (!t)
4076 m = MATCH_ERROR;
4077 goto cleanup;
4081 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4082 if ((gfc_current_state () == COMP_MODULE
4083 || gfc_current_state () == COMP_SUBMODULE)
4084 && !current_attr.save
4085 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4086 current_attr.save = SAVE_IMPLICIT;
4088 colon_seen = 1;
4089 return MATCH_YES;
4091 cleanup:
4092 gfc_current_locus = start;
4093 gfc_free_array_spec (current_as);
4094 current_as = NULL;
4095 return m;
4099 /* Set the binding label, dest_label, either with the binding label
4100 stored in the given gfc_typespec, ts, or if none was provided, it
4101 will be the symbol name in all lower case, as required by the draft
4102 (J3/04-007, section 15.4.1). If a binding label was given and
4103 there is more than one argument (num_idents), it is an error. */
4105 static bool
4106 set_binding_label (const char **dest_label, const char *sym_name,
4107 int num_idents)
4109 if (num_idents > 1 && has_name_equals)
4111 gfc_error ("Multiple identifiers provided with "
4112 "single NAME= specifier at %C");
4113 return false;
4116 if (curr_binding_label)
4117 /* Binding label given; store in temp holder till have sym. */
4118 *dest_label = curr_binding_label;
4119 else
4121 /* No binding label given, and the NAME= specifier did not exist,
4122 which means there was no NAME="". */
4123 if (sym_name != NULL && has_name_equals == 0)
4124 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4127 return true;
4131 /* Set the status of the given common block as being BIND(C) or not,
4132 depending on the given parameter, is_bind_c. */
4134 void
4135 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4137 com_block->is_bind_c = is_bind_c;
4138 return;
4142 /* Verify that the given gfc_typespec is for a C interoperable type. */
4144 bool
4145 gfc_verify_c_interop (gfc_typespec *ts)
4147 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4148 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4149 ? true : false;
4150 else if (ts->type == BT_CLASS)
4151 return false;
4152 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4153 return false;
4155 return true;
4159 /* Verify that the variables of a given common block, which has been
4160 defined with the attribute specifier bind(c), to be of a C
4161 interoperable type. Errors will be reported here, if
4162 encountered. */
4164 bool
4165 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4167 gfc_symbol *curr_sym = NULL;
4168 bool retval = true;
4170 curr_sym = com_block->head;
4172 /* Make sure we have at least one symbol. */
4173 if (curr_sym == NULL)
4174 return retval;
4176 /* Here we know we have a symbol, so we'll execute this loop
4177 at least once. */
4180 /* The second to last param, 1, says this is in a common block. */
4181 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4182 curr_sym = curr_sym->common_next;
4183 } while (curr_sym != NULL);
4185 return retval;
4189 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4190 an appropriate error message is reported. */
4192 bool
4193 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4194 int is_in_common, gfc_common_head *com_block)
4196 bool bind_c_function = false;
4197 bool retval = true;
4199 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4200 bind_c_function = true;
4202 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4204 tmp_sym = tmp_sym->result;
4205 /* Make sure it wasn't an implicitly typed result. */
4206 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4208 gfc_warning (OPT_Wc_binding_type,
4209 "Implicitly declared BIND(C) function %qs at "
4210 "%L may not be C interoperable", tmp_sym->name,
4211 &tmp_sym->declared_at);
4212 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4213 /* Mark it as C interoperable to prevent duplicate warnings. */
4214 tmp_sym->ts.is_c_interop = 1;
4215 tmp_sym->attr.is_c_interop = 1;
4219 /* Here, we know we have the bind(c) attribute, so if we have
4220 enough type info, then verify that it's a C interop kind.
4221 The info could be in the symbol already, or possibly still in
4222 the given ts (current_ts), so look in both. */
4223 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4225 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4227 /* See if we're dealing with a sym in a common block or not. */
4228 if (is_in_common == 1 && warn_c_binding_type)
4230 gfc_warning (OPT_Wc_binding_type,
4231 "Variable %qs in common block %qs at %L "
4232 "may not be a C interoperable "
4233 "kind though common block %qs is BIND(C)",
4234 tmp_sym->name, com_block->name,
4235 &(tmp_sym->declared_at), com_block->name);
4237 else
4239 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4240 gfc_error ("Type declaration %qs at %L is not C "
4241 "interoperable but it is BIND(C)",
4242 tmp_sym->name, &(tmp_sym->declared_at));
4243 else if (warn_c_binding_type)
4244 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4245 "may not be a C interoperable "
4246 "kind but it is BIND(C)",
4247 tmp_sym->name, &(tmp_sym->declared_at));
4251 /* Variables declared w/in a common block can't be bind(c)
4252 since there's no way for C to see these variables, so there's
4253 semantically no reason for the attribute. */
4254 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4256 gfc_error ("Variable %qs in common block %qs at "
4257 "%L cannot be declared with BIND(C) "
4258 "since it is not a global",
4259 tmp_sym->name, com_block->name,
4260 &(tmp_sym->declared_at));
4261 retval = false;
4264 /* Scalar variables that are bind(c) can not have the pointer
4265 or allocatable attributes. */
4266 if (tmp_sym->attr.is_bind_c == 1)
4268 if (tmp_sym->attr.pointer == 1)
4270 gfc_error ("Variable %qs at %L cannot have both the "
4271 "POINTER and BIND(C) attributes",
4272 tmp_sym->name, &(tmp_sym->declared_at));
4273 retval = false;
4276 if (tmp_sym->attr.allocatable == 1)
4278 gfc_error ("Variable %qs at %L cannot have both the "
4279 "ALLOCATABLE and BIND(C) attributes",
4280 tmp_sym->name, &(tmp_sym->declared_at));
4281 retval = false;
4286 /* If it is a BIND(C) function, make sure the return value is a
4287 scalar value. The previous tests in this function made sure
4288 the type is interoperable. */
4289 if (bind_c_function && tmp_sym->as != NULL)
4290 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4291 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4293 /* BIND(C) functions can not return a character string. */
4294 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4295 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4296 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4297 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4298 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4299 "be a character string", tmp_sym->name,
4300 &(tmp_sym->declared_at));
4303 /* See if the symbol has been marked as private. If it has, make sure
4304 there is no binding label and warn the user if there is one. */
4305 if (tmp_sym->attr.access == ACCESS_PRIVATE
4306 && tmp_sym->binding_label)
4307 /* Use gfc_warning_now because we won't say that the symbol fails
4308 just because of this. */
4309 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4310 "given the binding label %qs", tmp_sym->name,
4311 &(tmp_sym->declared_at), tmp_sym->binding_label);
4313 return retval;
4317 /* Set the appropriate fields for a symbol that's been declared as
4318 BIND(C) (the is_bind_c flag and the binding label), and verify that
4319 the type is C interoperable. Errors are reported by the functions
4320 used to set/test these fields. */
4322 bool
4323 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4325 bool retval = true;
4327 /* TODO: Do we need to make sure the vars aren't marked private? */
4329 /* Set the is_bind_c bit in symbol_attribute. */
4330 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4332 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4333 return false;
4335 return retval;
4339 /* Set the fields marking the given common block as BIND(C), including
4340 a binding label, and report any errors encountered. */
4342 bool
4343 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4345 bool retval = true;
4347 /* destLabel, common name, typespec (which may have binding label). */
4348 if (!set_binding_label (&com_block->binding_label, com_block->name,
4349 num_idents))
4350 return false;
4352 /* Set the given common block (com_block) to being bind(c) (1). */
4353 set_com_block_bind_c (com_block, 1);
4355 return retval;
4359 /* Retrieve the list of one or more identifiers that the given bind(c)
4360 attribute applies to. */
4362 bool
4363 get_bind_c_idents (void)
4365 char name[GFC_MAX_SYMBOL_LEN + 1];
4366 int num_idents = 0;
4367 gfc_symbol *tmp_sym = NULL;
4368 match found_id;
4369 gfc_common_head *com_block = NULL;
4371 if (gfc_match_name (name) == MATCH_YES)
4373 found_id = MATCH_YES;
4374 gfc_get_ha_symbol (name, &tmp_sym);
4376 else if (match_common_name (name) == MATCH_YES)
4378 found_id = MATCH_YES;
4379 com_block = gfc_get_common (name, 0);
4381 else
4383 gfc_error ("Need either entity or common block name for "
4384 "attribute specification statement at %C");
4385 return false;
4388 /* Save the current identifier and look for more. */
4391 /* Increment the number of identifiers found for this spec stmt. */
4392 num_idents++;
4394 /* Make sure we have a sym or com block, and verify that it can
4395 be bind(c). Set the appropriate field(s) and look for more
4396 identifiers. */
4397 if (tmp_sym != NULL || com_block != NULL)
4399 if (tmp_sym != NULL)
4401 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4402 return false;
4404 else
4406 if (!set_verify_bind_c_com_block (com_block, num_idents))
4407 return false;
4410 /* Look to see if we have another identifier. */
4411 tmp_sym = NULL;
4412 if (gfc_match_eos () == MATCH_YES)
4413 found_id = MATCH_NO;
4414 else if (gfc_match_char (',') != MATCH_YES)
4415 found_id = MATCH_NO;
4416 else if (gfc_match_name (name) == MATCH_YES)
4418 found_id = MATCH_YES;
4419 gfc_get_ha_symbol (name, &tmp_sym);
4421 else if (match_common_name (name) == MATCH_YES)
4423 found_id = MATCH_YES;
4424 com_block = gfc_get_common (name, 0);
4426 else
4428 gfc_error ("Missing entity or common block name for "
4429 "attribute specification statement at %C");
4430 return false;
4433 else
4435 gfc_internal_error ("Missing symbol");
4437 } while (found_id == MATCH_YES);
4439 /* if we get here we were successful */
4440 return true;
4444 /* Try and match a BIND(C) attribute specification statement. */
4446 match
4447 gfc_match_bind_c_stmt (void)
4449 match found_match = MATCH_NO;
4450 gfc_typespec *ts;
4452 ts = &current_ts;
4454 /* This may not be necessary. */
4455 gfc_clear_ts (ts);
4456 /* Clear the temporary binding label holder. */
4457 curr_binding_label = NULL;
4459 /* Look for the bind(c). */
4460 found_match = gfc_match_bind_c (NULL, true);
4462 if (found_match == MATCH_YES)
4464 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4465 return MATCH_ERROR;
4467 /* Look for the :: now, but it is not required. */
4468 gfc_match (" :: ");
4470 /* Get the identifier(s) that needs to be updated. This may need to
4471 change to hand the flag(s) for the attr specified so all identifiers
4472 found can have all appropriate parts updated (assuming that the same
4473 spec stmt can have multiple attrs, such as both bind(c) and
4474 allocatable...). */
4475 if (!get_bind_c_idents ())
4476 /* Error message should have printed already. */
4477 return MATCH_ERROR;
4480 return found_match;
4484 /* Match a data declaration statement. */
4486 match
4487 gfc_match_data_decl (void)
4489 gfc_symbol *sym;
4490 match m;
4491 int elem;
4493 num_idents_on_line = 0;
4495 m = gfc_match_decl_type_spec (&current_ts, 0);
4496 if (m != MATCH_YES)
4497 return m;
4499 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4500 && gfc_current_state () != COMP_DERIVED)
4502 sym = gfc_use_derived (current_ts.u.derived);
4504 if (sym == NULL)
4506 m = MATCH_ERROR;
4507 goto cleanup;
4510 current_ts.u.derived = sym;
4513 m = match_attr_spec ();
4514 if (m == MATCH_ERROR)
4516 m = MATCH_NO;
4517 goto cleanup;
4520 if (current_ts.type == BT_CLASS
4521 && current_ts.u.derived->attr.unlimited_polymorphic)
4522 goto ok;
4524 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4525 && current_ts.u.derived->components == NULL
4526 && !current_ts.u.derived->attr.zero_comp)
4529 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4530 goto ok;
4532 gfc_find_symbol (current_ts.u.derived->name,
4533 current_ts.u.derived->ns, 1, &sym);
4535 /* Any symbol that we find had better be a type definition
4536 which has its components defined. */
4537 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4538 && (current_ts.u.derived->components != NULL
4539 || current_ts.u.derived->attr.zero_comp))
4540 goto ok;
4542 gfc_error ("Derived type at %C has not been previously defined "
4543 "and so cannot appear in a derived type definition");
4544 m = MATCH_ERROR;
4545 goto cleanup;
4549 /* If we have an old-style character declaration, and no new-style
4550 attribute specifications, then there a comma is optional between
4551 the type specification and the variable list. */
4552 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4553 gfc_match_char (',');
4555 /* Give the types/attributes to symbols that follow. Give the element
4556 a number so that repeat character length expressions can be copied. */
4557 elem = 1;
4558 for (;;)
4560 num_idents_on_line++;
4561 m = variable_decl (elem++);
4562 if (m == MATCH_ERROR)
4563 goto cleanup;
4564 if (m == MATCH_NO)
4565 break;
4567 if (gfc_match_eos () == MATCH_YES)
4568 goto cleanup;
4569 if (gfc_match_char (',') != MATCH_YES)
4570 break;
4573 if (!gfc_error_flag_test ())
4574 gfc_error ("Syntax error in data declaration at %C");
4575 m = MATCH_ERROR;
4577 gfc_free_data_all (gfc_current_ns);
4579 cleanup:
4580 gfc_free_array_spec (current_as);
4581 current_as = NULL;
4582 return m;
4586 /* Match a prefix associated with a function or subroutine
4587 declaration. If the typespec pointer is nonnull, then a typespec
4588 can be matched. Note that if nothing matches, MATCH_YES is
4589 returned (the null string was matched). */
4591 match
4592 gfc_match_prefix (gfc_typespec *ts)
4594 bool seen_type;
4595 bool seen_impure;
4596 bool found_prefix;
4598 gfc_clear_attr (&current_attr);
4599 seen_type = false;
4600 seen_impure = false;
4602 gcc_assert (!gfc_matching_prefix);
4603 gfc_matching_prefix = true;
4607 found_prefix = false;
4609 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
4610 corresponding attribute seems natural and distinguishes these
4611 procedures from procedure types of PROC_MODULE, which these are
4612 as well. */
4613 if (gfc_match ("module% ") == MATCH_YES)
4615 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4616 goto error;
4618 current_attr.module_procedure = 1;
4619 found_prefix = true;
4622 if (!seen_type && ts != NULL
4623 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4624 && gfc_match_space () == MATCH_YES)
4627 seen_type = true;
4628 found_prefix = true;
4631 if (gfc_match ("elemental% ") == MATCH_YES)
4633 if (!gfc_add_elemental (&current_attr, NULL))
4634 goto error;
4636 found_prefix = true;
4639 if (gfc_match ("pure% ") == MATCH_YES)
4641 if (!gfc_add_pure (&current_attr, NULL))
4642 goto error;
4644 found_prefix = true;
4647 if (gfc_match ("recursive% ") == MATCH_YES)
4649 if (!gfc_add_recursive (&current_attr, NULL))
4650 goto error;
4652 found_prefix = true;
4655 /* IMPURE is a somewhat special case, as it needs not set an actual
4656 attribute but rather only prevents ELEMENTAL routines from being
4657 automatically PURE. */
4658 if (gfc_match ("impure% ") == MATCH_YES)
4660 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4661 goto error;
4663 seen_impure = true;
4664 found_prefix = true;
4667 while (found_prefix);
4669 /* IMPURE and PURE must not both appear, of course. */
4670 if (seen_impure && current_attr.pure)
4672 gfc_error ("PURE and IMPURE must not appear both at %C");
4673 goto error;
4676 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4677 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4679 if (!gfc_add_pure (&current_attr, NULL))
4680 goto error;
4683 /* At this point, the next item is not a prefix. */
4684 gcc_assert (gfc_matching_prefix);
4686 gfc_matching_prefix = false;
4687 return MATCH_YES;
4689 error:
4690 gcc_assert (gfc_matching_prefix);
4691 gfc_matching_prefix = false;
4692 return MATCH_ERROR;
4696 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4698 static bool
4699 copy_prefix (symbol_attribute *dest, locus *where)
4701 if (current_attr.pure && !gfc_add_pure (dest, where))
4702 return false;
4704 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4705 return false;
4707 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4708 return false;
4710 return true;
4714 /* Match a formal argument list. */
4716 match
4717 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4719 gfc_formal_arglist *head, *tail, *p, *q;
4720 char name[GFC_MAX_SYMBOL_LEN + 1];
4721 gfc_symbol *sym;
4722 match m;
4723 gfc_formal_arglist *formal = NULL;
4725 head = tail = NULL;
4727 /* Keep the interface formal argument list and null it so that the
4728 matching for the new declaration can be done. The numbers and
4729 names of the arguments are checked here. The interface formal
4730 arguments are retained in formal_arglist and the characteristics
4731 are compared in resolve.c(resolve_fl_procedure). See the remark
4732 in get_proc_name about the eventual need to copy the formal_arglist
4733 and populate the formal namespace of the interface symbol. */
4734 if (progname->attr.module_procedure
4735 && progname->attr.host_assoc)
4737 formal = progname->formal;
4738 progname->formal = NULL;
4741 if (gfc_match_char ('(') != MATCH_YES)
4743 if (null_flag)
4744 goto ok;
4745 return MATCH_NO;
4748 if (gfc_match_char (')') == MATCH_YES)
4749 goto ok;
4751 for (;;)
4753 if (gfc_match_char ('*') == MATCH_YES)
4755 sym = NULL;
4756 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4757 "at %C"))
4759 m = MATCH_ERROR;
4760 goto cleanup;
4763 else
4765 m = gfc_match_name (name);
4766 if (m != MATCH_YES)
4767 goto cleanup;
4769 if (gfc_get_symbol (name, NULL, &sym))
4770 goto cleanup;
4773 p = gfc_get_formal_arglist ();
4775 if (head == NULL)
4776 head = tail = p;
4777 else
4779 tail->next = p;
4780 tail = p;
4783 tail->sym = sym;
4785 /* We don't add the VARIABLE flavor because the name could be a
4786 dummy procedure. We don't apply these attributes to formal
4787 arguments of statement functions. */
4788 if (sym != NULL && !st_flag
4789 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4790 || !gfc_missing_attr (&sym->attr, NULL)))
4792 m = MATCH_ERROR;
4793 goto cleanup;
4796 /* The name of a program unit can be in a different namespace,
4797 so check for it explicitly. After the statement is accepted,
4798 the name is checked for especially in gfc_get_symbol(). */
4799 if (gfc_new_block != NULL && sym != NULL
4800 && strcmp (sym->name, gfc_new_block->name) == 0)
4802 gfc_error ("Name %qs at %C is the name of the procedure",
4803 sym->name);
4804 m = MATCH_ERROR;
4805 goto cleanup;
4808 if (gfc_match_char (')') == MATCH_YES)
4809 goto ok;
4811 m = gfc_match_char (',');
4812 if (m != MATCH_YES)
4814 gfc_error ("Unexpected junk in formal argument list at %C");
4815 goto cleanup;
4820 /* Check for duplicate symbols in the formal argument list. */
4821 if (head != NULL)
4823 for (p = head; p->next; p = p->next)
4825 if (p->sym == NULL)
4826 continue;
4828 for (q = p->next; q; q = q->next)
4829 if (p->sym == q->sym)
4831 gfc_error ("Duplicate symbol %qs in formal argument list "
4832 "at %C", p->sym->name);
4834 m = MATCH_ERROR;
4835 goto cleanup;
4840 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4842 m = MATCH_ERROR;
4843 goto cleanup;
4846 /* gfc_error_now used in following and return with MATCH_YES because
4847 doing otherwise results in a cascade of extraneous errors and in
4848 some cases an ICE in symbol.c(gfc_release_symbol). */
4849 if (progname->attr.module_procedure && progname->attr.host_assoc)
4851 bool arg_count_mismatch = false;
4853 if (!formal && head)
4854 arg_count_mismatch = true;
4856 /* Abbreviated module procedure declaration is not meant to have any
4857 formal arguments! */
4858 if (!progname->abr_modproc_decl && formal && !head)
4859 arg_count_mismatch = true;
4861 for (p = formal, q = head; p && q; p = p->next, q = q->next)
4863 if ((p->next != NULL && q->next == NULL)
4864 || (p->next == NULL && q->next != NULL))
4865 arg_count_mismatch = true;
4866 else if ((p->sym == NULL && q->sym == NULL)
4867 || strcmp (p->sym->name, q->sym->name) == 0)
4868 continue;
4869 else
4870 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4871 "argument names (%s/%s) at %C",
4872 p->sym->name, q->sym->name);
4875 if (arg_count_mismatch)
4876 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4877 "formal arguments at %C");
4880 return MATCH_YES;
4882 cleanup:
4883 gfc_free_formal_arglist (head);
4884 return m;
4888 /* Match a RESULT specification following a function declaration or
4889 ENTRY statement. Also matches the end-of-statement. */
4891 static match
4892 match_result (gfc_symbol *function, gfc_symbol **result)
4894 char name[GFC_MAX_SYMBOL_LEN + 1];
4895 gfc_symbol *r;
4896 match m;
4898 if (gfc_match (" result (") != MATCH_YES)
4899 return MATCH_NO;
4901 m = gfc_match_name (name);
4902 if (m != MATCH_YES)
4903 return m;
4905 /* Get the right paren, and that's it because there could be the
4906 bind(c) attribute after the result clause. */
4907 if (gfc_match_char (')') != MATCH_YES)
4909 /* TODO: should report the missing right paren here. */
4910 return MATCH_ERROR;
4913 if (strcmp (function->name, name) == 0)
4915 gfc_error ("RESULT variable at %C must be different than function name");
4916 return MATCH_ERROR;
4919 if (gfc_get_symbol (name, NULL, &r))
4920 return MATCH_ERROR;
4922 if (!gfc_add_result (&r->attr, r->name, NULL))
4923 return MATCH_ERROR;
4925 *result = r;
4927 return MATCH_YES;
4931 /* Match a function suffix, which could be a combination of a result
4932 clause and BIND(C), either one, or neither. The draft does not
4933 require them to come in a specific order. */
4935 match
4936 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4938 match is_bind_c; /* Found bind(c). */
4939 match is_result; /* Found result clause. */
4940 match found_match; /* Status of whether we've found a good match. */
4941 char peek_char; /* Character we're going to peek at. */
4942 bool allow_binding_name;
4944 /* Initialize to having found nothing. */
4945 found_match = MATCH_NO;
4946 is_bind_c = MATCH_NO;
4947 is_result = MATCH_NO;
4949 /* Get the next char to narrow between result and bind(c). */
4950 gfc_gobble_whitespace ();
4951 peek_char = gfc_peek_ascii_char ();
4953 /* C binding names are not allowed for internal procedures. */
4954 if (gfc_current_state () == COMP_CONTAINS
4955 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4956 allow_binding_name = false;
4957 else
4958 allow_binding_name = true;
4960 switch (peek_char)
4962 case 'r':
4963 /* Look for result clause. */
4964 is_result = match_result (sym, result);
4965 if (is_result == MATCH_YES)
4967 /* Now see if there is a bind(c) after it. */
4968 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4969 /* We've found the result clause and possibly bind(c). */
4970 found_match = MATCH_YES;
4972 else
4973 /* This should only be MATCH_ERROR. */
4974 found_match = is_result;
4975 break;
4976 case 'b':
4977 /* Look for bind(c) first. */
4978 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4979 if (is_bind_c == MATCH_YES)
4981 /* Now see if a result clause followed it. */
4982 is_result = match_result (sym, result);
4983 found_match = MATCH_YES;
4985 else
4987 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4988 found_match = MATCH_ERROR;
4990 break;
4991 default:
4992 gfc_error ("Unexpected junk after function declaration at %C");
4993 found_match = MATCH_ERROR;
4994 break;
4997 if (is_bind_c == MATCH_YES)
4999 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
5000 if (gfc_current_state () == COMP_CONTAINS
5001 && sym->ns->proc_name->attr.flavor != FL_MODULE
5002 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5003 "at %L may not be specified for an internal "
5004 "procedure", &gfc_current_locus))
5005 return MATCH_ERROR;
5007 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
5008 return MATCH_ERROR;
5011 return found_match;
5015 /* Procedure pointer return value without RESULT statement:
5016 Add "hidden" result variable named "ppr@". */
5018 static bool
5019 add_hidden_procptr_result (gfc_symbol *sym)
5021 bool case1,case2;
5023 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
5024 return false;
5026 /* First usage case: PROCEDURE and EXTERNAL statements. */
5027 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
5028 && strcmp (gfc_current_block ()->name, sym->name) == 0
5029 && sym->attr.external;
5030 /* Second usage case: INTERFACE statements. */
5031 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
5032 && gfc_state_stack->previous->state == COMP_FUNCTION
5033 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
5035 if (case1 || case2)
5037 gfc_symtree *stree;
5038 if (case1)
5039 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
5040 else if (case2)
5042 gfc_symtree *st2;
5043 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
5044 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
5045 st2->n.sym = stree->n.sym;
5047 sym->result = stree->n.sym;
5049 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
5050 sym->result->attr.pointer = sym->attr.pointer;
5051 sym->result->attr.external = sym->attr.external;
5052 sym->result->attr.referenced = sym->attr.referenced;
5053 sym->result->ts = sym->ts;
5054 sym->attr.proc_pointer = 0;
5055 sym->attr.pointer = 0;
5056 sym->attr.external = 0;
5057 if (sym->result->attr.external && sym->result->attr.pointer)
5059 sym->result->attr.pointer = 0;
5060 sym->result->attr.proc_pointer = 1;
5063 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
5065 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5066 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
5067 && sym->result && sym->result != sym && sym->result->attr.external
5068 && sym == gfc_current_ns->proc_name
5069 && sym == sym->result->ns->proc_name
5070 && strcmp ("ppr@", sym->result->name) == 0)
5072 sym->result->attr.proc_pointer = 1;
5073 sym->attr.pointer = 0;
5074 return true;
5076 else
5077 return false;
5081 /* Match the interface for a PROCEDURE declaration,
5082 including brackets (R1212). */
5084 static match
5085 match_procedure_interface (gfc_symbol **proc_if)
5087 match m;
5088 gfc_symtree *st;
5089 locus old_loc, entry_loc;
5090 gfc_namespace *old_ns = gfc_current_ns;
5091 char name[GFC_MAX_SYMBOL_LEN + 1];
5093 old_loc = entry_loc = gfc_current_locus;
5094 gfc_clear_ts (&current_ts);
5096 if (gfc_match (" (") != MATCH_YES)
5098 gfc_current_locus = entry_loc;
5099 return MATCH_NO;
5102 /* Get the type spec. for the procedure interface. */
5103 old_loc = gfc_current_locus;
5104 m = gfc_match_decl_type_spec (&current_ts, 0);
5105 gfc_gobble_whitespace ();
5106 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
5107 goto got_ts;
5109 if (m == MATCH_ERROR)
5110 return m;
5112 /* Procedure interface is itself a procedure. */
5113 gfc_current_locus = old_loc;
5114 m = gfc_match_name (name);
5116 /* First look to see if it is already accessible in the current
5117 namespace because it is use associated or contained. */
5118 st = NULL;
5119 if (gfc_find_sym_tree (name, NULL, 0, &st))
5120 return MATCH_ERROR;
5122 /* If it is still not found, then try the parent namespace, if it
5123 exists and create the symbol there if it is still not found. */
5124 if (gfc_current_ns->parent)
5125 gfc_current_ns = gfc_current_ns->parent;
5126 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5127 return MATCH_ERROR;
5129 gfc_current_ns = old_ns;
5130 *proc_if = st->n.sym;
5132 if (*proc_if)
5134 (*proc_if)->refs++;
5135 /* Resolve interface if possible. That way, attr.procedure is only set
5136 if it is declared by a later procedure-declaration-stmt, which is
5137 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5138 while ((*proc_if)->ts.interface)
5139 *proc_if = (*proc_if)->ts.interface;
5141 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5142 && (*proc_if)->ts.type == BT_UNKNOWN
5143 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5144 (*proc_if)->name, NULL))
5145 return MATCH_ERROR;
5148 got_ts:
5149 if (gfc_match (" )") != MATCH_YES)
5151 gfc_current_locus = entry_loc;
5152 return MATCH_NO;
5155 return MATCH_YES;
5159 /* Match a PROCEDURE declaration (R1211). */
5161 static match
5162 match_procedure_decl (void)
5164 match m;
5165 gfc_symbol *sym, *proc_if = NULL;
5166 int num;
5167 gfc_expr *initializer = NULL;
5169 /* Parse interface (with brackets). */
5170 m = match_procedure_interface (&proc_if);
5171 if (m != MATCH_YES)
5172 return m;
5174 /* Parse attributes (with colons). */
5175 m = match_attr_spec();
5176 if (m == MATCH_ERROR)
5177 return MATCH_ERROR;
5179 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5181 current_attr.is_bind_c = 1;
5182 has_name_equals = 0;
5183 curr_binding_label = NULL;
5186 /* Get procedure symbols. */
5187 for(num=1;;num++)
5189 m = gfc_match_symbol (&sym, 0);
5190 if (m == MATCH_NO)
5191 goto syntax;
5192 else if (m == MATCH_ERROR)
5193 return m;
5195 /* Add current_attr to the symbol attributes. */
5196 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5197 return MATCH_ERROR;
5199 if (sym->attr.is_bind_c)
5201 /* Check for C1218. */
5202 if (!proc_if || !proc_if->attr.is_bind_c)
5204 gfc_error ("BIND(C) attribute at %C requires "
5205 "an interface with BIND(C)");
5206 return MATCH_ERROR;
5208 /* Check for C1217. */
5209 if (has_name_equals && sym->attr.pointer)
5211 gfc_error ("BIND(C) procedure with NAME may not have "
5212 "POINTER attribute at %C");
5213 return MATCH_ERROR;
5215 if (has_name_equals && sym->attr.dummy)
5217 gfc_error ("Dummy procedure at %C may not have "
5218 "BIND(C) attribute with NAME");
5219 return MATCH_ERROR;
5221 /* Set binding label for BIND(C). */
5222 if (!set_binding_label (&sym->binding_label, sym->name, num))
5223 return MATCH_ERROR;
5226 if (!gfc_add_external (&sym->attr, NULL))
5227 return MATCH_ERROR;
5229 if (add_hidden_procptr_result (sym))
5230 sym = sym->result;
5232 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5233 return MATCH_ERROR;
5235 /* Set interface. */
5236 if (proc_if != NULL)
5238 if (sym->ts.type != BT_UNKNOWN)
5240 gfc_error ("Procedure %qs at %L already has basic type of %s",
5241 sym->name, &gfc_current_locus,
5242 gfc_basic_typename (sym->ts.type));
5243 return MATCH_ERROR;
5245 sym->ts.interface = proc_if;
5246 sym->attr.untyped = 1;
5247 sym->attr.if_source = IFSRC_IFBODY;
5249 else if (current_ts.type != BT_UNKNOWN)
5251 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5252 return MATCH_ERROR;
5253 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5254 sym->ts.interface->ts = current_ts;
5255 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5256 sym->ts.interface->attr.function = 1;
5257 sym->attr.function = 1;
5258 sym->attr.if_source = IFSRC_UNKNOWN;
5261 if (gfc_match (" =>") == MATCH_YES)
5263 if (!current_attr.pointer)
5265 gfc_error ("Initialization at %C isn't for a pointer variable");
5266 m = MATCH_ERROR;
5267 goto cleanup;
5270 m = match_pointer_init (&initializer, 1);
5271 if (m != MATCH_YES)
5272 goto cleanup;
5274 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5275 goto cleanup;
5279 if (gfc_match_eos () == MATCH_YES)
5280 return MATCH_YES;
5281 if (gfc_match_char (',') != MATCH_YES)
5282 goto syntax;
5285 syntax:
5286 gfc_error ("Syntax error in PROCEDURE statement at %C");
5287 return MATCH_ERROR;
5289 cleanup:
5290 /* Free stuff up and return. */
5291 gfc_free_expr (initializer);
5292 return m;
5296 static match
5297 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5300 /* Match a procedure pointer component declaration (R445). */
5302 static match
5303 match_ppc_decl (void)
5305 match m;
5306 gfc_symbol *proc_if = NULL;
5307 gfc_typespec ts;
5308 int num;
5309 gfc_component *c;
5310 gfc_expr *initializer = NULL;
5311 gfc_typebound_proc* tb;
5312 char name[GFC_MAX_SYMBOL_LEN + 1];
5314 /* Parse interface (with brackets). */
5315 m = match_procedure_interface (&proc_if);
5316 if (m != MATCH_YES)
5317 goto syntax;
5319 /* Parse attributes. */
5320 tb = XCNEW (gfc_typebound_proc);
5321 tb->where = gfc_current_locus;
5322 m = match_binding_attributes (tb, false, true);
5323 if (m == MATCH_ERROR)
5324 return m;
5326 gfc_clear_attr (&current_attr);
5327 current_attr.procedure = 1;
5328 current_attr.proc_pointer = 1;
5329 current_attr.access = tb->access;
5330 current_attr.flavor = FL_PROCEDURE;
5332 /* Match the colons (required). */
5333 if (gfc_match (" ::") != MATCH_YES)
5335 gfc_error ("Expected %<::%> after binding-attributes at %C");
5336 return MATCH_ERROR;
5339 /* Check for C450. */
5340 if (!tb->nopass && proc_if == NULL)
5342 gfc_error("NOPASS or explicit interface required at %C");
5343 return MATCH_ERROR;
5346 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5347 return MATCH_ERROR;
5349 /* Match PPC names. */
5350 ts = current_ts;
5351 for(num=1;;num++)
5353 m = gfc_match_name (name);
5354 if (m == MATCH_NO)
5355 goto syntax;
5356 else if (m == MATCH_ERROR)
5357 return m;
5359 if (!gfc_add_component (gfc_current_block(), name, &c))
5360 return MATCH_ERROR;
5362 /* Add current_attr to the symbol attributes. */
5363 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5364 return MATCH_ERROR;
5366 if (!gfc_add_external (&c->attr, NULL))
5367 return MATCH_ERROR;
5369 if (!gfc_add_proc (&c->attr, name, NULL))
5370 return MATCH_ERROR;
5372 if (num == 1)
5373 c->tb = tb;
5374 else
5376 c->tb = XCNEW (gfc_typebound_proc);
5377 c->tb->where = gfc_current_locus;
5378 *c->tb = *tb;
5381 /* Set interface. */
5382 if (proc_if != NULL)
5384 c->ts.interface = proc_if;
5385 c->attr.untyped = 1;
5386 c->attr.if_source = IFSRC_IFBODY;
5388 else if (ts.type != BT_UNKNOWN)
5390 c->ts = ts;
5391 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5392 c->ts.interface->result = c->ts.interface;
5393 c->ts.interface->ts = ts;
5394 c->ts.interface->attr.flavor = FL_PROCEDURE;
5395 c->ts.interface->attr.function = 1;
5396 c->attr.function = 1;
5397 c->attr.if_source = IFSRC_UNKNOWN;
5400 if (gfc_match (" =>") == MATCH_YES)
5402 m = match_pointer_init (&initializer, 1);
5403 if (m != MATCH_YES)
5405 gfc_free_expr (initializer);
5406 return m;
5408 c->initializer = initializer;
5411 if (gfc_match_eos () == MATCH_YES)
5412 return MATCH_YES;
5413 if (gfc_match_char (',') != MATCH_YES)
5414 goto syntax;
5417 syntax:
5418 gfc_error ("Syntax error in procedure pointer component at %C");
5419 return MATCH_ERROR;
5423 /* Match a PROCEDURE declaration inside an interface (R1206). */
5425 static match
5426 match_procedure_in_interface (void)
5428 match m;
5429 gfc_symbol *sym;
5430 char name[GFC_MAX_SYMBOL_LEN + 1];
5431 locus old_locus;
5433 if (current_interface.type == INTERFACE_NAMELESS
5434 || current_interface.type == INTERFACE_ABSTRACT)
5436 gfc_error ("PROCEDURE at %C must be in a generic interface");
5437 return MATCH_ERROR;
5440 /* Check if the F2008 optional double colon appears. */
5441 gfc_gobble_whitespace ();
5442 old_locus = gfc_current_locus;
5443 if (gfc_match ("::") == MATCH_YES)
5445 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5446 "MODULE PROCEDURE statement at %L", &old_locus))
5447 return MATCH_ERROR;
5449 else
5450 gfc_current_locus = old_locus;
5452 for(;;)
5454 m = gfc_match_name (name);
5455 if (m == MATCH_NO)
5456 goto syntax;
5457 else if (m == MATCH_ERROR)
5458 return m;
5459 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5460 return MATCH_ERROR;
5462 if (!gfc_add_interface (sym))
5463 return MATCH_ERROR;
5465 if (gfc_match_eos () == MATCH_YES)
5466 break;
5467 if (gfc_match_char (',') != MATCH_YES)
5468 goto syntax;
5471 return MATCH_YES;
5473 syntax:
5474 gfc_error ("Syntax error in PROCEDURE statement at %C");
5475 return MATCH_ERROR;
5479 /* General matcher for PROCEDURE declarations. */
5481 static match match_procedure_in_type (void);
5483 match
5484 gfc_match_procedure (void)
5486 match m;
5488 switch (gfc_current_state ())
5490 case COMP_NONE:
5491 case COMP_PROGRAM:
5492 case COMP_MODULE:
5493 case COMP_SUBMODULE:
5494 case COMP_SUBROUTINE:
5495 case COMP_FUNCTION:
5496 case COMP_BLOCK:
5497 m = match_procedure_decl ();
5498 break;
5499 case COMP_INTERFACE:
5500 m = match_procedure_in_interface ();
5501 break;
5502 case COMP_DERIVED:
5503 m = match_ppc_decl ();
5504 break;
5505 case COMP_DERIVED_CONTAINS:
5506 m = match_procedure_in_type ();
5507 break;
5508 default:
5509 return MATCH_NO;
5512 if (m != MATCH_YES)
5513 return m;
5515 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5516 return MATCH_ERROR;
5518 return m;
5522 /* Warn if a matched procedure has the same name as an intrinsic; this is
5523 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5524 parser-state-stack to find out whether we're in a module. */
5526 static void
5527 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5529 bool in_module;
5531 in_module = (gfc_state_stack->previous
5532 && (gfc_state_stack->previous->state == COMP_MODULE
5533 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5535 gfc_warn_intrinsic_shadow (sym, in_module, func);
5539 /* Match a function declaration. */
5541 match
5542 gfc_match_function_decl (void)
5544 char name[GFC_MAX_SYMBOL_LEN + 1];
5545 gfc_symbol *sym, *result;
5546 locus old_loc;
5547 match m;
5548 match suffix_match;
5549 match found_match; /* Status returned by match func. */
5551 if (gfc_current_state () != COMP_NONE
5552 && gfc_current_state () != COMP_INTERFACE
5553 && gfc_current_state () != COMP_CONTAINS)
5554 return MATCH_NO;
5556 gfc_clear_ts (&current_ts);
5558 old_loc = gfc_current_locus;
5560 m = gfc_match_prefix (&current_ts);
5561 if (m != MATCH_YES)
5563 gfc_current_locus = old_loc;
5564 return m;
5567 if (gfc_match ("function% %n", name) != MATCH_YES)
5569 gfc_current_locus = old_loc;
5570 return MATCH_NO;
5573 if (get_proc_name (name, &sym, false))
5574 return MATCH_ERROR;
5576 if (add_hidden_procptr_result (sym))
5577 sym = sym->result;
5579 if (current_attr.module_procedure)
5580 sym->attr.module_procedure = 1;
5582 gfc_new_block = sym;
5584 m = gfc_match_formal_arglist (sym, 0, 0);
5585 if (m == MATCH_NO)
5587 gfc_error ("Expected formal argument list in function "
5588 "definition at %C");
5589 m = MATCH_ERROR;
5590 goto cleanup;
5592 else if (m == MATCH_ERROR)
5593 goto cleanup;
5595 result = NULL;
5597 /* According to the draft, the bind(c) and result clause can
5598 come in either order after the formal_arg_list (i.e., either
5599 can be first, both can exist together or by themselves or neither
5600 one). Therefore, the match_result can't match the end of the
5601 string, and check for the bind(c) or result clause in either order. */
5602 found_match = gfc_match_eos ();
5604 /* Make sure that it isn't already declared as BIND(C). If it is, it
5605 must have been marked BIND(C) with a BIND(C) attribute and that is
5606 not allowed for procedures. */
5607 if (sym->attr.is_bind_c == 1)
5609 sym->attr.is_bind_c = 0;
5610 if (sym->old_symbol != NULL)
5611 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5612 "variables or common blocks",
5613 &(sym->old_symbol->declared_at));
5614 else
5615 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5616 "variables or common blocks", &gfc_current_locus);
5619 if (found_match != MATCH_YES)
5621 /* If we haven't found the end-of-statement, look for a suffix. */
5622 suffix_match = gfc_match_suffix (sym, &result);
5623 if (suffix_match == MATCH_YES)
5624 /* Need to get the eos now. */
5625 found_match = gfc_match_eos ();
5626 else
5627 found_match = suffix_match;
5630 if(found_match != MATCH_YES)
5631 m = MATCH_ERROR;
5632 else
5634 /* Make changes to the symbol. */
5635 m = MATCH_ERROR;
5637 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5638 goto cleanup;
5640 if (!gfc_missing_attr (&sym->attr, NULL))
5641 goto cleanup;
5643 if (!copy_prefix (&sym->attr, &sym->declared_at))
5645 if(!sym->attr.module_procedure)
5646 goto cleanup;
5647 else
5648 gfc_error_check ();
5651 /* Delay matching the function characteristics until after the
5652 specification block by signalling kind=-1. */
5653 sym->declared_at = old_loc;
5654 if (current_ts.type != BT_UNKNOWN)
5655 current_ts.kind = -1;
5656 else
5657 current_ts.kind = 0;
5659 if (result == NULL)
5661 if (current_ts.type != BT_UNKNOWN
5662 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5663 goto cleanup;
5664 sym->result = sym;
5666 else
5668 if (current_ts.type != BT_UNKNOWN
5669 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5670 goto cleanup;
5671 sym->result = result;
5675 /* Warn if this procedure has the same name as an intrinsic. */
5676 do_warn_intrinsic_shadow (sym, true);
5678 return MATCH_YES;
5681 cleanup:
5682 gfc_current_locus = old_loc;
5683 return m;
5687 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5688 pass the name of the entry, rather than the gfc_current_block name, and
5689 to return false upon finding an existing global entry. */
5691 static bool
5692 add_global_entry (const char *name, const char *binding_label, bool sub,
5693 locus *where)
5695 gfc_gsymbol *s;
5696 enum gfc_symbol_type type;
5698 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5700 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5701 name is a global identifier. */
5702 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5704 s = gfc_get_gsymbol (name);
5706 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5708 gfc_global_used (s, where);
5709 return false;
5711 else
5713 s->type = type;
5714 s->sym_name = name;
5715 s->where = *where;
5716 s->defined = 1;
5717 s->ns = gfc_current_ns;
5721 /* Don't add the symbol multiple times. */
5722 if (binding_label
5723 && (!gfc_notification_std (GFC_STD_F2008)
5724 || strcmp (name, binding_label) != 0))
5726 s = gfc_get_gsymbol (binding_label);
5728 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5730 gfc_global_used (s, where);
5731 return false;
5733 else
5735 s->type = type;
5736 s->sym_name = name;
5737 s->binding_label = binding_label;
5738 s->where = *where;
5739 s->defined = 1;
5740 s->ns = gfc_current_ns;
5744 return true;
5748 /* Match an ENTRY statement. */
5750 match
5751 gfc_match_entry (void)
5753 gfc_symbol *proc;
5754 gfc_symbol *result;
5755 gfc_symbol *entry;
5756 char name[GFC_MAX_SYMBOL_LEN + 1];
5757 gfc_compile_state state;
5758 match m;
5759 gfc_entry_list *el;
5760 locus old_loc;
5761 bool module_procedure;
5762 char peek_char;
5763 match is_bind_c;
5765 m = gfc_match_name (name);
5766 if (m != MATCH_YES)
5767 return m;
5769 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5770 return MATCH_ERROR;
5772 state = gfc_current_state ();
5773 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5775 switch (state)
5777 case COMP_PROGRAM:
5778 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5779 break;
5780 case COMP_MODULE:
5781 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5782 break;
5783 case COMP_SUBMODULE:
5784 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5785 break;
5786 case COMP_BLOCK_DATA:
5787 gfc_error ("ENTRY statement at %C cannot appear within "
5788 "a BLOCK DATA");
5789 break;
5790 case COMP_INTERFACE:
5791 gfc_error ("ENTRY statement at %C cannot appear within "
5792 "an INTERFACE");
5793 break;
5794 case COMP_DERIVED:
5795 gfc_error ("ENTRY statement at %C cannot appear within "
5796 "a DERIVED TYPE block");
5797 break;
5798 case COMP_IF:
5799 gfc_error ("ENTRY statement at %C cannot appear within "
5800 "an IF-THEN block");
5801 break;
5802 case COMP_DO:
5803 case COMP_DO_CONCURRENT:
5804 gfc_error ("ENTRY statement at %C cannot appear within "
5805 "a DO block");
5806 break;
5807 case COMP_SELECT:
5808 gfc_error ("ENTRY statement at %C cannot appear within "
5809 "a SELECT block");
5810 break;
5811 case COMP_FORALL:
5812 gfc_error ("ENTRY statement at %C cannot appear within "
5813 "a FORALL block");
5814 break;
5815 case COMP_WHERE:
5816 gfc_error ("ENTRY statement at %C cannot appear within "
5817 "a WHERE block");
5818 break;
5819 case COMP_CONTAINS:
5820 gfc_error ("ENTRY statement at %C cannot appear within "
5821 "a contained subprogram");
5822 break;
5823 default:
5824 gfc_error ("Unexpected ENTRY statement at %C");
5826 return MATCH_ERROR;
5829 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
5830 && gfc_state_stack->previous->state == COMP_INTERFACE)
5832 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
5833 return MATCH_ERROR;
5836 module_procedure = gfc_current_ns->parent != NULL
5837 && gfc_current_ns->parent->proc_name
5838 && gfc_current_ns->parent->proc_name->attr.flavor
5839 == FL_MODULE;
5841 if (gfc_current_ns->parent != NULL
5842 && gfc_current_ns->parent->proc_name
5843 && !module_procedure)
5845 gfc_error("ENTRY statement at %C cannot appear in a "
5846 "contained procedure");
5847 return MATCH_ERROR;
5850 /* Module function entries need special care in get_proc_name
5851 because previous references within the function will have
5852 created symbols attached to the current namespace. */
5853 if (get_proc_name (name, &entry,
5854 gfc_current_ns->parent != NULL
5855 && module_procedure))
5856 return MATCH_ERROR;
5858 proc = gfc_current_block ();
5860 /* Make sure that it isn't already declared as BIND(C). If it is, it
5861 must have been marked BIND(C) with a BIND(C) attribute and that is
5862 not allowed for procedures. */
5863 if (entry->attr.is_bind_c == 1)
5865 entry->attr.is_bind_c = 0;
5866 if (entry->old_symbol != NULL)
5867 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5868 "variables or common blocks",
5869 &(entry->old_symbol->declared_at));
5870 else
5871 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5872 "variables or common blocks", &gfc_current_locus);
5875 /* Check what next non-whitespace character is so we can tell if there
5876 is the required parens if we have a BIND(C). */
5877 old_loc = gfc_current_locus;
5878 gfc_gobble_whitespace ();
5879 peek_char = gfc_peek_ascii_char ();
5881 if (state == COMP_SUBROUTINE)
5883 m = gfc_match_formal_arglist (entry, 0, 1);
5884 if (m != MATCH_YES)
5885 return MATCH_ERROR;
5887 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5888 never be an internal procedure. */
5889 is_bind_c = gfc_match_bind_c (entry, true);
5890 if (is_bind_c == MATCH_ERROR)
5891 return MATCH_ERROR;
5892 if (is_bind_c == MATCH_YES)
5894 if (peek_char != '(')
5896 gfc_error ("Missing required parentheses before BIND(C) at %C");
5897 return MATCH_ERROR;
5899 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5900 &(entry->declared_at), 1))
5901 return MATCH_ERROR;
5904 if (!gfc_current_ns->parent
5905 && !add_global_entry (name, entry->binding_label, true,
5906 &old_loc))
5907 return MATCH_ERROR;
5909 /* An entry in a subroutine. */
5910 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5911 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5912 return MATCH_ERROR;
5914 else
5916 /* An entry in a function.
5917 We need to take special care because writing
5918 ENTRY f()
5920 ENTRY f
5921 is allowed, whereas
5922 ENTRY f() RESULT (r)
5923 can't be written as
5924 ENTRY f RESULT (r). */
5925 if (gfc_match_eos () == MATCH_YES)
5927 gfc_current_locus = old_loc;
5928 /* Match the empty argument list, and add the interface to
5929 the symbol. */
5930 m = gfc_match_formal_arglist (entry, 0, 1);
5932 else
5933 m = gfc_match_formal_arglist (entry, 0, 0);
5935 if (m != MATCH_YES)
5936 return MATCH_ERROR;
5938 result = NULL;
5940 if (gfc_match_eos () == MATCH_YES)
5942 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5943 || !gfc_add_function (&entry->attr, entry->name, NULL))
5944 return MATCH_ERROR;
5946 entry->result = entry;
5948 else
5950 m = gfc_match_suffix (entry, &result);
5951 if (m == MATCH_NO)
5952 gfc_syntax_error (ST_ENTRY);
5953 if (m != MATCH_YES)
5954 return MATCH_ERROR;
5956 if (result)
5958 if (!gfc_add_result (&result->attr, result->name, NULL)
5959 || !gfc_add_entry (&entry->attr, result->name, NULL)
5960 || !gfc_add_function (&entry->attr, result->name, NULL))
5961 return MATCH_ERROR;
5962 entry->result = result;
5964 else
5966 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5967 || !gfc_add_function (&entry->attr, entry->name, NULL))
5968 return MATCH_ERROR;
5969 entry->result = entry;
5973 if (!gfc_current_ns->parent
5974 && !add_global_entry (name, entry->binding_label, false,
5975 &old_loc))
5976 return MATCH_ERROR;
5979 if (gfc_match_eos () != MATCH_YES)
5981 gfc_syntax_error (ST_ENTRY);
5982 return MATCH_ERROR;
5985 entry->attr.recursive = proc->attr.recursive;
5986 entry->attr.elemental = proc->attr.elemental;
5987 entry->attr.pure = proc->attr.pure;
5989 el = gfc_get_entry_list ();
5990 el->sym = entry;
5991 el->next = gfc_current_ns->entries;
5992 gfc_current_ns->entries = el;
5993 if (el->next)
5994 el->id = el->next->id + 1;
5995 else
5996 el->id = 1;
5998 new_st.op = EXEC_ENTRY;
5999 new_st.ext.entry = el;
6001 return MATCH_YES;
6005 /* Match a subroutine statement, including optional prefixes. */
6007 match
6008 gfc_match_subroutine (void)
6010 char name[GFC_MAX_SYMBOL_LEN + 1];
6011 gfc_symbol *sym;
6012 match m;
6013 match is_bind_c;
6014 char peek_char;
6015 bool allow_binding_name;
6017 if (gfc_current_state () != COMP_NONE
6018 && gfc_current_state () != COMP_INTERFACE
6019 && gfc_current_state () != COMP_CONTAINS)
6020 return MATCH_NO;
6022 m = gfc_match_prefix (NULL);
6023 if (m != MATCH_YES)
6024 return m;
6026 m = gfc_match ("subroutine% %n", name);
6027 if (m != MATCH_YES)
6028 return m;
6030 if (get_proc_name (name, &sym, false))
6031 return MATCH_ERROR;
6033 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
6034 the symbol existed before. */
6035 sym->declared_at = gfc_current_locus;
6037 if (current_attr.module_procedure)
6038 sym->attr.module_procedure = 1;
6040 if (add_hidden_procptr_result (sym))
6041 sym = sym->result;
6043 gfc_new_block = sym;
6045 /* Check what next non-whitespace character is so we can tell if there
6046 is the required parens if we have a BIND(C). */
6047 gfc_gobble_whitespace ();
6048 peek_char = gfc_peek_ascii_char ();
6050 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
6051 return MATCH_ERROR;
6053 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
6054 return MATCH_ERROR;
6056 /* Make sure that it isn't already declared as BIND(C). If it is, it
6057 must have been marked BIND(C) with a BIND(C) attribute and that is
6058 not allowed for procedures. */
6059 if (sym->attr.is_bind_c == 1)
6061 sym->attr.is_bind_c = 0;
6062 if (sym->old_symbol != NULL)
6063 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6064 "variables or common blocks",
6065 &(sym->old_symbol->declared_at));
6066 else
6067 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6068 "variables or common blocks", &gfc_current_locus);
6071 /* C binding names are not allowed for internal procedures. */
6072 if (gfc_current_state () == COMP_CONTAINS
6073 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6074 allow_binding_name = false;
6075 else
6076 allow_binding_name = true;
6078 /* Here, we are just checking if it has the bind(c) attribute, and if
6079 so, then we need to make sure it's all correct. If it doesn't,
6080 we still need to continue matching the rest of the subroutine line. */
6081 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6082 if (is_bind_c == MATCH_ERROR)
6084 /* There was an attempt at the bind(c), but it was wrong. An
6085 error message should have been printed w/in the gfc_match_bind_c
6086 so here we'll just return the MATCH_ERROR. */
6087 return MATCH_ERROR;
6090 if (is_bind_c == MATCH_YES)
6092 /* The following is allowed in the Fortran 2008 draft. */
6093 if (gfc_current_state () == COMP_CONTAINS
6094 && sym->ns->proc_name->attr.flavor != FL_MODULE
6095 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6096 "at %L may not be specified for an internal "
6097 "procedure", &gfc_current_locus))
6098 return MATCH_ERROR;
6100 if (peek_char != '(')
6102 gfc_error ("Missing required parentheses before BIND(C) at %C");
6103 return MATCH_ERROR;
6105 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
6106 &(sym->declared_at), 1))
6107 return MATCH_ERROR;
6110 if (gfc_match_eos () != MATCH_YES)
6112 gfc_syntax_error (ST_SUBROUTINE);
6113 return MATCH_ERROR;
6116 if (!copy_prefix (&sym->attr, &sym->declared_at))
6118 if(!sym->attr.module_procedure)
6119 return MATCH_ERROR;
6120 else
6121 gfc_error_check ();
6124 /* Warn if it has the same name as an intrinsic. */
6125 do_warn_intrinsic_shadow (sym, false);
6127 return MATCH_YES;
6131 /* Check that the NAME identifier in a BIND attribute or statement
6132 is conform to C identifier rules. */
6134 match
6135 check_bind_name_identifier (char **name)
6137 char *n = *name, *p;
6139 /* Remove leading spaces. */
6140 while (*n == ' ')
6141 n++;
6143 /* On an empty string, free memory and set name to NULL. */
6144 if (*n == '\0')
6146 free (*name);
6147 *name = NULL;
6148 return MATCH_YES;
6151 /* Remove trailing spaces. */
6152 p = n + strlen(n) - 1;
6153 while (*p == ' ')
6154 *(p--) = '\0';
6156 /* Insert the identifier into the symbol table. */
6157 p = xstrdup (n);
6158 free (*name);
6159 *name = p;
6161 /* Now check that identifier is valid under C rules. */
6162 if (ISDIGIT (*p))
6164 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6165 return MATCH_ERROR;
6168 for (; *p; p++)
6169 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6171 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6172 return MATCH_ERROR;
6175 return MATCH_YES;
6179 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6180 given, and set the binding label in either the given symbol (if not
6181 NULL), or in the current_ts. The symbol may be NULL because we may
6182 encounter the BIND(C) before the declaration itself. Return
6183 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6184 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6185 or MATCH_YES if the specifier was correct and the binding label and
6186 bind(c) fields were set correctly for the given symbol or the
6187 current_ts. If allow_binding_name is false, no binding name may be
6188 given. */
6190 match
6191 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6193 char *binding_label = NULL;
6194 gfc_expr *e = NULL;
6196 /* Initialize the flag that specifies whether we encountered a NAME=
6197 specifier or not. */
6198 has_name_equals = 0;
6200 /* This much we have to be able to match, in this order, if
6201 there is a bind(c) label. */
6202 if (gfc_match (" bind ( c ") != MATCH_YES)
6203 return MATCH_NO;
6205 /* Now see if there is a binding label, or if we've reached the
6206 end of the bind(c) attribute without one. */
6207 if (gfc_match_char (',') == MATCH_YES)
6209 if (gfc_match (" name = ") != MATCH_YES)
6211 gfc_error ("Syntax error in NAME= specifier for binding label "
6212 "at %C");
6213 /* should give an error message here */
6214 return MATCH_ERROR;
6217 has_name_equals = 1;
6219 if (gfc_match_init_expr (&e) != MATCH_YES)
6221 gfc_free_expr (e);
6222 return MATCH_ERROR;
6225 if (!gfc_simplify_expr(e, 0))
6227 gfc_error ("NAME= specifier at %C should be a constant expression");
6228 gfc_free_expr (e);
6229 return MATCH_ERROR;
6232 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6233 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6235 gfc_error ("NAME= specifier at %C should be a scalar of "
6236 "default character kind");
6237 gfc_free_expr(e);
6238 return MATCH_ERROR;
6241 // Get a C string from the Fortran string constant
6242 binding_label = gfc_widechar_to_char (e->value.character.string,
6243 e->value.character.length);
6244 gfc_free_expr(e);
6246 // Check that it is valid (old gfc_match_name_C)
6247 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6248 return MATCH_ERROR;
6251 /* Get the required right paren. */
6252 if (gfc_match_char (')') != MATCH_YES)
6254 gfc_error ("Missing closing paren for binding label at %C");
6255 return MATCH_ERROR;
6258 if (has_name_equals && !allow_binding_name)
6260 gfc_error ("No binding name is allowed in BIND(C) at %C");
6261 return MATCH_ERROR;
6264 if (has_name_equals && sym != NULL && sym->attr.dummy)
6266 gfc_error ("For dummy procedure %s, no binding name is "
6267 "allowed in BIND(C) at %C", sym->name);
6268 return MATCH_ERROR;
6272 /* Save the binding label to the symbol. If sym is null, we're
6273 probably matching the typespec attributes of a declaration and
6274 haven't gotten the name yet, and therefore, no symbol yet. */
6275 if (binding_label)
6277 if (sym != NULL)
6278 sym->binding_label = binding_label;
6279 else
6280 curr_binding_label = binding_label;
6282 else if (allow_binding_name)
6284 /* No binding label, but if symbol isn't null, we
6285 can set the label for it here.
6286 If name="" or allow_binding_name is false, no C binding name is
6287 created. */
6288 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6289 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6292 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6293 && current_interface.type == INTERFACE_ABSTRACT)
6295 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6296 return MATCH_ERROR;
6299 return MATCH_YES;
6303 /* Return nonzero if we're currently compiling a contained procedure. */
6305 static int
6306 contained_procedure (void)
6308 gfc_state_data *s = gfc_state_stack;
6310 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6311 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6312 return 1;
6314 return 0;
6317 /* Set the kind of each enumerator. The kind is selected such that it is
6318 interoperable with the corresponding C enumeration type, making
6319 sure that -fshort-enums is honored. */
6321 static void
6322 set_enum_kind(void)
6324 enumerator_history *current_history = NULL;
6325 int kind;
6326 int i;
6328 if (max_enum == NULL || enum_history == NULL)
6329 return;
6331 if (!flag_short_enums)
6332 return;
6334 i = 0;
6337 kind = gfc_integer_kinds[i++].kind;
6339 while (kind < gfc_c_int_kind
6340 && gfc_check_integer_range (max_enum->initializer->value.integer,
6341 kind) != ARITH_OK);
6343 current_history = enum_history;
6344 while (current_history != NULL)
6346 current_history->sym->ts.kind = kind;
6347 current_history = current_history->next;
6352 /* Match any of the various end-block statements. Returns the type of
6353 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6354 and END BLOCK statements cannot be replaced by a single END statement. */
6356 match
6357 gfc_match_end (gfc_statement *st)
6359 char name[GFC_MAX_SYMBOL_LEN + 1];
6360 gfc_compile_state state;
6361 locus old_loc;
6362 const char *block_name;
6363 const char *target;
6364 int eos_ok;
6365 match m;
6366 gfc_namespace *parent_ns, *ns, *prev_ns;
6367 gfc_namespace **nsp;
6368 bool abreviated_modproc_decl;
6369 bool got_matching_end = false;
6371 old_loc = gfc_current_locus;
6372 if (gfc_match ("end") != MATCH_YES)
6373 return MATCH_NO;
6375 state = gfc_current_state ();
6376 block_name = gfc_current_block () == NULL
6377 ? NULL : gfc_current_block ()->name;
6379 switch (state)
6381 case COMP_ASSOCIATE:
6382 case COMP_BLOCK:
6383 if (!strncmp (block_name, "block@", strlen("block@")))
6384 block_name = NULL;
6385 break;
6387 case COMP_CONTAINS:
6388 case COMP_DERIVED_CONTAINS:
6389 state = gfc_state_stack->previous->state;
6390 block_name = gfc_state_stack->previous->sym == NULL
6391 ? NULL : gfc_state_stack->previous->sym->name;
6392 break;
6394 default:
6395 break;
6398 abreviated_modproc_decl
6399 = gfc_current_block ()
6400 && gfc_current_block ()->abr_modproc_decl;
6402 switch (state)
6404 case COMP_NONE:
6405 case COMP_PROGRAM:
6406 *st = ST_END_PROGRAM;
6407 target = " program";
6408 eos_ok = 1;
6409 break;
6411 case COMP_SUBROUTINE:
6412 *st = ST_END_SUBROUTINE;
6413 if (!abreviated_modproc_decl)
6414 target = " subroutine";
6415 else
6416 target = " procedure";
6417 eos_ok = !contained_procedure ();
6418 break;
6420 case COMP_FUNCTION:
6421 *st = ST_END_FUNCTION;
6422 if (!abreviated_modproc_decl)
6423 target = " function";
6424 else
6425 target = " procedure";
6426 eos_ok = !contained_procedure ();
6427 break;
6429 case COMP_BLOCK_DATA:
6430 *st = ST_END_BLOCK_DATA;
6431 target = " block data";
6432 eos_ok = 1;
6433 break;
6435 case COMP_MODULE:
6436 *st = ST_END_MODULE;
6437 target = " module";
6438 eos_ok = 1;
6439 break;
6441 case COMP_SUBMODULE:
6442 *st = ST_END_SUBMODULE;
6443 target = " submodule";
6444 eos_ok = 1;
6445 break;
6447 case COMP_INTERFACE:
6448 *st = ST_END_INTERFACE;
6449 target = " interface";
6450 eos_ok = 0;
6451 break;
6453 case COMP_DERIVED:
6454 case COMP_DERIVED_CONTAINS:
6455 *st = ST_END_TYPE;
6456 target = " type";
6457 eos_ok = 0;
6458 break;
6460 case COMP_ASSOCIATE:
6461 *st = ST_END_ASSOCIATE;
6462 target = " associate";
6463 eos_ok = 0;
6464 break;
6466 case COMP_BLOCK:
6467 *st = ST_END_BLOCK;
6468 target = " block";
6469 eos_ok = 0;
6470 break;
6472 case COMP_IF:
6473 *st = ST_ENDIF;
6474 target = " if";
6475 eos_ok = 0;
6476 break;
6478 case COMP_DO:
6479 case COMP_DO_CONCURRENT:
6480 *st = ST_ENDDO;
6481 target = " do";
6482 eos_ok = 0;
6483 break;
6485 case COMP_CRITICAL:
6486 *st = ST_END_CRITICAL;
6487 target = " critical";
6488 eos_ok = 0;
6489 break;
6491 case COMP_SELECT:
6492 case COMP_SELECT_TYPE:
6493 *st = ST_END_SELECT;
6494 target = " select";
6495 eos_ok = 0;
6496 break;
6498 case COMP_FORALL:
6499 *st = ST_END_FORALL;
6500 target = " forall";
6501 eos_ok = 0;
6502 break;
6504 case COMP_WHERE:
6505 *st = ST_END_WHERE;
6506 target = " where";
6507 eos_ok = 0;
6508 break;
6510 case COMP_ENUM:
6511 *st = ST_END_ENUM;
6512 target = " enum";
6513 eos_ok = 0;
6514 last_initializer = NULL;
6515 set_enum_kind ();
6516 gfc_free_enum_history ();
6517 break;
6519 default:
6520 gfc_error ("Unexpected END statement at %C");
6521 goto cleanup;
6524 old_loc = gfc_current_locus;
6525 if (gfc_match_eos () == MATCH_YES)
6527 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6529 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6530 "instead of %s statement at %L",
6531 abreviated_modproc_decl ? "END PROCEDURE"
6532 : gfc_ascii_statement(*st), &old_loc))
6533 goto cleanup;
6535 else if (!eos_ok)
6537 /* We would have required END [something]. */
6538 gfc_error ("%s statement expected at %L",
6539 gfc_ascii_statement (*st), &old_loc);
6540 goto cleanup;
6543 return MATCH_YES;
6546 /* Verify that we've got the sort of end-block that we're expecting. */
6547 if (gfc_match (target) != MATCH_YES)
6549 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6550 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6551 goto cleanup;
6553 else
6554 got_matching_end = true;
6556 old_loc = gfc_current_locus;
6557 /* If we're at the end, make sure a block name wasn't required. */
6558 if (gfc_match_eos () == MATCH_YES)
6561 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6562 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6563 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6564 return MATCH_YES;
6566 if (!block_name)
6567 return MATCH_YES;
6569 gfc_error ("Expected block name of %qs in %s statement at %L",
6570 block_name, gfc_ascii_statement (*st), &old_loc);
6572 return MATCH_ERROR;
6575 /* END INTERFACE has a special handler for its several possible endings. */
6576 if (*st == ST_END_INTERFACE)
6577 return gfc_match_end_interface ();
6579 /* We haven't hit the end of statement, so what is left must be an
6580 end-name. */
6581 m = gfc_match_space ();
6582 if (m == MATCH_YES)
6583 m = gfc_match_name (name);
6585 if (m == MATCH_NO)
6586 gfc_error ("Expected terminating name at %C");
6587 if (m != MATCH_YES)
6588 goto cleanup;
6590 if (block_name == NULL)
6591 goto syntax;
6593 /* We have to pick out the declared submodule name from the composite
6594 required by F2008:11.2.3 para 2, which ends in the declared name. */
6595 if (state == COMP_SUBMODULE)
6596 block_name = strchr (block_name, '.') + 1;
6598 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6600 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6601 gfc_ascii_statement (*st));
6602 goto cleanup;
6604 /* Procedure pointer as function result. */
6605 else if (strcmp (block_name, "ppr@") == 0
6606 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6608 gfc_error ("Expected label %qs for %s statement at %C",
6609 gfc_current_block ()->ns->proc_name->name,
6610 gfc_ascii_statement (*st));
6611 goto cleanup;
6614 if (gfc_match_eos () == MATCH_YES)
6615 return MATCH_YES;
6617 syntax:
6618 gfc_syntax_error (*st);
6620 cleanup:
6621 gfc_current_locus = old_loc;
6623 /* If we are missing an END BLOCK, we created a half-ready namespace.
6624 Remove it from the parent namespace's sibling list. */
6626 while (state == COMP_BLOCK && !got_matching_end)
6628 parent_ns = gfc_current_ns->parent;
6630 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6632 prev_ns = NULL;
6633 ns = *nsp;
6634 while (ns)
6636 if (ns == gfc_current_ns)
6638 if (prev_ns == NULL)
6639 *nsp = NULL;
6640 else
6641 prev_ns->sibling = ns->sibling;
6643 prev_ns = ns;
6644 ns = ns->sibling;
6647 gfc_free_namespace (gfc_current_ns);
6648 gfc_current_ns = parent_ns;
6649 gfc_state_stack = gfc_state_stack->previous;
6650 state = gfc_current_state ();
6653 return MATCH_ERROR;
6658 /***************** Attribute declaration statements ****************/
6660 /* Set the attribute of a single variable. */
6662 static match
6663 attr_decl1 (void)
6665 char name[GFC_MAX_SYMBOL_LEN + 1];
6666 gfc_array_spec *as;
6668 /* Workaround -Wmaybe-uninitialized false positive during
6669 profiledbootstrap by initializing them. */
6670 gfc_symbol *sym = NULL;
6671 locus var_locus;
6672 match m;
6674 as = NULL;
6676 m = gfc_match_name (name);
6677 if (m != MATCH_YES)
6678 goto cleanup;
6680 if (find_special (name, &sym, false))
6681 return MATCH_ERROR;
6683 if (!check_function_name (name))
6685 m = MATCH_ERROR;
6686 goto cleanup;
6689 var_locus = gfc_current_locus;
6691 /* Deal with possible array specification for certain attributes. */
6692 if (current_attr.dimension
6693 || current_attr.codimension
6694 || current_attr.allocatable
6695 || current_attr.pointer
6696 || current_attr.target)
6698 m = gfc_match_array_spec (&as, !current_attr.codimension,
6699 !current_attr.dimension
6700 && !current_attr.pointer
6701 && !current_attr.target);
6702 if (m == MATCH_ERROR)
6703 goto cleanup;
6705 if (current_attr.dimension && m == MATCH_NO)
6707 gfc_error ("Missing array specification at %L in DIMENSION "
6708 "statement", &var_locus);
6709 m = MATCH_ERROR;
6710 goto cleanup;
6713 if (current_attr.dimension && sym->value)
6715 gfc_error ("Dimensions specified for %s at %L after its "
6716 "initialisation", sym->name, &var_locus);
6717 m = MATCH_ERROR;
6718 goto cleanup;
6721 if (current_attr.codimension && m == MATCH_NO)
6723 gfc_error ("Missing array specification at %L in CODIMENSION "
6724 "statement", &var_locus);
6725 m = MATCH_ERROR;
6726 goto cleanup;
6729 if ((current_attr.allocatable || current_attr.pointer)
6730 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6732 gfc_error ("Array specification must be deferred at %L", &var_locus);
6733 m = MATCH_ERROR;
6734 goto cleanup;
6738 /* Update symbol table. DIMENSION attribute is set in
6739 gfc_set_array_spec(). For CLASS variables, this must be applied
6740 to the first component, or '_data' field. */
6741 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6743 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6745 m = MATCH_ERROR;
6746 goto cleanup;
6749 else
6751 if (current_attr.dimension == 0 && current_attr.codimension == 0
6752 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6754 m = MATCH_ERROR;
6755 goto cleanup;
6759 if (sym->ts.type == BT_CLASS
6760 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6762 m = MATCH_ERROR;
6763 goto cleanup;
6766 if (!gfc_set_array_spec (sym, as, &var_locus))
6768 m = MATCH_ERROR;
6769 goto cleanup;
6772 if (sym->attr.cray_pointee && sym->as != NULL)
6774 /* Fix the array spec. */
6775 m = gfc_mod_pointee_as (sym->as);
6776 if (m == MATCH_ERROR)
6777 goto cleanup;
6780 if (!gfc_add_attribute (&sym->attr, &var_locus))
6782 m = MATCH_ERROR;
6783 goto cleanup;
6786 if ((current_attr.external || current_attr.intrinsic)
6787 && sym->attr.flavor != FL_PROCEDURE
6788 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6790 m = MATCH_ERROR;
6791 goto cleanup;
6794 add_hidden_procptr_result (sym);
6796 return MATCH_YES;
6798 cleanup:
6799 gfc_free_array_spec (as);
6800 return m;
6804 /* Generic attribute declaration subroutine. Used for attributes that
6805 just have a list of names. */
6807 static match
6808 attr_decl (void)
6810 match m;
6812 /* Gobble the optional double colon, by simply ignoring the result
6813 of gfc_match(). */
6814 gfc_match (" ::");
6816 for (;;)
6818 m = attr_decl1 ();
6819 if (m != MATCH_YES)
6820 break;
6822 if (gfc_match_eos () == MATCH_YES)
6824 m = MATCH_YES;
6825 break;
6828 if (gfc_match_char (',') != MATCH_YES)
6830 gfc_error ("Unexpected character in variable list at %C");
6831 m = MATCH_ERROR;
6832 break;
6836 return m;
6840 /* This routine matches Cray Pointer declarations of the form:
6841 pointer ( <pointer>, <pointee> )
6843 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6844 The pointer, if already declared, should be an integer. Otherwise, we
6845 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6846 be either a scalar, or an array declaration. No space is allocated for
6847 the pointee. For the statement
6848 pointer (ipt, ar(10))
6849 any subsequent uses of ar will be translated (in C-notation) as
6850 ar(i) => ((<type> *) ipt)(i)
6851 After gimplification, pointee variable will disappear in the code. */
6853 static match
6854 cray_pointer_decl (void)
6856 match m;
6857 gfc_array_spec *as = NULL;
6858 gfc_symbol *cptr; /* Pointer symbol. */
6859 gfc_symbol *cpte; /* Pointee symbol. */
6860 locus var_locus;
6861 bool done = false;
6863 while (!done)
6865 if (gfc_match_char ('(') != MATCH_YES)
6867 gfc_error ("Expected %<(%> at %C");
6868 return MATCH_ERROR;
6871 /* Match pointer. */
6872 var_locus = gfc_current_locus;
6873 gfc_clear_attr (&current_attr);
6874 gfc_add_cray_pointer (&current_attr, &var_locus);
6875 current_ts.type = BT_INTEGER;
6876 current_ts.kind = gfc_index_integer_kind;
6878 m = gfc_match_symbol (&cptr, 0);
6879 if (m != MATCH_YES)
6881 gfc_error ("Expected variable name at %C");
6882 return m;
6885 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6886 return MATCH_ERROR;
6888 gfc_set_sym_referenced (cptr);
6890 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6892 cptr->ts.type = BT_INTEGER;
6893 cptr->ts.kind = gfc_index_integer_kind;
6895 else if (cptr->ts.type != BT_INTEGER)
6897 gfc_error ("Cray pointer at %C must be an integer");
6898 return MATCH_ERROR;
6900 else if (cptr->ts.kind < gfc_index_integer_kind)
6901 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6902 " memory addresses require %d bytes",
6903 cptr->ts.kind, gfc_index_integer_kind);
6905 if (gfc_match_char (',') != MATCH_YES)
6907 gfc_error ("Expected \",\" at %C");
6908 return MATCH_ERROR;
6911 /* Match Pointee. */
6912 var_locus = gfc_current_locus;
6913 gfc_clear_attr (&current_attr);
6914 gfc_add_cray_pointee (&current_attr, &var_locus);
6915 current_ts.type = BT_UNKNOWN;
6916 current_ts.kind = 0;
6918 m = gfc_match_symbol (&cpte, 0);
6919 if (m != MATCH_YES)
6921 gfc_error ("Expected variable name at %C");
6922 return m;
6925 /* Check for an optional array spec. */
6926 m = gfc_match_array_spec (&as, true, false);
6927 if (m == MATCH_ERROR)
6929 gfc_free_array_spec (as);
6930 return m;
6932 else if (m == MATCH_NO)
6934 gfc_free_array_spec (as);
6935 as = NULL;
6938 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6939 return MATCH_ERROR;
6941 gfc_set_sym_referenced (cpte);
6943 if (cpte->as == NULL)
6945 if (!gfc_set_array_spec (cpte, as, &var_locus))
6946 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6948 else if (as != NULL)
6950 gfc_error ("Duplicate array spec for Cray pointee at %C");
6951 gfc_free_array_spec (as);
6952 return MATCH_ERROR;
6955 as = NULL;
6957 if (cpte->as != NULL)
6959 /* Fix array spec. */
6960 m = gfc_mod_pointee_as (cpte->as);
6961 if (m == MATCH_ERROR)
6962 return m;
6965 /* Point the Pointee at the Pointer. */
6966 cpte->cp_pointer = cptr;
6968 if (gfc_match_char (')') != MATCH_YES)
6970 gfc_error ("Expected \")\" at %C");
6971 return MATCH_ERROR;
6973 m = gfc_match_char (',');
6974 if (m != MATCH_YES)
6975 done = true; /* Stop searching for more declarations. */
6979 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6980 || gfc_match_eos () != MATCH_YES)
6982 gfc_error ("Expected %<,%> or end of statement at %C");
6983 return MATCH_ERROR;
6985 return MATCH_YES;
6989 match
6990 gfc_match_external (void)
6993 gfc_clear_attr (&current_attr);
6994 current_attr.external = 1;
6996 return attr_decl ();
7000 match
7001 gfc_match_intent (void)
7003 sym_intent intent;
7005 /* This is not allowed within a BLOCK construct! */
7006 if (gfc_current_state () == COMP_BLOCK)
7008 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
7009 return MATCH_ERROR;
7012 intent = match_intent_spec ();
7013 if (intent == INTENT_UNKNOWN)
7014 return MATCH_ERROR;
7016 gfc_clear_attr (&current_attr);
7017 current_attr.intent = intent;
7019 return attr_decl ();
7023 match
7024 gfc_match_intrinsic (void)
7027 gfc_clear_attr (&current_attr);
7028 current_attr.intrinsic = 1;
7030 return attr_decl ();
7034 match
7035 gfc_match_optional (void)
7037 /* This is not allowed within a BLOCK construct! */
7038 if (gfc_current_state () == COMP_BLOCK)
7040 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
7041 return MATCH_ERROR;
7044 gfc_clear_attr (&current_attr);
7045 current_attr.optional = 1;
7047 return attr_decl ();
7051 match
7052 gfc_match_pointer (void)
7054 gfc_gobble_whitespace ();
7055 if (gfc_peek_ascii_char () == '(')
7057 if (!flag_cray_pointer)
7059 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7060 "flag");
7061 return MATCH_ERROR;
7063 return cray_pointer_decl ();
7065 else
7067 gfc_clear_attr (&current_attr);
7068 current_attr.pointer = 1;
7070 return attr_decl ();
7075 match
7076 gfc_match_allocatable (void)
7078 gfc_clear_attr (&current_attr);
7079 current_attr.allocatable = 1;
7081 return attr_decl ();
7085 match
7086 gfc_match_codimension (void)
7088 gfc_clear_attr (&current_attr);
7089 current_attr.codimension = 1;
7091 return attr_decl ();
7095 match
7096 gfc_match_contiguous (void)
7098 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
7099 return MATCH_ERROR;
7101 gfc_clear_attr (&current_attr);
7102 current_attr.contiguous = 1;
7104 return attr_decl ();
7108 match
7109 gfc_match_dimension (void)
7111 gfc_clear_attr (&current_attr);
7112 current_attr.dimension = 1;
7114 return attr_decl ();
7118 match
7119 gfc_match_target (void)
7121 gfc_clear_attr (&current_attr);
7122 current_attr.target = 1;
7124 return attr_decl ();
7128 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7129 statement. */
7131 static match
7132 access_attr_decl (gfc_statement st)
7134 char name[GFC_MAX_SYMBOL_LEN + 1];
7135 interface_type type;
7136 gfc_user_op *uop;
7137 gfc_symbol *sym, *dt_sym;
7138 gfc_intrinsic_op op;
7139 match m;
7141 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7142 goto done;
7144 for (;;)
7146 m = gfc_match_generic_spec (&type, name, &op);
7147 if (m == MATCH_NO)
7148 goto syntax;
7149 if (m == MATCH_ERROR)
7150 return MATCH_ERROR;
7152 switch (type)
7154 case INTERFACE_NAMELESS:
7155 case INTERFACE_ABSTRACT:
7156 goto syntax;
7158 case INTERFACE_GENERIC:
7159 if (gfc_get_symbol (name, NULL, &sym))
7160 goto done;
7162 if (!gfc_add_access (&sym->attr,
7163 (st == ST_PUBLIC)
7164 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7165 sym->name, NULL))
7166 return MATCH_ERROR;
7168 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7169 && !gfc_add_access (&dt_sym->attr,
7170 (st == ST_PUBLIC)
7171 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7172 sym->name, NULL))
7173 return MATCH_ERROR;
7175 break;
7177 case INTERFACE_INTRINSIC_OP:
7178 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7180 gfc_intrinsic_op other_op;
7182 gfc_current_ns->operator_access[op] =
7183 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7185 /* Handle the case if there is another op with the same
7186 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7187 other_op = gfc_equivalent_op (op);
7189 if (other_op != INTRINSIC_NONE)
7190 gfc_current_ns->operator_access[other_op] =
7191 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7194 else
7196 gfc_error ("Access specification of the %s operator at %C has "
7197 "already been specified", gfc_op2string (op));
7198 goto done;
7201 break;
7203 case INTERFACE_USER_OP:
7204 uop = gfc_get_uop (name);
7206 if (uop->access == ACCESS_UNKNOWN)
7208 uop->access = (st == ST_PUBLIC)
7209 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7211 else
7213 gfc_error ("Access specification of the .%s. operator at %C "
7214 "has already been specified", sym->name);
7215 goto done;
7218 break;
7221 if (gfc_match_char (',') == MATCH_NO)
7222 break;
7225 if (gfc_match_eos () != MATCH_YES)
7226 goto syntax;
7227 return MATCH_YES;
7229 syntax:
7230 gfc_syntax_error (st);
7232 done:
7233 return MATCH_ERROR;
7237 match
7238 gfc_match_protected (void)
7240 gfc_symbol *sym;
7241 match m;
7243 if (!gfc_current_ns->proc_name
7244 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7246 gfc_error ("PROTECTED at %C only allowed in specification "
7247 "part of a module");
7248 return MATCH_ERROR;
7252 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7253 return MATCH_ERROR;
7255 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7257 return MATCH_ERROR;
7260 if (gfc_match_eos () == MATCH_YES)
7261 goto syntax;
7263 for(;;)
7265 m = gfc_match_symbol (&sym, 0);
7266 switch (m)
7268 case MATCH_YES:
7269 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7270 return MATCH_ERROR;
7271 goto next_item;
7273 case MATCH_NO:
7274 break;
7276 case MATCH_ERROR:
7277 return MATCH_ERROR;
7280 next_item:
7281 if (gfc_match_eos () == MATCH_YES)
7282 break;
7283 if (gfc_match_char (',') != MATCH_YES)
7284 goto syntax;
7287 return MATCH_YES;
7289 syntax:
7290 gfc_error ("Syntax error in PROTECTED statement at %C");
7291 return MATCH_ERROR;
7295 /* The PRIVATE statement is a bit weird in that it can be an attribute
7296 declaration, but also works as a standalone statement inside of a
7297 type declaration or a module. */
7299 match
7300 gfc_match_private (gfc_statement *st)
7303 if (gfc_match ("private") != MATCH_YES)
7304 return MATCH_NO;
7306 if (gfc_current_state () != COMP_MODULE
7307 && !(gfc_current_state () == COMP_DERIVED
7308 && gfc_state_stack->previous
7309 && gfc_state_stack->previous->state == COMP_MODULE)
7310 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7311 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7312 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7314 gfc_error ("PRIVATE statement at %C is only allowed in the "
7315 "specification part of a module");
7316 return MATCH_ERROR;
7319 if (gfc_current_state () == COMP_DERIVED)
7321 if (gfc_match_eos () == MATCH_YES)
7323 *st = ST_PRIVATE;
7324 return MATCH_YES;
7327 gfc_syntax_error (ST_PRIVATE);
7328 return MATCH_ERROR;
7331 if (gfc_match_eos () == MATCH_YES)
7333 *st = ST_PRIVATE;
7334 return MATCH_YES;
7337 *st = ST_ATTR_DECL;
7338 return access_attr_decl (ST_PRIVATE);
7342 match
7343 gfc_match_public (gfc_statement *st)
7346 if (gfc_match ("public") != MATCH_YES)
7347 return MATCH_NO;
7349 if (gfc_current_state () != COMP_MODULE)
7351 gfc_error ("PUBLIC statement at %C is only allowed in the "
7352 "specification part of a module");
7353 return MATCH_ERROR;
7356 if (gfc_match_eos () == MATCH_YES)
7358 *st = ST_PUBLIC;
7359 return MATCH_YES;
7362 *st = ST_ATTR_DECL;
7363 return access_attr_decl (ST_PUBLIC);
7367 /* Workhorse for gfc_match_parameter. */
7369 static match
7370 do_parm (void)
7372 gfc_symbol *sym;
7373 gfc_expr *init;
7374 match m;
7375 bool t;
7377 m = gfc_match_symbol (&sym, 0);
7378 if (m == MATCH_NO)
7379 gfc_error ("Expected variable name at %C in PARAMETER statement");
7381 if (m != MATCH_YES)
7382 return m;
7384 if (gfc_match_char ('=') == MATCH_NO)
7386 gfc_error ("Expected = sign in PARAMETER statement at %C");
7387 return MATCH_ERROR;
7390 m = gfc_match_init_expr (&init);
7391 if (m == MATCH_NO)
7392 gfc_error ("Expected expression at %C in PARAMETER statement");
7393 if (m != MATCH_YES)
7394 return m;
7396 if (sym->ts.type == BT_UNKNOWN
7397 && !gfc_set_default_type (sym, 1, NULL))
7399 m = MATCH_ERROR;
7400 goto cleanup;
7403 if (!gfc_check_assign_symbol (sym, NULL, init)
7404 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7406 m = MATCH_ERROR;
7407 goto cleanup;
7410 if (sym->value)
7412 gfc_error ("Initializing already initialized variable at %C");
7413 m = MATCH_ERROR;
7414 goto cleanup;
7417 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7418 return (t) ? MATCH_YES : MATCH_ERROR;
7420 cleanup:
7421 gfc_free_expr (init);
7422 return m;
7426 /* Match a parameter statement, with the weird syntax that these have. */
7428 match
7429 gfc_match_parameter (void)
7431 match m;
7433 if (gfc_match_char ('(') == MATCH_NO)
7434 return MATCH_NO;
7436 for (;;)
7438 m = do_parm ();
7439 if (m != MATCH_YES)
7440 break;
7442 if (gfc_match (" )%t") == MATCH_YES)
7443 break;
7445 if (gfc_match_char (',') != MATCH_YES)
7447 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7448 m = MATCH_ERROR;
7449 break;
7453 return m;
7457 /* Save statements have a special syntax. */
7459 match
7460 gfc_match_save (void)
7462 char n[GFC_MAX_SYMBOL_LEN+1];
7463 gfc_common_head *c;
7464 gfc_symbol *sym;
7465 match m;
7467 if (gfc_match_eos () == MATCH_YES)
7469 if (gfc_current_ns->seen_save)
7471 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7472 "follows previous SAVE statement"))
7473 return MATCH_ERROR;
7476 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7477 return MATCH_YES;
7480 if (gfc_current_ns->save_all)
7482 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7483 "blanket SAVE statement"))
7484 return MATCH_ERROR;
7487 gfc_match (" ::");
7489 for (;;)
7491 m = gfc_match_symbol (&sym, 0);
7492 switch (m)
7494 case MATCH_YES:
7495 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7496 &gfc_current_locus))
7497 return MATCH_ERROR;
7498 goto next_item;
7500 case MATCH_NO:
7501 break;
7503 case MATCH_ERROR:
7504 return MATCH_ERROR;
7507 m = gfc_match (" / %n /", &n);
7508 if (m == MATCH_ERROR)
7509 return MATCH_ERROR;
7510 if (m == MATCH_NO)
7511 goto syntax;
7513 c = gfc_get_common (n, 0);
7514 c->saved = 1;
7516 gfc_current_ns->seen_save = 1;
7518 next_item:
7519 if (gfc_match_eos () == MATCH_YES)
7520 break;
7521 if (gfc_match_char (',') != MATCH_YES)
7522 goto syntax;
7525 return MATCH_YES;
7527 syntax:
7528 gfc_error ("Syntax error in SAVE statement at %C");
7529 return MATCH_ERROR;
7533 match
7534 gfc_match_value (void)
7536 gfc_symbol *sym;
7537 match m;
7539 /* This is not allowed within a BLOCK construct! */
7540 if (gfc_current_state () == COMP_BLOCK)
7542 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7543 return MATCH_ERROR;
7546 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7547 return MATCH_ERROR;
7549 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7551 return MATCH_ERROR;
7554 if (gfc_match_eos () == MATCH_YES)
7555 goto syntax;
7557 for(;;)
7559 m = gfc_match_symbol (&sym, 0);
7560 switch (m)
7562 case MATCH_YES:
7563 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7564 return MATCH_ERROR;
7565 goto next_item;
7567 case MATCH_NO:
7568 break;
7570 case MATCH_ERROR:
7571 return MATCH_ERROR;
7574 next_item:
7575 if (gfc_match_eos () == MATCH_YES)
7576 break;
7577 if (gfc_match_char (',') != MATCH_YES)
7578 goto syntax;
7581 return MATCH_YES;
7583 syntax:
7584 gfc_error ("Syntax error in VALUE statement at %C");
7585 return MATCH_ERROR;
7589 match
7590 gfc_match_volatile (void)
7592 gfc_symbol *sym;
7593 match m;
7595 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7596 return MATCH_ERROR;
7598 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7600 return MATCH_ERROR;
7603 if (gfc_match_eos () == MATCH_YES)
7604 goto syntax;
7606 for(;;)
7608 /* VOLATILE is special because it can be added to host-associated
7609 symbols locally. Except for coarrays. */
7610 m = gfc_match_symbol (&sym, 1);
7611 switch (m)
7613 case MATCH_YES:
7614 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7615 for variable in a BLOCK which is defined outside of the BLOCK. */
7616 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7618 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7619 "%C, which is use-/host-associated", sym->name);
7620 return MATCH_ERROR;
7622 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7623 return MATCH_ERROR;
7624 goto next_item;
7626 case MATCH_NO:
7627 break;
7629 case MATCH_ERROR:
7630 return MATCH_ERROR;
7633 next_item:
7634 if (gfc_match_eos () == MATCH_YES)
7635 break;
7636 if (gfc_match_char (',') != MATCH_YES)
7637 goto syntax;
7640 return MATCH_YES;
7642 syntax:
7643 gfc_error ("Syntax error in VOLATILE statement at %C");
7644 return MATCH_ERROR;
7648 match
7649 gfc_match_asynchronous (void)
7651 gfc_symbol *sym;
7652 match m;
7654 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7655 return MATCH_ERROR;
7657 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7659 return MATCH_ERROR;
7662 if (gfc_match_eos () == MATCH_YES)
7663 goto syntax;
7665 for(;;)
7667 /* ASYNCHRONOUS is special because it can be added to host-associated
7668 symbols locally. */
7669 m = gfc_match_symbol (&sym, 1);
7670 switch (m)
7672 case MATCH_YES:
7673 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7674 return MATCH_ERROR;
7675 goto next_item;
7677 case MATCH_NO:
7678 break;
7680 case MATCH_ERROR:
7681 return MATCH_ERROR;
7684 next_item:
7685 if (gfc_match_eos () == MATCH_YES)
7686 break;
7687 if (gfc_match_char (',') != MATCH_YES)
7688 goto syntax;
7691 return MATCH_YES;
7693 syntax:
7694 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7695 return MATCH_ERROR;
7699 /* Match a module procedure statement in a submodule. */
7701 match
7702 gfc_match_submod_proc (void)
7704 char name[GFC_MAX_SYMBOL_LEN + 1];
7705 gfc_symbol *sym, *fsym;
7706 match m;
7707 gfc_formal_arglist *formal, *head, *tail;
7709 if (gfc_current_state () != COMP_CONTAINS
7710 || !(gfc_state_stack->previous
7711 && (gfc_state_stack->previous->state == COMP_SUBMODULE
7712 || gfc_state_stack->previous->state == COMP_MODULE)))
7713 return MATCH_NO;
7715 m = gfc_match (" module% procedure% %n", name);
7716 if (m != MATCH_YES)
7717 return m;
7719 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
7720 "at %C"))
7721 return MATCH_ERROR;
7723 if (get_proc_name (name, &sym, false))
7724 return MATCH_ERROR;
7726 /* Make sure that the result field is appropriately filled, even though
7727 the result symbol will be replaced later on. */
7728 if (sym->ts.interface && sym->ts.interface->attr.function)
7730 if (sym->ts.interface->result
7731 && sym->ts.interface->result != sym->ts.interface)
7732 sym->result= sym->ts.interface->result;
7733 else
7734 sym->result = sym;
7737 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7738 the symbol existed before. */
7739 sym->declared_at = gfc_current_locus;
7741 if (!sym->attr.module_procedure)
7742 return MATCH_ERROR;
7744 /* Signal match_end to expect "end procedure". */
7745 sym->abr_modproc_decl = 1;
7747 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7748 sym->attr.if_source = IFSRC_DECL;
7750 gfc_new_block = sym;
7752 /* Make a new formal arglist with the symbols in the procedure
7753 namespace. */
7754 head = tail = NULL;
7755 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
7757 if (formal == sym->formal)
7758 head = tail = gfc_get_formal_arglist ();
7759 else
7761 tail->next = gfc_get_formal_arglist ();
7762 tail = tail->next;
7765 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
7766 goto cleanup;
7768 tail->sym = fsym;
7769 gfc_set_sym_referenced (fsym);
7772 /* The dummy symbols get cleaned up, when the formal_namespace of the
7773 interface declaration is cleared. This allows us to add the
7774 explicit interface as is done for other type of procedure. */
7775 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
7776 &gfc_current_locus))
7777 return MATCH_ERROR;
7779 if (gfc_match_eos () != MATCH_YES)
7781 gfc_syntax_error (ST_MODULE_PROC);
7782 return MATCH_ERROR;
7785 return MATCH_YES;
7787 cleanup:
7788 gfc_free_formal_arglist (head);
7789 return MATCH_ERROR;
7793 /* Match a module procedure statement. Note that we have to modify
7794 symbols in the parent's namespace because the current one was there
7795 to receive symbols that are in an interface's formal argument list. */
7797 match
7798 gfc_match_modproc (void)
7800 char name[GFC_MAX_SYMBOL_LEN + 1];
7801 gfc_symbol *sym;
7802 match m;
7803 locus old_locus;
7804 gfc_namespace *module_ns;
7805 gfc_interface *old_interface_head, *interface;
7807 if (gfc_state_stack->state != COMP_INTERFACE
7808 || gfc_state_stack->previous == NULL
7809 || current_interface.type == INTERFACE_NAMELESS
7810 || current_interface.type == INTERFACE_ABSTRACT)
7812 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7813 "interface");
7814 return MATCH_ERROR;
7817 module_ns = gfc_current_ns->parent;
7818 for (; module_ns; module_ns = module_ns->parent)
7819 if (module_ns->proc_name->attr.flavor == FL_MODULE
7820 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7821 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7822 && !module_ns->proc_name->attr.contained))
7823 break;
7825 if (module_ns == NULL)
7826 return MATCH_ERROR;
7828 /* Store the current state of the interface. We will need it if we
7829 end up with a syntax error and need to recover. */
7830 old_interface_head = gfc_current_interface_head ();
7832 /* Check if the F2008 optional double colon appears. */
7833 gfc_gobble_whitespace ();
7834 old_locus = gfc_current_locus;
7835 if (gfc_match ("::") == MATCH_YES)
7837 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7838 "MODULE PROCEDURE statement at %L", &old_locus))
7839 return MATCH_ERROR;
7841 else
7842 gfc_current_locus = old_locus;
7844 for (;;)
7846 bool last = false;
7847 old_locus = gfc_current_locus;
7849 m = gfc_match_name (name);
7850 if (m == MATCH_NO)
7851 goto syntax;
7852 if (m != MATCH_YES)
7853 return MATCH_ERROR;
7855 /* Check for syntax error before starting to add symbols to the
7856 current namespace. */
7857 if (gfc_match_eos () == MATCH_YES)
7858 last = true;
7860 if (!last && gfc_match_char (',') != MATCH_YES)
7861 goto syntax;
7863 /* Now we're sure the syntax is valid, we process this item
7864 further. */
7865 if (gfc_get_symbol (name, module_ns, &sym))
7866 return MATCH_ERROR;
7868 if (sym->attr.intrinsic)
7870 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7871 "PROCEDURE", &old_locus);
7872 return MATCH_ERROR;
7875 if (sym->attr.proc != PROC_MODULE
7876 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7877 return MATCH_ERROR;
7879 if (!gfc_add_interface (sym))
7880 return MATCH_ERROR;
7882 sym->attr.mod_proc = 1;
7883 sym->declared_at = old_locus;
7885 if (last)
7886 break;
7889 return MATCH_YES;
7891 syntax:
7892 /* Restore the previous state of the interface. */
7893 interface = gfc_current_interface_head ();
7894 gfc_set_current_interface_head (old_interface_head);
7896 /* Free the new interfaces. */
7897 while (interface != old_interface_head)
7899 gfc_interface *i = interface->next;
7900 free (interface);
7901 interface = i;
7904 /* And issue a syntax error. */
7905 gfc_syntax_error (ST_MODULE_PROC);
7906 return MATCH_ERROR;
7910 /* Check a derived type that is being extended. */
7912 static gfc_symbol*
7913 check_extended_derived_type (char *name)
7915 gfc_symbol *extended;
7917 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7919 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7920 return NULL;
7923 extended = gfc_find_dt_in_generic (extended);
7925 /* F08:C428. */
7926 if (!extended)
7928 gfc_error ("Symbol %qs at %C has not been previously defined", name);
7929 return NULL;
7932 if (extended->attr.flavor != FL_DERIVED)
7934 gfc_error ("%qs in EXTENDS expression at %C is not a "
7935 "derived type", name);
7936 return NULL;
7939 if (extended->attr.is_bind_c)
7941 gfc_error ("%qs cannot be extended at %C because it "
7942 "is BIND(C)", extended->name);
7943 return NULL;
7946 if (extended->attr.sequence)
7948 gfc_error ("%qs cannot be extended at %C because it "
7949 "is a SEQUENCE type", extended->name);
7950 return NULL;
7953 return extended;
7957 /* Match the optional attribute specifiers for a type declaration.
7958 Return MATCH_ERROR if an error is encountered in one of the handled
7959 attributes (public, private, bind(c)), MATCH_NO if what's found is
7960 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7961 checking on attribute conflicts needs to be done. */
7963 match
7964 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7966 /* See if the derived type is marked as private. */
7967 if (gfc_match (" , private") == MATCH_YES)
7969 if (gfc_current_state () != COMP_MODULE)
7971 gfc_error ("Derived type at %C can only be PRIVATE in the "
7972 "specification part of a module");
7973 return MATCH_ERROR;
7976 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7977 return MATCH_ERROR;
7979 else if (gfc_match (" , public") == MATCH_YES)
7981 if (gfc_current_state () != COMP_MODULE)
7983 gfc_error ("Derived type at %C can only be PUBLIC in the "
7984 "specification part of a module");
7985 return MATCH_ERROR;
7988 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7989 return MATCH_ERROR;
7991 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7993 /* If the type is defined to be bind(c) it then needs to make
7994 sure that all fields are interoperable. This will
7995 need to be a semantic check on the finished derived type.
7996 See 15.2.3 (lines 9-12) of F2003 draft. */
7997 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7998 return MATCH_ERROR;
8000 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
8002 else if (gfc_match (" , abstract") == MATCH_YES)
8004 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
8005 return MATCH_ERROR;
8007 if (!gfc_add_abstract (attr, &gfc_current_locus))
8008 return MATCH_ERROR;
8010 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
8012 if (!gfc_add_extension (attr, &gfc_current_locus))
8013 return MATCH_ERROR;
8015 else
8016 return MATCH_NO;
8018 /* If we get here, something matched. */
8019 return MATCH_YES;
8023 /* Match the beginning of a derived type declaration. If a type name
8024 was the result of a function, then it is possible to have a symbol
8025 already to be known as a derived type yet have no components. */
8027 match
8028 gfc_match_derived_decl (void)
8030 char name[GFC_MAX_SYMBOL_LEN + 1];
8031 char parent[GFC_MAX_SYMBOL_LEN + 1];
8032 symbol_attribute attr;
8033 gfc_symbol *sym, *gensym;
8034 gfc_symbol *extended;
8035 match m;
8036 match is_type_attr_spec = MATCH_NO;
8037 bool seen_attr = false;
8038 gfc_interface *intr = NULL, *head;
8040 if (gfc_current_state () == COMP_DERIVED)
8041 return MATCH_NO;
8043 name[0] = '\0';
8044 parent[0] = '\0';
8045 gfc_clear_attr (&attr);
8046 extended = NULL;
8050 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
8051 if (is_type_attr_spec == MATCH_ERROR)
8052 return MATCH_ERROR;
8053 if (is_type_attr_spec == MATCH_YES)
8054 seen_attr = true;
8055 } while (is_type_attr_spec == MATCH_YES);
8057 /* Deal with derived type extensions. The extension attribute has
8058 been added to 'attr' but now the parent type must be found and
8059 checked. */
8060 if (parent[0])
8061 extended = check_extended_derived_type (parent);
8063 if (parent[0] && !extended)
8064 return MATCH_ERROR;
8066 if (gfc_match (" ::") != MATCH_YES && seen_attr)
8068 gfc_error ("Expected :: in TYPE definition at %C");
8069 return MATCH_ERROR;
8072 m = gfc_match (" %n%t", name);
8073 if (m != MATCH_YES)
8074 return m;
8076 /* Make sure the name is not the name of an intrinsic type. */
8077 if (gfc_is_intrinsic_typename (name))
8079 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
8080 "type", name);
8081 return MATCH_ERROR;
8084 if (gfc_get_symbol (name, NULL, &gensym))
8085 return MATCH_ERROR;
8087 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
8089 gfc_error ("Derived type name %qs at %C already has a basic type "
8090 "of %s", gensym->name, gfc_typename (&gensym->ts));
8091 return MATCH_ERROR;
8094 if (!gensym->attr.generic
8095 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
8096 return MATCH_ERROR;
8098 if (!gensym->attr.function
8099 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
8100 return MATCH_ERROR;
8102 sym = gfc_find_dt_in_generic (gensym);
8104 if (sym && (sym->components != NULL || sym->attr.zero_comp))
8106 gfc_error ("Derived type definition of %qs at %C has already been "
8107 "defined", sym->name);
8108 return MATCH_ERROR;
8111 if (!sym)
8113 /* Use upper case to save the actual derived-type symbol. */
8114 gfc_get_symbol (gfc_get_string ("%c%s",
8115 (char) TOUPPER ((unsigned char) gensym->name[0]),
8116 &gensym->name[1]), NULL, &sym);
8117 sym->name = gfc_get_string (gensym->name);
8118 head = gensym->generic;
8119 intr = gfc_get_interface ();
8120 intr->sym = sym;
8121 intr->where = gfc_current_locus;
8122 intr->sym->declared_at = gfc_current_locus;
8123 intr->next = head;
8124 gensym->generic = intr;
8125 gensym->attr.if_source = IFSRC_DECL;
8128 /* The symbol may already have the derived attribute without the
8129 components. The ways this can happen is via a function
8130 definition, an INTRINSIC statement or a subtype in another
8131 derived type that is a pointer. The first part of the AND clause
8132 is true if the symbol is not the return value of a function. */
8133 if (sym->attr.flavor != FL_DERIVED
8134 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
8135 return MATCH_ERROR;
8137 if (attr.access != ACCESS_UNKNOWN
8138 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
8139 return MATCH_ERROR;
8140 else if (sym->attr.access == ACCESS_UNKNOWN
8141 && gensym->attr.access != ACCESS_UNKNOWN
8142 && !gfc_add_access (&sym->attr, gensym->attr.access,
8143 sym->name, NULL))
8144 return MATCH_ERROR;
8146 if (sym->attr.access != ACCESS_UNKNOWN
8147 && gensym->attr.access == ACCESS_UNKNOWN)
8148 gensym->attr.access = sym->attr.access;
8150 /* See if the derived type was labeled as bind(c). */
8151 if (attr.is_bind_c != 0)
8152 sym->attr.is_bind_c = attr.is_bind_c;
8154 /* Construct the f2k_derived namespace if it is not yet there. */
8155 if (!sym->f2k_derived)
8156 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8158 if (extended && !sym->components)
8160 gfc_component *p;
8162 /* Add the extended derived type as the first component. */
8163 gfc_add_component (sym, parent, &p);
8164 extended->refs++;
8165 gfc_set_sym_referenced (extended);
8167 p->ts.type = BT_DERIVED;
8168 p->ts.u.derived = extended;
8169 p->initializer = gfc_default_initializer (&p->ts);
8171 /* Set extension level. */
8172 if (extended->attr.extension == 255)
8174 /* Since the extension field is 8 bit wide, we can only have
8175 up to 255 extension levels. */
8176 gfc_error ("Maximum extension level reached with type %qs at %L",
8177 extended->name, &extended->declared_at);
8178 return MATCH_ERROR;
8180 sym->attr.extension = extended->attr.extension + 1;
8182 /* Provide the links between the extended type and its extension. */
8183 if (!extended->f2k_derived)
8184 extended->f2k_derived = gfc_get_namespace (NULL, 0);
8187 if (!sym->hash_value)
8188 /* Set the hash for the compound name for this type. */
8189 sym->hash_value = gfc_hash_value (sym);
8191 /* Take over the ABSTRACT attribute. */
8192 sym->attr.abstract = attr.abstract;
8194 gfc_new_block = sym;
8196 return MATCH_YES;
8200 /* Cray Pointees can be declared as:
8201 pointer (ipt, a (n,m,...,*)) */
8203 match
8204 gfc_mod_pointee_as (gfc_array_spec *as)
8206 as->cray_pointee = true; /* This will be useful to know later. */
8207 if (as->type == AS_ASSUMED_SIZE)
8208 as->cp_was_assumed = true;
8209 else if (as->type == AS_ASSUMED_SHAPE)
8211 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8212 return MATCH_ERROR;
8214 return MATCH_YES;
8218 /* Match the enum definition statement, here we are trying to match
8219 the first line of enum definition statement.
8220 Returns MATCH_YES if match is found. */
8222 match
8223 gfc_match_enum (void)
8225 match m;
8227 m = gfc_match_eos ();
8228 if (m != MATCH_YES)
8229 return m;
8231 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
8232 return MATCH_ERROR;
8234 return MATCH_YES;
8238 /* Returns an initializer whose value is one higher than the value of the
8239 LAST_INITIALIZER argument. If the argument is NULL, the
8240 initializers value will be set to zero. The initializer's kind
8241 will be set to gfc_c_int_kind.
8243 If -fshort-enums is given, the appropriate kind will be selected
8244 later after all enumerators have been parsed. A warning is issued
8245 here if an initializer exceeds gfc_c_int_kind. */
8247 static gfc_expr *
8248 enum_initializer (gfc_expr *last_initializer, locus where)
8250 gfc_expr *result;
8251 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
8253 mpz_init (result->value.integer);
8255 if (last_initializer != NULL)
8257 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8258 result->where = last_initializer->where;
8260 if (gfc_check_integer_range (result->value.integer,
8261 gfc_c_int_kind) != ARITH_OK)
8263 gfc_error ("Enumerator exceeds the C integer type at %C");
8264 return NULL;
8267 else
8269 /* Control comes here, if it's the very first enumerator and no
8270 initializer has been given. It will be initialized to zero. */
8271 mpz_set_si (result->value.integer, 0);
8274 return result;
8278 /* Match a variable name with an optional initializer. When this
8279 subroutine is called, a variable is expected to be parsed next.
8280 Depending on what is happening at the moment, updates either the
8281 symbol table or the current interface. */
8283 static match
8284 enumerator_decl (void)
8286 char name[GFC_MAX_SYMBOL_LEN + 1];
8287 gfc_expr *initializer;
8288 gfc_array_spec *as = NULL;
8289 gfc_symbol *sym;
8290 locus var_locus;
8291 match m;
8292 bool t;
8293 locus old_locus;
8295 initializer = NULL;
8296 old_locus = gfc_current_locus;
8298 /* When we get here, we've just matched a list of attributes and
8299 maybe a type and a double colon. The next thing we expect to see
8300 is the name of the symbol. */
8301 m = gfc_match_name (name);
8302 if (m != MATCH_YES)
8303 goto cleanup;
8305 var_locus = gfc_current_locus;
8307 /* OK, we've successfully matched the declaration. Now put the
8308 symbol in the current namespace. If we fail to create the symbol,
8309 bail out. */
8310 if (!build_sym (name, NULL, false, &as, &var_locus))
8312 m = MATCH_ERROR;
8313 goto cleanup;
8316 /* The double colon must be present in order to have initializers.
8317 Otherwise the statement is ambiguous with an assignment statement. */
8318 if (colon_seen)
8320 if (gfc_match_char ('=') == MATCH_YES)
8322 m = gfc_match_init_expr (&initializer);
8323 if (m == MATCH_NO)
8325 gfc_error ("Expected an initialization expression at %C");
8326 m = MATCH_ERROR;
8329 if (m != MATCH_YES)
8330 goto cleanup;
8334 /* If we do not have an initializer, the initialization value of the
8335 previous enumerator (stored in last_initializer) is incremented
8336 by 1 and is used to initialize the current enumerator. */
8337 if (initializer == NULL)
8338 initializer = enum_initializer (last_initializer, old_locus);
8340 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8342 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8343 &var_locus);
8344 m = MATCH_ERROR;
8345 goto cleanup;
8348 /* Store this current initializer, for the next enumerator variable
8349 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8350 use last_initializer below. */
8351 last_initializer = initializer;
8352 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8354 /* Maintain enumerator history. */
8355 gfc_find_symbol (name, NULL, 0, &sym);
8356 create_enum_history (sym, last_initializer);
8358 return (t) ? MATCH_YES : MATCH_ERROR;
8360 cleanup:
8361 /* Free stuff up and return. */
8362 gfc_free_expr (initializer);
8364 return m;
8368 /* Match the enumerator definition statement. */
8370 match
8371 gfc_match_enumerator_def (void)
8373 match m;
8374 bool t;
8376 gfc_clear_ts (&current_ts);
8378 m = gfc_match (" enumerator");
8379 if (m != MATCH_YES)
8380 return m;
8382 m = gfc_match (" :: ");
8383 if (m == MATCH_ERROR)
8384 return m;
8386 colon_seen = (m == MATCH_YES);
8388 if (gfc_current_state () != COMP_ENUM)
8390 gfc_error ("ENUM definition statement expected before %C");
8391 gfc_free_enum_history ();
8392 return MATCH_ERROR;
8395 (&current_ts)->type = BT_INTEGER;
8396 (&current_ts)->kind = gfc_c_int_kind;
8398 gfc_clear_attr (&current_attr);
8399 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
8400 if (!t)
8402 m = MATCH_ERROR;
8403 goto cleanup;
8406 for (;;)
8408 m = enumerator_decl ();
8409 if (m == MATCH_ERROR)
8411 gfc_free_enum_history ();
8412 goto cleanup;
8414 if (m == MATCH_NO)
8415 break;
8417 if (gfc_match_eos () == MATCH_YES)
8418 goto cleanup;
8419 if (gfc_match_char (',') != MATCH_YES)
8420 break;
8423 if (gfc_current_state () == COMP_ENUM)
8425 gfc_free_enum_history ();
8426 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8427 m = MATCH_ERROR;
8430 cleanup:
8431 gfc_free_array_spec (current_as);
8432 current_as = NULL;
8433 return m;
8438 /* Match binding attributes. */
8440 static match
8441 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
8443 bool found_passing = false;
8444 bool seen_ptr = false;
8445 match m = MATCH_YES;
8447 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8448 this case the defaults are in there. */
8449 ba->access = ACCESS_UNKNOWN;
8450 ba->pass_arg = NULL;
8451 ba->pass_arg_num = 0;
8452 ba->nopass = 0;
8453 ba->non_overridable = 0;
8454 ba->deferred = 0;
8455 ba->ppc = ppc;
8457 /* If we find a comma, we believe there are binding attributes. */
8458 m = gfc_match_char (',');
8459 if (m == MATCH_NO)
8460 goto done;
8464 /* Access specifier. */
8466 m = gfc_match (" public");
8467 if (m == MATCH_ERROR)
8468 goto error;
8469 if (m == MATCH_YES)
8471 if (ba->access != ACCESS_UNKNOWN)
8473 gfc_error ("Duplicate access-specifier at %C");
8474 goto error;
8477 ba->access = ACCESS_PUBLIC;
8478 continue;
8481 m = gfc_match (" private");
8482 if (m == MATCH_ERROR)
8483 goto error;
8484 if (m == MATCH_YES)
8486 if (ba->access != ACCESS_UNKNOWN)
8488 gfc_error ("Duplicate access-specifier at %C");
8489 goto error;
8492 ba->access = ACCESS_PRIVATE;
8493 continue;
8496 /* If inside GENERIC, the following is not allowed. */
8497 if (!generic)
8500 /* NOPASS flag. */
8501 m = gfc_match (" nopass");
8502 if (m == MATCH_ERROR)
8503 goto error;
8504 if (m == MATCH_YES)
8506 if (found_passing)
8508 gfc_error ("Binding attributes already specify passing,"
8509 " illegal NOPASS at %C");
8510 goto error;
8513 found_passing = true;
8514 ba->nopass = 1;
8515 continue;
8518 /* PASS possibly including argument. */
8519 m = gfc_match (" pass");
8520 if (m == MATCH_ERROR)
8521 goto error;
8522 if (m == MATCH_YES)
8524 char arg[GFC_MAX_SYMBOL_LEN + 1];
8526 if (found_passing)
8528 gfc_error ("Binding attributes already specify passing,"
8529 " illegal PASS at %C");
8530 goto error;
8533 m = gfc_match (" ( %n )", arg);
8534 if (m == MATCH_ERROR)
8535 goto error;
8536 if (m == MATCH_YES)
8537 ba->pass_arg = gfc_get_string (arg);
8538 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8540 found_passing = true;
8541 ba->nopass = 0;
8542 continue;
8545 if (ppc)
8547 /* POINTER flag. */
8548 m = gfc_match (" pointer");
8549 if (m == MATCH_ERROR)
8550 goto error;
8551 if (m == MATCH_YES)
8553 if (seen_ptr)
8555 gfc_error ("Duplicate POINTER attribute at %C");
8556 goto error;
8559 seen_ptr = true;
8560 continue;
8563 else
8565 /* NON_OVERRIDABLE flag. */
8566 m = gfc_match (" non_overridable");
8567 if (m == MATCH_ERROR)
8568 goto error;
8569 if (m == MATCH_YES)
8571 if (ba->non_overridable)
8573 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8574 goto error;
8577 ba->non_overridable = 1;
8578 continue;
8581 /* DEFERRED flag. */
8582 m = gfc_match (" deferred");
8583 if (m == MATCH_ERROR)
8584 goto error;
8585 if (m == MATCH_YES)
8587 if (ba->deferred)
8589 gfc_error ("Duplicate DEFERRED at %C");
8590 goto error;
8593 ba->deferred = 1;
8594 continue;
8600 /* Nothing matching found. */
8601 if (generic)
8602 gfc_error ("Expected access-specifier at %C");
8603 else
8604 gfc_error ("Expected binding attribute at %C");
8605 goto error;
8607 while (gfc_match_char (',') == MATCH_YES);
8609 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8610 if (ba->non_overridable && ba->deferred)
8612 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8613 goto error;
8616 m = MATCH_YES;
8618 done:
8619 if (ba->access == ACCESS_UNKNOWN)
8620 ba->access = gfc_typebound_default_access;
8622 if (ppc && !seen_ptr)
8624 gfc_error ("POINTER attribute is required for procedure pointer component"
8625 " at %C");
8626 goto error;
8629 return m;
8631 error:
8632 return MATCH_ERROR;
8636 /* Match a PROCEDURE specific binding inside a derived type. */
8638 static match
8639 match_procedure_in_type (void)
8641 char name[GFC_MAX_SYMBOL_LEN + 1];
8642 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8643 char* target = NULL, *ifc = NULL;
8644 gfc_typebound_proc tb;
8645 bool seen_colons;
8646 bool seen_attrs;
8647 match m;
8648 gfc_symtree* stree;
8649 gfc_namespace* ns;
8650 gfc_symbol* block;
8651 int num;
8653 /* Check current state. */
8654 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8655 block = gfc_state_stack->previous->sym;
8656 gcc_assert (block);
8658 /* Try to match PROCEDURE(interface). */
8659 if (gfc_match (" (") == MATCH_YES)
8661 m = gfc_match_name (target_buf);
8662 if (m == MATCH_ERROR)
8663 return m;
8664 if (m != MATCH_YES)
8666 gfc_error ("Interface-name expected after %<(%> at %C");
8667 return MATCH_ERROR;
8670 if (gfc_match (" )") != MATCH_YES)
8672 gfc_error ("%<)%> expected at %C");
8673 return MATCH_ERROR;
8676 ifc = target_buf;
8679 /* Construct the data structure. */
8680 memset (&tb, 0, sizeof (tb));
8681 tb.where = gfc_current_locus;
8683 /* Match binding attributes. */
8684 m = match_binding_attributes (&tb, false, false);
8685 if (m == MATCH_ERROR)
8686 return m;
8687 seen_attrs = (m == MATCH_YES);
8689 /* Check that attribute DEFERRED is given if an interface is specified. */
8690 if (tb.deferred && !ifc)
8692 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8693 return MATCH_ERROR;
8695 if (ifc && !tb.deferred)
8697 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8698 return MATCH_ERROR;
8701 /* Match the colons. */
8702 m = gfc_match (" ::");
8703 if (m == MATCH_ERROR)
8704 return m;
8705 seen_colons = (m == MATCH_YES);
8706 if (seen_attrs && !seen_colons)
8708 gfc_error ("Expected %<::%> after binding-attributes at %C");
8709 return MATCH_ERROR;
8712 /* Match the binding names. */
8713 for(num=1;;num++)
8715 m = gfc_match_name (name);
8716 if (m == MATCH_ERROR)
8717 return m;
8718 if (m == MATCH_NO)
8720 gfc_error ("Expected binding name at %C");
8721 return MATCH_ERROR;
8724 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8725 return MATCH_ERROR;
8727 /* Try to match the '=> target', if it's there. */
8728 target = ifc;
8729 m = gfc_match (" =>");
8730 if (m == MATCH_ERROR)
8731 return m;
8732 if (m == MATCH_YES)
8734 if (tb.deferred)
8736 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8737 return MATCH_ERROR;
8740 if (!seen_colons)
8742 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8743 " at %C");
8744 return MATCH_ERROR;
8747 m = gfc_match_name (target_buf);
8748 if (m == MATCH_ERROR)
8749 return m;
8750 if (m == MATCH_NO)
8752 gfc_error ("Expected binding target after %<=>%> at %C");
8753 return MATCH_ERROR;
8755 target = target_buf;
8758 /* If no target was found, it has the same name as the binding. */
8759 if (!target)
8760 target = name;
8762 /* Get the namespace to insert the symbols into. */
8763 ns = block->f2k_derived;
8764 gcc_assert (ns);
8766 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8767 if (tb.deferred && !block->attr.abstract)
8769 gfc_error ("Type %qs containing DEFERRED binding at %C "
8770 "is not ABSTRACT", block->name);
8771 return MATCH_ERROR;
8774 /* See if we already have a binding with this name in the symtree which
8775 would be an error. If a GENERIC already targeted this binding, it may
8776 be already there but then typebound is still NULL. */
8777 stree = gfc_find_symtree (ns->tb_sym_root, name);
8778 if (stree && stree->n.tb)
8780 gfc_error ("There is already a procedure with binding name %qs for "
8781 "the derived type %qs at %C", name, block->name);
8782 return MATCH_ERROR;
8785 /* Insert it and set attributes. */
8787 if (!stree)
8789 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8790 gcc_assert (stree);
8792 stree->n.tb = gfc_get_typebound_proc (&tb);
8794 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8795 false))
8796 return MATCH_ERROR;
8797 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8799 if (gfc_match_eos () == MATCH_YES)
8800 return MATCH_YES;
8801 if (gfc_match_char (',') != MATCH_YES)
8802 goto syntax;
8805 syntax:
8806 gfc_error ("Syntax error in PROCEDURE statement at %C");
8807 return MATCH_ERROR;
8811 /* Match a GENERIC procedure binding inside a derived type. */
8813 match
8814 gfc_match_generic (void)
8816 char name[GFC_MAX_SYMBOL_LEN + 1];
8817 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8818 gfc_symbol* block;
8819 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8820 gfc_typebound_proc* tb;
8821 gfc_namespace* ns;
8822 interface_type op_type;
8823 gfc_intrinsic_op op;
8824 match m;
8826 /* Check current state. */
8827 if (gfc_current_state () == COMP_DERIVED)
8829 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8830 return MATCH_ERROR;
8832 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8833 return MATCH_NO;
8834 block = gfc_state_stack->previous->sym;
8835 ns = block->f2k_derived;
8836 gcc_assert (block && ns);
8838 memset (&tbattr, 0, sizeof (tbattr));
8839 tbattr.where = gfc_current_locus;
8841 /* See if we get an access-specifier. */
8842 m = match_binding_attributes (&tbattr, true, false);
8843 if (m == MATCH_ERROR)
8844 goto error;
8846 /* Now the colons, those are required. */
8847 if (gfc_match (" ::") != MATCH_YES)
8849 gfc_error ("Expected %<::%> at %C");
8850 goto error;
8853 /* Match the binding name; depending on type (operator / generic) format
8854 it for future error messages into bind_name. */
8856 m = gfc_match_generic_spec (&op_type, name, &op);
8857 if (m == MATCH_ERROR)
8858 return MATCH_ERROR;
8859 if (m == MATCH_NO)
8861 gfc_error ("Expected generic name or operator descriptor at %C");
8862 goto error;
8865 switch (op_type)
8867 case INTERFACE_GENERIC:
8868 snprintf (bind_name, sizeof (bind_name), "%s", name);
8869 break;
8871 case INTERFACE_USER_OP:
8872 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8873 break;
8875 case INTERFACE_INTRINSIC_OP:
8876 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8877 gfc_op2string (op));
8878 break;
8880 case INTERFACE_NAMELESS:
8881 gfc_error ("Malformed GENERIC statement at %C");
8882 goto error;
8883 break;
8885 default:
8886 gcc_unreachable ();
8889 /* Match the required =>. */
8890 if (gfc_match (" =>") != MATCH_YES)
8892 gfc_error ("Expected %<=>%> at %C");
8893 goto error;
8896 /* Try to find existing GENERIC binding with this name / for this operator;
8897 if there is something, check that it is another GENERIC and then extend
8898 it rather than building a new node. Otherwise, create it and put it
8899 at the right position. */
8901 switch (op_type)
8903 case INTERFACE_USER_OP:
8904 case INTERFACE_GENERIC:
8906 const bool is_op = (op_type == INTERFACE_USER_OP);
8907 gfc_symtree* st;
8909 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8910 if (st)
8912 tb = st->n.tb;
8913 gcc_assert (tb);
8915 else
8916 tb = NULL;
8918 break;
8921 case INTERFACE_INTRINSIC_OP:
8922 tb = ns->tb_op[op];
8923 break;
8925 default:
8926 gcc_unreachable ();
8929 if (tb)
8931 if (!tb->is_generic)
8933 gcc_assert (op_type == INTERFACE_GENERIC);
8934 gfc_error ("There's already a non-generic procedure with binding name"
8935 " %qs for the derived type %qs at %C",
8936 bind_name, block->name);
8937 goto error;
8940 if (tb->access != tbattr.access)
8942 gfc_error ("Binding at %C must have the same access as already"
8943 " defined binding %qs", bind_name);
8944 goto error;
8947 else
8949 tb = gfc_get_typebound_proc (NULL);
8950 tb->where = gfc_current_locus;
8951 tb->access = tbattr.access;
8952 tb->is_generic = 1;
8953 tb->u.generic = NULL;
8955 switch (op_type)
8957 case INTERFACE_GENERIC:
8958 case INTERFACE_USER_OP:
8960 const bool is_op = (op_type == INTERFACE_USER_OP);
8961 gfc_symtree* st;
8963 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8964 name);
8965 gcc_assert (st);
8966 st->n.tb = tb;
8968 break;
8971 case INTERFACE_INTRINSIC_OP:
8972 ns->tb_op[op] = tb;
8973 break;
8975 default:
8976 gcc_unreachable ();
8980 /* Now, match all following names as specific targets. */
8983 gfc_symtree* target_st;
8984 gfc_tbp_generic* target;
8986 m = gfc_match_name (name);
8987 if (m == MATCH_ERROR)
8988 goto error;
8989 if (m == MATCH_NO)
8991 gfc_error ("Expected specific binding name at %C");
8992 goto error;
8995 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8997 /* See if this is a duplicate specification. */
8998 for (target = tb->u.generic; target; target = target->next)
8999 if (target_st == target->specific_st)
9001 gfc_error ("%qs already defined as specific binding for the"
9002 " generic %qs at %C", name, bind_name);
9003 goto error;
9006 target = gfc_get_tbp_generic ();
9007 target->specific_st = target_st;
9008 target->specific = NULL;
9009 target->next = tb->u.generic;
9010 target->is_operator = ((op_type == INTERFACE_USER_OP)
9011 || (op_type == INTERFACE_INTRINSIC_OP));
9012 tb->u.generic = target;
9014 while (gfc_match (" ,") == MATCH_YES);
9016 /* Here should be the end. */
9017 if (gfc_match_eos () != MATCH_YES)
9019 gfc_error ("Junk after GENERIC binding at %C");
9020 goto error;
9023 return MATCH_YES;
9025 error:
9026 return MATCH_ERROR;
9030 /* Match a FINAL declaration inside a derived type. */
9032 match
9033 gfc_match_final_decl (void)
9035 char name[GFC_MAX_SYMBOL_LEN + 1];
9036 gfc_symbol* sym;
9037 match m;
9038 gfc_namespace* module_ns;
9039 bool first, last;
9040 gfc_symbol* block;
9042 if (gfc_current_form == FORM_FREE)
9044 char c = gfc_peek_ascii_char ();
9045 if (!gfc_is_whitespace (c) && c != ':')
9046 return MATCH_NO;
9049 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
9051 if (gfc_current_form == FORM_FIXED)
9052 return MATCH_NO;
9054 gfc_error ("FINAL declaration at %C must be inside a derived type "
9055 "CONTAINS section");
9056 return MATCH_ERROR;
9059 block = gfc_state_stack->previous->sym;
9060 gcc_assert (block);
9062 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
9063 || gfc_state_stack->previous->previous->state != COMP_MODULE)
9065 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9066 " specification part of a MODULE");
9067 return MATCH_ERROR;
9070 module_ns = gfc_current_ns;
9071 gcc_assert (module_ns);
9072 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
9074 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9075 if (gfc_match (" ::") == MATCH_ERROR)
9076 return MATCH_ERROR;
9078 /* Match the sequence of procedure names. */
9079 first = true;
9080 last = false;
9083 gfc_finalizer* f;
9085 if (first && gfc_match_eos () == MATCH_YES)
9087 gfc_error ("Empty FINAL at %C");
9088 return MATCH_ERROR;
9091 m = gfc_match_name (name);
9092 if (m == MATCH_NO)
9094 gfc_error ("Expected module procedure name at %C");
9095 return MATCH_ERROR;
9097 else if (m != MATCH_YES)
9098 return MATCH_ERROR;
9100 if (gfc_match_eos () == MATCH_YES)
9101 last = true;
9102 if (!last && gfc_match_char (',') != MATCH_YES)
9104 gfc_error ("Expected %<,%> at %C");
9105 return MATCH_ERROR;
9108 if (gfc_get_symbol (name, module_ns, &sym))
9110 gfc_error ("Unknown procedure name %qs at %C", name);
9111 return MATCH_ERROR;
9114 /* Mark the symbol as module procedure. */
9115 if (sym->attr.proc != PROC_MODULE
9116 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9117 return MATCH_ERROR;
9119 /* Check if we already have this symbol in the list, this is an error. */
9120 for (f = block->f2k_derived->finalizers; f; f = f->next)
9121 if (f->proc_sym == sym)
9123 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9124 name);
9125 return MATCH_ERROR;
9128 /* Add this symbol to the list of finalizers. */
9129 gcc_assert (block->f2k_derived);
9130 sym->refs++;
9131 f = XCNEW (gfc_finalizer);
9132 f->proc_sym = sym;
9133 f->proc_tree = NULL;
9134 f->where = gfc_current_locus;
9135 f->next = block->f2k_derived->finalizers;
9136 block->f2k_derived->finalizers = f;
9138 first = false;
9140 while (!last);
9142 return MATCH_YES;
9146 const ext_attr_t ext_attr_list[] = {
9147 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9148 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9149 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9150 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9151 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9152 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9153 { NULL, EXT_ATTR_LAST, NULL }
9156 /* Match a !GCC$ ATTRIBUTES statement of the form:
9157 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9158 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9160 TODO: We should support all GCC attributes using the same syntax for
9161 the attribute list, i.e. the list in C
9162 __attributes(( attribute-list ))
9163 matches then
9164 !GCC$ ATTRIBUTES attribute-list ::
9165 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9166 saved into a TREE.
9168 As there is absolutely no risk of confusion, we should never return
9169 MATCH_NO. */
9170 match
9171 gfc_match_gcc_attributes (void)
9173 symbol_attribute attr;
9174 char name[GFC_MAX_SYMBOL_LEN + 1];
9175 unsigned id;
9176 gfc_symbol *sym;
9177 match m;
9179 gfc_clear_attr (&attr);
9180 for(;;)
9182 char ch;
9184 if (gfc_match_name (name) != MATCH_YES)
9185 return MATCH_ERROR;
9187 for (id = 0; id < EXT_ATTR_LAST; id++)
9188 if (strcmp (name, ext_attr_list[id].name) == 0)
9189 break;
9191 if (id == EXT_ATTR_LAST)
9193 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9194 return MATCH_ERROR;
9197 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
9198 return MATCH_ERROR;
9200 gfc_gobble_whitespace ();
9201 ch = gfc_next_ascii_char ();
9202 if (ch == ':')
9204 /* This is the successful exit condition for the loop. */
9205 if (gfc_next_ascii_char () == ':')
9206 break;
9209 if (ch == ',')
9210 continue;
9212 goto syntax;
9215 if (gfc_match_eos () == MATCH_YES)
9216 goto syntax;
9218 for(;;)
9220 m = gfc_match_name (name);
9221 if (m != MATCH_YES)
9222 return m;
9224 if (find_special (name, &sym, true))
9225 return MATCH_ERROR;
9227 sym->attr.ext_attr |= attr.ext_attr;
9229 if (gfc_match_eos () == MATCH_YES)
9230 break;
9232 if (gfc_match_char (',') != MATCH_YES)
9233 goto syntax;
9236 return MATCH_YES;
9238 syntax:
9239 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9240 return MATCH_ERROR;