Fix PR 93568 (thinko)
[official-gcc.git] / gcc / fortran / decl.c
blob499d2429aba652fc8dbf5a5745ec8f030aaafa43
1 /* Declaration statement matcher
2 Copyright (C) 2002-2020 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"
31 #include "target.h"
33 /* Macros to access allocate memory for gfc_data_variable,
34 gfc_data_value and gfc_data. */
35 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
36 #define gfc_get_data_value() XCNEW (gfc_data_value)
37 #define gfc_get_data() XCNEW (gfc_data)
40 static bool set_binding_label (const char **, const char *, int);
43 /* This flag is set if an old-style length selector is matched
44 during a type-declaration statement. */
46 static int old_char_selector;
48 /* When variables acquire types and attributes from a declaration
49 statement, they get them from the following static variables. The
50 first part of a declaration sets these variables and the second
51 part copies these into symbol structures. */
53 static gfc_typespec current_ts;
55 static symbol_attribute current_attr;
56 static gfc_array_spec *current_as;
57 static int colon_seen;
58 static int attr_seen;
60 /* The current binding label (if any). */
61 static const char* curr_binding_label;
62 /* Need to know how many identifiers are on the current data declaration
63 line in case we're given the BIND(C) attribute with a NAME= specifier. */
64 static int num_idents_on_line;
65 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
66 can supply a name if the curr_binding_label is nil and NAME= was not. */
67 static int has_name_equals = 0;
69 /* Initializer of the previous enumerator. */
71 static gfc_expr *last_initializer;
73 /* History of all the enumerators is maintained, so that
74 kind values of all the enumerators could be updated depending
75 upon the maximum initialized value. */
77 typedef struct enumerator_history
79 gfc_symbol *sym;
80 gfc_expr *initializer;
81 struct enumerator_history *next;
83 enumerator_history;
85 /* Header of enum history chain. */
87 static enumerator_history *enum_history = NULL;
89 /* Pointer of enum history node containing largest initializer. */
91 static enumerator_history *max_enum = NULL;
93 /* gfc_new_block points to the symbol of a newly matched block. */
95 gfc_symbol *gfc_new_block;
97 bool gfc_matching_function;
99 /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
100 int directive_unroll = -1;
102 /* Set upon parsing supported !GCC$ pragmas for use in the next loop. */
103 bool directive_ivdep = false;
104 bool directive_vector = false;
105 bool directive_novector = false;
107 /* Map of middle-end built-ins that should be vectorized. */
108 hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
110 /* If a kind expression of a component of a parameterized derived type is
111 parameterized, temporarily store the expression here. */
112 static gfc_expr *saved_kind_expr = NULL;
114 /* Used to store the parameter list arising in a PDT declaration and
115 in the typespec of a PDT variable or component. */
116 static gfc_actual_arglist *decl_type_param_list;
117 static gfc_actual_arglist *type_param_spec_list;
119 /********************* DATA statement subroutines *********************/
121 static bool in_match_data = false;
123 bool
124 gfc_in_match_data (void)
126 return in_match_data;
129 static void
130 set_in_match_data (bool set_value)
132 in_match_data = set_value;
135 /* Free a gfc_data_variable structure and everything beneath it. */
137 static void
138 free_variable (gfc_data_variable *p)
140 gfc_data_variable *q;
142 for (; p; p = q)
144 q = p->next;
145 gfc_free_expr (p->expr);
146 gfc_free_iterator (&p->iter, 0);
147 free_variable (p->list);
148 free (p);
153 /* Free a gfc_data_value structure and everything beneath it. */
155 static void
156 free_value (gfc_data_value *p)
158 gfc_data_value *q;
160 for (; p; p = q)
162 q = p->next;
163 mpz_clear (p->repeat);
164 gfc_free_expr (p->expr);
165 free (p);
170 /* Free a list of gfc_data structures. */
172 void
173 gfc_free_data (gfc_data *p)
175 gfc_data *q;
177 for (; p; p = q)
179 q = p->next;
180 free_variable (p->var);
181 free_value (p->value);
182 free (p);
187 /* Free all data in a namespace. */
189 static void
190 gfc_free_data_all (gfc_namespace *ns)
192 gfc_data *d;
194 for (;ns->data;)
196 d = ns->data->next;
197 free (ns->data);
198 ns->data = d;
202 /* Reject data parsed since the last restore point was marked. */
204 void
205 gfc_reject_data (gfc_namespace *ns)
207 gfc_data *d;
209 while (ns->data && ns->data != ns->old_data)
211 d = ns->data->next;
212 free (ns->data);
213 ns->data = d;
217 static match var_element (gfc_data_variable *);
219 /* Match a list of variables terminated by an iterator and a right
220 parenthesis. */
222 static match
223 var_list (gfc_data_variable *parent)
225 gfc_data_variable *tail, var;
226 match m;
228 m = var_element (&var);
229 if (m == MATCH_ERROR)
230 return MATCH_ERROR;
231 if (m == MATCH_NO)
232 goto syntax;
234 tail = gfc_get_data_variable ();
235 *tail = var;
237 parent->list = tail;
239 for (;;)
241 if (gfc_match_char (',') != MATCH_YES)
242 goto syntax;
244 m = gfc_match_iterator (&parent->iter, 1);
245 if (m == MATCH_YES)
246 break;
247 if (m == MATCH_ERROR)
248 return MATCH_ERROR;
250 m = var_element (&var);
251 if (m == MATCH_ERROR)
252 return MATCH_ERROR;
253 if (m == MATCH_NO)
254 goto syntax;
256 tail->next = gfc_get_data_variable ();
257 tail = tail->next;
259 *tail = var;
262 if (gfc_match_char (')') != MATCH_YES)
263 goto syntax;
264 return MATCH_YES;
266 syntax:
267 gfc_syntax_error (ST_DATA);
268 return MATCH_ERROR;
272 /* Match a single element in a data variable list, which can be a
273 variable-iterator list. */
275 static match
276 var_element (gfc_data_variable *new_var)
278 match m;
279 gfc_symbol *sym;
281 memset (new_var, 0, sizeof (gfc_data_variable));
283 if (gfc_match_char ('(') == MATCH_YES)
284 return var_list (new_var);
286 m = gfc_match_variable (&new_var->expr, 0);
287 if (m != MATCH_YES)
288 return m;
290 if (new_var->expr->expr_type == EXPR_CONSTANT
291 && new_var->expr->symtree == NULL)
293 gfc_error ("Inquiry parameter cannot appear in a "
294 "data-stmt-object-list at %C");
295 return MATCH_ERROR;
298 sym = new_var->expr->symtree->n.sym;
300 /* Symbol should already have an associated type. */
301 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
302 return MATCH_ERROR;
304 if (!sym->attr.function && gfc_current_ns->parent
305 && gfc_current_ns->parent == sym->ns)
307 gfc_error ("Host associated variable %qs may not be in the DATA "
308 "statement at %C", sym->name);
309 return MATCH_ERROR;
312 if (gfc_current_state () != COMP_BLOCK_DATA
313 && sym->attr.in_common
314 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
315 "common block variable %qs in DATA statement at %C",
316 sym->name))
317 return MATCH_ERROR;
319 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
320 return MATCH_ERROR;
322 return MATCH_YES;
326 /* Match the top-level list of data variables. */
328 static match
329 top_var_list (gfc_data *d)
331 gfc_data_variable var, *tail, *new_var;
332 match m;
334 tail = NULL;
336 for (;;)
338 m = var_element (&var);
339 if (m == MATCH_NO)
340 goto syntax;
341 if (m == MATCH_ERROR)
342 return MATCH_ERROR;
344 new_var = gfc_get_data_variable ();
345 *new_var = var;
346 if (new_var->expr)
347 new_var->expr->where = gfc_current_locus;
349 if (tail == NULL)
350 d->var = new_var;
351 else
352 tail->next = new_var;
354 tail = new_var;
356 if (gfc_match_char ('/') == MATCH_YES)
357 break;
358 if (gfc_match_char (',') != MATCH_YES)
359 goto syntax;
362 return MATCH_YES;
364 syntax:
365 gfc_syntax_error (ST_DATA);
366 gfc_free_data_all (gfc_current_ns);
367 return MATCH_ERROR;
371 static match
372 match_data_constant (gfc_expr **result)
374 char name[GFC_MAX_SYMBOL_LEN + 1];
375 gfc_symbol *sym, *dt_sym = NULL;
376 gfc_expr *expr;
377 match m;
378 locus old_loc;
380 m = gfc_match_literal_constant (&expr, 1);
381 if (m == MATCH_YES)
383 *result = expr;
384 return MATCH_YES;
387 if (m == MATCH_ERROR)
388 return MATCH_ERROR;
390 m = gfc_match_null (result);
391 if (m != MATCH_NO)
392 return m;
394 old_loc = gfc_current_locus;
396 /* Should this be a structure component, try to match it
397 before matching a name. */
398 m = gfc_match_rvalue (result);
399 if (m == MATCH_ERROR)
400 return m;
402 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
404 if (!gfc_simplify_expr (*result, 0))
405 m = MATCH_ERROR;
406 return m;
408 else if (m == MATCH_YES)
410 /* If a parameter inquiry ends up here, symtree is NULL but **result
411 contains the right constant expression. Check here. */
412 if ((*result)->symtree == NULL
413 && (*result)->expr_type == EXPR_CONSTANT
414 && ((*result)->ts.type == BT_INTEGER
415 || (*result)->ts.type == BT_REAL))
416 return m;
418 /* F2018:R845 data-stmt-constant is initial-data-target.
419 A data-stmt-constant shall be ... initial-data-target if and
420 only if the corresponding data-stmt-object has the POINTER
421 attribute. ... If data-stmt-constant is initial-data-target
422 the corresponding data statement object shall be
423 data-pointer-initialization compatible (7.5.4.6) with the initial
424 data target; the data statement object is initially associated
425 with the target. */
426 if ((*result)->symtree->n.sym->attr.save
427 && (*result)->symtree->n.sym->attr.target)
428 return m;
429 gfc_free_expr (*result);
432 gfc_current_locus = old_loc;
434 m = gfc_match_name (name);
435 if (m != MATCH_YES)
436 return m;
438 if (gfc_find_symbol (name, NULL, 1, &sym))
439 return MATCH_ERROR;
441 if (sym && sym->attr.generic)
442 dt_sym = gfc_find_dt_in_generic (sym);
444 if (sym == NULL
445 || (sym->attr.flavor != FL_PARAMETER
446 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
448 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
449 name);
450 *result = NULL;
451 return MATCH_ERROR;
453 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
454 return gfc_match_structure_constructor (dt_sym, result);
456 /* Check to see if the value is an initialization array expression. */
457 if (sym->value->expr_type == EXPR_ARRAY)
459 gfc_current_locus = old_loc;
461 m = gfc_match_init_expr (result);
462 if (m == MATCH_ERROR)
463 return m;
465 if (m == MATCH_YES)
467 if (!gfc_simplify_expr (*result, 0))
468 m = MATCH_ERROR;
470 if ((*result)->expr_type == EXPR_CONSTANT)
471 return m;
472 else
474 gfc_error ("Invalid initializer %s in Data statement at %C", name);
475 return MATCH_ERROR;
480 *result = gfc_copy_expr (sym->value);
481 return MATCH_YES;
485 /* Match a list of values in a DATA statement. The leading '/' has
486 already been seen at this point. */
488 static match
489 top_val_list (gfc_data *data)
491 gfc_data_value *new_val, *tail;
492 gfc_expr *expr;
493 match m;
495 tail = NULL;
497 for (;;)
499 m = match_data_constant (&expr);
500 if (m == MATCH_NO)
501 goto syntax;
502 if (m == MATCH_ERROR)
503 return MATCH_ERROR;
505 new_val = gfc_get_data_value ();
506 mpz_init (new_val->repeat);
508 if (tail == NULL)
509 data->value = new_val;
510 else
511 tail->next = new_val;
513 tail = new_val;
515 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
517 tail->expr = expr;
518 mpz_set_ui (tail->repeat, 1);
520 else
522 mpz_set (tail->repeat, expr->value.integer);
523 gfc_free_expr (expr);
525 m = match_data_constant (&tail->expr);
526 if (m == MATCH_NO)
527 goto syntax;
528 if (m == MATCH_ERROR)
529 return MATCH_ERROR;
532 if (gfc_match_char ('/') == MATCH_YES)
533 break;
534 if (gfc_match_char (',') == MATCH_NO)
535 goto syntax;
538 return MATCH_YES;
540 syntax:
541 gfc_syntax_error (ST_DATA);
542 gfc_free_data_all (gfc_current_ns);
543 return MATCH_ERROR;
547 /* Matches an old style initialization. */
549 static match
550 match_old_style_init (const char *name)
552 match m;
553 gfc_symtree *st;
554 gfc_symbol *sym;
555 gfc_data *newdata, *nd;
557 /* Set up data structure to hold initializers. */
558 gfc_find_sym_tree (name, NULL, 0, &st);
559 sym = st->n.sym;
561 newdata = gfc_get_data ();
562 newdata->var = gfc_get_data_variable ();
563 newdata->var->expr = gfc_get_variable_expr (st);
564 newdata->var->expr->where = sym->declared_at;
565 newdata->where = gfc_current_locus;
567 /* Match initial value list. This also eats the terminal '/'. */
568 m = top_val_list (newdata);
569 if (m != MATCH_YES)
571 free (newdata);
572 return m;
575 /* Check that a BOZ did not creep into an old-style initialization. */
576 for (nd = newdata; nd; nd = nd->next)
578 if (nd->value->expr->ts.type == BT_BOZ
579 && gfc_invalid_boz ("BOZ at %L cannot appear in an old-style "
580 "initialization", &nd->value->expr->where))
581 return MATCH_ERROR;
583 if (nd->var->expr->ts.type != BT_INTEGER
584 && nd->var->expr->ts.type != BT_REAL
585 && nd->value->expr->ts.type == BT_BOZ)
587 gfc_error ("BOZ literal constant near %L cannot be assigned to "
588 "a %qs variable in an old-style initialization",
589 &nd->value->expr->where,
590 gfc_typename (&nd->value->expr->ts));
591 return MATCH_ERROR;
595 if (gfc_pure (NULL))
597 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
598 free (newdata);
599 return MATCH_ERROR;
601 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
603 /* Mark the variable as having appeared in a data statement. */
604 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
606 free (newdata);
607 return MATCH_ERROR;
610 /* Chain in namespace list of DATA initializers. */
611 newdata->next = gfc_current_ns->data;
612 gfc_current_ns->data = newdata;
614 return m;
618 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
619 we are matching a DATA statement and are therefore issuing an error
620 if we encounter something unexpected, if not, we're trying to match
621 an old-style initialization expression of the form INTEGER I /2/. */
623 match
624 gfc_match_data (void)
626 gfc_data *new_data;
627 gfc_expr *e;
628 gfc_ref *ref;
629 match m;
630 char c;
632 /* DATA has been matched. In free form source code, the next character
633 needs to be whitespace or '(' from an implied do-loop. Check that
634 here. */
635 c = gfc_peek_ascii_char ();
636 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
637 return MATCH_NO;
639 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
640 if ((gfc_current_state () == COMP_FUNCTION
641 || gfc_current_state () == COMP_SUBROUTINE)
642 && gfc_state_stack->previous->state == COMP_INTERFACE)
644 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
645 return MATCH_ERROR;
648 set_in_match_data (true);
650 for (;;)
652 new_data = gfc_get_data ();
653 new_data->where = gfc_current_locus;
655 m = top_var_list (new_data);
656 if (m != MATCH_YES)
657 goto cleanup;
659 if (new_data->var->iter.var
660 && new_data->var->iter.var->ts.type == BT_INTEGER
661 && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
662 && new_data->var->list
663 && new_data->var->list->expr
664 && new_data->var->list->expr->ts.type == BT_CHARACTER
665 && new_data->var->list->expr->ref
666 && new_data->var->list->expr->ref->type == REF_SUBSTRING)
668 gfc_error ("Invalid substring in data-implied-do at %L in DATA "
669 "statement", &new_data->var->list->expr->where);
670 goto cleanup;
673 /* Check for an entity with an allocatable component, which is not
674 allowed. */
675 e = new_data->var->expr;
676 if (e)
678 bool invalid;
680 invalid = false;
681 for (ref = e->ref; ref; ref = ref->next)
682 if ((ref->type == REF_COMPONENT
683 && ref->u.c.component->attr.allocatable)
684 || (ref->type == REF_ARRAY
685 && e->symtree->n.sym->attr.pointer != 1
686 && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
687 invalid = true;
689 if (invalid)
691 gfc_error ("Allocatable component or deferred-shaped array "
692 "near %C in DATA statement");
693 goto cleanup;
696 /* F2008:C567 (R536) A data-i-do-object or a variable that appears
697 as a data-stmt-object shall not be an object designator in which
698 a pointer appears other than as the entire rightmost part-ref. */
699 ref = e->ref;
700 if (e->symtree->n.sym->ts.type == BT_DERIVED
701 && e->symtree->n.sym->attr.pointer
702 && ref->type == REF_COMPONENT)
703 goto partref;
705 for (; ref; ref = ref->next)
706 if (ref->type == REF_COMPONENT
707 && ref->u.c.component->attr.pointer
708 && ref->next)
709 goto partref;
712 m = top_val_list (new_data);
713 if (m != MATCH_YES)
714 goto cleanup;
716 new_data->next = gfc_current_ns->data;
717 gfc_current_ns->data = new_data;
719 if (gfc_match_eos () == MATCH_YES)
720 break;
722 gfc_match_char (','); /* Optional comma */
725 set_in_match_data (false);
727 if (gfc_pure (NULL))
729 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
730 return MATCH_ERROR;
732 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
734 return MATCH_YES;
736 partref:
738 gfc_error ("part-ref with pointer attribute near %L is not "
739 "rightmost part-ref of data-stmt-object",
740 &e->where);
742 cleanup:
743 set_in_match_data (false);
744 gfc_free_data (new_data);
745 return MATCH_ERROR;
749 /************************ Declaration statements *********************/
752 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
753 list). The difference here is the expression is a list of constants
754 and is surrounded by '/'.
755 The typespec ts must match the typespec of the variable which the
756 clist is initializing.
757 The arrayspec tells whether this should match a list of constants
758 corresponding to array elements or a scalar (as == NULL). */
760 static match
761 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
763 gfc_constructor_base array_head = NULL;
764 gfc_expr *expr = NULL;
765 match m = MATCH_ERROR;
766 locus where;
767 mpz_t repeat, cons_size, as_size;
768 bool scalar;
769 int cmp;
771 gcc_assert (ts);
773 /* We have already matched '/' - now look for a constant list, as with
774 top_val_list from decl.c, but append the result to an array. */
775 if (gfc_match ("/") == MATCH_YES)
777 gfc_error ("Empty old style initializer list at %C");
778 return MATCH_ERROR;
781 where = gfc_current_locus;
782 scalar = !as || !as->rank;
784 if (!scalar && !spec_size (as, &as_size))
786 gfc_error ("Array in initializer list at %L must have an explicit shape",
787 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
788 /* Nothing to cleanup yet. */
789 return MATCH_ERROR;
792 mpz_init_set_ui (repeat, 0);
794 for (;;)
796 m = match_data_constant (&expr);
797 if (m != MATCH_YES)
798 expr = NULL; /* match_data_constant may set expr to garbage */
799 if (m == MATCH_NO)
800 goto syntax;
801 if (m == MATCH_ERROR)
802 goto cleanup;
804 /* Found r in repeat spec r*c; look for the constant to repeat. */
805 if ( gfc_match_char ('*') == MATCH_YES)
807 if (scalar)
809 gfc_error ("Repeat spec invalid in scalar initializer at %C");
810 goto cleanup;
812 if (expr->ts.type != BT_INTEGER)
814 gfc_error ("Repeat spec must be an integer at %C");
815 goto cleanup;
817 mpz_set (repeat, expr->value.integer);
818 gfc_free_expr (expr);
819 expr = NULL;
821 m = match_data_constant (&expr);
822 if (m == MATCH_NO)
824 m = MATCH_ERROR;
825 gfc_error ("Expected data constant after repeat spec at %C");
827 if (m != MATCH_YES)
828 goto cleanup;
830 /* No repeat spec, we matched the data constant itself. */
831 else
832 mpz_set_ui (repeat, 1);
834 if (!scalar)
836 /* Add the constant initializer as many times as repeated. */
837 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
839 /* Make sure types of elements match */
840 if(ts && !gfc_compare_types (&expr->ts, ts)
841 && !gfc_convert_type (expr, ts, 1))
842 goto cleanup;
844 gfc_constructor_append_expr (&array_head,
845 gfc_copy_expr (expr), &gfc_current_locus);
848 gfc_free_expr (expr);
849 expr = NULL;
852 /* For scalar initializers quit after one element. */
853 else
855 if(gfc_match_char ('/') != MATCH_YES)
857 gfc_error ("End of scalar initializer expected at %C");
858 goto cleanup;
860 break;
863 if (gfc_match_char ('/') == MATCH_YES)
864 break;
865 if (gfc_match_char (',') == MATCH_NO)
866 goto syntax;
869 /* If we break early from here out, we encountered an error. */
870 m = MATCH_ERROR;
872 /* Set up expr as an array constructor. */
873 if (!scalar)
875 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
876 expr->ts = *ts;
877 expr->value.constructor = array_head;
879 expr->rank = as->rank;
880 expr->shape = gfc_get_shape (expr->rank);
882 /* Validate sizes. We built expr ourselves, so cons_size will be
883 constant (we fail above for non-constant expressions).
884 We still need to verify that the sizes match. */
885 gcc_assert (gfc_array_size (expr, &cons_size));
886 cmp = mpz_cmp (cons_size, as_size);
887 if (cmp < 0)
888 gfc_error ("Not enough elements in array initializer at %C");
889 else if (cmp > 0)
890 gfc_error ("Too many elements in array initializer at %C");
891 mpz_clear (cons_size);
892 if (cmp)
893 goto cleanup;
896 /* Make sure scalar types match. */
897 else if (!gfc_compare_types (&expr->ts, ts)
898 && !gfc_convert_type (expr, ts, 1))
899 goto cleanup;
901 if (expr->ts.u.cl)
902 expr->ts.u.cl->length_from_typespec = 1;
904 *result = expr;
905 m = MATCH_YES;
906 goto done;
908 syntax:
909 m = MATCH_ERROR;
910 gfc_error ("Syntax error in old style initializer list at %C");
912 cleanup:
913 if (expr)
914 expr->value.constructor = NULL;
915 gfc_free_expr (expr);
916 gfc_constructor_free (array_head);
918 done:
919 mpz_clear (repeat);
920 if (!scalar)
921 mpz_clear (as_size);
922 return m;
926 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
928 static bool
929 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
931 if ((from->type == AS_ASSUMED_RANK && to->corank)
932 || (to->type == AS_ASSUMED_RANK && from->corank))
934 gfc_error ("The assumed-rank array at %C shall not have a codimension");
935 return false;
938 if (to->rank == 0 && from->rank > 0)
940 to->rank = from->rank;
941 to->type = from->type;
942 to->cray_pointee = from->cray_pointee;
943 to->cp_was_assumed = from->cp_was_assumed;
945 for (int i = to->corank - 1; i >= 0; i--)
947 /* Do not exceed the limits on lower[] and upper[]. gfortran
948 cleans up elsewhere. */
949 int j = from->rank + i;
950 if (j >= GFC_MAX_DIMENSIONS)
951 break;
953 to->lower[j] = to->lower[i];
954 to->upper[j] = to->upper[i];
956 for (int i = 0; i < from->rank; i++)
958 if (copy)
960 to->lower[i] = gfc_copy_expr (from->lower[i]);
961 to->upper[i] = gfc_copy_expr (from->upper[i]);
963 else
965 to->lower[i] = from->lower[i];
966 to->upper[i] = from->upper[i];
970 else if (to->corank == 0 && from->corank > 0)
972 to->corank = from->corank;
973 to->cotype = from->cotype;
975 for (int i = 0; i < from->corank; i++)
977 /* Do not exceed the limits on lower[] and upper[]. gfortran
978 cleans up elsewhere. */
979 int k = from->rank + i;
980 int j = to->rank + i;
981 if (j >= GFC_MAX_DIMENSIONS)
982 break;
984 if (copy)
986 to->lower[j] = gfc_copy_expr (from->lower[k]);
987 to->upper[j] = gfc_copy_expr (from->upper[k]);
989 else
991 to->lower[j] = from->lower[k];
992 to->upper[j] = from->upper[k];
997 if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
999 gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
1000 "allowed dimensions of %d",
1001 to->rank, to->corank, GFC_MAX_DIMENSIONS);
1002 to->corank = GFC_MAX_DIMENSIONS - to->rank;
1003 return false;
1005 return true;
1009 /* Match an intent specification. Since this can only happen after an
1010 INTENT word, a legal intent-spec must follow. */
1012 static sym_intent
1013 match_intent_spec (void)
1016 if (gfc_match (" ( in out )") == MATCH_YES)
1017 return INTENT_INOUT;
1018 if (gfc_match (" ( in )") == MATCH_YES)
1019 return INTENT_IN;
1020 if (gfc_match (" ( out )") == MATCH_YES)
1021 return INTENT_OUT;
1023 gfc_error ("Bad INTENT specification at %C");
1024 return INTENT_UNKNOWN;
1028 /* Matches a character length specification, which is either a
1029 specification expression, '*', or ':'. */
1031 static match
1032 char_len_param_value (gfc_expr **expr, bool *deferred)
1034 match m;
1036 *expr = NULL;
1037 *deferred = false;
1039 if (gfc_match_char ('*') == MATCH_YES)
1040 return MATCH_YES;
1042 if (gfc_match_char (':') == MATCH_YES)
1044 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1045 return MATCH_ERROR;
1047 *deferred = true;
1049 return MATCH_YES;
1052 m = gfc_match_expr (expr);
1054 if (m == MATCH_NO || m == MATCH_ERROR)
1055 return m;
1057 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1058 return MATCH_ERROR;
1060 if ((*expr)->expr_type == EXPR_FUNCTION)
1062 if ((*expr)->ts.type == BT_INTEGER
1063 || ((*expr)->ts.type == BT_UNKNOWN
1064 && strcmp((*expr)->symtree->name, "null") != 0))
1065 return MATCH_YES;
1067 goto syntax;
1069 else if ((*expr)->expr_type == EXPR_CONSTANT)
1071 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1072 processor dependent and its value is greater than or equal to zero.
1073 F2008, 4.4.3.2: If the character length parameter value evaluates
1074 to a negative value, the length of character entities declared
1075 is zero. */
1077 if ((*expr)->ts.type == BT_INTEGER)
1079 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1080 mpz_set_si ((*expr)->value.integer, 0);
1082 else
1083 goto syntax;
1085 else if ((*expr)->expr_type == EXPR_ARRAY)
1086 goto syntax;
1087 else if ((*expr)->expr_type == EXPR_VARIABLE)
1089 bool t;
1090 gfc_expr *e;
1092 e = gfc_copy_expr (*expr);
1094 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1095 which causes an ICE if gfc_reduce_init_expr() is called. */
1096 if (e->ref && e->ref->type == REF_ARRAY
1097 && e->ref->u.ar.type == AR_UNKNOWN
1098 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1099 goto syntax;
1101 t = gfc_reduce_init_expr (e);
1103 if (!t && e->ts.type == BT_UNKNOWN
1104 && e->symtree->n.sym->attr.untyped == 1
1105 && (flag_implicit_none
1106 || e->symtree->n.sym->ns->seen_implicit_none == 1
1107 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1109 gfc_free_expr (e);
1110 goto syntax;
1113 if ((e->ref && e->ref->type == REF_ARRAY
1114 && e->ref->u.ar.type != AR_ELEMENT)
1115 || (!e->ref && e->expr_type == EXPR_ARRAY))
1117 gfc_free_expr (e);
1118 goto syntax;
1121 gfc_free_expr (e);
1124 return m;
1126 syntax:
1127 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1128 return MATCH_ERROR;
1132 /* A character length is a '*' followed by a literal integer or a
1133 char_len_param_value in parenthesis. */
1135 static match
1136 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1138 int length;
1139 match m;
1141 *deferred = false;
1142 m = gfc_match_char ('*');
1143 if (m != MATCH_YES)
1144 return m;
1146 m = gfc_match_small_literal_int (&length, NULL);
1147 if (m == MATCH_ERROR)
1148 return m;
1150 if (m == MATCH_YES)
1152 if (obsolescent_check
1153 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1154 return MATCH_ERROR;
1155 *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1156 return m;
1159 if (gfc_match_char ('(') == MATCH_NO)
1160 goto syntax;
1162 m = char_len_param_value (expr, deferred);
1163 if (m != MATCH_YES && gfc_matching_function)
1165 gfc_undo_symbols ();
1166 m = MATCH_YES;
1169 if (m == MATCH_ERROR)
1170 return m;
1171 if (m == MATCH_NO)
1172 goto syntax;
1174 if (gfc_match_char (')') == MATCH_NO)
1176 gfc_free_expr (*expr);
1177 *expr = NULL;
1178 goto syntax;
1181 return MATCH_YES;
1183 syntax:
1184 gfc_error ("Syntax error in character length specification at %C");
1185 return MATCH_ERROR;
1189 /* Special subroutine for finding a symbol. Check if the name is found
1190 in the current name space. If not, and we're compiling a function or
1191 subroutine and the parent compilation unit is an interface, then check
1192 to see if the name we've been given is the name of the interface
1193 (located in another namespace). */
1195 static int
1196 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1198 gfc_state_data *s;
1199 gfc_symtree *st;
1200 int i;
1202 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1203 if (i == 0)
1205 *result = st ? st->n.sym : NULL;
1206 goto end;
1209 if (gfc_current_state () != COMP_SUBROUTINE
1210 && gfc_current_state () != COMP_FUNCTION)
1211 goto end;
1213 s = gfc_state_stack->previous;
1214 if (s == NULL)
1215 goto end;
1217 if (s->state != COMP_INTERFACE)
1218 goto end;
1219 if (s->sym == NULL)
1220 goto end; /* Nameless interface. */
1222 if (strcmp (name, s->sym->name) == 0)
1224 *result = s->sym;
1225 return 0;
1228 end:
1229 return i;
1233 /* Special subroutine for getting a symbol node associated with a
1234 procedure name, used in SUBROUTINE and FUNCTION statements. The
1235 symbol is created in the parent using with symtree node in the
1236 child unit pointing to the symbol. If the current namespace has no
1237 parent, then the symbol is just created in the current unit. */
1239 static int
1240 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1242 gfc_symtree *st;
1243 gfc_symbol *sym;
1244 int rc = 0;
1246 /* Module functions have to be left in their own namespace because
1247 they have potentially (almost certainly!) already been referenced.
1248 In this sense, they are rather like external functions. This is
1249 fixed up in resolve.c(resolve_entries), where the symbol name-
1250 space is set to point to the master function, so that the fake
1251 result mechanism can work. */
1252 if (module_fcn_entry)
1254 /* Present if entry is declared to be a module procedure. */
1255 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1257 if (*result == NULL)
1258 rc = gfc_get_symbol (name, NULL, result);
1259 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1260 && (*result)->ts.type == BT_UNKNOWN
1261 && sym->attr.flavor == FL_UNKNOWN)
1262 /* Pick up the typespec for the entry, if declared in the function
1263 body. Note that this symbol is FL_UNKNOWN because it will
1264 only have appeared in a type declaration. The local symtree
1265 is set to point to the module symbol and a unique symtree
1266 to the local version. This latter ensures a correct clearing
1267 of the symbols. */
1269 /* If the ENTRY proceeds its specification, we need to ensure
1270 that this does not raise a "has no IMPLICIT type" error. */
1271 if (sym->ts.type == BT_UNKNOWN)
1272 sym->attr.untyped = 1;
1274 (*result)->ts = sym->ts;
1276 /* Put the symbol in the procedure namespace so that, should
1277 the ENTRY precede its specification, the specification
1278 can be applied. */
1279 (*result)->ns = gfc_current_ns;
1281 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1282 st->n.sym = *result;
1283 st = gfc_get_unique_symtree (gfc_current_ns);
1284 sym->refs++;
1285 st->n.sym = sym;
1288 else
1289 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1291 if (rc)
1292 return rc;
1294 sym = *result;
1295 if (sym->attr.proc == PROC_ST_FUNCTION)
1296 return rc;
1298 if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1300 /* Create a partially populated interface symbol to carry the
1301 characteristics of the procedure and the result. */
1302 sym->tlink = gfc_new_symbol (name, sym->ns);
1303 gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1304 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1305 if (sym->attr.dimension)
1306 sym->tlink->as = gfc_copy_array_spec (sym->as);
1308 /* Ideally, at this point, a copy would be made of the formal
1309 arguments and their namespace. However, this does not appear
1310 to be necessary, albeit at the expense of not being able to
1311 use gfc_compare_interfaces directly. */
1313 if (sym->result && sym->result != sym)
1315 sym->tlink->result = sym->result;
1316 sym->result = NULL;
1318 else if (sym->result)
1320 sym->tlink->result = sym->tlink;
1323 else if (sym && !sym->gfc_new
1324 && gfc_current_state () != COMP_INTERFACE)
1326 /* Trap another encompassed procedure with the same name. All
1327 these conditions are necessary to avoid picking up an entry
1328 whose name clashes with that of the encompassing procedure;
1329 this is handled using gsymbols to register unique, globally
1330 accessible names. */
1331 if (sym->attr.flavor != 0
1332 && sym->attr.proc != 0
1333 && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1334 && sym->attr.if_source != IFSRC_UNKNOWN)
1336 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1337 name, &sym->declared_at);
1338 return true;
1340 if (sym->attr.flavor != 0
1341 && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1343 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1344 name, &sym->declared_at);
1345 return true;
1348 if (sym->attr.external && sym->attr.procedure
1349 && gfc_current_state () == COMP_CONTAINS)
1351 gfc_error_now ("Contained procedure %qs at %C clashes with "
1352 "procedure defined at %L",
1353 name, &sym->declared_at);
1354 return true;
1357 /* Trap a procedure with a name the same as interface in the
1358 encompassing scope. */
1359 if (sym->attr.generic != 0
1360 && (sym->attr.subroutine || sym->attr.function)
1361 && !sym->attr.mod_proc)
1363 gfc_error_now ("Name %qs at %C is already defined"
1364 " as a generic interface at %L",
1365 name, &sym->declared_at);
1366 return true;
1369 /* Trap declarations of attributes in encompassing scope. The
1370 signature for this is that ts.kind is nonzero for no-CLASS
1371 entity. For a CLASS entity, ts.kind is zero. */
1372 if ((sym->ts.kind != 0 || sym->ts.type == BT_CLASS)
1373 && !sym->attr.implicit_type
1374 && sym->attr.proc == 0
1375 && gfc_current_ns->parent != NULL
1376 && sym->attr.access == 0
1377 && !module_fcn_entry)
1379 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1380 "from a previous declaration", name);
1381 return true;
1385 /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1386 subroutine-stmt of a module subprogram or of a nonabstract interface
1387 body that is declared in the scoping unit of a module or submodule. */
1388 if (sym->attr.external
1389 && (sym->attr.subroutine || sym->attr.function)
1390 && sym->attr.if_source == IFSRC_IFBODY
1391 && !current_attr.module_procedure
1392 && sym->attr.proc == PROC_MODULE
1393 && gfc_state_stack->state == COMP_CONTAINS)
1395 gfc_error_now ("Procedure %qs defined in interface body at %L "
1396 "clashes with internal procedure defined at %C",
1397 name, &sym->declared_at);
1398 return true;
1401 if (sym && !sym->gfc_new
1402 && sym->attr.flavor != FL_UNKNOWN
1403 && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1404 && gfc_state_stack->state == COMP_CONTAINS
1405 && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1407 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1408 name, &sym->declared_at);
1409 return true;
1412 if (gfc_current_ns->parent == NULL || *result == NULL)
1413 return rc;
1415 /* Module function entries will already have a symtree in
1416 the current namespace but will need one at module level. */
1417 if (module_fcn_entry)
1419 /* Present if entry is declared to be a module procedure. */
1420 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1421 if (st == NULL)
1422 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1424 else
1425 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1427 st->n.sym = sym;
1428 sym->refs++;
1430 /* See if the procedure should be a module procedure. */
1432 if (((sym->ns->proc_name != NULL
1433 && sym->ns->proc_name->attr.flavor == FL_MODULE
1434 && sym->attr.proc != PROC_MODULE)
1435 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1436 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1437 rc = 2;
1439 return rc;
1443 /* Verify that the given symbol representing a parameter is C
1444 interoperable, by checking to see if it was marked as such after
1445 its declaration. If the given symbol is not interoperable, a
1446 warning is reported, thus removing the need to return the status to
1447 the calling function. The standard does not require the user use
1448 one of the iso_c_binding named constants to declare an
1449 interoperable parameter, but we can't be sure if the param is C
1450 interop or not if the user doesn't. For example, integer(4) may be
1451 legal Fortran, but doesn't have meaning in C. It may interop with
1452 a number of the C types, which causes a problem because the
1453 compiler can't know which one. This code is almost certainly not
1454 portable, and the user will get what they deserve if the C type
1455 across platforms isn't always interoperable with integer(4). If
1456 the user had used something like integer(c_int) or integer(c_long),
1457 the compiler could have automatically handled the varying sizes
1458 across platforms. */
1460 bool
1461 gfc_verify_c_interop_param (gfc_symbol *sym)
1463 int is_c_interop = 0;
1464 bool retval = true;
1466 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1467 Don't repeat the checks here. */
1468 if (sym->attr.implicit_type)
1469 return true;
1471 /* For subroutines or functions that are passed to a BIND(C) procedure,
1472 they're interoperable if they're BIND(C) and their params are all
1473 interoperable. */
1474 if (sym->attr.flavor == FL_PROCEDURE)
1476 if (sym->attr.is_bind_c == 0)
1478 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1479 "attribute to be C interoperable", sym->name,
1480 &(sym->declared_at));
1481 return false;
1483 else
1485 if (sym->attr.is_c_interop == 1)
1486 /* We've already checked this procedure; don't check it again. */
1487 return true;
1488 else
1489 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1490 sym->common_block);
1494 /* See if we've stored a reference to a procedure that owns sym. */
1495 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1497 if (sym->ns->proc_name->attr.is_bind_c == 1)
1499 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1501 if (is_c_interop != 1)
1503 /* Make personalized messages to give better feedback. */
1504 if (sym->ts.type == BT_DERIVED)
1505 gfc_error ("Variable %qs at %L is a dummy argument to the "
1506 "BIND(C) procedure %qs but is not C interoperable "
1507 "because derived type %qs is not C interoperable",
1508 sym->name, &(sym->declared_at),
1509 sym->ns->proc_name->name,
1510 sym->ts.u.derived->name);
1511 else if (sym->ts.type == BT_CLASS)
1512 gfc_error ("Variable %qs at %L is a dummy argument to the "
1513 "BIND(C) procedure %qs but is not C interoperable "
1514 "because it is polymorphic",
1515 sym->name, &(sym->declared_at),
1516 sym->ns->proc_name->name);
1517 else if (warn_c_binding_type)
1518 gfc_warning (OPT_Wc_binding_type,
1519 "Variable %qs at %L is a dummy argument of the "
1520 "BIND(C) procedure %qs but may not be C "
1521 "interoperable",
1522 sym->name, &(sym->declared_at),
1523 sym->ns->proc_name->name);
1526 /* Character strings are only C interoperable if they have a
1527 length of 1. */
1528 if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension)
1530 gfc_charlen *cl = sym->ts.u.cl;
1531 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1532 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1534 gfc_error ("Character argument %qs at %L "
1535 "must be length 1 because "
1536 "procedure %qs is BIND(C)",
1537 sym->name, &sym->declared_at,
1538 sym->ns->proc_name->name);
1539 retval = false;
1543 /* We have to make sure that any param to a bind(c) routine does
1544 not have the allocatable, pointer, or optional attributes,
1545 according to J3/04-007, section 5.1. */
1546 if (sym->attr.allocatable == 1
1547 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1548 "ALLOCATABLE attribute in procedure %qs "
1549 "with BIND(C)", sym->name,
1550 &(sym->declared_at),
1551 sym->ns->proc_name->name))
1552 retval = false;
1554 if (sym->attr.pointer == 1
1555 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1556 "POINTER attribute in procedure %qs "
1557 "with BIND(C)", sym->name,
1558 &(sym->declared_at),
1559 sym->ns->proc_name->name))
1560 retval = false;
1562 if (sym->attr.optional == 1 && sym->attr.value)
1564 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1565 "and the VALUE attribute because procedure %qs "
1566 "is BIND(C)", sym->name, &(sym->declared_at),
1567 sym->ns->proc_name->name);
1568 retval = false;
1570 else if (sym->attr.optional == 1
1571 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1572 "at %L with OPTIONAL attribute in "
1573 "procedure %qs which is BIND(C)",
1574 sym->name, &(sym->declared_at),
1575 sym->ns->proc_name->name))
1576 retval = false;
1578 /* Make sure that if it has the dimension attribute, that it is
1579 either assumed size or explicit shape. Deferred shape is already
1580 covered by the pointer/allocatable attribute. */
1581 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1582 && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1583 "at %L as dummy argument to the BIND(C) "
1584 "procedure %qs at %L", sym->name,
1585 &(sym->declared_at),
1586 sym->ns->proc_name->name,
1587 &(sym->ns->proc_name->declared_at)))
1588 retval = false;
1592 return retval;
1597 /* Function called by variable_decl() that adds a name to the symbol table. */
1599 static bool
1600 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1601 gfc_array_spec **as, locus *var_locus)
1603 symbol_attribute attr;
1604 gfc_symbol *sym;
1605 int upper;
1606 gfc_symtree *st;
1608 /* Symbols in a submodule are host associated from the parent module or
1609 submodules. Therefore, they can be overridden by declarations in the
1610 submodule scope. Deal with this by attaching the existing symbol to
1611 a new symtree and recycling the old symtree with a new symbol... */
1612 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1613 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1614 && st->n.sym != NULL
1615 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1617 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1618 s->n.sym = st->n.sym;
1619 sym = gfc_new_symbol (name, gfc_current_ns);
1622 st->n.sym = sym;
1623 sym->refs++;
1624 gfc_set_sym_referenced (sym);
1626 /* ...Otherwise generate a new symtree and new symbol. */
1627 else if (gfc_get_symbol (name, NULL, &sym))
1628 return false;
1630 /* Check if the name has already been defined as a type. The
1631 first letter of the symtree will be in upper case then. Of
1632 course, this is only necessary if the upper case letter is
1633 actually different. */
1635 upper = TOUPPER(name[0]);
1636 if (upper != name[0])
1638 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1639 gfc_symtree *st;
1641 gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1642 strcpy (u_name, name);
1643 u_name[0] = upper;
1645 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1647 /* STRUCTURE types can alias symbol names */
1648 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1650 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1651 &st->n.sym->declared_at);
1652 return false;
1656 /* Start updating the symbol table. Add basic type attribute if present. */
1657 if (current_ts.type != BT_UNKNOWN
1658 && (sym->attr.implicit_type == 0
1659 || !gfc_compare_types (&sym->ts, &current_ts))
1660 && !gfc_add_type (sym, &current_ts, var_locus))
1661 return false;
1663 if (sym->ts.type == BT_CHARACTER)
1665 sym->ts.u.cl = cl;
1666 sym->ts.deferred = cl_deferred;
1669 /* Add dimension attribute if present. */
1670 if (!gfc_set_array_spec (sym, *as, var_locus))
1671 return false;
1672 *as = NULL;
1674 /* Add attribute to symbol. The copy is so that we can reset the
1675 dimension attribute. */
1676 attr = current_attr;
1677 attr.dimension = 0;
1678 attr.codimension = 0;
1680 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1681 return false;
1683 /* Finish any work that may need to be done for the binding label,
1684 if it's a bind(c). The bind(c) attr is found before the symbol
1685 is made, and before the symbol name (for data decls), so the
1686 current_ts is holding the binding label, or nothing if the
1687 name= attr wasn't given. Therefore, test here if we're dealing
1688 with a bind(c) and make sure the binding label is set correctly. */
1689 if (sym->attr.is_bind_c == 1)
1691 if (!sym->binding_label)
1693 /* Set the binding label and verify that if a NAME= was specified
1694 then only one identifier was in the entity-decl-list. */
1695 if (!set_binding_label (&sym->binding_label, sym->name,
1696 num_idents_on_line))
1697 return false;
1701 /* See if we know we're in a common block, and if it's a bind(c)
1702 common then we need to make sure we're an interoperable type. */
1703 if (sym->attr.in_common == 1)
1705 /* Test the common block object. */
1706 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1707 && sym->ts.is_c_interop != 1)
1709 gfc_error_now ("Variable %qs in common block %qs at %C "
1710 "must be declared with a C interoperable "
1711 "kind since common block %qs is BIND(C)",
1712 sym->name, sym->common_block->name,
1713 sym->common_block->name);
1714 gfc_clear_error ();
1718 sym->attr.implied_index = 0;
1720 /* Use the parameter expressions for a parameterized derived type. */
1721 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1722 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1723 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1725 if (sym->ts.type == BT_CLASS)
1726 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1728 return true;
1732 /* Set character constant to the given length. The constant will be padded or
1733 truncated. If we're inside an array constructor without a typespec, we
1734 additionally check that all elements have the same length; check_len -1
1735 means no checking. */
1737 void
1738 gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1739 gfc_charlen_t check_len)
1741 gfc_char_t *s;
1742 gfc_charlen_t slen;
1744 if (expr->ts.type != BT_CHARACTER)
1745 return;
1747 if (expr->expr_type != EXPR_CONSTANT)
1749 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1750 return;
1753 slen = expr->value.character.length;
1754 if (len != slen)
1756 s = gfc_get_wide_string (len + 1);
1757 memcpy (s, expr->value.character.string,
1758 MIN (len, slen) * sizeof (gfc_char_t));
1759 if (len > slen)
1760 gfc_wide_memset (&s[slen], ' ', len - slen);
1762 if (warn_character_truncation && slen > len)
1763 gfc_warning_now (OPT_Wcharacter_truncation,
1764 "CHARACTER expression at %L is being truncated "
1765 "(%ld/%ld)", &expr->where,
1766 (long) slen, (long) len);
1768 /* Apply the standard by 'hand' otherwise it gets cleared for
1769 initializers. */
1770 if (check_len != -1 && slen != check_len
1771 && !(gfc_option.allow_std & GFC_STD_GNU))
1772 gfc_error_now ("The CHARACTER elements of the array constructor "
1773 "at %L must have the same length (%ld/%ld)",
1774 &expr->where, (long) slen,
1775 (long) check_len);
1777 s[len] = '\0';
1778 free (expr->value.character.string);
1779 expr->value.character.string = s;
1780 expr->value.character.length = len;
1781 /* If explicit representation was given, clear it
1782 as it is no longer needed after padding. */
1783 if (expr->representation.length)
1785 expr->representation.length = 0;
1786 free (expr->representation.string);
1787 expr->representation.string = NULL;
1793 /* Function to create and update the enumerator history
1794 using the information passed as arguments.
1795 Pointer "max_enum" is also updated, to point to
1796 enum history node containing largest initializer.
1798 SYM points to the symbol node of enumerator.
1799 INIT points to its enumerator value. */
1801 static void
1802 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1804 enumerator_history *new_enum_history;
1805 gcc_assert (sym != NULL && init != NULL);
1807 new_enum_history = XCNEW (enumerator_history);
1809 new_enum_history->sym = sym;
1810 new_enum_history->initializer = init;
1811 new_enum_history->next = NULL;
1813 if (enum_history == NULL)
1815 enum_history = new_enum_history;
1816 max_enum = enum_history;
1818 else
1820 new_enum_history->next = enum_history;
1821 enum_history = new_enum_history;
1823 if (mpz_cmp (max_enum->initializer->value.integer,
1824 new_enum_history->initializer->value.integer) < 0)
1825 max_enum = new_enum_history;
1830 /* Function to free enum kind history. */
1832 void
1833 gfc_free_enum_history (void)
1835 enumerator_history *current = enum_history;
1836 enumerator_history *next;
1838 while (current != NULL)
1840 next = current->next;
1841 free (current);
1842 current = next;
1844 max_enum = NULL;
1845 enum_history = NULL;
1849 /* Function called by variable_decl() that adds an initialization
1850 expression to a symbol. */
1852 static bool
1853 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1855 symbol_attribute attr;
1856 gfc_symbol *sym;
1857 gfc_expr *init;
1859 init = *initp;
1860 if (find_special (name, &sym, false))
1861 return false;
1863 attr = sym->attr;
1865 /* If this symbol is confirming an implicit parameter type,
1866 then an initialization expression is not allowed. */
1867 if (attr.flavor == FL_PARAMETER
1868 && sym->value != NULL
1869 && *initp != NULL)
1871 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1872 sym->name);
1873 return false;
1876 if (init == NULL)
1878 /* An initializer is required for PARAMETER declarations. */
1879 if (attr.flavor == FL_PARAMETER)
1881 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1882 return false;
1885 else
1887 /* If a variable appears in a DATA block, it cannot have an
1888 initializer. */
1889 if (sym->attr.data)
1891 gfc_error ("Variable %qs at %C with an initializer already "
1892 "appears in a DATA statement", sym->name);
1893 return false;
1896 /* Check if the assignment can happen. This has to be put off
1897 until later for derived type variables and procedure pointers. */
1898 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1899 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1900 && !sym->attr.proc_pointer
1901 && !gfc_check_assign_symbol (sym, NULL, init))
1902 return false;
1904 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1905 && init->ts.type == BT_CHARACTER)
1907 /* Update symbol character length according initializer. */
1908 if (!gfc_check_assign_symbol (sym, NULL, init))
1909 return false;
1911 if (sym->ts.u.cl->length == NULL)
1913 gfc_charlen_t clen;
1914 /* If there are multiple CHARACTER variables declared on the
1915 same line, we don't want them to share the same length. */
1916 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1918 if (sym->attr.flavor == FL_PARAMETER)
1920 if (init->expr_type == EXPR_CONSTANT)
1922 clen = init->value.character.length;
1923 sym->ts.u.cl->length
1924 = gfc_get_int_expr (gfc_charlen_int_kind,
1925 NULL, clen);
1927 else if (init->expr_type == EXPR_ARRAY)
1929 if (init->ts.u.cl && init->ts.u.cl->length)
1931 const gfc_expr *length = init->ts.u.cl->length;
1932 if (length->expr_type != EXPR_CONSTANT)
1934 gfc_error ("Cannot initialize parameter array "
1935 "at %L "
1936 "with variable length elements",
1937 &sym->declared_at);
1938 return false;
1940 clen = mpz_get_si (length->value.integer);
1942 else if (init->value.constructor)
1944 gfc_constructor *c;
1945 c = gfc_constructor_first (init->value.constructor);
1946 clen = c->expr->value.character.length;
1948 else
1949 gcc_unreachable ();
1950 sym->ts.u.cl->length
1951 = gfc_get_int_expr (gfc_charlen_int_kind,
1952 NULL, clen);
1954 else if (init->ts.u.cl && init->ts.u.cl->length)
1955 sym->ts.u.cl->length =
1956 gfc_copy_expr (init->ts.u.cl->length);
1959 /* Update initializer character length according symbol. */
1960 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1962 if (!gfc_specification_expr (sym->ts.u.cl->length))
1963 return false;
1965 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
1966 false);
1967 /* resolve_charlen will complain later on if the length
1968 is too large. Just skeep the initialization in that case. */
1969 if (mpz_cmp (sym->ts.u.cl->length->value.integer,
1970 gfc_integer_kinds[k].huge) <= 0)
1972 HOST_WIDE_INT len
1973 = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
1975 if (init->expr_type == EXPR_CONSTANT)
1976 gfc_set_constant_character_len (len, init, -1);
1977 else if (init->expr_type == EXPR_ARRAY)
1979 gfc_constructor *c;
1981 /* Build a new charlen to prevent simplification from
1982 deleting the length before it is resolved. */
1983 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1984 init->ts.u.cl->length
1985 = gfc_copy_expr (sym->ts.u.cl->length);
1987 for (c = gfc_constructor_first (init->value.constructor);
1988 c; c = gfc_constructor_next (c))
1989 gfc_set_constant_character_len (len, c->expr, -1);
1995 /* If sym is implied-shape, set its upper bounds from init. */
1996 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1997 && sym->as->type == AS_IMPLIED_SHAPE)
1999 int dim;
2001 if (init->rank == 0)
2003 gfc_error ("Cannot initialize implied-shape array at %L"
2004 " with scalar", &sym->declared_at);
2005 return false;
2008 /* The shape may be NULL for EXPR_ARRAY, set it. */
2009 if (init->shape == NULL)
2011 gcc_assert (init->expr_type == EXPR_ARRAY);
2012 init->shape = gfc_get_shape (1);
2013 if (!gfc_array_size (init, &init->shape[0]))
2014 gfc_internal_error ("gfc_array_size failed");
2017 for (dim = 0; dim < sym->as->rank; ++dim)
2019 int k;
2020 gfc_expr *e, *lower;
2022 lower = sym->as->lower[dim];
2024 /* If the lower bound is an array element from another
2025 parameterized array, then it is marked with EXPR_VARIABLE and
2026 is an initialization expression. Try to reduce it. */
2027 if (lower->expr_type == EXPR_VARIABLE)
2028 gfc_reduce_init_expr (lower);
2030 if (lower->expr_type == EXPR_CONSTANT)
2032 /* All dimensions must be without upper bound. */
2033 gcc_assert (!sym->as->upper[dim]);
2035 k = lower->ts.kind;
2036 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2037 mpz_add (e->value.integer, lower->value.integer,
2038 init->shape[dim]);
2039 mpz_sub_ui (e->value.integer, e->value.integer, 1);
2040 sym->as->upper[dim] = e;
2042 else
2044 gfc_error ("Non-constant lower bound in implied-shape"
2045 " declaration at %L", &lower->where);
2046 return false;
2050 sym->as->type = AS_EXPLICIT;
2053 /* Need to check if the expression we initialized this
2054 to was one of the iso_c_binding named constants. If so,
2055 and we're a parameter (constant), let it be iso_c.
2056 For example:
2057 integer(c_int), parameter :: my_int = c_int
2058 integer(my_int) :: my_int_2
2059 If we mark my_int as iso_c (since we can see it's value
2060 is equal to one of the named constants), then my_int_2
2061 will be considered C interoperable. */
2062 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2064 sym->ts.is_iso_c |= init->ts.is_iso_c;
2065 sym->ts.is_c_interop |= init->ts.is_c_interop;
2066 /* attr bits needed for module files. */
2067 sym->attr.is_iso_c |= init->ts.is_iso_c;
2068 sym->attr.is_c_interop |= init->ts.is_c_interop;
2069 if (init->ts.is_iso_c)
2070 sym->ts.f90_type = init->ts.f90_type;
2073 /* Add initializer. Make sure we keep the ranks sane. */
2074 if (sym->attr.dimension && init->rank == 0)
2076 mpz_t size;
2077 gfc_expr *array;
2078 int n;
2079 if (sym->attr.flavor == FL_PARAMETER
2080 && init->expr_type == EXPR_CONSTANT
2081 && spec_size (sym->as, &size)
2082 && mpz_cmp_si (size, 0) > 0)
2084 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2085 &init->where);
2086 for (n = 0; n < (int)mpz_get_si (size); n++)
2087 gfc_constructor_append_expr (&array->value.constructor,
2088 n == 0
2089 ? init
2090 : gfc_copy_expr (init),
2091 &init->where);
2093 array->shape = gfc_get_shape (sym->as->rank);
2094 for (n = 0; n < sym->as->rank; n++)
2095 spec_dimen_size (sym->as, n, &array->shape[n]);
2097 init = array;
2098 mpz_clear (size);
2100 init->rank = sym->as->rank;
2103 sym->value = init;
2104 if (sym->attr.save == SAVE_NONE)
2105 sym->attr.save = SAVE_IMPLICIT;
2106 *initp = NULL;
2109 return true;
2113 /* Function called by variable_decl() that adds a name to a structure
2114 being built. */
2116 static bool
2117 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2118 gfc_array_spec **as)
2120 gfc_state_data *s;
2121 gfc_component *c;
2123 /* F03:C438/C439. If the current symbol is of the same derived type that we're
2124 constructing, it must have the pointer attribute. */
2125 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2126 && current_ts.u.derived == gfc_current_block ()
2127 && current_attr.pointer == 0)
2129 if (current_attr.allocatable
2130 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2131 "must have the POINTER attribute"))
2133 return false;
2135 else if (current_attr.allocatable == 0)
2137 gfc_error ("Component at %C must have the POINTER attribute");
2138 return false;
2142 /* F03:C437. */
2143 if (current_ts.type == BT_CLASS
2144 && !(current_attr.pointer || current_attr.allocatable))
2146 gfc_error ("Component %qs with CLASS at %C must be allocatable "
2147 "or pointer", name);
2148 return false;
2151 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2153 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2155 gfc_error ("Array component of structure at %C must have explicit "
2156 "or deferred shape");
2157 return false;
2161 /* If we are in a nested union/map definition, gfc_add_component will not
2162 properly find repeated components because:
2163 (i) gfc_add_component does a flat search, where components of unions
2164 and maps are implicity chained so nested components may conflict.
2165 (ii) Unions and maps are not linked as components of their parent
2166 structures until after they are parsed.
2167 For (i) we use gfc_find_component which searches recursively, and for (ii)
2168 we search each block directly from the parse stack until we find the top
2169 level structure. */
2171 s = gfc_state_stack;
2172 if (s->state == COMP_UNION || s->state == COMP_MAP)
2174 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2176 c = gfc_find_component (s->sym, name, true, true, NULL);
2177 if (c != NULL)
2179 gfc_error_now ("Component %qs at %C already declared at %L",
2180 name, &c->loc);
2181 return false;
2183 /* Break after we've searched the entire chain. */
2184 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2185 break;
2186 s = s->previous;
2190 if (!gfc_add_component (gfc_current_block(), name, &c))
2191 return false;
2193 c->ts = current_ts;
2194 if (c->ts.type == BT_CHARACTER)
2195 c->ts.u.cl = cl;
2197 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2198 && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2199 && saved_kind_expr != NULL)
2200 c->kind_expr = gfc_copy_expr (saved_kind_expr);
2202 c->attr = current_attr;
2204 c->initializer = *init;
2205 *init = NULL;
2207 c->as = *as;
2208 if (c->as != NULL)
2210 if (c->as->corank)
2211 c->attr.codimension = 1;
2212 if (c->as->rank)
2213 c->attr.dimension = 1;
2215 *as = NULL;
2217 gfc_apply_init (&c->ts, &c->attr, c->initializer);
2219 /* Check array components. */
2220 if (!c->attr.dimension)
2221 goto scalar;
2223 if (c->attr.pointer)
2225 if (c->as->type != AS_DEFERRED)
2227 gfc_error ("Pointer array component of structure at %C must have a "
2228 "deferred shape");
2229 return false;
2232 else if (c->attr.allocatable)
2234 if (c->as->type != AS_DEFERRED)
2236 gfc_error ("Allocatable component of structure at %C must have a "
2237 "deferred shape");
2238 return false;
2241 else
2243 if (c->as->type != AS_EXPLICIT)
2245 gfc_error ("Array component of structure at %C must have an "
2246 "explicit shape");
2247 return false;
2251 scalar:
2252 if (c->ts.type == BT_CLASS)
2253 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2255 if (c->attr.pdt_kind || c->attr.pdt_len)
2257 gfc_symbol *sym;
2258 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2259 0, &sym);
2260 if (sym == NULL)
2262 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2263 "in the type parameter name list at %L",
2264 c->name, &gfc_current_block ()->declared_at);
2265 return false;
2267 sym->ts = c->ts;
2268 sym->attr.pdt_kind = c->attr.pdt_kind;
2269 sym->attr.pdt_len = c->attr.pdt_len;
2270 if (c->initializer)
2271 sym->value = gfc_copy_expr (c->initializer);
2272 sym->attr.flavor = FL_VARIABLE;
2275 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2276 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2277 && decl_type_param_list)
2278 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2280 return true;
2284 /* Match a 'NULL()', and possibly take care of some side effects. */
2286 match
2287 gfc_match_null (gfc_expr **result)
2289 gfc_symbol *sym;
2290 match m, m2 = MATCH_NO;
2292 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2293 return MATCH_ERROR;
2295 if (m == MATCH_NO)
2297 locus old_loc;
2298 char name[GFC_MAX_SYMBOL_LEN + 1];
2300 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2301 return m2;
2303 old_loc = gfc_current_locus;
2304 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2305 return MATCH_ERROR;
2306 if (m2 != MATCH_YES
2307 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2308 return MATCH_ERROR;
2309 if (m2 == MATCH_NO)
2311 gfc_current_locus = old_loc;
2312 return MATCH_NO;
2316 /* The NULL symbol now has to be/become an intrinsic function. */
2317 if (gfc_get_symbol ("null", NULL, &sym))
2319 gfc_error ("NULL() initialization at %C is ambiguous");
2320 return MATCH_ERROR;
2323 gfc_intrinsic_symbol (sym);
2325 if (sym->attr.proc != PROC_INTRINSIC
2326 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2327 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2328 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2329 return MATCH_ERROR;
2331 *result = gfc_get_null_expr (&gfc_current_locus);
2333 /* Invalid per F2008, C512. */
2334 if (m2 == MATCH_YES)
2336 gfc_error ("NULL() initialization at %C may not have MOLD");
2337 return MATCH_ERROR;
2340 return MATCH_YES;
2344 /* Match the initialization expr for a data pointer or procedure pointer. */
2346 static match
2347 match_pointer_init (gfc_expr **init, int procptr)
2349 match m;
2351 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2353 gfc_error ("Initialization of pointer at %C is not allowed in "
2354 "a PURE procedure");
2355 return MATCH_ERROR;
2357 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2359 /* Match NULL() initialization. */
2360 m = gfc_match_null (init);
2361 if (m != MATCH_NO)
2362 return m;
2364 /* Match non-NULL initialization. */
2365 gfc_matching_ptr_assignment = !procptr;
2366 gfc_matching_procptr_assignment = procptr;
2367 m = gfc_match_rvalue (init);
2368 gfc_matching_ptr_assignment = 0;
2369 gfc_matching_procptr_assignment = 0;
2370 if (m == MATCH_ERROR)
2371 return MATCH_ERROR;
2372 else if (m == MATCH_NO)
2374 gfc_error ("Error in pointer initialization at %C");
2375 return MATCH_ERROR;
2378 if (!procptr && !gfc_resolve_expr (*init))
2379 return MATCH_ERROR;
2381 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2382 "initialization at %C"))
2383 return MATCH_ERROR;
2385 return MATCH_YES;
2389 static bool
2390 check_function_name (char *name)
2392 /* In functions that have a RESULT variable defined, the function name always
2393 refers to function calls. Therefore, the name is not allowed to appear in
2394 specification statements. When checking this, be careful about
2395 'hidden' procedure pointer results ('ppr@'). */
2397 if (gfc_current_state () == COMP_FUNCTION)
2399 gfc_symbol *block = gfc_current_block ();
2400 if (block && block->result && block->result != block
2401 && strcmp (block->result->name, "ppr@") != 0
2402 && strcmp (block->name, name) == 0)
2404 gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2405 "from appearing in a specification statement",
2406 block->result->name, &block->result->declared_at, name);
2407 return false;
2411 return true;
2415 /* Match a variable name with an optional initializer. When this
2416 subroutine is called, a variable is expected to be parsed next.
2417 Depending on what is happening at the moment, updates either the
2418 symbol table or the current interface. */
2420 static match
2421 variable_decl (int elem)
2423 char name[GFC_MAX_SYMBOL_LEN + 1];
2424 static unsigned int fill_id = 0;
2425 gfc_expr *initializer, *char_len;
2426 gfc_array_spec *as;
2427 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2428 gfc_charlen *cl;
2429 bool cl_deferred;
2430 locus var_locus;
2431 match m;
2432 bool t;
2433 gfc_symbol *sym;
2434 char c;
2436 initializer = NULL;
2437 as = NULL;
2438 cp_as = NULL;
2440 /* When we get here, we've just matched a list of attributes and
2441 maybe a type and a double colon. The next thing we expect to see
2442 is the name of the symbol. */
2444 /* If we are parsing a structure with legacy support, we allow the symbol
2445 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2446 m = MATCH_NO;
2447 gfc_gobble_whitespace ();
2448 c = gfc_peek_ascii_char ();
2449 if (c == '%')
2451 gfc_next_ascii_char (); /* Burn % character. */
2452 m = gfc_match ("fill");
2453 if (m == MATCH_YES)
2455 if (gfc_current_state () != COMP_STRUCTURE)
2457 if (flag_dec_structure)
2458 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2459 else
2460 gfc_error ("%qs at %C is a DEC extension, enable with "
2461 "%<-fdec-structure%>", "%FILL");
2462 m = MATCH_ERROR;
2463 goto cleanup;
2466 if (attr_seen)
2468 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2469 m = MATCH_ERROR;
2470 goto cleanup;
2473 /* %FILL components are given invalid fortran names. */
2474 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2476 else
2478 gfc_error ("Invalid character %qc in variable name at %C", c);
2479 return MATCH_ERROR;
2482 else
2484 m = gfc_match_name (name);
2485 if (m != MATCH_YES)
2486 goto cleanup;
2489 var_locus = gfc_current_locus;
2491 /* Now we could see the optional array spec. or character length. */
2492 m = gfc_match_array_spec (&as, true, true);
2493 if (m == MATCH_ERROR)
2494 goto cleanup;
2496 if (m == MATCH_NO)
2497 as = gfc_copy_array_spec (current_as);
2498 else if (current_as
2499 && !merge_array_spec (current_as, as, true))
2501 m = MATCH_ERROR;
2502 goto cleanup;
2505 if (flag_cray_pointer)
2506 cp_as = gfc_copy_array_spec (as);
2508 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2509 determine (and check) whether it can be implied-shape. If it
2510 was parsed as assumed-size, change it because PARAMETERs cannot
2511 be assumed-size.
2513 An explicit-shape-array cannot appear under several conditions.
2514 That check is done here as well. */
2515 if (as)
2517 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2519 m = MATCH_ERROR;
2520 gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
2521 name, &var_locus);
2522 goto cleanup;
2525 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2526 && current_attr.flavor == FL_PARAMETER)
2527 as->type = AS_IMPLIED_SHAPE;
2529 if (as->type == AS_IMPLIED_SHAPE
2530 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2531 &var_locus))
2533 m = MATCH_ERROR;
2534 goto cleanup;
2537 gfc_seen_div0 = false;
2539 /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2540 constant expressions shall appear only in a subprogram, derived
2541 type definition, BLOCK construct, or interface body. */
2542 if (as->type == AS_EXPLICIT
2543 && gfc_current_state () != COMP_BLOCK
2544 && gfc_current_state () != COMP_DERIVED
2545 && gfc_current_state () != COMP_FUNCTION
2546 && gfc_current_state () != COMP_INTERFACE
2547 && gfc_current_state () != COMP_SUBROUTINE)
2549 gfc_expr *e;
2550 bool not_constant = false;
2552 for (int i = 0; i < as->rank; i++)
2554 e = gfc_copy_expr (as->lower[i]);
2555 if (!gfc_resolve_expr (e) && gfc_seen_div0)
2557 m = MATCH_ERROR;
2558 goto cleanup;
2561 gfc_simplify_expr (e, 0);
2562 if (e && (e->expr_type != EXPR_CONSTANT))
2564 not_constant = true;
2565 break;
2567 gfc_free_expr (e);
2569 e = gfc_copy_expr (as->upper[i]);
2570 if (!gfc_resolve_expr (e) && gfc_seen_div0)
2572 m = MATCH_ERROR;
2573 goto cleanup;
2576 gfc_simplify_expr (e, 0);
2577 if (e && (e->expr_type != EXPR_CONSTANT))
2579 not_constant = true;
2580 break;
2582 gfc_free_expr (e);
2585 if (not_constant)
2587 gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2588 m = MATCH_ERROR;
2589 goto cleanup;
2592 if (as->type == AS_EXPLICIT)
2594 for (int i = 0; i < as->rank; i++)
2596 gfc_expr *e, *n;
2597 e = as->lower[i];
2598 if (e->expr_type != EXPR_CONSTANT)
2600 n = gfc_copy_expr (e);
2601 if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2603 m = MATCH_ERROR;
2604 goto cleanup;
2607 if (n->expr_type == EXPR_CONSTANT)
2608 gfc_replace_expr (e, n);
2609 else
2610 gfc_free_expr (n);
2612 e = as->upper[i];
2613 if (e->expr_type != EXPR_CONSTANT)
2615 n = gfc_copy_expr (e);
2616 if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2618 m = MATCH_ERROR;
2619 goto cleanup;
2622 if (n->expr_type == EXPR_CONSTANT)
2623 gfc_replace_expr (e, n);
2624 else
2625 gfc_free_expr (n);
2631 char_len = NULL;
2632 cl = NULL;
2633 cl_deferred = false;
2635 if (current_ts.type == BT_CHARACTER)
2637 switch (match_char_length (&char_len, &cl_deferred, false))
2639 case MATCH_YES:
2640 cl = gfc_new_charlen (gfc_current_ns, NULL);
2642 cl->length = char_len;
2643 break;
2645 /* Non-constant lengths need to be copied after the first
2646 element. Also copy assumed lengths. */
2647 case MATCH_NO:
2648 if (elem > 1
2649 && (current_ts.u.cl->length == NULL
2650 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2652 cl = gfc_new_charlen (gfc_current_ns, NULL);
2653 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2655 else
2656 cl = current_ts.u.cl;
2658 cl_deferred = current_ts.deferred;
2660 break;
2662 case MATCH_ERROR:
2663 goto cleanup;
2667 /* The dummy arguments and result of the abreviated form of MODULE
2668 PROCEDUREs, used in SUBMODULES should not be redefined. */
2669 if (gfc_current_ns->proc_name
2670 && gfc_current_ns->proc_name->abr_modproc_decl)
2672 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2673 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2675 m = MATCH_ERROR;
2676 gfc_error ("%qs at %C is a redefinition of the declaration "
2677 "in the corresponding interface for MODULE "
2678 "PROCEDURE %qs", sym->name,
2679 gfc_current_ns->proc_name->name);
2680 goto cleanup;
2684 /* %FILL components may not have initializers. */
2685 if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
2687 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2688 m = MATCH_ERROR;
2689 goto cleanup;
2692 /* If this symbol has already shown up in a Cray Pointer declaration,
2693 and this is not a component declaration,
2694 then we want to set the type & bail out. */
2695 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2697 gfc_find_symbol (name, gfc_current_ns, 0, &sym);
2698 if (sym != NULL && sym->attr.cray_pointee)
2700 m = MATCH_YES;
2701 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
2703 m = MATCH_ERROR;
2704 goto cleanup;
2707 /* Check to see if we have an array specification. */
2708 if (cp_as != NULL)
2710 if (sym->as != NULL)
2712 gfc_error ("Duplicate array spec for Cray pointee at %C");
2713 gfc_free_array_spec (cp_as);
2714 m = MATCH_ERROR;
2715 goto cleanup;
2717 else
2719 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2720 gfc_internal_error ("Cannot set pointee array spec.");
2722 /* Fix the array spec. */
2723 m = gfc_mod_pointee_as (sym->as);
2724 if (m == MATCH_ERROR)
2725 goto cleanup;
2728 goto cleanup;
2730 else
2732 gfc_free_array_spec (cp_as);
2736 /* Procedure pointer as function result. */
2737 if (gfc_current_state () == COMP_FUNCTION
2738 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2739 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2740 strcpy (name, "ppr@");
2742 if (gfc_current_state () == COMP_FUNCTION
2743 && strcmp (name, gfc_current_block ()->name) == 0
2744 && gfc_current_block ()->result
2745 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2746 strcpy (name, "ppr@");
2748 /* OK, we've successfully matched the declaration. Now put the
2749 symbol in the current namespace, because it might be used in the
2750 optional initialization expression for this symbol, e.g. this is
2751 perfectly legal:
2753 integer, parameter :: i = huge(i)
2755 This is only true for parameters or variables of a basic type.
2756 For components of derived types, it is not true, so we don't
2757 create a symbol for those yet. If we fail to create the symbol,
2758 bail out. */
2759 if (!gfc_comp_struct (gfc_current_state ())
2760 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2762 m = MATCH_ERROR;
2763 goto cleanup;
2766 if (!check_function_name (name))
2768 m = MATCH_ERROR;
2769 goto cleanup;
2772 /* We allow old-style initializations of the form
2773 integer i /2/, j(4) /3*3, 1/
2774 (if no colon has been seen). These are different from data
2775 statements in that initializers are only allowed to apply to the
2776 variable immediately preceding, i.e.
2777 integer i, j /1, 2/
2778 is not allowed. Therefore we have to do some work manually, that
2779 could otherwise be left to the matchers for DATA statements. */
2781 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2783 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2784 "initialization at %C"))
2785 return MATCH_ERROR;
2787 /* Allow old style initializations for components of STRUCTUREs and MAPs
2788 but not components of derived types. */
2789 else if (gfc_current_state () == COMP_DERIVED)
2791 gfc_error ("Invalid old style initialization for derived type "
2792 "component at %C");
2793 m = MATCH_ERROR;
2794 goto cleanup;
2797 /* For structure components, read the initializer as a special
2798 expression and let the rest of this function apply the initializer
2799 as usual. */
2800 else if (gfc_comp_struct (gfc_current_state ()))
2802 m = match_clist_expr (&initializer, &current_ts, as);
2803 if (m == MATCH_NO)
2804 gfc_error ("Syntax error in old style initialization of %s at %C",
2805 name);
2806 if (m != MATCH_YES)
2807 goto cleanup;
2810 /* Otherwise we treat the old style initialization just like a
2811 DATA declaration for the current variable. */
2812 else
2813 return match_old_style_init (name);
2816 /* The double colon must be present in order to have initializers.
2817 Otherwise the statement is ambiguous with an assignment statement. */
2818 if (colon_seen)
2820 if (gfc_match (" =>") == MATCH_YES)
2822 if (!current_attr.pointer)
2824 gfc_error ("Initialization at %C isn't for a pointer variable");
2825 m = MATCH_ERROR;
2826 goto cleanup;
2829 m = match_pointer_init (&initializer, 0);
2830 if (m != MATCH_YES)
2831 goto cleanup;
2833 /* The target of a pointer initialization must have the SAVE
2834 attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope
2835 is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */
2836 if (initializer->expr_type == EXPR_VARIABLE
2837 && initializer->symtree->n.sym->attr.save == SAVE_NONE
2838 && (gfc_current_state () == COMP_PROGRAM
2839 || gfc_current_state () == COMP_MODULE
2840 || gfc_current_state () == COMP_SUBMODULE))
2841 initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
2843 else if (gfc_match_char ('=') == MATCH_YES)
2845 if (current_attr.pointer)
2847 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2848 "not %<=%>");
2849 m = MATCH_ERROR;
2850 goto cleanup;
2853 m = gfc_match_init_expr (&initializer);
2854 if (m == MATCH_NO)
2856 gfc_error ("Expected an initialization expression at %C");
2857 m = MATCH_ERROR;
2860 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2861 && !gfc_comp_struct (gfc_state_stack->state))
2863 gfc_error ("Initialization of variable at %C is not allowed in "
2864 "a PURE procedure");
2865 m = MATCH_ERROR;
2868 if (current_attr.flavor != FL_PARAMETER
2869 && !gfc_comp_struct (gfc_state_stack->state))
2870 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2872 if (m != MATCH_YES)
2873 goto cleanup;
2877 if (initializer != NULL && current_attr.allocatable
2878 && gfc_comp_struct (gfc_current_state ()))
2880 gfc_error ("Initialization of allocatable component at %C is not "
2881 "allowed");
2882 m = MATCH_ERROR;
2883 goto cleanup;
2886 if (gfc_current_state () == COMP_DERIVED
2887 && gfc_current_block ()->attr.pdt_template)
2889 gfc_symbol *param;
2890 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2891 0, &param);
2892 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2894 gfc_error ("The component with KIND or LEN attribute at %C does not "
2895 "not appear in the type parameter list at %L",
2896 &gfc_current_block ()->declared_at);
2897 m = MATCH_ERROR;
2898 goto cleanup;
2900 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2902 gfc_error ("The component at %C that appears in the type parameter "
2903 "list at %L has neither the KIND nor LEN attribute",
2904 &gfc_current_block ()->declared_at);
2905 m = MATCH_ERROR;
2906 goto cleanup;
2908 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2910 gfc_error ("The component at %C which is a type parameter must be "
2911 "a scalar");
2912 m = MATCH_ERROR;
2913 goto cleanup;
2915 else if (param && initializer)
2916 param->value = gfc_copy_expr (initializer);
2919 /* Before adding a possible initilizer, do a simple check for compatibility
2920 of lhs and rhs types. Assigning a REAL value to a derived type is not a
2921 good thing. */
2922 if (current_ts.type == BT_DERIVED && initializer
2923 && (gfc_numeric_ts (&initializer->ts)
2924 || initializer->ts.type == BT_LOGICAL
2925 || initializer->ts.type == BT_CHARACTER))
2927 gfc_error ("Incompatible initialization between a derived type "
2928 "entity and an entity with %qs type at %C",
2929 gfc_typename (initializer));
2930 m = MATCH_ERROR;
2931 goto cleanup;
2935 /* Add the initializer. Note that it is fine if initializer is
2936 NULL here, because we sometimes also need to check if a
2937 declaration *must* have an initialization expression. */
2938 if (!gfc_comp_struct (gfc_current_state ()))
2939 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2940 else
2942 if (current_ts.type == BT_DERIVED
2943 && !current_attr.pointer && !initializer)
2944 initializer = gfc_default_initializer (&current_ts);
2945 t = build_struct (name, cl, &initializer, &as);
2947 /* If we match a nested structure definition we expect to see the
2948 * body even if the variable declarations blow up, so we need to keep
2949 * the structure declaration around. */
2950 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2951 gfc_commit_symbol (gfc_new_block);
2954 m = (t) ? MATCH_YES : MATCH_ERROR;
2956 cleanup:
2957 /* Free stuff up and return. */
2958 gfc_seen_div0 = false;
2959 gfc_free_expr (initializer);
2960 gfc_free_array_spec (as);
2962 return m;
2966 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2967 This assumes that the byte size is equal to the kind number for
2968 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2970 match
2971 gfc_match_old_kind_spec (gfc_typespec *ts)
2973 match m;
2974 int original_kind;
2976 if (gfc_match_char ('*') != MATCH_YES)
2977 return MATCH_NO;
2979 m = gfc_match_small_literal_int (&ts->kind, NULL);
2980 if (m != MATCH_YES)
2981 return MATCH_ERROR;
2983 original_kind = ts->kind;
2985 /* Massage the kind numbers for complex types. */
2986 if (ts->type == BT_COMPLEX)
2988 if (ts->kind % 2)
2990 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2991 gfc_basic_typename (ts->type), original_kind);
2992 return MATCH_ERROR;
2994 ts->kind /= 2;
2998 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2999 ts->kind = 8;
3001 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3003 if (ts->kind == 4)
3005 if (flag_real4_kind == 8)
3006 ts->kind = 8;
3007 if (flag_real4_kind == 10)
3008 ts->kind = 10;
3009 if (flag_real4_kind == 16)
3010 ts->kind = 16;
3013 if (ts->kind == 8)
3015 if (flag_real8_kind == 4)
3016 ts->kind = 4;
3017 if (flag_real8_kind == 10)
3018 ts->kind = 10;
3019 if (flag_real8_kind == 16)
3020 ts->kind = 16;
3024 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3026 gfc_error ("Old-style type declaration %s*%d not supported at %C",
3027 gfc_basic_typename (ts->type), original_kind);
3028 return MATCH_ERROR;
3031 if (!gfc_notify_std (GFC_STD_GNU,
3032 "Nonstandard type declaration %s*%d at %C",
3033 gfc_basic_typename(ts->type), original_kind))
3034 return MATCH_ERROR;
3036 return MATCH_YES;
3040 /* Match a kind specification. Since kinds are generally optional, we
3041 usually return MATCH_NO if something goes wrong. If a "kind="
3042 string is found, then we know we have an error. */
3044 match
3045 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
3047 locus where, loc;
3048 gfc_expr *e;
3049 match m, n;
3050 char c;
3052 m = MATCH_NO;
3053 n = MATCH_YES;
3054 e = NULL;
3055 saved_kind_expr = NULL;
3057 where = loc = gfc_current_locus;
3059 if (kind_expr_only)
3060 goto kind_expr;
3062 if (gfc_match_char ('(') == MATCH_NO)
3063 return MATCH_NO;
3065 /* Also gobbles optional text. */
3066 if (gfc_match (" kind = ") == MATCH_YES)
3067 m = MATCH_ERROR;
3069 loc = gfc_current_locus;
3071 kind_expr:
3073 n = gfc_match_init_expr (&e);
3075 if (gfc_derived_parameter_expr (e))
3077 ts->kind = 0;
3078 saved_kind_expr = gfc_copy_expr (e);
3079 goto close_brackets;
3082 if (n != MATCH_YES)
3084 if (gfc_matching_function)
3086 /* The function kind expression might include use associated or
3087 imported parameters and try again after the specification
3088 expressions..... */
3089 if (gfc_match_char (')') != MATCH_YES)
3091 gfc_error ("Missing right parenthesis at %C");
3092 m = MATCH_ERROR;
3093 goto no_match;
3096 gfc_free_expr (e);
3097 gfc_undo_symbols ();
3098 return MATCH_YES;
3100 else
3102 /* ....or else, the match is real. */
3103 if (n == MATCH_NO)
3104 gfc_error ("Expected initialization expression at %C");
3105 if (n != MATCH_YES)
3106 return MATCH_ERROR;
3110 if (e->rank != 0)
3112 gfc_error ("Expected scalar initialization expression at %C");
3113 m = MATCH_ERROR;
3114 goto no_match;
3117 if (gfc_extract_int (e, &ts->kind, 1))
3119 m = MATCH_ERROR;
3120 goto no_match;
3123 /* Before throwing away the expression, let's see if we had a
3124 C interoperable kind (and store the fact). */
3125 if (e->ts.is_c_interop == 1)
3127 /* Mark this as C interoperable if being declared with one
3128 of the named constants from iso_c_binding. */
3129 ts->is_c_interop = e->ts.is_iso_c;
3130 ts->f90_type = e->ts.f90_type;
3131 if (e->symtree)
3132 ts->interop_kind = e->symtree->n.sym;
3135 gfc_free_expr (e);
3136 e = NULL;
3138 /* Ignore errors to this point, if we've gotten here. This means
3139 we ignore the m=MATCH_ERROR from above. */
3140 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3142 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3143 gfc_basic_typename (ts->type));
3144 gfc_current_locus = where;
3145 return MATCH_ERROR;
3148 /* Warn if, e.g., c_int is used for a REAL variable, but not
3149 if, e.g., c_double is used for COMPLEX as the standard
3150 explicitly says that the kind type parameter for complex and real
3151 variable is the same, i.e. c_float == c_float_complex. */
3152 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3153 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3154 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3155 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3156 "is %s", gfc_basic_typename (ts->f90_type), &where,
3157 gfc_basic_typename (ts->type));
3159 close_brackets:
3161 gfc_gobble_whitespace ();
3162 if ((c = gfc_next_ascii_char ()) != ')'
3163 && (ts->type != BT_CHARACTER || c != ','))
3165 if (ts->type == BT_CHARACTER)
3166 gfc_error ("Missing right parenthesis or comma at %C");
3167 else
3168 gfc_error ("Missing right parenthesis at %C");
3169 m = MATCH_ERROR;
3171 else
3172 /* All tests passed. */
3173 m = MATCH_YES;
3175 if(m == MATCH_ERROR)
3176 gfc_current_locus = where;
3178 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3179 ts->kind = 8;
3181 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3183 if (ts->kind == 4)
3185 if (flag_real4_kind == 8)
3186 ts->kind = 8;
3187 if (flag_real4_kind == 10)
3188 ts->kind = 10;
3189 if (flag_real4_kind == 16)
3190 ts->kind = 16;
3193 if (ts->kind == 8)
3195 if (flag_real8_kind == 4)
3196 ts->kind = 4;
3197 if (flag_real8_kind == 10)
3198 ts->kind = 10;
3199 if (flag_real8_kind == 16)
3200 ts->kind = 16;
3204 /* Return what we know from the test(s). */
3205 return m;
3207 no_match:
3208 gfc_free_expr (e);
3209 gfc_current_locus = where;
3210 return m;
3214 static match
3215 match_char_kind (int * kind, int * is_iso_c)
3217 locus where;
3218 gfc_expr *e;
3219 match m, n;
3220 bool fail;
3222 m = MATCH_NO;
3223 e = NULL;
3224 where = gfc_current_locus;
3226 n = gfc_match_init_expr (&e);
3228 if (n != MATCH_YES && gfc_matching_function)
3230 /* The expression might include use-associated or imported
3231 parameters and try again after the specification
3232 expressions. */
3233 gfc_free_expr (e);
3234 gfc_undo_symbols ();
3235 return MATCH_YES;
3238 if (n == MATCH_NO)
3239 gfc_error ("Expected initialization expression at %C");
3240 if (n != MATCH_YES)
3241 return MATCH_ERROR;
3243 if (e->rank != 0)
3245 gfc_error ("Expected scalar initialization expression at %C");
3246 m = MATCH_ERROR;
3247 goto no_match;
3250 if (gfc_derived_parameter_expr (e))
3252 saved_kind_expr = e;
3253 *kind = 0;
3254 return MATCH_YES;
3257 fail = gfc_extract_int (e, kind, 1);
3258 *is_iso_c = e->ts.is_iso_c;
3259 if (fail)
3261 m = MATCH_ERROR;
3262 goto no_match;
3265 gfc_free_expr (e);
3267 /* Ignore errors to this point, if we've gotten here. This means
3268 we ignore the m=MATCH_ERROR from above. */
3269 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3271 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3272 m = MATCH_ERROR;
3274 else
3275 /* All tests passed. */
3276 m = MATCH_YES;
3278 if (m == MATCH_ERROR)
3279 gfc_current_locus = where;
3281 /* Return what we know from the test(s). */
3282 return m;
3284 no_match:
3285 gfc_free_expr (e);
3286 gfc_current_locus = where;
3287 return m;
3291 /* Match the various kind/length specifications in a CHARACTER
3292 declaration. We don't return MATCH_NO. */
3294 match
3295 gfc_match_char_spec (gfc_typespec *ts)
3297 int kind, seen_length, is_iso_c;
3298 gfc_charlen *cl;
3299 gfc_expr *len;
3300 match m;
3301 bool deferred;
3303 len = NULL;
3304 seen_length = 0;
3305 kind = 0;
3306 is_iso_c = 0;
3307 deferred = false;
3309 /* Try the old-style specification first. */
3310 old_char_selector = 0;
3312 m = match_char_length (&len, &deferred, true);
3313 if (m != MATCH_NO)
3315 if (m == MATCH_YES)
3316 old_char_selector = 1;
3317 seen_length = 1;
3318 goto done;
3321 m = gfc_match_char ('(');
3322 if (m != MATCH_YES)
3324 m = MATCH_YES; /* Character without length is a single char. */
3325 goto done;
3328 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3329 if (gfc_match (" kind =") == MATCH_YES)
3331 m = match_char_kind (&kind, &is_iso_c);
3333 if (m == MATCH_ERROR)
3334 goto done;
3335 if (m == MATCH_NO)
3336 goto syntax;
3338 if (gfc_match (" , len =") == MATCH_NO)
3339 goto rparen;
3341 m = char_len_param_value (&len, &deferred);
3342 if (m == MATCH_NO)
3343 goto syntax;
3344 if (m == MATCH_ERROR)
3345 goto done;
3346 seen_length = 1;
3348 goto rparen;
3351 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3352 if (gfc_match (" len =") == MATCH_YES)
3354 m = char_len_param_value (&len, &deferred);
3355 if (m == MATCH_NO)
3356 goto syntax;
3357 if (m == MATCH_ERROR)
3358 goto done;
3359 seen_length = 1;
3361 if (gfc_match_char (')') == MATCH_YES)
3362 goto done;
3364 if (gfc_match (" , kind =") != MATCH_YES)
3365 goto syntax;
3367 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3368 goto done;
3370 goto rparen;
3373 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3374 m = char_len_param_value (&len, &deferred);
3375 if (m == MATCH_NO)
3376 goto syntax;
3377 if (m == MATCH_ERROR)
3378 goto done;
3379 seen_length = 1;
3381 m = gfc_match_char (')');
3382 if (m == MATCH_YES)
3383 goto done;
3385 if (gfc_match_char (',') != MATCH_YES)
3386 goto syntax;
3388 gfc_match (" kind ="); /* Gobble optional text. */
3390 m = match_char_kind (&kind, &is_iso_c);
3391 if (m == MATCH_ERROR)
3392 goto done;
3393 if (m == MATCH_NO)
3394 goto syntax;
3396 rparen:
3397 /* Require a right-paren at this point. */
3398 m = gfc_match_char (')');
3399 if (m == MATCH_YES)
3400 goto done;
3402 syntax:
3403 gfc_error ("Syntax error in CHARACTER declaration at %C");
3404 m = MATCH_ERROR;
3405 gfc_free_expr (len);
3406 return m;
3408 done:
3409 /* Deal with character functions after USE and IMPORT statements. */
3410 if (gfc_matching_function)
3412 gfc_free_expr (len);
3413 gfc_undo_symbols ();
3414 return MATCH_YES;
3417 if (m != MATCH_YES)
3419 gfc_free_expr (len);
3420 return m;
3423 /* Do some final massaging of the length values. */
3424 cl = gfc_new_charlen (gfc_current_ns, NULL);
3426 if (seen_length == 0)
3427 cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3428 else
3430 /* If gfortran ends up here, then len may be reducible to a constant.
3431 Try to do that here. If it does not reduce, simply assign len to
3432 charlen. A complication occurs with user-defined generic functions,
3433 which are not resolved. Use a private namespace to deal with
3434 generic functions. */
3436 if (len && len->expr_type != EXPR_CONSTANT)
3438 gfc_namespace *old_ns;
3439 gfc_expr *e;
3441 old_ns = gfc_current_ns;
3442 gfc_current_ns = gfc_get_namespace (NULL, 0);
3444 e = gfc_copy_expr (len);
3445 gfc_reduce_init_expr (e);
3446 if (e->expr_type == EXPR_CONSTANT)
3448 gfc_replace_expr (len, e);
3449 if (mpz_cmp_si (len->value.integer, 0) < 0)
3450 mpz_set_ui (len->value.integer, 0);
3452 else
3453 gfc_free_expr (e);
3455 gfc_free_namespace (gfc_current_ns);
3456 gfc_current_ns = old_ns;
3459 cl->length = len;
3462 ts->u.cl = cl;
3463 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3464 ts->deferred = deferred;
3466 /* We have to know if it was a C interoperable kind so we can
3467 do accurate type checking of bind(c) procs, etc. */
3468 if (kind != 0)
3469 /* Mark this as C interoperable if being declared with one
3470 of the named constants from iso_c_binding. */
3471 ts->is_c_interop = is_iso_c;
3472 else if (len != NULL)
3473 /* Here, we might have parsed something such as: character(c_char)
3474 In this case, the parsing code above grabs the c_char when
3475 looking for the length (line 1690, roughly). it's the last
3476 testcase for parsing the kind params of a character variable.
3477 However, it's not actually the length. this seems like it
3478 could be an error.
3479 To see if the user used a C interop kind, test the expr
3480 of the so called length, and see if it's C interoperable. */
3481 ts->is_c_interop = len->ts.is_iso_c;
3483 return MATCH_YES;
3487 /* Matches a RECORD declaration. */
3489 static match
3490 match_record_decl (char *name)
3492 locus old_loc;
3493 old_loc = gfc_current_locus;
3494 match m;
3496 m = gfc_match (" record /");
3497 if (m == MATCH_YES)
3499 if (!flag_dec_structure)
3501 gfc_current_locus = old_loc;
3502 gfc_error ("RECORD at %C is an extension, enable it with "
3503 "%<-fdec-structure%>");
3504 return MATCH_ERROR;
3506 m = gfc_match (" %n/", name);
3507 if (m == MATCH_YES)
3508 return MATCH_YES;
3511 gfc_current_locus = old_loc;
3512 if (flag_dec_structure
3513 && (gfc_match (" record% ") == MATCH_YES
3514 || gfc_match (" record%t") == MATCH_YES))
3515 gfc_error ("Structure name expected after RECORD at %C");
3516 if (m == MATCH_NO)
3517 return MATCH_NO;
3519 return MATCH_ERROR;
3523 /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3524 of expressions to substitute into the possibly parameterized expression
3525 'e'. Using a list is inefficient but should not be too bad since the
3526 number of type parameters is not likely to be large. */
3527 static bool
3528 insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3529 int* f)
3531 gfc_actual_arglist *param;
3532 gfc_expr *copy;
3534 if (e->expr_type != EXPR_VARIABLE)
3535 return false;
3537 gcc_assert (e->symtree);
3538 if (e->symtree->n.sym->attr.pdt_kind
3539 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3541 for (param = type_param_spec_list; param; param = param->next)
3542 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3543 break;
3545 if (param)
3547 copy = gfc_copy_expr (param->expr);
3548 *e = *copy;
3549 free (copy);
3553 return false;
3557 bool
3558 gfc_insert_kind_parameter_exprs (gfc_expr *e)
3560 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3564 bool
3565 gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3567 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3568 type_param_spec_list = param_list;
3569 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3570 type_param_spec_list = NULL;
3571 type_param_spec_list = old_param_spec_list;
3574 /* Determines the instance of a parameterized derived type to be used by
3575 matching determining the values of the kind parameters and using them
3576 in the name of the instance. If the instance exists, it is used, otherwise
3577 a new derived type is created. */
3578 match
3579 gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3580 gfc_actual_arglist **ext_param_list)
3582 /* The PDT template symbol. */
3583 gfc_symbol *pdt = *sym;
3584 /* The symbol for the parameter in the template f2k_namespace. */
3585 gfc_symbol *param;
3586 /* The hoped for instance of the PDT. */
3587 gfc_symbol *instance;
3588 /* The list of parameters appearing in the PDT declaration. */
3589 gfc_formal_arglist *type_param_name_list;
3590 /* Used to store the parameter specification list during recursive calls. */
3591 gfc_actual_arglist *old_param_spec_list;
3592 /* Pointers to the parameter specification being used. */
3593 gfc_actual_arglist *actual_param;
3594 gfc_actual_arglist *tail = NULL;
3595 /* Used to build up the name of the PDT instance. The prefix uses 4
3596 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3597 char name[GFC_MAX_SYMBOL_LEN + 21];
3599 bool name_seen = (param_list == NULL);
3600 bool assumed_seen = false;
3601 bool deferred_seen = false;
3602 bool spec_error = false;
3603 int kind_value, i;
3604 gfc_expr *kind_expr;
3605 gfc_component *c1, *c2;
3606 match m;
3608 type_param_spec_list = NULL;
3610 type_param_name_list = pdt->formal;
3611 actual_param = param_list;
3612 sprintf (name, "Pdt%s", pdt->name);
3614 /* Run through the parameter name list and pick up the actual
3615 parameter values or use the default values in the PDT declaration. */
3616 for (; type_param_name_list;
3617 type_param_name_list = type_param_name_list->next)
3619 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3621 if (actual_param->spec_type == SPEC_ASSUMED)
3622 spec_error = deferred_seen;
3623 else
3624 spec_error = assumed_seen;
3626 if (spec_error)
3628 gfc_error ("The type parameter spec list at %C cannot contain "
3629 "both ASSUMED and DEFERRED parameters");
3630 goto error_return;
3634 if (actual_param && actual_param->name)
3635 name_seen = true;
3636 param = type_param_name_list->sym;
3638 if (!param || !param->name)
3639 continue;
3641 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3642 /* An error should already have been thrown in resolve.c
3643 (resolve_fl_derived0). */
3644 if (!pdt->attr.use_assoc && !c1)
3645 goto error_return;
3647 kind_expr = NULL;
3648 if (!name_seen)
3650 if (!actual_param && !(c1 && c1->initializer))
3652 gfc_error ("The type parameter spec list at %C does not contain "
3653 "enough parameter expressions");
3654 goto error_return;
3656 else if (!actual_param && c1 && c1->initializer)
3657 kind_expr = gfc_copy_expr (c1->initializer);
3658 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3659 kind_expr = gfc_copy_expr (actual_param->expr);
3661 else
3663 actual_param = param_list;
3664 for (;actual_param; actual_param = actual_param->next)
3665 if (actual_param->name
3666 && strcmp (actual_param->name, param->name) == 0)
3667 break;
3668 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3669 kind_expr = gfc_copy_expr (actual_param->expr);
3670 else
3672 if (c1->initializer)
3673 kind_expr = gfc_copy_expr (c1->initializer);
3674 else if (!(actual_param && param->attr.pdt_len))
3676 gfc_error ("The derived parameter %qs at %C does not "
3677 "have a default value", param->name);
3678 goto error_return;
3683 /* Store the current parameter expressions in a temporary actual
3684 arglist 'list' so that they can be substituted in the corresponding
3685 expressions in the PDT instance. */
3686 if (type_param_spec_list == NULL)
3688 type_param_spec_list = gfc_get_actual_arglist ();
3689 tail = type_param_spec_list;
3691 else
3693 tail->next = gfc_get_actual_arglist ();
3694 tail = tail->next;
3696 tail->name = param->name;
3698 if (kind_expr)
3700 /* Try simplification even for LEN expressions. */
3701 gfc_resolve_expr (kind_expr);
3702 gfc_simplify_expr (kind_expr, 1);
3703 /* Variable expressions seem to default to BT_PROCEDURE.
3704 TODO find out why this is and fix it. */
3705 if (kind_expr->ts.type != BT_INTEGER
3706 && kind_expr->ts.type != BT_PROCEDURE)
3708 gfc_error ("The parameter expression at %C must be of "
3709 "INTEGER type and not %s type",
3710 gfc_basic_typename (kind_expr->ts.type));
3711 goto error_return;
3714 tail->expr = gfc_copy_expr (kind_expr);
3717 if (actual_param)
3718 tail->spec_type = actual_param->spec_type;
3720 if (!param->attr.pdt_kind)
3722 if (!name_seen && actual_param)
3723 actual_param = actual_param->next;
3724 if (kind_expr)
3726 gfc_free_expr (kind_expr);
3727 kind_expr = NULL;
3729 continue;
3732 if (actual_param
3733 && (actual_param->spec_type == SPEC_ASSUMED
3734 || actual_param->spec_type == SPEC_DEFERRED))
3736 gfc_error ("The KIND parameter %qs at %C cannot either be "
3737 "ASSUMED or DEFERRED", param->name);
3738 goto error_return;
3741 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3743 gfc_error ("The value for the KIND parameter %qs at %C does not "
3744 "reduce to a constant expression", param->name);
3745 goto error_return;
3748 gfc_extract_int (kind_expr, &kind_value);
3749 sprintf (name + strlen (name), "_%d", kind_value);
3751 if (!name_seen && actual_param)
3752 actual_param = actual_param->next;
3753 gfc_free_expr (kind_expr);
3756 if (!name_seen && actual_param)
3758 gfc_error ("The type parameter spec list at %C contains too many "
3759 "parameter expressions");
3760 goto error_return;
3763 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3764 build it, using 'pdt' as a template. */
3765 if (gfc_get_symbol (name, pdt->ns, &instance))
3767 gfc_error ("Parameterized derived type at %C is ambiguous");
3768 goto error_return;
3771 m = MATCH_YES;
3773 if (instance->attr.flavor == FL_DERIVED
3774 && instance->attr.pdt_type)
3776 instance->refs++;
3777 if (ext_param_list)
3778 *ext_param_list = type_param_spec_list;
3779 *sym = instance;
3780 gfc_commit_symbols ();
3781 return m;
3784 /* Start building the new instance of the parameterized type. */
3785 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3786 instance->attr.pdt_template = 0;
3787 instance->attr.pdt_type = 1;
3788 instance->declared_at = gfc_current_locus;
3790 /* Add the components, replacing the parameters in all expressions
3791 with the expressions for their values in 'type_param_spec_list'. */
3792 c1 = pdt->components;
3793 tail = type_param_spec_list;
3794 for (; c1; c1 = c1->next)
3796 gfc_add_component (instance, c1->name, &c2);
3798 c2->ts = c1->ts;
3799 c2->attr = c1->attr;
3801 /* The order of declaration of the type_specs might not be the
3802 same as that of the components. */
3803 if (c1->attr.pdt_kind || c1->attr.pdt_len)
3805 for (tail = type_param_spec_list; tail; tail = tail->next)
3806 if (strcmp (c1->name, tail->name) == 0)
3807 break;
3810 /* Deal with type extension by recursively calling this function
3811 to obtain the instance of the extended type. */
3812 if (gfc_current_state () != COMP_DERIVED
3813 && c1 == pdt->components
3814 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3815 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3816 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3818 gfc_formal_arglist *f;
3820 old_param_spec_list = type_param_spec_list;
3822 /* Obtain a spec list appropriate to the extended type..*/
3823 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3824 type_param_spec_list = actual_param;
3825 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3826 actual_param = actual_param->next;
3827 if (actual_param)
3829 gfc_free_actual_arglist (actual_param->next);
3830 actual_param->next = NULL;
3833 /* Now obtain the PDT instance for the extended type. */
3834 c2->param_list = type_param_spec_list;
3835 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3836 NULL);
3837 type_param_spec_list = old_param_spec_list;
3839 c2->ts.u.derived->refs++;
3840 gfc_set_sym_referenced (c2->ts.u.derived);
3842 /* Set extension level. */
3843 if (c2->ts.u.derived->attr.extension == 255)
3845 /* Since the extension field is 8 bit wide, we can only have
3846 up to 255 extension levels. */
3847 gfc_error ("Maximum extension level reached with type %qs at %L",
3848 c2->ts.u.derived->name,
3849 &c2->ts.u.derived->declared_at);
3850 goto error_return;
3852 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3854 continue;
3857 /* Set the component kind using the parameterized expression. */
3858 if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3859 && c1->kind_expr != NULL)
3861 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3862 gfc_insert_kind_parameter_exprs (e);
3863 gfc_simplify_expr (e, 1);
3864 gfc_extract_int (e, &c2->ts.kind);
3865 gfc_free_expr (e);
3866 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3868 gfc_error ("Kind %d not supported for type %s at %C",
3869 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3870 goto error_return;
3874 /* Similarly, set the string length if parameterized. */
3875 if (c1->ts.type == BT_CHARACTER
3876 && c1->ts.u.cl->length
3877 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3879 gfc_expr *e;
3880 e = gfc_copy_expr (c1->ts.u.cl->length);
3881 gfc_insert_kind_parameter_exprs (e);
3882 gfc_simplify_expr (e, 1);
3883 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3884 c2->ts.u.cl->length = e;
3885 c2->attr.pdt_string = 1;
3888 /* Set up either the KIND/LEN initializer, if constant,
3889 or the parameterized expression. Use the template
3890 initializer if one is not already set in this instance. */
3891 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3893 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3894 c2->initializer = gfc_copy_expr (tail->expr);
3895 else if (tail && tail->expr)
3897 c2->param_list = gfc_get_actual_arglist ();
3898 c2->param_list->name = tail->name;
3899 c2->param_list->expr = gfc_copy_expr (tail->expr);
3900 c2->param_list->next = NULL;
3903 if (!c2->initializer && c1->initializer)
3904 c2->initializer = gfc_copy_expr (c1->initializer);
3907 /* Copy the array spec. */
3908 c2->as = gfc_copy_array_spec (c1->as);
3909 if (c1->ts.type == BT_CLASS)
3910 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
3912 /* Determine if an array spec is parameterized. If so, substitute
3913 in the parameter expressions for the bounds and set the pdt_array
3914 attribute. Notice that this attribute must be unconditionally set
3915 if this is an array of parameterized character length. */
3916 if (c1->as && c1->as->type == AS_EXPLICIT)
3918 bool pdt_array = false;
3920 /* Are the bounds of the array parameterized? */
3921 for (i = 0; i < c1->as->rank; i++)
3923 if (gfc_derived_parameter_expr (c1->as->lower[i]))
3924 pdt_array = true;
3925 if (gfc_derived_parameter_expr (c1->as->upper[i]))
3926 pdt_array = true;
3929 /* If they are, free the expressions for the bounds and
3930 replace them with the template expressions with substitute
3931 values. */
3932 for (i = 0; pdt_array && i < c1->as->rank; i++)
3934 gfc_expr *e;
3935 e = gfc_copy_expr (c1->as->lower[i]);
3936 gfc_insert_kind_parameter_exprs (e);
3937 gfc_simplify_expr (e, 1);
3938 gfc_free_expr (c2->as->lower[i]);
3939 c2->as->lower[i] = e;
3940 e = gfc_copy_expr (c1->as->upper[i]);
3941 gfc_insert_kind_parameter_exprs (e);
3942 gfc_simplify_expr (e, 1);
3943 gfc_free_expr (c2->as->upper[i]);
3944 c2->as->upper[i] = e;
3946 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
3947 if (c1->initializer)
3949 c2->initializer = gfc_copy_expr (c1->initializer);
3950 gfc_insert_kind_parameter_exprs (c2->initializer);
3951 gfc_simplify_expr (c2->initializer, 1);
3955 /* Recurse into this function for PDT components. */
3956 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3957 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
3959 gfc_actual_arglist *params;
3960 /* The component in the template has a list of specification
3961 expressions derived from its declaration. */
3962 params = gfc_copy_actual_arglist (c1->param_list);
3963 actual_param = params;
3964 /* Substitute the template parameters with the expressions
3965 from the specification list. */
3966 for (;actual_param; actual_param = actual_param->next)
3967 gfc_insert_parameter_exprs (actual_param->expr,
3968 type_param_spec_list);
3970 /* Now obtain the PDT instance for the component. */
3971 old_param_spec_list = type_param_spec_list;
3972 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
3973 type_param_spec_list = old_param_spec_list;
3975 c2->param_list = params;
3976 if (!(c2->attr.pointer || c2->attr.allocatable))
3977 c2->initializer = gfc_default_initializer (&c2->ts);
3979 if (c2->attr.allocatable)
3980 instance->attr.alloc_comp = 1;
3984 gfc_commit_symbol (instance);
3985 if (ext_param_list)
3986 *ext_param_list = type_param_spec_list;
3987 *sym = instance;
3988 return m;
3990 error_return:
3991 gfc_free_actual_arglist (type_param_spec_list);
3992 return MATCH_ERROR;
3996 /* Match a legacy nonstandard BYTE type-spec. */
3998 static match
3999 match_byte_typespec (gfc_typespec *ts)
4001 if (gfc_match (" byte") == MATCH_YES)
4003 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
4004 return MATCH_ERROR;
4006 if (gfc_current_form == FORM_FREE)
4008 char c = gfc_peek_ascii_char ();
4009 if (!gfc_is_whitespace (c) && c != ',')
4010 return MATCH_NO;
4013 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4015 gfc_error ("BYTE type used at %C "
4016 "is not available on the target machine");
4017 return MATCH_ERROR;
4020 ts->type = BT_INTEGER;
4021 ts->kind = 1;
4022 return MATCH_YES;
4024 return MATCH_NO;
4028 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
4029 structure to the matched specification. This is necessary for FUNCTION and
4030 IMPLICIT statements.
4032 If implicit_flag is nonzero, then we don't check for the optional
4033 kind specification. Not doing so is needed for matching an IMPLICIT
4034 statement correctly. */
4036 match
4037 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4039 char name[GFC_MAX_SYMBOL_LEN + 1];
4040 gfc_symbol *sym, *dt_sym;
4041 match m;
4042 char c;
4043 bool seen_deferred_kind, matched_type;
4044 const char *dt_name;
4046 decl_type_param_list = NULL;
4048 /* A belt and braces check that the typespec is correctly being treated
4049 as a deferred characteristic association. */
4050 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
4051 && (gfc_current_block ()->result->ts.kind == -1)
4052 && (ts->kind == -1);
4053 gfc_clear_ts (ts);
4054 if (seen_deferred_kind)
4055 ts->kind = -1;
4057 /* Clear the current binding label, in case one is given. */
4058 curr_binding_label = NULL;
4060 /* Match BYTE type-spec. */
4061 m = match_byte_typespec (ts);
4062 if (m != MATCH_NO)
4063 return m;
4065 m = gfc_match (" type (");
4066 matched_type = (m == MATCH_YES);
4067 if (matched_type)
4069 gfc_gobble_whitespace ();
4070 if (gfc_peek_ascii_char () == '*')
4072 if ((m = gfc_match ("*)")) != MATCH_YES)
4073 return m;
4074 if (gfc_comp_struct (gfc_current_state ()))
4076 gfc_error ("Assumed type at %C is not allowed for components");
4077 return MATCH_ERROR;
4079 if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4080 return MATCH_ERROR;
4081 ts->type = BT_ASSUMED;
4082 return MATCH_YES;
4085 m = gfc_match ("%n", name);
4086 matched_type = (m == MATCH_YES);
4089 if ((matched_type && strcmp ("integer", name) == 0)
4090 || (!matched_type && gfc_match (" integer") == MATCH_YES))
4092 ts->type = BT_INTEGER;
4093 ts->kind = gfc_default_integer_kind;
4094 goto get_kind;
4097 if ((matched_type && strcmp ("character", name) == 0)
4098 || (!matched_type && gfc_match (" character") == MATCH_YES))
4100 if (matched_type
4101 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4102 "intrinsic-type-spec at %C"))
4103 return MATCH_ERROR;
4105 ts->type = BT_CHARACTER;
4106 if (implicit_flag == 0)
4107 m = gfc_match_char_spec (ts);
4108 else
4109 m = MATCH_YES;
4111 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4113 gfc_error ("Malformed type-spec at %C");
4114 return MATCH_ERROR;
4117 return m;
4120 if ((matched_type && strcmp ("real", name) == 0)
4121 || (!matched_type && gfc_match (" real") == MATCH_YES))
4123 ts->type = BT_REAL;
4124 ts->kind = gfc_default_real_kind;
4125 goto get_kind;
4128 if ((matched_type
4129 && (strcmp ("doubleprecision", name) == 0
4130 || (strcmp ("double", name) == 0
4131 && gfc_match (" precision") == MATCH_YES)))
4132 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4134 if (matched_type
4135 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4136 "intrinsic-type-spec at %C"))
4137 return MATCH_ERROR;
4139 if (matched_type && gfc_match_char (')') != MATCH_YES)
4141 gfc_error ("Malformed type-spec at %C");
4142 return MATCH_ERROR;
4145 ts->type = BT_REAL;
4146 ts->kind = gfc_default_double_kind;
4147 return MATCH_YES;
4150 if ((matched_type && strcmp ("complex", name) == 0)
4151 || (!matched_type && gfc_match (" complex") == MATCH_YES))
4153 ts->type = BT_COMPLEX;
4154 ts->kind = gfc_default_complex_kind;
4155 goto get_kind;
4158 if ((matched_type
4159 && (strcmp ("doublecomplex", name) == 0
4160 || (strcmp ("double", name) == 0
4161 && gfc_match (" complex") == MATCH_YES)))
4162 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4164 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4165 return MATCH_ERROR;
4167 if (matched_type
4168 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4169 "intrinsic-type-spec at %C"))
4170 return MATCH_ERROR;
4172 if (matched_type && gfc_match_char (')') != MATCH_YES)
4174 gfc_error ("Malformed type-spec at %C");
4175 return MATCH_ERROR;
4178 ts->type = BT_COMPLEX;
4179 ts->kind = gfc_default_double_kind;
4180 return MATCH_YES;
4183 if ((matched_type && strcmp ("logical", name) == 0)
4184 || (!matched_type && gfc_match (" logical") == MATCH_YES))
4186 ts->type = BT_LOGICAL;
4187 ts->kind = gfc_default_logical_kind;
4188 goto get_kind;
4191 if (matched_type)
4193 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4194 if (m == MATCH_ERROR)
4195 return m;
4197 gfc_gobble_whitespace ();
4198 if (gfc_peek_ascii_char () != ')')
4200 gfc_error ("Malformed type-spec at %C");
4201 return MATCH_ERROR;
4203 m = gfc_match_char (')'); /* Burn closing ')'. */
4206 if (m != MATCH_YES)
4207 m = match_record_decl (name);
4209 if (matched_type || m == MATCH_YES)
4211 ts->type = BT_DERIVED;
4212 /* We accept record/s/ or type(s) where s is a structure, but we
4213 * don't need all the extra derived-type stuff for structures. */
4214 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4216 gfc_error ("Type name %qs at %C is ambiguous", name);
4217 return MATCH_ERROR;
4220 if (sym && sym->attr.flavor == FL_DERIVED
4221 && sym->attr.pdt_template
4222 && gfc_current_state () != COMP_DERIVED)
4224 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4225 if (m != MATCH_YES)
4226 return m;
4227 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4228 ts->u.derived = sym;
4229 strcpy (name, gfc_dt_lower_string (sym->name));
4232 if (sym && sym->attr.flavor == FL_STRUCT)
4234 ts->u.derived = sym;
4235 return MATCH_YES;
4237 /* Actually a derived type. */
4240 else
4242 /* Match nested STRUCTURE declarations; only valid within another
4243 structure declaration. */
4244 if (flag_dec_structure
4245 && (gfc_current_state () == COMP_STRUCTURE
4246 || gfc_current_state () == COMP_MAP))
4248 m = gfc_match (" structure");
4249 if (m == MATCH_YES)
4251 m = gfc_match_structure_decl ();
4252 if (m == MATCH_YES)
4254 /* gfc_new_block is updated by match_structure_decl. */
4255 ts->type = BT_DERIVED;
4256 ts->u.derived = gfc_new_block;
4257 return MATCH_YES;
4260 if (m == MATCH_ERROR)
4261 return MATCH_ERROR;
4264 /* Match CLASS declarations. */
4265 m = gfc_match (" class ( * )");
4266 if (m == MATCH_ERROR)
4267 return MATCH_ERROR;
4268 else if (m == MATCH_YES)
4270 gfc_symbol *upe;
4271 gfc_symtree *st;
4272 ts->type = BT_CLASS;
4273 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4274 if (upe == NULL)
4276 upe = gfc_new_symbol ("STAR", gfc_current_ns);
4277 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4278 st->n.sym = upe;
4279 gfc_set_sym_referenced (upe);
4280 upe->refs++;
4281 upe->ts.type = BT_VOID;
4282 upe->attr.unlimited_polymorphic = 1;
4283 /* This is essential to force the construction of
4284 unlimited polymorphic component class containers. */
4285 upe->attr.zero_comp = 1;
4286 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4287 &gfc_current_locus))
4288 return MATCH_ERROR;
4290 else
4292 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4293 st->n.sym = upe;
4294 upe->refs++;
4296 ts->u.derived = upe;
4297 return m;
4300 m = gfc_match (" class (");
4302 if (m == MATCH_YES)
4303 m = gfc_match ("%n", name);
4304 else
4305 return m;
4307 if (m != MATCH_YES)
4308 return m;
4309 ts->type = BT_CLASS;
4311 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4312 return MATCH_ERROR;
4314 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4315 if (m == MATCH_ERROR)
4316 return m;
4318 m = gfc_match_char (')');
4319 if (m != MATCH_YES)
4320 return m;
4323 /* Defer association of the derived type until the end of the
4324 specification block. However, if the derived type can be
4325 found, add it to the typespec. */
4326 if (gfc_matching_function)
4328 ts->u.derived = NULL;
4329 if (gfc_current_state () != COMP_INTERFACE
4330 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4332 sym = gfc_find_dt_in_generic (sym);
4333 ts->u.derived = sym;
4335 return MATCH_YES;
4338 /* Search for the name but allow the components to be defined later. If
4339 type = -1, this typespec has been seen in a function declaration but
4340 the type could not be accessed at that point. The actual derived type is
4341 stored in a symtree with the first letter of the name capitalized; the
4342 symtree with the all lower-case name contains the associated
4343 generic function. */
4344 dt_name = gfc_dt_upper_string (name);
4345 sym = NULL;
4346 dt_sym = NULL;
4347 if (ts->kind != -1)
4349 gfc_get_ha_symbol (name, &sym);
4350 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4352 gfc_error ("Type name %qs at %C is ambiguous", name);
4353 return MATCH_ERROR;
4355 if (sym->generic && !dt_sym)
4356 dt_sym = gfc_find_dt_in_generic (sym);
4358 /* Host associated PDTs can get confused with their constructors
4359 because they ar instantiated in the template's namespace. */
4360 if (!dt_sym)
4362 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4364 gfc_error ("Type name %qs at %C is ambiguous", name);
4365 return MATCH_ERROR;
4367 if (dt_sym && !dt_sym->attr.pdt_type)
4368 dt_sym = NULL;
4371 else if (ts->kind == -1)
4373 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4374 || gfc_current_ns->has_import_set;
4375 gfc_find_symbol (name, NULL, iface, &sym);
4376 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4378 gfc_error ("Type name %qs at %C is ambiguous", name);
4379 return MATCH_ERROR;
4381 if (sym && sym->generic && !dt_sym)
4382 dt_sym = gfc_find_dt_in_generic (sym);
4384 ts->kind = 0;
4385 if (sym == NULL)
4386 return MATCH_NO;
4389 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4390 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4391 || sym->attr.subroutine)
4393 gfc_error ("Type name %qs at %C conflicts with previously declared "
4394 "entity at %L, which has the same name", name,
4395 &sym->declared_at);
4396 return MATCH_ERROR;
4399 if (sym && sym->attr.flavor == FL_DERIVED
4400 && sym->attr.pdt_template
4401 && gfc_current_state () != COMP_DERIVED)
4403 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4404 if (m != MATCH_YES)
4405 return m;
4406 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4407 ts->u.derived = sym;
4408 strcpy (name, gfc_dt_lower_string (sym->name));
4411 gfc_save_symbol_data (sym);
4412 gfc_set_sym_referenced (sym);
4413 if (!sym->attr.generic
4414 && !gfc_add_generic (&sym->attr, sym->name, NULL))
4415 return MATCH_ERROR;
4417 if (!sym->attr.function
4418 && !gfc_add_function (&sym->attr, sym->name, NULL))
4419 return MATCH_ERROR;
4421 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4422 && dt_sym->attr.pdt_template
4423 && gfc_current_state () != COMP_DERIVED)
4425 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4426 if (m != MATCH_YES)
4427 return m;
4428 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4431 if (!dt_sym)
4433 gfc_interface *intr, *head;
4435 /* Use upper case to save the actual derived-type symbol. */
4436 gfc_get_symbol (dt_name, NULL, &dt_sym);
4437 dt_sym->name = gfc_get_string ("%s", sym->name);
4438 head = sym->generic;
4439 intr = gfc_get_interface ();
4440 intr->sym = dt_sym;
4441 intr->where = gfc_current_locus;
4442 intr->next = head;
4443 sym->generic = intr;
4444 sym->attr.if_source = IFSRC_DECL;
4446 else
4447 gfc_save_symbol_data (dt_sym);
4449 gfc_set_sym_referenced (dt_sym);
4451 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4452 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4453 return MATCH_ERROR;
4455 ts->u.derived = dt_sym;
4457 return MATCH_YES;
4459 get_kind:
4460 if (matched_type
4461 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4462 "intrinsic-type-spec at %C"))
4463 return MATCH_ERROR;
4465 /* For all types except double, derived and character, look for an
4466 optional kind specifier. MATCH_NO is actually OK at this point. */
4467 if (implicit_flag == 1)
4469 if (matched_type && gfc_match_char (')') != MATCH_YES)
4470 return MATCH_ERROR;
4472 return MATCH_YES;
4475 if (gfc_current_form == FORM_FREE)
4477 c = gfc_peek_ascii_char ();
4478 if (!gfc_is_whitespace (c) && c != '*' && c != '('
4479 && c != ':' && c != ',')
4481 if (matched_type && c == ')')
4483 gfc_next_ascii_char ();
4484 return MATCH_YES;
4486 gfc_error ("Malformed type-spec at %C");
4487 return MATCH_NO;
4491 m = gfc_match_kind_spec (ts, false);
4492 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4494 m = gfc_match_old_kind_spec (ts);
4495 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4496 return MATCH_ERROR;
4499 if (matched_type && gfc_match_char (')') != MATCH_YES)
4501 gfc_error ("Malformed type-spec at %C");
4502 return MATCH_ERROR;
4505 /* Defer association of the KIND expression of function results
4506 until after USE and IMPORT statements. */
4507 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4508 || gfc_matching_function)
4509 return MATCH_YES;
4511 if (m == MATCH_NO)
4512 m = MATCH_YES; /* No kind specifier found. */
4514 return m;
4518 /* Match an IMPLICIT NONE statement. Actually, this statement is
4519 already matched in parse.c, or we would not end up here in the
4520 first place. So the only thing we need to check, is if there is
4521 trailing garbage. If not, the match is successful. */
4523 match
4524 gfc_match_implicit_none (void)
4526 char c;
4527 match m;
4528 char name[GFC_MAX_SYMBOL_LEN + 1];
4529 bool type = false;
4530 bool external = false;
4531 locus cur_loc = gfc_current_locus;
4533 if (gfc_current_ns->seen_implicit_none
4534 || gfc_current_ns->has_implicit_none_export)
4536 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4537 return MATCH_ERROR;
4540 gfc_gobble_whitespace ();
4541 c = gfc_peek_ascii_char ();
4542 if (c == '(')
4544 (void) gfc_next_ascii_char ();
4545 if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
4546 return MATCH_ERROR;
4548 gfc_gobble_whitespace ();
4549 if (gfc_peek_ascii_char () == ')')
4551 (void) gfc_next_ascii_char ();
4552 type = true;
4554 else
4555 for(;;)
4557 m = gfc_match (" %n", name);
4558 if (m != MATCH_YES)
4559 return MATCH_ERROR;
4561 if (strcmp (name, "type") == 0)
4562 type = true;
4563 else if (strcmp (name, "external") == 0)
4564 external = true;
4565 else
4566 return MATCH_ERROR;
4568 gfc_gobble_whitespace ();
4569 c = gfc_next_ascii_char ();
4570 if (c == ',')
4571 continue;
4572 if (c == ')')
4573 break;
4574 return MATCH_ERROR;
4577 else
4578 type = true;
4580 if (gfc_match_eos () != MATCH_YES)
4581 return MATCH_ERROR;
4583 gfc_set_implicit_none (type, external, &cur_loc);
4585 return MATCH_YES;
4589 /* Match the letter range(s) of an IMPLICIT statement. */
4591 static match
4592 match_implicit_range (void)
4594 char c, c1, c2;
4595 int inner;
4596 locus cur_loc;
4598 cur_loc = gfc_current_locus;
4600 gfc_gobble_whitespace ();
4601 c = gfc_next_ascii_char ();
4602 if (c != '(')
4604 gfc_error ("Missing character range in IMPLICIT at %C");
4605 goto bad;
4608 inner = 1;
4609 while (inner)
4611 gfc_gobble_whitespace ();
4612 c1 = gfc_next_ascii_char ();
4613 if (!ISALPHA (c1))
4614 goto bad;
4616 gfc_gobble_whitespace ();
4617 c = gfc_next_ascii_char ();
4619 switch (c)
4621 case ')':
4622 inner = 0; /* Fall through. */
4624 case ',':
4625 c2 = c1;
4626 break;
4628 case '-':
4629 gfc_gobble_whitespace ();
4630 c2 = gfc_next_ascii_char ();
4631 if (!ISALPHA (c2))
4632 goto bad;
4634 gfc_gobble_whitespace ();
4635 c = gfc_next_ascii_char ();
4637 if ((c != ',') && (c != ')'))
4638 goto bad;
4639 if (c == ')')
4640 inner = 0;
4642 break;
4644 default:
4645 goto bad;
4648 if (c1 > c2)
4650 gfc_error ("Letters must be in alphabetic order in "
4651 "IMPLICIT statement at %C");
4652 goto bad;
4655 /* See if we can add the newly matched range to the pending
4656 implicits from this IMPLICIT statement. We do not check for
4657 conflicts with whatever earlier IMPLICIT statements may have
4658 set. This is done when we've successfully finished matching
4659 the current one. */
4660 if (!gfc_add_new_implicit_range (c1, c2))
4661 goto bad;
4664 return MATCH_YES;
4666 bad:
4667 gfc_syntax_error (ST_IMPLICIT);
4669 gfc_current_locus = cur_loc;
4670 return MATCH_ERROR;
4674 /* Match an IMPLICIT statement, storing the types for
4675 gfc_set_implicit() if the statement is accepted by the parser.
4676 There is a strange looking, but legal syntactic construction
4677 possible. It looks like:
4679 IMPLICIT INTEGER (a-b) (c-d)
4681 This is legal if "a-b" is a constant expression that happens to
4682 equal one of the legal kinds for integers. The real problem
4683 happens with an implicit specification that looks like:
4685 IMPLICIT INTEGER (a-b)
4687 In this case, a typespec matcher that is "greedy" (as most of the
4688 matchers are) gobbles the character range as a kindspec, leaving
4689 nothing left. We therefore have to go a bit more slowly in the
4690 matching process by inhibiting the kindspec checking during
4691 typespec matching and checking for a kind later. */
4693 match
4694 gfc_match_implicit (void)
4696 gfc_typespec ts;
4697 locus cur_loc;
4698 char c;
4699 match m;
4701 if (gfc_current_ns->seen_implicit_none)
4703 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4704 "statement");
4705 return MATCH_ERROR;
4708 gfc_clear_ts (&ts);
4710 /* We don't allow empty implicit statements. */
4711 if (gfc_match_eos () == MATCH_YES)
4713 gfc_error ("Empty IMPLICIT statement at %C");
4714 return MATCH_ERROR;
4719 /* First cleanup. */
4720 gfc_clear_new_implicit ();
4722 /* A basic type is mandatory here. */
4723 m = gfc_match_decl_type_spec (&ts, 1);
4724 if (m == MATCH_ERROR)
4725 goto error;
4726 if (m == MATCH_NO)
4727 goto syntax;
4729 cur_loc = gfc_current_locus;
4730 m = match_implicit_range ();
4732 if (m == MATCH_YES)
4734 /* We may have <TYPE> (<RANGE>). */
4735 gfc_gobble_whitespace ();
4736 c = gfc_peek_ascii_char ();
4737 if (c == ',' || c == '\n' || c == ';' || c == '!')
4739 /* Check for CHARACTER with no length parameter. */
4740 if (ts.type == BT_CHARACTER && !ts.u.cl)
4742 ts.kind = gfc_default_character_kind;
4743 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4744 ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4745 NULL, 1);
4748 /* Record the Successful match. */
4749 if (!gfc_merge_new_implicit (&ts))
4750 return MATCH_ERROR;
4751 if (c == ',')
4752 c = gfc_next_ascii_char ();
4753 else if (gfc_match_eos () == MATCH_ERROR)
4754 goto error;
4755 continue;
4758 gfc_current_locus = cur_loc;
4761 /* Discard the (incorrectly) matched range. */
4762 gfc_clear_new_implicit ();
4764 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4765 if (ts.type == BT_CHARACTER)
4766 m = gfc_match_char_spec (&ts);
4767 else
4769 m = gfc_match_kind_spec (&ts, false);
4770 if (m == MATCH_NO)
4772 m = gfc_match_old_kind_spec (&ts);
4773 if (m == MATCH_ERROR)
4774 goto error;
4775 if (m == MATCH_NO)
4776 goto syntax;
4779 if (m == MATCH_ERROR)
4780 goto error;
4782 m = match_implicit_range ();
4783 if (m == MATCH_ERROR)
4784 goto error;
4785 if (m == MATCH_NO)
4786 goto syntax;
4788 gfc_gobble_whitespace ();
4789 c = gfc_next_ascii_char ();
4790 if (c != ',' && gfc_match_eos () != MATCH_YES)
4791 goto syntax;
4793 if (!gfc_merge_new_implicit (&ts))
4794 return MATCH_ERROR;
4796 while (c == ',');
4798 return MATCH_YES;
4800 syntax:
4801 gfc_syntax_error (ST_IMPLICIT);
4803 error:
4804 return MATCH_ERROR;
4808 match
4809 gfc_match_import (void)
4811 char name[GFC_MAX_SYMBOL_LEN + 1];
4812 match m;
4813 gfc_symbol *sym;
4814 gfc_symtree *st;
4816 if (gfc_current_ns->proc_name == NULL
4817 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4819 gfc_error ("IMPORT statement at %C only permitted in "
4820 "an INTERFACE body");
4821 return MATCH_ERROR;
4824 if (gfc_current_ns->proc_name->attr.module_procedure)
4826 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4827 "in a module procedure interface body");
4828 return MATCH_ERROR;
4831 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4832 return MATCH_ERROR;
4834 if (gfc_match_eos () == MATCH_YES)
4836 /* All host variables should be imported. */
4837 gfc_current_ns->has_import_set = 1;
4838 return MATCH_YES;
4841 if (gfc_match (" ::") == MATCH_YES)
4843 if (gfc_match_eos () == MATCH_YES)
4845 gfc_error ("Expecting list of named entities at %C");
4846 return MATCH_ERROR;
4850 for(;;)
4852 sym = NULL;
4853 m = gfc_match (" %n", name);
4854 switch (m)
4856 case MATCH_YES:
4857 if (gfc_current_ns->parent != NULL
4858 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4860 gfc_error ("Type name %qs at %C is ambiguous", name);
4861 return MATCH_ERROR;
4863 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
4864 && gfc_find_symbol (name,
4865 gfc_current_ns->proc_name->ns->parent,
4866 1, &sym))
4868 gfc_error ("Type name %qs at %C is ambiguous", name);
4869 return MATCH_ERROR;
4872 if (sym == NULL)
4874 gfc_error ("Cannot IMPORT %qs from host scoping unit "
4875 "at %C - does not exist.", name);
4876 return MATCH_ERROR;
4879 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4881 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4882 "at %C", name);
4883 goto next_item;
4886 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4887 st->n.sym = sym;
4888 sym->refs++;
4889 sym->attr.imported = 1;
4891 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4893 /* The actual derived type is stored in a symtree with the first
4894 letter of the name capitalized; the symtree with the all
4895 lower-case name contains the associated generic function. */
4896 st = gfc_new_symtree (&gfc_current_ns->sym_root,
4897 gfc_dt_upper_string (name));
4898 st->n.sym = sym;
4899 sym->refs++;
4900 sym->attr.imported = 1;
4903 goto next_item;
4905 case MATCH_NO:
4906 break;
4908 case MATCH_ERROR:
4909 return MATCH_ERROR;
4912 next_item:
4913 if (gfc_match_eos () == MATCH_YES)
4914 break;
4915 if (gfc_match_char (',') != MATCH_YES)
4916 goto syntax;
4919 return MATCH_YES;
4921 syntax:
4922 gfc_error ("Syntax error in IMPORT statement at %C");
4923 return MATCH_ERROR;
4927 /* A minimal implementation of gfc_match without whitespace, escape
4928 characters or variable arguments. Returns true if the next
4929 characters match the TARGET template exactly. */
4931 static bool
4932 match_string_p (const char *target)
4934 const char *p;
4936 for (p = target; *p; p++)
4937 if ((char) gfc_next_ascii_char () != *p)
4938 return false;
4939 return true;
4942 /* Matches an attribute specification including array specs. If
4943 successful, leaves the variables current_attr and current_as
4944 holding the specification. Also sets the colon_seen variable for
4945 later use by matchers associated with initializations.
4947 This subroutine is a little tricky in the sense that we don't know
4948 if we really have an attr-spec until we hit the double colon.
4949 Until that time, we can only return MATCH_NO. This forces us to
4950 check for duplicate specification at this level. */
4952 static match
4953 match_attr_spec (void)
4955 /* Modifiers that can exist in a type statement. */
4956 enum
4957 { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
4958 DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
4959 DECL_DIMENSION, DECL_EXTERNAL,
4960 DECL_INTRINSIC, DECL_OPTIONAL,
4961 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
4962 DECL_STATIC, DECL_AUTOMATIC,
4963 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
4964 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
4965 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
4968 /* GFC_DECL_END is the sentinel, index starts at 0. */
4969 #define NUM_DECL GFC_DECL_END
4971 /* Make sure that values from sym_intent are safe to be used here. */
4972 gcc_assert (INTENT_IN > 0);
4974 locus start, seen_at[NUM_DECL];
4975 int seen[NUM_DECL];
4976 unsigned int d;
4977 const char *attr;
4978 match m;
4979 bool t;
4981 gfc_clear_attr (&current_attr);
4982 start = gfc_current_locus;
4984 current_as = NULL;
4985 colon_seen = 0;
4986 attr_seen = 0;
4988 /* See if we get all of the keywords up to the final double colon. */
4989 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4990 seen[d] = 0;
4992 for (;;)
4994 char ch;
4996 d = DECL_NONE;
4997 gfc_gobble_whitespace ();
4999 ch = gfc_next_ascii_char ();
5000 if (ch == ':')
5002 /* This is the successful exit condition for the loop. */
5003 if (gfc_next_ascii_char () == ':')
5004 break;
5006 else if (ch == ',')
5008 gfc_gobble_whitespace ();
5009 switch (gfc_peek_ascii_char ())
5011 case 'a':
5012 gfc_next_ascii_char ();
5013 switch (gfc_next_ascii_char ())
5015 case 'l':
5016 if (match_string_p ("locatable"))
5018 /* Matched "allocatable". */
5019 d = DECL_ALLOCATABLE;
5021 break;
5023 case 's':
5024 if (match_string_p ("ynchronous"))
5026 /* Matched "asynchronous". */
5027 d = DECL_ASYNCHRONOUS;
5029 break;
5031 case 'u':
5032 if (match_string_p ("tomatic"))
5034 /* Matched "automatic". */
5035 d = DECL_AUTOMATIC;
5037 break;
5039 break;
5041 case 'b':
5042 /* Try and match the bind(c). */
5043 m = gfc_match_bind_c (NULL, true);
5044 if (m == MATCH_YES)
5045 d = DECL_IS_BIND_C;
5046 else if (m == MATCH_ERROR)
5047 goto cleanup;
5048 break;
5050 case 'c':
5051 gfc_next_ascii_char ();
5052 if ('o' != gfc_next_ascii_char ())
5053 break;
5054 switch (gfc_next_ascii_char ())
5056 case 'd':
5057 if (match_string_p ("imension"))
5059 d = DECL_CODIMENSION;
5060 break;
5062 /* FALLTHRU */
5063 case 'n':
5064 if (match_string_p ("tiguous"))
5066 d = DECL_CONTIGUOUS;
5067 break;
5070 break;
5072 case 'd':
5073 if (match_string_p ("dimension"))
5074 d = DECL_DIMENSION;
5075 break;
5077 case 'e':
5078 if (match_string_p ("external"))
5079 d = DECL_EXTERNAL;
5080 break;
5082 case 'i':
5083 if (match_string_p ("int"))
5085 ch = gfc_next_ascii_char ();
5086 if (ch == 'e')
5088 if (match_string_p ("nt"))
5090 /* Matched "intent". */
5091 d = match_intent_spec ();
5092 if (d == INTENT_UNKNOWN)
5094 m = MATCH_ERROR;
5095 goto cleanup;
5099 else if (ch == 'r')
5101 if (match_string_p ("insic"))
5103 /* Matched "intrinsic". */
5104 d = DECL_INTRINSIC;
5108 break;
5110 case 'k':
5111 if (match_string_p ("kind"))
5112 d = DECL_KIND;
5113 break;
5115 case 'l':
5116 if (match_string_p ("len"))
5117 d = DECL_LEN;
5118 break;
5120 case 'o':
5121 if (match_string_p ("optional"))
5122 d = DECL_OPTIONAL;
5123 break;
5125 case 'p':
5126 gfc_next_ascii_char ();
5127 switch (gfc_next_ascii_char ())
5129 case 'a':
5130 if (match_string_p ("rameter"))
5132 /* Matched "parameter". */
5133 d = DECL_PARAMETER;
5135 break;
5137 case 'o':
5138 if (match_string_p ("inter"))
5140 /* Matched "pointer". */
5141 d = DECL_POINTER;
5143 break;
5145 case 'r':
5146 ch = gfc_next_ascii_char ();
5147 if (ch == 'i')
5149 if (match_string_p ("vate"))
5151 /* Matched "private". */
5152 d = DECL_PRIVATE;
5155 else if (ch == 'o')
5157 if (match_string_p ("tected"))
5159 /* Matched "protected". */
5160 d = DECL_PROTECTED;
5163 break;
5165 case 'u':
5166 if (match_string_p ("blic"))
5168 /* Matched "public". */
5169 d = DECL_PUBLIC;
5171 break;
5173 break;
5175 case 's':
5176 gfc_next_ascii_char ();
5177 switch (gfc_next_ascii_char ())
5179 case 'a':
5180 if (match_string_p ("ve"))
5182 /* Matched "save". */
5183 d = DECL_SAVE;
5185 break;
5187 case 't':
5188 if (match_string_p ("atic"))
5190 /* Matched "static". */
5191 d = DECL_STATIC;
5193 break;
5195 break;
5197 case 't':
5198 if (match_string_p ("target"))
5199 d = DECL_TARGET;
5200 break;
5202 case 'v':
5203 gfc_next_ascii_char ();
5204 ch = gfc_next_ascii_char ();
5205 if (ch == 'a')
5207 if (match_string_p ("lue"))
5209 /* Matched "value". */
5210 d = DECL_VALUE;
5213 else if (ch == 'o')
5215 if (match_string_p ("latile"))
5217 /* Matched "volatile". */
5218 d = DECL_VOLATILE;
5221 break;
5225 /* No double colon and no recognizable decl_type, so assume that
5226 we've been looking at something else the whole time. */
5227 if (d == DECL_NONE)
5229 m = MATCH_NO;
5230 goto cleanup;
5233 /* Check to make sure any parens are paired up correctly. */
5234 if (gfc_match_parens () == MATCH_ERROR)
5236 m = MATCH_ERROR;
5237 goto cleanup;
5240 seen[d]++;
5241 seen_at[d] = gfc_current_locus;
5243 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
5245 gfc_array_spec *as = NULL;
5247 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5248 d == DECL_CODIMENSION);
5250 if (current_as == NULL)
5251 current_as = as;
5252 else if (m == MATCH_YES)
5254 if (!merge_array_spec (as, current_as, false))
5255 m = MATCH_ERROR;
5256 free (as);
5259 if (m == MATCH_NO)
5261 if (d == DECL_CODIMENSION)
5262 gfc_error ("Missing codimension specification at %C");
5263 else
5264 gfc_error ("Missing dimension specification at %C");
5265 m = MATCH_ERROR;
5268 if (m == MATCH_ERROR)
5269 goto cleanup;
5273 /* Since we've seen a double colon, we have to be looking at an
5274 attr-spec. This means that we can now issue errors. */
5275 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5276 if (seen[d] > 1)
5278 switch (d)
5280 case DECL_ALLOCATABLE:
5281 attr = "ALLOCATABLE";
5282 break;
5283 case DECL_ASYNCHRONOUS:
5284 attr = "ASYNCHRONOUS";
5285 break;
5286 case DECL_CODIMENSION:
5287 attr = "CODIMENSION";
5288 break;
5289 case DECL_CONTIGUOUS:
5290 attr = "CONTIGUOUS";
5291 break;
5292 case DECL_DIMENSION:
5293 attr = "DIMENSION";
5294 break;
5295 case DECL_EXTERNAL:
5296 attr = "EXTERNAL";
5297 break;
5298 case DECL_IN:
5299 attr = "INTENT (IN)";
5300 break;
5301 case DECL_OUT:
5302 attr = "INTENT (OUT)";
5303 break;
5304 case DECL_INOUT:
5305 attr = "INTENT (IN OUT)";
5306 break;
5307 case DECL_INTRINSIC:
5308 attr = "INTRINSIC";
5309 break;
5310 case DECL_OPTIONAL:
5311 attr = "OPTIONAL";
5312 break;
5313 case DECL_KIND:
5314 attr = "KIND";
5315 break;
5316 case DECL_LEN:
5317 attr = "LEN";
5318 break;
5319 case DECL_PARAMETER:
5320 attr = "PARAMETER";
5321 break;
5322 case DECL_POINTER:
5323 attr = "POINTER";
5324 break;
5325 case DECL_PROTECTED:
5326 attr = "PROTECTED";
5327 break;
5328 case DECL_PRIVATE:
5329 attr = "PRIVATE";
5330 break;
5331 case DECL_PUBLIC:
5332 attr = "PUBLIC";
5333 break;
5334 case DECL_SAVE:
5335 attr = "SAVE";
5336 break;
5337 case DECL_STATIC:
5338 attr = "STATIC";
5339 break;
5340 case DECL_AUTOMATIC:
5341 attr = "AUTOMATIC";
5342 break;
5343 case DECL_TARGET:
5344 attr = "TARGET";
5345 break;
5346 case DECL_IS_BIND_C:
5347 attr = "IS_BIND_C";
5348 break;
5349 case DECL_VALUE:
5350 attr = "VALUE";
5351 break;
5352 case DECL_VOLATILE:
5353 attr = "VOLATILE";
5354 break;
5355 default:
5356 attr = NULL; /* This shouldn't happen. */
5359 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5360 m = MATCH_ERROR;
5361 goto cleanup;
5364 /* Now that we've dealt with duplicate attributes, add the attributes
5365 to the current attribute. */
5366 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5368 if (seen[d] == 0)
5369 continue;
5370 else
5371 attr_seen = 1;
5373 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5374 && !flag_dec_static)
5376 gfc_error ("%s at %L is a DEC extension, enable with "
5377 "%<-fdec-static%>",
5378 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5379 m = MATCH_ERROR;
5380 goto cleanup;
5382 /* Allow SAVE with STATIC, but don't complain. */
5383 if (d == DECL_STATIC && seen[DECL_SAVE])
5384 continue;
5386 if (gfc_current_state () == COMP_DERIVED
5387 && d != DECL_DIMENSION && d != DECL_CODIMENSION
5388 && d != DECL_POINTER && d != DECL_PRIVATE
5389 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
5391 if (d == DECL_ALLOCATABLE)
5393 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
5394 "attribute at %C in a TYPE definition"))
5396 m = MATCH_ERROR;
5397 goto cleanup;
5400 else if (d == DECL_KIND)
5402 if (!gfc_notify_std (GFC_STD_F2003, "KIND "
5403 "attribute at %C in a TYPE definition"))
5405 m = MATCH_ERROR;
5406 goto cleanup;
5408 if (current_ts.type != BT_INTEGER)
5410 gfc_error ("Component with KIND attribute at %C must be "
5411 "INTEGER");
5412 m = MATCH_ERROR;
5413 goto cleanup;
5415 if (current_ts.kind != gfc_default_integer_kind)
5417 gfc_error ("Component with KIND attribute at %C must be "
5418 "default integer kind (%d)",
5419 gfc_default_integer_kind);
5420 m = MATCH_ERROR;
5421 goto cleanup;
5424 else if (d == DECL_LEN)
5426 if (!gfc_notify_std (GFC_STD_F2003, "LEN "
5427 "attribute at %C in a TYPE definition"))
5429 m = MATCH_ERROR;
5430 goto cleanup;
5432 if (current_ts.type != BT_INTEGER)
5434 gfc_error ("Component with LEN attribute at %C must be "
5435 "INTEGER");
5436 m = MATCH_ERROR;
5437 goto cleanup;
5439 if (current_ts.kind != gfc_default_integer_kind)
5441 gfc_error ("Component with LEN attribute at %C must be "
5442 "default integer kind (%d)",
5443 gfc_default_integer_kind);
5444 m = MATCH_ERROR;
5445 goto cleanup;
5448 else
5450 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
5451 &seen_at[d]);
5452 m = MATCH_ERROR;
5453 goto cleanup;
5457 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5458 && gfc_current_state () != COMP_MODULE)
5460 if (d == DECL_PRIVATE)
5461 attr = "PRIVATE";
5462 else
5463 attr = "PUBLIC";
5464 if (gfc_current_state () == COMP_DERIVED
5465 && gfc_state_stack->previous
5466 && gfc_state_stack->previous->state == COMP_MODULE)
5468 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5469 "at %L in a TYPE definition", attr,
5470 &seen_at[d]))
5472 m = MATCH_ERROR;
5473 goto cleanup;
5476 else
5478 gfc_error ("%s attribute at %L is not allowed outside of the "
5479 "specification part of a module", attr, &seen_at[d]);
5480 m = MATCH_ERROR;
5481 goto cleanup;
5485 if (gfc_current_state () != COMP_DERIVED
5486 && (d == DECL_KIND || d == DECL_LEN))
5488 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5489 "definition", &seen_at[d]);
5490 m = MATCH_ERROR;
5491 goto cleanup;
5494 switch (d)
5496 case DECL_ALLOCATABLE:
5497 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5498 break;
5500 case DECL_ASYNCHRONOUS:
5501 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5502 t = false;
5503 else
5504 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5505 break;
5507 case DECL_CODIMENSION:
5508 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5509 break;
5511 case DECL_CONTIGUOUS:
5512 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5513 t = false;
5514 else
5515 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5516 break;
5518 case DECL_DIMENSION:
5519 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5520 break;
5522 case DECL_EXTERNAL:
5523 t = gfc_add_external (&current_attr, &seen_at[d]);
5524 break;
5526 case DECL_IN:
5527 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5528 break;
5530 case DECL_OUT:
5531 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5532 break;
5534 case DECL_INOUT:
5535 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5536 break;
5538 case DECL_INTRINSIC:
5539 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5540 break;
5542 case DECL_OPTIONAL:
5543 t = gfc_add_optional (&current_attr, &seen_at[d]);
5544 break;
5546 case DECL_KIND:
5547 t = gfc_add_kind (&current_attr, &seen_at[d]);
5548 break;
5550 case DECL_LEN:
5551 t = gfc_add_len (&current_attr, &seen_at[d]);
5552 break;
5554 case DECL_PARAMETER:
5555 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5556 break;
5558 case DECL_POINTER:
5559 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5560 break;
5562 case DECL_PROTECTED:
5563 if (gfc_current_state () != COMP_MODULE
5564 || (gfc_current_ns->proc_name
5565 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5567 gfc_error ("PROTECTED at %C only allowed in specification "
5568 "part of a module");
5569 t = false;
5570 break;
5573 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5574 t = false;
5575 else
5576 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5577 break;
5579 case DECL_PRIVATE:
5580 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5581 &seen_at[d]);
5582 break;
5584 case DECL_PUBLIC:
5585 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5586 &seen_at[d]);
5587 break;
5589 case DECL_STATIC:
5590 case DECL_SAVE:
5591 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5592 break;
5594 case DECL_AUTOMATIC:
5595 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5596 break;
5598 case DECL_TARGET:
5599 t = gfc_add_target (&current_attr, &seen_at[d]);
5600 break;
5602 case DECL_IS_BIND_C:
5603 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5604 break;
5606 case DECL_VALUE:
5607 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5608 t = false;
5609 else
5610 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5611 break;
5613 case DECL_VOLATILE:
5614 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5615 t = false;
5616 else
5617 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5618 break;
5620 default:
5621 gfc_internal_error ("match_attr_spec(): Bad attribute");
5624 if (!t)
5626 m = MATCH_ERROR;
5627 goto cleanup;
5631 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
5632 if ((gfc_current_state () == COMP_MODULE
5633 || gfc_current_state () == COMP_SUBMODULE)
5634 && !current_attr.save
5635 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5636 current_attr.save = SAVE_IMPLICIT;
5638 colon_seen = 1;
5639 return MATCH_YES;
5641 cleanup:
5642 gfc_current_locus = start;
5643 gfc_free_array_spec (current_as);
5644 current_as = NULL;
5645 attr_seen = 0;
5646 return m;
5650 /* Set the binding label, dest_label, either with the binding label
5651 stored in the given gfc_typespec, ts, or if none was provided, it
5652 will be the symbol name in all lower case, as required by the draft
5653 (J3/04-007, section 15.4.1). If a binding label was given and
5654 there is more than one argument (num_idents), it is an error. */
5656 static bool
5657 set_binding_label (const char **dest_label, const char *sym_name,
5658 int num_idents)
5660 if (num_idents > 1 && has_name_equals)
5662 gfc_error ("Multiple identifiers provided with "
5663 "single NAME= specifier at %C");
5664 return false;
5667 if (curr_binding_label)
5668 /* Binding label given; store in temp holder till have sym. */
5669 *dest_label = curr_binding_label;
5670 else
5672 /* No binding label given, and the NAME= specifier did not exist,
5673 which means there was no NAME="". */
5674 if (sym_name != NULL && has_name_equals == 0)
5675 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5678 return true;
5682 /* Set the status of the given common block as being BIND(C) or not,
5683 depending on the given parameter, is_bind_c. */
5685 void
5686 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5688 com_block->is_bind_c = is_bind_c;
5689 return;
5693 /* Verify that the given gfc_typespec is for a C interoperable type. */
5695 bool
5696 gfc_verify_c_interop (gfc_typespec *ts)
5698 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5699 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5700 ? true : false;
5701 else if (ts->type == BT_CLASS)
5702 return false;
5703 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5704 return false;
5706 return true;
5710 /* Verify that the variables of a given common block, which has been
5711 defined with the attribute specifier bind(c), to be of a C
5712 interoperable type. Errors will be reported here, if
5713 encountered. */
5715 bool
5716 verify_com_block_vars_c_interop (gfc_common_head *com_block)
5718 gfc_symbol *curr_sym = NULL;
5719 bool retval = true;
5721 curr_sym = com_block->head;
5723 /* Make sure we have at least one symbol. */
5724 if (curr_sym == NULL)
5725 return retval;
5727 /* Here we know we have a symbol, so we'll execute this loop
5728 at least once. */
5731 /* The second to last param, 1, says this is in a common block. */
5732 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5733 curr_sym = curr_sym->common_next;
5734 } while (curr_sym != NULL);
5736 return retval;
5740 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5741 an appropriate error message is reported. */
5743 bool
5744 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5745 int is_in_common, gfc_common_head *com_block)
5747 bool bind_c_function = false;
5748 bool retval = true;
5750 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5751 bind_c_function = true;
5753 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5755 tmp_sym = tmp_sym->result;
5756 /* Make sure it wasn't an implicitly typed result. */
5757 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5759 gfc_warning (OPT_Wc_binding_type,
5760 "Implicitly declared BIND(C) function %qs at "
5761 "%L may not be C interoperable", tmp_sym->name,
5762 &tmp_sym->declared_at);
5763 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5764 /* Mark it as C interoperable to prevent duplicate warnings. */
5765 tmp_sym->ts.is_c_interop = 1;
5766 tmp_sym->attr.is_c_interop = 1;
5770 /* Here, we know we have the bind(c) attribute, so if we have
5771 enough type info, then verify that it's a C interop kind.
5772 The info could be in the symbol already, or possibly still in
5773 the given ts (current_ts), so look in both. */
5774 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5776 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5778 /* See if we're dealing with a sym in a common block or not. */
5779 if (is_in_common == 1 && warn_c_binding_type)
5781 gfc_warning (OPT_Wc_binding_type,
5782 "Variable %qs in common block %qs at %L "
5783 "may not be a C interoperable "
5784 "kind though common block %qs is BIND(C)",
5785 tmp_sym->name, com_block->name,
5786 &(tmp_sym->declared_at), com_block->name);
5788 else
5790 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5791 gfc_error ("Type declaration %qs at %L is not C "
5792 "interoperable but it is BIND(C)",
5793 tmp_sym->name, &(tmp_sym->declared_at));
5794 else if (warn_c_binding_type)
5795 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5796 "may not be a C interoperable "
5797 "kind but it is BIND(C)",
5798 tmp_sym->name, &(tmp_sym->declared_at));
5802 /* Variables declared w/in a common block can't be bind(c)
5803 since there's no way for C to see these variables, so there's
5804 semantically no reason for the attribute. */
5805 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5807 gfc_error ("Variable %qs in common block %qs at "
5808 "%L cannot be declared with BIND(C) "
5809 "since it is not a global",
5810 tmp_sym->name, com_block->name,
5811 &(tmp_sym->declared_at));
5812 retval = false;
5815 /* Scalar variables that are bind(c) cannot have the pointer
5816 or allocatable attributes. */
5817 if (tmp_sym->attr.is_bind_c == 1)
5819 if (tmp_sym->attr.pointer == 1)
5821 gfc_error ("Variable %qs at %L cannot have both the "
5822 "POINTER and BIND(C) attributes",
5823 tmp_sym->name, &(tmp_sym->declared_at));
5824 retval = false;
5827 if (tmp_sym->attr.allocatable == 1)
5829 gfc_error ("Variable %qs at %L cannot have both the "
5830 "ALLOCATABLE and BIND(C) attributes",
5831 tmp_sym->name, &(tmp_sym->declared_at));
5832 retval = false;
5837 /* If it is a BIND(C) function, make sure the return value is a
5838 scalar value. The previous tests in this function made sure
5839 the type is interoperable. */
5840 if (bind_c_function && tmp_sym->as != NULL)
5841 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5842 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5844 /* BIND(C) functions cannot return a character string. */
5845 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5846 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5847 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5848 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5849 gfc_error ("Return type of BIND(C) function %qs of character "
5850 "type at %L must have length 1", tmp_sym->name,
5851 &(tmp_sym->declared_at));
5854 /* See if the symbol has been marked as private. If it has, make sure
5855 there is no binding label and warn the user if there is one. */
5856 if (tmp_sym->attr.access == ACCESS_PRIVATE
5857 && tmp_sym->binding_label)
5858 /* Use gfc_warning_now because we won't say that the symbol fails
5859 just because of this. */
5860 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5861 "given the binding label %qs", tmp_sym->name,
5862 &(tmp_sym->declared_at), tmp_sym->binding_label);
5864 return retval;
5868 /* Set the appropriate fields for a symbol that's been declared as
5869 BIND(C) (the is_bind_c flag and the binding label), and verify that
5870 the type is C interoperable. Errors are reported by the functions
5871 used to set/test these fields. */
5873 bool
5874 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5876 bool retval = true;
5878 /* TODO: Do we need to make sure the vars aren't marked private? */
5880 /* Set the is_bind_c bit in symbol_attribute. */
5881 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5883 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5884 return false;
5886 return retval;
5890 /* Set the fields marking the given common block as BIND(C), including
5891 a binding label, and report any errors encountered. */
5893 bool
5894 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5896 bool retval = true;
5898 /* destLabel, common name, typespec (which may have binding label). */
5899 if (!set_binding_label (&com_block->binding_label, com_block->name,
5900 num_idents))
5901 return false;
5903 /* Set the given common block (com_block) to being bind(c) (1). */
5904 set_com_block_bind_c (com_block, 1);
5906 return retval;
5910 /* Retrieve the list of one or more identifiers that the given bind(c)
5911 attribute applies to. */
5913 bool
5914 get_bind_c_idents (void)
5916 char name[GFC_MAX_SYMBOL_LEN + 1];
5917 int num_idents = 0;
5918 gfc_symbol *tmp_sym = NULL;
5919 match found_id;
5920 gfc_common_head *com_block = NULL;
5922 if (gfc_match_name (name) == MATCH_YES)
5924 found_id = MATCH_YES;
5925 gfc_get_ha_symbol (name, &tmp_sym);
5927 else if (match_common_name (name) == MATCH_YES)
5929 found_id = MATCH_YES;
5930 com_block = gfc_get_common (name, 0);
5932 else
5934 gfc_error ("Need either entity or common block name for "
5935 "attribute specification statement at %C");
5936 return false;
5939 /* Save the current identifier and look for more. */
5942 /* Increment the number of identifiers found for this spec stmt. */
5943 num_idents++;
5945 /* Make sure we have a sym or com block, and verify that it can
5946 be bind(c). Set the appropriate field(s) and look for more
5947 identifiers. */
5948 if (tmp_sym != NULL || com_block != NULL)
5950 if (tmp_sym != NULL)
5952 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
5953 return false;
5955 else
5957 if (!set_verify_bind_c_com_block (com_block, num_idents))
5958 return false;
5961 /* Look to see if we have another identifier. */
5962 tmp_sym = NULL;
5963 if (gfc_match_eos () == MATCH_YES)
5964 found_id = MATCH_NO;
5965 else if (gfc_match_char (',') != MATCH_YES)
5966 found_id = MATCH_NO;
5967 else if (gfc_match_name (name) == MATCH_YES)
5969 found_id = MATCH_YES;
5970 gfc_get_ha_symbol (name, &tmp_sym);
5972 else if (match_common_name (name) == MATCH_YES)
5974 found_id = MATCH_YES;
5975 com_block = gfc_get_common (name, 0);
5977 else
5979 gfc_error ("Missing entity or common block name for "
5980 "attribute specification statement at %C");
5981 return false;
5984 else
5986 gfc_internal_error ("Missing symbol");
5988 } while (found_id == MATCH_YES);
5990 /* if we get here we were successful */
5991 return true;
5995 /* Try and match a BIND(C) attribute specification statement. */
5997 match
5998 gfc_match_bind_c_stmt (void)
6000 match found_match = MATCH_NO;
6001 gfc_typespec *ts;
6003 ts = &current_ts;
6005 /* This may not be necessary. */
6006 gfc_clear_ts (ts);
6007 /* Clear the temporary binding label holder. */
6008 curr_binding_label = NULL;
6010 /* Look for the bind(c). */
6011 found_match = gfc_match_bind_c (NULL, true);
6013 if (found_match == MATCH_YES)
6015 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
6016 return MATCH_ERROR;
6018 /* Look for the :: now, but it is not required. */
6019 gfc_match (" :: ");
6021 /* Get the identifier(s) that needs to be updated. This may need to
6022 change to hand the flag(s) for the attr specified so all identifiers
6023 found can have all appropriate parts updated (assuming that the same
6024 spec stmt can have multiple attrs, such as both bind(c) and
6025 allocatable...). */
6026 if (!get_bind_c_idents ())
6027 /* Error message should have printed already. */
6028 return MATCH_ERROR;
6031 return found_match;
6035 /* Match a data declaration statement. */
6037 match
6038 gfc_match_data_decl (void)
6040 gfc_symbol *sym;
6041 match m;
6042 int elem;
6044 type_param_spec_list = NULL;
6045 decl_type_param_list = NULL;
6047 num_idents_on_line = 0;
6049 m = gfc_match_decl_type_spec (&current_ts, 0);
6050 if (m != MATCH_YES)
6051 return m;
6053 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6054 && !gfc_comp_struct (gfc_current_state ()))
6056 sym = gfc_use_derived (current_ts.u.derived);
6058 if (sym == NULL)
6060 m = MATCH_ERROR;
6061 goto cleanup;
6064 current_ts.u.derived = sym;
6067 m = match_attr_spec ();
6068 if (m == MATCH_ERROR)
6070 m = MATCH_NO;
6071 goto cleanup;
6074 if (current_ts.type == BT_CLASS
6075 && current_ts.u.derived->attr.unlimited_polymorphic)
6076 goto ok;
6078 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6079 && current_ts.u.derived->components == NULL
6080 && !current_ts.u.derived->attr.zero_comp)
6083 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
6084 goto ok;
6086 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
6087 goto ok;
6089 gfc_find_symbol (current_ts.u.derived->name,
6090 current_ts.u.derived->ns, 1, &sym);
6092 /* Any symbol that we find had better be a type definition
6093 which has its components defined, or be a structure definition
6094 actively being parsed. */
6095 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
6096 && (current_ts.u.derived->components != NULL
6097 || current_ts.u.derived->attr.zero_comp
6098 || current_ts.u.derived == gfc_new_block))
6099 goto ok;
6101 gfc_error ("Derived type at %C has not been previously defined "
6102 "and so cannot appear in a derived type definition");
6103 m = MATCH_ERROR;
6104 goto cleanup;
6108 /* If we have an old-style character declaration, and no new-style
6109 attribute specifications, then there a comma is optional between
6110 the type specification and the variable list. */
6111 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
6112 gfc_match_char (',');
6114 /* Give the types/attributes to symbols that follow. Give the element
6115 a number so that repeat character length expressions can be copied. */
6116 elem = 1;
6117 for (;;)
6119 num_idents_on_line++;
6120 m = variable_decl (elem++);
6121 if (m == MATCH_ERROR)
6122 goto cleanup;
6123 if (m == MATCH_NO)
6124 break;
6126 if (gfc_match_eos () == MATCH_YES)
6127 goto cleanup;
6128 if (gfc_match_char (',') != MATCH_YES)
6129 break;
6132 if (!gfc_error_flag_test ())
6134 /* An anonymous structure declaration is unambiguous; if we matched one
6135 according to gfc_match_structure_decl, we need to return MATCH_YES
6136 here to avoid confusing the remaining matchers, even if there was an
6137 error during variable_decl. We must flush any such errors. Note this
6138 causes the parser to gracefully continue parsing the remaining input
6139 as a structure body, which likely follows. */
6140 if (current_ts.type == BT_DERIVED && current_ts.u.derived
6141 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6143 gfc_error_now ("Syntax error in anonymous structure declaration"
6144 " at %C");
6145 /* Skip the bad variable_decl and line up for the start of the
6146 structure body. */
6147 gfc_error_recovery ();
6148 m = MATCH_YES;
6149 goto cleanup;
6152 gfc_error ("Syntax error in data declaration at %C");
6155 m = MATCH_ERROR;
6157 gfc_free_data_all (gfc_current_ns);
6159 cleanup:
6160 if (saved_kind_expr)
6161 gfc_free_expr (saved_kind_expr);
6162 if (type_param_spec_list)
6163 gfc_free_actual_arglist (type_param_spec_list);
6164 if (decl_type_param_list)
6165 gfc_free_actual_arglist (decl_type_param_list);
6166 saved_kind_expr = NULL;
6167 gfc_free_array_spec (current_as);
6168 current_as = NULL;
6169 return m;
6172 static bool
6173 in_module_or_interface(void)
6175 if (gfc_current_state () == COMP_MODULE
6176 || gfc_current_state () == COMP_SUBMODULE
6177 || gfc_current_state () == COMP_INTERFACE)
6178 return true;
6180 if (gfc_state_stack->state == COMP_CONTAINS
6181 || gfc_state_stack->state == COMP_FUNCTION
6182 || gfc_state_stack->state == COMP_SUBROUTINE)
6184 gfc_state_data *p;
6185 for (p = gfc_state_stack->previous; p ; p = p->previous)
6187 if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
6188 || p->state == COMP_INTERFACE)
6189 return true;
6192 return false;
6195 /* Match a prefix associated with a function or subroutine
6196 declaration. If the typespec pointer is nonnull, then a typespec
6197 can be matched. Note that if nothing matches, MATCH_YES is
6198 returned (the null string was matched). */
6200 match
6201 gfc_match_prefix (gfc_typespec *ts)
6203 bool seen_type;
6204 bool seen_impure;
6205 bool found_prefix;
6207 gfc_clear_attr (&current_attr);
6208 seen_type = false;
6209 seen_impure = false;
6211 gcc_assert (!gfc_matching_prefix);
6212 gfc_matching_prefix = true;
6216 found_prefix = false;
6218 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6219 corresponding attribute seems natural and distinguishes these
6220 procedures from procedure types of PROC_MODULE, which these are
6221 as well. */
6222 if (gfc_match ("module% ") == MATCH_YES)
6224 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6225 goto error;
6227 if (!in_module_or_interface ())
6229 gfc_error ("MODULE prefix at %C found outside of a module, "
6230 "submodule, or interface");
6231 goto error;
6234 current_attr.module_procedure = 1;
6235 found_prefix = true;
6238 if (!seen_type && ts != NULL)
6240 match m;
6241 m = gfc_match_decl_type_spec (ts, 0);
6242 if (m == MATCH_ERROR)
6243 goto error;
6244 if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
6246 seen_type = true;
6247 found_prefix = true;
6251 if (gfc_match ("elemental% ") == MATCH_YES)
6253 if (!gfc_add_elemental (&current_attr, NULL))
6254 goto error;
6256 found_prefix = true;
6259 if (gfc_match ("pure% ") == MATCH_YES)
6261 if (!gfc_add_pure (&current_attr, NULL))
6262 goto error;
6264 found_prefix = true;
6267 if (gfc_match ("recursive% ") == MATCH_YES)
6269 if (!gfc_add_recursive (&current_attr, NULL))
6270 goto error;
6272 found_prefix = true;
6275 /* IMPURE is a somewhat special case, as it needs not set an actual
6276 attribute but rather only prevents ELEMENTAL routines from being
6277 automatically PURE. */
6278 if (gfc_match ("impure% ") == MATCH_YES)
6280 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
6281 goto error;
6283 seen_impure = true;
6284 found_prefix = true;
6287 while (found_prefix);
6289 /* IMPURE and PURE must not both appear, of course. */
6290 if (seen_impure && current_attr.pure)
6292 gfc_error ("PURE and IMPURE must not appear both at %C");
6293 goto error;
6296 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6297 if (!seen_impure && current_attr.elemental && !current_attr.pure)
6299 if (!gfc_add_pure (&current_attr, NULL))
6300 goto error;
6303 /* At this point, the next item is not a prefix. */
6304 gcc_assert (gfc_matching_prefix);
6306 gfc_matching_prefix = false;
6307 return MATCH_YES;
6309 error:
6310 gcc_assert (gfc_matching_prefix);
6311 gfc_matching_prefix = false;
6312 return MATCH_ERROR;
6316 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6318 static bool
6319 copy_prefix (symbol_attribute *dest, locus *where)
6321 if (dest->module_procedure)
6323 if (current_attr.elemental)
6324 dest->elemental = 1;
6326 if (current_attr.pure)
6327 dest->pure = 1;
6329 if (current_attr.recursive)
6330 dest->recursive = 1;
6332 /* Module procedures are unusual in that the 'dest' is copied from
6333 the interface declaration. However, this is an oportunity to
6334 check that the submodule declaration is compliant with the
6335 interface. */
6336 if (dest->elemental && !current_attr.elemental)
6338 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6339 "missing at %L", where);
6340 return false;
6343 if (dest->pure && !current_attr.pure)
6345 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6346 "missing at %L", where);
6347 return false;
6350 if (dest->recursive && !current_attr.recursive)
6352 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6353 "missing at %L", where);
6354 return false;
6357 return true;
6360 if (current_attr.elemental && !gfc_add_elemental (dest, where))
6361 return false;
6363 if (current_attr.pure && !gfc_add_pure (dest, where))
6364 return false;
6366 if (current_attr.recursive && !gfc_add_recursive (dest, where))
6367 return false;
6369 return true;
6373 /* Match a formal argument list or, if typeparam is true, a
6374 type_param_name_list. */
6376 match
6377 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6378 int null_flag, bool typeparam)
6380 gfc_formal_arglist *head, *tail, *p, *q;
6381 char name[GFC_MAX_SYMBOL_LEN + 1];
6382 gfc_symbol *sym;
6383 match m;
6384 gfc_formal_arglist *formal = NULL;
6386 head = tail = NULL;
6388 /* Keep the interface formal argument list and null it so that the
6389 matching for the new declaration can be done. The numbers and
6390 names of the arguments are checked here. The interface formal
6391 arguments are retained in formal_arglist and the characteristics
6392 are compared in resolve.c(resolve_fl_procedure). See the remark
6393 in get_proc_name about the eventual need to copy the formal_arglist
6394 and populate the formal namespace of the interface symbol. */
6395 if (progname->attr.module_procedure
6396 && progname->attr.host_assoc)
6398 formal = progname->formal;
6399 progname->formal = NULL;
6402 if (gfc_match_char ('(') != MATCH_YES)
6404 if (null_flag)
6405 goto ok;
6406 return MATCH_NO;
6409 if (gfc_match_char (')') == MATCH_YES)
6411 if (typeparam)
6413 gfc_error_now ("A type parameter list is required at %C");
6414 m = MATCH_ERROR;
6415 goto cleanup;
6417 else
6418 goto ok;
6421 for (;;)
6423 if (gfc_match_char ('*') == MATCH_YES)
6425 sym = NULL;
6426 if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6427 "Alternate-return argument at %C"))
6429 m = MATCH_ERROR;
6430 goto cleanup;
6432 else if (typeparam)
6433 gfc_error_now ("A parameter name is required at %C");
6435 else
6437 m = gfc_match_name (name);
6438 if (m != MATCH_YES)
6440 if(typeparam)
6441 gfc_error_now ("A parameter name is required at %C");
6442 goto cleanup;
6445 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6446 goto cleanup;
6447 else if (typeparam
6448 && gfc_get_symbol (name, progname->f2k_derived, &sym))
6449 goto cleanup;
6452 p = gfc_get_formal_arglist ();
6454 if (head == NULL)
6455 head = tail = p;
6456 else
6458 tail->next = p;
6459 tail = p;
6462 tail->sym = sym;
6464 /* We don't add the VARIABLE flavor because the name could be a
6465 dummy procedure. We don't apply these attributes to formal
6466 arguments of statement functions. */
6467 if (sym != NULL && !st_flag
6468 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6469 || !gfc_missing_attr (&sym->attr, NULL)))
6471 m = MATCH_ERROR;
6472 goto cleanup;
6475 /* The name of a program unit can be in a different namespace,
6476 so check for it explicitly. After the statement is accepted,
6477 the name is checked for especially in gfc_get_symbol(). */
6478 if (gfc_new_block != NULL && sym != NULL && !typeparam
6479 && strcmp (sym->name, gfc_new_block->name) == 0)
6481 gfc_error ("Name %qs at %C is the name of the procedure",
6482 sym->name);
6483 m = MATCH_ERROR;
6484 goto cleanup;
6487 if (gfc_match_char (')') == MATCH_YES)
6488 goto ok;
6490 m = gfc_match_char (',');
6491 if (m != MATCH_YES)
6493 if (typeparam)
6494 gfc_error_now ("Expected parameter list in type declaration "
6495 "at %C");
6496 else
6497 gfc_error ("Unexpected junk in formal argument list at %C");
6498 goto cleanup;
6503 /* Check for duplicate symbols in the formal argument list. */
6504 if (head != NULL)
6506 for (p = head; p->next; p = p->next)
6508 if (p->sym == NULL)
6509 continue;
6511 for (q = p->next; q; q = q->next)
6512 if (p->sym == q->sym)
6514 if (typeparam)
6515 gfc_error_now ("Duplicate name %qs in parameter "
6516 "list at %C", p->sym->name);
6517 else
6518 gfc_error ("Duplicate symbol %qs in formal argument "
6519 "list at %C", p->sym->name);
6521 m = MATCH_ERROR;
6522 goto cleanup;
6527 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6529 m = MATCH_ERROR;
6530 goto cleanup;
6533 /* gfc_error_now used in following and return with MATCH_YES because
6534 doing otherwise results in a cascade of extraneous errors and in
6535 some cases an ICE in symbol.c(gfc_release_symbol). */
6536 if (progname->attr.module_procedure && progname->attr.host_assoc)
6538 bool arg_count_mismatch = false;
6540 if (!formal && head)
6541 arg_count_mismatch = true;
6543 /* Abbreviated module procedure declaration is not meant to have any
6544 formal arguments! */
6545 if (!progname->abr_modproc_decl && formal && !head)
6546 arg_count_mismatch = true;
6548 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6550 if ((p->next != NULL && q->next == NULL)
6551 || (p->next == NULL && q->next != NULL))
6552 arg_count_mismatch = true;
6553 else if ((p->sym == NULL && q->sym == NULL)
6554 || strcmp (p->sym->name, q->sym->name) == 0)
6555 continue;
6556 else
6557 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6558 "argument names (%s/%s) at %C",
6559 p->sym->name, q->sym->name);
6562 if (arg_count_mismatch)
6563 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6564 "formal arguments at %C");
6567 return MATCH_YES;
6569 cleanup:
6570 gfc_free_formal_arglist (head);
6571 return m;
6575 /* Match a RESULT specification following a function declaration or
6576 ENTRY statement. Also matches the end-of-statement. */
6578 static match
6579 match_result (gfc_symbol *function, gfc_symbol **result)
6581 char name[GFC_MAX_SYMBOL_LEN + 1];
6582 gfc_symbol *r;
6583 match m;
6585 if (gfc_match (" result (") != MATCH_YES)
6586 return MATCH_NO;
6588 m = gfc_match_name (name);
6589 if (m != MATCH_YES)
6590 return m;
6592 /* Get the right paren, and that's it because there could be the
6593 bind(c) attribute after the result clause. */
6594 if (gfc_match_char (')') != MATCH_YES)
6596 /* TODO: should report the missing right paren here. */
6597 return MATCH_ERROR;
6600 if (strcmp (function->name, name) == 0)
6602 gfc_error ("RESULT variable at %C must be different than function name");
6603 return MATCH_ERROR;
6606 if (gfc_get_symbol (name, NULL, &r))
6607 return MATCH_ERROR;
6609 if (!gfc_add_result (&r->attr, r->name, NULL))
6610 return MATCH_ERROR;
6612 *result = r;
6614 return MATCH_YES;
6618 /* Match a function suffix, which could be a combination of a result
6619 clause and BIND(C), either one, or neither. The draft does not
6620 require them to come in a specific order. */
6622 match
6623 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6625 match is_bind_c; /* Found bind(c). */
6626 match is_result; /* Found result clause. */
6627 match found_match; /* Status of whether we've found a good match. */
6628 char peek_char; /* Character we're going to peek at. */
6629 bool allow_binding_name;
6631 /* Initialize to having found nothing. */
6632 found_match = MATCH_NO;
6633 is_bind_c = MATCH_NO;
6634 is_result = MATCH_NO;
6636 /* Get the next char to narrow between result and bind(c). */
6637 gfc_gobble_whitespace ();
6638 peek_char = gfc_peek_ascii_char ();
6640 /* C binding names are not allowed for internal procedures. */
6641 if (gfc_current_state () == COMP_CONTAINS
6642 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6643 allow_binding_name = false;
6644 else
6645 allow_binding_name = true;
6647 switch (peek_char)
6649 case 'r':
6650 /* Look for result clause. */
6651 is_result = match_result (sym, result);
6652 if (is_result == MATCH_YES)
6654 /* Now see if there is a bind(c) after it. */
6655 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6656 /* We've found the result clause and possibly bind(c). */
6657 found_match = MATCH_YES;
6659 else
6660 /* This should only be MATCH_ERROR. */
6661 found_match = is_result;
6662 break;
6663 case 'b':
6664 /* Look for bind(c) first. */
6665 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6666 if (is_bind_c == MATCH_YES)
6668 /* Now see if a result clause followed it. */
6669 is_result = match_result (sym, result);
6670 found_match = MATCH_YES;
6672 else
6674 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6675 found_match = MATCH_ERROR;
6677 break;
6678 default:
6679 gfc_error ("Unexpected junk after function declaration at %C");
6680 found_match = MATCH_ERROR;
6681 break;
6684 if (is_bind_c == MATCH_YES)
6686 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
6687 if (gfc_current_state () == COMP_CONTAINS
6688 && sym->ns->proc_name->attr.flavor != FL_MODULE
6689 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6690 "at %L may not be specified for an internal "
6691 "procedure", &gfc_current_locus))
6692 return MATCH_ERROR;
6694 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6695 return MATCH_ERROR;
6698 return found_match;
6702 /* Procedure pointer return value without RESULT statement:
6703 Add "hidden" result variable named "ppr@". */
6705 static bool
6706 add_hidden_procptr_result (gfc_symbol *sym)
6708 bool case1,case2;
6710 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6711 return false;
6713 /* First usage case: PROCEDURE and EXTERNAL statements. */
6714 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6715 && strcmp (gfc_current_block ()->name, sym->name) == 0
6716 && sym->attr.external;
6717 /* Second usage case: INTERFACE statements. */
6718 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6719 && gfc_state_stack->previous->state == COMP_FUNCTION
6720 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6722 if (case1 || case2)
6724 gfc_symtree *stree;
6725 if (case1)
6726 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6727 else
6729 gfc_symtree *st2;
6730 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6731 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6732 st2->n.sym = stree->n.sym;
6733 stree->n.sym->refs++;
6735 sym->result = stree->n.sym;
6737 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6738 sym->result->attr.pointer = sym->attr.pointer;
6739 sym->result->attr.external = sym->attr.external;
6740 sym->result->attr.referenced = sym->attr.referenced;
6741 sym->result->ts = sym->ts;
6742 sym->attr.proc_pointer = 0;
6743 sym->attr.pointer = 0;
6744 sym->attr.external = 0;
6745 if (sym->result->attr.external && sym->result->attr.pointer)
6747 sym->result->attr.pointer = 0;
6748 sym->result->attr.proc_pointer = 1;
6751 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6753 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6754 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6755 && sym->result && sym->result != sym && sym->result->attr.external
6756 && sym == gfc_current_ns->proc_name
6757 && sym == sym->result->ns->proc_name
6758 && strcmp ("ppr@", sym->result->name) == 0)
6760 sym->result->attr.proc_pointer = 1;
6761 sym->attr.pointer = 0;
6762 return true;
6764 else
6765 return false;
6769 /* Match the interface for a PROCEDURE declaration,
6770 including brackets (R1212). */
6772 static match
6773 match_procedure_interface (gfc_symbol **proc_if)
6775 match m;
6776 gfc_symtree *st;
6777 locus old_loc, entry_loc;
6778 gfc_namespace *old_ns = gfc_current_ns;
6779 char name[GFC_MAX_SYMBOL_LEN + 1];
6781 old_loc = entry_loc = gfc_current_locus;
6782 gfc_clear_ts (&current_ts);
6784 if (gfc_match (" (") != MATCH_YES)
6786 gfc_current_locus = entry_loc;
6787 return MATCH_NO;
6790 /* Get the type spec. for the procedure interface. */
6791 old_loc = gfc_current_locus;
6792 m = gfc_match_decl_type_spec (&current_ts, 0);
6793 gfc_gobble_whitespace ();
6794 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6795 goto got_ts;
6797 if (m == MATCH_ERROR)
6798 return m;
6800 /* Procedure interface is itself a procedure. */
6801 gfc_current_locus = old_loc;
6802 m = gfc_match_name (name);
6804 /* First look to see if it is already accessible in the current
6805 namespace because it is use associated or contained. */
6806 st = NULL;
6807 if (gfc_find_sym_tree (name, NULL, 0, &st))
6808 return MATCH_ERROR;
6810 /* If it is still not found, then try the parent namespace, if it
6811 exists and create the symbol there if it is still not found. */
6812 if (gfc_current_ns->parent)
6813 gfc_current_ns = gfc_current_ns->parent;
6814 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6815 return MATCH_ERROR;
6817 gfc_current_ns = old_ns;
6818 *proc_if = st->n.sym;
6820 if (*proc_if)
6822 (*proc_if)->refs++;
6823 /* Resolve interface if possible. That way, attr.procedure is only set
6824 if it is declared by a later procedure-declaration-stmt, which is
6825 invalid per F08:C1216 (cf. resolve_procedure_interface). */
6826 while ((*proc_if)->ts.interface
6827 && *proc_if != (*proc_if)->ts.interface)
6828 *proc_if = (*proc_if)->ts.interface;
6830 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6831 && (*proc_if)->ts.type == BT_UNKNOWN
6832 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6833 (*proc_if)->name, NULL))
6834 return MATCH_ERROR;
6837 got_ts:
6838 if (gfc_match (" )") != MATCH_YES)
6840 gfc_current_locus = entry_loc;
6841 return MATCH_NO;
6844 return MATCH_YES;
6848 /* Match a PROCEDURE declaration (R1211). */
6850 static match
6851 match_procedure_decl (void)
6853 match m;
6854 gfc_symbol *sym, *proc_if = NULL;
6855 int num;
6856 gfc_expr *initializer = NULL;
6858 /* Parse interface (with brackets). */
6859 m = match_procedure_interface (&proc_if);
6860 if (m != MATCH_YES)
6861 return m;
6863 /* Parse attributes (with colons). */
6864 m = match_attr_spec();
6865 if (m == MATCH_ERROR)
6866 return MATCH_ERROR;
6868 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6870 current_attr.is_bind_c = 1;
6871 has_name_equals = 0;
6872 curr_binding_label = NULL;
6875 /* Get procedure symbols. */
6876 for(num=1;;num++)
6878 m = gfc_match_symbol (&sym, 0);
6879 if (m == MATCH_NO)
6880 goto syntax;
6881 else if (m == MATCH_ERROR)
6882 return m;
6884 /* Add current_attr to the symbol attributes. */
6885 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
6886 return MATCH_ERROR;
6888 if (sym->attr.is_bind_c)
6890 /* Check for C1218. */
6891 if (!proc_if || !proc_if->attr.is_bind_c)
6893 gfc_error ("BIND(C) attribute at %C requires "
6894 "an interface with BIND(C)");
6895 return MATCH_ERROR;
6897 /* Check for C1217. */
6898 if (has_name_equals && sym->attr.pointer)
6900 gfc_error ("BIND(C) procedure with NAME may not have "
6901 "POINTER attribute at %C");
6902 return MATCH_ERROR;
6904 if (has_name_equals && sym->attr.dummy)
6906 gfc_error ("Dummy procedure at %C may not have "
6907 "BIND(C) attribute with NAME");
6908 return MATCH_ERROR;
6910 /* Set binding label for BIND(C). */
6911 if (!set_binding_label (&sym->binding_label, sym->name, num))
6912 return MATCH_ERROR;
6915 if (!gfc_add_external (&sym->attr, NULL))
6916 return MATCH_ERROR;
6918 if (add_hidden_procptr_result (sym))
6919 sym = sym->result;
6921 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
6922 return MATCH_ERROR;
6924 /* Set interface. */
6925 if (proc_if != NULL)
6927 if (sym->ts.type != BT_UNKNOWN)
6929 gfc_error ("Procedure %qs at %L already has basic type of %s",
6930 sym->name, &gfc_current_locus,
6931 gfc_basic_typename (sym->ts.type));
6932 return MATCH_ERROR;
6934 sym->ts.interface = proc_if;
6935 sym->attr.untyped = 1;
6936 sym->attr.if_source = IFSRC_IFBODY;
6938 else if (current_ts.type != BT_UNKNOWN)
6940 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
6941 return MATCH_ERROR;
6942 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6943 sym->ts.interface->ts = current_ts;
6944 sym->ts.interface->attr.flavor = FL_PROCEDURE;
6945 sym->ts.interface->attr.function = 1;
6946 sym->attr.function = 1;
6947 sym->attr.if_source = IFSRC_UNKNOWN;
6950 if (gfc_match (" =>") == MATCH_YES)
6952 if (!current_attr.pointer)
6954 gfc_error ("Initialization at %C isn't for a pointer variable");
6955 m = MATCH_ERROR;
6956 goto cleanup;
6959 m = match_pointer_init (&initializer, 1);
6960 if (m != MATCH_YES)
6961 goto cleanup;
6963 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
6964 goto cleanup;
6968 if (gfc_match_eos () == MATCH_YES)
6969 return MATCH_YES;
6970 if (gfc_match_char (',') != MATCH_YES)
6971 goto syntax;
6974 syntax:
6975 gfc_error ("Syntax error in PROCEDURE statement at %C");
6976 return MATCH_ERROR;
6978 cleanup:
6979 /* Free stuff up and return. */
6980 gfc_free_expr (initializer);
6981 return m;
6985 static match
6986 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
6989 /* Match a procedure pointer component declaration (R445). */
6991 static match
6992 match_ppc_decl (void)
6994 match m;
6995 gfc_symbol *proc_if = NULL;
6996 gfc_typespec ts;
6997 int num;
6998 gfc_component *c;
6999 gfc_expr *initializer = NULL;
7000 gfc_typebound_proc* tb;
7001 char name[GFC_MAX_SYMBOL_LEN + 1];
7003 /* Parse interface (with brackets). */
7004 m = match_procedure_interface (&proc_if);
7005 if (m != MATCH_YES)
7006 goto syntax;
7008 /* Parse attributes. */
7009 tb = XCNEW (gfc_typebound_proc);
7010 tb->where = gfc_current_locus;
7011 m = match_binding_attributes (tb, false, true);
7012 if (m == MATCH_ERROR)
7013 return m;
7015 gfc_clear_attr (&current_attr);
7016 current_attr.procedure = 1;
7017 current_attr.proc_pointer = 1;
7018 current_attr.access = tb->access;
7019 current_attr.flavor = FL_PROCEDURE;
7021 /* Match the colons (required). */
7022 if (gfc_match (" ::") != MATCH_YES)
7024 gfc_error ("Expected %<::%> after binding-attributes at %C");
7025 return MATCH_ERROR;
7028 /* Check for C450. */
7029 if (!tb->nopass && proc_if == NULL)
7031 gfc_error("NOPASS or explicit interface required at %C");
7032 return MATCH_ERROR;
7035 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
7036 return MATCH_ERROR;
7038 /* Match PPC names. */
7039 ts = current_ts;
7040 for(num=1;;num++)
7042 m = gfc_match_name (name);
7043 if (m == MATCH_NO)
7044 goto syntax;
7045 else if (m == MATCH_ERROR)
7046 return m;
7048 if (!gfc_add_component (gfc_current_block(), name, &c))
7049 return MATCH_ERROR;
7051 /* Add current_attr to the symbol attributes. */
7052 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
7053 return MATCH_ERROR;
7055 if (!gfc_add_external (&c->attr, NULL))
7056 return MATCH_ERROR;
7058 if (!gfc_add_proc (&c->attr, name, NULL))
7059 return MATCH_ERROR;
7061 if (num == 1)
7062 c->tb = tb;
7063 else
7065 c->tb = XCNEW (gfc_typebound_proc);
7066 c->tb->where = gfc_current_locus;
7067 *c->tb = *tb;
7070 /* Set interface. */
7071 if (proc_if != NULL)
7073 c->ts.interface = proc_if;
7074 c->attr.untyped = 1;
7075 c->attr.if_source = IFSRC_IFBODY;
7077 else if (ts.type != BT_UNKNOWN)
7079 c->ts = ts;
7080 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7081 c->ts.interface->result = c->ts.interface;
7082 c->ts.interface->ts = ts;
7083 c->ts.interface->attr.flavor = FL_PROCEDURE;
7084 c->ts.interface->attr.function = 1;
7085 c->attr.function = 1;
7086 c->attr.if_source = IFSRC_UNKNOWN;
7089 if (gfc_match (" =>") == MATCH_YES)
7091 m = match_pointer_init (&initializer, 1);
7092 if (m != MATCH_YES)
7094 gfc_free_expr (initializer);
7095 return m;
7097 c->initializer = initializer;
7100 if (gfc_match_eos () == MATCH_YES)
7101 return MATCH_YES;
7102 if (gfc_match_char (',') != MATCH_YES)
7103 goto syntax;
7106 syntax:
7107 gfc_error ("Syntax error in procedure pointer component at %C");
7108 return MATCH_ERROR;
7112 /* Match a PROCEDURE declaration inside an interface (R1206). */
7114 static match
7115 match_procedure_in_interface (void)
7117 match m;
7118 gfc_symbol *sym;
7119 char name[GFC_MAX_SYMBOL_LEN + 1];
7120 locus old_locus;
7122 if (current_interface.type == INTERFACE_NAMELESS
7123 || current_interface.type == INTERFACE_ABSTRACT)
7125 gfc_error ("PROCEDURE at %C must be in a generic interface");
7126 return MATCH_ERROR;
7129 /* Check if the F2008 optional double colon appears. */
7130 gfc_gobble_whitespace ();
7131 old_locus = gfc_current_locus;
7132 if (gfc_match ("::") == MATCH_YES)
7134 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7135 "MODULE PROCEDURE statement at %L", &old_locus))
7136 return MATCH_ERROR;
7138 else
7139 gfc_current_locus = old_locus;
7141 for(;;)
7143 m = gfc_match_name (name);
7144 if (m == MATCH_NO)
7145 goto syntax;
7146 else if (m == MATCH_ERROR)
7147 return m;
7148 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
7149 return MATCH_ERROR;
7151 if (!gfc_add_interface (sym))
7152 return MATCH_ERROR;
7154 if (gfc_match_eos () == MATCH_YES)
7155 break;
7156 if (gfc_match_char (',') != MATCH_YES)
7157 goto syntax;
7160 return MATCH_YES;
7162 syntax:
7163 gfc_error ("Syntax error in PROCEDURE statement at %C");
7164 return MATCH_ERROR;
7168 /* General matcher for PROCEDURE declarations. */
7170 static match match_procedure_in_type (void);
7172 match
7173 gfc_match_procedure (void)
7175 match m;
7177 switch (gfc_current_state ())
7179 case COMP_NONE:
7180 case COMP_PROGRAM:
7181 case COMP_MODULE:
7182 case COMP_SUBMODULE:
7183 case COMP_SUBROUTINE:
7184 case COMP_FUNCTION:
7185 case COMP_BLOCK:
7186 m = match_procedure_decl ();
7187 break;
7188 case COMP_INTERFACE:
7189 m = match_procedure_in_interface ();
7190 break;
7191 case COMP_DERIVED:
7192 m = match_ppc_decl ();
7193 break;
7194 case COMP_DERIVED_CONTAINS:
7195 m = match_procedure_in_type ();
7196 break;
7197 default:
7198 return MATCH_NO;
7201 if (m != MATCH_YES)
7202 return m;
7204 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
7205 return MATCH_ERROR;
7207 return m;
7211 /* Warn if a matched procedure has the same name as an intrinsic; this is
7212 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7213 parser-state-stack to find out whether we're in a module. */
7215 static void
7216 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
7218 bool in_module;
7220 in_module = (gfc_state_stack->previous
7221 && (gfc_state_stack->previous->state == COMP_MODULE
7222 || gfc_state_stack->previous->state == COMP_SUBMODULE));
7224 gfc_warn_intrinsic_shadow (sym, in_module, func);
7228 /* Match a function declaration. */
7230 match
7231 gfc_match_function_decl (void)
7233 char name[GFC_MAX_SYMBOL_LEN + 1];
7234 gfc_symbol *sym, *result;
7235 locus old_loc;
7236 match m;
7237 match suffix_match;
7238 match found_match; /* Status returned by match func. */
7240 if (gfc_current_state () != COMP_NONE
7241 && gfc_current_state () != COMP_INTERFACE
7242 && gfc_current_state () != COMP_CONTAINS)
7243 return MATCH_NO;
7245 gfc_clear_ts (&current_ts);
7247 old_loc = gfc_current_locus;
7249 m = gfc_match_prefix (&current_ts);
7250 if (m != MATCH_YES)
7252 gfc_current_locus = old_loc;
7253 return m;
7256 if (gfc_match ("function% %n", name) != MATCH_YES)
7258 gfc_current_locus = old_loc;
7259 return MATCH_NO;
7262 if (get_proc_name (name, &sym, false))
7263 return MATCH_ERROR;
7265 if (add_hidden_procptr_result (sym))
7266 sym = sym->result;
7268 if (current_attr.module_procedure)
7269 sym->attr.module_procedure = 1;
7271 gfc_new_block = sym;
7273 m = gfc_match_formal_arglist (sym, 0, 0);
7274 if (m == MATCH_NO)
7276 gfc_error ("Expected formal argument list in function "
7277 "definition at %C");
7278 m = MATCH_ERROR;
7279 goto cleanup;
7281 else if (m == MATCH_ERROR)
7282 goto cleanup;
7284 result = NULL;
7286 /* According to the draft, the bind(c) and result clause can
7287 come in either order after the formal_arg_list (i.e., either
7288 can be first, both can exist together or by themselves or neither
7289 one). Therefore, the match_result can't match the end of the
7290 string, and check for the bind(c) or result clause in either order. */
7291 found_match = gfc_match_eos ();
7293 /* Make sure that it isn't already declared as BIND(C). If it is, it
7294 must have been marked BIND(C) with a BIND(C) attribute and that is
7295 not allowed for procedures. */
7296 if (sym->attr.is_bind_c == 1)
7298 sym->attr.is_bind_c = 0;
7300 if (gfc_state_stack->previous
7301 && gfc_state_stack->previous->state != COMP_SUBMODULE)
7303 locus loc;
7304 loc = sym->old_symbol != NULL
7305 ? sym->old_symbol->declared_at : gfc_current_locus;
7306 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7307 "variables or common blocks", &loc);
7311 if (found_match != MATCH_YES)
7313 /* If we haven't found the end-of-statement, look for a suffix. */
7314 suffix_match = gfc_match_suffix (sym, &result);
7315 if (suffix_match == MATCH_YES)
7316 /* Need to get the eos now. */
7317 found_match = gfc_match_eos ();
7318 else
7319 found_match = suffix_match;
7322 /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
7323 subprogram and a binding label is specified, it shall be the
7324 same as the binding label specified in the corresponding module
7325 procedure interface body. */
7326 if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
7327 && strcmp (sym->name, sym->old_symbol->name) == 0
7328 && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
7330 const char *null = "NULL", *s1, *s2;
7331 s1 = sym->binding_label;
7332 if (!s1) s1 = null;
7333 s2 = sym->old_symbol->binding_label;
7334 if (!s2) s2 = null;
7335 gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
7336 sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
7337 return MATCH_ERROR;
7340 if(found_match != MATCH_YES)
7341 m = MATCH_ERROR;
7342 else
7344 /* Make changes to the symbol. */
7345 m = MATCH_ERROR;
7347 if (!gfc_add_function (&sym->attr, sym->name, NULL))
7348 goto cleanup;
7350 if (!gfc_missing_attr (&sym->attr, NULL))
7351 goto cleanup;
7353 if (!copy_prefix (&sym->attr, &sym->declared_at))
7355 if(!sym->attr.module_procedure)
7356 goto cleanup;
7357 else
7358 gfc_error_check ();
7361 /* Delay matching the function characteristics until after the
7362 specification block by signalling kind=-1. */
7363 sym->declared_at = old_loc;
7364 if (current_ts.type != BT_UNKNOWN)
7365 current_ts.kind = -1;
7366 else
7367 current_ts.kind = 0;
7369 if (result == NULL)
7371 if (current_ts.type != BT_UNKNOWN
7372 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
7373 goto cleanup;
7374 sym->result = sym;
7376 else
7378 if (current_ts.type != BT_UNKNOWN
7379 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
7380 goto cleanup;
7381 sym->result = result;
7384 /* Warn if this procedure has the same name as an intrinsic. */
7385 do_warn_intrinsic_shadow (sym, true);
7387 return MATCH_YES;
7390 cleanup:
7391 gfc_current_locus = old_loc;
7392 return m;
7396 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
7397 pass the name of the entry, rather than the gfc_current_block name, and
7398 to return false upon finding an existing global entry. */
7400 static bool
7401 add_global_entry (const char *name, const char *binding_label, bool sub,
7402 locus *where)
7404 gfc_gsymbol *s;
7405 enum gfc_symbol_type type;
7407 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7409 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7410 name is a global identifier. */
7411 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
7413 s = gfc_get_gsymbol (name, false);
7415 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7417 gfc_global_used (s, where);
7418 return false;
7420 else
7422 s->type = type;
7423 s->sym_name = name;
7424 s->where = *where;
7425 s->defined = 1;
7426 s->ns = gfc_current_ns;
7430 /* Don't add the symbol multiple times. */
7431 if (binding_label
7432 && (!gfc_notification_std (GFC_STD_F2008)
7433 || strcmp (name, binding_label) != 0))
7435 s = gfc_get_gsymbol (binding_label, true);
7437 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7439 gfc_global_used (s, where);
7440 return false;
7442 else
7444 s->type = type;
7445 s->sym_name = name;
7446 s->binding_label = binding_label;
7447 s->where = *where;
7448 s->defined = 1;
7449 s->ns = gfc_current_ns;
7453 return true;
7457 /* Match an ENTRY statement. */
7459 match
7460 gfc_match_entry (void)
7462 gfc_symbol *proc;
7463 gfc_symbol *result;
7464 gfc_symbol *entry;
7465 char name[GFC_MAX_SYMBOL_LEN + 1];
7466 gfc_compile_state state;
7467 match m;
7468 gfc_entry_list *el;
7469 locus old_loc;
7470 bool module_procedure;
7471 char peek_char;
7472 match is_bind_c;
7474 m = gfc_match_name (name);
7475 if (m != MATCH_YES)
7476 return m;
7478 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
7479 return MATCH_ERROR;
7481 state = gfc_current_state ();
7482 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
7484 switch (state)
7486 case COMP_PROGRAM:
7487 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7488 break;
7489 case COMP_MODULE:
7490 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7491 break;
7492 case COMP_SUBMODULE:
7493 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7494 break;
7495 case COMP_BLOCK_DATA:
7496 gfc_error ("ENTRY statement at %C cannot appear within "
7497 "a BLOCK DATA");
7498 break;
7499 case COMP_INTERFACE:
7500 gfc_error ("ENTRY statement at %C cannot appear within "
7501 "an INTERFACE");
7502 break;
7503 case COMP_STRUCTURE:
7504 gfc_error ("ENTRY statement at %C cannot appear within "
7505 "a STRUCTURE block");
7506 break;
7507 case COMP_DERIVED:
7508 gfc_error ("ENTRY statement at %C cannot appear within "
7509 "a DERIVED TYPE block");
7510 break;
7511 case COMP_IF:
7512 gfc_error ("ENTRY statement at %C cannot appear within "
7513 "an IF-THEN block");
7514 break;
7515 case COMP_DO:
7516 case COMP_DO_CONCURRENT:
7517 gfc_error ("ENTRY statement at %C cannot appear within "
7518 "a DO block");
7519 break;
7520 case COMP_SELECT:
7521 gfc_error ("ENTRY statement at %C cannot appear within "
7522 "a SELECT block");
7523 break;
7524 case COMP_FORALL:
7525 gfc_error ("ENTRY statement at %C cannot appear within "
7526 "a FORALL block");
7527 break;
7528 case COMP_WHERE:
7529 gfc_error ("ENTRY statement at %C cannot appear within "
7530 "a WHERE block");
7531 break;
7532 case COMP_CONTAINS:
7533 gfc_error ("ENTRY statement at %C cannot appear within "
7534 "a contained subprogram");
7535 break;
7536 default:
7537 gfc_error ("Unexpected ENTRY statement at %C");
7539 return MATCH_ERROR;
7542 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7543 && gfc_state_stack->previous->state == COMP_INTERFACE)
7545 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7546 return MATCH_ERROR;
7549 module_procedure = gfc_current_ns->parent != NULL
7550 && gfc_current_ns->parent->proc_name
7551 && gfc_current_ns->parent->proc_name->attr.flavor
7552 == FL_MODULE;
7554 if (gfc_current_ns->parent != NULL
7555 && gfc_current_ns->parent->proc_name
7556 && !module_procedure)
7558 gfc_error("ENTRY statement at %C cannot appear in a "
7559 "contained procedure");
7560 return MATCH_ERROR;
7563 /* Module function entries need special care in get_proc_name
7564 because previous references within the function will have
7565 created symbols attached to the current namespace. */
7566 if (get_proc_name (name, &entry,
7567 gfc_current_ns->parent != NULL
7568 && module_procedure))
7569 return MATCH_ERROR;
7571 proc = gfc_current_block ();
7573 /* Make sure that it isn't already declared as BIND(C). If it is, it
7574 must have been marked BIND(C) with a BIND(C) attribute and that is
7575 not allowed for procedures. */
7576 if (entry->attr.is_bind_c == 1)
7578 locus loc;
7580 entry->attr.is_bind_c = 0;
7582 loc = entry->old_symbol != NULL
7583 ? entry->old_symbol->declared_at : gfc_current_locus;
7584 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7585 "variables or common blocks", &loc);
7588 /* Check what next non-whitespace character is so we can tell if there
7589 is the required parens if we have a BIND(C). */
7590 old_loc = gfc_current_locus;
7591 gfc_gobble_whitespace ();
7592 peek_char = gfc_peek_ascii_char ();
7594 if (state == COMP_SUBROUTINE)
7596 m = gfc_match_formal_arglist (entry, 0, 1);
7597 if (m != MATCH_YES)
7598 return MATCH_ERROR;
7600 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7601 never be an internal procedure. */
7602 is_bind_c = gfc_match_bind_c (entry, true);
7603 if (is_bind_c == MATCH_ERROR)
7604 return MATCH_ERROR;
7605 if (is_bind_c == MATCH_YES)
7607 if (peek_char != '(')
7609 gfc_error ("Missing required parentheses before BIND(C) at %C");
7610 return MATCH_ERROR;
7613 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7614 &(entry->declared_at), 1))
7615 return MATCH_ERROR;
7619 if (!gfc_current_ns->parent
7620 && !add_global_entry (name, entry->binding_label, true,
7621 &old_loc))
7622 return MATCH_ERROR;
7624 /* An entry in a subroutine. */
7625 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7626 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7627 return MATCH_ERROR;
7629 else
7631 /* An entry in a function.
7632 We need to take special care because writing
7633 ENTRY f()
7635 ENTRY f
7636 is allowed, whereas
7637 ENTRY f() RESULT (r)
7638 can't be written as
7639 ENTRY f RESULT (r). */
7640 if (gfc_match_eos () == MATCH_YES)
7642 gfc_current_locus = old_loc;
7643 /* Match the empty argument list, and add the interface to
7644 the symbol. */
7645 m = gfc_match_formal_arglist (entry, 0, 1);
7647 else
7648 m = gfc_match_formal_arglist (entry, 0, 0);
7650 if (m != MATCH_YES)
7651 return MATCH_ERROR;
7653 result = NULL;
7655 if (gfc_match_eos () == MATCH_YES)
7657 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7658 || !gfc_add_function (&entry->attr, entry->name, NULL))
7659 return MATCH_ERROR;
7661 entry->result = entry;
7663 else
7665 m = gfc_match_suffix (entry, &result);
7666 if (m == MATCH_NO)
7667 gfc_syntax_error (ST_ENTRY);
7668 if (m != MATCH_YES)
7669 return MATCH_ERROR;
7671 if (result)
7673 if (!gfc_add_result (&result->attr, result->name, NULL)
7674 || !gfc_add_entry (&entry->attr, result->name, NULL)
7675 || !gfc_add_function (&entry->attr, result->name, NULL))
7676 return MATCH_ERROR;
7677 entry->result = result;
7679 else
7681 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7682 || !gfc_add_function (&entry->attr, entry->name, NULL))
7683 return MATCH_ERROR;
7684 entry->result = entry;
7688 if (!gfc_current_ns->parent
7689 && !add_global_entry (name, entry->binding_label, false,
7690 &old_loc))
7691 return MATCH_ERROR;
7694 if (gfc_match_eos () != MATCH_YES)
7696 gfc_syntax_error (ST_ENTRY);
7697 return MATCH_ERROR;
7700 /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
7701 if (proc->attr.elemental && entry->attr.is_bind_c)
7703 gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
7704 "elemental procedure", &entry->declared_at);
7705 return MATCH_ERROR;
7708 entry->attr.recursive = proc->attr.recursive;
7709 entry->attr.elemental = proc->attr.elemental;
7710 entry->attr.pure = proc->attr.pure;
7712 el = gfc_get_entry_list ();
7713 el->sym = entry;
7714 el->next = gfc_current_ns->entries;
7715 gfc_current_ns->entries = el;
7716 if (el->next)
7717 el->id = el->next->id + 1;
7718 else
7719 el->id = 1;
7721 new_st.op = EXEC_ENTRY;
7722 new_st.ext.entry = el;
7724 return MATCH_YES;
7728 /* Match a subroutine statement, including optional prefixes. */
7730 match
7731 gfc_match_subroutine (void)
7733 char name[GFC_MAX_SYMBOL_LEN + 1];
7734 gfc_symbol *sym;
7735 match m;
7736 match is_bind_c;
7737 char peek_char;
7738 bool allow_binding_name;
7739 locus loc;
7741 if (gfc_current_state () != COMP_NONE
7742 && gfc_current_state () != COMP_INTERFACE
7743 && gfc_current_state () != COMP_CONTAINS)
7744 return MATCH_NO;
7746 m = gfc_match_prefix (NULL);
7747 if (m != MATCH_YES)
7748 return m;
7750 m = gfc_match ("subroutine% %n", name);
7751 if (m != MATCH_YES)
7752 return m;
7754 if (get_proc_name (name, &sym, false))
7755 return MATCH_ERROR;
7757 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7758 the symbol existed before. */
7759 sym->declared_at = gfc_current_locus;
7761 if (current_attr.module_procedure)
7762 sym->attr.module_procedure = 1;
7764 if (add_hidden_procptr_result (sym))
7765 sym = sym->result;
7767 gfc_new_block = sym;
7769 /* Check what next non-whitespace character is so we can tell if there
7770 is the required parens if we have a BIND(C). */
7771 gfc_gobble_whitespace ();
7772 peek_char = gfc_peek_ascii_char ();
7774 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7775 return MATCH_ERROR;
7777 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7778 return MATCH_ERROR;
7780 /* Make sure that it isn't already declared as BIND(C). If it is, it
7781 must have been marked BIND(C) with a BIND(C) attribute and that is
7782 not allowed for procedures. */
7783 if (sym->attr.is_bind_c == 1)
7785 sym->attr.is_bind_c = 0;
7787 if (gfc_state_stack->previous
7788 && gfc_state_stack->previous->state != COMP_SUBMODULE)
7790 locus loc;
7791 loc = sym->old_symbol != NULL
7792 ? sym->old_symbol->declared_at : gfc_current_locus;
7793 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7794 "variables or common blocks", &loc);
7798 /* C binding names are not allowed for internal procedures. */
7799 if (gfc_current_state () == COMP_CONTAINS
7800 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7801 allow_binding_name = false;
7802 else
7803 allow_binding_name = true;
7805 /* Here, we are just checking if it has the bind(c) attribute, and if
7806 so, then we need to make sure it's all correct. If it doesn't,
7807 we still need to continue matching the rest of the subroutine line. */
7808 gfc_gobble_whitespace ();
7809 loc = gfc_current_locus;
7810 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7811 if (is_bind_c == MATCH_ERROR)
7813 /* There was an attempt at the bind(c), but it was wrong. An
7814 error message should have been printed w/in the gfc_match_bind_c
7815 so here we'll just return the MATCH_ERROR. */
7816 return MATCH_ERROR;
7819 if (is_bind_c == MATCH_YES)
7821 gfc_formal_arglist *arg;
7823 /* The following is allowed in the Fortran 2008 draft. */
7824 if (gfc_current_state () == COMP_CONTAINS
7825 && sym->ns->proc_name->attr.flavor != FL_MODULE
7826 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7827 "at %L may not be specified for an internal "
7828 "procedure", &gfc_current_locus))
7829 return MATCH_ERROR;
7831 if (peek_char != '(')
7833 gfc_error ("Missing required parentheses before BIND(C) at %C");
7834 return MATCH_ERROR;
7837 /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
7838 subprogram and a binding label is specified, it shall be the
7839 same as the binding label specified in the corresponding module
7840 procedure interface body. */
7841 if (sym->attr.module_procedure && sym->old_symbol
7842 && strcmp (sym->name, sym->old_symbol->name) == 0
7843 && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
7845 const char *null = "NULL", *s1, *s2;
7846 s1 = sym->binding_label;
7847 if (!s1) s1 = null;
7848 s2 = sym->old_symbol->binding_label;
7849 if (!s2) s2 = null;
7850 gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
7851 sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
7852 return MATCH_ERROR;
7855 /* Scan the dummy arguments for an alternate return. */
7856 for (arg = sym->formal; arg; arg = arg->next)
7857 if (!arg->sym)
7859 gfc_error ("Alternate return dummy argument cannot appear in a "
7860 "SUBROUTINE with the BIND(C) attribute at %L", &loc);
7861 return MATCH_ERROR;
7864 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
7865 return MATCH_ERROR;
7868 if (gfc_match_eos () != MATCH_YES)
7870 gfc_syntax_error (ST_SUBROUTINE);
7871 return MATCH_ERROR;
7874 if (!copy_prefix (&sym->attr, &sym->declared_at))
7876 if(!sym->attr.module_procedure)
7877 return MATCH_ERROR;
7878 else
7879 gfc_error_check ();
7882 /* Warn if it has the same name as an intrinsic. */
7883 do_warn_intrinsic_shadow (sym, false);
7885 return MATCH_YES;
7889 /* Check that the NAME identifier in a BIND attribute or statement
7890 is conform to C identifier rules. */
7892 match
7893 check_bind_name_identifier (char **name)
7895 char *n = *name, *p;
7897 /* Remove leading spaces. */
7898 while (*n == ' ')
7899 n++;
7901 /* On an empty string, free memory and set name to NULL. */
7902 if (*n == '\0')
7904 free (*name);
7905 *name = NULL;
7906 return MATCH_YES;
7909 /* Remove trailing spaces. */
7910 p = n + strlen(n) - 1;
7911 while (*p == ' ')
7912 *(p--) = '\0';
7914 /* Insert the identifier into the symbol table. */
7915 p = xstrdup (n);
7916 free (*name);
7917 *name = p;
7919 /* Now check that identifier is valid under C rules. */
7920 if (ISDIGIT (*p))
7922 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7923 return MATCH_ERROR;
7926 for (; *p; p++)
7927 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
7929 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7930 return MATCH_ERROR;
7933 return MATCH_YES;
7937 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
7938 given, and set the binding label in either the given symbol (if not
7939 NULL), or in the current_ts. The symbol may be NULL because we may
7940 encounter the BIND(C) before the declaration itself. Return
7941 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
7942 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
7943 or MATCH_YES if the specifier was correct and the binding label and
7944 bind(c) fields were set correctly for the given symbol or the
7945 current_ts. If allow_binding_name is false, no binding name may be
7946 given. */
7948 match
7949 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
7951 char *binding_label = NULL;
7952 gfc_expr *e = NULL;
7954 /* Initialize the flag that specifies whether we encountered a NAME=
7955 specifier or not. */
7956 has_name_equals = 0;
7958 /* This much we have to be able to match, in this order, if
7959 there is a bind(c) label. */
7960 if (gfc_match (" bind ( c ") != MATCH_YES)
7961 return MATCH_NO;
7963 /* Now see if there is a binding label, or if we've reached the
7964 end of the bind(c) attribute without one. */
7965 if (gfc_match_char (',') == MATCH_YES)
7967 if (gfc_match (" name = ") != MATCH_YES)
7969 gfc_error ("Syntax error in NAME= specifier for binding label "
7970 "at %C");
7971 /* should give an error message here */
7972 return MATCH_ERROR;
7975 has_name_equals = 1;
7977 if (gfc_match_init_expr (&e) != MATCH_YES)
7979 gfc_free_expr (e);
7980 return MATCH_ERROR;
7983 if (!gfc_simplify_expr(e, 0))
7985 gfc_error ("NAME= specifier at %C should be a constant expression");
7986 gfc_free_expr (e);
7987 return MATCH_ERROR;
7990 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
7991 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
7993 gfc_error ("NAME= specifier at %C should be a scalar of "
7994 "default character kind");
7995 gfc_free_expr(e);
7996 return MATCH_ERROR;
7999 // Get a C string from the Fortran string constant
8000 binding_label = gfc_widechar_to_char (e->value.character.string,
8001 e->value.character.length);
8002 gfc_free_expr(e);
8004 // Check that it is valid (old gfc_match_name_C)
8005 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
8006 return MATCH_ERROR;
8009 /* Get the required right paren. */
8010 if (gfc_match_char (')') != MATCH_YES)
8012 gfc_error ("Missing closing paren for binding label at %C");
8013 return MATCH_ERROR;
8016 if (has_name_equals && !allow_binding_name)
8018 gfc_error ("No binding name is allowed in BIND(C) at %C");
8019 return MATCH_ERROR;
8022 if (has_name_equals && sym != NULL && sym->attr.dummy)
8024 gfc_error ("For dummy procedure %s, no binding name is "
8025 "allowed in BIND(C) at %C", sym->name);
8026 return MATCH_ERROR;
8030 /* Save the binding label to the symbol. If sym is null, we're
8031 probably matching the typespec attributes of a declaration and
8032 haven't gotten the name yet, and therefore, no symbol yet. */
8033 if (binding_label)
8035 if (sym != NULL)
8036 sym->binding_label = binding_label;
8037 else
8038 curr_binding_label = binding_label;
8040 else if (allow_binding_name)
8042 /* No binding label, but if symbol isn't null, we
8043 can set the label for it here.
8044 If name="" or allow_binding_name is false, no C binding name is
8045 created. */
8046 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
8047 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
8050 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
8051 && current_interface.type == INTERFACE_ABSTRACT)
8053 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
8054 return MATCH_ERROR;
8057 return MATCH_YES;
8061 /* Return nonzero if we're currently compiling a contained procedure. */
8063 static int
8064 contained_procedure (void)
8066 gfc_state_data *s = gfc_state_stack;
8068 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
8069 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
8070 return 1;
8072 return 0;
8075 /* Set the kind of each enumerator. The kind is selected such that it is
8076 interoperable with the corresponding C enumeration type, making
8077 sure that -fshort-enums is honored. */
8079 static void
8080 set_enum_kind(void)
8082 enumerator_history *current_history = NULL;
8083 int kind;
8084 int i;
8086 if (max_enum == NULL || enum_history == NULL)
8087 return;
8089 if (!flag_short_enums)
8090 return;
8092 i = 0;
8095 kind = gfc_integer_kinds[i++].kind;
8097 while (kind < gfc_c_int_kind
8098 && gfc_check_integer_range (max_enum->initializer->value.integer,
8099 kind) != ARITH_OK);
8101 current_history = enum_history;
8102 while (current_history != NULL)
8104 current_history->sym->ts.kind = kind;
8105 current_history = current_history->next;
8110 /* Match any of the various end-block statements. Returns the type of
8111 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
8112 and END BLOCK statements cannot be replaced by a single END statement. */
8114 match
8115 gfc_match_end (gfc_statement *st)
8117 char name[GFC_MAX_SYMBOL_LEN + 1];
8118 gfc_compile_state state;
8119 locus old_loc;
8120 const char *block_name;
8121 const char *target;
8122 int eos_ok;
8123 match m;
8124 gfc_namespace *parent_ns, *ns, *prev_ns;
8125 gfc_namespace **nsp;
8126 bool abreviated_modproc_decl = false;
8127 bool got_matching_end = false;
8129 old_loc = gfc_current_locus;
8130 if (gfc_match ("end") != MATCH_YES)
8131 return MATCH_NO;
8133 state = gfc_current_state ();
8134 block_name = gfc_current_block () == NULL
8135 ? NULL : gfc_current_block ()->name;
8137 switch (state)
8139 case COMP_ASSOCIATE:
8140 case COMP_BLOCK:
8141 if (gfc_str_startswith (block_name, "block@"))
8142 block_name = NULL;
8143 break;
8145 case COMP_CONTAINS:
8146 case COMP_DERIVED_CONTAINS:
8147 state = gfc_state_stack->previous->state;
8148 block_name = gfc_state_stack->previous->sym == NULL
8149 ? NULL : gfc_state_stack->previous->sym->name;
8150 abreviated_modproc_decl = gfc_state_stack->previous->sym
8151 && gfc_state_stack->previous->sym->abr_modproc_decl;
8152 break;
8154 default:
8155 break;
8158 if (!abreviated_modproc_decl)
8159 abreviated_modproc_decl = gfc_current_block ()
8160 && gfc_current_block ()->abr_modproc_decl;
8162 switch (state)
8164 case COMP_NONE:
8165 case COMP_PROGRAM:
8166 *st = ST_END_PROGRAM;
8167 target = " program";
8168 eos_ok = 1;
8169 break;
8171 case COMP_SUBROUTINE:
8172 *st = ST_END_SUBROUTINE;
8173 if (!abreviated_modproc_decl)
8174 target = " subroutine";
8175 else
8176 target = " procedure";
8177 eos_ok = !contained_procedure ();
8178 break;
8180 case COMP_FUNCTION:
8181 *st = ST_END_FUNCTION;
8182 if (!abreviated_modproc_decl)
8183 target = " function";
8184 else
8185 target = " procedure";
8186 eos_ok = !contained_procedure ();
8187 break;
8189 case COMP_BLOCK_DATA:
8190 *st = ST_END_BLOCK_DATA;
8191 target = " block data";
8192 eos_ok = 1;
8193 break;
8195 case COMP_MODULE:
8196 *st = ST_END_MODULE;
8197 target = " module";
8198 eos_ok = 1;
8199 break;
8201 case COMP_SUBMODULE:
8202 *st = ST_END_SUBMODULE;
8203 target = " submodule";
8204 eos_ok = 1;
8205 break;
8207 case COMP_INTERFACE:
8208 *st = ST_END_INTERFACE;
8209 target = " interface";
8210 eos_ok = 0;
8211 break;
8213 case COMP_MAP:
8214 *st = ST_END_MAP;
8215 target = " map";
8216 eos_ok = 0;
8217 break;
8219 case COMP_UNION:
8220 *st = ST_END_UNION;
8221 target = " union";
8222 eos_ok = 0;
8223 break;
8225 case COMP_STRUCTURE:
8226 *st = ST_END_STRUCTURE;
8227 target = " structure";
8228 eos_ok = 0;
8229 break;
8231 case COMP_DERIVED:
8232 case COMP_DERIVED_CONTAINS:
8233 *st = ST_END_TYPE;
8234 target = " type";
8235 eos_ok = 0;
8236 break;
8238 case COMP_ASSOCIATE:
8239 *st = ST_END_ASSOCIATE;
8240 target = " associate";
8241 eos_ok = 0;
8242 break;
8244 case COMP_BLOCK:
8245 *st = ST_END_BLOCK;
8246 target = " block";
8247 eos_ok = 0;
8248 break;
8250 case COMP_IF:
8251 *st = ST_ENDIF;
8252 target = " if";
8253 eos_ok = 0;
8254 break;
8256 case COMP_DO:
8257 case COMP_DO_CONCURRENT:
8258 *st = ST_ENDDO;
8259 target = " do";
8260 eos_ok = 0;
8261 break;
8263 case COMP_CRITICAL:
8264 *st = ST_END_CRITICAL;
8265 target = " critical";
8266 eos_ok = 0;
8267 break;
8269 case COMP_SELECT:
8270 case COMP_SELECT_TYPE:
8271 case COMP_SELECT_RANK:
8272 *st = ST_END_SELECT;
8273 target = " select";
8274 eos_ok = 0;
8275 break;
8277 case COMP_FORALL:
8278 *st = ST_END_FORALL;
8279 target = " forall";
8280 eos_ok = 0;
8281 break;
8283 case COMP_WHERE:
8284 *st = ST_END_WHERE;
8285 target = " where";
8286 eos_ok = 0;
8287 break;
8289 case COMP_ENUM:
8290 *st = ST_END_ENUM;
8291 target = " enum";
8292 eos_ok = 0;
8293 last_initializer = NULL;
8294 set_enum_kind ();
8295 gfc_free_enum_history ();
8296 break;
8298 default:
8299 gfc_error ("Unexpected END statement at %C");
8300 goto cleanup;
8303 old_loc = gfc_current_locus;
8304 if (gfc_match_eos () == MATCH_YES)
8306 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
8308 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
8309 "instead of %s statement at %L",
8310 abreviated_modproc_decl ? "END PROCEDURE"
8311 : gfc_ascii_statement(*st), &old_loc))
8312 goto cleanup;
8314 else if (!eos_ok)
8316 /* We would have required END [something]. */
8317 gfc_error ("%s statement expected at %L",
8318 gfc_ascii_statement (*st), &old_loc);
8319 goto cleanup;
8322 return MATCH_YES;
8325 /* Verify that we've got the sort of end-block that we're expecting. */
8326 if (gfc_match (target) != MATCH_YES)
8328 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
8329 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
8330 goto cleanup;
8332 else
8333 got_matching_end = true;
8335 old_loc = gfc_current_locus;
8336 /* If we're at the end, make sure a block name wasn't required. */
8337 if (gfc_match_eos () == MATCH_YES)
8340 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
8341 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
8342 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
8343 return MATCH_YES;
8345 if (!block_name)
8346 return MATCH_YES;
8348 gfc_error ("Expected block name of %qs in %s statement at %L",
8349 block_name, gfc_ascii_statement (*st), &old_loc);
8351 return MATCH_ERROR;
8354 /* END INTERFACE has a special handler for its several possible endings. */
8355 if (*st == ST_END_INTERFACE)
8356 return gfc_match_end_interface ();
8358 /* We haven't hit the end of statement, so what is left must be an
8359 end-name. */
8360 m = gfc_match_space ();
8361 if (m == MATCH_YES)
8362 m = gfc_match_name (name);
8364 if (m == MATCH_NO)
8365 gfc_error ("Expected terminating name at %C");
8366 if (m != MATCH_YES)
8367 goto cleanup;
8369 if (block_name == NULL)
8370 goto syntax;
8372 /* We have to pick out the declared submodule name from the composite
8373 required by F2008:11.2.3 para 2, which ends in the declared name. */
8374 if (state == COMP_SUBMODULE)
8375 block_name = strchr (block_name, '.') + 1;
8377 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
8379 gfc_error ("Expected label %qs for %s statement at %C", block_name,
8380 gfc_ascii_statement (*st));
8381 goto cleanup;
8383 /* Procedure pointer as function result. */
8384 else if (strcmp (block_name, "ppr@") == 0
8385 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8387 gfc_error ("Expected label %qs for %s statement at %C",
8388 gfc_current_block ()->ns->proc_name->name,
8389 gfc_ascii_statement (*st));
8390 goto cleanup;
8393 if (gfc_match_eos () == MATCH_YES)
8394 return MATCH_YES;
8396 syntax:
8397 gfc_syntax_error (*st);
8399 cleanup:
8400 gfc_current_locus = old_loc;
8402 /* If we are missing an END BLOCK, we created a half-ready namespace.
8403 Remove it from the parent namespace's sibling list. */
8405 while (state == COMP_BLOCK && !got_matching_end)
8407 parent_ns = gfc_current_ns->parent;
8409 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8411 prev_ns = NULL;
8412 ns = *nsp;
8413 while (ns)
8415 if (ns == gfc_current_ns)
8417 if (prev_ns == NULL)
8418 *nsp = NULL;
8419 else
8420 prev_ns->sibling = ns->sibling;
8422 prev_ns = ns;
8423 ns = ns->sibling;
8426 gfc_free_namespace (gfc_current_ns);
8427 gfc_current_ns = parent_ns;
8428 gfc_state_stack = gfc_state_stack->previous;
8429 state = gfc_current_state ();
8432 return MATCH_ERROR;
8437 /***************** Attribute declaration statements ****************/
8439 /* Set the attribute of a single variable. */
8441 static match
8442 attr_decl1 (void)
8444 char name[GFC_MAX_SYMBOL_LEN + 1];
8445 gfc_array_spec *as;
8447 /* Workaround -Wmaybe-uninitialized false positive during
8448 profiledbootstrap by initializing them. */
8449 gfc_symbol *sym = NULL;
8450 locus var_locus;
8451 match m;
8453 as = NULL;
8455 m = gfc_match_name (name);
8456 if (m != MATCH_YES)
8457 goto cleanup;
8459 if (find_special (name, &sym, false))
8460 return MATCH_ERROR;
8462 if (!check_function_name (name))
8464 m = MATCH_ERROR;
8465 goto cleanup;
8468 var_locus = gfc_current_locus;
8470 /* Deal with possible array specification for certain attributes. */
8471 if (current_attr.dimension
8472 || current_attr.codimension
8473 || current_attr.allocatable
8474 || current_attr.pointer
8475 || current_attr.target)
8477 m = gfc_match_array_spec (&as, !current_attr.codimension,
8478 !current_attr.dimension
8479 && !current_attr.pointer
8480 && !current_attr.target);
8481 if (m == MATCH_ERROR)
8482 goto cleanup;
8484 if (current_attr.dimension && m == MATCH_NO)
8486 gfc_error ("Missing array specification at %L in DIMENSION "
8487 "statement", &var_locus);
8488 m = MATCH_ERROR;
8489 goto cleanup;
8492 if (current_attr.dimension && sym->value)
8494 gfc_error ("Dimensions specified for %s at %L after its "
8495 "initialization", sym->name, &var_locus);
8496 m = MATCH_ERROR;
8497 goto cleanup;
8500 if (current_attr.codimension && m == MATCH_NO)
8502 gfc_error ("Missing array specification at %L in CODIMENSION "
8503 "statement", &var_locus);
8504 m = MATCH_ERROR;
8505 goto cleanup;
8508 if ((current_attr.allocatable || current_attr.pointer)
8509 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8511 gfc_error ("Array specification must be deferred at %L", &var_locus);
8512 m = MATCH_ERROR;
8513 goto cleanup;
8517 /* Update symbol table. DIMENSION attribute is set in
8518 gfc_set_array_spec(). For CLASS variables, this must be applied
8519 to the first component, or '_data' field. */
8520 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
8522 /* gfc_set_array_spec sets sym->attr not CLASS_DATA(sym)->attr. Check
8523 for duplicate attribute here. */
8524 if (CLASS_DATA(sym)->attr.dimension == 1 && as)
8526 gfc_error ("Duplicate DIMENSION attribute at %C");
8527 m = MATCH_ERROR;
8528 goto cleanup;
8531 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
8533 m = MATCH_ERROR;
8534 goto cleanup;
8537 else
8539 if (current_attr.dimension == 0 && current_attr.codimension == 0
8540 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
8542 m = MATCH_ERROR;
8543 goto cleanup;
8547 if (sym->ts.type == BT_CLASS
8548 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
8550 m = MATCH_ERROR;
8551 goto cleanup;
8554 if (!gfc_set_array_spec (sym, as, &var_locus))
8556 m = MATCH_ERROR;
8557 goto cleanup;
8560 if (sym->attr.cray_pointee && sym->as != NULL)
8562 /* Fix the array spec. */
8563 m = gfc_mod_pointee_as (sym->as);
8564 if (m == MATCH_ERROR)
8565 goto cleanup;
8568 if (!gfc_add_attribute (&sym->attr, &var_locus))
8570 m = MATCH_ERROR;
8571 goto cleanup;
8574 if ((current_attr.external || current_attr.intrinsic)
8575 && sym->attr.flavor != FL_PROCEDURE
8576 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8578 m = MATCH_ERROR;
8579 goto cleanup;
8582 add_hidden_procptr_result (sym);
8584 return MATCH_YES;
8586 cleanup:
8587 gfc_free_array_spec (as);
8588 return m;
8592 /* Generic attribute declaration subroutine. Used for attributes that
8593 just have a list of names. */
8595 static match
8596 attr_decl (void)
8598 match m;
8600 /* Gobble the optional double colon, by simply ignoring the result
8601 of gfc_match(). */
8602 gfc_match (" ::");
8604 for (;;)
8606 m = attr_decl1 ();
8607 if (m != MATCH_YES)
8608 break;
8610 if (gfc_match_eos () == MATCH_YES)
8612 m = MATCH_YES;
8613 break;
8616 if (gfc_match_char (',') != MATCH_YES)
8618 gfc_error ("Unexpected character in variable list at %C");
8619 m = MATCH_ERROR;
8620 break;
8624 return m;
8628 /* This routine matches Cray Pointer declarations of the form:
8629 pointer ( <pointer>, <pointee> )
8631 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8632 The pointer, if already declared, should be an integer. Otherwise, we
8633 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8634 be either a scalar, or an array declaration. No space is allocated for
8635 the pointee. For the statement
8636 pointer (ipt, ar(10))
8637 any subsequent uses of ar will be translated (in C-notation) as
8638 ar(i) => ((<type> *) ipt)(i)
8639 After gimplification, pointee variable will disappear in the code. */
8641 static match
8642 cray_pointer_decl (void)
8644 match m;
8645 gfc_array_spec *as = NULL;
8646 gfc_symbol *cptr; /* Pointer symbol. */
8647 gfc_symbol *cpte; /* Pointee symbol. */
8648 locus var_locus;
8649 bool done = false;
8651 while (!done)
8653 if (gfc_match_char ('(') != MATCH_YES)
8655 gfc_error ("Expected %<(%> at %C");
8656 return MATCH_ERROR;
8659 /* Match pointer. */
8660 var_locus = gfc_current_locus;
8661 gfc_clear_attr (&current_attr);
8662 gfc_add_cray_pointer (&current_attr, &var_locus);
8663 current_ts.type = BT_INTEGER;
8664 current_ts.kind = gfc_index_integer_kind;
8666 m = gfc_match_symbol (&cptr, 0);
8667 if (m != MATCH_YES)
8669 gfc_error ("Expected variable name at %C");
8670 return m;
8673 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8674 return MATCH_ERROR;
8676 gfc_set_sym_referenced (cptr);
8678 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8680 cptr->ts.type = BT_INTEGER;
8681 cptr->ts.kind = gfc_index_integer_kind;
8683 else if (cptr->ts.type != BT_INTEGER)
8685 gfc_error ("Cray pointer at %C must be an integer");
8686 return MATCH_ERROR;
8688 else if (cptr->ts.kind < gfc_index_integer_kind)
8689 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8690 " memory addresses require %d bytes",
8691 cptr->ts.kind, gfc_index_integer_kind);
8693 if (gfc_match_char (',') != MATCH_YES)
8695 gfc_error ("Expected \",\" at %C");
8696 return MATCH_ERROR;
8699 /* Match Pointee. */
8700 var_locus = gfc_current_locus;
8701 gfc_clear_attr (&current_attr);
8702 gfc_add_cray_pointee (&current_attr, &var_locus);
8703 current_ts.type = BT_UNKNOWN;
8704 current_ts.kind = 0;
8706 m = gfc_match_symbol (&cpte, 0);
8707 if (m != MATCH_YES)
8709 gfc_error ("Expected variable name at %C");
8710 return m;
8713 /* Check for an optional array spec. */
8714 m = gfc_match_array_spec (&as, true, false);
8715 if (m == MATCH_ERROR)
8717 gfc_free_array_spec (as);
8718 return m;
8720 else if (m == MATCH_NO)
8722 gfc_free_array_spec (as);
8723 as = NULL;
8726 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8727 return MATCH_ERROR;
8729 gfc_set_sym_referenced (cpte);
8731 if (cpte->as == NULL)
8733 if (!gfc_set_array_spec (cpte, as, &var_locus))
8734 gfc_internal_error ("Cannot set Cray pointee array spec.");
8736 else if (as != NULL)
8738 gfc_error ("Duplicate array spec for Cray pointee at %C");
8739 gfc_free_array_spec (as);
8740 return MATCH_ERROR;
8743 as = NULL;
8745 if (cpte->as != NULL)
8747 /* Fix array spec. */
8748 m = gfc_mod_pointee_as (cpte->as);
8749 if (m == MATCH_ERROR)
8750 return m;
8753 /* Point the Pointee at the Pointer. */
8754 cpte->cp_pointer = cptr;
8756 if (gfc_match_char (')') != MATCH_YES)
8758 gfc_error ("Expected \")\" at %C");
8759 return MATCH_ERROR;
8761 m = gfc_match_char (',');
8762 if (m != MATCH_YES)
8763 done = true; /* Stop searching for more declarations. */
8767 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8768 || gfc_match_eos () != MATCH_YES)
8770 gfc_error ("Expected %<,%> or end of statement at %C");
8771 return MATCH_ERROR;
8773 return MATCH_YES;
8777 match
8778 gfc_match_external (void)
8781 gfc_clear_attr (&current_attr);
8782 current_attr.external = 1;
8784 return attr_decl ();
8788 match
8789 gfc_match_intent (void)
8791 sym_intent intent;
8793 /* This is not allowed within a BLOCK construct! */
8794 if (gfc_current_state () == COMP_BLOCK)
8796 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8797 return MATCH_ERROR;
8800 intent = match_intent_spec ();
8801 if (intent == INTENT_UNKNOWN)
8802 return MATCH_ERROR;
8804 gfc_clear_attr (&current_attr);
8805 current_attr.intent = intent;
8807 return attr_decl ();
8811 match
8812 gfc_match_intrinsic (void)
8815 gfc_clear_attr (&current_attr);
8816 current_attr.intrinsic = 1;
8818 return attr_decl ();
8822 match
8823 gfc_match_optional (void)
8825 /* This is not allowed within a BLOCK construct! */
8826 if (gfc_current_state () == COMP_BLOCK)
8828 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8829 return MATCH_ERROR;
8832 gfc_clear_attr (&current_attr);
8833 current_attr.optional = 1;
8835 return attr_decl ();
8839 match
8840 gfc_match_pointer (void)
8842 gfc_gobble_whitespace ();
8843 if (gfc_peek_ascii_char () == '(')
8845 if (!flag_cray_pointer)
8847 gfc_error ("Cray pointer declaration at %C requires "
8848 "%<-fcray-pointer%> flag");
8849 return MATCH_ERROR;
8851 return cray_pointer_decl ();
8853 else
8855 gfc_clear_attr (&current_attr);
8856 current_attr.pointer = 1;
8858 return attr_decl ();
8863 match
8864 gfc_match_allocatable (void)
8866 gfc_clear_attr (&current_attr);
8867 current_attr.allocatable = 1;
8869 return attr_decl ();
8873 match
8874 gfc_match_codimension (void)
8876 gfc_clear_attr (&current_attr);
8877 current_attr.codimension = 1;
8879 return attr_decl ();
8883 match
8884 gfc_match_contiguous (void)
8886 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
8887 return MATCH_ERROR;
8889 gfc_clear_attr (&current_attr);
8890 current_attr.contiguous = 1;
8892 return attr_decl ();
8896 match
8897 gfc_match_dimension (void)
8899 gfc_clear_attr (&current_attr);
8900 current_attr.dimension = 1;
8902 return attr_decl ();
8906 match
8907 gfc_match_target (void)
8909 gfc_clear_attr (&current_attr);
8910 current_attr.target = 1;
8912 return attr_decl ();
8916 /* Match the list of entities being specified in a PUBLIC or PRIVATE
8917 statement. */
8919 static match
8920 access_attr_decl (gfc_statement st)
8922 char name[GFC_MAX_SYMBOL_LEN + 1];
8923 interface_type type;
8924 gfc_user_op *uop;
8925 gfc_symbol *sym, *dt_sym;
8926 gfc_intrinsic_op op;
8927 match m;
8928 gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8930 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8931 goto done;
8933 for (;;)
8935 m = gfc_match_generic_spec (&type, name, &op);
8936 if (m == MATCH_NO)
8937 goto syntax;
8938 if (m == MATCH_ERROR)
8939 goto done;
8941 switch (type)
8943 case INTERFACE_NAMELESS:
8944 case INTERFACE_ABSTRACT:
8945 goto syntax;
8947 case INTERFACE_GENERIC:
8948 case INTERFACE_DTIO:
8950 if (gfc_get_symbol (name, NULL, &sym))
8951 goto done;
8953 if (type == INTERFACE_DTIO
8954 && gfc_current_ns->proc_name
8955 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
8956 && sym->attr.flavor == FL_UNKNOWN)
8957 sym->attr.flavor = FL_PROCEDURE;
8959 if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
8960 goto done;
8962 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
8963 && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
8964 goto done;
8966 break;
8968 case INTERFACE_INTRINSIC_OP:
8969 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
8971 gfc_intrinsic_op other_op;
8973 gfc_current_ns->operator_access[op] = access;
8975 /* Handle the case if there is another op with the same
8976 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
8977 other_op = gfc_equivalent_op (op);
8979 if (other_op != INTRINSIC_NONE)
8980 gfc_current_ns->operator_access[other_op] = access;
8982 else
8984 gfc_error ("Access specification of the %s operator at %C has "
8985 "already been specified", gfc_op2string (op));
8986 goto done;
8989 break;
8991 case INTERFACE_USER_OP:
8992 uop = gfc_get_uop (name);
8994 if (uop->access == ACCESS_UNKNOWN)
8996 uop->access = access;
8998 else
9000 gfc_error ("Access specification of the .%s. operator at %C "
9001 "has already been specified", sym->name);
9002 goto done;
9005 break;
9008 if (gfc_match_char (',') == MATCH_NO)
9009 break;
9012 if (gfc_match_eos () != MATCH_YES)
9013 goto syntax;
9014 return MATCH_YES;
9016 syntax:
9017 gfc_syntax_error (st);
9019 done:
9020 return MATCH_ERROR;
9024 match
9025 gfc_match_protected (void)
9027 gfc_symbol *sym;
9028 match m;
9029 char c;
9031 /* PROTECTED has already been seen, but must be followed by whitespace
9032 or ::. */
9033 c = gfc_peek_ascii_char ();
9034 if (!gfc_is_whitespace (c) && c != ':')
9035 return MATCH_NO;
9037 if (!gfc_current_ns->proc_name
9038 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
9040 gfc_error ("PROTECTED at %C only allowed in specification "
9041 "part of a module");
9042 return MATCH_ERROR;
9046 gfc_match (" ::");
9048 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
9049 return MATCH_ERROR;
9051 /* PROTECTED has an entity-list. */
9052 if (gfc_match_eos () == MATCH_YES)
9053 goto syntax;
9055 for(;;)
9057 m = gfc_match_symbol (&sym, 0);
9058 switch (m)
9060 case MATCH_YES:
9061 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
9062 return MATCH_ERROR;
9063 goto next_item;
9065 case MATCH_NO:
9066 break;
9068 case MATCH_ERROR:
9069 return MATCH_ERROR;
9072 next_item:
9073 if (gfc_match_eos () == MATCH_YES)
9074 break;
9075 if (gfc_match_char (',') != MATCH_YES)
9076 goto syntax;
9079 return MATCH_YES;
9081 syntax:
9082 gfc_error ("Syntax error in PROTECTED statement at %C");
9083 return MATCH_ERROR;
9087 /* The PRIVATE statement is a bit weird in that it can be an attribute
9088 declaration, but also works as a standalone statement inside of a
9089 type declaration or a module. */
9091 match
9092 gfc_match_private (gfc_statement *st)
9094 gfc_state_data *prev;
9096 if (gfc_match ("private") != MATCH_YES)
9097 return MATCH_NO;
9099 /* Try matching PRIVATE without an access-list. */
9100 if (gfc_match_eos () == MATCH_YES)
9102 prev = gfc_state_stack->previous;
9103 if (gfc_current_state () != COMP_MODULE
9104 && !(gfc_current_state () == COMP_DERIVED
9105 && prev && prev->state == COMP_MODULE)
9106 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9107 && prev->previous && prev->previous->state == COMP_MODULE))
9109 gfc_error ("PRIVATE statement at %C is only allowed in the "
9110 "specification part of a module");
9111 return MATCH_ERROR;
9114 *st = ST_PRIVATE;
9115 return MATCH_YES;
9118 /* At this point in free-form source code, PRIVATE must be followed
9119 by whitespace or ::. */
9120 if (gfc_current_form == FORM_FREE)
9122 char c = gfc_peek_ascii_char ();
9123 if (!gfc_is_whitespace (c) && c != ':')
9124 return MATCH_NO;
9127 prev = gfc_state_stack->previous;
9128 if (gfc_current_state () != COMP_MODULE
9129 && !(gfc_current_state () == COMP_DERIVED
9130 && prev && prev->state == COMP_MODULE)
9131 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9132 && prev->previous && prev->previous->state == COMP_MODULE))
9134 gfc_error ("PRIVATE statement at %C is only allowed in the "
9135 "specification part of a module");
9136 return MATCH_ERROR;
9139 *st = ST_ATTR_DECL;
9140 return access_attr_decl (ST_PRIVATE);
9144 match
9145 gfc_match_public (gfc_statement *st)
9147 if (gfc_match ("public") != MATCH_YES)
9148 return MATCH_NO;
9150 /* Try matching PUBLIC without an access-list. */
9151 if (gfc_match_eos () == MATCH_YES)
9153 if (gfc_current_state () != COMP_MODULE)
9155 gfc_error ("PUBLIC statement at %C is only allowed in the "
9156 "specification part of a module");
9157 return MATCH_ERROR;
9160 *st = ST_PUBLIC;
9161 return MATCH_YES;
9164 /* At this point in free-form source code, PUBLIC must be followed
9165 by whitespace or ::. */
9166 if (gfc_current_form == FORM_FREE)
9168 char c = gfc_peek_ascii_char ();
9169 if (!gfc_is_whitespace (c) && c != ':')
9170 return MATCH_NO;
9173 if (gfc_current_state () != COMP_MODULE)
9175 gfc_error ("PUBLIC statement at %C is only allowed in the "
9176 "specification part of a module");
9177 return MATCH_ERROR;
9180 *st = ST_ATTR_DECL;
9181 return access_attr_decl (ST_PUBLIC);
9185 /* Workhorse for gfc_match_parameter. */
9187 static match
9188 do_parm (void)
9190 gfc_symbol *sym;
9191 gfc_expr *init;
9192 match m;
9193 bool t;
9195 m = gfc_match_symbol (&sym, 0);
9196 if (m == MATCH_NO)
9197 gfc_error ("Expected variable name at %C in PARAMETER statement");
9199 if (m != MATCH_YES)
9200 return m;
9202 if (gfc_match_char ('=') == MATCH_NO)
9204 gfc_error ("Expected = sign in PARAMETER statement at %C");
9205 return MATCH_ERROR;
9208 m = gfc_match_init_expr (&init);
9209 if (m == MATCH_NO)
9210 gfc_error ("Expected expression at %C in PARAMETER statement");
9211 if (m != MATCH_YES)
9212 return m;
9214 if (sym->ts.type == BT_UNKNOWN
9215 && !gfc_set_default_type (sym, 1, NULL))
9217 m = MATCH_ERROR;
9218 goto cleanup;
9221 if (!gfc_check_assign_symbol (sym, NULL, init)
9222 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
9224 m = MATCH_ERROR;
9225 goto cleanup;
9228 if (sym->value)
9230 gfc_error ("Initializing already initialized variable at %C");
9231 m = MATCH_ERROR;
9232 goto cleanup;
9235 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
9236 return (t) ? MATCH_YES : MATCH_ERROR;
9238 cleanup:
9239 gfc_free_expr (init);
9240 return m;
9244 /* Match a parameter statement, with the weird syntax that these have. */
9246 match
9247 gfc_match_parameter (void)
9249 const char *term = " )%t";
9250 match m;
9252 if (gfc_match_char ('(') == MATCH_NO)
9254 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
9255 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
9256 return MATCH_NO;
9257 term = " %t";
9260 for (;;)
9262 m = do_parm ();
9263 if (m != MATCH_YES)
9264 break;
9266 if (gfc_match (term) == MATCH_YES)
9267 break;
9269 if (gfc_match_char (',') != MATCH_YES)
9271 gfc_error ("Unexpected characters in PARAMETER statement at %C");
9272 m = MATCH_ERROR;
9273 break;
9277 return m;
9281 match
9282 gfc_match_automatic (void)
9284 gfc_symbol *sym;
9285 match m;
9286 bool seen_symbol = false;
9288 if (!flag_dec_static)
9290 gfc_error ("%s at %C is a DEC extension, enable with "
9291 "%<-fdec-static%>",
9292 "AUTOMATIC"
9294 return MATCH_ERROR;
9297 gfc_match (" ::");
9299 for (;;)
9301 m = gfc_match_symbol (&sym, 0);
9302 switch (m)
9304 case MATCH_NO:
9305 break;
9307 case MATCH_ERROR:
9308 return MATCH_ERROR;
9310 case MATCH_YES:
9311 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
9312 return MATCH_ERROR;
9313 seen_symbol = true;
9314 break;
9317 if (gfc_match_eos () == MATCH_YES)
9318 break;
9319 if (gfc_match_char (',') != MATCH_YES)
9320 goto syntax;
9323 if (!seen_symbol)
9325 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
9326 return MATCH_ERROR;
9329 return MATCH_YES;
9331 syntax:
9332 gfc_error ("Syntax error in AUTOMATIC statement at %C");
9333 return MATCH_ERROR;
9337 match
9338 gfc_match_static (void)
9340 gfc_symbol *sym;
9341 match m;
9342 bool seen_symbol = false;
9344 if (!flag_dec_static)
9346 gfc_error ("%s at %C is a DEC extension, enable with "
9347 "%<-fdec-static%>",
9348 "STATIC");
9349 return MATCH_ERROR;
9352 gfc_match (" ::");
9354 for (;;)
9356 m = gfc_match_symbol (&sym, 0);
9357 switch (m)
9359 case MATCH_NO:
9360 break;
9362 case MATCH_ERROR:
9363 return MATCH_ERROR;
9365 case MATCH_YES:
9366 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9367 &gfc_current_locus))
9368 return MATCH_ERROR;
9369 seen_symbol = true;
9370 break;
9373 if (gfc_match_eos () == MATCH_YES)
9374 break;
9375 if (gfc_match_char (',') != MATCH_YES)
9376 goto syntax;
9379 if (!seen_symbol)
9381 gfc_error ("Expected entity-list in STATIC statement at %C");
9382 return MATCH_ERROR;
9385 return MATCH_YES;
9387 syntax:
9388 gfc_error ("Syntax error in STATIC statement at %C");
9389 return MATCH_ERROR;
9393 /* Save statements have a special syntax. */
9395 match
9396 gfc_match_save (void)
9398 char n[GFC_MAX_SYMBOL_LEN+1];
9399 gfc_common_head *c;
9400 gfc_symbol *sym;
9401 match m;
9403 if (gfc_match_eos () == MATCH_YES)
9405 if (gfc_current_ns->seen_save)
9407 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9408 "follows previous SAVE statement"))
9409 return MATCH_ERROR;
9412 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9413 return MATCH_YES;
9416 if (gfc_current_ns->save_all)
9418 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9419 "blanket SAVE statement"))
9420 return MATCH_ERROR;
9423 gfc_match (" ::");
9425 for (;;)
9427 m = gfc_match_symbol (&sym, 0);
9428 switch (m)
9430 case MATCH_YES:
9431 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9432 &gfc_current_locus))
9433 return MATCH_ERROR;
9434 goto next_item;
9436 case MATCH_NO:
9437 break;
9439 case MATCH_ERROR:
9440 return MATCH_ERROR;
9443 m = gfc_match (" / %n /", &n);
9444 if (m == MATCH_ERROR)
9445 return MATCH_ERROR;
9446 if (m == MATCH_NO)
9447 goto syntax;
9449 c = gfc_get_common (n, 0);
9450 c->saved = 1;
9452 gfc_current_ns->seen_save = 1;
9454 next_item:
9455 if (gfc_match_eos () == MATCH_YES)
9456 break;
9457 if (gfc_match_char (',') != MATCH_YES)
9458 goto syntax;
9461 return MATCH_YES;
9463 syntax:
9464 if (gfc_current_ns->seen_save)
9466 gfc_error ("Syntax error in SAVE statement at %C");
9467 return MATCH_ERROR;
9469 else
9470 return MATCH_NO;
9474 match
9475 gfc_match_value (void)
9477 gfc_symbol *sym;
9478 match m;
9480 /* This is not allowed within a BLOCK construct! */
9481 if (gfc_current_state () == COMP_BLOCK)
9483 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9484 return MATCH_ERROR;
9487 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
9488 return MATCH_ERROR;
9490 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9492 return MATCH_ERROR;
9495 if (gfc_match_eos () == MATCH_YES)
9496 goto syntax;
9498 for(;;)
9500 m = gfc_match_symbol (&sym, 0);
9501 switch (m)
9503 case MATCH_YES:
9504 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
9505 return MATCH_ERROR;
9506 goto next_item;
9508 case MATCH_NO:
9509 break;
9511 case MATCH_ERROR:
9512 return MATCH_ERROR;
9515 next_item:
9516 if (gfc_match_eos () == MATCH_YES)
9517 break;
9518 if (gfc_match_char (',') != MATCH_YES)
9519 goto syntax;
9522 return MATCH_YES;
9524 syntax:
9525 gfc_error ("Syntax error in VALUE statement at %C");
9526 return MATCH_ERROR;
9530 match
9531 gfc_match_volatile (void)
9533 gfc_symbol *sym;
9534 char *name;
9535 match m;
9537 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
9538 return MATCH_ERROR;
9540 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9542 return MATCH_ERROR;
9545 if (gfc_match_eos () == MATCH_YES)
9546 goto syntax;
9548 for(;;)
9550 /* VOLATILE is special because it can be added to host-associated
9551 symbols locally. Except for coarrays. */
9552 m = gfc_match_symbol (&sym, 1);
9553 switch (m)
9555 case MATCH_YES:
9556 name = XCNEWVAR (char, strlen (sym->name) + 1);
9557 strcpy (name, sym->name);
9558 if (!check_function_name (name))
9559 return MATCH_ERROR;
9560 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9561 for variable in a BLOCK which is defined outside of the BLOCK. */
9562 if (sym->ns != gfc_current_ns && sym->attr.codimension)
9564 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
9565 "%C, which is use-/host-associated", sym->name);
9566 return MATCH_ERROR;
9568 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
9569 return MATCH_ERROR;
9570 goto next_item;
9572 case MATCH_NO:
9573 break;
9575 case MATCH_ERROR:
9576 return MATCH_ERROR;
9579 next_item:
9580 if (gfc_match_eos () == MATCH_YES)
9581 break;
9582 if (gfc_match_char (',') != MATCH_YES)
9583 goto syntax;
9586 return MATCH_YES;
9588 syntax:
9589 gfc_error ("Syntax error in VOLATILE statement at %C");
9590 return MATCH_ERROR;
9594 match
9595 gfc_match_asynchronous (void)
9597 gfc_symbol *sym;
9598 char *name;
9599 match m;
9601 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
9602 return MATCH_ERROR;
9604 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9606 return MATCH_ERROR;
9609 if (gfc_match_eos () == MATCH_YES)
9610 goto syntax;
9612 for(;;)
9614 /* ASYNCHRONOUS is special because it can be added to host-associated
9615 symbols locally. */
9616 m = gfc_match_symbol (&sym, 1);
9617 switch (m)
9619 case MATCH_YES:
9620 name = XCNEWVAR (char, strlen (sym->name) + 1);
9621 strcpy (name, sym->name);
9622 if (!check_function_name (name))
9623 return MATCH_ERROR;
9624 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9625 return MATCH_ERROR;
9626 goto next_item;
9628 case MATCH_NO:
9629 break;
9631 case MATCH_ERROR:
9632 return MATCH_ERROR;
9635 next_item:
9636 if (gfc_match_eos () == MATCH_YES)
9637 break;
9638 if (gfc_match_char (',') != MATCH_YES)
9639 goto syntax;
9642 return MATCH_YES;
9644 syntax:
9645 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9646 return MATCH_ERROR;
9650 /* Match a module procedure statement in a submodule. */
9652 match
9653 gfc_match_submod_proc (void)
9655 char name[GFC_MAX_SYMBOL_LEN + 1];
9656 gfc_symbol *sym, *fsym;
9657 match m;
9658 gfc_formal_arglist *formal, *head, *tail;
9660 if (gfc_current_state () != COMP_CONTAINS
9661 || !(gfc_state_stack->previous
9662 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9663 || gfc_state_stack->previous->state == COMP_MODULE)))
9664 return MATCH_NO;
9666 m = gfc_match (" module% procedure% %n", name);
9667 if (m != MATCH_YES)
9668 return m;
9670 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9671 "at %C"))
9672 return MATCH_ERROR;
9674 if (get_proc_name (name, &sym, false))
9675 return MATCH_ERROR;
9677 /* Make sure that the result field is appropriately filled, even though
9678 the result symbol will be replaced later on. */
9679 if (sym->tlink && sym->tlink->attr.function)
9681 if (sym->tlink->result
9682 && sym->tlink->result != sym->tlink)
9683 sym->result= sym->tlink->result;
9684 else
9685 sym->result = sym;
9688 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9689 the symbol existed before. */
9690 sym->declared_at = gfc_current_locus;
9692 if (!sym->attr.module_procedure)
9693 return MATCH_ERROR;
9695 /* Signal match_end to expect "end procedure". */
9696 sym->abr_modproc_decl = 1;
9698 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9699 sym->attr.if_source = IFSRC_DECL;
9701 gfc_new_block = sym;
9703 /* Make a new formal arglist with the symbols in the procedure
9704 namespace. */
9705 head = tail = NULL;
9706 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9708 if (formal == sym->formal)
9709 head = tail = gfc_get_formal_arglist ();
9710 else
9712 tail->next = gfc_get_formal_arglist ();
9713 tail = tail->next;
9716 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9717 goto cleanup;
9719 tail->sym = fsym;
9720 gfc_set_sym_referenced (fsym);
9723 /* The dummy symbols get cleaned up, when the formal_namespace of the
9724 interface declaration is cleared. This allows us to add the
9725 explicit interface as is done for other type of procedure. */
9726 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9727 &gfc_current_locus))
9728 return MATCH_ERROR;
9730 if (gfc_match_eos () != MATCH_YES)
9732 gfc_syntax_error (ST_MODULE_PROC);
9733 return MATCH_ERROR;
9736 return MATCH_YES;
9738 cleanup:
9739 gfc_free_formal_arglist (head);
9740 return MATCH_ERROR;
9744 /* Match a module procedure statement. Note that we have to modify
9745 symbols in the parent's namespace because the current one was there
9746 to receive symbols that are in an interface's formal argument list. */
9748 match
9749 gfc_match_modproc (void)
9751 char name[GFC_MAX_SYMBOL_LEN + 1];
9752 gfc_symbol *sym;
9753 match m;
9754 locus old_locus;
9755 gfc_namespace *module_ns;
9756 gfc_interface *old_interface_head, *interface;
9758 if (gfc_state_stack->state != COMP_INTERFACE
9759 || gfc_state_stack->previous == NULL
9760 || current_interface.type == INTERFACE_NAMELESS
9761 || current_interface.type == INTERFACE_ABSTRACT)
9763 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9764 "interface");
9765 return MATCH_ERROR;
9768 module_ns = gfc_current_ns->parent;
9769 for (; module_ns; module_ns = module_ns->parent)
9770 if (module_ns->proc_name->attr.flavor == FL_MODULE
9771 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9772 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9773 && !module_ns->proc_name->attr.contained))
9774 break;
9776 if (module_ns == NULL)
9777 return MATCH_ERROR;
9779 /* Store the current state of the interface. We will need it if we
9780 end up with a syntax error and need to recover. */
9781 old_interface_head = gfc_current_interface_head ();
9783 /* Check if the F2008 optional double colon appears. */
9784 gfc_gobble_whitespace ();
9785 old_locus = gfc_current_locus;
9786 if (gfc_match ("::") == MATCH_YES)
9788 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9789 "MODULE PROCEDURE statement at %L", &old_locus))
9790 return MATCH_ERROR;
9792 else
9793 gfc_current_locus = old_locus;
9795 for (;;)
9797 bool last = false;
9798 old_locus = gfc_current_locus;
9800 m = gfc_match_name (name);
9801 if (m == MATCH_NO)
9802 goto syntax;
9803 if (m != MATCH_YES)
9804 return MATCH_ERROR;
9806 /* Check for syntax error before starting to add symbols to the
9807 current namespace. */
9808 if (gfc_match_eos () == MATCH_YES)
9809 last = true;
9811 if (!last && gfc_match_char (',') != MATCH_YES)
9812 goto syntax;
9814 /* Now we're sure the syntax is valid, we process this item
9815 further. */
9816 if (gfc_get_symbol (name, module_ns, &sym))
9817 return MATCH_ERROR;
9819 if (sym->attr.intrinsic)
9821 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9822 "PROCEDURE", &old_locus);
9823 return MATCH_ERROR;
9826 if (sym->attr.proc != PROC_MODULE
9827 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9828 return MATCH_ERROR;
9830 if (!gfc_add_interface (sym))
9831 return MATCH_ERROR;
9833 sym->attr.mod_proc = 1;
9834 sym->declared_at = old_locus;
9836 if (last)
9837 break;
9840 return MATCH_YES;
9842 syntax:
9843 /* Restore the previous state of the interface. */
9844 interface = gfc_current_interface_head ();
9845 gfc_set_current_interface_head (old_interface_head);
9847 /* Free the new interfaces. */
9848 while (interface != old_interface_head)
9850 gfc_interface *i = interface->next;
9851 free (interface);
9852 interface = i;
9855 /* And issue a syntax error. */
9856 gfc_syntax_error (ST_MODULE_PROC);
9857 return MATCH_ERROR;
9861 /* Check a derived type that is being extended. */
9863 static gfc_symbol*
9864 check_extended_derived_type (char *name)
9866 gfc_symbol *extended;
9868 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9870 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9871 return NULL;
9874 extended = gfc_find_dt_in_generic (extended);
9876 /* F08:C428. */
9877 if (!extended)
9879 gfc_error ("Symbol %qs at %C has not been previously defined", name);
9880 return NULL;
9883 if (extended->attr.flavor != FL_DERIVED)
9885 gfc_error ("%qs in EXTENDS expression at %C is not a "
9886 "derived type", name);
9887 return NULL;
9890 if (extended->attr.is_bind_c)
9892 gfc_error ("%qs cannot be extended at %C because it "
9893 "is BIND(C)", extended->name);
9894 return NULL;
9897 if (extended->attr.sequence)
9899 gfc_error ("%qs cannot be extended at %C because it "
9900 "is a SEQUENCE type", extended->name);
9901 return NULL;
9904 return extended;
9908 /* Match the optional attribute specifiers for a type declaration.
9909 Return MATCH_ERROR if an error is encountered in one of the handled
9910 attributes (public, private, bind(c)), MATCH_NO if what's found is
9911 not a handled attribute, and MATCH_YES otherwise. TODO: More error
9912 checking on attribute conflicts needs to be done. */
9914 match
9915 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
9917 /* See if the derived type is marked as private. */
9918 if (gfc_match (" , private") == MATCH_YES)
9920 if (gfc_current_state () != COMP_MODULE)
9922 gfc_error ("Derived type at %C can only be PRIVATE in the "
9923 "specification part of a module");
9924 return MATCH_ERROR;
9927 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
9928 return MATCH_ERROR;
9930 else if (gfc_match (" , public") == MATCH_YES)
9932 if (gfc_current_state () != COMP_MODULE)
9934 gfc_error ("Derived type at %C can only be PUBLIC in the "
9935 "specification part of a module");
9936 return MATCH_ERROR;
9939 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
9940 return MATCH_ERROR;
9942 else if (gfc_match (" , bind ( c )") == MATCH_YES)
9944 /* If the type is defined to be bind(c) it then needs to make
9945 sure that all fields are interoperable. This will
9946 need to be a semantic check on the finished derived type.
9947 See 15.2.3 (lines 9-12) of F2003 draft. */
9948 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
9949 return MATCH_ERROR;
9951 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
9953 else if (gfc_match (" , abstract") == MATCH_YES)
9955 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
9956 return MATCH_ERROR;
9958 if (!gfc_add_abstract (attr, &gfc_current_locus))
9959 return MATCH_ERROR;
9961 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
9963 if (!gfc_add_extension (attr, &gfc_current_locus))
9964 return MATCH_ERROR;
9966 else
9967 return MATCH_NO;
9969 /* If we get here, something matched. */
9970 return MATCH_YES;
9974 /* Common function for type declaration blocks similar to derived types, such
9975 as STRUCTURES and MAPs. Unlike derived types, a structure type
9976 does NOT have a generic symbol matching the name given by the user.
9977 STRUCTUREs can share names with variables and PARAMETERs so we must allow
9978 for the creation of an independent symbol.
9979 Other parameters are a message to prefix errors with, the name of the new
9980 type to be created, and the flavor to add to the resulting symbol. */
9982 static bool
9983 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
9984 gfc_symbol **result)
9986 gfc_symbol *sym;
9987 locus where;
9989 gcc_assert (name[0] == (char) TOUPPER (name[0]));
9991 if (decl)
9992 where = *decl;
9993 else
9994 where = gfc_current_locus;
9996 if (gfc_get_symbol (name, NULL, &sym))
9997 return false;
9999 if (!sym)
10001 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
10002 return false;
10005 if (sym->components != NULL || sym->attr.zero_comp)
10007 gfc_error ("Type definition of %qs at %C was already defined at %L",
10008 sym->name, &sym->declared_at);
10009 return false;
10012 sym->declared_at = where;
10014 if (sym->attr.flavor != fl
10015 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
10016 return false;
10018 if (!sym->hash_value)
10019 /* Set the hash for the compound name for this type. */
10020 sym->hash_value = gfc_hash_value (sym);
10022 /* Normally the type is expected to have been completely parsed by the time
10023 a field declaration with this type is seen. For unions, maps, and nested
10024 structure declarations, we need to indicate that it is okay that we
10025 haven't seen any components yet. This will be updated after the structure
10026 is fully parsed. */
10027 sym->attr.zero_comp = 0;
10029 /* Structures always act like derived-types with the SEQUENCE attribute */
10030 gfc_add_sequence (&sym->attr, sym->name, NULL);
10032 if (result) *result = sym;
10034 return true;
10038 /* Match the opening of a MAP block. Like a struct within a union in C;
10039 behaves identical to STRUCTURE blocks. */
10041 match
10042 gfc_match_map (void)
10044 /* Counter used to give unique internal names to map structures. */
10045 static unsigned int gfc_map_id = 0;
10046 char name[GFC_MAX_SYMBOL_LEN + 1];
10047 gfc_symbol *sym;
10048 locus old_loc;
10050 old_loc = gfc_current_locus;
10052 if (gfc_match_eos () != MATCH_YES)
10054 gfc_error ("Junk after MAP statement at %C");
10055 gfc_current_locus = old_loc;
10056 return MATCH_ERROR;
10059 /* Map blocks are anonymous so we make up unique names for the symbol table
10060 which are invalid Fortran identifiers. */
10061 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
10063 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
10064 return MATCH_ERROR;
10066 gfc_new_block = sym;
10068 return MATCH_YES;
10072 /* Match the opening of a UNION block. */
10074 match
10075 gfc_match_union (void)
10077 /* Counter used to give unique internal names to union types. */
10078 static unsigned int gfc_union_id = 0;
10079 char name[GFC_MAX_SYMBOL_LEN + 1];
10080 gfc_symbol *sym;
10081 locus old_loc;
10083 old_loc = gfc_current_locus;
10085 if (gfc_match_eos () != MATCH_YES)
10087 gfc_error ("Junk after UNION statement at %C");
10088 gfc_current_locus = old_loc;
10089 return MATCH_ERROR;
10092 /* Unions are anonymous so we make up unique names for the symbol table
10093 which are invalid Fortran identifiers. */
10094 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
10096 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
10097 return MATCH_ERROR;
10099 gfc_new_block = sym;
10101 return MATCH_YES;
10105 /* Match the beginning of a STRUCTURE declaration. This is similar to
10106 matching the beginning of a derived type declaration with a few
10107 twists. The resulting type symbol has no access control or other
10108 interesting attributes. */
10110 match
10111 gfc_match_structure_decl (void)
10113 /* Counter used to give unique internal names to anonymous structures. */
10114 static unsigned int gfc_structure_id = 0;
10115 char name[GFC_MAX_SYMBOL_LEN + 1];
10116 gfc_symbol *sym;
10117 match m;
10118 locus where;
10120 if (!flag_dec_structure)
10122 gfc_error ("%s at %C is a DEC extension, enable with "
10123 "%<-fdec-structure%>",
10124 "STRUCTURE");
10125 return MATCH_ERROR;
10128 name[0] = '\0';
10130 m = gfc_match (" /%n/", name);
10131 if (m != MATCH_YES)
10133 /* Non-nested structure declarations require a structure name. */
10134 if (!gfc_comp_struct (gfc_current_state ()))
10136 gfc_error ("Structure name expected in non-nested structure "
10137 "declaration at %C");
10138 return MATCH_ERROR;
10140 /* This is an anonymous structure; make up a unique name for it
10141 (upper-case letters never make it to symbol names from the source).
10142 The important thing is initializing the type variable
10143 and setting gfc_new_symbol, which is immediately used by
10144 parse_structure () and variable_decl () to add components of
10145 this type. */
10146 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
10149 where = gfc_current_locus;
10150 /* No field list allowed after non-nested structure declaration. */
10151 if (!gfc_comp_struct (gfc_current_state ())
10152 && gfc_match_eos () != MATCH_YES)
10154 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
10155 return MATCH_ERROR;
10158 /* Make sure the name is not the name of an intrinsic type. */
10159 if (gfc_is_intrinsic_typename (name))
10161 gfc_error ("Structure name %qs at %C cannot be the same as an"
10162 " intrinsic type", name);
10163 return MATCH_ERROR;
10166 /* Store the actual type symbol for the structure with an upper-case first
10167 letter (an invalid Fortran identifier). */
10169 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
10170 return MATCH_ERROR;
10172 gfc_new_block = sym;
10173 return MATCH_YES;
10177 /* This function does some work to determine which matcher should be used to
10178 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
10179 * as an alias for PRINT from derived type declarations, TYPE IS statements,
10180 * and [parameterized] derived type declarations. */
10182 match
10183 gfc_match_type (gfc_statement *st)
10185 char name[GFC_MAX_SYMBOL_LEN + 1];
10186 match m;
10187 locus old_loc;
10189 /* Requires -fdec. */
10190 if (!flag_dec)
10191 return MATCH_NO;
10193 m = gfc_match ("type");
10194 if (m != MATCH_YES)
10195 return m;
10196 /* If we already have an error in the buffer, it is probably from failing to
10197 * match a derived type data declaration. Let it happen. */
10198 else if (gfc_error_flag_test ())
10199 return MATCH_NO;
10201 old_loc = gfc_current_locus;
10202 *st = ST_NONE;
10204 /* If we see an attribute list before anything else it's definitely a derived
10205 * type declaration. */
10206 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
10207 goto derived;
10209 /* By now "TYPE" has already been matched. If we do not see a name, this may
10210 * be something like "TYPE *" or "TYPE <fmt>". */
10211 m = gfc_match_name (name);
10212 if (m != MATCH_YES)
10214 /* Let print match if it can, otherwise throw an error from
10215 * gfc_match_derived_decl. */
10216 gfc_current_locus = old_loc;
10217 if (gfc_match_print () == MATCH_YES)
10219 *st = ST_WRITE;
10220 return MATCH_YES;
10222 goto derived;
10225 /* Check for EOS. */
10226 if (gfc_match_eos () == MATCH_YES)
10228 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
10229 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
10230 * Otherwise if gfc_match_derived_decl fails it's probably an existing
10231 * symbol which can be printed. */
10232 gfc_current_locus = old_loc;
10233 m = gfc_match_derived_decl ();
10234 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
10236 *st = ST_DERIVED_DECL;
10237 return m;
10240 else
10242 /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
10243 like <type name(parameter)>. */
10244 gfc_gobble_whitespace ();
10245 bool paren = gfc_peek_ascii_char () == '(';
10246 if (paren)
10248 if (strcmp ("is", name) == 0)
10249 goto typeis;
10250 else
10251 goto derived;
10255 /* Treat TYPE... like PRINT... */
10256 gfc_current_locus = old_loc;
10257 *st = ST_WRITE;
10258 return gfc_match_print ();
10260 derived:
10261 gfc_current_locus = old_loc;
10262 *st = ST_DERIVED_DECL;
10263 return gfc_match_derived_decl ();
10265 typeis:
10266 gfc_current_locus = old_loc;
10267 *st = ST_TYPE_IS;
10268 return gfc_match_type_is ();
10272 /* Match the beginning of a derived type declaration. If a type name
10273 was the result of a function, then it is possible to have a symbol
10274 already to be known as a derived type yet have no components. */
10276 match
10277 gfc_match_derived_decl (void)
10279 char name[GFC_MAX_SYMBOL_LEN + 1];
10280 char parent[GFC_MAX_SYMBOL_LEN + 1];
10281 symbol_attribute attr;
10282 gfc_symbol *sym, *gensym;
10283 gfc_symbol *extended;
10284 match m;
10285 match is_type_attr_spec = MATCH_NO;
10286 bool seen_attr = false;
10287 gfc_interface *intr = NULL, *head;
10288 bool parameterized_type = false;
10289 bool seen_colons = false;
10291 if (gfc_comp_struct (gfc_current_state ()))
10292 return MATCH_NO;
10294 name[0] = '\0';
10295 parent[0] = '\0';
10296 gfc_clear_attr (&attr);
10297 extended = NULL;
10301 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
10302 if (is_type_attr_spec == MATCH_ERROR)
10303 return MATCH_ERROR;
10304 if (is_type_attr_spec == MATCH_YES)
10305 seen_attr = true;
10306 } while (is_type_attr_spec == MATCH_YES);
10308 /* Deal with derived type extensions. The extension attribute has
10309 been added to 'attr' but now the parent type must be found and
10310 checked. */
10311 if (parent[0])
10312 extended = check_extended_derived_type (parent);
10314 if (parent[0] && !extended)
10315 return MATCH_ERROR;
10317 m = gfc_match (" ::");
10318 if (m == MATCH_YES)
10320 seen_colons = true;
10322 else if (seen_attr)
10324 gfc_error ("Expected :: in TYPE definition at %C");
10325 return MATCH_ERROR;
10328 /* In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
10329 But, we need to simply return for TYPE(. */
10330 if (m == MATCH_NO && gfc_current_form == FORM_FREE)
10332 char c = gfc_peek_ascii_char ();
10333 if (c == '(')
10334 return m;
10335 if (!gfc_is_whitespace (c))
10337 gfc_error ("Mangled derived type definition at %C");
10338 return MATCH_NO;
10342 m = gfc_match (" %n ", name);
10343 if (m != MATCH_YES)
10344 return m;
10346 /* Make sure that we don't identify TYPE IS (...) as a parameterized
10347 derived type named 'is'.
10348 TODO Expand the check, when 'name' = "is" by matching " (tname) "
10349 and checking if this is a(n intrinsic) typename. This picks up
10350 misplaced TYPE IS statements such as in select_type_1.f03. */
10351 if (gfc_peek_ascii_char () == '(')
10353 if (gfc_current_state () == COMP_SELECT_TYPE
10354 || (!seen_colons && !strcmp (name, "is")))
10355 return MATCH_NO;
10356 parameterized_type = true;
10359 m = gfc_match_eos ();
10360 if (m != MATCH_YES && !parameterized_type)
10361 return m;
10363 /* Make sure the name is not the name of an intrinsic type. */
10364 if (gfc_is_intrinsic_typename (name))
10366 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
10367 "type", name);
10368 return MATCH_ERROR;
10371 if (gfc_get_symbol (name, NULL, &gensym))
10372 return MATCH_ERROR;
10374 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
10376 if (gensym->ts.u.derived)
10377 gfc_error ("Derived type name %qs at %C already has a basic type "
10378 "of %s", gensym->name, gfc_typename (&gensym->ts));
10379 else
10380 gfc_error ("Derived type name %qs at %C already has a basic type",
10381 gensym->name);
10382 return MATCH_ERROR;
10385 if (!gensym->attr.generic
10386 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
10387 return MATCH_ERROR;
10389 if (!gensym->attr.function
10390 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
10391 return MATCH_ERROR;
10393 if (gensym->attr.dummy)
10395 gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
10396 name, &gensym->declared_at);
10397 return MATCH_ERROR;
10400 sym = gfc_find_dt_in_generic (gensym);
10402 if (sym && (sym->components != NULL || sym->attr.zero_comp))
10404 gfc_error ("Derived type definition of %qs at %C has already been "
10405 "defined", sym->name);
10406 return MATCH_ERROR;
10409 if (!sym)
10411 /* Use upper case to save the actual derived-type symbol. */
10412 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
10413 sym->name = gfc_get_string ("%s", gensym->name);
10414 head = gensym->generic;
10415 intr = gfc_get_interface ();
10416 intr->sym = sym;
10417 intr->where = gfc_current_locus;
10418 intr->sym->declared_at = gfc_current_locus;
10419 intr->next = head;
10420 gensym->generic = intr;
10421 gensym->attr.if_source = IFSRC_DECL;
10424 /* The symbol may already have the derived attribute without the
10425 components. The ways this can happen is via a function
10426 definition, an INTRINSIC statement or a subtype in another
10427 derived type that is a pointer. The first part of the AND clause
10428 is true if the symbol is not the return value of a function. */
10429 if (sym->attr.flavor != FL_DERIVED
10430 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
10431 return MATCH_ERROR;
10433 if (attr.access != ACCESS_UNKNOWN
10434 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
10435 return MATCH_ERROR;
10436 else if (sym->attr.access == ACCESS_UNKNOWN
10437 && gensym->attr.access != ACCESS_UNKNOWN
10438 && !gfc_add_access (&sym->attr, gensym->attr.access,
10439 sym->name, NULL))
10440 return MATCH_ERROR;
10442 if (sym->attr.access != ACCESS_UNKNOWN
10443 && gensym->attr.access == ACCESS_UNKNOWN)
10444 gensym->attr.access = sym->attr.access;
10446 /* See if the derived type was labeled as bind(c). */
10447 if (attr.is_bind_c != 0)
10448 sym->attr.is_bind_c = attr.is_bind_c;
10450 /* Construct the f2k_derived namespace if it is not yet there. */
10451 if (!sym->f2k_derived)
10452 sym->f2k_derived = gfc_get_namespace (NULL, 0);
10454 if (parameterized_type)
10456 /* Ignore error or mismatches by going to the end of the statement
10457 in order to avoid the component declarations causing problems. */
10458 m = gfc_match_formal_arglist (sym, 0, 0, true);
10459 if (m != MATCH_YES)
10460 gfc_error_recovery ();
10461 else
10462 sym->attr.pdt_template = 1;
10463 m = gfc_match_eos ();
10464 if (m != MATCH_YES)
10466 gfc_error_recovery ();
10467 gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10471 if (extended && !sym->components)
10473 gfc_component *p;
10474 gfc_formal_arglist *f, *g, *h;
10476 /* Add the extended derived type as the first component. */
10477 gfc_add_component (sym, parent, &p);
10478 extended->refs++;
10479 gfc_set_sym_referenced (extended);
10481 p->ts.type = BT_DERIVED;
10482 p->ts.u.derived = extended;
10483 p->initializer = gfc_default_initializer (&p->ts);
10485 /* Set extension level. */
10486 if (extended->attr.extension == 255)
10488 /* Since the extension field is 8 bit wide, we can only have
10489 up to 255 extension levels. */
10490 gfc_error ("Maximum extension level reached with type %qs at %L",
10491 extended->name, &extended->declared_at);
10492 return MATCH_ERROR;
10494 sym->attr.extension = extended->attr.extension + 1;
10496 /* Provide the links between the extended type and its extension. */
10497 if (!extended->f2k_derived)
10498 extended->f2k_derived = gfc_get_namespace (NULL, 0);
10500 /* Copy the extended type-param-name-list from the extended type,
10501 append those of the extension and add the whole lot to the
10502 extension. */
10503 if (extended->attr.pdt_template)
10505 g = h = NULL;
10506 sym->attr.pdt_template = 1;
10507 for (f = extended->formal; f; f = f->next)
10509 if (f == extended->formal)
10511 g = gfc_get_formal_arglist ();
10512 h = g;
10514 else
10516 g->next = gfc_get_formal_arglist ();
10517 g = g->next;
10519 g->sym = f->sym;
10521 g->next = sym->formal;
10522 sym->formal = h;
10526 if (!sym->hash_value)
10527 /* Set the hash for the compound name for this type. */
10528 sym->hash_value = gfc_hash_value (sym);
10530 /* Take over the ABSTRACT attribute. */
10531 sym->attr.abstract = attr.abstract;
10533 gfc_new_block = sym;
10535 return MATCH_YES;
10539 /* Cray Pointees can be declared as:
10540 pointer (ipt, a (n,m,...,*)) */
10542 match
10543 gfc_mod_pointee_as (gfc_array_spec *as)
10545 as->cray_pointee = true; /* This will be useful to know later. */
10546 if (as->type == AS_ASSUMED_SIZE)
10547 as->cp_was_assumed = true;
10548 else if (as->type == AS_ASSUMED_SHAPE)
10550 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10551 return MATCH_ERROR;
10553 return MATCH_YES;
10557 /* Match the enum definition statement, here we are trying to match
10558 the first line of enum definition statement.
10559 Returns MATCH_YES if match is found. */
10561 match
10562 gfc_match_enum (void)
10564 match m;
10566 m = gfc_match_eos ();
10567 if (m != MATCH_YES)
10568 return m;
10570 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
10571 return MATCH_ERROR;
10573 return MATCH_YES;
10577 /* Returns an initializer whose value is one higher than the value of the
10578 LAST_INITIALIZER argument. If the argument is NULL, the
10579 initializers value will be set to zero. The initializer's kind
10580 will be set to gfc_c_int_kind.
10582 If -fshort-enums is given, the appropriate kind will be selected
10583 later after all enumerators have been parsed. A warning is issued
10584 here if an initializer exceeds gfc_c_int_kind. */
10586 static gfc_expr *
10587 enum_initializer (gfc_expr *last_initializer, locus where)
10589 gfc_expr *result;
10590 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
10592 mpz_init (result->value.integer);
10594 if (last_initializer != NULL)
10596 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10597 result->where = last_initializer->where;
10599 if (gfc_check_integer_range (result->value.integer,
10600 gfc_c_int_kind) != ARITH_OK)
10602 gfc_error ("Enumerator exceeds the C integer type at %C");
10603 return NULL;
10606 else
10608 /* Control comes here, if it's the very first enumerator and no
10609 initializer has been given. It will be initialized to zero. */
10610 mpz_set_si (result->value.integer, 0);
10613 return result;
10617 /* Match a variable name with an optional initializer. When this
10618 subroutine is called, a variable is expected to be parsed next.
10619 Depending on what is happening at the moment, updates either the
10620 symbol table or the current interface. */
10622 static match
10623 enumerator_decl (void)
10625 char name[GFC_MAX_SYMBOL_LEN + 1];
10626 gfc_expr *initializer;
10627 gfc_array_spec *as = NULL;
10628 gfc_symbol *sym;
10629 locus var_locus;
10630 match m;
10631 bool t;
10632 locus old_locus;
10634 initializer = NULL;
10635 old_locus = gfc_current_locus;
10637 /* When we get here, we've just matched a list of attributes and
10638 maybe a type and a double colon. The next thing we expect to see
10639 is the name of the symbol. */
10640 m = gfc_match_name (name);
10641 if (m != MATCH_YES)
10642 goto cleanup;
10644 var_locus = gfc_current_locus;
10646 /* OK, we've successfully matched the declaration. Now put the
10647 symbol in the current namespace. If we fail to create the symbol,
10648 bail out. */
10649 if (!build_sym (name, NULL, false, &as, &var_locus))
10651 m = MATCH_ERROR;
10652 goto cleanup;
10655 /* The double colon must be present in order to have initializers.
10656 Otherwise the statement is ambiguous with an assignment statement. */
10657 if (colon_seen)
10659 if (gfc_match_char ('=') == MATCH_YES)
10661 m = gfc_match_init_expr (&initializer);
10662 if (m == MATCH_NO)
10664 gfc_error ("Expected an initialization expression at %C");
10665 m = MATCH_ERROR;
10668 if (m != MATCH_YES)
10669 goto cleanup;
10673 /* If we do not have an initializer, the initialization value of the
10674 previous enumerator (stored in last_initializer) is incremented
10675 by 1 and is used to initialize the current enumerator. */
10676 if (initializer == NULL)
10677 initializer = enum_initializer (last_initializer, old_locus);
10679 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10681 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10682 &var_locus);
10683 m = MATCH_ERROR;
10684 goto cleanup;
10687 /* Store this current initializer, for the next enumerator variable
10688 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10689 use last_initializer below. */
10690 last_initializer = initializer;
10691 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10693 /* Maintain enumerator history. */
10694 gfc_find_symbol (name, NULL, 0, &sym);
10695 create_enum_history (sym, last_initializer);
10697 return (t) ? MATCH_YES : MATCH_ERROR;
10699 cleanup:
10700 /* Free stuff up and return. */
10701 gfc_free_expr (initializer);
10703 return m;
10707 /* Match the enumerator definition statement. */
10709 match
10710 gfc_match_enumerator_def (void)
10712 match m;
10713 bool t;
10715 gfc_clear_ts (&current_ts);
10717 m = gfc_match (" enumerator");
10718 if (m != MATCH_YES)
10719 return m;
10721 m = gfc_match (" :: ");
10722 if (m == MATCH_ERROR)
10723 return m;
10725 colon_seen = (m == MATCH_YES);
10727 if (gfc_current_state () != COMP_ENUM)
10729 gfc_error ("ENUM definition statement expected before %C");
10730 gfc_free_enum_history ();
10731 return MATCH_ERROR;
10734 (&current_ts)->type = BT_INTEGER;
10735 (&current_ts)->kind = gfc_c_int_kind;
10737 gfc_clear_attr (&current_attr);
10738 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10739 if (!t)
10741 m = MATCH_ERROR;
10742 goto cleanup;
10745 for (;;)
10747 m = enumerator_decl ();
10748 if (m == MATCH_ERROR)
10750 gfc_free_enum_history ();
10751 goto cleanup;
10753 if (m == MATCH_NO)
10754 break;
10756 if (gfc_match_eos () == MATCH_YES)
10757 goto cleanup;
10758 if (gfc_match_char (',') != MATCH_YES)
10759 break;
10762 if (gfc_current_state () == COMP_ENUM)
10764 gfc_free_enum_history ();
10765 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10766 m = MATCH_ERROR;
10769 cleanup:
10770 gfc_free_array_spec (current_as);
10771 current_as = NULL;
10772 return m;
10777 /* Match binding attributes. */
10779 static match
10780 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10782 bool found_passing = false;
10783 bool seen_ptr = false;
10784 match m = MATCH_YES;
10786 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
10787 this case the defaults are in there. */
10788 ba->access = ACCESS_UNKNOWN;
10789 ba->pass_arg = NULL;
10790 ba->pass_arg_num = 0;
10791 ba->nopass = 0;
10792 ba->non_overridable = 0;
10793 ba->deferred = 0;
10794 ba->ppc = ppc;
10796 /* If we find a comma, we believe there are binding attributes. */
10797 m = gfc_match_char (',');
10798 if (m == MATCH_NO)
10799 goto done;
10803 /* Access specifier. */
10805 m = gfc_match (" public");
10806 if (m == MATCH_ERROR)
10807 goto error;
10808 if (m == MATCH_YES)
10810 if (ba->access != ACCESS_UNKNOWN)
10812 gfc_error ("Duplicate access-specifier at %C");
10813 goto error;
10816 ba->access = ACCESS_PUBLIC;
10817 continue;
10820 m = gfc_match (" private");
10821 if (m == MATCH_ERROR)
10822 goto error;
10823 if (m == MATCH_YES)
10825 if (ba->access != ACCESS_UNKNOWN)
10827 gfc_error ("Duplicate access-specifier at %C");
10828 goto error;
10831 ba->access = ACCESS_PRIVATE;
10832 continue;
10835 /* If inside GENERIC, the following is not allowed. */
10836 if (!generic)
10839 /* NOPASS flag. */
10840 m = gfc_match (" nopass");
10841 if (m == MATCH_ERROR)
10842 goto error;
10843 if (m == MATCH_YES)
10845 if (found_passing)
10847 gfc_error ("Binding attributes already specify passing,"
10848 " illegal NOPASS at %C");
10849 goto error;
10852 found_passing = true;
10853 ba->nopass = 1;
10854 continue;
10857 /* PASS possibly including argument. */
10858 m = gfc_match (" pass");
10859 if (m == MATCH_ERROR)
10860 goto error;
10861 if (m == MATCH_YES)
10863 char arg[GFC_MAX_SYMBOL_LEN + 1];
10865 if (found_passing)
10867 gfc_error ("Binding attributes already specify passing,"
10868 " illegal PASS at %C");
10869 goto error;
10872 m = gfc_match (" ( %n )", arg);
10873 if (m == MATCH_ERROR)
10874 goto error;
10875 if (m == MATCH_YES)
10876 ba->pass_arg = gfc_get_string ("%s", arg);
10877 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10879 found_passing = true;
10880 ba->nopass = 0;
10881 continue;
10884 if (ppc)
10886 /* POINTER flag. */
10887 m = gfc_match (" pointer");
10888 if (m == MATCH_ERROR)
10889 goto error;
10890 if (m == MATCH_YES)
10892 if (seen_ptr)
10894 gfc_error ("Duplicate POINTER attribute at %C");
10895 goto error;
10898 seen_ptr = true;
10899 continue;
10902 else
10904 /* NON_OVERRIDABLE flag. */
10905 m = gfc_match (" non_overridable");
10906 if (m == MATCH_ERROR)
10907 goto error;
10908 if (m == MATCH_YES)
10910 if (ba->non_overridable)
10912 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
10913 goto error;
10916 ba->non_overridable = 1;
10917 continue;
10920 /* DEFERRED flag. */
10921 m = gfc_match (" deferred");
10922 if (m == MATCH_ERROR)
10923 goto error;
10924 if (m == MATCH_YES)
10926 if (ba->deferred)
10928 gfc_error ("Duplicate DEFERRED at %C");
10929 goto error;
10932 ba->deferred = 1;
10933 continue;
10939 /* Nothing matching found. */
10940 if (generic)
10941 gfc_error ("Expected access-specifier at %C");
10942 else
10943 gfc_error ("Expected binding attribute at %C");
10944 goto error;
10946 while (gfc_match_char (',') == MATCH_YES);
10948 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
10949 if (ba->non_overridable && ba->deferred)
10951 gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
10952 goto error;
10955 m = MATCH_YES;
10957 done:
10958 if (ba->access == ACCESS_UNKNOWN)
10959 ba->access = ppc ? gfc_current_block()->component_access
10960 : gfc_typebound_default_access;
10962 if (ppc && !seen_ptr)
10964 gfc_error ("POINTER attribute is required for procedure pointer component"
10965 " at %C");
10966 goto error;
10969 return m;
10971 error:
10972 return MATCH_ERROR;
10976 /* Match a PROCEDURE specific binding inside a derived type. */
10978 static match
10979 match_procedure_in_type (void)
10981 char name[GFC_MAX_SYMBOL_LEN + 1];
10982 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
10983 char* target = NULL, *ifc = NULL;
10984 gfc_typebound_proc tb;
10985 bool seen_colons;
10986 bool seen_attrs;
10987 match m;
10988 gfc_symtree* stree;
10989 gfc_namespace* ns;
10990 gfc_symbol* block;
10991 int num;
10993 /* Check current state. */
10994 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
10995 block = gfc_state_stack->previous->sym;
10996 gcc_assert (block);
10998 /* Try to match PROCEDURE(interface). */
10999 if (gfc_match (" (") == MATCH_YES)
11001 m = gfc_match_name (target_buf);
11002 if (m == MATCH_ERROR)
11003 return m;
11004 if (m != MATCH_YES)
11006 gfc_error ("Interface-name expected after %<(%> at %C");
11007 return MATCH_ERROR;
11010 if (gfc_match (" )") != MATCH_YES)
11012 gfc_error ("%<)%> expected at %C");
11013 return MATCH_ERROR;
11016 ifc = target_buf;
11019 /* Construct the data structure. */
11020 memset (&tb, 0, sizeof (tb));
11021 tb.where = gfc_current_locus;
11023 /* Match binding attributes. */
11024 m = match_binding_attributes (&tb, false, false);
11025 if (m == MATCH_ERROR)
11026 return m;
11027 seen_attrs = (m == MATCH_YES);
11029 /* Check that attribute DEFERRED is given if an interface is specified. */
11030 if (tb.deferred && !ifc)
11032 gfc_error ("Interface must be specified for DEFERRED binding at %C");
11033 return MATCH_ERROR;
11035 if (ifc && !tb.deferred)
11037 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
11038 return MATCH_ERROR;
11041 /* Match the colons. */
11042 m = gfc_match (" ::");
11043 if (m == MATCH_ERROR)
11044 return m;
11045 seen_colons = (m == MATCH_YES);
11046 if (seen_attrs && !seen_colons)
11048 gfc_error ("Expected %<::%> after binding-attributes at %C");
11049 return MATCH_ERROR;
11052 /* Match the binding names. */
11053 for(num=1;;num++)
11055 m = gfc_match_name (name);
11056 if (m == MATCH_ERROR)
11057 return m;
11058 if (m == MATCH_NO)
11060 gfc_error ("Expected binding name at %C");
11061 return MATCH_ERROR;
11064 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
11065 return MATCH_ERROR;
11067 /* Try to match the '=> target', if it's there. */
11068 target = ifc;
11069 m = gfc_match (" =>");
11070 if (m == MATCH_ERROR)
11071 return m;
11072 if (m == MATCH_YES)
11074 if (tb.deferred)
11076 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
11077 return MATCH_ERROR;
11080 if (!seen_colons)
11082 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
11083 " at %C");
11084 return MATCH_ERROR;
11087 m = gfc_match_name (target_buf);
11088 if (m == MATCH_ERROR)
11089 return m;
11090 if (m == MATCH_NO)
11092 gfc_error ("Expected binding target after %<=>%> at %C");
11093 return MATCH_ERROR;
11095 target = target_buf;
11098 /* If no target was found, it has the same name as the binding. */
11099 if (!target)
11100 target = name;
11102 /* Get the namespace to insert the symbols into. */
11103 ns = block->f2k_derived;
11104 gcc_assert (ns);
11106 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
11107 if (tb.deferred && !block->attr.abstract)
11109 gfc_error ("Type %qs containing DEFERRED binding at %C "
11110 "is not ABSTRACT", block->name);
11111 return MATCH_ERROR;
11114 /* See if we already have a binding with this name in the symtree which
11115 would be an error. If a GENERIC already targeted this binding, it may
11116 be already there but then typebound is still NULL. */
11117 stree = gfc_find_symtree (ns->tb_sym_root, name);
11118 if (stree && stree->n.tb)
11120 gfc_error ("There is already a procedure with binding name %qs for "
11121 "the derived type %qs at %C", name, block->name);
11122 return MATCH_ERROR;
11125 /* Insert it and set attributes. */
11127 if (!stree)
11129 stree = gfc_new_symtree (&ns->tb_sym_root, name);
11130 gcc_assert (stree);
11132 stree->n.tb = gfc_get_typebound_proc (&tb);
11134 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
11135 false))
11136 return MATCH_ERROR;
11137 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
11138 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
11139 target, &stree->n.tb->u.specific->n.sym->declared_at);
11141 if (gfc_match_eos () == MATCH_YES)
11142 return MATCH_YES;
11143 if (gfc_match_char (',') != MATCH_YES)
11144 goto syntax;
11147 syntax:
11148 gfc_error ("Syntax error in PROCEDURE statement at %C");
11149 return MATCH_ERROR;
11153 /* Match a GENERIC procedure binding inside a derived type. */
11155 match
11156 gfc_match_generic (void)
11158 char name[GFC_MAX_SYMBOL_LEN + 1];
11159 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
11160 gfc_symbol* block;
11161 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
11162 gfc_typebound_proc* tb;
11163 gfc_namespace* ns;
11164 interface_type op_type;
11165 gfc_intrinsic_op op;
11166 match m;
11168 /* Check current state. */
11169 if (gfc_current_state () == COMP_DERIVED)
11171 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
11172 return MATCH_ERROR;
11174 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
11175 return MATCH_NO;
11176 block = gfc_state_stack->previous->sym;
11177 ns = block->f2k_derived;
11178 gcc_assert (block && ns);
11180 memset (&tbattr, 0, sizeof (tbattr));
11181 tbattr.where = gfc_current_locus;
11183 /* See if we get an access-specifier. */
11184 m = match_binding_attributes (&tbattr, true, false);
11185 if (m == MATCH_ERROR)
11186 goto error;
11188 /* Now the colons, those are required. */
11189 if (gfc_match (" ::") != MATCH_YES)
11191 gfc_error ("Expected %<::%> at %C");
11192 goto error;
11195 /* Match the binding name; depending on type (operator / generic) format
11196 it for future error messages into bind_name. */
11198 m = gfc_match_generic_spec (&op_type, name, &op);
11199 if (m == MATCH_ERROR)
11200 return MATCH_ERROR;
11201 if (m == MATCH_NO)
11203 gfc_error ("Expected generic name or operator descriptor at %C");
11204 goto error;
11207 switch (op_type)
11209 case INTERFACE_GENERIC:
11210 case INTERFACE_DTIO:
11211 snprintf (bind_name, sizeof (bind_name), "%s", name);
11212 break;
11214 case INTERFACE_USER_OP:
11215 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
11216 break;
11218 case INTERFACE_INTRINSIC_OP:
11219 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
11220 gfc_op2string (op));
11221 break;
11223 case INTERFACE_NAMELESS:
11224 gfc_error ("Malformed GENERIC statement at %C");
11225 goto error;
11226 break;
11228 default:
11229 gcc_unreachable ();
11232 /* Match the required =>. */
11233 if (gfc_match (" =>") != MATCH_YES)
11235 gfc_error ("Expected %<=>%> at %C");
11236 goto error;
11239 /* Try to find existing GENERIC binding with this name / for this operator;
11240 if there is something, check that it is another GENERIC and then extend
11241 it rather than building a new node. Otherwise, create it and put it
11242 at the right position. */
11244 switch (op_type)
11246 case INTERFACE_DTIO:
11247 case INTERFACE_USER_OP:
11248 case INTERFACE_GENERIC:
11250 const bool is_op = (op_type == INTERFACE_USER_OP);
11251 gfc_symtree* st;
11253 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
11254 tb = st ? st->n.tb : NULL;
11255 break;
11258 case INTERFACE_INTRINSIC_OP:
11259 tb = ns->tb_op[op];
11260 break;
11262 default:
11263 gcc_unreachable ();
11266 if (tb)
11268 if (!tb->is_generic)
11270 gcc_assert (op_type == INTERFACE_GENERIC);
11271 gfc_error ("There's already a non-generic procedure with binding name"
11272 " %qs for the derived type %qs at %C",
11273 bind_name, block->name);
11274 goto error;
11277 if (tb->access != tbattr.access)
11279 gfc_error ("Binding at %C must have the same access as already"
11280 " defined binding %qs", bind_name);
11281 goto error;
11284 else
11286 tb = gfc_get_typebound_proc (NULL);
11287 tb->where = gfc_current_locus;
11288 tb->access = tbattr.access;
11289 tb->is_generic = 1;
11290 tb->u.generic = NULL;
11292 switch (op_type)
11294 case INTERFACE_DTIO:
11295 case INTERFACE_GENERIC:
11296 case INTERFACE_USER_OP:
11298 const bool is_op = (op_type == INTERFACE_USER_OP);
11299 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
11300 &ns->tb_sym_root, name);
11301 gcc_assert (st);
11302 st->n.tb = tb;
11304 break;
11307 case INTERFACE_INTRINSIC_OP:
11308 ns->tb_op[op] = tb;
11309 break;
11311 default:
11312 gcc_unreachable ();
11316 /* Now, match all following names as specific targets. */
11319 gfc_symtree* target_st;
11320 gfc_tbp_generic* target;
11322 m = gfc_match_name (name);
11323 if (m == MATCH_ERROR)
11324 goto error;
11325 if (m == MATCH_NO)
11327 gfc_error ("Expected specific binding name at %C");
11328 goto error;
11331 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
11333 /* See if this is a duplicate specification. */
11334 for (target = tb->u.generic; target; target = target->next)
11335 if (target_st == target->specific_st)
11337 gfc_error ("%qs already defined as specific binding for the"
11338 " generic %qs at %C", name, bind_name);
11339 goto error;
11342 target = gfc_get_tbp_generic ();
11343 target->specific_st = target_st;
11344 target->specific = NULL;
11345 target->next = tb->u.generic;
11346 target->is_operator = ((op_type == INTERFACE_USER_OP)
11347 || (op_type == INTERFACE_INTRINSIC_OP));
11348 tb->u.generic = target;
11350 while (gfc_match (" ,") == MATCH_YES);
11352 /* Here should be the end. */
11353 if (gfc_match_eos () != MATCH_YES)
11355 gfc_error ("Junk after GENERIC binding at %C");
11356 goto error;
11359 return MATCH_YES;
11361 error:
11362 return MATCH_ERROR;
11366 /* Match a FINAL declaration inside a derived type. */
11368 match
11369 gfc_match_final_decl (void)
11371 char name[GFC_MAX_SYMBOL_LEN + 1];
11372 gfc_symbol* sym;
11373 match m;
11374 gfc_namespace* module_ns;
11375 bool first, last;
11376 gfc_symbol* block;
11378 if (gfc_current_form == FORM_FREE)
11380 char c = gfc_peek_ascii_char ();
11381 if (!gfc_is_whitespace (c) && c != ':')
11382 return MATCH_NO;
11385 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
11387 if (gfc_current_form == FORM_FIXED)
11388 return MATCH_NO;
11390 gfc_error ("FINAL declaration at %C must be inside a derived type "
11391 "CONTAINS section");
11392 return MATCH_ERROR;
11395 block = gfc_state_stack->previous->sym;
11396 gcc_assert (block);
11398 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
11399 || gfc_state_stack->previous->previous->state != COMP_MODULE)
11401 gfc_error ("Derived type declaration with FINAL at %C must be in the"
11402 " specification part of a MODULE");
11403 return MATCH_ERROR;
11406 module_ns = gfc_current_ns;
11407 gcc_assert (module_ns);
11408 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
11410 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
11411 if (gfc_match (" ::") == MATCH_ERROR)
11412 return MATCH_ERROR;
11414 /* Match the sequence of procedure names. */
11415 first = true;
11416 last = false;
11419 gfc_finalizer* f;
11421 if (first && gfc_match_eos () == MATCH_YES)
11423 gfc_error ("Empty FINAL at %C");
11424 return MATCH_ERROR;
11427 m = gfc_match_name (name);
11428 if (m == MATCH_NO)
11430 gfc_error ("Expected module procedure name at %C");
11431 return MATCH_ERROR;
11433 else if (m != MATCH_YES)
11434 return MATCH_ERROR;
11436 if (gfc_match_eos () == MATCH_YES)
11437 last = true;
11438 if (!last && gfc_match_char (',') != MATCH_YES)
11440 gfc_error ("Expected %<,%> at %C");
11441 return MATCH_ERROR;
11444 if (gfc_get_symbol (name, module_ns, &sym))
11446 gfc_error ("Unknown procedure name %qs at %C", name);
11447 return MATCH_ERROR;
11450 /* Mark the symbol as module procedure. */
11451 if (sym->attr.proc != PROC_MODULE
11452 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
11453 return MATCH_ERROR;
11455 /* Check if we already have this symbol in the list, this is an error. */
11456 for (f = block->f2k_derived->finalizers; f; f = f->next)
11457 if (f->proc_sym == sym)
11459 gfc_error ("%qs at %C is already defined as FINAL procedure",
11460 name);
11461 return MATCH_ERROR;
11464 /* Add this symbol to the list of finalizers. */
11465 gcc_assert (block->f2k_derived);
11466 sym->refs++;
11467 f = XCNEW (gfc_finalizer);
11468 f->proc_sym = sym;
11469 f->proc_tree = NULL;
11470 f->where = gfc_current_locus;
11471 f->next = block->f2k_derived->finalizers;
11472 block->f2k_derived->finalizers = f;
11474 first = false;
11476 while (!last);
11478 return MATCH_YES;
11482 const ext_attr_t ext_attr_list[] = {
11483 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
11484 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
11485 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
11486 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
11487 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
11488 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
11489 { NULL, EXT_ATTR_LAST, NULL }
11492 /* Match a !GCC$ ATTRIBUTES statement of the form:
11493 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11494 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11496 TODO: We should support all GCC attributes using the same syntax for
11497 the attribute list, i.e. the list in C
11498 __attributes(( attribute-list ))
11499 matches then
11500 !GCC$ ATTRIBUTES attribute-list ::
11501 Cf. c-parser.c's c_parser_attributes; the data can then directly be
11502 saved into a TREE.
11504 As there is absolutely no risk of confusion, we should never return
11505 MATCH_NO. */
11506 match
11507 gfc_match_gcc_attributes (void)
11509 symbol_attribute attr;
11510 char name[GFC_MAX_SYMBOL_LEN + 1];
11511 unsigned id;
11512 gfc_symbol *sym;
11513 match m;
11515 gfc_clear_attr (&attr);
11516 for(;;)
11518 char ch;
11520 if (gfc_match_name (name) != MATCH_YES)
11521 return MATCH_ERROR;
11523 for (id = 0; id < EXT_ATTR_LAST; id++)
11524 if (strcmp (name, ext_attr_list[id].name) == 0)
11525 break;
11527 if (id == EXT_ATTR_LAST)
11529 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11530 return MATCH_ERROR;
11533 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
11534 return MATCH_ERROR;
11536 gfc_gobble_whitespace ();
11537 ch = gfc_next_ascii_char ();
11538 if (ch == ':')
11540 /* This is the successful exit condition for the loop. */
11541 if (gfc_next_ascii_char () == ':')
11542 break;
11545 if (ch == ',')
11546 continue;
11548 goto syntax;
11551 if (gfc_match_eos () == MATCH_YES)
11552 goto syntax;
11554 for(;;)
11556 m = gfc_match_name (name);
11557 if (m != MATCH_YES)
11558 return m;
11560 if (find_special (name, &sym, true))
11561 return MATCH_ERROR;
11563 sym->attr.ext_attr |= attr.ext_attr;
11565 if (gfc_match_eos () == MATCH_YES)
11566 break;
11568 if (gfc_match_char (',') != MATCH_YES)
11569 goto syntax;
11572 return MATCH_YES;
11574 syntax:
11575 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11576 return MATCH_ERROR;
11580 /* Match a !GCC$ UNROLL statement of the form:
11581 !GCC$ UNROLL n
11583 The parameter n is the number of times we are supposed to unroll.
11585 When we come here, we have already matched the !GCC$ UNROLL string. */
11586 match
11587 gfc_match_gcc_unroll (void)
11589 int value;
11591 if (gfc_match_small_int (&value) == MATCH_YES)
11593 if (value < 0 || value > USHRT_MAX)
11595 gfc_error ("%<GCC unroll%> directive requires a"
11596 " non-negative integral constant"
11597 " less than or equal to %u at %C",
11598 USHRT_MAX
11600 return MATCH_ERROR;
11602 if (gfc_match_eos () == MATCH_YES)
11604 directive_unroll = value == 0 ? 1 : value;
11605 return MATCH_YES;
11609 gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11610 return MATCH_ERROR;
11613 /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
11615 The parameter b is name of a middle-end built-in.
11616 FLAGS is optional and must be one of:
11617 - (inbranch)
11618 - (notinbranch)
11620 IF('target') is optional and TARGET is a name of a multilib ABI.
11622 When we come here, we have already matched the !GCC$ builtin string. */
11624 match
11625 gfc_match_gcc_builtin (void)
11627 char builtin[GFC_MAX_SYMBOL_LEN + 1];
11628 char target[GFC_MAX_SYMBOL_LEN + 1];
11630 if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
11631 return MATCH_ERROR;
11633 gfc_simd_clause clause = SIMD_NONE;
11634 if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
11635 clause = SIMD_NOTINBRANCH;
11636 else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
11637 clause = SIMD_INBRANCH;
11639 if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
11641 const char *abi = targetm.get_multilib_abi_name ();
11642 if (abi == NULL || strcmp (abi, target) != 0)
11643 return MATCH_YES;
11646 if (gfc_vectorized_builtins == NULL)
11647 gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
11649 char *r = XNEWVEC (char, strlen (builtin) + 32);
11650 sprintf (r, "__builtin_%s", builtin);
11652 bool existed;
11653 int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
11654 value |= clause;
11655 if (existed)
11656 free (r);
11658 return MATCH_YES;
11661 /* Match an !GCC$ IVDEP statement.
11662 When we come here, we have already matched the !GCC$ IVDEP string. */
11664 match
11665 gfc_match_gcc_ivdep (void)
11667 if (gfc_match_eos () == MATCH_YES)
11669 directive_ivdep = true;
11670 return MATCH_YES;
11673 gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
11674 return MATCH_ERROR;
11677 /* Match an !GCC$ VECTOR statement.
11678 When we come here, we have already matched the !GCC$ VECTOR string. */
11680 match
11681 gfc_match_gcc_vector (void)
11683 if (gfc_match_eos () == MATCH_YES)
11685 directive_vector = true;
11686 directive_novector = false;
11687 return MATCH_YES;
11690 gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
11691 return MATCH_ERROR;
11694 /* Match an !GCC$ NOVECTOR statement.
11695 When we come here, we have already matched the !GCC$ NOVECTOR string. */
11697 match
11698 gfc_match_gcc_novector (void)
11700 if (gfc_match_eos () == MATCH_YES)
11702 directive_novector = true;
11703 directive_vector = false;
11704 return MATCH_YES;
11707 gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
11708 return MATCH_ERROR;