re PR fortran/85798 (ICE in get_array_index, at fortran/data.c:69)
[official-gcc.git] / gcc / fortran / decl.c
blob51c19c17d1d47b4ed6dece1b7663f5d20bd16e62
1 /* Declaration statement matcher
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "stringpool.h"
28 #include "match.h"
29 #include "parse.h"
30 #include "constructor.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts;
54 static symbol_attribute current_attr;
55 static gfc_array_spec *current_as;
56 static int colon_seen;
57 static int attr_seen;
59 /* The current binding label (if any). */
60 static const char* curr_binding_label;
61 /* Need to know how many identifiers are on the current data declaration
62 line in case we're given the BIND(C) attribute with a NAME= specifier. */
63 static int num_idents_on_line;
64 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
65 can supply a name if the curr_binding_label is nil and NAME= was not. */
66 static int has_name_equals = 0;
68 /* Initializer of the previous enumerator. */
70 static gfc_expr *last_initializer;
72 /* History of all the enumerators is maintained, so that
73 kind values of all the enumerators could be updated depending
74 upon the maximum initialized value. */
76 typedef struct enumerator_history
78 gfc_symbol *sym;
79 gfc_expr *initializer;
80 struct enumerator_history *next;
82 enumerator_history;
84 /* Header of enum history chain. */
86 static enumerator_history *enum_history = NULL;
88 /* Pointer of enum history node containing largest initializer. */
90 static enumerator_history *max_enum = NULL;
92 /* gfc_new_block points to the symbol of a newly matched block. */
94 gfc_symbol *gfc_new_block;
96 bool gfc_matching_function;
98 /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
99 int directive_unroll = -1;
101 /* Map of middle-end built-ins that should be vectorized. */
102 hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
104 /* If a kind expression of a component of a parameterized derived type is
105 parameterized, temporarily store the expression here. */
106 static gfc_expr *saved_kind_expr = NULL;
108 /* Used to store the parameter list arising in a PDT declaration and
109 in the typespec of a PDT variable or component. */
110 static gfc_actual_arglist *decl_type_param_list;
111 static gfc_actual_arglist *type_param_spec_list;
113 /********************* DATA statement subroutines *********************/
115 static bool in_match_data = false;
117 bool
118 gfc_in_match_data (void)
120 return in_match_data;
123 static void
124 set_in_match_data (bool set_value)
126 in_match_data = set_value;
129 /* Free a gfc_data_variable structure and everything beneath it. */
131 static void
132 free_variable (gfc_data_variable *p)
134 gfc_data_variable *q;
136 for (; p; p = q)
138 q = p->next;
139 gfc_free_expr (p->expr);
140 gfc_free_iterator (&p->iter, 0);
141 free_variable (p->list);
142 free (p);
147 /* Free a gfc_data_value structure and everything beneath it. */
149 static void
150 free_value (gfc_data_value *p)
152 gfc_data_value *q;
154 for (; p; p = q)
156 q = p->next;
157 mpz_clear (p->repeat);
158 gfc_free_expr (p->expr);
159 free (p);
164 /* Free a list of gfc_data structures. */
166 void
167 gfc_free_data (gfc_data *p)
169 gfc_data *q;
171 for (; p; p = q)
173 q = p->next;
174 free_variable (p->var);
175 free_value (p->value);
176 free (p);
181 /* Free all data in a namespace. */
183 static void
184 gfc_free_data_all (gfc_namespace *ns)
186 gfc_data *d;
188 for (;ns->data;)
190 d = ns->data->next;
191 free (ns->data);
192 ns->data = d;
196 /* Reject data parsed since the last restore point was marked. */
198 void
199 gfc_reject_data (gfc_namespace *ns)
201 gfc_data *d;
203 while (ns->data && ns->data != ns->old_data)
205 d = ns->data->next;
206 free (ns->data);
207 ns->data = d;
211 static match var_element (gfc_data_variable *);
213 /* Match a list of variables terminated by an iterator and a right
214 parenthesis. */
216 static match
217 var_list (gfc_data_variable *parent)
219 gfc_data_variable *tail, var;
220 match m;
222 m = var_element (&var);
223 if (m == MATCH_ERROR)
224 return MATCH_ERROR;
225 if (m == MATCH_NO)
226 goto syntax;
228 tail = gfc_get_data_variable ();
229 *tail = var;
231 parent->list = tail;
233 for (;;)
235 if (gfc_match_char (',') != MATCH_YES)
236 goto syntax;
238 m = gfc_match_iterator (&parent->iter, 1);
239 if (m == MATCH_YES)
240 break;
241 if (m == MATCH_ERROR)
242 return MATCH_ERROR;
244 m = var_element (&var);
245 if (m == MATCH_ERROR)
246 return MATCH_ERROR;
247 if (m == MATCH_NO)
248 goto syntax;
250 tail->next = gfc_get_data_variable ();
251 tail = tail->next;
253 *tail = var;
256 if (gfc_match_char (')') != MATCH_YES)
257 goto syntax;
258 return MATCH_YES;
260 syntax:
261 gfc_syntax_error (ST_DATA);
262 return MATCH_ERROR;
266 /* Match a single element in a data variable list, which can be a
267 variable-iterator list. */
269 static match
270 var_element (gfc_data_variable *new_var)
272 match m;
273 gfc_symbol *sym;
275 memset (new_var, 0, sizeof (gfc_data_variable));
277 if (gfc_match_char ('(') == MATCH_YES)
278 return var_list (new_var);
280 m = gfc_match_variable (&new_var->expr, 0);
281 if (m != MATCH_YES)
282 return m;
284 if (new_var->expr->expr_type == EXPR_CONSTANT
285 && new_var->expr->symtree == NULL)
287 gfc_error ("Inquiry parameter cannot appear in a "
288 "data-stmt-object-list at %C");
289 return MATCH_ERROR;
292 sym = new_var->expr->symtree->n.sym;
294 /* Symbol should already have an associated type. */
295 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
296 return MATCH_ERROR;
298 if (!sym->attr.function && gfc_current_ns->parent
299 && gfc_current_ns->parent == sym->ns)
301 gfc_error ("Host associated variable %qs may not be in the DATA "
302 "statement at %C", sym->name);
303 return MATCH_ERROR;
306 if (gfc_current_state () != COMP_BLOCK_DATA
307 && sym->attr.in_common
308 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
309 "common block variable %qs in DATA statement at %C",
310 sym->name))
311 return MATCH_ERROR;
313 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
314 return MATCH_ERROR;
316 return MATCH_YES;
320 /* Match the top-level list of data variables. */
322 static match
323 top_var_list (gfc_data *d)
325 gfc_data_variable var, *tail, *new_var;
326 match m;
328 tail = NULL;
330 for (;;)
332 m = var_element (&var);
333 if (m == MATCH_NO)
334 goto syntax;
335 if (m == MATCH_ERROR)
336 return MATCH_ERROR;
338 new_var = gfc_get_data_variable ();
339 *new_var = var;
341 if (tail == NULL)
342 d->var = new_var;
343 else
344 tail->next = new_var;
346 tail = new_var;
348 if (gfc_match_char ('/') == MATCH_YES)
349 break;
350 if (gfc_match_char (',') != MATCH_YES)
351 goto syntax;
354 return MATCH_YES;
356 syntax:
357 gfc_syntax_error (ST_DATA);
358 gfc_free_data_all (gfc_current_ns);
359 return MATCH_ERROR;
363 static match
364 match_data_constant (gfc_expr **result)
366 char name[GFC_MAX_SYMBOL_LEN + 1];
367 gfc_symbol *sym, *dt_sym = NULL;
368 gfc_expr *expr;
369 match m;
370 locus old_loc;
372 m = gfc_match_literal_constant (&expr, 1);
373 if (m == MATCH_YES)
375 *result = expr;
376 return MATCH_YES;
379 if (m == MATCH_ERROR)
380 return MATCH_ERROR;
382 m = gfc_match_null (result);
383 if (m != MATCH_NO)
384 return m;
386 old_loc = gfc_current_locus;
388 /* Should this be a structure component, try to match it
389 before matching a name. */
390 m = gfc_match_rvalue (result);
391 if (m == MATCH_ERROR)
392 return m;
394 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
396 if (!gfc_simplify_expr (*result, 0))
397 m = MATCH_ERROR;
398 return m;
400 else if (m == MATCH_YES)
402 /* If a parameter inquiry ends up here, symtree is NULL but **result
403 contains the right constant expression. Check here. */
404 if ((*result)->symtree == NULL
405 && (*result)->expr_type == EXPR_CONSTANT
406 && ((*result)->ts.type == BT_INTEGER
407 || (*result)->ts.type == BT_REAL))
408 return m;
410 /* F2018:R845 data-stmt-constant is initial-data-target.
411 A data-stmt-constant shall be ... initial-data-target if and
412 only if the corresponding data-stmt-object has the POINTER
413 attribute. ... If data-stmt-constant is initial-data-target
414 the corresponding data statement object shall be
415 data-pointer-initialization compatible (7.5.4.6) with the initial
416 data target; the data statement object is initially associated
417 with the target. */
418 if ((*result)->symtree->n.sym->attr.save
419 && (*result)->symtree->n.sym->attr.target)
420 return m;
421 gfc_free_expr (*result);
424 gfc_current_locus = old_loc;
426 m = gfc_match_name (name);
427 if (m != MATCH_YES)
428 return m;
430 if (gfc_find_symbol (name, NULL, 1, &sym))
431 return MATCH_ERROR;
433 if (sym && sym->attr.generic)
434 dt_sym = gfc_find_dt_in_generic (sym);
436 if (sym == NULL
437 || (sym->attr.flavor != FL_PARAMETER
438 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
440 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
441 name);
442 *result = NULL;
443 return MATCH_ERROR;
445 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
446 return gfc_match_structure_constructor (dt_sym, result);
448 /* Check to see if the value is an initialization array expression. */
449 if (sym->value->expr_type == EXPR_ARRAY)
451 gfc_current_locus = old_loc;
453 m = gfc_match_init_expr (result);
454 if (m == MATCH_ERROR)
455 return m;
457 if (m == MATCH_YES)
459 if (!gfc_simplify_expr (*result, 0))
460 m = MATCH_ERROR;
462 if ((*result)->expr_type == EXPR_CONSTANT)
463 return m;
464 else
466 gfc_error ("Invalid initializer %s in Data statement at %C", name);
467 return MATCH_ERROR;
472 *result = gfc_copy_expr (sym->value);
473 return MATCH_YES;
477 /* Match a list of values in a DATA statement. The leading '/' has
478 already been seen at this point. */
480 static match
481 top_val_list (gfc_data *data)
483 gfc_data_value *new_val, *tail;
484 gfc_expr *expr;
485 match m;
487 tail = NULL;
489 for (;;)
491 m = match_data_constant (&expr);
492 if (m == MATCH_NO)
493 goto syntax;
494 if (m == MATCH_ERROR)
495 return MATCH_ERROR;
497 new_val = gfc_get_data_value ();
498 mpz_init (new_val->repeat);
500 if (tail == NULL)
501 data->value = new_val;
502 else
503 tail->next = new_val;
505 tail = new_val;
507 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
509 tail->expr = expr;
510 mpz_set_ui (tail->repeat, 1);
512 else
514 mpz_set (tail->repeat, expr->value.integer);
515 gfc_free_expr (expr);
517 m = match_data_constant (&tail->expr);
518 if (m == MATCH_NO)
519 goto syntax;
520 if (m == MATCH_ERROR)
521 return MATCH_ERROR;
524 if (gfc_match_char ('/') == MATCH_YES)
525 break;
526 if (gfc_match_char (',') == MATCH_NO)
527 goto syntax;
530 return MATCH_YES;
532 syntax:
533 gfc_syntax_error (ST_DATA);
534 gfc_free_data_all (gfc_current_ns);
535 return MATCH_ERROR;
539 /* Matches an old style initialization. */
541 static match
542 match_old_style_init (const char *name)
544 match m;
545 gfc_symtree *st;
546 gfc_symbol *sym;
547 gfc_data *newdata;
549 /* Set up data structure to hold initializers. */
550 gfc_find_sym_tree (name, NULL, 0, &st);
551 sym = st->n.sym;
553 newdata = gfc_get_data ();
554 newdata->var = gfc_get_data_variable ();
555 newdata->var->expr = gfc_get_variable_expr (st);
556 newdata->var->expr->where = sym->declared_at;
557 newdata->where = gfc_current_locus;
559 /* Match initial value list. This also eats the terminal '/'. */
560 m = top_val_list (newdata);
561 if (m != MATCH_YES)
563 free (newdata);
564 return m;
567 if (gfc_pure (NULL))
569 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
570 free (newdata);
571 return MATCH_ERROR;
573 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
575 /* Mark the variable as having appeared in a data statement. */
576 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
578 free (newdata);
579 return MATCH_ERROR;
582 /* Chain in namespace list of DATA initializers. */
583 newdata->next = gfc_current_ns->data;
584 gfc_current_ns->data = newdata;
586 return m;
590 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
591 we are matching a DATA statement and are therefore issuing an error
592 if we encounter something unexpected, if not, we're trying to match
593 an old-style initialization expression of the form INTEGER I /2/. */
595 match
596 gfc_match_data (void)
598 gfc_data *new_data;
599 gfc_expr *e;
600 match m;
602 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
603 if ((gfc_current_state () == COMP_FUNCTION
604 || gfc_current_state () == COMP_SUBROUTINE)
605 && gfc_state_stack->previous->state == COMP_INTERFACE)
607 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
608 return MATCH_ERROR;
611 set_in_match_data (true);
613 for (;;)
615 new_data = gfc_get_data ();
616 new_data->where = gfc_current_locus;
618 m = top_var_list (new_data);
619 if (m != MATCH_YES)
620 goto cleanup;
622 if (new_data->var->iter.var
623 && new_data->var->iter.var->ts.type == BT_INTEGER
624 && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
625 && new_data->var->list
626 && new_data->var->list->expr
627 && new_data->var->list->expr->ts.type == BT_CHARACTER
628 && new_data->var->list->expr->ref
629 && new_data->var->list->expr->ref->type == REF_SUBSTRING)
631 gfc_error ("Invalid substring in data-implied-do at %L in DATA "
632 "statement", &new_data->var->list->expr->where);
633 goto cleanup;
636 /* Check for an entity with an allocatable component, which is not
637 allowed. */
638 e = new_data->var->expr;
639 if (e)
641 bool invalid;
643 invalid = false;
644 for (gfc_ref *ref = e->ref; ref; ref = ref->next)
645 if ((ref->type == REF_COMPONENT
646 && ref->u.c.component->attr.allocatable)
647 || (ref->type == REF_ARRAY
648 && e->symtree->n.sym->attr.pointer != 1
649 && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
650 invalid = true;
652 if (invalid)
654 gfc_error ("Allocatable component or deferred-shaped array "
655 "near %C in DATA statement");
656 goto cleanup;
660 m = top_val_list (new_data);
661 if (m != MATCH_YES)
662 goto cleanup;
664 new_data->next = gfc_current_ns->data;
665 gfc_current_ns->data = new_data;
667 if (gfc_match_eos () == MATCH_YES)
668 break;
670 gfc_match_char (','); /* Optional comma */
673 set_in_match_data (false);
675 if (gfc_pure (NULL))
677 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
678 return MATCH_ERROR;
680 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
682 return MATCH_YES;
684 cleanup:
685 set_in_match_data (false);
686 gfc_free_data (new_data);
687 return MATCH_ERROR;
691 /************************ Declaration statements *********************/
694 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
695 list). The difference here is the expression is a list of constants
696 and is surrounded by '/'.
697 The typespec ts must match the typespec of the variable which the
698 clist is initializing.
699 The arrayspec tells whether this should match a list of constants
700 corresponding to array elements or a scalar (as == NULL). */
702 static match
703 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
705 gfc_constructor_base array_head = NULL;
706 gfc_expr *expr = NULL;
707 match m = MATCH_ERROR;
708 locus where;
709 mpz_t repeat, cons_size, as_size;
710 bool scalar;
711 int cmp;
713 gcc_assert (ts);
715 /* We have already matched '/' - now look for a constant list, as with
716 top_val_list from decl.c, but append the result to an array. */
717 if (gfc_match ("/") == MATCH_YES)
719 gfc_error ("Empty old style initializer list at %C");
720 return MATCH_ERROR;
723 where = gfc_current_locus;
724 scalar = !as || !as->rank;
726 if (!scalar && !spec_size (as, &as_size))
728 gfc_error ("Array in initializer list at %L must have an explicit shape",
729 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
730 /* Nothing to cleanup yet. */
731 return MATCH_ERROR;
734 mpz_init_set_ui (repeat, 0);
736 for (;;)
738 m = match_data_constant (&expr);
739 if (m != MATCH_YES)
740 expr = NULL; /* match_data_constant may set expr to garbage */
741 if (m == MATCH_NO)
742 goto syntax;
743 if (m == MATCH_ERROR)
744 goto cleanup;
746 /* Found r in repeat spec r*c; look for the constant to repeat. */
747 if ( gfc_match_char ('*') == MATCH_YES)
749 if (scalar)
751 gfc_error ("Repeat spec invalid in scalar initializer at %C");
752 goto cleanup;
754 if (expr->ts.type != BT_INTEGER)
756 gfc_error ("Repeat spec must be an integer at %C");
757 goto cleanup;
759 mpz_set (repeat, expr->value.integer);
760 gfc_free_expr (expr);
761 expr = NULL;
763 m = match_data_constant (&expr);
764 if (m == MATCH_NO)
766 m = MATCH_ERROR;
767 gfc_error ("Expected data constant after repeat spec at %C");
769 if (m != MATCH_YES)
770 goto cleanup;
772 /* No repeat spec, we matched the data constant itself. */
773 else
774 mpz_set_ui (repeat, 1);
776 if (!scalar)
778 /* Add the constant initializer as many times as repeated. */
779 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
781 /* Make sure types of elements match */
782 if(ts && !gfc_compare_types (&expr->ts, ts)
783 && !gfc_convert_type (expr, ts, 1))
784 goto cleanup;
786 gfc_constructor_append_expr (&array_head,
787 gfc_copy_expr (expr), &gfc_current_locus);
790 gfc_free_expr (expr);
791 expr = NULL;
794 /* For scalar initializers quit after one element. */
795 else
797 if(gfc_match_char ('/') != MATCH_YES)
799 gfc_error ("End of scalar initializer expected at %C");
800 goto cleanup;
802 break;
805 if (gfc_match_char ('/') == MATCH_YES)
806 break;
807 if (gfc_match_char (',') == MATCH_NO)
808 goto syntax;
811 /* If we break early from here out, we encountered an error. */
812 m = MATCH_ERROR;
814 /* Set up expr as an array constructor. */
815 if (!scalar)
817 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
818 expr->ts = *ts;
819 expr->value.constructor = array_head;
821 expr->rank = as->rank;
822 expr->shape = gfc_get_shape (expr->rank);
824 /* Validate sizes. We built expr ourselves, so cons_size will be
825 constant (we fail above for non-constant expressions).
826 We still need to verify that the sizes match. */
827 gcc_assert (gfc_array_size (expr, &cons_size));
828 cmp = mpz_cmp (cons_size, as_size);
829 if (cmp < 0)
830 gfc_error ("Not enough elements in array initializer at %C");
831 else if (cmp > 0)
832 gfc_error ("Too many elements in array initializer at %C");
833 mpz_clear (cons_size);
834 if (cmp)
835 goto cleanup;
838 /* Make sure scalar types match. */
839 else if (!gfc_compare_types (&expr->ts, ts)
840 && !gfc_convert_type (expr, ts, 1))
841 goto cleanup;
843 if (expr->ts.u.cl)
844 expr->ts.u.cl->length_from_typespec = 1;
846 *result = expr;
847 m = MATCH_YES;
848 goto done;
850 syntax:
851 m = MATCH_ERROR;
852 gfc_error ("Syntax error in old style initializer list at %C");
854 cleanup:
855 if (expr)
856 expr->value.constructor = NULL;
857 gfc_free_expr (expr);
858 gfc_constructor_free (array_head);
860 done:
861 mpz_clear (repeat);
862 if (!scalar)
863 mpz_clear (as_size);
864 return m;
868 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
870 static bool
871 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
873 int i, j;
875 if ((from->type == AS_ASSUMED_RANK && to->corank)
876 || (to->type == AS_ASSUMED_RANK && from->corank))
878 gfc_error ("The assumed-rank array at %C shall not have a codimension");
879 return false;
882 if (to->rank == 0 && from->rank > 0)
884 to->rank = from->rank;
885 to->type = from->type;
886 to->cray_pointee = from->cray_pointee;
887 to->cp_was_assumed = from->cp_was_assumed;
889 for (i = 0; i < to->corank; i++)
891 /* Do not exceed the limits on lower[] and upper[]. gfortran
892 cleans up elsewhere. */
893 j = from->rank + i;
894 if (j >= GFC_MAX_DIMENSIONS)
895 break;
897 to->lower[j] = to->lower[i];
898 to->upper[j] = to->upper[i];
900 for (i = 0; i < from->rank; i++)
902 if (copy)
904 to->lower[i] = gfc_copy_expr (from->lower[i]);
905 to->upper[i] = gfc_copy_expr (from->upper[i]);
907 else
909 to->lower[i] = from->lower[i];
910 to->upper[i] = from->upper[i];
914 else if (to->corank == 0 && from->corank > 0)
916 to->corank = from->corank;
917 to->cotype = from->cotype;
919 for (i = 0; i < from->corank; i++)
921 /* Do not exceed the limits on lower[] and upper[]. gfortran
922 cleans up elsewhere. */
923 j = to->rank + i;
924 if (j >= GFC_MAX_DIMENSIONS)
925 break;
927 if (copy)
929 to->lower[j] = gfc_copy_expr (from->lower[i]);
930 to->upper[j] = gfc_copy_expr (from->upper[i]);
932 else
934 to->lower[j] = from->lower[i];
935 to->upper[j] = from->upper[i];
940 if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
942 gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
943 "allowed dimensions of %d",
944 to->rank, to->corank, GFC_MAX_DIMENSIONS);
945 to->corank = GFC_MAX_DIMENSIONS - to->rank;
946 return false;
948 return true;
952 /* Match an intent specification. Since this can only happen after an
953 INTENT word, a legal intent-spec must follow. */
955 static sym_intent
956 match_intent_spec (void)
959 if (gfc_match (" ( in out )") == MATCH_YES)
960 return INTENT_INOUT;
961 if (gfc_match (" ( in )") == MATCH_YES)
962 return INTENT_IN;
963 if (gfc_match (" ( out )") == MATCH_YES)
964 return INTENT_OUT;
966 gfc_error ("Bad INTENT specification at %C");
967 return INTENT_UNKNOWN;
971 /* Matches a character length specification, which is either a
972 specification expression, '*', or ':'. */
974 static match
975 char_len_param_value (gfc_expr **expr, bool *deferred)
977 match m;
979 *expr = NULL;
980 *deferred = false;
982 if (gfc_match_char ('*') == MATCH_YES)
983 return MATCH_YES;
985 if (gfc_match_char (':') == MATCH_YES)
987 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
988 return MATCH_ERROR;
990 *deferred = true;
992 return MATCH_YES;
995 m = gfc_match_expr (expr);
997 if (m == MATCH_NO || m == MATCH_ERROR)
998 return m;
1000 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1001 return MATCH_ERROR;
1003 if ((*expr)->expr_type == EXPR_FUNCTION)
1005 if ((*expr)->ts.type == BT_INTEGER
1006 || ((*expr)->ts.type == BT_UNKNOWN
1007 && strcmp((*expr)->symtree->name, "null") != 0))
1008 return MATCH_YES;
1010 goto syntax;
1012 else if ((*expr)->expr_type == EXPR_CONSTANT)
1014 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1015 processor dependent and its value is greater than or equal to zero.
1016 F2008, 4.4.3.2: If the character length parameter value evaluates
1017 to a negative value, the length of character entities declared
1018 is zero. */
1020 if ((*expr)->ts.type == BT_INTEGER)
1022 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1023 mpz_set_si ((*expr)->value.integer, 0);
1025 else
1026 goto syntax;
1028 else if ((*expr)->expr_type == EXPR_ARRAY)
1029 goto syntax;
1030 else if ((*expr)->expr_type == EXPR_VARIABLE)
1032 bool t;
1033 gfc_expr *e;
1035 e = gfc_copy_expr (*expr);
1037 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1038 which causes an ICE if gfc_reduce_init_expr() is called. */
1039 if (e->ref && e->ref->type == REF_ARRAY
1040 && e->ref->u.ar.type == AR_UNKNOWN
1041 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1042 goto syntax;
1044 t = gfc_reduce_init_expr (e);
1046 if (!t && e->ts.type == BT_UNKNOWN
1047 && e->symtree->n.sym->attr.untyped == 1
1048 && (flag_implicit_none
1049 || e->symtree->n.sym->ns->seen_implicit_none == 1
1050 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1052 gfc_free_expr (e);
1053 goto syntax;
1056 if ((e->ref && e->ref->type == REF_ARRAY
1057 && e->ref->u.ar.type != AR_ELEMENT)
1058 || (!e->ref && e->expr_type == EXPR_ARRAY))
1060 gfc_free_expr (e);
1061 goto syntax;
1064 gfc_free_expr (e);
1067 return m;
1069 syntax:
1070 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1071 return MATCH_ERROR;
1075 /* A character length is a '*' followed by a literal integer or a
1076 char_len_param_value in parenthesis. */
1078 static match
1079 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1081 int length;
1082 match m;
1084 *deferred = false;
1085 m = gfc_match_char ('*');
1086 if (m != MATCH_YES)
1087 return m;
1089 m = gfc_match_small_literal_int (&length, NULL);
1090 if (m == MATCH_ERROR)
1091 return m;
1093 if (m == MATCH_YES)
1095 if (obsolescent_check
1096 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1097 return MATCH_ERROR;
1098 *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1099 return m;
1102 if (gfc_match_char ('(') == MATCH_NO)
1103 goto syntax;
1105 m = char_len_param_value (expr, deferred);
1106 if (m != MATCH_YES && gfc_matching_function)
1108 gfc_undo_symbols ();
1109 m = MATCH_YES;
1112 if (m == MATCH_ERROR)
1113 return m;
1114 if (m == MATCH_NO)
1115 goto syntax;
1117 if (gfc_match_char (')') == MATCH_NO)
1119 gfc_free_expr (*expr);
1120 *expr = NULL;
1121 goto syntax;
1124 return MATCH_YES;
1126 syntax:
1127 gfc_error ("Syntax error in character length specification at %C");
1128 return MATCH_ERROR;
1132 /* Special subroutine for finding a symbol. Check if the name is found
1133 in the current name space. If not, and we're compiling a function or
1134 subroutine and the parent compilation unit is an interface, then check
1135 to see if the name we've been given is the name of the interface
1136 (located in another namespace). */
1138 static int
1139 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1141 gfc_state_data *s;
1142 gfc_symtree *st;
1143 int i;
1145 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1146 if (i == 0)
1148 *result = st ? st->n.sym : NULL;
1149 goto end;
1152 if (gfc_current_state () != COMP_SUBROUTINE
1153 && gfc_current_state () != COMP_FUNCTION)
1154 goto end;
1156 s = gfc_state_stack->previous;
1157 if (s == NULL)
1158 goto end;
1160 if (s->state != COMP_INTERFACE)
1161 goto end;
1162 if (s->sym == NULL)
1163 goto end; /* Nameless interface. */
1165 if (strcmp (name, s->sym->name) == 0)
1167 *result = s->sym;
1168 return 0;
1171 end:
1172 return i;
1176 /* Special subroutine for getting a symbol node associated with a
1177 procedure name, used in SUBROUTINE and FUNCTION statements. The
1178 symbol is created in the parent using with symtree node in the
1179 child unit pointing to the symbol. If the current namespace has no
1180 parent, then the symbol is just created in the current unit. */
1182 static int
1183 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1185 gfc_symtree *st;
1186 gfc_symbol *sym;
1187 int rc = 0;
1189 /* Module functions have to be left in their own namespace because
1190 they have potentially (almost certainly!) already been referenced.
1191 In this sense, they are rather like external functions. This is
1192 fixed up in resolve.c(resolve_entries), where the symbol name-
1193 space is set to point to the master function, so that the fake
1194 result mechanism can work. */
1195 if (module_fcn_entry)
1197 /* Present if entry is declared to be a module procedure. */
1198 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1200 if (*result == NULL)
1201 rc = gfc_get_symbol (name, NULL, result);
1202 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1203 && (*result)->ts.type == BT_UNKNOWN
1204 && sym->attr.flavor == FL_UNKNOWN)
1205 /* Pick up the typespec for the entry, if declared in the function
1206 body. Note that this symbol is FL_UNKNOWN because it will
1207 only have appeared in a type declaration. The local symtree
1208 is set to point to the module symbol and a unique symtree
1209 to the local version. This latter ensures a correct clearing
1210 of the symbols. */
1212 /* If the ENTRY proceeds its specification, we need to ensure
1213 that this does not raise a "has no IMPLICIT type" error. */
1214 if (sym->ts.type == BT_UNKNOWN)
1215 sym->attr.untyped = 1;
1217 (*result)->ts = sym->ts;
1219 /* Put the symbol in the procedure namespace so that, should
1220 the ENTRY precede its specification, the specification
1221 can be applied. */
1222 (*result)->ns = gfc_current_ns;
1224 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1225 st->n.sym = *result;
1226 st = gfc_get_unique_symtree (gfc_current_ns);
1227 sym->refs++;
1228 st->n.sym = sym;
1231 else
1232 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1234 if (rc)
1235 return rc;
1237 sym = *result;
1238 if (sym->attr.proc == PROC_ST_FUNCTION)
1239 return rc;
1241 if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1243 /* Create a partially populated interface symbol to carry the
1244 characteristics of the procedure and the result. */
1245 sym->tlink = gfc_new_symbol (name, sym->ns);
1246 gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1247 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1248 if (sym->attr.dimension)
1249 sym->tlink->as = gfc_copy_array_spec (sym->as);
1251 /* Ideally, at this point, a copy would be made of the formal
1252 arguments and their namespace. However, this does not appear
1253 to be necessary, albeit at the expense of not being able to
1254 use gfc_compare_interfaces directly. */
1256 if (sym->result && sym->result != sym)
1258 sym->tlink->result = sym->result;
1259 sym->result = NULL;
1261 else if (sym->result)
1263 sym->tlink->result = sym->tlink;
1266 else if (sym && !sym->gfc_new
1267 && gfc_current_state () != COMP_INTERFACE)
1269 /* Trap another encompassed procedure with the same name. All
1270 these conditions are necessary to avoid picking up an entry
1271 whose name clashes with that of the encompassing procedure;
1272 this is handled using gsymbols to register unique, globally
1273 accessible names. */
1274 if (sym->attr.flavor != 0
1275 && sym->attr.proc != 0
1276 && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1277 && sym->attr.if_source != IFSRC_UNKNOWN)
1279 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1280 name, &sym->declared_at);
1281 return true;
1283 if (sym->attr.flavor != 0
1284 && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1286 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1287 name, &sym->declared_at);
1288 return true;
1291 if (sym->attr.external && sym->attr.procedure
1292 && gfc_current_state () == COMP_CONTAINS)
1294 gfc_error_now ("Contained procedure %qs at %C clashes with "
1295 "procedure defined at %L",
1296 name, &sym->declared_at);
1297 return true;
1300 /* Trap a procedure with a name the same as interface in the
1301 encompassing scope. */
1302 if (sym->attr.generic != 0
1303 && (sym->attr.subroutine || sym->attr.function)
1304 && !sym->attr.mod_proc)
1306 gfc_error_now ("Name %qs at %C is already defined"
1307 " as a generic interface at %L",
1308 name, &sym->declared_at);
1309 return true;
1312 /* Trap declarations of attributes in encompassing scope. The
1313 signature for this is that ts.kind is set. Legitimate
1314 references only set ts.type. */
1315 if (sym->ts.kind != 0
1316 && !sym->attr.implicit_type
1317 && sym->attr.proc == 0
1318 && gfc_current_ns->parent != NULL
1319 && sym->attr.access == 0
1320 && !module_fcn_entry)
1322 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1323 "from a previous declaration", name);
1324 return true;
1328 /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1329 subroutine-stmt of a module subprogram or of a nonabstract interface
1330 body that is declared in the scoping unit of a module or submodule. */
1331 if (sym->attr.external
1332 && (sym->attr.subroutine || sym->attr.function)
1333 && sym->attr.if_source == IFSRC_IFBODY
1334 && !current_attr.module_procedure
1335 && sym->attr.proc == PROC_MODULE
1336 && gfc_state_stack->state == COMP_CONTAINS)
1338 gfc_error_now ("Procedure %qs defined in interface body at %L "
1339 "clashes with internal procedure defined at %C",
1340 name, &sym->declared_at);
1341 return true;
1344 if (sym && !sym->gfc_new
1345 && sym->attr.flavor != FL_UNKNOWN
1346 && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1347 && gfc_state_stack->state == COMP_CONTAINS
1348 && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1350 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1351 name, &sym->declared_at);
1352 return true;
1355 if (gfc_current_ns->parent == NULL || *result == NULL)
1356 return rc;
1358 /* Module function entries will already have a symtree in
1359 the current namespace but will need one at module level. */
1360 if (module_fcn_entry)
1362 /* Present if entry is declared to be a module procedure. */
1363 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1364 if (st == NULL)
1365 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1367 else
1368 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1370 st->n.sym = sym;
1371 sym->refs++;
1373 /* See if the procedure should be a module procedure. */
1375 if (((sym->ns->proc_name != NULL
1376 && sym->ns->proc_name->attr.flavor == FL_MODULE
1377 && sym->attr.proc != PROC_MODULE)
1378 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1379 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1380 rc = 2;
1382 return rc;
1386 /* Verify that the given symbol representing a parameter is C
1387 interoperable, by checking to see if it was marked as such after
1388 its declaration. If the given symbol is not interoperable, a
1389 warning is reported, thus removing the need to return the status to
1390 the calling function. The standard does not require the user use
1391 one of the iso_c_binding named constants to declare an
1392 interoperable parameter, but we can't be sure if the param is C
1393 interop or not if the user doesn't. For example, integer(4) may be
1394 legal Fortran, but doesn't have meaning in C. It may interop with
1395 a number of the C types, which causes a problem because the
1396 compiler can't know which one. This code is almost certainly not
1397 portable, and the user will get what they deserve if the C type
1398 across platforms isn't always interoperable with integer(4). If
1399 the user had used something like integer(c_int) or integer(c_long),
1400 the compiler could have automatically handled the varying sizes
1401 across platforms. */
1403 bool
1404 gfc_verify_c_interop_param (gfc_symbol *sym)
1406 int is_c_interop = 0;
1407 bool retval = true;
1409 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1410 Don't repeat the checks here. */
1411 if (sym->attr.implicit_type)
1412 return true;
1414 /* For subroutines or functions that are passed to a BIND(C) procedure,
1415 they're interoperable if they're BIND(C) and their params are all
1416 interoperable. */
1417 if (sym->attr.flavor == FL_PROCEDURE)
1419 if (sym->attr.is_bind_c == 0)
1421 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1422 "attribute to be C interoperable", sym->name,
1423 &(sym->declared_at));
1424 return false;
1426 else
1428 if (sym->attr.is_c_interop == 1)
1429 /* We've already checked this procedure; don't check it again. */
1430 return true;
1431 else
1432 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1433 sym->common_block);
1437 /* See if we've stored a reference to a procedure that owns sym. */
1438 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1440 if (sym->ns->proc_name->attr.is_bind_c == 1)
1442 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1444 if (is_c_interop != 1)
1446 /* Make personalized messages to give better feedback. */
1447 if (sym->ts.type == BT_DERIVED)
1448 gfc_error ("Variable %qs at %L is a dummy argument to the "
1449 "BIND(C) procedure %qs but is not C interoperable "
1450 "because derived type %qs is not C interoperable",
1451 sym->name, &(sym->declared_at),
1452 sym->ns->proc_name->name,
1453 sym->ts.u.derived->name);
1454 else if (sym->ts.type == BT_CLASS)
1455 gfc_error ("Variable %qs at %L is a dummy argument to the "
1456 "BIND(C) procedure %qs but is not C interoperable "
1457 "because it is polymorphic",
1458 sym->name, &(sym->declared_at),
1459 sym->ns->proc_name->name);
1460 else if (warn_c_binding_type)
1461 gfc_warning (OPT_Wc_binding_type,
1462 "Variable %qs at %L is a dummy argument of the "
1463 "BIND(C) procedure %qs but may not be C "
1464 "interoperable",
1465 sym->name, &(sym->declared_at),
1466 sym->ns->proc_name->name);
1469 /* Character strings are only C interoperable if they have a
1470 length of 1. */
1471 if (sym->ts.type == BT_CHARACTER)
1473 gfc_charlen *cl = sym->ts.u.cl;
1474 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1475 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1477 gfc_error ("Character argument %qs at %L "
1478 "must be length 1 because "
1479 "procedure %qs is BIND(C)",
1480 sym->name, &sym->declared_at,
1481 sym->ns->proc_name->name);
1482 retval = false;
1486 /* We have to make sure that any param to a bind(c) routine does
1487 not have the allocatable, pointer, or optional attributes,
1488 according to J3/04-007, section 5.1. */
1489 if (sym->attr.allocatable == 1
1490 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1491 "ALLOCATABLE attribute in procedure %qs "
1492 "with BIND(C)", sym->name,
1493 &(sym->declared_at),
1494 sym->ns->proc_name->name))
1495 retval = false;
1497 if (sym->attr.pointer == 1
1498 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1499 "POINTER attribute in procedure %qs "
1500 "with BIND(C)", sym->name,
1501 &(sym->declared_at),
1502 sym->ns->proc_name->name))
1503 retval = false;
1505 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1507 gfc_error ("Scalar variable %qs at %L with POINTER or "
1508 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1509 " supported", sym->name, &(sym->declared_at),
1510 sym->ns->proc_name->name);
1511 retval = false;
1514 if (sym->attr.optional == 1 && sym->attr.value)
1516 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1517 "and the VALUE attribute because procedure %qs "
1518 "is BIND(C)", sym->name, &(sym->declared_at),
1519 sym->ns->proc_name->name);
1520 retval = false;
1522 else if (sym->attr.optional == 1
1523 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1524 "at %L with OPTIONAL attribute in "
1525 "procedure %qs which is BIND(C)",
1526 sym->name, &(sym->declared_at),
1527 sym->ns->proc_name->name))
1528 retval = false;
1530 /* Make sure that if it has the dimension attribute, that it is
1531 either assumed size or explicit shape. Deferred shape is already
1532 covered by the pointer/allocatable attribute. */
1533 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1534 && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1535 "at %L as dummy argument to the BIND(C) "
1536 "procedure %qs at %L", sym->name,
1537 &(sym->declared_at),
1538 sym->ns->proc_name->name,
1539 &(sym->ns->proc_name->declared_at)))
1540 retval = false;
1544 return retval;
1549 /* Function called by variable_decl() that adds a name to the symbol table. */
1551 static bool
1552 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1553 gfc_array_spec **as, locus *var_locus)
1555 symbol_attribute attr;
1556 gfc_symbol *sym;
1557 int upper;
1558 gfc_symtree *st;
1560 /* Symbols in a submodule are host associated from the parent module or
1561 submodules. Therefore, they can be overridden by declarations in the
1562 submodule scope. Deal with this by attaching the existing symbol to
1563 a new symtree and recycling the old symtree with a new symbol... */
1564 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1565 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1566 && st->n.sym != NULL
1567 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1569 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1570 s->n.sym = st->n.sym;
1571 sym = gfc_new_symbol (name, gfc_current_ns);
1574 st->n.sym = sym;
1575 sym->refs++;
1576 gfc_set_sym_referenced (sym);
1578 /* ...Otherwise generate a new symtree and new symbol. */
1579 else if (gfc_get_symbol (name, NULL, &sym))
1580 return false;
1582 /* Check if the name has already been defined as a type. The
1583 first letter of the symtree will be in upper case then. Of
1584 course, this is only necessary if the upper case letter is
1585 actually different. */
1587 upper = TOUPPER(name[0]);
1588 if (upper != name[0])
1590 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1591 gfc_symtree *st;
1593 gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1594 strcpy (u_name, name);
1595 u_name[0] = upper;
1597 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1599 /* STRUCTURE types can alias symbol names */
1600 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1602 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1603 &st->n.sym->declared_at);
1604 return false;
1608 /* Start updating the symbol table. Add basic type attribute if present. */
1609 if (current_ts.type != BT_UNKNOWN
1610 && (sym->attr.implicit_type == 0
1611 || !gfc_compare_types (&sym->ts, &current_ts))
1612 && !gfc_add_type (sym, &current_ts, var_locus))
1613 return false;
1615 if (sym->ts.type == BT_CHARACTER)
1617 sym->ts.u.cl = cl;
1618 sym->ts.deferred = cl_deferred;
1621 /* Add dimension attribute if present. */
1622 if (!gfc_set_array_spec (sym, *as, var_locus))
1623 return false;
1624 *as = NULL;
1626 /* Add attribute to symbol. The copy is so that we can reset the
1627 dimension attribute. */
1628 attr = current_attr;
1629 attr.dimension = 0;
1630 attr.codimension = 0;
1632 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1633 return false;
1635 /* Finish any work that may need to be done for the binding label,
1636 if it's a bind(c). The bind(c) attr is found before the symbol
1637 is made, and before the symbol name (for data decls), so the
1638 current_ts is holding the binding label, or nothing if the
1639 name= attr wasn't given. Therefore, test here if we're dealing
1640 with a bind(c) and make sure the binding label is set correctly. */
1641 if (sym->attr.is_bind_c == 1)
1643 if (!sym->binding_label)
1645 /* Set the binding label and verify that if a NAME= was specified
1646 then only one identifier was in the entity-decl-list. */
1647 if (!set_binding_label (&sym->binding_label, sym->name,
1648 num_idents_on_line))
1649 return false;
1653 /* See if we know we're in a common block, and if it's a bind(c)
1654 common then we need to make sure we're an interoperable type. */
1655 if (sym->attr.in_common == 1)
1657 /* Test the common block object. */
1658 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1659 && sym->ts.is_c_interop != 1)
1661 gfc_error_now ("Variable %qs in common block %qs at %C "
1662 "must be declared with a C interoperable "
1663 "kind since common block %qs is BIND(C)",
1664 sym->name, sym->common_block->name,
1665 sym->common_block->name);
1666 gfc_clear_error ();
1670 sym->attr.implied_index = 0;
1672 /* Use the parameter expressions for a parameterized derived type. */
1673 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1674 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1675 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1677 if (sym->ts.type == BT_CLASS)
1678 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1680 return true;
1684 /* Set character constant to the given length. The constant will be padded or
1685 truncated. If we're inside an array constructor without a typespec, we
1686 additionally check that all elements have the same length; check_len -1
1687 means no checking. */
1689 void
1690 gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1691 gfc_charlen_t check_len)
1693 gfc_char_t *s;
1694 gfc_charlen_t slen;
1696 if (expr->ts.type != BT_CHARACTER)
1697 return;
1699 if (expr->expr_type != EXPR_CONSTANT)
1701 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1702 return;
1705 slen = expr->value.character.length;
1706 if (len != slen)
1708 s = gfc_get_wide_string (len + 1);
1709 memcpy (s, expr->value.character.string,
1710 MIN (len, slen) * sizeof (gfc_char_t));
1711 if (len > slen)
1712 gfc_wide_memset (&s[slen], ' ', len - slen);
1714 if (warn_character_truncation && slen > len)
1715 gfc_warning_now (OPT_Wcharacter_truncation,
1716 "CHARACTER expression at %L is being truncated "
1717 "(%ld/%ld)", &expr->where,
1718 (long) slen, (long) len);
1720 /* Apply the standard by 'hand' otherwise it gets cleared for
1721 initializers. */
1722 if (check_len != -1 && slen != check_len
1723 && !(gfc_option.allow_std & GFC_STD_GNU))
1724 gfc_error_now ("The CHARACTER elements of the array constructor "
1725 "at %L must have the same length (%ld/%ld)",
1726 &expr->where, (long) slen,
1727 (long) check_len);
1729 s[len] = '\0';
1730 free (expr->value.character.string);
1731 expr->value.character.string = s;
1732 expr->value.character.length = len;
1737 /* Function to create and update the enumerator history
1738 using the information passed as arguments.
1739 Pointer "max_enum" is also updated, to point to
1740 enum history node containing largest initializer.
1742 SYM points to the symbol node of enumerator.
1743 INIT points to its enumerator value. */
1745 static void
1746 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1748 enumerator_history *new_enum_history;
1749 gcc_assert (sym != NULL && init != NULL);
1751 new_enum_history = XCNEW (enumerator_history);
1753 new_enum_history->sym = sym;
1754 new_enum_history->initializer = init;
1755 new_enum_history->next = NULL;
1757 if (enum_history == NULL)
1759 enum_history = new_enum_history;
1760 max_enum = enum_history;
1762 else
1764 new_enum_history->next = enum_history;
1765 enum_history = new_enum_history;
1767 if (mpz_cmp (max_enum->initializer->value.integer,
1768 new_enum_history->initializer->value.integer) < 0)
1769 max_enum = new_enum_history;
1774 /* Function to free enum kind history. */
1776 void
1777 gfc_free_enum_history (void)
1779 enumerator_history *current = enum_history;
1780 enumerator_history *next;
1782 while (current != NULL)
1784 next = current->next;
1785 free (current);
1786 current = next;
1788 max_enum = NULL;
1789 enum_history = NULL;
1793 /* Function called by variable_decl() that adds an initialization
1794 expression to a symbol. */
1796 static bool
1797 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1799 symbol_attribute attr;
1800 gfc_symbol *sym;
1801 gfc_expr *init;
1803 init = *initp;
1804 if (find_special (name, &sym, false))
1805 return false;
1807 attr = sym->attr;
1809 /* If this symbol is confirming an implicit parameter type,
1810 then an initialization expression is not allowed. */
1811 if (attr.flavor == FL_PARAMETER
1812 && sym->value != NULL
1813 && *initp != NULL)
1815 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1816 sym->name);
1817 return false;
1820 if (init == NULL)
1822 /* An initializer is required for PARAMETER declarations. */
1823 if (attr.flavor == FL_PARAMETER)
1825 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1826 return false;
1829 else
1831 /* If a variable appears in a DATA block, it cannot have an
1832 initializer. */
1833 if (sym->attr.data)
1835 gfc_error ("Variable %qs at %C with an initializer already "
1836 "appears in a DATA statement", sym->name);
1837 return false;
1840 /* Check if the assignment can happen. This has to be put off
1841 until later for derived type variables and procedure pointers. */
1842 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1843 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1844 && !sym->attr.proc_pointer
1845 && !gfc_check_assign_symbol (sym, NULL, init))
1846 return false;
1848 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1849 && init->ts.type == BT_CHARACTER)
1851 /* Update symbol character length according initializer. */
1852 if (!gfc_check_assign_symbol (sym, NULL, init))
1853 return false;
1855 if (sym->ts.u.cl->length == NULL)
1857 gfc_charlen_t clen;
1858 /* If there are multiple CHARACTER variables declared on the
1859 same line, we don't want them to share the same length. */
1860 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1862 if (sym->attr.flavor == FL_PARAMETER)
1864 if (init->expr_type == EXPR_CONSTANT)
1866 clen = init->value.character.length;
1867 sym->ts.u.cl->length
1868 = gfc_get_int_expr (gfc_charlen_int_kind,
1869 NULL, clen);
1871 else if (init->expr_type == EXPR_ARRAY)
1873 if (init->ts.u.cl && init->ts.u.cl->length)
1875 const gfc_expr *length = init->ts.u.cl->length;
1876 if (length->expr_type != EXPR_CONSTANT)
1878 gfc_error ("Cannot initialize parameter array "
1879 "at %L "
1880 "with variable length elements",
1881 &sym->declared_at);
1882 return false;
1884 clen = mpz_get_si (length->value.integer);
1886 else if (init->value.constructor)
1888 gfc_constructor *c;
1889 c = gfc_constructor_first (init->value.constructor);
1890 clen = c->expr->value.character.length;
1892 else
1893 gcc_unreachable ();
1894 sym->ts.u.cl->length
1895 = gfc_get_int_expr (gfc_charlen_int_kind,
1896 NULL, clen);
1898 else if (init->ts.u.cl && init->ts.u.cl->length)
1899 sym->ts.u.cl->length =
1900 gfc_copy_expr (sym->value->ts.u.cl->length);
1903 /* Update initializer character length according symbol. */
1904 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1906 if (!gfc_specification_expr (sym->ts.u.cl->length))
1907 return false;
1909 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
1910 false);
1911 /* resolve_charlen will complain later on if the length
1912 is too large. Just skeep the initialization in that case. */
1913 if (mpz_cmp (sym->ts.u.cl->length->value.integer,
1914 gfc_integer_kinds[k].huge) <= 0)
1916 HOST_WIDE_INT len
1917 = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
1919 if (init->expr_type == EXPR_CONSTANT)
1920 gfc_set_constant_character_len (len, init, -1);
1921 else if (init->expr_type == EXPR_ARRAY)
1923 gfc_constructor *c;
1925 /* Build a new charlen to prevent simplification from
1926 deleting the length before it is resolved. */
1927 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1928 init->ts.u.cl->length
1929 = gfc_copy_expr (sym->ts.u.cl->length);
1931 for (c = gfc_constructor_first (init->value.constructor);
1932 c; c = gfc_constructor_next (c))
1933 gfc_set_constant_character_len (len, c->expr, -1);
1939 /* If sym is implied-shape, set its upper bounds from init. */
1940 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1941 && sym->as->type == AS_IMPLIED_SHAPE)
1943 int dim;
1945 if (init->rank == 0)
1947 gfc_error ("Can't initialize implied-shape array at %L"
1948 " with scalar", &sym->declared_at);
1949 return false;
1952 /* Shape should be present, we get an initialization expression. */
1953 gcc_assert (init->shape);
1955 for (dim = 0; dim < sym->as->rank; ++dim)
1957 int k;
1958 gfc_expr *e, *lower;
1960 lower = sym->as->lower[dim];
1962 /* If the lower bound is an array element from another
1963 parameterized array, then it is marked with EXPR_VARIABLE and
1964 is an initialization expression. Try to reduce it. */
1965 if (lower->expr_type == EXPR_VARIABLE)
1966 gfc_reduce_init_expr (lower);
1968 if (lower->expr_type == EXPR_CONSTANT)
1970 /* All dimensions must be without upper bound. */
1971 gcc_assert (!sym->as->upper[dim]);
1973 k = lower->ts.kind;
1974 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1975 mpz_add (e->value.integer, lower->value.integer,
1976 init->shape[dim]);
1977 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1978 sym->as->upper[dim] = e;
1980 else
1982 gfc_error ("Non-constant lower bound in implied-shape"
1983 " declaration at %L", &lower->where);
1984 return false;
1988 sym->as->type = AS_EXPLICIT;
1991 /* Need to check if the expression we initialized this
1992 to was one of the iso_c_binding named constants. If so,
1993 and we're a parameter (constant), let it be iso_c.
1994 For example:
1995 integer(c_int), parameter :: my_int = c_int
1996 integer(my_int) :: my_int_2
1997 If we mark my_int as iso_c (since we can see it's value
1998 is equal to one of the named constants), then my_int_2
1999 will be considered C interoperable. */
2000 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2002 sym->ts.is_iso_c |= init->ts.is_iso_c;
2003 sym->ts.is_c_interop |= init->ts.is_c_interop;
2004 /* attr bits needed for module files. */
2005 sym->attr.is_iso_c |= init->ts.is_iso_c;
2006 sym->attr.is_c_interop |= init->ts.is_c_interop;
2007 if (init->ts.is_iso_c)
2008 sym->ts.f90_type = init->ts.f90_type;
2011 /* Add initializer. Make sure we keep the ranks sane. */
2012 if (sym->attr.dimension && init->rank == 0)
2014 mpz_t size;
2015 gfc_expr *array;
2016 int n;
2017 if (sym->attr.flavor == FL_PARAMETER
2018 && init->expr_type == EXPR_CONSTANT
2019 && spec_size (sym->as, &size)
2020 && mpz_cmp_si (size, 0) > 0)
2022 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2023 &init->where);
2024 for (n = 0; n < (int)mpz_get_si (size); n++)
2025 gfc_constructor_append_expr (&array->value.constructor,
2026 n == 0
2027 ? init
2028 : gfc_copy_expr (init),
2029 &init->where);
2031 array->shape = gfc_get_shape (sym->as->rank);
2032 for (n = 0; n < sym->as->rank; n++)
2033 spec_dimen_size (sym->as, n, &array->shape[n]);
2035 init = array;
2036 mpz_clear (size);
2038 init->rank = sym->as->rank;
2041 sym->value = init;
2042 if (sym->attr.save == SAVE_NONE)
2043 sym->attr.save = SAVE_IMPLICIT;
2044 *initp = NULL;
2047 return true;
2051 /* Function called by variable_decl() that adds a name to a structure
2052 being built. */
2054 static bool
2055 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2056 gfc_array_spec **as)
2058 gfc_state_data *s;
2059 gfc_component *c;
2061 /* F03:C438/C439. If the current symbol is of the same derived type that we're
2062 constructing, it must have the pointer attribute. */
2063 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2064 && current_ts.u.derived == gfc_current_block ()
2065 && current_attr.pointer == 0)
2067 if (current_attr.allocatable
2068 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2069 "must have the POINTER attribute"))
2071 return false;
2073 else if (current_attr.allocatable == 0)
2075 gfc_error ("Component at %C must have the POINTER attribute");
2076 return false;
2080 /* F03:C437. */
2081 if (current_ts.type == BT_CLASS
2082 && !(current_attr.pointer || current_attr.allocatable))
2084 gfc_error ("Component %qs with CLASS at %C must be allocatable "
2085 "or pointer", name);
2086 return false;
2089 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2091 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2093 gfc_error ("Array component of structure at %C must have explicit "
2094 "or deferred shape");
2095 return false;
2099 /* If we are in a nested union/map definition, gfc_add_component will not
2100 properly find repeated components because:
2101 (i) gfc_add_component does a flat search, where components of unions
2102 and maps are implicity chained so nested components may conflict.
2103 (ii) Unions and maps are not linked as components of their parent
2104 structures until after they are parsed.
2105 For (i) we use gfc_find_component which searches recursively, and for (ii)
2106 we search each block directly from the parse stack until we find the top
2107 level structure. */
2109 s = gfc_state_stack;
2110 if (s->state == COMP_UNION || s->state == COMP_MAP)
2112 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2114 c = gfc_find_component (s->sym, name, true, true, NULL);
2115 if (c != NULL)
2117 gfc_error_now ("Component %qs at %C already declared at %L",
2118 name, &c->loc);
2119 return false;
2121 /* Break after we've searched the entire chain. */
2122 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2123 break;
2124 s = s->previous;
2128 if (!gfc_add_component (gfc_current_block(), name, &c))
2129 return false;
2131 c->ts = current_ts;
2132 if (c->ts.type == BT_CHARACTER)
2133 c->ts.u.cl = cl;
2135 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2136 && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2137 && saved_kind_expr != NULL)
2138 c->kind_expr = gfc_copy_expr (saved_kind_expr);
2140 c->attr = current_attr;
2142 c->initializer = *init;
2143 *init = NULL;
2145 c->as = *as;
2146 if (c->as != NULL)
2148 if (c->as->corank)
2149 c->attr.codimension = 1;
2150 if (c->as->rank)
2151 c->attr.dimension = 1;
2153 *as = NULL;
2155 gfc_apply_init (&c->ts, &c->attr, c->initializer);
2157 /* Check array components. */
2158 if (!c->attr.dimension)
2159 goto scalar;
2161 if (c->attr.pointer)
2163 if (c->as->type != AS_DEFERRED)
2165 gfc_error ("Pointer array component of structure at %C must have a "
2166 "deferred shape");
2167 return false;
2170 else if (c->attr.allocatable)
2172 if (c->as->type != AS_DEFERRED)
2174 gfc_error ("Allocatable component of structure at %C must have a "
2175 "deferred shape");
2176 return false;
2179 else
2181 if (c->as->type != AS_EXPLICIT)
2183 gfc_error ("Array component of structure at %C must have an "
2184 "explicit shape");
2185 return false;
2189 scalar:
2190 if (c->ts.type == BT_CLASS)
2191 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2193 if (c->attr.pdt_kind || c->attr.pdt_len)
2195 gfc_symbol *sym;
2196 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2197 0, &sym);
2198 if (sym == NULL)
2200 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2201 "in the type parameter name list at %L",
2202 c->name, &gfc_current_block ()->declared_at);
2203 return false;
2205 sym->ts = c->ts;
2206 sym->attr.pdt_kind = c->attr.pdt_kind;
2207 sym->attr.pdt_len = c->attr.pdt_len;
2208 if (c->initializer)
2209 sym->value = gfc_copy_expr (c->initializer);
2210 sym->attr.flavor = FL_VARIABLE;
2213 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2214 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2215 && decl_type_param_list)
2216 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2218 return true;
2222 /* Match a 'NULL()', and possibly take care of some side effects. */
2224 match
2225 gfc_match_null (gfc_expr **result)
2227 gfc_symbol *sym;
2228 match m, m2 = MATCH_NO;
2230 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2231 return MATCH_ERROR;
2233 if (m == MATCH_NO)
2235 locus old_loc;
2236 char name[GFC_MAX_SYMBOL_LEN + 1];
2238 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2239 return m2;
2241 old_loc = gfc_current_locus;
2242 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2243 return MATCH_ERROR;
2244 if (m2 != MATCH_YES
2245 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2246 return MATCH_ERROR;
2247 if (m2 == MATCH_NO)
2249 gfc_current_locus = old_loc;
2250 return MATCH_NO;
2254 /* The NULL symbol now has to be/become an intrinsic function. */
2255 if (gfc_get_symbol ("null", NULL, &sym))
2257 gfc_error ("NULL() initialization at %C is ambiguous");
2258 return MATCH_ERROR;
2261 gfc_intrinsic_symbol (sym);
2263 if (sym->attr.proc != PROC_INTRINSIC
2264 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2265 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2266 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2267 return MATCH_ERROR;
2269 *result = gfc_get_null_expr (&gfc_current_locus);
2271 /* Invalid per F2008, C512. */
2272 if (m2 == MATCH_YES)
2274 gfc_error ("NULL() initialization at %C may not have MOLD");
2275 return MATCH_ERROR;
2278 return MATCH_YES;
2282 /* Match the initialization expr for a data pointer or procedure pointer. */
2284 static match
2285 match_pointer_init (gfc_expr **init, int procptr)
2287 match m;
2289 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2291 gfc_error ("Initialization of pointer at %C is not allowed in "
2292 "a PURE procedure");
2293 return MATCH_ERROR;
2295 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2297 /* Match NULL() initialization. */
2298 m = gfc_match_null (init);
2299 if (m != MATCH_NO)
2300 return m;
2302 /* Match non-NULL initialization. */
2303 gfc_matching_ptr_assignment = !procptr;
2304 gfc_matching_procptr_assignment = procptr;
2305 m = gfc_match_rvalue (init);
2306 gfc_matching_ptr_assignment = 0;
2307 gfc_matching_procptr_assignment = 0;
2308 if (m == MATCH_ERROR)
2309 return MATCH_ERROR;
2310 else if (m == MATCH_NO)
2312 gfc_error ("Error in pointer initialization at %C");
2313 return MATCH_ERROR;
2316 if (!procptr && !gfc_resolve_expr (*init))
2317 return MATCH_ERROR;
2319 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2320 "initialization at %C"))
2321 return MATCH_ERROR;
2323 return MATCH_YES;
2327 static bool
2328 check_function_name (char *name)
2330 /* In functions that have a RESULT variable defined, the function name always
2331 refers to function calls. Therefore, the name is not allowed to appear in
2332 specification statements. When checking this, be careful about
2333 'hidden' procedure pointer results ('ppr@'). */
2335 if (gfc_current_state () == COMP_FUNCTION)
2337 gfc_symbol *block = gfc_current_block ();
2338 if (block && block->result && block->result != block
2339 && strcmp (block->result->name, "ppr@") != 0
2340 && strcmp (block->name, name) == 0)
2342 gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2343 "from appearing in a specification statement",
2344 block->result->name, &block->result->declared_at, name);
2345 return false;
2349 return true;
2353 /* Match a variable name with an optional initializer. When this
2354 subroutine is called, a variable is expected to be parsed next.
2355 Depending on what is happening at the moment, updates either the
2356 symbol table or the current interface. */
2358 static match
2359 variable_decl (int elem)
2361 char name[GFC_MAX_SYMBOL_LEN + 1];
2362 static unsigned int fill_id = 0;
2363 gfc_expr *initializer, *char_len;
2364 gfc_array_spec *as;
2365 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2366 gfc_charlen *cl;
2367 bool cl_deferred;
2368 locus var_locus;
2369 match m;
2370 bool t;
2371 gfc_symbol *sym;
2373 initializer = NULL;
2374 as = NULL;
2375 cp_as = NULL;
2377 /* When we get here, we've just matched a list of attributes and
2378 maybe a type and a double colon. The next thing we expect to see
2379 is the name of the symbol. */
2381 /* If we are parsing a structure with legacy support, we allow the symbol
2382 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2383 m = MATCH_NO;
2384 gfc_gobble_whitespace ();
2385 if (gfc_peek_ascii_char () == '%')
2387 gfc_next_ascii_char ();
2388 m = gfc_match ("fill");
2391 if (m != MATCH_YES)
2393 m = gfc_match_name (name);
2394 if (m != MATCH_YES)
2395 goto cleanup;
2398 else
2400 m = MATCH_ERROR;
2401 if (gfc_current_state () != COMP_STRUCTURE)
2403 if (flag_dec_structure)
2404 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2405 else
2406 gfc_error ("%qs at %C is a DEC extension, enable with "
2407 "%<-fdec-structure%>", "%FILL");
2408 goto cleanup;
2411 if (attr_seen)
2413 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2414 goto cleanup;
2417 /* %FILL components are given invalid fortran names. */
2418 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2419 m = MATCH_YES;
2422 var_locus = gfc_current_locus;
2424 /* Now we could see the optional array spec. or character length. */
2425 m = gfc_match_array_spec (&as, true, true);
2426 if (m == MATCH_ERROR)
2427 goto cleanup;
2429 if (m == MATCH_NO)
2430 as = gfc_copy_array_spec (current_as);
2431 else if (current_as
2432 && !merge_array_spec (current_as, as, true))
2434 m = MATCH_ERROR;
2435 goto cleanup;
2438 if (flag_cray_pointer)
2439 cp_as = gfc_copy_array_spec (as);
2441 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2442 determine (and check) whether it can be implied-shape. If it
2443 was parsed as assumed-size, change it because PARAMETERs can not
2444 be assumed-size.
2446 An explicit-shape-array cannot appear under several conditions.
2447 That check is done here as well. */
2448 if (as)
2450 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2452 m = MATCH_ERROR;
2453 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2454 name, &var_locus);
2455 goto cleanup;
2458 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2459 && current_attr.flavor == FL_PARAMETER)
2460 as->type = AS_IMPLIED_SHAPE;
2462 if (as->type == AS_IMPLIED_SHAPE
2463 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2464 &var_locus))
2466 m = MATCH_ERROR;
2467 goto cleanup;
2470 /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2471 constant expressions shall appear only in a subprogram, derived
2472 type definition, BLOCK construct, or interface body. */
2473 if (as->type == AS_EXPLICIT
2474 && gfc_current_state () != COMP_BLOCK
2475 && gfc_current_state () != COMP_DERIVED
2476 && gfc_current_state () != COMP_FUNCTION
2477 && gfc_current_state () != COMP_INTERFACE
2478 && gfc_current_state () != COMP_SUBROUTINE)
2480 gfc_expr *e;
2481 bool not_constant = false;
2483 for (int i = 0; i < as->rank; i++)
2485 e = gfc_copy_expr (as->lower[i]);
2486 gfc_resolve_expr (e);
2487 gfc_simplify_expr (e, 0);
2488 if (e && (e->expr_type != EXPR_CONSTANT))
2490 not_constant = true;
2491 break;
2493 gfc_free_expr (e);
2495 e = gfc_copy_expr (as->upper[i]);
2496 gfc_resolve_expr (e);
2497 gfc_simplify_expr (e, 0);
2498 if (e && (e->expr_type != EXPR_CONSTANT))
2500 not_constant = true;
2501 break;
2503 gfc_free_expr (e);
2506 if (not_constant)
2508 gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2509 m = MATCH_ERROR;
2510 goto cleanup;
2513 if (as->type == AS_EXPLICIT)
2515 for (int i = 0; i < as->rank; i++)
2517 gfc_expr *e, *n;
2518 e = as->lower[i];
2519 if (e->expr_type != EXPR_CONSTANT)
2521 n = gfc_copy_expr (e);
2522 gfc_simplify_expr (n, 1);
2523 if (n->expr_type == EXPR_CONSTANT)
2524 gfc_replace_expr (e, n);
2525 else
2526 gfc_free_expr (n);
2528 e = as->upper[i];
2529 if (e->expr_type != EXPR_CONSTANT)
2531 n = gfc_copy_expr (e);
2532 gfc_simplify_expr (n, 1);
2533 if (n->expr_type == EXPR_CONSTANT)
2534 gfc_replace_expr (e, n);
2535 else
2536 gfc_free_expr (n);
2542 char_len = NULL;
2543 cl = NULL;
2544 cl_deferred = false;
2546 if (current_ts.type == BT_CHARACTER)
2548 switch (match_char_length (&char_len, &cl_deferred, false))
2550 case MATCH_YES:
2551 cl = gfc_new_charlen (gfc_current_ns, NULL);
2553 cl->length = char_len;
2554 break;
2556 /* Non-constant lengths need to be copied after the first
2557 element. Also copy assumed lengths. */
2558 case MATCH_NO:
2559 if (elem > 1
2560 && (current_ts.u.cl->length == NULL
2561 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2563 cl = gfc_new_charlen (gfc_current_ns, NULL);
2564 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2566 else
2567 cl = current_ts.u.cl;
2569 cl_deferred = current_ts.deferred;
2571 break;
2573 case MATCH_ERROR:
2574 goto cleanup;
2578 /* The dummy arguments and result of the abreviated form of MODULE
2579 PROCEDUREs, used in SUBMODULES should not be redefined. */
2580 if (gfc_current_ns->proc_name
2581 && gfc_current_ns->proc_name->abr_modproc_decl)
2583 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2584 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2586 m = MATCH_ERROR;
2587 gfc_error ("%qs at %C is a redefinition of the declaration "
2588 "in the corresponding interface for MODULE "
2589 "PROCEDURE %qs", sym->name,
2590 gfc_current_ns->proc_name->name);
2591 goto cleanup;
2595 /* %FILL components may not have initializers. */
2596 if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
2598 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2599 m = MATCH_ERROR;
2600 goto cleanup;
2603 /* If this symbol has already shown up in a Cray Pointer declaration,
2604 and this is not a component declaration,
2605 then we want to set the type & bail out. */
2606 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2608 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2609 if (sym != NULL && sym->attr.cray_pointee)
2611 m = MATCH_YES;
2612 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
2614 m = MATCH_ERROR;
2615 goto cleanup;
2618 /* Check to see if we have an array specification. */
2619 if (cp_as != NULL)
2621 if (sym->as != NULL)
2623 gfc_error ("Duplicate array spec for Cray pointee at %C");
2624 gfc_free_array_spec (cp_as);
2625 m = MATCH_ERROR;
2626 goto cleanup;
2628 else
2630 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2631 gfc_internal_error ("Couldn't set pointee array spec.");
2633 /* Fix the array spec. */
2634 m = gfc_mod_pointee_as (sym->as);
2635 if (m == MATCH_ERROR)
2636 goto cleanup;
2639 goto cleanup;
2641 else
2643 gfc_free_array_spec (cp_as);
2647 /* Procedure pointer as function result. */
2648 if (gfc_current_state () == COMP_FUNCTION
2649 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2650 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2651 strcpy (name, "ppr@");
2653 if (gfc_current_state () == COMP_FUNCTION
2654 && strcmp (name, gfc_current_block ()->name) == 0
2655 && gfc_current_block ()->result
2656 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2657 strcpy (name, "ppr@");
2659 /* OK, we've successfully matched the declaration. Now put the
2660 symbol in the current namespace, because it might be used in the
2661 optional initialization expression for this symbol, e.g. this is
2662 perfectly legal:
2664 integer, parameter :: i = huge(i)
2666 This is only true for parameters or variables of a basic type.
2667 For components of derived types, it is not true, so we don't
2668 create a symbol for those yet. If we fail to create the symbol,
2669 bail out. */
2670 if (!gfc_comp_struct (gfc_current_state ())
2671 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2673 m = MATCH_ERROR;
2674 goto cleanup;
2677 if (!check_function_name (name))
2679 m = MATCH_ERROR;
2680 goto cleanup;
2683 /* We allow old-style initializations of the form
2684 integer i /2/, j(4) /3*3, 1/
2685 (if no colon has been seen). These are different from data
2686 statements in that initializers are only allowed to apply to the
2687 variable immediately preceding, i.e.
2688 integer i, j /1, 2/
2689 is not allowed. Therefore we have to do some work manually, that
2690 could otherwise be left to the matchers for DATA statements. */
2692 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2694 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2695 "initialization at %C"))
2696 return MATCH_ERROR;
2698 /* Allow old style initializations for components of STRUCTUREs and MAPs
2699 but not components of derived types. */
2700 else if (gfc_current_state () == COMP_DERIVED)
2702 gfc_error ("Invalid old style initialization for derived type "
2703 "component at %C");
2704 m = MATCH_ERROR;
2705 goto cleanup;
2708 /* For structure components, read the initializer as a special
2709 expression and let the rest of this function apply the initializer
2710 as usual. */
2711 else if (gfc_comp_struct (gfc_current_state ()))
2713 m = match_clist_expr (&initializer, &current_ts, as);
2714 if (m == MATCH_NO)
2715 gfc_error ("Syntax error in old style initialization of %s at %C",
2716 name);
2717 if (m != MATCH_YES)
2718 goto cleanup;
2721 /* Otherwise we treat the old style initialization just like a
2722 DATA declaration for the current variable. */
2723 else
2724 return match_old_style_init (name);
2727 /* The double colon must be present in order to have initializers.
2728 Otherwise the statement is ambiguous with an assignment statement. */
2729 if (colon_seen)
2731 if (gfc_match (" =>") == MATCH_YES)
2733 if (!current_attr.pointer)
2735 gfc_error ("Initialization at %C isn't for a pointer variable");
2736 m = MATCH_ERROR;
2737 goto cleanup;
2740 m = match_pointer_init (&initializer, 0);
2741 if (m != MATCH_YES)
2742 goto cleanup;
2744 else if (gfc_match_char ('=') == MATCH_YES)
2746 if (current_attr.pointer)
2748 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2749 "not %<=%>");
2750 m = MATCH_ERROR;
2751 goto cleanup;
2754 m = gfc_match_init_expr (&initializer);
2755 if (m == MATCH_NO)
2757 gfc_error ("Expected an initialization expression at %C");
2758 m = MATCH_ERROR;
2761 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2762 && !gfc_comp_struct (gfc_state_stack->state))
2764 gfc_error ("Initialization of variable at %C is not allowed in "
2765 "a PURE procedure");
2766 m = MATCH_ERROR;
2769 if (current_attr.flavor != FL_PARAMETER
2770 && !gfc_comp_struct (gfc_state_stack->state))
2771 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2773 if (m != MATCH_YES)
2774 goto cleanup;
2778 if (initializer != NULL && current_attr.allocatable
2779 && gfc_comp_struct (gfc_current_state ()))
2781 gfc_error ("Initialization of allocatable component at %C is not "
2782 "allowed");
2783 m = MATCH_ERROR;
2784 goto cleanup;
2787 if (gfc_current_state () == COMP_DERIVED
2788 && gfc_current_block ()->attr.pdt_template)
2790 gfc_symbol *param;
2791 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2792 0, &param);
2793 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2795 gfc_error ("The component with KIND or LEN attribute at %C does not "
2796 "not appear in the type parameter list at %L",
2797 &gfc_current_block ()->declared_at);
2798 m = MATCH_ERROR;
2799 goto cleanup;
2801 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2803 gfc_error ("The component at %C that appears in the type parameter "
2804 "list at %L has neither the KIND nor LEN attribute",
2805 &gfc_current_block ()->declared_at);
2806 m = MATCH_ERROR;
2807 goto cleanup;
2809 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2811 gfc_error ("The component at %C which is a type parameter must be "
2812 "a scalar");
2813 m = MATCH_ERROR;
2814 goto cleanup;
2816 else if (param && initializer)
2817 param->value = gfc_copy_expr (initializer);
2820 /* Before adding a possible initilizer, do a simple check for compatibility
2821 of lhs and rhs types. Assigning a REAL value to a derived type is not a
2822 good thing. */
2823 if (current_ts.type == BT_DERIVED && initializer
2824 && (gfc_numeric_ts (&initializer->ts)
2825 || initializer->ts.type == BT_LOGICAL
2826 || initializer->ts.type == BT_CHARACTER))
2828 gfc_error ("Incompatible initialization between a derived type "
2829 "entity and an entity with %qs type at %C",
2830 gfc_typename (&initializer->ts));
2831 m = MATCH_ERROR;
2832 goto cleanup;
2836 /* Add the initializer. Note that it is fine if initializer is
2837 NULL here, because we sometimes also need to check if a
2838 declaration *must* have an initialization expression. */
2839 if (!gfc_comp_struct (gfc_current_state ()))
2840 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2841 else
2843 if (current_ts.type == BT_DERIVED
2844 && !current_attr.pointer && !initializer)
2845 initializer = gfc_default_initializer (&current_ts);
2846 t = build_struct (name, cl, &initializer, &as);
2848 /* If we match a nested structure definition we expect to see the
2849 * body even if the variable declarations blow up, so we need to keep
2850 * the structure declaration around. */
2851 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2852 gfc_commit_symbol (gfc_new_block);
2855 m = (t) ? MATCH_YES : MATCH_ERROR;
2857 cleanup:
2858 /* Free stuff up and return. */
2859 gfc_free_expr (initializer);
2860 gfc_free_array_spec (as);
2862 return m;
2866 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2867 This assumes that the byte size is equal to the kind number for
2868 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2870 match
2871 gfc_match_old_kind_spec (gfc_typespec *ts)
2873 match m;
2874 int original_kind;
2876 if (gfc_match_char ('*') != MATCH_YES)
2877 return MATCH_NO;
2879 m = gfc_match_small_literal_int (&ts->kind, NULL);
2880 if (m != MATCH_YES)
2881 return MATCH_ERROR;
2883 original_kind = ts->kind;
2885 /* Massage the kind numbers for complex types. */
2886 if (ts->type == BT_COMPLEX)
2888 if (ts->kind % 2)
2890 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2891 gfc_basic_typename (ts->type), original_kind);
2892 return MATCH_ERROR;
2894 ts->kind /= 2;
2898 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2899 ts->kind = 8;
2901 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2903 if (ts->kind == 4)
2905 if (flag_real4_kind == 8)
2906 ts->kind = 8;
2907 if (flag_real4_kind == 10)
2908 ts->kind = 10;
2909 if (flag_real4_kind == 16)
2910 ts->kind = 16;
2913 if (ts->kind == 8)
2915 if (flag_real8_kind == 4)
2916 ts->kind = 4;
2917 if (flag_real8_kind == 10)
2918 ts->kind = 10;
2919 if (flag_real8_kind == 16)
2920 ts->kind = 16;
2924 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2926 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2927 gfc_basic_typename (ts->type), original_kind);
2928 return MATCH_ERROR;
2931 if (!gfc_notify_std (GFC_STD_GNU,
2932 "Nonstandard type declaration %s*%d at %C",
2933 gfc_basic_typename(ts->type), original_kind))
2934 return MATCH_ERROR;
2936 return MATCH_YES;
2940 /* Match a kind specification. Since kinds are generally optional, we
2941 usually return MATCH_NO if something goes wrong. If a "kind="
2942 string is found, then we know we have an error. */
2944 match
2945 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2947 locus where, loc;
2948 gfc_expr *e;
2949 match m, n;
2950 char c;
2952 m = MATCH_NO;
2953 n = MATCH_YES;
2954 e = NULL;
2955 saved_kind_expr = NULL;
2957 where = loc = gfc_current_locus;
2959 if (kind_expr_only)
2960 goto kind_expr;
2962 if (gfc_match_char ('(') == MATCH_NO)
2963 return MATCH_NO;
2965 /* Also gobbles optional text. */
2966 if (gfc_match (" kind = ") == MATCH_YES)
2967 m = MATCH_ERROR;
2969 loc = gfc_current_locus;
2971 kind_expr:
2973 n = gfc_match_init_expr (&e);
2975 if (gfc_derived_parameter_expr (e))
2977 ts->kind = 0;
2978 saved_kind_expr = gfc_copy_expr (e);
2979 goto close_brackets;
2982 if (n != MATCH_YES)
2984 if (gfc_matching_function)
2986 /* The function kind expression might include use associated or
2987 imported parameters and try again after the specification
2988 expressions..... */
2989 if (gfc_match_char (')') != MATCH_YES)
2991 gfc_error ("Missing right parenthesis at %C");
2992 m = MATCH_ERROR;
2993 goto no_match;
2996 gfc_free_expr (e);
2997 gfc_undo_symbols ();
2998 return MATCH_YES;
3000 else
3002 /* ....or else, the match is real. */
3003 if (n == MATCH_NO)
3004 gfc_error ("Expected initialization expression at %C");
3005 if (n != MATCH_YES)
3006 return MATCH_ERROR;
3010 if (e->rank != 0)
3012 gfc_error ("Expected scalar initialization expression at %C");
3013 m = MATCH_ERROR;
3014 goto no_match;
3017 if (gfc_extract_int (e, &ts->kind, 1))
3019 m = MATCH_ERROR;
3020 goto no_match;
3023 /* Before throwing away the expression, let's see if we had a
3024 C interoperable kind (and store the fact). */
3025 if (e->ts.is_c_interop == 1)
3027 /* Mark this as C interoperable if being declared with one
3028 of the named constants from iso_c_binding. */
3029 ts->is_c_interop = e->ts.is_iso_c;
3030 ts->f90_type = e->ts.f90_type;
3031 if (e->symtree)
3032 ts->interop_kind = e->symtree->n.sym;
3035 gfc_free_expr (e);
3036 e = NULL;
3038 /* Ignore errors to this point, if we've gotten here. This means
3039 we ignore the m=MATCH_ERROR from above. */
3040 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3042 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3043 gfc_basic_typename (ts->type));
3044 gfc_current_locus = where;
3045 return MATCH_ERROR;
3048 /* Warn if, e.g., c_int is used for a REAL variable, but not
3049 if, e.g., c_double is used for COMPLEX as the standard
3050 explicitly says that the kind type parameter for complex and real
3051 variable is the same, i.e. c_float == c_float_complex. */
3052 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3053 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3054 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3055 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3056 "is %s", gfc_basic_typename (ts->f90_type), &where,
3057 gfc_basic_typename (ts->type));
3059 close_brackets:
3061 gfc_gobble_whitespace ();
3062 if ((c = gfc_next_ascii_char ()) != ')'
3063 && (ts->type != BT_CHARACTER || c != ','))
3065 if (ts->type == BT_CHARACTER)
3066 gfc_error ("Missing right parenthesis or comma at %C");
3067 else
3068 gfc_error ("Missing right parenthesis at %C");
3069 m = MATCH_ERROR;
3071 else
3072 /* All tests passed. */
3073 m = MATCH_YES;
3075 if(m == MATCH_ERROR)
3076 gfc_current_locus = where;
3078 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3079 ts->kind = 8;
3081 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3083 if (ts->kind == 4)
3085 if (flag_real4_kind == 8)
3086 ts->kind = 8;
3087 if (flag_real4_kind == 10)
3088 ts->kind = 10;
3089 if (flag_real4_kind == 16)
3090 ts->kind = 16;
3093 if (ts->kind == 8)
3095 if (flag_real8_kind == 4)
3096 ts->kind = 4;
3097 if (flag_real8_kind == 10)
3098 ts->kind = 10;
3099 if (flag_real8_kind == 16)
3100 ts->kind = 16;
3104 /* Return what we know from the test(s). */
3105 return m;
3107 no_match:
3108 gfc_free_expr (e);
3109 gfc_current_locus = where;
3110 return m;
3114 static match
3115 match_char_kind (int * kind, int * is_iso_c)
3117 locus where;
3118 gfc_expr *e;
3119 match m, n;
3120 bool fail;
3122 m = MATCH_NO;
3123 e = NULL;
3124 where = gfc_current_locus;
3126 n = gfc_match_init_expr (&e);
3128 if (n != MATCH_YES && gfc_matching_function)
3130 /* The expression might include use-associated or imported
3131 parameters and try again after the specification
3132 expressions. */
3133 gfc_free_expr (e);
3134 gfc_undo_symbols ();
3135 return MATCH_YES;
3138 if (n == MATCH_NO)
3139 gfc_error ("Expected initialization expression at %C");
3140 if (n != MATCH_YES)
3141 return MATCH_ERROR;
3143 if (e->rank != 0)
3145 gfc_error ("Expected scalar initialization expression at %C");
3146 m = MATCH_ERROR;
3147 goto no_match;
3150 if (gfc_derived_parameter_expr (e))
3152 saved_kind_expr = e;
3153 *kind = 0;
3154 return MATCH_YES;
3157 fail = gfc_extract_int (e, kind, 1);
3158 *is_iso_c = e->ts.is_iso_c;
3159 if (fail)
3161 m = MATCH_ERROR;
3162 goto no_match;
3165 gfc_free_expr (e);
3167 /* Ignore errors to this point, if we've gotten here. This means
3168 we ignore the m=MATCH_ERROR from above. */
3169 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3171 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3172 m = MATCH_ERROR;
3174 else
3175 /* All tests passed. */
3176 m = MATCH_YES;
3178 if (m == MATCH_ERROR)
3179 gfc_current_locus = where;
3181 /* Return what we know from the test(s). */
3182 return m;
3184 no_match:
3185 gfc_free_expr (e);
3186 gfc_current_locus = where;
3187 return m;
3191 /* Match the various kind/length specifications in a CHARACTER
3192 declaration. We don't return MATCH_NO. */
3194 match
3195 gfc_match_char_spec (gfc_typespec *ts)
3197 int kind, seen_length, is_iso_c;
3198 gfc_charlen *cl;
3199 gfc_expr *len;
3200 match m;
3201 bool deferred;
3203 len = NULL;
3204 seen_length = 0;
3205 kind = 0;
3206 is_iso_c = 0;
3207 deferred = false;
3209 /* Try the old-style specification first. */
3210 old_char_selector = 0;
3212 m = match_char_length (&len, &deferred, true);
3213 if (m != MATCH_NO)
3215 if (m == MATCH_YES)
3216 old_char_selector = 1;
3217 seen_length = 1;
3218 goto done;
3221 m = gfc_match_char ('(');
3222 if (m != MATCH_YES)
3224 m = MATCH_YES; /* Character without length is a single char. */
3225 goto done;
3228 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3229 if (gfc_match (" kind =") == MATCH_YES)
3231 m = match_char_kind (&kind, &is_iso_c);
3233 if (m == MATCH_ERROR)
3234 goto done;
3235 if (m == MATCH_NO)
3236 goto syntax;
3238 if (gfc_match (" , len =") == MATCH_NO)
3239 goto rparen;
3241 m = char_len_param_value (&len, &deferred);
3242 if (m == MATCH_NO)
3243 goto syntax;
3244 if (m == MATCH_ERROR)
3245 goto done;
3246 seen_length = 1;
3248 goto rparen;
3251 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3252 if (gfc_match (" len =") == MATCH_YES)
3254 m = char_len_param_value (&len, &deferred);
3255 if (m == MATCH_NO)
3256 goto syntax;
3257 if (m == MATCH_ERROR)
3258 goto done;
3259 seen_length = 1;
3261 if (gfc_match_char (')') == MATCH_YES)
3262 goto done;
3264 if (gfc_match (" , kind =") != MATCH_YES)
3265 goto syntax;
3267 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3268 goto done;
3270 goto rparen;
3273 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3274 m = char_len_param_value (&len, &deferred);
3275 if (m == MATCH_NO)
3276 goto syntax;
3277 if (m == MATCH_ERROR)
3278 goto done;
3279 seen_length = 1;
3281 m = gfc_match_char (')');
3282 if (m == MATCH_YES)
3283 goto done;
3285 if (gfc_match_char (',') != MATCH_YES)
3286 goto syntax;
3288 gfc_match (" kind ="); /* Gobble optional text. */
3290 m = match_char_kind (&kind, &is_iso_c);
3291 if (m == MATCH_ERROR)
3292 goto done;
3293 if (m == MATCH_NO)
3294 goto syntax;
3296 rparen:
3297 /* Require a right-paren at this point. */
3298 m = gfc_match_char (')');
3299 if (m == MATCH_YES)
3300 goto done;
3302 syntax:
3303 gfc_error ("Syntax error in CHARACTER declaration at %C");
3304 m = MATCH_ERROR;
3305 gfc_free_expr (len);
3306 return m;
3308 done:
3309 /* Deal with character functions after USE and IMPORT statements. */
3310 if (gfc_matching_function)
3312 gfc_free_expr (len);
3313 gfc_undo_symbols ();
3314 return MATCH_YES;
3317 if (m != MATCH_YES)
3319 gfc_free_expr (len);
3320 return m;
3323 /* Do some final massaging of the length values. */
3324 cl = gfc_new_charlen (gfc_current_ns, NULL);
3326 if (seen_length == 0)
3327 cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3328 else
3330 /* If gfortran ends up here, then len may be reducible to a constant.
3331 Try to do that here. If it does not reduce, simply assign len to
3332 charlen. A complication occurs with user-defined generic functions,
3333 which are not resolved. Use a private namespace to deal with
3334 generic functions. */
3336 if (len && len->expr_type != EXPR_CONSTANT)
3338 gfc_namespace *old_ns;
3339 gfc_expr *e;
3341 old_ns = gfc_current_ns;
3342 gfc_current_ns = gfc_get_namespace (NULL, 0);
3344 e = gfc_copy_expr (len);
3345 gfc_reduce_init_expr (e);
3346 if (e->expr_type == EXPR_CONSTANT)
3348 gfc_replace_expr (len, e);
3349 if (mpz_cmp_si (len->value.integer, 0) < 0)
3350 mpz_set_ui (len->value.integer, 0);
3352 else
3353 gfc_free_expr (e);
3355 gfc_free_namespace (gfc_current_ns);
3356 gfc_current_ns = old_ns;
3359 cl->length = len;
3362 ts->u.cl = cl;
3363 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3364 ts->deferred = deferred;
3366 /* We have to know if it was a C interoperable kind so we can
3367 do accurate type checking of bind(c) procs, etc. */
3368 if (kind != 0)
3369 /* Mark this as C interoperable if being declared with one
3370 of the named constants from iso_c_binding. */
3371 ts->is_c_interop = is_iso_c;
3372 else if (len != NULL)
3373 /* Here, we might have parsed something such as: character(c_char)
3374 In this case, the parsing code above grabs the c_char when
3375 looking for the length (line 1690, roughly). it's the last
3376 testcase for parsing the kind params of a character variable.
3377 However, it's not actually the length. this seems like it
3378 could be an error.
3379 To see if the user used a C interop kind, test the expr
3380 of the so called length, and see if it's C interoperable. */
3381 ts->is_c_interop = len->ts.is_iso_c;
3383 return MATCH_YES;
3387 /* Matches a RECORD declaration. */
3389 static match
3390 match_record_decl (char *name)
3392 locus old_loc;
3393 old_loc = gfc_current_locus;
3394 match m;
3396 m = gfc_match (" record /");
3397 if (m == MATCH_YES)
3399 if (!flag_dec_structure)
3401 gfc_current_locus = old_loc;
3402 gfc_error ("RECORD at %C is an extension, enable it with "
3403 "-fdec-structure");
3404 return MATCH_ERROR;
3406 m = gfc_match (" %n/", name);
3407 if (m == MATCH_YES)
3408 return MATCH_YES;
3411 gfc_current_locus = old_loc;
3412 if (flag_dec_structure
3413 && (gfc_match (" record% ") == MATCH_YES
3414 || gfc_match (" record%t") == MATCH_YES))
3415 gfc_error ("Structure name expected after RECORD at %C");
3416 if (m == MATCH_NO)
3417 return MATCH_NO;
3419 return MATCH_ERROR;
3423 /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3424 of expressions to substitute into the possibly parameterized expression
3425 'e'. Using a list is inefficient but should not be too bad since the
3426 number of type parameters is not likely to be large. */
3427 static bool
3428 insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3429 int* f)
3431 gfc_actual_arglist *param;
3432 gfc_expr *copy;
3434 if (e->expr_type != EXPR_VARIABLE)
3435 return false;
3437 gcc_assert (e->symtree);
3438 if (e->symtree->n.sym->attr.pdt_kind
3439 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3441 for (param = type_param_spec_list; param; param = param->next)
3442 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3443 break;
3445 if (param)
3447 copy = gfc_copy_expr (param->expr);
3448 *e = *copy;
3449 free (copy);
3453 return false;
3457 bool
3458 gfc_insert_kind_parameter_exprs (gfc_expr *e)
3460 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3464 bool
3465 gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3467 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3468 type_param_spec_list = param_list;
3469 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3470 type_param_spec_list = NULL;
3471 type_param_spec_list = old_param_spec_list;
3474 /* Determines the instance of a parameterized derived type to be used by
3475 matching determining the values of the kind parameters and using them
3476 in the name of the instance. If the instance exists, it is used, otherwise
3477 a new derived type is created. */
3478 match
3479 gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3480 gfc_actual_arglist **ext_param_list)
3482 /* The PDT template symbol. */
3483 gfc_symbol *pdt = *sym;
3484 /* The symbol for the parameter in the template f2k_namespace. */
3485 gfc_symbol *param;
3486 /* The hoped for instance of the PDT. */
3487 gfc_symbol *instance;
3488 /* The list of parameters appearing in the PDT declaration. */
3489 gfc_formal_arglist *type_param_name_list;
3490 /* Used to store the parameter specification list during recursive calls. */
3491 gfc_actual_arglist *old_param_spec_list;
3492 /* Pointers to the parameter specification being used. */
3493 gfc_actual_arglist *actual_param;
3494 gfc_actual_arglist *tail = NULL;
3495 /* Used to build up the name of the PDT instance. The prefix uses 4
3496 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3497 char name[GFC_MAX_SYMBOL_LEN + 21];
3499 bool name_seen = (param_list == NULL);
3500 bool assumed_seen = false;
3501 bool deferred_seen = false;
3502 bool spec_error = false;
3503 int kind_value, i;
3504 gfc_expr *kind_expr;
3505 gfc_component *c1, *c2;
3506 match m;
3508 type_param_spec_list = NULL;
3510 type_param_name_list = pdt->formal;
3511 actual_param = param_list;
3512 sprintf (name, "Pdt%s", pdt->name);
3514 /* Run through the parameter name list and pick up the actual
3515 parameter values or use the default values in the PDT declaration. */
3516 for (; type_param_name_list;
3517 type_param_name_list = type_param_name_list->next)
3519 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3521 if (actual_param->spec_type == SPEC_ASSUMED)
3522 spec_error = deferred_seen;
3523 else
3524 spec_error = assumed_seen;
3526 if (spec_error)
3528 gfc_error ("The type parameter spec list at %C cannot contain "
3529 "both ASSUMED and DEFERRED parameters");
3530 goto error_return;
3534 if (actual_param && actual_param->name)
3535 name_seen = true;
3536 param = type_param_name_list->sym;
3538 if (!param || !param->name)
3539 continue;
3541 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3542 /* An error should already have been thrown in resolve.c
3543 (resolve_fl_derived0). */
3544 if (!pdt->attr.use_assoc && !c1)
3545 goto error_return;
3547 kind_expr = NULL;
3548 if (!name_seen)
3550 if (!actual_param && !(c1 && c1->initializer))
3552 gfc_error ("The type parameter spec list at %C does not contain "
3553 "enough parameter expressions");
3554 goto error_return;
3556 else if (!actual_param && c1 && c1->initializer)
3557 kind_expr = gfc_copy_expr (c1->initializer);
3558 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3559 kind_expr = gfc_copy_expr (actual_param->expr);
3561 else
3563 actual_param = param_list;
3564 for (;actual_param; actual_param = actual_param->next)
3565 if (actual_param->name
3566 && strcmp (actual_param->name, param->name) == 0)
3567 break;
3568 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3569 kind_expr = gfc_copy_expr (actual_param->expr);
3570 else
3572 if (c1->initializer)
3573 kind_expr = gfc_copy_expr (c1->initializer);
3574 else if (!(actual_param && param->attr.pdt_len))
3576 gfc_error ("The derived parameter %qs at %C does not "
3577 "have a default value", param->name);
3578 goto error_return;
3583 /* Store the current parameter expressions in a temporary actual
3584 arglist 'list' so that they can be substituted in the corresponding
3585 expressions in the PDT instance. */
3586 if (type_param_spec_list == NULL)
3588 type_param_spec_list = gfc_get_actual_arglist ();
3589 tail = type_param_spec_list;
3591 else
3593 tail->next = gfc_get_actual_arglist ();
3594 tail = tail->next;
3596 tail->name = param->name;
3598 if (kind_expr)
3600 /* Try simplification even for LEN expressions. */
3601 gfc_resolve_expr (kind_expr);
3602 gfc_simplify_expr (kind_expr, 1);
3603 /* Variable expressions seem to default to BT_PROCEDURE.
3604 TODO find out why this is and fix it. */
3605 if (kind_expr->ts.type != BT_INTEGER
3606 && kind_expr->ts.type != BT_PROCEDURE)
3608 gfc_error ("The parameter expression at %C must be of "
3609 "INTEGER type and not %s type",
3610 gfc_basic_typename (kind_expr->ts.type));
3611 goto error_return;
3614 tail->expr = gfc_copy_expr (kind_expr);
3617 if (actual_param)
3618 tail->spec_type = actual_param->spec_type;
3620 if (!param->attr.pdt_kind)
3622 if (!name_seen && actual_param)
3623 actual_param = actual_param->next;
3624 if (kind_expr)
3626 gfc_free_expr (kind_expr);
3627 kind_expr = NULL;
3629 continue;
3632 if (actual_param
3633 && (actual_param->spec_type == SPEC_ASSUMED
3634 || actual_param->spec_type == SPEC_DEFERRED))
3636 gfc_error ("The KIND parameter %qs at %C cannot either be "
3637 "ASSUMED or DEFERRED", param->name);
3638 goto error_return;
3641 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3643 gfc_error ("The value for the KIND parameter %qs at %C does not "
3644 "reduce to a constant expression", param->name);
3645 goto error_return;
3648 gfc_extract_int (kind_expr, &kind_value);
3649 sprintf (name + strlen (name), "_%d", kind_value);
3651 if (!name_seen && actual_param)
3652 actual_param = actual_param->next;
3653 gfc_free_expr (kind_expr);
3656 if (!name_seen && actual_param)
3658 gfc_error ("The type parameter spec list at %C contains too many "
3659 "parameter expressions");
3660 goto error_return;
3663 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3664 build it, using 'pdt' as a template. */
3665 if (gfc_get_symbol (name, pdt->ns, &instance))
3667 gfc_error ("Parameterized derived type at %C is ambiguous");
3668 goto error_return;
3671 m = MATCH_YES;
3673 if (instance->attr.flavor == FL_DERIVED
3674 && instance->attr.pdt_type)
3676 instance->refs++;
3677 if (ext_param_list)
3678 *ext_param_list = type_param_spec_list;
3679 *sym = instance;
3680 gfc_commit_symbols ();
3681 return m;
3684 /* Start building the new instance of the parameterized type. */
3685 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3686 instance->attr.pdt_template = 0;
3687 instance->attr.pdt_type = 1;
3688 instance->declared_at = gfc_current_locus;
3690 /* Add the components, replacing the parameters in all expressions
3691 with the expressions for their values in 'type_param_spec_list'. */
3692 c1 = pdt->components;
3693 tail = type_param_spec_list;
3694 for (; c1; c1 = c1->next)
3696 gfc_add_component (instance, c1->name, &c2);
3698 c2->ts = c1->ts;
3699 c2->attr = c1->attr;
3701 /* The order of declaration of the type_specs might not be the
3702 same as that of the components. */
3703 if (c1->attr.pdt_kind || c1->attr.pdt_len)
3705 for (tail = type_param_spec_list; tail; tail = tail->next)
3706 if (strcmp (c1->name, tail->name) == 0)
3707 break;
3710 /* Deal with type extension by recursively calling this function
3711 to obtain the instance of the extended type. */
3712 if (gfc_current_state () != COMP_DERIVED
3713 && c1 == pdt->components
3714 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3715 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3716 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3718 gfc_formal_arglist *f;
3720 old_param_spec_list = type_param_spec_list;
3722 /* Obtain a spec list appropriate to the extended type..*/
3723 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3724 type_param_spec_list = actual_param;
3725 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3726 actual_param = actual_param->next;
3727 if (actual_param)
3729 gfc_free_actual_arglist (actual_param->next);
3730 actual_param->next = NULL;
3733 /* Now obtain the PDT instance for the extended type. */
3734 c2->param_list = type_param_spec_list;
3735 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3736 NULL);
3737 type_param_spec_list = old_param_spec_list;
3739 c2->ts.u.derived->refs++;
3740 gfc_set_sym_referenced (c2->ts.u.derived);
3742 /* Set extension level. */
3743 if (c2->ts.u.derived->attr.extension == 255)
3745 /* Since the extension field is 8 bit wide, we can only have
3746 up to 255 extension levels. */
3747 gfc_error ("Maximum extension level reached with type %qs at %L",
3748 c2->ts.u.derived->name,
3749 &c2->ts.u.derived->declared_at);
3750 goto error_return;
3752 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3754 continue;
3757 /* Set the component kind using the parameterized expression. */
3758 if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3759 && c1->kind_expr != NULL)
3761 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3762 gfc_insert_kind_parameter_exprs (e);
3763 gfc_simplify_expr (e, 1);
3764 gfc_extract_int (e, &c2->ts.kind);
3765 gfc_free_expr (e);
3766 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3768 gfc_error ("Kind %d not supported for type %s at %C",
3769 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3770 goto error_return;
3774 /* Similarly, set the string length if parameterized. */
3775 if (c1->ts.type == BT_CHARACTER
3776 && c1->ts.u.cl->length
3777 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3779 gfc_expr *e;
3780 e = gfc_copy_expr (c1->ts.u.cl->length);
3781 gfc_insert_kind_parameter_exprs (e);
3782 gfc_simplify_expr (e, 1);
3783 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3784 c2->ts.u.cl->length = e;
3785 c2->attr.pdt_string = 1;
3788 /* Set up either the KIND/LEN initializer, if constant,
3789 or the parameterized expression. Use the template
3790 initializer if one is not already set in this instance. */
3791 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3793 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3794 c2->initializer = gfc_copy_expr (tail->expr);
3795 else if (tail && tail->expr)
3797 c2->param_list = gfc_get_actual_arglist ();
3798 c2->param_list->name = tail->name;
3799 c2->param_list->expr = gfc_copy_expr (tail->expr);
3800 c2->param_list->next = NULL;
3803 if (!c2->initializer && c1->initializer)
3804 c2->initializer = gfc_copy_expr (c1->initializer);
3807 /* Copy the array spec. */
3808 c2->as = gfc_copy_array_spec (c1->as);
3809 if (c1->ts.type == BT_CLASS)
3810 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
3812 /* Determine if an array spec is parameterized. If so, substitute
3813 in the parameter expressions for the bounds and set the pdt_array
3814 attribute. Notice that this attribute must be unconditionally set
3815 if this is an array of parameterized character length. */
3816 if (c1->as && c1->as->type == AS_EXPLICIT)
3818 bool pdt_array = false;
3820 /* Are the bounds of the array parameterized? */
3821 for (i = 0; i < c1->as->rank; i++)
3823 if (gfc_derived_parameter_expr (c1->as->lower[i]))
3824 pdt_array = true;
3825 if (gfc_derived_parameter_expr (c1->as->upper[i]))
3826 pdt_array = true;
3829 /* If they are, free the expressions for the bounds and
3830 replace them with the template expressions with substitute
3831 values. */
3832 for (i = 0; pdt_array && i < c1->as->rank; i++)
3834 gfc_expr *e;
3835 e = gfc_copy_expr (c1->as->lower[i]);
3836 gfc_insert_kind_parameter_exprs (e);
3837 gfc_simplify_expr (e, 1);
3838 gfc_free_expr (c2->as->lower[i]);
3839 c2->as->lower[i] = e;
3840 e = gfc_copy_expr (c1->as->upper[i]);
3841 gfc_insert_kind_parameter_exprs (e);
3842 gfc_simplify_expr (e, 1);
3843 gfc_free_expr (c2->as->upper[i]);
3844 c2->as->upper[i] = e;
3846 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
3847 if (c1->initializer)
3849 c2->initializer = gfc_copy_expr (c1->initializer);
3850 gfc_insert_kind_parameter_exprs (c2->initializer);
3851 gfc_simplify_expr (c2->initializer, 1);
3855 /* Recurse into this function for PDT components. */
3856 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3857 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
3859 gfc_actual_arglist *params;
3860 /* The component in the template has a list of specification
3861 expressions derived from its declaration. */
3862 params = gfc_copy_actual_arglist (c1->param_list);
3863 actual_param = params;
3864 /* Substitute the template parameters with the expressions
3865 from the specification list. */
3866 for (;actual_param; actual_param = actual_param->next)
3867 gfc_insert_parameter_exprs (actual_param->expr,
3868 type_param_spec_list);
3870 /* Now obtain the PDT instance for the component. */
3871 old_param_spec_list = type_param_spec_list;
3872 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
3873 type_param_spec_list = old_param_spec_list;
3875 c2->param_list = params;
3876 if (!(c2->attr.pointer || c2->attr.allocatable))
3877 c2->initializer = gfc_default_initializer (&c2->ts);
3879 if (c2->attr.allocatable)
3880 instance->attr.alloc_comp = 1;
3884 gfc_commit_symbol (instance);
3885 if (ext_param_list)
3886 *ext_param_list = type_param_spec_list;
3887 *sym = instance;
3888 return m;
3890 error_return:
3891 gfc_free_actual_arglist (type_param_spec_list);
3892 return MATCH_ERROR;
3896 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
3897 structure to the matched specification. This is necessary for FUNCTION and
3898 IMPLICIT statements.
3900 If implicit_flag is nonzero, then we don't check for the optional
3901 kind specification. Not doing so is needed for matching an IMPLICIT
3902 statement correctly. */
3904 match
3905 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
3907 char name[GFC_MAX_SYMBOL_LEN + 1];
3908 gfc_symbol *sym, *dt_sym;
3909 match m;
3910 char c;
3911 bool seen_deferred_kind, matched_type;
3912 const char *dt_name;
3914 decl_type_param_list = NULL;
3916 /* A belt and braces check that the typespec is correctly being treated
3917 as a deferred characteristic association. */
3918 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
3919 && (gfc_current_block ()->result->ts.kind == -1)
3920 && (ts->kind == -1);
3921 gfc_clear_ts (ts);
3922 if (seen_deferred_kind)
3923 ts->kind = -1;
3925 /* Clear the current binding label, in case one is given. */
3926 curr_binding_label = NULL;
3928 if (gfc_match (" byte") == MATCH_YES)
3930 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
3931 return MATCH_ERROR;
3933 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
3935 gfc_error ("BYTE type used at %C "
3936 "is not available on the target machine");
3937 return MATCH_ERROR;
3940 ts->type = BT_INTEGER;
3941 ts->kind = 1;
3942 return MATCH_YES;
3946 m = gfc_match (" type (");
3947 matched_type = (m == MATCH_YES);
3948 if (matched_type)
3950 gfc_gobble_whitespace ();
3951 if (gfc_peek_ascii_char () == '*')
3953 if ((m = gfc_match ("*)")) != MATCH_YES)
3954 return m;
3955 if (gfc_comp_struct (gfc_current_state ()))
3957 gfc_error ("Assumed type at %C is not allowed for components");
3958 return MATCH_ERROR;
3960 if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
3961 return MATCH_ERROR;
3962 ts->type = BT_ASSUMED;
3963 return MATCH_YES;
3966 m = gfc_match ("%n", name);
3967 matched_type = (m == MATCH_YES);
3970 if ((matched_type && strcmp ("integer", name) == 0)
3971 || (!matched_type && gfc_match (" integer") == MATCH_YES))
3973 ts->type = BT_INTEGER;
3974 ts->kind = gfc_default_integer_kind;
3975 goto get_kind;
3978 if ((matched_type && strcmp ("character", name) == 0)
3979 || (!matched_type && gfc_match (" character") == MATCH_YES))
3981 if (matched_type
3982 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3983 "intrinsic-type-spec at %C"))
3984 return MATCH_ERROR;
3986 ts->type = BT_CHARACTER;
3987 if (implicit_flag == 0)
3988 m = gfc_match_char_spec (ts);
3989 else
3990 m = MATCH_YES;
3992 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
3993 m = MATCH_ERROR;
3995 return m;
3998 if ((matched_type && strcmp ("real", name) == 0)
3999 || (!matched_type && gfc_match (" real") == MATCH_YES))
4001 ts->type = BT_REAL;
4002 ts->kind = gfc_default_real_kind;
4003 goto get_kind;
4006 if ((matched_type
4007 && (strcmp ("doubleprecision", name) == 0
4008 || (strcmp ("double", name) == 0
4009 && gfc_match (" precision") == MATCH_YES)))
4010 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4012 if (matched_type
4013 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4014 "intrinsic-type-spec at %C"))
4015 return MATCH_ERROR;
4016 if (matched_type && gfc_match_char (')') != MATCH_YES)
4017 return MATCH_ERROR;
4019 ts->type = BT_REAL;
4020 ts->kind = gfc_default_double_kind;
4021 return MATCH_YES;
4024 if ((matched_type && strcmp ("complex", name) == 0)
4025 || (!matched_type && gfc_match (" complex") == MATCH_YES))
4027 ts->type = BT_COMPLEX;
4028 ts->kind = gfc_default_complex_kind;
4029 goto get_kind;
4032 if ((matched_type
4033 && (strcmp ("doublecomplex", name) == 0
4034 || (strcmp ("double", name) == 0
4035 && gfc_match (" complex") == MATCH_YES)))
4036 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4038 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4039 return MATCH_ERROR;
4041 if (matched_type
4042 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4043 "intrinsic-type-spec at %C"))
4044 return MATCH_ERROR;
4046 if (matched_type && gfc_match_char (')') != MATCH_YES)
4047 return MATCH_ERROR;
4049 ts->type = BT_COMPLEX;
4050 ts->kind = gfc_default_double_kind;
4051 return MATCH_YES;
4054 if ((matched_type && strcmp ("logical", name) == 0)
4055 || (!matched_type && gfc_match (" logical") == MATCH_YES))
4057 ts->type = BT_LOGICAL;
4058 ts->kind = gfc_default_logical_kind;
4059 goto get_kind;
4062 if (matched_type)
4064 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4065 if (m == MATCH_ERROR)
4066 return m;
4068 m = gfc_match_char (')');
4071 if (m != MATCH_YES)
4072 m = match_record_decl (name);
4074 if (matched_type || m == MATCH_YES)
4076 ts->type = BT_DERIVED;
4077 /* We accept record/s/ or type(s) where s is a structure, but we
4078 * don't need all the extra derived-type stuff for structures. */
4079 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4081 gfc_error ("Type name %qs at %C is ambiguous", name);
4082 return MATCH_ERROR;
4085 if (sym && sym->attr.flavor == FL_DERIVED
4086 && sym->attr.pdt_template
4087 && gfc_current_state () != COMP_DERIVED)
4089 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4090 if (m != MATCH_YES)
4091 return m;
4092 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4093 ts->u.derived = sym;
4094 strcpy (name, gfc_dt_lower_string (sym->name));
4097 if (sym && sym->attr.flavor == FL_STRUCT)
4099 ts->u.derived = sym;
4100 return MATCH_YES;
4102 /* Actually a derived type. */
4105 else
4107 /* Match nested STRUCTURE declarations; only valid within another
4108 structure declaration. */
4109 if (flag_dec_structure
4110 && (gfc_current_state () == COMP_STRUCTURE
4111 || gfc_current_state () == COMP_MAP))
4113 m = gfc_match (" structure");
4114 if (m == MATCH_YES)
4116 m = gfc_match_structure_decl ();
4117 if (m == MATCH_YES)
4119 /* gfc_new_block is updated by match_structure_decl. */
4120 ts->type = BT_DERIVED;
4121 ts->u.derived = gfc_new_block;
4122 return MATCH_YES;
4125 if (m == MATCH_ERROR)
4126 return MATCH_ERROR;
4129 /* Match CLASS declarations. */
4130 m = gfc_match (" class ( * )");
4131 if (m == MATCH_ERROR)
4132 return MATCH_ERROR;
4133 else if (m == MATCH_YES)
4135 gfc_symbol *upe;
4136 gfc_symtree *st;
4137 ts->type = BT_CLASS;
4138 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4139 if (upe == NULL)
4141 upe = gfc_new_symbol ("STAR", gfc_current_ns);
4142 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4143 st->n.sym = upe;
4144 gfc_set_sym_referenced (upe);
4145 upe->refs++;
4146 upe->ts.type = BT_VOID;
4147 upe->attr.unlimited_polymorphic = 1;
4148 /* This is essential to force the construction of
4149 unlimited polymorphic component class containers. */
4150 upe->attr.zero_comp = 1;
4151 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4152 &gfc_current_locus))
4153 return MATCH_ERROR;
4155 else
4157 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4158 st->n.sym = upe;
4159 upe->refs++;
4161 ts->u.derived = upe;
4162 return m;
4165 m = gfc_match (" class (");
4167 if (m == MATCH_YES)
4168 m = gfc_match ("%n", name);
4169 else
4170 return m;
4172 if (m != MATCH_YES)
4173 return m;
4174 ts->type = BT_CLASS;
4176 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4177 return MATCH_ERROR;
4179 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4180 if (m == MATCH_ERROR)
4181 return m;
4183 m = gfc_match_char (')');
4184 if (m != MATCH_YES)
4185 return m;
4188 /* Defer association of the derived type until the end of the
4189 specification block. However, if the derived type can be
4190 found, add it to the typespec. */
4191 if (gfc_matching_function)
4193 ts->u.derived = NULL;
4194 if (gfc_current_state () != COMP_INTERFACE
4195 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4197 sym = gfc_find_dt_in_generic (sym);
4198 ts->u.derived = sym;
4200 return MATCH_YES;
4203 /* Search for the name but allow the components to be defined later. If
4204 type = -1, this typespec has been seen in a function declaration but
4205 the type could not be accessed at that point. The actual derived type is
4206 stored in a symtree with the first letter of the name capitalized; the
4207 symtree with the all lower-case name contains the associated
4208 generic function. */
4209 dt_name = gfc_dt_upper_string (name);
4210 sym = NULL;
4211 dt_sym = NULL;
4212 if (ts->kind != -1)
4214 gfc_get_ha_symbol (name, &sym);
4215 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4217 gfc_error ("Type name %qs at %C is ambiguous", name);
4218 return MATCH_ERROR;
4220 if (sym->generic && !dt_sym)
4221 dt_sym = gfc_find_dt_in_generic (sym);
4223 /* Host associated PDTs can get confused with their constructors
4224 because they ar instantiated in the template's namespace. */
4225 if (!dt_sym)
4227 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4229 gfc_error ("Type name %qs at %C is ambiguous", name);
4230 return MATCH_ERROR;
4232 if (dt_sym && !dt_sym->attr.pdt_type)
4233 dt_sym = NULL;
4236 else if (ts->kind == -1)
4238 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4239 || gfc_current_ns->has_import_set;
4240 gfc_find_symbol (name, NULL, iface, &sym);
4241 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4243 gfc_error ("Type name %qs at %C is ambiguous", name);
4244 return MATCH_ERROR;
4246 if (sym && sym->generic && !dt_sym)
4247 dt_sym = gfc_find_dt_in_generic (sym);
4249 ts->kind = 0;
4250 if (sym == NULL)
4251 return MATCH_NO;
4254 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4255 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4256 || sym->attr.subroutine)
4258 gfc_error ("Type name %qs at %C conflicts with previously declared "
4259 "entity at %L, which has the same name", name,
4260 &sym->declared_at);
4261 return MATCH_ERROR;
4264 if (sym && sym->attr.flavor == FL_DERIVED
4265 && sym->attr.pdt_template
4266 && gfc_current_state () != COMP_DERIVED)
4268 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4269 if (m != MATCH_YES)
4270 return m;
4271 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4272 ts->u.derived = sym;
4273 strcpy (name, gfc_dt_lower_string (sym->name));
4276 gfc_save_symbol_data (sym);
4277 gfc_set_sym_referenced (sym);
4278 if (!sym->attr.generic
4279 && !gfc_add_generic (&sym->attr, sym->name, NULL))
4280 return MATCH_ERROR;
4282 if (!sym->attr.function
4283 && !gfc_add_function (&sym->attr, sym->name, NULL))
4284 return MATCH_ERROR;
4286 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4287 && dt_sym->attr.pdt_template
4288 && gfc_current_state () != COMP_DERIVED)
4290 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4291 if (m != MATCH_YES)
4292 return m;
4293 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4296 if (!dt_sym)
4298 gfc_interface *intr, *head;
4300 /* Use upper case to save the actual derived-type symbol. */
4301 gfc_get_symbol (dt_name, NULL, &dt_sym);
4302 dt_sym->name = gfc_get_string ("%s", sym->name);
4303 head = sym->generic;
4304 intr = gfc_get_interface ();
4305 intr->sym = dt_sym;
4306 intr->where = gfc_current_locus;
4307 intr->next = head;
4308 sym->generic = intr;
4309 sym->attr.if_source = IFSRC_DECL;
4311 else
4312 gfc_save_symbol_data (dt_sym);
4314 gfc_set_sym_referenced (dt_sym);
4316 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4317 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4318 return MATCH_ERROR;
4320 ts->u.derived = dt_sym;
4322 return MATCH_YES;
4324 get_kind:
4325 if (matched_type
4326 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4327 "intrinsic-type-spec at %C"))
4328 return MATCH_ERROR;
4330 /* For all types except double, derived and character, look for an
4331 optional kind specifier. MATCH_NO is actually OK at this point. */
4332 if (implicit_flag == 1)
4334 if (matched_type && gfc_match_char (')') != MATCH_YES)
4335 return MATCH_ERROR;
4337 return MATCH_YES;
4340 if (gfc_current_form == FORM_FREE)
4342 c = gfc_peek_ascii_char ();
4343 if (!gfc_is_whitespace (c) && c != '*' && c != '('
4344 && c != ':' && c != ',')
4346 if (matched_type && c == ')')
4348 gfc_next_ascii_char ();
4349 return MATCH_YES;
4351 return MATCH_NO;
4355 m = gfc_match_kind_spec (ts, false);
4356 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4358 m = gfc_match_old_kind_spec (ts);
4359 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4360 return MATCH_ERROR;
4363 if (matched_type && gfc_match_char (')') != MATCH_YES)
4364 return MATCH_ERROR;
4366 /* Defer association of the KIND expression of function results
4367 until after USE and IMPORT statements. */
4368 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4369 || gfc_matching_function)
4370 return MATCH_YES;
4372 if (m == MATCH_NO)
4373 m = MATCH_YES; /* No kind specifier found. */
4375 return m;
4379 /* Match an IMPLICIT NONE statement. Actually, this statement is
4380 already matched in parse.c, or we would not end up here in the
4381 first place. So the only thing we need to check, is if there is
4382 trailing garbage. If not, the match is successful. */
4384 match
4385 gfc_match_implicit_none (void)
4387 char c;
4388 match m;
4389 char name[GFC_MAX_SYMBOL_LEN + 1];
4390 bool type = false;
4391 bool external = false;
4392 locus cur_loc = gfc_current_locus;
4394 if (gfc_current_ns->seen_implicit_none
4395 || gfc_current_ns->has_implicit_none_export)
4397 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4398 return MATCH_ERROR;
4401 gfc_gobble_whitespace ();
4402 c = gfc_peek_ascii_char ();
4403 if (c == '(')
4405 (void) gfc_next_ascii_char ();
4406 if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
4407 return MATCH_ERROR;
4409 gfc_gobble_whitespace ();
4410 if (gfc_peek_ascii_char () == ')')
4412 (void) gfc_next_ascii_char ();
4413 type = true;
4415 else
4416 for(;;)
4418 m = gfc_match (" %n", name);
4419 if (m != MATCH_YES)
4420 return MATCH_ERROR;
4422 if (strcmp (name, "type") == 0)
4423 type = true;
4424 else if (strcmp (name, "external") == 0)
4425 external = true;
4426 else
4427 return MATCH_ERROR;
4429 gfc_gobble_whitespace ();
4430 c = gfc_next_ascii_char ();
4431 if (c == ',')
4432 continue;
4433 if (c == ')')
4434 break;
4435 return MATCH_ERROR;
4438 else
4439 type = true;
4441 if (gfc_match_eos () != MATCH_YES)
4442 return MATCH_ERROR;
4444 gfc_set_implicit_none (type, external, &cur_loc);
4446 return MATCH_YES;
4450 /* Match the letter range(s) of an IMPLICIT statement. */
4452 static match
4453 match_implicit_range (void)
4455 char c, c1, c2;
4456 int inner;
4457 locus cur_loc;
4459 cur_loc = gfc_current_locus;
4461 gfc_gobble_whitespace ();
4462 c = gfc_next_ascii_char ();
4463 if (c != '(')
4465 gfc_error ("Missing character range in IMPLICIT at %C");
4466 goto bad;
4469 inner = 1;
4470 while (inner)
4472 gfc_gobble_whitespace ();
4473 c1 = gfc_next_ascii_char ();
4474 if (!ISALPHA (c1))
4475 goto bad;
4477 gfc_gobble_whitespace ();
4478 c = gfc_next_ascii_char ();
4480 switch (c)
4482 case ')':
4483 inner = 0; /* Fall through. */
4485 case ',':
4486 c2 = c1;
4487 break;
4489 case '-':
4490 gfc_gobble_whitespace ();
4491 c2 = gfc_next_ascii_char ();
4492 if (!ISALPHA (c2))
4493 goto bad;
4495 gfc_gobble_whitespace ();
4496 c = gfc_next_ascii_char ();
4498 if ((c != ',') && (c != ')'))
4499 goto bad;
4500 if (c == ')')
4501 inner = 0;
4503 break;
4505 default:
4506 goto bad;
4509 if (c1 > c2)
4511 gfc_error ("Letters must be in alphabetic order in "
4512 "IMPLICIT statement at %C");
4513 goto bad;
4516 /* See if we can add the newly matched range to the pending
4517 implicits from this IMPLICIT statement. We do not check for
4518 conflicts with whatever earlier IMPLICIT statements may have
4519 set. This is done when we've successfully finished matching
4520 the current one. */
4521 if (!gfc_add_new_implicit_range (c1, c2))
4522 goto bad;
4525 return MATCH_YES;
4527 bad:
4528 gfc_syntax_error (ST_IMPLICIT);
4530 gfc_current_locus = cur_loc;
4531 return MATCH_ERROR;
4535 /* Match an IMPLICIT statement, storing the types for
4536 gfc_set_implicit() if the statement is accepted by the parser.
4537 There is a strange looking, but legal syntactic construction
4538 possible. It looks like:
4540 IMPLICIT INTEGER (a-b) (c-d)
4542 This is legal if "a-b" is a constant expression that happens to
4543 equal one of the legal kinds for integers. The real problem
4544 happens with an implicit specification that looks like:
4546 IMPLICIT INTEGER (a-b)
4548 In this case, a typespec matcher that is "greedy" (as most of the
4549 matchers are) gobbles the character range as a kindspec, leaving
4550 nothing left. We therefore have to go a bit more slowly in the
4551 matching process by inhibiting the kindspec checking during
4552 typespec matching and checking for a kind later. */
4554 match
4555 gfc_match_implicit (void)
4557 gfc_typespec ts;
4558 locus cur_loc;
4559 char c;
4560 match m;
4562 if (gfc_current_ns->seen_implicit_none)
4564 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4565 "statement");
4566 return MATCH_ERROR;
4569 gfc_clear_ts (&ts);
4571 /* We don't allow empty implicit statements. */
4572 if (gfc_match_eos () == MATCH_YES)
4574 gfc_error ("Empty IMPLICIT statement at %C");
4575 return MATCH_ERROR;
4580 /* First cleanup. */
4581 gfc_clear_new_implicit ();
4583 /* A basic type is mandatory here. */
4584 m = gfc_match_decl_type_spec (&ts, 1);
4585 if (m == MATCH_ERROR)
4586 goto error;
4587 if (m == MATCH_NO)
4588 goto syntax;
4590 cur_loc = gfc_current_locus;
4591 m = match_implicit_range ();
4593 if (m == MATCH_YES)
4595 /* We may have <TYPE> (<RANGE>). */
4596 gfc_gobble_whitespace ();
4597 c = gfc_peek_ascii_char ();
4598 if (c == ',' || c == '\n' || c == ';' || c == '!')
4600 /* Check for CHARACTER with no length parameter. */
4601 if (ts.type == BT_CHARACTER && !ts.u.cl)
4603 ts.kind = gfc_default_character_kind;
4604 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4605 ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4606 NULL, 1);
4609 /* Record the Successful match. */
4610 if (!gfc_merge_new_implicit (&ts))
4611 return MATCH_ERROR;
4612 if (c == ',')
4613 c = gfc_next_ascii_char ();
4614 else if (gfc_match_eos () == MATCH_ERROR)
4615 goto error;
4616 continue;
4619 gfc_current_locus = cur_loc;
4622 /* Discard the (incorrectly) matched range. */
4623 gfc_clear_new_implicit ();
4625 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4626 if (ts.type == BT_CHARACTER)
4627 m = gfc_match_char_spec (&ts);
4628 else
4630 m = gfc_match_kind_spec (&ts, false);
4631 if (m == MATCH_NO)
4633 m = gfc_match_old_kind_spec (&ts);
4634 if (m == MATCH_ERROR)
4635 goto error;
4636 if (m == MATCH_NO)
4637 goto syntax;
4640 if (m == MATCH_ERROR)
4641 goto error;
4643 m = match_implicit_range ();
4644 if (m == MATCH_ERROR)
4645 goto error;
4646 if (m == MATCH_NO)
4647 goto syntax;
4649 gfc_gobble_whitespace ();
4650 c = gfc_next_ascii_char ();
4651 if (c != ',' && gfc_match_eos () != MATCH_YES)
4652 goto syntax;
4654 if (!gfc_merge_new_implicit (&ts))
4655 return MATCH_ERROR;
4657 while (c == ',');
4659 return MATCH_YES;
4661 syntax:
4662 gfc_syntax_error (ST_IMPLICIT);
4664 error:
4665 return MATCH_ERROR;
4669 match
4670 gfc_match_import (void)
4672 char name[GFC_MAX_SYMBOL_LEN + 1];
4673 match m;
4674 gfc_symbol *sym;
4675 gfc_symtree *st;
4677 if (gfc_current_ns->proc_name == NULL
4678 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4680 gfc_error ("IMPORT statement at %C only permitted in "
4681 "an INTERFACE body");
4682 return MATCH_ERROR;
4685 if (gfc_current_ns->proc_name->attr.module_procedure)
4687 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4688 "in a module procedure interface body");
4689 return MATCH_ERROR;
4692 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4693 return MATCH_ERROR;
4695 if (gfc_match_eos () == MATCH_YES)
4697 /* All host variables should be imported. */
4698 gfc_current_ns->has_import_set = 1;
4699 return MATCH_YES;
4702 if (gfc_match (" ::") == MATCH_YES)
4704 if (gfc_match_eos () == MATCH_YES)
4706 gfc_error ("Expecting list of named entities at %C");
4707 return MATCH_ERROR;
4711 for(;;)
4713 sym = NULL;
4714 m = gfc_match (" %n", name);
4715 switch (m)
4717 case MATCH_YES:
4718 if (gfc_current_ns->parent != NULL
4719 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4721 gfc_error ("Type name %qs at %C is ambiguous", name);
4722 return MATCH_ERROR;
4724 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
4725 && gfc_find_symbol (name,
4726 gfc_current_ns->proc_name->ns->parent,
4727 1, &sym))
4729 gfc_error ("Type name %qs at %C is ambiguous", name);
4730 return MATCH_ERROR;
4733 if (sym == NULL)
4735 gfc_error ("Cannot IMPORT %qs from host scoping unit "
4736 "at %C - does not exist.", name);
4737 return MATCH_ERROR;
4740 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4742 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4743 "at %C", name);
4744 goto next_item;
4747 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4748 st->n.sym = sym;
4749 sym->refs++;
4750 sym->attr.imported = 1;
4752 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4754 /* The actual derived type is stored in a symtree with the first
4755 letter of the name capitalized; the symtree with the all
4756 lower-case name contains the associated generic function. */
4757 st = gfc_new_symtree (&gfc_current_ns->sym_root,
4758 gfc_dt_upper_string (name));
4759 st->n.sym = sym;
4760 sym->refs++;
4761 sym->attr.imported = 1;
4764 goto next_item;
4766 case MATCH_NO:
4767 break;
4769 case MATCH_ERROR:
4770 return MATCH_ERROR;
4773 next_item:
4774 if (gfc_match_eos () == MATCH_YES)
4775 break;
4776 if (gfc_match_char (',') != MATCH_YES)
4777 goto syntax;
4780 return MATCH_YES;
4782 syntax:
4783 gfc_error ("Syntax error in IMPORT statement at %C");
4784 return MATCH_ERROR;
4788 /* A minimal implementation of gfc_match without whitespace, escape
4789 characters or variable arguments. Returns true if the next
4790 characters match the TARGET template exactly. */
4792 static bool
4793 match_string_p (const char *target)
4795 const char *p;
4797 for (p = target; *p; p++)
4798 if ((char) gfc_next_ascii_char () != *p)
4799 return false;
4800 return true;
4803 /* Matches an attribute specification including array specs. If
4804 successful, leaves the variables current_attr and current_as
4805 holding the specification. Also sets the colon_seen variable for
4806 later use by matchers associated with initializations.
4808 This subroutine is a little tricky in the sense that we don't know
4809 if we really have an attr-spec until we hit the double colon.
4810 Until that time, we can only return MATCH_NO. This forces us to
4811 check for duplicate specification at this level. */
4813 static match
4814 match_attr_spec (void)
4816 /* Modifiers that can exist in a type statement. */
4817 enum
4818 { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
4819 DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
4820 DECL_DIMENSION, DECL_EXTERNAL,
4821 DECL_INTRINSIC, DECL_OPTIONAL,
4822 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
4823 DECL_STATIC, DECL_AUTOMATIC,
4824 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
4825 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
4826 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
4829 /* GFC_DECL_END is the sentinel, index starts at 0. */
4830 #define NUM_DECL GFC_DECL_END
4832 /* Make sure that values from sym_intent are safe to be used here. */
4833 gcc_assert (INTENT_IN > 0);
4835 locus start, seen_at[NUM_DECL];
4836 int seen[NUM_DECL];
4837 unsigned int d;
4838 const char *attr;
4839 match m;
4840 bool t;
4842 gfc_clear_attr (&current_attr);
4843 start = gfc_current_locus;
4845 current_as = NULL;
4846 colon_seen = 0;
4847 attr_seen = 0;
4849 /* See if we get all of the keywords up to the final double colon. */
4850 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4851 seen[d] = 0;
4853 for (;;)
4855 char ch;
4857 d = DECL_NONE;
4858 gfc_gobble_whitespace ();
4860 ch = gfc_next_ascii_char ();
4861 if (ch == ':')
4863 /* This is the successful exit condition for the loop. */
4864 if (gfc_next_ascii_char () == ':')
4865 break;
4867 else if (ch == ',')
4869 gfc_gobble_whitespace ();
4870 switch (gfc_peek_ascii_char ())
4872 case 'a':
4873 gfc_next_ascii_char ();
4874 switch (gfc_next_ascii_char ())
4876 case 'l':
4877 if (match_string_p ("locatable"))
4879 /* Matched "allocatable". */
4880 d = DECL_ALLOCATABLE;
4882 break;
4884 case 's':
4885 if (match_string_p ("ynchronous"))
4887 /* Matched "asynchronous". */
4888 d = DECL_ASYNCHRONOUS;
4890 break;
4892 case 'u':
4893 if (match_string_p ("tomatic"))
4895 /* Matched "automatic". */
4896 d = DECL_AUTOMATIC;
4898 break;
4900 break;
4902 case 'b':
4903 /* Try and match the bind(c). */
4904 m = gfc_match_bind_c (NULL, true);
4905 if (m == MATCH_YES)
4906 d = DECL_IS_BIND_C;
4907 else if (m == MATCH_ERROR)
4908 goto cleanup;
4909 break;
4911 case 'c':
4912 gfc_next_ascii_char ();
4913 if ('o' != gfc_next_ascii_char ())
4914 break;
4915 switch (gfc_next_ascii_char ())
4917 case 'd':
4918 if (match_string_p ("imension"))
4920 d = DECL_CODIMENSION;
4921 break;
4923 /* FALLTHRU */
4924 case 'n':
4925 if (match_string_p ("tiguous"))
4927 d = DECL_CONTIGUOUS;
4928 break;
4931 break;
4933 case 'd':
4934 if (match_string_p ("dimension"))
4935 d = DECL_DIMENSION;
4936 break;
4938 case 'e':
4939 if (match_string_p ("external"))
4940 d = DECL_EXTERNAL;
4941 break;
4943 case 'i':
4944 if (match_string_p ("int"))
4946 ch = gfc_next_ascii_char ();
4947 if (ch == 'e')
4949 if (match_string_p ("nt"))
4951 /* Matched "intent". */
4952 d = match_intent_spec ();
4953 if (d == INTENT_UNKNOWN)
4955 m = MATCH_ERROR;
4956 goto cleanup;
4960 else if (ch == 'r')
4962 if (match_string_p ("insic"))
4964 /* Matched "intrinsic". */
4965 d = DECL_INTRINSIC;
4969 break;
4971 case 'k':
4972 if (match_string_p ("kind"))
4973 d = DECL_KIND;
4974 break;
4976 case 'l':
4977 if (match_string_p ("len"))
4978 d = DECL_LEN;
4979 break;
4981 case 'o':
4982 if (match_string_p ("optional"))
4983 d = DECL_OPTIONAL;
4984 break;
4986 case 'p':
4987 gfc_next_ascii_char ();
4988 switch (gfc_next_ascii_char ())
4990 case 'a':
4991 if (match_string_p ("rameter"))
4993 /* Matched "parameter". */
4994 d = DECL_PARAMETER;
4996 break;
4998 case 'o':
4999 if (match_string_p ("inter"))
5001 /* Matched "pointer". */
5002 d = DECL_POINTER;
5004 break;
5006 case 'r':
5007 ch = gfc_next_ascii_char ();
5008 if (ch == 'i')
5010 if (match_string_p ("vate"))
5012 /* Matched "private". */
5013 d = DECL_PRIVATE;
5016 else if (ch == 'o')
5018 if (match_string_p ("tected"))
5020 /* Matched "protected". */
5021 d = DECL_PROTECTED;
5024 break;
5026 case 'u':
5027 if (match_string_p ("blic"))
5029 /* Matched "public". */
5030 d = DECL_PUBLIC;
5032 break;
5034 break;
5036 case 's':
5037 gfc_next_ascii_char ();
5038 switch (gfc_next_ascii_char ())
5040 case 'a':
5041 if (match_string_p ("ve"))
5043 /* Matched "save". */
5044 d = DECL_SAVE;
5046 break;
5048 case 't':
5049 if (match_string_p ("atic"))
5051 /* Matched "static". */
5052 d = DECL_STATIC;
5054 break;
5056 break;
5058 case 't':
5059 if (match_string_p ("target"))
5060 d = DECL_TARGET;
5061 break;
5063 case 'v':
5064 gfc_next_ascii_char ();
5065 ch = gfc_next_ascii_char ();
5066 if (ch == 'a')
5068 if (match_string_p ("lue"))
5070 /* Matched "value". */
5071 d = DECL_VALUE;
5074 else if (ch == 'o')
5076 if (match_string_p ("latile"))
5078 /* Matched "volatile". */
5079 d = DECL_VOLATILE;
5082 break;
5086 /* No double colon and no recognizable decl_type, so assume that
5087 we've been looking at something else the whole time. */
5088 if (d == DECL_NONE)
5090 m = MATCH_NO;
5091 goto cleanup;
5094 /* Check to make sure any parens are paired up correctly. */
5095 if (gfc_match_parens () == MATCH_ERROR)
5097 m = MATCH_ERROR;
5098 goto cleanup;
5101 seen[d]++;
5102 seen_at[d] = gfc_current_locus;
5104 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
5106 gfc_array_spec *as = NULL;
5108 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5109 d == DECL_CODIMENSION);
5111 if (current_as == NULL)
5112 current_as = as;
5113 else if (m == MATCH_YES)
5115 if (!merge_array_spec (as, current_as, false))
5116 m = MATCH_ERROR;
5117 free (as);
5120 if (m == MATCH_NO)
5122 if (d == DECL_CODIMENSION)
5123 gfc_error ("Missing codimension specification at %C");
5124 else
5125 gfc_error ("Missing dimension specification at %C");
5126 m = MATCH_ERROR;
5129 if (m == MATCH_ERROR)
5130 goto cleanup;
5134 /* Since we've seen a double colon, we have to be looking at an
5135 attr-spec. This means that we can now issue errors. */
5136 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5137 if (seen[d] > 1)
5139 switch (d)
5141 case DECL_ALLOCATABLE:
5142 attr = "ALLOCATABLE";
5143 break;
5144 case DECL_ASYNCHRONOUS:
5145 attr = "ASYNCHRONOUS";
5146 break;
5147 case DECL_CODIMENSION:
5148 attr = "CODIMENSION";
5149 break;
5150 case DECL_CONTIGUOUS:
5151 attr = "CONTIGUOUS";
5152 break;
5153 case DECL_DIMENSION:
5154 attr = "DIMENSION";
5155 break;
5156 case DECL_EXTERNAL:
5157 attr = "EXTERNAL";
5158 break;
5159 case DECL_IN:
5160 attr = "INTENT (IN)";
5161 break;
5162 case DECL_OUT:
5163 attr = "INTENT (OUT)";
5164 break;
5165 case DECL_INOUT:
5166 attr = "INTENT (IN OUT)";
5167 break;
5168 case DECL_INTRINSIC:
5169 attr = "INTRINSIC";
5170 break;
5171 case DECL_OPTIONAL:
5172 attr = "OPTIONAL";
5173 break;
5174 case DECL_KIND:
5175 attr = "KIND";
5176 break;
5177 case DECL_LEN:
5178 attr = "LEN";
5179 break;
5180 case DECL_PARAMETER:
5181 attr = "PARAMETER";
5182 break;
5183 case DECL_POINTER:
5184 attr = "POINTER";
5185 break;
5186 case DECL_PROTECTED:
5187 attr = "PROTECTED";
5188 break;
5189 case DECL_PRIVATE:
5190 attr = "PRIVATE";
5191 break;
5192 case DECL_PUBLIC:
5193 attr = "PUBLIC";
5194 break;
5195 case DECL_SAVE:
5196 attr = "SAVE";
5197 break;
5198 case DECL_STATIC:
5199 attr = "STATIC";
5200 break;
5201 case DECL_AUTOMATIC:
5202 attr = "AUTOMATIC";
5203 break;
5204 case DECL_TARGET:
5205 attr = "TARGET";
5206 break;
5207 case DECL_IS_BIND_C:
5208 attr = "IS_BIND_C";
5209 break;
5210 case DECL_VALUE:
5211 attr = "VALUE";
5212 break;
5213 case DECL_VOLATILE:
5214 attr = "VOLATILE";
5215 break;
5216 default:
5217 attr = NULL; /* This shouldn't happen. */
5220 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5221 m = MATCH_ERROR;
5222 goto cleanup;
5225 /* Now that we've dealt with duplicate attributes, add the attributes
5226 to the current attribute. */
5227 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5229 if (seen[d] == 0)
5230 continue;
5231 else
5232 attr_seen = 1;
5234 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5235 && !flag_dec_static)
5237 gfc_error ("%s at %L is a DEC extension, enable with "
5238 "%<-fdec-static%>",
5239 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5240 m = MATCH_ERROR;
5241 goto cleanup;
5243 /* Allow SAVE with STATIC, but don't complain. */
5244 if (d == DECL_STATIC && seen[DECL_SAVE])
5245 continue;
5247 if (gfc_current_state () == COMP_DERIVED
5248 && d != DECL_DIMENSION && d != DECL_CODIMENSION
5249 && d != DECL_POINTER && d != DECL_PRIVATE
5250 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
5252 if (d == DECL_ALLOCATABLE)
5254 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
5255 "attribute at %C in a TYPE definition"))
5257 m = MATCH_ERROR;
5258 goto cleanup;
5261 else if (d == DECL_KIND)
5263 if (!gfc_notify_std (GFC_STD_F2003, "KIND "
5264 "attribute at %C in a TYPE definition"))
5266 m = MATCH_ERROR;
5267 goto cleanup;
5269 if (current_ts.type != BT_INTEGER)
5271 gfc_error ("Component with KIND attribute at %C must be "
5272 "INTEGER");
5273 m = MATCH_ERROR;
5274 goto cleanup;
5276 if (current_ts.kind != gfc_default_integer_kind)
5278 gfc_error ("Component with KIND attribute at %C must be "
5279 "default integer kind (%d)",
5280 gfc_default_integer_kind);
5281 m = MATCH_ERROR;
5282 goto cleanup;
5285 else if (d == DECL_LEN)
5287 if (!gfc_notify_std (GFC_STD_F2003, "LEN "
5288 "attribute at %C in a TYPE definition"))
5290 m = MATCH_ERROR;
5291 goto cleanup;
5293 if (current_ts.type != BT_INTEGER)
5295 gfc_error ("Component with LEN attribute at %C must be "
5296 "INTEGER");
5297 m = MATCH_ERROR;
5298 goto cleanup;
5300 if (current_ts.kind != gfc_default_integer_kind)
5302 gfc_error ("Component with LEN attribute at %C must be "
5303 "default integer kind (%d)",
5304 gfc_default_integer_kind);
5305 m = MATCH_ERROR;
5306 goto cleanup;
5309 else
5311 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
5312 &seen_at[d]);
5313 m = MATCH_ERROR;
5314 goto cleanup;
5318 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5319 && gfc_current_state () != COMP_MODULE)
5321 if (d == DECL_PRIVATE)
5322 attr = "PRIVATE";
5323 else
5324 attr = "PUBLIC";
5325 if (gfc_current_state () == COMP_DERIVED
5326 && gfc_state_stack->previous
5327 && gfc_state_stack->previous->state == COMP_MODULE)
5329 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5330 "at %L in a TYPE definition", attr,
5331 &seen_at[d]))
5333 m = MATCH_ERROR;
5334 goto cleanup;
5337 else
5339 gfc_error ("%s attribute at %L is not allowed outside of the "
5340 "specification part of a module", attr, &seen_at[d]);
5341 m = MATCH_ERROR;
5342 goto cleanup;
5346 if (gfc_current_state () != COMP_DERIVED
5347 && (d == DECL_KIND || d == DECL_LEN))
5349 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5350 "definition", &seen_at[d]);
5351 m = MATCH_ERROR;
5352 goto cleanup;
5355 switch (d)
5357 case DECL_ALLOCATABLE:
5358 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5359 break;
5361 case DECL_ASYNCHRONOUS:
5362 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5363 t = false;
5364 else
5365 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5366 break;
5368 case DECL_CODIMENSION:
5369 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5370 break;
5372 case DECL_CONTIGUOUS:
5373 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5374 t = false;
5375 else
5376 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5377 break;
5379 case DECL_DIMENSION:
5380 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5381 break;
5383 case DECL_EXTERNAL:
5384 t = gfc_add_external (&current_attr, &seen_at[d]);
5385 break;
5387 case DECL_IN:
5388 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5389 break;
5391 case DECL_OUT:
5392 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5393 break;
5395 case DECL_INOUT:
5396 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5397 break;
5399 case DECL_INTRINSIC:
5400 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5401 break;
5403 case DECL_OPTIONAL:
5404 t = gfc_add_optional (&current_attr, &seen_at[d]);
5405 break;
5407 case DECL_KIND:
5408 t = gfc_add_kind (&current_attr, &seen_at[d]);
5409 break;
5411 case DECL_LEN:
5412 t = gfc_add_len (&current_attr, &seen_at[d]);
5413 break;
5415 case DECL_PARAMETER:
5416 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5417 break;
5419 case DECL_POINTER:
5420 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5421 break;
5423 case DECL_PROTECTED:
5424 if (gfc_current_state () != COMP_MODULE
5425 || (gfc_current_ns->proc_name
5426 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5428 gfc_error ("PROTECTED at %C only allowed in specification "
5429 "part of a module");
5430 t = false;
5431 break;
5434 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5435 t = false;
5436 else
5437 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5438 break;
5440 case DECL_PRIVATE:
5441 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5442 &seen_at[d]);
5443 break;
5445 case DECL_PUBLIC:
5446 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5447 &seen_at[d]);
5448 break;
5450 case DECL_STATIC:
5451 case DECL_SAVE:
5452 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5453 break;
5455 case DECL_AUTOMATIC:
5456 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5457 break;
5459 case DECL_TARGET:
5460 t = gfc_add_target (&current_attr, &seen_at[d]);
5461 break;
5463 case DECL_IS_BIND_C:
5464 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5465 break;
5467 case DECL_VALUE:
5468 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5469 t = false;
5470 else
5471 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5472 break;
5474 case DECL_VOLATILE:
5475 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5476 t = false;
5477 else
5478 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5479 break;
5481 default:
5482 gfc_internal_error ("match_attr_spec(): Bad attribute");
5485 if (!t)
5487 m = MATCH_ERROR;
5488 goto cleanup;
5492 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
5493 if ((gfc_current_state () == COMP_MODULE
5494 || gfc_current_state () == COMP_SUBMODULE)
5495 && !current_attr.save
5496 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5497 current_attr.save = SAVE_IMPLICIT;
5499 colon_seen = 1;
5500 return MATCH_YES;
5502 cleanup:
5503 gfc_current_locus = start;
5504 gfc_free_array_spec (current_as);
5505 current_as = NULL;
5506 attr_seen = 0;
5507 return m;
5511 /* Set the binding label, dest_label, either with the binding label
5512 stored in the given gfc_typespec, ts, or if none was provided, it
5513 will be the symbol name in all lower case, as required by the draft
5514 (J3/04-007, section 15.4.1). If a binding label was given and
5515 there is more than one argument (num_idents), it is an error. */
5517 static bool
5518 set_binding_label (const char **dest_label, const char *sym_name,
5519 int num_idents)
5521 if (num_idents > 1 && has_name_equals)
5523 gfc_error ("Multiple identifiers provided with "
5524 "single NAME= specifier at %C");
5525 return false;
5528 if (curr_binding_label)
5529 /* Binding label given; store in temp holder till have sym. */
5530 *dest_label = curr_binding_label;
5531 else
5533 /* No binding label given, and the NAME= specifier did not exist,
5534 which means there was no NAME="". */
5535 if (sym_name != NULL && has_name_equals == 0)
5536 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5539 return true;
5543 /* Set the status of the given common block as being BIND(C) or not,
5544 depending on the given parameter, is_bind_c. */
5546 void
5547 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5549 com_block->is_bind_c = is_bind_c;
5550 return;
5554 /* Verify that the given gfc_typespec is for a C interoperable type. */
5556 bool
5557 gfc_verify_c_interop (gfc_typespec *ts)
5559 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5560 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5561 ? true : false;
5562 else if (ts->type == BT_CLASS)
5563 return false;
5564 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5565 return false;
5567 return true;
5571 /* Verify that the variables of a given common block, which has been
5572 defined with the attribute specifier bind(c), to be of a C
5573 interoperable type. Errors will be reported here, if
5574 encountered. */
5576 bool
5577 verify_com_block_vars_c_interop (gfc_common_head *com_block)
5579 gfc_symbol *curr_sym = NULL;
5580 bool retval = true;
5582 curr_sym = com_block->head;
5584 /* Make sure we have at least one symbol. */
5585 if (curr_sym == NULL)
5586 return retval;
5588 /* Here we know we have a symbol, so we'll execute this loop
5589 at least once. */
5592 /* The second to last param, 1, says this is in a common block. */
5593 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5594 curr_sym = curr_sym->common_next;
5595 } while (curr_sym != NULL);
5597 return retval;
5601 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5602 an appropriate error message is reported. */
5604 bool
5605 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5606 int is_in_common, gfc_common_head *com_block)
5608 bool bind_c_function = false;
5609 bool retval = true;
5611 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5612 bind_c_function = true;
5614 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5616 tmp_sym = tmp_sym->result;
5617 /* Make sure it wasn't an implicitly typed result. */
5618 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5620 gfc_warning (OPT_Wc_binding_type,
5621 "Implicitly declared BIND(C) function %qs at "
5622 "%L may not be C interoperable", tmp_sym->name,
5623 &tmp_sym->declared_at);
5624 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5625 /* Mark it as C interoperable to prevent duplicate warnings. */
5626 tmp_sym->ts.is_c_interop = 1;
5627 tmp_sym->attr.is_c_interop = 1;
5631 /* Here, we know we have the bind(c) attribute, so if we have
5632 enough type info, then verify that it's a C interop kind.
5633 The info could be in the symbol already, or possibly still in
5634 the given ts (current_ts), so look in both. */
5635 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5637 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5639 /* See if we're dealing with a sym in a common block or not. */
5640 if (is_in_common == 1 && warn_c_binding_type)
5642 gfc_warning (OPT_Wc_binding_type,
5643 "Variable %qs in common block %qs at %L "
5644 "may not be a C interoperable "
5645 "kind though common block %qs is BIND(C)",
5646 tmp_sym->name, com_block->name,
5647 &(tmp_sym->declared_at), com_block->name);
5649 else
5651 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5652 gfc_error ("Type declaration %qs at %L is not C "
5653 "interoperable but it is BIND(C)",
5654 tmp_sym->name, &(tmp_sym->declared_at));
5655 else if (warn_c_binding_type)
5656 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5657 "may not be a C interoperable "
5658 "kind but it is BIND(C)",
5659 tmp_sym->name, &(tmp_sym->declared_at));
5663 /* Variables declared w/in a common block can't be bind(c)
5664 since there's no way for C to see these variables, so there's
5665 semantically no reason for the attribute. */
5666 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5668 gfc_error ("Variable %qs in common block %qs at "
5669 "%L cannot be declared with BIND(C) "
5670 "since it is not a global",
5671 tmp_sym->name, com_block->name,
5672 &(tmp_sym->declared_at));
5673 retval = false;
5676 /* Scalar variables that are bind(c) can not have the pointer
5677 or allocatable attributes. */
5678 if (tmp_sym->attr.is_bind_c == 1)
5680 if (tmp_sym->attr.pointer == 1)
5682 gfc_error ("Variable %qs at %L cannot have both the "
5683 "POINTER and BIND(C) attributes",
5684 tmp_sym->name, &(tmp_sym->declared_at));
5685 retval = false;
5688 if (tmp_sym->attr.allocatable == 1)
5690 gfc_error ("Variable %qs at %L cannot have both the "
5691 "ALLOCATABLE and BIND(C) attributes",
5692 tmp_sym->name, &(tmp_sym->declared_at));
5693 retval = false;
5698 /* If it is a BIND(C) function, make sure the return value is a
5699 scalar value. The previous tests in this function made sure
5700 the type is interoperable. */
5701 if (bind_c_function && tmp_sym->as != NULL)
5702 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5703 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5705 /* BIND(C) functions can not return a character string. */
5706 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5707 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5708 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5709 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5710 gfc_error ("Return type of BIND(C) function %qs of character "
5711 "type at %L must have length 1", tmp_sym->name,
5712 &(tmp_sym->declared_at));
5715 /* See if the symbol has been marked as private. If it has, make sure
5716 there is no binding label and warn the user if there is one. */
5717 if (tmp_sym->attr.access == ACCESS_PRIVATE
5718 && tmp_sym->binding_label)
5719 /* Use gfc_warning_now because we won't say that the symbol fails
5720 just because of this. */
5721 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5722 "given the binding label %qs", tmp_sym->name,
5723 &(tmp_sym->declared_at), tmp_sym->binding_label);
5725 return retval;
5729 /* Set the appropriate fields for a symbol that's been declared as
5730 BIND(C) (the is_bind_c flag and the binding label), and verify that
5731 the type is C interoperable. Errors are reported by the functions
5732 used to set/test these fields. */
5734 bool
5735 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5737 bool retval = true;
5739 /* TODO: Do we need to make sure the vars aren't marked private? */
5741 /* Set the is_bind_c bit in symbol_attribute. */
5742 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5744 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5745 return false;
5747 return retval;
5751 /* Set the fields marking the given common block as BIND(C), including
5752 a binding label, and report any errors encountered. */
5754 bool
5755 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5757 bool retval = true;
5759 /* destLabel, common name, typespec (which may have binding label). */
5760 if (!set_binding_label (&com_block->binding_label, com_block->name,
5761 num_idents))
5762 return false;
5764 /* Set the given common block (com_block) to being bind(c) (1). */
5765 set_com_block_bind_c (com_block, 1);
5767 return retval;
5771 /* Retrieve the list of one or more identifiers that the given bind(c)
5772 attribute applies to. */
5774 bool
5775 get_bind_c_idents (void)
5777 char name[GFC_MAX_SYMBOL_LEN + 1];
5778 int num_idents = 0;
5779 gfc_symbol *tmp_sym = NULL;
5780 match found_id;
5781 gfc_common_head *com_block = NULL;
5783 if (gfc_match_name (name) == MATCH_YES)
5785 found_id = MATCH_YES;
5786 gfc_get_ha_symbol (name, &tmp_sym);
5788 else if (match_common_name (name) == MATCH_YES)
5790 found_id = MATCH_YES;
5791 com_block = gfc_get_common (name, 0);
5793 else
5795 gfc_error ("Need either entity or common block name for "
5796 "attribute specification statement at %C");
5797 return false;
5800 /* Save the current identifier and look for more. */
5803 /* Increment the number of identifiers found for this spec stmt. */
5804 num_idents++;
5806 /* Make sure we have a sym or com block, and verify that it can
5807 be bind(c). Set the appropriate field(s) and look for more
5808 identifiers. */
5809 if (tmp_sym != NULL || com_block != NULL)
5811 if (tmp_sym != NULL)
5813 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
5814 return false;
5816 else
5818 if (!set_verify_bind_c_com_block (com_block, num_idents))
5819 return false;
5822 /* Look to see if we have another identifier. */
5823 tmp_sym = NULL;
5824 if (gfc_match_eos () == MATCH_YES)
5825 found_id = MATCH_NO;
5826 else if (gfc_match_char (',') != MATCH_YES)
5827 found_id = MATCH_NO;
5828 else if (gfc_match_name (name) == MATCH_YES)
5830 found_id = MATCH_YES;
5831 gfc_get_ha_symbol (name, &tmp_sym);
5833 else if (match_common_name (name) == MATCH_YES)
5835 found_id = MATCH_YES;
5836 com_block = gfc_get_common (name, 0);
5838 else
5840 gfc_error ("Missing entity or common block name for "
5841 "attribute specification statement at %C");
5842 return false;
5845 else
5847 gfc_internal_error ("Missing symbol");
5849 } while (found_id == MATCH_YES);
5851 /* if we get here we were successful */
5852 return true;
5856 /* Try and match a BIND(C) attribute specification statement. */
5858 match
5859 gfc_match_bind_c_stmt (void)
5861 match found_match = MATCH_NO;
5862 gfc_typespec *ts;
5864 ts = &current_ts;
5866 /* This may not be necessary. */
5867 gfc_clear_ts (ts);
5868 /* Clear the temporary binding label holder. */
5869 curr_binding_label = NULL;
5871 /* Look for the bind(c). */
5872 found_match = gfc_match_bind_c (NULL, true);
5874 if (found_match == MATCH_YES)
5876 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
5877 return MATCH_ERROR;
5879 /* Look for the :: now, but it is not required. */
5880 gfc_match (" :: ");
5882 /* Get the identifier(s) that needs to be updated. This may need to
5883 change to hand the flag(s) for the attr specified so all identifiers
5884 found can have all appropriate parts updated (assuming that the same
5885 spec stmt can have multiple attrs, such as both bind(c) and
5886 allocatable...). */
5887 if (!get_bind_c_idents ())
5888 /* Error message should have printed already. */
5889 return MATCH_ERROR;
5892 return found_match;
5896 /* Match a data declaration statement. */
5898 match
5899 gfc_match_data_decl (void)
5901 gfc_symbol *sym;
5902 match m;
5903 int elem;
5905 type_param_spec_list = NULL;
5906 decl_type_param_list = NULL;
5908 num_idents_on_line = 0;
5910 m = gfc_match_decl_type_spec (&current_ts, 0);
5911 if (m != MATCH_YES)
5912 return m;
5914 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5915 && !gfc_comp_struct (gfc_current_state ()))
5917 sym = gfc_use_derived (current_ts.u.derived);
5919 if (sym == NULL)
5921 m = MATCH_ERROR;
5922 goto cleanup;
5925 current_ts.u.derived = sym;
5928 m = match_attr_spec ();
5929 if (m == MATCH_ERROR)
5931 m = MATCH_NO;
5932 goto cleanup;
5935 if (current_ts.type == BT_CLASS
5936 && current_ts.u.derived->attr.unlimited_polymorphic)
5937 goto ok;
5939 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5940 && current_ts.u.derived->components == NULL
5941 && !current_ts.u.derived->attr.zero_comp)
5944 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
5945 goto ok;
5947 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
5948 goto ok;
5950 gfc_find_symbol (current_ts.u.derived->name,
5951 current_ts.u.derived->ns, 1, &sym);
5953 /* Any symbol that we find had better be a type definition
5954 which has its components defined, or be a structure definition
5955 actively being parsed. */
5956 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
5957 && (current_ts.u.derived->components != NULL
5958 || current_ts.u.derived->attr.zero_comp
5959 || current_ts.u.derived == gfc_new_block))
5960 goto ok;
5962 gfc_error ("Derived type at %C has not been previously defined "
5963 "and so cannot appear in a derived type definition");
5964 m = MATCH_ERROR;
5965 goto cleanup;
5969 /* If we have an old-style character declaration, and no new-style
5970 attribute specifications, then there a comma is optional between
5971 the type specification and the variable list. */
5972 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
5973 gfc_match_char (',');
5975 /* Give the types/attributes to symbols that follow. Give the element
5976 a number so that repeat character length expressions can be copied. */
5977 elem = 1;
5978 for (;;)
5980 num_idents_on_line++;
5981 m = variable_decl (elem++);
5982 if (m == MATCH_ERROR)
5983 goto cleanup;
5984 if (m == MATCH_NO)
5985 break;
5987 if (gfc_match_eos () == MATCH_YES)
5988 goto cleanup;
5989 if (gfc_match_char (',') != MATCH_YES)
5990 break;
5993 if (!gfc_error_flag_test ())
5995 /* An anonymous structure declaration is unambiguous; if we matched one
5996 according to gfc_match_structure_decl, we need to return MATCH_YES
5997 here to avoid confusing the remaining matchers, even if there was an
5998 error during variable_decl. We must flush any such errors. Note this
5999 causes the parser to gracefully continue parsing the remaining input
6000 as a structure body, which likely follows. */
6001 if (current_ts.type == BT_DERIVED && current_ts.u.derived
6002 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6004 gfc_error_now ("Syntax error in anonymous structure declaration"
6005 " at %C");
6006 /* Skip the bad variable_decl and line up for the start of the
6007 structure body. */
6008 gfc_error_recovery ();
6009 m = MATCH_YES;
6010 goto cleanup;
6013 gfc_error ("Syntax error in data declaration at %C");
6016 m = MATCH_ERROR;
6018 gfc_free_data_all (gfc_current_ns);
6020 cleanup:
6021 if (saved_kind_expr)
6022 gfc_free_expr (saved_kind_expr);
6023 if (type_param_spec_list)
6024 gfc_free_actual_arglist (type_param_spec_list);
6025 if (decl_type_param_list)
6026 gfc_free_actual_arglist (decl_type_param_list);
6027 saved_kind_expr = NULL;
6028 gfc_free_array_spec (current_as);
6029 current_as = NULL;
6030 return m;
6034 /* Match a prefix associated with a function or subroutine
6035 declaration. If the typespec pointer is nonnull, then a typespec
6036 can be matched. Note that if nothing matches, MATCH_YES is
6037 returned (the null string was matched). */
6039 match
6040 gfc_match_prefix (gfc_typespec *ts)
6042 bool seen_type;
6043 bool seen_impure;
6044 bool found_prefix;
6046 gfc_clear_attr (&current_attr);
6047 seen_type = false;
6048 seen_impure = false;
6050 gcc_assert (!gfc_matching_prefix);
6051 gfc_matching_prefix = true;
6055 found_prefix = false;
6057 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6058 corresponding attribute seems natural and distinguishes these
6059 procedures from procedure types of PROC_MODULE, which these are
6060 as well. */
6061 if (gfc_match ("module% ") == MATCH_YES)
6063 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6064 goto error;
6066 current_attr.module_procedure = 1;
6067 found_prefix = true;
6070 if (!seen_type && ts != NULL
6071 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
6072 && gfc_match_space () == MATCH_YES)
6075 seen_type = true;
6076 found_prefix = true;
6079 if (gfc_match ("elemental% ") == MATCH_YES)
6081 if (!gfc_add_elemental (&current_attr, NULL))
6082 goto error;
6084 found_prefix = true;
6087 if (gfc_match ("pure% ") == MATCH_YES)
6089 if (!gfc_add_pure (&current_attr, NULL))
6090 goto error;
6092 found_prefix = true;
6095 if (gfc_match ("recursive% ") == MATCH_YES)
6097 if (!gfc_add_recursive (&current_attr, NULL))
6098 goto error;
6100 found_prefix = true;
6103 /* IMPURE is a somewhat special case, as it needs not set an actual
6104 attribute but rather only prevents ELEMENTAL routines from being
6105 automatically PURE. */
6106 if (gfc_match ("impure% ") == MATCH_YES)
6108 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
6109 goto error;
6111 seen_impure = true;
6112 found_prefix = true;
6115 while (found_prefix);
6117 /* IMPURE and PURE must not both appear, of course. */
6118 if (seen_impure && current_attr.pure)
6120 gfc_error ("PURE and IMPURE must not appear both at %C");
6121 goto error;
6124 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6125 if (!seen_impure && current_attr.elemental && !current_attr.pure)
6127 if (!gfc_add_pure (&current_attr, NULL))
6128 goto error;
6131 /* At this point, the next item is not a prefix. */
6132 gcc_assert (gfc_matching_prefix);
6134 gfc_matching_prefix = false;
6135 return MATCH_YES;
6137 error:
6138 gcc_assert (gfc_matching_prefix);
6139 gfc_matching_prefix = false;
6140 return MATCH_ERROR;
6144 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6146 static bool
6147 copy_prefix (symbol_attribute *dest, locus *where)
6149 if (dest->module_procedure)
6151 if (current_attr.elemental)
6152 dest->elemental = 1;
6154 if (current_attr.pure)
6155 dest->pure = 1;
6157 if (current_attr.recursive)
6158 dest->recursive = 1;
6160 /* Module procedures are unusual in that the 'dest' is copied from
6161 the interface declaration. However, this is an oportunity to
6162 check that the submodule declaration is compliant with the
6163 interface. */
6164 if (dest->elemental && !current_attr.elemental)
6166 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6167 "missing at %L", where);
6168 return false;
6171 if (dest->pure && !current_attr.pure)
6173 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6174 "missing at %L", where);
6175 return false;
6178 if (dest->recursive && !current_attr.recursive)
6180 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6181 "missing at %L", where);
6182 return false;
6185 return true;
6188 if (current_attr.elemental && !gfc_add_elemental (dest, where))
6189 return false;
6191 if (current_attr.pure && !gfc_add_pure (dest, where))
6192 return false;
6194 if (current_attr.recursive && !gfc_add_recursive (dest, where))
6195 return false;
6197 return true;
6201 /* Match a formal argument list or, if typeparam is true, a
6202 type_param_name_list. */
6204 match
6205 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6206 int null_flag, bool typeparam)
6208 gfc_formal_arglist *head, *tail, *p, *q;
6209 char name[GFC_MAX_SYMBOL_LEN + 1];
6210 gfc_symbol *sym;
6211 match m;
6212 gfc_formal_arglist *formal = NULL;
6214 head = tail = NULL;
6216 /* Keep the interface formal argument list and null it so that the
6217 matching for the new declaration can be done. The numbers and
6218 names of the arguments are checked here. The interface formal
6219 arguments are retained in formal_arglist and the characteristics
6220 are compared in resolve.c(resolve_fl_procedure). See the remark
6221 in get_proc_name about the eventual need to copy the formal_arglist
6222 and populate the formal namespace of the interface symbol. */
6223 if (progname->attr.module_procedure
6224 && progname->attr.host_assoc)
6226 formal = progname->formal;
6227 progname->formal = NULL;
6230 if (gfc_match_char ('(') != MATCH_YES)
6232 if (null_flag)
6233 goto ok;
6234 return MATCH_NO;
6237 if (gfc_match_char (')') == MATCH_YES)
6238 goto ok;
6240 for (;;)
6242 if (gfc_match_char ('*') == MATCH_YES)
6244 sym = NULL;
6245 if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6246 "Alternate-return argument at %C"))
6248 m = MATCH_ERROR;
6249 goto cleanup;
6251 else if (typeparam)
6252 gfc_error_now ("A parameter name is required at %C");
6254 else
6256 m = gfc_match_name (name);
6257 if (m != MATCH_YES)
6259 if(typeparam)
6260 gfc_error_now ("A parameter name is required at %C");
6261 goto cleanup;
6264 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6265 goto cleanup;
6266 else if (typeparam
6267 && gfc_get_symbol (name, progname->f2k_derived, &sym))
6268 goto cleanup;
6271 p = gfc_get_formal_arglist ();
6273 if (head == NULL)
6274 head = tail = p;
6275 else
6277 tail->next = p;
6278 tail = p;
6281 tail->sym = sym;
6283 /* We don't add the VARIABLE flavor because the name could be a
6284 dummy procedure. We don't apply these attributes to formal
6285 arguments of statement functions. */
6286 if (sym != NULL && !st_flag
6287 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6288 || !gfc_missing_attr (&sym->attr, NULL)))
6290 m = MATCH_ERROR;
6291 goto cleanup;
6294 /* The name of a program unit can be in a different namespace,
6295 so check for it explicitly. After the statement is accepted,
6296 the name is checked for especially in gfc_get_symbol(). */
6297 if (gfc_new_block != NULL && sym != NULL && !typeparam
6298 && strcmp (sym->name, gfc_new_block->name) == 0)
6300 gfc_error ("Name %qs at %C is the name of the procedure",
6301 sym->name);
6302 m = MATCH_ERROR;
6303 goto cleanup;
6306 if (gfc_match_char (')') == MATCH_YES)
6307 goto ok;
6309 m = gfc_match_char (',');
6310 if (m != MATCH_YES)
6312 if (typeparam)
6313 gfc_error_now ("Expected parameter list in type declaration "
6314 "at %C");
6315 else
6316 gfc_error ("Unexpected junk in formal argument list at %C");
6317 goto cleanup;
6322 /* Check for duplicate symbols in the formal argument list. */
6323 if (head != NULL)
6325 for (p = head; p->next; p = p->next)
6327 if (p->sym == NULL)
6328 continue;
6330 for (q = p->next; q; q = q->next)
6331 if (p->sym == q->sym)
6333 if (typeparam)
6334 gfc_error_now ("Duplicate name %qs in parameter "
6335 "list at %C", p->sym->name);
6336 else
6337 gfc_error ("Duplicate symbol %qs in formal argument "
6338 "list at %C", p->sym->name);
6340 m = MATCH_ERROR;
6341 goto cleanup;
6346 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6348 m = MATCH_ERROR;
6349 goto cleanup;
6352 /* gfc_error_now used in following and return with MATCH_YES because
6353 doing otherwise results in a cascade of extraneous errors and in
6354 some cases an ICE in symbol.c(gfc_release_symbol). */
6355 if (progname->attr.module_procedure && progname->attr.host_assoc)
6357 bool arg_count_mismatch = false;
6359 if (!formal && head)
6360 arg_count_mismatch = true;
6362 /* Abbreviated module procedure declaration is not meant to have any
6363 formal arguments! */
6364 if (!progname->abr_modproc_decl && formal && !head)
6365 arg_count_mismatch = true;
6367 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6369 if ((p->next != NULL && q->next == NULL)
6370 || (p->next == NULL && q->next != NULL))
6371 arg_count_mismatch = true;
6372 else if ((p->sym == NULL && q->sym == NULL)
6373 || strcmp (p->sym->name, q->sym->name) == 0)
6374 continue;
6375 else
6376 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6377 "argument names (%s/%s) at %C",
6378 p->sym->name, q->sym->name);
6381 if (arg_count_mismatch)
6382 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6383 "formal arguments at %C");
6386 return MATCH_YES;
6388 cleanup:
6389 gfc_free_formal_arglist (head);
6390 return m;
6394 /* Match a RESULT specification following a function declaration or
6395 ENTRY statement. Also matches the end-of-statement. */
6397 static match
6398 match_result (gfc_symbol *function, gfc_symbol **result)
6400 char name[GFC_MAX_SYMBOL_LEN + 1];
6401 gfc_symbol *r;
6402 match m;
6404 if (gfc_match (" result (") != MATCH_YES)
6405 return MATCH_NO;
6407 m = gfc_match_name (name);
6408 if (m != MATCH_YES)
6409 return m;
6411 /* Get the right paren, and that's it because there could be the
6412 bind(c) attribute after the result clause. */
6413 if (gfc_match_char (')') != MATCH_YES)
6415 /* TODO: should report the missing right paren here. */
6416 return MATCH_ERROR;
6419 if (strcmp (function->name, name) == 0)
6421 gfc_error ("RESULT variable at %C must be different than function name");
6422 return MATCH_ERROR;
6425 if (gfc_get_symbol (name, NULL, &r))
6426 return MATCH_ERROR;
6428 if (!gfc_add_result (&r->attr, r->name, NULL))
6429 return MATCH_ERROR;
6431 *result = r;
6433 return MATCH_YES;
6437 /* Match a function suffix, which could be a combination of a result
6438 clause and BIND(C), either one, or neither. The draft does not
6439 require them to come in a specific order. */
6441 match
6442 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6444 match is_bind_c; /* Found bind(c). */
6445 match is_result; /* Found result clause. */
6446 match found_match; /* Status of whether we've found a good match. */
6447 char peek_char; /* Character we're going to peek at. */
6448 bool allow_binding_name;
6450 /* Initialize to having found nothing. */
6451 found_match = MATCH_NO;
6452 is_bind_c = MATCH_NO;
6453 is_result = MATCH_NO;
6455 /* Get the next char to narrow between result and bind(c). */
6456 gfc_gobble_whitespace ();
6457 peek_char = gfc_peek_ascii_char ();
6459 /* C binding names are not allowed for internal procedures. */
6460 if (gfc_current_state () == COMP_CONTAINS
6461 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6462 allow_binding_name = false;
6463 else
6464 allow_binding_name = true;
6466 switch (peek_char)
6468 case 'r':
6469 /* Look for result clause. */
6470 is_result = match_result (sym, result);
6471 if (is_result == MATCH_YES)
6473 /* Now see if there is a bind(c) after it. */
6474 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6475 /* We've found the result clause and possibly bind(c). */
6476 found_match = MATCH_YES;
6478 else
6479 /* This should only be MATCH_ERROR. */
6480 found_match = is_result;
6481 break;
6482 case 'b':
6483 /* Look for bind(c) first. */
6484 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6485 if (is_bind_c == MATCH_YES)
6487 /* Now see if a result clause followed it. */
6488 is_result = match_result (sym, result);
6489 found_match = MATCH_YES;
6491 else
6493 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6494 found_match = MATCH_ERROR;
6496 break;
6497 default:
6498 gfc_error ("Unexpected junk after function declaration at %C");
6499 found_match = MATCH_ERROR;
6500 break;
6503 if (is_bind_c == MATCH_YES)
6505 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
6506 if (gfc_current_state () == COMP_CONTAINS
6507 && sym->ns->proc_name->attr.flavor != FL_MODULE
6508 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6509 "at %L may not be specified for an internal "
6510 "procedure", &gfc_current_locus))
6511 return MATCH_ERROR;
6513 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6514 return MATCH_ERROR;
6517 return found_match;
6521 /* Procedure pointer return value without RESULT statement:
6522 Add "hidden" result variable named "ppr@". */
6524 static bool
6525 add_hidden_procptr_result (gfc_symbol *sym)
6527 bool case1,case2;
6529 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6530 return false;
6532 /* First usage case: PROCEDURE and EXTERNAL statements. */
6533 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6534 && strcmp (gfc_current_block ()->name, sym->name) == 0
6535 && sym->attr.external;
6536 /* Second usage case: INTERFACE statements. */
6537 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6538 && gfc_state_stack->previous->state == COMP_FUNCTION
6539 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6541 if (case1 || case2)
6543 gfc_symtree *stree;
6544 if (case1)
6545 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6546 else
6548 gfc_symtree *st2;
6549 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6550 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6551 st2->n.sym = stree->n.sym;
6552 stree->n.sym->refs++;
6554 sym->result = stree->n.sym;
6556 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6557 sym->result->attr.pointer = sym->attr.pointer;
6558 sym->result->attr.external = sym->attr.external;
6559 sym->result->attr.referenced = sym->attr.referenced;
6560 sym->result->ts = sym->ts;
6561 sym->attr.proc_pointer = 0;
6562 sym->attr.pointer = 0;
6563 sym->attr.external = 0;
6564 if (sym->result->attr.external && sym->result->attr.pointer)
6566 sym->result->attr.pointer = 0;
6567 sym->result->attr.proc_pointer = 1;
6570 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6572 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6573 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6574 && sym->result && sym->result != sym && sym->result->attr.external
6575 && sym == gfc_current_ns->proc_name
6576 && sym == sym->result->ns->proc_name
6577 && strcmp ("ppr@", sym->result->name) == 0)
6579 sym->result->attr.proc_pointer = 1;
6580 sym->attr.pointer = 0;
6581 return true;
6583 else
6584 return false;
6588 /* Match the interface for a PROCEDURE declaration,
6589 including brackets (R1212). */
6591 static match
6592 match_procedure_interface (gfc_symbol **proc_if)
6594 match m;
6595 gfc_symtree *st;
6596 locus old_loc, entry_loc;
6597 gfc_namespace *old_ns = gfc_current_ns;
6598 char name[GFC_MAX_SYMBOL_LEN + 1];
6600 old_loc = entry_loc = gfc_current_locus;
6601 gfc_clear_ts (&current_ts);
6603 if (gfc_match (" (") != MATCH_YES)
6605 gfc_current_locus = entry_loc;
6606 return MATCH_NO;
6609 /* Get the type spec. for the procedure interface. */
6610 old_loc = gfc_current_locus;
6611 m = gfc_match_decl_type_spec (&current_ts, 0);
6612 gfc_gobble_whitespace ();
6613 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6614 goto got_ts;
6616 if (m == MATCH_ERROR)
6617 return m;
6619 /* Procedure interface is itself a procedure. */
6620 gfc_current_locus = old_loc;
6621 m = gfc_match_name (name);
6623 /* First look to see if it is already accessible in the current
6624 namespace because it is use associated or contained. */
6625 st = NULL;
6626 if (gfc_find_sym_tree (name, NULL, 0, &st))
6627 return MATCH_ERROR;
6629 /* If it is still not found, then try the parent namespace, if it
6630 exists and create the symbol there if it is still not found. */
6631 if (gfc_current_ns->parent)
6632 gfc_current_ns = gfc_current_ns->parent;
6633 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6634 return MATCH_ERROR;
6636 gfc_current_ns = old_ns;
6637 *proc_if = st->n.sym;
6639 if (*proc_if)
6641 (*proc_if)->refs++;
6642 /* Resolve interface if possible. That way, attr.procedure is only set
6643 if it is declared by a later procedure-declaration-stmt, which is
6644 invalid per F08:C1216 (cf. resolve_procedure_interface). */
6645 while ((*proc_if)->ts.interface
6646 && *proc_if != (*proc_if)->ts.interface)
6647 *proc_if = (*proc_if)->ts.interface;
6649 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6650 && (*proc_if)->ts.type == BT_UNKNOWN
6651 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6652 (*proc_if)->name, NULL))
6653 return MATCH_ERROR;
6656 got_ts:
6657 if (gfc_match (" )") != MATCH_YES)
6659 gfc_current_locus = entry_loc;
6660 return MATCH_NO;
6663 return MATCH_YES;
6667 /* Match a PROCEDURE declaration (R1211). */
6669 static match
6670 match_procedure_decl (void)
6672 match m;
6673 gfc_symbol *sym, *proc_if = NULL;
6674 int num;
6675 gfc_expr *initializer = NULL;
6677 /* Parse interface (with brackets). */
6678 m = match_procedure_interface (&proc_if);
6679 if (m != MATCH_YES)
6680 return m;
6682 /* Parse attributes (with colons). */
6683 m = match_attr_spec();
6684 if (m == MATCH_ERROR)
6685 return MATCH_ERROR;
6687 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6689 current_attr.is_bind_c = 1;
6690 has_name_equals = 0;
6691 curr_binding_label = NULL;
6694 /* Get procedure symbols. */
6695 for(num=1;;num++)
6697 m = gfc_match_symbol (&sym, 0);
6698 if (m == MATCH_NO)
6699 goto syntax;
6700 else if (m == MATCH_ERROR)
6701 return m;
6703 /* Add current_attr to the symbol attributes. */
6704 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
6705 return MATCH_ERROR;
6707 if (sym->attr.is_bind_c)
6709 /* Check for C1218. */
6710 if (!proc_if || !proc_if->attr.is_bind_c)
6712 gfc_error ("BIND(C) attribute at %C requires "
6713 "an interface with BIND(C)");
6714 return MATCH_ERROR;
6716 /* Check for C1217. */
6717 if (has_name_equals && sym->attr.pointer)
6719 gfc_error ("BIND(C) procedure with NAME may not have "
6720 "POINTER attribute at %C");
6721 return MATCH_ERROR;
6723 if (has_name_equals && sym->attr.dummy)
6725 gfc_error ("Dummy procedure at %C may not have "
6726 "BIND(C) attribute with NAME");
6727 return MATCH_ERROR;
6729 /* Set binding label for BIND(C). */
6730 if (!set_binding_label (&sym->binding_label, sym->name, num))
6731 return MATCH_ERROR;
6734 if (!gfc_add_external (&sym->attr, NULL))
6735 return MATCH_ERROR;
6737 if (add_hidden_procptr_result (sym))
6738 sym = sym->result;
6740 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
6741 return MATCH_ERROR;
6743 /* Set interface. */
6744 if (proc_if != NULL)
6746 if (sym->ts.type != BT_UNKNOWN)
6748 gfc_error ("Procedure %qs at %L already has basic type of %s",
6749 sym->name, &gfc_current_locus,
6750 gfc_basic_typename (sym->ts.type));
6751 return MATCH_ERROR;
6753 sym->ts.interface = proc_if;
6754 sym->attr.untyped = 1;
6755 sym->attr.if_source = IFSRC_IFBODY;
6757 else if (current_ts.type != BT_UNKNOWN)
6759 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
6760 return MATCH_ERROR;
6761 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6762 sym->ts.interface->ts = current_ts;
6763 sym->ts.interface->attr.flavor = FL_PROCEDURE;
6764 sym->ts.interface->attr.function = 1;
6765 sym->attr.function = 1;
6766 sym->attr.if_source = IFSRC_UNKNOWN;
6769 if (gfc_match (" =>") == MATCH_YES)
6771 if (!current_attr.pointer)
6773 gfc_error ("Initialization at %C isn't for a pointer variable");
6774 m = MATCH_ERROR;
6775 goto cleanup;
6778 m = match_pointer_init (&initializer, 1);
6779 if (m != MATCH_YES)
6780 goto cleanup;
6782 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
6783 goto cleanup;
6787 if (gfc_match_eos () == MATCH_YES)
6788 return MATCH_YES;
6789 if (gfc_match_char (',') != MATCH_YES)
6790 goto syntax;
6793 syntax:
6794 gfc_error ("Syntax error in PROCEDURE statement at %C");
6795 return MATCH_ERROR;
6797 cleanup:
6798 /* Free stuff up and return. */
6799 gfc_free_expr (initializer);
6800 return m;
6804 static match
6805 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
6808 /* Match a procedure pointer component declaration (R445). */
6810 static match
6811 match_ppc_decl (void)
6813 match m;
6814 gfc_symbol *proc_if = NULL;
6815 gfc_typespec ts;
6816 int num;
6817 gfc_component *c;
6818 gfc_expr *initializer = NULL;
6819 gfc_typebound_proc* tb;
6820 char name[GFC_MAX_SYMBOL_LEN + 1];
6822 /* Parse interface (with brackets). */
6823 m = match_procedure_interface (&proc_if);
6824 if (m != MATCH_YES)
6825 goto syntax;
6827 /* Parse attributes. */
6828 tb = XCNEW (gfc_typebound_proc);
6829 tb->where = gfc_current_locus;
6830 m = match_binding_attributes (tb, false, true);
6831 if (m == MATCH_ERROR)
6832 return m;
6834 gfc_clear_attr (&current_attr);
6835 current_attr.procedure = 1;
6836 current_attr.proc_pointer = 1;
6837 current_attr.access = tb->access;
6838 current_attr.flavor = FL_PROCEDURE;
6840 /* Match the colons (required). */
6841 if (gfc_match (" ::") != MATCH_YES)
6843 gfc_error ("Expected %<::%> after binding-attributes at %C");
6844 return MATCH_ERROR;
6847 /* Check for C450. */
6848 if (!tb->nopass && proc_if == NULL)
6850 gfc_error("NOPASS or explicit interface required at %C");
6851 return MATCH_ERROR;
6854 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
6855 return MATCH_ERROR;
6857 /* Match PPC names. */
6858 ts = current_ts;
6859 for(num=1;;num++)
6861 m = gfc_match_name (name);
6862 if (m == MATCH_NO)
6863 goto syntax;
6864 else if (m == MATCH_ERROR)
6865 return m;
6867 if (!gfc_add_component (gfc_current_block(), name, &c))
6868 return MATCH_ERROR;
6870 /* Add current_attr to the symbol attributes. */
6871 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
6872 return MATCH_ERROR;
6874 if (!gfc_add_external (&c->attr, NULL))
6875 return MATCH_ERROR;
6877 if (!gfc_add_proc (&c->attr, name, NULL))
6878 return MATCH_ERROR;
6880 if (num == 1)
6881 c->tb = tb;
6882 else
6884 c->tb = XCNEW (gfc_typebound_proc);
6885 c->tb->where = gfc_current_locus;
6886 *c->tb = *tb;
6889 /* Set interface. */
6890 if (proc_if != NULL)
6892 c->ts.interface = proc_if;
6893 c->attr.untyped = 1;
6894 c->attr.if_source = IFSRC_IFBODY;
6896 else if (ts.type != BT_UNKNOWN)
6898 c->ts = ts;
6899 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6900 c->ts.interface->result = c->ts.interface;
6901 c->ts.interface->ts = ts;
6902 c->ts.interface->attr.flavor = FL_PROCEDURE;
6903 c->ts.interface->attr.function = 1;
6904 c->attr.function = 1;
6905 c->attr.if_source = IFSRC_UNKNOWN;
6908 if (gfc_match (" =>") == MATCH_YES)
6910 m = match_pointer_init (&initializer, 1);
6911 if (m != MATCH_YES)
6913 gfc_free_expr (initializer);
6914 return m;
6916 c->initializer = initializer;
6919 if (gfc_match_eos () == MATCH_YES)
6920 return MATCH_YES;
6921 if (gfc_match_char (',') != MATCH_YES)
6922 goto syntax;
6925 syntax:
6926 gfc_error ("Syntax error in procedure pointer component at %C");
6927 return MATCH_ERROR;
6931 /* Match a PROCEDURE declaration inside an interface (R1206). */
6933 static match
6934 match_procedure_in_interface (void)
6936 match m;
6937 gfc_symbol *sym;
6938 char name[GFC_MAX_SYMBOL_LEN + 1];
6939 locus old_locus;
6941 if (current_interface.type == INTERFACE_NAMELESS
6942 || current_interface.type == INTERFACE_ABSTRACT)
6944 gfc_error ("PROCEDURE at %C must be in a generic interface");
6945 return MATCH_ERROR;
6948 /* Check if the F2008 optional double colon appears. */
6949 gfc_gobble_whitespace ();
6950 old_locus = gfc_current_locus;
6951 if (gfc_match ("::") == MATCH_YES)
6953 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
6954 "MODULE PROCEDURE statement at %L", &old_locus))
6955 return MATCH_ERROR;
6957 else
6958 gfc_current_locus = old_locus;
6960 for(;;)
6962 m = gfc_match_name (name);
6963 if (m == MATCH_NO)
6964 goto syntax;
6965 else if (m == MATCH_ERROR)
6966 return m;
6967 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
6968 return MATCH_ERROR;
6970 if (!gfc_add_interface (sym))
6971 return MATCH_ERROR;
6973 if (gfc_match_eos () == MATCH_YES)
6974 break;
6975 if (gfc_match_char (',') != MATCH_YES)
6976 goto syntax;
6979 return MATCH_YES;
6981 syntax:
6982 gfc_error ("Syntax error in PROCEDURE statement at %C");
6983 return MATCH_ERROR;
6987 /* General matcher for PROCEDURE declarations. */
6989 static match match_procedure_in_type (void);
6991 match
6992 gfc_match_procedure (void)
6994 match m;
6996 switch (gfc_current_state ())
6998 case COMP_NONE:
6999 case COMP_PROGRAM:
7000 case COMP_MODULE:
7001 case COMP_SUBMODULE:
7002 case COMP_SUBROUTINE:
7003 case COMP_FUNCTION:
7004 case COMP_BLOCK:
7005 m = match_procedure_decl ();
7006 break;
7007 case COMP_INTERFACE:
7008 m = match_procedure_in_interface ();
7009 break;
7010 case COMP_DERIVED:
7011 m = match_ppc_decl ();
7012 break;
7013 case COMP_DERIVED_CONTAINS:
7014 m = match_procedure_in_type ();
7015 break;
7016 default:
7017 return MATCH_NO;
7020 if (m != MATCH_YES)
7021 return m;
7023 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
7024 return MATCH_ERROR;
7026 return m;
7030 /* Warn if a matched procedure has the same name as an intrinsic; this is
7031 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7032 parser-state-stack to find out whether we're in a module. */
7034 static void
7035 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
7037 bool in_module;
7039 in_module = (gfc_state_stack->previous
7040 && (gfc_state_stack->previous->state == COMP_MODULE
7041 || gfc_state_stack->previous->state == COMP_SUBMODULE));
7043 gfc_warn_intrinsic_shadow (sym, in_module, func);
7047 /* Match a function declaration. */
7049 match
7050 gfc_match_function_decl (void)
7052 char name[GFC_MAX_SYMBOL_LEN + 1];
7053 gfc_symbol *sym, *result;
7054 locus old_loc;
7055 match m;
7056 match suffix_match;
7057 match found_match; /* Status returned by match func. */
7059 if (gfc_current_state () != COMP_NONE
7060 && gfc_current_state () != COMP_INTERFACE
7061 && gfc_current_state () != COMP_CONTAINS)
7062 return MATCH_NO;
7064 gfc_clear_ts (&current_ts);
7066 old_loc = gfc_current_locus;
7068 m = gfc_match_prefix (&current_ts);
7069 if (m != MATCH_YES)
7071 gfc_current_locus = old_loc;
7072 return m;
7075 if (gfc_match ("function% %n", name) != MATCH_YES)
7077 gfc_current_locus = old_loc;
7078 return MATCH_NO;
7081 if (get_proc_name (name, &sym, false))
7082 return MATCH_ERROR;
7084 if (add_hidden_procptr_result (sym))
7085 sym = sym->result;
7087 if (current_attr.module_procedure)
7088 sym->attr.module_procedure = 1;
7090 gfc_new_block = sym;
7092 m = gfc_match_formal_arglist (sym, 0, 0);
7093 if (m == MATCH_NO)
7095 gfc_error ("Expected formal argument list in function "
7096 "definition at %C");
7097 m = MATCH_ERROR;
7098 goto cleanup;
7100 else if (m == MATCH_ERROR)
7101 goto cleanup;
7103 result = NULL;
7105 /* According to the draft, the bind(c) and result clause can
7106 come in either order after the formal_arg_list (i.e., either
7107 can be first, both can exist together or by themselves or neither
7108 one). Therefore, the match_result can't match the end of the
7109 string, and check for the bind(c) or result clause in either order. */
7110 found_match = gfc_match_eos ();
7112 /* Make sure that it isn't already declared as BIND(C). If it is, it
7113 must have been marked BIND(C) with a BIND(C) attribute and that is
7114 not allowed for procedures. */
7115 if (sym->attr.is_bind_c == 1)
7117 sym->attr.is_bind_c = 0;
7118 if (sym->old_symbol != NULL)
7119 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7120 "variables or common blocks",
7121 &(sym->old_symbol->declared_at));
7122 else
7123 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7124 "variables or common blocks", &gfc_current_locus);
7127 if (found_match != MATCH_YES)
7129 /* If we haven't found the end-of-statement, look for a suffix. */
7130 suffix_match = gfc_match_suffix (sym, &result);
7131 if (suffix_match == MATCH_YES)
7132 /* Need to get the eos now. */
7133 found_match = gfc_match_eos ();
7134 else
7135 found_match = suffix_match;
7138 if(found_match != MATCH_YES)
7139 m = MATCH_ERROR;
7140 else
7142 /* Make changes to the symbol. */
7143 m = MATCH_ERROR;
7145 if (!gfc_add_function (&sym->attr, sym->name, NULL))
7146 goto cleanup;
7148 if (!gfc_missing_attr (&sym->attr, NULL))
7149 goto cleanup;
7151 if (!copy_prefix (&sym->attr, &sym->declared_at))
7153 if(!sym->attr.module_procedure)
7154 goto cleanup;
7155 else
7156 gfc_error_check ();
7159 /* Delay matching the function characteristics until after the
7160 specification block by signalling kind=-1. */
7161 sym->declared_at = old_loc;
7162 if (current_ts.type != BT_UNKNOWN)
7163 current_ts.kind = -1;
7164 else
7165 current_ts.kind = 0;
7167 if (result == NULL)
7169 if (current_ts.type != BT_UNKNOWN
7170 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
7171 goto cleanup;
7172 sym->result = sym;
7174 else
7176 if (current_ts.type != BT_UNKNOWN
7177 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
7178 goto cleanup;
7179 sym->result = result;
7182 /* Warn if this procedure has the same name as an intrinsic. */
7183 do_warn_intrinsic_shadow (sym, true);
7185 return MATCH_YES;
7188 cleanup:
7189 gfc_current_locus = old_loc;
7190 return m;
7194 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
7195 pass the name of the entry, rather than the gfc_current_block name, and
7196 to return false upon finding an existing global entry. */
7198 static bool
7199 add_global_entry (const char *name, const char *binding_label, bool sub,
7200 locus *where)
7202 gfc_gsymbol *s;
7203 enum gfc_symbol_type type;
7205 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7207 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7208 name is a global identifier. */
7209 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
7211 s = gfc_get_gsymbol (name);
7213 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7215 gfc_global_used (s, where);
7216 return false;
7218 else
7220 s->type = type;
7221 s->sym_name = name;
7222 s->where = *where;
7223 s->defined = 1;
7224 s->ns = gfc_current_ns;
7228 /* Don't add the symbol multiple times. */
7229 if (binding_label
7230 && (!gfc_notification_std (GFC_STD_F2008)
7231 || strcmp (name, binding_label) != 0))
7233 s = gfc_get_gsymbol (binding_label);
7235 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7237 gfc_global_used (s, where);
7238 return false;
7240 else
7242 s->type = type;
7243 s->sym_name = name;
7244 s->binding_label = binding_label;
7245 s->where = *where;
7246 s->defined = 1;
7247 s->ns = gfc_current_ns;
7251 return true;
7255 /* Match an ENTRY statement. */
7257 match
7258 gfc_match_entry (void)
7260 gfc_symbol *proc;
7261 gfc_symbol *result;
7262 gfc_symbol *entry;
7263 char name[GFC_MAX_SYMBOL_LEN + 1];
7264 gfc_compile_state state;
7265 match m;
7266 gfc_entry_list *el;
7267 locus old_loc;
7268 bool module_procedure;
7269 char peek_char;
7270 match is_bind_c;
7272 m = gfc_match_name (name);
7273 if (m != MATCH_YES)
7274 return m;
7276 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
7277 return MATCH_ERROR;
7279 state = gfc_current_state ();
7280 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
7282 switch (state)
7284 case COMP_PROGRAM:
7285 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7286 break;
7287 case COMP_MODULE:
7288 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7289 break;
7290 case COMP_SUBMODULE:
7291 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7292 break;
7293 case COMP_BLOCK_DATA:
7294 gfc_error ("ENTRY statement at %C cannot appear within "
7295 "a BLOCK DATA");
7296 break;
7297 case COMP_INTERFACE:
7298 gfc_error ("ENTRY statement at %C cannot appear within "
7299 "an INTERFACE");
7300 break;
7301 case COMP_STRUCTURE:
7302 gfc_error ("ENTRY statement at %C cannot appear within "
7303 "a STRUCTURE block");
7304 break;
7305 case COMP_DERIVED:
7306 gfc_error ("ENTRY statement at %C cannot appear within "
7307 "a DERIVED TYPE block");
7308 break;
7309 case COMP_IF:
7310 gfc_error ("ENTRY statement at %C cannot appear within "
7311 "an IF-THEN block");
7312 break;
7313 case COMP_DO:
7314 case COMP_DO_CONCURRENT:
7315 gfc_error ("ENTRY statement at %C cannot appear within "
7316 "a DO block");
7317 break;
7318 case COMP_SELECT:
7319 gfc_error ("ENTRY statement at %C cannot appear within "
7320 "a SELECT block");
7321 break;
7322 case COMP_FORALL:
7323 gfc_error ("ENTRY statement at %C cannot appear within "
7324 "a FORALL block");
7325 break;
7326 case COMP_WHERE:
7327 gfc_error ("ENTRY statement at %C cannot appear within "
7328 "a WHERE block");
7329 break;
7330 case COMP_CONTAINS:
7331 gfc_error ("ENTRY statement at %C cannot appear within "
7332 "a contained subprogram");
7333 break;
7334 default:
7335 gfc_error ("Unexpected ENTRY statement at %C");
7337 return MATCH_ERROR;
7340 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7341 && gfc_state_stack->previous->state == COMP_INTERFACE)
7343 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7344 return MATCH_ERROR;
7347 module_procedure = gfc_current_ns->parent != NULL
7348 && gfc_current_ns->parent->proc_name
7349 && gfc_current_ns->parent->proc_name->attr.flavor
7350 == FL_MODULE;
7352 if (gfc_current_ns->parent != NULL
7353 && gfc_current_ns->parent->proc_name
7354 && !module_procedure)
7356 gfc_error("ENTRY statement at %C cannot appear in a "
7357 "contained procedure");
7358 return MATCH_ERROR;
7361 /* Module function entries need special care in get_proc_name
7362 because previous references within the function will have
7363 created symbols attached to the current namespace. */
7364 if (get_proc_name (name, &entry,
7365 gfc_current_ns->parent != NULL
7366 && module_procedure))
7367 return MATCH_ERROR;
7369 proc = gfc_current_block ();
7371 /* Make sure that it isn't already declared as BIND(C). If it is, it
7372 must have been marked BIND(C) with a BIND(C) attribute and that is
7373 not allowed for procedures. */
7374 if (entry->attr.is_bind_c == 1)
7376 entry->attr.is_bind_c = 0;
7377 if (entry->old_symbol != NULL)
7378 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7379 "variables or common blocks",
7380 &(entry->old_symbol->declared_at));
7381 else
7382 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7383 "variables or common blocks", &gfc_current_locus);
7386 /* Check what next non-whitespace character is so we can tell if there
7387 is the required parens if we have a BIND(C). */
7388 old_loc = gfc_current_locus;
7389 gfc_gobble_whitespace ();
7390 peek_char = gfc_peek_ascii_char ();
7392 if (state == COMP_SUBROUTINE)
7394 m = gfc_match_formal_arglist (entry, 0, 1);
7395 if (m != MATCH_YES)
7396 return MATCH_ERROR;
7398 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7399 never be an internal procedure. */
7400 is_bind_c = gfc_match_bind_c (entry, true);
7401 if (is_bind_c == MATCH_ERROR)
7402 return MATCH_ERROR;
7403 if (is_bind_c == MATCH_YES)
7405 if (peek_char != '(')
7407 gfc_error ("Missing required parentheses before BIND(C) at %C");
7408 return MATCH_ERROR;
7410 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7411 &(entry->declared_at), 1))
7412 return MATCH_ERROR;
7415 if (!gfc_current_ns->parent
7416 && !add_global_entry (name, entry->binding_label, true,
7417 &old_loc))
7418 return MATCH_ERROR;
7420 /* An entry in a subroutine. */
7421 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7422 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7423 return MATCH_ERROR;
7425 else
7427 /* An entry in a function.
7428 We need to take special care because writing
7429 ENTRY f()
7431 ENTRY f
7432 is allowed, whereas
7433 ENTRY f() RESULT (r)
7434 can't be written as
7435 ENTRY f RESULT (r). */
7436 if (gfc_match_eos () == MATCH_YES)
7438 gfc_current_locus = old_loc;
7439 /* Match the empty argument list, and add the interface to
7440 the symbol. */
7441 m = gfc_match_formal_arglist (entry, 0, 1);
7443 else
7444 m = gfc_match_formal_arglist (entry, 0, 0);
7446 if (m != MATCH_YES)
7447 return MATCH_ERROR;
7449 result = NULL;
7451 if (gfc_match_eos () == MATCH_YES)
7453 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7454 || !gfc_add_function (&entry->attr, entry->name, NULL))
7455 return MATCH_ERROR;
7457 entry->result = entry;
7459 else
7461 m = gfc_match_suffix (entry, &result);
7462 if (m == MATCH_NO)
7463 gfc_syntax_error (ST_ENTRY);
7464 if (m != MATCH_YES)
7465 return MATCH_ERROR;
7467 if (result)
7469 if (!gfc_add_result (&result->attr, result->name, NULL)
7470 || !gfc_add_entry (&entry->attr, result->name, NULL)
7471 || !gfc_add_function (&entry->attr, result->name, NULL))
7472 return MATCH_ERROR;
7473 entry->result = result;
7475 else
7477 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7478 || !gfc_add_function (&entry->attr, entry->name, NULL))
7479 return MATCH_ERROR;
7480 entry->result = entry;
7484 if (!gfc_current_ns->parent
7485 && !add_global_entry (name, entry->binding_label, false,
7486 &old_loc))
7487 return MATCH_ERROR;
7490 if (gfc_match_eos () != MATCH_YES)
7492 gfc_syntax_error (ST_ENTRY);
7493 return MATCH_ERROR;
7496 entry->attr.recursive = proc->attr.recursive;
7497 entry->attr.elemental = proc->attr.elemental;
7498 entry->attr.pure = proc->attr.pure;
7500 el = gfc_get_entry_list ();
7501 el->sym = entry;
7502 el->next = gfc_current_ns->entries;
7503 gfc_current_ns->entries = el;
7504 if (el->next)
7505 el->id = el->next->id + 1;
7506 else
7507 el->id = 1;
7509 new_st.op = EXEC_ENTRY;
7510 new_st.ext.entry = el;
7512 return MATCH_YES;
7516 /* Match a subroutine statement, including optional prefixes. */
7518 match
7519 gfc_match_subroutine (void)
7521 char name[GFC_MAX_SYMBOL_LEN + 1];
7522 gfc_symbol *sym;
7523 match m;
7524 match is_bind_c;
7525 char peek_char;
7526 bool allow_binding_name;
7528 if (gfc_current_state () != COMP_NONE
7529 && gfc_current_state () != COMP_INTERFACE
7530 && gfc_current_state () != COMP_CONTAINS)
7531 return MATCH_NO;
7533 m = gfc_match_prefix (NULL);
7534 if (m != MATCH_YES)
7535 return m;
7537 m = gfc_match ("subroutine% %n", name);
7538 if (m != MATCH_YES)
7539 return m;
7541 if (get_proc_name (name, &sym, false))
7542 return MATCH_ERROR;
7544 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7545 the symbol existed before. */
7546 sym->declared_at = gfc_current_locus;
7548 if (current_attr.module_procedure)
7549 sym->attr.module_procedure = 1;
7551 if (add_hidden_procptr_result (sym))
7552 sym = sym->result;
7554 gfc_new_block = sym;
7556 /* Check what next non-whitespace character is so we can tell if there
7557 is the required parens if we have a BIND(C). */
7558 gfc_gobble_whitespace ();
7559 peek_char = gfc_peek_ascii_char ();
7561 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7562 return MATCH_ERROR;
7564 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7565 return MATCH_ERROR;
7567 /* Make sure that it isn't already declared as BIND(C). If it is, it
7568 must have been marked BIND(C) with a BIND(C) attribute and that is
7569 not allowed for procedures. */
7570 if (sym->attr.is_bind_c == 1)
7572 sym->attr.is_bind_c = 0;
7573 if (sym->old_symbol != NULL)
7574 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7575 "variables or common blocks",
7576 &(sym->old_symbol->declared_at));
7577 else
7578 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7579 "variables or common blocks", &gfc_current_locus);
7582 /* C binding names are not allowed for internal procedures. */
7583 if (gfc_current_state () == COMP_CONTAINS
7584 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7585 allow_binding_name = false;
7586 else
7587 allow_binding_name = true;
7589 /* Here, we are just checking if it has the bind(c) attribute, and if
7590 so, then we need to make sure it's all correct. If it doesn't,
7591 we still need to continue matching the rest of the subroutine line. */
7592 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7593 if (is_bind_c == MATCH_ERROR)
7595 /* There was an attempt at the bind(c), but it was wrong. An
7596 error message should have been printed w/in the gfc_match_bind_c
7597 so here we'll just return the MATCH_ERROR. */
7598 return MATCH_ERROR;
7601 if (is_bind_c == MATCH_YES)
7603 /* The following is allowed in the Fortran 2008 draft. */
7604 if (gfc_current_state () == COMP_CONTAINS
7605 && sym->ns->proc_name->attr.flavor != FL_MODULE
7606 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7607 "at %L may not be specified for an internal "
7608 "procedure", &gfc_current_locus))
7609 return MATCH_ERROR;
7611 if (peek_char != '(')
7613 gfc_error ("Missing required parentheses before BIND(C) at %C");
7614 return MATCH_ERROR;
7616 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
7617 &(sym->declared_at), 1))
7618 return MATCH_ERROR;
7621 if (gfc_match_eos () != MATCH_YES)
7623 gfc_syntax_error (ST_SUBROUTINE);
7624 return MATCH_ERROR;
7627 if (!copy_prefix (&sym->attr, &sym->declared_at))
7629 if(!sym->attr.module_procedure)
7630 return MATCH_ERROR;
7631 else
7632 gfc_error_check ();
7635 /* Warn if it has the same name as an intrinsic. */
7636 do_warn_intrinsic_shadow (sym, false);
7638 return MATCH_YES;
7642 /* Check that the NAME identifier in a BIND attribute or statement
7643 is conform to C identifier rules. */
7645 match
7646 check_bind_name_identifier (char **name)
7648 char *n = *name, *p;
7650 /* Remove leading spaces. */
7651 while (*n == ' ')
7652 n++;
7654 /* On an empty string, free memory and set name to NULL. */
7655 if (*n == '\0')
7657 free (*name);
7658 *name = NULL;
7659 return MATCH_YES;
7662 /* Remove trailing spaces. */
7663 p = n + strlen(n) - 1;
7664 while (*p == ' ')
7665 *(p--) = '\0';
7667 /* Insert the identifier into the symbol table. */
7668 p = xstrdup (n);
7669 free (*name);
7670 *name = p;
7672 /* Now check that identifier is valid under C rules. */
7673 if (ISDIGIT (*p))
7675 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7676 return MATCH_ERROR;
7679 for (; *p; p++)
7680 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
7682 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7683 return MATCH_ERROR;
7686 return MATCH_YES;
7690 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
7691 given, and set the binding label in either the given symbol (if not
7692 NULL), or in the current_ts. The symbol may be NULL because we may
7693 encounter the BIND(C) before the declaration itself. Return
7694 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
7695 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
7696 or MATCH_YES if the specifier was correct and the binding label and
7697 bind(c) fields were set correctly for the given symbol or the
7698 current_ts. If allow_binding_name is false, no binding name may be
7699 given. */
7701 match
7702 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
7704 char *binding_label = NULL;
7705 gfc_expr *e = NULL;
7707 /* Initialize the flag that specifies whether we encountered a NAME=
7708 specifier or not. */
7709 has_name_equals = 0;
7711 /* This much we have to be able to match, in this order, if
7712 there is a bind(c) label. */
7713 if (gfc_match (" bind ( c ") != MATCH_YES)
7714 return MATCH_NO;
7716 /* Now see if there is a binding label, or if we've reached the
7717 end of the bind(c) attribute without one. */
7718 if (gfc_match_char (',') == MATCH_YES)
7720 if (gfc_match (" name = ") != MATCH_YES)
7722 gfc_error ("Syntax error in NAME= specifier for binding label "
7723 "at %C");
7724 /* should give an error message here */
7725 return MATCH_ERROR;
7728 has_name_equals = 1;
7730 if (gfc_match_init_expr (&e) != MATCH_YES)
7732 gfc_free_expr (e);
7733 return MATCH_ERROR;
7736 if (!gfc_simplify_expr(e, 0))
7738 gfc_error ("NAME= specifier at %C should be a constant expression");
7739 gfc_free_expr (e);
7740 return MATCH_ERROR;
7743 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
7744 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
7746 gfc_error ("NAME= specifier at %C should be a scalar of "
7747 "default character kind");
7748 gfc_free_expr(e);
7749 return MATCH_ERROR;
7752 // Get a C string from the Fortran string constant
7753 binding_label = gfc_widechar_to_char (e->value.character.string,
7754 e->value.character.length);
7755 gfc_free_expr(e);
7757 // Check that it is valid (old gfc_match_name_C)
7758 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
7759 return MATCH_ERROR;
7762 /* Get the required right paren. */
7763 if (gfc_match_char (')') != MATCH_YES)
7765 gfc_error ("Missing closing paren for binding label at %C");
7766 return MATCH_ERROR;
7769 if (has_name_equals && !allow_binding_name)
7771 gfc_error ("No binding name is allowed in BIND(C) at %C");
7772 return MATCH_ERROR;
7775 if (has_name_equals && sym != NULL && sym->attr.dummy)
7777 gfc_error ("For dummy procedure %s, no binding name is "
7778 "allowed in BIND(C) at %C", sym->name);
7779 return MATCH_ERROR;
7783 /* Save the binding label to the symbol. If sym is null, we're
7784 probably matching the typespec attributes of a declaration and
7785 haven't gotten the name yet, and therefore, no symbol yet. */
7786 if (binding_label)
7788 if (sym != NULL)
7789 sym->binding_label = binding_label;
7790 else
7791 curr_binding_label = binding_label;
7793 else if (allow_binding_name)
7795 /* No binding label, but if symbol isn't null, we
7796 can set the label for it here.
7797 If name="" or allow_binding_name is false, no C binding name is
7798 created. */
7799 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
7800 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
7803 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
7804 && current_interface.type == INTERFACE_ABSTRACT)
7806 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
7807 return MATCH_ERROR;
7810 return MATCH_YES;
7814 /* Return nonzero if we're currently compiling a contained procedure. */
7816 static int
7817 contained_procedure (void)
7819 gfc_state_data *s = gfc_state_stack;
7821 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
7822 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
7823 return 1;
7825 return 0;
7828 /* Set the kind of each enumerator. The kind is selected such that it is
7829 interoperable with the corresponding C enumeration type, making
7830 sure that -fshort-enums is honored. */
7832 static void
7833 set_enum_kind(void)
7835 enumerator_history *current_history = NULL;
7836 int kind;
7837 int i;
7839 if (max_enum == NULL || enum_history == NULL)
7840 return;
7842 if (!flag_short_enums)
7843 return;
7845 i = 0;
7848 kind = gfc_integer_kinds[i++].kind;
7850 while (kind < gfc_c_int_kind
7851 && gfc_check_integer_range (max_enum->initializer->value.integer,
7852 kind) != ARITH_OK);
7854 current_history = enum_history;
7855 while (current_history != NULL)
7857 current_history->sym->ts.kind = kind;
7858 current_history = current_history->next;
7863 /* Match any of the various end-block statements. Returns the type of
7864 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
7865 and END BLOCK statements cannot be replaced by a single END statement. */
7867 match
7868 gfc_match_end (gfc_statement *st)
7870 char name[GFC_MAX_SYMBOL_LEN + 1];
7871 gfc_compile_state state;
7872 locus old_loc;
7873 const char *block_name;
7874 const char *target;
7875 int eos_ok;
7876 match m;
7877 gfc_namespace *parent_ns, *ns, *prev_ns;
7878 gfc_namespace **nsp;
7879 bool abreviated_modproc_decl = false;
7880 bool got_matching_end = false;
7882 old_loc = gfc_current_locus;
7883 if (gfc_match ("end") != MATCH_YES)
7884 return MATCH_NO;
7886 state = gfc_current_state ();
7887 block_name = gfc_current_block () == NULL
7888 ? NULL : gfc_current_block ()->name;
7890 switch (state)
7892 case COMP_ASSOCIATE:
7893 case COMP_BLOCK:
7894 if (gfc_str_startswith (block_name, "block@"))
7895 block_name = NULL;
7896 break;
7898 case COMP_CONTAINS:
7899 case COMP_DERIVED_CONTAINS:
7900 state = gfc_state_stack->previous->state;
7901 block_name = gfc_state_stack->previous->sym == NULL
7902 ? NULL : gfc_state_stack->previous->sym->name;
7903 abreviated_modproc_decl = gfc_state_stack->previous->sym
7904 && gfc_state_stack->previous->sym->abr_modproc_decl;
7905 break;
7907 default:
7908 break;
7911 if (!abreviated_modproc_decl)
7912 abreviated_modproc_decl = gfc_current_block ()
7913 && gfc_current_block ()->abr_modproc_decl;
7915 switch (state)
7917 case COMP_NONE:
7918 case COMP_PROGRAM:
7919 *st = ST_END_PROGRAM;
7920 target = " program";
7921 eos_ok = 1;
7922 break;
7924 case COMP_SUBROUTINE:
7925 *st = ST_END_SUBROUTINE;
7926 if (!abreviated_modproc_decl)
7927 target = " subroutine";
7928 else
7929 target = " procedure";
7930 eos_ok = !contained_procedure ();
7931 break;
7933 case COMP_FUNCTION:
7934 *st = ST_END_FUNCTION;
7935 if (!abreviated_modproc_decl)
7936 target = " function";
7937 else
7938 target = " procedure";
7939 eos_ok = !contained_procedure ();
7940 break;
7942 case COMP_BLOCK_DATA:
7943 *st = ST_END_BLOCK_DATA;
7944 target = " block data";
7945 eos_ok = 1;
7946 break;
7948 case COMP_MODULE:
7949 *st = ST_END_MODULE;
7950 target = " module";
7951 eos_ok = 1;
7952 break;
7954 case COMP_SUBMODULE:
7955 *st = ST_END_SUBMODULE;
7956 target = " submodule";
7957 eos_ok = 1;
7958 break;
7960 case COMP_INTERFACE:
7961 *st = ST_END_INTERFACE;
7962 target = " interface";
7963 eos_ok = 0;
7964 break;
7966 case COMP_MAP:
7967 *st = ST_END_MAP;
7968 target = " map";
7969 eos_ok = 0;
7970 break;
7972 case COMP_UNION:
7973 *st = ST_END_UNION;
7974 target = " union";
7975 eos_ok = 0;
7976 break;
7978 case COMP_STRUCTURE:
7979 *st = ST_END_STRUCTURE;
7980 target = " structure";
7981 eos_ok = 0;
7982 break;
7984 case COMP_DERIVED:
7985 case COMP_DERIVED_CONTAINS:
7986 *st = ST_END_TYPE;
7987 target = " type";
7988 eos_ok = 0;
7989 break;
7991 case COMP_ASSOCIATE:
7992 *st = ST_END_ASSOCIATE;
7993 target = " associate";
7994 eos_ok = 0;
7995 break;
7997 case COMP_BLOCK:
7998 *st = ST_END_BLOCK;
7999 target = " block";
8000 eos_ok = 0;
8001 break;
8003 case COMP_IF:
8004 *st = ST_ENDIF;
8005 target = " if";
8006 eos_ok = 0;
8007 break;
8009 case COMP_DO:
8010 case COMP_DO_CONCURRENT:
8011 *st = ST_ENDDO;
8012 target = " do";
8013 eos_ok = 0;
8014 break;
8016 case COMP_CRITICAL:
8017 *st = ST_END_CRITICAL;
8018 target = " critical";
8019 eos_ok = 0;
8020 break;
8022 case COMP_SELECT:
8023 case COMP_SELECT_TYPE:
8024 *st = ST_END_SELECT;
8025 target = " select";
8026 eos_ok = 0;
8027 break;
8029 case COMP_FORALL:
8030 *st = ST_END_FORALL;
8031 target = " forall";
8032 eos_ok = 0;
8033 break;
8035 case COMP_WHERE:
8036 *st = ST_END_WHERE;
8037 target = " where";
8038 eos_ok = 0;
8039 break;
8041 case COMP_ENUM:
8042 *st = ST_END_ENUM;
8043 target = " enum";
8044 eos_ok = 0;
8045 last_initializer = NULL;
8046 set_enum_kind ();
8047 gfc_free_enum_history ();
8048 break;
8050 default:
8051 gfc_error ("Unexpected END statement at %C");
8052 goto cleanup;
8055 old_loc = gfc_current_locus;
8056 if (gfc_match_eos () == MATCH_YES)
8058 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
8060 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
8061 "instead of %s statement at %L",
8062 abreviated_modproc_decl ? "END PROCEDURE"
8063 : gfc_ascii_statement(*st), &old_loc))
8064 goto cleanup;
8066 else if (!eos_ok)
8068 /* We would have required END [something]. */
8069 gfc_error ("%s statement expected at %L",
8070 gfc_ascii_statement (*st), &old_loc);
8071 goto cleanup;
8074 return MATCH_YES;
8077 /* Verify that we've got the sort of end-block that we're expecting. */
8078 if (gfc_match (target) != MATCH_YES)
8080 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
8081 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
8082 goto cleanup;
8084 else
8085 got_matching_end = true;
8087 old_loc = gfc_current_locus;
8088 /* If we're at the end, make sure a block name wasn't required. */
8089 if (gfc_match_eos () == MATCH_YES)
8092 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
8093 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
8094 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
8095 return MATCH_YES;
8097 if (!block_name)
8098 return MATCH_YES;
8100 gfc_error ("Expected block name of %qs in %s statement at %L",
8101 block_name, gfc_ascii_statement (*st), &old_loc);
8103 return MATCH_ERROR;
8106 /* END INTERFACE has a special handler for its several possible endings. */
8107 if (*st == ST_END_INTERFACE)
8108 return gfc_match_end_interface ();
8110 /* We haven't hit the end of statement, so what is left must be an
8111 end-name. */
8112 m = gfc_match_space ();
8113 if (m == MATCH_YES)
8114 m = gfc_match_name (name);
8116 if (m == MATCH_NO)
8117 gfc_error ("Expected terminating name at %C");
8118 if (m != MATCH_YES)
8119 goto cleanup;
8121 if (block_name == NULL)
8122 goto syntax;
8124 /* We have to pick out the declared submodule name from the composite
8125 required by F2008:11.2.3 para 2, which ends in the declared name. */
8126 if (state == COMP_SUBMODULE)
8127 block_name = strchr (block_name, '.') + 1;
8129 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
8131 gfc_error ("Expected label %qs for %s statement at %C", block_name,
8132 gfc_ascii_statement (*st));
8133 goto cleanup;
8135 /* Procedure pointer as function result. */
8136 else if (strcmp (block_name, "ppr@") == 0
8137 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8139 gfc_error ("Expected label %qs for %s statement at %C",
8140 gfc_current_block ()->ns->proc_name->name,
8141 gfc_ascii_statement (*st));
8142 goto cleanup;
8145 if (gfc_match_eos () == MATCH_YES)
8146 return MATCH_YES;
8148 syntax:
8149 gfc_syntax_error (*st);
8151 cleanup:
8152 gfc_current_locus = old_loc;
8154 /* If we are missing an END BLOCK, we created a half-ready namespace.
8155 Remove it from the parent namespace's sibling list. */
8157 while (state == COMP_BLOCK && !got_matching_end)
8159 parent_ns = gfc_current_ns->parent;
8161 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8163 prev_ns = NULL;
8164 ns = *nsp;
8165 while (ns)
8167 if (ns == gfc_current_ns)
8169 if (prev_ns == NULL)
8170 *nsp = NULL;
8171 else
8172 prev_ns->sibling = ns->sibling;
8174 prev_ns = ns;
8175 ns = ns->sibling;
8178 gfc_free_namespace (gfc_current_ns);
8179 gfc_current_ns = parent_ns;
8180 gfc_state_stack = gfc_state_stack->previous;
8181 state = gfc_current_state ();
8184 return MATCH_ERROR;
8189 /***************** Attribute declaration statements ****************/
8191 /* Set the attribute of a single variable. */
8193 static match
8194 attr_decl1 (void)
8196 char name[GFC_MAX_SYMBOL_LEN + 1];
8197 gfc_array_spec *as;
8199 /* Workaround -Wmaybe-uninitialized false positive during
8200 profiledbootstrap by initializing them. */
8201 gfc_symbol *sym = NULL;
8202 locus var_locus;
8203 match m;
8205 as = NULL;
8207 m = gfc_match_name (name);
8208 if (m != MATCH_YES)
8209 goto cleanup;
8211 if (find_special (name, &sym, false))
8212 return MATCH_ERROR;
8214 if (!check_function_name (name))
8216 m = MATCH_ERROR;
8217 goto cleanup;
8220 var_locus = gfc_current_locus;
8222 /* Deal with possible array specification for certain attributes. */
8223 if (current_attr.dimension
8224 || current_attr.codimension
8225 || current_attr.allocatable
8226 || current_attr.pointer
8227 || current_attr.target)
8229 m = gfc_match_array_spec (&as, !current_attr.codimension,
8230 !current_attr.dimension
8231 && !current_attr.pointer
8232 && !current_attr.target);
8233 if (m == MATCH_ERROR)
8234 goto cleanup;
8236 if (current_attr.dimension && m == MATCH_NO)
8238 gfc_error ("Missing array specification at %L in DIMENSION "
8239 "statement", &var_locus);
8240 m = MATCH_ERROR;
8241 goto cleanup;
8244 if (current_attr.dimension && sym->value)
8246 gfc_error ("Dimensions specified for %s at %L after its "
8247 "initialization", sym->name, &var_locus);
8248 m = MATCH_ERROR;
8249 goto cleanup;
8252 if (current_attr.codimension && m == MATCH_NO)
8254 gfc_error ("Missing array specification at %L in CODIMENSION "
8255 "statement", &var_locus);
8256 m = MATCH_ERROR;
8257 goto cleanup;
8260 if ((current_attr.allocatable || current_attr.pointer)
8261 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8263 gfc_error ("Array specification must be deferred at %L", &var_locus);
8264 m = MATCH_ERROR;
8265 goto cleanup;
8269 /* Update symbol table. DIMENSION attribute is set in
8270 gfc_set_array_spec(). For CLASS variables, this must be applied
8271 to the first component, or '_data' field. */
8272 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
8274 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
8276 m = MATCH_ERROR;
8277 goto cleanup;
8280 else
8282 if (current_attr.dimension == 0 && current_attr.codimension == 0
8283 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
8285 m = MATCH_ERROR;
8286 goto cleanup;
8290 if (sym->ts.type == BT_CLASS
8291 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
8293 m = MATCH_ERROR;
8294 goto cleanup;
8297 if (!gfc_set_array_spec (sym, as, &var_locus))
8299 m = MATCH_ERROR;
8300 goto cleanup;
8303 if (sym->attr.cray_pointee && sym->as != NULL)
8305 /* Fix the array spec. */
8306 m = gfc_mod_pointee_as (sym->as);
8307 if (m == MATCH_ERROR)
8308 goto cleanup;
8311 if (!gfc_add_attribute (&sym->attr, &var_locus))
8313 m = MATCH_ERROR;
8314 goto cleanup;
8317 if ((current_attr.external || current_attr.intrinsic)
8318 && sym->attr.flavor != FL_PROCEDURE
8319 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8321 m = MATCH_ERROR;
8322 goto cleanup;
8325 add_hidden_procptr_result (sym);
8327 return MATCH_YES;
8329 cleanup:
8330 gfc_free_array_spec (as);
8331 return m;
8335 /* Generic attribute declaration subroutine. Used for attributes that
8336 just have a list of names. */
8338 static match
8339 attr_decl (void)
8341 match m;
8343 /* Gobble the optional double colon, by simply ignoring the result
8344 of gfc_match(). */
8345 gfc_match (" ::");
8347 for (;;)
8349 m = attr_decl1 ();
8350 if (m != MATCH_YES)
8351 break;
8353 if (gfc_match_eos () == MATCH_YES)
8355 m = MATCH_YES;
8356 break;
8359 if (gfc_match_char (',') != MATCH_YES)
8361 gfc_error ("Unexpected character in variable list at %C");
8362 m = MATCH_ERROR;
8363 break;
8367 return m;
8371 /* This routine matches Cray Pointer declarations of the form:
8372 pointer ( <pointer>, <pointee> )
8374 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8375 The pointer, if already declared, should be an integer. Otherwise, we
8376 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8377 be either a scalar, or an array declaration. No space is allocated for
8378 the pointee. For the statement
8379 pointer (ipt, ar(10))
8380 any subsequent uses of ar will be translated (in C-notation) as
8381 ar(i) => ((<type> *) ipt)(i)
8382 After gimplification, pointee variable will disappear in the code. */
8384 static match
8385 cray_pointer_decl (void)
8387 match m;
8388 gfc_array_spec *as = NULL;
8389 gfc_symbol *cptr; /* Pointer symbol. */
8390 gfc_symbol *cpte; /* Pointee symbol. */
8391 locus var_locus;
8392 bool done = false;
8394 while (!done)
8396 if (gfc_match_char ('(') != MATCH_YES)
8398 gfc_error ("Expected %<(%> at %C");
8399 return MATCH_ERROR;
8402 /* Match pointer. */
8403 var_locus = gfc_current_locus;
8404 gfc_clear_attr (&current_attr);
8405 gfc_add_cray_pointer (&current_attr, &var_locus);
8406 current_ts.type = BT_INTEGER;
8407 current_ts.kind = gfc_index_integer_kind;
8409 m = gfc_match_symbol (&cptr, 0);
8410 if (m != MATCH_YES)
8412 gfc_error ("Expected variable name at %C");
8413 return m;
8416 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8417 return MATCH_ERROR;
8419 gfc_set_sym_referenced (cptr);
8421 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8423 cptr->ts.type = BT_INTEGER;
8424 cptr->ts.kind = gfc_index_integer_kind;
8426 else if (cptr->ts.type != BT_INTEGER)
8428 gfc_error ("Cray pointer at %C must be an integer");
8429 return MATCH_ERROR;
8431 else if (cptr->ts.kind < gfc_index_integer_kind)
8432 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8433 " memory addresses require %d bytes",
8434 cptr->ts.kind, gfc_index_integer_kind);
8436 if (gfc_match_char (',') != MATCH_YES)
8438 gfc_error ("Expected \",\" at %C");
8439 return MATCH_ERROR;
8442 /* Match Pointee. */
8443 var_locus = gfc_current_locus;
8444 gfc_clear_attr (&current_attr);
8445 gfc_add_cray_pointee (&current_attr, &var_locus);
8446 current_ts.type = BT_UNKNOWN;
8447 current_ts.kind = 0;
8449 m = gfc_match_symbol (&cpte, 0);
8450 if (m != MATCH_YES)
8452 gfc_error ("Expected variable name at %C");
8453 return m;
8456 /* Check for an optional array spec. */
8457 m = gfc_match_array_spec (&as, true, false);
8458 if (m == MATCH_ERROR)
8460 gfc_free_array_spec (as);
8461 return m;
8463 else if (m == MATCH_NO)
8465 gfc_free_array_spec (as);
8466 as = NULL;
8469 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8470 return MATCH_ERROR;
8472 gfc_set_sym_referenced (cpte);
8474 if (cpte->as == NULL)
8476 if (!gfc_set_array_spec (cpte, as, &var_locus))
8477 gfc_internal_error ("Couldn't set Cray pointee array spec.");
8479 else if (as != NULL)
8481 gfc_error ("Duplicate array spec for Cray pointee at %C");
8482 gfc_free_array_spec (as);
8483 return MATCH_ERROR;
8486 as = NULL;
8488 if (cpte->as != NULL)
8490 /* Fix array spec. */
8491 m = gfc_mod_pointee_as (cpte->as);
8492 if (m == MATCH_ERROR)
8493 return m;
8496 /* Point the Pointee at the Pointer. */
8497 cpte->cp_pointer = cptr;
8499 if (gfc_match_char (')') != MATCH_YES)
8501 gfc_error ("Expected \")\" at %C");
8502 return MATCH_ERROR;
8504 m = gfc_match_char (',');
8505 if (m != MATCH_YES)
8506 done = true; /* Stop searching for more declarations. */
8510 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8511 || gfc_match_eos () != MATCH_YES)
8513 gfc_error ("Expected %<,%> or end of statement at %C");
8514 return MATCH_ERROR;
8516 return MATCH_YES;
8520 match
8521 gfc_match_external (void)
8524 gfc_clear_attr (&current_attr);
8525 current_attr.external = 1;
8527 return attr_decl ();
8531 match
8532 gfc_match_intent (void)
8534 sym_intent intent;
8536 /* This is not allowed within a BLOCK construct! */
8537 if (gfc_current_state () == COMP_BLOCK)
8539 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8540 return MATCH_ERROR;
8543 intent = match_intent_spec ();
8544 if (intent == INTENT_UNKNOWN)
8545 return MATCH_ERROR;
8547 gfc_clear_attr (&current_attr);
8548 current_attr.intent = intent;
8550 return attr_decl ();
8554 match
8555 gfc_match_intrinsic (void)
8558 gfc_clear_attr (&current_attr);
8559 current_attr.intrinsic = 1;
8561 return attr_decl ();
8565 match
8566 gfc_match_optional (void)
8568 /* This is not allowed within a BLOCK construct! */
8569 if (gfc_current_state () == COMP_BLOCK)
8571 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8572 return MATCH_ERROR;
8575 gfc_clear_attr (&current_attr);
8576 current_attr.optional = 1;
8578 return attr_decl ();
8582 match
8583 gfc_match_pointer (void)
8585 gfc_gobble_whitespace ();
8586 if (gfc_peek_ascii_char () == '(')
8588 if (!flag_cray_pointer)
8590 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
8591 "flag");
8592 return MATCH_ERROR;
8594 return cray_pointer_decl ();
8596 else
8598 gfc_clear_attr (&current_attr);
8599 current_attr.pointer = 1;
8601 return attr_decl ();
8606 match
8607 gfc_match_allocatable (void)
8609 gfc_clear_attr (&current_attr);
8610 current_attr.allocatable = 1;
8612 return attr_decl ();
8616 match
8617 gfc_match_codimension (void)
8619 gfc_clear_attr (&current_attr);
8620 current_attr.codimension = 1;
8622 return attr_decl ();
8626 match
8627 gfc_match_contiguous (void)
8629 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
8630 return MATCH_ERROR;
8632 gfc_clear_attr (&current_attr);
8633 current_attr.contiguous = 1;
8635 return attr_decl ();
8639 match
8640 gfc_match_dimension (void)
8642 gfc_clear_attr (&current_attr);
8643 current_attr.dimension = 1;
8645 return attr_decl ();
8649 match
8650 gfc_match_target (void)
8652 gfc_clear_attr (&current_attr);
8653 current_attr.target = 1;
8655 return attr_decl ();
8659 /* Match the list of entities being specified in a PUBLIC or PRIVATE
8660 statement. */
8662 static match
8663 access_attr_decl (gfc_statement st)
8665 char name[GFC_MAX_SYMBOL_LEN + 1];
8666 interface_type type;
8667 gfc_user_op *uop;
8668 gfc_symbol *sym, *dt_sym;
8669 gfc_intrinsic_op op;
8670 match m;
8672 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8673 goto done;
8675 for (;;)
8677 m = gfc_match_generic_spec (&type, name, &op);
8678 if (m == MATCH_NO)
8679 goto syntax;
8680 if (m == MATCH_ERROR)
8681 return MATCH_ERROR;
8683 switch (type)
8685 case INTERFACE_NAMELESS:
8686 case INTERFACE_ABSTRACT:
8687 goto syntax;
8689 case INTERFACE_GENERIC:
8690 case INTERFACE_DTIO:
8692 if (gfc_get_symbol (name, NULL, &sym))
8693 goto done;
8695 if (type == INTERFACE_DTIO
8696 && gfc_current_ns->proc_name
8697 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
8698 && sym->attr.flavor == FL_UNKNOWN)
8699 sym->attr.flavor = FL_PROCEDURE;
8701 if (!gfc_add_access (&sym->attr,
8702 (st == ST_PUBLIC)
8703 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8704 sym->name, NULL))
8705 return MATCH_ERROR;
8707 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
8708 && !gfc_add_access (&dt_sym->attr,
8709 (st == ST_PUBLIC)
8710 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8711 sym->name, NULL))
8712 return MATCH_ERROR;
8714 break;
8716 case INTERFACE_INTRINSIC_OP:
8717 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
8719 gfc_intrinsic_op other_op;
8721 gfc_current_ns->operator_access[op] =
8722 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8724 /* Handle the case if there is another op with the same
8725 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
8726 other_op = gfc_equivalent_op (op);
8728 if (other_op != INTRINSIC_NONE)
8729 gfc_current_ns->operator_access[other_op] =
8730 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8733 else
8735 gfc_error ("Access specification of the %s operator at %C has "
8736 "already been specified", gfc_op2string (op));
8737 goto done;
8740 break;
8742 case INTERFACE_USER_OP:
8743 uop = gfc_get_uop (name);
8745 if (uop->access == ACCESS_UNKNOWN)
8747 uop->access = (st == ST_PUBLIC)
8748 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8750 else
8752 gfc_error ("Access specification of the .%s. operator at %C "
8753 "has already been specified", sym->name);
8754 goto done;
8757 break;
8760 if (gfc_match_char (',') == MATCH_NO)
8761 break;
8764 if (gfc_match_eos () != MATCH_YES)
8765 goto syntax;
8766 return MATCH_YES;
8768 syntax:
8769 gfc_syntax_error (st);
8771 done:
8772 return MATCH_ERROR;
8776 match
8777 gfc_match_protected (void)
8779 gfc_symbol *sym;
8780 match m;
8782 if (!gfc_current_ns->proc_name
8783 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
8785 gfc_error ("PROTECTED at %C only allowed in specification "
8786 "part of a module");
8787 return MATCH_ERROR;
8791 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
8792 return MATCH_ERROR;
8794 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8796 return MATCH_ERROR;
8799 if (gfc_match_eos () == MATCH_YES)
8800 goto syntax;
8802 for(;;)
8804 m = gfc_match_symbol (&sym, 0);
8805 switch (m)
8807 case MATCH_YES:
8808 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
8809 return MATCH_ERROR;
8810 goto next_item;
8812 case MATCH_NO:
8813 break;
8815 case MATCH_ERROR:
8816 return MATCH_ERROR;
8819 next_item:
8820 if (gfc_match_eos () == MATCH_YES)
8821 break;
8822 if (gfc_match_char (',') != MATCH_YES)
8823 goto syntax;
8826 return MATCH_YES;
8828 syntax:
8829 gfc_error ("Syntax error in PROTECTED statement at %C");
8830 return MATCH_ERROR;
8834 /* The PRIVATE statement is a bit weird in that it can be an attribute
8835 declaration, but also works as a standalone statement inside of a
8836 type declaration or a module. */
8838 match
8839 gfc_match_private (gfc_statement *st)
8842 if (gfc_match ("private") != MATCH_YES)
8843 return MATCH_NO;
8845 if (gfc_current_state () != COMP_MODULE
8846 && !(gfc_current_state () == COMP_DERIVED
8847 && gfc_state_stack->previous
8848 && gfc_state_stack->previous->state == COMP_MODULE)
8849 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
8850 && gfc_state_stack->previous && gfc_state_stack->previous->previous
8851 && gfc_state_stack->previous->previous->state == COMP_MODULE))
8853 gfc_error ("PRIVATE statement at %C is only allowed in the "
8854 "specification part of a module");
8855 return MATCH_ERROR;
8858 if (gfc_current_state () == COMP_DERIVED)
8860 if (gfc_match_eos () == MATCH_YES)
8862 *st = ST_PRIVATE;
8863 return MATCH_YES;
8866 gfc_syntax_error (ST_PRIVATE);
8867 return MATCH_ERROR;
8870 if (gfc_match_eos () == MATCH_YES)
8872 *st = ST_PRIVATE;
8873 return MATCH_YES;
8876 *st = ST_ATTR_DECL;
8877 return access_attr_decl (ST_PRIVATE);
8881 match
8882 gfc_match_public (gfc_statement *st)
8885 if (gfc_match ("public") != MATCH_YES)
8886 return MATCH_NO;
8888 if (gfc_current_state () != COMP_MODULE)
8890 gfc_error ("PUBLIC statement at %C is only allowed in the "
8891 "specification part of a module");
8892 return MATCH_ERROR;
8895 if (gfc_match_eos () == MATCH_YES)
8897 *st = ST_PUBLIC;
8898 return MATCH_YES;
8901 *st = ST_ATTR_DECL;
8902 return access_attr_decl (ST_PUBLIC);
8906 /* Workhorse for gfc_match_parameter. */
8908 static match
8909 do_parm (void)
8911 gfc_symbol *sym;
8912 gfc_expr *init;
8913 match m;
8914 bool t;
8916 m = gfc_match_symbol (&sym, 0);
8917 if (m == MATCH_NO)
8918 gfc_error ("Expected variable name at %C in PARAMETER statement");
8920 if (m != MATCH_YES)
8921 return m;
8923 if (gfc_match_char ('=') == MATCH_NO)
8925 gfc_error ("Expected = sign in PARAMETER statement at %C");
8926 return MATCH_ERROR;
8929 m = gfc_match_init_expr (&init);
8930 if (m == MATCH_NO)
8931 gfc_error ("Expected expression at %C in PARAMETER statement");
8932 if (m != MATCH_YES)
8933 return m;
8935 if (sym->ts.type == BT_UNKNOWN
8936 && !gfc_set_default_type (sym, 1, NULL))
8938 m = MATCH_ERROR;
8939 goto cleanup;
8942 if (!gfc_check_assign_symbol (sym, NULL, init)
8943 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
8945 m = MATCH_ERROR;
8946 goto cleanup;
8949 if (sym->value)
8951 gfc_error ("Initializing already initialized variable at %C");
8952 m = MATCH_ERROR;
8953 goto cleanup;
8956 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
8957 return (t) ? MATCH_YES : MATCH_ERROR;
8959 cleanup:
8960 gfc_free_expr (init);
8961 return m;
8965 /* Match a parameter statement, with the weird syntax that these have. */
8967 match
8968 gfc_match_parameter (void)
8970 const char *term = " )%t";
8971 match m;
8973 if (gfc_match_char ('(') == MATCH_NO)
8975 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
8976 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
8977 return MATCH_NO;
8978 term = " %t";
8981 for (;;)
8983 m = do_parm ();
8984 if (m != MATCH_YES)
8985 break;
8987 if (gfc_match (term) == MATCH_YES)
8988 break;
8990 if (gfc_match_char (',') != MATCH_YES)
8992 gfc_error ("Unexpected characters in PARAMETER statement at %C");
8993 m = MATCH_ERROR;
8994 break;
8998 return m;
9002 match
9003 gfc_match_automatic (void)
9005 gfc_symbol *sym;
9006 match m;
9007 bool seen_symbol = false;
9009 if (!flag_dec_static)
9011 gfc_error ("%s at %C is a DEC extension, enable with "
9012 "%<-fdec-static%>",
9013 "AUTOMATIC"
9015 return MATCH_ERROR;
9018 gfc_match (" ::");
9020 for (;;)
9022 m = gfc_match_symbol (&sym, 0);
9023 switch (m)
9025 case MATCH_NO:
9026 break;
9028 case MATCH_ERROR:
9029 return MATCH_ERROR;
9031 case MATCH_YES:
9032 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
9033 return MATCH_ERROR;
9034 seen_symbol = true;
9035 break;
9038 if (gfc_match_eos () == MATCH_YES)
9039 break;
9040 if (gfc_match_char (',') != MATCH_YES)
9041 goto syntax;
9044 if (!seen_symbol)
9046 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
9047 return MATCH_ERROR;
9050 return MATCH_YES;
9052 syntax:
9053 gfc_error ("Syntax error in AUTOMATIC statement at %C");
9054 return MATCH_ERROR;
9058 match
9059 gfc_match_static (void)
9061 gfc_symbol *sym;
9062 match m;
9063 bool seen_symbol = false;
9065 if (!flag_dec_static)
9067 gfc_error ("%s at %C is a DEC extension, enable with "
9068 "%<-fdec-static%>",
9069 "STATIC");
9070 return MATCH_ERROR;
9073 gfc_match (" ::");
9075 for (;;)
9077 m = gfc_match_symbol (&sym, 0);
9078 switch (m)
9080 case MATCH_NO:
9081 break;
9083 case MATCH_ERROR:
9084 return MATCH_ERROR;
9086 case MATCH_YES:
9087 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9088 &gfc_current_locus))
9089 return MATCH_ERROR;
9090 seen_symbol = true;
9091 break;
9094 if (gfc_match_eos () == MATCH_YES)
9095 break;
9096 if (gfc_match_char (',') != MATCH_YES)
9097 goto syntax;
9100 if (!seen_symbol)
9102 gfc_error ("Expected entity-list in STATIC statement at %C");
9103 return MATCH_ERROR;
9106 return MATCH_YES;
9108 syntax:
9109 gfc_error ("Syntax error in STATIC statement at %C");
9110 return MATCH_ERROR;
9114 /* Save statements have a special syntax. */
9116 match
9117 gfc_match_save (void)
9119 char n[GFC_MAX_SYMBOL_LEN+1];
9120 gfc_common_head *c;
9121 gfc_symbol *sym;
9122 match m;
9124 if (gfc_match_eos () == MATCH_YES)
9126 if (gfc_current_ns->seen_save)
9128 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9129 "follows previous SAVE statement"))
9130 return MATCH_ERROR;
9133 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9134 return MATCH_YES;
9137 if (gfc_current_ns->save_all)
9139 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9140 "blanket SAVE statement"))
9141 return MATCH_ERROR;
9144 gfc_match (" ::");
9146 for (;;)
9148 m = gfc_match_symbol (&sym, 0);
9149 switch (m)
9151 case MATCH_YES:
9152 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9153 &gfc_current_locus))
9154 return MATCH_ERROR;
9155 goto next_item;
9157 case MATCH_NO:
9158 break;
9160 case MATCH_ERROR:
9161 return MATCH_ERROR;
9164 m = gfc_match (" / %n /", &n);
9165 if (m == MATCH_ERROR)
9166 return MATCH_ERROR;
9167 if (m == MATCH_NO)
9168 goto syntax;
9170 c = gfc_get_common (n, 0);
9171 c->saved = 1;
9173 gfc_current_ns->seen_save = 1;
9175 next_item:
9176 if (gfc_match_eos () == MATCH_YES)
9177 break;
9178 if (gfc_match_char (',') != MATCH_YES)
9179 goto syntax;
9182 return MATCH_YES;
9184 syntax:
9185 gfc_error ("Syntax error in SAVE statement at %C");
9186 return MATCH_ERROR;
9190 match
9191 gfc_match_value (void)
9193 gfc_symbol *sym;
9194 match m;
9196 /* This is not allowed within a BLOCK construct! */
9197 if (gfc_current_state () == COMP_BLOCK)
9199 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9200 return MATCH_ERROR;
9203 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
9204 return MATCH_ERROR;
9206 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9208 return MATCH_ERROR;
9211 if (gfc_match_eos () == MATCH_YES)
9212 goto syntax;
9214 for(;;)
9216 m = gfc_match_symbol (&sym, 0);
9217 switch (m)
9219 case MATCH_YES:
9220 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
9221 return MATCH_ERROR;
9222 goto next_item;
9224 case MATCH_NO:
9225 break;
9227 case MATCH_ERROR:
9228 return MATCH_ERROR;
9231 next_item:
9232 if (gfc_match_eos () == MATCH_YES)
9233 break;
9234 if (gfc_match_char (',') != MATCH_YES)
9235 goto syntax;
9238 return MATCH_YES;
9240 syntax:
9241 gfc_error ("Syntax error in VALUE statement at %C");
9242 return MATCH_ERROR;
9246 match
9247 gfc_match_volatile (void)
9249 gfc_symbol *sym;
9250 char *name;
9251 match m;
9253 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
9254 return MATCH_ERROR;
9256 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9258 return MATCH_ERROR;
9261 if (gfc_match_eos () == MATCH_YES)
9262 goto syntax;
9264 for(;;)
9266 /* VOLATILE is special because it can be added to host-associated
9267 symbols locally. Except for coarrays. */
9268 m = gfc_match_symbol (&sym, 1);
9269 switch (m)
9271 case MATCH_YES:
9272 name = XCNEWVAR (char, strlen (sym->name) + 1);
9273 strcpy (name, sym->name);
9274 if (!check_function_name (name))
9275 return MATCH_ERROR;
9276 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9277 for variable in a BLOCK which is defined outside of the BLOCK. */
9278 if (sym->ns != gfc_current_ns && sym->attr.codimension)
9280 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
9281 "%C, which is use-/host-associated", sym->name);
9282 return MATCH_ERROR;
9284 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
9285 return MATCH_ERROR;
9286 goto next_item;
9288 case MATCH_NO:
9289 break;
9291 case MATCH_ERROR:
9292 return MATCH_ERROR;
9295 next_item:
9296 if (gfc_match_eos () == MATCH_YES)
9297 break;
9298 if (gfc_match_char (',') != MATCH_YES)
9299 goto syntax;
9302 return MATCH_YES;
9304 syntax:
9305 gfc_error ("Syntax error in VOLATILE statement at %C");
9306 return MATCH_ERROR;
9310 match
9311 gfc_match_asynchronous (void)
9313 gfc_symbol *sym;
9314 char *name;
9315 match m;
9317 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
9318 return MATCH_ERROR;
9320 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9322 return MATCH_ERROR;
9325 if (gfc_match_eos () == MATCH_YES)
9326 goto syntax;
9328 for(;;)
9330 /* ASYNCHRONOUS is special because it can be added to host-associated
9331 symbols locally. */
9332 m = gfc_match_symbol (&sym, 1);
9333 switch (m)
9335 case MATCH_YES:
9336 name = XCNEWVAR (char, strlen (sym->name) + 1);
9337 strcpy (name, sym->name);
9338 if (!check_function_name (name))
9339 return MATCH_ERROR;
9340 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9341 return MATCH_ERROR;
9342 goto next_item;
9344 case MATCH_NO:
9345 break;
9347 case MATCH_ERROR:
9348 return MATCH_ERROR;
9351 next_item:
9352 if (gfc_match_eos () == MATCH_YES)
9353 break;
9354 if (gfc_match_char (',') != MATCH_YES)
9355 goto syntax;
9358 return MATCH_YES;
9360 syntax:
9361 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9362 return MATCH_ERROR;
9366 /* Match a module procedure statement in a submodule. */
9368 match
9369 gfc_match_submod_proc (void)
9371 char name[GFC_MAX_SYMBOL_LEN + 1];
9372 gfc_symbol *sym, *fsym;
9373 match m;
9374 gfc_formal_arglist *formal, *head, *tail;
9376 if (gfc_current_state () != COMP_CONTAINS
9377 || !(gfc_state_stack->previous
9378 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9379 || gfc_state_stack->previous->state == COMP_MODULE)))
9380 return MATCH_NO;
9382 m = gfc_match (" module% procedure% %n", name);
9383 if (m != MATCH_YES)
9384 return m;
9386 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9387 "at %C"))
9388 return MATCH_ERROR;
9390 if (get_proc_name (name, &sym, false))
9391 return MATCH_ERROR;
9393 /* Make sure that the result field is appropriately filled, even though
9394 the result symbol will be replaced later on. */
9395 if (sym->tlink && sym->tlink->attr.function)
9397 if (sym->tlink->result
9398 && sym->tlink->result != sym->tlink)
9399 sym->result= sym->tlink->result;
9400 else
9401 sym->result = sym;
9404 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9405 the symbol existed before. */
9406 sym->declared_at = gfc_current_locus;
9408 if (!sym->attr.module_procedure)
9409 return MATCH_ERROR;
9411 /* Signal match_end to expect "end procedure". */
9412 sym->abr_modproc_decl = 1;
9414 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9415 sym->attr.if_source = IFSRC_DECL;
9417 gfc_new_block = sym;
9419 /* Make a new formal arglist with the symbols in the procedure
9420 namespace. */
9421 head = tail = NULL;
9422 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9424 if (formal == sym->formal)
9425 head = tail = gfc_get_formal_arglist ();
9426 else
9428 tail->next = gfc_get_formal_arglist ();
9429 tail = tail->next;
9432 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9433 goto cleanup;
9435 tail->sym = fsym;
9436 gfc_set_sym_referenced (fsym);
9439 /* The dummy symbols get cleaned up, when the formal_namespace of the
9440 interface declaration is cleared. This allows us to add the
9441 explicit interface as is done for other type of procedure. */
9442 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9443 &gfc_current_locus))
9444 return MATCH_ERROR;
9446 if (gfc_match_eos () != MATCH_YES)
9448 gfc_syntax_error (ST_MODULE_PROC);
9449 return MATCH_ERROR;
9452 return MATCH_YES;
9454 cleanup:
9455 gfc_free_formal_arglist (head);
9456 return MATCH_ERROR;
9460 /* Match a module procedure statement. Note that we have to modify
9461 symbols in the parent's namespace because the current one was there
9462 to receive symbols that are in an interface's formal argument list. */
9464 match
9465 gfc_match_modproc (void)
9467 char name[GFC_MAX_SYMBOL_LEN + 1];
9468 gfc_symbol *sym;
9469 match m;
9470 locus old_locus;
9471 gfc_namespace *module_ns;
9472 gfc_interface *old_interface_head, *interface;
9474 if (gfc_state_stack->state != COMP_INTERFACE
9475 || gfc_state_stack->previous == NULL
9476 || current_interface.type == INTERFACE_NAMELESS
9477 || current_interface.type == INTERFACE_ABSTRACT)
9479 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9480 "interface");
9481 return MATCH_ERROR;
9484 module_ns = gfc_current_ns->parent;
9485 for (; module_ns; module_ns = module_ns->parent)
9486 if (module_ns->proc_name->attr.flavor == FL_MODULE
9487 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9488 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9489 && !module_ns->proc_name->attr.contained))
9490 break;
9492 if (module_ns == NULL)
9493 return MATCH_ERROR;
9495 /* Store the current state of the interface. We will need it if we
9496 end up with a syntax error and need to recover. */
9497 old_interface_head = gfc_current_interface_head ();
9499 /* Check if the F2008 optional double colon appears. */
9500 gfc_gobble_whitespace ();
9501 old_locus = gfc_current_locus;
9502 if (gfc_match ("::") == MATCH_YES)
9504 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9505 "MODULE PROCEDURE statement at %L", &old_locus))
9506 return MATCH_ERROR;
9508 else
9509 gfc_current_locus = old_locus;
9511 for (;;)
9513 bool last = false;
9514 old_locus = gfc_current_locus;
9516 m = gfc_match_name (name);
9517 if (m == MATCH_NO)
9518 goto syntax;
9519 if (m != MATCH_YES)
9520 return MATCH_ERROR;
9522 /* Check for syntax error before starting to add symbols to the
9523 current namespace. */
9524 if (gfc_match_eos () == MATCH_YES)
9525 last = true;
9527 if (!last && gfc_match_char (',') != MATCH_YES)
9528 goto syntax;
9530 /* Now we're sure the syntax is valid, we process this item
9531 further. */
9532 if (gfc_get_symbol (name, module_ns, &sym))
9533 return MATCH_ERROR;
9535 if (sym->attr.intrinsic)
9537 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9538 "PROCEDURE", &old_locus);
9539 return MATCH_ERROR;
9542 if (sym->attr.proc != PROC_MODULE
9543 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9544 return MATCH_ERROR;
9546 if (!gfc_add_interface (sym))
9547 return MATCH_ERROR;
9549 sym->attr.mod_proc = 1;
9550 sym->declared_at = old_locus;
9552 if (last)
9553 break;
9556 return MATCH_YES;
9558 syntax:
9559 /* Restore the previous state of the interface. */
9560 interface = gfc_current_interface_head ();
9561 gfc_set_current_interface_head (old_interface_head);
9563 /* Free the new interfaces. */
9564 while (interface != old_interface_head)
9566 gfc_interface *i = interface->next;
9567 free (interface);
9568 interface = i;
9571 /* And issue a syntax error. */
9572 gfc_syntax_error (ST_MODULE_PROC);
9573 return MATCH_ERROR;
9577 /* Check a derived type that is being extended. */
9579 static gfc_symbol*
9580 check_extended_derived_type (char *name)
9582 gfc_symbol *extended;
9584 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9586 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9587 return NULL;
9590 extended = gfc_find_dt_in_generic (extended);
9592 /* F08:C428. */
9593 if (!extended)
9595 gfc_error ("Symbol %qs at %C has not been previously defined", name);
9596 return NULL;
9599 if (extended->attr.flavor != FL_DERIVED)
9601 gfc_error ("%qs in EXTENDS expression at %C is not a "
9602 "derived type", name);
9603 return NULL;
9606 if (extended->attr.is_bind_c)
9608 gfc_error ("%qs cannot be extended at %C because it "
9609 "is BIND(C)", extended->name);
9610 return NULL;
9613 if (extended->attr.sequence)
9615 gfc_error ("%qs cannot be extended at %C because it "
9616 "is a SEQUENCE type", extended->name);
9617 return NULL;
9620 return extended;
9624 /* Match the optional attribute specifiers for a type declaration.
9625 Return MATCH_ERROR if an error is encountered in one of the handled
9626 attributes (public, private, bind(c)), MATCH_NO if what's found is
9627 not a handled attribute, and MATCH_YES otherwise. TODO: More error
9628 checking on attribute conflicts needs to be done. */
9630 match
9631 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
9633 /* See if the derived type is marked as private. */
9634 if (gfc_match (" , private") == MATCH_YES)
9636 if (gfc_current_state () != COMP_MODULE)
9638 gfc_error ("Derived type at %C can only be PRIVATE in the "
9639 "specification part of a module");
9640 return MATCH_ERROR;
9643 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
9644 return MATCH_ERROR;
9646 else if (gfc_match (" , public") == MATCH_YES)
9648 if (gfc_current_state () != COMP_MODULE)
9650 gfc_error ("Derived type at %C can only be PUBLIC in the "
9651 "specification part of a module");
9652 return MATCH_ERROR;
9655 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
9656 return MATCH_ERROR;
9658 else if (gfc_match (" , bind ( c )") == MATCH_YES)
9660 /* If the type is defined to be bind(c) it then needs to make
9661 sure that all fields are interoperable. This will
9662 need to be a semantic check on the finished derived type.
9663 See 15.2.3 (lines 9-12) of F2003 draft. */
9664 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
9665 return MATCH_ERROR;
9667 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
9669 else if (gfc_match (" , abstract") == MATCH_YES)
9671 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
9672 return MATCH_ERROR;
9674 if (!gfc_add_abstract (attr, &gfc_current_locus))
9675 return MATCH_ERROR;
9677 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
9679 if (!gfc_add_extension (attr, &gfc_current_locus))
9680 return MATCH_ERROR;
9682 else
9683 return MATCH_NO;
9685 /* If we get here, something matched. */
9686 return MATCH_YES;
9690 /* Common function for type declaration blocks similar to derived types, such
9691 as STRUCTURES and MAPs. Unlike derived types, a structure type
9692 does NOT have a generic symbol matching the name given by the user.
9693 STRUCTUREs can share names with variables and PARAMETERs so we must allow
9694 for the creation of an independent symbol.
9695 Other parameters are a message to prefix errors with, the name of the new
9696 type to be created, and the flavor to add to the resulting symbol. */
9698 static bool
9699 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
9700 gfc_symbol **result)
9702 gfc_symbol *sym;
9703 locus where;
9705 gcc_assert (name[0] == (char) TOUPPER (name[0]));
9707 if (decl)
9708 where = *decl;
9709 else
9710 where = gfc_current_locus;
9712 if (gfc_get_symbol (name, NULL, &sym))
9713 return false;
9715 if (!sym)
9717 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
9718 return false;
9721 if (sym->components != NULL || sym->attr.zero_comp)
9723 gfc_error ("Type definition of %qs at %C was already defined at %L",
9724 sym->name, &sym->declared_at);
9725 return false;
9728 sym->declared_at = where;
9730 if (sym->attr.flavor != fl
9731 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
9732 return false;
9734 if (!sym->hash_value)
9735 /* Set the hash for the compound name for this type. */
9736 sym->hash_value = gfc_hash_value (sym);
9738 /* Normally the type is expected to have been completely parsed by the time
9739 a field declaration with this type is seen. For unions, maps, and nested
9740 structure declarations, we need to indicate that it is okay that we
9741 haven't seen any components yet. This will be updated after the structure
9742 is fully parsed. */
9743 sym->attr.zero_comp = 0;
9745 /* Structures always act like derived-types with the SEQUENCE attribute */
9746 gfc_add_sequence (&sym->attr, sym->name, NULL);
9748 if (result) *result = sym;
9750 return true;
9754 /* Match the opening of a MAP block. Like a struct within a union in C;
9755 behaves identical to STRUCTURE blocks. */
9757 match
9758 gfc_match_map (void)
9760 /* Counter used to give unique internal names to map structures. */
9761 static unsigned int gfc_map_id = 0;
9762 char name[GFC_MAX_SYMBOL_LEN + 1];
9763 gfc_symbol *sym;
9764 locus old_loc;
9766 old_loc = gfc_current_locus;
9768 if (gfc_match_eos () != MATCH_YES)
9770 gfc_error ("Junk after MAP statement at %C");
9771 gfc_current_locus = old_loc;
9772 return MATCH_ERROR;
9775 /* Map blocks are anonymous so we make up unique names for the symbol table
9776 which are invalid Fortran identifiers. */
9777 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
9779 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
9780 return MATCH_ERROR;
9782 gfc_new_block = sym;
9784 return MATCH_YES;
9788 /* Match the opening of a UNION block. */
9790 match
9791 gfc_match_union (void)
9793 /* Counter used to give unique internal names to union types. */
9794 static unsigned int gfc_union_id = 0;
9795 char name[GFC_MAX_SYMBOL_LEN + 1];
9796 gfc_symbol *sym;
9797 locus old_loc;
9799 old_loc = gfc_current_locus;
9801 if (gfc_match_eos () != MATCH_YES)
9803 gfc_error ("Junk after UNION statement at %C");
9804 gfc_current_locus = old_loc;
9805 return MATCH_ERROR;
9808 /* Unions are anonymous so we make up unique names for the symbol table
9809 which are invalid Fortran identifiers. */
9810 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
9812 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
9813 return MATCH_ERROR;
9815 gfc_new_block = sym;
9817 return MATCH_YES;
9821 /* Match the beginning of a STRUCTURE declaration. This is similar to
9822 matching the beginning of a derived type declaration with a few
9823 twists. The resulting type symbol has no access control or other
9824 interesting attributes. */
9826 match
9827 gfc_match_structure_decl (void)
9829 /* Counter used to give unique internal names to anonymous structures. */
9830 static unsigned int gfc_structure_id = 0;
9831 char name[GFC_MAX_SYMBOL_LEN + 1];
9832 gfc_symbol *sym;
9833 match m;
9834 locus where;
9836 if (!flag_dec_structure)
9838 gfc_error ("%s at %C is a DEC extension, enable with "
9839 "%<-fdec-structure%>",
9840 "STRUCTURE");
9841 return MATCH_ERROR;
9844 name[0] = '\0';
9846 m = gfc_match (" /%n/", name);
9847 if (m != MATCH_YES)
9849 /* Non-nested structure declarations require a structure name. */
9850 if (!gfc_comp_struct (gfc_current_state ()))
9852 gfc_error ("Structure name expected in non-nested structure "
9853 "declaration at %C");
9854 return MATCH_ERROR;
9856 /* This is an anonymous structure; make up a unique name for it
9857 (upper-case letters never make it to symbol names from the source).
9858 The important thing is initializing the type variable
9859 and setting gfc_new_symbol, which is immediately used by
9860 parse_structure () and variable_decl () to add components of
9861 this type. */
9862 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
9865 where = gfc_current_locus;
9866 /* No field list allowed after non-nested structure declaration. */
9867 if (!gfc_comp_struct (gfc_current_state ())
9868 && gfc_match_eos () != MATCH_YES)
9870 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
9871 return MATCH_ERROR;
9874 /* Make sure the name is not the name of an intrinsic type. */
9875 if (gfc_is_intrinsic_typename (name))
9877 gfc_error ("Structure name %qs at %C cannot be the same as an"
9878 " intrinsic type", name);
9879 return MATCH_ERROR;
9882 /* Store the actual type symbol for the structure with an upper-case first
9883 letter (an invalid Fortran identifier). */
9885 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
9886 return MATCH_ERROR;
9888 gfc_new_block = sym;
9889 return MATCH_YES;
9893 /* This function does some work to determine which matcher should be used to
9894 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
9895 * as an alias for PRINT from derived type declarations, TYPE IS statements,
9896 * and [parameterized] derived type declarations. */
9898 match
9899 gfc_match_type (gfc_statement *st)
9901 char name[GFC_MAX_SYMBOL_LEN + 1];
9902 match m;
9903 locus old_loc;
9905 /* Requires -fdec. */
9906 if (!flag_dec)
9907 return MATCH_NO;
9909 m = gfc_match ("type");
9910 if (m != MATCH_YES)
9911 return m;
9912 /* If we already have an error in the buffer, it is probably from failing to
9913 * match a derived type data declaration. Let it happen. */
9914 else if (gfc_error_flag_test ())
9915 return MATCH_NO;
9917 old_loc = gfc_current_locus;
9918 *st = ST_NONE;
9920 /* If we see an attribute list before anything else it's definitely a derived
9921 * type declaration. */
9922 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
9923 goto derived;
9925 /* By now "TYPE" has already been matched. If we do not see a name, this may
9926 * be something like "TYPE *" or "TYPE <fmt>". */
9927 m = gfc_match_name (name);
9928 if (m != MATCH_YES)
9930 /* Let print match if it can, otherwise throw an error from
9931 * gfc_match_derived_decl. */
9932 gfc_current_locus = old_loc;
9933 if (gfc_match_print () == MATCH_YES)
9935 *st = ST_WRITE;
9936 return MATCH_YES;
9938 goto derived;
9941 /* Check for EOS. */
9942 if (gfc_match_eos () == MATCH_YES)
9944 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
9945 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
9946 * Otherwise if gfc_match_derived_decl fails it's probably an existing
9947 * symbol which can be printed. */
9948 gfc_current_locus = old_loc;
9949 m = gfc_match_derived_decl ();
9950 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
9952 *st = ST_DERIVED_DECL;
9953 return m;
9956 else
9958 /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
9959 like <type name(parameter)>. */
9960 gfc_gobble_whitespace ();
9961 bool paren = gfc_peek_ascii_char () == '(';
9962 if (paren)
9964 if (strcmp ("is", name) == 0)
9965 goto typeis;
9966 else
9967 goto derived;
9971 /* Treat TYPE... like PRINT... */
9972 gfc_current_locus = old_loc;
9973 *st = ST_WRITE;
9974 return gfc_match_print ();
9976 derived:
9977 gfc_current_locus = old_loc;
9978 *st = ST_DERIVED_DECL;
9979 return gfc_match_derived_decl ();
9981 typeis:
9982 gfc_current_locus = old_loc;
9983 *st = ST_TYPE_IS;
9984 return gfc_match_type_is ();
9988 /* Match the beginning of a derived type declaration. If a type name
9989 was the result of a function, then it is possible to have a symbol
9990 already to be known as a derived type yet have no components. */
9992 match
9993 gfc_match_derived_decl (void)
9995 char name[GFC_MAX_SYMBOL_LEN + 1];
9996 char parent[GFC_MAX_SYMBOL_LEN + 1];
9997 symbol_attribute attr;
9998 gfc_symbol *sym, *gensym;
9999 gfc_symbol *extended;
10000 match m;
10001 match is_type_attr_spec = MATCH_NO;
10002 bool seen_attr = false;
10003 gfc_interface *intr = NULL, *head;
10004 bool parameterized_type = false;
10005 bool seen_colons = false;
10007 if (gfc_comp_struct (gfc_current_state ()))
10008 return MATCH_NO;
10010 name[0] = '\0';
10011 parent[0] = '\0';
10012 gfc_clear_attr (&attr);
10013 extended = NULL;
10017 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
10018 if (is_type_attr_spec == MATCH_ERROR)
10019 return MATCH_ERROR;
10020 if (is_type_attr_spec == MATCH_YES)
10021 seen_attr = true;
10022 } while (is_type_attr_spec == MATCH_YES);
10024 /* Deal with derived type extensions. The extension attribute has
10025 been added to 'attr' but now the parent type must be found and
10026 checked. */
10027 if (parent[0])
10028 extended = check_extended_derived_type (parent);
10030 if (parent[0] && !extended)
10031 return MATCH_ERROR;
10033 m = gfc_match (" ::");
10034 if (m == MATCH_YES)
10036 seen_colons = true;
10038 else if (seen_attr)
10040 gfc_error ("Expected :: in TYPE definition at %C");
10041 return MATCH_ERROR;
10044 m = gfc_match (" %n ", name);
10045 if (m != MATCH_YES)
10046 return m;
10048 /* Make sure that we don't identify TYPE IS (...) as a parameterized
10049 derived type named 'is'.
10050 TODO Expand the check, when 'name' = "is" by matching " (tname) "
10051 and checking if this is a(n intrinsic) typename. his picks up
10052 misplaced TYPE IS statements such as in select_type_1.f03. */
10053 if (gfc_peek_ascii_char () == '(')
10055 if (gfc_current_state () == COMP_SELECT_TYPE
10056 || (!seen_colons && !strcmp (name, "is")))
10057 return MATCH_NO;
10058 parameterized_type = true;
10061 m = gfc_match_eos ();
10062 if (m != MATCH_YES && !parameterized_type)
10063 return m;
10065 /* Make sure the name is not the name of an intrinsic type. */
10066 if (gfc_is_intrinsic_typename (name))
10068 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
10069 "type", name);
10070 return MATCH_ERROR;
10073 if (gfc_get_symbol (name, NULL, &gensym))
10074 return MATCH_ERROR;
10076 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
10078 if (gensym->ts.u.derived)
10079 gfc_error ("Derived type name %qs at %C already has a basic type "
10080 "of %s", gensym->name, gfc_typename (&gensym->ts));
10081 else
10082 gfc_error ("Derived type name %qs at %C already has a basic type",
10083 gensym->name);
10084 return MATCH_ERROR;
10087 if (!gensym->attr.generic
10088 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
10089 return MATCH_ERROR;
10091 if (!gensym->attr.function
10092 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
10093 return MATCH_ERROR;
10095 sym = gfc_find_dt_in_generic (gensym);
10097 if (sym && (sym->components != NULL || sym->attr.zero_comp))
10099 gfc_error ("Derived type definition of %qs at %C has already been "
10100 "defined", sym->name);
10101 return MATCH_ERROR;
10104 if (!sym)
10106 /* Use upper case to save the actual derived-type symbol. */
10107 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
10108 sym->name = gfc_get_string ("%s", gensym->name);
10109 head = gensym->generic;
10110 intr = gfc_get_interface ();
10111 intr->sym = sym;
10112 intr->where = gfc_current_locus;
10113 intr->sym->declared_at = gfc_current_locus;
10114 intr->next = head;
10115 gensym->generic = intr;
10116 gensym->attr.if_source = IFSRC_DECL;
10119 /* The symbol may already have the derived attribute without the
10120 components. The ways this can happen is via a function
10121 definition, an INTRINSIC statement or a subtype in another
10122 derived type that is a pointer. The first part of the AND clause
10123 is true if the symbol is not the return value of a function. */
10124 if (sym->attr.flavor != FL_DERIVED
10125 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
10126 return MATCH_ERROR;
10128 if (attr.access != ACCESS_UNKNOWN
10129 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
10130 return MATCH_ERROR;
10131 else if (sym->attr.access == ACCESS_UNKNOWN
10132 && gensym->attr.access != ACCESS_UNKNOWN
10133 && !gfc_add_access (&sym->attr, gensym->attr.access,
10134 sym->name, NULL))
10135 return MATCH_ERROR;
10137 if (sym->attr.access != ACCESS_UNKNOWN
10138 && gensym->attr.access == ACCESS_UNKNOWN)
10139 gensym->attr.access = sym->attr.access;
10141 /* See if the derived type was labeled as bind(c). */
10142 if (attr.is_bind_c != 0)
10143 sym->attr.is_bind_c = attr.is_bind_c;
10145 /* Construct the f2k_derived namespace if it is not yet there. */
10146 if (!sym->f2k_derived)
10147 sym->f2k_derived = gfc_get_namespace (NULL, 0);
10149 if (parameterized_type)
10151 /* Ignore error or mismatches by going to the end of the statement
10152 in order to avoid the component declarations causing problems. */
10153 m = gfc_match_formal_arglist (sym, 0, 0, true);
10154 if (m != MATCH_YES)
10155 gfc_error_recovery ();
10156 m = gfc_match_eos ();
10157 if (m != MATCH_YES)
10159 gfc_error_recovery ();
10160 gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10162 sym->attr.pdt_template = 1;
10165 if (extended && !sym->components)
10167 gfc_component *p;
10168 gfc_formal_arglist *f, *g, *h;
10170 /* Add the extended derived type as the first component. */
10171 gfc_add_component (sym, parent, &p);
10172 extended->refs++;
10173 gfc_set_sym_referenced (extended);
10175 p->ts.type = BT_DERIVED;
10176 p->ts.u.derived = extended;
10177 p->initializer = gfc_default_initializer (&p->ts);
10179 /* Set extension level. */
10180 if (extended->attr.extension == 255)
10182 /* Since the extension field is 8 bit wide, we can only have
10183 up to 255 extension levels. */
10184 gfc_error ("Maximum extension level reached with type %qs at %L",
10185 extended->name, &extended->declared_at);
10186 return MATCH_ERROR;
10188 sym->attr.extension = extended->attr.extension + 1;
10190 /* Provide the links between the extended type and its extension. */
10191 if (!extended->f2k_derived)
10192 extended->f2k_derived = gfc_get_namespace (NULL, 0);
10194 /* Copy the extended type-param-name-list from the extended type,
10195 append those of the extension and add the whole lot to the
10196 extension. */
10197 if (extended->attr.pdt_template)
10199 g = h = NULL;
10200 sym->attr.pdt_template = 1;
10201 for (f = extended->formal; f; f = f->next)
10203 if (f == extended->formal)
10205 g = gfc_get_formal_arglist ();
10206 h = g;
10208 else
10210 g->next = gfc_get_formal_arglist ();
10211 g = g->next;
10213 g->sym = f->sym;
10215 g->next = sym->formal;
10216 sym->formal = h;
10220 if (!sym->hash_value)
10221 /* Set the hash for the compound name for this type. */
10222 sym->hash_value = gfc_hash_value (sym);
10224 /* Take over the ABSTRACT attribute. */
10225 sym->attr.abstract = attr.abstract;
10227 gfc_new_block = sym;
10229 return MATCH_YES;
10233 /* Cray Pointees can be declared as:
10234 pointer (ipt, a (n,m,...,*)) */
10236 match
10237 gfc_mod_pointee_as (gfc_array_spec *as)
10239 as->cray_pointee = true; /* This will be useful to know later. */
10240 if (as->type == AS_ASSUMED_SIZE)
10241 as->cp_was_assumed = true;
10242 else if (as->type == AS_ASSUMED_SHAPE)
10244 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10245 return MATCH_ERROR;
10247 return MATCH_YES;
10251 /* Match the enum definition statement, here we are trying to match
10252 the first line of enum definition statement.
10253 Returns MATCH_YES if match is found. */
10255 match
10256 gfc_match_enum (void)
10258 match m;
10260 m = gfc_match_eos ();
10261 if (m != MATCH_YES)
10262 return m;
10264 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
10265 return MATCH_ERROR;
10267 return MATCH_YES;
10271 /* Returns an initializer whose value is one higher than the value of the
10272 LAST_INITIALIZER argument. If the argument is NULL, the
10273 initializers value will be set to zero. The initializer's kind
10274 will be set to gfc_c_int_kind.
10276 If -fshort-enums is given, the appropriate kind will be selected
10277 later after all enumerators have been parsed. A warning is issued
10278 here if an initializer exceeds gfc_c_int_kind. */
10280 static gfc_expr *
10281 enum_initializer (gfc_expr *last_initializer, locus where)
10283 gfc_expr *result;
10284 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
10286 mpz_init (result->value.integer);
10288 if (last_initializer != NULL)
10290 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10291 result->where = last_initializer->where;
10293 if (gfc_check_integer_range (result->value.integer,
10294 gfc_c_int_kind) != ARITH_OK)
10296 gfc_error ("Enumerator exceeds the C integer type at %C");
10297 return NULL;
10300 else
10302 /* Control comes here, if it's the very first enumerator and no
10303 initializer has been given. It will be initialized to zero. */
10304 mpz_set_si (result->value.integer, 0);
10307 return result;
10311 /* Match a variable name with an optional initializer. When this
10312 subroutine is called, a variable is expected to be parsed next.
10313 Depending on what is happening at the moment, updates either the
10314 symbol table or the current interface. */
10316 static match
10317 enumerator_decl (void)
10319 char name[GFC_MAX_SYMBOL_LEN + 1];
10320 gfc_expr *initializer;
10321 gfc_array_spec *as = NULL;
10322 gfc_symbol *sym;
10323 locus var_locus;
10324 match m;
10325 bool t;
10326 locus old_locus;
10328 initializer = NULL;
10329 old_locus = gfc_current_locus;
10331 /* When we get here, we've just matched a list of attributes and
10332 maybe a type and a double colon. The next thing we expect to see
10333 is the name of the symbol. */
10334 m = gfc_match_name (name);
10335 if (m != MATCH_YES)
10336 goto cleanup;
10338 var_locus = gfc_current_locus;
10340 /* OK, we've successfully matched the declaration. Now put the
10341 symbol in the current namespace. If we fail to create the symbol,
10342 bail out. */
10343 if (!build_sym (name, NULL, false, &as, &var_locus))
10345 m = MATCH_ERROR;
10346 goto cleanup;
10349 /* The double colon must be present in order to have initializers.
10350 Otherwise the statement is ambiguous with an assignment statement. */
10351 if (colon_seen)
10353 if (gfc_match_char ('=') == MATCH_YES)
10355 m = gfc_match_init_expr (&initializer);
10356 if (m == MATCH_NO)
10358 gfc_error ("Expected an initialization expression at %C");
10359 m = MATCH_ERROR;
10362 if (m != MATCH_YES)
10363 goto cleanup;
10367 /* If we do not have an initializer, the initialization value of the
10368 previous enumerator (stored in last_initializer) is incremented
10369 by 1 and is used to initialize the current enumerator. */
10370 if (initializer == NULL)
10371 initializer = enum_initializer (last_initializer, old_locus);
10373 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10375 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10376 &var_locus);
10377 m = MATCH_ERROR;
10378 goto cleanup;
10381 /* Store this current initializer, for the next enumerator variable
10382 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10383 use last_initializer below. */
10384 last_initializer = initializer;
10385 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10387 /* Maintain enumerator history. */
10388 gfc_find_symbol (name, NULL, 0, &sym);
10389 create_enum_history (sym, last_initializer);
10391 return (t) ? MATCH_YES : MATCH_ERROR;
10393 cleanup:
10394 /* Free stuff up and return. */
10395 gfc_free_expr (initializer);
10397 return m;
10401 /* Match the enumerator definition statement. */
10403 match
10404 gfc_match_enumerator_def (void)
10406 match m;
10407 bool t;
10409 gfc_clear_ts (&current_ts);
10411 m = gfc_match (" enumerator");
10412 if (m != MATCH_YES)
10413 return m;
10415 m = gfc_match (" :: ");
10416 if (m == MATCH_ERROR)
10417 return m;
10419 colon_seen = (m == MATCH_YES);
10421 if (gfc_current_state () != COMP_ENUM)
10423 gfc_error ("ENUM definition statement expected before %C");
10424 gfc_free_enum_history ();
10425 return MATCH_ERROR;
10428 (&current_ts)->type = BT_INTEGER;
10429 (&current_ts)->kind = gfc_c_int_kind;
10431 gfc_clear_attr (&current_attr);
10432 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10433 if (!t)
10435 m = MATCH_ERROR;
10436 goto cleanup;
10439 for (;;)
10441 m = enumerator_decl ();
10442 if (m == MATCH_ERROR)
10444 gfc_free_enum_history ();
10445 goto cleanup;
10447 if (m == MATCH_NO)
10448 break;
10450 if (gfc_match_eos () == MATCH_YES)
10451 goto cleanup;
10452 if (gfc_match_char (',') != MATCH_YES)
10453 break;
10456 if (gfc_current_state () == COMP_ENUM)
10458 gfc_free_enum_history ();
10459 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10460 m = MATCH_ERROR;
10463 cleanup:
10464 gfc_free_array_spec (current_as);
10465 current_as = NULL;
10466 return m;
10471 /* Match binding attributes. */
10473 static match
10474 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10476 bool found_passing = false;
10477 bool seen_ptr = false;
10478 match m = MATCH_YES;
10480 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
10481 this case the defaults are in there. */
10482 ba->access = ACCESS_UNKNOWN;
10483 ba->pass_arg = NULL;
10484 ba->pass_arg_num = 0;
10485 ba->nopass = 0;
10486 ba->non_overridable = 0;
10487 ba->deferred = 0;
10488 ba->ppc = ppc;
10490 /* If we find a comma, we believe there are binding attributes. */
10491 m = gfc_match_char (',');
10492 if (m == MATCH_NO)
10493 goto done;
10497 /* Access specifier. */
10499 m = gfc_match (" public");
10500 if (m == MATCH_ERROR)
10501 goto error;
10502 if (m == MATCH_YES)
10504 if (ba->access != ACCESS_UNKNOWN)
10506 gfc_error ("Duplicate access-specifier at %C");
10507 goto error;
10510 ba->access = ACCESS_PUBLIC;
10511 continue;
10514 m = gfc_match (" private");
10515 if (m == MATCH_ERROR)
10516 goto error;
10517 if (m == MATCH_YES)
10519 if (ba->access != ACCESS_UNKNOWN)
10521 gfc_error ("Duplicate access-specifier at %C");
10522 goto error;
10525 ba->access = ACCESS_PRIVATE;
10526 continue;
10529 /* If inside GENERIC, the following is not allowed. */
10530 if (!generic)
10533 /* NOPASS flag. */
10534 m = gfc_match (" nopass");
10535 if (m == MATCH_ERROR)
10536 goto error;
10537 if (m == MATCH_YES)
10539 if (found_passing)
10541 gfc_error ("Binding attributes already specify passing,"
10542 " illegal NOPASS at %C");
10543 goto error;
10546 found_passing = true;
10547 ba->nopass = 1;
10548 continue;
10551 /* PASS possibly including argument. */
10552 m = gfc_match (" pass");
10553 if (m == MATCH_ERROR)
10554 goto error;
10555 if (m == MATCH_YES)
10557 char arg[GFC_MAX_SYMBOL_LEN + 1];
10559 if (found_passing)
10561 gfc_error ("Binding attributes already specify passing,"
10562 " illegal PASS at %C");
10563 goto error;
10566 m = gfc_match (" ( %n )", arg);
10567 if (m == MATCH_ERROR)
10568 goto error;
10569 if (m == MATCH_YES)
10570 ba->pass_arg = gfc_get_string ("%s", arg);
10571 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10573 found_passing = true;
10574 ba->nopass = 0;
10575 continue;
10578 if (ppc)
10580 /* POINTER flag. */
10581 m = gfc_match (" pointer");
10582 if (m == MATCH_ERROR)
10583 goto error;
10584 if (m == MATCH_YES)
10586 if (seen_ptr)
10588 gfc_error ("Duplicate POINTER attribute at %C");
10589 goto error;
10592 seen_ptr = true;
10593 continue;
10596 else
10598 /* NON_OVERRIDABLE flag. */
10599 m = gfc_match (" non_overridable");
10600 if (m == MATCH_ERROR)
10601 goto error;
10602 if (m == MATCH_YES)
10604 if (ba->non_overridable)
10606 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
10607 goto error;
10610 ba->non_overridable = 1;
10611 continue;
10614 /* DEFERRED flag. */
10615 m = gfc_match (" deferred");
10616 if (m == MATCH_ERROR)
10617 goto error;
10618 if (m == MATCH_YES)
10620 if (ba->deferred)
10622 gfc_error ("Duplicate DEFERRED at %C");
10623 goto error;
10626 ba->deferred = 1;
10627 continue;
10633 /* Nothing matching found. */
10634 if (generic)
10635 gfc_error ("Expected access-specifier at %C");
10636 else
10637 gfc_error ("Expected binding attribute at %C");
10638 goto error;
10640 while (gfc_match_char (',') == MATCH_YES);
10642 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
10643 if (ba->non_overridable && ba->deferred)
10645 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
10646 goto error;
10649 m = MATCH_YES;
10651 done:
10652 if (ba->access == ACCESS_UNKNOWN)
10653 ba->access = ppc ? gfc_current_block()->component_access
10654 : gfc_typebound_default_access;
10656 if (ppc && !seen_ptr)
10658 gfc_error ("POINTER attribute is required for procedure pointer component"
10659 " at %C");
10660 goto error;
10663 return m;
10665 error:
10666 return MATCH_ERROR;
10670 /* Match a PROCEDURE specific binding inside a derived type. */
10672 static match
10673 match_procedure_in_type (void)
10675 char name[GFC_MAX_SYMBOL_LEN + 1];
10676 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
10677 char* target = NULL, *ifc = NULL;
10678 gfc_typebound_proc tb;
10679 bool seen_colons;
10680 bool seen_attrs;
10681 match m;
10682 gfc_symtree* stree;
10683 gfc_namespace* ns;
10684 gfc_symbol* block;
10685 int num;
10687 /* Check current state. */
10688 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
10689 block = gfc_state_stack->previous->sym;
10690 gcc_assert (block);
10692 /* Try to match PROCEDURE(interface). */
10693 if (gfc_match (" (") == MATCH_YES)
10695 m = gfc_match_name (target_buf);
10696 if (m == MATCH_ERROR)
10697 return m;
10698 if (m != MATCH_YES)
10700 gfc_error ("Interface-name expected after %<(%> at %C");
10701 return MATCH_ERROR;
10704 if (gfc_match (" )") != MATCH_YES)
10706 gfc_error ("%<)%> expected at %C");
10707 return MATCH_ERROR;
10710 ifc = target_buf;
10713 /* Construct the data structure. */
10714 memset (&tb, 0, sizeof (tb));
10715 tb.where = gfc_current_locus;
10717 /* Match binding attributes. */
10718 m = match_binding_attributes (&tb, false, false);
10719 if (m == MATCH_ERROR)
10720 return m;
10721 seen_attrs = (m == MATCH_YES);
10723 /* Check that attribute DEFERRED is given if an interface is specified. */
10724 if (tb.deferred && !ifc)
10726 gfc_error ("Interface must be specified for DEFERRED binding at %C");
10727 return MATCH_ERROR;
10729 if (ifc && !tb.deferred)
10731 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
10732 return MATCH_ERROR;
10735 /* Match the colons. */
10736 m = gfc_match (" ::");
10737 if (m == MATCH_ERROR)
10738 return m;
10739 seen_colons = (m == MATCH_YES);
10740 if (seen_attrs && !seen_colons)
10742 gfc_error ("Expected %<::%> after binding-attributes at %C");
10743 return MATCH_ERROR;
10746 /* Match the binding names. */
10747 for(num=1;;num++)
10749 m = gfc_match_name (name);
10750 if (m == MATCH_ERROR)
10751 return m;
10752 if (m == MATCH_NO)
10754 gfc_error ("Expected binding name at %C");
10755 return MATCH_ERROR;
10758 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
10759 return MATCH_ERROR;
10761 /* Try to match the '=> target', if it's there. */
10762 target = ifc;
10763 m = gfc_match (" =>");
10764 if (m == MATCH_ERROR)
10765 return m;
10766 if (m == MATCH_YES)
10768 if (tb.deferred)
10770 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
10771 return MATCH_ERROR;
10774 if (!seen_colons)
10776 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
10777 " at %C");
10778 return MATCH_ERROR;
10781 m = gfc_match_name (target_buf);
10782 if (m == MATCH_ERROR)
10783 return m;
10784 if (m == MATCH_NO)
10786 gfc_error ("Expected binding target after %<=>%> at %C");
10787 return MATCH_ERROR;
10789 target = target_buf;
10792 /* If no target was found, it has the same name as the binding. */
10793 if (!target)
10794 target = name;
10796 /* Get the namespace to insert the symbols into. */
10797 ns = block->f2k_derived;
10798 gcc_assert (ns);
10800 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
10801 if (tb.deferred && !block->attr.abstract)
10803 gfc_error ("Type %qs containing DEFERRED binding at %C "
10804 "is not ABSTRACT", block->name);
10805 return MATCH_ERROR;
10808 /* See if we already have a binding with this name in the symtree which
10809 would be an error. If a GENERIC already targeted this binding, it may
10810 be already there but then typebound is still NULL. */
10811 stree = gfc_find_symtree (ns->tb_sym_root, name);
10812 if (stree && stree->n.tb)
10814 gfc_error ("There is already a procedure with binding name %qs for "
10815 "the derived type %qs at %C", name, block->name);
10816 return MATCH_ERROR;
10819 /* Insert it and set attributes. */
10821 if (!stree)
10823 stree = gfc_new_symtree (&ns->tb_sym_root, name);
10824 gcc_assert (stree);
10826 stree->n.tb = gfc_get_typebound_proc (&tb);
10828 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
10829 false))
10830 return MATCH_ERROR;
10831 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
10832 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
10833 target, &stree->n.tb->u.specific->n.sym->declared_at);
10835 if (gfc_match_eos () == MATCH_YES)
10836 return MATCH_YES;
10837 if (gfc_match_char (',') != MATCH_YES)
10838 goto syntax;
10841 syntax:
10842 gfc_error ("Syntax error in PROCEDURE statement at %C");
10843 return MATCH_ERROR;
10847 /* Match a GENERIC procedure binding inside a derived type. */
10849 match
10850 gfc_match_generic (void)
10852 char name[GFC_MAX_SYMBOL_LEN + 1];
10853 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
10854 gfc_symbol* block;
10855 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
10856 gfc_typebound_proc* tb;
10857 gfc_namespace* ns;
10858 interface_type op_type;
10859 gfc_intrinsic_op op;
10860 match m;
10862 /* Check current state. */
10863 if (gfc_current_state () == COMP_DERIVED)
10865 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
10866 return MATCH_ERROR;
10868 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
10869 return MATCH_NO;
10870 block = gfc_state_stack->previous->sym;
10871 ns = block->f2k_derived;
10872 gcc_assert (block && ns);
10874 memset (&tbattr, 0, sizeof (tbattr));
10875 tbattr.where = gfc_current_locus;
10877 /* See if we get an access-specifier. */
10878 m = match_binding_attributes (&tbattr, true, false);
10879 if (m == MATCH_ERROR)
10880 goto error;
10882 /* Now the colons, those are required. */
10883 if (gfc_match (" ::") != MATCH_YES)
10885 gfc_error ("Expected %<::%> at %C");
10886 goto error;
10889 /* Match the binding name; depending on type (operator / generic) format
10890 it for future error messages into bind_name. */
10892 m = gfc_match_generic_spec (&op_type, name, &op);
10893 if (m == MATCH_ERROR)
10894 return MATCH_ERROR;
10895 if (m == MATCH_NO)
10897 gfc_error ("Expected generic name or operator descriptor at %C");
10898 goto error;
10901 switch (op_type)
10903 case INTERFACE_GENERIC:
10904 case INTERFACE_DTIO:
10905 snprintf (bind_name, sizeof (bind_name), "%s", name);
10906 break;
10908 case INTERFACE_USER_OP:
10909 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
10910 break;
10912 case INTERFACE_INTRINSIC_OP:
10913 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
10914 gfc_op2string (op));
10915 break;
10917 case INTERFACE_NAMELESS:
10918 gfc_error ("Malformed GENERIC statement at %C");
10919 goto error;
10920 break;
10922 default:
10923 gcc_unreachable ();
10926 /* Match the required =>. */
10927 if (gfc_match (" =>") != MATCH_YES)
10929 gfc_error ("Expected %<=>%> at %C");
10930 goto error;
10933 /* Try to find existing GENERIC binding with this name / for this operator;
10934 if there is something, check that it is another GENERIC and then extend
10935 it rather than building a new node. Otherwise, create it and put it
10936 at the right position. */
10938 switch (op_type)
10940 case INTERFACE_DTIO:
10941 case INTERFACE_USER_OP:
10942 case INTERFACE_GENERIC:
10944 const bool is_op = (op_type == INTERFACE_USER_OP);
10945 gfc_symtree* st;
10947 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
10948 tb = st ? st->n.tb : NULL;
10949 break;
10952 case INTERFACE_INTRINSIC_OP:
10953 tb = ns->tb_op[op];
10954 break;
10956 default:
10957 gcc_unreachable ();
10960 if (tb)
10962 if (!tb->is_generic)
10964 gcc_assert (op_type == INTERFACE_GENERIC);
10965 gfc_error ("There's already a non-generic procedure with binding name"
10966 " %qs for the derived type %qs at %C",
10967 bind_name, block->name);
10968 goto error;
10971 if (tb->access != tbattr.access)
10973 gfc_error ("Binding at %C must have the same access as already"
10974 " defined binding %qs", bind_name);
10975 goto error;
10978 else
10980 tb = gfc_get_typebound_proc (NULL);
10981 tb->where = gfc_current_locus;
10982 tb->access = tbattr.access;
10983 tb->is_generic = 1;
10984 tb->u.generic = NULL;
10986 switch (op_type)
10988 case INTERFACE_DTIO:
10989 case INTERFACE_GENERIC:
10990 case INTERFACE_USER_OP:
10992 const bool is_op = (op_type == INTERFACE_USER_OP);
10993 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
10994 &ns->tb_sym_root, name);
10995 gcc_assert (st);
10996 st->n.tb = tb;
10998 break;
11001 case INTERFACE_INTRINSIC_OP:
11002 ns->tb_op[op] = tb;
11003 break;
11005 default:
11006 gcc_unreachable ();
11010 /* Now, match all following names as specific targets. */
11013 gfc_symtree* target_st;
11014 gfc_tbp_generic* target;
11016 m = gfc_match_name (name);
11017 if (m == MATCH_ERROR)
11018 goto error;
11019 if (m == MATCH_NO)
11021 gfc_error ("Expected specific binding name at %C");
11022 goto error;
11025 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
11027 /* See if this is a duplicate specification. */
11028 for (target = tb->u.generic; target; target = target->next)
11029 if (target_st == target->specific_st)
11031 gfc_error ("%qs already defined as specific binding for the"
11032 " generic %qs at %C", name, bind_name);
11033 goto error;
11036 target = gfc_get_tbp_generic ();
11037 target->specific_st = target_st;
11038 target->specific = NULL;
11039 target->next = tb->u.generic;
11040 target->is_operator = ((op_type == INTERFACE_USER_OP)
11041 || (op_type == INTERFACE_INTRINSIC_OP));
11042 tb->u.generic = target;
11044 while (gfc_match (" ,") == MATCH_YES);
11046 /* Here should be the end. */
11047 if (gfc_match_eos () != MATCH_YES)
11049 gfc_error ("Junk after GENERIC binding at %C");
11050 goto error;
11053 return MATCH_YES;
11055 error:
11056 return MATCH_ERROR;
11060 /* Match a FINAL declaration inside a derived type. */
11062 match
11063 gfc_match_final_decl (void)
11065 char name[GFC_MAX_SYMBOL_LEN + 1];
11066 gfc_symbol* sym;
11067 match m;
11068 gfc_namespace* module_ns;
11069 bool first, last;
11070 gfc_symbol* block;
11072 if (gfc_current_form == FORM_FREE)
11074 char c = gfc_peek_ascii_char ();
11075 if (!gfc_is_whitespace (c) && c != ':')
11076 return MATCH_NO;
11079 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
11081 if (gfc_current_form == FORM_FIXED)
11082 return MATCH_NO;
11084 gfc_error ("FINAL declaration at %C must be inside a derived type "
11085 "CONTAINS section");
11086 return MATCH_ERROR;
11089 block = gfc_state_stack->previous->sym;
11090 gcc_assert (block);
11092 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
11093 || gfc_state_stack->previous->previous->state != COMP_MODULE)
11095 gfc_error ("Derived type declaration with FINAL at %C must be in the"
11096 " specification part of a MODULE");
11097 return MATCH_ERROR;
11100 module_ns = gfc_current_ns;
11101 gcc_assert (module_ns);
11102 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
11104 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
11105 if (gfc_match (" ::") == MATCH_ERROR)
11106 return MATCH_ERROR;
11108 /* Match the sequence of procedure names. */
11109 first = true;
11110 last = false;
11113 gfc_finalizer* f;
11115 if (first && gfc_match_eos () == MATCH_YES)
11117 gfc_error ("Empty FINAL at %C");
11118 return MATCH_ERROR;
11121 m = gfc_match_name (name);
11122 if (m == MATCH_NO)
11124 gfc_error ("Expected module procedure name at %C");
11125 return MATCH_ERROR;
11127 else if (m != MATCH_YES)
11128 return MATCH_ERROR;
11130 if (gfc_match_eos () == MATCH_YES)
11131 last = true;
11132 if (!last && gfc_match_char (',') != MATCH_YES)
11134 gfc_error ("Expected %<,%> at %C");
11135 return MATCH_ERROR;
11138 if (gfc_get_symbol (name, module_ns, &sym))
11140 gfc_error ("Unknown procedure name %qs at %C", name);
11141 return MATCH_ERROR;
11144 /* Mark the symbol as module procedure. */
11145 if (sym->attr.proc != PROC_MODULE
11146 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
11147 return MATCH_ERROR;
11149 /* Check if we already have this symbol in the list, this is an error. */
11150 for (f = block->f2k_derived->finalizers; f; f = f->next)
11151 if (f->proc_sym == sym)
11153 gfc_error ("%qs at %C is already defined as FINAL procedure",
11154 name);
11155 return MATCH_ERROR;
11158 /* Add this symbol to the list of finalizers. */
11159 gcc_assert (block->f2k_derived);
11160 sym->refs++;
11161 f = XCNEW (gfc_finalizer);
11162 f->proc_sym = sym;
11163 f->proc_tree = NULL;
11164 f->where = gfc_current_locus;
11165 f->next = block->f2k_derived->finalizers;
11166 block->f2k_derived->finalizers = f;
11168 first = false;
11170 while (!last);
11172 return MATCH_YES;
11176 const ext_attr_t ext_attr_list[] = {
11177 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
11178 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
11179 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
11180 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
11181 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
11182 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
11183 { NULL, EXT_ATTR_LAST, NULL }
11186 /* Match a !GCC$ ATTRIBUTES statement of the form:
11187 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11188 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11190 TODO: We should support all GCC attributes using the same syntax for
11191 the attribute list, i.e. the list in C
11192 __attributes(( attribute-list ))
11193 matches then
11194 !GCC$ ATTRIBUTES attribute-list ::
11195 Cf. c-parser.c's c_parser_attributes; the data can then directly be
11196 saved into a TREE.
11198 As there is absolutely no risk of confusion, we should never return
11199 MATCH_NO. */
11200 match
11201 gfc_match_gcc_attributes (void)
11203 symbol_attribute attr;
11204 char name[GFC_MAX_SYMBOL_LEN + 1];
11205 unsigned id;
11206 gfc_symbol *sym;
11207 match m;
11209 gfc_clear_attr (&attr);
11210 for(;;)
11212 char ch;
11214 if (gfc_match_name (name) != MATCH_YES)
11215 return MATCH_ERROR;
11217 for (id = 0; id < EXT_ATTR_LAST; id++)
11218 if (strcmp (name, ext_attr_list[id].name) == 0)
11219 break;
11221 if (id == EXT_ATTR_LAST)
11223 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11224 return MATCH_ERROR;
11227 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
11228 return MATCH_ERROR;
11230 gfc_gobble_whitespace ();
11231 ch = gfc_next_ascii_char ();
11232 if (ch == ':')
11234 /* This is the successful exit condition for the loop. */
11235 if (gfc_next_ascii_char () == ':')
11236 break;
11239 if (ch == ',')
11240 continue;
11242 goto syntax;
11245 if (gfc_match_eos () == MATCH_YES)
11246 goto syntax;
11248 for(;;)
11250 m = gfc_match_name (name);
11251 if (m != MATCH_YES)
11252 return m;
11254 if (find_special (name, &sym, true))
11255 return MATCH_ERROR;
11257 sym->attr.ext_attr |= attr.ext_attr;
11259 if (gfc_match_eos () == MATCH_YES)
11260 break;
11262 if (gfc_match_char (',') != MATCH_YES)
11263 goto syntax;
11266 return MATCH_YES;
11268 syntax:
11269 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11270 return MATCH_ERROR;
11274 /* Match a !GCC$ UNROLL statement of the form:
11275 !GCC$ UNROLL n
11277 The parameter n is the number of times we are supposed to unroll.
11279 When we come here, we have already matched the !GCC$ UNROLL string. */
11280 match
11281 gfc_match_gcc_unroll (void)
11283 int value;
11285 if (gfc_match_small_int (&value) == MATCH_YES)
11287 if (value < 0 || value > USHRT_MAX)
11289 gfc_error ("%<GCC unroll%> directive requires a"
11290 " non-negative integral constant"
11291 " less than or equal to %u at %C",
11292 USHRT_MAX
11294 return MATCH_ERROR;
11296 if (gfc_match_eos () == MATCH_YES)
11298 directive_unroll = value == 0 ? 1 : value;
11299 return MATCH_YES;
11303 gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11304 return MATCH_ERROR;
11307 /* Match a !GCC$ builtin (b) attributes simd flags form:
11309 The parameter b is name of a middle-end built-in.
11310 Flags are one of:
11311 - (empty)
11312 - inbranch
11313 - notinbranch
11315 When we come here, we have already matched the !GCC$ builtin string. */
11316 match
11317 gfc_match_gcc_builtin (void)
11319 char builtin[GFC_MAX_SYMBOL_LEN + 1];
11321 if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
11322 return MATCH_ERROR;
11324 gfc_simd_clause clause = SIMD_NONE;
11325 if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
11326 clause = SIMD_NOTINBRANCH;
11327 else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
11328 clause = SIMD_INBRANCH;
11330 if (gfc_vectorized_builtins == NULL)
11331 gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
11333 char *r = XNEWVEC (char, strlen (builtin) + 32);
11334 sprintf (r, "__builtin_%s", builtin);
11336 bool existed;
11337 int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
11338 value |= clause;
11339 if (existed)
11340 free (r);
11342 return MATCH_YES;