re PR fortran/67987 (ICE on declaring and initializing character with negative len)
[official-gcc.git] / gcc / fortran / decl.c
blob4871b7c364e10a0301580383c6ce3d2049a437cf
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)->value.function.actual
719 && (*expr)->value.function.actual->expr->symtree)
721 gfc_expr *e;
722 e = (*expr)->value.function.actual->expr;
723 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
724 && e->expr_type == EXPR_VARIABLE)
726 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
727 goto syntax;
728 if (e->symtree->n.sym->ts.type == BT_CHARACTER
729 && e->symtree->n.sym->ts.u.cl
730 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
731 goto syntax;
736 /* F2008, 4.4.3.1: The length is a type parameter; its kind is processor
737 dependent and its value is greater than or equal to zero.
738 F2008, 4.4.3.2: If the character length parameter value evaluates to
739 a negative value, the length of character entities declared is zero. */
740 if ((*expr)->expr_type == EXPR_CONSTANT
741 && mpz_cmp_si ((*expr)->value.integer, 0) < 0)
742 mpz_set_si ((*expr)->value.integer, 0);
744 return m;
746 syntax:
747 gfc_error ("Conflict in attributes of function argument at %C");
748 return MATCH_ERROR;
752 /* A character length is a '*' followed by a literal integer or a
753 char_len_param_value in parenthesis. */
755 static match
756 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
758 int length;
759 match m;
761 *deferred = false;
762 m = gfc_match_char ('*');
763 if (m != MATCH_YES)
764 return m;
766 m = gfc_match_small_literal_int (&length, NULL);
767 if (m == MATCH_ERROR)
768 return m;
770 if (m == MATCH_YES)
772 if (obsolescent_check
773 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
774 return MATCH_ERROR;
775 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
776 return m;
779 if (gfc_match_char ('(') == MATCH_NO)
780 goto syntax;
782 m = char_len_param_value (expr, deferred);
783 if (m != MATCH_YES && gfc_matching_function)
785 gfc_undo_symbols ();
786 m = MATCH_YES;
789 if (m == MATCH_ERROR)
790 return m;
791 if (m == MATCH_NO)
792 goto syntax;
794 if (gfc_match_char (')') == MATCH_NO)
796 gfc_free_expr (*expr);
797 *expr = NULL;
798 goto syntax;
801 return MATCH_YES;
803 syntax:
804 gfc_error ("Syntax error in character length specification at %C");
805 return MATCH_ERROR;
809 /* Special subroutine for finding a symbol. Check if the name is found
810 in the current name space. If not, and we're compiling a function or
811 subroutine and the parent compilation unit is an interface, then check
812 to see if the name we've been given is the name of the interface
813 (located in another namespace). */
815 static int
816 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
818 gfc_state_data *s;
819 gfc_symtree *st;
820 int i;
822 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
823 if (i == 0)
825 *result = st ? st->n.sym : NULL;
826 goto end;
829 if (gfc_current_state () != COMP_SUBROUTINE
830 && gfc_current_state () != COMP_FUNCTION)
831 goto end;
833 s = gfc_state_stack->previous;
834 if (s == NULL)
835 goto end;
837 if (s->state != COMP_INTERFACE)
838 goto end;
839 if (s->sym == NULL)
840 goto end; /* Nameless interface. */
842 if (strcmp (name, s->sym->name) == 0)
844 *result = s->sym;
845 return 0;
848 end:
849 return i;
853 /* Special subroutine for getting a symbol node associated with a
854 procedure name, used in SUBROUTINE and FUNCTION statements. The
855 symbol is created in the parent using with symtree node in the
856 child unit pointing to the symbol. If the current namespace has no
857 parent, then the symbol is just created in the current unit. */
859 static int
860 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
862 gfc_symtree *st;
863 gfc_symbol *sym;
864 int rc = 0;
866 /* Module functions have to be left in their own namespace because
867 they have potentially (almost certainly!) already been referenced.
868 In this sense, they are rather like external functions. This is
869 fixed up in resolve.c(resolve_entries), where the symbol name-
870 space is set to point to the master function, so that the fake
871 result mechanism can work. */
872 if (module_fcn_entry)
874 /* Present if entry is declared to be a module procedure. */
875 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
877 if (*result == NULL)
878 rc = gfc_get_symbol (name, NULL, result);
879 else if (!gfc_get_symbol (name, NULL, &sym) && sym
880 && (*result)->ts.type == BT_UNKNOWN
881 && sym->attr.flavor == FL_UNKNOWN)
882 /* Pick up the typespec for the entry, if declared in the function
883 body. Note that this symbol is FL_UNKNOWN because it will
884 only have appeared in a type declaration. The local symtree
885 is set to point to the module symbol and a unique symtree
886 to the local version. This latter ensures a correct clearing
887 of the symbols. */
889 /* If the ENTRY proceeds its specification, we need to ensure
890 that this does not raise a "has no IMPLICIT type" error. */
891 if (sym->ts.type == BT_UNKNOWN)
892 sym->attr.untyped = 1;
894 (*result)->ts = sym->ts;
896 /* Put the symbol in the procedure namespace so that, should
897 the ENTRY precede its specification, the specification
898 can be applied. */
899 (*result)->ns = gfc_current_ns;
901 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
902 st->n.sym = *result;
903 st = gfc_get_unique_symtree (gfc_current_ns);
904 st->n.sym = sym;
907 else
908 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
910 if (rc)
911 return rc;
913 sym = *result;
914 if (sym->attr.proc == PROC_ST_FUNCTION)
915 return rc;
917 if (sym->attr.module_procedure
918 && sym->attr.if_source == IFSRC_IFBODY)
920 /* Create a partially populated interface symbol to carry the
921 characteristics of the procedure and the result. */
922 sym->ts.interface = gfc_new_symbol (name, sym->ns);
923 gfc_add_type (sym->ts.interface, &(sym->ts),
924 &gfc_current_locus);
925 gfc_copy_attr (&sym->ts.interface->attr, &sym->attr, NULL);
926 if (sym->attr.dimension)
927 sym->ts.interface->as = gfc_copy_array_spec (sym->as);
929 /* Ideally, at this point, a copy would be made of the formal
930 arguments and their namespace. However, this does not appear
931 to be necessary, albeit at the expense of not being able to
932 use gfc_compare_interfaces directly. */
934 if (sym->result && sym->result != sym)
936 sym->ts.interface->result = sym->result;
937 sym->result = NULL;
939 else if (sym->result)
941 sym->ts.interface->result = sym->ts.interface;
944 else if (sym && !sym->gfc_new
945 && gfc_current_state () != COMP_INTERFACE)
947 /* Trap another encompassed procedure with the same name. All
948 these conditions are necessary to avoid picking up an entry
949 whose name clashes with that of the encompassing procedure;
950 this is handled using gsymbols to register unique,globally
951 accessible names. */
952 if (sym->attr.flavor != 0
953 && sym->attr.proc != 0
954 && (sym->attr.subroutine || sym->attr.function)
955 && sym->attr.if_source != IFSRC_UNKNOWN)
956 gfc_error_now ("Procedure %qs at %C is already defined at %L",
957 name, &sym->declared_at);
959 /* Trap a procedure with a name the same as interface in the
960 encompassing scope. */
961 if (sym->attr.generic != 0
962 && (sym->attr.subroutine || sym->attr.function)
963 && !sym->attr.mod_proc)
964 gfc_error_now ("Name %qs at %C is already defined"
965 " as a generic interface at %L",
966 name, &sym->declared_at);
968 /* Trap declarations of attributes in encompassing scope. The
969 signature for this is that ts.kind is set. Legitimate
970 references only set ts.type. */
971 if (sym->ts.kind != 0
972 && !sym->attr.implicit_type
973 && sym->attr.proc == 0
974 && gfc_current_ns->parent != NULL
975 && sym->attr.access == 0
976 && !module_fcn_entry)
977 gfc_error_now ("Procedure %qs at %C has an explicit interface "
978 "and must not have attributes declared at %L",
979 name, &sym->declared_at);
982 if (gfc_current_ns->parent == NULL || *result == NULL)
983 return rc;
985 /* Module function entries will already have a symtree in
986 the current namespace but will need one at module level. */
987 if (module_fcn_entry)
989 /* Present if entry is declared to be a module procedure. */
990 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
991 if (st == NULL)
992 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
994 else
995 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
997 st->n.sym = sym;
998 sym->refs++;
1000 /* See if the procedure should be a module procedure. */
1002 if (((sym->ns->proc_name != NULL
1003 && sym->ns->proc_name->attr.flavor == FL_MODULE
1004 && sym->attr.proc != PROC_MODULE)
1005 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1006 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1007 rc = 2;
1009 return rc;
1013 /* Verify that the given symbol representing a parameter is C
1014 interoperable, by checking to see if it was marked as such after
1015 its declaration. If the given symbol is not interoperable, a
1016 warning is reported, thus removing the need to return the status to
1017 the calling function. The standard does not require the user use
1018 one of the iso_c_binding named constants to declare an
1019 interoperable parameter, but we can't be sure if the param is C
1020 interop or not if the user doesn't. For example, integer(4) may be
1021 legal Fortran, but doesn't have meaning in C. It may interop with
1022 a number of the C types, which causes a problem because the
1023 compiler can't know which one. This code is almost certainly not
1024 portable, and the user will get what they deserve if the C type
1025 across platforms isn't always interoperable with integer(4). If
1026 the user had used something like integer(c_int) or integer(c_long),
1027 the compiler could have automatically handled the varying sizes
1028 across platforms. */
1030 bool
1031 gfc_verify_c_interop_param (gfc_symbol *sym)
1033 int is_c_interop = 0;
1034 bool retval = true;
1036 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1037 Don't repeat the checks here. */
1038 if (sym->attr.implicit_type)
1039 return true;
1041 /* For subroutines or functions that are passed to a BIND(C) procedure,
1042 they're interoperable if they're BIND(C) and their params are all
1043 interoperable. */
1044 if (sym->attr.flavor == FL_PROCEDURE)
1046 if (sym->attr.is_bind_c == 0)
1048 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1049 "attribute to be C interoperable", sym->name,
1050 &(sym->declared_at));
1051 return false;
1053 else
1055 if (sym->attr.is_c_interop == 1)
1056 /* We've already checked this procedure; don't check it again. */
1057 return true;
1058 else
1059 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1060 sym->common_block);
1064 /* See if we've stored a reference to a procedure that owns sym. */
1065 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1067 if (sym->ns->proc_name->attr.is_bind_c == 1)
1069 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1071 if (is_c_interop != 1)
1073 /* Make personalized messages to give better feedback. */
1074 if (sym->ts.type == BT_DERIVED)
1075 gfc_error ("Variable %qs at %L is a dummy argument to the "
1076 "BIND(C) procedure %qs but is not C interoperable "
1077 "because derived type %qs is not C interoperable",
1078 sym->name, &(sym->declared_at),
1079 sym->ns->proc_name->name,
1080 sym->ts.u.derived->name);
1081 else if (sym->ts.type == BT_CLASS)
1082 gfc_error ("Variable %qs at %L is a dummy argument to the "
1083 "BIND(C) procedure %qs but is not C interoperable "
1084 "because it is polymorphic",
1085 sym->name, &(sym->declared_at),
1086 sym->ns->proc_name->name);
1087 else if (warn_c_binding_type)
1088 gfc_warning (OPT_Wc_binding_type,
1089 "Variable %qs at %L is a dummy argument of the "
1090 "BIND(C) procedure %qs but may not be C "
1091 "interoperable",
1092 sym->name, &(sym->declared_at),
1093 sym->ns->proc_name->name);
1096 /* Character strings are only C interoperable if they have a
1097 length of 1. */
1098 if (sym->ts.type == BT_CHARACTER)
1100 gfc_charlen *cl = sym->ts.u.cl;
1101 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1102 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1104 gfc_error ("Character argument %qs at %L "
1105 "must be length 1 because "
1106 "procedure %qs is BIND(C)",
1107 sym->name, &sym->declared_at,
1108 sym->ns->proc_name->name);
1109 retval = false;
1113 /* We have to make sure that any param to a bind(c) routine does
1114 not have the allocatable, pointer, or optional attributes,
1115 according to J3/04-007, section 5.1. */
1116 if (sym->attr.allocatable == 1
1117 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1118 "ALLOCATABLE attribute in procedure %qs "
1119 "with BIND(C)", sym->name,
1120 &(sym->declared_at),
1121 sym->ns->proc_name->name))
1122 retval = false;
1124 if (sym->attr.pointer == 1
1125 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1126 "POINTER attribute in procedure %qs "
1127 "with BIND(C)", sym->name,
1128 &(sym->declared_at),
1129 sym->ns->proc_name->name))
1130 retval = false;
1132 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1134 gfc_error ("Scalar variable %qs at %L with POINTER or "
1135 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1136 " supported", sym->name, &(sym->declared_at),
1137 sym->ns->proc_name->name);
1138 retval = false;
1141 if (sym->attr.optional == 1 && sym->attr.value)
1143 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1144 "and the VALUE attribute because procedure %qs "
1145 "is BIND(C)", sym->name, &(sym->declared_at),
1146 sym->ns->proc_name->name);
1147 retval = false;
1149 else if (sym->attr.optional == 1
1150 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1151 "at %L with OPTIONAL attribute in "
1152 "procedure %qs which is BIND(C)",
1153 sym->name, &(sym->declared_at),
1154 sym->ns->proc_name->name))
1155 retval = false;
1157 /* Make sure that if it has the dimension attribute, that it is
1158 either assumed size or explicit shape. Deferred shape is already
1159 covered by the pointer/allocatable attribute. */
1160 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1161 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1162 "at %L as dummy argument to the BIND(C) "
1163 "procedure '%s' at %L", sym->name,
1164 &(sym->declared_at),
1165 sym->ns->proc_name->name,
1166 &(sym->ns->proc_name->declared_at)))
1167 retval = false;
1171 return retval;
1176 /* Function called by variable_decl() that adds a name to the symbol table. */
1178 static bool
1179 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1180 gfc_array_spec **as, locus *var_locus)
1182 symbol_attribute attr;
1183 gfc_symbol *sym;
1185 if (gfc_get_symbol (name, NULL, &sym))
1186 return false;
1188 /* Start updating the symbol table. Add basic type attribute if present. */
1189 if (current_ts.type != BT_UNKNOWN
1190 && (sym->attr.implicit_type == 0
1191 || !gfc_compare_types (&sym->ts, &current_ts))
1192 && !gfc_add_type (sym, &current_ts, var_locus))
1193 return false;
1195 if (sym->ts.type == BT_CHARACTER)
1197 sym->ts.u.cl = cl;
1198 sym->ts.deferred = cl_deferred;
1201 /* Add dimension attribute if present. */
1202 if (!gfc_set_array_spec (sym, *as, var_locus))
1203 return false;
1204 *as = NULL;
1206 /* Add attribute to symbol. The copy is so that we can reset the
1207 dimension attribute. */
1208 attr = current_attr;
1209 attr.dimension = 0;
1210 attr.codimension = 0;
1212 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1213 return false;
1215 /* Finish any work that may need to be done for the binding label,
1216 if it's a bind(c). The bind(c) attr is found before the symbol
1217 is made, and before the symbol name (for data decls), so the
1218 current_ts is holding the binding label, or nothing if the
1219 name= attr wasn't given. Therefore, test here if we're dealing
1220 with a bind(c) and make sure the binding label is set correctly. */
1221 if (sym->attr.is_bind_c == 1)
1223 if (!sym->binding_label)
1225 /* Set the binding label and verify that if a NAME= was specified
1226 then only one identifier was in the entity-decl-list. */
1227 if (!set_binding_label (&sym->binding_label, sym->name,
1228 num_idents_on_line))
1229 return false;
1233 /* See if we know we're in a common block, and if it's a bind(c)
1234 common then we need to make sure we're an interoperable type. */
1235 if (sym->attr.in_common == 1)
1237 /* Test the common block object. */
1238 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1239 && sym->ts.is_c_interop != 1)
1241 gfc_error_now ("Variable %qs in common block %qs at %C "
1242 "must be declared with a C interoperable "
1243 "kind since common block %qs is BIND(C)",
1244 sym->name, sym->common_block->name,
1245 sym->common_block->name);
1246 gfc_clear_error ();
1250 sym->attr.implied_index = 0;
1252 if (sym->ts.type == BT_CLASS)
1253 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1255 return true;
1259 /* Set character constant to the given length. The constant will be padded or
1260 truncated. If we're inside an array constructor without a typespec, we
1261 additionally check that all elements have the same length; check_len -1
1262 means no checking. */
1264 void
1265 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1267 gfc_char_t *s;
1268 int slen;
1270 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1271 gcc_assert (expr->ts.type == BT_CHARACTER);
1273 slen = expr->value.character.length;
1274 if (len != slen)
1276 s = gfc_get_wide_string (len + 1);
1277 memcpy (s, expr->value.character.string,
1278 MIN (len, slen) * sizeof (gfc_char_t));
1279 if (len > slen)
1280 gfc_wide_memset (&s[slen], ' ', len - slen);
1282 if (warn_character_truncation && slen > len)
1283 gfc_warning_now (OPT_Wcharacter_truncation,
1284 "CHARACTER expression at %L is being truncated "
1285 "(%d/%d)", &expr->where, slen, len);
1287 /* Apply the standard by 'hand' otherwise it gets cleared for
1288 initializers. */
1289 if (check_len != -1 && slen != check_len
1290 && !(gfc_option.allow_std & GFC_STD_GNU))
1291 gfc_error_now ("The CHARACTER elements of the array constructor "
1292 "at %L must have the same length (%d/%d)",
1293 &expr->where, slen, check_len);
1295 s[len] = '\0';
1296 free (expr->value.character.string);
1297 expr->value.character.string = s;
1298 expr->value.character.length = len;
1303 /* Function to create and update the enumerator history
1304 using the information passed as arguments.
1305 Pointer "max_enum" is also updated, to point to
1306 enum history node containing largest initializer.
1308 SYM points to the symbol node of enumerator.
1309 INIT points to its enumerator value. */
1311 static void
1312 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1314 enumerator_history *new_enum_history;
1315 gcc_assert (sym != NULL && init != NULL);
1317 new_enum_history = XCNEW (enumerator_history);
1319 new_enum_history->sym = sym;
1320 new_enum_history->initializer = init;
1321 new_enum_history->next = NULL;
1323 if (enum_history == NULL)
1325 enum_history = new_enum_history;
1326 max_enum = enum_history;
1328 else
1330 new_enum_history->next = enum_history;
1331 enum_history = new_enum_history;
1333 if (mpz_cmp (max_enum->initializer->value.integer,
1334 new_enum_history->initializer->value.integer) < 0)
1335 max_enum = new_enum_history;
1340 /* Function to free enum kind history. */
1342 void
1343 gfc_free_enum_history (void)
1345 enumerator_history *current = enum_history;
1346 enumerator_history *next;
1348 while (current != NULL)
1350 next = current->next;
1351 free (current);
1352 current = next;
1354 max_enum = NULL;
1355 enum_history = NULL;
1359 /* Function called by variable_decl() that adds an initialization
1360 expression to a symbol. */
1362 static bool
1363 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1365 symbol_attribute attr;
1366 gfc_symbol *sym;
1367 gfc_expr *init;
1369 init = *initp;
1370 if (find_special (name, &sym, false))
1371 return false;
1373 attr = sym->attr;
1375 /* If this symbol is confirming an implicit parameter type,
1376 then an initialization expression is not allowed. */
1377 if (attr.flavor == FL_PARAMETER
1378 && sym->value != NULL
1379 && *initp != NULL)
1381 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1382 sym->name);
1383 return false;
1386 if (init == NULL)
1388 /* An initializer is required for PARAMETER declarations. */
1389 if (attr.flavor == FL_PARAMETER)
1391 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1392 return false;
1395 else
1397 /* If a variable appears in a DATA block, it cannot have an
1398 initializer. */
1399 if (sym->attr.data)
1401 gfc_error ("Variable %qs at %C with an initializer already "
1402 "appears in a DATA statement", sym->name);
1403 return false;
1406 /* Check if the assignment can happen. This has to be put off
1407 until later for derived type variables and procedure pointers. */
1408 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1409 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1410 && !sym->attr.proc_pointer
1411 && !gfc_check_assign_symbol (sym, NULL, init))
1412 return false;
1414 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1415 && init->ts.type == BT_CHARACTER)
1417 /* Update symbol character length according initializer. */
1418 if (!gfc_check_assign_symbol (sym, NULL, init))
1419 return false;
1421 if (sym->ts.u.cl->length == NULL)
1423 int clen;
1424 /* If there are multiple CHARACTER variables declared on the
1425 same line, we don't want them to share the same length. */
1426 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1428 if (sym->attr.flavor == FL_PARAMETER)
1430 if (init->expr_type == EXPR_CONSTANT)
1432 clen = init->value.character.length;
1433 sym->ts.u.cl->length
1434 = gfc_get_int_expr (gfc_default_integer_kind,
1435 NULL, clen);
1437 else if (init->expr_type == EXPR_ARRAY)
1439 clen = mpz_get_si (init->ts.u.cl->length->value.integer);
1440 sym->ts.u.cl->length
1441 = gfc_get_int_expr (gfc_default_integer_kind,
1442 NULL, clen);
1444 else if (init->ts.u.cl && init->ts.u.cl->length)
1445 sym->ts.u.cl->length =
1446 gfc_copy_expr (sym->value->ts.u.cl->length);
1449 /* Update initializer character length according symbol. */
1450 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1452 int len;
1454 if (!gfc_specification_expr (sym->ts.u.cl->length))
1455 return false;
1457 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1459 if (init->expr_type == EXPR_CONSTANT)
1460 gfc_set_constant_character_len (len, init, -1);
1461 else if (init->expr_type == EXPR_ARRAY)
1463 gfc_constructor *c;
1465 /* Build a new charlen to prevent simplification from
1466 deleting the length before it is resolved. */
1467 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1468 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1470 for (c = gfc_constructor_first (init->value.constructor);
1471 c; c = gfc_constructor_next (c))
1472 gfc_set_constant_character_len (len, c->expr, -1);
1477 /* If sym is implied-shape, set its upper bounds from init. */
1478 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1479 && sym->as->type == AS_IMPLIED_SHAPE)
1481 int dim;
1483 if (init->rank == 0)
1485 gfc_error ("Can't initialize implied-shape array at %L"
1486 " with scalar", &sym->declared_at);
1487 return false;
1489 gcc_assert (sym->as->rank == init->rank);
1491 /* Shape should be present, we get an initialization expression. */
1492 gcc_assert (init->shape);
1494 for (dim = 0; dim < sym->as->rank; ++dim)
1496 int k;
1497 gfc_expr* lower;
1498 gfc_expr* e;
1500 lower = sym->as->lower[dim];
1501 if (lower->expr_type != EXPR_CONSTANT)
1503 gfc_error ("Non-constant lower bound in implied-shape"
1504 " declaration at %L", &lower->where);
1505 return false;
1508 /* All dimensions must be without upper bound. */
1509 gcc_assert (!sym->as->upper[dim]);
1511 k = lower->ts.kind;
1512 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1513 mpz_add (e->value.integer,
1514 lower->value.integer, init->shape[dim]);
1515 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1516 sym->as->upper[dim] = e;
1519 sym->as->type = AS_EXPLICIT;
1522 /* Need to check if the expression we initialized this
1523 to was one of the iso_c_binding named constants. If so,
1524 and we're a parameter (constant), let it be iso_c.
1525 For example:
1526 integer(c_int), parameter :: my_int = c_int
1527 integer(my_int) :: my_int_2
1528 If we mark my_int as iso_c (since we can see it's value
1529 is equal to one of the named constants), then my_int_2
1530 will be considered C interoperable. */
1531 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1533 sym->ts.is_iso_c |= init->ts.is_iso_c;
1534 sym->ts.is_c_interop |= init->ts.is_c_interop;
1535 /* attr bits needed for module files. */
1536 sym->attr.is_iso_c |= init->ts.is_iso_c;
1537 sym->attr.is_c_interop |= init->ts.is_c_interop;
1538 if (init->ts.is_iso_c)
1539 sym->ts.f90_type = init->ts.f90_type;
1542 /* Add initializer. Make sure we keep the ranks sane. */
1543 if (sym->attr.dimension && init->rank == 0)
1545 mpz_t size;
1546 gfc_expr *array;
1547 int n;
1548 if (sym->attr.flavor == FL_PARAMETER
1549 && init->expr_type == EXPR_CONSTANT
1550 && spec_size (sym->as, &size)
1551 && mpz_cmp_si (size, 0) > 0)
1553 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1554 &init->where);
1555 for (n = 0; n < (int)mpz_get_si (size); n++)
1556 gfc_constructor_append_expr (&array->value.constructor,
1557 n == 0
1558 ? init
1559 : gfc_copy_expr (init),
1560 &init->where);
1562 array->shape = gfc_get_shape (sym->as->rank);
1563 for (n = 0; n < sym->as->rank; n++)
1564 spec_dimen_size (sym->as, n, &array->shape[n]);
1566 init = array;
1567 mpz_clear (size);
1569 init->rank = sym->as->rank;
1572 sym->value = init;
1573 if (sym->attr.save == SAVE_NONE)
1574 sym->attr.save = SAVE_IMPLICIT;
1575 *initp = NULL;
1578 return true;
1582 /* Function called by variable_decl() that adds a name to a structure
1583 being built. */
1585 static bool
1586 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1587 gfc_array_spec **as)
1589 gfc_component *c;
1590 bool t = true;
1592 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1593 constructing, it must have the pointer attribute. */
1594 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1595 && current_ts.u.derived == gfc_current_block ()
1596 && current_attr.pointer == 0)
1598 gfc_error ("Component at %C must have the POINTER attribute");
1599 return false;
1602 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1604 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1606 gfc_error ("Array component of structure at %C must have explicit "
1607 "or deferred shape");
1608 return false;
1612 if (!gfc_add_component (gfc_current_block(), name, &c))
1613 return false;
1615 c->ts = current_ts;
1616 if (c->ts.type == BT_CHARACTER)
1617 c->ts.u.cl = cl;
1618 c->attr = current_attr;
1620 c->initializer = *init;
1621 *init = NULL;
1623 c->as = *as;
1624 if (c->as != NULL)
1626 if (c->as->corank)
1627 c->attr.codimension = 1;
1628 if (c->as->rank)
1629 c->attr.dimension = 1;
1631 *as = NULL;
1633 /* Should this ever get more complicated, combine with similar section
1634 in add_init_expr_to_sym into a separate function. */
1635 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1636 && c->ts.u.cl
1637 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1639 int len;
1641 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1642 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1643 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1645 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1647 if (c->initializer->expr_type == EXPR_CONSTANT)
1648 gfc_set_constant_character_len (len, c->initializer, -1);
1649 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1650 c->initializer->ts.u.cl->length->value.integer))
1652 gfc_constructor *ctor;
1653 ctor = gfc_constructor_first (c->initializer->value.constructor);
1655 if (ctor)
1657 int first_len;
1658 bool has_ts = (c->initializer->ts.u.cl
1659 && c->initializer->ts.u.cl->length_from_typespec);
1661 /* Remember the length of the first element for checking
1662 that all elements *in the constructor* have the same
1663 length. This need not be the length of the LHS! */
1664 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1665 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1666 first_len = ctor->expr->value.character.length;
1668 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1669 if (ctor->expr->expr_type == EXPR_CONSTANT)
1671 gfc_set_constant_character_len (len, ctor->expr,
1672 has_ts ? -1 : first_len);
1673 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1679 /* Check array components. */
1680 if (!c->attr.dimension)
1681 goto scalar;
1683 if (c->attr.pointer)
1685 if (c->as->type != AS_DEFERRED)
1687 gfc_error ("Pointer array component of structure at %C must have a "
1688 "deferred shape");
1689 t = false;
1692 else if (c->attr.allocatable)
1694 if (c->as->type != AS_DEFERRED)
1696 gfc_error ("Allocatable component of structure at %C must have a "
1697 "deferred shape");
1698 t = false;
1701 else
1703 if (c->as->type != AS_EXPLICIT)
1705 gfc_error ("Array component of structure at %C must have an "
1706 "explicit shape");
1707 t = false;
1711 scalar:
1712 if (c->ts.type == BT_CLASS)
1714 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1716 if (t)
1717 t = t2;
1720 return t;
1724 /* Match a 'NULL()', and possibly take care of some side effects. */
1726 match
1727 gfc_match_null (gfc_expr **result)
1729 gfc_symbol *sym;
1730 match m, m2 = MATCH_NO;
1732 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1733 return MATCH_ERROR;
1735 if (m == MATCH_NO)
1737 locus old_loc;
1738 char name[GFC_MAX_SYMBOL_LEN + 1];
1740 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1741 return m2;
1743 old_loc = gfc_current_locus;
1744 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1745 return MATCH_ERROR;
1746 if (m2 != MATCH_YES
1747 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1748 return MATCH_ERROR;
1749 if (m2 == MATCH_NO)
1751 gfc_current_locus = old_loc;
1752 return MATCH_NO;
1756 /* The NULL symbol now has to be/become an intrinsic function. */
1757 if (gfc_get_symbol ("null", NULL, &sym))
1759 gfc_error ("NULL() initialization at %C is ambiguous");
1760 return MATCH_ERROR;
1763 gfc_intrinsic_symbol (sym);
1765 if (sym->attr.proc != PROC_INTRINSIC
1766 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1767 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1768 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1769 return MATCH_ERROR;
1771 *result = gfc_get_null_expr (&gfc_current_locus);
1773 /* Invalid per F2008, C512. */
1774 if (m2 == MATCH_YES)
1776 gfc_error ("NULL() initialization at %C may not have MOLD");
1777 return MATCH_ERROR;
1780 return MATCH_YES;
1784 /* Match the initialization expr for a data pointer or procedure pointer. */
1786 static match
1787 match_pointer_init (gfc_expr **init, int procptr)
1789 match m;
1791 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1793 gfc_error ("Initialization of pointer at %C is not allowed in "
1794 "a PURE procedure");
1795 return MATCH_ERROR;
1797 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1799 /* Match NULL() initialization. */
1800 m = gfc_match_null (init);
1801 if (m != MATCH_NO)
1802 return m;
1804 /* Match non-NULL initialization. */
1805 gfc_matching_ptr_assignment = !procptr;
1806 gfc_matching_procptr_assignment = procptr;
1807 m = gfc_match_rvalue (init);
1808 gfc_matching_ptr_assignment = 0;
1809 gfc_matching_procptr_assignment = 0;
1810 if (m == MATCH_ERROR)
1811 return MATCH_ERROR;
1812 else if (m == MATCH_NO)
1814 gfc_error ("Error in pointer initialization at %C");
1815 return MATCH_ERROR;
1818 if (!procptr && !gfc_resolve_expr (*init))
1819 return MATCH_ERROR;
1821 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1822 "initialization at %C"))
1823 return MATCH_ERROR;
1825 return MATCH_YES;
1829 static bool
1830 check_function_name (char *name)
1832 /* In functions that have a RESULT variable defined, the function name always
1833 refers to function calls. Therefore, the name is not allowed to appear in
1834 specification statements. When checking this, be careful about
1835 'hidden' procedure pointer results ('ppr@'). */
1837 if (gfc_current_state () == COMP_FUNCTION)
1839 gfc_symbol *block = gfc_current_block ();
1840 if (block && block->result && block->result != block
1841 && strcmp (block->result->name, "ppr@") != 0
1842 && strcmp (block->name, name) == 0)
1844 gfc_error ("Function name %qs not allowed at %C", name);
1845 return false;
1849 return true;
1853 /* Match a variable name with an optional initializer. When this
1854 subroutine is called, a variable is expected to be parsed next.
1855 Depending on what is happening at the moment, updates either the
1856 symbol table or the current interface. */
1858 static match
1859 variable_decl (int elem)
1861 char name[GFC_MAX_SYMBOL_LEN + 1];
1862 gfc_expr *initializer, *char_len;
1863 gfc_array_spec *as;
1864 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1865 gfc_charlen *cl;
1866 bool cl_deferred;
1867 locus var_locus;
1868 match m;
1869 bool t;
1870 gfc_symbol *sym;
1872 initializer = NULL;
1873 as = NULL;
1874 cp_as = NULL;
1876 /* When we get here, we've just matched a list of attributes and
1877 maybe a type and a double colon. The next thing we expect to see
1878 is the name of the symbol. */
1879 m = gfc_match_name (name);
1880 if (m != MATCH_YES)
1881 goto cleanup;
1883 var_locus = gfc_current_locus;
1885 /* Now we could see the optional array spec. or character length. */
1886 m = gfc_match_array_spec (&as, true, true);
1887 if (m == MATCH_ERROR)
1888 goto cleanup;
1890 if (m == MATCH_NO)
1891 as = gfc_copy_array_spec (current_as);
1892 else if (current_as
1893 && !merge_array_spec (current_as, as, true))
1895 m = MATCH_ERROR;
1896 goto cleanup;
1899 if (flag_cray_pointer)
1900 cp_as = gfc_copy_array_spec (as);
1902 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1903 determine (and check) whether it can be implied-shape. If it
1904 was parsed as assumed-size, change it because PARAMETERs can not
1905 be assumed-size. */
1906 if (as)
1908 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1910 m = MATCH_ERROR;
1911 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1912 name, &var_locus);
1913 goto cleanup;
1916 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1917 && current_attr.flavor == FL_PARAMETER)
1918 as->type = AS_IMPLIED_SHAPE;
1920 if (as->type == AS_IMPLIED_SHAPE
1921 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1922 &var_locus))
1924 m = MATCH_ERROR;
1925 goto cleanup;
1929 char_len = NULL;
1930 cl = NULL;
1931 cl_deferred = false;
1933 if (current_ts.type == BT_CHARACTER)
1935 switch (match_char_length (&char_len, &cl_deferred, false))
1937 case MATCH_YES:
1938 cl = gfc_new_charlen (gfc_current_ns, NULL);
1940 cl->length = char_len;
1941 break;
1943 /* Non-constant lengths need to be copied after the first
1944 element. Also copy assumed lengths. */
1945 case MATCH_NO:
1946 if (elem > 1
1947 && (current_ts.u.cl->length == NULL
1948 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1950 cl = gfc_new_charlen (gfc_current_ns, NULL);
1951 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1953 else
1954 cl = current_ts.u.cl;
1956 cl_deferred = current_ts.deferred;
1958 break;
1960 case MATCH_ERROR:
1961 goto cleanup;
1965 /* The dummy arguments and result of the abreviated form of MODULE
1966 PROCEDUREs, used in SUBMODULES should not be redefined. */
1967 if (gfc_current_ns->proc_name
1968 && gfc_current_ns->proc_name->abr_modproc_decl)
1970 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1971 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
1973 m = MATCH_ERROR;
1974 gfc_error ("'%s' at %C is a redefinition of the declaration "
1975 "in the corresponding interface for MODULE "
1976 "PROCEDURE '%s'", sym->name,
1977 gfc_current_ns->proc_name->name);
1978 goto cleanup;
1982 /* If this symbol has already shown up in a Cray Pointer declaration,
1983 and this is not a component declaration,
1984 then we want to set the type & bail out. */
1985 if (flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
1987 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1988 if (sym != NULL && sym->attr.cray_pointee)
1990 sym->ts.type = current_ts.type;
1991 sym->ts.kind = current_ts.kind;
1992 sym->ts.u.cl = cl;
1993 sym->ts.u.derived = current_ts.u.derived;
1994 sym->ts.is_c_interop = current_ts.is_c_interop;
1995 sym->ts.is_iso_c = current_ts.is_iso_c;
1996 m = MATCH_YES;
1998 /* Check to see if we have an array specification. */
1999 if (cp_as != NULL)
2001 if (sym->as != NULL)
2003 gfc_error ("Duplicate array spec for Cray pointee at %C");
2004 gfc_free_array_spec (cp_as);
2005 m = MATCH_ERROR;
2006 goto cleanup;
2008 else
2010 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2011 gfc_internal_error ("Couldn't set pointee array spec.");
2013 /* Fix the array spec. */
2014 m = gfc_mod_pointee_as (sym->as);
2015 if (m == MATCH_ERROR)
2016 goto cleanup;
2019 goto cleanup;
2021 else
2023 gfc_free_array_spec (cp_as);
2027 /* Procedure pointer as function result. */
2028 if (gfc_current_state () == COMP_FUNCTION
2029 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2030 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2031 strcpy (name, "ppr@");
2033 if (gfc_current_state () == COMP_FUNCTION
2034 && strcmp (name, gfc_current_block ()->name) == 0
2035 && gfc_current_block ()->result
2036 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2037 strcpy (name, "ppr@");
2039 /* OK, we've successfully matched the declaration. Now put the
2040 symbol in the current namespace, because it might be used in the
2041 optional initialization expression for this symbol, e.g. this is
2042 perfectly legal:
2044 integer, parameter :: i = huge(i)
2046 This is only true for parameters or variables of a basic type.
2047 For components of derived types, it is not true, so we don't
2048 create a symbol for those yet. If we fail to create the symbol,
2049 bail out. */
2050 if (gfc_current_state () != COMP_DERIVED
2051 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2053 m = MATCH_ERROR;
2054 goto cleanup;
2057 if (!check_function_name (name))
2059 m = MATCH_ERROR;
2060 goto cleanup;
2063 /* We allow old-style initializations of the form
2064 integer i /2/, j(4) /3*3, 1/
2065 (if no colon has been seen). These are different from data
2066 statements in that initializers are only allowed to apply to the
2067 variable immediately preceding, i.e.
2068 integer i, j /1, 2/
2069 is not allowed. Therefore we have to do some work manually, that
2070 could otherwise be left to the matchers for DATA statements. */
2072 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2074 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2075 "initialization at %C"))
2076 return MATCH_ERROR;
2077 else if (gfc_current_state () == COMP_DERIVED)
2079 gfc_error ("Invalid old style initialization for derived type "
2080 "component at %C");
2081 m = MATCH_ERROR;
2082 goto cleanup;
2085 return match_old_style_init (name);
2088 /* The double colon must be present in order to have initializers.
2089 Otherwise the statement is ambiguous with an assignment statement. */
2090 if (colon_seen)
2092 if (gfc_match (" =>") == MATCH_YES)
2094 if (!current_attr.pointer)
2096 gfc_error ("Initialization at %C isn't for a pointer variable");
2097 m = MATCH_ERROR;
2098 goto cleanup;
2101 m = match_pointer_init (&initializer, 0);
2102 if (m != MATCH_YES)
2103 goto cleanup;
2105 else if (gfc_match_char ('=') == MATCH_YES)
2107 if (current_attr.pointer)
2109 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2110 "not %<=%>");
2111 m = MATCH_ERROR;
2112 goto cleanup;
2115 m = gfc_match_init_expr (&initializer);
2116 if (m == MATCH_NO)
2118 gfc_error ("Expected an initialization expression at %C");
2119 m = MATCH_ERROR;
2122 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2123 && gfc_state_stack->state != COMP_DERIVED)
2125 gfc_error ("Initialization of variable at %C is not allowed in "
2126 "a PURE procedure");
2127 m = MATCH_ERROR;
2130 if (current_attr.flavor != FL_PARAMETER
2131 && gfc_state_stack->state != COMP_DERIVED)
2132 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2134 if (m != MATCH_YES)
2135 goto cleanup;
2139 if (initializer != NULL && current_attr.allocatable
2140 && gfc_current_state () == COMP_DERIVED)
2142 gfc_error ("Initialization of allocatable component at %C is not "
2143 "allowed");
2144 m = MATCH_ERROR;
2145 goto cleanup;
2148 /* Add the initializer. Note that it is fine if initializer is
2149 NULL here, because we sometimes also need to check if a
2150 declaration *must* have an initialization expression. */
2151 if (gfc_current_state () != COMP_DERIVED)
2152 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2153 else
2155 if (current_ts.type == BT_DERIVED
2156 && !current_attr.pointer && !initializer)
2157 initializer = gfc_default_initializer (&current_ts);
2158 t = build_struct (name, cl, &initializer, &as);
2161 m = (t) ? MATCH_YES : MATCH_ERROR;
2163 cleanup:
2164 /* Free stuff up and return. */
2165 gfc_free_expr (initializer);
2166 gfc_free_array_spec (as);
2168 return m;
2172 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2173 This assumes that the byte size is equal to the kind number for
2174 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2176 match
2177 gfc_match_old_kind_spec (gfc_typespec *ts)
2179 match m;
2180 int original_kind;
2182 if (gfc_match_char ('*') != MATCH_YES)
2183 return MATCH_NO;
2185 m = gfc_match_small_literal_int (&ts->kind, NULL);
2186 if (m != MATCH_YES)
2187 return MATCH_ERROR;
2189 original_kind = ts->kind;
2191 /* Massage the kind numbers for complex types. */
2192 if (ts->type == BT_COMPLEX)
2194 if (ts->kind % 2)
2196 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2197 gfc_basic_typename (ts->type), original_kind);
2198 return MATCH_ERROR;
2200 ts->kind /= 2;
2204 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2205 ts->kind = 8;
2207 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2209 if (ts->kind == 4)
2211 if (flag_real4_kind == 8)
2212 ts->kind = 8;
2213 if (flag_real4_kind == 10)
2214 ts->kind = 10;
2215 if (flag_real4_kind == 16)
2216 ts->kind = 16;
2219 if (ts->kind == 8)
2221 if (flag_real8_kind == 4)
2222 ts->kind = 4;
2223 if (flag_real8_kind == 10)
2224 ts->kind = 10;
2225 if (flag_real8_kind == 16)
2226 ts->kind = 16;
2230 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2232 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2233 gfc_basic_typename (ts->type), original_kind);
2234 return MATCH_ERROR;
2237 if (!gfc_notify_std (GFC_STD_GNU,
2238 "Nonstandard type declaration %s*%d at %C",
2239 gfc_basic_typename(ts->type), original_kind))
2240 return MATCH_ERROR;
2242 return MATCH_YES;
2246 /* Match a kind specification. Since kinds are generally optional, we
2247 usually return MATCH_NO if something goes wrong. If a "kind="
2248 string is found, then we know we have an error. */
2250 match
2251 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2253 locus where, loc;
2254 gfc_expr *e;
2255 match m, n;
2256 char c;
2257 const char *msg;
2259 m = MATCH_NO;
2260 n = MATCH_YES;
2261 e = NULL;
2263 where = loc = gfc_current_locus;
2265 if (kind_expr_only)
2266 goto kind_expr;
2268 if (gfc_match_char ('(') == MATCH_NO)
2269 return MATCH_NO;
2271 /* Also gobbles optional text. */
2272 if (gfc_match (" kind = ") == MATCH_YES)
2273 m = MATCH_ERROR;
2275 loc = gfc_current_locus;
2277 kind_expr:
2278 n = gfc_match_init_expr (&e);
2280 if (n != MATCH_YES)
2282 if (gfc_matching_function)
2284 /* The function kind expression might include use associated or
2285 imported parameters and try again after the specification
2286 expressions..... */
2287 if (gfc_match_char (')') != MATCH_YES)
2289 gfc_error ("Missing right parenthesis at %C");
2290 m = MATCH_ERROR;
2291 goto no_match;
2294 gfc_free_expr (e);
2295 gfc_undo_symbols ();
2296 return MATCH_YES;
2298 else
2300 /* ....or else, the match is real. */
2301 if (n == MATCH_NO)
2302 gfc_error ("Expected initialization expression at %C");
2303 if (n != MATCH_YES)
2304 return MATCH_ERROR;
2308 if (e->rank != 0)
2310 gfc_error ("Expected scalar initialization expression at %C");
2311 m = MATCH_ERROR;
2312 goto no_match;
2315 msg = gfc_extract_int (e, &ts->kind);
2317 if (msg != NULL)
2319 gfc_error (msg);
2320 m = MATCH_ERROR;
2321 goto no_match;
2324 /* Before throwing away the expression, let's see if we had a
2325 C interoperable kind (and store the fact). */
2326 if (e->ts.is_c_interop == 1)
2328 /* Mark this as C interoperable if being declared with one
2329 of the named constants from iso_c_binding. */
2330 ts->is_c_interop = e->ts.is_iso_c;
2331 ts->f90_type = e->ts.f90_type;
2334 gfc_free_expr (e);
2335 e = NULL;
2337 /* Ignore errors to this point, if we've gotten here. This means
2338 we ignore the m=MATCH_ERROR from above. */
2339 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2341 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2342 gfc_basic_typename (ts->type));
2343 gfc_current_locus = where;
2344 return MATCH_ERROR;
2347 /* Warn if, e.g., c_int is used for a REAL variable, but not
2348 if, e.g., c_double is used for COMPLEX as the standard
2349 explicitly says that the kind type parameter for complex and real
2350 variable is the same, i.e. c_float == c_float_complex. */
2351 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2352 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2353 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2354 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2355 "is %s", gfc_basic_typename (ts->f90_type), &where,
2356 gfc_basic_typename (ts->type));
2358 gfc_gobble_whitespace ();
2359 if ((c = gfc_next_ascii_char ()) != ')'
2360 && (ts->type != BT_CHARACTER || c != ','))
2362 if (ts->type == BT_CHARACTER)
2363 gfc_error ("Missing right parenthesis or comma at %C");
2364 else
2365 gfc_error ("Missing right parenthesis at %C");
2366 m = MATCH_ERROR;
2368 else
2369 /* All tests passed. */
2370 m = MATCH_YES;
2372 if(m == MATCH_ERROR)
2373 gfc_current_locus = where;
2375 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2376 ts->kind = 8;
2378 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2380 if (ts->kind == 4)
2382 if (flag_real4_kind == 8)
2383 ts->kind = 8;
2384 if (flag_real4_kind == 10)
2385 ts->kind = 10;
2386 if (flag_real4_kind == 16)
2387 ts->kind = 16;
2390 if (ts->kind == 8)
2392 if (flag_real8_kind == 4)
2393 ts->kind = 4;
2394 if (flag_real8_kind == 10)
2395 ts->kind = 10;
2396 if (flag_real8_kind == 16)
2397 ts->kind = 16;
2401 /* Return what we know from the test(s). */
2402 return m;
2404 no_match:
2405 gfc_free_expr (e);
2406 gfc_current_locus = where;
2407 return m;
2411 static match
2412 match_char_kind (int * kind, int * is_iso_c)
2414 locus where;
2415 gfc_expr *e;
2416 match m, n;
2417 const char *msg;
2419 m = MATCH_NO;
2420 e = NULL;
2421 where = gfc_current_locus;
2423 n = gfc_match_init_expr (&e);
2425 if (n != MATCH_YES && gfc_matching_function)
2427 /* The expression might include use-associated or imported
2428 parameters and try again after the specification
2429 expressions. */
2430 gfc_free_expr (e);
2431 gfc_undo_symbols ();
2432 return MATCH_YES;
2435 if (n == MATCH_NO)
2436 gfc_error ("Expected initialization expression at %C");
2437 if (n != MATCH_YES)
2438 return MATCH_ERROR;
2440 if (e->rank != 0)
2442 gfc_error ("Expected scalar initialization expression at %C");
2443 m = MATCH_ERROR;
2444 goto no_match;
2447 msg = gfc_extract_int (e, kind);
2448 *is_iso_c = e->ts.is_iso_c;
2449 if (msg != NULL)
2451 gfc_error (msg);
2452 m = MATCH_ERROR;
2453 goto no_match;
2456 gfc_free_expr (e);
2458 /* Ignore errors to this point, if we've gotten here. This means
2459 we ignore the m=MATCH_ERROR from above. */
2460 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2462 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2463 m = MATCH_ERROR;
2465 else
2466 /* All tests passed. */
2467 m = MATCH_YES;
2469 if (m == MATCH_ERROR)
2470 gfc_current_locus = where;
2472 /* Return what we know from the test(s). */
2473 return m;
2475 no_match:
2476 gfc_free_expr (e);
2477 gfc_current_locus = where;
2478 return m;
2482 /* Match the various kind/length specifications in a CHARACTER
2483 declaration. We don't return MATCH_NO. */
2485 match
2486 gfc_match_char_spec (gfc_typespec *ts)
2488 int kind, seen_length, is_iso_c;
2489 gfc_charlen *cl;
2490 gfc_expr *len;
2491 match m;
2492 bool deferred;
2494 len = NULL;
2495 seen_length = 0;
2496 kind = 0;
2497 is_iso_c = 0;
2498 deferred = false;
2500 /* Try the old-style specification first. */
2501 old_char_selector = 0;
2503 m = match_char_length (&len, &deferred, true);
2504 if (m != MATCH_NO)
2506 if (m == MATCH_YES)
2507 old_char_selector = 1;
2508 seen_length = 1;
2509 goto done;
2512 m = gfc_match_char ('(');
2513 if (m != MATCH_YES)
2515 m = MATCH_YES; /* Character without length is a single char. */
2516 goto done;
2519 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2520 if (gfc_match (" kind =") == MATCH_YES)
2522 m = match_char_kind (&kind, &is_iso_c);
2524 if (m == MATCH_ERROR)
2525 goto done;
2526 if (m == MATCH_NO)
2527 goto syntax;
2529 if (gfc_match (" , len =") == MATCH_NO)
2530 goto rparen;
2532 m = char_len_param_value (&len, &deferred);
2533 if (m == MATCH_NO)
2534 goto syntax;
2535 if (m == MATCH_ERROR)
2536 goto done;
2537 seen_length = 1;
2539 goto rparen;
2542 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2543 if (gfc_match (" len =") == MATCH_YES)
2545 m = char_len_param_value (&len, &deferred);
2546 if (m == MATCH_NO)
2547 goto syntax;
2548 if (m == MATCH_ERROR)
2549 goto done;
2550 seen_length = 1;
2552 if (gfc_match_char (')') == MATCH_YES)
2553 goto done;
2555 if (gfc_match (" , kind =") != MATCH_YES)
2556 goto syntax;
2558 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2559 goto done;
2561 goto rparen;
2564 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2565 m = char_len_param_value (&len, &deferred);
2566 if (m == MATCH_NO)
2567 goto syntax;
2568 if (m == MATCH_ERROR)
2569 goto done;
2570 seen_length = 1;
2572 m = gfc_match_char (')');
2573 if (m == MATCH_YES)
2574 goto done;
2576 if (gfc_match_char (',') != MATCH_YES)
2577 goto syntax;
2579 gfc_match (" kind ="); /* Gobble optional text. */
2581 m = match_char_kind (&kind, &is_iso_c);
2582 if (m == MATCH_ERROR)
2583 goto done;
2584 if (m == MATCH_NO)
2585 goto syntax;
2587 rparen:
2588 /* Require a right-paren at this point. */
2589 m = gfc_match_char (')');
2590 if (m == MATCH_YES)
2591 goto done;
2593 syntax:
2594 gfc_error ("Syntax error in CHARACTER declaration at %C");
2595 m = MATCH_ERROR;
2596 gfc_free_expr (len);
2597 return m;
2599 done:
2600 /* Deal with character functions after USE and IMPORT statements. */
2601 if (gfc_matching_function)
2603 gfc_free_expr (len);
2604 gfc_undo_symbols ();
2605 return MATCH_YES;
2608 if (m != MATCH_YES)
2610 gfc_free_expr (len);
2611 return m;
2614 /* Do some final massaging of the length values. */
2615 cl = gfc_new_charlen (gfc_current_ns, NULL);
2617 if (seen_length == 0)
2618 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2619 else
2620 cl->length = len;
2622 ts->u.cl = cl;
2623 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2624 ts->deferred = deferred;
2626 /* We have to know if it was a C interoperable kind so we can
2627 do accurate type checking of bind(c) procs, etc. */
2628 if (kind != 0)
2629 /* Mark this as C interoperable if being declared with one
2630 of the named constants from iso_c_binding. */
2631 ts->is_c_interop = is_iso_c;
2632 else if (len != NULL)
2633 /* Here, we might have parsed something such as: character(c_char)
2634 In this case, the parsing code above grabs the c_char when
2635 looking for the length (line 1690, roughly). it's the last
2636 testcase for parsing the kind params of a character variable.
2637 However, it's not actually the length. this seems like it
2638 could be an error.
2639 To see if the user used a C interop kind, test the expr
2640 of the so called length, and see if it's C interoperable. */
2641 ts->is_c_interop = len->ts.is_iso_c;
2643 return MATCH_YES;
2647 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2648 structure to the matched specification. This is necessary for FUNCTION and
2649 IMPLICIT statements.
2651 If implicit_flag is nonzero, then we don't check for the optional
2652 kind specification. Not doing so is needed for matching an IMPLICIT
2653 statement correctly. */
2655 match
2656 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2658 char name[GFC_MAX_SYMBOL_LEN + 1];
2659 gfc_symbol *sym, *dt_sym;
2660 match m;
2661 char c;
2662 bool seen_deferred_kind, matched_type;
2663 const char *dt_name;
2665 /* A belt and braces check that the typespec is correctly being treated
2666 as a deferred characteristic association. */
2667 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2668 && (gfc_current_block ()->result->ts.kind == -1)
2669 && (ts->kind == -1);
2670 gfc_clear_ts (ts);
2671 if (seen_deferred_kind)
2672 ts->kind = -1;
2674 /* Clear the current binding label, in case one is given. */
2675 curr_binding_label = NULL;
2677 if (gfc_match (" byte") == MATCH_YES)
2679 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2680 return MATCH_ERROR;
2682 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2684 gfc_error ("BYTE type used at %C "
2685 "is not available on the target machine");
2686 return MATCH_ERROR;
2689 ts->type = BT_INTEGER;
2690 ts->kind = 1;
2691 return MATCH_YES;
2695 m = gfc_match (" type (");
2696 matched_type = (m == MATCH_YES);
2697 if (matched_type)
2699 gfc_gobble_whitespace ();
2700 if (gfc_peek_ascii_char () == '*')
2702 if ((m = gfc_match ("*)")) != MATCH_YES)
2703 return m;
2704 if (gfc_current_state () == COMP_DERIVED)
2706 gfc_error ("Assumed type at %C is not allowed for components");
2707 return MATCH_ERROR;
2709 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2710 "at %C"))
2711 return MATCH_ERROR;
2712 ts->type = BT_ASSUMED;
2713 return MATCH_YES;
2716 m = gfc_match ("%n", name);
2717 matched_type = (m == MATCH_YES);
2720 if ((matched_type && strcmp ("integer", name) == 0)
2721 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2723 ts->type = BT_INTEGER;
2724 ts->kind = gfc_default_integer_kind;
2725 goto get_kind;
2728 if ((matched_type && strcmp ("character", name) == 0)
2729 || (!matched_type && gfc_match (" character") == MATCH_YES))
2731 if (matched_type
2732 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2733 "intrinsic-type-spec at %C"))
2734 return MATCH_ERROR;
2736 ts->type = BT_CHARACTER;
2737 if (implicit_flag == 0)
2738 m = gfc_match_char_spec (ts);
2739 else
2740 m = MATCH_YES;
2742 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2743 m = MATCH_ERROR;
2745 return m;
2748 if ((matched_type && strcmp ("real", name) == 0)
2749 || (!matched_type && gfc_match (" real") == MATCH_YES))
2751 ts->type = BT_REAL;
2752 ts->kind = gfc_default_real_kind;
2753 goto get_kind;
2756 if ((matched_type
2757 && (strcmp ("doubleprecision", name) == 0
2758 || (strcmp ("double", name) == 0
2759 && gfc_match (" precision") == MATCH_YES)))
2760 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2762 if (matched_type
2763 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2764 "intrinsic-type-spec at %C"))
2765 return MATCH_ERROR;
2766 if (matched_type && gfc_match_char (')') != MATCH_YES)
2767 return MATCH_ERROR;
2769 ts->type = BT_REAL;
2770 ts->kind = gfc_default_double_kind;
2771 return MATCH_YES;
2774 if ((matched_type && strcmp ("complex", name) == 0)
2775 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2777 ts->type = BT_COMPLEX;
2778 ts->kind = gfc_default_complex_kind;
2779 goto get_kind;
2782 if ((matched_type
2783 && (strcmp ("doublecomplex", name) == 0
2784 || (strcmp ("double", name) == 0
2785 && gfc_match (" complex") == MATCH_YES)))
2786 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2788 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2789 return MATCH_ERROR;
2791 if (matched_type
2792 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2793 "intrinsic-type-spec at %C"))
2794 return MATCH_ERROR;
2796 if (matched_type && gfc_match_char (')') != MATCH_YES)
2797 return MATCH_ERROR;
2799 ts->type = BT_COMPLEX;
2800 ts->kind = gfc_default_double_kind;
2801 return MATCH_YES;
2804 if ((matched_type && strcmp ("logical", name) == 0)
2805 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2807 ts->type = BT_LOGICAL;
2808 ts->kind = gfc_default_logical_kind;
2809 goto get_kind;
2812 if (matched_type)
2813 m = gfc_match_char (')');
2815 if (m == MATCH_YES)
2816 ts->type = BT_DERIVED;
2817 else
2819 /* Match CLASS declarations. */
2820 m = gfc_match (" class ( * )");
2821 if (m == MATCH_ERROR)
2822 return MATCH_ERROR;
2823 else if (m == MATCH_YES)
2825 gfc_symbol *upe;
2826 gfc_symtree *st;
2827 ts->type = BT_CLASS;
2828 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2829 if (upe == NULL)
2831 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2832 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2833 st->n.sym = upe;
2834 gfc_set_sym_referenced (upe);
2835 upe->refs++;
2836 upe->ts.type = BT_VOID;
2837 upe->attr.unlimited_polymorphic = 1;
2838 /* This is essential to force the construction of
2839 unlimited polymorphic component class containers. */
2840 upe->attr.zero_comp = 1;
2841 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2842 &gfc_current_locus))
2843 return MATCH_ERROR;
2845 else
2847 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2848 if (st == NULL)
2849 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2850 st->n.sym = upe;
2851 upe->refs++;
2853 ts->u.derived = upe;
2854 return m;
2857 m = gfc_match (" class ( %n )", name);
2858 if (m != MATCH_YES)
2859 return m;
2860 ts->type = BT_CLASS;
2862 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2863 return MATCH_ERROR;
2866 /* Defer association of the derived type until the end of the
2867 specification block. However, if the derived type can be
2868 found, add it to the typespec. */
2869 if (gfc_matching_function)
2871 ts->u.derived = NULL;
2872 if (gfc_current_state () != COMP_INTERFACE
2873 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2875 sym = gfc_find_dt_in_generic (sym);
2876 ts->u.derived = sym;
2878 return MATCH_YES;
2881 /* Search for the name but allow the components to be defined later. If
2882 type = -1, this typespec has been seen in a function declaration but
2883 the type could not be accessed at that point. The actual derived type is
2884 stored in a symtree with the first letter of the name capitalized; the
2885 symtree with the all lower-case name contains the associated
2886 generic function. */
2887 dt_name = gfc_get_string ("%c%s",
2888 (char) TOUPPER ((unsigned char) name[0]),
2889 (const char*)&name[1]);
2890 sym = NULL;
2891 dt_sym = NULL;
2892 if (ts->kind != -1)
2894 gfc_get_ha_symbol (name, &sym);
2895 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2897 gfc_error ("Type name %qs at %C is ambiguous", name);
2898 return MATCH_ERROR;
2900 if (sym->generic && !dt_sym)
2901 dt_sym = gfc_find_dt_in_generic (sym);
2903 else if (ts->kind == -1)
2905 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2906 || gfc_current_ns->has_import_set;
2907 gfc_find_symbol (name, NULL, iface, &sym);
2908 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2910 gfc_error ("Type name %qs at %C is ambiguous", name);
2911 return MATCH_ERROR;
2913 if (sym && sym->generic && !dt_sym)
2914 dt_sym = gfc_find_dt_in_generic (sym);
2916 ts->kind = 0;
2917 if (sym == NULL)
2918 return MATCH_NO;
2921 if ((sym->attr.flavor != FL_UNKNOWN
2922 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2923 || sym->attr.subroutine)
2925 gfc_error ("Type name %qs at %C conflicts with previously declared "
2926 "entity at %L, which has the same name", name,
2927 &sym->declared_at);
2928 return MATCH_ERROR;
2931 gfc_save_symbol_data (sym);
2932 gfc_set_sym_referenced (sym);
2933 if (!sym->attr.generic
2934 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2935 return MATCH_ERROR;
2937 if (!sym->attr.function
2938 && !gfc_add_function (&sym->attr, sym->name, NULL))
2939 return MATCH_ERROR;
2941 if (!dt_sym)
2943 gfc_interface *intr, *head;
2945 /* Use upper case to save the actual derived-type symbol. */
2946 gfc_get_symbol (dt_name, NULL, &dt_sym);
2947 dt_sym->name = gfc_get_string (sym->name);
2948 head = sym->generic;
2949 intr = gfc_get_interface ();
2950 intr->sym = dt_sym;
2951 intr->where = gfc_current_locus;
2952 intr->next = head;
2953 sym->generic = intr;
2954 sym->attr.if_source = IFSRC_DECL;
2956 else
2957 gfc_save_symbol_data (dt_sym);
2959 gfc_set_sym_referenced (dt_sym);
2961 if (dt_sym->attr.flavor != FL_DERIVED
2962 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2963 return MATCH_ERROR;
2965 ts->u.derived = dt_sym;
2967 return MATCH_YES;
2969 get_kind:
2970 if (matched_type
2971 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2972 "intrinsic-type-spec at %C"))
2973 return MATCH_ERROR;
2975 /* For all types except double, derived and character, look for an
2976 optional kind specifier. MATCH_NO is actually OK at this point. */
2977 if (implicit_flag == 1)
2979 if (matched_type && gfc_match_char (')') != MATCH_YES)
2980 return MATCH_ERROR;
2982 return MATCH_YES;
2985 if (gfc_current_form == FORM_FREE)
2987 c = gfc_peek_ascii_char ();
2988 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2989 && c != ':' && c != ',')
2991 if (matched_type && c == ')')
2993 gfc_next_ascii_char ();
2994 return MATCH_YES;
2996 return MATCH_NO;
3000 m = gfc_match_kind_spec (ts, false);
3001 if (m == MATCH_NO && ts->type != BT_CHARACTER)
3002 m = gfc_match_old_kind_spec (ts);
3004 if (matched_type && gfc_match_char (')') != MATCH_YES)
3005 return MATCH_ERROR;
3007 /* Defer association of the KIND expression of function results
3008 until after USE and IMPORT statements. */
3009 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
3010 || gfc_matching_function)
3011 return MATCH_YES;
3013 if (m == MATCH_NO)
3014 m = MATCH_YES; /* No kind specifier found. */
3016 return m;
3020 /* Match an IMPLICIT NONE statement. Actually, this statement is
3021 already matched in parse.c, or we would not end up here in the
3022 first place. So the only thing we need to check, is if there is
3023 trailing garbage. If not, the match is successful. */
3025 match
3026 gfc_match_implicit_none (void)
3028 char c;
3029 match m;
3030 char name[GFC_MAX_SYMBOL_LEN + 1];
3031 bool type = false;
3032 bool external = false;
3033 locus cur_loc = gfc_current_locus;
3035 if (gfc_current_ns->seen_implicit_none
3036 || gfc_current_ns->has_implicit_none_export)
3038 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3039 return MATCH_ERROR;
3042 gfc_gobble_whitespace ();
3043 c = gfc_peek_ascii_char ();
3044 if (c == '(')
3046 (void) gfc_next_ascii_char ();
3047 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3048 return MATCH_ERROR;
3050 gfc_gobble_whitespace ();
3051 if (gfc_peek_ascii_char () == ')')
3053 (void) gfc_next_ascii_char ();
3054 type = true;
3056 else
3057 for(;;)
3059 m = gfc_match (" %n", name);
3060 if (m != MATCH_YES)
3061 return MATCH_ERROR;
3063 if (strcmp (name, "type") == 0)
3064 type = true;
3065 else if (strcmp (name, "external") == 0)
3066 external = true;
3067 else
3068 return MATCH_ERROR;
3070 gfc_gobble_whitespace ();
3071 c = gfc_next_ascii_char ();
3072 if (c == ',')
3073 continue;
3074 if (c == ')')
3075 break;
3076 return MATCH_ERROR;
3079 else
3080 type = true;
3082 if (gfc_match_eos () != MATCH_YES)
3083 return MATCH_ERROR;
3085 gfc_set_implicit_none (type, external, &cur_loc);
3087 return MATCH_YES;
3091 /* Match the letter range(s) of an IMPLICIT statement. */
3093 static match
3094 match_implicit_range (void)
3096 char c, c1, c2;
3097 int inner;
3098 locus cur_loc;
3100 cur_loc = gfc_current_locus;
3102 gfc_gobble_whitespace ();
3103 c = gfc_next_ascii_char ();
3104 if (c != '(')
3106 gfc_error ("Missing character range in IMPLICIT at %C");
3107 goto bad;
3110 inner = 1;
3111 while (inner)
3113 gfc_gobble_whitespace ();
3114 c1 = gfc_next_ascii_char ();
3115 if (!ISALPHA (c1))
3116 goto bad;
3118 gfc_gobble_whitespace ();
3119 c = gfc_next_ascii_char ();
3121 switch (c)
3123 case ')':
3124 inner = 0; /* Fall through. */
3126 case ',':
3127 c2 = c1;
3128 break;
3130 case '-':
3131 gfc_gobble_whitespace ();
3132 c2 = gfc_next_ascii_char ();
3133 if (!ISALPHA (c2))
3134 goto bad;
3136 gfc_gobble_whitespace ();
3137 c = gfc_next_ascii_char ();
3139 if ((c != ',') && (c != ')'))
3140 goto bad;
3141 if (c == ')')
3142 inner = 0;
3144 break;
3146 default:
3147 goto bad;
3150 if (c1 > c2)
3152 gfc_error ("Letters must be in alphabetic order in "
3153 "IMPLICIT statement at %C");
3154 goto bad;
3157 /* See if we can add the newly matched range to the pending
3158 implicits from this IMPLICIT statement. We do not check for
3159 conflicts with whatever earlier IMPLICIT statements may have
3160 set. This is done when we've successfully finished matching
3161 the current one. */
3162 if (!gfc_add_new_implicit_range (c1, c2))
3163 goto bad;
3166 return MATCH_YES;
3168 bad:
3169 gfc_syntax_error (ST_IMPLICIT);
3171 gfc_current_locus = cur_loc;
3172 return MATCH_ERROR;
3176 /* Match an IMPLICIT statement, storing the types for
3177 gfc_set_implicit() if the statement is accepted by the parser.
3178 There is a strange looking, but legal syntactic construction
3179 possible. It looks like:
3181 IMPLICIT INTEGER (a-b) (c-d)
3183 This is legal if "a-b" is a constant expression that happens to
3184 equal one of the legal kinds for integers. The real problem
3185 happens with an implicit specification that looks like:
3187 IMPLICIT INTEGER (a-b)
3189 In this case, a typespec matcher that is "greedy" (as most of the
3190 matchers are) gobbles the character range as a kindspec, leaving
3191 nothing left. We therefore have to go a bit more slowly in the
3192 matching process by inhibiting the kindspec checking during
3193 typespec matching and checking for a kind later. */
3195 match
3196 gfc_match_implicit (void)
3198 gfc_typespec ts;
3199 locus cur_loc;
3200 char c;
3201 match m;
3203 if (gfc_current_ns->seen_implicit_none)
3205 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3206 "statement");
3207 return MATCH_ERROR;
3210 gfc_clear_ts (&ts);
3212 /* We don't allow empty implicit statements. */
3213 if (gfc_match_eos () == MATCH_YES)
3215 gfc_error ("Empty IMPLICIT statement at %C");
3216 return MATCH_ERROR;
3221 /* First cleanup. */
3222 gfc_clear_new_implicit ();
3224 /* A basic type is mandatory here. */
3225 m = gfc_match_decl_type_spec (&ts, 1);
3226 if (m == MATCH_ERROR)
3227 goto error;
3228 if (m == MATCH_NO)
3229 goto syntax;
3231 cur_loc = gfc_current_locus;
3232 m = match_implicit_range ();
3234 if (m == MATCH_YES)
3236 /* We may have <TYPE> (<RANGE>). */
3237 gfc_gobble_whitespace ();
3238 c = gfc_peek_ascii_char ();
3239 if (c == ',' || c == '\n' || c == ';' || c == '!')
3241 /* Check for CHARACTER with no length parameter. */
3242 if (ts.type == BT_CHARACTER && !ts.u.cl)
3244 ts.kind = gfc_default_character_kind;
3245 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3246 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3247 NULL, 1);
3250 /* Record the Successful match. */
3251 if (!gfc_merge_new_implicit (&ts))
3252 return MATCH_ERROR;
3253 if (c == ',')
3254 c = gfc_next_ascii_char ();
3255 else if (gfc_match_eos () == MATCH_ERROR)
3256 goto error;
3257 continue;
3260 gfc_current_locus = cur_loc;
3263 /* Discard the (incorrectly) matched range. */
3264 gfc_clear_new_implicit ();
3266 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3267 if (ts.type == BT_CHARACTER)
3268 m = gfc_match_char_spec (&ts);
3269 else
3271 m = gfc_match_kind_spec (&ts, false);
3272 if (m == MATCH_NO)
3274 m = gfc_match_old_kind_spec (&ts);
3275 if (m == MATCH_ERROR)
3276 goto error;
3277 if (m == MATCH_NO)
3278 goto syntax;
3281 if (m == MATCH_ERROR)
3282 goto error;
3284 m = match_implicit_range ();
3285 if (m == MATCH_ERROR)
3286 goto error;
3287 if (m == MATCH_NO)
3288 goto syntax;
3290 gfc_gobble_whitespace ();
3291 c = gfc_next_ascii_char ();
3292 if (c != ',' && gfc_match_eos () != MATCH_YES)
3293 goto syntax;
3295 if (!gfc_merge_new_implicit (&ts))
3296 return MATCH_ERROR;
3298 while (c == ',');
3300 return MATCH_YES;
3302 syntax:
3303 gfc_syntax_error (ST_IMPLICIT);
3305 error:
3306 return MATCH_ERROR;
3310 match
3311 gfc_match_import (void)
3313 char name[GFC_MAX_SYMBOL_LEN + 1];
3314 match m;
3315 gfc_symbol *sym;
3316 gfc_symtree *st;
3318 if (gfc_current_ns->proc_name == NULL
3319 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3321 gfc_error ("IMPORT statement at %C only permitted in "
3322 "an INTERFACE body");
3323 return MATCH_ERROR;
3326 if (gfc_current_ns->proc_name->attr.module_procedure)
3328 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3329 "in a module procedure interface body");
3330 return MATCH_ERROR;
3333 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3334 return MATCH_ERROR;
3336 if (gfc_match_eos () == MATCH_YES)
3338 /* All host variables should be imported. */
3339 gfc_current_ns->has_import_set = 1;
3340 return MATCH_YES;
3343 if (gfc_match (" ::") == MATCH_YES)
3345 if (gfc_match_eos () == MATCH_YES)
3347 gfc_error ("Expecting list of named entities at %C");
3348 return MATCH_ERROR;
3352 for(;;)
3354 sym = NULL;
3355 m = gfc_match (" %n", name);
3356 switch (m)
3358 case MATCH_YES:
3359 if (gfc_current_ns->parent != NULL
3360 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3362 gfc_error ("Type name %qs at %C is ambiguous", name);
3363 return MATCH_ERROR;
3365 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3366 && gfc_find_symbol (name,
3367 gfc_current_ns->proc_name->ns->parent,
3368 1, &sym))
3370 gfc_error ("Type name %qs at %C is ambiguous", name);
3371 return MATCH_ERROR;
3374 if (sym == NULL)
3376 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3377 "at %C - does not exist.", name);
3378 return MATCH_ERROR;
3381 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3383 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3384 "at %C", name);
3385 goto next_item;
3388 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3389 st->n.sym = sym;
3390 sym->refs++;
3391 sym->attr.imported = 1;
3393 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3395 /* The actual derived type is stored in a symtree with the first
3396 letter of the name capitalized; the symtree with the all
3397 lower-case name contains the associated generic function. */
3398 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3399 gfc_get_string ("%c%s",
3400 (char) TOUPPER ((unsigned char) name[0]),
3401 &name[1]));
3402 st->n.sym = sym;
3403 sym->refs++;
3404 sym->attr.imported = 1;
3407 goto next_item;
3409 case MATCH_NO:
3410 break;
3412 case MATCH_ERROR:
3413 return MATCH_ERROR;
3416 next_item:
3417 if (gfc_match_eos () == MATCH_YES)
3418 break;
3419 if (gfc_match_char (',') != MATCH_YES)
3420 goto syntax;
3423 return MATCH_YES;
3425 syntax:
3426 gfc_error ("Syntax error in IMPORT statement at %C");
3427 return MATCH_ERROR;
3431 /* A minimal implementation of gfc_match without whitespace, escape
3432 characters or variable arguments. Returns true if the next
3433 characters match the TARGET template exactly. */
3435 static bool
3436 match_string_p (const char *target)
3438 const char *p;
3440 for (p = target; *p; p++)
3441 if ((char) gfc_next_ascii_char () != *p)
3442 return false;
3443 return true;
3446 /* Matches an attribute specification including array specs. If
3447 successful, leaves the variables current_attr and current_as
3448 holding the specification. Also sets the colon_seen variable for
3449 later use by matchers associated with initializations.
3451 This subroutine is a little tricky in the sense that we don't know
3452 if we really have an attr-spec until we hit the double colon.
3453 Until that time, we can only return MATCH_NO. This forces us to
3454 check for duplicate specification at this level. */
3456 static match
3457 match_attr_spec (void)
3459 /* Modifiers that can exist in a type statement. */
3460 enum
3461 { GFC_DECL_BEGIN = 0,
3462 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3463 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3464 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3465 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3466 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3467 DECL_NONE, GFC_DECL_END /* Sentinel */
3470 /* GFC_DECL_END is the sentinel, index starts at 0. */
3471 #define NUM_DECL GFC_DECL_END
3473 locus start, seen_at[NUM_DECL];
3474 int seen[NUM_DECL];
3475 unsigned int d;
3476 const char *attr;
3477 match m;
3478 bool t;
3480 gfc_clear_attr (&current_attr);
3481 start = gfc_current_locus;
3483 current_as = NULL;
3484 colon_seen = 0;
3486 /* See if we get all of the keywords up to the final double colon. */
3487 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3488 seen[d] = 0;
3490 for (;;)
3492 char ch;
3494 d = DECL_NONE;
3495 gfc_gobble_whitespace ();
3497 ch = gfc_next_ascii_char ();
3498 if (ch == ':')
3500 /* This is the successful exit condition for the loop. */
3501 if (gfc_next_ascii_char () == ':')
3502 break;
3504 else if (ch == ',')
3506 gfc_gobble_whitespace ();
3507 switch (gfc_peek_ascii_char ())
3509 case 'a':
3510 gfc_next_ascii_char ();
3511 switch (gfc_next_ascii_char ())
3513 case 'l':
3514 if (match_string_p ("locatable"))
3516 /* Matched "allocatable". */
3517 d = DECL_ALLOCATABLE;
3519 break;
3521 case 's':
3522 if (match_string_p ("ynchronous"))
3524 /* Matched "asynchronous". */
3525 d = DECL_ASYNCHRONOUS;
3527 break;
3529 break;
3531 case 'b':
3532 /* Try and match the bind(c). */
3533 m = gfc_match_bind_c (NULL, true);
3534 if (m == MATCH_YES)
3535 d = DECL_IS_BIND_C;
3536 else if (m == MATCH_ERROR)
3537 goto cleanup;
3538 break;
3540 case 'c':
3541 gfc_next_ascii_char ();
3542 if ('o' != gfc_next_ascii_char ())
3543 break;
3544 switch (gfc_next_ascii_char ())
3546 case 'd':
3547 if (match_string_p ("imension"))
3549 d = DECL_CODIMENSION;
3550 break;
3552 case 'n':
3553 if (match_string_p ("tiguous"))
3555 d = DECL_CONTIGUOUS;
3556 break;
3559 break;
3561 case 'd':
3562 if (match_string_p ("dimension"))
3563 d = DECL_DIMENSION;
3564 break;
3566 case 'e':
3567 if (match_string_p ("external"))
3568 d = DECL_EXTERNAL;
3569 break;
3571 case 'i':
3572 if (match_string_p ("int"))
3574 ch = gfc_next_ascii_char ();
3575 if (ch == 'e')
3577 if (match_string_p ("nt"))
3579 /* Matched "intent". */
3580 /* TODO: Call match_intent_spec from here. */
3581 if (gfc_match (" ( in out )") == MATCH_YES)
3582 d = DECL_INOUT;
3583 else if (gfc_match (" ( in )") == MATCH_YES)
3584 d = DECL_IN;
3585 else if (gfc_match (" ( out )") == MATCH_YES)
3586 d = DECL_OUT;
3589 else if (ch == 'r')
3591 if (match_string_p ("insic"))
3593 /* Matched "intrinsic". */
3594 d = DECL_INTRINSIC;
3598 break;
3600 case 'o':
3601 if (match_string_p ("optional"))
3602 d = DECL_OPTIONAL;
3603 break;
3605 case 'p':
3606 gfc_next_ascii_char ();
3607 switch (gfc_next_ascii_char ())
3609 case 'a':
3610 if (match_string_p ("rameter"))
3612 /* Matched "parameter". */
3613 d = DECL_PARAMETER;
3615 break;
3617 case 'o':
3618 if (match_string_p ("inter"))
3620 /* Matched "pointer". */
3621 d = DECL_POINTER;
3623 break;
3625 case 'r':
3626 ch = gfc_next_ascii_char ();
3627 if (ch == 'i')
3629 if (match_string_p ("vate"))
3631 /* Matched "private". */
3632 d = DECL_PRIVATE;
3635 else if (ch == 'o')
3637 if (match_string_p ("tected"))
3639 /* Matched "protected". */
3640 d = DECL_PROTECTED;
3643 break;
3645 case 'u':
3646 if (match_string_p ("blic"))
3648 /* Matched "public". */
3649 d = DECL_PUBLIC;
3651 break;
3653 break;
3655 case 's':
3656 if (match_string_p ("save"))
3657 d = DECL_SAVE;
3658 break;
3660 case 't':
3661 if (match_string_p ("target"))
3662 d = DECL_TARGET;
3663 break;
3665 case 'v':
3666 gfc_next_ascii_char ();
3667 ch = gfc_next_ascii_char ();
3668 if (ch == 'a')
3670 if (match_string_p ("lue"))
3672 /* Matched "value". */
3673 d = DECL_VALUE;
3676 else if (ch == 'o')
3678 if (match_string_p ("latile"))
3680 /* Matched "volatile". */
3681 d = DECL_VOLATILE;
3684 break;
3688 /* No double colon and no recognizable decl_type, so assume that
3689 we've been looking at something else the whole time. */
3690 if (d == DECL_NONE)
3692 m = MATCH_NO;
3693 goto cleanup;
3696 /* Check to make sure any parens are paired up correctly. */
3697 if (gfc_match_parens () == MATCH_ERROR)
3699 m = MATCH_ERROR;
3700 goto cleanup;
3703 seen[d]++;
3704 seen_at[d] = gfc_current_locus;
3706 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3708 gfc_array_spec *as = NULL;
3710 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3711 d == DECL_CODIMENSION);
3713 if (current_as == NULL)
3714 current_as = as;
3715 else if (m == MATCH_YES)
3717 if (!merge_array_spec (as, current_as, false))
3718 m = MATCH_ERROR;
3719 free (as);
3722 if (m == MATCH_NO)
3724 if (d == DECL_CODIMENSION)
3725 gfc_error ("Missing codimension specification at %C");
3726 else
3727 gfc_error ("Missing dimension specification at %C");
3728 m = MATCH_ERROR;
3731 if (m == MATCH_ERROR)
3732 goto cleanup;
3736 /* Since we've seen a double colon, we have to be looking at an
3737 attr-spec. This means that we can now issue errors. */
3738 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3739 if (seen[d] > 1)
3741 switch (d)
3743 case DECL_ALLOCATABLE:
3744 attr = "ALLOCATABLE";
3745 break;
3746 case DECL_ASYNCHRONOUS:
3747 attr = "ASYNCHRONOUS";
3748 break;
3749 case DECL_CODIMENSION:
3750 attr = "CODIMENSION";
3751 break;
3752 case DECL_CONTIGUOUS:
3753 attr = "CONTIGUOUS";
3754 break;
3755 case DECL_DIMENSION:
3756 attr = "DIMENSION";
3757 break;
3758 case DECL_EXTERNAL:
3759 attr = "EXTERNAL";
3760 break;
3761 case DECL_IN:
3762 attr = "INTENT (IN)";
3763 break;
3764 case DECL_OUT:
3765 attr = "INTENT (OUT)";
3766 break;
3767 case DECL_INOUT:
3768 attr = "INTENT (IN OUT)";
3769 break;
3770 case DECL_INTRINSIC:
3771 attr = "INTRINSIC";
3772 break;
3773 case DECL_OPTIONAL:
3774 attr = "OPTIONAL";
3775 break;
3776 case DECL_PARAMETER:
3777 attr = "PARAMETER";
3778 break;
3779 case DECL_POINTER:
3780 attr = "POINTER";
3781 break;
3782 case DECL_PROTECTED:
3783 attr = "PROTECTED";
3784 break;
3785 case DECL_PRIVATE:
3786 attr = "PRIVATE";
3787 break;
3788 case DECL_PUBLIC:
3789 attr = "PUBLIC";
3790 break;
3791 case DECL_SAVE:
3792 attr = "SAVE";
3793 break;
3794 case DECL_TARGET:
3795 attr = "TARGET";
3796 break;
3797 case DECL_IS_BIND_C:
3798 attr = "IS_BIND_C";
3799 break;
3800 case DECL_VALUE:
3801 attr = "VALUE";
3802 break;
3803 case DECL_VOLATILE:
3804 attr = "VOLATILE";
3805 break;
3806 default:
3807 attr = NULL; /* This shouldn't happen. */
3810 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3811 m = MATCH_ERROR;
3812 goto cleanup;
3815 /* Now that we've dealt with duplicate attributes, add the attributes
3816 to the current attribute. */
3817 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3819 if (seen[d] == 0)
3820 continue;
3822 if (gfc_current_state () == COMP_DERIVED
3823 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3824 && d != DECL_POINTER && d != DECL_PRIVATE
3825 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3827 if (d == DECL_ALLOCATABLE)
3829 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3830 "attribute at %C in a TYPE definition"))
3832 m = MATCH_ERROR;
3833 goto cleanup;
3836 else
3838 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3839 &seen_at[d]);
3840 m = MATCH_ERROR;
3841 goto cleanup;
3845 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3846 && gfc_current_state () != COMP_MODULE)
3848 if (d == DECL_PRIVATE)
3849 attr = "PRIVATE";
3850 else
3851 attr = "PUBLIC";
3852 if (gfc_current_state () == COMP_DERIVED
3853 && gfc_state_stack->previous
3854 && gfc_state_stack->previous->state == COMP_MODULE)
3856 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3857 "at %L in a TYPE definition", attr,
3858 &seen_at[d]))
3860 m = MATCH_ERROR;
3861 goto cleanup;
3864 else
3866 gfc_error ("%s attribute at %L is not allowed outside of the "
3867 "specification part of a module", attr, &seen_at[d]);
3868 m = MATCH_ERROR;
3869 goto cleanup;
3873 switch (d)
3875 case DECL_ALLOCATABLE:
3876 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3877 break;
3879 case DECL_ASYNCHRONOUS:
3880 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3881 t = false;
3882 else
3883 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3884 break;
3886 case DECL_CODIMENSION:
3887 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3888 break;
3890 case DECL_CONTIGUOUS:
3891 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3892 t = false;
3893 else
3894 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3895 break;
3897 case DECL_DIMENSION:
3898 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3899 break;
3901 case DECL_EXTERNAL:
3902 t = gfc_add_external (&current_attr, &seen_at[d]);
3903 break;
3905 case DECL_IN:
3906 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3907 break;
3909 case DECL_OUT:
3910 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3911 break;
3913 case DECL_INOUT:
3914 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3915 break;
3917 case DECL_INTRINSIC:
3918 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3919 break;
3921 case DECL_OPTIONAL:
3922 t = gfc_add_optional (&current_attr, &seen_at[d]);
3923 break;
3925 case DECL_PARAMETER:
3926 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3927 break;
3929 case DECL_POINTER:
3930 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3931 break;
3933 case DECL_PROTECTED:
3934 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3936 gfc_error ("PROTECTED at %C only allowed in specification "
3937 "part of a module");
3938 t = false;
3939 break;
3942 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3943 t = false;
3944 else
3945 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3946 break;
3948 case DECL_PRIVATE:
3949 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3950 &seen_at[d]);
3951 break;
3953 case DECL_PUBLIC:
3954 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3955 &seen_at[d]);
3956 break;
3958 case DECL_SAVE:
3959 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3960 break;
3962 case DECL_TARGET:
3963 t = gfc_add_target (&current_attr, &seen_at[d]);
3964 break;
3966 case DECL_IS_BIND_C:
3967 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3968 break;
3970 case DECL_VALUE:
3971 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
3972 t = false;
3973 else
3974 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3975 break;
3977 case DECL_VOLATILE:
3978 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
3979 t = false;
3980 else
3981 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3982 break;
3984 default:
3985 gfc_internal_error ("match_attr_spec(): Bad attribute");
3988 if (!t)
3990 m = MATCH_ERROR;
3991 goto cleanup;
3995 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3996 if ((gfc_current_state () == COMP_MODULE
3997 || gfc_current_state () == COMP_SUBMODULE)
3998 && !current_attr.save
3999 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4000 current_attr.save = SAVE_IMPLICIT;
4002 colon_seen = 1;
4003 return MATCH_YES;
4005 cleanup:
4006 gfc_current_locus = start;
4007 gfc_free_array_spec (current_as);
4008 current_as = NULL;
4009 return m;
4013 /* Set the binding label, dest_label, either with the binding label
4014 stored in the given gfc_typespec, ts, or if none was provided, it
4015 will be the symbol name in all lower case, as required by the draft
4016 (J3/04-007, section 15.4.1). If a binding label was given and
4017 there is more than one argument (num_idents), it is an error. */
4019 static bool
4020 set_binding_label (const char **dest_label, const char *sym_name,
4021 int num_idents)
4023 if (num_idents > 1 && has_name_equals)
4025 gfc_error ("Multiple identifiers provided with "
4026 "single NAME= specifier at %C");
4027 return false;
4030 if (curr_binding_label)
4031 /* Binding label given; store in temp holder till have sym. */
4032 *dest_label = curr_binding_label;
4033 else
4035 /* No binding label given, and the NAME= specifier did not exist,
4036 which means there was no NAME="". */
4037 if (sym_name != NULL && has_name_equals == 0)
4038 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4041 return true;
4045 /* Set the status of the given common block as being BIND(C) or not,
4046 depending on the given parameter, is_bind_c. */
4048 void
4049 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4051 com_block->is_bind_c = is_bind_c;
4052 return;
4056 /* Verify that the given gfc_typespec is for a C interoperable type. */
4058 bool
4059 gfc_verify_c_interop (gfc_typespec *ts)
4061 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4062 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4063 ? true : false;
4064 else if (ts->type == BT_CLASS)
4065 return false;
4066 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4067 return false;
4069 return true;
4073 /* Verify that the variables of a given common block, which has been
4074 defined with the attribute specifier bind(c), to be of a C
4075 interoperable type. Errors will be reported here, if
4076 encountered. */
4078 bool
4079 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4081 gfc_symbol *curr_sym = NULL;
4082 bool retval = true;
4084 curr_sym = com_block->head;
4086 /* Make sure we have at least one symbol. */
4087 if (curr_sym == NULL)
4088 return retval;
4090 /* Here we know we have a symbol, so we'll execute this loop
4091 at least once. */
4094 /* The second to last param, 1, says this is in a common block. */
4095 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4096 curr_sym = curr_sym->common_next;
4097 } while (curr_sym != NULL);
4099 return retval;
4103 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4104 an appropriate error message is reported. */
4106 bool
4107 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4108 int is_in_common, gfc_common_head *com_block)
4110 bool bind_c_function = false;
4111 bool retval = true;
4113 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4114 bind_c_function = true;
4116 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4118 tmp_sym = tmp_sym->result;
4119 /* Make sure it wasn't an implicitly typed result. */
4120 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4122 gfc_warning (OPT_Wc_binding_type,
4123 "Implicitly declared BIND(C) function %qs at "
4124 "%L may not be C interoperable", tmp_sym->name,
4125 &tmp_sym->declared_at);
4126 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4127 /* Mark it as C interoperable to prevent duplicate warnings. */
4128 tmp_sym->ts.is_c_interop = 1;
4129 tmp_sym->attr.is_c_interop = 1;
4133 /* Here, we know we have the bind(c) attribute, so if we have
4134 enough type info, then verify that it's a C interop kind.
4135 The info could be in the symbol already, or possibly still in
4136 the given ts (current_ts), so look in both. */
4137 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4139 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4141 /* See if we're dealing with a sym in a common block or not. */
4142 if (is_in_common == 1 && warn_c_binding_type)
4144 gfc_warning (OPT_Wc_binding_type,
4145 "Variable %qs in common block %qs at %L "
4146 "may not be a C interoperable "
4147 "kind though common block %qs is BIND(C)",
4148 tmp_sym->name, com_block->name,
4149 &(tmp_sym->declared_at), com_block->name);
4151 else
4153 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4154 gfc_error ("Type declaration %qs at %L is not C "
4155 "interoperable but it is BIND(C)",
4156 tmp_sym->name, &(tmp_sym->declared_at));
4157 else if (warn_c_binding_type)
4158 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4159 "may not be a C interoperable "
4160 "kind but it is BIND(C)",
4161 tmp_sym->name, &(tmp_sym->declared_at));
4165 /* Variables declared w/in a common block can't be bind(c)
4166 since there's no way for C to see these variables, so there's
4167 semantically no reason for the attribute. */
4168 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4170 gfc_error ("Variable %qs in common block %qs at "
4171 "%L cannot be declared with BIND(C) "
4172 "since it is not a global",
4173 tmp_sym->name, com_block->name,
4174 &(tmp_sym->declared_at));
4175 retval = false;
4178 /* Scalar variables that are bind(c) can not have the pointer
4179 or allocatable attributes. */
4180 if (tmp_sym->attr.is_bind_c == 1)
4182 if (tmp_sym->attr.pointer == 1)
4184 gfc_error ("Variable %qs at %L cannot have both the "
4185 "POINTER and BIND(C) attributes",
4186 tmp_sym->name, &(tmp_sym->declared_at));
4187 retval = false;
4190 if (tmp_sym->attr.allocatable == 1)
4192 gfc_error ("Variable %qs at %L cannot have both the "
4193 "ALLOCATABLE and BIND(C) attributes",
4194 tmp_sym->name, &(tmp_sym->declared_at));
4195 retval = false;
4200 /* If it is a BIND(C) function, make sure the return value is a
4201 scalar value. The previous tests in this function made sure
4202 the type is interoperable. */
4203 if (bind_c_function && tmp_sym->as != NULL)
4204 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4205 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4207 /* BIND(C) functions can not return a character string. */
4208 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4209 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4210 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4211 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4212 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4213 "be a character string", tmp_sym->name,
4214 &(tmp_sym->declared_at));
4217 /* See if the symbol has been marked as private. If it has, make sure
4218 there is no binding label and warn the user if there is one. */
4219 if (tmp_sym->attr.access == ACCESS_PRIVATE
4220 && tmp_sym->binding_label)
4221 /* Use gfc_warning_now because we won't say that the symbol fails
4222 just because of this. */
4223 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4224 "given the binding label %qs", tmp_sym->name,
4225 &(tmp_sym->declared_at), tmp_sym->binding_label);
4227 return retval;
4231 /* Set the appropriate fields for a symbol that's been declared as
4232 BIND(C) (the is_bind_c flag and the binding label), and verify that
4233 the type is C interoperable. Errors are reported by the functions
4234 used to set/test these fields. */
4236 bool
4237 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4239 bool retval = true;
4241 /* TODO: Do we need to make sure the vars aren't marked private? */
4243 /* Set the is_bind_c bit in symbol_attribute. */
4244 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4246 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4247 return false;
4249 return retval;
4253 /* Set the fields marking the given common block as BIND(C), including
4254 a binding label, and report any errors encountered. */
4256 bool
4257 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4259 bool retval = true;
4261 /* destLabel, common name, typespec (which may have binding label). */
4262 if (!set_binding_label (&com_block->binding_label, com_block->name,
4263 num_idents))
4264 return false;
4266 /* Set the given common block (com_block) to being bind(c) (1). */
4267 set_com_block_bind_c (com_block, 1);
4269 return retval;
4273 /* Retrieve the list of one or more identifiers that the given bind(c)
4274 attribute applies to. */
4276 bool
4277 get_bind_c_idents (void)
4279 char name[GFC_MAX_SYMBOL_LEN + 1];
4280 int num_idents = 0;
4281 gfc_symbol *tmp_sym = NULL;
4282 match found_id;
4283 gfc_common_head *com_block = NULL;
4285 if (gfc_match_name (name) == MATCH_YES)
4287 found_id = MATCH_YES;
4288 gfc_get_ha_symbol (name, &tmp_sym);
4290 else if (match_common_name (name) == MATCH_YES)
4292 found_id = MATCH_YES;
4293 com_block = gfc_get_common (name, 0);
4295 else
4297 gfc_error ("Need either entity or common block name for "
4298 "attribute specification statement at %C");
4299 return false;
4302 /* Save the current identifier and look for more. */
4305 /* Increment the number of identifiers found for this spec stmt. */
4306 num_idents++;
4308 /* Make sure we have a sym or com block, and verify that it can
4309 be bind(c). Set the appropriate field(s) and look for more
4310 identifiers. */
4311 if (tmp_sym != NULL || com_block != NULL)
4313 if (tmp_sym != NULL)
4315 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4316 return false;
4318 else
4320 if (!set_verify_bind_c_com_block (com_block, num_idents))
4321 return false;
4324 /* Look to see if we have another identifier. */
4325 tmp_sym = NULL;
4326 if (gfc_match_eos () == MATCH_YES)
4327 found_id = MATCH_NO;
4328 else if (gfc_match_char (',') != MATCH_YES)
4329 found_id = MATCH_NO;
4330 else if (gfc_match_name (name) == MATCH_YES)
4332 found_id = MATCH_YES;
4333 gfc_get_ha_symbol (name, &tmp_sym);
4335 else if (match_common_name (name) == MATCH_YES)
4337 found_id = MATCH_YES;
4338 com_block = gfc_get_common (name, 0);
4340 else
4342 gfc_error ("Missing entity or common block name for "
4343 "attribute specification statement at %C");
4344 return false;
4347 else
4349 gfc_internal_error ("Missing symbol");
4351 } while (found_id == MATCH_YES);
4353 /* if we get here we were successful */
4354 return true;
4358 /* Try and match a BIND(C) attribute specification statement. */
4360 match
4361 gfc_match_bind_c_stmt (void)
4363 match found_match = MATCH_NO;
4364 gfc_typespec *ts;
4366 ts = &current_ts;
4368 /* This may not be necessary. */
4369 gfc_clear_ts (ts);
4370 /* Clear the temporary binding label holder. */
4371 curr_binding_label = NULL;
4373 /* Look for the bind(c). */
4374 found_match = gfc_match_bind_c (NULL, true);
4376 if (found_match == MATCH_YES)
4378 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4379 return MATCH_ERROR;
4381 /* Look for the :: now, but it is not required. */
4382 gfc_match (" :: ");
4384 /* Get the identifier(s) that needs to be updated. This may need to
4385 change to hand the flag(s) for the attr specified so all identifiers
4386 found can have all appropriate parts updated (assuming that the same
4387 spec stmt can have multiple attrs, such as both bind(c) and
4388 allocatable...). */
4389 if (!get_bind_c_idents ())
4390 /* Error message should have printed already. */
4391 return MATCH_ERROR;
4394 return found_match;
4398 /* Match a data declaration statement. */
4400 match
4401 gfc_match_data_decl (void)
4403 gfc_symbol *sym;
4404 match m;
4405 int elem;
4407 num_idents_on_line = 0;
4409 m = gfc_match_decl_type_spec (&current_ts, 0);
4410 if (m != MATCH_YES)
4411 return m;
4413 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4414 && gfc_current_state () != COMP_DERIVED)
4416 sym = gfc_use_derived (current_ts.u.derived);
4418 if (sym == NULL)
4420 m = MATCH_ERROR;
4421 goto cleanup;
4424 current_ts.u.derived = sym;
4427 m = match_attr_spec ();
4428 if (m == MATCH_ERROR)
4430 m = MATCH_NO;
4431 goto cleanup;
4434 if (current_ts.type == BT_CLASS
4435 && current_ts.u.derived->attr.unlimited_polymorphic)
4436 goto ok;
4438 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4439 && current_ts.u.derived->components == NULL
4440 && !current_ts.u.derived->attr.zero_comp)
4443 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4444 goto ok;
4446 gfc_find_symbol (current_ts.u.derived->name,
4447 current_ts.u.derived->ns, 1, &sym);
4449 /* Any symbol that we find had better be a type definition
4450 which has its components defined. */
4451 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4452 && (current_ts.u.derived->components != NULL
4453 || current_ts.u.derived->attr.zero_comp))
4454 goto ok;
4456 gfc_error ("Derived type at %C has not been previously defined "
4457 "and so cannot appear in a derived type definition");
4458 m = MATCH_ERROR;
4459 goto cleanup;
4463 /* If we have an old-style character declaration, and no new-style
4464 attribute specifications, then there a comma is optional between
4465 the type specification and the variable list. */
4466 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4467 gfc_match_char (',');
4469 /* Give the types/attributes to symbols that follow. Give the element
4470 a number so that repeat character length expressions can be copied. */
4471 elem = 1;
4472 for (;;)
4474 num_idents_on_line++;
4475 m = variable_decl (elem++);
4476 if (m == MATCH_ERROR)
4477 goto cleanup;
4478 if (m == MATCH_NO)
4479 break;
4481 if (gfc_match_eos () == MATCH_YES)
4482 goto cleanup;
4483 if (gfc_match_char (',') != MATCH_YES)
4484 break;
4487 if (!gfc_error_flag_test ())
4488 gfc_error ("Syntax error in data declaration at %C");
4489 m = MATCH_ERROR;
4491 gfc_free_data_all (gfc_current_ns);
4493 cleanup:
4494 gfc_free_array_spec (current_as);
4495 current_as = NULL;
4496 return m;
4500 /* Match a prefix associated with a function or subroutine
4501 declaration. If the typespec pointer is nonnull, then a typespec
4502 can be matched. Note that if nothing matches, MATCH_YES is
4503 returned (the null string was matched). */
4505 match
4506 gfc_match_prefix (gfc_typespec *ts)
4508 bool seen_type;
4509 bool seen_impure;
4510 bool found_prefix;
4512 gfc_clear_attr (&current_attr);
4513 seen_type = false;
4514 seen_impure = false;
4516 gcc_assert (!gfc_matching_prefix);
4517 gfc_matching_prefix = true;
4521 found_prefix = false;
4523 if (!seen_type && ts != NULL
4524 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4525 && gfc_match_space () == MATCH_YES)
4528 seen_type = true;
4529 found_prefix = true;
4532 if (gfc_match ("elemental% ") == MATCH_YES)
4534 if (!gfc_add_elemental (&current_attr, NULL))
4535 goto error;
4537 found_prefix = true;
4540 if (gfc_match ("pure% ") == MATCH_YES)
4542 if (!gfc_add_pure (&current_attr, NULL))
4543 goto error;
4545 found_prefix = true;
4548 if (gfc_match ("recursive% ") == MATCH_YES)
4550 if (!gfc_add_recursive (&current_attr, NULL))
4551 goto error;
4553 found_prefix = true;
4556 /* IMPURE is a somewhat special case, as it needs not set an actual
4557 attribute but rather only prevents ELEMENTAL routines from being
4558 automatically PURE. */
4559 if (gfc_match ("impure% ") == MATCH_YES)
4561 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4562 goto error;
4564 seen_impure = true;
4565 found_prefix = true;
4568 while (found_prefix);
4570 /* IMPURE and PURE must not both appear, of course. */
4571 if (seen_impure && current_attr.pure)
4573 gfc_error ("PURE and IMPURE must not appear both at %C");
4574 goto error;
4577 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4578 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4580 if (!gfc_add_pure (&current_attr, NULL))
4581 goto error;
4584 /* At this point, the next item is not a prefix. */
4585 gcc_assert (gfc_matching_prefix);
4587 /* MODULE should be the last prefix before FUNCTION or SUBROUTINE.
4588 Since this is a prefix like PURE, ELEMENTAL, etc., having a
4589 corresponding attribute seems natural and distinguishes these
4590 procedures from procedure types of PROC_MODULE, which these are
4591 as well. */
4592 if ((gfc_current_state () == COMP_INTERFACE
4593 || gfc_current_state () == COMP_CONTAINS)
4594 && gfc_match ("module% ") == MATCH_YES)
4596 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4597 goto error;
4598 else
4599 current_attr.module_procedure = 1;
4602 gfc_matching_prefix = false;
4603 return MATCH_YES;
4605 error:
4606 gcc_assert (gfc_matching_prefix);
4607 gfc_matching_prefix = false;
4608 return MATCH_ERROR;
4612 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4614 static bool
4615 copy_prefix (symbol_attribute *dest, locus *where)
4617 if (current_attr.pure && !gfc_add_pure (dest, where))
4618 return false;
4620 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4621 return false;
4623 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4624 return false;
4626 return true;
4630 /* Match a formal argument list. */
4632 match
4633 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4635 gfc_formal_arglist *head, *tail, *p, *q;
4636 char name[GFC_MAX_SYMBOL_LEN + 1];
4637 gfc_symbol *sym;
4638 match m;
4639 gfc_formal_arglist *formal = NULL;
4641 head = tail = NULL;
4643 /* Keep the interface formal argument list and null it so that the
4644 matching for the new declaration can be done. The numbers and
4645 names of the arguments are checked here. The interface formal
4646 arguments are retained in formal_arglist and the characteristics
4647 are compared in resolve.c(resolve_fl_procedure). See the remark
4648 in get_proc_name about the eventual need to copy the formal_arglist
4649 and populate the formal namespace of the interface symbol. */
4650 if (progname->attr.module_procedure
4651 && progname->attr.host_assoc)
4653 formal = progname->formal;
4654 progname->formal = NULL;
4657 if (gfc_match_char ('(') != MATCH_YES)
4659 if (null_flag)
4660 goto ok;
4661 return MATCH_NO;
4664 if (gfc_match_char (')') == MATCH_YES)
4665 goto ok;
4667 for (;;)
4669 if (gfc_match_char ('*') == MATCH_YES)
4671 sym = NULL;
4672 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4673 "at %C"))
4675 m = MATCH_ERROR;
4676 goto cleanup;
4679 else
4681 m = gfc_match_name (name);
4682 if (m != MATCH_YES)
4683 goto cleanup;
4685 if (gfc_get_symbol (name, NULL, &sym))
4686 goto cleanup;
4689 p = gfc_get_formal_arglist ();
4691 if (head == NULL)
4692 head = tail = p;
4693 else
4695 tail->next = p;
4696 tail = p;
4699 tail->sym = sym;
4701 /* We don't add the VARIABLE flavor because the name could be a
4702 dummy procedure. We don't apply these attributes to formal
4703 arguments of statement functions. */
4704 if (sym != NULL && !st_flag
4705 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4706 || !gfc_missing_attr (&sym->attr, NULL)))
4708 m = MATCH_ERROR;
4709 goto cleanup;
4712 /* The name of a program unit can be in a different namespace,
4713 so check for it explicitly. After the statement is accepted,
4714 the name is checked for especially in gfc_get_symbol(). */
4715 if (gfc_new_block != NULL && sym != NULL
4716 && strcmp (sym->name, gfc_new_block->name) == 0)
4718 gfc_error ("Name %qs at %C is the name of the procedure",
4719 sym->name);
4720 m = MATCH_ERROR;
4721 goto cleanup;
4724 if (gfc_match_char (')') == MATCH_YES)
4725 goto ok;
4727 m = gfc_match_char (',');
4728 if (m != MATCH_YES)
4730 gfc_error ("Unexpected junk in formal argument list at %C");
4731 goto cleanup;
4736 /* Check for duplicate symbols in the formal argument list. */
4737 if (head != NULL)
4739 for (p = head; p->next; p = p->next)
4741 if (p->sym == NULL)
4742 continue;
4744 for (q = p->next; q; q = q->next)
4745 if (p->sym == q->sym)
4747 gfc_error ("Duplicate symbol %qs in formal argument list "
4748 "at %C", p->sym->name);
4750 m = MATCH_ERROR;
4751 goto cleanup;
4756 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4758 m = MATCH_ERROR;
4759 goto cleanup;
4762 if (formal)
4764 for (p = formal, q = head; p && q; p = p->next, q = q->next)
4766 if ((p->next != NULL && q->next == NULL)
4767 || (p->next == NULL && q->next != NULL))
4768 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4769 "formal arguments at %C");
4770 else if ((p->sym == NULL && q->sym == NULL)
4771 || strcmp (p->sym->name, q->sym->name) == 0)
4772 continue;
4773 else
4774 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4775 "argument names (%s/%s) at %C",
4776 p->sym->name, q->sym->name);
4780 return MATCH_YES;
4782 cleanup:
4783 gfc_free_formal_arglist (head);
4784 return m;
4788 /* Match a RESULT specification following a function declaration or
4789 ENTRY statement. Also matches the end-of-statement. */
4791 static match
4792 match_result (gfc_symbol *function, gfc_symbol **result)
4794 char name[GFC_MAX_SYMBOL_LEN + 1];
4795 gfc_symbol *r;
4796 match m;
4798 if (gfc_match (" result (") != MATCH_YES)
4799 return MATCH_NO;
4801 m = gfc_match_name (name);
4802 if (m != MATCH_YES)
4803 return m;
4805 /* Get the right paren, and that's it because there could be the
4806 bind(c) attribute after the result clause. */
4807 if (gfc_match_char (')') != MATCH_YES)
4809 /* TODO: should report the missing right paren here. */
4810 return MATCH_ERROR;
4813 if (strcmp (function->name, name) == 0)
4815 gfc_error ("RESULT variable at %C must be different than function name");
4816 return MATCH_ERROR;
4819 if (gfc_get_symbol (name, NULL, &r))
4820 return MATCH_ERROR;
4822 if (!gfc_add_result (&r->attr, r->name, NULL))
4823 return MATCH_ERROR;
4825 *result = r;
4827 return MATCH_YES;
4831 /* Match a function suffix, which could be a combination of a result
4832 clause and BIND(C), either one, or neither. The draft does not
4833 require them to come in a specific order. */
4835 match
4836 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4838 match is_bind_c; /* Found bind(c). */
4839 match is_result; /* Found result clause. */
4840 match found_match; /* Status of whether we've found a good match. */
4841 char peek_char; /* Character we're going to peek at. */
4842 bool allow_binding_name;
4844 /* Initialize to having found nothing. */
4845 found_match = MATCH_NO;
4846 is_bind_c = MATCH_NO;
4847 is_result = MATCH_NO;
4849 /* Get the next char to narrow between result and bind(c). */
4850 gfc_gobble_whitespace ();
4851 peek_char = gfc_peek_ascii_char ();
4853 /* C binding names are not allowed for internal procedures. */
4854 if (gfc_current_state () == COMP_CONTAINS
4855 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4856 allow_binding_name = false;
4857 else
4858 allow_binding_name = true;
4860 switch (peek_char)
4862 case 'r':
4863 /* Look for result clause. */
4864 is_result = match_result (sym, result);
4865 if (is_result == MATCH_YES)
4867 /* Now see if there is a bind(c) after it. */
4868 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4869 /* We've found the result clause and possibly bind(c). */
4870 found_match = MATCH_YES;
4872 else
4873 /* This should only be MATCH_ERROR. */
4874 found_match = is_result;
4875 break;
4876 case 'b':
4877 /* Look for bind(c) first. */
4878 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4879 if (is_bind_c == MATCH_YES)
4881 /* Now see if a result clause followed it. */
4882 is_result = match_result (sym, result);
4883 found_match = MATCH_YES;
4885 else
4887 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4888 found_match = MATCH_ERROR;
4890 break;
4891 default:
4892 gfc_error ("Unexpected junk after function declaration at %C");
4893 found_match = MATCH_ERROR;
4894 break;
4897 if (is_bind_c == MATCH_YES)
4899 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4900 if (gfc_current_state () == COMP_CONTAINS
4901 && sym->ns->proc_name->attr.flavor != FL_MODULE
4902 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4903 "at %L may not be specified for an internal "
4904 "procedure", &gfc_current_locus))
4905 return MATCH_ERROR;
4907 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4908 return MATCH_ERROR;
4911 return found_match;
4915 /* Procedure pointer return value without RESULT statement:
4916 Add "hidden" result variable named "ppr@". */
4918 static bool
4919 add_hidden_procptr_result (gfc_symbol *sym)
4921 bool case1,case2;
4923 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4924 return false;
4926 /* First usage case: PROCEDURE and EXTERNAL statements. */
4927 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4928 && strcmp (gfc_current_block ()->name, sym->name) == 0
4929 && sym->attr.external;
4930 /* Second usage case: INTERFACE statements. */
4931 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4932 && gfc_state_stack->previous->state == COMP_FUNCTION
4933 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4935 if (case1 || case2)
4937 gfc_symtree *stree;
4938 if (case1)
4939 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4940 else if (case2)
4942 gfc_symtree *st2;
4943 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4944 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4945 st2->n.sym = stree->n.sym;
4947 sym->result = stree->n.sym;
4949 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4950 sym->result->attr.pointer = sym->attr.pointer;
4951 sym->result->attr.external = sym->attr.external;
4952 sym->result->attr.referenced = sym->attr.referenced;
4953 sym->result->ts = sym->ts;
4954 sym->attr.proc_pointer = 0;
4955 sym->attr.pointer = 0;
4956 sym->attr.external = 0;
4957 if (sym->result->attr.external && sym->result->attr.pointer)
4959 sym->result->attr.pointer = 0;
4960 sym->result->attr.proc_pointer = 1;
4963 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4965 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4966 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4967 && sym->result && sym->result != sym && sym->result->attr.external
4968 && sym == gfc_current_ns->proc_name
4969 && sym == sym->result->ns->proc_name
4970 && strcmp ("ppr@", sym->result->name) == 0)
4972 sym->result->attr.proc_pointer = 1;
4973 sym->attr.pointer = 0;
4974 return true;
4976 else
4977 return false;
4981 /* Match the interface for a PROCEDURE declaration,
4982 including brackets (R1212). */
4984 static match
4985 match_procedure_interface (gfc_symbol **proc_if)
4987 match m;
4988 gfc_symtree *st;
4989 locus old_loc, entry_loc;
4990 gfc_namespace *old_ns = gfc_current_ns;
4991 char name[GFC_MAX_SYMBOL_LEN + 1];
4993 old_loc = entry_loc = gfc_current_locus;
4994 gfc_clear_ts (&current_ts);
4996 if (gfc_match (" (") != MATCH_YES)
4998 gfc_current_locus = entry_loc;
4999 return MATCH_NO;
5002 /* Get the type spec. for the procedure interface. */
5003 old_loc = gfc_current_locus;
5004 m = gfc_match_decl_type_spec (&current_ts, 0);
5005 gfc_gobble_whitespace ();
5006 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
5007 goto got_ts;
5009 if (m == MATCH_ERROR)
5010 return m;
5012 /* Procedure interface is itself a procedure. */
5013 gfc_current_locus = old_loc;
5014 m = gfc_match_name (name);
5016 /* First look to see if it is already accessible in the current
5017 namespace because it is use associated or contained. */
5018 st = NULL;
5019 if (gfc_find_sym_tree (name, NULL, 0, &st))
5020 return MATCH_ERROR;
5022 /* If it is still not found, then try the parent namespace, if it
5023 exists and create the symbol there if it is still not found. */
5024 if (gfc_current_ns->parent)
5025 gfc_current_ns = gfc_current_ns->parent;
5026 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5027 return MATCH_ERROR;
5029 gfc_current_ns = old_ns;
5030 *proc_if = st->n.sym;
5032 if (*proc_if)
5034 (*proc_if)->refs++;
5035 /* Resolve interface if possible. That way, attr.procedure is only set
5036 if it is declared by a later procedure-declaration-stmt, which is
5037 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5038 while ((*proc_if)->ts.interface)
5039 *proc_if = (*proc_if)->ts.interface;
5041 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5042 && (*proc_if)->ts.type == BT_UNKNOWN
5043 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5044 (*proc_if)->name, NULL))
5045 return MATCH_ERROR;
5048 got_ts:
5049 if (gfc_match (" )") != MATCH_YES)
5051 gfc_current_locus = entry_loc;
5052 return MATCH_NO;
5055 return MATCH_YES;
5059 /* Match a PROCEDURE declaration (R1211). */
5061 static match
5062 match_procedure_decl (void)
5064 match m;
5065 gfc_symbol *sym, *proc_if = NULL;
5066 int num;
5067 gfc_expr *initializer = NULL;
5069 /* Parse interface (with brackets). */
5070 m = match_procedure_interface (&proc_if);
5071 if (m != MATCH_YES)
5072 return m;
5074 /* Parse attributes (with colons). */
5075 m = match_attr_spec();
5076 if (m == MATCH_ERROR)
5077 return MATCH_ERROR;
5079 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5081 current_attr.is_bind_c = 1;
5082 has_name_equals = 0;
5083 curr_binding_label = NULL;
5086 /* Get procedure symbols. */
5087 for(num=1;;num++)
5089 m = gfc_match_symbol (&sym, 0);
5090 if (m == MATCH_NO)
5091 goto syntax;
5092 else if (m == MATCH_ERROR)
5093 return m;
5095 /* Add current_attr to the symbol attributes. */
5096 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5097 return MATCH_ERROR;
5099 if (sym->attr.is_bind_c)
5101 /* Check for C1218. */
5102 if (!proc_if || !proc_if->attr.is_bind_c)
5104 gfc_error ("BIND(C) attribute at %C requires "
5105 "an interface with BIND(C)");
5106 return MATCH_ERROR;
5108 /* Check for C1217. */
5109 if (has_name_equals && sym->attr.pointer)
5111 gfc_error ("BIND(C) procedure with NAME may not have "
5112 "POINTER attribute at %C");
5113 return MATCH_ERROR;
5115 if (has_name_equals && sym->attr.dummy)
5117 gfc_error ("Dummy procedure at %C may not have "
5118 "BIND(C) attribute with NAME");
5119 return MATCH_ERROR;
5121 /* Set binding label for BIND(C). */
5122 if (!set_binding_label (&sym->binding_label, sym->name, num))
5123 return MATCH_ERROR;
5126 if (!gfc_add_external (&sym->attr, NULL))
5127 return MATCH_ERROR;
5129 if (add_hidden_procptr_result (sym))
5130 sym = sym->result;
5132 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5133 return MATCH_ERROR;
5135 /* Set interface. */
5136 if (proc_if != NULL)
5138 if (sym->ts.type != BT_UNKNOWN)
5140 gfc_error ("Procedure %qs at %L already has basic type of %s",
5141 sym->name, &gfc_current_locus,
5142 gfc_basic_typename (sym->ts.type));
5143 return MATCH_ERROR;
5145 sym->ts.interface = proc_if;
5146 sym->attr.untyped = 1;
5147 sym->attr.if_source = IFSRC_IFBODY;
5149 else if (current_ts.type != BT_UNKNOWN)
5151 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5152 return MATCH_ERROR;
5153 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5154 sym->ts.interface->ts = current_ts;
5155 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5156 sym->ts.interface->attr.function = 1;
5157 sym->attr.function = 1;
5158 sym->attr.if_source = IFSRC_UNKNOWN;
5161 if (gfc_match (" =>") == MATCH_YES)
5163 if (!current_attr.pointer)
5165 gfc_error ("Initialization at %C isn't for a pointer variable");
5166 m = MATCH_ERROR;
5167 goto cleanup;
5170 m = match_pointer_init (&initializer, 1);
5171 if (m != MATCH_YES)
5172 goto cleanup;
5174 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5175 goto cleanup;
5179 if (gfc_match_eos () == MATCH_YES)
5180 return MATCH_YES;
5181 if (gfc_match_char (',') != MATCH_YES)
5182 goto syntax;
5185 syntax:
5186 gfc_error ("Syntax error in PROCEDURE statement at %C");
5187 return MATCH_ERROR;
5189 cleanup:
5190 /* Free stuff up and return. */
5191 gfc_free_expr (initializer);
5192 return m;
5196 static match
5197 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5200 /* Match a procedure pointer component declaration (R445). */
5202 static match
5203 match_ppc_decl (void)
5205 match m;
5206 gfc_symbol *proc_if = NULL;
5207 gfc_typespec ts;
5208 int num;
5209 gfc_component *c;
5210 gfc_expr *initializer = NULL;
5211 gfc_typebound_proc* tb;
5212 char name[GFC_MAX_SYMBOL_LEN + 1];
5214 /* Parse interface (with brackets). */
5215 m = match_procedure_interface (&proc_if);
5216 if (m != MATCH_YES)
5217 goto syntax;
5219 /* Parse attributes. */
5220 tb = XCNEW (gfc_typebound_proc);
5221 tb->where = gfc_current_locus;
5222 m = match_binding_attributes (tb, false, true);
5223 if (m == MATCH_ERROR)
5224 return m;
5226 gfc_clear_attr (&current_attr);
5227 current_attr.procedure = 1;
5228 current_attr.proc_pointer = 1;
5229 current_attr.access = tb->access;
5230 current_attr.flavor = FL_PROCEDURE;
5232 /* Match the colons (required). */
5233 if (gfc_match (" ::") != MATCH_YES)
5235 gfc_error ("Expected %<::%> after binding-attributes at %C");
5236 return MATCH_ERROR;
5239 /* Check for C450. */
5240 if (!tb->nopass && proc_if == NULL)
5242 gfc_error("NOPASS or explicit interface required at %C");
5243 return MATCH_ERROR;
5246 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5247 return MATCH_ERROR;
5249 /* Match PPC names. */
5250 ts = current_ts;
5251 for(num=1;;num++)
5253 m = gfc_match_name (name);
5254 if (m == MATCH_NO)
5255 goto syntax;
5256 else if (m == MATCH_ERROR)
5257 return m;
5259 if (!gfc_add_component (gfc_current_block(), name, &c))
5260 return MATCH_ERROR;
5262 /* Add current_attr to the symbol attributes. */
5263 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5264 return MATCH_ERROR;
5266 if (!gfc_add_external (&c->attr, NULL))
5267 return MATCH_ERROR;
5269 if (!gfc_add_proc (&c->attr, name, NULL))
5270 return MATCH_ERROR;
5272 if (num == 1)
5273 c->tb = tb;
5274 else
5276 c->tb = XCNEW (gfc_typebound_proc);
5277 c->tb->where = gfc_current_locus;
5278 *c->tb = *tb;
5281 /* Set interface. */
5282 if (proc_if != NULL)
5284 c->ts.interface = proc_if;
5285 c->attr.untyped = 1;
5286 c->attr.if_source = IFSRC_IFBODY;
5288 else if (ts.type != BT_UNKNOWN)
5290 c->ts = ts;
5291 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5292 c->ts.interface->result = c->ts.interface;
5293 c->ts.interface->ts = ts;
5294 c->ts.interface->attr.flavor = FL_PROCEDURE;
5295 c->ts.interface->attr.function = 1;
5296 c->attr.function = 1;
5297 c->attr.if_source = IFSRC_UNKNOWN;
5300 if (gfc_match (" =>") == MATCH_YES)
5302 m = match_pointer_init (&initializer, 1);
5303 if (m != MATCH_YES)
5305 gfc_free_expr (initializer);
5306 return m;
5308 c->initializer = initializer;
5311 if (gfc_match_eos () == MATCH_YES)
5312 return MATCH_YES;
5313 if (gfc_match_char (',') != MATCH_YES)
5314 goto syntax;
5317 syntax:
5318 gfc_error ("Syntax error in procedure pointer component at %C");
5319 return MATCH_ERROR;
5323 /* Match a PROCEDURE declaration inside an interface (R1206). */
5325 static match
5326 match_procedure_in_interface (void)
5328 match m;
5329 gfc_symbol *sym;
5330 char name[GFC_MAX_SYMBOL_LEN + 1];
5331 locus old_locus;
5333 if (current_interface.type == INTERFACE_NAMELESS
5334 || current_interface.type == INTERFACE_ABSTRACT)
5336 gfc_error ("PROCEDURE at %C must be in a generic interface");
5337 return MATCH_ERROR;
5340 /* Check if the F2008 optional double colon appears. */
5341 gfc_gobble_whitespace ();
5342 old_locus = gfc_current_locus;
5343 if (gfc_match ("::") == MATCH_YES)
5345 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5346 "MODULE PROCEDURE statement at %L", &old_locus))
5347 return MATCH_ERROR;
5349 else
5350 gfc_current_locus = old_locus;
5352 for(;;)
5354 m = gfc_match_name (name);
5355 if (m == MATCH_NO)
5356 goto syntax;
5357 else if (m == MATCH_ERROR)
5358 return m;
5359 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5360 return MATCH_ERROR;
5362 if (!gfc_add_interface (sym))
5363 return MATCH_ERROR;
5365 if (gfc_match_eos () == MATCH_YES)
5366 break;
5367 if (gfc_match_char (',') != MATCH_YES)
5368 goto syntax;
5371 return MATCH_YES;
5373 syntax:
5374 gfc_error ("Syntax error in PROCEDURE statement at %C");
5375 return MATCH_ERROR;
5379 /* General matcher for PROCEDURE declarations. */
5381 static match match_procedure_in_type (void);
5383 match
5384 gfc_match_procedure (void)
5386 match m;
5388 switch (gfc_current_state ())
5390 case COMP_NONE:
5391 case COMP_PROGRAM:
5392 case COMP_MODULE:
5393 case COMP_SUBMODULE:
5394 case COMP_SUBROUTINE:
5395 case COMP_FUNCTION:
5396 case COMP_BLOCK:
5397 m = match_procedure_decl ();
5398 break;
5399 case COMP_INTERFACE:
5400 m = match_procedure_in_interface ();
5401 break;
5402 case COMP_DERIVED:
5403 m = match_ppc_decl ();
5404 break;
5405 case COMP_DERIVED_CONTAINS:
5406 m = match_procedure_in_type ();
5407 break;
5408 default:
5409 return MATCH_NO;
5412 if (m != MATCH_YES)
5413 return m;
5415 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5416 return MATCH_ERROR;
5418 return m;
5422 /* Warn if a matched procedure has the same name as an intrinsic; this is
5423 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5424 parser-state-stack to find out whether we're in a module. */
5426 static void
5427 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5429 bool in_module;
5431 in_module = (gfc_state_stack->previous
5432 && (gfc_state_stack->previous->state == COMP_MODULE
5433 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5435 gfc_warn_intrinsic_shadow (sym, in_module, func);
5439 /* Match a function declaration. */
5441 match
5442 gfc_match_function_decl (void)
5444 char name[GFC_MAX_SYMBOL_LEN + 1];
5445 gfc_symbol *sym, *result;
5446 locus old_loc;
5447 match m;
5448 match suffix_match;
5449 match found_match; /* Status returned by match func. */
5451 if (gfc_current_state () != COMP_NONE
5452 && gfc_current_state () != COMP_INTERFACE
5453 && gfc_current_state () != COMP_CONTAINS)
5454 return MATCH_NO;
5456 gfc_clear_ts (&current_ts);
5458 old_loc = gfc_current_locus;
5460 m = gfc_match_prefix (&current_ts);
5461 if (m != MATCH_YES)
5463 gfc_current_locus = old_loc;
5464 return m;
5467 if (gfc_match ("function% %n", name) != MATCH_YES)
5469 gfc_current_locus = old_loc;
5470 return MATCH_NO;
5473 if (get_proc_name (name, &sym, false))
5474 return MATCH_ERROR;
5476 if (add_hidden_procptr_result (sym))
5477 sym = sym->result;
5479 if (current_attr.module_procedure)
5480 sym->attr.module_procedure = 1;
5482 gfc_new_block = sym;
5484 m = gfc_match_formal_arglist (sym, 0, 0);
5485 if (m == MATCH_NO)
5487 gfc_error ("Expected formal argument list in function "
5488 "definition at %C");
5489 m = MATCH_ERROR;
5490 goto cleanup;
5492 else if (m == MATCH_ERROR)
5493 goto cleanup;
5495 result = NULL;
5497 /* According to the draft, the bind(c) and result clause can
5498 come in either order after the formal_arg_list (i.e., either
5499 can be first, both can exist together or by themselves or neither
5500 one). Therefore, the match_result can't match the end of the
5501 string, and check for the bind(c) or result clause in either order. */
5502 found_match = gfc_match_eos ();
5504 /* Make sure that it isn't already declared as BIND(C). If it is, it
5505 must have been marked BIND(C) with a BIND(C) attribute and that is
5506 not allowed for procedures. */
5507 if (sym->attr.is_bind_c == 1)
5509 sym->attr.is_bind_c = 0;
5510 if (sym->old_symbol != NULL)
5511 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5512 "variables or common blocks",
5513 &(sym->old_symbol->declared_at));
5514 else
5515 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5516 "variables or common blocks", &gfc_current_locus);
5519 if (found_match != MATCH_YES)
5521 /* If we haven't found the end-of-statement, look for a suffix. */
5522 suffix_match = gfc_match_suffix (sym, &result);
5523 if (suffix_match == MATCH_YES)
5524 /* Need to get the eos now. */
5525 found_match = gfc_match_eos ();
5526 else
5527 found_match = suffix_match;
5530 if(found_match != MATCH_YES)
5531 m = MATCH_ERROR;
5532 else
5534 /* Make changes to the symbol. */
5535 m = MATCH_ERROR;
5537 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5538 goto cleanup;
5540 if (!gfc_missing_attr (&sym->attr, NULL)
5541 || !copy_prefix (&sym->attr, &sym->declared_at))
5542 goto cleanup;
5544 /* Delay matching the function characteristics until after the
5545 specification block by signalling kind=-1. */
5546 sym->declared_at = old_loc;
5547 if (current_ts.type != BT_UNKNOWN)
5548 current_ts.kind = -1;
5549 else
5550 current_ts.kind = 0;
5552 if (result == NULL)
5554 if (current_ts.type != BT_UNKNOWN
5555 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5556 goto cleanup;
5557 sym->result = sym;
5559 else
5561 if (current_ts.type != BT_UNKNOWN
5562 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5563 goto cleanup;
5564 sym->result = result;
5567 /* Warn if this procedure has the same name as an intrinsic. */
5568 do_warn_intrinsic_shadow (sym, true);
5570 return MATCH_YES;
5573 cleanup:
5574 gfc_current_locus = old_loc;
5575 return m;
5579 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5580 pass the name of the entry, rather than the gfc_current_block name, and
5581 to return false upon finding an existing global entry. */
5583 static bool
5584 add_global_entry (const char *name, const char *binding_label, bool sub,
5585 locus *where)
5587 gfc_gsymbol *s;
5588 enum gfc_symbol_type type;
5590 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5592 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5593 name is a global identifier. */
5594 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5596 s = gfc_get_gsymbol (name);
5598 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5600 gfc_global_used (s, where);
5601 return false;
5603 else
5605 s->type = type;
5606 s->sym_name = name;
5607 s->where = *where;
5608 s->defined = 1;
5609 s->ns = gfc_current_ns;
5613 /* Don't add the symbol multiple times. */
5614 if (binding_label
5615 && (!gfc_notification_std (GFC_STD_F2008)
5616 || strcmp (name, binding_label) != 0))
5618 s = gfc_get_gsymbol (binding_label);
5620 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5622 gfc_global_used (s, where);
5623 return false;
5625 else
5627 s->type = type;
5628 s->sym_name = name;
5629 s->binding_label = binding_label;
5630 s->where = *where;
5631 s->defined = 1;
5632 s->ns = gfc_current_ns;
5636 return true;
5640 /* Match an ENTRY statement. */
5642 match
5643 gfc_match_entry (void)
5645 gfc_symbol *proc;
5646 gfc_symbol *result;
5647 gfc_symbol *entry;
5648 char name[GFC_MAX_SYMBOL_LEN + 1];
5649 gfc_compile_state state;
5650 match m;
5651 gfc_entry_list *el;
5652 locus old_loc;
5653 bool module_procedure;
5654 char peek_char;
5655 match is_bind_c;
5657 m = gfc_match_name (name);
5658 if (m != MATCH_YES)
5659 return m;
5661 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5662 return MATCH_ERROR;
5664 state = gfc_current_state ();
5665 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5667 switch (state)
5669 case COMP_PROGRAM:
5670 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5671 break;
5672 case COMP_MODULE:
5673 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5674 break;
5675 case COMP_SUBMODULE:
5676 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5677 break;
5678 case COMP_BLOCK_DATA:
5679 gfc_error ("ENTRY statement at %C cannot appear within "
5680 "a BLOCK DATA");
5681 break;
5682 case COMP_INTERFACE:
5683 gfc_error ("ENTRY statement at %C cannot appear within "
5684 "an INTERFACE");
5685 break;
5686 case COMP_DERIVED:
5687 gfc_error ("ENTRY statement at %C cannot appear within "
5688 "a DERIVED TYPE block");
5689 break;
5690 case COMP_IF:
5691 gfc_error ("ENTRY statement at %C cannot appear within "
5692 "an IF-THEN block");
5693 break;
5694 case COMP_DO:
5695 case COMP_DO_CONCURRENT:
5696 gfc_error ("ENTRY statement at %C cannot appear within "
5697 "a DO block");
5698 break;
5699 case COMP_SELECT:
5700 gfc_error ("ENTRY statement at %C cannot appear within "
5701 "a SELECT block");
5702 break;
5703 case COMP_FORALL:
5704 gfc_error ("ENTRY statement at %C cannot appear within "
5705 "a FORALL block");
5706 break;
5707 case COMP_WHERE:
5708 gfc_error ("ENTRY statement at %C cannot appear within "
5709 "a WHERE block");
5710 break;
5711 case COMP_CONTAINS:
5712 gfc_error ("ENTRY statement at %C cannot appear within "
5713 "a contained subprogram");
5714 break;
5715 default:
5716 gfc_error ("Unexpected ENTRY statement at %C");
5718 return MATCH_ERROR;
5721 module_procedure = gfc_current_ns->parent != NULL
5722 && gfc_current_ns->parent->proc_name
5723 && gfc_current_ns->parent->proc_name->attr.flavor
5724 == FL_MODULE;
5726 if (gfc_current_ns->parent != NULL
5727 && gfc_current_ns->parent->proc_name
5728 && !module_procedure)
5730 gfc_error("ENTRY statement at %C cannot appear in a "
5731 "contained procedure");
5732 return MATCH_ERROR;
5735 /* Module function entries need special care in get_proc_name
5736 because previous references within the function will have
5737 created symbols attached to the current namespace. */
5738 if (get_proc_name (name, &entry,
5739 gfc_current_ns->parent != NULL
5740 && module_procedure))
5741 return MATCH_ERROR;
5743 proc = gfc_current_block ();
5745 /* Make sure that it isn't already declared as BIND(C). If it is, it
5746 must have been marked BIND(C) with a BIND(C) attribute and that is
5747 not allowed for procedures. */
5748 if (entry->attr.is_bind_c == 1)
5750 entry->attr.is_bind_c = 0;
5751 if (entry->old_symbol != NULL)
5752 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5753 "variables or common blocks",
5754 &(entry->old_symbol->declared_at));
5755 else
5756 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5757 "variables or common blocks", &gfc_current_locus);
5760 /* Check what next non-whitespace character is so we can tell if there
5761 is the required parens if we have a BIND(C). */
5762 old_loc = gfc_current_locus;
5763 gfc_gobble_whitespace ();
5764 peek_char = gfc_peek_ascii_char ();
5766 if (state == COMP_SUBROUTINE)
5768 m = gfc_match_formal_arglist (entry, 0, 1);
5769 if (m != MATCH_YES)
5770 return MATCH_ERROR;
5772 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5773 never be an internal procedure. */
5774 is_bind_c = gfc_match_bind_c (entry, true);
5775 if (is_bind_c == MATCH_ERROR)
5776 return MATCH_ERROR;
5777 if (is_bind_c == MATCH_YES)
5779 if (peek_char != '(')
5781 gfc_error ("Missing required parentheses before BIND(C) at %C");
5782 return MATCH_ERROR;
5784 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5785 &(entry->declared_at), 1))
5786 return MATCH_ERROR;
5789 if (!gfc_current_ns->parent
5790 && !add_global_entry (name, entry->binding_label, true,
5791 &old_loc))
5792 return MATCH_ERROR;
5794 /* An entry in a subroutine. */
5795 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5796 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5797 return MATCH_ERROR;
5799 else
5801 /* An entry in a function.
5802 We need to take special care because writing
5803 ENTRY f()
5805 ENTRY f
5806 is allowed, whereas
5807 ENTRY f() RESULT (r)
5808 can't be written as
5809 ENTRY f RESULT (r). */
5810 if (gfc_match_eos () == MATCH_YES)
5812 gfc_current_locus = old_loc;
5813 /* Match the empty argument list, and add the interface to
5814 the symbol. */
5815 m = gfc_match_formal_arglist (entry, 0, 1);
5817 else
5818 m = gfc_match_formal_arglist (entry, 0, 0);
5820 if (m != MATCH_YES)
5821 return MATCH_ERROR;
5823 result = NULL;
5825 if (gfc_match_eos () == MATCH_YES)
5827 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5828 || !gfc_add_function (&entry->attr, entry->name, NULL))
5829 return MATCH_ERROR;
5831 entry->result = entry;
5833 else
5835 m = gfc_match_suffix (entry, &result);
5836 if (m == MATCH_NO)
5837 gfc_syntax_error (ST_ENTRY);
5838 if (m != MATCH_YES)
5839 return MATCH_ERROR;
5841 if (result)
5843 if (!gfc_add_result (&result->attr, result->name, NULL)
5844 || !gfc_add_entry (&entry->attr, result->name, NULL)
5845 || !gfc_add_function (&entry->attr, result->name, NULL))
5846 return MATCH_ERROR;
5847 entry->result = result;
5849 else
5851 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5852 || !gfc_add_function (&entry->attr, entry->name, NULL))
5853 return MATCH_ERROR;
5854 entry->result = entry;
5858 if (!gfc_current_ns->parent
5859 && !add_global_entry (name, entry->binding_label, false,
5860 &old_loc))
5861 return MATCH_ERROR;
5864 if (gfc_match_eos () != MATCH_YES)
5866 gfc_syntax_error (ST_ENTRY);
5867 return MATCH_ERROR;
5870 entry->attr.recursive = proc->attr.recursive;
5871 entry->attr.elemental = proc->attr.elemental;
5872 entry->attr.pure = proc->attr.pure;
5874 el = gfc_get_entry_list ();
5875 el->sym = entry;
5876 el->next = gfc_current_ns->entries;
5877 gfc_current_ns->entries = el;
5878 if (el->next)
5879 el->id = el->next->id + 1;
5880 else
5881 el->id = 1;
5883 new_st.op = EXEC_ENTRY;
5884 new_st.ext.entry = el;
5886 return MATCH_YES;
5890 /* Match a subroutine statement, including optional prefixes. */
5892 match
5893 gfc_match_subroutine (void)
5895 char name[GFC_MAX_SYMBOL_LEN + 1];
5896 gfc_symbol *sym;
5897 match m;
5898 match is_bind_c;
5899 char peek_char;
5900 bool allow_binding_name;
5902 if (gfc_current_state () != COMP_NONE
5903 && gfc_current_state () != COMP_INTERFACE
5904 && gfc_current_state () != COMP_CONTAINS)
5905 return MATCH_NO;
5907 m = gfc_match_prefix (NULL);
5908 if (m != MATCH_YES)
5909 return m;
5911 m = gfc_match ("subroutine% %n", name);
5912 if (m != MATCH_YES)
5913 return m;
5915 if (get_proc_name (name, &sym, false))
5916 return MATCH_ERROR;
5918 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5919 the symbol existed before. */
5920 sym->declared_at = gfc_current_locus;
5922 if (current_attr.module_procedure)
5923 sym->attr.module_procedure = 1;
5925 if (add_hidden_procptr_result (sym))
5926 sym = sym->result;
5928 gfc_new_block = sym;
5930 /* Check what next non-whitespace character is so we can tell if there
5931 is the required parens if we have a BIND(C). */
5932 gfc_gobble_whitespace ();
5933 peek_char = gfc_peek_ascii_char ();
5935 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5936 return MATCH_ERROR;
5938 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5939 return MATCH_ERROR;
5941 /* Make sure that it isn't already declared as BIND(C). If it is, it
5942 must have been marked BIND(C) with a BIND(C) attribute and that is
5943 not allowed for procedures. */
5944 if (sym->attr.is_bind_c == 1)
5946 sym->attr.is_bind_c = 0;
5947 if (sym->old_symbol != NULL)
5948 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5949 "variables or common blocks",
5950 &(sym->old_symbol->declared_at));
5951 else
5952 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5953 "variables or common blocks", &gfc_current_locus);
5956 /* C binding names are not allowed for internal procedures. */
5957 if (gfc_current_state () == COMP_CONTAINS
5958 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5959 allow_binding_name = false;
5960 else
5961 allow_binding_name = true;
5963 /* Here, we are just checking if it has the bind(c) attribute, and if
5964 so, then we need to make sure it's all correct. If it doesn't,
5965 we still need to continue matching the rest of the subroutine line. */
5966 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5967 if (is_bind_c == MATCH_ERROR)
5969 /* There was an attempt at the bind(c), but it was wrong. An
5970 error message should have been printed w/in the gfc_match_bind_c
5971 so here we'll just return the MATCH_ERROR. */
5972 return MATCH_ERROR;
5975 if (is_bind_c == MATCH_YES)
5977 /* The following is allowed in the Fortran 2008 draft. */
5978 if (gfc_current_state () == COMP_CONTAINS
5979 && sym->ns->proc_name->attr.flavor != FL_MODULE
5980 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5981 "at %L may not be specified for an internal "
5982 "procedure", &gfc_current_locus))
5983 return MATCH_ERROR;
5985 if (peek_char != '(')
5987 gfc_error ("Missing required parentheses before BIND(C) at %C");
5988 return MATCH_ERROR;
5990 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5991 &(sym->declared_at), 1))
5992 return MATCH_ERROR;
5995 if (gfc_match_eos () != MATCH_YES)
5997 gfc_syntax_error (ST_SUBROUTINE);
5998 return MATCH_ERROR;
6001 if (!copy_prefix (&sym->attr, &sym->declared_at))
6002 return MATCH_ERROR;
6004 /* Warn if it has the same name as an intrinsic. */
6005 do_warn_intrinsic_shadow (sym, false);
6007 return MATCH_YES;
6011 /* Check that the NAME identifier in a BIND attribute or statement
6012 is conform to C identifier rules. */
6014 match
6015 check_bind_name_identifier (char **name)
6017 char *n = *name, *p;
6019 /* Remove leading spaces. */
6020 while (*n == ' ')
6021 n++;
6023 /* On an empty string, free memory and set name to NULL. */
6024 if (*n == '\0')
6026 free (*name);
6027 *name = NULL;
6028 return MATCH_YES;
6031 /* Remove trailing spaces. */
6032 p = n + strlen(n) - 1;
6033 while (*p == ' ')
6034 *(p--) = '\0';
6036 /* Insert the identifier into the symbol table. */
6037 p = xstrdup (n);
6038 free (*name);
6039 *name = p;
6041 /* Now check that identifier is valid under C rules. */
6042 if (ISDIGIT (*p))
6044 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6045 return MATCH_ERROR;
6048 for (; *p; p++)
6049 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6051 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6052 return MATCH_ERROR;
6055 return MATCH_YES;
6059 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6060 given, and set the binding label in either the given symbol (if not
6061 NULL), or in the current_ts. The symbol may be NULL because we may
6062 encounter the BIND(C) before the declaration itself. Return
6063 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6064 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6065 or MATCH_YES if the specifier was correct and the binding label and
6066 bind(c) fields were set correctly for the given symbol or the
6067 current_ts. If allow_binding_name is false, no binding name may be
6068 given. */
6070 match
6071 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6073 char *binding_label = NULL;
6074 gfc_expr *e = NULL;
6076 /* Initialize the flag that specifies whether we encountered a NAME=
6077 specifier or not. */
6078 has_name_equals = 0;
6080 /* This much we have to be able to match, in this order, if
6081 there is a bind(c) label. */
6082 if (gfc_match (" bind ( c ") != MATCH_YES)
6083 return MATCH_NO;
6085 /* Now see if there is a binding label, or if we've reached the
6086 end of the bind(c) attribute without one. */
6087 if (gfc_match_char (',') == MATCH_YES)
6089 if (gfc_match (" name = ") != MATCH_YES)
6091 gfc_error ("Syntax error in NAME= specifier for binding label "
6092 "at %C");
6093 /* should give an error message here */
6094 return MATCH_ERROR;
6097 has_name_equals = 1;
6099 if (gfc_match_init_expr (&e) != MATCH_YES)
6101 gfc_free_expr (e);
6102 return MATCH_ERROR;
6105 if (!gfc_simplify_expr(e, 0))
6107 gfc_error ("NAME= specifier at %C should be a constant expression");
6108 gfc_free_expr (e);
6109 return MATCH_ERROR;
6112 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6113 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6115 gfc_error ("NAME= specifier at %C should be a scalar of "
6116 "default character kind");
6117 gfc_free_expr(e);
6118 return MATCH_ERROR;
6121 // Get a C string from the Fortran string constant
6122 binding_label = gfc_widechar_to_char (e->value.character.string,
6123 e->value.character.length);
6124 gfc_free_expr(e);
6126 // Check that it is valid (old gfc_match_name_C)
6127 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6128 return MATCH_ERROR;
6131 /* Get the required right paren. */
6132 if (gfc_match_char (')') != MATCH_YES)
6134 gfc_error ("Missing closing paren for binding label at %C");
6135 return MATCH_ERROR;
6138 if (has_name_equals && !allow_binding_name)
6140 gfc_error ("No binding name is allowed in BIND(C) at %C");
6141 return MATCH_ERROR;
6144 if (has_name_equals && sym != NULL && sym->attr.dummy)
6146 gfc_error ("For dummy procedure %s, no binding name is "
6147 "allowed in BIND(C) at %C", sym->name);
6148 return MATCH_ERROR;
6152 /* Save the binding label to the symbol. If sym is null, we're
6153 probably matching the typespec attributes of a declaration and
6154 haven't gotten the name yet, and therefore, no symbol yet. */
6155 if (binding_label)
6157 if (sym != NULL)
6158 sym->binding_label = binding_label;
6159 else
6160 curr_binding_label = binding_label;
6162 else if (allow_binding_name)
6164 /* No binding label, but if symbol isn't null, we
6165 can set the label for it here.
6166 If name="" or allow_binding_name is false, no C binding name is
6167 created. */
6168 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6169 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6172 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6173 && current_interface.type == INTERFACE_ABSTRACT)
6175 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6176 return MATCH_ERROR;
6179 return MATCH_YES;
6183 /* Return nonzero if we're currently compiling a contained procedure. */
6185 static int
6186 contained_procedure (void)
6188 gfc_state_data *s = gfc_state_stack;
6190 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6191 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6192 return 1;
6194 return 0;
6197 /* Set the kind of each enumerator. The kind is selected such that it is
6198 interoperable with the corresponding C enumeration type, making
6199 sure that -fshort-enums is honored. */
6201 static void
6202 set_enum_kind(void)
6204 enumerator_history *current_history = NULL;
6205 int kind;
6206 int i;
6208 if (max_enum == NULL || enum_history == NULL)
6209 return;
6211 if (!flag_short_enums)
6212 return;
6214 i = 0;
6217 kind = gfc_integer_kinds[i++].kind;
6219 while (kind < gfc_c_int_kind
6220 && gfc_check_integer_range (max_enum->initializer->value.integer,
6221 kind) != ARITH_OK);
6223 current_history = enum_history;
6224 while (current_history != NULL)
6226 current_history->sym->ts.kind = kind;
6227 current_history = current_history->next;
6232 /* Match any of the various end-block statements. Returns the type of
6233 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6234 and END BLOCK statements cannot be replaced by a single END statement. */
6236 match
6237 gfc_match_end (gfc_statement *st)
6239 char name[GFC_MAX_SYMBOL_LEN + 1];
6240 gfc_compile_state state;
6241 locus old_loc;
6242 const char *block_name;
6243 const char *target;
6244 int eos_ok;
6245 match m;
6246 gfc_namespace *parent_ns, *ns, *prev_ns;
6247 gfc_namespace **nsp;
6248 bool abreviated_modproc_decl;
6250 old_loc = gfc_current_locus;
6251 if (gfc_match ("end") != MATCH_YES)
6252 return MATCH_NO;
6254 state = gfc_current_state ();
6255 block_name = gfc_current_block () == NULL
6256 ? NULL : gfc_current_block ()->name;
6258 switch (state)
6260 case COMP_ASSOCIATE:
6261 case COMP_BLOCK:
6262 if (!strncmp (block_name, "block@", strlen("block@")))
6263 block_name = NULL;
6264 break;
6266 case COMP_CONTAINS:
6267 case COMP_DERIVED_CONTAINS:
6268 state = gfc_state_stack->previous->state;
6269 block_name = gfc_state_stack->previous->sym == NULL
6270 ? NULL : gfc_state_stack->previous->sym->name;
6271 break;
6273 default:
6274 break;
6277 abreviated_modproc_decl
6278 = gfc_current_block ()
6279 && gfc_current_block ()->abr_modproc_decl;
6281 switch (state)
6283 case COMP_NONE:
6284 case COMP_PROGRAM:
6285 *st = ST_END_PROGRAM;
6286 target = " program";
6287 eos_ok = 1;
6288 break;
6290 case COMP_SUBROUTINE:
6291 *st = ST_END_SUBROUTINE;
6292 if (!abreviated_modproc_decl)
6293 target = " subroutine";
6294 else
6295 target = " procedure";
6296 eos_ok = !contained_procedure ();
6297 break;
6299 case COMP_FUNCTION:
6300 *st = ST_END_FUNCTION;
6301 if (!abreviated_modproc_decl)
6302 target = " function";
6303 else
6304 target = " procedure";
6305 eos_ok = !contained_procedure ();
6306 break;
6308 case COMP_BLOCK_DATA:
6309 *st = ST_END_BLOCK_DATA;
6310 target = " block data";
6311 eos_ok = 1;
6312 break;
6314 case COMP_MODULE:
6315 *st = ST_END_MODULE;
6316 target = " module";
6317 eos_ok = 1;
6318 break;
6320 case COMP_SUBMODULE:
6321 *st = ST_END_SUBMODULE;
6322 target = " submodule";
6323 eos_ok = 1;
6324 break;
6326 case COMP_INTERFACE:
6327 *st = ST_END_INTERFACE;
6328 target = " interface";
6329 eos_ok = 0;
6330 break;
6332 case COMP_DERIVED:
6333 case COMP_DERIVED_CONTAINS:
6334 *st = ST_END_TYPE;
6335 target = " type";
6336 eos_ok = 0;
6337 break;
6339 case COMP_ASSOCIATE:
6340 *st = ST_END_ASSOCIATE;
6341 target = " associate";
6342 eos_ok = 0;
6343 break;
6345 case COMP_BLOCK:
6346 *st = ST_END_BLOCK;
6347 target = " block";
6348 eos_ok = 0;
6349 break;
6351 case COMP_IF:
6352 *st = ST_ENDIF;
6353 target = " if";
6354 eos_ok = 0;
6355 break;
6357 case COMP_DO:
6358 case COMP_DO_CONCURRENT:
6359 *st = ST_ENDDO;
6360 target = " do";
6361 eos_ok = 0;
6362 break;
6364 case COMP_CRITICAL:
6365 *st = ST_END_CRITICAL;
6366 target = " critical";
6367 eos_ok = 0;
6368 break;
6370 case COMP_SELECT:
6371 case COMP_SELECT_TYPE:
6372 *st = ST_END_SELECT;
6373 target = " select";
6374 eos_ok = 0;
6375 break;
6377 case COMP_FORALL:
6378 *st = ST_END_FORALL;
6379 target = " forall";
6380 eos_ok = 0;
6381 break;
6383 case COMP_WHERE:
6384 *st = ST_END_WHERE;
6385 target = " where";
6386 eos_ok = 0;
6387 break;
6389 case COMP_ENUM:
6390 *st = ST_END_ENUM;
6391 target = " enum";
6392 eos_ok = 0;
6393 last_initializer = NULL;
6394 set_enum_kind ();
6395 gfc_free_enum_history ();
6396 break;
6398 default:
6399 gfc_error ("Unexpected END statement at %C");
6400 goto cleanup;
6403 old_loc = gfc_current_locus;
6404 if (gfc_match_eos () == MATCH_YES)
6406 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6408 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6409 "instead of %s statement at %L",
6410 abreviated_modproc_decl ? "END PROCEDURE"
6411 : gfc_ascii_statement(*st), &old_loc))
6412 goto cleanup;
6414 else if (!eos_ok)
6416 /* We would have required END [something]. */
6417 gfc_error ("%s statement expected at %L",
6418 gfc_ascii_statement (*st), &old_loc);
6419 goto cleanup;
6422 return MATCH_YES;
6425 /* Verify that we've got the sort of end-block that we're expecting. */
6426 if (gfc_match (target) != MATCH_YES)
6428 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6429 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6430 goto cleanup;
6433 old_loc = gfc_current_locus;
6434 /* If we're at the end, make sure a block name wasn't required. */
6435 if (gfc_match_eos () == MATCH_YES)
6438 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6439 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6440 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6441 return MATCH_YES;
6443 if (!block_name)
6444 return MATCH_YES;
6446 gfc_error ("Expected block name of %qs in %s statement at %L",
6447 block_name, gfc_ascii_statement (*st), &old_loc);
6449 return MATCH_ERROR;
6452 /* END INTERFACE has a special handler for its several possible endings. */
6453 if (*st == ST_END_INTERFACE)
6454 return gfc_match_end_interface ();
6456 /* We haven't hit the end of statement, so what is left must be an
6457 end-name. */
6458 m = gfc_match_space ();
6459 if (m == MATCH_YES)
6460 m = gfc_match_name (name);
6462 if (m == MATCH_NO)
6463 gfc_error ("Expected terminating name at %C");
6464 if (m != MATCH_YES)
6465 goto cleanup;
6467 if (block_name == NULL)
6468 goto syntax;
6470 /* We have to pick out the declared submodule name from the composite
6471 required by F2008:11.2.3 para 2, which ends in the declared name. */
6472 if (state == COMP_SUBMODULE)
6473 block_name = strchr (block_name, '.') + 1;
6475 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6477 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6478 gfc_ascii_statement (*st));
6479 goto cleanup;
6481 /* Procedure pointer as function result. */
6482 else if (strcmp (block_name, "ppr@") == 0
6483 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6485 gfc_error ("Expected label %qs for %s statement at %C",
6486 gfc_current_block ()->ns->proc_name->name,
6487 gfc_ascii_statement (*st));
6488 goto cleanup;
6491 if (gfc_match_eos () == MATCH_YES)
6492 return MATCH_YES;
6494 syntax:
6495 gfc_syntax_error (*st);
6497 cleanup:
6498 gfc_current_locus = old_loc;
6500 /* If we are missing an END BLOCK, we created a half-ready namespace.
6501 Remove it from the parent namespace's sibling list. */
6503 while (state == COMP_BLOCK)
6505 parent_ns = gfc_current_ns->parent;
6507 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6509 prev_ns = NULL;
6510 ns = *nsp;
6511 while (ns)
6513 if (ns == gfc_current_ns)
6515 if (prev_ns == NULL)
6516 *nsp = NULL;
6517 else
6518 prev_ns->sibling = ns->sibling;
6520 prev_ns = ns;
6521 ns = ns->sibling;
6524 gfc_free_namespace (gfc_current_ns);
6525 gfc_current_ns = parent_ns;
6526 gfc_state_stack = gfc_state_stack->previous;
6527 state = gfc_current_state ();
6530 return MATCH_ERROR;
6535 /***************** Attribute declaration statements ****************/
6537 /* Set the attribute of a single variable. */
6539 static match
6540 attr_decl1 (void)
6542 char name[GFC_MAX_SYMBOL_LEN + 1];
6543 gfc_array_spec *as;
6545 /* Workaround -Wmaybe-uninitialized false positive during
6546 profiledbootstrap by initializing them. */
6547 gfc_symbol *sym = NULL;
6548 locus var_locus;
6549 match m;
6551 as = NULL;
6553 m = gfc_match_name (name);
6554 if (m != MATCH_YES)
6555 goto cleanup;
6557 if (find_special (name, &sym, false))
6558 return MATCH_ERROR;
6560 if (!check_function_name (name))
6562 m = MATCH_ERROR;
6563 goto cleanup;
6566 var_locus = gfc_current_locus;
6568 /* Deal with possible array specification for certain attributes. */
6569 if (current_attr.dimension
6570 || current_attr.codimension
6571 || current_attr.allocatable
6572 || current_attr.pointer
6573 || current_attr.target)
6575 m = gfc_match_array_spec (&as, !current_attr.codimension,
6576 !current_attr.dimension
6577 && !current_attr.pointer
6578 && !current_attr.target);
6579 if (m == MATCH_ERROR)
6580 goto cleanup;
6582 if (current_attr.dimension && m == MATCH_NO)
6584 gfc_error ("Missing array specification at %L in DIMENSION "
6585 "statement", &var_locus);
6586 m = MATCH_ERROR;
6587 goto cleanup;
6590 if (current_attr.dimension && sym->value)
6592 gfc_error ("Dimensions specified for %s at %L after its "
6593 "initialisation", sym->name, &var_locus);
6594 m = MATCH_ERROR;
6595 goto cleanup;
6598 if (current_attr.codimension && m == MATCH_NO)
6600 gfc_error ("Missing array specification at %L in CODIMENSION "
6601 "statement", &var_locus);
6602 m = MATCH_ERROR;
6603 goto cleanup;
6606 if ((current_attr.allocatable || current_attr.pointer)
6607 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6609 gfc_error ("Array specification must be deferred at %L", &var_locus);
6610 m = MATCH_ERROR;
6611 goto cleanup;
6615 /* Update symbol table. DIMENSION attribute is set in
6616 gfc_set_array_spec(). For CLASS variables, this must be applied
6617 to the first component, or '_data' field. */
6618 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6620 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6622 m = MATCH_ERROR;
6623 goto cleanup;
6626 else
6628 if (current_attr.dimension == 0 && current_attr.codimension == 0
6629 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6631 m = MATCH_ERROR;
6632 goto cleanup;
6636 if (sym->ts.type == BT_CLASS
6637 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6639 m = MATCH_ERROR;
6640 goto cleanup;
6643 if (!gfc_set_array_spec (sym, as, &var_locus))
6645 m = MATCH_ERROR;
6646 goto cleanup;
6649 if (sym->attr.cray_pointee && sym->as != NULL)
6651 /* Fix the array spec. */
6652 m = gfc_mod_pointee_as (sym->as);
6653 if (m == MATCH_ERROR)
6654 goto cleanup;
6657 if (!gfc_add_attribute (&sym->attr, &var_locus))
6659 m = MATCH_ERROR;
6660 goto cleanup;
6663 if ((current_attr.external || current_attr.intrinsic)
6664 && sym->attr.flavor != FL_PROCEDURE
6665 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6667 m = MATCH_ERROR;
6668 goto cleanup;
6671 add_hidden_procptr_result (sym);
6673 return MATCH_YES;
6675 cleanup:
6676 gfc_free_array_spec (as);
6677 return m;
6681 /* Generic attribute declaration subroutine. Used for attributes that
6682 just have a list of names. */
6684 static match
6685 attr_decl (void)
6687 match m;
6689 /* Gobble the optional double colon, by simply ignoring the result
6690 of gfc_match(). */
6691 gfc_match (" ::");
6693 for (;;)
6695 m = attr_decl1 ();
6696 if (m != MATCH_YES)
6697 break;
6699 if (gfc_match_eos () == MATCH_YES)
6701 m = MATCH_YES;
6702 break;
6705 if (gfc_match_char (',') != MATCH_YES)
6707 gfc_error ("Unexpected character in variable list at %C");
6708 m = MATCH_ERROR;
6709 break;
6713 return m;
6717 /* This routine matches Cray Pointer declarations of the form:
6718 pointer ( <pointer>, <pointee> )
6720 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6721 The pointer, if already declared, should be an integer. Otherwise, we
6722 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6723 be either a scalar, or an array declaration. No space is allocated for
6724 the pointee. For the statement
6725 pointer (ipt, ar(10))
6726 any subsequent uses of ar will be translated (in C-notation) as
6727 ar(i) => ((<type> *) ipt)(i)
6728 After gimplification, pointee variable will disappear in the code. */
6730 static match
6731 cray_pointer_decl (void)
6733 match m;
6734 gfc_array_spec *as = NULL;
6735 gfc_symbol *cptr; /* Pointer symbol. */
6736 gfc_symbol *cpte; /* Pointee symbol. */
6737 locus var_locus;
6738 bool done = false;
6740 while (!done)
6742 if (gfc_match_char ('(') != MATCH_YES)
6744 gfc_error ("Expected %<(%> at %C");
6745 return MATCH_ERROR;
6748 /* Match pointer. */
6749 var_locus = gfc_current_locus;
6750 gfc_clear_attr (&current_attr);
6751 gfc_add_cray_pointer (&current_attr, &var_locus);
6752 current_ts.type = BT_INTEGER;
6753 current_ts.kind = gfc_index_integer_kind;
6755 m = gfc_match_symbol (&cptr, 0);
6756 if (m != MATCH_YES)
6758 gfc_error ("Expected variable name at %C");
6759 return m;
6762 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6763 return MATCH_ERROR;
6765 gfc_set_sym_referenced (cptr);
6767 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6769 cptr->ts.type = BT_INTEGER;
6770 cptr->ts.kind = gfc_index_integer_kind;
6772 else if (cptr->ts.type != BT_INTEGER)
6774 gfc_error ("Cray pointer at %C must be an integer");
6775 return MATCH_ERROR;
6777 else if (cptr->ts.kind < gfc_index_integer_kind)
6778 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6779 " memory addresses require %d bytes",
6780 cptr->ts.kind, gfc_index_integer_kind);
6782 if (gfc_match_char (',') != MATCH_YES)
6784 gfc_error ("Expected \",\" at %C");
6785 return MATCH_ERROR;
6788 /* Match Pointee. */
6789 var_locus = gfc_current_locus;
6790 gfc_clear_attr (&current_attr);
6791 gfc_add_cray_pointee (&current_attr, &var_locus);
6792 current_ts.type = BT_UNKNOWN;
6793 current_ts.kind = 0;
6795 m = gfc_match_symbol (&cpte, 0);
6796 if (m != MATCH_YES)
6798 gfc_error ("Expected variable name at %C");
6799 return m;
6802 /* Check for an optional array spec. */
6803 m = gfc_match_array_spec (&as, true, false);
6804 if (m == MATCH_ERROR)
6806 gfc_free_array_spec (as);
6807 return m;
6809 else if (m == MATCH_NO)
6811 gfc_free_array_spec (as);
6812 as = NULL;
6815 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6816 return MATCH_ERROR;
6818 gfc_set_sym_referenced (cpte);
6820 if (cpte->as == NULL)
6822 if (!gfc_set_array_spec (cpte, as, &var_locus))
6823 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6825 else if (as != NULL)
6827 gfc_error ("Duplicate array spec for Cray pointee at %C");
6828 gfc_free_array_spec (as);
6829 return MATCH_ERROR;
6832 as = NULL;
6834 if (cpte->as != NULL)
6836 /* Fix array spec. */
6837 m = gfc_mod_pointee_as (cpte->as);
6838 if (m == MATCH_ERROR)
6839 return m;
6842 /* Point the Pointee at the Pointer. */
6843 cpte->cp_pointer = cptr;
6845 if (gfc_match_char (')') != MATCH_YES)
6847 gfc_error ("Expected \")\" at %C");
6848 return MATCH_ERROR;
6850 m = gfc_match_char (',');
6851 if (m != MATCH_YES)
6852 done = true; /* Stop searching for more declarations. */
6856 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6857 || gfc_match_eos () != MATCH_YES)
6859 gfc_error ("Expected %<,%> or end of statement at %C");
6860 return MATCH_ERROR;
6862 return MATCH_YES;
6866 match
6867 gfc_match_external (void)
6870 gfc_clear_attr (&current_attr);
6871 current_attr.external = 1;
6873 return attr_decl ();
6877 match
6878 gfc_match_intent (void)
6880 sym_intent intent;
6882 /* This is not allowed within a BLOCK construct! */
6883 if (gfc_current_state () == COMP_BLOCK)
6885 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6886 return MATCH_ERROR;
6889 intent = match_intent_spec ();
6890 if (intent == INTENT_UNKNOWN)
6891 return MATCH_ERROR;
6893 gfc_clear_attr (&current_attr);
6894 current_attr.intent = intent;
6896 return attr_decl ();
6900 match
6901 gfc_match_intrinsic (void)
6904 gfc_clear_attr (&current_attr);
6905 current_attr.intrinsic = 1;
6907 return attr_decl ();
6911 match
6912 gfc_match_optional (void)
6914 /* This is not allowed within a BLOCK construct! */
6915 if (gfc_current_state () == COMP_BLOCK)
6917 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6918 return MATCH_ERROR;
6921 gfc_clear_attr (&current_attr);
6922 current_attr.optional = 1;
6924 return attr_decl ();
6928 match
6929 gfc_match_pointer (void)
6931 gfc_gobble_whitespace ();
6932 if (gfc_peek_ascii_char () == '(')
6934 if (!flag_cray_pointer)
6936 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6937 "flag");
6938 return MATCH_ERROR;
6940 return cray_pointer_decl ();
6942 else
6944 gfc_clear_attr (&current_attr);
6945 current_attr.pointer = 1;
6947 return attr_decl ();
6952 match
6953 gfc_match_allocatable (void)
6955 gfc_clear_attr (&current_attr);
6956 current_attr.allocatable = 1;
6958 return attr_decl ();
6962 match
6963 gfc_match_codimension (void)
6965 gfc_clear_attr (&current_attr);
6966 current_attr.codimension = 1;
6968 return attr_decl ();
6972 match
6973 gfc_match_contiguous (void)
6975 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
6976 return MATCH_ERROR;
6978 gfc_clear_attr (&current_attr);
6979 current_attr.contiguous = 1;
6981 return attr_decl ();
6985 match
6986 gfc_match_dimension (void)
6988 gfc_clear_attr (&current_attr);
6989 current_attr.dimension = 1;
6991 return attr_decl ();
6995 match
6996 gfc_match_target (void)
6998 gfc_clear_attr (&current_attr);
6999 current_attr.target = 1;
7001 return attr_decl ();
7005 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7006 statement. */
7008 static match
7009 access_attr_decl (gfc_statement st)
7011 char name[GFC_MAX_SYMBOL_LEN + 1];
7012 interface_type type;
7013 gfc_user_op *uop;
7014 gfc_symbol *sym, *dt_sym;
7015 gfc_intrinsic_op op;
7016 match m;
7018 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7019 goto done;
7021 for (;;)
7023 m = gfc_match_generic_spec (&type, name, &op);
7024 if (m == MATCH_NO)
7025 goto syntax;
7026 if (m == MATCH_ERROR)
7027 return MATCH_ERROR;
7029 switch (type)
7031 case INTERFACE_NAMELESS:
7032 case INTERFACE_ABSTRACT:
7033 goto syntax;
7035 case INTERFACE_GENERIC:
7036 if (gfc_get_symbol (name, NULL, &sym))
7037 goto done;
7039 if (!gfc_add_access (&sym->attr,
7040 (st == ST_PUBLIC)
7041 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7042 sym->name, NULL))
7043 return MATCH_ERROR;
7045 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7046 && !gfc_add_access (&dt_sym->attr,
7047 (st == ST_PUBLIC)
7048 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7049 sym->name, NULL))
7050 return MATCH_ERROR;
7052 break;
7054 case INTERFACE_INTRINSIC_OP:
7055 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7057 gfc_intrinsic_op other_op;
7059 gfc_current_ns->operator_access[op] =
7060 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7062 /* Handle the case if there is another op with the same
7063 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7064 other_op = gfc_equivalent_op (op);
7066 if (other_op != INTRINSIC_NONE)
7067 gfc_current_ns->operator_access[other_op] =
7068 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7071 else
7073 gfc_error ("Access specification of the %s operator at %C has "
7074 "already been specified", gfc_op2string (op));
7075 goto done;
7078 break;
7080 case INTERFACE_USER_OP:
7081 uop = gfc_get_uop (name);
7083 if (uop->access == ACCESS_UNKNOWN)
7085 uop->access = (st == ST_PUBLIC)
7086 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7088 else
7090 gfc_error ("Access specification of the .%s. operator at %C "
7091 "has already been specified", sym->name);
7092 goto done;
7095 break;
7098 if (gfc_match_char (',') == MATCH_NO)
7099 break;
7102 if (gfc_match_eos () != MATCH_YES)
7103 goto syntax;
7104 return MATCH_YES;
7106 syntax:
7107 gfc_syntax_error (st);
7109 done:
7110 return MATCH_ERROR;
7114 match
7115 gfc_match_protected (void)
7117 gfc_symbol *sym;
7118 match m;
7120 if (!gfc_current_ns->proc_name
7121 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7123 gfc_error ("PROTECTED at %C only allowed in specification "
7124 "part of a module");
7125 return MATCH_ERROR;
7129 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7130 return MATCH_ERROR;
7132 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7134 return MATCH_ERROR;
7137 if (gfc_match_eos () == MATCH_YES)
7138 goto syntax;
7140 for(;;)
7142 m = gfc_match_symbol (&sym, 0);
7143 switch (m)
7145 case MATCH_YES:
7146 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7147 return MATCH_ERROR;
7148 goto next_item;
7150 case MATCH_NO:
7151 break;
7153 case MATCH_ERROR:
7154 return MATCH_ERROR;
7157 next_item:
7158 if (gfc_match_eos () == MATCH_YES)
7159 break;
7160 if (gfc_match_char (',') != MATCH_YES)
7161 goto syntax;
7164 return MATCH_YES;
7166 syntax:
7167 gfc_error ("Syntax error in PROTECTED statement at %C");
7168 return MATCH_ERROR;
7172 /* The PRIVATE statement is a bit weird in that it can be an attribute
7173 declaration, but also works as a standalone statement inside of a
7174 type declaration or a module. */
7176 match
7177 gfc_match_private (gfc_statement *st)
7180 if (gfc_match ("private") != MATCH_YES)
7181 return MATCH_NO;
7183 if (gfc_current_state () != COMP_MODULE
7184 && !(gfc_current_state () == COMP_DERIVED
7185 && gfc_state_stack->previous
7186 && gfc_state_stack->previous->state == COMP_MODULE)
7187 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7188 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7189 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7191 gfc_error ("PRIVATE statement at %C is only allowed in the "
7192 "specification part of a module");
7193 return MATCH_ERROR;
7196 if (gfc_current_state () == COMP_DERIVED)
7198 if (gfc_match_eos () == MATCH_YES)
7200 *st = ST_PRIVATE;
7201 return MATCH_YES;
7204 gfc_syntax_error (ST_PRIVATE);
7205 return MATCH_ERROR;
7208 if (gfc_match_eos () == MATCH_YES)
7210 *st = ST_PRIVATE;
7211 return MATCH_YES;
7214 *st = ST_ATTR_DECL;
7215 return access_attr_decl (ST_PRIVATE);
7219 match
7220 gfc_match_public (gfc_statement *st)
7223 if (gfc_match ("public") != MATCH_YES)
7224 return MATCH_NO;
7226 if (gfc_current_state () != COMP_MODULE)
7228 gfc_error ("PUBLIC statement at %C is only allowed in the "
7229 "specification part of a module");
7230 return MATCH_ERROR;
7233 if (gfc_match_eos () == MATCH_YES)
7235 *st = ST_PUBLIC;
7236 return MATCH_YES;
7239 *st = ST_ATTR_DECL;
7240 return access_attr_decl (ST_PUBLIC);
7244 /* Workhorse for gfc_match_parameter. */
7246 static match
7247 do_parm (void)
7249 gfc_symbol *sym;
7250 gfc_expr *init;
7251 match m;
7252 bool t;
7254 m = gfc_match_symbol (&sym, 0);
7255 if (m == MATCH_NO)
7256 gfc_error ("Expected variable name at %C in PARAMETER statement");
7258 if (m != MATCH_YES)
7259 return m;
7261 if (gfc_match_char ('=') == MATCH_NO)
7263 gfc_error ("Expected = sign in PARAMETER statement at %C");
7264 return MATCH_ERROR;
7267 m = gfc_match_init_expr (&init);
7268 if (m == MATCH_NO)
7269 gfc_error ("Expected expression at %C in PARAMETER statement");
7270 if (m != MATCH_YES)
7271 return m;
7273 if (sym->ts.type == BT_UNKNOWN
7274 && !gfc_set_default_type (sym, 1, NULL))
7276 m = MATCH_ERROR;
7277 goto cleanup;
7280 if (!gfc_check_assign_symbol (sym, NULL, init)
7281 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7283 m = MATCH_ERROR;
7284 goto cleanup;
7287 if (sym->value)
7289 gfc_error ("Initializing already initialized variable at %C");
7290 m = MATCH_ERROR;
7291 goto cleanup;
7294 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7295 return (t) ? MATCH_YES : MATCH_ERROR;
7297 cleanup:
7298 gfc_free_expr (init);
7299 return m;
7303 /* Match a parameter statement, with the weird syntax that these have. */
7305 match
7306 gfc_match_parameter (void)
7308 match m;
7310 if (gfc_match_char ('(') == MATCH_NO)
7311 return MATCH_NO;
7313 for (;;)
7315 m = do_parm ();
7316 if (m != MATCH_YES)
7317 break;
7319 if (gfc_match (" )%t") == MATCH_YES)
7320 break;
7322 if (gfc_match_char (',') != MATCH_YES)
7324 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7325 m = MATCH_ERROR;
7326 break;
7330 return m;
7334 /* Save statements have a special syntax. */
7336 match
7337 gfc_match_save (void)
7339 char n[GFC_MAX_SYMBOL_LEN+1];
7340 gfc_common_head *c;
7341 gfc_symbol *sym;
7342 match m;
7344 if (gfc_match_eos () == MATCH_YES)
7346 if (gfc_current_ns->seen_save)
7348 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7349 "follows previous SAVE statement"))
7350 return MATCH_ERROR;
7353 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7354 return MATCH_YES;
7357 if (gfc_current_ns->save_all)
7359 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7360 "blanket SAVE statement"))
7361 return MATCH_ERROR;
7364 gfc_match (" ::");
7366 for (;;)
7368 m = gfc_match_symbol (&sym, 0);
7369 switch (m)
7371 case MATCH_YES:
7372 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7373 &gfc_current_locus))
7374 return MATCH_ERROR;
7375 goto next_item;
7377 case MATCH_NO:
7378 break;
7380 case MATCH_ERROR:
7381 return MATCH_ERROR;
7384 m = gfc_match (" / %n /", &n);
7385 if (m == MATCH_ERROR)
7386 return MATCH_ERROR;
7387 if (m == MATCH_NO)
7388 goto syntax;
7390 c = gfc_get_common (n, 0);
7391 c->saved = 1;
7393 gfc_current_ns->seen_save = 1;
7395 next_item:
7396 if (gfc_match_eos () == MATCH_YES)
7397 break;
7398 if (gfc_match_char (',') != MATCH_YES)
7399 goto syntax;
7402 return MATCH_YES;
7404 syntax:
7405 gfc_error ("Syntax error in SAVE statement at %C");
7406 return MATCH_ERROR;
7410 match
7411 gfc_match_value (void)
7413 gfc_symbol *sym;
7414 match m;
7416 /* This is not allowed within a BLOCK construct! */
7417 if (gfc_current_state () == COMP_BLOCK)
7419 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7420 return MATCH_ERROR;
7423 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7424 return MATCH_ERROR;
7426 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7428 return MATCH_ERROR;
7431 if (gfc_match_eos () == MATCH_YES)
7432 goto syntax;
7434 for(;;)
7436 m = gfc_match_symbol (&sym, 0);
7437 switch (m)
7439 case MATCH_YES:
7440 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7441 return MATCH_ERROR;
7442 goto next_item;
7444 case MATCH_NO:
7445 break;
7447 case MATCH_ERROR:
7448 return MATCH_ERROR;
7451 next_item:
7452 if (gfc_match_eos () == MATCH_YES)
7453 break;
7454 if (gfc_match_char (',') != MATCH_YES)
7455 goto syntax;
7458 return MATCH_YES;
7460 syntax:
7461 gfc_error ("Syntax error in VALUE statement at %C");
7462 return MATCH_ERROR;
7466 match
7467 gfc_match_volatile (void)
7469 gfc_symbol *sym;
7470 match m;
7472 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7473 return MATCH_ERROR;
7475 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7477 return MATCH_ERROR;
7480 if (gfc_match_eos () == MATCH_YES)
7481 goto syntax;
7483 for(;;)
7485 /* VOLATILE is special because it can be added to host-associated
7486 symbols locally. Except for coarrays. */
7487 m = gfc_match_symbol (&sym, 1);
7488 switch (m)
7490 case MATCH_YES:
7491 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7492 for variable in a BLOCK which is defined outside of the BLOCK. */
7493 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7495 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7496 "%C, which is use-/host-associated", sym->name);
7497 return MATCH_ERROR;
7499 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7500 return MATCH_ERROR;
7501 goto next_item;
7503 case MATCH_NO:
7504 break;
7506 case MATCH_ERROR:
7507 return MATCH_ERROR;
7510 next_item:
7511 if (gfc_match_eos () == MATCH_YES)
7512 break;
7513 if (gfc_match_char (',') != MATCH_YES)
7514 goto syntax;
7517 return MATCH_YES;
7519 syntax:
7520 gfc_error ("Syntax error in VOLATILE statement at %C");
7521 return MATCH_ERROR;
7525 match
7526 gfc_match_asynchronous (void)
7528 gfc_symbol *sym;
7529 match m;
7531 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7532 return MATCH_ERROR;
7534 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7536 return MATCH_ERROR;
7539 if (gfc_match_eos () == MATCH_YES)
7540 goto syntax;
7542 for(;;)
7544 /* ASYNCHRONOUS is special because it can be added to host-associated
7545 symbols locally. */
7546 m = gfc_match_symbol (&sym, 1);
7547 switch (m)
7549 case MATCH_YES:
7550 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7551 return MATCH_ERROR;
7552 goto next_item;
7554 case MATCH_NO:
7555 break;
7557 case MATCH_ERROR:
7558 return MATCH_ERROR;
7561 next_item:
7562 if (gfc_match_eos () == MATCH_YES)
7563 break;
7564 if (gfc_match_char (',') != MATCH_YES)
7565 goto syntax;
7568 return MATCH_YES;
7570 syntax:
7571 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7572 return MATCH_ERROR;
7576 /* Match a module procedure statement in a submodule. */
7578 match
7579 gfc_match_submod_proc (void)
7581 char name[GFC_MAX_SYMBOL_LEN + 1];
7582 gfc_symbol *sym, *fsym;
7583 match m;
7584 gfc_formal_arglist *formal, *head, *tail;
7586 if (gfc_current_state () != COMP_CONTAINS
7587 || !(gfc_state_stack->previous
7588 && gfc_state_stack->previous->state == COMP_SUBMODULE))
7589 return MATCH_NO;
7591 m = gfc_match (" module% procedure% %n", name);
7592 if (m != MATCH_YES)
7593 return m;
7595 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
7596 "at %C"))
7597 return MATCH_ERROR;
7599 if (get_proc_name (name, &sym, false))
7600 return MATCH_ERROR;
7602 /* Make sure that the result field is appropriately filled, even though
7603 the result symbol will be replaced later on. */
7604 if (sym->ts.interface->attr.function)
7606 if (sym->ts.interface->result
7607 && sym->ts.interface->result != sym->ts.interface)
7608 sym->result= sym->ts.interface->result;
7609 else
7610 sym->result = sym;
7613 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7614 the symbol existed before. */
7615 sym->declared_at = gfc_current_locus;
7617 if (!sym->attr.module_procedure)
7618 return MATCH_ERROR;
7620 /* Signal match_end to expect "end procedure". */
7621 sym->abr_modproc_decl = 1;
7623 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7624 sym->attr.if_source = IFSRC_DECL;
7626 gfc_new_block = sym;
7628 /* Make a new formal arglist with the symbols in the procedure
7629 namespace. */
7630 head = tail = NULL;
7631 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
7633 if (formal == sym->formal)
7634 head = tail = gfc_get_formal_arglist ();
7635 else
7637 tail->next = gfc_get_formal_arglist ();
7638 tail = tail->next;
7641 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
7642 goto cleanup;
7644 tail->sym = fsym;
7645 gfc_set_sym_referenced (fsym);
7648 /* The dummy symbols get cleaned up, when the formal_namespace of the
7649 interface declaration is cleared. This allows us to add the
7650 explicit interface as is done for other type of procedure. */
7651 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
7652 &gfc_current_locus))
7653 return MATCH_ERROR;
7655 if (gfc_match_eos () != MATCH_YES)
7657 gfc_syntax_error (ST_MODULE_PROC);
7658 return MATCH_ERROR;
7661 return MATCH_YES;
7663 cleanup:
7664 gfc_free_formal_arglist (head);
7665 return MATCH_ERROR;
7669 /* Match a module procedure statement. Note that we have to modify
7670 symbols in the parent's namespace because the current one was there
7671 to receive symbols that are in an interface's formal argument list. */
7673 match
7674 gfc_match_modproc (void)
7676 char name[GFC_MAX_SYMBOL_LEN + 1];
7677 gfc_symbol *sym;
7678 match m;
7679 locus old_locus;
7680 gfc_namespace *module_ns;
7681 gfc_interface *old_interface_head, *interface;
7683 if (gfc_state_stack->state != COMP_INTERFACE
7684 || gfc_state_stack->previous == NULL
7685 || current_interface.type == INTERFACE_NAMELESS
7686 || current_interface.type == INTERFACE_ABSTRACT)
7688 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7689 "interface");
7690 return MATCH_ERROR;
7693 module_ns = gfc_current_ns->parent;
7694 for (; module_ns; module_ns = module_ns->parent)
7695 if (module_ns->proc_name->attr.flavor == FL_MODULE
7696 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7697 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7698 && !module_ns->proc_name->attr.contained))
7699 break;
7701 if (module_ns == NULL)
7702 return MATCH_ERROR;
7704 /* Store the current state of the interface. We will need it if we
7705 end up with a syntax error and need to recover. */
7706 old_interface_head = gfc_current_interface_head ();
7708 /* Check if the F2008 optional double colon appears. */
7709 gfc_gobble_whitespace ();
7710 old_locus = gfc_current_locus;
7711 if (gfc_match ("::") == MATCH_YES)
7713 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7714 "MODULE PROCEDURE statement at %L", &old_locus))
7715 return MATCH_ERROR;
7717 else
7718 gfc_current_locus = old_locus;
7720 for (;;)
7722 bool last = false;
7723 old_locus = gfc_current_locus;
7725 m = gfc_match_name (name);
7726 if (m == MATCH_NO)
7727 goto syntax;
7728 if (m != MATCH_YES)
7729 return MATCH_ERROR;
7731 /* Check for syntax error before starting to add symbols to the
7732 current namespace. */
7733 if (gfc_match_eos () == MATCH_YES)
7734 last = true;
7736 if (!last && gfc_match_char (',') != MATCH_YES)
7737 goto syntax;
7739 /* Now we're sure the syntax is valid, we process this item
7740 further. */
7741 if (gfc_get_symbol (name, module_ns, &sym))
7742 return MATCH_ERROR;
7744 if (sym->attr.intrinsic)
7746 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7747 "PROCEDURE", &old_locus);
7748 return MATCH_ERROR;
7751 if (sym->attr.proc != PROC_MODULE
7752 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7753 return MATCH_ERROR;
7755 if (!gfc_add_interface (sym))
7756 return MATCH_ERROR;
7758 sym->attr.mod_proc = 1;
7759 sym->declared_at = old_locus;
7761 if (last)
7762 break;
7765 return MATCH_YES;
7767 syntax:
7768 /* Restore the previous state of the interface. */
7769 interface = gfc_current_interface_head ();
7770 gfc_set_current_interface_head (old_interface_head);
7772 /* Free the new interfaces. */
7773 while (interface != old_interface_head)
7775 gfc_interface *i = interface->next;
7776 free (interface);
7777 interface = i;
7780 /* And issue a syntax error. */
7781 gfc_syntax_error (ST_MODULE_PROC);
7782 return MATCH_ERROR;
7786 /* Check a derived type that is being extended. */
7788 static gfc_symbol*
7789 check_extended_derived_type (char *name)
7791 gfc_symbol *extended;
7793 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7795 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7796 return NULL;
7799 extended = gfc_find_dt_in_generic (extended);
7801 /* F08:C428. */
7802 if (!extended)
7804 gfc_error ("Symbol %qs at %C has not been previously defined", name);
7805 return NULL;
7808 if (extended->attr.flavor != FL_DERIVED)
7810 gfc_error ("%qs in EXTENDS expression at %C is not a "
7811 "derived type", name);
7812 return NULL;
7815 if (extended->attr.is_bind_c)
7817 gfc_error ("%qs cannot be extended at %C because it "
7818 "is BIND(C)", extended->name);
7819 return NULL;
7822 if (extended->attr.sequence)
7824 gfc_error ("%qs cannot be extended at %C because it "
7825 "is a SEQUENCE type", extended->name);
7826 return NULL;
7829 return extended;
7833 /* Match the optional attribute specifiers for a type declaration.
7834 Return MATCH_ERROR if an error is encountered in one of the handled
7835 attributes (public, private, bind(c)), MATCH_NO if what's found is
7836 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7837 checking on attribute conflicts needs to be done. */
7839 match
7840 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7842 /* See if the derived type is marked as private. */
7843 if (gfc_match (" , private") == MATCH_YES)
7845 if (gfc_current_state () != COMP_MODULE)
7847 gfc_error ("Derived type at %C can only be PRIVATE in the "
7848 "specification part of a module");
7849 return MATCH_ERROR;
7852 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7853 return MATCH_ERROR;
7855 else if (gfc_match (" , public") == MATCH_YES)
7857 if (gfc_current_state () != COMP_MODULE)
7859 gfc_error ("Derived type at %C can only be PUBLIC in the "
7860 "specification part of a module");
7861 return MATCH_ERROR;
7864 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7865 return MATCH_ERROR;
7867 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7869 /* If the type is defined to be bind(c) it then needs to make
7870 sure that all fields are interoperable. This will
7871 need to be a semantic check on the finished derived type.
7872 See 15.2.3 (lines 9-12) of F2003 draft. */
7873 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7874 return MATCH_ERROR;
7876 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7878 else if (gfc_match (" , abstract") == MATCH_YES)
7880 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7881 return MATCH_ERROR;
7883 if (!gfc_add_abstract (attr, &gfc_current_locus))
7884 return MATCH_ERROR;
7886 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7888 if (!gfc_add_extension (attr, &gfc_current_locus))
7889 return MATCH_ERROR;
7891 else
7892 return MATCH_NO;
7894 /* If we get here, something matched. */
7895 return MATCH_YES;
7899 /* Match the beginning of a derived type declaration. If a type name
7900 was the result of a function, then it is possible to have a symbol
7901 already to be known as a derived type yet have no components. */
7903 match
7904 gfc_match_derived_decl (void)
7906 char name[GFC_MAX_SYMBOL_LEN + 1];
7907 char parent[GFC_MAX_SYMBOL_LEN + 1];
7908 symbol_attribute attr;
7909 gfc_symbol *sym, *gensym;
7910 gfc_symbol *extended;
7911 match m;
7912 match is_type_attr_spec = MATCH_NO;
7913 bool seen_attr = false;
7914 gfc_interface *intr = NULL, *head;
7916 if (gfc_current_state () == COMP_DERIVED)
7917 return MATCH_NO;
7919 name[0] = '\0';
7920 parent[0] = '\0';
7921 gfc_clear_attr (&attr);
7922 extended = NULL;
7926 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7927 if (is_type_attr_spec == MATCH_ERROR)
7928 return MATCH_ERROR;
7929 if (is_type_attr_spec == MATCH_YES)
7930 seen_attr = true;
7931 } while (is_type_attr_spec == MATCH_YES);
7933 /* Deal with derived type extensions. The extension attribute has
7934 been added to 'attr' but now the parent type must be found and
7935 checked. */
7936 if (parent[0])
7937 extended = check_extended_derived_type (parent);
7939 if (parent[0] && !extended)
7940 return MATCH_ERROR;
7942 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7944 gfc_error ("Expected :: in TYPE definition at %C");
7945 return MATCH_ERROR;
7948 m = gfc_match (" %n%t", name);
7949 if (m != MATCH_YES)
7950 return m;
7952 /* Make sure the name is not the name of an intrinsic type. */
7953 if (gfc_is_intrinsic_typename (name))
7955 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
7956 "type", name);
7957 return MATCH_ERROR;
7960 if (gfc_get_symbol (name, NULL, &gensym))
7961 return MATCH_ERROR;
7963 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7965 gfc_error ("Derived type name %qs at %C already has a basic type "
7966 "of %s", gensym->name, gfc_typename (&gensym->ts));
7967 return MATCH_ERROR;
7970 if (!gensym->attr.generic
7971 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
7972 return MATCH_ERROR;
7974 if (!gensym->attr.function
7975 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
7976 return MATCH_ERROR;
7978 sym = gfc_find_dt_in_generic (gensym);
7980 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7982 gfc_error ("Derived type definition of %qs at %C has already been "
7983 "defined", sym->name);
7984 return MATCH_ERROR;
7987 if (!sym)
7989 /* Use upper case to save the actual derived-type symbol. */
7990 gfc_get_symbol (gfc_get_string ("%c%s",
7991 (char) TOUPPER ((unsigned char) gensym->name[0]),
7992 &gensym->name[1]), NULL, &sym);
7993 sym->name = gfc_get_string (gensym->name);
7994 head = gensym->generic;
7995 intr = gfc_get_interface ();
7996 intr->sym = sym;
7997 intr->where = gfc_current_locus;
7998 intr->sym->declared_at = gfc_current_locus;
7999 intr->next = head;
8000 gensym->generic = intr;
8001 gensym->attr.if_source = IFSRC_DECL;
8004 /* The symbol may already have the derived attribute without the
8005 components. The ways this can happen is via a function
8006 definition, an INTRINSIC statement or a subtype in another
8007 derived type that is a pointer. The first part of the AND clause
8008 is true if the symbol is not the return value of a function. */
8009 if (sym->attr.flavor != FL_DERIVED
8010 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
8011 return MATCH_ERROR;
8013 if (attr.access != ACCESS_UNKNOWN
8014 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
8015 return MATCH_ERROR;
8016 else if (sym->attr.access == ACCESS_UNKNOWN
8017 && gensym->attr.access != ACCESS_UNKNOWN
8018 && !gfc_add_access (&sym->attr, gensym->attr.access,
8019 sym->name, NULL))
8020 return MATCH_ERROR;
8022 if (sym->attr.access != ACCESS_UNKNOWN
8023 && gensym->attr.access == ACCESS_UNKNOWN)
8024 gensym->attr.access = sym->attr.access;
8026 /* See if the derived type was labeled as bind(c). */
8027 if (attr.is_bind_c != 0)
8028 sym->attr.is_bind_c = attr.is_bind_c;
8030 /* Construct the f2k_derived namespace if it is not yet there. */
8031 if (!sym->f2k_derived)
8032 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8034 if (extended && !sym->components)
8036 gfc_component *p;
8038 /* Add the extended derived type as the first component. */
8039 gfc_add_component (sym, parent, &p);
8040 extended->refs++;
8041 gfc_set_sym_referenced (extended);
8043 p->ts.type = BT_DERIVED;
8044 p->ts.u.derived = extended;
8045 p->initializer = gfc_default_initializer (&p->ts);
8047 /* Set extension level. */
8048 if (extended->attr.extension == 255)
8050 /* Since the extension field is 8 bit wide, we can only have
8051 up to 255 extension levels. */
8052 gfc_error ("Maximum extension level reached with type %qs at %L",
8053 extended->name, &extended->declared_at);
8054 return MATCH_ERROR;
8056 sym->attr.extension = extended->attr.extension + 1;
8058 /* Provide the links between the extended type and its extension. */
8059 if (!extended->f2k_derived)
8060 extended->f2k_derived = gfc_get_namespace (NULL, 0);
8063 if (!sym->hash_value)
8064 /* Set the hash for the compound name for this type. */
8065 sym->hash_value = gfc_hash_value (sym);
8067 /* Take over the ABSTRACT attribute. */
8068 sym->attr.abstract = attr.abstract;
8070 gfc_new_block = sym;
8072 return MATCH_YES;
8076 /* Cray Pointees can be declared as:
8077 pointer (ipt, a (n,m,...,*)) */
8079 match
8080 gfc_mod_pointee_as (gfc_array_spec *as)
8082 as->cray_pointee = true; /* This will be useful to know later. */
8083 if (as->type == AS_ASSUMED_SIZE)
8084 as->cp_was_assumed = true;
8085 else if (as->type == AS_ASSUMED_SHAPE)
8087 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8088 return MATCH_ERROR;
8090 return MATCH_YES;
8094 /* Match the enum definition statement, here we are trying to match
8095 the first line of enum definition statement.
8096 Returns MATCH_YES if match is found. */
8098 match
8099 gfc_match_enum (void)
8101 match m;
8103 m = gfc_match_eos ();
8104 if (m != MATCH_YES)
8105 return m;
8107 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
8108 return MATCH_ERROR;
8110 return MATCH_YES;
8114 /* Returns an initializer whose value is one higher than the value of the
8115 LAST_INITIALIZER argument. If the argument is NULL, the
8116 initializers value will be set to zero. The initializer's kind
8117 will be set to gfc_c_int_kind.
8119 If -fshort-enums is given, the appropriate kind will be selected
8120 later after all enumerators have been parsed. A warning is issued
8121 here if an initializer exceeds gfc_c_int_kind. */
8123 static gfc_expr *
8124 enum_initializer (gfc_expr *last_initializer, locus where)
8126 gfc_expr *result;
8127 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
8129 mpz_init (result->value.integer);
8131 if (last_initializer != NULL)
8133 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8134 result->where = last_initializer->where;
8136 if (gfc_check_integer_range (result->value.integer,
8137 gfc_c_int_kind) != ARITH_OK)
8139 gfc_error ("Enumerator exceeds the C integer type at %C");
8140 return NULL;
8143 else
8145 /* Control comes here, if it's the very first enumerator and no
8146 initializer has been given. It will be initialized to zero. */
8147 mpz_set_si (result->value.integer, 0);
8150 return result;
8154 /* Match a variable name with an optional initializer. When this
8155 subroutine is called, a variable is expected to be parsed next.
8156 Depending on what is happening at the moment, updates either the
8157 symbol table or the current interface. */
8159 static match
8160 enumerator_decl (void)
8162 char name[GFC_MAX_SYMBOL_LEN + 1];
8163 gfc_expr *initializer;
8164 gfc_array_spec *as = NULL;
8165 gfc_symbol *sym;
8166 locus var_locus;
8167 match m;
8168 bool t;
8169 locus old_locus;
8171 initializer = NULL;
8172 old_locus = gfc_current_locus;
8174 /* When we get here, we've just matched a list of attributes and
8175 maybe a type and a double colon. The next thing we expect to see
8176 is the name of the symbol. */
8177 m = gfc_match_name (name);
8178 if (m != MATCH_YES)
8179 goto cleanup;
8181 var_locus = gfc_current_locus;
8183 /* OK, we've successfully matched the declaration. Now put the
8184 symbol in the current namespace. If we fail to create the symbol,
8185 bail out. */
8186 if (!build_sym (name, NULL, false, &as, &var_locus))
8188 m = MATCH_ERROR;
8189 goto cleanup;
8192 /* The double colon must be present in order to have initializers.
8193 Otherwise the statement is ambiguous with an assignment statement. */
8194 if (colon_seen)
8196 if (gfc_match_char ('=') == MATCH_YES)
8198 m = gfc_match_init_expr (&initializer);
8199 if (m == MATCH_NO)
8201 gfc_error ("Expected an initialization expression at %C");
8202 m = MATCH_ERROR;
8205 if (m != MATCH_YES)
8206 goto cleanup;
8210 /* If we do not have an initializer, the initialization value of the
8211 previous enumerator (stored in last_initializer) is incremented
8212 by 1 and is used to initialize the current enumerator. */
8213 if (initializer == NULL)
8214 initializer = enum_initializer (last_initializer, old_locus);
8216 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8218 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8219 &var_locus);
8220 m = MATCH_ERROR;
8221 goto cleanup;
8224 /* Store this current initializer, for the next enumerator variable
8225 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8226 use last_initializer below. */
8227 last_initializer = initializer;
8228 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8230 /* Maintain enumerator history. */
8231 gfc_find_symbol (name, NULL, 0, &sym);
8232 create_enum_history (sym, last_initializer);
8234 return (t) ? MATCH_YES : MATCH_ERROR;
8236 cleanup:
8237 /* Free stuff up and return. */
8238 gfc_free_expr (initializer);
8240 return m;
8244 /* Match the enumerator definition statement. */
8246 match
8247 gfc_match_enumerator_def (void)
8249 match m;
8250 bool t;
8252 gfc_clear_ts (&current_ts);
8254 m = gfc_match (" enumerator");
8255 if (m != MATCH_YES)
8256 return m;
8258 m = gfc_match (" :: ");
8259 if (m == MATCH_ERROR)
8260 return m;
8262 colon_seen = (m == MATCH_YES);
8264 if (gfc_current_state () != COMP_ENUM)
8266 gfc_error ("ENUM definition statement expected before %C");
8267 gfc_free_enum_history ();
8268 return MATCH_ERROR;
8271 (&current_ts)->type = BT_INTEGER;
8272 (&current_ts)->kind = gfc_c_int_kind;
8274 gfc_clear_attr (&current_attr);
8275 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
8276 if (!t)
8278 m = MATCH_ERROR;
8279 goto cleanup;
8282 for (;;)
8284 m = enumerator_decl ();
8285 if (m == MATCH_ERROR)
8287 gfc_free_enum_history ();
8288 goto cleanup;
8290 if (m == MATCH_NO)
8291 break;
8293 if (gfc_match_eos () == MATCH_YES)
8294 goto cleanup;
8295 if (gfc_match_char (',') != MATCH_YES)
8296 break;
8299 if (gfc_current_state () == COMP_ENUM)
8301 gfc_free_enum_history ();
8302 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8303 m = MATCH_ERROR;
8306 cleanup:
8307 gfc_free_array_spec (current_as);
8308 current_as = NULL;
8309 return m;
8314 /* Match binding attributes. */
8316 static match
8317 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
8319 bool found_passing = false;
8320 bool seen_ptr = false;
8321 match m = MATCH_YES;
8323 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8324 this case the defaults are in there. */
8325 ba->access = ACCESS_UNKNOWN;
8326 ba->pass_arg = NULL;
8327 ba->pass_arg_num = 0;
8328 ba->nopass = 0;
8329 ba->non_overridable = 0;
8330 ba->deferred = 0;
8331 ba->ppc = ppc;
8333 /* If we find a comma, we believe there are binding attributes. */
8334 m = gfc_match_char (',');
8335 if (m == MATCH_NO)
8336 goto done;
8340 /* Access specifier. */
8342 m = gfc_match (" public");
8343 if (m == MATCH_ERROR)
8344 goto error;
8345 if (m == MATCH_YES)
8347 if (ba->access != ACCESS_UNKNOWN)
8349 gfc_error ("Duplicate access-specifier at %C");
8350 goto error;
8353 ba->access = ACCESS_PUBLIC;
8354 continue;
8357 m = gfc_match (" private");
8358 if (m == MATCH_ERROR)
8359 goto error;
8360 if (m == MATCH_YES)
8362 if (ba->access != ACCESS_UNKNOWN)
8364 gfc_error ("Duplicate access-specifier at %C");
8365 goto error;
8368 ba->access = ACCESS_PRIVATE;
8369 continue;
8372 /* If inside GENERIC, the following is not allowed. */
8373 if (!generic)
8376 /* NOPASS flag. */
8377 m = gfc_match (" nopass");
8378 if (m == MATCH_ERROR)
8379 goto error;
8380 if (m == MATCH_YES)
8382 if (found_passing)
8384 gfc_error ("Binding attributes already specify passing,"
8385 " illegal NOPASS at %C");
8386 goto error;
8389 found_passing = true;
8390 ba->nopass = 1;
8391 continue;
8394 /* PASS possibly including argument. */
8395 m = gfc_match (" pass");
8396 if (m == MATCH_ERROR)
8397 goto error;
8398 if (m == MATCH_YES)
8400 char arg[GFC_MAX_SYMBOL_LEN + 1];
8402 if (found_passing)
8404 gfc_error ("Binding attributes already specify passing,"
8405 " illegal PASS at %C");
8406 goto error;
8409 m = gfc_match (" ( %n )", arg);
8410 if (m == MATCH_ERROR)
8411 goto error;
8412 if (m == MATCH_YES)
8413 ba->pass_arg = gfc_get_string (arg);
8414 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8416 found_passing = true;
8417 ba->nopass = 0;
8418 continue;
8421 if (ppc)
8423 /* POINTER flag. */
8424 m = gfc_match (" pointer");
8425 if (m == MATCH_ERROR)
8426 goto error;
8427 if (m == MATCH_YES)
8429 if (seen_ptr)
8431 gfc_error ("Duplicate POINTER attribute at %C");
8432 goto error;
8435 seen_ptr = true;
8436 continue;
8439 else
8441 /* NON_OVERRIDABLE flag. */
8442 m = gfc_match (" non_overridable");
8443 if (m == MATCH_ERROR)
8444 goto error;
8445 if (m == MATCH_YES)
8447 if (ba->non_overridable)
8449 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8450 goto error;
8453 ba->non_overridable = 1;
8454 continue;
8457 /* DEFERRED flag. */
8458 m = gfc_match (" deferred");
8459 if (m == MATCH_ERROR)
8460 goto error;
8461 if (m == MATCH_YES)
8463 if (ba->deferred)
8465 gfc_error ("Duplicate DEFERRED at %C");
8466 goto error;
8469 ba->deferred = 1;
8470 continue;
8476 /* Nothing matching found. */
8477 if (generic)
8478 gfc_error ("Expected access-specifier at %C");
8479 else
8480 gfc_error ("Expected binding attribute at %C");
8481 goto error;
8483 while (gfc_match_char (',') == MATCH_YES);
8485 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8486 if (ba->non_overridable && ba->deferred)
8488 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8489 goto error;
8492 m = MATCH_YES;
8494 done:
8495 if (ba->access == ACCESS_UNKNOWN)
8496 ba->access = gfc_typebound_default_access;
8498 if (ppc && !seen_ptr)
8500 gfc_error ("POINTER attribute is required for procedure pointer component"
8501 " at %C");
8502 goto error;
8505 return m;
8507 error:
8508 return MATCH_ERROR;
8512 /* Match a PROCEDURE specific binding inside a derived type. */
8514 static match
8515 match_procedure_in_type (void)
8517 char name[GFC_MAX_SYMBOL_LEN + 1];
8518 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8519 char* target = NULL, *ifc = NULL;
8520 gfc_typebound_proc tb;
8521 bool seen_colons;
8522 bool seen_attrs;
8523 match m;
8524 gfc_symtree* stree;
8525 gfc_namespace* ns;
8526 gfc_symbol* block;
8527 int num;
8529 /* Check current state. */
8530 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8531 block = gfc_state_stack->previous->sym;
8532 gcc_assert (block);
8534 /* Try to match PROCEDURE(interface). */
8535 if (gfc_match (" (") == MATCH_YES)
8537 m = gfc_match_name (target_buf);
8538 if (m == MATCH_ERROR)
8539 return m;
8540 if (m != MATCH_YES)
8542 gfc_error ("Interface-name expected after %<(%> at %C");
8543 return MATCH_ERROR;
8546 if (gfc_match (" )") != MATCH_YES)
8548 gfc_error ("%<)%> expected at %C");
8549 return MATCH_ERROR;
8552 ifc = target_buf;
8555 /* Construct the data structure. */
8556 memset (&tb, 0, sizeof (tb));
8557 tb.where = gfc_current_locus;
8559 /* Match binding attributes. */
8560 m = match_binding_attributes (&tb, false, false);
8561 if (m == MATCH_ERROR)
8562 return m;
8563 seen_attrs = (m == MATCH_YES);
8565 /* Check that attribute DEFERRED is given if an interface is specified. */
8566 if (tb.deferred && !ifc)
8568 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8569 return MATCH_ERROR;
8571 if (ifc && !tb.deferred)
8573 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8574 return MATCH_ERROR;
8577 /* Match the colons. */
8578 m = gfc_match (" ::");
8579 if (m == MATCH_ERROR)
8580 return m;
8581 seen_colons = (m == MATCH_YES);
8582 if (seen_attrs && !seen_colons)
8584 gfc_error ("Expected %<::%> after binding-attributes at %C");
8585 return MATCH_ERROR;
8588 /* Match the binding names. */
8589 for(num=1;;num++)
8591 m = gfc_match_name (name);
8592 if (m == MATCH_ERROR)
8593 return m;
8594 if (m == MATCH_NO)
8596 gfc_error ("Expected binding name at %C");
8597 return MATCH_ERROR;
8600 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8601 return MATCH_ERROR;
8603 /* Try to match the '=> target', if it's there. */
8604 target = ifc;
8605 m = gfc_match (" =>");
8606 if (m == MATCH_ERROR)
8607 return m;
8608 if (m == MATCH_YES)
8610 if (tb.deferred)
8612 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8613 return MATCH_ERROR;
8616 if (!seen_colons)
8618 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8619 " at %C");
8620 return MATCH_ERROR;
8623 m = gfc_match_name (target_buf);
8624 if (m == MATCH_ERROR)
8625 return m;
8626 if (m == MATCH_NO)
8628 gfc_error ("Expected binding target after %<=>%> at %C");
8629 return MATCH_ERROR;
8631 target = target_buf;
8634 /* If no target was found, it has the same name as the binding. */
8635 if (!target)
8636 target = name;
8638 /* Get the namespace to insert the symbols into. */
8639 ns = block->f2k_derived;
8640 gcc_assert (ns);
8642 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8643 if (tb.deferred && !block->attr.abstract)
8645 gfc_error ("Type %qs containing DEFERRED binding at %C "
8646 "is not ABSTRACT", block->name);
8647 return MATCH_ERROR;
8650 /* See if we already have a binding with this name in the symtree which
8651 would be an error. If a GENERIC already targeted this binding, it may
8652 be already there but then typebound is still NULL. */
8653 stree = gfc_find_symtree (ns->tb_sym_root, name);
8654 if (stree && stree->n.tb)
8656 gfc_error ("There is already a procedure with binding name %qs for "
8657 "the derived type %qs at %C", name, block->name);
8658 return MATCH_ERROR;
8661 /* Insert it and set attributes. */
8663 if (!stree)
8665 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8666 gcc_assert (stree);
8668 stree->n.tb = gfc_get_typebound_proc (&tb);
8670 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8671 false))
8672 return MATCH_ERROR;
8673 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8675 if (gfc_match_eos () == MATCH_YES)
8676 return MATCH_YES;
8677 if (gfc_match_char (',') != MATCH_YES)
8678 goto syntax;
8681 syntax:
8682 gfc_error ("Syntax error in PROCEDURE statement at %C");
8683 return MATCH_ERROR;
8687 /* Match a GENERIC procedure binding inside a derived type. */
8689 match
8690 gfc_match_generic (void)
8692 char name[GFC_MAX_SYMBOL_LEN + 1];
8693 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8694 gfc_symbol* block;
8695 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8696 gfc_typebound_proc* tb;
8697 gfc_namespace* ns;
8698 interface_type op_type;
8699 gfc_intrinsic_op op;
8700 match m;
8702 /* Check current state. */
8703 if (gfc_current_state () == COMP_DERIVED)
8705 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8706 return MATCH_ERROR;
8708 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8709 return MATCH_NO;
8710 block = gfc_state_stack->previous->sym;
8711 ns = block->f2k_derived;
8712 gcc_assert (block && ns);
8714 memset (&tbattr, 0, sizeof (tbattr));
8715 tbattr.where = gfc_current_locus;
8717 /* See if we get an access-specifier. */
8718 m = match_binding_attributes (&tbattr, true, false);
8719 if (m == MATCH_ERROR)
8720 goto error;
8722 /* Now the colons, those are required. */
8723 if (gfc_match (" ::") != MATCH_YES)
8725 gfc_error ("Expected %<::%> at %C");
8726 goto error;
8729 /* Match the binding name; depending on type (operator / generic) format
8730 it for future error messages into bind_name. */
8732 m = gfc_match_generic_spec (&op_type, name, &op);
8733 if (m == MATCH_ERROR)
8734 return MATCH_ERROR;
8735 if (m == MATCH_NO)
8737 gfc_error ("Expected generic name or operator descriptor at %C");
8738 goto error;
8741 switch (op_type)
8743 case INTERFACE_GENERIC:
8744 snprintf (bind_name, sizeof (bind_name), "%s", name);
8745 break;
8747 case INTERFACE_USER_OP:
8748 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8749 break;
8751 case INTERFACE_INTRINSIC_OP:
8752 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8753 gfc_op2string (op));
8754 break;
8756 case INTERFACE_NAMELESS:
8757 gfc_error ("Malformed GENERIC statement at %C");
8758 goto error;
8759 break;
8761 default:
8762 gcc_unreachable ();
8765 /* Match the required =>. */
8766 if (gfc_match (" =>") != MATCH_YES)
8768 gfc_error ("Expected %<=>%> at %C");
8769 goto error;
8772 /* Try to find existing GENERIC binding with this name / for this operator;
8773 if there is something, check that it is another GENERIC and then extend
8774 it rather than building a new node. Otherwise, create it and put it
8775 at the right position. */
8777 switch (op_type)
8779 case INTERFACE_USER_OP:
8780 case INTERFACE_GENERIC:
8782 const bool is_op = (op_type == INTERFACE_USER_OP);
8783 gfc_symtree* st;
8785 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8786 if (st)
8788 tb = st->n.tb;
8789 gcc_assert (tb);
8791 else
8792 tb = NULL;
8794 break;
8797 case INTERFACE_INTRINSIC_OP:
8798 tb = ns->tb_op[op];
8799 break;
8801 default:
8802 gcc_unreachable ();
8805 if (tb)
8807 if (!tb->is_generic)
8809 gcc_assert (op_type == INTERFACE_GENERIC);
8810 gfc_error ("There's already a non-generic procedure with binding name"
8811 " %qs for the derived type %qs at %C",
8812 bind_name, block->name);
8813 goto error;
8816 if (tb->access != tbattr.access)
8818 gfc_error ("Binding at %C must have the same access as already"
8819 " defined binding %qs", bind_name);
8820 goto error;
8823 else
8825 tb = gfc_get_typebound_proc (NULL);
8826 tb->where = gfc_current_locus;
8827 tb->access = tbattr.access;
8828 tb->is_generic = 1;
8829 tb->u.generic = NULL;
8831 switch (op_type)
8833 case INTERFACE_GENERIC:
8834 case INTERFACE_USER_OP:
8836 const bool is_op = (op_type == INTERFACE_USER_OP);
8837 gfc_symtree* st;
8839 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8840 name);
8841 gcc_assert (st);
8842 st->n.tb = tb;
8844 break;
8847 case INTERFACE_INTRINSIC_OP:
8848 ns->tb_op[op] = tb;
8849 break;
8851 default:
8852 gcc_unreachable ();
8856 /* Now, match all following names as specific targets. */
8859 gfc_symtree* target_st;
8860 gfc_tbp_generic* target;
8862 m = gfc_match_name (name);
8863 if (m == MATCH_ERROR)
8864 goto error;
8865 if (m == MATCH_NO)
8867 gfc_error ("Expected specific binding name at %C");
8868 goto error;
8871 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8873 /* See if this is a duplicate specification. */
8874 for (target = tb->u.generic; target; target = target->next)
8875 if (target_st == target->specific_st)
8877 gfc_error ("%qs already defined as specific binding for the"
8878 " generic %qs at %C", name, bind_name);
8879 goto error;
8882 target = gfc_get_tbp_generic ();
8883 target->specific_st = target_st;
8884 target->specific = NULL;
8885 target->next = tb->u.generic;
8886 target->is_operator = ((op_type == INTERFACE_USER_OP)
8887 || (op_type == INTERFACE_INTRINSIC_OP));
8888 tb->u.generic = target;
8890 while (gfc_match (" ,") == MATCH_YES);
8892 /* Here should be the end. */
8893 if (gfc_match_eos () != MATCH_YES)
8895 gfc_error ("Junk after GENERIC binding at %C");
8896 goto error;
8899 return MATCH_YES;
8901 error:
8902 return MATCH_ERROR;
8906 /* Match a FINAL declaration inside a derived type. */
8908 match
8909 gfc_match_final_decl (void)
8911 char name[GFC_MAX_SYMBOL_LEN + 1];
8912 gfc_symbol* sym;
8913 match m;
8914 gfc_namespace* module_ns;
8915 bool first, last;
8916 gfc_symbol* block;
8918 if (gfc_current_form == FORM_FREE)
8920 char c = gfc_peek_ascii_char ();
8921 if (!gfc_is_whitespace (c) && c != ':')
8922 return MATCH_NO;
8925 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8927 if (gfc_current_form == FORM_FIXED)
8928 return MATCH_NO;
8930 gfc_error ("FINAL declaration at %C must be inside a derived type "
8931 "CONTAINS section");
8932 return MATCH_ERROR;
8935 block = gfc_state_stack->previous->sym;
8936 gcc_assert (block);
8938 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8939 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8941 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8942 " specification part of a MODULE");
8943 return MATCH_ERROR;
8946 module_ns = gfc_current_ns;
8947 gcc_assert (module_ns);
8948 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8950 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8951 if (gfc_match (" ::") == MATCH_ERROR)
8952 return MATCH_ERROR;
8954 /* Match the sequence of procedure names. */
8955 first = true;
8956 last = false;
8959 gfc_finalizer* f;
8961 if (first && gfc_match_eos () == MATCH_YES)
8963 gfc_error ("Empty FINAL at %C");
8964 return MATCH_ERROR;
8967 m = gfc_match_name (name);
8968 if (m == MATCH_NO)
8970 gfc_error ("Expected module procedure name at %C");
8971 return MATCH_ERROR;
8973 else if (m != MATCH_YES)
8974 return MATCH_ERROR;
8976 if (gfc_match_eos () == MATCH_YES)
8977 last = true;
8978 if (!last && gfc_match_char (',') != MATCH_YES)
8980 gfc_error ("Expected %<,%> at %C");
8981 return MATCH_ERROR;
8984 if (gfc_get_symbol (name, module_ns, &sym))
8986 gfc_error ("Unknown procedure name %qs at %C", name);
8987 return MATCH_ERROR;
8990 /* Mark the symbol as module procedure. */
8991 if (sym->attr.proc != PROC_MODULE
8992 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8993 return MATCH_ERROR;
8995 /* Check if we already have this symbol in the list, this is an error. */
8996 for (f = block->f2k_derived->finalizers; f; f = f->next)
8997 if (f->proc_sym == sym)
8999 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9000 name);
9001 return MATCH_ERROR;
9004 /* Add this symbol to the list of finalizers. */
9005 gcc_assert (block->f2k_derived);
9006 ++sym->refs;
9007 f = XCNEW (gfc_finalizer);
9008 f->proc_sym = sym;
9009 f->proc_tree = NULL;
9010 f->where = gfc_current_locus;
9011 f->next = block->f2k_derived->finalizers;
9012 block->f2k_derived->finalizers = f;
9014 first = false;
9016 while (!last);
9018 return MATCH_YES;
9022 const ext_attr_t ext_attr_list[] = {
9023 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9024 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9025 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9026 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9027 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9028 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9029 { NULL, EXT_ATTR_LAST, NULL }
9032 /* Match a !GCC$ ATTRIBUTES statement of the form:
9033 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9034 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9036 TODO: We should support all GCC attributes using the same syntax for
9037 the attribute list, i.e. the list in C
9038 __attributes(( attribute-list ))
9039 matches then
9040 !GCC$ ATTRIBUTES attribute-list ::
9041 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9042 saved into a TREE.
9044 As there is absolutely no risk of confusion, we should never return
9045 MATCH_NO. */
9046 match
9047 gfc_match_gcc_attributes (void)
9049 symbol_attribute attr;
9050 char name[GFC_MAX_SYMBOL_LEN + 1];
9051 unsigned id;
9052 gfc_symbol *sym;
9053 match m;
9055 gfc_clear_attr (&attr);
9056 for(;;)
9058 char ch;
9060 if (gfc_match_name (name) != MATCH_YES)
9061 return MATCH_ERROR;
9063 for (id = 0; id < EXT_ATTR_LAST; id++)
9064 if (strcmp (name, ext_attr_list[id].name) == 0)
9065 break;
9067 if (id == EXT_ATTR_LAST)
9069 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9070 return MATCH_ERROR;
9073 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
9074 return MATCH_ERROR;
9076 gfc_gobble_whitespace ();
9077 ch = gfc_next_ascii_char ();
9078 if (ch == ':')
9080 /* This is the successful exit condition for the loop. */
9081 if (gfc_next_ascii_char () == ':')
9082 break;
9085 if (ch == ',')
9086 continue;
9088 goto syntax;
9091 if (gfc_match_eos () == MATCH_YES)
9092 goto syntax;
9094 for(;;)
9096 m = gfc_match_name (name);
9097 if (m != MATCH_YES)
9098 return m;
9100 if (find_special (name, &sym, true))
9101 return MATCH_ERROR;
9103 sym->attr.ext_attr |= attr.ext_attr;
9105 if (gfc_match_eos () == MATCH_YES)
9106 break;
9108 if (gfc_match_char (',') != MATCH_YES)
9109 goto syntax;
9112 return MATCH_YES;
9114 syntax:
9115 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9116 return MATCH_ERROR;