2015-10-27 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / decl.c
blob07c539162f4a3857cf38fdd4f30b000896ed6d23
1 /* Declaration statement matcher
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "options.h"
28 #include "constructor.h"
29 #include "alias.h"
30 #include "tree.h"
31 #include "stringpool.h"
33 /* Macros to access allocate memory for gfc_data_variable,
34 gfc_data_value and gfc_data. */
35 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
36 #define gfc_get_data_value() XCNEW (gfc_data_value)
37 #define gfc_get_data() XCNEW (gfc_data)
40 static bool set_binding_label (const char **, const char *, int);
43 /* This flag is set if an old-style length selector is matched
44 during a type-declaration statement. */
46 static int old_char_selector;
48 /* When variables acquire types and attributes from a declaration
49 statement, they get them from the following static variables. The
50 first part of a declaration sets these variables and the second
51 part copies these into symbol structures. */
53 static gfc_typespec current_ts;
55 static symbol_attribute current_attr;
56 static gfc_array_spec *current_as;
57 static int colon_seen;
59 /* The current binding label (if any). */
60 static const char* curr_binding_label;
61 /* Need to know how many identifiers are on the current data declaration
62 line in case we're given the BIND(C) attribute with a NAME= specifier. */
63 static int num_idents_on_line;
64 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
65 can supply a name if the curr_binding_label is nil and NAME= was not. */
66 static int has_name_equals = 0;
68 /* Initializer of the previous enumerator. */
70 static gfc_expr *last_initializer;
72 /* History of all the enumerators is maintained, so that
73 kind values of all the enumerators could be updated depending
74 upon the maximum initialized value. */
76 typedef struct enumerator_history
78 gfc_symbol *sym;
79 gfc_expr *initializer;
80 struct enumerator_history *next;
82 enumerator_history;
84 /* Header of enum history chain. */
86 static enumerator_history *enum_history = NULL;
88 /* Pointer of enum history node containing largest initializer. */
90 static enumerator_history *max_enum = NULL;
92 /* gfc_new_block points to the symbol of a newly matched block. */
94 gfc_symbol *gfc_new_block;
96 bool gfc_matching_function;
99 /********************* DATA statement subroutines *********************/
101 static bool in_match_data = false;
103 bool
104 gfc_in_match_data (void)
106 return in_match_data;
109 static void
110 set_in_match_data (bool set_value)
112 in_match_data = set_value;
115 /* Free a gfc_data_variable structure and everything beneath it. */
117 static void
118 free_variable (gfc_data_variable *p)
120 gfc_data_variable *q;
122 for (; p; p = q)
124 q = p->next;
125 gfc_free_expr (p->expr);
126 gfc_free_iterator (&p->iter, 0);
127 free_variable (p->list);
128 free (p);
133 /* Free a gfc_data_value structure and everything beneath it. */
135 static void
136 free_value (gfc_data_value *p)
138 gfc_data_value *q;
140 for (; p; p = q)
142 q = p->next;
143 mpz_clear (p->repeat);
144 gfc_free_expr (p->expr);
145 free (p);
150 /* Free a list of gfc_data structures. */
152 void
153 gfc_free_data (gfc_data *p)
155 gfc_data *q;
157 for (; p; p = q)
159 q = p->next;
160 free_variable (p->var);
161 free_value (p->value);
162 free (p);
167 /* Free all data in a namespace. */
169 static void
170 gfc_free_data_all (gfc_namespace *ns)
172 gfc_data *d;
174 for (;ns->data;)
176 d = ns->data->next;
177 free (ns->data);
178 ns->data = d;
182 /* Reject data parsed since the last restore point was marked. */
184 void
185 gfc_reject_data (gfc_namespace *ns)
187 gfc_data *d;
189 while (ns->data && ns->data != ns->old_data)
191 d = ns->data->next;
192 free (ns->data);
193 ns->data = d;
197 static match var_element (gfc_data_variable *);
199 /* Match a list of variables terminated by an iterator and a right
200 parenthesis. */
202 static match
203 var_list (gfc_data_variable *parent)
205 gfc_data_variable *tail, var;
206 match m;
208 m = var_element (&var);
209 if (m == MATCH_ERROR)
210 return MATCH_ERROR;
211 if (m == MATCH_NO)
212 goto syntax;
214 tail = gfc_get_data_variable ();
215 *tail = var;
217 parent->list = tail;
219 for (;;)
221 if (gfc_match_char (',') != MATCH_YES)
222 goto syntax;
224 m = gfc_match_iterator (&parent->iter, 1);
225 if (m == MATCH_YES)
226 break;
227 if (m == MATCH_ERROR)
228 return MATCH_ERROR;
230 m = var_element (&var);
231 if (m == MATCH_ERROR)
232 return MATCH_ERROR;
233 if (m == MATCH_NO)
234 goto syntax;
236 tail->next = gfc_get_data_variable ();
237 tail = tail->next;
239 *tail = var;
242 if (gfc_match_char (')') != MATCH_YES)
243 goto syntax;
244 return MATCH_YES;
246 syntax:
247 gfc_syntax_error (ST_DATA);
248 return MATCH_ERROR;
252 /* Match a single element in a data variable list, which can be a
253 variable-iterator list. */
255 static match
256 var_element (gfc_data_variable *new_var)
258 match m;
259 gfc_symbol *sym;
261 memset (new_var, 0, sizeof (gfc_data_variable));
263 if (gfc_match_char ('(') == MATCH_YES)
264 return var_list (new_var);
266 m = gfc_match_variable (&new_var->expr, 0);
267 if (m != MATCH_YES)
268 return m;
270 sym = new_var->expr->symtree->n.sym;
272 /* Symbol should already have an associated type. */
273 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
274 return MATCH_ERROR;
276 if (!sym->attr.function && gfc_current_ns->parent
277 && gfc_current_ns->parent == sym->ns)
279 gfc_error ("Host associated variable %qs may not be in the DATA "
280 "statement at %C", sym->name);
281 return MATCH_ERROR;
284 if (gfc_current_state () != COMP_BLOCK_DATA
285 && sym->attr.in_common
286 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
287 "common block variable %qs in DATA statement at %C",
288 sym->name))
289 return MATCH_ERROR;
291 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
292 return MATCH_ERROR;
294 return MATCH_YES;
298 /* Match the top-level list of data variables. */
300 static match
301 top_var_list (gfc_data *d)
303 gfc_data_variable var, *tail, *new_var;
304 match m;
306 tail = NULL;
308 for (;;)
310 m = var_element (&var);
311 if (m == MATCH_NO)
312 goto syntax;
313 if (m == MATCH_ERROR)
314 return MATCH_ERROR;
316 new_var = gfc_get_data_variable ();
317 *new_var = var;
319 if (tail == NULL)
320 d->var = new_var;
321 else
322 tail->next = new_var;
324 tail = new_var;
326 if (gfc_match_char ('/') == MATCH_YES)
327 break;
328 if (gfc_match_char (',') != MATCH_YES)
329 goto syntax;
332 return MATCH_YES;
334 syntax:
335 gfc_syntax_error (ST_DATA);
336 gfc_free_data_all (gfc_current_ns);
337 return MATCH_ERROR;
341 static match
342 match_data_constant (gfc_expr **result)
344 char name[GFC_MAX_SYMBOL_LEN + 1];
345 gfc_symbol *sym, *dt_sym = NULL;
346 gfc_expr *expr;
347 match m;
348 locus old_loc;
350 m = gfc_match_literal_constant (&expr, 1);
351 if (m == MATCH_YES)
353 *result = expr;
354 return MATCH_YES;
357 if (m == MATCH_ERROR)
358 return MATCH_ERROR;
360 m = gfc_match_null (result);
361 if (m != MATCH_NO)
362 return m;
364 old_loc = gfc_current_locus;
366 /* Should this be a structure component, try to match it
367 before matching a name. */
368 m = gfc_match_rvalue (result);
369 if (m == MATCH_ERROR)
370 return m;
372 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
374 if (!gfc_simplify_expr (*result, 0))
375 m = MATCH_ERROR;
376 return m;
378 else if (m == MATCH_YES)
379 gfc_free_expr (*result);
381 gfc_current_locus = old_loc;
383 m = gfc_match_name (name);
384 if (m != MATCH_YES)
385 return m;
387 if (gfc_find_symbol (name, NULL, 1, &sym))
388 return MATCH_ERROR;
390 if (sym && sym->attr.generic)
391 dt_sym = gfc_find_dt_in_generic (sym);
393 if (sym == NULL
394 || (sym->attr.flavor != FL_PARAMETER
395 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
397 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
398 name);
399 return MATCH_ERROR;
401 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
402 return gfc_match_structure_constructor (dt_sym, result);
404 /* Check to see if the value is an initialization array expression. */
405 if (sym->value->expr_type == EXPR_ARRAY)
407 gfc_current_locus = old_loc;
409 m = gfc_match_init_expr (result);
410 if (m == MATCH_ERROR)
411 return m;
413 if (m == MATCH_YES)
415 if (!gfc_simplify_expr (*result, 0))
416 m = MATCH_ERROR;
418 if ((*result)->expr_type == EXPR_CONSTANT)
419 return m;
420 else
422 gfc_error ("Invalid initializer %s in Data statement at %C", name);
423 return MATCH_ERROR;
428 *result = gfc_copy_expr (sym->value);
429 return MATCH_YES;
433 /* Match a list of values in a DATA statement. The leading '/' has
434 already been seen at this point. */
436 static match
437 top_val_list (gfc_data *data)
439 gfc_data_value *new_val, *tail;
440 gfc_expr *expr;
441 match m;
443 tail = NULL;
445 for (;;)
447 m = match_data_constant (&expr);
448 if (m == MATCH_NO)
449 goto syntax;
450 if (m == MATCH_ERROR)
451 return MATCH_ERROR;
453 new_val = gfc_get_data_value ();
454 mpz_init (new_val->repeat);
456 if (tail == NULL)
457 data->value = new_val;
458 else
459 tail->next = new_val;
461 tail = new_val;
463 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
465 tail->expr = expr;
466 mpz_set_ui (tail->repeat, 1);
468 else
470 mpz_set (tail->repeat, expr->value.integer);
471 gfc_free_expr (expr);
473 m = match_data_constant (&tail->expr);
474 if (m == MATCH_NO)
475 goto syntax;
476 if (m == MATCH_ERROR)
477 return MATCH_ERROR;
480 if (gfc_match_char ('/') == MATCH_YES)
481 break;
482 if (gfc_match_char (',') == MATCH_NO)
483 goto syntax;
486 return MATCH_YES;
488 syntax:
489 gfc_syntax_error (ST_DATA);
490 gfc_free_data_all (gfc_current_ns);
491 return MATCH_ERROR;
495 /* Matches an old style initialization. */
497 static match
498 match_old_style_init (const char *name)
500 match m;
501 gfc_symtree *st;
502 gfc_symbol *sym;
503 gfc_data *newdata;
505 /* Set up data structure to hold initializers. */
506 gfc_find_sym_tree (name, NULL, 0, &st);
507 sym = st->n.sym;
509 newdata = gfc_get_data ();
510 newdata->var = gfc_get_data_variable ();
511 newdata->var->expr = gfc_get_variable_expr (st);
512 newdata->where = gfc_current_locus;
514 /* Match initial value list. This also eats the terminal '/'. */
515 m = top_val_list (newdata);
516 if (m != MATCH_YES)
518 free (newdata);
519 return m;
522 if (gfc_pure (NULL))
524 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
525 free (newdata);
526 return MATCH_ERROR;
528 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
530 /* Mark the variable as having appeared in a data statement. */
531 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
533 free (newdata);
534 return MATCH_ERROR;
537 /* Chain in namespace list of DATA initializers. */
538 newdata->next = gfc_current_ns->data;
539 gfc_current_ns->data = newdata;
541 return m;
545 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
546 we are matching a DATA statement and are therefore issuing an error
547 if we encounter something unexpected, if not, we're trying to match
548 an old-style initialization expression of the form INTEGER I /2/. */
550 match
551 gfc_match_data (void)
553 gfc_data *new_data;
554 match m;
556 set_in_match_data (true);
558 for (;;)
560 new_data = gfc_get_data ();
561 new_data->where = gfc_current_locus;
563 m = top_var_list (new_data);
564 if (m != MATCH_YES)
565 goto cleanup;
567 m = top_val_list (new_data);
568 if (m != MATCH_YES)
569 goto cleanup;
571 new_data->next = gfc_current_ns->data;
572 gfc_current_ns->data = new_data;
574 if (gfc_match_eos () == MATCH_YES)
575 break;
577 gfc_match_char (','); /* Optional comma */
580 set_in_match_data (false);
582 if (gfc_pure (NULL))
584 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
585 return MATCH_ERROR;
587 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
589 return MATCH_YES;
591 cleanup:
592 set_in_match_data (false);
593 gfc_free_data (new_data);
594 return MATCH_ERROR;
598 /************************ Declaration statements *********************/
601 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
603 static bool
604 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
606 int i;
608 if ((from->type == AS_ASSUMED_RANK && to->corank)
609 || (to->type == AS_ASSUMED_RANK && from->corank))
611 gfc_error ("The assumed-rank array at %C shall not have a codimension");
612 return false;
615 if (to->rank == 0 && from->rank > 0)
617 to->rank = from->rank;
618 to->type = from->type;
619 to->cray_pointee = from->cray_pointee;
620 to->cp_was_assumed = from->cp_was_assumed;
622 for (i = 0; i < to->corank; i++)
624 to->lower[from->rank + i] = to->lower[i];
625 to->upper[from->rank + i] = to->upper[i];
627 for (i = 0; i < from->rank; i++)
629 if (copy)
631 to->lower[i] = gfc_copy_expr (from->lower[i]);
632 to->upper[i] = gfc_copy_expr (from->upper[i]);
634 else
636 to->lower[i] = from->lower[i];
637 to->upper[i] = from->upper[i];
641 else if (to->corank == 0 && from->corank > 0)
643 to->corank = from->corank;
644 to->cotype = from->cotype;
646 for (i = 0; i < from->corank; i++)
648 if (copy)
650 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
651 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
653 else
655 to->lower[to->rank + i] = from->lower[i];
656 to->upper[to->rank + i] = from->upper[i];
661 return true;
665 /* Match an intent specification. Since this can only happen after an
666 INTENT word, a legal intent-spec must follow. */
668 static sym_intent
669 match_intent_spec (void)
672 if (gfc_match (" ( in out )") == MATCH_YES)
673 return INTENT_INOUT;
674 if (gfc_match (" ( in )") == MATCH_YES)
675 return INTENT_IN;
676 if (gfc_match (" ( out )") == MATCH_YES)
677 return INTENT_OUT;
679 gfc_error ("Bad INTENT specification at %C");
680 return INTENT_UNKNOWN;
684 /* Matches a character length specification, which is either a
685 specification expression, '*', or ':'. */
687 static match
688 char_len_param_value (gfc_expr **expr, bool *deferred)
690 match m;
692 *expr = NULL;
693 *deferred = false;
695 if (gfc_match_char ('*') == MATCH_YES)
696 return MATCH_YES;
698 if (gfc_match_char (':') == MATCH_YES)
700 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
701 return MATCH_ERROR;
703 *deferred = true;
705 return MATCH_YES;
708 m = gfc_match_expr (expr);
710 if (m == MATCH_NO || m == MATCH_ERROR)
711 return m;
713 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
714 return MATCH_ERROR;
716 if ((*expr)->expr_type == EXPR_FUNCTION)
718 if ((*expr)->ts.type == BT_INTEGER
719 || ((*expr)->ts.type == BT_UNKNOWN
720 && strcmp((*expr)->symtree->name, "null") != 0))
721 return MATCH_YES;
723 goto syntax;
725 else if ((*expr)->expr_type == EXPR_CONSTANT)
727 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
728 processor dependent and its value is greater than or equal to zero.
729 F2008, 4.4.3.2: If the character length parameter value evaluates
730 to a negative value, the length of character entities declared
731 is zero. */
733 if ((*expr)->ts.type == BT_INTEGER)
735 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
736 mpz_set_si ((*expr)->value.integer, 0);
738 else
739 goto syntax;
741 else if ((*expr)->expr_type == EXPR_ARRAY)
742 goto syntax;
743 else if ((*expr)->expr_type == EXPR_VARIABLE)
745 gfc_expr *e;
747 e = gfc_copy_expr (*expr);
749 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
750 which causes an ICE if gfc_reduce_init_expr() is called. */
751 if (e->ref && e->ref->type == REF_ARRAY
752 && e->ref->u.ar.type == AR_UNKNOWN
753 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
754 goto syntax;
756 gfc_reduce_init_expr (e);
758 if ((e->ref && e->ref->type == REF_ARRAY
759 && e->ref->u.ar.type != AR_ELEMENT)
760 || (!e->ref && e->expr_type == EXPR_ARRAY))
762 gfc_free_expr (e);
763 goto syntax;
766 gfc_free_expr (e);
769 return m;
771 syntax:
772 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
773 return MATCH_ERROR;
777 /* A character length is a '*' followed by a literal integer or a
778 char_len_param_value in parenthesis. */
780 static match
781 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
783 int length;
784 match m;
786 *deferred = false;
787 m = gfc_match_char ('*');
788 if (m != MATCH_YES)
789 return m;
791 m = gfc_match_small_literal_int (&length, NULL);
792 if (m == MATCH_ERROR)
793 return m;
795 if (m == MATCH_YES)
797 if (obsolescent_check
798 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
799 return MATCH_ERROR;
800 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
801 return m;
804 if (gfc_match_char ('(') == MATCH_NO)
805 goto syntax;
807 m = char_len_param_value (expr, deferred);
808 if (m != MATCH_YES && gfc_matching_function)
810 gfc_undo_symbols ();
811 m = MATCH_YES;
814 if (m == MATCH_ERROR)
815 return m;
816 if (m == MATCH_NO)
817 goto syntax;
819 if (gfc_match_char (')') == MATCH_NO)
821 gfc_free_expr (*expr);
822 *expr = NULL;
823 goto syntax;
826 return MATCH_YES;
828 syntax:
829 gfc_error ("Syntax error in character length specification at %C");
830 return MATCH_ERROR;
834 /* Special subroutine for finding a symbol. Check if the name is found
835 in the current name space. If not, and we're compiling a function or
836 subroutine and the parent compilation unit is an interface, then check
837 to see if the name we've been given is the name of the interface
838 (located in another namespace). */
840 static int
841 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
843 gfc_state_data *s;
844 gfc_symtree *st;
845 int i;
847 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
848 if (i == 0)
850 *result = st ? st->n.sym : NULL;
851 goto end;
854 if (gfc_current_state () != COMP_SUBROUTINE
855 && gfc_current_state () != COMP_FUNCTION)
856 goto end;
858 s = gfc_state_stack->previous;
859 if (s == NULL)
860 goto end;
862 if (s->state != COMP_INTERFACE)
863 goto end;
864 if (s->sym == NULL)
865 goto end; /* Nameless interface. */
867 if (strcmp (name, s->sym->name) == 0)
869 *result = s->sym;
870 return 0;
873 end:
874 return i;
878 /* Special subroutine for getting a symbol node associated with a
879 procedure name, used in SUBROUTINE and FUNCTION statements. The
880 symbol is created in the parent using with symtree node in the
881 child unit pointing to the symbol. If the current namespace has no
882 parent, then the symbol is just created in the current unit. */
884 static int
885 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
887 gfc_symtree *st;
888 gfc_symbol *sym;
889 int rc = 0;
891 /* Module functions have to be left in their own namespace because
892 they have potentially (almost certainly!) already been referenced.
893 In this sense, they are rather like external functions. This is
894 fixed up in resolve.c(resolve_entries), where the symbol name-
895 space is set to point to the master function, so that the fake
896 result mechanism can work. */
897 if (module_fcn_entry)
899 /* Present if entry is declared to be a module procedure. */
900 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
902 if (*result == NULL)
903 rc = gfc_get_symbol (name, NULL, result);
904 else if (!gfc_get_symbol (name, NULL, &sym) && sym
905 && (*result)->ts.type == BT_UNKNOWN
906 && sym->attr.flavor == FL_UNKNOWN)
907 /* Pick up the typespec for the entry, if declared in the function
908 body. Note that this symbol is FL_UNKNOWN because it will
909 only have appeared in a type declaration. The local symtree
910 is set to point to the module symbol and a unique symtree
911 to the local version. This latter ensures a correct clearing
912 of the symbols. */
914 /* If the ENTRY proceeds its specification, we need to ensure
915 that this does not raise a "has no IMPLICIT type" error. */
916 if (sym->ts.type == BT_UNKNOWN)
917 sym->attr.untyped = 1;
919 (*result)->ts = sym->ts;
921 /* Put the symbol in the procedure namespace so that, should
922 the ENTRY precede its specification, the specification
923 can be applied. */
924 (*result)->ns = gfc_current_ns;
926 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
927 st->n.sym = *result;
928 st = gfc_get_unique_symtree (gfc_current_ns);
929 st->n.sym = sym;
932 else
933 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
935 if (rc)
936 return rc;
938 sym = *result;
939 if (sym->attr.proc == PROC_ST_FUNCTION)
940 return rc;
942 if (sym->attr.module_procedure
943 && sym->attr.if_source == IFSRC_IFBODY)
945 /* Create a partially populated interface symbol to carry the
946 characteristics of the procedure and the result. */
947 sym->ts.interface = gfc_new_symbol (name, sym->ns);
948 gfc_add_type (sym->ts.interface, &(sym->ts),
949 &gfc_current_locus);
950 gfc_copy_attr (&sym->ts.interface->attr, &sym->attr, NULL);
951 if (sym->attr.dimension)
952 sym->ts.interface->as = gfc_copy_array_spec (sym->as);
954 /* Ideally, at this point, a copy would be made of the formal
955 arguments and their namespace. However, this does not appear
956 to be necessary, albeit at the expense of not being able to
957 use gfc_compare_interfaces directly. */
959 if (sym->result && sym->result != sym)
961 sym->ts.interface->result = sym->result;
962 sym->result = NULL;
964 else if (sym->result)
966 sym->ts.interface->result = sym->ts.interface;
969 else if (sym && !sym->gfc_new
970 && gfc_current_state () != COMP_INTERFACE)
972 /* Trap another encompassed procedure with the same name. All
973 these conditions are necessary to avoid picking up an entry
974 whose name clashes with that of the encompassing procedure;
975 this is handled using gsymbols to register unique,globally
976 accessible names. */
977 if (sym->attr.flavor != 0
978 && sym->attr.proc != 0
979 && (sym->attr.subroutine || sym->attr.function)
980 && sym->attr.if_source != IFSRC_UNKNOWN)
981 gfc_error_now ("Procedure %qs at %C is already defined at %L",
982 name, &sym->declared_at);
984 /* Trap a procedure with a name the same as interface in the
985 encompassing scope. */
986 if (sym->attr.generic != 0
987 && (sym->attr.subroutine || sym->attr.function)
988 && !sym->attr.mod_proc)
989 gfc_error_now ("Name %qs at %C is already defined"
990 " as a generic interface at %L",
991 name, &sym->declared_at);
993 /* Trap declarations of attributes in encompassing scope. The
994 signature for this is that ts.kind is set. Legitimate
995 references only set ts.type. */
996 if (sym->ts.kind != 0
997 && !sym->attr.implicit_type
998 && sym->attr.proc == 0
999 && gfc_current_ns->parent != NULL
1000 && sym->attr.access == 0
1001 && !module_fcn_entry)
1002 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1003 "and must not have attributes declared at %L",
1004 name, &sym->declared_at);
1007 if (gfc_current_ns->parent == NULL || *result == NULL)
1008 return rc;
1010 /* Module function entries will already have a symtree in
1011 the current namespace but will need one at module level. */
1012 if (module_fcn_entry)
1014 /* Present if entry is declared to be a module procedure. */
1015 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1016 if (st == NULL)
1017 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1019 else
1020 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1022 st->n.sym = sym;
1023 sym->refs++;
1025 /* See if the procedure should be a module procedure. */
1027 if (((sym->ns->proc_name != NULL
1028 && sym->ns->proc_name->attr.flavor == FL_MODULE
1029 && sym->attr.proc != PROC_MODULE)
1030 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1031 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1032 rc = 2;
1034 return rc;
1038 /* Verify that the given symbol representing a parameter is C
1039 interoperable, by checking to see if it was marked as such after
1040 its declaration. If the given symbol is not interoperable, a
1041 warning is reported, thus removing the need to return the status to
1042 the calling function. The standard does not require the user use
1043 one of the iso_c_binding named constants to declare an
1044 interoperable parameter, but we can't be sure if the param is C
1045 interop or not if the user doesn't. For example, integer(4) may be
1046 legal Fortran, but doesn't have meaning in C. It may interop with
1047 a number of the C types, which causes a problem because the
1048 compiler can't know which one. This code is almost certainly not
1049 portable, and the user will get what they deserve if the C type
1050 across platforms isn't always interoperable with integer(4). If
1051 the user had used something like integer(c_int) or integer(c_long),
1052 the compiler could have automatically handled the varying sizes
1053 across platforms. */
1055 bool
1056 gfc_verify_c_interop_param (gfc_symbol *sym)
1058 int is_c_interop = 0;
1059 bool retval = true;
1061 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1062 Don't repeat the checks here. */
1063 if (sym->attr.implicit_type)
1064 return true;
1066 /* For subroutines or functions that are passed to a BIND(C) procedure,
1067 they're interoperable if they're BIND(C) and their params are all
1068 interoperable. */
1069 if (sym->attr.flavor == FL_PROCEDURE)
1071 if (sym->attr.is_bind_c == 0)
1073 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1074 "attribute to be C interoperable", sym->name,
1075 &(sym->declared_at));
1076 return false;
1078 else
1080 if (sym->attr.is_c_interop == 1)
1081 /* We've already checked this procedure; don't check it again. */
1082 return true;
1083 else
1084 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1085 sym->common_block);
1089 /* See if we've stored a reference to a procedure that owns sym. */
1090 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1092 if (sym->ns->proc_name->attr.is_bind_c == 1)
1094 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1096 if (is_c_interop != 1)
1098 /* Make personalized messages to give better feedback. */
1099 if (sym->ts.type == BT_DERIVED)
1100 gfc_error ("Variable %qs at %L is a dummy argument to the "
1101 "BIND(C) procedure %qs but is not C interoperable "
1102 "because derived type %qs is not C interoperable",
1103 sym->name, &(sym->declared_at),
1104 sym->ns->proc_name->name,
1105 sym->ts.u.derived->name);
1106 else if (sym->ts.type == BT_CLASS)
1107 gfc_error ("Variable %qs at %L is a dummy argument to the "
1108 "BIND(C) procedure %qs but is not C interoperable "
1109 "because it is polymorphic",
1110 sym->name, &(sym->declared_at),
1111 sym->ns->proc_name->name);
1112 else if (warn_c_binding_type)
1113 gfc_warning (OPT_Wc_binding_type,
1114 "Variable %qs at %L is a dummy argument of the "
1115 "BIND(C) procedure %qs but may not be C "
1116 "interoperable",
1117 sym->name, &(sym->declared_at),
1118 sym->ns->proc_name->name);
1121 /* Character strings are only C interoperable if they have a
1122 length of 1. */
1123 if (sym->ts.type == BT_CHARACTER)
1125 gfc_charlen *cl = sym->ts.u.cl;
1126 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1127 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1129 gfc_error ("Character argument %qs at %L "
1130 "must be length 1 because "
1131 "procedure %qs is BIND(C)",
1132 sym->name, &sym->declared_at,
1133 sym->ns->proc_name->name);
1134 retval = false;
1138 /* We have to make sure that any param to a bind(c) routine does
1139 not have the allocatable, pointer, or optional attributes,
1140 according to J3/04-007, section 5.1. */
1141 if (sym->attr.allocatable == 1
1142 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1143 "ALLOCATABLE attribute in procedure %qs "
1144 "with BIND(C)", sym->name,
1145 &(sym->declared_at),
1146 sym->ns->proc_name->name))
1147 retval = false;
1149 if (sym->attr.pointer == 1
1150 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1151 "POINTER attribute in procedure %qs "
1152 "with BIND(C)", sym->name,
1153 &(sym->declared_at),
1154 sym->ns->proc_name->name))
1155 retval = false;
1157 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1159 gfc_error ("Scalar variable %qs at %L with POINTER or "
1160 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1161 " supported", sym->name, &(sym->declared_at),
1162 sym->ns->proc_name->name);
1163 retval = false;
1166 if (sym->attr.optional == 1 && sym->attr.value)
1168 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1169 "and the VALUE attribute because procedure %qs "
1170 "is BIND(C)", sym->name, &(sym->declared_at),
1171 sym->ns->proc_name->name);
1172 retval = false;
1174 else if (sym->attr.optional == 1
1175 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1176 "at %L with OPTIONAL attribute in "
1177 "procedure %qs which is BIND(C)",
1178 sym->name, &(sym->declared_at),
1179 sym->ns->proc_name->name))
1180 retval = false;
1182 /* Make sure that if it has the dimension attribute, that it is
1183 either assumed size or explicit shape. Deferred shape is already
1184 covered by the pointer/allocatable attribute. */
1185 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1186 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1187 "at %L as dummy argument to the BIND(C) "
1188 "procedure '%s' at %L", sym->name,
1189 &(sym->declared_at),
1190 sym->ns->proc_name->name,
1191 &(sym->ns->proc_name->declared_at)))
1192 retval = false;
1196 return retval;
1201 /* Function called by variable_decl() that adds a name to the symbol table. */
1203 static bool
1204 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1205 gfc_array_spec **as, locus *var_locus)
1207 symbol_attribute attr;
1208 gfc_symbol *sym;
1210 if (gfc_get_symbol (name, NULL, &sym))
1211 return false;
1213 /* Start updating the symbol table. Add basic type attribute if present. */
1214 if (current_ts.type != BT_UNKNOWN
1215 && (sym->attr.implicit_type == 0
1216 || !gfc_compare_types (&sym->ts, &current_ts))
1217 && !gfc_add_type (sym, &current_ts, var_locus))
1218 return false;
1220 if (sym->ts.type == BT_CHARACTER)
1222 sym->ts.u.cl = cl;
1223 sym->ts.deferred = cl_deferred;
1226 /* Add dimension attribute if present. */
1227 if (!gfc_set_array_spec (sym, *as, var_locus))
1228 return false;
1229 *as = NULL;
1231 /* Add attribute to symbol. The copy is so that we can reset the
1232 dimension attribute. */
1233 attr = current_attr;
1234 attr.dimension = 0;
1235 attr.codimension = 0;
1237 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1238 return false;
1240 /* Finish any work that may need to be done for the binding label,
1241 if it's a bind(c). The bind(c) attr is found before the symbol
1242 is made, and before the symbol name (for data decls), so the
1243 current_ts is holding the binding label, or nothing if the
1244 name= attr wasn't given. Therefore, test here if we're dealing
1245 with a bind(c) and make sure the binding label is set correctly. */
1246 if (sym->attr.is_bind_c == 1)
1248 if (!sym->binding_label)
1250 /* Set the binding label and verify that if a NAME= was specified
1251 then only one identifier was in the entity-decl-list. */
1252 if (!set_binding_label (&sym->binding_label, sym->name,
1253 num_idents_on_line))
1254 return false;
1258 /* See if we know we're in a common block, and if it's a bind(c)
1259 common then we need to make sure we're an interoperable type. */
1260 if (sym->attr.in_common == 1)
1262 /* Test the common block object. */
1263 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1264 && sym->ts.is_c_interop != 1)
1266 gfc_error_now ("Variable %qs in common block %qs at %C "
1267 "must be declared with a C interoperable "
1268 "kind since common block %qs is BIND(C)",
1269 sym->name, sym->common_block->name,
1270 sym->common_block->name);
1271 gfc_clear_error ();
1275 sym->attr.implied_index = 0;
1277 if (sym->ts.type == BT_CLASS)
1278 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1280 return true;
1284 /* Set character constant to the given length. The constant will be padded or
1285 truncated. If we're inside an array constructor without a typespec, we
1286 additionally check that all elements have the same length; check_len -1
1287 means no checking. */
1289 void
1290 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1292 gfc_char_t *s;
1293 int slen;
1295 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1296 gcc_assert (expr->ts.type == BT_CHARACTER);
1298 slen = expr->value.character.length;
1299 if (len != slen)
1301 s = gfc_get_wide_string (len + 1);
1302 memcpy (s, expr->value.character.string,
1303 MIN (len, slen) * sizeof (gfc_char_t));
1304 if (len > slen)
1305 gfc_wide_memset (&s[slen], ' ', len - slen);
1307 if (warn_character_truncation && slen > len)
1308 gfc_warning_now (OPT_Wcharacter_truncation,
1309 "CHARACTER expression at %L is being truncated "
1310 "(%d/%d)", &expr->where, slen, len);
1312 /* Apply the standard by 'hand' otherwise it gets cleared for
1313 initializers. */
1314 if (check_len != -1 && slen != check_len
1315 && !(gfc_option.allow_std & GFC_STD_GNU))
1316 gfc_error_now ("The CHARACTER elements of the array constructor "
1317 "at %L must have the same length (%d/%d)",
1318 &expr->where, slen, check_len);
1320 s[len] = '\0';
1321 free (expr->value.character.string);
1322 expr->value.character.string = s;
1323 expr->value.character.length = len;
1328 /* Function to create and update the enumerator history
1329 using the information passed as arguments.
1330 Pointer "max_enum" is also updated, to point to
1331 enum history node containing largest initializer.
1333 SYM points to the symbol node of enumerator.
1334 INIT points to its enumerator value. */
1336 static void
1337 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1339 enumerator_history *new_enum_history;
1340 gcc_assert (sym != NULL && init != NULL);
1342 new_enum_history = XCNEW (enumerator_history);
1344 new_enum_history->sym = sym;
1345 new_enum_history->initializer = init;
1346 new_enum_history->next = NULL;
1348 if (enum_history == NULL)
1350 enum_history = new_enum_history;
1351 max_enum = enum_history;
1353 else
1355 new_enum_history->next = enum_history;
1356 enum_history = new_enum_history;
1358 if (mpz_cmp (max_enum->initializer->value.integer,
1359 new_enum_history->initializer->value.integer) < 0)
1360 max_enum = new_enum_history;
1365 /* Function to free enum kind history. */
1367 void
1368 gfc_free_enum_history (void)
1370 enumerator_history *current = enum_history;
1371 enumerator_history *next;
1373 while (current != NULL)
1375 next = current->next;
1376 free (current);
1377 current = next;
1379 max_enum = NULL;
1380 enum_history = NULL;
1384 /* Function called by variable_decl() that adds an initialization
1385 expression to a symbol. */
1387 static bool
1388 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1390 symbol_attribute attr;
1391 gfc_symbol *sym;
1392 gfc_expr *init;
1394 init = *initp;
1395 if (find_special (name, &sym, false))
1396 return false;
1398 attr = sym->attr;
1400 /* If this symbol is confirming an implicit parameter type,
1401 then an initialization expression is not allowed. */
1402 if (attr.flavor == FL_PARAMETER
1403 && sym->value != NULL
1404 && *initp != NULL)
1406 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1407 sym->name);
1408 return false;
1411 if (init == NULL)
1413 /* An initializer is required for PARAMETER declarations. */
1414 if (attr.flavor == FL_PARAMETER)
1416 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1417 return false;
1420 else
1422 /* If a variable appears in a DATA block, it cannot have an
1423 initializer. */
1424 if (sym->attr.data)
1426 gfc_error ("Variable %qs at %C with an initializer already "
1427 "appears in a DATA statement", sym->name);
1428 return false;
1431 /* Check if the assignment can happen. This has to be put off
1432 until later for derived type variables and procedure pointers. */
1433 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1434 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1435 && !sym->attr.proc_pointer
1436 && !gfc_check_assign_symbol (sym, NULL, init))
1437 return false;
1439 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1440 && init->ts.type == BT_CHARACTER)
1442 /* Update symbol character length according initializer. */
1443 if (!gfc_check_assign_symbol (sym, NULL, init))
1444 return false;
1446 if (sym->ts.u.cl->length == NULL)
1448 int clen;
1449 /* If there are multiple CHARACTER variables declared on the
1450 same line, we don't want them to share the same length. */
1451 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1453 if (sym->attr.flavor == FL_PARAMETER)
1455 if (init->expr_type == EXPR_CONSTANT)
1457 clen = init->value.character.length;
1458 sym->ts.u.cl->length
1459 = gfc_get_int_expr (gfc_default_integer_kind,
1460 NULL, clen);
1462 else if (init->expr_type == EXPR_ARRAY)
1464 clen = mpz_get_si (init->ts.u.cl->length->value.integer);
1465 sym->ts.u.cl->length
1466 = gfc_get_int_expr (gfc_default_integer_kind,
1467 NULL, clen);
1469 else if (init->ts.u.cl && init->ts.u.cl->length)
1470 sym->ts.u.cl->length =
1471 gfc_copy_expr (sym->value->ts.u.cl->length);
1474 /* Update initializer character length according symbol. */
1475 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1477 int len;
1479 if (!gfc_specification_expr (sym->ts.u.cl->length))
1480 return false;
1482 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1484 if (init->expr_type == EXPR_CONSTANT)
1485 gfc_set_constant_character_len (len, init, -1);
1486 else if (init->expr_type == EXPR_ARRAY)
1488 gfc_constructor *c;
1490 /* Build a new charlen to prevent simplification from
1491 deleting the length before it is resolved. */
1492 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1493 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1495 for (c = gfc_constructor_first (init->value.constructor);
1496 c; c = gfc_constructor_next (c))
1497 gfc_set_constant_character_len (len, c->expr, -1);
1502 /* If sym is implied-shape, set its upper bounds from init. */
1503 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1504 && sym->as->type == AS_IMPLIED_SHAPE)
1506 int dim;
1508 if (init->rank == 0)
1510 gfc_error ("Can't initialize implied-shape array at %L"
1511 " with scalar", &sym->declared_at);
1512 return false;
1515 /* Shape should be present, we get an initialization expression. */
1516 gcc_assert (init->shape);
1518 for (dim = 0; dim < sym->as->rank; ++dim)
1520 int k;
1521 gfc_expr* lower;
1522 gfc_expr* e;
1524 lower = sym->as->lower[dim];
1525 if (lower->expr_type != EXPR_CONSTANT)
1527 gfc_error ("Non-constant lower bound in implied-shape"
1528 " declaration at %L", &lower->where);
1529 return false;
1532 /* All dimensions must be without upper bound. */
1533 gcc_assert (!sym->as->upper[dim]);
1535 k = lower->ts.kind;
1536 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1537 mpz_add (e->value.integer,
1538 lower->value.integer, init->shape[dim]);
1539 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1540 sym->as->upper[dim] = e;
1543 sym->as->type = AS_EXPLICIT;
1546 /* Need to check if the expression we initialized this
1547 to was one of the iso_c_binding named constants. If so,
1548 and we're a parameter (constant), let it be iso_c.
1549 For example:
1550 integer(c_int), parameter :: my_int = c_int
1551 integer(my_int) :: my_int_2
1552 If we mark my_int as iso_c (since we can see it's value
1553 is equal to one of the named constants), then my_int_2
1554 will be considered C interoperable. */
1555 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1557 sym->ts.is_iso_c |= init->ts.is_iso_c;
1558 sym->ts.is_c_interop |= init->ts.is_c_interop;
1559 /* attr bits needed for module files. */
1560 sym->attr.is_iso_c |= init->ts.is_iso_c;
1561 sym->attr.is_c_interop |= init->ts.is_c_interop;
1562 if (init->ts.is_iso_c)
1563 sym->ts.f90_type = init->ts.f90_type;
1566 /* Add initializer. Make sure we keep the ranks sane. */
1567 if (sym->attr.dimension && init->rank == 0)
1569 mpz_t size;
1570 gfc_expr *array;
1571 int n;
1572 if (sym->attr.flavor == FL_PARAMETER
1573 && init->expr_type == EXPR_CONSTANT
1574 && spec_size (sym->as, &size)
1575 && mpz_cmp_si (size, 0) > 0)
1577 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1578 &init->where);
1579 for (n = 0; n < (int)mpz_get_si (size); n++)
1580 gfc_constructor_append_expr (&array->value.constructor,
1581 n == 0
1582 ? init
1583 : gfc_copy_expr (init),
1584 &init->where);
1586 array->shape = gfc_get_shape (sym->as->rank);
1587 for (n = 0; n < sym->as->rank; n++)
1588 spec_dimen_size (sym->as, n, &array->shape[n]);
1590 init = array;
1591 mpz_clear (size);
1593 init->rank = sym->as->rank;
1596 sym->value = init;
1597 if (sym->attr.save == SAVE_NONE)
1598 sym->attr.save = SAVE_IMPLICIT;
1599 *initp = NULL;
1602 return true;
1606 /* Function called by variable_decl() that adds a name to a structure
1607 being built. */
1609 static bool
1610 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1611 gfc_array_spec **as)
1613 gfc_component *c;
1614 bool t = true;
1616 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1617 constructing, it must have the pointer attribute. */
1618 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1619 && current_ts.u.derived == gfc_current_block ()
1620 && current_attr.pointer == 0)
1622 gfc_error ("Component at %C must have the POINTER attribute");
1623 return false;
1626 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1628 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1630 gfc_error ("Array component of structure at %C must have explicit "
1631 "or deferred shape");
1632 return false;
1636 if (!gfc_add_component (gfc_current_block(), name, &c))
1637 return false;
1639 c->ts = current_ts;
1640 if (c->ts.type == BT_CHARACTER)
1641 c->ts.u.cl = cl;
1642 c->attr = current_attr;
1644 c->initializer = *init;
1645 *init = NULL;
1647 c->as = *as;
1648 if (c->as != NULL)
1650 if (c->as->corank)
1651 c->attr.codimension = 1;
1652 if (c->as->rank)
1653 c->attr.dimension = 1;
1655 *as = NULL;
1657 /* Should this ever get more complicated, combine with similar section
1658 in add_init_expr_to_sym into a separate function. */
1659 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1660 && c->ts.u.cl
1661 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1663 int len;
1665 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1666 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1667 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1669 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1671 if (c->initializer->expr_type == EXPR_CONSTANT)
1672 gfc_set_constant_character_len (len, c->initializer, -1);
1673 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1674 c->initializer->ts.u.cl->length->value.integer))
1676 gfc_constructor *ctor;
1677 ctor = gfc_constructor_first (c->initializer->value.constructor);
1679 if (ctor)
1681 int first_len;
1682 bool has_ts = (c->initializer->ts.u.cl
1683 && c->initializer->ts.u.cl->length_from_typespec);
1685 /* Remember the length of the first element for checking
1686 that all elements *in the constructor* have the same
1687 length. This need not be the length of the LHS! */
1688 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1689 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1690 first_len = ctor->expr->value.character.length;
1692 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1693 if (ctor->expr->expr_type == EXPR_CONSTANT)
1695 gfc_set_constant_character_len (len, ctor->expr,
1696 has_ts ? -1 : first_len);
1697 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1703 /* Check array components. */
1704 if (!c->attr.dimension)
1705 goto scalar;
1707 if (c->attr.pointer)
1709 if (c->as->type != AS_DEFERRED)
1711 gfc_error ("Pointer array component of structure at %C must have a "
1712 "deferred shape");
1713 t = false;
1716 else if (c->attr.allocatable)
1718 if (c->as->type != AS_DEFERRED)
1720 gfc_error ("Allocatable component of structure at %C must have a "
1721 "deferred shape");
1722 t = false;
1725 else
1727 if (c->as->type != AS_EXPLICIT)
1729 gfc_error ("Array component of structure at %C must have an "
1730 "explicit shape");
1731 t = false;
1735 scalar:
1736 if (c->ts.type == BT_CLASS)
1738 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1740 if (t)
1741 t = t2;
1744 return t;
1748 /* Match a 'NULL()', and possibly take care of some side effects. */
1750 match
1751 gfc_match_null (gfc_expr **result)
1753 gfc_symbol *sym;
1754 match m, m2 = MATCH_NO;
1756 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1757 return MATCH_ERROR;
1759 if (m == MATCH_NO)
1761 locus old_loc;
1762 char name[GFC_MAX_SYMBOL_LEN + 1];
1764 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1765 return m2;
1767 old_loc = gfc_current_locus;
1768 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1769 return MATCH_ERROR;
1770 if (m2 != MATCH_YES
1771 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1772 return MATCH_ERROR;
1773 if (m2 == MATCH_NO)
1775 gfc_current_locus = old_loc;
1776 return MATCH_NO;
1780 /* The NULL symbol now has to be/become an intrinsic function. */
1781 if (gfc_get_symbol ("null", NULL, &sym))
1783 gfc_error ("NULL() initialization at %C is ambiguous");
1784 return MATCH_ERROR;
1787 gfc_intrinsic_symbol (sym);
1789 if (sym->attr.proc != PROC_INTRINSIC
1790 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1791 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1792 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1793 return MATCH_ERROR;
1795 *result = gfc_get_null_expr (&gfc_current_locus);
1797 /* Invalid per F2008, C512. */
1798 if (m2 == MATCH_YES)
1800 gfc_error ("NULL() initialization at %C may not have MOLD");
1801 return MATCH_ERROR;
1804 return MATCH_YES;
1808 /* Match the initialization expr for a data pointer or procedure pointer. */
1810 static match
1811 match_pointer_init (gfc_expr **init, int procptr)
1813 match m;
1815 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1817 gfc_error ("Initialization of pointer at %C is not allowed in "
1818 "a PURE procedure");
1819 return MATCH_ERROR;
1821 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1823 /* Match NULL() initialization. */
1824 m = gfc_match_null (init);
1825 if (m != MATCH_NO)
1826 return m;
1828 /* Match non-NULL initialization. */
1829 gfc_matching_ptr_assignment = !procptr;
1830 gfc_matching_procptr_assignment = procptr;
1831 m = gfc_match_rvalue (init);
1832 gfc_matching_ptr_assignment = 0;
1833 gfc_matching_procptr_assignment = 0;
1834 if (m == MATCH_ERROR)
1835 return MATCH_ERROR;
1836 else if (m == MATCH_NO)
1838 gfc_error ("Error in pointer initialization at %C");
1839 return MATCH_ERROR;
1842 if (!procptr && !gfc_resolve_expr (*init))
1843 return MATCH_ERROR;
1845 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1846 "initialization at %C"))
1847 return MATCH_ERROR;
1849 return MATCH_YES;
1853 static bool
1854 check_function_name (char *name)
1856 /* In functions that have a RESULT variable defined, the function name always
1857 refers to function calls. Therefore, the name is not allowed to appear in
1858 specification statements. When checking this, be careful about
1859 'hidden' procedure pointer results ('ppr@'). */
1861 if (gfc_current_state () == COMP_FUNCTION)
1863 gfc_symbol *block = gfc_current_block ();
1864 if (block && block->result && block->result != block
1865 && strcmp (block->result->name, "ppr@") != 0
1866 && strcmp (block->name, name) == 0)
1868 gfc_error ("Function name %qs not allowed at %C", name);
1869 return false;
1873 return true;
1877 /* Match a variable name with an optional initializer. When this
1878 subroutine is called, a variable is expected to be parsed next.
1879 Depending on what is happening at the moment, updates either the
1880 symbol table or the current interface. */
1882 static match
1883 variable_decl (int elem)
1885 char name[GFC_MAX_SYMBOL_LEN + 1];
1886 gfc_expr *initializer, *char_len;
1887 gfc_array_spec *as;
1888 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1889 gfc_charlen *cl;
1890 bool cl_deferred;
1891 locus var_locus;
1892 match m;
1893 bool t;
1894 gfc_symbol *sym;
1896 initializer = NULL;
1897 as = NULL;
1898 cp_as = NULL;
1900 /* When we get here, we've just matched a list of attributes and
1901 maybe a type and a double colon. The next thing we expect to see
1902 is the name of the symbol. */
1903 m = gfc_match_name (name);
1904 if (m != MATCH_YES)
1905 goto cleanup;
1907 var_locus = gfc_current_locus;
1909 /* Now we could see the optional array spec. or character length. */
1910 m = gfc_match_array_spec (&as, true, true);
1911 if (m == MATCH_ERROR)
1912 goto cleanup;
1914 if (m == MATCH_NO)
1915 as = gfc_copy_array_spec (current_as);
1916 else if (current_as
1917 && !merge_array_spec (current_as, as, true))
1919 m = MATCH_ERROR;
1920 goto cleanup;
1923 if (flag_cray_pointer)
1924 cp_as = gfc_copy_array_spec (as);
1926 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1927 determine (and check) whether it can be implied-shape. If it
1928 was parsed as assumed-size, change it because PARAMETERs can not
1929 be assumed-size. */
1930 if (as)
1932 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1934 m = MATCH_ERROR;
1935 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1936 name, &var_locus);
1937 goto cleanup;
1940 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1941 && current_attr.flavor == FL_PARAMETER)
1942 as->type = AS_IMPLIED_SHAPE;
1944 if (as->type == AS_IMPLIED_SHAPE
1945 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1946 &var_locus))
1948 m = MATCH_ERROR;
1949 goto cleanup;
1953 char_len = NULL;
1954 cl = NULL;
1955 cl_deferred = false;
1957 if (current_ts.type == BT_CHARACTER)
1959 switch (match_char_length (&char_len, &cl_deferred, false))
1961 case MATCH_YES:
1962 cl = gfc_new_charlen (gfc_current_ns, NULL);
1964 cl->length = char_len;
1965 break;
1967 /* Non-constant lengths need to be copied after the first
1968 element. Also copy assumed lengths. */
1969 case MATCH_NO:
1970 if (elem > 1
1971 && (current_ts.u.cl->length == NULL
1972 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1974 cl = gfc_new_charlen (gfc_current_ns, NULL);
1975 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1977 else
1978 cl = current_ts.u.cl;
1980 cl_deferred = current_ts.deferred;
1982 break;
1984 case MATCH_ERROR:
1985 goto cleanup;
1989 /* The dummy arguments and result of the abreviated form of MODULE
1990 PROCEDUREs, used in SUBMODULES should not be redefined. */
1991 if (gfc_current_ns->proc_name
1992 && gfc_current_ns->proc_name->abr_modproc_decl)
1994 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1995 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
1997 m = MATCH_ERROR;
1998 gfc_error ("'%s' at %C is a redefinition of the declaration "
1999 "in the corresponding interface for MODULE "
2000 "PROCEDURE '%s'", sym->name,
2001 gfc_current_ns->proc_name->name);
2002 goto cleanup;
2006 /* If this symbol has already shown up in a Cray Pointer declaration,
2007 and this is not a component declaration,
2008 then we want to set the type & bail out. */
2009 if (flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
2011 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2012 if (sym != NULL && sym->attr.cray_pointee)
2014 sym->ts.type = current_ts.type;
2015 sym->ts.kind = current_ts.kind;
2016 sym->ts.u.cl = cl;
2017 sym->ts.u.derived = current_ts.u.derived;
2018 sym->ts.is_c_interop = current_ts.is_c_interop;
2019 sym->ts.is_iso_c = current_ts.is_iso_c;
2020 m = MATCH_YES;
2022 /* Check to see if we have an array specification. */
2023 if (cp_as != NULL)
2025 if (sym->as != NULL)
2027 gfc_error ("Duplicate array spec for Cray pointee at %C");
2028 gfc_free_array_spec (cp_as);
2029 m = MATCH_ERROR;
2030 goto cleanup;
2032 else
2034 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2035 gfc_internal_error ("Couldn't set pointee array spec.");
2037 /* Fix the array spec. */
2038 m = gfc_mod_pointee_as (sym->as);
2039 if (m == MATCH_ERROR)
2040 goto cleanup;
2043 goto cleanup;
2045 else
2047 gfc_free_array_spec (cp_as);
2051 /* Procedure pointer as function result. */
2052 if (gfc_current_state () == COMP_FUNCTION
2053 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2054 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2055 strcpy (name, "ppr@");
2057 if (gfc_current_state () == COMP_FUNCTION
2058 && strcmp (name, gfc_current_block ()->name) == 0
2059 && gfc_current_block ()->result
2060 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2061 strcpy (name, "ppr@");
2063 /* OK, we've successfully matched the declaration. Now put the
2064 symbol in the current namespace, because it might be used in the
2065 optional initialization expression for this symbol, e.g. this is
2066 perfectly legal:
2068 integer, parameter :: i = huge(i)
2070 This is only true for parameters or variables of a basic type.
2071 For components of derived types, it is not true, so we don't
2072 create a symbol for those yet. If we fail to create the symbol,
2073 bail out. */
2074 if (gfc_current_state () != COMP_DERIVED
2075 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2077 m = MATCH_ERROR;
2078 goto cleanup;
2081 if (!check_function_name (name))
2083 m = MATCH_ERROR;
2084 goto cleanup;
2087 /* We allow old-style initializations of the form
2088 integer i /2/, j(4) /3*3, 1/
2089 (if no colon has been seen). These are different from data
2090 statements in that initializers are only allowed to apply to the
2091 variable immediately preceding, i.e.
2092 integer i, j /1, 2/
2093 is not allowed. Therefore we have to do some work manually, that
2094 could otherwise be left to the matchers for DATA statements. */
2096 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2098 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2099 "initialization at %C"))
2100 return MATCH_ERROR;
2101 else if (gfc_current_state () == COMP_DERIVED)
2103 gfc_error ("Invalid old style initialization for derived type "
2104 "component at %C");
2105 m = MATCH_ERROR;
2106 goto cleanup;
2109 return match_old_style_init (name);
2112 /* The double colon must be present in order to have initializers.
2113 Otherwise the statement is ambiguous with an assignment statement. */
2114 if (colon_seen)
2116 if (gfc_match (" =>") == MATCH_YES)
2118 if (!current_attr.pointer)
2120 gfc_error ("Initialization at %C isn't for a pointer variable");
2121 m = MATCH_ERROR;
2122 goto cleanup;
2125 m = match_pointer_init (&initializer, 0);
2126 if (m != MATCH_YES)
2127 goto cleanup;
2129 else if (gfc_match_char ('=') == MATCH_YES)
2131 if (current_attr.pointer)
2133 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2134 "not %<=%>");
2135 m = MATCH_ERROR;
2136 goto cleanup;
2139 m = gfc_match_init_expr (&initializer);
2140 if (m == MATCH_NO)
2142 gfc_error ("Expected an initialization expression at %C");
2143 m = MATCH_ERROR;
2146 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2147 && gfc_state_stack->state != COMP_DERIVED)
2149 gfc_error ("Initialization of variable at %C is not allowed in "
2150 "a PURE procedure");
2151 m = MATCH_ERROR;
2154 if (current_attr.flavor != FL_PARAMETER
2155 && gfc_state_stack->state != COMP_DERIVED)
2156 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2158 if (m != MATCH_YES)
2159 goto cleanup;
2163 if (initializer != NULL && current_attr.allocatable
2164 && gfc_current_state () == COMP_DERIVED)
2166 gfc_error ("Initialization of allocatable component at %C is not "
2167 "allowed");
2168 m = MATCH_ERROR;
2169 goto cleanup;
2172 /* Add the initializer. Note that it is fine if initializer is
2173 NULL here, because we sometimes also need to check if a
2174 declaration *must* have an initialization expression. */
2175 if (gfc_current_state () != COMP_DERIVED)
2176 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2177 else
2179 if (current_ts.type == BT_DERIVED
2180 && !current_attr.pointer && !initializer)
2181 initializer = gfc_default_initializer (&current_ts);
2182 t = build_struct (name, cl, &initializer, &as);
2185 m = (t) ? MATCH_YES : MATCH_ERROR;
2187 cleanup:
2188 /* Free stuff up and return. */
2189 gfc_free_expr (initializer);
2190 gfc_free_array_spec (as);
2192 return m;
2196 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2197 This assumes that the byte size is equal to the kind number for
2198 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2200 match
2201 gfc_match_old_kind_spec (gfc_typespec *ts)
2203 match m;
2204 int original_kind;
2206 if (gfc_match_char ('*') != MATCH_YES)
2207 return MATCH_NO;
2209 m = gfc_match_small_literal_int (&ts->kind, NULL);
2210 if (m != MATCH_YES)
2211 return MATCH_ERROR;
2213 original_kind = ts->kind;
2215 /* Massage the kind numbers for complex types. */
2216 if (ts->type == BT_COMPLEX)
2218 if (ts->kind % 2)
2220 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2221 gfc_basic_typename (ts->type), original_kind);
2222 return MATCH_ERROR;
2224 ts->kind /= 2;
2228 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2229 ts->kind = 8;
2231 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2233 if (ts->kind == 4)
2235 if (flag_real4_kind == 8)
2236 ts->kind = 8;
2237 if (flag_real4_kind == 10)
2238 ts->kind = 10;
2239 if (flag_real4_kind == 16)
2240 ts->kind = 16;
2243 if (ts->kind == 8)
2245 if (flag_real8_kind == 4)
2246 ts->kind = 4;
2247 if (flag_real8_kind == 10)
2248 ts->kind = 10;
2249 if (flag_real8_kind == 16)
2250 ts->kind = 16;
2254 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2256 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2257 gfc_basic_typename (ts->type), original_kind);
2258 return MATCH_ERROR;
2261 if (!gfc_notify_std (GFC_STD_GNU,
2262 "Nonstandard type declaration %s*%d at %C",
2263 gfc_basic_typename(ts->type), original_kind))
2264 return MATCH_ERROR;
2266 return MATCH_YES;
2270 /* Match a kind specification. Since kinds are generally optional, we
2271 usually return MATCH_NO if something goes wrong. If a "kind="
2272 string is found, then we know we have an error. */
2274 match
2275 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2277 locus where, loc;
2278 gfc_expr *e;
2279 match m, n;
2280 char c;
2281 const char *msg;
2283 m = MATCH_NO;
2284 n = MATCH_YES;
2285 e = NULL;
2287 where = loc = gfc_current_locus;
2289 if (kind_expr_only)
2290 goto kind_expr;
2292 if (gfc_match_char ('(') == MATCH_NO)
2293 return MATCH_NO;
2295 /* Also gobbles optional text. */
2296 if (gfc_match (" kind = ") == MATCH_YES)
2297 m = MATCH_ERROR;
2299 loc = gfc_current_locus;
2301 kind_expr:
2302 n = gfc_match_init_expr (&e);
2304 if (n != MATCH_YES)
2306 if (gfc_matching_function)
2308 /* The function kind expression might include use associated or
2309 imported parameters and try again after the specification
2310 expressions..... */
2311 if (gfc_match_char (')') != MATCH_YES)
2313 gfc_error ("Missing right parenthesis at %C");
2314 m = MATCH_ERROR;
2315 goto no_match;
2318 gfc_free_expr (e);
2319 gfc_undo_symbols ();
2320 return MATCH_YES;
2322 else
2324 /* ....or else, the match is real. */
2325 if (n == MATCH_NO)
2326 gfc_error ("Expected initialization expression at %C");
2327 if (n != MATCH_YES)
2328 return MATCH_ERROR;
2332 if (e->rank != 0)
2334 gfc_error ("Expected scalar initialization expression at %C");
2335 m = MATCH_ERROR;
2336 goto no_match;
2339 msg = gfc_extract_int (e, &ts->kind);
2341 if (msg != NULL)
2343 gfc_error (msg);
2344 m = MATCH_ERROR;
2345 goto no_match;
2348 /* Before throwing away the expression, let's see if we had a
2349 C interoperable kind (and store the fact). */
2350 if (e->ts.is_c_interop == 1)
2352 /* Mark this as C interoperable if being declared with one
2353 of the named constants from iso_c_binding. */
2354 ts->is_c_interop = e->ts.is_iso_c;
2355 ts->f90_type = e->ts.f90_type;
2358 gfc_free_expr (e);
2359 e = NULL;
2361 /* Ignore errors to this point, if we've gotten here. This means
2362 we ignore the m=MATCH_ERROR from above. */
2363 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2365 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2366 gfc_basic_typename (ts->type));
2367 gfc_current_locus = where;
2368 return MATCH_ERROR;
2371 /* Warn if, e.g., c_int is used for a REAL variable, but not
2372 if, e.g., c_double is used for COMPLEX as the standard
2373 explicitly says that the kind type parameter for complex and real
2374 variable is the same, i.e. c_float == c_float_complex. */
2375 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2376 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2377 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2378 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2379 "is %s", gfc_basic_typename (ts->f90_type), &where,
2380 gfc_basic_typename (ts->type));
2382 gfc_gobble_whitespace ();
2383 if ((c = gfc_next_ascii_char ()) != ')'
2384 && (ts->type != BT_CHARACTER || c != ','))
2386 if (ts->type == BT_CHARACTER)
2387 gfc_error ("Missing right parenthesis or comma at %C");
2388 else
2389 gfc_error ("Missing right parenthesis at %C");
2390 m = MATCH_ERROR;
2392 else
2393 /* All tests passed. */
2394 m = MATCH_YES;
2396 if(m == MATCH_ERROR)
2397 gfc_current_locus = where;
2399 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2400 ts->kind = 8;
2402 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2404 if (ts->kind == 4)
2406 if (flag_real4_kind == 8)
2407 ts->kind = 8;
2408 if (flag_real4_kind == 10)
2409 ts->kind = 10;
2410 if (flag_real4_kind == 16)
2411 ts->kind = 16;
2414 if (ts->kind == 8)
2416 if (flag_real8_kind == 4)
2417 ts->kind = 4;
2418 if (flag_real8_kind == 10)
2419 ts->kind = 10;
2420 if (flag_real8_kind == 16)
2421 ts->kind = 16;
2425 /* Return what we know from the test(s). */
2426 return m;
2428 no_match:
2429 gfc_free_expr (e);
2430 gfc_current_locus = where;
2431 return m;
2435 static match
2436 match_char_kind (int * kind, int * is_iso_c)
2438 locus where;
2439 gfc_expr *e;
2440 match m, n;
2441 const char *msg;
2443 m = MATCH_NO;
2444 e = NULL;
2445 where = gfc_current_locus;
2447 n = gfc_match_init_expr (&e);
2449 if (n != MATCH_YES && gfc_matching_function)
2451 /* The expression might include use-associated or imported
2452 parameters and try again after the specification
2453 expressions. */
2454 gfc_free_expr (e);
2455 gfc_undo_symbols ();
2456 return MATCH_YES;
2459 if (n == MATCH_NO)
2460 gfc_error ("Expected initialization expression at %C");
2461 if (n != MATCH_YES)
2462 return MATCH_ERROR;
2464 if (e->rank != 0)
2466 gfc_error ("Expected scalar initialization expression at %C");
2467 m = MATCH_ERROR;
2468 goto no_match;
2471 msg = gfc_extract_int (e, kind);
2472 *is_iso_c = e->ts.is_iso_c;
2473 if (msg != NULL)
2475 gfc_error (msg);
2476 m = MATCH_ERROR;
2477 goto no_match;
2480 gfc_free_expr (e);
2482 /* Ignore errors to this point, if we've gotten here. This means
2483 we ignore the m=MATCH_ERROR from above. */
2484 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2486 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2487 m = MATCH_ERROR;
2489 else
2490 /* All tests passed. */
2491 m = MATCH_YES;
2493 if (m == MATCH_ERROR)
2494 gfc_current_locus = where;
2496 /* Return what we know from the test(s). */
2497 return m;
2499 no_match:
2500 gfc_free_expr (e);
2501 gfc_current_locus = where;
2502 return m;
2506 /* Match the various kind/length specifications in a CHARACTER
2507 declaration. We don't return MATCH_NO. */
2509 match
2510 gfc_match_char_spec (gfc_typespec *ts)
2512 int kind, seen_length, is_iso_c;
2513 gfc_charlen *cl;
2514 gfc_expr *len;
2515 match m;
2516 bool deferred;
2518 len = NULL;
2519 seen_length = 0;
2520 kind = 0;
2521 is_iso_c = 0;
2522 deferred = false;
2524 /* Try the old-style specification first. */
2525 old_char_selector = 0;
2527 m = match_char_length (&len, &deferred, true);
2528 if (m != MATCH_NO)
2530 if (m == MATCH_YES)
2531 old_char_selector = 1;
2532 seen_length = 1;
2533 goto done;
2536 m = gfc_match_char ('(');
2537 if (m != MATCH_YES)
2539 m = MATCH_YES; /* Character without length is a single char. */
2540 goto done;
2543 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2544 if (gfc_match (" kind =") == MATCH_YES)
2546 m = match_char_kind (&kind, &is_iso_c);
2548 if (m == MATCH_ERROR)
2549 goto done;
2550 if (m == MATCH_NO)
2551 goto syntax;
2553 if (gfc_match (" , len =") == MATCH_NO)
2554 goto rparen;
2556 m = char_len_param_value (&len, &deferred);
2557 if (m == MATCH_NO)
2558 goto syntax;
2559 if (m == MATCH_ERROR)
2560 goto done;
2561 seen_length = 1;
2563 goto rparen;
2566 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2567 if (gfc_match (" len =") == MATCH_YES)
2569 m = char_len_param_value (&len, &deferred);
2570 if (m == MATCH_NO)
2571 goto syntax;
2572 if (m == MATCH_ERROR)
2573 goto done;
2574 seen_length = 1;
2576 if (gfc_match_char (')') == MATCH_YES)
2577 goto done;
2579 if (gfc_match (" , kind =") != MATCH_YES)
2580 goto syntax;
2582 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2583 goto done;
2585 goto rparen;
2588 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2589 m = char_len_param_value (&len, &deferred);
2590 if (m == MATCH_NO)
2591 goto syntax;
2592 if (m == MATCH_ERROR)
2593 goto done;
2594 seen_length = 1;
2596 m = gfc_match_char (')');
2597 if (m == MATCH_YES)
2598 goto done;
2600 if (gfc_match_char (',') != MATCH_YES)
2601 goto syntax;
2603 gfc_match (" kind ="); /* Gobble optional text. */
2605 m = match_char_kind (&kind, &is_iso_c);
2606 if (m == MATCH_ERROR)
2607 goto done;
2608 if (m == MATCH_NO)
2609 goto syntax;
2611 rparen:
2612 /* Require a right-paren at this point. */
2613 m = gfc_match_char (')');
2614 if (m == MATCH_YES)
2615 goto done;
2617 syntax:
2618 gfc_error ("Syntax error in CHARACTER declaration at %C");
2619 m = MATCH_ERROR;
2620 gfc_free_expr (len);
2621 return m;
2623 done:
2624 /* Deal with character functions after USE and IMPORT statements. */
2625 if (gfc_matching_function)
2627 gfc_free_expr (len);
2628 gfc_undo_symbols ();
2629 return MATCH_YES;
2632 if (m != MATCH_YES)
2634 gfc_free_expr (len);
2635 return m;
2638 /* Do some final massaging of the length values. */
2639 cl = gfc_new_charlen (gfc_current_ns, NULL);
2641 if (seen_length == 0)
2642 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2643 else
2644 cl->length = len;
2646 ts->u.cl = cl;
2647 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2648 ts->deferred = deferred;
2650 /* We have to know if it was a C interoperable kind so we can
2651 do accurate type checking of bind(c) procs, etc. */
2652 if (kind != 0)
2653 /* Mark this as C interoperable if being declared with one
2654 of the named constants from iso_c_binding. */
2655 ts->is_c_interop = is_iso_c;
2656 else if (len != NULL)
2657 /* Here, we might have parsed something such as: character(c_char)
2658 In this case, the parsing code above grabs the c_char when
2659 looking for the length (line 1690, roughly). it's the last
2660 testcase for parsing the kind params of a character variable.
2661 However, it's not actually the length. this seems like it
2662 could be an error.
2663 To see if the user used a C interop kind, test the expr
2664 of the so called length, and see if it's C interoperable. */
2665 ts->is_c_interop = len->ts.is_iso_c;
2667 return MATCH_YES;
2671 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2672 structure to the matched specification. This is necessary for FUNCTION and
2673 IMPLICIT statements.
2675 If implicit_flag is nonzero, then we don't check for the optional
2676 kind specification. Not doing so is needed for matching an IMPLICIT
2677 statement correctly. */
2679 match
2680 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2682 char name[GFC_MAX_SYMBOL_LEN + 1];
2683 gfc_symbol *sym, *dt_sym;
2684 match m;
2685 char c;
2686 bool seen_deferred_kind, matched_type;
2687 const char *dt_name;
2689 /* A belt and braces check that the typespec is correctly being treated
2690 as a deferred characteristic association. */
2691 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2692 && (gfc_current_block ()->result->ts.kind == -1)
2693 && (ts->kind == -1);
2694 gfc_clear_ts (ts);
2695 if (seen_deferred_kind)
2696 ts->kind = -1;
2698 /* Clear the current binding label, in case one is given. */
2699 curr_binding_label = NULL;
2701 if (gfc_match (" byte") == MATCH_YES)
2703 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2704 return MATCH_ERROR;
2706 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2708 gfc_error ("BYTE type used at %C "
2709 "is not available on the target machine");
2710 return MATCH_ERROR;
2713 ts->type = BT_INTEGER;
2714 ts->kind = 1;
2715 return MATCH_YES;
2719 m = gfc_match (" type (");
2720 matched_type = (m == MATCH_YES);
2721 if (matched_type)
2723 gfc_gobble_whitespace ();
2724 if (gfc_peek_ascii_char () == '*')
2726 if ((m = gfc_match ("*)")) != MATCH_YES)
2727 return m;
2728 if (gfc_current_state () == COMP_DERIVED)
2730 gfc_error ("Assumed type at %C is not allowed for components");
2731 return MATCH_ERROR;
2733 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2734 "at %C"))
2735 return MATCH_ERROR;
2736 ts->type = BT_ASSUMED;
2737 return MATCH_YES;
2740 m = gfc_match ("%n", name);
2741 matched_type = (m == MATCH_YES);
2744 if ((matched_type && strcmp ("integer", name) == 0)
2745 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2747 ts->type = BT_INTEGER;
2748 ts->kind = gfc_default_integer_kind;
2749 goto get_kind;
2752 if ((matched_type && strcmp ("character", name) == 0)
2753 || (!matched_type && gfc_match (" character") == MATCH_YES))
2755 if (matched_type
2756 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2757 "intrinsic-type-spec at %C"))
2758 return MATCH_ERROR;
2760 ts->type = BT_CHARACTER;
2761 if (implicit_flag == 0)
2762 m = gfc_match_char_spec (ts);
2763 else
2764 m = MATCH_YES;
2766 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2767 m = MATCH_ERROR;
2769 return m;
2772 if ((matched_type && strcmp ("real", name) == 0)
2773 || (!matched_type && gfc_match (" real") == MATCH_YES))
2775 ts->type = BT_REAL;
2776 ts->kind = gfc_default_real_kind;
2777 goto get_kind;
2780 if ((matched_type
2781 && (strcmp ("doubleprecision", name) == 0
2782 || (strcmp ("double", name) == 0
2783 && gfc_match (" precision") == MATCH_YES)))
2784 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2786 if (matched_type
2787 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2788 "intrinsic-type-spec at %C"))
2789 return MATCH_ERROR;
2790 if (matched_type && gfc_match_char (')') != MATCH_YES)
2791 return MATCH_ERROR;
2793 ts->type = BT_REAL;
2794 ts->kind = gfc_default_double_kind;
2795 return MATCH_YES;
2798 if ((matched_type && strcmp ("complex", name) == 0)
2799 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2801 ts->type = BT_COMPLEX;
2802 ts->kind = gfc_default_complex_kind;
2803 goto get_kind;
2806 if ((matched_type
2807 && (strcmp ("doublecomplex", name) == 0
2808 || (strcmp ("double", name) == 0
2809 && gfc_match (" complex") == MATCH_YES)))
2810 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2812 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2813 return MATCH_ERROR;
2815 if (matched_type
2816 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2817 "intrinsic-type-spec at %C"))
2818 return MATCH_ERROR;
2820 if (matched_type && gfc_match_char (')') != MATCH_YES)
2821 return MATCH_ERROR;
2823 ts->type = BT_COMPLEX;
2824 ts->kind = gfc_default_double_kind;
2825 return MATCH_YES;
2828 if ((matched_type && strcmp ("logical", name) == 0)
2829 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2831 ts->type = BT_LOGICAL;
2832 ts->kind = gfc_default_logical_kind;
2833 goto get_kind;
2836 if (matched_type)
2837 m = gfc_match_char (')');
2839 if (m == MATCH_YES)
2840 ts->type = BT_DERIVED;
2841 else
2843 /* Match CLASS declarations. */
2844 m = gfc_match (" class ( * )");
2845 if (m == MATCH_ERROR)
2846 return MATCH_ERROR;
2847 else if (m == MATCH_YES)
2849 gfc_symbol *upe;
2850 gfc_symtree *st;
2851 ts->type = BT_CLASS;
2852 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2853 if (upe == NULL)
2855 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2856 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2857 st->n.sym = upe;
2858 gfc_set_sym_referenced (upe);
2859 upe->refs++;
2860 upe->ts.type = BT_VOID;
2861 upe->attr.unlimited_polymorphic = 1;
2862 /* This is essential to force the construction of
2863 unlimited polymorphic component class containers. */
2864 upe->attr.zero_comp = 1;
2865 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2866 &gfc_current_locus))
2867 return MATCH_ERROR;
2869 else
2871 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2872 if (st == NULL)
2873 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2874 st->n.sym = upe;
2875 upe->refs++;
2877 ts->u.derived = upe;
2878 return m;
2881 m = gfc_match (" class ( %n )", name);
2882 if (m != MATCH_YES)
2883 return m;
2884 ts->type = BT_CLASS;
2886 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2887 return MATCH_ERROR;
2890 /* Defer association of the derived type until the end of the
2891 specification block. However, if the derived type can be
2892 found, add it to the typespec. */
2893 if (gfc_matching_function)
2895 ts->u.derived = NULL;
2896 if (gfc_current_state () != COMP_INTERFACE
2897 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2899 sym = gfc_find_dt_in_generic (sym);
2900 ts->u.derived = sym;
2902 return MATCH_YES;
2905 /* Search for the name but allow the components to be defined later. If
2906 type = -1, this typespec has been seen in a function declaration but
2907 the type could not be accessed at that point. The actual derived type is
2908 stored in a symtree with the first letter of the name capitalized; the
2909 symtree with the all lower-case name contains the associated
2910 generic function. */
2911 dt_name = gfc_get_string ("%c%s",
2912 (char) TOUPPER ((unsigned char) name[0]),
2913 (const char*)&name[1]);
2914 sym = NULL;
2915 dt_sym = NULL;
2916 if (ts->kind != -1)
2918 gfc_get_ha_symbol (name, &sym);
2919 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2921 gfc_error ("Type name %qs at %C is ambiguous", name);
2922 return MATCH_ERROR;
2924 if (sym->generic && !dt_sym)
2925 dt_sym = gfc_find_dt_in_generic (sym);
2927 else if (ts->kind == -1)
2929 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2930 || gfc_current_ns->has_import_set;
2931 gfc_find_symbol (name, NULL, iface, &sym);
2932 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2934 gfc_error ("Type name %qs at %C is ambiguous", name);
2935 return MATCH_ERROR;
2937 if (sym && sym->generic && !dt_sym)
2938 dt_sym = gfc_find_dt_in_generic (sym);
2940 ts->kind = 0;
2941 if (sym == NULL)
2942 return MATCH_NO;
2945 if ((sym->attr.flavor != FL_UNKNOWN
2946 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2947 || sym->attr.subroutine)
2949 gfc_error ("Type name %qs at %C conflicts with previously declared "
2950 "entity at %L, which has the same name", name,
2951 &sym->declared_at);
2952 return MATCH_ERROR;
2955 gfc_save_symbol_data (sym);
2956 gfc_set_sym_referenced (sym);
2957 if (!sym->attr.generic
2958 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2959 return MATCH_ERROR;
2961 if (!sym->attr.function
2962 && !gfc_add_function (&sym->attr, sym->name, NULL))
2963 return MATCH_ERROR;
2965 if (!dt_sym)
2967 gfc_interface *intr, *head;
2969 /* Use upper case to save the actual derived-type symbol. */
2970 gfc_get_symbol (dt_name, NULL, &dt_sym);
2971 dt_sym->name = gfc_get_string (sym->name);
2972 head = sym->generic;
2973 intr = gfc_get_interface ();
2974 intr->sym = dt_sym;
2975 intr->where = gfc_current_locus;
2976 intr->next = head;
2977 sym->generic = intr;
2978 sym->attr.if_source = IFSRC_DECL;
2980 else
2981 gfc_save_symbol_data (dt_sym);
2983 gfc_set_sym_referenced (dt_sym);
2985 if (dt_sym->attr.flavor != FL_DERIVED
2986 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2987 return MATCH_ERROR;
2989 ts->u.derived = dt_sym;
2991 return MATCH_YES;
2993 get_kind:
2994 if (matched_type
2995 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2996 "intrinsic-type-spec at %C"))
2997 return MATCH_ERROR;
2999 /* For all types except double, derived and character, look for an
3000 optional kind specifier. MATCH_NO is actually OK at this point. */
3001 if (implicit_flag == 1)
3003 if (matched_type && gfc_match_char (')') != MATCH_YES)
3004 return MATCH_ERROR;
3006 return MATCH_YES;
3009 if (gfc_current_form == FORM_FREE)
3011 c = gfc_peek_ascii_char ();
3012 if (!gfc_is_whitespace (c) && c != '*' && c != '('
3013 && c != ':' && c != ',')
3015 if (matched_type && c == ')')
3017 gfc_next_ascii_char ();
3018 return MATCH_YES;
3020 return MATCH_NO;
3024 m = gfc_match_kind_spec (ts, false);
3025 if (m == MATCH_NO && ts->type != BT_CHARACTER)
3027 m = gfc_match_old_kind_spec (ts);
3028 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
3029 return MATCH_ERROR;
3032 if (matched_type && gfc_match_char (')') != MATCH_YES)
3033 return MATCH_ERROR;
3035 /* Defer association of the KIND expression of function results
3036 until after USE and IMPORT statements. */
3037 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
3038 || gfc_matching_function)
3039 return MATCH_YES;
3041 if (m == MATCH_NO)
3042 m = MATCH_YES; /* No kind specifier found. */
3044 return m;
3048 /* Match an IMPLICIT NONE statement. Actually, this statement is
3049 already matched in parse.c, or we would not end up here in the
3050 first place. So the only thing we need to check, is if there is
3051 trailing garbage. If not, the match is successful. */
3053 match
3054 gfc_match_implicit_none (void)
3056 char c;
3057 match m;
3058 char name[GFC_MAX_SYMBOL_LEN + 1];
3059 bool type = false;
3060 bool external = false;
3061 locus cur_loc = gfc_current_locus;
3063 if (gfc_current_ns->seen_implicit_none
3064 || gfc_current_ns->has_implicit_none_export)
3066 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3067 return MATCH_ERROR;
3070 gfc_gobble_whitespace ();
3071 c = gfc_peek_ascii_char ();
3072 if (c == '(')
3074 (void) gfc_next_ascii_char ();
3075 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3076 return MATCH_ERROR;
3078 gfc_gobble_whitespace ();
3079 if (gfc_peek_ascii_char () == ')')
3081 (void) gfc_next_ascii_char ();
3082 type = true;
3084 else
3085 for(;;)
3087 m = gfc_match (" %n", name);
3088 if (m != MATCH_YES)
3089 return MATCH_ERROR;
3091 if (strcmp (name, "type") == 0)
3092 type = true;
3093 else if (strcmp (name, "external") == 0)
3094 external = true;
3095 else
3096 return MATCH_ERROR;
3098 gfc_gobble_whitespace ();
3099 c = gfc_next_ascii_char ();
3100 if (c == ',')
3101 continue;
3102 if (c == ')')
3103 break;
3104 return MATCH_ERROR;
3107 else
3108 type = true;
3110 if (gfc_match_eos () != MATCH_YES)
3111 return MATCH_ERROR;
3113 gfc_set_implicit_none (type, external, &cur_loc);
3115 return MATCH_YES;
3119 /* Match the letter range(s) of an IMPLICIT statement. */
3121 static match
3122 match_implicit_range (void)
3124 char c, c1, c2;
3125 int inner;
3126 locus cur_loc;
3128 cur_loc = gfc_current_locus;
3130 gfc_gobble_whitespace ();
3131 c = gfc_next_ascii_char ();
3132 if (c != '(')
3134 gfc_error ("Missing character range in IMPLICIT at %C");
3135 goto bad;
3138 inner = 1;
3139 while (inner)
3141 gfc_gobble_whitespace ();
3142 c1 = gfc_next_ascii_char ();
3143 if (!ISALPHA (c1))
3144 goto bad;
3146 gfc_gobble_whitespace ();
3147 c = gfc_next_ascii_char ();
3149 switch (c)
3151 case ')':
3152 inner = 0; /* Fall through. */
3154 case ',':
3155 c2 = c1;
3156 break;
3158 case '-':
3159 gfc_gobble_whitespace ();
3160 c2 = gfc_next_ascii_char ();
3161 if (!ISALPHA (c2))
3162 goto bad;
3164 gfc_gobble_whitespace ();
3165 c = gfc_next_ascii_char ();
3167 if ((c != ',') && (c != ')'))
3168 goto bad;
3169 if (c == ')')
3170 inner = 0;
3172 break;
3174 default:
3175 goto bad;
3178 if (c1 > c2)
3180 gfc_error ("Letters must be in alphabetic order in "
3181 "IMPLICIT statement at %C");
3182 goto bad;
3185 /* See if we can add the newly matched range to the pending
3186 implicits from this IMPLICIT statement. We do not check for
3187 conflicts with whatever earlier IMPLICIT statements may have
3188 set. This is done when we've successfully finished matching
3189 the current one. */
3190 if (!gfc_add_new_implicit_range (c1, c2))
3191 goto bad;
3194 return MATCH_YES;
3196 bad:
3197 gfc_syntax_error (ST_IMPLICIT);
3199 gfc_current_locus = cur_loc;
3200 return MATCH_ERROR;
3204 /* Match an IMPLICIT statement, storing the types for
3205 gfc_set_implicit() if the statement is accepted by the parser.
3206 There is a strange looking, but legal syntactic construction
3207 possible. It looks like:
3209 IMPLICIT INTEGER (a-b) (c-d)
3211 This is legal if "a-b" is a constant expression that happens to
3212 equal one of the legal kinds for integers. The real problem
3213 happens with an implicit specification that looks like:
3215 IMPLICIT INTEGER (a-b)
3217 In this case, a typespec matcher that is "greedy" (as most of the
3218 matchers are) gobbles the character range as a kindspec, leaving
3219 nothing left. We therefore have to go a bit more slowly in the
3220 matching process by inhibiting the kindspec checking during
3221 typespec matching and checking for a kind later. */
3223 match
3224 gfc_match_implicit (void)
3226 gfc_typespec ts;
3227 locus cur_loc;
3228 char c;
3229 match m;
3231 if (gfc_current_ns->seen_implicit_none)
3233 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3234 "statement");
3235 return MATCH_ERROR;
3238 gfc_clear_ts (&ts);
3240 /* We don't allow empty implicit statements. */
3241 if (gfc_match_eos () == MATCH_YES)
3243 gfc_error ("Empty IMPLICIT statement at %C");
3244 return MATCH_ERROR;
3249 /* First cleanup. */
3250 gfc_clear_new_implicit ();
3252 /* A basic type is mandatory here. */
3253 m = gfc_match_decl_type_spec (&ts, 1);
3254 if (m == MATCH_ERROR)
3255 goto error;
3256 if (m == MATCH_NO)
3257 goto syntax;
3259 cur_loc = gfc_current_locus;
3260 m = match_implicit_range ();
3262 if (m == MATCH_YES)
3264 /* We may have <TYPE> (<RANGE>). */
3265 gfc_gobble_whitespace ();
3266 c = gfc_peek_ascii_char ();
3267 if (c == ',' || c == '\n' || c == ';' || c == '!')
3269 /* Check for CHARACTER with no length parameter. */
3270 if (ts.type == BT_CHARACTER && !ts.u.cl)
3272 ts.kind = gfc_default_character_kind;
3273 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3274 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3275 NULL, 1);
3278 /* Record the Successful match. */
3279 if (!gfc_merge_new_implicit (&ts))
3280 return MATCH_ERROR;
3281 if (c == ',')
3282 c = gfc_next_ascii_char ();
3283 else if (gfc_match_eos () == MATCH_ERROR)
3284 goto error;
3285 continue;
3288 gfc_current_locus = cur_loc;
3291 /* Discard the (incorrectly) matched range. */
3292 gfc_clear_new_implicit ();
3294 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3295 if (ts.type == BT_CHARACTER)
3296 m = gfc_match_char_spec (&ts);
3297 else
3299 m = gfc_match_kind_spec (&ts, false);
3300 if (m == MATCH_NO)
3302 m = gfc_match_old_kind_spec (&ts);
3303 if (m == MATCH_ERROR)
3304 goto error;
3305 if (m == MATCH_NO)
3306 goto syntax;
3309 if (m == MATCH_ERROR)
3310 goto error;
3312 m = match_implicit_range ();
3313 if (m == MATCH_ERROR)
3314 goto error;
3315 if (m == MATCH_NO)
3316 goto syntax;
3318 gfc_gobble_whitespace ();
3319 c = gfc_next_ascii_char ();
3320 if (c != ',' && gfc_match_eos () != MATCH_YES)
3321 goto syntax;
3323 if (!gfc_merge_new_implicit (&ts))
3324 return MATCH_ERROR;
3326 while (c == ',');
3328 return MATCH_YES;
3330 syntax:
3331 gfc_syntax_error (ST_IMPLICIT);
3333 error:
3334 return MATCH_ERROR;
3338 match
3339 gfc_match_import (void)
3341 char name[GFC_MAX_SYMBOL_LEN + 1];
3342 match m;
3343 gfc_symbol *sym;
3344 gfc_symtree *st;
3346 if (gfc_current_ns->proc_name == NULL
3347 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3349 gfc_error ("IMPORT statement at %C only permitted in "
3350 "an INTERFACE body");
3351 return MATCH_ERROR;
3354 if (gfc_current_ns->proc_name->attr.module_procedure)
3356 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3357 "in a module procedure interface body");
3358 return MATCH_ERROR;
3361 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3362 return MATCH_ERROR;
3364 if (gfc_match_eos () == MATCH_YES)
3366 /* All host variables should be imported. */
3367 gfc_current_ns->has_import_set = 1;
3368 return MATCH_YES;
3371 if (gfc_match (" ::") == MATCH_YES)
3373 if (gfc_match_eos () == MATCH_YES)
3375 gfc_error ("Expecting list of named entities at %C");
3376 return MATCH_ERROR;
3380 for(;;)
3382 sym = NULL;
3383 m = gfc_match (" %n", name);
3384 switch (m)
3386 case MATCH_YES:
3387 if (gfc_current_ns->parent != NULL
3388 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3390 gfc_error ("Type name %qs at %C is ambiguous", name);
3391 return MATCH_ERROR;
3393 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3394 && gfc_find_symbol (name,
3395 gfc_current_ns->proc_name->ns->parent,
3396 1, &sym))
3398 gfc_error ("Type name %qs at %C is ambiguous", name);
3399 return MATCH_ERROR;
3402 if (sym == NULL)
3404 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3405 "at %C - does not exist.", name);
3406 return MATCH_ERROR;
3409 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3411 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3412 "at %C", name);
3413 goto next_item;
3416 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3417 st->n.sym = sym;
3418 sym->refs++;
3419 sym->attr.imported = 1;
3421 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3423 /* The actual derived type is stored in a symtree with the first
3424 letter of the name capitalized; the symtree with the all
3425 lower-case name contains the associated generic function. */
3426 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3427 gfc_get_string ("%c%s",
3428 (char) TOUPPER ((unsigned char) name[0]),
3429 &name[1]));
3430 st->n.sym = sym;
3431 sym->refs++;
3432 sym->attr.imported = 1;
3435 goto next_item;
3437 case MATCH_NO:
3438 break;
3440 case MATCH_ERROR:
3441 return MATCH_ERROR;
3444 next_item:
3445 if (gfc_match_eos () == MATCH_YES)
3446 break;
3447 if (gfc_match_char (',') != MATCH_YES)
3448 goto syntax;
3451 return MATCH_YES;
3453 syntax:
3454 gfc_error ("Syntax error in IMPORT statement at %C");
3455 return MATCH_ERROR;
3459 /* A minimal implementation of gfc_match without whitespace, escape
3460 characters or variable arguments. Returns true if the next
3461 characters match the TARGET template exactly. */
3463 static bool
3464 match_string_p (const char *target)
3466 const char *p;
3468 for (p = target; *p; p++)
3469 if ((char) gfc_next_ascii_char () != *p)
3470 return false;
3471 return true;
3474 /* Matches an attribute specification including array specs. If
3475 successful, leaves the variables current_attr and current_as
3476 holding the specification. Also sets the colon_seen variable for
3477 later use by matchers associated with initializations.
3479 This subroutine is a little tricky in the sense that we don't know
3480 if we really have an attr-spec until we hit the double colon.
3481 Until that time, we can only return MATCH_NO. This forces us to
3482 check for duplicate specification at this level. */
3484 static match
3485 match_attr_spec (void)
3487 /* Modifiers that can exist in a type statement. */
3488 enum
3489 { GFC_DECL_BEGIN = 0,
3490 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3491 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3492 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3493 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3494 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3495 DECL_NONE, GFC_DECL_END /* Sentinel */
3498 /* GFC_DECL_END is the sentinel, index starts at 0. */
3499 #define NUM_DECL GFC_DECL_END
3501 locus start, seen_at[NUM_DECL];
3502 int seen[NUM_DECL];
3503 unsigned int d;
3504 const char *attr;
3505 match m;
3506 bool t;
3508 gfc_clear_attr (&current_attr);
3509 start = gfc_current_locus;
3511 current_as = NULL;
3512 colon_seen = 0;
3514 /* See if we get all of the keywords up to the final double colon. */
3515 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3516 seen[d] = 0;
3518 for (;;)
3520 char ch;
3522 d = DECL_NONE;
3523 gfc_gobble_whitespace ();
3525 ch = gfc_next_ascii_char ();
3526 if (ch == ':')
3528 /* This is the successful exit condition for the loop. */
3529 if (gfc_next_ascii_char () == ':')
3530 break;
3532 else if (ch == ',')
3534 gfc_gobble_whitespace ();
3535 switch (gfc_peek_ascii_char ())
3537 case 'a':
3538 gfc_next_ascii_char ();
3539 switch (gfc_next_ascii_char ())
3541 case 'l':
3542 if (match_string_p ("locatable"))
3544 /* Matched "allocatable". */
3545 d = DECL_ALLOCATABLE;
3547 break;
3549 case 's':
3550 if (match_string_p ("ynchronous"))
3552 /* Matched "asynchronous". */
3553 d = DECL_ASYNCHRONOUS;
3555 break;
3557 break;
3559 case 'b':
3560 /* Try and match the bind(c). */
3561 m = gfc_match_bind_c (NULL, true);
3562 if (m == MATCH_YES)
3563 d = DECL_IS_BIND_C;
3564 else if (m == MATCH_ERROR)
3565 goto cleanup;
3566 break;
3568 case 'c':
3569 gfc_next_ascii_char ();
3570 if ('o' != gfc_next_ascii_char ())
3571 break;
3572 switch (gfc_next_ascii_char ())
3574 case 'd':
3575 if (match_string_p ("imension"))
3577 d = DECL_CODIMENSION;
3578 break;
3580 case 'n':
3581 if (match_string_p ("tiguous"))
3583 d = DECL_CONTIGUOUS;
3584 break;
3587 break;
3589 case 'd':
3590 if (match_string_p ("dimension"))
3591 d = DECL_DIMENSION;
3592 break;
3594 case 'e':
3595 if (match_string_p ("external"))
3596 d = DECL_EXTERNAL;
3597 break;
3599 case 'i':
3600 if (match_string_p ("int"))
3602 ch = gfc_next_ascii_char ();
3603 if (ch == 'e')
3605 if (match_string_p ("nt"))
3607 /* Matched "intent". */
3608 /* TODO: Call match_intent_spec from here. */
3609 if (gfc_match (" ( in out )") == MATCH_YES)
3610 d = DECL_INOUT;
3611 else if (gfc_match (" ( in )") == MATCH_YES)
3612 d = DECL_IN;
3613 else if (gfc_match (" ( out )") == MATCH_YES)
3614 d = DECL_OUT;
3617 else if (ch == 'r')
3619 if (match_string_p ("insic"))
3621 /* Matched "intrinsic". */
3622 d = DECL_INTRINSIC;
3626 break;
3628 case 'o':
3629 if (match_string_p ("optional"))
3630 d = DECL_OPTIONAL;
3631 break;
3633 case 'p':
3634 gfc_next_ascii_char ();
3635 switch (gfc_next_ascii_char ())
3637 case 'a':
3638 if (match_string_p ("rameter"))
3640 /* Matched "parameter". */
3641 d = DECL_PARAMETER;
3643 break;
3645 case 'o':
3646 if (match_string_p ("inter"))
3648 /* Matched "pointer". */
3649 d = DECL_POINTER;
3651 break;
3653 case 'r':
3654 ch = gfc_next_ascii_char ();
3655 if (ch == 'i')
3657 if (match_string_p ("vate"))
3659 /* Matched "private". */
3660 d = DECL_PRIVATE;
3663 else if (ch == 'o')
3665 if (match_string_p ("tected"))
3667 /* Matched "protected". */
3668 d = DECL_PROTECTED;
3671 break;
3673 case 'u':
3674 if (match_string_p ("blic"))
3676 /* Matched "public". */
3677 d = DECL_PUBLIC;
3679 break;
3681 break;
3683 case 's':
3684 if (match_string_p ("save"))
3685 d = DECL_SAVE;
3686 break;
3688 case 't':
3689 if (match_string_p ("target"))
3690 d = DECL_TARGET;
3691 break;
3693 case 'v':
3694 gfc_next_ascii_char ();
3695 ch = gfc_next_ascii_char ();
3696 if (ch == 'a')
3698 if (match_string_p ("lue"))
3700 /* Matched "value". */
3701 d = DECL_VALUE;
3704 else if (ch == 'o')
3706 if (match_string_p ("latile"))
3708 /* Matched "volatile". */
3709 d = DECL_VOLATILE;
3712 break;
3716 /* No double colon and no recognizable decl_type, so assume that
3717 we've been looking at something else the whole time. */
3718 if (d == DECL_NONE)
3720 m = MATCH_NO;
3721 goto cleanup;
3724 /* Check to make sure any parens are paired up correctly. */
3725 if (gfc_match_parens () == MATCH_ERROR)
3727 m = MATCH_ERROR;
3728 goto cleanup;
3731 seen[d]++;
3732 seen_at[d] = gfc_current_locus;
3734 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3736 gfc_array_spec *as = NULL;
3738 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3739 d == DECL_CODIMENSION);
3741 if (current_as == NULL)
3742 current_as = as;
3743 else if (m == MATCH_YES)
3745 if (!merge_array_spec (as, current_as, false))
3746 m = MATCH_ERROR;
3747 free (as);
3750 if (m == MATCH_NO)
3752 if (d == DECL_CODIMENSION)
3753 gfc_error ("Missing codimension specification at %C");
3754 else
3755 gfc_error ("Missing dimension specification at %C");
3756 m = MATCH_ERROR;
3759 if (m == MATCH_ERROR)
3760 goto cleanup;
3764 /* Since we've seen a double colon, we have to be looking at an
3765 attr-spec. This means that we can now issue errors. */
3766 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3767 if (seen[d] > 1)
3769 switch (d)
3771 case DECL_ALLOCATABLE:
3772 attr = "ALLOCATABLE";
3773 break;
3774 case DECL_ASYNCHRONOUS:
3775 attr = "ASYNCHRONOUS";
3776 break;
3777 case DECL_CODIMENSION:
3778 attr = "CODIMENSION";
3779 break;
3780 case DECL_CONTIGUOUS:
3781 attr = "CONTIGUOUS";
3782 break;
3783 case DECL_DIMENSION:
3784 attr = "DIMENSION";
3785 break;
3786 case DECL_EXTERNAL:
3787 attr = "EXTERNAL";
3788 break;
3789 case DECL_IN:
3790 attr = "INTENT (IN)";
3791 break;
3792 case DECL_OUT:
3793 attr = "INTENT (OUT)";
3794 break;
3795 case DECL_INOUT:
3796 attr = "INTENT (IN OUT)";
3797 break;
3798 case DECL_INTRINSIC:
3799 attr = "INTRINSIC";
3800 break;
3801 case DECL_OPTIONAL:
3802 attr = "OPTIONAL";
3803 break;
3804 case DECL_PARAMETER:
3805 attr = "PARAMETER";
3806 break;
3807 case DECL_POINTER:
3808 attr = "POINTER";
3809 break;
3810 case DECL_PROTECTED:
3811 attr = "PROTECTED";
3812 break;
3813 case DECL_PRIVATE:
3814 attr = "PRIVATE";
3815 break;
3816 case DECL_PUBLIC:
3817 attr = "PUBLIC";
3818 break;
3819 case DECL_SAVE:
3820 attr = "SAVE";
3821 break;
3822 case DECL_TARGET:
3823 attr = "TARGET";
3824 break;
3825 case DECL_IS_BIND_C:
3826 attr = "IS_BIND_C";
3827 break;
3828 case DECL_VALUE:
3829 attr = "VALUE";
3830 break;
3831 case DECL_VOLATILE:
3832 attr = "VOLATILE";
3833 break;
3834 default:
3835 attr = NULL; /* This shouldn't happen. */
3838 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3839 m = MATCH_ERROR;
3840 goto cleanup;
3843 /* Now that we've dealt with duplicate attributes, add the attributes
3844 to the current attribute. */
3845 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3847 if (seen[d] == 0)
3848 continue;
3850 if (gfc_current_state () == COMP_DERIVED
3851 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3852 && d != DECL_POINTER && d != DECL_PRIVATE
3853 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3855 if (d == DECL_ALLOCATABLE)
3857 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3858 "attribute at %C in a TYPE definition"))
3860 m = MATCH_ERROR;
3861 goto cleanup;
3864 else
3866 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3867 &seen_at[d]);
3868 m = MATCH_ERROR;
3869 goto cleanup;
3873 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3874 && gfc_current_state () != COMP_MODULE)
3876 if (d == DECL_PRIVATE)
3877 attr = "PRIVATE";
3878 else
3879 attr = "PUBLIC";
3880 if (gfc_current_state () == COMP_DERIVED
3881 && gfc_state_stack->previous
3882 && gfc_state_stack->previous->state == COMP_MODULE)
3884 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3885 "at %L in a TYPE definition", attr,
3886 &seen_at[d]))
3888 m = MATCH_ERROR;
3889 goto cleanup;
3892 else
3894 gfc_error ("%s attribute at %L is not allowed outside of the "
3895 "specification part of a module", attr, &seen_at[d]);
3896 m = MATCH_ERROR;
3897 goto cleanup;
3901 switch (d)
3903 case DECL_ALLOCATABLE:
3904 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3905 break;
3907 case DECL_ASYNCHRONOUS:
3908 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3909 t = false;
3910 else
3911 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3912 break;
3914 case DECL_CODIMENSION:
3915 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3916 break;
3918 case DECL_CONTIGUOUS:
3919 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3920 t = false;
3921 else
3922 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3923 break;
3925 case DECL_DIMENSION:
3926 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3927 break;
3929 case DECL_EXTERNAL:
3930 t = gfc_add_external (&current_attr, &seen_at[d]);
3931 break;
3933 case DECL_IN:
3934 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3935 break;
3937 case DECL_OUT:
3938 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3939 break;
3941 case DECL_INOUT:
3942 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3943 break;
3945 case DECL_INTRINSIC:
3946 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3947 break;
3949 case DECL_OPTIONAL:
3950 t = gfc_add_optional (&current_attr, &seen_at[d]);
3951 break;
3953 case DECL_PARAMETER:
3954 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3955 break;
3957 case DECL_POINTER:
3958 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3959 break;
3961 case DECL_PROTECTED:
3962 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3964 gfc_error ("PROTECTED at %C only allowed in specification "
3965 "part of a module");
3966 t = false;
3967 break;
3970 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3971 t = false;
3972 else
3973 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3974 break;
3976 case DECL_PRIVATE:
3977 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3978 &seen_at[d]);
3979 break;
3981 case DECL_PUBLIC:
3982 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3983 &seen_at[d]);
3984 break;
3986 case DECL_SAVE:
3987 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3988 break;
3990 case DECL_TARGET:
3991 t = gfc_add_target (&current_attr, &seen_at[d]);
3992 break;
3994 case DECL_IS_BIND_C:
3995 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3996 break;
3998 case DECL_VALUE:
3999 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
4000 t = false;
4001 else
4002 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
4003 break;
4005 case DECL_VOLATILE:
4006 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
4007 t = false;
4008 else
4009 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
4010 break;
4012 default:
4013 gfc_internal_error ("match_attr_spec(): Bad attribute");
4016 if (!t)
4018 m = MATCH_ERROR;
4019 goto cleanup;
4023 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4024 if ((gfc_current_state () == COMP_MODULE
4025 || gfc_current_state () == COMP_SUBMODULE)
4026 && !current_attr.save
4027 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4028 current_attr.save = SAVE_IMPLICIT;
4030 colon_seen = 1;
4031 return MATCH_YES;
4033 cleanup:
4034 gfc_current_locus = start;
4035 gfc_free_array_spec (current_as);
4036 current_as = NULL;
4037 return m;
4041 /* Set the binding label, dest_label, either with the binding label
4042 stored in the given gfc_typespec, ts, or if none was provided, it
4043 will be the symbol name in all lower case, as required by the draft
4044 (J3/04-007, section 15.4.1). If a binding label was given and
4045 there is more than one argument (num_idents), it is an error. */
4047 static bool
4048 set_binding_label (const char **dest_label, const char *sym_name,
4049 int num_idents)
4051 if (num_idents > 1 && has_name_equals)
4053 gfc_error ("Multiple identifiers provided with "
4054 "single NAME= specifier at %C");
4055 return false;
4058 if (curr_binding_label)
4059 /* Binding label given; store in temp holder till have sym. */
4060 *dest_label = curr_binding_label;
4061 else
4063 /* No binding label given, and the NAME= specifier did not exist,
4064 which means there was no NAME="". */
4065 if (sym_name != NULL && has_name_equals == 0)
4066 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4069 return true;
4073 /* Set the status of the given common block as being BIND(C) or not,
4074 depending on the given parameter, is_bind_c. */
4076 void
4077 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4079 com_block->is_bind_c = is_bind_c;
4080 return;
4084 /* Verify that the given gfc_typespec is for a C interoperable type. */
4086 bool
4087 gfc_verify_c_interop (gfc_typespec *ts)
4089 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4090 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4091 ? true : false;
4092 else if (ts->type == BT_CLASS)
4093 return false;
4094 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4095 return false;
4097 return true;
4101 /* Verify that the variables of a given common block, which has been
4102 defined with the attribute specifier bind(c), to be of a C
4103 interoperable type. Errors will be reported here, if
4104 encountered. */
4106 bool
4107 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4109 gfc_symbol *curr_sym = NULL;
4110 bool retval = true;
4112 curr_sym = com_block->head;
4114 /* Make sure we have at least one symbol. */
4115 if (curr_sym == NULL)
4116 return retval;
4118 /* Here we know we have a symbol, so we'll execute this loop
4119 at least once. */
4122 /* The second to last param, 1, says this is in a common block. */
4123 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4124 curr_sym = curr_sym->common_next;
4125 } while (curr_sym != NULL);
4127 return retval;
4131 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4132 an appropriate error message is reported. */
4134 bool
4135 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4136 int is_in_common, gfc_common_head *com_block)
4138 bool bind_c_function = false;
4139 bool retval = true;
4141 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4142 bind_c_function = true;
4144 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4146 tmp_sym = tmp_sym->result;
4147 /* Make sure it wasn't an implicitly typed result. */
4148 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4150 gfc_warning (OPT_Wc_binding_type,
4151 "Implicitly declared BIND(C) function %qs at "
4152 "%L may not be C interoperable", tmp_sym->name,
4153 &tmp_sym->declared_at);
4154 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4155 /* Mark it as C interoperable to prevent duplicate warnings. */
4156 tmp_sym->ts.is_c_interop = 1;
4157 tmp_sym->attr.is_c_interop = 1;
4161 /* Here, we know we have the bind(c) attribute, so if we have
4162 enough type info, then verify that it's a C interop kind.
4163 The info could be in the symbol already, or possibly still in
4164 the given ts (current_ts), so look in both. */
4165 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4167 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4169 /* See if we're dealing with a sym in a common block or not. */
4170 if (is_in_common == 1 && warn_c_binding_type)
4172 gfc_warning (OPT_Wc_binding_type,
4173 "Variable %qs in common block %qs at %L "
4174 "may not be a C interoperable "
4175 "kind though common block %qs is BIND(C)",
4176 tmp_sym->name, com_block->name,
4177 &(tmp_sym->declared_at), com_block->name);
4179 else
4181 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4182 gfc_error ("Type declaration %qs at %L is not C "
4183 "interoperable but it is BIND(C)",
4184 tmp_sym->name, &(tmp_sym->declared_at));
4185 else if (warn_c_binding_type)
4186 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4187 "may not be a C interoperable "
4188 "kind but it is BIND(C)",
4189 tmp_sym->name, &(tmp_sym->declared_at));
4193 /* Variables declared w/in a common block can't be bind(c)
4194 since there's no way for C to see these variables, so there's
4195 semantically no reason for the attribute. */
4196 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4198 gfc_error ("Variable %qs in common block %qs at "
4199 "%L cannot be declared with BIND(C) "
4200 "since it is not a global",
4201 tmp_sym->name, com_block->name,
4202 &(tmp_sym->declared_at));
4203 retval = false;
4206 /* Scalar variables that are bind(c) can not have the pointer
4207 or allocatable attributes. */
4208 if (tmp_sym->attr.is_bind_c == 1)
4210 if (tmp_sym->attr.pointer == 1)
4212 gfc_error ("Variable %qs at %L cannot have both the "
4213 "POINTER and BIND(C) attributes",
4214 tmp_sym->name, &(tmp_sym->declared_at));
4215 retval = false;
4218 if (tmp_sym->attr.allocatable == 1)
4220 gfc_error ("Variable %qs at %L cannot have both the "
4221 "ALLOCATABLE and BIND(C) attributes",
4222 tmp_sym->name, &(tmp_sym->declared_at));
4223 retval = false;
4228 /* If it is a BIND(C) function, make sure the return value is a
4229 scalar value. The previous tests in this function made sure
4230 the type is interoperable. */
4231 if (bind_c_function && tmp_sym->as != NULL)
4232 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4233 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4235 /* BIND(C) functions can not return a character string. */
4236 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4237 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4238 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4239 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4240 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4241 "be a character string", tmp_sym->name,
4242 &(tmp_sym->declared_at));
4245 /* See if the symbol has been marked as private. If it has, make sure
4246 there is no binding label and warn the user if there is one. */
4247 if (tmp_sym->attr.access == ACCESS_PRIVATE
4248 && tmp_sym->binding_label)
4249 /* Use gfc_warning_now because we won't say that the symbol fails
4250 just because of this. */
4251 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4252 "given the binding label %qs", tmp_sym->name,
4253 &(tmp_sym->declared_at), tmp_sym->binding_label);
4255 return retval;
4259 /* Set the appropriate fields for a symbol that's been declared as
4260 BIND(C) (the is_bind_c flag and the binding label), and verify that
4261 the type is C interoperable. Errors are reported by the functions
4262 used to set/test these fields. */
4264 bool
4265 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4267 bool retval = true;
4269 /* TODO: Do we need to make sure the vars aren't marked private? */
4271 /* Set the is_bind_c bit in symbol_attribute. */
4272 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4274 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4275 return false;
4277 return retval;
4281 /* Set the fields marking the given common block as BIND(C), including
4282 a binding label, and report any errors encountered. */
4284 bool
4285 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4287 bool retval = true;
4289 /* destLabel, common name, typespec (which may have binding label). */
4290 if (!set_binding_label (&com_block->binding_label, com_block->name,
4291 num_idents))
4292 return false;
4294 /* Set the given common block (com_block) to being bind(c) (1). */
4295 set_com_block_bind_c (com_block, 1);
4297 return retval;
4301 /* Retrieve the list of one or more identifiers that the given bind(c)
4302 attribute applies to. */
4304 bool
4305 get_bind_c_idents (void)
4307 char name[GFC_MAX_SYMBOL_LEN + 1];
4308 int num_idents = 0;
4309 gfc_symbol *tmp_sym = NULL;
4310 match found_id;
4311 gfc_common_head *com_block = NULL;
4313 if (gfc_match_name (name) == MATCH_YES)
4315 found_id = MATCH_YES;
4316 gfc_get_ha_symbol (name, &tmp_sym);
4318 else if (match_common_name (name) == MATCH_YES)
4320 found_id = MATCH_YES;
4321 com_block = gfc_get_common (name, 0);
4323 else
4325 gfc_error ("Need either entity or common block name for "
4326 "attribute specification statement at %C");
4327 return false;
4330 /* Save the current identifier and look for more. */
4333 /* Increment the number of identifiers found for this spec stmt. */
4334 num_idents++;
4336 /* Make sure we have a sym or com block, and verify that it can
4337 be bind(c). Set the appropriate field(s) and look for more
4338 identifiers. */
4339 if (tmp_sym != NULL || com_block != NULL)
4341 if (tmp_sym != NULL)
4343 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4344 return false;
4346 else
4348 if (!set_verify_bind_c_com_block (com_block, num_idents))
4349 return false;
4352 /* Look to see if we have another identifier. */
4353 tmp_sym = NULL;
4354 if (gfc_match_eos () == MATCH_YES)
4355 found_id = MATCH_NO;
4356 else if (gfc_match_char (',') != MATCH_YES)
4357 found_id = MATCH_NO;
4358 else if (gfc_match_name (name) == MATCH_YES)
4360 found_id = MATCH_YES;
4361 gfc_get_ha_symbol (name, &tmp_sym);
4363 else if (match_common_name (name) == MATCH_YES)
4365 found_id = MATCH_YES;
4366 com_block = gfc_get_common (name, 0);
4368 else
4370 gfc_error ("Missing entity or common block name for "
4371 "attribute specification statement at %C");
4372 return false;
4375 else
4377 gfc_internal_error ("Missing symbol");
4379 } while (found_id == MATCH_YES);
4381 /* if we get here we were successful */
4382 return true;
4386 /* Try and match a BIND(C) attribute specification statement. */
4388 match
4389 gfc_match_bind_c_stmt (void)
4391 match found_match = MATCH_NO;
4392 gfc_typespec *ts;
4394 ts = &current_ts;
4396 /* This may not be necessary. */
4397 gfc_clear_ts (ts);
4398 /* Clear the temporary binding label holder. */
4399 curr_binding_label = NULL;
4401 /* Look for the bind(c). */
4402 found_match = gfc_match_bind_c (NULL, true);
4404 if (found_match == MATCH_YES)
4406 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4407 return MATCH_ERROR;
4409 /* Look for the :: now, but it is not required. */
4410 gfc_match (" :: ");
4412 /* Get the identifier(s) that needs to be updated. This may need to
4413 change to hand the flag(s) for the attr specified so all identifiers
4414 found can have all appropriate parts updated (assuming that the same
4415 spec stmt can have multiple attrs, such as both bind(c) and
4416 allocatable...). */
4417 if (!get_bind_c_idents ())
4418 /* Error message should have printed already. */
4419 return MATCH_ERROR;
4422 return found_match;
4426 /* Match a data declaration statement. */
4428 match
4429 gfc_match_data_decl (void)
4431 gfc_symbol *sym;
4432 match m;
4433 int elem;
4435 num_idents_on_line = 0;
4437 m = gfc_match_decl_type_spec (&current_ts, 0);
4438 if (m != MATCH_YES)
4439 return m;
4441 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4442 && gfc_current_state () != COMP_DERIVED)
4444 sym = gfc_use_derived (current_ts.u.derived);
4446 if (sym == NULL)
4448 m = MATCH_ERROR;
4449 goto cleanup;
4452 current_ts.u.derived = sym;
4455 m = match_attr_spec ();
4456 if (m == MATCH_ERROR)
4458 m = MATCH_NO;
4459 goto cleanup;
4462 if (current_ts.type == BT_CLASS
4463 && current_ts.u.derived->attr.unlimited_polymorphic)
4464 goto ok;
4466 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4467 && current_ts.u.derived->components == NULL
4468 && !current_ts.u.derived->attr.zero_comp)
4471 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4472 goto ok;
4474 gfc_find_symbol (current_ts.u.derived->name,
4475 current_ts.u.derived->ns, 1, &sym);
4477 /* Any symbol that we find had better be a type definition
4478 which has its components defined. */
4479 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4480 && (current_ts.u.derived->components != NULL
4481 || current_ts.u.derived->attr.zero_comp))
4482 goto ok;
4484 gfc_error ("Derived type at %C has not been previously defined "
4485 "and so cannot appear in a derived type definition");
4486 m = MATCH_ERROR;
4487 goto cleanup;
4491 /* If we have an old-style character declaration, and no new-style
4492 attribute specifications, then there a comma is optional between
4493 the type specification and the variable list. */
4494 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4495 gfc_match_char (',');
4497 /* Give the types/attributes to symbols that follow. Give the element
4498 a number so that repeat character length expressions can be copied. */
4499 elem = 1;
4500 for (;;)
4502 num_idents_on_line++;
4503 m = variable_decl (elem++);
4504 if (m == MATCH_ERROR)
4505 goto cleanup;
4506 if (m == MATCH_NO)
4507 break;
4509 if (gfc_match_eos () == MATCH_YES)
4510 goto cleanup;
4511 if (gfc_match_char (',') != MATCH_YES)
4512 break;
4515 if (!gfc_error_flag_test ())
4516 gfc_error ("Syntax error in data declaration at %C");
4517 m = MATCH_ERROR;
4519 gfc_free_data_all (gfc_current_ns);
4521 cleanup:
4522 gfc_free_array_spec (current_as);
4523 current_as = NULL;
4524 return m;
4528 /* Match a prefix associated with a function or subroutine
4529 declaration. If the typespec pointer is nonnull, then a typespec
4530 can be matched. Note that if nothing matches, MATCH_YES is
4531 returned (the null string was matched). */
4533 match
4534 gfc_match_prefix (gfc_typespec *ts)
4536 bool seen_type;
4537 bool seen_impure;
4538 bool found_prefix;
4540 gfc_clear_attr (&current_attr);
4541 seen_type = false;
4542 seen_impure = false;
4544 gcc_assert (!gfc_matching_prefix);
4545 gfc_matching_prefix = true;
4549 found_prefix = false;
4551 if (!seen_type && ts != NULL
4552 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4553 && gfc_match_space () == MATCH_YES)
4556 seen_type = true;
4557 found_prefix = true;
4560 if (gfc_match ("elemental% ") == MATCH_YES)
4562 if (!gfc_add_elemental (&current_attr, NULL))
4563 goto error;
4565 found_prefix = true;
4568 if (gfc_match ("pure% ") == MATCH_YES)
4570 if (!gfc_add_pure (&current_attr, NULL))
4571 goto error;
4573 found_prefix = true;
4576 if (gfc_match ("recursive% ") == MATCH_YES)
4578 if (!gfc_add_recursive (&current_attr, NULL))
4579 goto error;
4581 found_prefix = true;
4584 /* IMPURE is a somewhat special case, as it needs not set an actual
4585 attribute but rather only prevents ELEMENTAL routines from being
4586 automatically PURE. */
4587 if (gfc_match ("impure% ") == MATCH_YES)
4589 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4590 goto error;
4592 seen_impure = true;
4593 found_prefix = true;
4596 while (found_prefix);
4598 /* IMPURE and PURE must not both appear, of course. */
4599 if (seen_impure && current_attr.pure)
4601 gfc_error ("PURE and IMPURE must not appear both at %C");
4602 goto error;
4605 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4606 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4608 if (!gfc_add_pure (&current_attr, NULL))
4609 goto error;
4612 /* At this point, the next item is not a prefix. */
4613 gcc_assert (gfc_matching_prefix);
4615 /* MODULE should be the last prefix before FUNCTION or SUBROUTINE.
4616 Since this is a prefix like PURE, ELEMENTAL, etc., having a
4617 corresponding attribute seems natural and distinguishes these
4618 procedures from procedure types of PROC_MODULE, which these are
4619 as well. */
4620 if ((gfc_current_state () == COMP_INTERFACE
4621 || gfc_current_state () == COMP_CONTAINS)
4622 && gfc_match ("module% ") == MATCH_YES)
4624 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4625 goto error;
4626 else
4627 current_attr.module_procedure = 1;
4630 gfc_matching_prefix = false;
4631 return MATCH_YES;
4633 error:
4634 gcc_assert (gfc_matching_prefix);
4635 gfc_matching_prefix = false;
4636 return MATCH_ERROR;
4640 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4642 static bool
4643 copy_prefix (symbol_attribute *dest, locus *where)
4645 if (current_attr.pure && !gfc_add_pure (dest, where))
4646 return false;
4648 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4649 return false;
4651 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4652 return false;
4654 return true;
4658 /* Match a formal argument list. */
4660 match
4661 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4663 gfc_formal_arglist *head, *tail, *p, *q;
4664 char name[GFC_MAX_SYMBOL_LEN + 1];
4665 gfc_symbol *sym;
4666 match m;
4667 gfc_formal_arglist *formal = NULL;
4669 head = tail = NULL;
4671 /* Keep the interface formal argument list and null it so that the
4672 matching for the new declaration can be done. The numbers and
4673 names of the arguments are checked here. The interface formal
4674 arguments are retained in formal_arglist and the characteristics
4675 are compared in resolve.c(resolve_fl_procedure). See the remark
4676 in get_proc_name about the eventual need to copy the formal_arglist
4677 and populate the formal namespace of the interface symbol. */
4678 if (progname->attr.module_procedure
4679 && progname->attr.host_assoc)
4681 formal = progname->formal;
4682 progname->formal = NULL;
4685 if (gfc_match_char ('(') != MATCH_YES)
4687 if (null_flag)
4688 goto ok;
4689 return MATCH_NO;
4692 if (gfc_match_char (')') == MATCH_YES)
4693 goto ok;
4695 for (;;)
4697 if (gfc_match_char ('*') == MATCH_YES)
4699 sym = NULL;
4700 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4701 "at %C"))
4703 m = MATCH_ERROR;
4704 goto cleanup;
4707 else
4709 m = gfc_match_name (name);
4710 if (m != MATCH_YES)
4711 goto cleanup;
4713 if (gfc_get_symbol (name, NULL, &sym))
4714 goto cleanup;
4717 p = gfc_get_formal_arglist ();
4719 if (head == NULL)
4720 head = tail = p;
4721 else
4723 tail->next = p;
4724 tail = p;
4727 tail->sym = sym;
4729 /* We don't add the VARIABLE flavor because the name could be a
4730 dummy procedure. We don't apply these attributes to formal
4731 arguments of statement functions. */
4732 if (sym != NULL && !st_flag
4733 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4734 || !gfc_missing_attr (&sym->attr, NULL)))
4736 m = MATCH_ERROR;
4737 goto cleanup;
4740 /* The name of a program unit can be in a different namespace,
4741 so check for it explicitly. After the statement is accepted,
4742 the name is checked for especially in gfc_get_symbol(). */
4743 if (gfc_new_block != NULL && sym != NULL
4744 && strcmp (sym->name, gfc_new_block->name) == 0)
4746 gfc_error ("Name %qs at %C is the name of the procedure",
4747 sym->name);
4748 m = MATCH_ERROR;
4749 goto cleanup;
4752 if (gfc_match_char (')') == MATCH_YES)
4753 goto ok;
4755 m = gfc_match_char (',');
4756 if (m != MATCH_YES)
4758 gfc_error ("Unexpected junk in formal argument list at %C");
4759 goto cleanup;
4764 /* Check for duplicate symbols in the formal argument list. */
4765 if (head != NULL)
4767 for (p = head; p->next; p = p->next)
4769 if (p->sym == NULL)
4770 continue;
4772 for (q = p->next; q; q = q->next)
4773 if (p->sym == q->sym)
4775 gfc_error ("Duplicate symbol %qs in formal argument list "
4776 "at %C", p->sym->name);
4778 m = MATCH_ERROR;
4779 goto cleanup;
4784 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4786 m = MATCH_ERROR;
4787 goto cleanup;
4790 if (formal)
4792 for (p = formal, q = head; p && q; p = p->next, q = q->next)
4794 if ((p->next != NULL && q->next == NULL)
4795 || (p->next == NULL && q->next != NULL))
4796 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4797 "formal arguments at %C");
4798 else if ((p->sym == NULL && q->sym == NULL)
4799 || strcmp (p->sym->name, q->sym->name) == 0)
4800 continue;
4801 else
4802 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4803 "argument names (%s/%s) at %C",
4804 p->sym->name, q->sym->name);
4808 return MATCH_YES;
4810 cleanup:
4811 gfc_free_formal_arglist (head);
4812 return m;
4816 /* Match a RESULT specification following a function declaration or
4817 ENTRY statement. Also matches the end-of-statement. */
4819 static match
4820 match_result (gfc_symbol *function, gfc_symbol **result)
4822 char name[GFC_MAX_SYMBOL_LEN + 1];
4823 gfc_symbol *r;
4824 match m;
4826 if (gfc_match (" result (") != MATCH_YES)
4827 return MATCH_NO;
4829 m = gfc_match_name (name);
4830 if (m != MATCH_YES)
4831 return m;
4833 /* Get the right paren, and that's it because there could be the
4834 bind(c) attribute after the result clause. */
4835 if (gfc_match_char (')') != MATCH_YES)
4837 /* TODO: should report the missing right paren here. */
4838 return MATCH_ERROR;
4841 if (strcmp (function->name, name) == 0)
4843 gfc_error ("RESULT variable at %C must be different than function name");
4844 return MATCH_ERROR;
4847 if (gfc_get_symbol (name, NULL, &r))
4848 return MATCH_ERROR;
4850 if (!gfc_add_result (&r->attr, r->name, NULL))
4851 return MATCH_ERROR;
4853 *result = r;
4855 return MATCH_YES;
4859 /* Match a function suffix, which could be a combination of a result
4860 clause and BIND(C), either one, or neither. The draft does not
4861 require them to come in a specific order. */
4863 match
4864 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4866 match is_bind_c; /* Found bind(c). */
4867 match is_result; /* Found result clause. */
4868 match found_match; /* Status of whether we've found a good match. */
4869 char peek_char; /* Character we're going to peek at. */
4870 bool allow_binding_name;
4872 /* Initialize to having found nothing. */
4873 found_match = MATCH_NO;
4874 is_bind_c = MATCH_NO;
4875 is_result = MATCH_NO;
4877 /* Get the next char to narrow between result and bind(c). */
4878 gfc_gobble_whitespace ();
4879 peek_char = gfc_peek_ascii_char ();
4881 /* C binding names are not allowed for internal procedures. */
4882 if (gfc_current_state () == COMP_CONTAINS
4883 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4884 allow_binding_name = false;
4885 else
4886 allow_binding_name = true;
4888 switch (peek_char)
4890 case 'r':
4891 /* Look for result clause. */
4892 is_result = match_result (sym, result);
4893 if (is_result == MATCH_YES)
4895 /* Now see if there is a bind(c) after it. */
4896 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4897 /* We've found the result clause and possibly bind(c). */
4898 found_match = MATCH_YES;
4900 else
4901 /* This should only be MATCH_ERROR. */
4902 found_match = is_result;
4903 break;
4904 case 'b':
4905 /* Look for bind(c) first. */
4906 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4907 if (is_bind_c == MATCH_YES)
4909 /* Now see if a result clause followed it. */
4910 is_result = match_result (sym, result);
4911 found_match = MATCH_YES;
4913 else
4915 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4916 found_match = MATCH_ERROR;
4918 break;
4919 default:
4920 gfc_error ("Unexpected junk after function declaration at %C");
4921 found_match = MATCH_ERROR;
4922 break;
4925 if (is_bind_c == MATCH_YES)
4927 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4928 if (gfc_current_state () == COMP_CONTAINS
4929 && sym->ns->proc_name->attr.flavor != FL_MODULE
4930 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4931 "at %L may not be specified for an internal "
4932 "procedure", &gfc_current_locus))
4933 return MATCH_ERROR;
4935 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4936 return MATCH_ERROR;
4939 return found_match;
4943 /* Procedure pointer return value without RESULT statement:
4944 Add "hidden" result variable named "ppr@". */
4946 static bool
4947 add_hidden_procptr_result (gfc_symbol *sym)
4949 bool case1,case2;
4951 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4952 return false;
4954 /* First usage case: PROCEDURE and EXTERNAL statements. */
4955 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4956 && strcmp (gfc_current_block ()->name, sym->name) == 0
4957 && sym->attr.external;
4958 /* Second usage case: INTERFACE statements. */
4959 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4960 && gfc_state_stack->previous->state == COMP_FUNCTION
4961 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4963 if (case1 || case2)
4965 gfc_symtree *stree;
4966 if (case1)
4967 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4968 else if (case2)
4970 gfc_symtree *st2;
4971 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4972 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4973 st2->n.sym = stree->n.sym;
4975 sym->result = stree->n.sym;
4977 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4978 sym->result->attr.pointer = sym->attr.pointer;
4979 sym->result->attr.external = sym->attr.external;
4980 sym->result->attr.referenced = sym->attr.referenced;
4981 sym->result->ts = sym->ts;
4982 sym->attr.proc_pointer = 0;
4983 sym->attr.pointer = 0;
4984 sym->attr.external = 0;
4985 if (sym->result->attr.external && sym->result->attr.pointer)
4987 sym->result->attr.pointer = 0;
4988 sym->result->attr.proc_pointer = 1;
4991 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4993 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4994 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4995 && sym->result && sym->result != sym && sym->result->attr.external
4996 && sym == gfc_current_ns->proc_name
4997 && sym == sym->result->ns->proc_name
4998 && strcmp ("ppr@", sym->result->name) == 0)
5000 sym->result->attr.proc_pointer = 1;
5001 sym->attr.pointer = 0;
5002 return true;
5004 else
5005 return false;
5009 /* Match the interface for a PROCEDURE declaration,
5010 including brackets (R1212). */
5012 static match
5013 match_procedure_interface (gfc_symbol **proc_if)
5015 match m;
5016 gfc_symtree *st;
5017 locus old_loc, entry_loc;
5018 gfc_namespace *old_ns = gfc_current_ns;
5019 char name[GFC_MAX_SYMBOL_LEN + 1];
5021 old_loc = entry_loc = gfc_current_locus;
5022 gfc_clear_ts (&current_ts);
5024 if (gfc_match (" (") != MATCH_YES)
5026 gfc_current_locus = entry_loc;
5027 return MATCH_NO;
5030 /* Get the type spec. for the procedure interface. */
5031 old_loc = gfc_current_locus;
5032 m = gfc_match_decl_type_spec (&current_ts, 0);
5033 gfc_gobble_whitespace ();
5034 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
5035 goto got_ts;
5037 if (m == MATCH_ERROR)
5038 return m;
5040 /* Procedure interface is itself a procedure. */
5041 gfc_current_locus = old_loc;
5042 m = gfc_match_name (name);
5044 /* First look to see if it is already accessible in the current
5045 namespace because it is use associated or contained. */
5046 st = NULL;
5047 if (gfc_find_sym_tree (name, NULL, 0, &st))
5048 return MATCH_ERROR;
5050 /* If it is still not found, then try the parent namespace, if it
5051 exists and create the symbol there if it is still not found. */
5052 if (gfc_current_ns->parent)
5053 gfc_current_ns = gfc_current_ns->parent;
5054 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5055 return MATCH_ERROR;
5057 gfc_current_ns = old_ns;
5058 *proc_if = st->n.sym;
5060 if (*proc_if)
5062 (*proc_if)->refs++;
5063 /* Resolve interface if possible. That way, attr.procedure is only set
5064 if it is declared by a later procedure-declaration-stmt, which is
5065 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5066 while ((*proc_if)->ts.interface)
5067 *proc_if = (*proc_if)->ts.interface;
5069 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5070 && (*proc_if)->ts.type == BT_UNKNOWN
5071 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5072 (*proc_if)->name, NULL))
5073 return MATCH_ERROR;
5076 got_ts:
5077 if (gfc_match (" )") != MATCH_YES)
5079 gfc_current_locus = entry_loc;
5080 return MATCH_NO;
5083 return MATCH_YES;
5087 /* Match a PROCEDURE declaration (R1211). */
5089 static match
5090 match_procedure_decl (void)
5092 match m;
5093 gfc_symbol *sym, *proc_if = NULL;
5094 int num;
5095 gfc_expr *initializer = NULL;
5097 /* Parse interface (with brackets). */
5098 m = match_procedure_interface (&proc_if);
5099 if (m != MATCH_YES)
5100 return m;
5102 /* Parse attributes (with colons). */
5103 m = match_attr_spec();
5104 if (m == MATCH_ERROR)
5105 return MATCH_ERROR;
5107 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5109 current_attr.is_bind_c = 1;
5110 has_name_equals = 0;
5111 curr_binding_label = NULL;
5114 /* Get procedure symbols. */
5115 for(num=1;;num++)
5117 m = gfc_match_symbol (&sym, 0);
5118 if (m == MATCH_NO)
5119 goto syntax;
5120 else if (m == MATCH_ERROR)
5121 return m;
5123 /* Add current_attr to the symbol attributes. */
5124 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5125 return MATCH_ERROR;
5127 if (sym->attr.is_bind_c)
5129 /* Check for C1218. */
5130 if (!proc_if || !proc_if->attr.is_bind_c)
5132 gfc_error ("BIND(C) attribute at %C requires "
5133 "an interface with BIND(C)");
5134 return MATCH_ERROR;
5136 /* Check for C1217. */
5137 if (has_name_equals && sym->attr.pointer)
5139 gfc_error ("BIND(C) procedure with NAME may not have "
5140 "POINTER attribute at %C");
5141 return MATCH_ERROR;
5143 if (has_name_equals && sym->attr.dummy)
5145 gfc_error ("Dummy procedure at %C may not have "
5146 "BIND(C) attribute with NAME");
5147 return MATCH_ERROR;
5149 /* Set binding label for BIND(C). */
5150 if (!set_binding_label (&sym->binding_label, sym->name, num))
5151 return MATCH_ERROR;
5154 if (!gfc_add_external (&sym->attr, NULL))
5155 return MATCH_ERROR;
5157 if (add_hidden_procptr_result (sym))
5158 sym = sym->result;
5160 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5161 return MATCH_ERROR;
5163 /* Set interface. */
5164 if (proc_if != NULL)
5166 if (sym->ts.type != BT_UNKNOWN)
5168 gfc_error ("Procedure %qs at %L already has basic type of %s",
5169 sym->name, &gfc_current_locus,
5170 gfc_basic_typename (sym->ts.type));
5171 return MATCH_ERROR;
5173 sym->ts.interface = proc_if;
5174 sym->attr.untyped = 1;
5175 sym->attr.if_source = IFSRC_IFBODY;
5177 else if (current_ts.type != BT_UNKNOWN)
5179 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5180 return MATCH_ERROR;
5181 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5182 sym->ts.interface->ts = current_ts;
5183 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5184 sym->ts.interface->attr.function = 1;
5185 sym->attr.function = 1;
5186 sym->attr.if_source = IFSRC_UNKNOWN;
5189 if (gfc_match (" =>") == MATCH_YES)
5191 if (!current_attr.pointer)
5193 gfc_error ("Initialization at %C isn't for a pointer variable");
5194 m = MATCH_ERROR;
5195 goto cleanup;
5198 m = match_pointer_init (&initializer, 1);
5199 if (m != MATCH_YES)
5200 goto cleanup;
5202 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5203 goto cleanup;
5207 if (gfc_match_eos () == MATCH_YES)
5208 return MATCH_YES;
5209 if (gfc_match_char (',') != MATCH_YES)
5210 goto syntax;
5213 syntax:
5214 gfc_error ("Syntax error in PROCEDURE statement at %C");
5215 return MATCH_ERROR;
5217 cleanup:
5218 /* Free stuff up and return. */
5219 gfc_free_expr (initializer);
5220 return m;
5224 static match
5225 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5228 /* Match a procedure pointer component declaration (R445). */
5230 static match
5231 match_ppc_decl (void)
5233 match m;
5234 gfc_symbol *proc_if = NULL;
5235 gfc_typespec ts;
5236 int num;
5237 gfc_component *c;
5238 gfc_expr *initializer = NULL;
5239 gfc_typebound_proc* tb;
5240 char name[GFC_MAX_SYMBOL_LEN + 1];
5242 /* Parse interface (with brackets). */
5243 m = match_procedure_interface (&proc_if);
5244 if (m != MATCH_YES)
5245 goto syntax;
5247 /* Parse attributes. */
5248 tb = XCNEW (gfc_typebound_proc);
5249 tb->where = gfc_current_locus;
5250 m = match_binding_attributes (tb, false, true);
5251 if (m == MATCH_ERROR)
5252 return m;
5254 gfc_clear_attr (&current_attr);
5255 current_attr.procedure = 1;
5256 current_attr.proc_pointer = 1;
5257 current_attr.access = tb->access;
5258 current_attr.flavor = FL_PROCEDURE;
5260 /* Match the colons (required). */
5261 if (gfc_match (" ::") != MATCH_YES)
5263 gfc_error ("Expected %<::%> after binding-attributes at %C");
5264 return MATCH_ERROR;
5267 /* Check for C450. */
5268 if (!tb->nopass && proc_if == NULL)
5270 gfc_error("NOPASS or explicit interface required at %C");
5271 return MATCH_ERROR;
5274 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5275 return MATCH_ERROR;
5277 /* Match PPC names. */
5278 ts = current_ts;
5279 for(num=1;;num++)
5281 m = gfc_match_name (name);
5282 if (m == MATCH_NO)
5283 goto syntax;
5284 else if (m == MATCH_ERROR)
5285 return m;
5287 if (!gfc_add_component (gfc_current_block(), name, &c))
5288 return MATCH_ERROR;
5290 /* Add current_attr to the symbol attributes. */
5291 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5292 return MATCH_ERROR;
5294 if (!gfc_add_external (&c->attr, NULL))
5295 return MATCH_ERROR;
5297 if (!gfc_add_proc (&c->attr, name, NULL))
5298 return MATCH_ERROR;
5300 if (num == 1)
5301 c->tb = tb;
5302 else
5304 c->tb = XCNEW (gfc_typebound_proc);
5305 c->tb->where = gfc_current_locus;
5306 *c->tb = *tb;
5309 /* Set interface. */
5310 if (proc_if != NULL)
5312 c->ts.interface = proc_if;
5313 c->attr.untyped = 1;
5314 c->attr.if_source = IFSRC_IFBODY;
5316 else if (ts.type != BT_UNKNOWN)
5318 c->ts = ts;
5319 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5320 c->ts.interface->result = c->ts.interface;
5321 c->ts.interface->ts = ts;
5322 c->ts.interface->attr.flavor = FL_PROCEDURE;
5323 c->ts.interface->attr.function = 1;
5324 c->attr.function = 1;
5325 c->attr.if_source = IFSRC_UNKNOWN;
5328 if (gfc_match (" =>") == MATCH_YES)
5330 m = match_pointer_init (&initializer, 1);
5331 if (m != MATCH_YES)
5333 gfc_free_expr (initializer);
5334 return m;
5336 c->initializer = initializer;
5339 if (gfc_match_eos () == MATCH_YES)
5340 return MATCH_YES;
5341 if (gfc_match_char (',') != MATCH_YES)
5342 goto syntax;
5345 syntax:
5346 gfc_error ("Syntax error in procedure pointer component at %C");
5347 return MATCH_ERROR;
5351 /* Match a PROCEDURE declaration inside an interface (R1206). */
5353 static match
5354 match_procedure_in_interface (void)
5356 match m;
5357 gfc_symbol *sym;
5358 char name[GFC_MAX_SYMBOL_LEN + 1];
5359 locus old_locus;
5361 if (current_interface.type == INTERFACE_NAMELESS
5362 || current_interface.type == INTERFACE_ABSTRACT)
5364 gfc_error ("PROCEDURE at %C must be in a generic interface");
5365 return MATCH_ERROR;
5368 /* Check if the F2008 optional double colon appears. */
5369 gfc_gobble_whitespace ();
5370 old_locus = gfc_current_locus;
5371 if (gfc_match ("::") == MATCH_YES)
5373 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5374 "MODULE PROCEDURE statement at %L", &old_locus))
5375 return MATCH_ERROR;
5377 else
5378 gfc_current_locus = old_locus;
5380 for(;;)
5382 m = gfc_match_name (name);
5383 if (m == MATCH_NO)
5384 goto syntax;
5385 else if (m == MATCH_ERROR)
5386 return m;
5387 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5388 return MATCH_ERROR;
5390 if (!gfc_add_interface (sym))
5391 return MATCH_ERROR;
5393 if (gfc_match_eos () == MATCH_YES)
5394 break;
5395 if (gfc_match_char (',') != MATCH_YES)
5396 goto syntax;
5399 return MATCH_YES;
5401 syntax:
5402 gfc_error ("Syntax error in PROCEDURE statement at %C");
5403 return MATCH_ERROR;
5407 /* General matcher for PROCEDURE declarations. */
5409 static match match_procedure_in_type (void);
5411 match
5412 gfc_match_procedure (void)
5414 match m;
5416 switch (gfc_current_state ())
5418 case COMP_NONE:
5419 case COMP_PROGRAM:
5420 case COMP_MODULE:
5421 case COMP_SUBMODULE:
5422 case COMP_SUBROUTINE:
5423 case COMP_FUNCTION:
5424 case COMP_BLOCK:
5425 m = match_procedure_decl ();
5426 break;
5427 case COMP_INTERFACE:
5428 m = match_procedure_in_interface ();
5429 break;
5430 case COMP_DERIVED:
5431 m = match_ppc_decl ();
5432 break;
5433 case COMP_DERIVED_CONTAINS:
5434 m = match_procedure_in_type ();
5435 break;
5436 default:
5437 return MATCH_NO;
5440 if (m != MATCH_YES)
5441 return m;
5443 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5444 return MATCH_ERROR;
5446 return m;
5450 /* Warn if a matched procedure has the same name as an intrinsic; this is
5451 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5452 parser-state-stack to find out whether we're in a module. */
5454 static void
5455 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5457 bool in_module;
5459 in_module = (gfc_state_stack->previous
5460 && (gfc_state_stack->previous->state == COMP_MODULE
5461 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5463 gfc_warn_intrinsic_shadow (sym, in_module, func);
5467 /* Match a function declaration. */
5469 match
5470 gfc_match_function_decl (void)
5472 char name[GFC_MAX_SYMBOL_LEN + 1];
5473 gfc_symbol *sym, *result;
5474 locus old_loc;
5475 match m;
5476 match suffix_match;
5477 match found_match; /* Status returned by match func. */
5479 if (gfc_current_state () != COMP_NONE
5480 && gfc_current_state () != COMP_INTERFACE
5481 && gfc_current_state () != COMP_CONTAINS)
5482 return MATCH_NO;
5484 gfc_clear_ts (&current_ts);
5486 old_loc = gfc_current_locus;
5488 m = gfc_match_prefix (&current_ts);
5489 if (m != MATCH_YES)
5491 gfc_current_locus = old_loc;
5492 return m;
5495 if (gfc_match ("function% %n", name) != MATCH_YES)
5497 gfc_current_locus = old_loc;
5498 return MATCH_NO;
5501 if (get_proc_name (name, &sym, false))
5502 return MATCH_ERROR;
5504 if (add_hidden_procptr_result (sym))
5505 sym = sym->result;
5507 if (current_attr.module_procedure)
5508 sym->attr.module_procedure = 1;
5510 gfc_new_block = sym;
5512 m = gfc_match_formal_arglist (sym, 0, 0);
5513 if (m == MATCH_NO)
5515 gfc_error ("Expected formal argument list in function "
5516 "definition at %C");
5517 m = MATCH_ERROR;
5518 goto cleanup;
5520 else if (m == MATCH_ERROR)
5521 goto cleanup;
5523 result = NULL;
5525 /* According to the draft, the bind(c) and result clause can
5526 come in either order after the formal_arg_list (i.e., either
5527 can be first, both can exist together or by themselves or neither
5528 one). Therefore, the match_result can't match the end of the
5529 string, and check for the bind(c) or result clause in either order. */
5530 found_match = gfc_match_eos ();
5532 /* Make sure that it isn't already declared as BIND(C). If it is, it
5533 must have been marked BIND(C) with a BIND(C) attribute and that is
5534 not allowed for procedures. */
5535 if (sym->attr.is_bind_c == 1)
5537 sym->attr.is_bind_c = 0;
5538 if (sym->old_symbol != NULL)
5539 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5540 "variables or common blocks",
5541 &(sym->old_symbol->declared_at));
5542 else
5543 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5544 "variables or common blocks", &gfc_current_locus);
5547 if (found_match != MATCH_YES)
5549 /* If we haven't found the end-of-statement, look for a suffix. */
5550 suffix_match = gfc_match_suffix (sym, &result);
5551 if (suffix_match == MATCH_YES)
5552 /* Need to get the eos now. */
5553 found_match = gfc_match_eos ();
5554 else
5555 found_match = suffix_match;
5558 if(found_match != MATCH_YES)
5559 m = MATCH_ERROR;
5560 else
5562 /* Make changes to the symbol. */
5563 m = MATCH_ERROR;
5565 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5566 goto cleanup;
5568 if (!gfc_missing_attr (&sym->attr, NULL)
5569 || !copy_prefix (&sym->attr, &sym->declared_at))
5570 goto cleanup;
5572 /* Delay matching the function characteristics until after the
5573 specification block by signalling kind=-1. */
5574 sym->declared_at = old_loc;
5575 if (current_ts.type != BT_UNKNOWN)
5576 current_ts.kind = -1;
5577 else
5578 current_ts.kind = 0;
5580 if (result == NULL)
5582 if (current_ts.type != BT_UNKNOWN
5583 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5584 goto cleanup;
5585 sym->result = sym;
5587 else
5589 if (current_ts.type != BT_UNKNOWN
5590 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5591 goto cleanup;
5592 sym->result = result;
5595 /* Warn if this procedure has the same name as an intrinsic. */
5596 do_warn_intrinsic_shadow (sym, true);
5598 return MATCH_YES;
5601 cleanup:
5602 gfc_current_locus = old_loc;
5603 return m;
5607 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5608 pass the name of the entry, rather than the gfc_current_block name, and
5609 to return false upon finding an existing global entry. */
5611 static bool
5612 add_global_entry (const char *name, const char *binding_label, bool sub,
5613 locus *where)
5615 gfc_gsymbol *s;
5616 enum gfc_symbol_type type;
5618 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5620 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5621 name is a global identifier. */
5622 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5624 s = gfc_get_gsymbol (name);
5626 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5628 gfc_global_used (s, where);
5629 return false;
5631 else
5633 s->type = type;
5634 s->sym_name = name;
5635 s->where = *where;
5636 s->defined = 1;
5637 s->ns = gfc_current_ns;
5641 /* Don't add the symbol multiple times. */
5642 if (binding_label
5643 && (!gfc_notification_std (GFC_STD_F2008)
5644 || strcmp (name, binding_label) != 0))
5646 s = gfc_get_gsymbol (binding_label);
5648 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5650 gfc_global_used (s, where);
5651 return false;
5653 else
5655 s->type = type;
5656 s->sym_name = name;
5657 s->binding_label = binding_label;
5658 s->where = *where;
5659 s->defined = 1;
5660 s->ns = gfc_current_ns;
5664 return true;
5668 /* Match an ENTRY statement. */
5670 match
5671 gfc_match_entry (void)
5673 gfc_symbol *proc;
5674 gfc_symbol *result;
5675 gfc_symbol *entry;
5676 char name[GFC_MAX_SYMBOL_LEN + 1];
5677 gfc_compile_state state;
5678 match m;
5679 gfc_entry_list *el;
5680 locus old_loc;
5681 bool module_procedure;
5682 char peek_char;
5683 match is_bind_c;
5685 m = gfc_match_name (name);
5686 if (m != MATCH_YES)
5687 return m;
5689 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5690 return MATCH_ERROR;
5692 state = gfc_current_state ();
5693 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5695 switch (state)
5697 case COMP_PROGRAM:
5698 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5699 break;
5700 case COMP_MODULE:
5701 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5702 break;
5703 case COMP_SUBMODULE:
5704 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5705 break;
5706 case COMP_BLOCK_DATA:
5707 gfc_error ("ENTRY statement at %C cannot appear within "
5708 "a BLOCK DATA");
5709 break;
5710 case COMP_INTERFACE:
5711 gfc_error ("ENTRY statement at %C cannot appear within "
5712 "an INTERFACE");
5713 break;
5714 case COMP_DERIVED:
5715 gfc_error ("ENTRY statement at %C cannot appear within "
5716 "a DERIVED TYPE block");
5717 break;
5718 case COMP_IF:
5719 gfc_error ("ENTRY statement at %C cannot appear within "
5720 "an IF-THEN block");
5721 break;
5722 case COMP_DO:
5723 case COMP_DO_CONCURRENT:
5724 gfc_error ("ENTRY statement at %C cannot appear within "
5725 "a DO block");
5726 break;
5727 case COMP_SELECT:
5728 gfc_error ("ENTRY statement at %C cannot appear within "
5729 "a SELECT block");
5730 break;
5731 case COMP_FORALL:
5732 gfc_error ("ENTRY statement at %C cannot appear within "
5733 "a FORALL block");
5734 break;
5735 case COMP_WHERE:
5736 gfc_error ("ENTRY statement at %C cannot appear within "
5737 "a WHERE block");
5738 break;
5739 case COMP_CONTAINS:
5740 gfc_error ("ENTRY statement at %C cannot appear within "
5741 "a contained subprogram");
5742 break;
5743 default:
5744 gfc_error ("Unexpected ENTRY statement at %C");
5746 return MATCH_ERROR;
5749 module_procedure = gfc_current_ns->parent != NULL
5750 && gfc_current_ns->parent->proc_name
5751 && gfc_current_ns->parent->proc_name->attr.flavor
5752 == FL_MODULE;
5754 if (gfc_current_ns->parent != NULL
5755 && gfc_current_ns->parent->proc_name
5756 && !module_procedure)
5758 gfc_error("ENTRY statement at %C cannot appear in a "
5759 "contained procedure");
5760 return MATCH_ERROR;
5763 /* Module function entries need special care in get_proc_name
5764 because previous references within the function will have
5765 created symbols attached to the current namespace. */
5766 if (get_proc_name (name, &entry,
5767 gfc_current_ns->parent != NULL
5768 && module_procedure))
5769 return MATCH_ERROR;
5771 proc = gfc_current_block ();
5773 /* Make sure that it isn't already declared as BIND(C). If it is, it
5774 must have been marked BIND(C) with a BIND(C) attribute and that is
5775 not allowed for procedures. */
5776 if (entry->attr.is_bind_c == 1)
5778 entry->attr.is_bind_c = 0;
5779 if (entry->old_symbol != NULL)
5780 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5781 "variables or common blocks",
5782 &(entry->old_symbol->declared_at));
5783 else
5784 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5785 "variables or common blocks", &gfc_current_locus);
5788 /* Check what next non-whitespace character is so we can tell if there
5789 is the required parens if we have a BIND(C). */
5790 old_loc = gfc_current_locus;
5791 gfc_gobble_whitespace ();
5792 peek_char = gfc_peek_ascii_char ();
5794 if (state == COMP_SUBROUTINE)
5796 m = gfc_match_formal_arglist (entry, 0, 1);
5797 if (m != MATCH_YES)
5798 return MATCH_ERROR;
5800 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5801 never be an internal procedure. */
5802 is_bind_c = gfc_match_bind_c (entry, true);
5803 if (is_bind_c == MATCH_ERROR)
5804 return MATCH_ERROR;
5805 if (is_bind_c == MATCH_YES)
5807 if (peek_char != '(')
5809 gfc_error ("Missing required parentheses before BIND(C) at %C");
5810 return MATCH_ERROR;
5812 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5813 &(entry->declared_at), 1))
5814 return MATCH_ERROR;
5817 if (!gfc_current_ns->parent
5818 && !add_global_entry (name, entry->binding_label, true,
5819 &old_loc))
5820 return MATCH_ERROR;
5822 /* An entry in a subroutine. */
5823 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5824 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5825 return MATCH_ERROR;
5827 else
5829 /* An entry in a function.
5830 We need to take special care because writing
5831 ENTRY f()
5833 ENTRY f
5834 is allowed, whereas
5835 ENTRY f() RESULT (r)
5836 can't be written as
5837 ENTRY f RESULT (r). */
5838 if (gfc_match_eos () == MATCH_YES)
5840 gfc_current_locus = old_loc;
5841 /* Match the empty argument list, and add the interface to
5842 the symbol. */
5843 m = gfc_match_formal_arglist (entry, 0, 1);
5845 else
5846 m = gfc_match_formal_arglist (entry, 0, 0);
5848 if (m != MATCH_YES)
5849 return MATCH_ERROR;
5851 result = NULL;
5853 if (gfc_match_eos () == MATCH_YES)
5855 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5856 || !gfc_add_function (&entry->attr, entry->name, NULL))
5857 return MATCH_ERROR;
5859 entry->result = entry;
5861 else
5863 m = gfc_match_suffix (entry, &result);
5864 if (m == MATCH_NO)
5865 gfc_syntax_error (ST_ENTRY);
5866 if (m != MATCH_YES)
5867 return MATCH_ERROR;
5869 if (result)
5871 if (!gfc_add_result (&result->attr, result->name, NULL)
5872 || !gfc_add_entry (&entry->attr, result->name, NULL)
5873 || !gfc_add_function (&entry->attr, result->name, NULL))
5874 return MATCH_ERROR;
5875 entry->result = result;
5877 else
5879 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5880 || !gfc_add_function (&entry->attr, entry->name, NULL))
5881 return MATCH_ERROR;
5882 entry->result = entry;
5886 if (!gfc_current_ns->parent
5887 && !add_global_entry (name, entry->binding_label, false,
5888 &old_loc))
5889 return MATCH_ERROR;
5892 if (gfc_match_eos () != MATCH_YES)
5894 gfc_syntax_error (ST_ENTRY);
5895 return MATCH_ERROR;
5898 entry->attr.recursive = proc->attr.recursive;
5899 entry->attr.elemental = proc->attr.elemental;
5900 entry->attr.pure = proc->attr.pure;
5902 el = gfc_get_entry_list ();
5903 el->sym = entry;
5904 el->next = gfc_current_ns->entries;
5905 gfc_current_ns->entries = el;
5906 if (el->next)
5907 el->id = el->next->id + 1;
5908 else
5909 el->id = 1;
5911 new_st.op = EXEC_ENTRY;
5912 new_st.ext.entry = el;
5914 return MATCH_YES;
5918 /* Match a subroutine statement, including optional prefixes. */
5920 match
5921 gfc_match_subroutine (void)
5923 char name[GFC_MAX_SYMBOL_LEN + 1];
5924 gfc_symbol *sym;
5925 match m;
5926 match is_bind_c;
5927 char peek_char;
5928 bool allow_binding_name;
5930 if (gfc_current_state () != COMP_NONE
5931 && gfc_current_state () != COMP_INTERFACE
5932 && gfc_current_state () != COMP_CONTAINS)
5933 return MATCH_NO;
5935 m = gfc_match_prefix (NULL);
5936 if (m != MATCH_YES)
5937 return m;
5939 m = gfc_match ("subroutine% %n", name);
5940 if (m != MATCH_YES)
5941 return m;
5943 if (get_proc_name (name, &sym, false))
5944 return MATCH_ERROR;
5946 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5947 the symbol existed before. */
5948 sym->declared_at = gfc_current_locus;
5950 if (current_attr.module_procedure)
5951 sym->attr.module_procedure = 1;
5953 if (add_hidden_procptr_result (sym))
5954 sym = sym->result;
5956 gfc_new_block = sym;
5958 /* Check what next non-whitespace character is so we can tell if there
5959 is the required parens if we have a BIND(C). */
5960 gfc_gobble_whitespace ();
5961 peek_char = gfc_peek_ascii_char ();
5963 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5964 return MATCH_ERROR;
5966 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5967 return MATCH_ERROR;
5969 /* Make sure that it isn't already declared as BIND(C). If it is, it
5970 must have been marked BIND(C) with a BIND(C) attribute and that is
5971 not allowed for procedures. */
5972 if (sym->attr.is_bind_c == 1)
5974 sym->attr.is_bind_c = 0;
5975 if (sym->old_symbol != NULL)
5976 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5977 "variables or common blocks",
5978 &(sym->old_symbol->declared_at));
5979 else
5980 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5981 "variables or common blocks", &gfc_current_locus);
5984 /* C binding names are not allowed for internal procedures. */
5985 if (gfc_current_state () == COMP_CONTAINS
5986 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5987 allow_binding_name = false;
5988 else
5989 allow_binding_name = true;
5991 /* Here, we are just checking if it has the bind(c) attribute, and if
5992 so, then we need to make sure it's all correct. If it doesn't,
5993 we still need to continue matching the rest of the subroutine line. */
5994 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5995 if (is_bind_c == MATCH_ERROR)
5997 /* There was an attempt at the bind(c), but it was wrong. An
5998 error message should have been printed w/in the gfc_match_bind_c
5999 so here we'll just return the MATCH_ERROR. */
6000 return MATCH_ERROR;
6003 if (is_bind_c == MATCH_YES)
6005 /* The following is allowed in the Fortran 2008 draft. */
6006 if (gfc_current_state () == COMP_CONTAINS
6007 && sym->ns->proc_name->attr.flavor != FL_MODULE
6008 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6009 "at %L may not be specified for an internal "
6010 "procedure", &gfc_current_locus))
6011 return MATCH_ERROR;
6013 if (peek_char != '(')
6015 gfc_error ("Missing required parentheses before BIND(C) at %C");
6016 return MATCH_ERROR;
6018 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
6019 &(sym->declared_at), 1))
6020 return MATCH_ERROR;
6023 if (gfc_match_eos () != MATCH_YES)
6025 gfc_syntax_error (ST_SUBROUTINE);
6026 return MATCH_ERROR;
6029 if (!copy_prefix (&sym->attr, &sym->declared_at))
6030 return MATCH_ERROR;
6032 /* Warn if it has the same name as an intrinsic. */
6033 do_warn_intrinsic_shadow (sym, false);
6035 return MATCH_YES;
6039 /* Check that the NAME identifier in a BIND attribute or statement
6040 is conform to C identifier rules. */
6042 match
6043 check_bind_name_identifier (char **name)
6045 char *n = *name, *p;
6047 /* Remove leading spaces. */
6048 while (*n == ' ')
6049 n++;
6051 /* On an empty string, free memory and set name to NULL. */
6052 if (*n == '\0')
6054 free (*name);
6055 *name = NULL;
6056 return MATCH_YES;
6059 /* Remove trailing spaces. */
6060 p = n + strlen(n) - 1;
6061 while (*p == ' ')
6062 *(p--) = '\0';
6064 /* Insert the identifier into the symbol table. */
6065 p = xstrdup (n);
6066 free (*name);
6067 *name = p;
6069 /* Now check that identifier is valid under C rules. */
6070 if (ISDIGIT (*p))
6072 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6073 return MATCH_ERROR;
6076 for (; *p; p++)
6077 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6079 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6080 return MATCH_ERROR;
6083 return MATCH_YES;
6087 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6088 given, and set the binding label in either the given symbol (if not
6089 NULL), or in the current_ts. The symbol may be NULL because we may
6090 encounter the BIND(C) before the declaration itself. Return
6091 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6092 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6093 or MATCH_YES if the specifier was correct and the binding label and
6094 bind(c) fields were set correctly for the given symbol or the
6095 current_ts. If allow_binding_name is false, no binding name may be
6096 given. */
6098 match
6099 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6101 char *binding_label = NULL;
6102 gfc_expr *e = NULL;
6104 /* Initialize the flag that specifies whether we encountered a NAME=
6105 specifier or not. */
6106 has_name_equals = 0;
6108 /* This much we have to be able to match, in this order, if
6109 there is a bind(c) label. */
6110 if (gfc_match (" bind ( c ") != MATCH_YES)
6111 return MATCH_NO;
6113 /* Now see if there is a binding label, or if we've reached the
6114 end of the bind(c) attribute without one. */
6115 if (gfc_match_char (',') == MATCH_YES)
6117 if (gfc_match (" name = ") != MATCH_YES)
6119 gfc_error ("Syntax error in NAME= specifier for binding label "
6120 "at %C");
6121 /* should give an error message here */
6122 return MATCH_ERROR;
6125 has_name_equals = 1;
6127 if (gfc_match_init_expr (&e) != MATCH_YES)
6129 gfc_free_expr (e);
6130 return MATCH_ERROR;
6133 if (!gfc_simplify_expr(e, 0))
6135 gfc_error ("NAME= specifier at %C should be a constant expression");
6136 gfc_free_expr (e);
6137 return MATCH_ERROR;
6140 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6141 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6143 gfc_error ("NAME= specifier at %C should be a scalar of "
6144 "default character kind");
6145 gfc_free_expr(e);
6146 return MATCH_ERROR;
6149 // Get a C string from the Fortran string constant
6150 binding_label = gfc_widechar_to_char (e->value.character.string,
6151 e->value.character.length);
6152 gfc_free_expr(e);
6154 // Check that it is valid (old gfc_match_name_C)
6155 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6156 return MATCH_ERROR;
6159 /* Get the required right paren. */
6160 if (gfc_match_char (')') != MATCH_YES)
6162 gfc_error ("Missing closing paren for binding label at %C");
6163 return MATCH_ERROR;
6166 if (has_name_equals && !allow_binding_name)
6168 gfc_error ("No binding name is allowed in BIND(C) at %C");
6169 return MATCH_ERROR;
6172 if (has_name_equals && sym != NULL && sym->attr.dummy)
6174 gfc_error ("For dummy procedure %s, no binding name is "
6175 "allowed in BIND(C) at %C", sym->name);
6176 return MATCH_ERROR;
6180 /* Save the binding label to the symbol. If sym is null, we're
6181 probably matching the typespec attributes of a declaration and
6182 haven't gotten the name yet, and therefore, no symbol yet. */
6183 if (binding_label)
6185 if (sym != NULL)
6186 sym->binding_label = binding_label;
6187 else
6188 curr_binding_label = binding_label;
6190 else if (allow_binding_name)
6192 /* No binding label, but if symbol isn't null, we
6193 can set the label for it here.
6194 If name="" or allow_binding_name is false, no C binding name is
6195 created. */
6196 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6197 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6200 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6201 && current_interface.type == INTERFACE_ABSTRACT)
6203 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6204 return MATCH_ERROR;
6207 return MATCH_YES;
6211 /* Return nonzero if we're currently compiling a contained procedure. */
6213 static int
6214 contained_procedure (void)
6216 gfc_state_data *s = gfc_state_stack;
6218 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6219 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6220 return 1;
6222 return 0;
6225 /* Set the kind of each enumerator. The kind is selected such that it is
6226 interoperable with the corresponding C enumeration type, making
6227 sure that -fshort-enums is honored. */
6229 static void
6230 set_enum_kind(void)
6232 enumerator_history *current_history = NULL;
6233 int kind;
6234 int i;
6236 if (max_enum == NULL || enum_history == NULL)
6237 return;
6239 if (!flag_short_enums)
6240 return;
6242 i = 0;
6245 kind = gfc_integer_kinds[i++].kind;
6247 while (kind < gfc_c_int_kind
6248 && gfc_check_integer_range (max_enum->initializer->value.integer,
6249 kind) != ARITH_OK);
6251 current_history = enum_history;
6252 while (current_history != NULL)
6254 current_history->sym->ts.kind = kind;
6255 current_history = current_history->next;
6260 /* Match any of the various end-block statements. Returns the type of
6261 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6262 and END BLOCK statements cannot be replaced by a single END statement. */
6264 match
6265 gfc_match_end (gfc_statement *st)
6267 char name[GFC_MAX_SYMBOL_LEN + 1];
6268 gfc_compile_state state;
6269 locus old_loc;
6270 const char *block_name;
6271 const char *target;
6272 int eos_ok;
6273 match m;
6274 gfc_namespace *parent_ns, *ns, *prev_ns;
6275 gfc_namespace **nsp;
6276 bool abreviated_modproc_decl;
6278 old_loc = gfc_current_locus;
6279 if (gfc_match ("end") != MATCH_YES)
6280 return MATCH_NO;
6282 state = gfc_current_state ();
6283 block_name = gfc_current_block () == NULL
6284 ? NULL : gfc_current_block ()->name;
6286 switch (state)
6288 case COMP_ASSOCIATE:
6289 case COMP_BLOCK:
6290 if (!strncmp (block_name, "block@", strlen("block@")))
6291 block_name = NULL;
6292 break;
6294 case COMP_CONTAINS:
6295 case COMP_DERIVED_CONTAINS:
6296 state = gfc_state_stack->previous->state;
6297 block_name = gfc_state_stack->previous->sym == NULL
6298 ? NULL : gfc_state_stack->previous->sym->name;
6299 break;
6301 default:
6302 break;
6305 abreviated_modproc_decl
6306 = gfc_current_block ()
6307 && gfc_current_block ()->abr_modproc_decl;
6309 switch (state)
6311 case COMP_NONE:
6312 case COMP_PROGRAM:
6313 *st = ST_END_PROGRAM;
6314 target = " program";
6315 eos_ok = 1;
6316 break;
6318 case COMP_SUBROUTINE:
6319 *st = ST_END_SUBROUTINE;
6320 if (!abreviated_modproc_decl)
6321 target = " subroutine";
6322 else
6323 target = " procedure";
6324 eos_ok = !contained_procedure ();
6325 break;
6327 case COMP_FUNCTION:
6328 *st = ST_END_FUNCTION;
6329 if (!abreviated_modproc_decl)
6330 target = " function";
6331 else
6332 target = " procedure";
6333 eos_ok = !contained_procedure ();
6334 break;
6336 case COMP_BLOCK_DATA:
6337 *st = ST_END_BLOCK_DATA;
6338 target = " block data";
6339 eos_ok = 1;
6340 break;
6342 case COMP_MODULE:
6343 *st = ST_END_MODULE;
6344 target = " module";
6345 eos_ok = 1;
6346 break;
6348 case COMP_SUBMODULE:
6349 *st = ST_END_SUBMODULE;
6350 target = " submodule";
6351 eos_ok = 1;
6352 break;
6354 case COMP_INTERFACE:
6355 *st = ST_END_INTERFACE;
6356 target = " interface";
6357 eos_ok = 0;
6358 break;
6360 case COMP_DERIVED:
6361 case COMP_DERIVED_CONTAINS:
6362 *st = ST_END_TYPE;
6363 target = " type";
6364 eos_ok = 0;
6365 break;
6367 case COMP_ASSOCIATE:
6368 *st = ST_END_ASSOCIATE;
6369 target = " associate";
6370 eos_ok = 0;
6371 break;
6373 case COMP_BLOCK:
6374 *st = ST_END_BLOCK;
6375 target = " block";
6376 eos_ok = 0;
6377 break;
6379 case COMP_IF:
6380 *st = ST_ENDIF;
6381 target = " if";
6382 eos_ok = 0;
6383 break;
6385 case COMP_DO:
6386 case COMP_DO_CONCURRENT:
6387 *st = ST_ENDDO;
6388 target = " do";
6389 eos_ok = 0;
6390 break;
6392 case COMP_CRITICAL:
6393 *st = ST_END_CRITICAL;
6394 target = " critical";
6395 eos_ok = 0;
6396 break;
6398 case COMP_SELECT:
6399 case COMP_SELECT_TYPE:
6400 *st = ST_END_SELECT;
6401 target = " select";
6402 eos_ok = 0;
6403 break;
6405 case COMP_FORALL:
6406 *st = ST_END_FORALL;
6407 target = " forall";
6408 eos_ok = 0;
6409 break;
6411 case COMP_WHERE:
6412 *st = ST_END_WHERE;
6413 target = " where";
6414 eos_ok = 0;
6415 break;
6417 case COMP_ENUM:
6418 *st = ST_END_ENUM;
6419 target = " enum";
6420 eos_ok = 0;
6421 last_initializer = NULL;
6422 set_enum_kind ();
6423 gfc_free_enum_history ();
6424 break;
6426 default:
6427 gfc_error ("Unexpected END statement at %C");
6428 goto cleanup;
6431 old_loc = gfc_current_locus;
6432 if (gfc_match_eos () == MATCH_YES)
6434 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6436 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6437 "instead of %s statement at %L",
6438 abreviated_modproc_decl ? "END PROCEDURE"
6439 : gfc_ascii_statement(*st), &old_loc))
6440 goto cleanup;
6442 else if (!eos_ok)
6444 /* We would have required END [something]. */
6445 gfc_error ("%s statement expected at %L",
6446 gfc_ascii_statement (*st), &old_loc);
6447 goto cleanup;
6450 return MATCH_YES;
6453 /* Verify that we've got the sort of end-block that we're expecting. */
6454 if (gfc_match (target) != MATCH_YES)
6456 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6457 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6458 goto cleanup;
6461 old_loc = gfc_current_locus;
6462 /* If we're at the end, make sure a block name wasn't required. */
6463 if (gfc_match_eos () == MATCH_YES)
6466 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6467 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6468 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6469 return MATCH_YES;
6471 if (!block_name)
6472 return MATCH_YES;
6474 gfc_error ("Expected block name of %qs in %s statement at %L",
6475 block_name, gfc_ascii_statement (*st), &old_loc);
6477 return MATCH_ERROR;
6480 /* END INTERFACE has a special handler for its several possible endings. */
6481 if (*st == ST_END_INTERFACE)
6482 return gfc_match_end_interface ();
6484 /* We haven't hit the end of statement, so what is left must be an
6485 end-name. */
6486 m = gfc_match_space ();
6487 if (m == MATCH_YES)
6488 m = gfc_match_name (name);
6490 if (m == MATCH_NO)
6491 gfc_error ("Expected terminating name at %C");
6492 if (m != MATCH_YES)
6493 goto cleanup;
6495 if (block_name == NULL)
6496 goto syntax;
6498 /* We have to pick out the declared submodule name from the composite
6499 required by F2008:11.2.3 para 2, which ends in the declared name. */
6500 if (state == COMP_SUBMODULE)
6501 block_name = strchr (block_name, '.') + 1;
6503 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6505 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6506 gfc_ascii_statement (*st));
6507 goto cleanup;
6509 /* Procedure pointer as function result. */
6510 else if (strcmp (block_name, "ppr@") == 0
6511 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6513 gfc_error ("Expected label %qs for %s statement at %C",
6514 gfc_current_block ()->ns->proc_name->name,
6515 gfc_ascii_statement (*st));
6516 goto cleanup;
6519 if (gfc_match_eos () == MATCH_YES)
6520 return MATCH_YES;
6522 syntax:
6523 gfc_syntax_error (*st);
6525 cleanup:
6526 gfc_current_locus = old_loc;
6528 /* If we are missing an END BLOCK, we created a half-ready namespace.
6529 Remove it from the parent namespace's sibling list. */
6531 while (state == COMP_BLOCK)
6533 parent_ns = gfc_current_ns->parent;
6535 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6537 prev_ns = NULL;
6538 ns = *nsp;
6539 while (ns)
6541 if (ns == gfc_current_ns)
6543 if (prev_ns == NULL)
6544 *nsp = NULL;
6545 else
6546 prev_ns->sibling = ns->sibling;
6548 prev_ns = ns;
6549 ns = ns->sibling;
6552 gfc_free_namespace (gfc_current_ns);
6553 gfc_current_ns = parent_ns;
6554 gfc_state_stack = gfc_state_stack->previous;
6555 state = gfc_current_state ();
6558 return MATCH_ERROR;
6563 /***************** Attribute declaration statements ****************/
6565 /* Set the attribute of a single variable. */
6567 static match
6568 attr_decl1 (void)
6570 char name[GFC_MAX_SYMBOL_LEN + 1];
6571 gfc_array_spec *as;
6573 /* Workaround -Wmaybe-uninitialized false positive during
6574 profiledbootstrap by initializing them. */
6575 gfc_symbol *sym = NULL;
6576 locus var_locus;
6577 match m;
6579 as = NULL;
6581 m = gfc_match_name (name);
6582 if (m != MATCH_YES)
6583 goto cleanup;
6585 if (find_special (name, &sym, false))
6586 return MATCH_ERROR;
6588 if (!check_function_name (name))
6590 m = MATCH_ERROR;
6591 goto cleanup;
6594 var_locus = gfc_current_locus;
6596 /* Deal with possible array specification for certain attributes. */
6597 if (current_attr.dimension
6598 || current_attr.codimension
6599 || current_attr.allocatable
6600 || current_attr.pointer
6601 || current_attr.target)
6603 m = gfc_match_array_spec (&as, !current_attr.codimension,
6604 !current_attr.dimension
6605 && !current_attr.pointer
6606 && !current_attr.target);
6607 if (m == MATCH_ERROR)
6608 goto cleanup;
6610 if (current_attr.dimension && m == MATCH_NO)
6612 gfc_error ("Missing array specification at %L in DIMENSION "
6613 "statement", &var_locus);
6614 m = MATCH_ERROR;
6615 goto cleanup;
6618 if (current_attr.dimension && sym->value)
6620 gfc_error ("Dimensions specified for %s at %L after its "
6621 "initialisation", sym->name, &var_locus);
6622 m = MATCH_ERROR;
6623 goto cleanup;
6626 if (current_attr.codimension && m == MATCH_NO)
6628 gfc_error ("Missing array specification at %L in CODIMENSION "
6629 "statement", &var_locus);
6630 m = MATCH_ERROR;
6631 goto cleanup;
6634 if ((current_attr.allocatable || current_attr.pointer)
6635 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6637 gfc_error ("Array specification must be deferred at %L", &var_locus);
6638 m = MATCH_ERROR;
6639 goto cleanup;
6643 /* Update symbol table. DIMENSION attribute is set in
6644 gfc_set_array_spec(). For CLASS variables, this must be applied
6645 to the first component, or '_data' field. */
6646 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6648 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6650 m = MATCH_ERROR;
6651 goto cleanup;
6654 else
6656 if (current_attr.dimension == 0 && current_attr.codimension == 0
6657 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6659 m = MATCH_ERROR;
6660 goto cleanup;
6664 if (sym->ts.type == BT_CLASS
6665 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6667 m = MATCH_ERROR;
6668 goto cleanup;
6671 if (!gfc_set_array_spec (sym, as, &var_locus))
6673 m = MATCH_ERROR;
6674 goto cleanup;
6677 if (sym->attr.cray_pointee && sym->as != NULL)
6679 /* Fix the array spec. */
6680 m = gfc_mod_pointee_as (sym->as);
6681 if (m == MATCH_ERROR)
6682 goto cleanup;
6685 if (!gfc_add_attribute (&sym->attr, &var_locus))
6687 m = MATCH_ERROR;
6688 goto cleanup;
6691 if ((current_attr.external || current_attr.intrinsic)
6692 && sym->attr.flavor != FL_PROCEDURE
6693 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6695 m = MATCH_ERROR;
6696 goto cleanup;
6699 add_hidden_procptr_result (sym);
6701 return MATCH_YES;
6703 cleanup:
6704 gfc_free_array_spec (as);
6705 return m;
6709 /* Generic attribute declaration subroutine. Used for attributes that
6710 just have a list of names. */
6712 static match
6713 attr_decl (void)
6715 match m;
6717 /* Gobble the optional double colon, by simply ignoring the result
6718 of gfc_match(). */
6719 gfc_match (" ::");
6721 for (;;)
6723 m = attr_decl1 ();
6724 if (m != MATCH_YES)
6725 break;
6727 if (gfc_match_eos () == MATCH_YES)
6729 m = MATCH_YES;
6730 break;
6733 if (gfc_match_char (',') != MATCH_YES)
6735 gfc_error ("Unexpected character in variable list at %C");
6736 m = MATCH_ERROR;
6737 break;
6741 return m;
6745 /* This routine matches Cray Pointer declarations of the form:
6746 pointer ( <pointer>, <pointee> )
6748 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6749 The pointer, if already declared, should be an integer. Otherwise, we
6750 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6751 be either a scalar, or an array declaration. No space is allocated for
6752 the pointee. For the statement
6753 pointer (ipt, ar(10))
6754 any subsequent uses of ar will be translated (in C-notation) as
6755 ar(i) => ((<type> *) ipt)(i)
6756 After gimplification, pointee variable will disappear in the code. */
6758 static match
6759 cray_pointer_decl (void)
6761 match m;
6762 gfc_array_spec *as = NULL;
6763 gfc_symbol *cptr; /* Pointer symbol. */
6764 gfc_symbol *cpte; /* Pointee symbol. */
6765 locus var_locus;
6766 bool done = false;
6768 while (!done)
6770 if (gfc_match_char ('(') != MATCH_YES)
6772 gfc_error ("Expected %<(%> at %C");
6773 return MATCH_ERROR;
6776 /* Match pointer. */
6777 var_locus = gfc_current_locus;
6778 gfc_clear_attr (&current_attr);
6779 gfc_add_cray_pointer (&current_attr, &var_locus);
6780 current_ts.type = BT_INTEGER;
6781 current_ts.kind = gfc_index_integer_kind;
6783 m = gfc_match_symbol (&cptr, 0);
6784 if (m != MATCH_YES)
6786 gfc_error ("Expected variable name at %C");
6787 return m;
6790 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6791 return MATCH_ERROR;
6793 gfc_set_sym_referenced (cptr);
6795 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6797 cptr->ts.type = BT_INTEGER;
6798 cptr->ts.kind = gfc_index_integer_kind;
6800 else if (cptr->ts.type != BT_INTEGER)
6802 gfc_error ("Cray pointer at %C must be an integer");
6803 return MATCH_ERROR;
6805 else if (cptr->ts.kind < gfc_index_integer_kind)
6806 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6807 " memory addresses require %d bytes",
6808 cptr->ts.kind, gfc_index_integer_kind);
6810 if (gfc_match_char (',') != MATCH_YES)
6812 gfc_error ("Expected \",\" at %C");
6813 return MATCH_ERROR;
6816 /* Match Pointee. */
6817 var_locus = gfc_current_locus;
6818 gfc_clear_attr (&current_attr);
6819 gfc_add_cray_pointee (&current_attr, &var_locus);
6820 current_ts.type = BT_UNKNOWN;
6821 current_ts.kind = 0;
6823 m = gfc_match_symbol (&cpte, 0);
6824 if (m != MATCH_YES)
6826 gfc_error ("Expected variable name at %C");
6827 return m;
6830 /* Check for an optional array spec. */
6831 m = gfc_match_array_spec (&as, true, false);
6832 if (m == MATCH_ERROR)
6834 gfc_free_array_spec (as);
6835 return m;
6837 else if (m == MATCH_NO)
6839 gfc_free_array_spec (as);
6840 as = NULL;
6843 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6844 return MATCH_ERROR;
6846 gfc_set_sym_referenced (cpte);
6848 if (cpte->as == NULL)
6850 if (!gfc_set_array_spec (cpte, as, &var_locus))
6851 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6853 else if (as != NULL)
6855 gfc_error ("Duplicate array spec for Cray pointee at %C");
6856 gfc_free_array_spec (as);
6857 return MATCH_ERROR;
6860 as = NULL;
6862 if (cpte->as != NULL)
6864 /* Fix array spec. */
6865 m = gfc_mod_pointee_as (cpte->as);
6866 if (m == MATCH_ERROR)
6867 return m;
6870 /* Point the Pointee at the Pointer. */
6871 cpte->cp_pointer = cptr;
6873 if (gfc_match_char (')') != MATCH_YES)
6875 gfc_error ("Expected \")\" at %C");
6876 return MATCH_ERROR;
6878 m = gfc_match_char (',');
6879 if (m != MATCH_YES)
6880 done = true; /* Stop searching for more declarations. */
6884 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6885 || gfc_match_eos () != MATCH_YES)
6887 gfc_error ("Expected %<,%> or end of statement at %C");
6888 return MATCH_ERROR;
6890 return MATCH_YES;
6894 match
6895 gfc_match_external (void)
6898 gfc_clear_attr (&current_attr);
6899 current_attr.external = 1;
6901 return attr_decl ();
6905 match
6906 gfc_match_intent (void)
6908 sym_intent intent;
6910 /* This is not allowed within a BLOCK construct! */
6911 if (gfc_current_state () == COMP_BLOCK)
6913 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6914 return MATCH_ERROR;
6917 intent = match_intent_spec ();
6918 if (intent == INTENT_UNKNOWN)
6919 return MATCH_ERROR;
6921 gfc_clear_attr (&current_attr);
6922 current_attr.intent = intent;
6924 return attr_decl ();
6928 match
6929 gfc_match_intrinsic (void)
6932 gfc_clear_attr (&current_attr);
6933 current_attr.intrinsic = 1;
6935 return attr_decl ();
6939 match
6940 gfc_match_optional (void)
6942 /* This is not allowed within a BLOCK construct! */
6943 if (gfc_current_state () == COMP_BLOCK)
6945 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6946 return MATCH_ERROR;
6949 gfc_clear_attr (&current_attr);
6950 current_attr.optional = 1;
6952 return attr_decl ();
6956 match
6957 gfc_match_pointer (void)
6959 gfc_gobble_whitespace ();
6960 if (gfc_peek_ascii_char () == '(')
6962 if (!flag_cray_pointer)
6964 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6965 "flag");
6966 return MATCH_ERROR;
6968 return cray_pointer_decl ();
6970 else
6972 gfc_clear_attr (&current_attr);
6973 current_attr.pointer = 1;
6975 return attr_decl ();
6980 match
6981 gfc_match_allocatable (void)
6983 gfc_clear_attr (&current_attr);
6984 current_attr.allocatable = 1;
6986 return attr_decl ();
6990 match
6991 gfc_match_codimension (void)
6993 gfc_clear_attr (&current_attr);
6994 current_attr.codimension = 1;
6996 return attr_decl ();
7000 match
7001 gfc_match_contiguous (void)
7003 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
7004 return MATCH_ERROR;
7006 gfc_clear_attr (&current_attr);
7007 current_attr.contiguous = 1;
7009 return attr_decl ();
7013 match
7014 gfc_match_dimension (void)
7016 gfc_clear_attr (&current_attr);
7017 current_attr.dimension = 1;
7019 return attr_decl ();
7023 match
7024 gfc_match_target (void)
7026 gfc_clear_attr (&current_attr);
7027 current_attr.target = 1;
7029 return attr_decl ();
7033 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7034 statement. */
7036 static match
7037 access_attr_decl (gfc_statement st)
7039 char name[GFC_MAX_SYMBOL_LEN + 1];
7040 interface_type type;
7041 gfc_user_op *uop;
7042 gfc_symbol *sym, *dt_sym;
7043 gfc_intrinsic_op op;
7044 match m;
7046 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7047 goto done;
7049 for (;;)
7051 m = gfc_match_generic_spec (&type, name, &op);
7052 if (m == MATCH_NO)
7053 goto syntax;
7054 if (m == MATCH_ERROR)
7055 return MATCH_ERROR;
7057 switch (type)
7059 case INTERFACE_NAMELESS:
7060 case INTERFACE_ABSTRACT:
7061 goto syntax;
7063 case INTERFACE_GENERIC:
7064 if (gfc_get_symbol (name, NULL, &sym))
7065 goto done;
7067 if (!gfc_add_access (&sym->attr,
7068 (st == ST_PUBLIC)
7069 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7070 sym->name, NULL))
7071 return MATCH_ERROR;
7073 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7074 && !gfc_add_access (&dt_sym->attr,
7075 (st == ST_PUBLIC)
7076 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7077 sym->name, NULL))
7078 return MATCH_ERROR;
7080 break;
7082 case INTERFACE_INTRINSIC_OP:
7083 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7085 gfc_intrinsic_op other_op;
7087 gfc_current_ns->operator_access[op] =
7088 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7090 /* Handle the case if there is another op with the same
7091 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7092 other_op = gfc_equivalent_op (op);
7094 if (other_op != INTRINSIC_NONE)
7095 gfc_current_ns->operator_access[other_op] =
7096 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7099 else
7101 gfc_error ("Access specification of the %s operator at %C has "
7102 "already been specified", gfc_op2string (op));
7103 goto done;
7106 break;
7108 case INTERFACE_USER_OP:
7109 uop = gfc_get_uop (name);
7111 if (uop->access == ACCESS_UNKNOWN)
7113 uop->access = (st == ST_PUBLIC)
7114 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7116 else
7118 gfc_error ("Access specification of the .%s. operator at %C "
7119 "has already been specified", sym->name);
7120 goto done;
7123 break;
7126 if (gfc_match_char (',') == MATCH_NO)
7127 break;
7130 if (gfc_match_eos () != MATCH_YES)
7131 goto syntax;
7132 return MATCH_YES;
7134 syntax:
7135 gfc_syntax_error (st);
7137 done:
7138 return MATCH_ERROR;
7142 match
7143 gfc_match_protected (void)
7145 gfc_symbol *sym;
7146 match m;
7148 if (!gfc_current_ns->proc_name
7149 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7151 gfc_error ("PROTECTED at %C only allowed in specification "
7152 "part of a module");
7153 return MATCH_ERROR;
7157 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7158 return MATCH_ERROR;
7160 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7162 return MATCH_ERROR;
7165 if (gfc_match_eos () == MATCH_YES)
7166 goto syntax;
7168 for(;;)
7170 m = gfc_match_symbol (&sym, 0);
7171 switch (m)
7173 case MATCH_YES:
7174 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7175 return MATCH_ERROR;
7176 goto next_item;
7178 case MATCH_NO:
7179 break;
7181 case MATCH_ERROR:
7182 return MATCH_ERROR;
7185 next_item:
7186 if (gfc_match_eos () == MATCH_YES)
7187 break;
7188 if (gfc_match_char (',') != MATCH_YES)
7189 goto syntax;
7192 return MATCH_YES;
7194 syntax:
7195 gfc_error ("Syntax error in PROTECTED statement at %C");
7196 return MATCH_ERROR;
7200 /* The PRIVATE statement is a bit weird in that it can be an attribute
7201 declaration, but also works as a standalone statement inside of a
7202 type declaration or a module. */
7204 match
7205 gfc_match_private (gfc_statement *st)
7208 if (gfc_match ("private") != MATCH_YES)
7209 return MATCH_NO;
7211 if (gfc_current_state () != COMP_MODULE
7212 && !(gfc_current_state () == COMP_DERIVED
7213 && gfc_state_stack->previous
7214 && gfc_state_stack->previous->state == COMP_MODULE)
7215 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7216 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7217 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7219 gfc_error ("PRIVATE statement at %C is only allowed in the "
7220 "specification part of a module");
7221 return MATCH_ERROR;
7224 if (gfc_current_state () == COMP_DERIVED)
7226 if (gfc_match_eos () == MATCH_YES)
7228 *st = ST_PRIVATE;
7229 return MATCH_YES;
7232 gfc_syntax_error (ST_PRIVATE);
7233 return MATCH_ERROR;
7236 if (gfc_match_eos () == MATCH_YES)
7238 *st = ST_PRIVATE;
7239 return MATCH_YES;
7242 *st = ST_ATTR_DECL;
7243 return access_attr_decl (ST_PRIVATE);
7247 match
7248 gfc_match_public (gfc_statement *st)
7251 if (gfc_match ("public") != MATCH_YES)
7252 return MATCH_NO;
7254 if (gfc_current_state () != COMP_MODULE)
7256 gfc_error ("PUBLIC statement at %C is only allowed in the "
7257 "specification part of a module");
7258 return MATCH_ERROR;
7261 if (gfc_match_eos () == MATCH_YES)
7263 *st = ST_PUBLIC;
7264 return MATCH_YES;
7267 *st = ST_ATTR_DECL;
7268 return access_attr_decl (ST_PUBLIC);
7272 /* Workhorse for gfc_match_parameter. */
7274 static match
7275 do_parm (void)
7277 gfc_symbol *sym;
7278 gfc_expr *init;
7279 match m;
7280 bool t;
7282 m = gfc_match_symbol (&sym, 0);
7283 if (m == MATCH_NO)
7284 gfc_error ("Expected variable name at %C in PARAMETER statement");
7286 if (m != MATCH_YES)
7287 return m;
7289 if (gfc_match_char ('=') == MATCH_NO)
7291 gfc_error ("Expected = sign in PARAMETER statement at %C");
7292 return MATCH_ERROR;
7295 m = gfc_match_init_expr (&init);
7296 if (m == MATCH_NO)
7297 gfc_error ("Expected expression at %C in PARAMETER statement");
7298 if (m != MATCH_YES)
7299 return m;
7301 if (sym->ts.type == BT_UNKNOWN
7302 && !gfc_set_default_type (sym, 1, NULL))
7304 m = MATCH_ERROR;
7305 goto cleanup;
7308 if (!gfc_check_assign_symbol (sym, NULL, init)
7309 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7311 m = MATCH_ERROR;
7312 goto cleanup;
7315 if (sym->value)
7317 gfc_error ("Initializing already initialized variable at %C");
7318 m = MATCH_ERROR;
7319 goto cleanup;
7322 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7323 return (t) ? MATCH_YES : MATCH_ERROR;
7325 cleanup:
7326 gfc_free_expr (init);
7327 return m;
7331 /* Match a parameter statement, with the weird syntax that these have. */
7333 match
7334 gfc_match_parameter (void)
7336 match m;
7338 if (gfc_match_char ('(') == MATCH_NO)
7339 return MATCH_NO;
7341 for (;;)
7343 m = do_parm ();
7344 if (m != MATCH_YES)
7345 break;
7347 if (gfc_match (" )%t") == MATCH_YES)
7348 break;
7350 if (gfc_match_char (',') != MATCH_YES)
7352 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7353 m = MATCH_ERROR;
7354 break;
7358 return m;
7362 /* Save statements have a special syntax. */
7364 match
7365 gfc_match_save (void)
7367 char n[GFC_MAX_SYMBOL_LEN+1];
7368 gfc_common_head *c;
7369 gfc_symbol *sym;
7370 match m;
7372 if (gfc_match_eos () == MATCH_YES)
7374 if (gfc_current_ns->seen_save)
7376 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7377 "follows previous SAVE statement"))
7378 return MATCH_ERROR;
7381 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7382 return MATCH_YES;
7385 if (gfc_current_ns->save_all)
7387 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7388 "blanket SAVE statement"))
7389 return MATCH_ERROR;
7392 gfc_match (" ::");
7394 for (;;)
7396 m = gfc_match_symbol (&sym, 0);
7397 switch (m)
7399 case MATCH_YES:
7400 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7401 &gfc_current_locus))
7402 return MATCH_ERROR;
7403 goto next_item;
7405 case MATCH_NO:
7406 break;
7408 case MATCH_ERROR:
7409 return MATCH_ERROR;
7412 m = gfc_match (" / %n /", &n);
7413 if (m == MATCH_ERROR)
7414 return MATCH_ERROR;
7415 if (m == MATCH_NO)
7416 goto syntax;
7418 c = gfc_get_common (n, 0);
7419 c->saved = 1;
7421 gfc_current_ns->seen_save = 1;
7423 next_item:
7424 if (gfc_match_eos () == MATCH_YES)
7425 break;
7426 if (gfc_match_char (',') != MATCH_YES)
7427 goto syntax;
7430 return MATCH_YES;
7432 syntax:
7433 gfc_error ("Syntax error in SAVE statement at %C");
7434 return MATCH_ERROR;
7438 match
7439 gfc_match_value (void)
7441 gfc_symbol *sym;
7442 match m;
7444 /* This is not allowed within a BLOCK construct! */
7445 if (gfc_current_state () == COMP_BLOCK)
7447 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7448 return MATCH_ERROR;
7451 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7452 return MATCH_ERROR;
7454 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7456 return MATCH_ERROR;
7459 if (gfc_match_eos () == MATCH_YES)
7460 goto syntax;
7462 for(;;)
7464 m = gfc_match_symbol (&sym, 0);
7465 switch (m)
7467 case MATCH_YES:
7468 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7469 return MATCH_ERROR;
7470 goto next_item;
7472 case MATCH_NO:
7473 break;
7475 case MATCH_ERROR:
7476 return MATCH_ERROR;
7479 next_item:
7480 if (gfc_match_eos () == MATCH_YES)
7481 break;
7482 if (gfc_match_char (',') != MATCH_YES)
7483 goto syntax;
7486 return MATCH_YES;
7488 syntax:
7489 gfc_error ("Syntax error in VALUE statement at %C");
7490 return MATCH_ERROR;
7494 match
7495 gfc_match_volatile (void)
7497 gfc_symbol *sym;
7498 match m;
7500 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7501 return MATCH_ERROR;
7503 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7505 return MATCH_ERROR;
7508 if (gfc_match_eos () == MATCH_YES)
7509 goto syntax;
7511 for(;;)
7513 /* VOLATILE is special because it can be added to host-associated
7514 symbols locally. Except for coarrays. */
7515 m = gfc_match_symbol (&sym, 1);
7516 switch (m)
7518 case MATCH_YES:
7519 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7520 for variable in a BLOCK which is defined outside of the BLOCK. */
7521 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7523 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7524 "%C, which is use-/host-associated", sym->name);
7525 return MATCH_ERROR;
7527 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7528 return MATCH_ERROR;
7529 goto next_item;
7531 case MATCH_NO:
7532 break;
7534 case MATCH_ERROR:
7535 return MATCH_ERROR;
7538 next_item:
7539 if (gfc_match_eos () == MATCH_YES)
7540 break;
7541 if (gfc_match_char (',') != MATCH_YES)
7542 goto syntax;
7545 return MATCH_YES;
7547 syntax:
7548 gfc_error ("Syntax error in VOLATILE statement at %C");
7549 return MATCH_ERROR;
7553 match
7554 gfc_match_asynchronous (void)
7556 gfc_symbol *sym;
7557 match m;
7559 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7560 return MATCH_ERROR;
7562 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7564 return MATCH_ERROR;
7567 if (gfc_match_eos () == MATCH_YES)
7568 goto syntax;
7570 for(;;)
7572 /* ASYNCHRONOUS is special because it can be added to host-associated
7573 symbols locally. */
7574 m = gfc_match_symbol (&sym, 1);
7575 switch (m)
7577 case MATCH_YES:
7578 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7579 return MATCH_ERROR;
7580 goto next_item;
7582 case MATCH_NO:
7583 break;
7585 case MATCH_ERROR:
7586 return MATCH_ERROR;
7589 next_item:
7590 if (gfc_match_eos () == MATCH_YES)
7591 break;
7592 if (gfc_match_char (',') != MATCH_YES)
7593 goto syntax;
7596 return MATCH_YES;
7598 syntax:
7599 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7600 return MATCH_ERROR;
7604 /* Match a module procedure statement in a submodule. */
7606 match
7607 gfc_match_submod_proc (void)
7609 char name[GFC_MAX_SYMBOL_LEN + 1];
7610 gfc_symbol *sym, *fsym;
7611 match m;
7612 gfc_formal_arglist *formal, *head, *tail;
7614 if (gfc_current_state () != COMP_CONTAINS
7615 || !(gfc_state_stack->previous
7616 && gfc_state_stack->previous->state == COMP_SUBMODULE))
7617 return MATCH_NO;
7619 m = gfc_match (" module% procedure% %n", name);
7620 if (m != MATCH_YES)
7621 return m;
7623 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
7624 "at %C"))
7625 return MATCH_ERROR;
7627 if (get_proc_name (name, &sym, false))
7628 return MATCH_ERROR;
7630 /* Make sure that the result field is appropriately filled, even though
7631 the result symbol will be replaced later on. */
7632 if (sym->ts.interface->attr.function)
7634 if (sym->ts.interface->result
7635 && sym->ts.interface->result != sym->ts.interface)
7636 sym->result= sym->ts.interface->result;
7637 else
7638 sym->result = sym;
7641 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7642 the symbol existed before. */
7643 sym->declared_at = gfc_current_locus;
7645 if (!sym->attr.module_procedure)
7646 return MATCH_ERROR;
7648 /* Signal match_end to expect "end procedure". */
7649 sym->abr_modproc_decl = 1;
7651 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7652 sym->attr.if_source = IFSRC_DECL;
7654 gfc_new_block = sym;
7656 /* Make a new formal arglist with the symbols in the procedure
7657 namespace. */
7658 head = tail = NULL;
7659 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
7661 if (formal == sym->formal)
7662 head = tail = gfc_get_formal_arglist ();
7663 else
7665 tail->next = gfc_get_formal_arglist ();
7666 tail = tail->next;
7669 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
7670 goto cleanup;
7672 tail->sym = fsym;
7673 gfc_set_sym_referenced (fsym);
7676 /* The dummy symbols get cleaned up, when the formal_namespace of the
7677 interface declaration is cleared. This allows us to add the
7678 explicit interface as is done for other type of procedure. */
7679 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
7680 &gfc_current_locus))
7681 return MATCH_ERROR;
7683 if (gfc_match_eos () != MATCH_YES)
7685 gfc_syntax_error (ST_MODULE_PROC);
7686 return MATCH_ERROR;
7689 return MATCH_YES;
7691 cleanup:
7692 gfc_free_formal_arglist (head);
7693 return MATCH_ERROR;
7697 /* Match a module procedure statement. Note that we have to modify
7698 symbols in the parent's namespace because the current one was there
7699 to receive symbols that are in an interface's formal argument list. */
7701 match
7702 gfc_match_modproc (void)
7704 char name[GFC_MAX_SYMBOL_LEN + 1];
7705 gfc_symbol *sym;
7706 match m;
7707 locus old_locus;
7708 gfc_namespace *module_ns;
7709 gfc_interface *old_interface_head, *interface;
7711 if (gfc_state_stack->state != COMP_INTERFACE
7712 || gfc_state_stack->previous == NULL
7713 || current_interface.type == INTERFACE_NAMELESS
7714 || current_interface.type == INTERFACE_ABSTRACT)
7716 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7717 "interface");
7718 return MATCH_ERROR;
7721 module_ns = gfc_current_ns->parent;
7722 for (; module_ns; module_ns = module_ns->parent)
7723 if (module_ns->proc_name->attr.flavor == FL_MODULE
7724 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7725 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7726 && !module_ns->proc_name->attr.contained))
7727 break;
7729 if (module_ns == NULL)
7730 return MATCH_ERROR;
7732 /* Store the current state of the interface. We will need it if we
7733 end up with a syntax error and need to recover. */
7734 old_interface_head = gfc_current_interface_head ();
7736 /* Check if the F2008 optional double colon appears. */
7737 gfc_gobble_whitespace ();
7738 old_locus = gfc_current_locus;
7739 if (gfc_match ("::") == MATCH_YES)
7741 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7742 "MODULE PROCEDURE statement at %L", &old_locus))
7743 return MATCH_ERROR;
7745 else
7746 gfc_current_locus = old_locus;
7748 for (;;)
7750 bool last = false;
7751 old_locus = gfc_current_locus;
7753 m = gfc_match_name (name);
7754 if (m == MATCH_NO)
7755 goto syntax;
7756 if (m != MATCH_YES)
7757 return MATCH_ERROR;
7759 /* Check for syntax error before starting to add symbols to the
7760 current namespace. */
7761 if (gfc_match_eos () == MATCH_YES)
7762 last = true;
7764 if (!last && gfc_match_char (',') != MATCH_YES)
7765 goto syntax;
7767 /* Now we're sure the syntax is valid, we process this item
7768 further. */
7769 if (gfc_get_symbol (name, module_ns, &sym))
7770 return MATCH_ERROR;
7772 if (sym->attr.intrinsic)
7774 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7775 "PROCEDURE", &old_locus);
7776 return MATCH_ERROR;
7779 if (sym->attr.proc != PROC_MODULE
7780 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7781 return MATCH_ERROR;
7783 if (!gfc_add_interface (sym))
7784 return MATCH_ERROR;
7786 sym->attr.mod_proc = 1;
7787 sym->declared_at = old_locus;
7789 if (last)
7790 break;
7793 return MATCH_YES;
7795 syntax:
7796 /* Restore the previous state of the interface. */
7797 interface = gfc_current_interface_head ();
7798 gfc_set_current_interface_head (old_interface_head);
7800 /* Free the new interfaces. */
7801 while (interface != old_interface_head)
7803 gfc_interface *i = interface->next;
7804 free (interface);
7805 interface = i;
7808 /* And issue a syntax error. */
7809 gfc_syntax_error (ST_MODULE_PROC);
7810 return MATCH_ERROR;
7814 /* Check a derived type that is being extended. */
7816 static gfc_symbol*
7817 check_extended_derived_type (char *name)
7819 gfc_symbol *extended;
7821 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7823 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7824 return NULL;
7827 extended = gfc_find_dt_in_generic (extended);
7829 /* F08:C428. */
7830 if (!extended)
7832 gfc_error ("Symbol %qs at %C has not been previously defined", name);
7833 return NULL;
7836 if (extended->attr.flavor != FL_DERIVED)
7838 gfc_error ("%qs in EXTENDS expression at %C is not a "
7839 "derived type", name);
7840 return NULL;
7843 if (extended->attr.is_bind_c)
7845 gfc_error ("%qs cannot be extended at %C because it "
7846 "is BIND(C)", extended->name);
7847 return NULL;
7850 if (extended->attr.sequence)
7852 gfc_error ("%qs cannot be extended at %C because it "
7853 "is a SEQUENCE type", extended->name);
7854 return NULL;
7857 return extended;
7861 /* Match the optional attribute specifiers for a type declaration.
7862 Return MATCH_ERROR if an error is encountered in one of the handled
7863 attributes (public, private, bind(c)), MATCH_NO if what's found is
7864 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7865 checking on attribute conflicts needs to be done. */
7867 match
7868 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7870 /* See if the derived type is marked as private. */
7871 if (gfc_match (" , private") == MATCH_YES)
7873 if (gfc_current_state () != COMP_MODULE)
7875 gfc_error ("Derived type at %C can only be PRIVATE in the "
7876 "specification part of a module");
7877 return MATCH_ERROR;
7880 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7881 return MATCH_ERROR;
7883 else if (gfc_match (" , public") == MATCH_YES)
7885 if (gfc_current_state () != COMP_MODULE)
7887 gfc_error ("Derived type at %C can only be PUBLIC in the "
7888 "specification part of a module");
7889 return MATCH_ERROR;
7892 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7893 return MATCH_ERROR;
7895 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7897 /* If the type is defined to be bind(c) it then needs to make
7898 sure that all fields are interoperable. This will
7899 need to be a semantic check on the finished derived type.
7900 See 15.2.3 (lines 9-12) of F2003 draft. */
7901 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7902 return MATCH_ERROR;
7904 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7906 else if (gfc_match (" , abstract") == MATCH_YES)
7908 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7909 return MATCH_ERROR;
7911 if (!gfc_add_abstract (attr, &gfc_current_locus))
7912 return MATCH_ERROR;
7914 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7916 if (!gfc_add_extension (attr, &gfc_current_locus))
7917 return MATCH_ERROR;
7919 else
7920 return MATCH_NO;
7922 /* If we get here, something matched. */
7923 return MATCH_YES;
7927 /* Match the beginning of a derived type declaration. If a type name
7928 was the result of a function, then it is possible to have a symbol
7929 already to be known as a derived type yet have no components. */
7931 match
7932 gfc_match_derived_decl (void)
7934 char name[GFC_MAX_SYMBOL_LEN + 1];
7935 char parent[GFC_MAX_SYMBOL_LEN + 1];
7936 symbol_attribute attr;
7937 gfc_symbol *sym, *gensym;
7938 gfc_symbol *extended;
7939 match m;
7940 match is_type_attr_spec = MATCH_NO;
7941 bool seen_attr = false;
7942 gfc_interface *intr = NULL, *head;
7944 if (gfc_current_state () == COMP_DERIVED)
7945 return MATCH_NO;
7947 name[0] = '\0';
7948 parent[0] = '\0';
7949 gfc_clear_attr (&attr);
7950 extended = NULL;
7954 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7955 if (is_type_attr_spec == MATCH_ERROR)
7956 return MATCH_ERROR;
7957 if (is_type_attr_spec == MATCH_YES)
7958 seen_attr = true;
7959 } while (is_type_attr_spec == MATCH_YES);
7961 /* Deal with derived type extensions. The extension attribute has
7962 been added to 'attr' but now the parent type must be found and
7963 checked. */
7964 if (parent[0])
7965 extended = check_extended_derived_type (parent);
7967 if (parent[0] && !extended)
7968 return MATCH_ERROR;
7970 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7972 gfc_error ("Expected :: in TYPE definition at %C");
7973 return MATCH_ERROR;
7976 m = gfc_match (" %n%t", name);
7977 if (m != MATCH_YES)
7978 return m;
7980 /* Make sure the name is not the name of an intrinsic type. */
7981 if (gfc_is_intrinsic_typename (name))
7983 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
7984 "type", name);
7985 return MATCH_ERROR;
7988 if (gfc_get_symbol (name, NULL, &gensym))
7989 return MATCH_ERROR;
7991 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7993 gfc_error ("Derived type name %qs at %C already has a basic type "
7994 "of %s", gensym->name, gfc_typename (&gensym->ts));
7995 return MATCH_ERROR;
7998 if (!gensym->attr.generic
7999 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
8000 return MATCH_ERROR;
8002 if (!gensym->attr.function
8003 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
8004 return MATCH_ERROR;
8006 sym = gfc_find_dt_in_generic (gensym);
8008 if (sym && (sym->components != NULL || sym->attr.zero_comp))
8010 gfc_error ("Derived type definition of %qs at %C has already been "
8011 "defined", sym->name);
8012 return MATCH_ERROR;
8015 if (!sym)
8017 /* Use upper case to save the actual derived-type symbol. */
8018 gfc_get_symbol (gfc_get_string ("%c%s",
8019 (char) TOUPPER ((unsigned char) gensym->name[0]),
8020 &gensym->name[1]), NULL, &sym);
8021 sym->name = gfc_get_string (gensym->name);
8022 head = gensym->generic;
8023 intr = gfc_get_interface ();
8024 intr->sym = sym;
8025 intr->where = gfc_current_locus;
8026 intr->sym->declared_at = gfc_current_locus;
8027 intr->next = head;
8028 gensym->generic = intr;
8029 gensym->attr.if_source = IFSRC_DECL;
8032 /* The symbol may already have the derived attribute without the
8033 components. The ways this can happen is via a function
8034 definition, an INTRINSIC statement or a subtype in another
8035 derived type that is a pointer. The first part of the AND clause
8036 is true if the symbol is not the return value of a function. */
8037 if (sym->attr.flavor != FL_DERIVED
8038 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
8039 return MATCH_ERROR;
8041 if (attr.access != ACCESS_UNKNOWN
8042 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
8043 return MATCH_ERROR;
8044 else if (sym->attr.access == ACCESS_UNKNOWN
8045 && gensym->attr.access != ACCESS_UNKNOWN
8046 && !gfc_add_access (&sym->attr, gensym->attr.access,
8047 sym->name, NULL))
8048 return MATCH_ERROR;
8050 if (sym->attr.access != ACCESS_UNKNOWN
8051 && gensym->attr.access == ACCESS_UNKNOWN)
8052 gensym->attr.access = sym->attr.access;
8054 /* See if the derived type was labeled as bind(c). */
8055 if (attr.is_bind_c != 0)
8056 sym->attr.is_bind_c = attr.is_bind_c;
8058 /* Construct the f2k_derived namespace if it is not yet there. */
8059 if (!sym->f2k_derived)
8060 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8062 if (extended && !sym->components)
8064 gfc_component *p;
8066 /* Add the extended derived type as the first component. */
8067 gfc_add_component (sym, parent, &p);
8068 extended->refs++;
8069 gfc_set_sym_referenced (extended);
8071 p->ts.type = BT_DERIVED;
8072 p->ts.u.derived = extended;
8073 p->initializer = gfc_default_initializer (&p->ts);
8075 /* Set extension level. */
8076 if (extended->attr.extension == 255)
8078 /* Since the extension field is 8 bit wide, we can only have
8079 up to 255 extension levels. */
8080 gfc_error ("Maximum extension level reached with type %qs at %L",
8081 extended->name, &extended->declared_at);
8082 return MATCH_ERROR;
8084 sym->attr.extension = extended->attr.extension + 1;
8086 /* Provide the links between the extended type and its extension. */
8087 if (!extended->f2k_derived)
8088 extended->f2k_derived = gfc_get_namespace (NULL, 0);
8091 if (!sym->hash_value)
8092 /* Set the hash for the compound name for this type. */
8093 sym->hash_value = gfc_hash_value (sym);
8095 /* Take over the ABSTRACT attribute. */
8096 sym->attr.abstract = attr.abstract;
8098 gfc_new_block = sym;
8100 return MATCH_YES;
8104 /* Cray Pointees can be declared as:
8105 pointer (ipt, a (n,m,...,*)) */
8107 match
8108 gfc_mod_pointee_as (gfc_array_spec *as)
8110 as->cray_pointee = true; /* This will be useful to know later. */
8111 if (as->type == AS_ASSUMED_SIZE)
8112 as->cp_was_assumed = true;
8113 else if (as->type == AS_ASSUMED_SHAPE)
8115 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8116 return MATCH_ERROR;
8118 return MATCH_YES;
8122 /* Match the enum definition statement, here we are trying to match
8123 the first line of enum definition statement.
8124 Returns MATCH_YES if match is found. */
8126 match
8127 gfc_match_enum (void)
8129 match m;
8131 m = gfc_match_eos ();
8132 if (m != MATCH_YES)
8133 return m;
8135 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
8136 return MATCH_ERROR;
8138 return MATCH_YES;
8142 /* Returns an initializer whose value is one higher than the value of the
8143 LAST_INITIALIZER argument. If the argument is NULL, the
8144 initializers value will be set to zero. The initializer's kind
8145 will be set to gfc_c_int_kind.
8147 If -fshort-enums is given, the appropriate kind will be selected
8148 later after all enumerators have been parsed. A warning is issued
8149 here if an initializer exceeds gfc_c_int_kind. */
8151 static gfc_expr *
8152 enum_initializer (gfc_expr *last_initializer, locus where)
8154 gfc_expr *result;
8155 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
8157 mpz_init (result->value.integer);
8159 if (last_initializer != NULL)
8161 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8162 result->where = last_initializer->where;
8164 if (gfc_check_integer_range (result->value.integer,
8165 gfc_c_int_kind) != ARITH_OK)
8167 gfc_error ("Enumerator exceeds the C integer type at %C");
8168 return NULL;
8171 else
8173 /* Control comes here, if it's the very first enumerator and no
8174 initializer has been given. It will be initialized to zero. */
8175 mpz_set_si (result->value.integer, 0);
8178 return result;
8182 /* Match a variable name with an optional initializer. When this
8183 subroutine is called, a variable is expected to be parsed next.
8184 Depending on what is happening at the moment, updates either the
8185 symbol table or the current interface. */
8187 static match
8188 enumerator_decl (void)
8190 char name[GFC_MAX_SYMBOL_LEN + 1];
8191 gfc_expr *initializer;
8192 gfc_array_spec *as = NULL;
8193 gfc_symbol *sym;
8194 locus var_locus;
8195 match m;
8196 bool t;
8197 locus old_locus;
8199 initializer = NULL;
8200 old_locus = gfc_current_locus;
8202 /* When we get here, we've just matched a list of attributes and
8203 maybe a type and a double colon. The next thing we expect to see
8204 is the name of the symbol. */
8205 m = gfc_match_name (name);
8206 if (m != MATCH_YES)
8207 goto cleanup;
8209 var_locus = gfc_current_locus;
8211 /* OK, we've successfully matched the declaration. Now put the
8212 symbol in the current namespace. If we fail to create the symbol,
8213 bail out. */
8214 if (!build_sym (name, NULL, false, &as, &var_locus))
8216 m = MATCH_ERROR;
8217 goto cleanup;
8220 /* The double colon must be present in order to have initializers.
8221 Otherwise the statement is ambiguous with an assignment statement. */
8222 if (colon_seen)
8224 if (gfc_match_char ('=') == MATCH_YES)
8226 m = gfc_match_init_expr (&initializer);
8227 if (m == MATCH_NO)
8229 gfc_error ("Expected an initialization expression at %C");
8230 m = MATCH_ERROR;
8233 if (m != MATCH_YES)
8234 goto cleanup;
8238 /* If we do not have an initializer, the initialization value of the
8239 previous enumerator (stored in last_initializer) is incremented
8240 by 1 and is used to initialize the current enumerator. */
8241 if (initializer == NULL)
8242 initializer = enum_initializer (last_initializer, old_locus);
8244 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8246 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8247 &var_locus);
8248 m = MATCH_ERROR;
8249 goto cleanup;
8252 /* Store this current initializer, for the next enumerator variable
8253 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8254 use last_initializer below. */
8255 last_initializer = initializer;
8256 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8258 /* Maintain enumerator history. */
8259 gfc_find_symbol (name, NULL, 0, &sym);
8260 create_enum_history (sym, last_initializer);
8262 return (t) ? MATCH_YES : MATCH_ERROR;
8264 cleanup:
8265 /* Free stuff up and return. */
8266 gfc_free_expr (initializer);
8268 return m;
8272 /* Match the enumerator definition statement. */
8274 match
8275 gfc_match_enumerator_def (void)
8277 match m;
8278 bool t;
8280 gfc_clear_ts (&current_ts);
8282 m = gfc_match (" enumerator");
8283 if (m != MATCH_YES)
8284 return m;
8286 m = gfc_match (" :: ");
8287 if (m == MATCH_ERROR)
8288 return m;
8290 colon_seen = (m == MATCH_YES);
8292 if (gfc_current_state () != COMP_ENUM)
8294 gfc_error ("ENUM definition statement expected before %C");
8295 gfc_free_enum_history ();
8296 return MATCH_ERROR;
8299 (&current_ts)->type = BT_INTEGER;
8300 (&current_ts)->kind = gfc_c_int_kind;
8302 gfc_clear_attr (&current_attr);
8303 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
8304 if (!t)
8306 m = MATCH_ERROR;
8307 goto cleanup;
8310 for (;;)
8312 m = enumerator_decl ();
8313 if (m == MATCH_ERROR)
8315 gfc_free_enum_history ();
8316 goto cleanup;
8318 if (m == MATCH_NO)
8319 break;
8321 if (gfc_match_eos () == MATCH_YES)
8322 goto cleanup;
8323 if (gfc_match_char (',') != MATCH_YES)
8324 break;
8327 if (gfc_current_state () == COMP_ENUM)
8329 gfc_free_enum_history ();
8330 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8331 m = MATCH_ERROR;
8334 cleanup:
8335 gfc_free_array_spec (current_as);
8336 current_as = NULL;
8337 return m;
8342 /* Match binding attributes. */
8344 static match
8345 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
8347 bool found_passing = false;
8348 bool seen_ptr = false;
8349 match m = MATCH_YES;
8351 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8352 this case the defaults are in there. */
8353 ba->access = ACCESS_UNKNOWN;
8354 ba->pass_arg = NULL;
8355 ba->pass_arg_num = 0;
8356 ba->nopass = 0;
8357 ba->non_overridable = 0;
8358 ba->deferred = 0;
8359 ba->ppc = ppc;
8361 /* If we find a comma, we believe there are binding attributes. */
8362 m = gfc_match_char (',');
8363 if (m == MATCH_NO)
8364 goto done;
8368 /* Access specifier. */
8370 m = gfc_match (" public");
8371 if (m == MATCH_ERROR)
8372 goto error;
8373 if (m == MATCH_YES)
8375 if (ba->access != ACCESS_UNKNOWN)
8377 gfc_error ("Duplicate access-specifier at %C");
8378 goto error;
8381 ba->access = ACCESS_PUBLIC;
8382 continue;
8385 m = gfc_match (" private");
8386 if (m == MATCH_ERROR)
8387 goto error;
8388 if (m == MATCH_YES)
8390 if (ba->access != ACCESS_UNKNOWN)
8392 gfc_error ("Duplicate access-specifier at %C");
8393 goto error;
8396 ba->access = ACCESS_PRIVATE;
8397 continue;
8400 /* If inside GENERIC, the following is not allowed. */
8401 if (!generic)
8404 /* NOPASS flag. */
8405 m = gfc_match (" nopass");
8406 if (m == MATCH_ERROR)
8407 goto error;
8408 if (m == MATCH_YES)
8410 if (found_passing)
8412 gfc_error ("Binding attributes already specify passing,"
8413 " illegal NOPASS at %C");
8414 goto error;
8417 found_passing = true;
8418 ba->nopass = 1;
8419 continue;
8422 /* PASS possibly including argument. */
8423 m = gfc_match (" pass");
8424 if (m == MATCH_ERROR)
8425 goto error;
8426 if (m == MATCH_YES)
8428 char arg[GFC_MAX_SYMBOL_LEN + 1];
8430 if (found_passing)
8432 gfc_error ("Binding attributes already specify passing,"
8433 " illegal PASS at %C");
8434 goto error;
8437 m = gfc_match (" ( %n )", arg);
8438 if (m == MATCH_ERROR)
8439 goto error;
8440 if (m == MATCH_YES)
8441 ba->pass_arg = gfc_get_string (arg);
8442 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8444 found_passing = true;
8445 ba->nopass = 0;
8446 continue;
8449 if (ppc)
8451 /* POINTER flag. */
8452 m = gfc_match (" pointer");
8453 if (m == MATCH_ERROR)
8454 goto error;
8455 if (m == MATCH_YES)
8457 if (seen_ptr)
8459 gfc_error ("Duplicate POINTER attribute at %C");
8460 goto error;
8463 seen_ptr = true;
8464 continue;
8467 else
8469 /* NON_OVERRIDABLE flag. */
8470 m = gfc_match (" non_overridable");
8471 if (m == MATCH_ERROR)
8472 goto error;
8473 if (m == MATCH_YES)
8475 if (ba->non_overridable)
8477 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8478 goto error;
8481 ba->non_overridable = 1;
8482 continue;
8485 /* DEFERRED flag. */
8486 m = gfc_match (" deferred");
8487 if (m == MATCH_ERROR)
8488 goto error;
8489 if (m == MATCH_YES)
8491 if (ba->deferred)
8493 gfc_error ("Duplicate DEFERRED at %C");
8494 goto error;
8497 ba->deferred = 1;
8498 continue;
8504 /* Nothing matching found. */
8505 if (generic)
8506 gfc_error ("Expected access-specifier at %C");
8507 else
8508 gfc_error ("Expected binding attribute at %C");
8509 goto error;
8511 while (gfc_match_char (',') == MATCH_YES);
8513 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8514 if (ba->non_overridable && ba->deferred)
8516 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8517 goto error;
8520 m = MATCH_YES;
8522 done:
8523 if (ba->access == ACCESS_UNKNOWN)
8524 ba->access = gfc_typebound_default_access;
8526 if (ppc && !seen_ptr)
8528 gfc_error ("POINTER attribute is required for procedure pointer component"
8529 " at %C");
8530 goto error;
8533 return m;
8535 error:
8536 return MATCH_ERROR;
8540 /* Match a PROCEDURE specific binding inside a derived type. */
8542 static match
8543 match_procedure_in_type (void)
8545 char name[GFC_MAX_SYMBOL_LEN + 1];
8546 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8547 char* target = NULL, *ifc = NULL;
8548 gfc_typebound_proc tb;
8549 bool seen_colons;
8550 bool seen_attrs;
8551 match m;
8552 gfc_symtree* stree;
8553 gfc_namespace* ns;
8554 gfc_symbol* block;
8555 int num;
8557 /* Check current state. */
8558 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8559 block = gfc_state_stack->previous->sym;
8560 gcc_assert (block);
8562 /* Try to match PROCEDURE(interface). */
8563 if (gfc_match (" (") == MATCH_YES)
8565 m = gfc_match_name (target_buf);
8566 if (m == MATCH_ERROR)
8567 return m;
8568 if (m != MATCH_YES)
8570 gfc_error ("Interface-name expected after %<(%> at %C");
8571 return MATCH_ERROR;
8574 if (gfc_match (" )") != MATCH_YES)
8576 gfc_error ("%<)%> expected at %C");
8577 return MATCH_ERROR;
8580 ifc = target_buf;
8583 /* Construct the data structure. */
8584 memset (&tb, 0, sizeof (tb));
8585 tb.where = gfc_current_locus;
8587 /* Match binding attributes. */
8588 m = match_binding_attributes (&tb, false, false);
8589 if (m == MATCH_ERROR)
8590 return m;
8591 seen_attrs = (m == MATCH_YES);
8593 /* Check that attribute DEFERRED is given if an interface is specified. */
8594 if (tb.deferred && !ifc)
8596 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8597 return MATCH_ERROR;
8599 if (ifc && !tb.deferred)
8601 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8602 return MATCH_ERROR;
8605 /* Match the colons. */
8606 m = gfc_match (" ::");
8607 if (m == MATCH_ERROR)
8608 return m;
8609 seen_colons = (m == MATCH_YES);
8610 if (seen_attrs && !seen_colons)
8612 gfc_error ("Expected %<::%> after binding-attributes at %C");
8613 return MATCH_ERROR;
8616 /* Match the binding names. */
8617 for(num=1;;num++)
8619 m = gfc_match_name (name);
8620 if (m == MATCH_ERROR)
8621 return m;
8622 if (m == MATCH_NO)
8624 gfc_error ("Expected binding name at %C");
8625 return MATCH_ERROR;
8628 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8629 return MATCH_ERROR;
8631 /* Try to match the '=> target', if it's there. */
8632 target = ifc;
8633 m = gfc_match (" =>");
8634 if (m == MATCH_ERROR)
8635 return m;
8636 if (m == MATCH_YES)
8638 if (tb.deferred)
8640 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8641 return MATCH_ERROR;
8644 if (!seen_colons)
8646 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8647 " at %C");
8648 return MATCH_ERROR;
8651 m = gfc_match_name (target_buf);
8652 if (m == MATCH_ERROR)
8653 return m;
8654 if (m == MATCH_NO)
8656 gfc_error ("Expected binding target after %<=>%> at %C");
8657 return MATCH_ERROR;
8659 target = target_buf;
8662 /* If no target was found, it has the same name as the binding. */
8663 if (!target)
8664 target = name;
8666 /* Get the namespace to insert the symbols into. */
8667 ns = block->f2k_derived;
8668 gcc_assert (ns);
8670 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8671 if (tb.deferred && !block->attr.abstract)
8673 gfc_error ("Type %qs containing DEFERRED binding at %C "
8674 "is not ABSTRACT", block->name);
8675 return MATCH_ERROR;
8678 /* See if we already have a binding with this name in the symtree which
8679 would be an error. If a GENERIC already targeted this binding, it may
8680 be already there but then typebound is still NULL. */
8681 stree = gfc_find_symtree (ns->tb_sym_root, name);
8682 if (stree && stree->n.tb)
8684 gfc_error ("There is already a procedure with binding name %qs for "
8685 "the derived type %qs at %C", name, block->name);
8686 return MATCH_ERROR;
8689 /* Insert it and set attributes. */
8691 if (!stree)
8693 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8694 gcc_assert (stree);
8696 stree->n.tb = gfc_get_typebound_proc (&tb);
8698 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8699 false))
8700 return MATCH_ERROR;
8701 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8703 if (gfc_match_eos () == MATCH_YES)
8704 return MATCH_YES;
8705 if (gfc_match_char (',') != MATCH_YES)
8706 goto syntax;
8709 syntax:
8710 gfc_error ("Syntax error in PROCEDURE statement at %C");
8711 return MATCH_ERROR;
8715 /* Match a GENERIC procedure binding inside a derived type. */
8717 match
8718 gfc_match_generic (void)
8720 char name[GFC_MAX_SYMBOL_LEN + 1];
8721 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8722 gfc_symbol* block;
8723 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8724 gfc_typebound_proc* tb;
8725 gfc_namespace* ns;
8726 interface_type op_type;
8727 gfc_intrinsic_op op;
8728 match m;
8730 /* Check current state. */
8731 if (gfc_current_state () == COMP_DERIVED)
8733 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8734 return MATCH_ERROR;
8736 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8737 return MATCH_NO;
8738 block = gfc_state_stack->previous->sym;
8739 ns = block->f2k_derived;
8740 gcc_assert (block && ns);
8742 memset (&tbattr, 0, sizeof (tbattr));
8743 tbattr.where = gfc_current_locus;
8745 /* See if we get an access-specifier. */
8746 m = match_binding_attributes (&tbattr, true, false);
8747 if (m == MATCH_ERROR)
8748 goto error;
8750 /* Now the colons, those are required. */
8751 if (gfc_match (" ::") != MATCH_YES)
8753 gfc_error ("Expected %<::%> at %C");
8754 goto error;
8757 /* Match the binding name; depending on type (operator / generic) format
8758 it for future error messages into bind_name. */
8760 m = gfc_match_generic_spec (&op_type, name, &op);
8761 if (m == MATCH_ERROR)
8762 return MATCH_ERROR;
8763 if (m == MATCH_NO)
8765 gfc_error ("Expected generic name or operator descriptor at %C");
8766 goto error;
8769 switch (op_type)
8771 case INTERFACE_GENERIC:
8772 snprintf (bind_name, sizeof (bind_name), "%s", name);
8773 break;
8775 case INTERFACE_USER_OP:
8776 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8777 break;
8779 case INTERFACE_INTRINSIC_OP:
8780 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8781 gfc_op2string (op));
8782 break;
8784 case INTERFACE_NAMELESS:
8785 gfc_error ("Malformed GENERIC statement at %C");
8786 goto error;
8787 break;
8789 default:
8790 gcc_unreachable ();
8793 /* Match the required =>. */
8794 if (gfc_match (" =>") != MATCH_YES)
8796 gfc_error ("Expected %<=>%> at %C");
8797 goto error;
8800 /* Try to find existing GENERIC binding with this name / for this operator;
8801 if there is something, check that it is another GENERIC and then extend
8802 it rather than building a new node. Otherwise, create it and put it
8803 at the right position. */
8805 switch (op_type)
8807 case INTERFACE_USER_OP:
8808 case INTERFACE_GENERIC:
8810 const bool is_op = (op_type == INTERFACE_USER_OP);
8811 gfc_symtree* st;
8813 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8814 if (st)
8816 tb = st->n.tb;
8817 gcc_assert (tb);
8819 else
8820 tb = NULL;
8822 break;
8825 case INTERFACE_INTRINSIC_OP:
8826 tb = ns->tb_op[op];
8827 break;
8829 default:
8830 gcc_unreachable ();
8833 if (tb)
8835 if (!tb->is_generic)
8837 gcc_assert (op_type == INTERFACE_GENERIC);
8838 gfc_error ("There's already a non-generic procedure with binding name"
8839 " %qs for the derived type %qs at %C",
8840 bind_name, block->name);
8841 goto error;
8844 if (tb->access != tbattr.access)
8846 gfc_error ("Binding at %C must have the same access as already"
8847 " defined binding %qs", bind_name);
8848 goto error;
8851 else
8853 tb = gfc_get_typebound_proc (NULL);
8854 tb->where = gfc_current_locus;
8855 tb->access = tbattr.access;
8856 tb->is_generic = 1;
8857 tb->u.generic = NULL;
8859 switch (op_type)
8861 case INTERFACE_GENERIC:
8862 case INTERFACE_USER_OP:
8864 const bool is_op = (op_type == INTERFACE_USER_OP);
8865 gfc_symtree* st;
8867 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8868 name);
8869 gcc_assert (st);
8870 st->n.tb = tb;
8872 break;
8875 case INTERFACE_INTRINSIC_OP:
8876 ns->tb_op[op] = tb;
8877 break;
8879 default:
8880 gcc_unreachable ();
8884 /* Now, match all following names as specific targets. */
8887 gfc_symtree* target_st;
8888 gfc_tbp_generic* target;
8890 m = gfc_match_name (name);
8891 if (m == MATCH_ERROR)
8892 goto error;
8893 if (m == MATCH_NO)
8895 gfc_error ("Expected specific binding name at %C");
8896 goto error;
8899 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8901 /* See if this is a duplicate specification. */
8902 for (target = tb->u.generic; target; target = target->next)
8903 if (target_st == target->specific_st)
8905 gfc_error ("%qs already defined as specific binding for the"
8906 " generic %qs at %C", name, bind_name);
8907 goto error;
8910 target = gfc_get_tbp_generic ();
8911 target->specific_st = target_st;
8912 target->specific = NULL;
8913 target->next = tb->u.generic;
8914 target->is_operator = ((op_type == INTERFACE_USER_OP)
8915 || (op_type == INTERFACE_INTRINSIC_OP));
8916 tb->u.generic = target;
8918 while (gfc_match (" ,") == MATCH_YES);
8920 /* Here should be the end. */
8921 if (gfc_match_eos () != MATCH_YES)
8923 gfc_error ("Junk after GENERIC binding at %C");
8924 goto error;
8927 return MATCH_YES;
8929 error:
8930 return MATCH_ERROR;
8934 /* Match a FINAL declaration inside a derived type. */
8936 match
8937 gfc_match_final_decl (void)
8939 char name[GFC_MAX_SYMBOL_LEN + 1];
8940 gfc_symbol* sym;
8941 match m;
8942 gfc_namespace* module_ns;
8943 bool first, last;
8944 gfc_symbol* block;
8946 if (gfc_current_form == FORM_FREE)
8948 char c = gfc_peek_ascii_char ();
8949 if (!gfc_is_whitespace (c) && c != ':')
8950 return MATCH_NO;
8953 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8955 if (gfc_current_form == FORM_FIXED)
8956 return MATCH_NO;
8958 gfc_error ("FINAL declaration at %C must be inside a derived type "
8959 "CONTAINS section");
8960 return MATCH_ERROR;
8963 block = gfc_state_stack->previous->sym;
8964 gcc_assert (block);
8966 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8967 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8969 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8970 " specification part of a MODULE");
8971 return MATCH_ERROR;
8974 module_ns = gfc_current_ns;
8975 gcc_assert (module_ns);
8976 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8978 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8979 if (gfc_match (" ::") == MATCH_ERROR)
8980 return MATCH_ERROR;
8982 /* Match the sequence of procedure names. */
8983 first = true;
8984 last = false;
8987 gfc_finalizer* f;
8989 if (first && gfc_match_eos () == MATCH_YES)
8991 gfc_error ("Empty FINAL at %C");
8992 return MATCH_ERROR;
8995 m = gfc_match_name (name);
8996 if (m == MATCH_NO)
8998 gfc_error ("Expected module procedure name at %C");
8999 return MATCH_ERROR;
9001 else if (m != MATCH_YES)
9002 return MATCH_ERROR;
9004 if (gfc_match_eos () == MATCH_YES)
9005 last = true;
9006 if (!last && gfc_match_char (',') != MATCH_YES)
9008 gfc_error ("Expected %<,%> at %C");
9009 return MATCH_ERROR;
9012 if (gfc_get_symbol (name, module_ns, &sym))
9014 gfc_error ("Unknown procedure name %qs at %C", name);
9015 return MATCH_ERROR;
9018 /* Mark the symbol as module procedure. */
9019 if (sym->attr.proc != PROC_MODULE
9020 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9021 return MATCH_ERROR;
9023 /* Check if we already have this symbol in the list, this is an error. */
9024 for (f = block->f2k_derived->finalizers; f; f = f->next)
9025 if (f->proc_sym == sym)
9027 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9028 name);
9029 return MATCH_ERROR;
9032 /* Add this symbol to the list of finalizers. */
9033 gcc_assert (block->f2k_derived);
9034 ++sym->refs;
9035 f = XCNEW (gfc_finalizer);
9036 f->proc_sym = sym;
9037 f->proc_tree = NULL;
9038 f->where = gfc_current_locus;
9039 f->next = block->f2k_derived->finalizers;
9040 block->f2k_derived->finalizers = f;
9042 first = false;
9044 while (!last);
9046 return MATCH_YES;
9050 const ext_attr_t ext_attr_list[] = {
9051 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9052 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9053 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9054 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9055 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9056 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9057 { NULL, EXT_ATTR_LAST, NULL }
9060 /* Match a !GCC$ ATTRIBUTES statement of the form:
9061 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9062 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9064 TODO: We should support all GCC attributes using the same syntax for
9065 the attribute list, i.e. the list in C
9066 __attributes(( attribute-list ))
9067 matches then
9068 !GCC$ ATTRIBUTES attribute-list ::
9069 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9070 saved into a TREE.
9072 As there is absolutely no risk of confusion, we should never return
9073 MATCH_NO. */
9074 match
9075 gfc_match_gcc_attributes (void)
9077 symbol_attribute attr;
9078 char name[GFC_MAX_SYMBOL_LEN + 1];
9079 unsigned id;
9080 gfc_symbol *sym;
9081 match m;
9083 gfc_clear_attr (&attr);
9084 for(;;)
9086 char ch;
9088 if (gfc_match_name (name) != MATCH_YES)
9089 return MATCH_ERROR;
9091 for (id = 0; id < EXT_ATTR_LAST; id++)
9092 if (strcmp (name, ext_attr_list[id].name) == 0)
9093 break;
9095 if (id == EXT_ATTR_LAST)
9097 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9098 return MATCH_ERROR;
9101 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
9102 return MATCH_ERROR;
9104 gfc_gobble_whitespace ();
9105 ch = gfc_next_ascii_char ();
9106 if (ch == ':')
9108 /* This is the successful exit condition for the loop. */
9109 if (gfc_next_ascii_char () == ':')
9110 break;
9113 if (ch == ',')
9114 continue;
9116 goto syntax;
9119 if (gfc_match_eos () == MATCH_YES)
9120 goto syntax;
9122 for(;;)
9124 m = gfc_match_name (name);
9125 if (m != MATCH_YES)
9126 return m;
9128 if (find_special (name, &sym, true))
9129 return MATCH_ERROR;
9131 sym->attr.ext_attr |= attr.ext_attr;
9133 if (gfc_match_eos () == MATCH_YES)
9134 break;
9136 if (gfc_match_char (',') != MATCH_YES)
9137 goto syntax;
9140 return MATCH_YES;
9142 syntax:
9143 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9144 return MATCH_ERROR;