[AArch64] Tweak sve/vcond_6.c test
[official-gcc.git] / gcc / fortran / decl.c
blobd1b75ccd54c750f0fbf7f9bdd3ade01deaca56eb
1 /* Declaration statement matcher
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "stringpool.h"
28 #include "match.h"
29 #include "parse.h"
30 #include "constructor.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts;
54 static symbol_attribute current_attr;
55 static gfc_array_spec *current_as;
56 static int colon_seen;
57 static int attr_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;
98 /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
99 int directive_unroll = -1;
101 /* If a kind expression of a component of a parameterized derived type is
102 parameterized, temporarily store the expression here. */
103 static gfc_expr *saved_kind_expr = NULL;
105 /* Used to store the parameter list arising in a PDT declaration and
106 in the typespec of a PDT variable or component. */
107 static gfc_actual_arglist *decl_type_param_list;
108 static gfc_actual_arglist *type_param_spec_list;
110 /********************* DATA statement subroutines *********************/
112 static bool in_match_data = false;
114 bool
115 gfc_in_match_data (void)
117 return in_match_data;
120 static void
121 set_in_match_data (bool set_value)
123 in_match_data = set_value;
126 /* Free a gfc_data_variable structure and everything beneath it. */
128 static void
129 free_variable (gfc_data_variable *p)
131 gfc_data_variable *q;
133 for (; p; p = q)
135 q = p->next;
136 gfc_free_expr (p->expr);
137 gfc_free_iterator (&p->iter, 0);
138 free_variable (p->list);
139 free (p);
144 /* Free a gfc_data_value structure and everything beneath it. */
146 static void
147 free_value (gfc_data_value *p)
149 gfc_data_value *q;
151 for (; p; p = q)
153 q = p->next;
154 mpz_clear (p->repeat);
155 gfc_free_expr (p->expr);
156 free (p);
161 /* Free a list of gfc_data structures. */
163 void
164 gfc_free_data (gfc_data *p)
166 gfc_data *q;
168 for (; p; p = q)
170 q = p->next;
171 free_variable (p->var);
172 free_value (p->value);
173 free (p);
178 /* Free all data in a namespace. */
180 static void
181 gfc_free_data_all (gfc_namespace *ns)
183 gfc_data *d;
185 for (;ns->data;)
187 d = ns->data->next;
188 free (ns->data);
189 ns->data = d;
193 /* Reject data parsed since the last restore point was marked. */
195 void
196 gfc_reject_data (gfc_namespace *ns)
198 gfc_data *d;
200 while (ns->data && ns->data != ns->old_data)
202 d = ns->data->next;
203 free (ns->data);
204 ns->data = d;
208 static match var_element (gfc_data_variable *);
210 /* Match a list of variables terminated by an iterator and a right
211 parenthesis. */
213 static match
214 var_list (gfc_data_variable *parent)
216 gfc_data_variable *tail, var;
217 match m;
219 m = var_element (&var);
220 if (m == MATCH_ERROR)
221 return MATCH_ERROR;
222 if (m == MATCH_NO)
223 goto syntax;
225 tail = gfc_get_data_variable ();
226 *tail = var;
228 parent->list = tail;
230 for (;;)
232 if (gfc_match_char (',') != MATCH_YES)
233 goto syntax;
235 m = gfc_match_iterator (&parent->iter, 1);
236 if (m == MATCH_YES)
237 break;
238 if (m == MATCH_ERROR)
239 return MATCH_ERROR;
241 m = var_element (&var);
242 if (m == MATCH_ERROR)
243 return MATCH_ERROR;
244 if (m == MATCH_NO)
245 goto syntax;
247 tail->next = gfc_get_data_variable ();
248 tail = tail->next;
250 *tail = var;
253 if (gfc_match_char (')') != MATCH_YES)
254 goto syntax;
255 return MATCH_YES;
257 syntax:
258 gfc_syntax_error (ST_DATA);
259 return MATCH_ERROR;
263 /* Match a single element in a data variable list, which can be a
264 variable-iterator list. */
266 static match
267 var_element (gfc_data_variable *new_var)
269 match m;
270 gfc_symbol *sym;
272 memset (new_var, 0, sizeof (gfc_data_variable));
274 if (gfc_match_char ('(') == MATCH_YES)
275 return var_list (new_var);
277 m = gfc_match_variable (&new_var->expr, 0);
278 if (m != MATCH_YES)
279 return m;
281 sym = new_var->expr->symtree->n.sym;
283 /* Symbol should already have an associated type. */
284 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
285 return MATCH_ERROR;
287 if (!sym->attr.function && gfc_current_ns->parent
288 && gfc_current_ns->parent == sym->ns)
290 gfc_error ("Host associated variable %qs may not be in the DATA "
291 "statement at %C", sym->name);
292 return MATCH_ERROR;
295 if (gfc_current_state () != COMP_BLOCK_DATA
296 && sym->attr.in_common
297 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
298 "common block variable %qs in DATA statement at %C",
299 sym->name))
300 return MATCH_ERROR;
302 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
303 return MATCH_ERROR;
305 return MATCH_YES;
309 /* Match the top-level list of data variables. */
311 static match
312 top_var_list (gfc_data *d)
314 gfc_data_variable var, *tail, *new_var;
315 match m;
317 tail = NULL;
319 for (;;)
321 m = var_element (&var);
322 if (m == MATCH_NO)
323 goto syntax;
324 if (m == MATCH_ERROR)
325 return MATCH_ERROR;
327 new_var = gfc_get_data_variable ();
328 *new_var = var;
330 if (tail == NULL)
331 d->var = new_var;
332 else
333 tail->next = new_var;
335 tail = new_var;
337 if (gfc_match_char ('/') == MATCH_YES)
338 break;
339 if (gfc_match_char (',') != MATCH_YES)
340 goto syntax;
343 return MATCH_YES;
345 syntax:
346 gfc_syntax_error (ST_DATA);
347 gfc_free_data_all (gfc_current_ns);
348 return MATCH_ERROR;
352 static match
353 match_data_constant (gfc_expr **result)
355 char name[GFC_MAX_SYMBOL_LEN + 1];
356 gfc_symbol *sym, *dt_sym = NULL;
357 gfc_expr *expr;
358 match m;
359 locus old_loc;
361 m = gfc_match_literal_constant (&expr, 1);
362 if (m == MATCH_YES)
364 *result = expr;
365 return MATCH_YES;
368 if (m == MATCH_ERROR)
369 return MATCH_ERROR;
371 m = gfc_match_null (result);
372 if (m != MATCH_NO)
373 return m;
375 old_loc = gfc_current_locus;
377 /* Should this be a structure component, try to match it
378 before matching a name. */
379 m = gfc_match_rvalue (result);
380 if (m == MATCH_ERROR)
381 return m;
383 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
385 if (!gfc_simplify_expr (*result, 0))
386 m = MATCH_ERROR;
387 return m;
389 else if (m == MATCH_YES)
390 gfc_free_expr (*result);
392 gfc_current_locus = old_loc;
394 m = gfc_match_name (name);
395 if (m != MATCH_YES)
396 return m;
398 if (gfc_find_symbol (name, NULL, 1, &sym))
399 return MATCH_ERROR;
401 if (sym && sym->attr.generic)
402 dt_sym = gfc_find_dt_in_generic (sym);
404 if (sym == NULL
405 || (sym->attr.flavor != FL_PARAMETER
406 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
408 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
409 name);
410 *result = NULL;
411 return MATCH_ERROR;
413 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
414 return gfc_match_structure_constructor (dt_sym, result);
416 /* Check to see if the value is an initialization array expression. */
417 if (sym->value->expr_type == EXPR_ARRAY)
419 gfc_current_locus = old_loc;
421 m = gfc_match_init_expr (result);
422 if (m == MATCH_ERROR)
423 return m;
425 if (m == MATCH_YES)
427 if (!gfc_simplify_expr (*result, 0))
428 m = MATCH_ERROR;
430 if ((*result)->expr_type == EXPR_CONSTANT)
431 return m;
432 else
434 gfc_error ("Invalid initializer %s in Data statement at %C", name);
435 return MATCH_ERROR;
440 *result = gfc_copy_expr (sym->value);
441 return MATCH_YES;
445 /* Match a list of values in a DATA statement. The leading '/' has
446 already been seen at this point. */
448 static match
449 top_val_list (gfc_data *data)
451 gfc_data_value *new_val, *tail;
452 gfc_expr *expr;
453 match m;
455 tail = NULL;
457 for (;;)
459 m = match_data_constant (&expr);
460 if (m == MATCH_NO)
461 goto syntax;
462 if (m == MATCH_ERROR)
463 return MATCH_ERROR;
465 new_val = gfc_get_data_value ();
466 mpz_init (new_val->repeat);
468 if (tail == NULL)
469 data->value = new_val;
470 else
471 tail->next = new_val;
473 tail = new_val;
475 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
477 tail->expr = expr;
478 mpz_set_ui (tail->repeat, 1);
480 else
482 mpz_set (tail->repeat, expr->value.integer);
483 gfc_free_expr (expr);
485 m = match_data_constant (&tail->expr);
486 if (m == MATCH_NO)
487 goto syntax;
488 if (m == MATCH_ERROR)
489 return MATCH_ERROR;
492 if (gfc_match_char ('/') == MATCH_YES)
493 break;
494 if (gfc_match_char (',') == MATCH_NO)
495 goto syntax;
498 return MATCH_YES;
500 syntax:
501 gfc_syntax_error (ST_DATA);
502 gfc_free_data_all (gfc_current_ns);
503 return MATCH_ERROR;
507 /* Matches an old style initialization. */
509 static match
510 match_old_style_init (const char *name)
512 match m;
513 gfc_symtree *st;
514 gfc_symbol *sym;
515 gfc_data *newdata;
517 /* Set up data structure to hold initializers. */
518 gfc_find_sym_tree (name, NULL, 0, &st);
519 sym = st->n.sym;
521 newdata = gfc_get_data ();
522 newdata->var = gfc_get_data_variable ();
523 newdata->var->expr = gfc_get_variable_expr (st);
524 newdata->where = gfc_current_locus;
526 /* Match initial value list. This also eats the terminal '/'. */
527 m = top_val_list (newdata);
528 if (m != MATCH_YES)
530 free (newdata);
531 return m;
534 if (gfc_pure (NULL))
536 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
537 free (newdata);
538 return MATCH_ERROR;
540 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
542 /* Mark the variable as having appeared in a data statement. */
543 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
545 free (newdata);
546 return MATCH_ERROR;
549 /* Chain in namespace list of DATA initializers. */
550 newdata->next = gfc_current_ns->data;
551 gfc_current_ns->data = newdata;
553 return m;
557 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
558 we are matching a DATA statement and are therefore issuing an error
559 if we encounter something unexpected, if not, we're trying to match
560 an old-style initialization expression of the form INTEGER I /2/. */
562 match
563 gfc_match_data (void)
565 gfc_data *new_data;
566 match m;
568 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
569 if ((gfc_current_state () == COMP_FUNCTION
570 || gfc_current_state () == COMP_SUBROUTINE)
571 && gfc_state_stack->previous->state == COMP_INTERFACE)
573 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
574 return MATCH_ERROR;
577 set_in_match_data (true);
579 for (;;)
581 new_data = gfc_get_data ();
582 new_data->where = gfc_current_locus;
584 m = top_var_list (new_data);
585 if (m != MATCH_YES)
586 goto cleanup;
588 if (new_data->var->iter.var
589 && new_data->var->iter.var->ts.type == BT_INTEGER
590 && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
591 && new_data->var->list
592 && new_data->var->list->expr
593 && new_data->var->list->expr->ts.type == BT_CHARACTER
594 && new_data->var->list->expr->ref
595 && new_data->var->list->expr->ref->type == REF_SUBSTRING)
597 gfc_error ("Invalid substring in data-implied-do at %L in DATA "
598 "statement", &new_data->var->list->expr->where);
599 goto cleanup;
602 m = top_val_list (new_data);
603 if (m != MATCH_YES)
604 goto cleanup;
606 new_data->next = gfc_current_ns->data;
607 gfc_current_ns->data = new_data;
609 if (gfc_match_eos () == MATCH_YES)
610 break;
612 gfc_match_char (','); /* Optional comma */
615 set_in_match_data (false);
617 if (gfc_pure (NULL))
619 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
620 return MATCH_ERROR;
622 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
624 return MATCH_YES;
626 cleanup:
627 set_in_match_data (false);
628 gfc_free_data (new_data);
629 return MATCH_ERROR;
633 /************************ Declaration statements *********************/
636 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
637 list). The difference here is the expression is a list of constants
638 and is surrounded by '/'.
639 The typespec ts must match the typespec of the variable which the
640 clist is initializing.
641 The arrayspec tells whether this should match a list of constants
642 corresponding to array elements or a scalar (as == NULL). */
644 static match
645 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
647 gfc_constructor_base array_head = NULL;
648 gfc_expr *expr = NULL;
649 match m;
650 locus where;
651 mpz_t repeat, cons_size, as_size;
652 bool scalar;
653 int cmp;
655 gcc_assert (ts);
657 mpz_init_set_ui (repeat, 0);
658 scalar = !as || !as->rank;
660 /* We have already matched '/' - now look for a constant list, as with
661 top_val_list from decl.c, but append the result to an array. */
662 if (gfc_match ("/") == MATCH_YES)
664 gfc_error ("Empty old style initializer list at %C");
665 goto cleanup;
668 where = gfc_current_locus;
669 for (;;)
671 m = match_data_constant (&expr);
672 if (m != MATCH_YES)
673 expr = NULL; /* match_data_constant may set expr to garbage */
674 if (m == MATCH_NO)
675 goto syntax;
676 if (m == MATCH_ERROR)
677 goto cleanup;
679 /* Found r in repeat spec r*c; look for the constant to repeat. */
680 if ( gfc_match_char ('*') == MATCH_YES)
682 if (scalar)
684 gfc_error ("Repeat spec invalid in scalar initializer at %C");
685 goto cleanup;
687 if (expr->ts.type != BT_INTEGER)
689 gfc_error ("Repeat spec must be an integer at %C");
690 goto cleanup;
692 mpz_set (repeat, expr->value.integer);
693 gfc_free_expr (expr);
694 expr = NULL;
696 m = match_data_constant (&expr);
697 if (m == MATCH_NO)
698 gfc_error ("Expected data constant after repeat spec at %C");
699 if (m != MATCH_YES)
700 goto cleanup;
702 /* No repeat spec, we matched the data constant itself. */
703 else
704 mpz_set_ui (repeat, 1);
706 if (!scalar)
708 /* Add the constant initializer as many times as repeated. */
709 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
711 /* Make sure types of elements match */
712 if(ts && !gfc_compare_types (&expr->ts, ts)
713 && !gfc_convert_type (expr, ts, 1))
714 goto cleanup;
716 gfc_constructor_append_expr (&array_head,
717 gfc_copy_expr (expr), &gfc_current_locus);
720 gfc_free_expr (expr);
721 expr = NULL;
724 /* For scalar initializers quit after one element. */
725 else
727 if(gfc_match_char ('/') != MATCH_YES)
729 gfc_error ("End of scalar initializer expected at %C");
730 goto cleanup;
732 break;
735 if (gfc_match_char ('/') == MATCH_YES)
736 break;
737 if (gfc_match_char (',') == MATCH_NO)
738 goto syntax;
741 /* Set up expr as an array constructor. */
742 if (!scalar)
744 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
745 expr->ts = *ts;
746 expr->value.constructor = array_head;
748 expr->rank = as->rank;
749 expr->shape = gfc_get_shape (expr->rank);
751 /* Validate sizes. We built expr ourselves, so cons_size will be
752 constant (we fail above for non-constant expressions).
753 We still need to verify that the array-spec has constant size. */
754 cmp = 0;
755 gcc_assert (gfc_array_size (expr, &cons_size));
756 if (!spec_size (as, &as_size))
758 gfc_error ("Expected constant array-spec in initializer list at %L",
759 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
760 cmp = -1;
762 else
764 /* Make sure the specs are of the same size. */
765 cmp = mpz_cmp (cons_size, as_size);
766 if (cmp < 0)
767 gfc_error ("Not enough elements in array initializer at %C");
768 else if (cmp > 0)
769 gfc_error ("Too many elements in array initializer at %C");
770 mpz_clear (as_size);
772 mpz_clear (cons_size);
773 if (cmp)
774 goto cleanup;
777 /* Make sure scalar types match. */
778 else if (!gfc_compare_types (&expr->ts, ts)
779 && !gfc_convert_type (expr, ts, 1))
780 goto cleanup;
782 if (expr->ts.u.cl)
783 expr->ts.u.cl->length_from_typespec = 1;
785 *result = expr;
786 mpz_clear (repeat);
787 return MATCH_YES;
789 syntax:
790 gfc_error ("Syntax error in old style initializer list at %C");
792 cleanup:
793 if (expr)
794 expr->value.constructor = NULL;
795 gfc_free_expr (expr);
796 gfc_constructor_free (array_head);
797 mpz_clear (repeat);
798 return MATCH_ERROR;
802 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
804 static bool
805 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
807 int i, j;
809 if ((from->type == AS_ASSUMED_RANK && to->corank)
810 || (to->type == AS_ASSUMED_RANK && from->corank))
812 gfc_error ("The assumed-rank array at %C shall not have a codimension");
813 return false;
816 if (to->rank == 0 && from->rank > 0)
818 to->rank = from->rank;
819 to->type = from->type;
820 to->cray_pointee = from->cray_pointee;
821 to->cp_was_assumed = from->cp_was_assumed;
823 for (i = 0; i < to->corank; i++)
825 /* Do not exceed the limits on lower[] and upper[]. gfortran
826 cleans up elsewhere. */
827 j = from->rank + i;
828 if (j >= GFC_MAX_DIMENSIONS)
829 break;
831 to->lower[j] = to->lower[i];
832 to->upper[j] = to->upper[i];
834 for (i = 0; i < from->rank; i++)
836 if (copy)
838 to->lower[i] = gfc_copy_expr (from->lower[i]);
839 to->upper[i] = gfc_copy_expr (from->upper[i]);
841 else
843 to->lower[i] = from->lower[i];
844 to->upper[i] = from->upper[i];
848 else if (to->corank == 0 && from->corank > 0)
850 to->corank = from->corank;
851 to->cotype = from->cotype;
853 for (i = 0; i < from->corank; i++)
855 /* Do not exceed the limits on lower[] and upper[]. gfortran
856 cleans up elsewhere. */
857 j = to->rank + i;
858 if (j >= GFC_MAX_DIMENSIONS)
859 break;
861 if (copy)
863 to->lower[j] = gfc_copy_expr (from->lower[i]);
864 to->upper[j] = gfc_copy_expr (from->upper[i]);
866 else
868 to->lower[j] = from->lower[i];
869 to->upper[j] = from->upper[i];
874 if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
876 gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
877 "allowed dimensions of %d",
878 to->rank, to->corank, GFC_MAX_DIMENSIONS);
879 to->corank = GFC_MAX_DIMENSIONS - to->rank;
880 return false;
882 return true;
886 /* Match an intent specification. Since this can only happen after an
887 INTENT word, a legal intent-spec must follow. */
889 static sym_intent
890 match_intent_spec (void)
893 if (gfc_match (" ( in out )") == MATCH_YES)
894 return INTENT_INOUT;
895 if (gfc_match (" ( in )") == MATCH_YES)
896 return INTENT_IN;
897 if (gfc_match (" ( out )") == MATCH_YES)
898 return INTENT_OUT;
900 gfc_error ("Bad INTENT specification at %C");
901 return INTENT_UNKNOWN;
905 /* Matches a character length specification, which is either a
906 specification expression, '*', or ':'. */
908 static match
909 char_len_param_value (gfc_expr **expr, bool *deferred)
911 match m;
913 *expr = NULL;
914 *deferred = false;
916 if (gfc_match_char ('*') == MATCH_YES)
917 return MATCH_YES;
919 if (gfc_match_char (':') == MATCH_YES)
921 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
922 return MATCH_ERROR;
924 *deferred = true;
926 return MATCH_YES;
929 m = gfc_match_expr (expr);
931 if (m == MATCH_NO || m == MATCH_ERROR)
932 return m;
934 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
935 return MATCH_ERROR;
937 if ((*expr)->expr_type == EXPR_FUNCTION)
939 if ((*expr)->ts.type == BT_INTEGER
940 || ((*expr)->ts.type == BT_UNKNOWN
941 && strcmp((*expr)->symtree->name, "null") != 0))
942 return MATCH_YES;
944 goto syntax;
946 else if ((*expr)->expr_type == EXPR_CONSTANT)
948 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
949 processor dependent and its value is greater than or equal to zero.
950 F2008, 4.4.3.2: If the character length parameter value evaluates
951 to a negative value, the length of character entities declared
952 is zero. */
954 if ((*expr)->ts.type == BT_INTEGER)
956 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
957 mpz_set_si ((*expr)->value.integer, 0);
959 else
960 goto syntax;
962 else if ((*expr)->expr_type == EXPR_ARRAY)
963 goto syntax;
964 else if ((*expr)->expr_type == EXPR_VARIABLE)
966 bool t;
967 gfc_expr *e;
969 e = gfc_copy_expr (*expr);
971 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
972 which causes an ICE if gfc_reduce_init_expr() is called. */
973 if (e->ref && e->ref->type == REF_ARRAY
974 && e->ref->u.ar.type == AR_UNKNOWN
975 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
976 goto syntax;
978 t = gfc_reduce_init_expr (e);
980 if (!t && e->ts.type == BT_UNKNOWN
981 && e->symtree->n.sym->attr.untyped == 1
982 && (flag_implicit_none
983 || e->symtree->n.sym->ns->seen_implicit_none == 1
984 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
986 gfc_free_expr (e);
987 goto syntax;
990 if ((e->ref && e->ref->type == REF_ARRAY
991 && e->ref->u.ar.type != AR_ELEMENT)
992 || (!e->ref && e->expr_type == EXPR_ARRAY))
994 gfc_free_expr (e);
995 goto syntax;
998 gfc_free_expr (e);
1001 return m;
1003 syntax:
1004 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1005 return MATCH_ERROR;
1009 /* A character length is a '*' followed by a literal integer or a
1010 char_len_param_value in parenthesis. */
1012 static match
1013 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1015 int length;
1016 match m;
1018 *deferred = false;
1019 m = gfc_match_char ('*');
1020 if (m != MATCH_YES)
1021 return m;
1023 m = gfc_match_small_literal_int (&length, NULL);
1024 if (m == MATCH_ERROR)
1025 return m;
1027 if (m == MATCH_YES)
1029 if (obsolescent_check
1030 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1031 return MATCH_ERROR;
1032 *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1033 return m;
1036 if (gfc_match_char ('(') == MATCH_NO)
1037 goto syntax;
1039 m = char_len_param_value (expr, deferred);
1040 if (m != MATCH_YES && gfc_matching_function)
1042 gfc_undo_symbols ();
1043 m = MATCH_YES;
1046 if (m == MATCH_ERROR)
1047 return m;
1048 if (m == MATCH_NO)
1049 goto syntax;
1051 if (gfc_match_char (')') == MATCH_NO)
1053 gfc_free_expr (*expr);
1054 *expr = NULL;
1055 goto syntax;
1058 return MATCH_YES;
1060 syntax:
1061 gfc_error ("Syntax error in character length specification at %C");
1062 return MATCH_ERROR;
1066 /* Special subroutine for finding a symbol. Check if the name is found
1067 in the current name space. If not, and we're compiling a function or
1068 subroutine and the parent compilation unit is an interface, then check
1069 to see if the name we've been given is the name of the interface
1070 (located in another namespace). */
1072 static int
1073 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1075 gfc_state_data *s;
1076 gfc_symtree *st;
1077 int i;
1079 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1080 if (i == 0)
1082 *result = st ? st->n.sym : NULL;
1083 goto end;
1086 if (gfc_current_state () != COMP_SUBROUTINE
1087 && gfc_current_state () != COMP_FUNCTION)
1088 goto end;
1090 s = gfc_state_stack->previous;
1091 if (s == NULL)
1092 goto end;
1094 if (s->state != COMP_INTERFACE)
1095 goto end;
1096 if (s->sym == NULL)
1097 goto end; /* Nameless interface. */
1099 if (strcmp (name, s->sym->name) == 0)
1101 *result = s->sym;
1102 return 0;
1105 end:
1106 return i;
1110 /* Special subroutine for getting a symbol node associated with a
1111 procedure name, used in SUBROUTINE and FUNCTION statements. The
1112 symbol is created in the parent using with symtree node in the
1113 child unit pointing to the symbol. If the current namespace has no
1114 parent, then the symbol is just created in the current unit. */
1116 static int
1117 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1119 gfc_symtree *st;
1120 gfc_symbol *sym;
1121 int rc = 0;
1123 /* Module functions have to be left in their own namespace because
1124 they have potentially (almost certainly!) already been referenced.
1125 In this sense, they are rather like external functions. This is
1126 fixed up in resolve.c(resolve_entries), where the symbol name-
1127 space is set to point to the master function, so that the fake
1128 result mechanism can work. */
1129 if (module_fcn_entry)
1131 /* Present if entry is declared to be a module procedure. */
1132 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1134 if (*result == NULL)
1135 rc = gfc_get_symbol (name, NULL, result);
1136 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1137 && (*result)->ts.type == BT_UNKNOWN
1138 && sym->attr.flavor == FL_UNKNOWN)
1139 /* Pick up the typespec for the entry, if declared in the function
1140 body. Note that this symbol is FL_UNKNOWN because it will
1141 only have appeared in a type declaration. The local symtree
1142 is set to point to the module symbol and a unique symtree
1143 to the local version. This latter ensures a correct clearing
1144 of the symbols. */
1146 /* If the ENTRY proceeds its specification, we need to ensure
1147 that this does not raise a "has no IMPLICIT type" error. */
1148 if (sym->ts.type == BT_UNKNOWN)
1149 sym->attr.untyped = 1;
1151 (*result)->ts = sym->ts;
1153 /* Put the symbol in the procedure namespace so that, should
1154 the ENTRY precede its specification, the specification
1155 can be applied. */
1156 (*result)->ns = gfc_current_ns;
1158 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1159 st->n.sym = *result;
1160 st = gfc_get_unique_symtree (gfc_current_ns);
1161 sym->refs++;
1162 st->n.sym = sym;
1165 else
1166 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1168 if (rc)
1169 return rc;
1171 sym = *result;
1172 if (sym->attr.proc == PROC_ST_FUNCTION)
1173 return rc;
1175 if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1177 /* Create a partially populated interface symbol to carry the
1178 characteristics of the procedure and the result. */
1179 sym->tlink = gfc_new_symbol (name, sym->ns);
1180 gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1181 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1182 if (sym->attr.dimension)
1183 sym->tlink->as = gfc_copy_array_spec (sym->as);
1185 /* Ideally, at this point, a copy would be made of the formal
1186 arguments and their namespace. However, this does not appear
1187 to be necessary, albeit at the expense of not being able to
1188 use gfc_compare_interfaces directly. */
1190 if (sym->result && sym->result != sym)
1192 sym->tlink->result = sym->result;
1193 sym->result = NULL;
1195 else if (sym->result)
1197 sym->tlink->result = sym->tlink;
1200 else if (sym && !sym->gfc_new
1201 && gfc_current_state () != COMP_INTERFACE)
1203 /* Trap another encompassed procedure with the same name. All
1204 these conditions are necessary to avoid picking up an entry
1205 whose name clashes with that of the encompassing procedure;
1206 this is handled using gsymbols to register unique, globally
1207 accessible names. */
1208 if (sym->attr.flavor != 0
1209 && sym->attr.proc != 0
1210 && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1211 && sym->attr.if_source != IFSRC_UNKNOWN)
1212 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1213 name, &sym->declared_at);
1215 if (sym->attr.flavor != 0
1216 && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1217 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1218 name, &sym->declared_at);
1220 if (sym->attr.external && sym->attr.procedure
1221 && gfc_current_state () == COMP_CONTAINS)
1222 gfc_error_now ("Contained procedure %qs at %C clashes with "
1223 "procedure defined at %L",
1224 name, &sym->declared_at);
1226 /* Trap a procedure with a name the same as interface in the
1227 encompassing scope. */
1228 if (sym->attr.generic != 0
1229 && (sym->attr.subroutine || sym->attr.function)
1230 && !sym->attr.mod_proc)
1231 gfc_error_now ("Name %qs at %C is already defined"
1232 " as a generic interface at %L",
1233 name, &sym->declared_at);
1235 /* Trap declarations of attributes in encompassing scope. The
1236 signature for this is that ts.kind is set. Legitimate
1237 references only set ts.type. */
1238 if (sym->ts.kind != 0
1239 && !sym->attr.implicit_type
1240 && sym->attr.proc == 0
1241 && gfc_current_ns->parent != NULL
1242 && sym->attr.access == 0
1243 && !module_fcn_entry)
1244 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1245 "from a previous declaration", name);
1248 /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1249 subroutine-stmt of a module subprogram or of a nonabstract interface
1250 body that is declared in the scoping unit of a module or submodule. */
1251 if (sym->attr.external
1252 && (sym->attr.subroutine || sym->attr.function)
1253 && sym->attr.if_source == IFSRC_IFBODY
1254 && !current_attr.module_procedure
1255 && sym->attr.proc == PROC_MODULE
1256 && gfc_state_stack->state == COMP_CONTAINS)
1257 gfc_error_now ("Procedure %qs defined in interface body at %L "
1258 "clashes with internal procedure defined at %C",
1259 name, &sym->declared_at);
1261 if (sym && !sym->gfc_new
1262 && sym->attr.flavor != FL_UNKNOWN
1263 && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1264 && gfc_state_stack->state == COMP_CONTAINS
1265 && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1266 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1267 name, &sym->declared_at);
1269 if (gfc_current_ns->parent == NULL || *result == NULL)
1270 return rc;
1272 /* Module function entries will already have a symtree in
1273 the current namespace but will need one at module level. */
1274 if (module_fcn_entry)
1276 /* Present if entry is declared to be a module procedure. */
1277 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1278 if (st == NULL)
1279 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1281 else
1282 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1284 st->n.sym = sym;
1285 sym->refs++;
1287 /* See if the procedure should be a module procedure. */
1289 if (((sym->ns->proc_name != NULL
1290 && sym->ns->proc_name->attr.flavor == FL_MODULE
1291 && sym->attr.proc != PROC_MODULE)
1292 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1293 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1294 rc = 2;
1296 return rc;
1300 /* Verify that the given symbol representing a parameter is C
1301 interoperable, by checking to see if it was marked as such after
1302 its declaration. If the given symbol is not interoperable, a
1303 warning is reported, thus removing the need to return the status to
1304 the calling function. The standard does not require the user use
1305 one of the iso_c_binding named constants to declare an
1306 interoperable parameter, but we can't be sure if the param is C
1307 interop or not if the user doesn't. For example, integer(4) may be
1308 legal Fortran, but doesn't have meaning in C. It may interop with
1309 a number of the C types, which causes a problem because the
1310 compiler can't know which one. This code is almost certainly not
1311 portable, and the user will get what they deserve if the C type
1312 across platforms isn't always interoperable with integer(4). If
1313 the user had used something like integer(c_int) or integer(c_long),
1314 the compiler could have automatically handled the varying sizes
1315 across platforms. */
1317 bool
1318 gfc_verify_c_interop_param (gfc_symbol *sym)
1320 int is_c_interop = 0;
1321 bool retval = true;
1323 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1324 Don't repeat the checks here. */
1325 if (sym->attr.implicit_type)
1326 return true;
1328 /* For subroutines or functions that are passed to a BIND(C) procedure,
1329 they're interoperable if they're BIND(C) and their params are all
1330 interoperable. */
1331 if (sym->attr.flavor == FL_PROCEDURE)
1333 if (sym->attr.is_bind_c == 0)
1335 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1336 "attribute to be C interoperable", sym->name,
1337 &(sym->declared_at));
1338 return false;
1340 else
1342 if (sym->attr.is_c_interop == 1)
1343 /* We've already checked this procedure; don't check it again. */
1344 return true;
1345 else
1346 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1347 sym->common_block);
1351 /* See if we've stored a reference to a procedure that owns sym. */
1352 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1354 if (sym->ns->proc_name->attr.is_bind_c == 1)
1356 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1358 if (is_c_interop != 1)
1360 /* Make personalized messages to give better feedback. */
1361 if (sym->ts.type == BT_DERIVED)
1362 gfc_error ("Variable %qs at %L is a dummy argument to the "
1363 "BIND(C) procedure %qs but is not C interoperable "
1364 "because derived type %qs is not C interoperable",
1365 sym->name, &(sym->declared_at),
1366 sym->ns->proc_name->name,
1367 sym->ts.u.derived->name);
1368 else if (sym->ts.type == BT_CLASS)
1369 gfc_error ("Variable %qs at %L is a dummy argument to the "
1370 "BIND(C) procedure %qs but is not C interoperable "
1371 "because it is polymorphic",
1372 sym->name, &(sym->declared_at),
1373 sym->ns->proc_name->name);
1374 else if (warn_c_binding_type)
1375 gfc_warning (OPT_Wc_binding_type,
1376 "Variable %qs at %L is a dummy argument of the "
1377 "BIND(C) procedure %qs but may not be C "
1378 "interoperable",
1379 sym->name, &(sym->declared_at),
1380 sym->ns->proc_name->name);
1383 /* Character strings are only C interoperable if they have a
1384 length of 1. */
1385 if (sym->ts.type == BT_CHARACTER)
1387 gfc_charlen *cl = sym->ts.u.cl;
1388 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1389 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1391 gfc_error ("Character argument %qs at %L "
1392 "must be length 1 because "
1393 "procedure %qs is BIND(C)",
1394 sym->name, &sym->declared_at,
1395 sym->ns->proc_name->name);
1396 retval = false;
1400 /* We have to make sure that any param to a bind(c) routine does
1401 not have the allocatable, pointer, or optional attributes,
1402 according to J3/04-007, section 5.1. */
1403 if (sym->attr.allocatable == 1
1404 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1405 "ALLOCATABLE attribute in procedure %qs "
1406 "with BIND(C)", sym->name,
1407 &(sym->declared_at),
1408 sym->ns->proc_name->name))
1409 retval = false;
1411 if (sym->attr.pointer == 1
1412 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1413 "POINTER attribute in procedure %qs "
1414 "with BIND(C)", sym->name,
1415 &(sym->declared_at),
1416 sym->ns->proc_name->name))
1417 retval = false;
1419 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1421 gfc_error ("Scalar variable %qs at %L with POINTER or "
1422 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1423 " supported", sym->name, &(sym->declared_at),
1424 sym->ns->proc_name->name);
1425 retval = false;
1428 if (sym->attr.optional == 1 && sym->attr.value)
1430 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1431 "and the VALUE attribute because procedure %qs "
1432 "is BIND(C)", sym->name, &(sym->declared_at),
1433 sym->ns->proc_name->name);
1434 retval = false;
1436 else if (sym->attr.optional == 1
1437 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1438 "at %L with OPTIONAL attribute in "
1439 "procedure %qs which is BIND(C)",
1440 sym->name, &(sym->declared_at),
1441 sym->ns->proc_name->name))
1442 retval = false;
1444 /* Make sure that if it has the dimension attribute, that it is
1445 either assumed size or explicit shape. Deferred shape is already
1446 covered by the pointer/allocatable attribute. */
1447 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1448 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1449 "at %L as dummy argument to the BIND(C) "
1450 "procedure %qs at %L", sym->name,
1451 &(sym->declared_at),
1452 sym->ns->proc_name->name,
1453 &(sym->ns->proc_name->declared_at)))
1454 retval = false;
1458 return retval;
1463 /* Function called by variable_decl() that adds a name to the symbol table. */
1465 static bool
1466 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1467 gfc_array_spec **as, locus *var_locus)
1469 symbol_attribute attr;
1470 gfc_symbol *sym;
1471 int upper;
1472 gfc_symtree *st;
1474 /* Symbols in a submodule are host associated from the parent module or
1475 submodules. Therefore, they can be overridden by declarations in the
1476 submodule scope. Deal with this by attaching the existing symbol to
1477 a new symtree and recycling the old symtree with a new symbol... */
1478 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1479 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1480 && st->n.sym != NULL
1481 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1483 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1484 s->n.sym = st->n.sym;
1485 sym = gfc_new_symbol (name, gfc_current_ns);
1488 st->n.sym = sym;
1489 sym->refs++;
1490 gfc_set_sym_referenced (sym);
1492 /* ...Otherwise generate a new symtree and new symbol. */
1493 else if (gfc_get_symbol (name, NULL, &sym))
1494 return false;
1496 /* Check if the name has already been defined as a type. The
1497 first letter of the symtree will be in upper case then. Of
1498 course, this is only necessary if the upper case letter is
1499 actually different. */
1501 upper = TOUPPER(name[0]);
1502 if (upper != name[0])
1504 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1505 gfc_symtree *st;
1507 gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1508 strcpy (u_name, name);
1509 u_name[0] = upper;
1511 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1513 /* STRUCTURE types can alias symbol names */
1514 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1516 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1517 &st->n.sym->declared_at);
1518 return false;
1522 /* Start updating the symbol table. Add basic type attribute if present. */
1523 if (current_ts.type != BT_UNKNOWN
1524 && (sym->attr.implicit_type == 0
1525 || !gfc_compare_types (&sym->ts, &current_ts))
1526 && !gfc_add_type (sym, &current_ts, var_locus))
1527 return false;
1529 if (sym->ts.type == BT_CHARACTER)
1531 sym->ts.u.cl = cl;
1532 sym->ts.deferred = cl_deferred;
1535 /* Add dimension attribute if present. */
1536 if (!gfc_set_array_spec (sym, *as, var_locus))
1537 return false;
1538 *as = NULL;
1540 /* Add attribute to symbol. The copy is so that we can reset the
1541 dimension attribute. */
1542 attr = current_attr;
1543 attr.dimension = 0;
1544 attr.codimension = 0;
1546 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1547 return false;
1549 /* Finish any work that may need to be done for the binding label,
1550 if it's a bind(c). The bind(c) attr is found before the symbol
1551 is made, and before the symbol name (for data decls), so the
1552 current_ts is holding the binding label, or nothing if the
1553 name= attr wasn't given. Therefore, test here if we're dealing
1554 with a bind(c) and make sure the binding label is set correctly. */
1555 if (sym->attr.is_bind_c == 1)
1557 if (!sym->binding_label)
1559 /* Set the binding label and verify that if a NAME= was specified
1560 then only one identifier was in the entity-decl-list. */
1561 if (!set_binding_label (&sym->binding_label, sym->name,
1562 num_idents_on_line))
1563 return false;
1567 /* See if we know we're in a common block, and if it's a bind(c)
1568 common then we need to make sure we're an interoperable type. */
1569 if (sym->attr.in_common == 1)
1571 /* Test the common block object. */
1572 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1573 && sym->ts.is_c_interop != 1)
1575 gfc_error_now ("Variable %qs in common block %qs at %C "
1576 "must be declared with a C interoperable "
1577 "kind since common block %qs is BIND(C)",
1578 sym->name, sym->common_block->name,
1579 sym->common_block->name);
1580 gfc_clear_error ();
1584 sym->attr.implied_index = 0;
1586 /* Use the parameter expressions for a parameterized derived type. */
1587 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1588 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1589 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1591 if (sym->ts.type == BT_CLASS)
1592 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1594 return true;
1598 /* Set character constant to the given length. The constant will be padded or
1599 truncated. If we're inside an array constructor without a typespec, we
1600 additionally check that all elements have the same length; check_len -1
1601 means no checking. */
1603 void
1604 gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1605 gfc_charlen_t check_len)
1607 gfc_char_t *s;
1608 gfc_charlen_t slen;
1610 if (expr->ts.type != BT_CHARACTER)
1611 return;
1613 if (expr->expr_type != EXPR_CONSTANT)
1615 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1616 return;
1619 slen = expr->value.character.length;
1620 if (len != slen)
1622 s = gfc_get_wide_string (len + 1);
1623 memcpy (s, expr->value.character.string,
1624 MIN (len, slen) * sizeof (gfc_char_t));
1625 if (len > slen)
1626 gfc_wide_memset (&s[slen], ' ', len - slen);
1628 if (warn_character_truncation && slen > len)
1629 gfc_warning_now (OPT_Wcharacter_truncation,
1630 "CHARACTER expression at %L is being truncated "
1631 "(%ld/%ld)", &expr->where,
1632 (long) slen, (long) len);
1634 /* Apply the standard by 'hand' otherwise it gets cleared for
1635 initializers. */
1636 if (check_len != -1 && slen != check_len
1637 && !(gfc_option.allow_std & GFC_STD_GNU))
1638 gfc_error_now ("The CHARACTER elements of the array constructor "
1639 "at %L must have the same length (%ld/%ld)",
1640 &expr->where, (long) slen,
1641 (long) check_len);
1643 s[len] = '\0';
1644 free (expr->value.character.string);
1645 expr->value.character.string = s;
1646 expr->value.character.length = len;
1651 /* Function to create and update the enumerator history
1652 using the information passed as arguments.
1653 Pointer "max_enum" is also updated, to point to
1654 enum history node containing largest initializer.
1656 SYM points to the symbol node of enumerator.
1657 INIT points to its enumerator value. */
1659 static void
1660 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1662 enumerator_history *new_enum_history;
1663 gcc_assert (sym != NULL && init != NULL);
1665 new_enum_history = XCNEW (enumerator_history);
1667 new_enum_history->sym = sym;
1668 new_enum_history->initializer = init;
1669 new_enum_history->next = NULL;
1671 if (enum_history == NULL)
1673 enum_history = new_enum_history;
1674 max_enum = enum_history;
1676 else
1678 new_enum_history->next = enum_history;
1679 enum_history = new_enum_history;
1681 if (mpz_cmp (max_enum->initializer->value.integer,
1682 new_enum_history->initializer->value.integer) < 0)
1683 max_enum = new_enum_history;
1688 /* Function to free enum kind history. */
1690 void
1691 gfc_free_enum_history (void)
1693 enumerator_history *current = enum_history;
1694 enumerator_history *next;
1696 while (current != NULL)
1698 next = current->next;
1699 free (current);
1700 current = next;
1702 max_enum = NULL;
1703 enum_history = NULL;
1707 /* Function called by variable_decl() that adds an initialization
1708 expression to a symbol. */
1710 static bool
1711 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1713 symbol_attribute attr;
1714 gfc_symbol *sym;
1715 gfc_expr *init;
1717 init = *initp;
1718 if (find_special (name, &sym, false))
1719 return false;
1721 attr = sym->attr;
1723 /* If this symbol is confirming an implicit parameter type,
1724 then an initialization expression is not allowed. */
1725 if (attr.flavor == FL_PARAMETER
1726 && sym->value != NULL
1727 && *initp != NULL)
1729 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1730 sym->name);
1731 return false;
1734 if (init == NULL)
1736 /* An initializer is required for PARAMETER declarations. */
1737 if (attr.flavor == FL_PARAMETER)
1739 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1740 return false;
1743 else
1745 /* If a variable appears in a DATA block, it cannot have an
1746 initializer. */
1747 if (sym->attr.data)
1749 gfc_error ("Variable %qs at %C with an initializer already "
1750 "appears in a DATA statement", sym->name);
1751 return false;
1754 /* Check if the assignment can happen. This has to be put off
1755 until later for derived type variables and procedure pointers. */
1756 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1757 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1758 && !sym->attr.proc_pointer
1759 && !gfc_check_assign_symbol (sym, NULL, init))
1760 return false;
1762 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1763 && init->ts.type == BT_CHARACTER)
1765 /* Update symbol character length according initializer. */
1766 if (!gfc_check_assign_symbol (sym, NULL, init))
1767 return false;
1769 if (sym->ts.u.cl->length == NULL)
1771 gfc_charlen_t clen;
1772 /* If there are multiple CHARACTER variables declared on the
1773 same line, we don't want them to share the same length. */
1774 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1776 if (sym->attr.flavor == FL_PARAMETER)
1778 if (init->expr_type == EXPR_CONSTANT)
1780 clen = init->value.character.length;
1781 sym->ts.u.cl->length
1782 = gfc_get_int_expr (gfc_charlen_int_kind,
1783 NULL, clen);
1785 else if (init->expr_type == EXPR_ARRAY)
1787 if (init->ts.u.cl && init->ts.u.cl->length)
1789 const gfc_expr *length = init->ts.u.cl->length;
1790 if (length->expr_type != EXPR_CONSTANT)
1792 gfc_error ("Cannot initialize parameter array "
1793 "at %L "
1794 "with variable length elements",
1795 &sym->declared_at);
1796 return false;
1798 clen = mpz_get_si (length->value.integer);
1800 else if (init->value.constructor)
1802 gfc_constructor *c;
1803 c = gfc_constructor_first (init->value.constructor);
1804 clen = c->expr->value.character.length;
1806 else
1807 gcc_unreachable ();
1808 sym->ts.u.cl->length
1809 = gfc_get_int_expr (gfc_charlen_int_kind,
1810 NULL, clen);
1812 else if (init->ts.u.cl && init->ts.u.cl->length)
1813 sym->ts.u.cl->length =
1814 gfc_copy_expr (sym->value->ts.u.cl->length);
1817 /* Update initializer character length according symbol. */
1818 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1820 if (!gfc_specification_expr (sym->ts.u.cl->length))
1821 return false;
1823 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
1824 false);
1825 /* resolve_charlen will complain later on if the length
1826 is too large. Just skeep the initialization in that case. */
1827 if (mpz_cmp (sym->ts.u.cl->length->value.integer,
1828 gfc_integer_kinds[k].huge) <= 0)
1830 HOST_WIDE_INT len
1831 = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
1833 if (init->expr_type == EXPR_CONSTANT)
1834 gfc_set_constant_character_len (len, init, -1);
1835 else if (init->expr_type == EXPR_ARRAY)
1837 gfc_constructor *c;
1839 /* Build a new charlen to prevent simplification from
1840 deleting the length before it is resolved. */
1841 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1842 init->ts.u.cl->length
1843 = gfc_copy_expr (sym->ts.u.cl->length);
1845 for (c = gfc_constructor_first (init->value.constructor);
1846 c; c = gfc_constructor_next (c))
1847 gfc_set_constant_character_len (len, c->expr, -1);
1853 /* If sym is implied-shape, set its upper bounds from init. */
1854 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1855 && sym->as->type == AS_IMPLIED_SHAPE)
1857 int dim;
1859 if (init->rank == 0)
1861 gfc_error ("Can't initialize implied-shape array at %L"
1862 " with scalar", &sym->declared_at);
1863 return false;
1866 /* Shape should be present, we get an initialization expression. */
1867 gcc_assert (init->shape);
1869 for (dim = 0; dim < sym->as->rank; ++dim)
1871 int k;
1872 gfc_expr *e, *lower;
1874 lower = sym->as->lower[dim];
1876 /* If the lower bound is an array element from another
1877 parameterized array, then it is marked with EXPR_VARIABLE and
1878 is an initialization expression. Try to reduce it. */
1879 if (lower->expr_type == EXPR_VARIABLE)
1880 gfc_reduce_init_expr (lower);
1882 if (lower->expr_type == EXPR_CONSTANT)
1884 /* All dimensions must be without upper bound. */
1885 gcc_assert (!sym->as->upper[dim]);
1887 k = lower->ts.kind;
1888 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1889 mpz_add (e->value.integer, lower->value.integer,
1890 init->shape[dim]);
1891 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1892 sym->as->upper[dim] = e;
1894 else
1896 gfc_error ("Non-constant lower bound in implied-shape"
1897 " declaration at %L", &lower->where);
1898 return false;
1902 sym->as->type = AS_EXPLICIT;
1905 /* Need to check if the expression we initialized this
1906 to was one of the iso_c_binding named constants. If so,
1907 and we're a parameter (constant), let it be iso_c.
1908 For example:
1909 integer(c_int), parameter :: my_int = c_int
1910 integer(my_int) :: my_int_2
1911 If we mark my_int as iso_c (since we can see it's value
1912 is equal to one of the named constants), then my_int_2
1913 will be considered C interoperable. */
1914 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
1916 sym->ts.is_iso_c |= init->ts.is_iso_c;
1917 sym->ts.is_c_interop |= init->ts.is_c_interop;
1918 /* attr bits needed for module files. */
1919 sym->attr.is_iso_c |= init->ts.is_iso_c;
1920 sym->attr.is_c_interop |= init->ts.is_c_interop;
1921 if (init->ts.is_iso_c)
1922 sym->ts.f90_type = init->ts.f90_type;
1925 /* Add initializer. Make sure we keep the ranks sane. */
1926 if (sym->attr.dimension && init->rank == 0)
1928 mpz_t size;
1929 gfc_expr *array;
1930 int n;
1931 if (sym->attr.flavor == FL_PARAMETER
1932 && init->expr_type == EXPR_CONSTANT
1933 && spec_size (sym->as, &size)
1934 && mpz_cmp_si (size, 0) > 0)
1936 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1937 &init->where);
1938 for (n = 0; n < (int)mpz_get_si (size); n++)
1939 gfc_constructor_append_expr (&array->value.constructor,
1940 n == 0
1941 ? init
1942 : gfc_copy_expr (init),
1943 &init->where);
1945 array->shape = gfc_get_shape (sym->as->rank);
1946 for (n = 0; n < sym->as->rank; n++)
1947 spec_dimen_size (sym->as, n, &array->shape[n]);
1949 init = array;
1950 mpz_clear (size);
1952 init->rank = sym->as->rank;
1955 sym->value = init;
1956 if (sym->attr.save == SAVE_NONE)
1957 sym->attr.save = SAVE_IMPLICIT;
1958 *initp = NULL;
1961 return true;
1965 /* Function called by variable_decl() that adds a name to a structure
1966 being built. */
1968 static bool
1969 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1970 gfc_array_spec **as)
1972 gfc_state_data *s;
1973 gfc_component *c;
1975 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1976 constructing, it must have the pointer attribute. */
1977 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1978 && current_ts.u.derived == gfc_current_block ()
1979 && current_attr.pointer == 0)
1981 if (current_attr.allocatable
1982 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
1983 "must have the POINTER attribute"))
1985 return false;
1987 else if (current_attr.allocatable == 0)
1989 gfc_error ("Component at %C must have the POINTER attribute");
1990 return false;
1994 /* F03:C437. */
1995 if (current_ts.type == BT_CLASS
1996 && !(current_attr.pointer || current_attr.allocatable))
1998 gfc_error ("Component %qs with CLASS at %C must be allocatable "
1999 "or pointer", name);
2000 return false;
2003 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2005 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2007 gfc_error ("Array component of structure at %C must have explicit "
2008 "or deferred shape");
2009 return false;
2013 /* If we are in a nested union/map definition, gfc_add_component will not
2014 properly find repeated components because:
2015 (i) gfc_add_component does a flat search, where components of unions
2016 and maps are implicity chained so nested components may conflict.
2017 (ii) Unions and maps are not linked as components of their parent
2018 structures until after they are parsed.
2019 For (i) we use gfc_find_component which searches recursively, and for (ii)
2020 we search each block directly from the parse stack until we find the top
2021 level structure. */
2023 s = gfc_state_stack;
2024 if (s->state == COMP_UNION || s->state == COMP_MAP)
2026 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2028 c = gfc_find_component (s->sym, name, true, true, NULL);
2029 if (c != NULL)
2031 gfc_error_now ("Component %qs at %C already declared at %L",
2032 name, &c->loc);
2033 return false;
2035 /* Break after we've searched the entire chain. */
2036 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2037 break;
2038 s = s->previous;
2042 if (!gfc_add_component (gfc_current_block(), name, &c))
2043 return false;
2045 c->ts = current_ts;
2046 if (c->ts.type == BT_CHARACTER)
2047 c->ts.u.cl = cl;
2049 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2050 && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2051 && saved_kind_expr != NULL)
2052 c->kind_expr = gfc_copy_expr (saved_kind_expr);
2054 c->attr = current_attr;
2056 c->initializer = *init;
2057 *init = NULL;
2059 c->as = *as;
2060 if (c->as != NULL)
2062 if (c->as->corank)
2063 c->attr.codimension = 1;
2064 if (c->as->rank)
2065 c->attr.dimension = 1;
2067 *as = NULL;
2069 gfc_apply_init (&c->ts, &c->attr, c->initializer);
2071 /* Check array components. */
2072 if (!c->attr.dimension)
2073 goto scalar;
2075 if (c->attr.pointer)
2077 if (c->as->type != AS_DEFERRED)
2079 gfc_error ("Pointer array component of structure at %C must have a "
2080 "deferred shape");
2081 return false;
2084 else if (c->attr.allocatable)
2086 if (c->as->type != AS_DEFERRED)
2088 gfc_error ("Allocatable component of structure at %C must have a "
2089 "deferred shape");
2090 return false;
2093 else
2095 if (c->as->type != AS_EXPLICIT)
2097 gfc_error ("Array component of structure at %C must have an "
2098 "explicit shape");
2099 return false;
2103 scalar:
2104 if (c->ts.type == BT_CLASS)
2105 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2107 if (c->attr.pdt_kind || c->attr.pdt_len)
2109 gfc_symbol *sym;
2110 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2111 0, &sym);
2112 if (sym == NULL)
2114 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2115 "in the type parameter name list at %L",
2116 c->name, &gfc_current_block ()->declared_at);
2117 return false;
2119 sym->ts = c->ts;
2120 sym->attr.pdt_kind = c->attr.pdt_kind;
2121 sym->attr.pdt_len = c->attr.pdt_len;
2122 if (c->initializer)
2123 sym->value = gfc_copy_expr (c->initializer);
2124 sym->attr.flavor = FL_VARIABLE;
2127 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2128 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2129 && decl_type_param_list)
2130 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2132 return true;
2136 /* Match a 'NULL()', and possibly take care of some side effects. */
2138 match
2139 gfc_match_null (gfc_expr **result)
2141 gfc_symbol *sym;
2142 match m, m2 = MATCH_NO;
2144 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2145 return MATCH_ERROR;
2147 if (m == MATCH_NO)
2149 locus old_loc;
2150 char name[GFC_MAX_SYMBOL_LEN + 1];
2152 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2153 return m2;
2155 old_loc = gfc_current_locus;
2156 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2157 return MATCH_ERROR;
2158 if (m2 != MATCH_YES
2159 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2160 return MATCH_ERROR;
2161 if (m2 == MATCH_NO)
2163 gfc_current_locus = old_loc;
2164 return MATCH_NO;
2168 /* The NULL symbol now has to be/become an intrinsic function. */
2169 if (gfc_get_symbol ("null", NULL, &sym))
2171 gfc_error ("NULL() initialization at %C is ambiguous");
2172 return MATCH_ERROR;
2175 gfc_intrinsic_symbol (sym);
2177 if (sym->attr.proc != PROC_INTRINSIC
2178 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2179 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2180 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2181 return MATCH_ERROR;
2183 *result = gfc_get_null_expr (&gfc_current_locus);
2185 /* Invalid per F2008, C512. */
2186 if (m2 == MATCH_YES)
2188 gfc_error ("NULL() initialization at %C may not have MOLD");
2189 return MATCH_ERROR;
2192 return MATCH_YES;
2196 /* Match the initialization expr for a data pointer or procedure pointer. */
2198 static match
2199 match_pointer_init (gfc_expr **init, int procptr)
2201 match m;
2203 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2205 gfc_error ("Initialization of pointer at %C is not allowed in "
2206 "a PURE procedure");
2207 return MATCH_ERROR;
2209 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2211 /* Match NULL() initialization. */
2212 m = gfc_match_null (init);
2213 if (m != MATCH_NO)
2214 return m;
2216 /* Match non-NULL initialization. */
2217 gfc_matching_ptr_assignment = !procptr;
2218 gfc_matching_procptr_assignment = procptr;
2219 m = gfc_match_rvalue (init);
2220 gfc_matching_ptr_assignment = 0;
2221 gfc_matching_procptr_assignment = 0;
2222 if (m == MATCH_ERROR)
2223 return MATCH_ERROR;
2224 else if (m == MATCH_NO)
2226 gfc_error ("Error in pointer initialization at %C");
2227 return MATCH_ERROR;
2230 if (!procptr && !gfc_resolve_expr (*init))
2231 return MATCH_ERROR;
2233 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2234 "initialization at %C"))
2235 return MATCH_ERROR;
2237 return MATCH_YES;
2241 static bool
2242 check_function_name (char *name)
2244 /* In functions that have a RESULT variable defined, the function name always
2245 refers to function calls. Therefore, the name is not allowed to appear in
2246 specification statements. When checking this, be careful about
2247 'hidden' procedure pointer results ('ppr@'). */
2249 if (gfc_current_state () == COMP_FUNCTION)
2251 gfc_symbol *block = gfc_current_block ();
2252 if (block && block->result && block->result != block
2253 && strcmp (block->result->name, "ppr@") != 0
2254 && strcmp (block->name, name) == 0)
2256 gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2257 "from appearing in a specification statement",
2258 block->result->name, &block->result->declared_at, name);
2259 return false;
2263 return true;
2267 /* Match a variable name with an optional initializer. When this
2268 subroutine is called, a variable is expected to be parsed next.
2269 Depending on what is happening at the moment, updates either the
2270 symbol table or the current interface. */
2272 static match
2273 variable_decl (int elem)
2275 char name[GFC_MAX_SYMBOL_LEN + 1];
2276 static unsigned int fill_id = 0;
2277 gfc_expr *initializer, *char_len;
2278 gfc_array_spec *as;
2279 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2280 gfc_charlen *cl;
2281 bool cl_deferred;
2282 locus var_locus;
2283 match m;
2284 bool t;
2285 gfc_symbol *sym;
2287 initializer = NULL;
2288 as = NULL;
2289 cp_as = NULL;
2291 /* When we get here, we've just matched a list of attributes and
2292 maybe a type and a double colon. The next thing we expect to see
2293 is the name of the symbol. */
2295 /* If we are parsing a structure with legacy support, we allow the symbol
2296 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2297 m = MATCH_NO;
2298 gfc_gobble_whitespace ();
2299 if (gfc_peek_ascii_char () == '%')
2301 gfc_next_ascii_char ();
2302 m = gfc_match ("fill");
2305 if (m != MATCH_YES)
2307 m = gfc_match_name (name);
2308 if (m != MATCH_YES)
2309 goto cleanup;
2312 else
2314 m = MATCH_ERROR;
2315 if (gfc_current_state () != COMP_STRUCTURE)
2317 if (flag_dec_structure)
2318 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2319 else
2320 gfc_error ("%qs at %C is a DEC extension, enable with "
2321 "%<-fdec-structure%>", "%FILL");
2322 goto cleanup;
2325 if (attr_seen)
2327 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2328 goto cleanup;
2331 /* %FILL components are given invalid fortran names. */
2332 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2333 m = MATCH_YES;
2336 var_locus = gfc_current_locus;
2338 /* Now we could see the optional array spec. or character length. */
2339 m = gfc_match_array_spec (&as, true, true);
2340 if (m == MATCH_ERROR)
2341 goto cleanup;
2343 if (m == MATCH_NO)
2344 as = gfc_copy_array_spec (current_as);
2345 else if (current_as
2346 && !merge_array_spec (current_as, as, true))
2348 m = MATCH_ERROR;
2349 goto cleanup;
2352 if (flag_cray_pointer)
2353 cp_as = gfc_copy_array_spec (as);
2355 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2356 determine (and check) whether it can be implied-shape. If it
2357 was parsed as assumed-size, change it because PARAMETERs can not
2358 be assumed-size.
2360 An explicit-shape-array cannot appear under several conditions.
2361 That check is done here as well. */
2362 if (as)
2364 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2366 m = MATCH_ERROR;
2367 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2368 name, &var_locus);
2369 goto cleanup;
2372 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2373 && current_attr.flavor == FL_PARAMETER)
2374 as->type = AS_IMPLIED_SHAPE;
2376 if (as->type == AS_IMPLIED_SHAPE
2377 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2378 &var_locus))
2380 m = MATCH_ERROR;
2381 goto cleanup;
2384 /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2385 constant expressions shall appear only in a subprogram, derived
2386 type definition, BLOCK construct, or interface body. */
2387 if (as->type == AS_EXPLICIT
2388 && gfc_current_state () != COMP_BLOCK
2389 && gfc_current_state () != COMP_DERIVED
2390 && gfc_current_state () != COMP_FUNCTION
2391 && gfc_current_state () != COMP_INTERFACE
2392 && gfc_current_state () != COMP_SUBROUTINE)
2394 gfc_expr *e;
2395 bool not_constant = false;
2397 for (int i = 0; i < as->rank; i++)
2399 e = gfc_copy_expr (as->lower[i]);
2400 gfc_resolve_expr (e);
2401 gfc_simplify_expr (e, 0);
2402 if (e && (e->expr_type != EXPR_CONSTANT))
2404 not_constant = true;
2405 break;
2407 gfc_free_expr (e);
2409 e = gfc_copy_expr (as->upper[i]);
2410 gfc_resolve_expr (e);
2411 gfc_simplify_expr (e, 0);
2412 if (e && (e->expr_type != EXPR_CONSTANT))
2414 not_constant = true;
2415 break;
2417 gfc_free_expr (e);
2420 if (not_constant)
2422 gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2423 m = MATCH_ERROR;
2424 goto cleanup;
2427 if (as->type == AS_EXPLICIT)
2429 for (int i = 0; i < as->rank; i++)
2431 gfc_expr *e, *n;
2432 e = as->lower[i];
2433 if (e->expr_type != EXPR_CONSTANT)
2435 n = gfc_copy_expr (e);
2436 gfc_simplify_expr (n, 1);
2437 if (n->expr_type == EXPR_CONSTANT)
2438 gfc_replace_expr (e, n);
2439 else
2440 gfc_free_expr (n);
2442 e = as->upper[i];
2443 if (e->expr_type != EXPR_CONSTANT)
2445 n = gfc_copy_expr (e);
2446 gfc_simplify_expr (n, 1);
2447 if (n->expr_type == EXPR_CONSTANT)
2448 gfc_replace_expr (e, n);
2449 else
2450 gfc_free_expr (n);
2456 char_len = NULL;
2457 cl = NULL;
2458 cl_deferred = false;
2460 if (current_ts.type == BT_CHARACTER)
2462 switch (match_char_length (&char_len, &cl_deferred, false))
2464 case MATCH_YES:
2465 cl = gfc_new_charlen (gfc_current_ns, NULL);
2467 cl->length = char_len;
2468 break;
2470 /* Non-constant lengths need to be copied after the first
2471 element. Also copy assumed lengths. */
2472 case MATCH_NO:
2473 if (elem > 1
2474 && (current_ts.u.cl->length == NULL
2475 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2477 cl = gfc_new_charlen (gfc_current_ns, NULL);
2478 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2480 else
2481 cl = current_ts.u.cl;
2483 cl_deferred = current_ts.deferred;
2485 break;
2487 case MATCH_ERROR:
2488 goto cleanup;
2492 /* The dummy arguments and result of the abreviated form of MODULE
2493 PROCEDUREs, used in SUBMODULES should not be redefined. */
2494 if (gfc_current_ns->proc_name
2495 && gfc_current_ns->proc_name->abr_modproc_decl)
2497 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2498 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2500 m = MATCH_ERROR;
2501 gfc_error ("%qs at %C is a redefinition of the declaration "
2502 "in the corresponding interface for MODULE "
2503 "PROCEDURE %qs", sym->name,
2504 gfc_current_ns->proc_name->name);
2505 goto cleanup;
2509 /* %FILL components may not have initializers. */
2510 if (strncmp (name, "%FILL", 5) == 0 && gfc_match_eos () != MATCH_YES)
2512 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2513 m = MATCH_ERROR;
2514 goto cleanup;
2517 /* If this symbol has already shown up in a Cray Pointer declaration,
2518 and this is not a component declaration,
2519 then we want to set the type & bail out. */
2520 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2522 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2523 if (sym != NULL && sym->attr.cray_pointee)
2525 sym->ts.type = current_ts.type;
2526 sym->ts.kind = current_ts.kind;
2527 sym->ts.u.cl = cl;
2528 sym->ts.u.derived = current_ts.u.derived;
2529 sym->ts.is_c_interop = current_ts.is_c_interop;
2530 sym->ts.is_iso_c = current_ts.is_iso_c;
2531 m = MATCH_YES;
2533 /* Check to see if we have an array specification. */
2534 if (cp_as != NULL)
2536 if (sym->as != NULL)
2538 gfc_error ("Duplicate array spec for Cray pointee at %C");
2539 gfc_free_array_spec (cp_as);
2540 m = MATCH_ERROR;
2541 goto cleanup;
2543 else
2545 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2546 gfc_internal_error ("Couldn't set pointee array spec.");
2548 /* Fix the array spec. */
2549 m = gfc_mod_pointee_as (sym->as);
2550 if (m == MATCH_ERROR)
2551 goto cleanup;
2554 goto cleanup;
2556 else
2558 gfc_free_array_spec (cp_as);
2562 /* Procedure pointer as function result. */
2563 if (gfc_current_state () == COMP_FUNCTION
2564 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2565 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2566 strcpy (name, "ppr@");
2568 if (gfc_current_state () == COMP_FUNCTION
2569 && strcmp (name, gfc_current_block ()->name) == 0
2570 && gfc_current_block ()->result
2571 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2572 strcpy (name, "ppr@");
2574 /* OK, we've successfully matched the declaration. Now put the
2575 symbol in the current namespace, because it might be used in the
2576 optional initialization expression for this symbol, e.g. this is
2577 perfectly legal:
2579 integer, parameter :: i = huge(i)
2581 This is only true for parameters or variables of a basic type.
2582 For components of derived types, it is not true, so we don't
2583 create a symbol for those yet. If we fail to create the symbol,
2584 bail out. */
2585 if (!gfc_comp_struct (gfc_current_state ())
2586 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2588 m = MATCH_ERROR;
2589 goto cleanup;
2592 if (!check_function_name (name))
2594 m = MATCH_ERROR;
2595 goto cleanup;
2598 /* We allow old-style initializations of the form
2599 integer i /2/, j(4) /3*3, 1/
2600 (if no colon has been seen). These are different from data
2601 statements in that initializers are only allowed to apply to the
2602 variable immediately preceding, i.e.
2603 integer i, j /1, 2/
2604 is not allowed. Therefore we have to do some work manually, that
2605 could otherwise be left to the matchers for DATA statements. */
2607 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2609 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2610 "initialization at %C"))
2611 return MATCH_ERROR;
2613 /* Allow old style initializations for components of STRUCTUREs and MAPs
2614 but not components of derived types. */
2615 else if (gfc_current_state () == COMP_DERIVED)
2617 gfc_error ("Invalid old style initialization for derived type "
2618 "component at %C");
2619 m = MATCH_ERROR;
2620 goto cleanup;
2623 /* For structure components, read the initializer as a special
2624 expression and let the rest of this function apply the initializer
2625 as usual. */
2626 else if (gfc_comp_struct (gfc_current_state ()))
2628 m = match_clist_expr (&initializer, &current_ts, as);
2629 if (m == MATCH_NO)
2630 gfc_error ("Syntax error in old style initialization of %s at %C",
2631 name);
2632 if (m != MATCH_YES)
2633 goto cleanup;
2636 /* Otherwise we treat the old style initialization just like a
2637 DATA declaration for the current variable. */
2638 else
2639 return match_old_style_init (name);
2642 /* The double colon must be present in order to have initializers.
2643 Otherwise the statement is ambiguous with an assignment statement. */
2644 if (colon_seen)
2646 if (gfc_match (" =>") == MATCH_YES)
2648 if (!current_attr.pointer)
2650 gfc_error ("Initialization at %C isn't for a pointer variable");
2651 m = MATCH_ERROR;
2652 goto cleanup;
2655 m = match_pointer_init (&initializer, 0);
2656 if (m != MATCH_YES)
2657 goto cleanup;
2659 else if (gfc_match_char ('=') == MATCH_YES)
2661 if (current_attr.pointer)
2663 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2664 "not %<=%>");
2665 m = MATCH_ERROR;
2666 goto cleanup;
2669 m = gfc_match_init_expr (&initializer);
2670 if (m == MATCH_NO)
2672 gfc_error ("Expected an initialization expression at %C");
2673 m = MATCH_ERROR;
2676 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2677 && !gfc_comp_struct (gfc_state_stack->state))
2679 gfc_error ("Initialization of variable at %C is not allowed in "
2680 "a PURE procedure");
2681 m = MATCH_ERROR;
2684 if (current_attr.flavor != FL_PARAMETER
2685 && !gfc_comp_struct (gfc_state_stack->state))
2686 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2688 if (m != MATCH_YES)
2689 goto cleanup;
2693 if (initializer != NULL && current_attr.allocatable
2694 && gfc_comp_struct (gfc_current_state ()))
2696 gfc_error ("Initialization of allocatable component at %C is not "
2697 "allowed");
2698 m = MATCH_ERROR;
2699 goto cleanup;
2702 if (gfc_current_state () == COMP_DERIVED
2703 && gfc_current_block ()->attr.pdt_template)
2705 gfc_symbol *param;
2706 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2707 0, &param);
2708 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2710 gfc_error ("The component with KIND or LEN attribute at %C does not "
2711 "not appear in the type parameter list at %L",
2712 &gfc_current_block ()->declared_at);
2713 m = MATCH_ERROR;
2714 goto cleanup;
2716 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2718 gfc_error ("The component at %C that appears in the type parameter "
2719 "list at %L has neither the KIND nor LEN attribute",
2720 &gfc_current_block ()->declared_at);
2721 m = MATCH_ERROR;
2722 goto cleanup;
2724 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2726 gfc_error ("The component at %C which is a type parameter must be "
2727 "a scalar");
2728 m = MATCH_ERROR;
2729 goto cleanup;
2731 else if (param && initializer)
2732 param->value = gfc_copy_expr (initializer);
2735 /* Add the initializer. Note that it is fine if initializer is
2736 NULL here, because we sometimes also need to check if a
2737 declaration *must* have an initialization expression. */
2738 if (!gfc_comp_struct (gfc_current_state ()))
2739 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2740 else
2742 if (current_ts.type == BT_DERIVED
2743 && !current_attr.pointer && !initializer)
2744 initializer = gfc_default_initializer (&current_ts);
2745 t = build_struct (name, cl, &initializer, &as);
2747 /* If we match a nested structure definition we expect to see the
2748 * body even if the variable declarations blow up, so we need to keep
2749 * the structure declaration around. */
2750 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2751 gfc_commit_symbol (gfc_new_block);
2754 m = (t) ? MATCH_YES : MATCH_ERROR;
2756 cleanup:
2757 /* Free stuff up and return. */
2758 gfc_free_expr (initializer);
2759 gfc_free_array_spec (as);
2761 return m;
2765 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2766 This assumes that the byte size is equal to the kind number for
2767 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2769 match
2770 gfc_match_old_kind_spec (gfc_typespec *ts)
2772 match m;
2773 int original_kind;
2775 if (gfc_match_char ('*') != MATCH_YES)
2776 return MATCH_NO;
2778 m = gfc_match_small_literal_int (&ts->kind, NULL);
2779 if (m != MATCH_YES)
2780 return MATCH_ERROR;
2782 original_kind = ts->kind;
2784 /* Massage the kind numbers for complex types. */
2785 if (ts->type == BT_COMPLEX)
2787 if (ts->kind % 2)
2789 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2790 gfc_basic_typename (ts->type), original_kind);
2791 return MATCH_ERROR;
2793 ts->kind /= 2;
2797 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2798 ts->kind = 8;
2800 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2802 if (ts->kind == 4)
2804 if (flag_real4_kind == 8)
2805 ts->kind = 8;
2806 if (flag_real4_kind == 10)
2807 ts->kind = 10;
2808 if (flag_real4_kind == 16)
2809 ts->kind = 16;
2812 if (ts->kind == 8)
2814 if (flag_real8_kind == 4)
2815 ts->kind = 4;
2816 if (flag_real8_kind == 10)
2817 ts->kind = 10;
2818 if (flag_real8_kind == 16)
2819 ts->kind = 16;
2823 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2825 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2826 gfc_basic_typename (ts->type), original_kind);
2827 return MATCH_ERROR;
2830 if (!gfc_notify_std (GFC_STD_GNU,
2831 "Nonstandard type declaration %s*%d at %C",
2832 gfc_basic_typename(ts->type), original_kind))
2833 return MATCH_ERROR;
2835 return MATCH_YES;
2839 /* Match a kind specification. Since kinds are generally optional, we
2840 usually return MATCH_NO if something goes wrong. If a "kind="
2841 string is found, then we know we have an error. */
2843 match
2844 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2846 locus where, loc;
2847 gfc_expr *e;
2848 match m, n;
2849 char c;
2851 m = MATCH_NO;
2852 n = MATCH_YES;
2853 e = NULL;
2854 saved_kind_expr = NULL;
2856 where = loc = gfc_current_locus;
2858 if (kind_expr_only)
2859 goto kind_expr;
2861 if (gfc_match_char ('(') == MATCH_NO)
2862 return MATCH_NO;
2864 /* Also gobbles optional text. */
2865 if (gfc_match (" kind = ") == MATCH_YES)
2866 m = MATCH_ERROR;
2868 loc = gfc_current_locus;
2870 kind_expr:
2872 n = gfc_match_init_expr (&e);
2874 if (gfc_derived_parameter_expr (e))
2876 ts->kind = 0;
2877 saved_kind_expr = gfc_copy_expr (e);
2878 goto close_brackets;
2881 if (n != MATCH_YES)
2883 if (gfc_matching_function)
2885 /* The function kind expression might include use associated or
2886 imported parameters and try again after the specification
2887 expressions..... */
2888 if (gfc_match_char (')') != MATCH_YES)
2890 gfc_error ("Missing right parenthesis at %C");
2891 m = MATCH_ERROR;
2892 goto no_match;
2895 gfc_free_expr (e);
2896 gfc_undo_symbols ();
2897 return MATCH_YES;
2899 else
2901 /* ....or else, the match is real. */
2902 if (n == MATCH_NO)
2903 gfc_error ("Expected initialization expression at %C");
2904 if (n != MATCH_YES)
2905 return MATCH_ERROR;
2909 if (e->rank != 0)
2911 gfc_error ("Expected scalar initialization expression at %C");
2912 m = MATCH_ERROR;
2913 goto no_match;
2916 if (gfc_extract_int (e, &ts->kind, 1))
2918 m = MATCH_ERROR;
2919 goto no_match;
2922 /* Before throwing away the expression, let's see if we had a
2923 C interoperable kind (and store the fact). */
2924 if (e->ts.is_c_interop == 1)
2926 /* Mark this as C interoperable if being declared with one
2927 of the named constants from iso_c_binding. */
2928 ts->is_c_interop = e->ts.is_iso_c;
2929 ts->f90_type = e->ts.f90_type;
2930 if (e->symtree)
2931 ts->interop_kind = e->symtree->n.sym;
2934 gfc_free_expr (e);
2935 e = NULL;
2937 /* Ignore errors to this point, if we've gotten here. This means
2938 we ignore the m=MATCH_ERROR from above. */
2939 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2941 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2942 gfc_basic_typename (ts->type));
2943 gfc_current_locus = where;
2944 return MATCH_ERROR;
2947 /* Warn if, e.g., c_int is used for a REAL variable, but not
2948 if, e.g., c_double is used for COMPLEX as the standard
2949 explicitly says that the kind type parameter for complex and real
2950 variable is the same, i.e. c_float == c_float_complex. */
2951 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2952 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2953 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2954 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2955 "is %s", gfc_basic_typename (ts->f90_type), &where,
2956 gfc_basic_typename (ts->type));
2958 close_brackets:
2960 gfc_gobble_whitespace ();
2961 if ((c = gfc_next_ascii_char ()) != ')'
2962 && (ts->type != BT_CHARACTER || c != ','))
2964 if (ts->type == BT_CHARACTER)
2965 gfc_error ("Missing right parenthesis or comma at %C");
2966 else
2967 gfc_error ("Missing right parenthesis at %C");
2968 m = MATCH_ERROR;
2970 else
2971 /* All tests passed. */
2972 m = MATCH_YES;
2974 if(m == MATCH_ERROR)
2975 gfc_current_locus = where;
2977 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2978 ts->kind = 8;
2980 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2982 if (ts->kind == 4)
2984 if (flag_real4_kind == 8)
2985 ts->kind = 8;
2986 if (flag_real4_kind == 10)
2987 ts->kind = 10;
2988 if (flag_real4_kind == 16)
2989 ts->kind = 16;
2992 if (ts->kind == 8)
2994 if (flag_real8_kind == 4)
2995 ts->kind = 4;
2996 if (flag_real8_kind == 10)
2997 ts->kind = 10;
2998 if (flag_real8_kind == 16)
2999 ts->kind = 16;
3003 /* Return what we know from the test(s). */
3004 return m;
3006 no_match:
3007 gfc_free_expr (e);
3008 gfc_current_locus = where;
3009 return m;
3013 static match
3014 match_char_kind (int * kind, int * is_iso_c)
3016 locus where;
3017 gfc_expr *e;
3018 match m, n;
3019 bool fail;
3021 m = MATCH_NO;
3022 e = NULL;
3023 where = gfc_current_locus;
3025 n = gfc_match_init_expr (&e);
3027 if (n != MATCH_YES && gfc_matching_function)
3029 /* The expression might include use-associated or imported
3030 parameters and try again after the specification
3031 expressions. */
3032 gfc_free_expr (e);
3033 gfc_undo_symbols ();
3034 return MATCH_YES;
3037 if (n == MATCH_NO)
3038 gfc_error ("Expected initialization expression at %C");
3039 if (n != MATCH_YES)
3040 return MATCH_ERROR;
3042 if (e->rank != 0)
3044 gfc_error ("Expected scalar initialization expression at %C");
3045 m = MATCH_ERROR;
3046 goto no_match;
3049 if (gfc_derived_parameter_expr (e))
3051 saved_kind_expr = e;
3052 *kind = 0;
3053 return MATCH_YES;
3056 fail = gfc_extract_int (e, kind, 1);
3057 *is_iso_c = e->ts.is_iso_c;
3058 if (fail)
3060 m = MATCH_ERROR;
3061 goto no_match;
3064 gfc_free_expr (e);
3066 /* Ignore errors to this point, if we've gotten here. This means
3067 we ignore the m=MATCH_ERROR from above. */
3068 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3070 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3071 m = MATCH_ERROR;
3073 else
3074 /* All tests passed. */
3075 m = MATCH_YES;
3077 if (m == MATCH_ERROR)
3078 gfc_current_locus = where;
3080 /* Return what we know from the test(s). */
3081 return m;
3083 no_match:
3084 gfc_free_expr (e);
3085 gfc_current_locus = where;
3086 return m;
3090 /* Match the various kind/length specifications in a CHARACTER
3091 declaration. We don't return MATCH_NO. */
3093 match
3094 gfc_match_char_spec (gfc_typespec *ts)
3096 int kind, seen_length, is_iso_c;
3097 gfc_charlen *cl;
3098 gfc_expr *len;
3099 match m;
3100 bool deferred;
3102 len = NULL;
3103 seen_length = 0;
3104 kind = 0;
3105 is_iso_c = 0;
3106 deferred = false;
3108 /* Try the old-style specification first. */
3109 old_char_selector = 0;
3111 m = match_char_length (&len, &deferred, true);
3112 if (m != MATCH_NO)
3114 if (m == MATCH_YES)
3115 old_char_selector = 1;
3116 seen_length = 1;
3117 goto done;
3120 m = gfc_match_char ('(');
3121 if (m != MATCH_YES)
3123 m = MATCH_YES; /* Character without length is a single char. */
3124 goto done;
3127 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3128 if (gfc_match (" kind =") == MATCH_YES)
3130 m = match_char_kind (&kind, &is_iso_c);
3132 if (m == MATCH_ERROR)
3133 goto done;
3134 if (m == MATCH_NO)
3135 goto syntax;
3137 if (gfc_match (" , len =") == MATCH_NO)
3138 goto rparen;
3140 m = char_len_param_value (&len, &deferred);
3141 if (m == MATCH_NO)
3142 goto syntax;
3143 if (m == MATCH_ERROR)
3144 goto done;
3145 seen_length = 1;
3147 goto rparen;
3150 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3151 if (gfc_match (" len =") == MATCH_YES)
3153 m = char_len_param_value (&len, &deferred);
3154 if (m == MATCH_NO)
3155 goto syntax;
3156 if (m == MATCH_ERROR)
3157 goto done;
3158 seen_length = 1;
3160 if (gfc_match_char (')') == MATCH_YES)
3161 goto done;
3163 if (gfc_match (" , kind =") != MATCH_YES)
3164 goto syntax;
3166 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3167 goto done;
3169 goto rparen;
3172 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3173 m = char_len_param_value (&len, &deferred);
3174 if (m == MATCH_NO)
3175 goto syntax;
3176 if (m == MATCH_ERROR)
3177 goto done;
3178 seen_length = 1;
3180 m = gfc_match_char (')');
3181 if (m == MATCH_YES)
3182 goto done;
3184 if (gfc_match_char (',') != MATCH_YES)
3185 goto syntax;
3187 gfc_match (" kind ="); /* Gobble optional text. */
3189 m = match_char_kind (&kind, &is_iso_c);
3190 if (m == MATCH_ERROR)
3191 goto done;
3192 if (m == MATCH_NO)
3193 goto syntax;
3195 rparen:
3196 /* Require a right-paren at this point. */
3197 m = gfc_match_char (')');
3198 if (m == MATCH_YES)
3199 goto done;
3201 syntax:
3202 gfc_error ("Syntax error in CHARACTER declaration at %C");
3203 m = MATCH_ERROR;
3204 gfc_free_expr (len);
3205 return m;
3207 done:
3208 /* Deal with character functions after USE and IMPORT statements. */
3209 if (gfc_matching_function)
3211 gfc_free_expr (len);
3212 gfc_undo_symbols ();
3213 return MATCH_YES;
3216 if (m != MATCH_YES)
3218 gfc_free_expr (len);
3219 return m;
3222 /* Do some final massaging of the length values. */
3223 cl = gfc_new_charlen (gfc_current_ns, NULL);
3225 if (seen_length == 0)
3226 cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3227 else
3229 /* If gfortran ends up here, then the len may be reducible to a
3230 constant. Try to do that here. If it does not reduce, simply
3231 assign len to the charlen. */
3232 if (len && len->expr_type != EXPR_CONSTANT)
3234 gfc_expr *e;
3235 e = gfc_copy_expr (len);
3236 gfc_reduce_init_expr (e);
3237 if (e->expr_type == EXPR_CONSTANT)
3239 gfc_replace_expr (len, e);
3240 if (mpz_cmp_si (len->value.integer, 0) < 0)
3241 mpz_set_ui (len->value.integer, 0);
3243 else
3244 gfc_free_expr (e);
3245 cl->length = len;
3247 else
3248 cl->length = len;
3251 ts->u.cl = cl;
3252 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3253 ts->deferred = deferred;
3255 /* We have to know if it was a C interoperable kind so we can
3256 do accurate type checking of bind(c) procs, etc. */
3257 if (kind != 0)
3258 /* Mark this as C interoperable if being declared with one
3259 of the named constants from iso_c_binding. */
3260 ts->is_c_interop = is_iso_c;
3261 else if (len != NULL)
3262 /* Here, we might have parsed something such as: character(c_char)
3263 In this case, the parsing code above grabs the c_char when
3264 looking for the length (line 1690, roughly). it's the last
3265 testcase for parsing the kind params of a character variable.
3266 However, it's not actually the length. this seems like it
3267 could be an error.
3268 To see if the user used a C interop kind, test the expr
3269 of the so called length, and see if it's C interoperable. */
3270 ts->is_c_interop = len->ts.is_iso_c;
3272 return MATCH_YES;
3276 /* Matches a RECORD declaration. */
3278 static match
3279 match_record_decl (char *name)
3281 locus old_loc;
3282 old_loc = gfc_current_locus;
3283 match m;
3285 m = gfc_match (" record /");
3286 if (m == MATCH_YES)
3288 if (!flag_dec_structure)
3290 gfc_current_locus = old_loc;
3291 gfc_error ("RECORD at %C is an extension, enable it with "
3292 "-fdec-structure");
3293 return MATCH_ERROR;
3295 m = gfc_match (" %n/", name);
3296 if (m == MATCH_YES)
3297 return MATCH_YES;
3300 gfc_current_locus = old_loc;
3301 if (flag_dec_structure
3302 && (gfc_match (" record% ") == MATCH_YES
3303 || gfc_match (" record%t") == MATCH_YES))
3304 gfc_error ("Structure name expected after RECORD at %C");
3305 if (m == MATCH_NO)
3306 return MATCH_NO;
3308 return MATCH_ERROR;
3312 /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3313 of expressions to substitute into the possibly parameterized expression
3314 'e'. Using a list is inefficient but should not be too bad since the
3315 number of type parameters is not likely to be large. */
3316 static bool
3317 insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3318 int* f)
3320 gfc_actual_arglist *param;
3321 gfc_expr *copy;
3323 if (e->expr_type != EXPR_VARIABLE)
3324 return false;
3326 gcc_assert (e->symtree);
3327 if (e->symtree->n.sym->attr.pdt_kind
3328 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3330 for (param = type_param_spec_list; param; param = param->next)
3331 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3332 break;
3334 if (param)
3336 copy = gfc_copy_expr (param->expr);
3337 *e = *copy;
3338 free (copy);
3342 return false;
3346 bool
3347 gfc_insert_kind_parameter_exprs (gfc_expr *e)
3349 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3353 bool
3354 gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3356 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3357 type_param_spec_list = param_list;
3358 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3359 type_param_spec_list = NULL;
3360 type_param_spec_list = old_param_spec_list;
3363 /* Determines the instance of a parameterized derived type to be used by
3364 matching determining the values of the kind parameters and using them
3365 in the name of the instance. If the instance exists, it is used, otherwise
3366 a new derived type is created. */
3367 match
3368 gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3369 gfc_actual_arglist **ext_param_list)
3371 /* The PDT template symbol. */
3372 gfc_symbol *pdt = *sym;
3373 /* The symbol for the parameter in the template f2k_namespace. */
3374 gfc_symbol *param;
3375 /* The hoped for instance of the PDT. */
3376 gfc_symbol *instance;
3377 /* The list of parameters appearing in the PDT declaration. */
3378 gfc_formal_arglist *type_param_name_list;
3379 /* Used to store the parameter specification list during recursive calls. */
3380 gfc_actual_arglist *old_param_spec_list;
3381 /* Pointers to the parameter specification being used. */
3382 gfc_actual_arglist *actual_param;
3383 gfc_actual_arglist *tail = NULL;
3384 /* Used to build up the name of the PDT instance. The prefix uses 4
3385 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3386 char name[GFC_MAX_SYMBOL_LEN + 21];
3388 bool name_seen = (param_list == NULL);
3389 bool assumed_seen = false;
3390 bool deferred_seen = false;
3391 bool spec_error = false;
3392 int kind_value, i;
3393 gfc_expr *kind_expr;
3394 gfc_component *c1, *c2;
3395 match m;
3397 type_param_spec_list = NULL;
3399 type_param_name_list = pdt->formal;
3400 actual_param = param_list;
3401 sprintf (name, "Pdt%s", pdt->name);
3403 /* Run through the parameter name list and pick up the actual
3404 parameter values or use the default values in the PDT declaration. */
3405 for (; type_param_name_list;
3406 type_param_name_list = type_param_name_list->next)
3408 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3410 if (actual_param->spec_type == SPEC_ASSUMED)
3411 spec_error = deferred_seen;
3412 else
3413 spec_error = assumed_seen;
3415 if (spec_error)
3417 gfc_error ("The type parameter spec list at %C cannot contain "
3418 "both ASSUMED and DEFERRED parameters");
3419 goto error_return;
3423 if (actual_param && actual_param->name)
3424 name_seen = true;
3425 param = type_param_name_list->sym;
3427 if (!param || !param->name)
3428 continue;
3430 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3431 /* An error should already have been thrown in resolve.c
3432 (resolve_fl_derived0). */
3433 if (!pdt->attr.use_assoc && !c1)
3434 goto error_return;
3436 kind_expr = NULL;
3437 if (!name_seen)
3439 if (!actual_param && !(c1 && c1->initializer))
3441 gfc_error ("The type parameter spec list at %C does not contain "
3442 "enough parameter expressions");
3443 goto error_return;
3445 else if (!actual_param && c1 && c1->initializer)
3446 kind_expr = gfc_copy_expr (c1->initializer);
3447 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3448 kind_expr = gfc_copy_expr (actual_param->expr);
3450 else
3452 actual_param = param_list;
3453 for (;actual_param; actual_param = actual_param->next)
3454 if (actual_param->name
3455 && strcmp (actual_param->name, param->name) == 0)
3456 break;
3457 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3458 kind_expr = gfc_copy_expr (actual_param->expr);
3459 else
3461 if (c1->initializer)
3462 kind_expr = gfc_copy_expr (c1->initializer);
3463 else if (!(actual_param && param->attr.pdt_len))
3465 gfc_error ("The derived parameter %qs at %C does not "
3466 "have a default value", param->name);
3467 goto error_return;
3472 /* Store the current parameter expressions in a temporary actual
3473 arglist 'list' so that they can be substituted in the corresponding
3474 expressions in the PDT instance. */
3475 if (type_param_spec_list == NULL)
3477 type_param_spec_list = gfc_get_actual_arglist ();
3478 tail = type_param_spec_list;
3480 else
3482 tail->next = gfc_get_actual_arglist ();
3483 tail = tail->next;
3485 tail->name = param->name;
3487 if (kind_expr)
3489 /* Try simplification even for LEN expressions. */
3490 gfc_resolve_expr (kind_expr);
3491 gfc_simplify_expr (kind_expr, 1);
3492 /* Variable expressions seem to default to BT_PROCEDURE.
3493 TODO find out why this is and fix it. */
3494 if (kind_expr->ts.type != BT_INTEGER
3495 && kind_expr->ts.type != BT_PROCEDURE)
3497 gfc_error ("The parameter expression at %C must be of "
3498 "INTEGER type and not %s type",
3499 gfc_basic_typename (kind_expr->ts.type));
3500 goto error_return;
3503 tail->expr = gfc_copy_expr (kind_expr);
3506 if (actual_param)
3507 tail->spec_type = actual_param->spec_type;
3509 if (!param->attr.pdt_kind)
3511 if (!name_seen && actual_param)
3512 actual_param = actual_param->next;
3513 if (kind_expr)
3515 gfc_free_expr (kind_expr);
3516 kind_expr = NULL;
3518 continue;
3521 if (actual_param
3522 && (actual_param->spec_type == SPEC_ASSUMED
3523 || actual_param->spec_type == SPEC_DEFERRED))
3525 gfc_error ("The KIND parameter %qs at %C cannot either be "
3526 "ASSUMED or DEFERRED", param->name);
3527 goto error_return;
3530 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3532 gfc_error ("The value for the KIND parameter %qs at %C does not "
3533 "reduce to a constant expression", param->name);
3534 goto error_return;
3537 gfc_extract_int (kind_expr, &kind_value);
3538 sprintf (name + strlen (name), "_%d", kind_value);
3540 if (!name_seen && actual_param)
3541 actual_param = actual_param->next;
3542 gfc_free_expr (kind_expr);
3545 if (!name_seen && actual_param)
3547 gfc_error ("The type parameter spec list at %C contains too many "
3548 "parameter expressions");
3549 goto error_return;
3552 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3553 build it, using 'pdt' as a template. */
3554 if (gfc_get_symbol (name, pdt->ns, &instance))
3556 gfc_error ("Parameterized derived type at %C is ambiguous");
3557 goto error_return;
3560 m = MATCH_YES;
3562 if (instance->attr.flavor == FL_DERIVED
3563 && instance->attr.pdt_type)
3565 instance->refs++;
3566 if (ext_param_list)
3567 *ext_param_list = type_param_spec_list;
3568 *sym = instance;
3569 gfc_commit_symbols ();
3570 return m;
3573 /* Start building the new instance of the parameterized type. */
3574 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3575 instance->attr.pdt_template = 0;
3576 instance->attr.pdt_type = 1;
3577 instance->declared_at = gfc_current_locus;
3579 /* Add the components, replacing the parameters in all expressions
3580 with the expressions for their values in 'type_param_spec_list'. */
3581 c1 = pdt->components;
3582 tail = type_param_spec_list;
3583 for (; c1; c1 = c1->next)
3585 gfc_add_component (instance, c1->name, &c2);
3587 c2->ts = c1->ts;
3588 c2->attr = c1->attr;
3590 /* The order of declaration of the type_specs might not be the
3591 same as that of the components. */
3592 if (c1->attr.pdt_kind || c1->attr.pdt_len)
3594 for (tail = type_param_spec_list; tail; tail = tail->next)
3595 if (strcmp (c1->name, tail->name) == 0)
3596 break;
3599 /* Deal with type extension by recursively calling this function
3600 to obtain the instance of the extended type. */
3601 if (gfc_current_state () != COMP_DERIVED
3602 && c1 == pdt->components
3603 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3604 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3605 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3607 gfc_formal_arglist *f;
3609 old_param_spec_list = type_param_spec_list;
3611 /* Obtain a spec list appropriate to the extended type..*/
3612 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3613 type_param_spec_list = actual_param;
3614 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3615 actual_param = actual_param->next;
3616 if (actual_param)
3618 gfc_free_actual_arglist (actual_param->next);
3619 actual_param->next = NULL;
3622 /* Now obtain the PDT instance for the extended type. */
3623 c2->param_list = type_param_spec_list;
3624 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3625 NULL);
3626 type_param_spec_list = old_param_spec_list;
3628 c2->ts.u.derived->refs++;
3629 gfc_set_sym_referenced (c2->ts.u.derived);
3631 /* Set extension level. */
3632 if (c2->ts.u.derived->attr.extension == 255)
3634 /* Since the extension field is 8 bit wide, we can only have
3635 up to 255 extension levels. */
3636 gfc_error ("Maximum extension level reached with type %qs at %L",
3637 c2->ts.u.derived->name,
3638 &c2->ts.u.derived->declared_at);
3639 goto error_return;
3641 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3643 continue;
3646 /* Set the component kind using the parameterized expression. */
3647 if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3648 && c1->kind_expr != NULL)
3650 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3651 gfc_insert_kind_parameter_exprs (e);
3652 gfc_simplify_expr (e, 1);
3653 gfc_extract_int (e, &c2->ts.kind);
3654 gfc_free_expr (e);
3655 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3657 gfc_error ("Kind %d not supported for type %s at %C",
3658 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3659 goto error_return;
3663 /* Similarly, set the string length if parameterized. */
3664 if (c1->ts.type == BT_CHARACTER
3665 && c1->ts.u.cl->length
3666 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3668 gfc_expr *e;
3669 e = gfc_copy_expr (c1->ts.u.cl->length);
3670 gfc_insert_kind_parameter_exprs (e);
3671 gfc_simplify_expr (e, 1);
3672 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3673 c2->ts.u.cl->length = e;
3674 c2->attr.pdt_string = 1;
3677 /* Set up either the KIND/LEN initializer, if constant,
3678 or the parameterized expression. Use the template
3679 initializer if one is not already set in this instance. */
3680 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3682 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3683 c2->initializer = gfc_copy_expr (tail->expr);
3684 else if (tail && tail->expr)
3686 c2->param_list = gfc_get_actual_arglist ();
3687 c2->param_list->name = tail->name;
3688 c2->param_list->expr = gfc_copy_expr (tail->expr);
3689 c2->param_list->next = NULL;
3692 if (!c2->initializer && c1->initializer)
3693 c2->initializer = gfc_copy_expr (c1->initializer);
3696 /* Copy the array spec. */
3697 c2->as = gfc_copy_array_spec (c1->as);
3698 if (c1->ts.type == BT_CLASS)
3699 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
3701 /* Determine if an array spec is parameterized. If so, substitute
3702 in the parameter expressions for the bounds and set the pdt_array
3703 attribute. Notice that this attribute must be unconditionally set
3704 if this is an array of parameterized character length. */
3705 if (c1->as && c1->as->type == AS_EXPLICIT)
3707 bool pdt_array = false;
3709 /* Are the bounds of the array parameterized? */
3710 for (i = 0; i < c1->as->rank; i++)
3712 if (gfc_derived_parameter_expr (c1->as->lower[i]))
3713 pdt_array = true;
3714 if (gfc_derived_parameter_expr (c1->as->upper[i]))
3715 pdt_array = true;
3718 /* If they are, free the expressions for the bounds and
3719 replace them with the template expressions with substitute
3720 values. */
3721 for (i = 0; pdt_array && i < c1->as->rank; i++)
3723 gfc_expr *e;
3724 e = gfc_copy_expr (c1->as->lower[i]);
3725 gfc_insert_kind_parameter_exprs (e);
3726 gfc_simplify_expr (e, 1);
3727 gfc_free_expr (c2->as->lower[i]);
3728 c2->as->lower[i] = e;
3729 e = gfc_copy_expr (c1->as->upper[i]);
3730 gfc_insert_kind_parameter_exprs (e);
3731 gfc_simplify_expr (e, 1);
3732 gfc_free_expr (c2->as->upper[i]);
3733 c2->as->upper[i] = e;
3735 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
3736 if (c1->initializer)
3738 c2->initializer = gfc_copy_expr (c1->initializer);
3739 gfc_insert_kind_parameter_exprs (c2->initializer);
3740 gfc_simplify_expr (c2->initializer, 1);
3744 /* Recurse into this function for PDT components. */
3745 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3746 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
3748 gfc_actual_arglist *params;
3749 /* The component in the template has a list of specification
3750 expressions derived from its declaration. */
3751 params = gfc_copy_actual_arglist (c1->param_list);
3752 actual_param = params;
3753 /* Substitute the template parameters with the expressions
3754 from the specification list. */
3755 for (;actual_param; actual_param = actual_param->next)
3756 gfc_insert_parameter_exprs (actual_param->expr,
3757 type_param_spec_list);
3759 /* Now obtain the PDT instance for the component. */
3760 old_param_spec_list = type_param_spec_list;
3761 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
3762 type_param_spec_list = old_param_spec_list;
3764 c2->param_list = params;
3765 if (!(c2->attr.pointer || c2->attr.allocatable))
3766 c2->initializer = gfc_default_initializer (&c2->ts);
3768 if (c2->attr.allocatable)
3769 instance->attr.alloc_comp = 1;
3773 gfc_commit_symbol (instance);
3774 if (ext_param_list)
3775 *ext_param_list = type_param_spec_list;
3776 *sym = instance;
3777 return m;
3779 error_return:
3780 gfc_free_actual_arglist (type_param_spec_list);
3781 return MATCH_ERROR;
3785 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
3786 structure to the matched specification. This is necessary for FUNCTION and
3787 IMPLICIT statements.
3789 If implicit_flag is nonzero, then we don't check for the optional
3790 kind specification. Not doing so is needed for matching an IMPLICIT
3791 statement correctly. */
3793 match
3794 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
3796 char name[GFC_MAX_SYMBOL_LEN + 1];
3797 gfc_symbol *sym, *dt_sym;
3798 match m;
3799 char c;
3800 bool seen_deferred_kind, matched_type;
3801 const char *dt_name;
3803 decl_type_param_list = NULL;
3805 /* A belt and braces check that the typespec is correctly being treated
3806 as a deferred characteristic association. */
3807 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
3808 && (gfc_current_block ()->result->ts.kind == -1)
3809 && (ts->kind == -1);
3810 gfc_clear_ts (ts);
3811 if (seen_deferred_kind)
3812 ts->kind = -1;
3814 /* Clear the current binding label, in case one is given. */
3815 curr_binding_label = NULL;
3817 if (gfc_match (" byte") == MATCH_YES)
3819 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
3820 return MATCH_ERROR;
3822 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
3824 gfc_error ("BYTE type used at %C "
3825 "is not available on the target machine");
3826 return MATCH_ERROR;
3829 ts->type = BT_INTEGER;
3830 ts->kind = 1;
3831 return MATCH_YES;
3835 m = gfc_match (" type (");
3836 matched_type = (m == MATCH_YES);
3837 if (matched_type)
3839 gfc_gobble_whitespace ();
3840 if (gfc_peek_ascii_char () == '*')
3842 if ((m = gfc_match ("*)")) != MATCH_YES)
3843 return m;
3844 if (gfc_comp_struct (gfc_current_state ()))
3846 gfc_error ("Assumed type at %C is not allowed for components");
3847 return MATCH_ERROR;
3849 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
3850 "at %C"))
3851 return MATCH_ERROR;
3852 ts->type = BT_ASSUMED;
3853 return MATCH_YES;
3856 m = gfc_match ("%n", name);
3857 matched_type = (m == MATCH_YES);
3860 if ((matched_type && strcmp ("integer", name) == 0)
3861 || (!matched_type && gfc_match (" integer") == MATCH_YES))
3863 ts->type = BT_INTEGER;
3864 ts->kind = gfc_default_integer_kind;
3865 goto get_kind;
3868 if ((matched_type && strcmp ("character", name) == 0)
3869 || (!matched_type && gfc_match (" character") == MATCH_YES))
3871 if (matched_type
3872 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3873 "intrinsic-type-spec at %C"))
3874 return MATCH_ERROR;
3876 ts->type = BT_CHARACTER;
3877 if (implicit_flag == 0)
3878 m = gfc_match_char_spec (ts);
3879 else
3880 m = MATCH_YES;
3882 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
3883 m = MATCH_ERROR;
3885 return m;
3888 if ((matched_type && strcmp ("real", name) == 0)
3889 || (!matched_type && gfc_match (" real") == MATCH_YES))
3891 ts->type = BT_REAL;
3892 ts->kind = gfc_default_real_kind;
3893 goto get_kind;
3896 if ((matched_type
3897 && (strcmp ("doubleprecision", name) == 0
3898 || (strcmp ("double", name) == 0
3899 && gfc_match (" precision") == MATCH_YES)))
3900 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
3902 if (matched_type
3903 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3904 "intrinsic-type-spec at %C"))
3905 return MATCH_ERROR;
3906 if (matched_type && gfc_match_char (')') != MATCH_YES)
3907 return MATCH_ERROR;
3909 ts->type = BT_REAL;
3910 ts->kind = gfc_default_double_kind;
3911 return MATCH_YES;
3914 if ((matched_type && strcmp ("complex", name) == 0)
3915 || (!matched_type && gfc_match (" complex") == MATCH_YES))
3917 ts->type = BT_COMPLEX;
3918 ts->kind = gfc_default_complex_kind;
3919 goto get_kind;
3922 if ((matched_type
3923 && (strcmp ("doublecomplex", name) == 0
3924 || (strcmp ("double", name) == 0
3925 && gfc_match (" complex") == MATCH_YES)))
3926 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
3928 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
3929 return MATCH_ERROR;
3931 if (matched_type
3932 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3933 "intrinsic-type-spec at %C"))
3934 return MATCH_ERROR;
3936 if (matched_type && gfc_match_char (')') != MATCH_YES)
3937 return MATCH_ERROR;
3939 ts->type = BT_COMPLEX;
3940 ts->kind = gfc_default_double_kind;
3941 return MATCH_YES;
3944 if ((matched_type && strcmp ("logical", name) == 0)
3945 || (!matched_type && gfc_match (" logical") == MATCH_YES))
3947 ts->type = BT_LOGICAL;
3948 ts->kind = gfc_default_logical_kind;
3949 goto get_kind;
3952 if (matched_type)
3954 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
3955 if (m == MATCH_ERROR)
3956 return m;
3958 m = gfc_match_char (')');
3961 if (m != MATCH_YES)
3962 m = match_record_decl (name);
3964 if (matched_type || m == MATCH_YES)
3966 ts->type = BT_DERIVED;
3967 /* We accept record/s/ or type(s) where s is a structure, but we
3968 * don't need all the extra derived-type stuff for structures. */
3969 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
3971 gfc_error ("Type name %qs at %C is ambiguous", name);
3972 return MATCH_ERROR;
3975 if (sym && sym->attr.flavor == FL_DERIVED
3976 && sym->attr.pdt_template
3977 && gfc_current_state () != COMP_DERIVED)
3979 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
3980 if (m != MATCH_YES)
3981 return m;
3982 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
3983 ts->u.derived = sym;
3984 strcpy (name, gfc_dt_lower_string (sym->name));
3987 if (sym && sym->attr.flavor == FL_STRUCT)
3989 ts->u.derived = sym;
3990 return MATCH_YES;
3992 /* Actually a derived type. */
3995 else
3997 /* Match nested STRUCTURE declarations; only valid within another
3998 structure declaration. */
3999 if (flag_dec_structure
4000 && (gfc_current_state () == COMP_STRUCTURE
4001 || gfc_current_state () == COMP_MAP))
4003 m = gfc_match (" structure");
4004 if (m == MATCH_YES)
4006 m = gfc_match_structure_decl ();
4007 if (m == MATCH_YES)
4009 /* gfc_new_block is updated by match_structure_decl. */
4010 ts->type = BT_DERIVED;
4011 ts->u.derived = gfc_new_block;
4012 return MATCH_YES;
4015 if (m == MATCH_ERROR)
4016 return MATCH_ERROR;
4019 /* Match CLASS declarations. */
4020 m = gfc_match (" class ( * )");
4021 if (m == MATCH_ERROR)
4022 return MATCH_ERROR;
4023 else if (m == MATCH_YES)
4025 gfc_symbol *upe;
4026 gfc_symtree *st;
4027 ts->type = BT_CLASS;
4028 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4029 if (upe == NULL)
4031 upe = gfc_new_symbol ("STAR", gfc_current_ns);
4032 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4033 st->n.sym = upe;
4034 gfc_set_sym_referenced (upe);
4035 upe->refs++;
4036 upe->ts.type = BT_VOID;
4037 upe->attr.unlimited_polymorphic = 1;
4038 /* This is essential to force the construction of
4039 unlimited polymorphic component class containers. */
4040 upe->attr.zero_comp = 1;
4041 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4042 &gfc_current_locus))
4043 return MATCH_ERROR;
4045 else
4047 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4048 st->n.sym = upe;
4049 upe->refs++;
4051 ts->u.derived = upe;
4052 return m;
4055 m = gfc_match (" class (");
4057 if (m == MATCH_YES)
4058 m = gfc_match ("%n", name);
4059 else
4060 return m;
4062 if (m != MATCH_YES)
4063 return m;
4064 ts->type = BT_CLASS;
4066 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4067 return MATCH_ERROR;
4069 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4070 if (m == MATCH_ERROR)
4071 return m;
4073 m = gfc_match_char (')');
4074 if (m != MATCH_YES)
4075 return m;
4078 /* Defer association of the derived type until the end of the
4079 specification block. However, if the derived type can be
4080 found, add it to the typespec. */
4081 if (gfc_matching_function)
4083 ts->u.derived = NULL;
4084 if (gfc_current_state () != COMP_INTERFACE
4085 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4087 sym = gfc_find_dt_in_generic (sym);
4088 ts->u.derived = sym;
4090 return MATCH_YES;
4093 /* Search for the name but allow the components to be defined later. If
4094 type = -1, this typespec has been seen in a function declaration but
4095 the type could not be accessed at that point. The actual derived type is
4096 stored in a symtree with the first letter of the name capitalized; the
4097 symtree with the all lower-case name contains the associated
4098 generic function. */
4099 dt_name = gfc_dt_upper_string (name);
4100 sym = NULL;
4101 dt_sym = NULL;
4102 if (ts->kind != -1)
4104 gfc_get_ha_symbol (name, &sym);
4105 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4107 gfc_error ("Type name %qs at %C is ambiguous", name);
4108 return MATCH_ERROR;
4110 if (sym->generic && !dt_sym)
4111 dt_sym = gfc_find_dt_in_generic (sym);
4113 /* Host associated PDTs can get confused with their constructors
4114 because they ar instantiated in the template's namespace. */
4115 if (!dt_sym)
4117 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4119 gfc_error ("Type name %qs at %C is ambiguous", name);
4120 return MATCH_ERROR;
4122 if (dt_sym && !dt_sym->attr.pdt_type)
4123 dt_sym = NULL;
4126 else if (ts->kind == -1)
4128 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4129 || gfc_current_ns->has_import_set;
4130 gfc_find_symbol (name, NULL, iface, &sym);
4131 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4133 gfc_error ("Type name %qs at %C is ambiguous", name);
4134 return MATCH_ERROR;
4136 if (sym && sym->generic && !dt_sym)
4137 dt_sym = gfc_find_dt_in_generic (sym);
4139 ts->kind = 0;
4140 if (sym == NULL)
4141 return MATCH_NO;
4144 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4145 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4146 || sym->attr.subroutine)
4148 gfc_error ("Type name %qs at %C conflicts with previously declared "
4149 "entity at %L, which has the same name", name,
4150 &sym->declared_at);
4151 return MATCH_ERROR;
4154 if (sym && sym->attr.flavor == FL_DERIVED
4155 && sym->attr.pdt_template
4156 && gfc_current_state () != COMP_DERIVED)
4158 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4159 if (m != MATCH_YES)
4160 return m;
4161 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4162 ts->u.derived = sym;
4163 strcpy (name, gfc_dt_lower_string (sym->name));
4166 gfc_save_symbol_data (sym);
4167 gfc_set_sym_referenced (sym);
4168 if (!sym->attr.generic
4169 && !gfc_add_generic (&sym->attr, sym->name, NULL))
4170 return MATCH_ERROR;
4172 if (!sym->attr.function
4173 && !gfc_add_function (&sym->attr, sym->name, NULL))
4174 return MATCH_ERROR;
4176 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4177 && dt_sym->attr.pdt_template
4178 && gfc_current_state () != COMP_DERIVED)
4180 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4181 if (m != MATCH_YES)
4182 return m;
4183 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4186 if (!dt_sym)
4188 gfc_interface *intr, *head;
4190 /* Use upper case to save the actual derived-type symbol. */
4191 gfc_get_symbol (dt_name, NULL, &dt_sym);
4192 dt_sym->name = gfc_get_string ("%s", sym->name);
4193 head = sym->generic;
4194 intr = gfc_get_interface ();
4195 intr->sym = dt_sym;
4196 intr->where = gfc_current_locus;
4197 intr->next = head;
4198 sym->generic = intr;
4199 sym->attr.if_source = IFSRC_DECL;
4201 else
4202 gfc_save_symbol_data (dt_sym);
4204 gfc_set_sym_referenced (dt_sym);
4206 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4207 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4208 return MATCH_ERROR;
4210 ts->u.derived = dt_sym;
4212 return MATCH_YES;
4214 get_kind:
4215 if (matched_type
4216 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4217 "intrinsic-type-spec at %C"))
4218 return MATCH_ERROR;
4220 /* For all types except double, derived and character, look for an
4221 optional kind specifier. MATCH_NO is actually OK at this point. */
4222 if (implicit_flag == 1)
4224 if (matched_type && gfc_match_char (')') != MATCH_YES)
4225 return MATCH_ERROR;
4227 return MATCH_YES;
4230 if (gfc_current_form == FORM_FREE)
4232 c = gfc_peek_ascii_char ();
4233 if (!gfc_is_whitespace (c) && c != '*' && c != '('
4234 && c != ':' && c != ',')
4236 if (matched_type && c == ')')
4238 gfc_next_ascii_char ();
4239 return MATCH_YES;
4241 return MATCH_NO;
4245 m = gfc_match_kind_spec (ts, false);
4246 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4248 m = gfc_match_old_kind_spec (ts);
4249 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4250 return MATCH_ERROR;
4253 if (matched_type && gfc_match_char (')') != MATCH_YES)
4254 return MATCH_ERROR;
4256 /* Defer association of the KIND expression of function results
4257 until after USE and IMPORT statements. */
4258 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4259 || gfc_matching_function)
4260 return MATCH_YES;
4262 if (m == MATCH_NO)
4263 m = MATCH_YES; /* No kind specifier found. */
4265 return m;
4269 /* Match an IMPLICIT NONE statement. Actually, this statement is
4270 already matched in parse.c, or we would not end up here in the
4271 first place. So the only thing we need to check, is if there is
4272 trailing garbage. If not, the match is successful. */
4274 match
4275 gfc_match_implicit_none (void)
4277 char c;
4278 match m;
4279 char name[GFC_MAX_SYMBOL_LEN + 1];
4280 bool type = false;
4281 bool external = false;
4282 locus cur_loc = gfc_current_locus;
4284 if (gfc_current_ns->seen_implicit_none
4285 || gfc_current_ns->has_implicit_none_export)
4287 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4288 return MATCH_ERROR;
4291 gfc_gobble_whitespace ();
4292 c = gfc_peek_ascii_char ();
4293 if (c == '(')
4295 (void) gfc_next_ascii_char ();
4296 if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
4297 return MATCH_ERROR;
4299 gfc_gobble_whitespace ();
4300 if (gfc_peek_ascii_char () == ')')
4302 (void) gfc_next_ascii_char ();
4303 type = true;
4305 else
4306 for(;;)
4308 m = gfc_match (" %n", name);
4309 if (m != MATCH_YES)
4310 return MATCH_ERROR;
4312 if (strcmp (name, "type") == 0)
4313 type = true;
4314 else if (strcmp (name, "external") == 0)
4315 external = true;
4316 else
4317 return MATCH_ERROR;
4319 gfc_gobble_whitespace ();
4320 c = gfc_next_ascii_char ();
4321 if (c == ',')
4322 continue;
4323 if (c == ')')
4324 break;
4325 return MATCH_ERROR;
4328 else
4329 type = true;
4331 if (gfc_match_eos () != MATCH_YES)
4332 return MATCH_ERROR;
4334 gfc_set_implicit_none (type, external, &cur_loc);
4336 return MATCH_YES;
4340 /* Match the letter range(s) of an IMPLICIT statement. */
4342 static match
4343 match_implicit_range (void)
4345 char c, c1, c2;
4346 int inner;
4347 locus cur_loc;
4349 cur_loc = gfc_current_locus;
4351 gfc_gobble_whitespace ();
4352 c = gfc_next_ascii_char ();
4353 if (c != '(')
4355 gfc_error ("Missing character range in IMPLICIT at %C");
4356 goto bad;
4359 inner = 1;
4360 while (inner)
4362 gfc_gobble_whitespace ();
4363 c1 = gfc_next_ascii_char ();
4364 if (!ISALPHA (c1))
4365 goto bad;
4367 gfc_gobble_whitespace ();
4368 c = gfc_next_ascii_char ();
4370 switch (c)
4372 case ')':
4373 inner = 0; /* Fall through. */
4375 case ',':
4376 c2 = c1;
4377 break;
4379 case '-':
4380 gfc_gobble_whitespace ();
4381 c2 = gfc_next_ascii_char ();
4382 if (!ISALPHA (c2))
4383 goto bad;
4385 gfc_gobble_whitespace ();
4386 c = gfc_next_ascii_char ();
4388 if ((c != ',') && (c != ')'))
4389 goto bad;
4390 if (c == ')')
4391 inner = 0;
4393 break;
4395 default:
4396 goto bad;
4399 if (c1 > c2)
4401 gfc_error ("Letters must be in alphabetic order in "
4402 "IMPLICIT statement at %C");
4403 goto bad;
4406 /* See if we can add the newly matched range to the pending
4407 implicits from this IMPLICIT statement. We do not check for
4408 conflicts with whatever earlier IMPLICIT statements may have
4409 set. This is done when we've successfully finished matching
4410 the current one. */
4411 if (!gfc_add_new_implicit_range (c1, c2))
4412 goto bad;
4415 return MATCH_YES;
4417 bad:
4418 gfc_syntax_error (ST_IMPLICIT);
4420 gfc_current_locus = cur_loc;
4421 return MATCH_ERROR;
4425 /* Match an IMPLICIT statement, storing the types for
4426 gfc_set_implicit() if the statement is accepted by the parser.
4427 There is a strange looking, but legal syntactic construction
4428 possible. It looks like:
4430 IMPLICIT INTEGER (a-b) (c-d)
4432 This is legal if "a-b" is a constant expression that happens to
4433 equal one of the legal kinds for integers. The real problem
4434 happens with an implicit specification that looks like:
4436 IMPLICIT INTEGER (a-b)
4438 In this case, a typespec matcher that is "greedy" (as most of the
4439 matchers are) gobbles the character range as a kindspec, leaving
4440 nothing left. We therefore have to go a bit more slowly in the
4441 matching process by inhibiting the kindspec checking during
4442 typespec matching and checking for a kind later. */
4444 match
4445 gfc_match_implicit (void)
4447 gfc_typespec ts;
4448 locus cur_loc;
4449 char c;
4450 match m;
4452 if (gfc_current_ns->seen_implicit_none)
4454 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4455 "statement");
4456 return MATCH_ERROR;
4459 gfc_clear_ts (&ts);
4461 /* We don't allow empty implicit statements. */
4462 if (gfc_match_eos () == MATCH_YES)
4464 gfc_error ("Empty IMPLICIT statement at %C");
4465 return MATCH_ERROR;
4470 /* First cleanup. */
4471 gfc_clear_new_implicit ();
4473 /* A basic type is mandatory here. */
4474 m = gfc_match_decl_type_spec (&ts, 1);
4475 if (m == MATCH_ERROR)
4476 goto error;
4477 if (m == MATCH_NO)
4478 goto syntax;
4480 cur_loc = gfc_current_locus;
4481 m = match_implicit_range ();
4483 if (m == MATCH_YES)
4485 /* We may have <TYPE> (<RANGE>). */
4486 gfc_gobble_whitespace ();
4487 c = gfc_peek_ascii_char ();
4488 if (c == ',' || c == '\n' || c == ';' || c == '!')
4490 /* Check for CHARACTER with no length parameter. */
4491 if (ts.type == BT_CHARACTER && !ts.u.cl)
4493 ts.kind = gfc_default_character_kind;
4494 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4495 ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4496 NULL, 1);
4499 /* Record the Successful match. */
4500 if (!gfc_merge_new_implicit (&ts))
4501 return MATCH_ERROR;
4502 if (c == ',')
4503 c = gfc_next_ascii_char ();
4504 else if (gfc_match_eos () == MATCH_ERROR)
4505 goto error;
4506 continue;
4509 gfc_current_locus = cur_loc;
4512 /* Discard the (incorrectly) matched range. */
4513 gfc_clear_new_implicit ();
4515 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4516 if (ts.type == BT_CHARACTER)
4517 m = gfc_match_char_spec (&ts);
4518 else
4520 m = gfc_match_kind_spec (&ts, false);
4521 if (m == MATCH_NO)
4523 m = gfc_match_old_kind_spec (&ts);
4524 if (m == MATCH_ERROR)
4525 goto error;
4526 if (m == MATCH_NO)
4527 goto syntax;
4530 if (m == MATCH_ERROR)
4531 goto error;
4533 m = match_implicit_range ();
4534 if (m == MATCH_ERROR)
4535 goto error;
4536 if (m == MATCH_NO)
4537 goto syntax;
4539 gfc_gobble_whitespace ();
4540 c = gfc_next_ascii_char ();
4541 if (c != ',' && gfc_match_eos () != MATCH_YES)
4542 goto syntax;
4544 if (!gfc_merge_new_implicit (&ts))
4545 return MATCH_ERROR;
4547 while (c == ',');
4549 return MATCH_YES;
4551 syntax:
4552 gfc_syntax_error (ST_IMPLICIT);
4554 error:
4555 return MATCH_ERROR;
4559 match
4560 gfc_match_import (void)
4562 char name[GFC_MAX_SYMBOL_LEN + 1];
4563 match m;
4564 gfc_symbol *sym;
4565 gfc_symtree *st;
4567 if (gfc_current_ns->proc_name == NULL
4568 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4570 gfc_error ("IMPORT statement at %C only permitted in "
4571 "an INTERFACE body");
4572 return MATCH_ERROR;
4575 if (gfc_current_ns->proc_name->attr.module_procedure)
4577 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4578 "in a module procedure interface body");
4579 return MATCH_ERROR;
4582 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4583 return MATCH_ERROR;
4585 if (gfc_match_eos () == MATCH_YES)
4587 /* All host variables should be imported. */
4588 gfc_current_ns->has_import_set = 1;
4589 return MATCH_YES;
4592 if (gfc_match (" ::") == MATCH_YES)
4594 if (gfc_match_eos () == MATCH_YES)
4596 gfc_error ("Expecting list of named entities at %C");
4597 return MATCH_ERROR;
4601 for(;;)
4603 sym = NULL;
4604 m = gfc_match (" %n", name);
4605 switch (m)
4607 case MATCH_YES:
4608 if (gfc_current_ns->parent != NULL
4609 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4611 gfc_error ("Type name %qs at %C is ambiguous", name);
4612 return MATCH_ERROR;
4614 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
4615 && gfc_find_symbol (name,
4616 gfc_current_ns->proc_name->ns->parent,
4617 1, &sym))
4619 gfc_error ("Type name %qs at %C is ambiguous", name);
4620 return MATCH_ERROR;
4623 if (sym == NULL)
4625 gfc_error ("Cannot IMPORT %qs from host scoping unit "
4626 "at %C - does not exist.", name);
4627 return MATCH_ERROR;
4630 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4632 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4633 "at %C", name);
4634 goto next_item;
4637 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4638 st->n.sym = sym;
4639 sym->refs++;
4640 sym->attr.imported = 1;
4642 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4644 /* The actual derived type is stored in a symtree with the first
4645 letter of the name capitalized; the symtree with the all
4646 lower-case name contains the associated generic function. */
4647 st = gfc_new_symtree (&gfc_current_ns->sym_root,
4648 gfc_dt_upper_string (name));
4649 st->n.sym = sym;
4650 sym->refs++;
4651 sym->attr.imported = 1;
4654 goto next_item;
4656 case MATCH_NO:
4657 break;
4659 case MATCH_ERROR:
4660 return MATCH_ERROR;
4663 next_item:
4664 if (gfc_match_eos () == MATCH_YES)
4665 break;
4666 if (gfc_match_char (',') != MATCH_YES)
4667 goto syntax;
4670 return MATCH_YES;
4672 syntax:
4673 gfc_error ("Syntax error in IMPORT statement at %C");
4674 return MATCH_ERROR;
4678 /* A minimal implementation of gfc_match without whitespace, escape
4679 characters or variable arguments. Returns true if the next
4680 characters match the TARGET template exactly. */
4682 static bool
4683 match_string_p (const char *target)
4685 const char *p;
4687 for (p = target; *p; p++)
4688 if ((char) gfc_next_ascii_char () != *p)
4689 return false;
4690 return true;
4693 /* Matches an attribute specification including array specs. If
4694 successful, leaves the variables current_attr and current_as
4695 holding the specification. Also sets the colon_seen variable for
4696 later use by matchers associated with initializations.
4698 This subroutine is a little tricky in the sense that we don't know
4699 if we really have an attr-spec until we hit the double colon.
4700 Until that time, we can only return MATCH_NO. This forces us to
4701 check for duplicate specification at this level. */
4703 static match
4704 match_attr_spec (void)
4706 /* Modifiers that can exist in a type statement. */
4707 enum
4708 { GFC_DECL_BEGIN = 0,
4709 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
4710 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
4711 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
4712 DECL_STATIC, DECL_AUTOMATIC,
4713 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
4714 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
4715 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
4718 /* GFC_DECL_END is the sentinel, index starts at 0. */
4719 #define NUM_DECL GFC_DECL_END
4721 locus start, seen_at[NUM_DECL];
4722 int seen[NUM_DECL];
4723 unsigned int d;
4724 const char *attr;
4725 match m;
4726 bool t;
4728 gfc_clear_attr (&current_attr);
4729 start = gfc_current_locus;
4731 current_as = NULL;
4732 colon_seen = 0;
4733 attr_seen = 0;
4735 /* See if we get all of the keywords up to the final double colon. */
4736 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4737 seen[d] = 0;
4739 for (;;)
4741 char ch;
4743 d = DECL_NONE;
4744 gfc_gobble_whitespace ();
4746 ch = gfc_next_ascii_char ();
4747 if (ch == ':')
4749 /* This is the successful exit condition for the loop. */
4750 if (gfc_next_ascii_char () == ':')
4751 break;
4753 else if (ch == ',')
4755 gfc_gobble_whitespace ();
4756 switch (gfc_peek_ascii_char ())
4758 case 'a':
4759 gfc_next_ascii_char ();
4760 switch (gfc_next_ascii_char ())
4762 case 'l':
4763 if (match_string_p ("locatable"))
4765 /* Matched "allocatable". */
4766 d = DECL_ALLOCATABLE;
4768 break;
4770 case 's':
4771 if (match_string_p ("ynchronous"))
4773 /* Matched "asynchronous". */
4774 d = DECL_ASYNCHRONOUS;
4776 break;
4778 case 'u':
4779 if (match_string_p ("tomatic"))
4781 /* Matched "automatic". */
4782 d = DECL_AUTOMATIC;
4784 break;
4786 break;
4788 case 'b':
4789 /* Try and match the bind(c). */
4790 m = gfc_match_bind_c (NULL, true);
4791 if (m == MATCH_YES)
4792 d = DECL_IS_BIND_C;
4793 else if (m == MATCH_ERROR)
4794 goto cleanup;
4795 break;
4797 case 'c':
4798 gfc_next_ascii_char ();
4799 if ('o' != gfc_next_ascii_char ())
4800 break;
4801 switch (gfc_next_ascii_char ())
4803 case 'd':
4804 if (match_string_p ("imension"))
4806 d = DECL_CODIMENSION;
4807 break;
4809 /* FALLTHRU */
4810 case 'n':
4811 if (match_string_p ("tiguous"))
4813 d = DECL_CONTIGUOUS;
4814 break;
4817 break;
4819 case 'd':
4820 if (match_string_p ("dimension"))
4821 d = DECL_DIMENSION;
4822 break;
4824 case 'e':
4825 if (match_string_p ("external"))
4826 d = DECL_EXTERNAL;
4827 break;
4829 case 'i':
4830 if (match_string_p ("int"))
4832 ch = gfc_next_ascii_char ();
4833 if (ch == 'e')
4835 if (match_string_p ("nt"))
4837 /* Matched "intent". */
4838 /* TODO: Call match_intent_spec from here. */
4839 if (gfc_match (" ( in out )") == MATCH_YES)
4840 d = DECL_INOUT;
4841 else if (gfc_match (" ( in )") == MATCH_YES)
4842 d = DECL_IN;
4843 else if (gfc_match (" ( out )") == MATCH_YES)
4844 d = DECL_OUT;
4847 else if (ch == 'r')
4849 if (match_string_p ("insic"))
4851 /* Matched "intrinsic". */
4852 d = DECL_INTRINSIC;
4856 break;
4858 case 'k':
4859 if (match_string_p ("kind"))
4860 d = DECL_KIND;
4861 break;
4863 case 'l':
4864 if (match_string_p ("len"))
4865 d = DECL_LEN;
4866 break;
4868 case 'o':
4869 if (match_string_p ("optional"))
4870 d = DECL_OPTIONAL;
4871 break;
4873 case 'p':
4874 gfc_next_ascii_char ();
4875 switch (gfc_next_ascii_char ())
4877 case 'a':
4878 if (match_string_p ("rameter"))
4880 /* Matched "parameter". */
4881 d = DECL_PARAMETER;
4883 break;
4885 case 'o':
4886 if (match_string_p ("inter"))
4888 /* Matched "pointer". */
4889 d = DECL_POINTER;
4891 break;
4893 case 'r':
4894 ch = gfc_next_ascii_char ();
4895 if (ch == 'i')
4897 if (match_string_p ("vate"))
4899 /* Matched "private". */
4900 d = DECL_PRIVATE;
4903 else if (ch == 'o')
4905 if (match_string_p ("tected"))
4907 /* Matched "protected". */
4908 d = DECL_PROTECTED;
4911 break;
4913 case 'u':
4914 if (match_string_p ("blic"))
4916 /* Matched "public". */
4917 d = DECL_PUBLIC;
4919 break;
4921 break;
4923 case 's':
4924 gfc_next_ascii_char ();
4925 switch (gfc_next_ascii_char ())
4927 case 'a':
4928 if (match_string_p ("ve"))
4930 /* Matched "save". */
4931 d = DECL_SAVE;
4933 break;
4935 case 't':
4936 if (match_string_p ("atic"))
4938 /* Matched "static". */
4939 d = DECL_STATIC;
4941 break;
4943 break;
4945 case 't':
4946 if (match_string_p ("target"))
4947 d = DECL_TARGET;
4948 break;
4950 case 'v':
4951 gfc_next_ascii_char ();
4952 ch = gfc_next_ascii_char ();
4953 if (ch == 'a')
4955 if (match_string_p ("lue"))
4957 /* Matched "value". */
4958 d = DECL_VALUE;
4961 else if (ch == 'o')
4963 if (match_string_p ("latile"))
4965 /* Matched "volatile". */
4966 d = DECL_VOLATILE;
4969 break;
4973 /* No double colon and no recognizable decl_type, so assume that
4974 we've been looking at something else the whole time. */
4975 if (d == DECL_NONE)
4977 m = MATCH_NO;
4978 goto cleanup;
4981 /* Check to make sure any parens are paired up correctly. */
4982 if (gfc_match_parens () == MATCH_ERROR)
4984 m = MATCH_ERROR;
4985 goto cleanup;
4988 seen[d]++;
4989 seen_at[d] = gfc_current_locus;
4991 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
4993 gfc_array_spec *as = NULL;
4995 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
4996 d == DECL_CODIMENSION);
4998 if (current_as == NULL)
4999 current_as = as;
5000 else if (m == MATCH_YES)
5002 if (!merge_array_spec (as, current_as, false))
5003 m = MATCH_ERROR;
5004 free (as);
5007 if (m == MATCH_NO)
5009 if (d == DECL_CODIMENSION)
5010 gfc_error ("Missing codimension specification at %C");
5011 else
5012 gfc_error ("Missing dimension specification at %C");
5013 m = MATCH_ERROR;
5016 if (m == MATCH_ERROR)
5017 goto cleanup;
5021 /* Since we've seen a double colon, we have to be looking at an
5022 attr-spec. This means that we can now issue errors. */
5023 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5024 if (seen[d] > 1)
5026 switch (d)
5028 case DECL_ALLOCATABLE:
5029 attr = "ALLOCATABLE";
5030 break;
5031 case DECL_ASYNCHRONOUS:
5032 attr = "ASYNCHRONOUS";
5033 break;
5034 case DECL_CODIMENSION:
5035 attr = "CODIMENSION";
5036 break;
5037 case DECL_CONTIGUOUS:
5038 attr = "CONTIGUOUS";
5039 break;
5040 case DECL_DIMENSION:
5041 attr = "DIMENSION";
5042 break;
5043 case DECL_EXTERNAL:
5044 attr = "EXTERNAL";
5045 break;
5046 case DECL_IN:
5047 attr = "INTENT (IN)";
5048 break;
5049 case DECL_OUT:
5050 attr = "INTENT (OUT)";
5051 break;
5052 case DECL_INOUT:
5053 attr = "INTENT (IN OUT)";
5054 break;
5055 case DECL_INTRINSIC:
5056 attr = "INTRINSIC";
5057 break;
5058 case DECL_OPTIONAL:
5059 attr = "OPTIONAL";
5060 break;
5061 case DECL_KIND:
5062 attr = "KIND";
5063 break;
5064 case DECL_LEN:
5065 attr = "LEN";
5066 break;
5067 case DECL_PARAMETER:
5068 attr = "PARAMETER";
5069 break;
5070 case DECL_POINTER:
5071 attr = "POINTER";
5072 break;
5073 case DECL_PROTECTED:
5074 attr = "PROTECTED";
5075 break;
5076 case DECL_PRIVATE:
5077 attr = "PRIVATE";
5078 break;
5079 case DECL_PUBLIC:
5080 attr = "PUBLIC";
5081 break;
5082 case DECL_SAVE:
5083 attr = "SAVE";
5084 break;
5085 case DECL_STATIC:
5086 attr = "STATIC";
5087 break;
5088 case DECL_AUTOMATIC:
5089 attr = "AUTOMATIC";
5090 break;
5091 case DECL_TARGET:
5092 attr = "TARGET";
5093 break;
5094 case DECL_IS_BIND_C:
5095 attr = "IS_BIND_C";
5096 break;
5097 case DECL_VALUE:
5098 attr = "VALUE";
5099 break;
5100 case DECL_VOLATILE:
5101 attr = "VOLATILE";
5102 break;
5103 default:
5104 attr = NULL; /* This shouldn't happen. */
5107 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5108 m = MATCH_ERROR;
5109 goto cleanup;
5112 /* Now that we've dealt with duplicate attributes, add the attributes
5113 to the current attribute. */
5114 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5116 if (seen[d] == 0)
5117 continue;
5118 else
5119 attr_seen = 1;
5121 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5122 && !flag_dec_static)
5124 gfc_error ("%s at %L is a DEC extension, enable with "
5125 "%<-fdec-static%>",
5126 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5127 m = MATCH_ERROR;
5128 goto cleanup;
5130 /* Allow SAVE with STATIC, but don't complain. */
5131 if (d == DECL_STATIC && seen[DECL_SAVE])
5132 continue;
5134 if (gfc_current_state () == COMP_DERIVED
5135 && d != DECL_DIMENSION && d != DECL_CODIMENSION
5136 && d != DECL_POINTER && d != DECL_PRIVATE
5137 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
5139 if (d == DECL_ALLOCATABLE)
5141 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
5142 "attribute at %C in a TYPE definition"))
5144 m = MATCH_ERROR;
5145 goto cleanup;
5148 else if (d == DECL_KIND)
5150 if (!gfc_notify_std (GFC_STD_F2003, "KIND "
5151 "attribute at %C in a TYPE definition"))
5153 m = MATCH_ERROR;
5154 goto cleanup;
5156 if (current_ts.type != BT_INTEGER)
5158 gfc_error ("Component with KIND attribute at %C must be "
5159 "INTEGER");
5160 m = MATCH_ERROR;
5161 goto cleanup;
5163 if (current_ts.kind != gfc_default_integer_kind)
5165 gfc_error ("Component with KIND attribute at %C must be "
5166 "default integer kind (%d)",
5167 gfc_default_integer_kind);
5168 m = MATCH_ERROR;
5169 goto cleanup;
5172 else if (d == DECL_LEN)
5174 if (!gfc_notify_std (GFC_STD_F2003, "LEN "
5175 "attribute at %C in a TYPE definition"))
5177 m = MATCH_ERROR;
5178 goto cleanup;
5180 if (current_ts.type != BT_INTEGER)
5182 gfc_error ("Component with LEN attribute at %C must be "
5183 "INTEGER");
5184 m = MATCH_ERROR;
5185 goto cleanup;
5187 if (current_ts.kind != gfc_default_integer_kind)
5189 gfc_error ("Component with LEN attribute at %C must be "
5190 "default integer kind (%d)",
5191 gfc_default_integer_kind);
5192 m = MATCH_ERROR;
5193 goto cleanup;
5196 else
5198 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
5199 &seen_at[d]);
5200 m = MATCH_ERROR;
5201 goto cleanup;
5205 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5206 && gfc_current_state () != COMP_MODULE)
5208 if (d == DECL_PRIVATE)
5209 attr = "PRIVATE";
5210 else
5211 attr = "PUBLIC";
5212 if (gfc_current_state () == COMP_DERIVED
5213 && gfc_state_stack->previous
5214 && gfc_state_stack->previous->state == COMP_MODULE)
5216 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5217 "at %L in a TYPE definition", attr,
5218 &seen_at[d]))
5220 m = MATCH_ERROR;
5221 goto cleanup;
5224 else
5226 gfc_error ("%s attribute at %L is not allowed outside of the "
5227 "specification part of a module", attr, &seen_at[d]);
5228 m = MATCH_ERROR;
5229 goto cleanup;
5233 if (gfc_current_state () != COMP_DERIVED
5234 && (d == DECL_KIND || d == DECL_LEN))
5236 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5237 "definition", &seen_at[d]);
5238 m = MATCH_ERROR;
5239 goto cleanup;
5242 switch (d)
5244 case DECL_ALLOCATABLE:
5245 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5246 break;
5248 case DECL_ASYNCHRONOUS:
5249 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5250 t = false;
5251 else
5252 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5253 break;
5255 case DECL_CODIMENSION:
5256 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5257 break;
5259 case DECL_CONTIGUOUS:
5260 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5261 t = false;
5262 else
5263 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5264 break;
5266 case DECL_DIMENSION:
5267 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5268 break;
5270 case DECL_EXTERNAL:
5271 t = gfc_add_external (&current_attr, &seen_at[d]);
5272 break;
5274 case DECL_IN:
5275 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5276 break;
5278 case DECL_OUT:
5279 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5280 break;
5282 case DECL_INOUT:
5283 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5284 break;
5286 case DECL_INTRINSIC:
5287 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5288 break;
5290 case DECL_OPTIONAL:
5291 t = gfc_add_optional (&current_attr, &seen_at[d]);
5292 break;
5294 case DECL_KIND:
5295 t = gfc_add_kind (&current_attr, &seen_at[d]);
5296 break;
5298 case DECL_LEN:
5299 t = gfc_add_len (&current_attr, &seen_at[d]);
5300 break;
5302 case DECL_PARAMETER:
5303 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5304 break;
5306 case DECL_POINTER:
5307 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5308 break;
5310 case DECL_PROTECTED:
5311 if (gfc_current_state () != COMP_MODULE
5312 || (gfc_current_ns->proc_name
5313 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5315 gfc_error ("PROTECTED at %C only allowed in specification "
5316 "part of a module");
5317 t = false;
5318 break;
5321 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5322 t = false;
5323 else
5324 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5325 break;
5327 case DECL_PRIVATE:
5328 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5329 &seen_at[d]);
5330 break;
5332 case DECL_PUBLIC:
5333 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5334 &seen_at[d]);
5335 break;
5337 case DECL_STATIC:
5338 case DECL_SAVE:
5339 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5340 break;
5342 case DECL_AUTOMATIC:
5343 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5344 break;
5346 case DECL_TARGET:
5347 t = gfc_add_target (&current_attr, &seen_at[d]);
5348 break;
5350 case DECL_IS_BIND_C:
5351 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5352 break;
5354 case DECL_VALUE:
5355 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5356 t = false;
5357 else
5358 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5359 break;
5361 case DECL_VOLATILE:
5362 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5363 t = false;
5364 else
5365 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5366 break;
5368 default:
5369 gfc_internal_error ("match_attr_spec(): Bad attribute");
5372 if (!t)
5374 m = MATCH_ERROR;
5375 goto cleanup;
5379 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
5380 if ((gfc_current_state () == COMP_MODULE
5381 || gfc_current_state () == COMP_SUBMODULE)
5382 && !current_attr.save
5383 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5384 current_attr.save = SAVE_IMPLICIT;
5386 colon_seen = 1;
5387 return MATCH_YES;
5389 cleanup:
5390 gfc_current_locus = start;
5391 gfc_free_array_spec (current_as);
5392 current_as = NULL;
5393 attr_seen = 0;
5394 return m;
5398 /* Set the binding label, dest_label, either with the binding label
5399 stored in the given gfc_typespec, ts, or if none was provided, it
5400 will be the symbol name in all lower case, as required by the draft
5401 (J3/04-007, section 15.4.1). If a binding label was given and
5402 there is more than one argument (num_idents), it is an error. */
5404 static bool
5405 set_binding_label (const char **dest_label, const char *sym_name,
5406 int num_idents)
5408 if (num_idents > 1 && has_name_equals)
5410 gfc_error ("Multiple identifiers provided with "
5411 "single NAME= specifier at %C");
5412 return false;
5415 if (curr_binding_label)
5416 /* Binding label given; store in temp holder till have sym. */
5417 *dest_label = curr_binding_label;
5418 else
5420 /* No binding label given, and the NAME= specifier did not exist,
5421 which means there was no NAME="". */
5422 if (sym_name != NULL && has_name_equals == 0)
5423 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5426 return true;
5430 /* Set the status of the given common block as being BIND(C) or not,
5431 depending on the given parameter, is_bind_c. */
5433 void
5434 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5436 com_block->is_bind_c = is_bind_c;
5437 return;
5441 /* Verify that the given gfc_typespec is for a C interoperable type. */
5443 bool
5444 gfc_verify_c_interop (gfc_typespec *ts)
5446 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5447 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5448 ? true : false;
5449 else if (ts->type == BT_CLASS)
5450 return false;
5451 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5452 return false;
5454 return true;
5458 /* Verify that the variables of a given common block, which has been
5459 defined with the attribute specifier bind(c), to be of a C
5460 interoperable type. Errors will be reported here, if
5461 encountered. */
5463 bool
5464 verify_com_block_vars_c_interop (gfc_common_head *com_block)
5466 gfc_symbol *curr_sym = NULL;
5467 bool retval = true;
5469 curr_sym = com_block->head;
5471 /* Make sure we have at least one symbol. */
5472 if (curr_sym == NULL)
5473 return retval;
5475 /* Here we know we have a symbol, so we'll execute this loop
5476 at least once. */
5479 /* The second to last param, 1, says this is in a common block. */
5480 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5481 curr_sym = curr_sym->common_next;
5482 } while (curr_sym != NULL);
5484 return retval;
5488 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5489 an appropriate error message is reported. */
5491 bool
5492 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5493 int is_in_common, gfc_common_head *com_block)
5495 bool bind_c_function = false;
5496 bool retval = true;
5498 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5499 bind_c_function = true;
5501 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5503 tmp_sym = tmp_sym->result;
5504 /* Make sure it wasn't an implicitly typed result. */
5505 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5507 gfc_warning (OPT_Wc_binding_type,
5508 "Implicitly declared BIND(C) function %qs at "
5509 "%L may not be C interoperable", tmp_sym->name,
5510 &tmp_sym->declared_at);
5511 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5512 /* Mark it as C interoperable to prevent duplicate warnings. */
5513 tmp_sym->ts.is_c_interop = 1;
5514 tmp_sym->attr.is_c_interop = 1;
5518 /* Here, we know we have the bind(c) attribute, so if we have
5519 enough type info, then verify that it's a C interop kind.
5520 The info could be in the symbol already, or possibly still in
5521 the given ts (current_ts), so look in both. */
5522 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5524 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5526 /* See if we're dealing with a sym in a common block or not. */
5527 if (is_in_common == 1 && warn_c_binding_type)
5529 gfc_warning (OPT_Wc_binding_type,
5530 "Variable %qs in common block %qs at %L "
5531 "may not be a C interoperable "
5532 "kind though common block %qs is BIND(C)",
5533 tmp_sym->name, com_block->name,
5534 &(tmp_sym->declared_at), com_block->name);
5536 else
5538 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5539 gfc_error ("Type declaration %qs at %L is not C "
5540 "interoperable but it is BIND(C)",
5541 tmp_sym->name, &(tmp_sym->declared_at));
5542 else if (warn_c_binding_type)
5543 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5544 "may not be a C interoperable "
5545 "kind but it is BIND(C)",
5546 tmp_sym->name, &(tmp_sym->declared_at));
5550 /* Variables declared w/in a common block can't be bind(c)
5551 since there's no way for C to see these variables, so there's
5552 semantically no reason for the attribute. */
5553 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5555 gfc_error ("Variable %qs in common block %qs at "
5556 "%L cannot be declared with BIND(C) "
5557 "since it is not a global",
5558 tmp_sym->name, com_block->name,
5559 &(tmp_sym->declared_at));
5560 retval = false;
5563 /* Scalar variables that are bind(c) can not have the pointer
5564 or allocatable attributes. */
5565 if (tmp_sym->attr.is_bind_c == 1)
5567 if (tmp_sym->attr.pointer == 1)
5569 gfc_error ("Variable %qs at %L cannot have both the "
5570 "POINTER and BIND(C) attributes",
5571 tmp_sym->name, &(tmp_sym->declared_at));
5572 retval = false;
5575 if (tmp_sym->attr.allocatable == 1)
5577 gfc_error ("Variable %qs at %L cannot have both the "
5578 "ALLOCATABLE and BIND(C) attributes",
5579 tmp_sym->name, &(tmp_sym->declared_at));
5580 retval = false;
5585 /* If it is a BIND(C) function, make sure the return value is a
5586 scalar value. The previous tests in this function made sure
5587 the type is interoperable. */
5588 if (bind_c_function && tmp_sym->as != NULL)
5589 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5590 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5592 /* BIND(C) functions can not return a character string. */
5593 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5594 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5595 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5596 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5597 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5598 "be a character string", tmp_sym->name,
5599 &(tmp_sym->declared_at));
5602 /* See if the symbol has been marked as private. If it has, make sure
5603 there is no binding label and warn the user if there is one. */
5604 if (tmp_sym->attr.access == ACCESS_PRIVATE
5605 && tmp_sym->binding_label)
5606 /* Use gfc_warning_now because we won't say that the symbol fails
5607 just because of this. */
5608 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5609 "given the binding label %qs", tmp_sym->name,
5610 &(tmp_sym->declared_at), tmp_sym->binding_label);
5612 return retval;
5616 /* Set the appropriate fields for a symbol that's been declared as
5617 BIND(C) (the is_bind_c flag and the binding label), and verify that
5618 the type is C interoperable. Errors are reported by the functions
5619 used to set/test these fields. */
5621 bool
5622 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5624 bool retval = true;
5626 /* TODO: Do we need to make sure the vars aren't marked private? */
5628 /* Set the is_bind_c bit in symbol_attribute. */
5629 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5631 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5632 return false;
5634 return retval;
5638 /* Set the fields marking the given common block as BIND(C), including
5639 a binding label, and report any errors encountered. */
5641 bool
5642 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5644 bool retval = true;
5646 /* destLabel, common name, typespec (which may have binding label). */
5647 if (!set_binding_label (&com_block->binding_label, com_block->name,
5648 num_idents))
5649 return false;
5651 /* Set the given common block (com_block) to being bind(c) (1). */
5652 set_com_block_bind_c (com_block, 1);
5654 return retval;
5658 /* Retrieve the list of one or more identifiers that the given bind(c)
5659 attribute applies to. */
5661 bool
5662 get_bind_c_idents (void)
5664 char name[GFC_MAX_SYMBOL_LEN + 1];
5665 int num_idents = 0;
5666 gfc_symbol *tmp_sym = NULL;
5667 match found_id;
5668 gfc_common_head *com_block = NULL;
5670 if (gfc_match_name (name) == MATCH_YES)
5672 found_id = MATCH_YES;
5673 gfc_get_ha_symbol (name, &tmp_sym);
5675 else if (match_common_name (name) == MATCH_YES)
5677 found_id = MATCH_YES;
5678 com_block = gfc_get_common (name, 0);
5680 else
5682 gfc_error ("Need either entity or common block name for "
5683 "attribute specification statement at %C");
5684 return false;
5687 /* Save the current identifier and look for more. */
5690 /* Increment the number of identifiers found for this spec stmt. */
5691 num_idents++;
5693 /* Make sure we have a sym or com block, and verify that it can
5694 be bind(c). Set the appropriate field(s) and look for more
5695 identifiers. */
5696 if (tmp_sym != NULL || com_block != NULL)
5698 if (tmp_sym != NULL)
5700 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
5701 return false;
5703 else
5705 if (!set_verify_bind_c_com_block (com_block, num_idents))
5706 return false;
5709 /* Look to see if we have another identifier. */
5710 tmp_sym = NULL;
5711 if (gfc_match_eos () == MATCH_YES)
5712 found_id = MATCH_NO;
5713 else if (gfc_match_char (',') != MATCH_YES)
5714 found_id = MATCH_NO;
5715 else if (gfc_match_name (name) == MATCH_YES)
5717 found_id = MATCH_YES;
5718 gfc_get_ha_symbol (name, &tmp_sym);
5720 else if (match_common_name (name) == MATCH_YES)
5722 found_id = MATCH_YES;
5723 com_block = gfc_get_common (name, 0);
5725 else
5727 gfc_error ("Missing entity or common block name for "
5728 "attribute specification statement at %C");
5729 return false;
5732 else
5734 gfc_internal_error ("Missing symbol");
5736 } while (found_id == MATCH_YES);
5738 /* if we get here we were successful */
5739 return true;
5743 /* Try and match a BIND(C) attribute specification statement. */
5745 match
5746 gfc_match_bind_c_stmt (void)
5748 match found_match = MATCH_NO;
5749 gfc_typespec *ts;
5751 ts = &current_ts;
5753 /* This may not be necessary. */
5754 gfc_clear_ts (ts);
5755 /* Clear the temporary binding label holder. */
5756 curr_binding_label = NULL;
5758 /* Look for the bind(c). */
5759 found_match = gfc_match_bind_c (NULL, true);
5761 if (found_match == MATCH_YES)
5763 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
5764 return MATCH_ERROR;
5766 /* Look for the :: now, but it is not required. */
5767 gfc_match (" :: ");
5769 /* Get the identifier(s) that needs to be updated. This may need to
5770 change to hand the flag(s) for the attr specified so all identifiers
5771 found can have all appropriate parts updated (assuming that the same
5772 spec stmt can have multiple attrs, such as both bind(c) and
5773 allocatable...). */
5774 if (!get_bind_c_idents ())
5775 /* Error message should have printed already. */
5776 return MATCH_ERROR;
5779 return found_match;
5783 /* Match a data declaration statement. */
5785 match
5786 gfc_match_data_decl (void)
5788 gfc_symbol *sym;
5789 match m;
5790 int elem;
5792 type_param_spec_list = NULL;
5793 decl_type_param_list = NULL;
5795 num_idents_on_line = 0;
5797 m = gfc_match_decl_type_spec (&current_ts, 0);
5798 if (m != MATCH_YES)
5799 return m;
5801 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5802 && !gfc_comp_struct (gfc_current_state ()))
5804 sym = gfc_use_derived (current_ts.u.derived);
5806 if (sym == NULL)
5808 m = MATCH_ERROR;
5809 goto cleanup;
5812 current_ts.u.derived = sym;
5815 m = match_attr_spec ();
5816 if (m == MATCH_ERROR)
5818 m = MATCH_NO;
5819 goto cleanup;
5822 if (current_ts.type == BT_CLASS
5823 && current_ts.u.derived->attr.unlimited_polymorphic)
5824 goto ok;
5826 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5827 && current_ts.u.derived->components == NULL
5828 && !current_ts.u.derived->attr.zero_comp)
5831 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
5832 goto ok;
5834 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED
5835 && current_ts.u.derived == gfc_current_block ())
5836 goto ok;
5838 gfc_find_symbol (current_ts.u.derived->name,
5839 current_ts.u.derived->ns, 1, &sym);
5841 /* Any symbol that we find had better be a type definition
5842 which has its components defined, or be a structure definition
5843 actively being parsed. */
5844 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
5845 && (current_ts.u.derived->components != NULL
5846 || current_ts.u.derived->attr.zero_comp
5847 || current_ts.u.derived == gfc_new_block))
5848 goto ok;
5850 gfc_error ("Derived type at %C has not been previously defined "
5851 "and so cannot appear in a derived type definition");
5852 m = MATCH_ERROR;
5853 goto cleanup;
5857 /* If we have an old-style character declaration, and no new-style
5858 attribute specifications, then there a comma is optional between
5859 the type specification and the variable list. */
5860 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
5861 gfc_match_char (',');
5863 /* Give the types/attributes to symbols that follow. Give the element
5864 a number so that repeat character length expressions can be copied. */
5865 elem = 1;
5866 for (;;)
5868 num_idents_on_line++;
5869 m = variable_decl (elem++);
5870 if (m == MATCH_ERROR)
5871 goto cleanup;
5872 if (m == MATCH_NO)
5873 break;
5875 if (gfc_match_eos () == MATCH_YES)
5876 goto cleanup;
5877 if (gfc_match_char (',') != MATCH_YES)
5878 break;
5881 if (!gfc_error_flag_test ())
5883 /* An anonymous structure declaration is unambiguous; if we matched one
5884 according to gfc_match_structure_decl, we need to return MATCH_YES
5885 here to avoid confusing the remaining matchers, even if there was an
5886 error during variable_decl. We must flush any such errors. Note this
5887 causes the parser to gracefully continue parsing the remaining input
5888 as a structure body, which likely follows. */
5889 if (current_ts.type == BT_DERIVED && current_ts.u.derived
5890 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
5892 gfc_error_now ("Syntax error in anonymous structure declaration"
5893 " at %C");
5894 /* Skip the bad variable_decl and line up for the start of the
5895 structure body. */
5896 gfc_error_recovery ();
5897 m = MATCH_YES;
5898 goto cleanup;
5901 gfc_error ("Syntax error in data declaration at %C");
5904 m = MATCH_ERROR;
5906 gfc_free_data_all (gfc_current_ns);
5908 cleanup:
5909 if (saved_kind_expr)
5910 gfc_free_expr (saved_kind_expr);
5911 if (type_param_spec_list)
5912 gfc_free_actual_arglist (type_param_spec_list);
5913 if (decl_type_param_list)
5914 gfc_free_actual_arglist (decl_type_param_list);
5915 saved_kind_expr = NULL;
5916 gfc_free_array_spec (current_as);
5917 current_as = NULL;
5918 return m;
5922 /* Match a prefix associated with a function or subroutine
5923 declaration. If the typespec pointer is nonnull, then a typespec
5924 can be matched. Note that if nothing matches, MATCH_YES is
5925 returned (the null string was matched). */
5927 match
5928 gfc_match_prefix (gfc_typespec *ts)
5930 bool seen_type;
5931 bool seen_impure;
5932 bool found_prefix;
5934 gfc_clear_attr (&current_attr);
5935 seen_type = false;
5936 seen_impure = false;
5938 gcc_assert (!gfc_matching_prefix);
5939 gfc_matching_prefix = true;
5943 found_prefix = false;
5945 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
5946 corresponding attribute seems natural and distinguishes these
5947 procedures from procedure types of PROC_MODULE, which these are
5948 as well. */
5949 if (gfc_match ("module% ") == MATCH_YES)
5951 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
5952 goto error;
5954 current_attr.module_procedure = 1;
5955 found_prefix = true;
5958 if (!seen_type && ts != NULL
5959 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
5960 && gfc_match_space () == MATCH_YES)
5963 seen_type = true;
5964 found_prefix = true;
5967 if (gfc_match ("elemental% ") == MATCH_YES)
5969 if (!gfc_add_elemental (&current_attr, NULL))
5970 goto error;
5972 found_prefix = true;
5975 if (gfc_match ("pure% ") == MATCH_YES)
5977 if (!gfc_add_pure (&current_attr, NULL))
5978 goto error;
5980 found_prefix = true;
5983 if (gfc_match ("recursive% ") == MATCH_YES)
5985 if (!gfc_add_recursive (&current_attr, NULL))
5986 goto error;
5988 found_prefix = true;
5991 /* IMPURE is a somewhat special case, as it needs not set an actual
5992 attribute but rather only prevents ELEMENTAL routines from being
5993 automatically PURE. */
5994 if (gfc_match ("impure% ") == MATCH_YES)
5996 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
5997 goto error;
5999 seen_impure = true;
6000 found_prefix = true;
6003 while (found_prefix);
6005 /* IMPURE and PURE must not both appear, of course. */
6006 if (seen_impure && current_attr.pure)
6008 gfc_error ("PURE and IMPURE must not appear both at %C");
6009 goto error;
6012 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6013 if (!seen_impure && current_attr.elemental && !current_attr.pure)
6015 if (!gfc_add_pure (&current_attr, NULL))
6016 goto error;
6019 /* At this point, the next item is not a prefix. */
6020 gcc_assert (gfc_matching_prefix);
6022 gfc_matching_prefix = false;
6023 return MATCH_YES;
6025 error:
6026 gcc_assert (gfc_matching_prefix);
6027 gfc_matching_prefix = false;
6028 return MATCH_ERROR;
6032 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6034 static bool
6035 copy_prefix (symbol_attribute *dest, locus *where)
6037 if (dest->module_procedure)
6039 if (current_attr.elemental)
6040 dest->elemental = 1;
6042 if (current_attr.pure)
6043 dest->pure = 1;
6045 if (current_attr.recursive)
6046 dest->recursive = 1;
6048 /* Module procedures are unusual in that the 'dest' is copied from
6049 the interface declaration. However, this is an oportunity to
6050 check that the submodule declaration is compliant with the
6051 interface. */
6052 if (dest->elemental && !current_attr.elemental)
6054 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6055 "missing at %L", where);
6056 return false;
6059 if (dest->pure && !current_attr.pure)
6061 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6062 "missing at %L", where);
6063 return false;
6066 if (dest->recursive && !current_attr.recursive)
6068 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6069 "missing at %L", where);
6070 return false;
6073 return true;
6076 if (current_attr.elemental && !gfc_add_elemental (dest, where))
6077 return false;
6079 if (current_attr.pure && !gfc_add_pure (dest, where))
6080 return false;
6082 if (current_attr.recursive && !gfc_add_recursive (dest, where))
6083 return false;
6085 return true;
6089 /* Match a formal argument list or, if typeparam is true, a
6090 type_param_name_list. */
6092 match
6093 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6094 int null_flag, bool typeparam)
6096 gfc_formal_arglist *head, *tail, *p, *q;
6097 char name[GFC_MAX_SYMBOL_LEN + 1];
6098 gfc_symbol *sym;
6099 match m;
6100 gfc_formal_arglist *formal = NULL;
6102 head = tail = NULL;
6104 /* Keep the interface formal argument list and null it so that the
6105 matching for the new declaration can be done. The numbers and
6106 names of the arguments are checked here. The interface formal
6107 arguments are retained in formal_arglist and the characteristics
6108 are compared in resolve.c(resolve_fl_procedure). See the remark
6109 in get_proc_name about the eventual need to copy the formal_arglist
6110 and populate the formal namespace of the interface symbol. */
6111 if (progname->attr.module_procedure
6112 && progname->attr.host_assoc)
6114 formal = progname->formal;
6115 progname->formal = NULL;
6118 if (gfc_match_char ('(') != MATCH_YES)
6120 if (null_flag)
6121 goto ok;
6122 return MATCH_NO;
6125 if (gfc_match_char (')') == MATCH_YES)
6126 goto ok;
6128 for (;;)
6130 if (gfc_match_char ('*') == MATCH_YES)
6132 sym = NULL;
6133 if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6134 "Alternate-return argument at %C"))
6136 m = MATCH_ERROR;
6137 goto cleanup;
6139 else if (typeparam)
6140 gfc_error_now ("A parameter name is required at %C");
6142 else
6144 m = gfc_match_name (name);
6145 if (m != MATCH_YES)
6147 if(typeparam)
6148 gfc_error_now ("A parameter name is required at %C");
6149 goto cleanup;
6152 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6153 goto cleanup;
6154 else if (typeparam
6155 && gfc_get_symbol (name, progname->f2k_derived, &sym))
6156 goto cleanup;
6159 p = gfc_get_formal_arglist ();
6161 if (head == NULL)
6162 head = tail = p;
6163 else
6165 tail->next = p;
6166 tail = p;
6169 tail->sym = sym;
6171 /* We don't add the VARIABLE flavor because the name could be a
6172 dummy procedure. We don't apply these attributes to formal
6173 arguments of statement functions. */
6174 if (sym != NULL && !st_flag
6175 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6176 || !gfc_missing_attr (&sym->attr, NULL)))
6178 m = MATCH_ERROR;
6179 goto cleanup;
6182 /* The name of a program unit can be in a different namespace,
6183 so check for it explicitly. After the statement is accepted,
6184 the name is checked for especially in gfc_get_symbol(). */
6185 if (gfc_new_block != NULL && sym != NULL && !typeparam
6186 && strcmp (sym->name, gfc_new_block->name) == 0)
6188 gfc_error ("Name %qs at %C is the name of the procedure",
6189 sym->name);
6190 m = MATCH_ERROR;
6191 goto cleanup;
6194 if (gfc_match_char (')') == MATCH_YES)
6195 goto ok;
6197 m = gfc_match_char (',');
6198 if (m != MATCH_YES)
6200 if (typeparam)
6201 gfc_error_now ("Expected parameter list in type declaration "
6202 "at %C");
6203 else
6204 gfc_error ("Unexpected junk in formal argument list at %C");
6205 goto cleanup;
6210 /* Check for duplicate symbols in the formal argument list. */
6211 if (head != NULL)
6213 for (p = head; p->next; p = p->next)
6215 if (p->sym == NULL)
6216 continue;
6218 for (q = p->next; q; q = q->next)
6219 if (p->sym == q->sym)
6221 if (typeparam)
6222 gfc_error_now ("Duplicate name %qs in parameter "
6223 "list at %C", p->sym->name);
6224 else
6225 gfc_error ("Duplicate symbol %qs in formal argument "
6226 "list at %C", p->sym->name);
6228 m = MATCH_ERROR;
6229 goto cleanup;
6234 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6236 m = MATCH_ERROR;
6237 goto cleanup;
6240 /* gfc_error_now used in following and return with MATCH_YES because
6241 doing otherwise results in a cascade of extraneous errors and in
6242 some cases an ICE in symbol.c(gfc_release_symbol). */
6243 if (progname->attr.module_procedure && progname->attr.host_assoc)
6245 bool arg_count_mismatch = false;
6247 if (!formal && head)
6248 arg_count_mismatch = true;
6250 /* Abbreviated module procedure declaration is not meant to have any
6251 formal arguments! */
6252 if (!progname->abr_modproc_decl && formal && !head)
6253 arg_count_mismatch = true;
6255 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6257 if ((p->next != NULL && q->next == NULL)
6258 || (p->next == NULL && q->next != NULL))
6259 arg_count_mismatch = true;
6260 else if ((p->sym == NULL && q->sym == NULL)
6261 || strcmp (p->sym->name, q->sym->name) == 0)
6262 continue;
6263 else
6264 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6265 "argument names (%s/%s) at %C",
6266 p->sym->name, q->sym->name);
6269 if (arg_count_mismatch)
6270 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6271 "formal arguments at %C");
6274 return MATCH_YES;
6276 cleanup:
6277 gfc_free_formal_arglist (head);
6278 return m;
6282 /* Match a RESULT specification following a function declaration or
6283 ENTRY statement. Also matches the end-of-statement. */
6285 static match
6286 match_result (gfc_symbol *function, gfc_symbol **result)
6288 char name[GFC_MAX_SYMBOL_LEN + 1];
6289 gfc_symbol *r;
6290 match m;
6292 if (gfc_match (" result (") != MATCH_YES)
6293 return MATCH_NO;
6295 m = gfc_match_name (name);
6296 if (m != MATCH_YES)
6297 return m;
6299 /* Get the right paren, and that's it because there could be the
6300 bind(c) attribute after the result clause. */
6301 if (gfc_match_char (')') != MATCH_YES)
6303 /* TODO: should report the missing right paren here. */
6304 return MATCH_ERROR;
6307 if (strcmp (function->name, name) == 0)
6309 gfc_error ("RESULT variable at %C must be different than function name");
6310 return MATCH_ERROR;
6313 if (gfc_get_symbol (name, NULL, &r))
6314 return MATCH_ERROR;
6316 if (!gfc_add_result (&r->attr, r->name, NULL))
6317 return MATCH_ERROR;
6319 *result = r;
6321 return MATCH_YES;
6325 /* Match a function suffix, which could be a combination of a result
6326 clause and BIND(C), either one, or neither. The draft does not
6327 require them to come in a specific order. */
6329 match
6330 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6332 match is_bind_c; /* Found bind(c). */
6333 match is_result; /* Found result clause. */
6334 match found_match; /* Status of whether we've found a good match. */
6335 char peek_char; /* Character we're going to peek at. */
6336 bool allow_binding_name;
6338 /* Initialize to having found nothing. */
6339 found_match = MATCH_NO;
6340 is_bind_c = MATCH_NO;
6341 is_result = MATCH_NO;
6343 /* Get the next char to narrow between result and bind(c). */
6344 gfc_gobble_whitespace ();
6345 peek_char = gfc_peek_ascii_char ();
6347 /* C binding names are not allowed for internal procedures. */
6348 if (gfc_current_state () == COMP_CONTAINS
6349 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6350 allow_binding_name = false;
6351 else
6352 allow_binding_name = true;
6354 switch (peek_char)
6356 case 'r':
6357 /* Look for result clause. */
6358 is_result = match_result (sym, result);
6359 if (is_result == MATCH_YES)
6361 /* Now see if there is a bind(c) after it. */
6362 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6363 /* We've found the result clause and possibly bind(c). */
6364 found_match = MATCH_YES;
6366 else
6367 /* This should only be MATCH_ERROR. */
6368 found_match = is_result;
6369 break;
6370 case 'b':
6371 /* Look for bind(c) first. */
6372 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6373 if (is_bind_c == MATCH_YES)
6375 /* Now see if a result clause followed it. */
6376 is_result = match_result (sym, result);
6377 found_match = MATCH_YES;
6379 else
6381 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6382 found_match = MATCH_ERROR;
6384 break;
6385 default:
6386 gfc_error ("Unexpected junk after function declaration at %C");
6387 found_match = MATCH_ERROR;
6388 break;
6391 if (is_bind_c == MATCH_YES)
6393 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
6394 if (gfc_current_state () == COMP_CONTAINS
6395 && sym->ns->proc_name->attr.flavor != FL_MODULE
6396 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6397 "at %L may not be specified for an internal "
6398 "procedure", &gfc_current_locus))
6399 return MATCH_ERROR;
6401 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6402 return MATCH_ERROR;
6405 return found_match;
6409 /* Procedure pointer return value without RESULT statement:
6410 Add "hidden" result variable named "ppr@". */
6412 static bool
6413 add_hidden_procptr_result (gfc_symbol *sym)
6415 bool case1,case2;
6417 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6418 return false;
6420 /* First usage case: PROCEDURE and EXTERNAL statements. */
6421 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6422 && strcmp (gfc_current_block ()->name, sym->name) == 0
6423 && sym->attr.external;
6424 /* Second usage case: INTERFACE statements. */
6425 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6426 && gfc_state_stack->previous->state == COMP_FUNCTION
6427 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6429 if (case1 || case2)
6431 gfc_symtree *stree;
6432 if (case1)
6433 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6434 else if (case2)
6436 gfc_symtree *st2;
6437 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6438 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6439 st2->n.sym = stree->n.sym;
6440 stree->n.sym->refs++;
6442 sym->result = stree->n.sym;
6444 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6445 sym->result->attr.pointer = sym->attr.pointer;
6446 sym->result->attr.external = sym->attr.external;
6447 sym->result->attr.referenced = sym->attr.referenced;
6448 sym->result->ts = sym->ts;
6449 sym->attr.proc_pointer = 0;
6450 sym->attr.pointer = 0;
6451 sym->attr.external = 0;
6452 if (sym->result->attr.external && sym->result->attr.pointer)
6454 sym->result->attr.pointer = 0;
6455 sym->result->attr.proc_pointer = 1;
6458 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6460 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6461 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6462 && sym->result && sym->result != sym && sym->result->attr.external
6463 && sym == gfc_current_ns->proc_name
6464 && sym == sym->result->ns->proc_name
6465 && strcmp ("ppr@", sym->result->name) == 0)
6467 sym->result->attr.proc_pointer = 1;
6468 sym->attr.pointer = 0;
6469 return true;
6471 else
6472 return false;
6476 /* Match the interface for a PROCEDURE declaration,
6477 including brackets (R1212). */
6479 static match
6480 match_procedure_interface (gfc_symbol **proc_if)
6482 match m;
6483 gfc_symtree *st;
6484 locus old_loc, entry_loc;
6485 gfc_namespace *old_ns = gfc_current_ns;
6486 char name[GFC_MAX_SYMBOL_LEN + 1];
6488 old_loc = entry_loc = gfc_current_locus;
6489 gfc_clear_ts (&current_ts);
6491 if (gfc_match (" (") != MATCH_YES)
6493 gfc_current_locus = entry_loc;
6494 return MATCH_NO;
6497 /* Get the type spec. for the procedure interface. */
6498 old_loc = gfc_current_locus;
6499 m = gfc_match_decl_type_spec (&current_ts, 0);
6500 gfc_gobble_whitespace ();
6501 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6502 goto got_ts;
6504 if (m == MATCH_ERROR)
6505 return m;
6507 /* Procedure interface is itself a procedure. */
6508 gfc_current_locus = old_loc;
6509 m = gfc_match_name (name);
6511 /* First look to see if it is already accessible in the current
6512 namespace because it is use associated or contained. */
6513 st = NULL;
6514 if (gfc_find_sym_tree (name, NULL, 0, &st))
6515 return MATCH_ERROR;
6517 /* If it is still not found, then try the parent namespace, if it
6518 exists and create the symbol there if it is still not found. */
6519 if (gfc_current_ns->parent)
6520 gfc_current_ns = gfc_current_ns->parent;
6521 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6522 return MATCH_ERROR;
6524 gfc_current_ns = old_ns;
6525 *proc_if = st->n.sym;
6527 if (*proc_if)
6529 (*proc_if)->refs++;
6530 /* Resolve interface if possible. That way, attr.procedure is only set
6531 if it is declared by a later procedure-declaration-stmt, which is
6532 invalid per F08:C1216 (cf. resolve_procedure_interface). */
6533 while ((*proc_if)->ts.interface
6534 && *proc_if != (*proc_if)->ts.interface)
6535 *proc_if = (*proc_if)->ts.interface;
6537 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6538 && (*proc_if)->ts.type == BT_UNKNOWN
6539 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6540 (*proc_if)->name, NULL))
6541 return MATCH_ERROR;
6544 got_ts:
6545 if (gfc_match (" )") != MATCH_YES)
6547 gfc_current_locus = entry_loc;
6548 return MATCH_NO;
6551 return MATCH_YES;
6555 /* Match a PROCEDURE declaration (R1211). */
6557 static match
6558 match_procedure_decl (void)
6560 match m;
6561 gfc_symbol *sym, *proc_if = NULL;
6562 int num;
6563 gfc_expr *initializer = NULL;
6565 /* Parse interface (with brackets). */
6566 m = match_procedure_interface (&proc_if);
6567 if (m != MATCH_YES)
6568 return m;
6570 /* Parse attributes (with colons). */
6571 m = match_attr_spec();
6572 if (m == MATCH_ERROR)
6573 return MATCH_ERROR;
6575 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6577 current_attr.is_bind_c = 1;
6578 has_name_equals = 0;
6579 curr_binding_label = NULL;
6582 /* Get procedure symbols. */
6583 for(num=1;;num++)
6585 m = gfc_match_symbol (&sym, 0);
6586 if (m == MATCH_NO)
6587 goto syntax;
6588 else if (m == MATCH_ERROR)
6589 return m;
6591 /* Add current_attr to the symbol attributes. */
6592 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
6593 return MATCH_ERROR;
6595 if (sym->attr.is_bind_c)
6597 /* Check for C1218. */
6598 if (!proc_if || !proc_if->attr.is_bind_c)
6600 gfc_error ("BIND(C) attribute at %C requires "
6601 "an interface with BIND(C)");
6602 return MATCH_ERROR;
6604 /* Check for C1217. */
6605 if (has_name_equals && sym->attr.pointer)
6607 gfc_error ("BIND(C) procedure with NAME may not have "
6608 "POINTER attribute at %C");
6609 return MATCH_ERROR;
6611 if (has_name_equals && sym->attr.dummy)
6613 gfc_error ("Dummy procedure at %C may not have "
6614 "BIND(C) attribute with NAME");
6615 return MATCH_ERROR;
6617 /* Set binding label for BIND(C). */
6618 if (!set_binding_label (&sym->binding_label, sym->name, num))
6619 return MATCH_ERROR;
6622 if (!gfc_add_external (&sym->attr, NULL))
6623 return MATCH_ERROR;
6625 if (add_hidden_procptr_result (sym))
6626 sym = sym->result;
6628 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
6629 return MATCH_ERROR;
6631 /* Set interface. */
6632 if (proc_if != NULL)
6634 if (sym->ts.type != BT_UNKNOWN)
6636 gfc_error ("Procedure %qs at %L already has basic type of %s",
6637 sym->name, &gfc_current_locus,
6638 gfc_basic_typename (sym->ts.type));
6639 return MATCH_ERROR;
6641 sym->ts.interface = proc_if;
6642 sym->attr.untyped = 1;
6643 sym->attr.if_source = IFSRC_IFBODY;
6645 else if (current_ts.type != BT_UNKNOWN)
6647 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
6648 return MATCH_ERROR;
6649 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6650 sym->ts.interface->ts = current_ts;
6651 sym->ts.interface->attr.flavor = FL_PROCEDURE;
6652 sym->ts.interface->attr.function = 1;
6653 sym->attr.function = 1;
6654 sym->attr.if_source = IFSRC_UNKNOWN;
6657 if (gfc_match (" =>") == MATCH_YES)
6659 if (!current_attr.pointer)
6661 gfc_error ("Initialization at %C isn't for a pointer variable");
6662 m = MATCH_ERROR;
6663 goto cleanup;
6666 m = match_pointer_init (&initializer, 1);
6667 if (m != MATCH_YES)
6668 goto cleanup;
6670 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
6671 goto cleanup;
6675 if (gfc_match_eos () == MATCH_YES)
6676 return MATCH_YES;
6677 if (gfc_match_char (',') != MATCH_YES)
6678 goto syntax;
6681 syntax:
6682 gfc_error ("Syntax error in PROCEDURE statement at %C");
6683 return MATCH_ERROR;
6685 cleanup:
6686 /* Free stuff up and return. */
6687 gfc_free_expr (initializer);
6688 return m;
6692 static match
6693 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
6696 /* Match a procedure pointer component declaration (R445). */
6698 static match
6699 match_ppc_decl (void)
6701 match m;
6702 gfc_symbol *proc_if = NULL;
6703 gfc_typespec ts;
6704 int num;
6705 gfc_component *c;
6706 gfc_expr *initializer = NULL;
6707 gfc_typebound_proc* tb;
6708 char name[GFC_MAX_SYMBOL_LEN + 1];
6710 /* Parse interface (with brackets). */
6711 m = match_procedure_interface (&proc_if);
6712 if (m != MATCH_YES)
6713 goto syntax;
6715 /* Parse attributes. */
6716 tb = XCNEW (gfc_typebound_proc);
6717 tb->where = gfc_current_locus;
6718 m = match_binding_attributes (tb, false, true);
6719 if (m == MATCH_ERROR)
6720 return m;
6722 gfc_clear_attr (&current_attr);
6723 current_attr.procedure = 1;
6724 current_attr.proc_pointer = 1;
6725 current_attr.access = tb->access;
6726 current_attr.flavor = FL_PROCEDURE;
6728 /* Match the colons (required). */
6729 if (gfc_match (" ::") != MATCH_YES)
6731 gfc_error ("Expected %<::%> after binding-attributes at %C");
6732 return MATCH_ERROR;
6735 /* Check for C450. */
6736 if (!tb->nopass && proc_if == NULL)
6738 gfc_error("NOPASS or explicit interface required at %C");
6739 return MATCH_ERROR;
6742 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
6743 return MATCH_ERROR;
6745 /* Match PPC names. */
6746 ts = current_ts;
6747 for(num=1;;num++)
6749 m = gfc_match_name (name);
6750 if (m == MATCH_NO)
6751 goto syntax;
6752 else if (m == MATCH_ERROR)
6753 return m;
6755 if (!gfc_add_component (gfc_current_block(), name, &c))
6756 return MATCH_ERROR;
6758 /* Add current_attr to the symbol attributes. */
6759 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
6760 return MATCH_ERROR;
6762 if (!gfc_add_external (&c->attr, NULL))
6763 return MATCH_ERROR;
6765 if (!gfc_add_proc (&c->attr, name, NULL))
6766 return MATCH_ERROR;
6768 if (num == 1)
6769 c->tb = tb;
6770 else
6772 c->tb = XCNEW (gfc_typebound_proc);
6773 c->tb->where = gfc_current_locus;
6774 *c->tb = *tb;
6777 /* Set interface. */
6778 if (proc_if != NULL)
6780 c->ts.interface = proc_if;
6781 c->attr.untyped = 1;
6782 c->attr.if_source = IFSRC_IFBODY;
6784 else if (ts.type != BT_UNKNOWN)
6786 c->ts = ts;
6787 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6788 c->ts.interface->result = c->ts.interface;
6789 c->ts.interface->ts = ts;
6790 c->ts.interface->attr.flavor = FL_PROCEDURE;
6791 c->ts.interface->attr.function = 1;
6792 c->attr.function = 1;
6793 c->attr.if_source = IFSRC_UNKNOWN;
6796 if (gfc_match (" =>") == MATCH_YES)
6798 m = match_pointer_init (&initializer, 1);
6799 if (m != MATCH_YES)
6801 gfc_free_expr (initializer);
6802 return m;
6804 c->initializer = initializer;
6807 if (gfc_match_eos () == MATCH_YES)
6808 return MATCH_YES;
6809 if (gfc_match_char (',') != MATCH_YES)
6810 goto syntax;
6813 syntax:
6814 gfc_error ("Syntax error in procedure pointer component at %C");
6815 return MATCH_ERROR;
6819 /* Match a PROCEDURE declaration inside an interface (R1206). */
6821 static match
6822 match_procedure_in_interface (void)
6824 match m;
6825 gfc_symbol *sym;
6826 char name[GFC_MAX_SYMBOL_LEN + 1];
6827 locus old_locus;
6829 if (current_interface.type == INTERFACE_NAMELESS
6830 || current_interface.type == INTERFACE_ABSTRACT)
6832 gfc_error ("PROCEDURE at %C must be in a generic interface");
6833 return MATCH_ERROR;
6836 /* Check if the F2008 optional double colon appears. */
6837 gfc_gobble_whitespace ();
6838 old_locus = gfc_current_locus;
6839 if (gfc_match ("::") == MATCH_YES)
6841 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
6842 "MODULE PROCEDURE statement at %L", &old_locus))
6843 return MATCH_ERROR;
6845 else
6846 gfc_current_locus = old_locus;
6848 for(;;)
6850 m = gfc_match_name (name);
6851 if (m == MATCH_NO)
6852 goto syntax;
6853 else if (m == MATCH_ERROR)
6854 return m;
6855 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
6856 return MATCH_ERROR;
6858 if (!gfc_add_interface (sym))
6859 return MATCH_ERROR;
6861 if (gfc_match_eos () == MATCH_YES)
6862 break;
6863 if (gfc_match_char (',') != MATCH_YES)
6864 goto syntax;
6867 return MATCH_YES;
6869 syntax:
6870 gfc_error ("Syntax error in PROCEDURE statement at %C");
6871 return MATCH_ERROR;
6875 /* General matcher for PROCEDURE declarations. */
6877 static match match_procedure_in_type (void);
6879 match
6880 gfc_match_procedure (void)
6882 match m;
6884 switch (gfc_current_state ())
6886 case COMP_NONE:
6887 case COMP_PROGRAM:
6888 case COMP_MODULE:
6889 case COMP_SUBMODULE:
6890 case COMP_SUBROUTINE:
6891 case COMP_FUNCTION:
6892 case COMP_BLOCK:
6893 m = match_procedure_decl ();
6894 break;
6895 case COMP_INTERFACE:
6896 m = match_procedure_in_interface ();
6897 break;
6898 case COMP_DERIVED:
6899 m = match_ppc_decl ();
6900 break;
6901 case COMP_DERIVED_CONTAINS:
6902 m = match_procedure_in_type ();
6903 break;
6904 default:
6905 return MATCH_NO;
6908 if (m != MATCH_YES)
6909 return m;
6911 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
6912 return MATCH_ERROR;
6914 return m;
6918 /* Warn if a matched procedure has the same name as an intrinsic; this is
6919 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
6920 parser-state-stack to find out whether we're in a module. */
6922 static void
6923 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
6925 bool in_module;
6927 in_module = (gfc_state_stack->previous
6928 && (gfc_state_stack->previous->state == COMP_MODULE
6929 || gfc_state_stack->previous->state == COMP_SUBMODULE));
6931 gfc_warn_intrinsic_shadow (sym, in_module, func);
6935 /* Match a function declaration. */
6937 match
6938 gfc_match_function_decl (void)
6940 char name[GFC_MAX_SYMBOL_LEN + 1];
6941 gfc_symbol *sym, *result;
6942 locus old_loc;
6943 match m;
6944 match suffix_match;
6945 match found_match; /* Status returned by match func. */
6947 if (gfc_current_state () != COMP_NONE
6948 && gfc_current_state () != COMP_INTERFACE
6949 && gfc_current_state () != COMP_CONTAINS)
6950 return MATCH_NO;
6952 gfc_clear_ts (&current_ts);
6954 old_loc = gfc_current_locus;
6956 m = gfc_match_prefix (&current_ts);
6957 if (m != MATCH_YES)
6959 gfc_current_locus = old_loc;
6960 return m;
6963 if (gfc_match ("function% %n", name) != MATCH_YES)
6965 gfc_current_locus = old_loc;
6966 return MATCH_NO;
6969 if (get_proc_name (name, &sym, false))
6970 return MATCH_ERROR;
6972 if (add_hidden_procptr_result (sym))
6973 sym = sym->result;
6975 if (current_attr.module_procedure)
6976 sym->attr.module_procedure = 1;
6978 gfc_new_block = sym;
6980 m = gfc_match_formal_arglist (sym, 0, 0);
6981 if (m == MATCH_NO)
6983 gfc_error ("Expected formal argument list in function "
6984 "definition at %C");
6985 m = MATCH_ERROR;
6986 goto cleanup;
6988 else if (m == MATCH_ERROR)
6989 goto cleanup;
6991 result = NULL;
6993 /* According to the draft, the bind(c) and result clause can
6994 come in either order after the formal_arg_list (i.e., either
6995 can be first, both can exist together or by themselves or neither
6996 one). Therefore, the match_result can't match the end of the
6997 string, and check for the bind(c) or result clause in either order. */
6998 found_match = gfc_match_eos ();
7000 /* Make sure that it isn't already declared as BIND(C). If it is, it
7001 must have been marked BIND(C) with a BIND(C) attribute and that is
7002 not allowed for procedures. */
7003 if (sym->attr.is_bind_c == 1)
7005 sym->attr.is_bind_c = 0;
7006 if (sym->old_symbol != NULL)
7007 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7008 "variables or common blocks",
7009 &(sym->old_symbol->declared_at));
7010 else
7011 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7012 "variables or common blocks", &gfc_current_locus);
7015 if (found_match != MATCH_YES)
7017 /* If we haven't found the end-of-statement, look for a suffix. */
7018 suffix_match = gfc_match_suffix (sym, &result);
7019 if (suffix_match == MATCH_YES)
7020 /* Need to get the eos now. */
7021 found_match = gfc_match_eos ();
7022 else
7023 found_match = suffix_match;
7026 if(found_match != MATCH_YES)
7027 m = MATCH_ERROR;
7028 else
7030 /* Make changes to the symbol. */
7031 m = MATCH_ERROR;
7033 if (!gfc_add_function (&sym->attr, sym->name, NULL))
7034 goto cleanup;
7036 if (!gfc_missing_attr (&sym->attr, NULL))
7037 goto cleanup;
7039 if (!copy_prefix (&sym->attr, &sym->declared_at))
7041 if(!sym->attr.module_procedure)
7042 goto cleanup;
7043 else
7044 gfc_error_check ();
7047 /* Delay matching the function characteristics until after the
7048 specification block by signalling kind=-1. */
7049 sym->declared_at = old_loc;
7050 if (current_ts.type != BT_UNKNOWN)
7051 current_ts.kind = -1;
7052 else
7053 current_ts.kind = 0;
7055 if (result == NULL)
7057 if (current_ts.type != BT_UNKNOWN
7058 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
7059 goto cleanup;
7060 sym->result = sym;
7062 else
7064 if (current_ts.type != BT_UNKNOWN
7065 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
7066 goto cleanup;
7067 sym->result = result;
7070 /* Warn if this procedure has the same name as an intrinsic. */
7071 do_warn_intrinsic_shadow (sym, true);
7073 return MATCH_YES;
7076 cleanup:
7077 gfc_current_locus = old_loc;
7078 return m;
7082 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
7083 pass the name of the entry, rather than the gfc_current_block name, and
7084 to return false upon finding an existing global entry. */
7086 static bool
7087 add_global_entry (const char *name, const char *binding_label, bool sub,
7088 locus *where)
7090 gfc_gsymbol *s;
7091 enum gfc_symbol_type type;
7093 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7095 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7096 name is a global identifier. */
7097 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
7099 s = gfc_get_gsymbol (name);
7101 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7103 gfc_global_used (s, where);
7104 return false;
7106 else
7108 s->type = type;
7109 s->sym_name = name;
7110 s->where = *where;
7111 s->defined = 1;
7112 s->ns = gfc_current_ns;
7116 /* Don't add the symbol multiple times. */
7117 if (binding_label
7118 && (!gfc_notification_std (GFC_STD_F2008)
7119 || strcmp (name, binding_label) != 0))
7121 s = gfc_get_gsymbol (binding_label);
7123 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7125 gfc_global_used (s, where);
7126 return false;
7128 else
7130 s->type = type;
7131 s->sym_name = name;
7132 s->binding_label = binding_label;
7133 s->where = *where;
7134 s->defined = 1;
7135 s->ns = gfc_current_ns;
7139 return true;
7143 /* Match an ENTRY statement. */
7145 match
7146 gfc_match_entry (void)
7148 gfc_symbol *proc;
7149 gfc_symbol *result;
7150 gfc_symbol *entry;
7151 char name[GFC_MAX_SYMBOL_LEN + 1];
7152 gfc_compile_state state;
7153 match m;
7154 gfc_entry_list *el;
7155 locus old_loc;
7156 bool module_procedure;
7157 char peek_char;
7158 match is_bind_c;
7160 m = gfc_match_name (name);
7161 if (m != MATCH_YES)
7162 return m;
7164 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
7165 return MATCH_ERROR;
7167 state = gfc_current_state ();
7168 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
7170 switch (state)
7172 case COMP_PROGRAM:
7173 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7174 break;
7175 case COMP_MODULE:
7176 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7177 break;
7178 case COMP_SUBMODULE:
7179 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7180 break;
7181 case COMP_BLOCK_DATA:
7182 gfc_error ("ENTRY statement at %C cannot appear within "
7183 "a BLOCK DATA");
7184 break;
7185 case COMP_INTERFACE:
7186 gfc_error ("ENTRY statement at %C cannot appear within "
7187 "an INTERFACE");
7188 break;
7189 case COMP_STRUCTURE:
7190 gfc_error ("ENTRY statement at %C cannot appear within "
7191 "a STRUCTURE block");
7192 break;
7193 case COMP_DERIVED:
7194 gfc_error ("ENTRY statement at %C cannot appear within "
7195 "a DERIVED TYPE block");
7196 break;
7197 case COMP_IF:
7198 gfc_error ("ENTRY statement at %C cannot appear within "
7199 "an IF-THEN block");
7200 break;
7201 case COMP_DO:
7202 case COMP_DO_CONCURRENT:
7203 gfc_error ("ENTRY statement at %C cannot appear within "
7204 "a DO block");
7205 break;
7206 case COMP_SELECT:
7207 gfc_error ("ENTRY statement at %C cannot appear within "
7208 "a SELECT block");
7209 break;
7210 case COMP_FORALL:
7211 gfc_error ("ENTRY statement at %C cannot appear within "
7212 "a FORALL block");
7213 break;
7214 case COMP_WHERE:
7215 gfc_error ("ENTRY statement at %C cannot appear within "
7216 "a WHERE block");
7217 break;
7218 case COMP_CONTAINS:
7219 gfc_error ("ENTRY statement at %C cannot appear within "
7220 "a contained subprogram");
7221 break;
7222 default:
7223 gfc_error ("Unexpected ENTRY statement at %C");
7225 return MATCH_ERROR;
7228 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7229 && gfc_state_stack->previous->state == COMP_INTERFACE)
7231 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7232 return MATCH_ERROR;
7235 module_procedure = gfc_current_ns->parent != NULL
7236 && gfc_current_ns->parent->proc_name
7237 && gfc_current_ns->parent->proc_name->attr.flavor
7238 == FL_MODULE;
7240 if (gfc_current_ns->parent != NULL
7241 && gfc_current_ns->parent->proc_name
7242 && !module_procedure)
7244 gfc_error("ENTRY statement at %C cannot appear in a "
7245 "contained procedure");
7246 return MATCH_ERROR;
7249 /* Module function entries need special care in get_proc_name
7250 because previous references within the function will have
7251 created symbols attached to the current namespace. */
7252 if (get_proc_name (name, &entry,
7253 gfc_current_ns->parent != NULL
7254 && module_procedure))
7255 return MATCH_ERROR;
7257 proc = gfc_current_block ();
7259 /* Make sure that it isn't already declared as BIND(C). If it is, it
7260 must have been marked BIND(C) with a BIND(C) attribute and that is
7261 not allowed for procedures. */
7262 if (entry->attr.is_bind_c == 1)
7264 entry->attr.is_bind_c = 0;
7265 if (entry->old_symbol != NULL)
7266 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7267 "variables or common blocks",
7268 &(entry->old_symbol->declared_at));
7269 else
7270 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7271 "variables or common blocks", &gfc_current_locus);
7274 /* Check what next non-whitespace character is so we can tell if there
7275 is the required parens if we have a BIND(C). */
7276 old_loc = gfc_current_locus;
7277 gfc_gobble_whitespace ();
7278 peek_char = gfc_peek_ascii_char ();
7280 if (state == COMP_SUBROUTINE)
7282 m = gfc_match_formal_arglist (entry, 0, 1);
7283 if (m != MATCH_YES)
7284 return MATCH_ERROR;
7286 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7287 never be an internal procedure. */
7288 is_bind_c = gfc_match_bind_c (entry, true);
7289 if (is_bind_c == MATCH_ERROR)
7290 return MATCH_ERROR;
7291 if (is_bind_c == MATCH_YES)
7293 if (peek_char != '(')
7295 gfc_error ("Missing required parentheses before BIND(C) at %C");
7296 return MATCH_ERROR;
7298 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7299 &(entry->declared_at), 1))
7300 return MATCH_ERROR;
7303 if (!gfc_current_ns->parent
7304 && !add_global_entry (name, entry->binding_label, true,
7305 &old_loc))
7306 return MATCH_ERROR;
7308 /* An entry in a subroutine. */
7309 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7310 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7311 return MATCH_ERROR;
7313 else
7315 /* An entry in a function.
7316 We need to take special care because writing
7317 ENTRY f()
7319 ENTRY f
7320 is allowed, whereas
7321 ENTRY f() RESULT (r)
7322 can't be written as
7323 ENTRY f RESULT (r). */
7324 if (gfc_match_eos () == MATCH_YES)
7326 gfc_current_locus = old_loc;
7327 /* Match the empty argument list, and add the interface to
7328 the symbol. */
7329 m = gfc_match_formal_arglist (entry, 0, 1);
7331 else
7332 m = gfc_match_formal_arglist (entry, 0, 0);
7334 if (m != MATCH_YES)
7335 return MATCH_ERROR;
7337 result = NULL;
7339 if (gfc_match_eos () == MATCH_YES)
7341 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7342 || !gfc_add_function (&entry->attr, entry->name, NULL))
7343 return MATCH_ERROR;
7345 entry->result = entry;
7347 else
7349 m = gfc_match_suffix (entry, &result);
7350 if (m == MATCH_NO)
7351 gfc_syntax_error (ST_ENTRY);
7352 if (m != MATCH_YES)
7353 return MATCH_ERROR;
7355 if (result)
7357 if (!gfc_add_result (&result->attr, result->name, NULL)
7358 || !gfc_add_entry (&entry->attr, result->name, NULL)
7359 || !gfc_add_function (&entry->attr, result->name, NULL))
7360 return MATCH_ERROR;
7361 entry->result = result;
7363 else
7365 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7366 || !gfc_add_function (&entry->attr, entry->name, NULL))
7367 return MATCH_ERROR;
7368 entry->result = entry;
7372 if (!gfc_current_ns->parent
7373 && !add_global_entry (name, entry->binding_label, false,
7374 &old_loc))
7375 return MATCH_ERROR;
7378 if (gfc_match_eos () != MATCH_YES)
7380 gfc_syntax_error (ST_ENTRY);
7381 return MATCH_ERROR;
7384 entry->attr.recursive = proc->attr.recursive;
7385 entry->attr.elemental = proc->attr.elemental;
7386 entry->attr.pure = proc->attr.pure;
7388 el = gfc_get_entry_list ();
7389 el->sym = entry;
7390 el->next = gfc_current_ns->entries;
7391 gfc_current_ns->entries = el;
7392 if (el->next)
7393 el->id = el->next->id + 1;
7394 else
7395 el->id = 1;
7397 new_st.op = EXEC_ENTRY;
7398 new_st.ext.entry = el;
7400 return MATCH_YES;
7404 /* Match a subroutine statement, including optional prefixes. */
7406 match
7407 gfc_match_subroutine (void)
7409 char name[GFC_MAX_SYMBOL_LEN + 1];
7410 gfc_symbol *sym;
7411 match m;
7412 match is_bind_c;
7413 char peek_char;
7414 bool allow_binding_name;
7416 if (gfc_current_state () != COMP_NONE
7417 && gfc_current_state () != COMP_INTERFACE
7418 && gfc_current_state () != COMP_CONTAINS)
7419 return MATCH_NO;
7421 m = gfc_match_prefix (NULL);
7422 if (m != MATCH_YES)
7423 return m;
7425 m = gfc_match ("subroutine% %n", name);
7426 if (m != MATCH_YES)
7427 return m;
7429 if (get_proc_name (name, &sym, false))
7430 return MATCH_ERROR;
7432 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7433 the symbol existed before. */
7434 sym->declared_at = gfc_current_locus;
7436 if (current_attr.module_procedure)
7437 sym->attr.module_procedure = 1;
7439 if (add_hidden_procptr_result (sym))
7440 sym = sym->result;
7442 gfc_new_block = sym;
7444 /* Check what next non-whitespace character is so we can tell if there
7445 is the required parens if we have a BIND(C). */
7446 gfc_gobble_whitespace ();
7447 peek_char = gfc_peek_ascii_char ();
7449 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7450 return MATCH_ERROR;
7452 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7453 return MATCH_ERROR;
7455 /* Make sure that it isn't already declared as BIND(C). If it is, it
7456 must have been marked BIND(C) with a BIND(C) attribute and that is
7457 not allowed for procedures. */
7458 if (sym->attr.is_bind_c == 1)
7460 sym->attr.is_bind_c = 0;
7461 if (sym->old_symbol != NULL)
7462 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7463 "variables or common blocks",
7464 &(sym->old_symbol->declared_at));
7465 else
7466 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7467 "variables or common blocks", &gfc_current_locus);
7470 /* C binding names are not allowed for internal procedures. */
7471 if (gfc_current_state () == COMP_CONTAINS
7472 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7473 allow_binding_name = false;
7474 else
7475 allow_binding_name = true;
7477 /* Here, we are just checking if it has the bind(c) attribute, and if
7478 so, then we need to make sure it's all correct. If it doesn't,
7479 we still need to continue matching the rest of the subroutine line. */
7480 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7481 if (is_bind_c == MATCH_ERROR)
7483 /* There was an attempt at the bind(c), but it was wrong. An
7484 error message should have been printed w/in the gfc_match_bind_c
7485 so here we'll just return the MATCH_ERROR. */
7486 return MATCH_ERROR;
7489 if (is_bind_c == MATCH_YES)
7491 /* The following is allowed in the Fortran 2008 draft. */
7492 if (gfc_current_state () == COMP_CONTAINS
7493 && sym->ns->proc_name->attr.flavor != FL_MODULE
7494 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7495 "at %L may not be specified for an internal "
7496 "procedure", &gfc_current_locus))
7497 return MATCH_ERROR;
7499 if (peek_char != '(')
7501 gfc_error ("Missing required parentheses before BIND(C) at %C");
7502 return MATCH_ERROR;
7504 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
7505 &(sym->declared_at), 1))
7506 return MATCH_ERROR;
7509 if (gfc_match_eos () != MATCH_YES)
7511 gfc_syntax_error (ST_SUBROUTINE);
7512 return MATCH_ERROR;
7515 if (!copy_prefix (&sym->attr, &sym->declared_at))
7517 if(!sym->attr.module_procedure)
7518 return MATCH_ERROR;
7519 else
7520 gfc_error_check ();
7523 /* Warn if it has the same name as an intrinsic. */
7524 do_warn_intrinsic_shadow (sym, false);
7526 return MATCH_YES;
7530 /* Check that the NAME identifier in a BIND attribute or statement
7531 is conform to C identifier rules. */
7533 match
7534 check_bind_name_identifier (char **name)
7536 char *n = *name, *p;
7538 /* Remove leading spaces. */
7539 while (*n == ' ')
7540 n++;
7542 /* On an empty string, free memory and set name to NULL. */
7543 if (*n == '\0')
7545 free (*name);
7546 *name = NULL;
7547 return MATCH_YES;
7550 /* Remove trailing spaces. */
7551 p = n + strlen(n) - 1;
7552 while (*p == ' ')
7553 *(p--) = '\0';
7555 /* Insert the identifier into the symbol table. */
7556 p = xstrdup (n);
7557 free (*name);
7558 *name = p;
7560 /* Now check that identifier is valid under C rules. */
7561 if (ISDIGIT (*p))
7563 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7564 return MATCH_ERROR;
7567 for (; *p; p++)
7568 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
7570 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7571 return MATCH_ERROR;
7574 return MATCH_YES;
7578 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
7579 given, and set the binding label in either the given symbol (if not
7580 NULL), or in the current_ts. The symbol may be NULL because we may
7581 encounter the BIND(C) before the declaration itself. Return
7582 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
7583 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
7584 or MATCH_YES if the specifier was correct and the binding label and
7585 bind(c) fields were set correctly for the given symbol or the
7586 current_ts. If allow_binding_name is false, no binding name may be
7587 given. */
7589 match
7590 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
7592 char *binding_label = NULL;
7593 gfc_expr *e = NULL;
7595 /* Initialize the flag that specifies whether we encountered a NAME=
7596 specifier or not. */
7597 has_name_equals = 0;
7599 /* This much we have to be able to match, in this order, if
7600 there is a bind(c) label. */
7601 if (gfc_match (" bind ( c ") != MATCH_YES)
7602 return MATCH_NO;
7604 /* Now see if there is a binding label, or if we've reached the
7605 end of the bind(c) attribute without one. */
7606 if (gfc_match_char (',') == MATCH_YES)
7608 if (gfc_match (" name = ") != MATCH_YES)
7610 gfc_error ("Syntax error in NAME= specifier for binding label "
7611 "at %C");
7612 /* should give an error message here */
7613 return MATCH_ERROR;
7616 has_name_equals = 1;
7618 if (gfc_match_init_expr (&e) != MATCH_YES)
7620 gfc_free_expr (e);
7621 return MATCH_ERROR;
7624 if (!gfc_simplify_expr(e, 0))
7626 gfc_error ("NAME= specifier at %C should be a constant expression");
7627 gfc_free_expr (e);
7628 return MATCH_ERROR;
7631 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
7632 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
7634 gfc_error ("NAME= specifier at %C should be a scalar of "
7635 "default character kind");
7636 gfc_free_expr(e);
7637 return MATCH_ERROR;
7640 // Get a C string from the Fortran string constant
7641 binding_label = gfc_widechar_to_char (e->value.character.string,
7642 e->value.character.length);
7643 gfc_free_expr(e);
7645 // Check that it is valid (old gfc_match_name_C)
7646 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
7647 return MATCH_ERROR;
7650 /* Get the required right paren. */
7651 if (gfc_match_char (')') != MATCH_YES)
7653 gfc_error ("Missing closing paren for binding label at %C");
7654 return MATCH_ERROR;
7657 if (has_name_equals && !allow_binding_name)
7659 gfc_error ("No binding name is allowed in BIND(C) at %C");
7660 return MATCH_ERROR;
7663 if (has_name_equals && sym != NULL && sym->attr.dummy)
7665 gfc_error ("For dummy procedure %s, no binding name is "
7666 "allowed in BIND(C) at %C", sym->name);
7667 return MATCH_ERROR;
7671 /* Save the binding label to the symbol. If sym is null, we're
7672 probably matching the typespec attributes of a declaration and
7673 haven't gotten the name yet, and therefore, no symbol yet. */
7674 if (binding_label)
7676 if (sym != NULL)
7677 sym->binding_label = binding_label;
7678 else
7679 curr_binding_label = binding_label;
7681 else if (allow_binding_name)
7683 /* No binding label, but if symbol isn't null, we
7684 can set the label for it here.
7685 If name="" or allow_binding_name is false, no C binding name is
7686 created. */
7687 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
7688 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
7691 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
7692 && current_interface.type == INTERFACE_ABSTRACT)
7694 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
7695 return MATCH_ERROR;
7698 return MATCH_YES;
7702 /* Return nonzero if we're currently compiling a contained procedure. */
7704 static int
7705 contained_procedure (void)
7707 gfc_state_data *s = gfc_state_stack;
7709 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
7710 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
7711 return 1;
7713 return 0;
7716 /* Set the kind of each enumerator. The kind is selected such that it is
7717 interoperable with the corresponding C enumeration type, making
7718 sure that -fshort-enums is honored. */
7720 static void
7721 set_enum_kind(void)
7723 enumerator_history *current_history = NULL;
7724 int kind;
7725 int i;
7727 if (max_enum == NULL || enum_history == NULL)
7728 return;
7730 if (!flag_short_enums)
7731 return;
7733 i = 0;
7736 kind = gfc_integer_kinds[i++].kind;
7738 while (kind < gfc_c_int_kind
7739 && gfc_check_integer_range (max_enum->initializer->value.integer,
7740 kind) != ARITH_OK);
7742 current_history = enum_history;
7743 while (current_history != NULL)
7745 current_history->sym->ts.kind = kind;
7746 current_history = current_history->next;
7751 /* Match any of the various end-block statements. Returns the type of
7752 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
7753 and END BLOCK statements cannot be replaced by a single END statement. */
7755 match
7756 gfc_match_end (gfc_statement *st)
7758 char name[GFC_MAX_SYMBOL_LEN + 1];
7759 gfc_compile_state state;
7760 locus old_loc;
7761 const char *block_name;
7762 const char *target;
7763 int eos_ok;
7764 match m;
7765 gfc_namespace *parent_ns, *ns, *prev_ns;
7766 gfc_namespace **nsp;
7767 bool abreviated_modproc_decl = false;
7768 bool got_matching_end = false;
7770 old_loc = gfc_current_locus;
7771 if (gfc_match ("end") != MATCH_YES)
7772 return MATCH_NO;
7774 state = gfc_current_state ();
7775 block_name = gfc_current_block () == NULL
7776 ? NULL : gfc_current_block ()->name;
7778 switch (state)
7780 case COMP_ASSOCIATE:
7781 case COMP_BLOCK:
7782 if (!strncmp (block_name, "block@", strlen("block@")))
7783 block_name = NULL;
7784 break;
7786 case COMP_CONTAINS:
7787 case COMP_DERIVED_CONTAINS:
7788 state = gfc_state_stack->previous->state;
7789 block_name = gfc_state_stack->previous->sym == NULL
7790 ? NULL : gfc_state_stack->previous->sym->name;
7791 abreviated_modproc_decl = gfc_state_stack->previous->sym
7792 && gfc_state_stack->previous->sym->abr_modproc_decl;
7793 break;
7795 default:
7796 break;
7799 if (!abreviated_modproc_decl)
7800 abreviated_modproc_decl = gfc_current_block ()
7801 && gfc_current_block ()->abr_modproc_decl;
7803 switch (state)
7805 case COMP_NONE:
7806 case COMP_PROGRAM:
7807 *st = ST_END_PROGRAM;
7808 target = " program";
7809 eos_ok = 1;
7810 break;
7812 case COMP_SUBROUTINE:
7813 *st = ST_END_SUBROUTINE;
7814 if (!abreviated_modproc_decl)
7815 target = " subroutine";
7816 else
7817 target = " procedure";
7818 eos_ok = !contained_procedure ();
7819 break;
7821 case COMP_FUNCTION:
7822 *st = ST_END_FUNCTION;
7823 if (!abreviated_modproc_decl)
7824 target = " function";
7825 else
7826 target = " procedure";
7827 eos_ok = !contained_procedure ();
7828 break;
7830 case COMP_BLOCK_DATA:
7831 *st = ST_END_BLOCK_DATA;
7832 target = " block data";
7833 eos_ok = 1;
7834 break;
7836 case COMP_MODULE:
7837 *st = ST_END_MODULE;
7838 target = " module";
7839 eos_ok = 1;
7840 break;
7842 case COMP_SUBMODULE:
7843 *st = ST_END_SUBMODULE;
7844 target = " submodule";
7845 eos_ok = 1;
7846 break;
7848 case COMP_INTERFACE:
7849 *st = ST_END_INTERFACE;
7850 target = " interface";
7851 eos_ok = 0;
7852 break;
7854 case COMP_MAP:
7855 *st = ST_END_MAP;
7856 target = " map";
7857 eos_ok = 0;
7858 break;
7860 case COMP_UNION:
7861 *st = ST_END_UNION;
7862 target = " union";
7863 eos_ok = 0;
7864 break;
7866 case COMP_STRUCTURE:
7867 *st = ST_END_STRUCTURE;
7868 target = " structure";
7869 eos_ok = 0;
7870 break;
7872 case COMP_DERIVED:
7873 case COMP_DERIVED_CONTAINS:
7874 *st = ST_END_TYPE;
7875 target = " type";
7876 eos_ok = 0;
7877 break;
7879 case COMP_ASSOCIATE:
7880 *st = ST_END_ASSOCIATE;
7881 target = " associate";
7882 eos_ok = 0;
7883 break;
7885 case COMP_BLOCK:
7886 *st = ST_END_BLOCK;
7887 target = " block";
7888 eos_ok = 0;
7889 break;
7891 case COMP_IF:
7892 *st = ST_ENDIF;
7893 target = " if";
7894 eos_ok = 0;
7895 break;
7897 case COMP_DO:
7898 case COMP_DO_CONCURRENT:
7899 *st = ST_ENDDO;
7900 target = " do";
7901 eos_ok = 0;
7902 break;
7904 case COMP_CRITICAL:
7905 *st = ST_END_CRITICAL;
7906 target = " critical";
7907 eos_ok = 0;
7908 break;
7910 case COMP_SELECT:
7911 case COMP_SELECT_TYPE:
7912 *st = ST_END_SELECT;
7913 target = " select";
7914 eos_ok = 0;
7915 break;
7917 case COMP_FORALL:
7918 *st = ST_END_FORALL;
7919 target = " forall";
7920 eos_ok = 0;
7921 break;
7923 case COMP_WHERE:
7924 *st = ST_END_WHERE;
7925 target = " where";
7926 eos_ok = 0;
7927 break;
7929 case COMP_ENUM:
7930 *st = ST_END_ENUM;
7931 target = " enum";
7932 eos_ok = 0;
7933 last_initializer = NULL;
7934 set_enum_kind ();
7935 gfc_free_enum_history ();
7936 break;
7938 default:
7939 gfc_error ("Unexpected END statement at %C");
7940 goto cleanup;
7943 old_loc = gfc_current_locus;
7944 if (gfc_match_eos () == MATCH_YES)
7946 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
7948 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
7949 "instead of %s statement at %L",
7950 abreviated_modproc_decl ? "END PROCEDURE"
7951 : gfc_ascii_statement(*st), &old_loc))
7952 goto cleanup;
7954 else if (!eos_ok)
7956 /* We would have required END [something]. */
7957 gfc_error ("%s statement expected at %L",
7958 gfc_ascii_statement (*st), &old_loc);
7959 goto cleanup;
7962 return MATCH_YES;
7965 /* Verify that we've got the sort of end-block that we're expecting. */
7966 if (gfc_match (target) != MATCH_YES)
7968 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
7969 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
7970 goto cleanup;
7972 else
7973 got_matching_end = true;
7975 old_loc = gfc_current_locus;
7976 /* If we're at the end, make sure a block name wasn't required. */
7977 if (gfc_match_eos () == MATCH_YES)
7980 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
7981 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
7982 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
7983 return MATCH_YES;
7985 if (!block_name)
7986 return MATCH_YES;
7988 gfc_error ("Expected block name of %qs in %s statement at %L",
7989 block_name, gfc_ascii_statement (*st), &old_loc);
7991 return MATCH_ERROR;
7994 /* END INTERFACE has a special handler for its several possible endings. */
7995 if (*st == ST_END_INTERFACE)
7996 return gfc_match_end_interface ();
7998 /* We haven't hit the end of statement, so what is left must be an
7999 end-name. */
8000 m = gfc_match_space ();
8001 if (m == MATCH_YES)
8002 m = gfc_match_name (name);
8004 if (m == MATCH_NO)
8005 gfc_error ("Expected terminating name at %C");
8006 if (m != MATCH_YES)
8007 goto cleanup;
8009 if (block_name == NULL)
8010 goto syntax;
8012 /* We have to pick out the declared submodule name from the composite
8013 required by F2008:11.2.3 para 2, which ends in the declared name. */
8014 if (state == COMP_SUBMODULE)
8015 block_name = strchr (block_name, '.') + 1;
8017 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
8019 gfc_error ("Expected label %qs for %s statement at %C", block_name,
8020 gfc_ascii_statement (*st));
8021 goto cleanup;
8023 /* Procedure pointer as function result. */
8024 else if (strcmp (block_name, "ppr@") == 0
8025 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8027 gfc_error ("Expected label %qs for %s statement at %C",
8028 gfc_current_block ()->ns->proc_name->name,
8029 gfc_ascii_statement (*st));
8030 goto cleanup;
8033 if (gfc_match_eos () == MATCH_YES)
8034 return MATCH_YES;
8036 syntax:
8037 gfc_syntax_error (*st);
8039 cleanup:
8040 gfc_current_locus = old_loc;
8042 /* If we are missing an END BLOCK, we created a half-ready namespace.
8043 Remove it from the parent namespace's sibling list. */
8045 while (state == COMP_BLOCK && !got_matching_end)
8047 parent_ns = gfc_current_ns->parent;
8049 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8051 prev_ns = NULL;
8052 ns = *nsp;
8053 while (ns)
8055 if (ns == gfc_current_ns)
8057 if (prev_ns == NULL)
8058 *nsp = NULL;
8059 else
8060 prev_ns->sibling = ns->sibling;
8062 prev_ns = ns;
8063 ns = ns->sibling;
8066 gfc_free_namespace (gfc_current_ns);
8067 gfc_current_ns = parent_ns;
8068 gfc_state_stack = gfc_state_stack->previous;
8069 state = gfc_current_state ();
8072 return MATCH_ERROR;
8077 /***************** Attribute declaration statements ****************/
8079 /* Set the attribute of a single variable. */
8081 static match
8082 attr_decl1 (void)
8084 char name[GFC_MAX_SYMBOL_LEN + 1];
8085 gfc_array_spec *as;
8087 /* Workaround -Wmaybe-uninitialized false positive during
8088 profiledbootstrap by initializing them. */
8089 gfc_symbol *sym = NULL;
8090 locus var_locus;
8091 match m;
8093 as = NULL;
8095 m = gfc_match_name (name);
8096 if (m != MATCH_YES)
8097 goto cleanup;
8099 if (find_special (name, &sym, false))
8100 return MATCH_ERROR;
8102 if (!check_function_name (name))
8104 m = MATCH_ERROR;
8105 goto cleanup;
8108 var_locus = gfc_current_locus;
8110 /* Deal with possible array specification for certain attributes. */
8111 if (current_attr.dimension
8112 || current_attr.codimension
8113 || current_attr.allocatable
8114 || current_attr.pointer
8115 || current_attr.target)
8117 m = gfc_match_array_spec (&as, !current_attr.codimension,
8118 !current_attr.dimension
8119 && !current_attr.pointer
8120 && !current_attr.target);
8121 if (m == MATCH_ERROR)
8122 goto cleanup;
8124 if (current_attr.dimension && m == MATCH_NO)
8126 gfc_error ("Missing array specification at %L in DIMENSION "
8127 "statement", &var_locus);
8128 m = MATCH_ERROR;
8129 goto cleanup;
8132 if (current_attr.dimension && sym->value)
8134 gfc_error ("Dimensions specified for %s at %L after its "
8135 "initialization", sym->name, &var_locus);
8136 m = MATCH_ERROR;
8137 goto cleanup;
8140 if (current_attr.codimension && m == MATCH_NO)
8142 gfc_error ("Missing array specification at %L in CODIMENSION "
8143 "statement", &var_locus);
8144 m = MATCH_ERROR;
8145 goto cleanup;
8148 if ((current_attr.allocatable || current_attr.pointer)
8149 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8151 gfc_error ("Array specification must be deferred at %L", &var_locus);
8152 m = MATCH_ERROR;
8153 goto cleanup;
8157 /* Update symbol table. DIMENSION attribute is set in
8158 gfc_set_array_spec(). For CLASS variables, this must be applied
8159 to the first component, or '_data' field. */
8160 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
8162 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
8164 m = MATCH_ERROR;
8165 goto cleanup;
8168 else
8170 if (current_attr.dimension == 0 && current_attr.codimension == 0
8171 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
8173 m = MATCH_ERROR;
8174 goto cleanup;
8178 if (sym->ts.type == BT_CLASS
8179 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
8181 m = MATCH_ERROR;
8182 goto cleanup;
8185 if (!gfc_set_array_spec (sym, as, &var_locus))
8187 m = MATCH_ERROR;
8188 goto cleanup;
8191 if (sym->attr.cray_pointee && sym->as != NULL)
8193 /* Fix the array spec. */
8194 m = gfc_mod_pointee_as (sym->as);
8195 if (m == MATCH_ERROR)
8196 goto cleanup;
8199 if (!gfc_add_attribute (&sym->attr, &var_locus))
8201 m = MATCH_ERROR;
8202 goto cleanup;
8205 if ((current_attr.external || current_attr.intrinsic)
8206 && sym->attr.flavor != FL_PROCEDURE
8207 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8209 m = MATCH_ERROR;
8210 goto cleanup;
8213 add_hidden_procptr_result (sym);
8215 return MATCH_YES;
8217 cleanup:
8218 gfc_free_array_spec (as);
8219 return m;
8223 /* Generic attribute declaration subroutine. Used for attributes that
8224 just have a list of names. */
8226 static match
8227 attr_decl (void)
8229 match m;
8231 /* Gobble the optional double colon, by simply ignoring the result
8232 of gfc_match(). */
8233 gfc_match (" ::");
8235 for (;;)
8237 m = attr_decl1 ();
8238 if (m != MATCH_YES)
8239 break;
8241 if (gfc_match_eos () == MATCH_YES)
8243 m = MATCH_YES;
8244 break;
8247 if (gfc_match_char (',') != MATCH_YES)
8249 gfc_error ("Unexpected character in variable list at %C");
8250 m = MATCH_ERROR;
8251 break;
8255 return m;
8259 /* This routine matches Cray Pointer declarations of the form:
8260 pointer ( <pointer>, <pointee> )
8262 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8263 The pointer, if already declared, should be an integer. Otherwise, we
8264 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8265 be either a scalar, or an array declaration. No space is allocated for
8266 the pointee. For the statement
8267 pointer (ipt, ar(10))
8268 any subsequent uses of ar will be translated (in C-notation) as
8269 ar(i) => ((<type> *) ipt)(i)
8270 After gimplification, pointee variable will disappear in the code. */
8272 static match
8273 cray_pointer_decl (void)
8275 match m;
8276 gfc_array_spec *as = NULL;
8277 gfc_symbol *cptr; /* Pointer symbol. */
8278 gfc_symbol *cpte; /* Pointee symbol. */
8279 locus var_locus;
8280 bool done = false;
8282 while (!done)
8284 if (gfc_match_char ('(') != MATCH_YES)
8286 gfc_error ("Expected %<(%> at %C");
8287 return MATCH_ERROR;
8290 /* Match pointer. */
8291 var_locus = gfc_current_locus;
8292 gfc_clear_attr (&current_attr);
8293 gfc_add_cray_pointer (&current_attr, &var_locus);
8294 current_ts.type = BT_INTEGER;
8295 current_ts.kind = gfc_index_integer_kind;
8297 m = gfc_match_symbol (&cptr, 0);
8298 if (m != MATCH_YES)
8300 gfc_error ("Expected variable name at %C");
8301 return m;
8304 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8305 return MATCH_ERROR;
8307 gfc_set_sym_referenced (cptr);
8309 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8311 cptr->ts.type = BT_INTEGER;
8312 cptr->ts.kind = gfc_index_integer_kind;
8314 else if (cptr->ts.type != BT_INTEGER)
8316 gfc_error ("Cray pointer at %C must be an integer");
8317 return MATCH_ERROR;
8319 else if (cptr->ts.kind < gfc_index_integer_kind)
8320 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8321 " memory addresses require %d bytes",
8322 cptr->ts.kind, gfc_index_integer_kind);
8324 if (gfc_match_char (',') != MATCH_YES)
8326 gfc_error ("Expected \",\" at %C");
8327 return MATCH_ERROR;
8330 /* Match Pointee. */
8331 var_locus = gfc_current_locus;
8332 gfc_clear_attr (&current_attr);
8333 gfc_add_cray_pointee (&current_attr, &var_locus);
8334 current_ts.type = BT_UNKNOWN;
8335 current_ts.kind = 0;
8337 m = gfc_match_symbol (&cpte, 0);
8338 if (m != MATCH_YES)
8340 gfc_error ("Expected variable name at %C");
8341 return m;
8344 /* Check for an optional array spec. */
8345 m = gfc_match_array_spec (&as, true, false);
8346 if (m == MATCH_ERROR)
8348 gfc_free_array_spec (as);
8349 return m;
8351 else if (m == MATCH_NO)
8353 gfc_free_array_spec (as);
8354 as = NULL;
8357 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8358 return MATCH_ERROR;
8360 gfc_set_sym_referenced (cpte);
8362 if (cpte->as == NULL)
8364 if (!gfc_set_array_spec (cpte, as, &var_locus))
8365 gfc_internal_error ("Couldn't set Cray pointee array spec.");
8367 else if (as != NULL)
8369 gfc_error ("Duplicate array spec for Cray pointee at %C");
8370 gfc_free_array_spec (as);
8371 return MATCH_ERROR;
8374 as = NULL;
8376 if (cpte->as != NULL)
8378 /* Fix array spec. */
8379 m = gfc_mod_pointee_as (cpte->as);
8380 if (m == MATCH_ERROR)
8381 return m;
8384 /* Point the Pointee at the Pointer. */
8385 cpte->cp_pointer = cptr;
8387 if (gfc_match_char (')') != MATCH_YES)
8389 gfc_error ("Expected \")\" at %C");
8390 return MATCH_ERROR;
8392 m = gfc_match_char (',');
8393 if (m != MATCH_YES)
8394 done = true; /* Stop searching for more declarations. */
8398 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8399 || gfc_match_eos () != MATCH_YES)
8401 gfc_error ("Expected %<,%> or end of statement at %C");
8402 return MATCH_ERROR;
8404 return MATCH_YES;
8408 match
8409 gfc_match_external (void)
8412 gfc_clear_attr (&current_attr);
8413 current_attr.external = 1;
8415 return attr_decl ();
8419 match
8420 gfc_match_intent (void)
8422 sym_intent intent;
8424 /* This is not allowed within a BLOCK construct! */
8425 if (gfc_current_state () == COMP_BLOCK)
8427 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8428 return MATCH_ERROR;
8431 intent = match_intent_spec ();
8432 if (intent == INTENT_UNKNOWN)
8433 return MATCH_ERROR;
8435 gfc_clear_attr (&current_attr);
8436 current_attr.intent = intent;
8438 return attr_decl ();
8442 match
8443 gfc_match_intrinsic (void)
8446 gfc_clear_attr (&current_attr);
8447 current_attr.intrinsic = 1;
8449 return attr_decl ();
8453 match
8454 gfc_match_optional (void)
8456 /* This is not allowed within a BLOCK construct! */
8457 if (gfc_current_state () == COMP_BLOCK)
8459 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8460 return MATCH_ERROR;
8463 gfc_clear_attr (&current_attr);
8464 current_attr.optional = 1;
8466 return attr_decl ();
8470 match
8471 gfc_match_pointer (void)
8473 gfc_gobble_whitespace ();
8474 if (gfc_peek_ascii_char () == '(')
8476 if (!flag_cray_pointer)
8478 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
8479 "flag");
8480 return MATCH_ERROR;
8482 return cray_pointer_decl ();
8484 else
8486 gfc_clear_attr (&current_attr);
8487 current_attr.pointer = 1;
8489 return attr_decl ();
8494 match
8495 gfc_match_allocatable (void)
8497 gfc_clear_attr (&current_attr);
8498 current_attr.allocatable = 1;
8500 return attr_decl ();
8504 match
8505 gfc_match_codimension (void)
8507 gfc_clear_attr (&current_attr);
8508 current_attr.codimension = 1;
8510 return attr_decl ();
8514 match
8515 gfc_match_contiguous (void)
8517 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
8518 return MATCH_ERROR;
8520 gfc_clear_attr (&current_attr);
8521 current_attr.contiguous = 1;
8523 return attr_decl ();
8527 match
8528 gfc_match_dimension (void)
8530 gfc_clear_attr (&current_attr);
8531 current_attr.dimension = 1;
8533 return attr_decl ();
8537 match
8538 gfc_match_target (void)
8540 gfc_clear_attr (&current_attr);
8541 current_attr.target = 1;
8543 return attr_decl ();
8547 /* Match the list of entities being specified in a PUBLIC or PRIVATE
8548 statement. */
8550 static match
8551 access_attr_decl (gfc_statement st)
8553 char name[GFC_MAX_SYMBOL_LEN + 1];
8554 interface_type type;
8555 gfc_user_op *uop;
8556 gfc_symbol *sym, *dt_sym;
8557 gfc_intrinsic_op op;
8558 match m;
8560 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8561 goto done;
8563 for (;;)
8565 m = gfc_match_generic_spec (&type, name, &op);
8566 if (m == MATCH_NO)
8567 goto syntax;
8568 if (m == MATCH_ERROR)
8569 return MATCH_ERROR;
8571 switch (type)
8573 case INTERFACE_NAMELESS:
8574 case INTERFACE_ABSTRACT:
8575 goto syntax;
8577 case INTERFACE_GENERIC:
8578 case INTERFACE_DTIO:
8580 if (gfc_get_symbol (name, NULL, &sym))
8581 goto done;
8583 if (type == INTERFACE_DTIO
8584 && gfc_current_ns->proc_name
8585 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
8586 && sym->attr.flavor == FL_UNKNOWN)
8587 sym->attr.flavor = FL_PROCEDURE;
8589 if (!gfc_add_access (&sym->attr,
8590 (st == ST_PUBLIC)
8591 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8592 sym->name, NULL))
8593 return MATCH_ERROR;
8595 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
8596 && !gfc_add_access (&dt_sym->attr,
8597 (st == ST_PUBLIC)
8598 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8599 sym->name, NULL))
8600 return MATCH_ERROR;
8602 break;
8604 case INTERFACE_INTRINSIC_OP:
8605 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
8607 gfc_intrinsic_op other_op;
8609 gfc_current_ns->operator_access[op] =
8610 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8612 /* Handle the case if there is another op with the same
8613 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
8614 other_op = gfc_equivalent_op (op);
8616 if (other_op != INTRINSIC_NONE)
8617 gfc_current_ns->operator_access[other_op] =
8618 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8621 else
8623 gfc_error ("Access specification of the %s operator at %C has "
8624 "already been specified", gfc_op2string (op));
8625 goto done;
8628 break;
8630 case INTERFACE_USER_OP:
8631 uop = gfc_get_uop (name);
8633 if (uop->access == ACCESS_UNKNOWN)
8635 uop->access = (st == ST_PUBLIC)
8636 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8638 else
8640 gfc_error ("Access specification of the .%s. operator at %C "
8641 "has already been specified", sym->name);
8642 goto done;
8645 break;
8648 if (gfc_match_char (',') == MATCH_NO)
8649 break;
8652 if (gfc_match_eos () != MATCH_YES)
8653 goto syntax;
8654 return MATCH_YES;
8656 syntax:
8657 gfc_syntax_error (st);
8659 done:
8660 return MATCH_ERROR;
8664 match
8665 gfc_match_protected (void)
8667 gfc_symbol *sym;
8668 match m;
8670 if (!gfc_current_ns->proc_name
8671 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
8673 gfc_error ("PROTECTED at %C only allowed in specification "
8674 "part of a module");
8675 return MATCH_ERROR;
8679 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
8680 return MATCH_ERROR;
8682 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8684 return MATCH_ERROR;
8687 if (gfc_match_eos () == MATCH_YES)
8688 goto syntax;
8690 for(;;)
8692 m = gfc_match_symbol (&sym, 0);
8693 switch (m)
8695 case MATCH_YES:
8696 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
8697 return MATCH_ERROR;
8698 goto next_item;
8700 case MATCH_NO:
8701 break;
8703 case MATCH_ERROR:
8704 return MATCH_ERROR;
8707 next_item:
8708 if (gfc_match_eos () == MATCH_YES)
8709 break;
8710 if (gfc_match_char (',') != MATCH_YES)
8711 goto syntax;
8714 return MATCH_YES;
8716 syntax:
8717 gfc_error ("Syntax error in PROTECTED statement at %C");
8718 return MATCH_ERROR;
8722 /* The PRIVATE statement is a bit weird in that it can be an attribute
8723 declaration, but also works as a standalone statement inside of a
8724 type declaration or a module. */
8726 match
8727 gfc_match_private (gfc_statement *st)
8730 if (gfc_match ("private") != MATCH_YES)
8731 return MATCH_NO;
8733 if (gfc_current_state () != COMP_MODULE
8734 && !(gfc_current_state () == COMP_DERIVED
8735 && gfc_state_stack->previous
8736 && gfc_state_stack->previous->state == COMP_MODULE)
8737 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
8738 && gfc_state_stack->previous && gfc_state_stack->previous->previous
8739 && gfc_state_stack->previous->previous->state == COMP_MODULE))
8741 gfc_error ("PRIVATE statement at %C is only allowed in the "
8742 "specification part of a module");
8743 return MATCH_ERROR;
8746 if (gfc_current_state () == COMP_DERIVED)
8748 if (gfc_match_eos () == MATCH_YES)
8750 *st = ST_PRIVATE;
8751 return MATCH_YES;
8754 gfc_syntax_error (ST_PRIVATE);
8755 return MATCH_ERROR;
8758 if (gfc_match_eos () == MATCH_YES)
8760 *st = ST_PRIVATE;
8761 return MATCH_YES;
8764 *st = ST_ATTR_DECL;
8765 return access_attr_decl (ST_PRIVATE);
8769 match
8770 gfc_match_public (gfc_statement *st)
8773 if (gfc_match ("public") != MATCH_YES)
8774 return MATCH_NO;
8776 if (gfc_current_state () != COMP_MODULE)
8778 gfc_error ("PUBLIC statement at %C is only allowed in the "
8779 "specification part of a module");
8780 return MATCH_ERROR;
8783 if (gfc_match_eos () == MATCH_YES)
8785 *st = ST_PUBLIC;
8786 return MATCH_YES;
8789 *st = ST_ATTR_DECL;
8790 return access_attr_decl (ST_PUBLIC);
8794 /* Workhorse for gfc_match_parameter. */
8796 static match
8797 do_parm (void)
8799 gfc_symbol *sym;
8800 gfc_expr *init;
8801 match m;
8802 bool t;
8804 m = gfc_match_symbol (&sym, 0);
8805 if (m == MATCH_NO)
8806 gfc_error ("Expected variable name at %C in PARAMETER statement");
8808 if (m != MATCH_YES)
8809 return m;
8811 if (gfc_match_char ('=') == MATCH_NO)
8813 gfc_error ("Expected = sign in PARAMETER statement at %C");
8814 return MATCH_ERROR;
8817 m = gfc_match_init_expr (&init);
8818 if (m == MATCH_NO)
8819 gfc_error ("Expected expression at %C in PARAMETER statement");
8820 if (m != MATCH_YES)
8821 return m;
8823 if (sym->ts.type == BT_UNKNOWN
8824 && !gfc_set_default_type (sym, 1, NULL))
8826 m = MATCH_ERROR;
8827 goto cleanup;
8830 if (!gfc_check_assign_symbol (sym, NULL, init)
8831 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
8833 m = MATCH_ERROR;
8834 goto cleanup;
8837 if (sym->value)
8839 gfc_error ("Initializing already initialized variable at %C");
8840 m = MATCH_ERROR;
8841 goto cleanup;
8844 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
8845 return (t) ? MATCH_YES : MATCH_ERROR;
8847 cleanup:
8848 gfc_free_expr (init);
8849 return m;
8853 /* Match a parameter statement, with the weird syntax that these have. */
8855 match
8856 gfc_match_parameter (void)
8858 const char *term = " )%t";
8859 match m;
8861 if (gfc_match_char ('(') == MATCH_NO)
8863 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
8864 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
8865 return MATCH_NO;
8866 term = " %t";
8869 for (;;)
8871 m = do_parm ();
8872 if (m != MATCH_YES)
8873 break;
8875 if (gfc_match (term) == MATCH_YES)
8876 break;
8878 if (gfc_match_char (',') != MATCH_YES)
8880 gfc_error ("Unexpected characters in PARAMETER statement at %C");
8881 m = MATCH_ERROR;
8882 break;
8886 return m;
8890 match
8891 gfc_match_automatic (void)
8893 gfc_symbol *sym;
8894 match m;
8895 bool seen_symbol = false;
8897 if (!flag_dec_static)
8899 gfc_error ("%s at %C is a DEC extension, enable with "
8900 "%<-fdec-static%>",
8901 "AUTOMATIC"
8903 return MATCH_ERROR;
8906 gfc_match (" ::");
8908 for (;;)
8910 m = gfc_match_symbol (&sym, 0);
8911 switch (m)
8913 case MATCH_NO:
8914 break;
8916 case MATCH_ERROR:
8917 return MATCH_ERROR;
8919 case MATCH_YES:
8920 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
8921 return MATCH_ERROR;
8922 seen_symbol = true;
8923 break;
8926 if (gfc_match_eos () == MATCH_YES)
8927 break;
8928 if (gfc_match_char (',') != MATCH_YES)
8929 goto syntax;
8932 if (!seen_symbol)
8934 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
8935 return MATCH_ERROR;
8938 return MATCH_YES;
8940 syntax:
8941 gfc_error ("Syntax error in AUTOMATIC statement at %C");
8942 return MATCH_ERROR;
8946 match
8947 gfc_match_static (void)
8949 gfc_symbol *sym;
8950 match m;
8951 bool seen_symbol = false;
8953 if (!flag_dec_static)
8955 gfc_error ("%s at %C is a DEC extension, enable with "
8956 "%<-fdec-static%>",
8957 "STATIC");
8958 return MATCH_ERROR;
8961 gfc_match (" ::");
8963 for (;;)
8965 m = gfc_match_symbol (&sym, 0);
8966 switch (m)
8968 case MATCH_NO:
8969 break;
8971 case MATCH_ERROR:
8972 return MATCH_ERROR;
8974 case MATCH_YES:
8975 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
8976 &gfc_current_locus))
8977 return MATCH_ERROR;
8978 seen_symbol = true;
8979 break;
8982 if (gfc_match_eos () == MATCH_YES)
8983 break;
8984 if (gfc_match_char (',') != MATCH_YES)
8985 goto syntax;
8988 if (!seen_symbol)
8990 gfc_error ("Expected entity-list in STATIC statement at %C");
8991 return MATCH_ERROR;
8994 return MATCH_YES;
8996 syntax:
8997 gfc_error ("Syntax error in STATIC statement at %C");
8998 return MATCH_ERROR;
9002 /* Save statements have a special syntax. */
9004 match
9005 gfc_match_save (void)
9007 char n[GFC_MAX_SYMBOL_LEN+1];
9008 gfc_common_head *c;
9009 gfc_symbol *sym;
9010 match m;
9012 if (gfc_match_eos () == MATCH_YES)
9014 if (gfc_current_ns->seen_save)
9016 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9017 "follows previous SAVE statement"))
9018 return MATCH_ERROR;
9021 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9022 return MATCH_YES;
9025 if (gfc_current_ns->save_all)
9027 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9028 "blanket SAVE statement"))
9029 return MATCH_ERROR;
9032 gfc_match (" ::");
9034 for (;;)
9036 m = gfc_match_symbol (&sym, 0);
9037 switch (m)
9039 case MATCH_YES:
9040 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9041 &gfc_current_locus))
9042 return MATCH_ERROR;
9043 goto next_item;
9045 case MATCH_NO:
9046 break;
9048 case MATCH_ERROR:
9049 return MATCH_ERROR;
9052 m = gfc_match (" / %n /", &n);
9053 if (m == MATCH_ERROR)
9054 return MATCH_ERROR;
9055 if (m == MATCH_NO)
9056 goto syntax;
9058 c = gfc_get_common (n, 0);
9059 c->saved = 1;
9061 gfc_current_ns->seen_save = 1;
9063 next_item:
9064 if (gfc_match_eos () == MATCH_YES)
9065 break;
9066 if (gfc_match_char (',') != MATCH_YES)
9067 goto syntax;
9070 return MATCH_YES;
9072 syntax:
9073 gfc_error ("Syntax error in SAVE statement at %C");
9074 return MATCH_ERROR;
9078 match
9079 gfc_match_value (void)
9081 gfc_symbol *sym;
9082 match m;
9084 /* This is not allowed within a BLOCK construct! */
9085 if (gfc_current_state () == COMP_BLOCK)
9087 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9088 return MATCH_ERROR;
9091 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
9092 return MATCH_ERROR;
9094 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9096 return MATCH_ERROR;
9099 if (gfc_match_eos () == MATCH_YES)
9100 goto syntax;
9102 for(;;)
9104 m = gfc_match_symbol (&sym, 0);
9105 switch (m)
9107 case MATCH_YES:
9108 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
9109 return MATCH_ERROR;
9110 goto next_item;
9112 case MATCH_NO:
9113 break;
9115 case MATCH_ERROR:
9116 return MATCH_ERROR;
9119 next_item:
9120 if (gfc_match_eos () == MATCH_YES)
9121 break;
9122 if (gfc_match_char (',') != MATCH_YES)
9123 goto syntax;
9126 return MATCH_YES;
9128 syntax:
9129 gfc_error ("Syntax error in VALUE statement at %C");
9130 return MATCH_ERROR;
9134 match
9135 gfc_match_volatile (void)
9137 gfc_symbol *sym;
9138 char *name;
9139 match m;
9141 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
9142 return MATCH_ERROR;
9144 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9146 return MATCH_ERROR;
9149 if (gfc_match_eos () == MATCH_YES)
9150 goto syntax;
9152 for(;;)
9154 /* VOLATILE is special because it can be added to host-associated
9155 symbols locally. Except for coarrays. */
9156 m = gfc_match_symbol (&sym, 1);
9157 switch (m)
9159 case MATCH_YES:
9160 name = XCNEWVAR (char, strlen (sym->name) + 1);
9161 strcpy (name, sym->name);
9162 if (!check_function_name (name))
9163 return MATCH_ERROR;
9164 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9165 for variable in a BLOCK which is defined outside of the BLOCK. */
9166 if (sym->ns != gfc_current_ns && sym->attr.codimension)
9168 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
9169 "%C, which is use-/host-associated", sym->name);
9170 return MATCH_ERROR;
9172 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
9173 return MATCH_ERROR;
9174 goto next_item;
9176 case MATCH_NO:
9177 break;
9179 case MATCH_ERROR:
9180 return MATCH_ERROR;
9183 next_item:
9184 if (gfc_match_eos () == MATCH_YES)
9185 break;
9186 if (gfc_match_char (',') != MATCH_YES)
9187 goto syntax;
9190 return MATCH_YES;
9192 syntax:
9193 gfc_error ("Syntax error in VOLATILE statement at %C");
9194 return MATCH_ERROR;
9198 match
9199 gfc_match_asynchronous (void)
9201 gfc_symbol *sym;
9202 char *name;
9203 match m;
9205 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
9206 return MATCH_ERROR;
9208 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9210 return MATCH_ERROR;
9213 if (gfc_match_eos () == MATCH_YES)
9214 goto syntax;
9216 for(;;)
9218 /* ASYNCHRONOUS is special because it can be added to host-associated
9219 symbols locally. */
9220 m = gfc_match_symbol (&sym, 1);
9221 switch (m)
9223 case MATCH_YES:
9224 name = XCNEWVAR (char, strlen (sym->name) + 1);
9225 strcpy (name, sym->name);
9226 if (!check_function_name (name))
9227 return MATCH_ERROR;
9228 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9229 return MATCH_ERROR;
9230 goto next_item;
9232 case MATCH_NO:
9233 break;
9235 case MATCH_ERROR:
9236 return MATCH_ERROR;
9239 next_item:
9240 if (gfc_match_eos () == MATCH_YES)
9241 break;
9242 if (gfc_match_char (',') != MATCH_YES)
9243 goto syntax;
9246 return MATCH_YES;
9248 syntax:
9249 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9250 return MATCH_ERROR;
9254 /* Match a module procedure statement in a submodule. */
9256 match
9257 gfc_match_submod_proc (void)
9259 char name[GFC_MAX_SYMBOL_LEN + 1];
9260 gfc_symbol *sym, *fsym;
9261 match m;
9262 gfc_formal_arglist *formal, *head, *tail;
9264 if (gfc_current_state () != COMP_CONTAINS
9265 || !(gfc_state_stack->previous
9266 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9267 || gfc_state_stack->previous->state == COMP_MODULE)))
9268 return MATCH_NO;
9270 m = gfc_match (" module% procedure% %n", name);
9271 if (m != MATCH_YES)
9272 return m;
9274 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9275 "at %C"))
9276 return MATCH_ERROR;
9278 if (get_proc_name (name, &sym, false))
9279 return MATCH_ERROR;
9281 /* Make sure that the result field is appropriately filled, even though
9282 the result symbol will be replaced later on. */
9283 if (sym->tlink && sym->tlink->attr.function)
9285 if (sym->tlink->result
9286 && sym->tlink->result != sym->tlink)
9287 sym->result= sym->tlink->result;
9288 else
9289 sym->result = sym;
9292 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9293 the symbol existed before. */
9294 sym->declared_at = gfc_current_locus;
9296 if (!sym->attr.module_procedure)
9297 return MATCH_ERROR;
9299 /* Signal match_end to expect "end procedure". */
9300 sym->abr_modproc_decl = 1;
9302 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9303 sym->attr.if_source = IFSRC_DECL;
9305 gfc_new_block = sym;
9307 /* Make a new formal arglist with the symbols in the procedure
9308 namespace. */
9309 head = tail = NULL;
9310 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9312 if (formal == sym->formal)
9313 head = tail = gfc_get_formal_arglist ();
9314 else
9316 tail->next = gfc_get_formal_arglist ();
9317 tail = tail->next;
9320 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9321 goto cleanup;
9323 tail->sym = fsym;
9324 gfc_set_sym_referenced (fsym);
9327 /* The dummy symbols get cleaned up, when the formal_namespace of the
9328 interface declaration is cleared. This allows us to add the
9329 explicit interface as is done for other type of procedure. */
9330 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9331 &gfc_current_locus))
9332 return MATCH_ERROR;
9334 if (gfc_match_eos () != MATCH_YES)
9336 gfc_syntax_error (ST_MODULE_PROC);
9337 return MATCH_ERROR;
9340 return MATCH_YES;
9342 cleanup:
9343 gfc_free_formal_arglist (head);
9344 return MATCH_ERROR;
9348 /* Match a module procedure statement. Note that we have to modify
9349 symbols in the parent's namespace because the current one was there
9350 to receive symbols that are in an interface's formal argument list. */
9352 match
9353 gfc_match_modproc (void)
9355 char name[GFC_MAX_SYMBOL_LEN + 1];
9356 gfc_symbol *sym;
9357 match m;
9358 locus old_locus;
9359 gfc_namespace *module_ns;
9360 gfc_interface *old_interface_head, *interface;
9362 if (gfc_state_stack->state != COMP_INTERFACE
9363 || gfc_state_stack->previous == NULL
9364 || current_interface.type == INTERFACE_NAMELESS
9365 || current_interface.type == INTERFACE_ABSTRACT)
9367 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9368 "interface");
9369 return MATCH_ERROR;
9372 module_ns = gfc_current_ns->parent;
9373 for (; module_ns; module_ns = module_ns->parent)
9374 if (module_ns->proc_name->attr.flavor == FL_MODULE
9375 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9376 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9377 && !module_ns->proc_name->attr.contained))
9378 break;
9380 if (module_ns == NULL)
9381 return MATCH_ERROR;
9383 /* Store the current state of the interface. We will need it if we
9384 end up with a syntax error and need to recover. */
9385 old_interface_head = gfc_current_interface_head ();
9387 /* Check if the F2008 optional double colon appears. */
9388 gfc_gobble_whitespace ();
9389 old_locus = gfc_current_locus;
9390 if (gfc_match ("::") == MATCH_YES)
9392 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9393 "MODULE PROCEDURE statement at %L", &old_locus))
9394 return MATCH_ERROR;
9396 else
9397 gfc_current_locus = old_locus;
9399 for (;;)
9401 bool last = false;
9402 old_locus = gfc_current_locus;
9404 m = gfc_match_name (name);
9405 if (m == MATCH_NO)
9406 goto syntax;
9407 if (m != MATCH_YES)
9408 return MATCH_ERROR;
9410 /* Check for syntax error before starting to add symbols to the
9411 current namespace. */
9412 if (gfc_match_eos () == MATCH_YES)
9413 last = true;
9415 if (!last && gfc_match_char (',') != MATCH_YES)
9416 goto syntax;
9418 /* Now we're sure the syntax is valid, we process this item
9419 further. */
9420 if (gfc_get_symbol (name, module_ns, &sym))
9421 return MATCH_ERROR;
9423 if (sym->attr.intrinsic)
9425 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9426 "PROCEDURE", &old_locus);
9427 return MATCH_ERROR;
9430 if (sym->attr.proc != PROC_MODULE
9431 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9432 return MATCH_ERROR;
9434 if (!gfc_add_interface (sym))
9435 return MATCH_ERROR;
9437 sym->attr.mod_proc = 1;
9438 sym->declared_at = old_locus;
9440 if (last)
9441 break;
9444 return MATCH_YES;
9446 syntax:
9447 /* Restore the previous state of the interface. */
9448 interface = gfc_current_interface_head ();
9449 gfc_set_current_interface_head (old_interface_head);
9451 /* Free the new interfaces. */
9452 while (interface != old_interface_head)
9454 gfc_interface *i = interface->next;
9455 free (interface);
9456 interface = i;
9459 /* And issue a syntax error. */
9460 gfc_syntax_error (ST_MODULE_PROC);
9461 return MATCH_ERROR;
9465 /* Check a derived type that is being extended. */
9467 static gfc_symbol*
9468 check_extended_derived_type (char *name)
9470 gfc_symbol *extended;
9472 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9474 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9475 return NULL;
9478 extended = gfc_find_dt_in_generic (extended);
9480 /* F08:C428. */
9481 if (!extended)
9483 gfc_error ("Symbol %qs at %C has not been previously defined", name);
9484 return NULL;
9487 if (extended->attr.flavor != FL_DERIVED)
9489 gfc_error ("%qs in EXTENDS expression at %C is not a "
9490 "derived type", name);
9491 return NULL;
9494 if (extended->attr.is_bind_c)
9496 gfc_error ("%qs cannot be extended at %C because it "
9497 "is BIND(C)", extended->name);
9498 return NULL;
9501 if (extended->attr.sequence)
9503 gfc_error ("%qs cannot be extended at %C because it "
9504 "is a SEQUENCE type", extended->name);
9505 return NULL;
9508 return extended;
9512 /* Match the optional attribute specifiers for a type declaration.
9513 Return MATCH_ERROR if an error is encountered in one of the handled
9514 attributes (public, private, bind(c)), MATCH_NO if what's found is
9515 not a handled attribute, and MATCH_YES otherwise. TODO: More error
9516 checking on attribute conflicts needs to be done. */
9518 match
9519 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
9521 /* See if the derived type is marked as private. */
9522 if (gfc_match (" , private") == MATCH_YES)
9524 if (gfc_current_state () != COMP_MODULE)
9526 gfc_error ("Derived type at %C can only be PRIVATE in the "
9527 "specification part of a module");
9528 return MATCH_ERROR;
9531 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
9532 return MATCH_ERROR;
9534 else if (gfc_match (" , public") == MATCH_YES)
9536 if (gfc_current_state () != COMP_MODULE)
9538 gfc_error ("Derived type at %C can only be PUBLIC in the "
9539 "specification part of a module");
9540 return MATCH_ERROR;
9543 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
9544 return MATCH_ERROR;
9546 else if (gfc_match (" , bind ( c )") == MATCH_YES)
9548 /* If the type is defined to be bind(c) it then needs to make
9549 sure that all fields are interoperable. This will
9550 need to be a semantic check on the finished derived type.
9551 See 15.2.3 (lines 9-12) of F2003 draft. */
9552 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
9553 return MATCH_ERROR;
9555 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
9557 else if (gfc_match (" , abstract") == MATCH_YES)
9559 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
9560 return MATCH_ERROR;
9562 if (!gfc_add_abstract (attr, &gfc_current_locus))
9563 return MATCH_ERROR;
9565 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
9567 if (!gfc_add_extension (attr, &gfc_current_locus))
9568 return MATCH_ERROR;
9570 else
9571 return MATCH_NO;
9573 /* If we get here, something matched. */
9574 return MATCH_YES;
9578 /* Common function for type declaration blocks similar to derived types, such
9579 as STRUCTURES and MAPs. Unlike derived types, a structure type
9580 does NOT have a generic symbol matching the name given by the user.
9581 STRUCTUREs can share names with variables and PARAMETERs so we must allow
9582 for the creation of an independent symbol.
9583 Other parameters are a message to prefix errors with, the name of the new
9584 type to be created, and the flavor to add to the resulting symbol. */
9586 static bool
9587 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
9588 gfc_symbol **result)
9590 gfc_symbol *sym;
9591 locus where;
9593 gcc_assert (name[0] == (char) TOUPPER (name[0]));
9595 if (decl)
9596 where = *decl;
9597 else
9598 where = gfc_current_locus;
9600 if (gfc_get_symbol (name, NULL, &sym))
9601 return false;
9603 if (!sym)
9605 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
9606 return false;
9609 if (sym->components != NULL || sym->attr.zero_comp)
9611 gfc_error ("Type definition of %qs at %C was already defined at %L",
9612 sym->name, &sym->declared_at);
9613 return false;
9616 sym->declared_at = where;
9618 if (sym->attr.flavor != fl
9619 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
9620 return false;
9622 if (!sym->hash_value)
9623 /* Set the hash for the compound name for this type. */
9624 sym->hash_value = gfc_hash_value (sym);
9626 /* Normally the type is expected to have been completely parsed by the time
9627 a field declaration with this type is seen. For unions, maps, and nested
9628 structure declarations, we need to indicate that it is okay that we
9629 haven't seen any components yet. This will be updated after the structure
9630 is fully parsed. */
9631 sym->attr.zero_comp = 0;
9633 /* Structures always act like derived-types with the SEQUENCE attribute */
9634 gfc_add_sequence (&sym->attr, sym->name, NULL);
9636 if (result) *result = sym;
9638 return true;
9642 /* Match the opening of a MAP block. Like a struct within a union in C;
9643 behaves identical to STRUCTURE blocks. */
9645 match
9646 gfc_match_map (void)
9648 /* Counter used to give unique internal names to map structures. */
9649 static unsigned int gfc_map_id = 0;
9650 char name[GFC_MAX_SYMBOL_LEN + 1];
9651 gfc_symbol *sym;
9652 locus old_loc;
9654 old_loc = gfc_current_locus;
9656 if (gfc_match_eos () != MATCH_YES)
9658 gfc_error ("Junk after MAP statement at %C");
9659 gfc_current_locus = old_loc;
9660 return MATCH_ERROR;
9663 /* Map blocks are anonymous so we make up unique names for the symbol table
9664 which are invalid Fortran identifiers. */
9665 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
9667 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
9668 return MATCH_ERROR;
9670 gfc_new_block = sym;
9672 return MATCH_YES;
9676 /* Match the opening of a UNION block. */
9678 match
9679 gfc_match_union (void)
9681 /* Counter used to give unique internal names to union types. */
9682 static unsigned int gfc_union_id = 0;
9683 char name[GFC_MAX_SYMBOL_LEN + 1];
9684 gfc_symbol *sym;
9685 locus old_loc;
9687 old_loc = gfc_current_locus;
9689 if (gfc_match_eos () != MATCH_YES)
9691 gfc_error ("Junk after UNION statement at %C");
9692 gfc_current_locus = old_loc;
9693 return MATCH_ERROR;
9696 /* Unions are anonymous so we make up unique names for the symbol table
9697 which are invalid Fortran identifiers. */
9698 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
9700 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
9701 return MATCH_ERROR;
9703 gfc_new_block = sym;
9705 return MATCH_YES;
9709 /* Match the beginning of a STRUCTURE declaration. This is similar to
9710 matching the beginning of a derived type declaration with a few
9711 twists. The resulting type symbol has no access control or other
9712 interesting attributes. */
9714 match
9715 gfc_match_structure_decl (void)
9717 /* Counter used to give unique internal names to anonymous structures. */
9718 static unsigned int gfc_structure_id = 0;
9719 char name[GFC_MAX_SYMBOL_LEN + 1];
9720 gfc_symbol *sym;
9721 match m;
9722 locus where;
9724 if (!flag_dec_structure)
9726 gfc_error ("%s at %C is a DEC extension, enable with "
9727 "%<-fdec-structure%>",
9728 "STRUCTURE");
9729 return MATCH_ERROR;
9732 name[0] = '\0';
9734 m = gfc_match (" /%n/", name);
9735 if (m != MATCH_YES)
9737 /* Non-nested structure declarations require a structure name. */
9738 if (!gfc_comp_struct (gfc_current_state ()))
9740 gfc_error ("Structure name expected in non-nested structure "
9741 "declaration at %C");
9742 return MATCH_ERROR;
9744 /* This is an anonymous structure; make up a unique name for it
9745 (upper-case letters never make it to symbol names from the source).
9746 The important thing is initializing the type variable
9747 and setting gfc_new_symbol, which is immediately used by
9748 parse_structure () and variable_decl () to add components of
9749 this type. */
9750 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
9753 where = gfc_current_locus;
9754 /* No field list allowed after non-nested structure declaration. */
9755 if (!gfc_comp_struct (gfc_current_state ())
9756 && gfc_match_eos () != MATCH_YES)
9758 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
9759 return MATCH_ERROR;
9762 /* Make sure the name is not the name of an intrinsic type. */
9763 if (gfc_is_intrinsic_typename (name))
9765 gfc_error ("Structure name %qs at %C cannot be the same as an"
9766 " intrinsic type", name);
9767 return MATCH_ERROR;
9770 /* Store the actual type symbol for the structure with an upper-case first
9771 letter (an invalid Fortran identifier). */
9773 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
9774 return MATCH_ERROR;
9776 gfc_new_block = sym;
9777 return MATCH_YES;
9781 /* This function does some work to determine which matcher should be used to
9782 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
9783 * as an alias for PRINT from derived type declarations, TYPE IS statements,
9784 * and derived type data declarations. */
9786 match
9787 gfc_match_type (gfc_statement *st)
9789 char name[GFC_MAX_SYMBOL_LEN + 1];
9790 match m;
9791 locus old_loc;
9793 /* Requires -fdec. */
9794 if (!flag_dec)
9795 return MATCH_NO;
9797 m = gfc_match ("type");
9798 if (m != MATCH_YES)
9799 return m;
9800 /* If we already have an error in the buffer, it is probably from failing to
9801 * match a derived type data declaration. Let it happen. */
9802 else if (gfc_error_flag_test ())
9803 return MATCH_NO;
9805 old_loc = gfc_current_locus;
9806 *st = ST_NONE;
9808 /* If we see an attribute list before anything else it's definitely a derived
9809 * type declaration. */
9810 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
9812 gfc_current_locus = old_loc;
9813 *st = ST_DERIVED_DECL;
9814 return gfc_match_derived_decl ();
9817 /* By now "TYPE" has already been matched. If we do not see a name, this may
9818 * be something like "TYPE *" or "TYPE <fmt>". */
9819 m = gfc_match_name (name);
9820 if (m != MATCH_YES)
9822 /* Let print match if it can, otherwise throw an error from
9823 * gfc_match_derived_decl. */
9824 gfc_current_locus = old_loc;
9825 if (gfc_match_print () == MATCH_YES)
9827 *st = ST_WRITE;
9828 return MATCH_YES;
9830 gfc_current_locus = old_loc;
9831 *st = ST_DERIVED_DECL;
9832 return gfc_match_derived_decl ();
9835 /* A derived type declaration requires an EOS. Without it, assume print. */
9836 m = gfc_match_eos ();
9837 if (m == MATCH_NO)
9839 /* Check manually for TYPE IS (... - this is invalid print syntax. */
9840 if (strncmp ("is", name, 3) == 0
9841 && gfc_match (" (", name) == MATCH_YES)
9843 gfc_current_locus = old_loc;
9844 gcc_assert (gfc_match (" is") == MATCH_YES);
9845 *st = ST_TYPE_IS;
9846 return gfc_match_type_is ();
9848 gfc_current_locus = old_loc;
9849 *st = ST_WRITE;
9850 return gfc_match_print ();
9852 else
9854 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
9855 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
9856 * Otherwise if gfc_match_derived_decl fails it's probably an existing
9857 * symbol which can be printed. */
9858 gfc_current_locus = old_loc;
9859 m = gfc_match_derived_decl ();
9860 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
9862 *st = ST_DERIVED_DECL;
9863 return m;
9865 gfc_current_locus = old_loc;
9866 *st = ST_WRITE;
9867 return gfc_match_print ();
9870 return MATCH_NO;
9874 /* Match the beginning of a derived type declaration. If a type name
9875 was the result of a function, then it is possible to have a symbol
9876 already to be known as a derived type yet have no components. */
9878 match
9879 gfc_match_derived_decl (void)
9881 char name[GFC_MAX_SYMBOL_LEN + 1];
9882 char parent[GFC_MAX_SYMBOL_LEN + 1];
9883 symbol_attribute attr;
9884 gfc_symbol *sym, *gensym;
9885 gfc_symbol *extended;
9886 match m;
9887 match is_type_attr_spec = MATCH_NO;
9888 bool seen_attr = false;
9889 gfc_interface *intr = NULL, *head;
9890 bool parameterized_type = false;
9891 bool seen_colons = false;
9893 if (gfc_comp_struct (gfc_current_state ()))
9894 return MATCH_NO;
9896 name[0] = '\0';
9897 parent[0] = '\0';
9898 gfc_clear_attr (&attr);
9899 extended = NULL;
9903 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
9904 if (is_type_attr_spec == MATCH_ERROR)
9905 return MATCH_ERROR;
9906 if (is_type_attr_spec == MATCH_YES)
9907 seen_attr = true;
9908 } while (is_type_attr_spec == MATCH_YES);
9910 /* Deal with derived type extensions. The extension attribute has
9911 been added to 'attr' but now the parent type must be found and
9912 checked. */
9913 if (parent[0])
9914 extended = check_extended_derived_type (parent);
9916 if (parent[0] && !extended)
9917 return MATCH_ERROR;
9919 m = gfc_match (" ::");
9920 if (m == MATCH_YES)
9922 seen_colons = true;
9924 else if (seen_attr)
9926 gfc_error ("Expected :: in TYPE definition at %C");
9927 return MATCH_ERROR;
9930 m = gfc_match (" %n ", name);
9931 if (m != MATCH_YES)
9932 return m;
9934 /* Make sure that we don't identify TYPE IS (...) as a parameterized
9935 derived type named 'is'.
9936 TODO Expand the check, when 'name' = "is" by matching " (tname) "
9937 and checking if this is a(n intrinsic) typename. his picks up
9938 misplaced TYPE IS statements such as in select_type_1.f03. */
9939 if (gfc_peek_ascii_char () == '(')
9941 if (gfc_current_state () == COMP_SELECT_TYPE
9942 || (!seen_colons && !strcmp (name, "is")))
9943 return MATCH_NO;
9944 parameterized_type = true;
9947 m = gfc_match_eos ();
9948 if (m != MATCH_YES && !parameterized_type)
9949 return m;
9951 /* Make sure the name is not the name of an intrinsic type. */
9952 if (gfc_is_intrinsic_typename (name))
9954 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
9955 "type", name);
9956 return MATCH_ERROR;
9959 if (gfc_get_symbol (name, NULL, &gensym))
9960 return MATCH_ERROR;
9962 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
9964 gfc_error ("Derived type name %qs at %C already has a basic type "
9965 "of %s", gensym->name, gfc_typename (&gensym->ts));
9966 return MATCH_ERROR;
9969 if (!gensym->attr.generic
9970 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
9971 return MATCH_ERROR;
9973 if (!gensym->attr.function
9974 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
9975 return MATCH_ERROR;
9977 sym = gfc_find_dt_in_generic (gensym);
9979 if (sym && (sym->components != NULL || sym->attr.zero_comp))
9981 gfc_error ("Derived type definition of %qs at %C has already been "
9982 "defined", sym->name);
9983 return MATCH_ERROR;
9986 if (!sym)
9988 /* Use upper case to save the actual derived-type symbol. */
9989 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
9990 sym->name = gfc_get_string ("%s", gensym->name);
9991 head = gensym->generic;
9992 intr = gfc_get_interface ();
9993 intr->sym = sym;
9994 intr->where = gfc_current_locus;
9995 intr->sym->declared_at = gfc_current_locus;
9996 intr->next = head;
9997 gensym->generic = intr;
9998 gensym->attr.if_source = IFSRC_DECL;
10001 /* The symbol may already have the derived attribute without the
10002 components. The ways this can happen is via a function
10003 definition, an INTRINSIC statement or a subtype in another
10004 derived type that is a pointer. The first part of the AND clause
10005 is true if the symbol is not the return value of a function. */
10006 if (sym->attr.flavor != FL_DERIVED
10007 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
10008 return MATCH_ERROR;
10010 if (attr.access != ACCESS_UNKNOWN
10011 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
10012 return MATCH_ERROR;
10013 else if (sym->attr.access == ACCESS_UNKNOWN
10014 && gensym->attr.access != ACCESS_UNKNOWN
10015 && !gfc_add_access (&sym->attr, gensym->attr.access,
10016 sym->name, NULL))
10017 return MATCH_ERROR;
10019 if (sym->attr.access != ACCESS_UNKNOWN
10020 && gensym->attr.access == ACCESS_UNKNOWN)
10021 gensym->attr.access = sym->attr.access;
10023 /* See if the derived type was labeled as bind(c). */
10024 if (attr.is_bind_c != 0)
10025 sym->attr.is_bind_c = attr.is_bind_c;
10027 /* Construct the f2k_derived namespace if it is not yet there. */
10028 if (!sym->f2k_derived)
10029 sym->f2k_derived = gfc_get_namespace (NULL, 0);
10031 if (parameterized_type)
10033 /* Ignore error or mismatches by going to the end of the statement
10034 in order to avoid the component declarations causing problems. */
10035 m = gfc_match_formal_arglist (sym, 0, 0, true);
10036 if (m != MATCH_YES)
10037 gfc_error_recovery ();
10038 m = gfc_match_eos ();
10039 if (m != MATCH_YES)
10041 gfc_error_recovery ();
10042 gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10044 sym->attr.pdt_template = 1;
10047 if (extended && !sym->components)
10049 gfc_component *p;
10050 gfc_formal_arglist *f, *g, *h;
10052 /* Add the extended derived type as the first component. */
10053 gfc_add_component (sym, parent, &p);
10054 extended->refs++;
10055 gfc_set_sym_referenced (extended);
10057 p->ts.type = BT_DERIVED;
10058 p->ts.u.derived = extended;
10059 p->initializer = gfc_default_initializer (&p->ts);
10061 /* Set extension level. */
10062 if (extended->attr.extension == 255)
10064 /* Since the extension field is 8 bit wide, we can only have
10065 up to 255 extension levels. */
10066 gfc_error ("Maximum extension level reached with type %qs at %L",
10067 extended->name, &extended->declared_at);
10068 return MATCH_ERROR;
10070 sym->attr.extension = extended->attr.extension + 1;
10072 /* Provide the links between the extended type and its extension. */
10073 if (!extended->f2k_derived)
10074 extended->f2k_derived = gfc_get_namespace (NULL, 0);
10076 /* Copy the extended type-param-name-list from the extended type,
10077 append those of the extension and add the whole lot to the
10078 extension. */
10079 if (extended->attr.pdt_template)
10081 g = h = NULL;
10082 sym->attr.pdt_template = 1;
10083 for (f = extended->formal; f; f = f->next)
10085 if (f == extended->formal)
10087 g = gfc_get_formal_arglist ();
10088 h = g;
10090 else
10092 g->next = gfc_get_formal_arglist ();
10093 g = g->next;
10095 g->sym = f->sym;
10097 g->next = sym->formal;
10098 sym->formal = h;
10102 if (!sym->hash_value)
10103 /* Set the hash for the compound name for this type. */
10104 sym->hash_value = gfc_hash_value (sym);
10106 /* Take over the ABSTRACT attribute. */
10107 sym->attr.abstract = attr.abstract;
10109 gfc_new_block = sym;
10111 return MATCH_YES;
10115 /* Cray Pointees can be declared as:
10116 pointer (ipt, a (n,m,...,*)) */
10118 match
10119 gfc_mod_pointee_as (gfc_array_spec *as)
10121 as->cray_pointee = true; /* This will be useful to know later. */
10122 if (as->type == AS_ASSUMED_SIZE)
10123 as->cp_was_assumed = true;
10124 else if (as->type == AS_ASSUMED_SHAPE)
10126 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10127 return MATCH_ERROR;
10129 return MATCH_YES;
10133 /* Match the enum definition statement, here we are trying to match
10134 the first line of enum definition statement.
10135 Returns MATCH_YES if match is found. */
10137 match
10138 gfc_match_enum (void)
10140 match m;
10142 m = gfc_match_eos ();
10143 if (m != MATCH_YES)
10144 return m;
10146 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
10147 return MATCH_ERROR;
10149 return MATCH_YES;
10153 /* Returns an initializer whose value is one higher than the value of the
10154 LAST_INITIALIZER argument. If the argument is NULL, the
10155 initializers value will be set to zero. The initializer's kind
10156 will be set to gfc_c_int_kind.
10158 If -fshort-enums is given, the appropriate kind will be selected
10159 later after all enumerators have been parsed. A warning is issued
10160 here if an initializer exceeds gfc_c_int_kind. */
10162 static gfc_expr *
10163 enum_initializer (gfc_expr *last_initializer, locus where)
10165 gfc_expr *result;
10166 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
10168 mpz_init (result->value.integer);
10170 if (last_initializer != NULL)
10172 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10173 result->where = last_initializer->where;
10175 if (gfc_check_integer_range (result->value.integer,
10176 gfc_c_int_kind) != ARITH_OK)
10178 gfc_error ("Enumerator exceeds the C integer type at %C");
10179 return NULL;
10182 else
10184 /* Control comes here, if it's the very first enumerator and no
10185 initializer has been given. It will be initialized to zero. */
10186 mpz_set_si (result->value.integer, 0);
10189 return result;
10193 /* Match a variable name with an optional initializer. When this
10194 subroutine is called, a variable is expected to be parsed next.
10195 Depending on what is happening at the moment, updates either the
10196 symbol table or the current interface. */
10198 static match
10199 enumerator_decl (void)
10201 char name[GFC_MAX_SYMBOL_LEN + 1];
10202 gfc_expr *initializer;
10203 gfc_array_spec *as = NULL;
10204 gfc_symbol *sym;
10205 locus var_locus;
10206 match m;
10207 bool t;
10208 locus old_locus;
10210 initializer = NULL;
10211 old_locus = gfc_current_locus;
10213 /* When we get here, we've just matched a list of attributes and
10214 maybe a type and a double colon. The next thing we expect to see
10215 is the name of the symbol. */
10216 m = gfc_match_name (name);
10217 if (m != MATCH_YES)
10218 goto cleanup;
10220 var_locus = gfc_current_locus;
10222 /* OK, we've successfully matched the declaration. Now put the
10223 symbol in the current namespace. If we fail to create the symbol,
10224 bail out. */
10225 if (!build_sym (name, NULL, false, &as, &var_locus))
10227 m = MATCH_ERROR;
10228 goto cleanup;
10231 /* The double colon must be present in order to have initializers.
10232 Otherwise the statement is ambiguous with an assignment statement. */
10233 if (colon_seen)
10235 if (gfc_match_char ('=') == MATCH_YES)
10237 m = gfc_match_init_expr (&initializer);
10238 if (m == MATCH_NO)
10240 gfc_error ("Expected an initialization expression at %C");
10241 m = MATCH_ERROR;
10244 if (m != MATCH_YES)
10245 goto cleanup;
10249 /* If we do not have an initializer, the initialization value of the
10250 previous enumerator (stored in last_initializer) is incremented
10251 by 1 and is used to initialize the current enumerator. */
10252 if (initializer == NULL)
10253 initializer = enum_initializer (last_initializer, old_locus);
10255 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10257 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10258 &var_locus);
10259 m = MATCH_ERROR;
10260 goto cleanup;
10263 /* Store this current initializer, for the next enumerator variable
10264 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10265 use last_initializer below. */
10266 last_initializer = initializer;
10267 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10269 /* Maintain enumerator history. */
10270 gfc_find_symbol (name, NULL, 0, &sym);
10271 create_enum_history (sym, last_initializer);
10273 return (t) ? MATCH_YES : MATCH_ERROR;
10275 cleanup:
10276 /* Free stuff up and return. */
10277 gfc_free_expr (initializer);
10279 return m;
10283 /* Match the enumerator definition statement. */
10285 match
10286 gfc_match_enumerator_def (void)
10288 match m;
10289 bool t;
10291 gfc_clear_ts (&current_ts);
10293 m = gfc_match (" enumerator");
10294 if (m != MATCH_YES)
10295 return m;
10297 m = gfc_match (" :: ");
10298 if (m == MATCH_ERROR)
10299 return m;
10301 colon_seen = (m == MATCH_YES);
10303 if (gfc_current_state () != COMP_ENUM)
10305 gfc_error ("ENUM definition statement expected before %C");
10306 gfc_free_enum_history ();
10307 return MATCH_ERROR;
10310 (&current_ts)->type = BT_INTEGER;
10311 (&current_ts)->kind = gfc_c_int_kind;
10313 gfc_clear_attr (&current_attr);
10314 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10315 if (!t)
10317 m = MATCH_ERROR;
10318 goto cleanup;
10321 for (;;)
10323 m = enumerator_decl ();
10324 if (m == MATCH_ERROR)
10326 gfc_free_enum_history ();
10327 goto cleanup;
10329 if (m == MATCH_NO)
10330 break;
10332 if (gfc_match_eos () == MATCH_YES)
10333 goto cleanup;
10334 if (gfc_match_char (',') != MATCH_YES)
10335 break;
10338 if (gfc_current_state () == COMP_ENUM)
10340 gfc_free_enum_history ();
10341 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10342 m = MATCH_ERROR;
10345 cleanup:
10346 gfc_free_array_spec (current_as);
10347 current_as = NULL;
10348 return m;
10353 /* Match binding attributes. */
10355 static match
10356 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10358 bool found_passing = false;
10359 bool seen_ptr = false;
10360 match m = MATCH_YES;
10362 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
10363 this case the defaults are in there. */
10364 ba->access = ACCESS_UNKNOWN;
10365 ba->pass_arg = NULL;
10366 ba->pass_arg_num = 0;
10367 ba->nopass = 0;
10368 ba->non_overridable = 0;
10369 ba->deferred = 0;
10370 ba->ppc = ppc;
10372 /* If we find a comma, we believe there are binding attributes. */
10373 m = gfc_match_char (',');
10374 if (m == MATCH_NO)
10375 goto done;
10379 /* Access specifier. */
10381 m = gfc_match (" public");
10382 if (m == MATCH_ERROR)
10383 goto error;
10384 if (m == MATCH_YES)
10386 if (ba->access != ACCESS_UNKNOWN)
10388 gfc_error ("Duplicate access-specifier at %C");
10389 goto error;
10392 ba->access = ACCESS_PUBLIC;
10393 continue;
10396 m = gfc_match (" private");
10397 if (m == MATCH_ERROR)
10398 goto error;
10399 if (m == MATCH_YES)
10401 if (ba->access != ACCESS_UNKNOWN)
10403 gfc_error ("Duplicate access-specifier at %C");
10404 goto error;
10407 ba->access = ACCESS_PRIVATE;
10408 continue;
10411 /* If inside GENERIC, the following is not allowed. */
10412 if (!generic)
10415 /* NOPASS flag. */
10416 m = gfc_match (" nopass");
10417 if (m == MATCH_ERROR)
10418 goto error;
10419 if (m == MATCH_YES)
10421 if (found_passing)
10423 gfc_error ("Binding attributes already specify passing,"
10424 " illegal NOPASS at %C");
10425 goto error;
10428 found_passing = true;
10429 ba->nopass = 1;
10430 continue;
10433 /* PASS possibly including argument. */
10434 m = gfc_match (" pass");
10435 if (m == MATCH_ERROR)
10436 goto error;
10437 if (m == MATCH_YES)
10439 char arg[GFC_MAX_SYMBOL_LEN + 1];
10441 if (found_passing)
10443 gfc_error ("Binding attributes already specify passing,"
10444 " illegal PASS at %C");
10445 goto error;
10448 m = gfc_match (" ( %n )", arg);
10449 if (m == MATCH_ERROR)
10450 goto error;
10451 if (m == MATCH_YES)
10452 ba->pass_arg = gfc_get_string ("%s", arg);
10453 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10455 found_passing = true;
10456 ba->nopass = 0;
10457 continue;
10460 if (ppc)
10462 /* POINTER flag. */
10463 m = gfc_match (" pointer");
10464 if (m == MATCH_ERROR)
10465 goto error;
10466 if (m == MATCH_YES)
10468 if (seen_ptr)
10470 gfc_error ("Duplicate POINTER attribute at %C");
10471 goto error;
10474 seen_ptr = true;
10475 continue;
10478 else
10480 /* NON_OVERRIDABLE flag. */
10481 m = gfc_match (" non_overridable");
10482 if (m == MATCH_ERROR)
10483 goto error;
10484 if (m == MATCH_YES)
10486 if (ba->non_overridable)
10488 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
10489 goto error;
10492 ba->non_overridable = 1;
10493 continue;
10496 /* DEFERRED flag. */
10497 m = gfc_match (" deferred");
10498 if (m == MATCH_ERROR)
10499 goto error;
10500 if (m == MATCH_YES)
10502 if (ba->deferred)
10504 gfc_error ("Duplicate DEFERRED at %C");
10505 goto error;
10508 ba->deferred = 1;
10509 continue;
10515 /* Nothing matching found. */
10516 if (generic)
10517 gfc_error ("Expected access-specifier at %C");
10518 else
10519 gfc_error ("Expected binding attribute at %C");
10520 goto error;
10522 while (gfc_match_char (',') == MATCH_YES);
10524 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
10525 if (ba->non_overridable && ba->deferred)
10527 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
10528 goto error;
10531 m = MATCH_YES;
10533 done:
10534 if (ba->access == ACCESS_UNKNOWN)
10535 ba->access = gfc_typebound_default_access;
10537 if (ppc && !seen_ptr)
10539 gfc_error ("POINTER attribute is required for procedure pointer component"
10540 " at %C");
10541 goto error;
10544 return m;
10546 error:
10547 return MATCH_ERROR;
10551 /* Match a PROCEDURE specific binding inside a derived type. */
10553 static match
10554 match_procedure_in_type (void)
10556 char name[GFC_MAX_SYMBOL_LEN + 1];
10557 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
10558 char* target = NULL, *ifc = NULL;
10559 gfc_typebound_proc tb;
10560 bool seen_colons;
10561 bool seen_attrs;
10562 match m;
10563 gfc_symtree* stree;
10564 gfc_namespace* ns;
10565 gfc_symbol* block;
10566 int num;
10568 /* Check current state. */
10569 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
10570 block = gfc_state_stack->previous->sym;
10571 gcc_assert (block);
10573 /* Try to match PROCEDURE(interface). */
10574 if (gfc_match (" (") == MATCH_YES)
10576 m = gfc_match_name (target_buf);
10577 if (m == MATCH_ERROR)
10578 return m;
10579 if (m != MATCH_YES)
10581 gfc_error ("Interface-name expected after %<(%> at %C");
10582 return MATCH_ERROR;
10585 if (gfc_match (" )") != MATCH_YES)
10587 gfc_error ("%<)%> expected at %C");
10588 return MATCH_ERROR;
10591 ifc = target_buf;
10594 /* Construct the data structure. */
10595 memset (&tb, 0, sizeof (tb));
10596 tb.where = gfc_current_locus;
10598 /* Match binding attributes. */
10599 m = match_binding_attributes (&tb, false, false);
10600 if (m == MATCH_ERROR)
10601 return m;
10602 seen_attrs = (m == MATCH_YES);
10604 /* Check that attribute DEFERRED is given if an interface is specified. */
10605 if (tb.deferred && !ifc)
10607 gfc_error ("Interface must be specified for DEFERRED binding at %C");
10608 return MATCH_ERROR;
10610 if (ifc && !tb.deferred)
10612 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
10613 return MATCH_ERROR;
10616 /* Match the colons. */
10617 m = gfc_match (" ::");
10618 if (m == MATCH_ERROR)
10619 return m;
10620 seen_colons = (m == MATCH_YES);
10621 if (seen_attrs && !seen_colons)
10623 gfc_error ("Expected %<::%> after binding-attributes at %C");
10624 return MATCH_ERROR;
10627 /* Match the binding names. */
10628 for(num=1;;num++)
10630 m = gfc_match_name (name);
10631 if (m == MATCH_ERROR)
10632 return m;
10633 if (m == MATCH_NO)
10635 gfc_error ("Expected binding name at %C");
10636 return MATCH_ERROR;
10639 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
10640 return MATCH_ERROR;
10642 /* Try to match the '=> target', if it's there. */
10643 target = ifc;
10644 m = gfc_match (" =>");
10645 if (m == MATCH_ERROR)
10646 return m;
10647 if (m == MATCH_YES)
10649 if (tb.deferred)
10651 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
10652 return MATCH_ERROR;
10655 if (!seen_colons)
10657 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
10658 " at %C");
10659 return MATCH_ERROR;
10662 m = gfc_match_name (target_buf);
10663 if (m == MATCH_ERROR)
10664 return m;
10665 if (m == MATCH_NO)
10667 gfc_error ("Expected binding target after %<=>%> at %C");
10668 return MATCH_ERROR;
10670 target = target_buf;
10673 /* If no target was found, it has the same name as the binding. */
10674 if (!target)
10675 target = name;
10677 /* Get the namespace to insert the symbols into. */
10678 ns = block->f2k_derived;
10679 gcc_assert (ns);
10681 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
10682 if (tb.deferred && !block->attr.abstract)
10684 gfc_error ("Type %qs containing DEFERRED binding at %C "
10685 "is not ABSTRACT", block->name);
10686 return MATCH_ERROR;
10689 /* See if we already have a binding with this name in the symtree which
10690 would be an error. If a GENERIC already targeted this binding, it may
10691 be already there but then typebound is still NULL. */
10692 stree = gfc_find_symtree (ns->tb_sym_root, name);
10693 if (stree && stree->n.tb)
10695 gfc_error ("There is already a procedure with binding name %qs for "
10696 "the derived type %qs at %C", name, block->name);
10697 return MATCH_ERROR;
10700 /* Insert it and set attributes. */
10702 if (!stree)
10704 stree = gfc_new_symtree (&ns->tb_sym_root, name);
10705 gcc_assert (stree);
10707 stree->n.tb = gfc_get_typebound_proc (&tb);
10709 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
10710 false))
10711 return MATCH_ERROR;
10712 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
10713 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
10714 target, &stree->n.tb->u.specific->n.sym->declared_at);
10716 if (gfc_match_eos () == MATCH_YES)
10717 return MATCH_YES;
10718 if (gfc_match_char (',') != MATCH_YES)
10719 goto syntax;
10722 syntax:
10723 gfc_error ("Syntax error in PROCEDURE statement at %C");
10724 return MATCH_ERROR;
10728 /* Match a GENERIC procedure binding inside a derived type. */
10730 match
10731 gfc_match_generic (void)
10733 char name[GFC_MAX_SYMBOL_LEN + 1];
10734 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
10735 gfc_symbol* block;
10736 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
10737 gfc_typebound_proc* tb;
10738 gfc_namespace* ns;
10739 interface_type op_type;
10740 gfc_intrinsic_op op;
10741 match m;
10743 /* Check current state. */
10744 if (gfc_current_state () == COMP_DERIVED)
10746 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
10747 return MATCH_ERROR;
10749 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
10750 return MATCH_NO;
10751 block = gfc_state_stack->previous->sym;
10752 ns = block->f2k_derived;
10753 gcc_assert (block && ns);
10755 memset (&tbattr, 0, sizeof (tbattr));
10756 tbattr.where = gfc_current_locus;
10758 /* See if we get an access-specifier. */
10759 m = match_binding_attributes (&tbattr, true, false);
10760 if (m == MATCH_ERROR)
10761 goto error;
10763 /* Now the colons, those are required. */
10764 if (gfc_match (" ::") != MATCH_YES)
10766 gfc_error ("Expected %<::%> at %C");
10767 goto error;
10770 /* Match the binding name; depending on type (operator / generic) format
10771 it for future error messages into bind_name. */
10773 m = gfc_match_generic_spec (&op_type, name, &op);
10774 if (m == MATCH_ERROR)
10775 return MATCH_ERROR;
10776 if (m == MATCH_NO)
10778 gfc_error ("Expected generic name or operator descriptor at %C");
10779 goto error;
10782 switch (op_type)
10784 case INTERFACE_GENERIC:
10785 case INTERFACE_DTIO:
10786 snprintf (bind_name, sizeof (bind_name), "%s", name);
10787 break;
10789 case INTERFACE_USER_OP:
10790 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
10791 break;
10793 case INTERFACE_INTRINSIC_OP:
10794 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
10795 gfc_op2string (op));
10796 break;
10798 case INTERFACE_NAMELESS:
10799 gfc_error ("Malformed GENERIC statement at %C");
10800 goto error;
10801 break;
10803 default:
10804 gcc_unreachable ();
10807 /* Match the required =>. */
10808 if (gfc_match (" =>") != MATCH_YES)
10810 gfc_error ("Expected %<=>%> at %C");
10811 goto error;
10814 /* Try to find existing GENERIC binding with this name / for this operator;
10815 if there is something, check that it is another GENERIC and then extend
10816 it rather than building a new node. Otherwise, create it and put it
10817 at the right position. */
10819 switch (op_type)
10821 case INTERFACE_DTIO:
10822 case INTERFACE_USER_OP:
10823 case INTERFACE_GENERIC:
10825 const bool is_op = (op_type == INTERFACE_USER_OP);
10826 gfc_symtree* st;
10828 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
10829 tb = st ? st->n.tb : NULL;
10830 break;
10833 case INTERFACE_INTRINSIC_OP:
10834 tb = ns->tb_op[op];
10835 break;
10837 default:
10838 gcc_unreachable ();
10841 if (tb)
10843 if (!tb->is_generic)
10845 gcc_assert (op_type == INTERFACE_GENERIC);
10846 gfc_error ("There's already a non-generic procedure with binding name"
10847 " %qs for the derived type %qs at %C",
10848 bind_name, block->name);
10849 goto error;
10852 if (tb->access != tbattr.access)
10854 gfc_error ("Binding at %C must have the same access as already"
10855 " defined binding %qs", bind_name);
10856 goto error;
10859 else
10861 tb = gfc_get_typebound_proc (NULL);
10862 tb->where = gfc_current_locus;
10863 tb->access = tbattr.access;
10864 tb->is_generic = 1;
10865 tb->u.generic = NULL;
10867 switch (op_type)
10869 case INTERFACE_DTIO:
10870 case INTERFACE_GENERIC:
10871 case INTERFACE_USER_OP:
10873 const bool is_op = (op_type == INTERFACE_USER_OP);
10874 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
10875 &ns->tb_sym_root, name);
10876 gcc_assert (st);
10877 st->n.tb = tb;
10879 break;
10882 case INTERFACE_INTRINSIC_OP:
10883 ns->tb_op[op] = tb;
10884 break;
10886 default:
10887 gcc_unreachable ();
10891 /* Now, match all following names as specific targets. */
10894 gfc_symtree* target_st;
10895 gfc_tbp_generic* target;
10897 m = gfc_match_name (name);
10898 if (m == MATCH_ERROR)
10899 goto error;
10900 if (m == MATCH_NO)
10902 gfc_error ("Expected specific binding name at %C");
10903 goto error;
10906 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
10908 /* See if this is a duplicate specification. */
10909 for (target = tb->u.generic; target; target = target->next)
10910 if (target_st == target->specific_st)
10912 gfc_error ("%qs already defined as specific binding for the"
10913 " generic %qs at %C", name, bind_name);
10914 goto error;
10917 target = gfc_get_tbp_generic ();
10918 target->specific_st = target_st;
10919 target->specific = NULL;
10920 target->next = tb->u.generic;
10921 target->is_operator = ((op_type == INTERFACE_USER_OP)
10922 || (op_type == INTERFACE_INTRINSIC_OP));
10923 tb->u.generic = target;
10925 while (gfc_match (" ,") == MATCH_YES);
10927 /* Here should be the end. */
10928 if (gfc_match_eos () != MATCH_YES)
10930 gfc_error ("Junk after GENERIC binding at %C");
10931 goto error;
10934 return MATCH_YES;
10936 error:
10937 return MATCH_ERROR;
10941 /* Match a FINAL declaration inside a derived type. */
10943 match
10944 gfc_match_final_decl (void)
10946 char name[GFC_MAX_SYMBOL_LEN + 1];
10947 gfc_symbol* sym;
10948 match m;
10949 gfc_namespace* module_ns;
10950 bool first, last;
10951 gfc_symbol* block;
10953 if (gfc_current_form == FORM_FREE)
10955 char c = gfc_peek_ascii_char ();
10956 if (!gfc_is_whitespace (c) && c != ':')
10957 return MATCH_NO;
10960 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
10962 if (gfc_current_form == FORM_FIXED)
10963 return MATCH_NO;
10965 gfc_error ("FINAL declaration at %C must be inside a derived type "
10966 "CONTAINS section");
10967 return MATCH_ERROR;
10970 block = gfc_state_stack->previous->sym;
10971 gcc_assert (block);
10973 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
10974 || gfc_state_stack->previous->previous->state != COMP_MODULE)
10976 gfc_error ("Derived type declaration with FINAL at %C must be in the"
10977 " specification part of a MODULE");
10978 return MATCH_ERROR;
10981 module_ns = gfc_current_ns;
10982 gcc_assert (module_ns);
10983 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
10985 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
10986 if (gfc_match (" ::") == MATCH_ERROR)
10987 return MATCH_ERROR;
10989 /* Match the sequence of procedure names. */
10990 first = true;
10991 last = false;
10994 gfc_finalizer* f;
10996 if (first && gfc_match_eos () == MATCH_YES)
10998 gfc_error ("Empty FINAL at %C");
10999 return MATCH_ERROR;
11002 m = gfc_match_name (name);
11003 if (m == MATCH_NO)
11005 gfc_error ("Expected module procedure name at %C");
11006 return MATCH_ERROR;
11008 else if (m != MATCH_YES)
11009 return MATCH_ERROR;
11011 if (gfc_match_eos () == MATCH_YES)
11012 last = true;
11013 if (!last && gfc_match_char (',') != MATCH_YES)
11015 gfc_error ("Expected %<,%> at %C");
11016 return MATCH_ERROR;
11019 if (gfc_get_symbol (name, module_ns, &sym))
11021 gfc_error ("Unknown procedure name %qs at %C", name);
11022 return MATCH_ERROR;
11025 /* Mark the symbol as module procedure. */
11026 if (sym->attr.proc != PROC_MODULE
11027 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
11028 return MATCH_ERROR;
11030 /* Check if we already have this symbol in the list, this is an error. */
11031 for (f = block->f2k_derived->finalizers; f; f = f->next)
11032 if (f->proc_sym == sym)
11034 gfc_error ("%qs at %C is already defined as FINAL procedure",
11035 name);
11036 return MATCH_ERROR;
11039 /* Add this symbol to the list of finalizers. */
11040 gcc_assert (block->f2k_derived);
11041 sym->refs++;
11042 f = XCNEW (gfc_finalizer);
11043 f->proc_sym = sym;
11044 f->proc_tree = NULL;
11045 f->where = gfc_current_locus;
11046 f->next = block->f2k_derived->finalizers;
11047 block->f2k_derived->finalizers = f;
11049 first = false;
11051 while (!last);
11053 return MATCH_YES;
11057 const ext_attr_t ext_attr_list[] = {
11058 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
11059 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
11060 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
11061 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
11062 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
11063 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
11064 { NULL, EXT_ATTR_LAST, NULL }
11067 /* Match a !GCC$ ATTRIBUTES statement of the form:
11068 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11069 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11071 TODO: We should support all GCC attributes using the same syntax for
11072 the attribute list, i.e. the list in C
11073 __attributes(( attribute-list ))
11074 matches then
11075 !GCC$ ATTRIBUTES attribute-list ::
11076 Cf. c-parser.c's c_parser_attributes; the data can then directly be
11077 saved into a TREE.
11079 As there is absolutely no risk of confusion, we should never return
11080 MATCH_NO. */
11081 match
11082 gfc_match_gcc_attributes (void)
11084 symbol_attribute attr;
11085 char name[GFC_MAX_SYMBOL_LEN + 1];
11086 unsigned id;
11087 gfc_symbol *sym;
11088 match m;
11090 gfc_clear_attr (&attr);
11091 for(;;)
11093 char ch;
11095 if (gfc_match_name (name) != MATCH_YES)
11096 return MATCH_ERROR;
11098 for (id = 0; id < EXT_ATTR_LAST; id++)
11099 if (strcmp (name, ext_attr_list[id].name) == 0)
11100 break;
11102 if (id == EXT_ATTR_LAST)
11104 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11105 return MATCH_ERROR;
11108 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
11109 return MATCH_ERROR;
11111 gfc_gobble_whitespace ();
11112 ch = gfc_next_ascii_char ();
11113 if (ch == ':')
11115 /* This is the successful exit condition for the loop. */
11116 if (gfc_next_ascii_char () == ':')
11117 break;
11120 if (ch == ',')
11121 continue;
11123 goto syntax;
11126 if (gfc_match_eos () == MATCH_YES)
11127 goto syntax;
11129 for(;;)
11131 m = gfc_match_name (name);
11132 if (m != MATCH_YES)
11133 return m;
11135 if (find_special (name, &sym, true))
11136 return MATCH_ERROR;
11138 sym->attr.ext_attr |= attr.ext_attr;
11140 if (gfc_match_eos () == MATCH_YES)
11141 break;
11143 if (gfc_match_char (',') != MATCH_YES)
11144 goto syntax;
11147 return MATCH_YES;
11149 syntax:
11150 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11151 return MATCH_ERROR;
11155 /* Match a !GCC$ UNROLL statement of the form:
11156 !GCC$ UNROLL n
11158 The parameter n is the number of times we are supposed to unroll.
11160 When we come here, we have already matched the !GCC$ UNROLL string. */
11161 match
11162 gfc_match_gcc_unroll (void)
11164 int value;
11166 if (gfc_match_small_int (&value) == MATCH_YES)
11168 if (value < 0 || value > USHRT_MAX)
11170 gfc_error ("%<GCC unroll%> directive requires a"
11171 " non-negative integral constant"
11172 " less than or equal to %u at %C",
11173 USHRT_MAX
11175 return MATCH_ERROR;
11177 if (gfc_match_eos () == MATCH_YES)
11179 directive_unroll = value == 0 ? 1 : value;
11180 return MATCH_YES;
11184 gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11185 return MATCH_ERROR;