PR rtl-optimization/82913
[official-gcc.git] / gcc / fortran / decl.c
blob1a2d8f004cac519af366172624b4828cfa9b12e7
1 /* Declaration statement matcher
2 Copyright (C) 2002-2017 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 /* If a kind expression of a component of a parameterized derived type is
99 parameterized, temporarily store the expression here. */
100 static gfc_expr *saved_kind_expr = NULL;
102 /* Used to store the parameter list arising in a PDT declaration and
103 in the typespec of a PDT variable or component. */
104 static gfc_actual_arglist *decl_type_param_list;
105 static gfc_actual_arglist *type_param_spec_list;
108 /********************* DATA statement subroutines *********************/
110 static bool in_match_data = false;
112 bool
113 gfc_in_match_data (void)
115 return in_match_data;
118 static void
119 set_in_match_data (bool set_value)
121 in_match_data = set_value;
124 /* Free a gfc_data_variable structure and everything beneath it. */
126 static void
127 free_variable (gfc_data_variable *p)
129 gfc_data_variable *q;
131 for (; p; p = q)
133 q = p->next;
134 gfc_free_expr (p->expr);
135 gfc_free_iterator (&p->iter, 0);
136 free_variable (p->list);
137 free (p);
142 /* Free a gfc_data_value structure and everything beneath it. */
144 static void
145 free_value (gfc_data_value *p)
147 gfc_data_value *q;
149 for (; p; p = q)
151 q = p->next;
152 mpz_clear (p->repeat);
153 gfc_free_expr (p->expr);
154 free (p);
159 /* Free a list of gfc_data structures. */
161 void
162 gfc_free_data (gfc_data *p)
164 gfc_data *q;
166 for (; p; p = q)
168 q = p->next;
169 free_variable (p->var);
170 free_value (p->value);
171 free (p);
176 /* Free all data in a namespace. */
178 static void
179 gfc_free_data_all (gfc_namespace *ns)
181 gfc_data *d;
183 for (;ns->data;)
185 d = ns->data->next;
186 free (ns->data);
187 ns->data = d;
191 /* Reject data parsed since the last restore point was marked. */
193 void
194 gfc_reject_data (gfc_namespace *ns)
196 gfc_data *d;
198 while (ns->data && ns->data != ns->old_data)
200 d = ns->data->next;
201 free (ns->data);
202 ns->data = d;
206 static match var_element (gfc_data_variable *);
208 /* Match a list of variables terminated by an iterator and a right
209 parenthesis. */
211 static match
212 var_list (gfc_data_variable *parent)
214 gfc_data_variable *tail, var;
215 match m;
217 m = var_element (&var);
218 if (m == MATCH_ERROR)
219 return MATCH_ERROR;
220 if (m == MATCH_NO)
221 goto syntax;
223 tail = gfc_get_data_variable ();
224 *tail = var;
226 parent->list = tail;
228 for (;;)
230 if (gfc_match_char (',') != MATCH_YES)
231 goto syntax;
233 m = gfc_match_iterator (&parent->iter, 1);
234 if (m == MATCH_YES)
235 break;
236 if (m == MATCH_ERROR)
237 return MATCH_ERROR;
239 m = var_element (&var);
240 if (m == MATCH_ERROR)
241 return MATCH_ERROR;
242 if (m == MATCH_NO)
243 goto syntax;
245 tail->next = gfc_get_data_variable ();
246 tail = tail->next;
248 *tail = var;
251 if (gfc_match_char (')') != MATCH_YES)
252 goto syntax;
253 return MATCH_YES;
255 syntax:
256 gfc_syntax_error (ST_DATA);
257 return MATCH_ERROR;
261 /* Match a single element in a data variable list, which can be a
262 variable-iterator list. */
264 static match
265 var_element (gfc_data_variable *new_var)
267 match m;
268 gfc_symbol *sym;
270 memset (new_var, 0, sizeof (gfc_data_variable));
272 if (gfc_match_char ('(') == MATCH_YES)
273 return var_list (new_var);
275 m = gfc_match_variable (&new_var->expr, 0);
276 if (m != MATCH_YES)
277 return m;
279 sym = new_var->expr->symtree->n.sym;
281 /* Symbol should already have an associated type. */
282 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
283 return MATCH_ERROR;
285 if (!sym->attr.function && gfc_current_ns->parent
286 && gfc_current_ns->parent == sym->ns)
288 gfc_error ("Host associated variable %qs may not be in the DATA "
289 "statement at %C", sym->name);
290 return MATCH_ERROR;
293 if (gfc_current_state () != COMP_BLOCK_DATA
294 && sym->attr.in_common
295 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
296 "common block variable %qs in DATA statement at %C",
297 sym->name))
298 return MATCH_ERROR;
300 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
301 return MATCH_ERROR;
303 return MATCH_YES;
307 /* Match the top-level list of data variables. */
309 static match
310 top_var_list (gfc_data *d)
312 gfc_data_variable var, *tail, *new_var;
313 match m;
315 tail = NULL;
317 for (;;)
319 m = var_element (&var);
320 if (m == MATCH_NO)
321 goto syntax;
322 if (m == MATCH_ERROR)
323 return MATCH_ERROR;
325 new_var = gfc_get_data_variable ();
326 *new_var = var;
328 if (tail == NULL)
329 d->var = new_var;
330 else
331 tail->next = new_var;
333 tail = new_var;
335 if (gfc_match_char ('/') == MATCH_YES)
336 break;
337 if (gfc_match_char (',') != MATCH_YES)
338 goto syntax;
341 return MATCH_YES;
343 syntax:
344 gfc_syntax_error (ST_DATA);
345 gfc_free_data_all (gfc_current_ns);
346 return MATCH_ERROR;
350 static match
351 match_data_constant (gfc_expr **result)
353 char name[GFC_MAX_SYMBOL_LEN + 1];
354 gfc_symbol *sym, *dt_sym = NULL;
355 gfc_expr *expr;
356 match m;
357 locus old_loc;
359 m = gfc_match_literal_constant (&expr, 1);
360 if (m == MATCH_YES)
362 *result = expr;
363 return MATCH_YES;
366 if (m == MATCH_ERROR)
367 return MATCH_ERROR;
369 m = gfc_match_null (result);
370 if (m != MATCH_NO)
371 return m;
373 old_loc = gfc_current_locus;
375 /* Should this be a structure component, try to match it
376 before matching a name. */
377 m = gfc_match_rvalue (result);
378 if (m == MATCH_ERROR)
379 return m;
381 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
383 if (!gfc_simplify_expr (*result, 0))
384 m = MATCH_ERROR;
385 return m;
387 else if (m == MATCH_YES)
388 gfc_free_expr (*result);
390 gfc_current_locus = old_loc;
392 m = gfc_match_name (name);
393 if (m != MATCH_YES)
394 return m;
396 if (gfc_find_symbol (name, NULL, 1, &sym))
397 return MATCH_ERROR;
399 if (sym && sym->attr.generic)
400 dt_sym = gfc_find_dt_in_generic (sym);
402 if (sym == NULL
403 || (sym->attr.flavor != FL_PARAMETER
404 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
406 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
407 name);
408 *result = NULL;
409 return MATCH_ERROR;
411 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
412 return gfc_match_structure_constructor (dt_sym, result);
414 /* Check to see if the value is an initialization array expression. */
415 if (sym->value->expr_type == EXPR_ARRAY)
417 gfc_current_locus = old_loc;
419 m = gfc_match_init_expr (result);
420 if (m == MATCH_ERROR)
421 return m;
423 if (m == MATCH_YES)
425 if (!gfc_simplify_expr (*result, 0))
426 m = MATCH_ERROR;
428 if ((*result)->expr_type == EXPR_CONSTANT)
429 return m;
430 else
432 gfc_error ("Invalid initializer %s in Data statement at %C", name);
433 return MATCH_ERROR;
438 *result = gfc_copy_expr (sym->value);
439 return MATCH_YES;
443 /* Match a list of values in a DATA statement. The leading '/' has
444 already been seen at this point. */
446 static match
447 top_val_list (gfc_data *data)
449 gfc_data_value *new_val, *tail;
450 gfc_expr *expr;
451 match m;
453 tail = NULL;
455 for (;;)
457 m = match_data_constant (&expr);
458 if (m == MATCH_NO)
459 goto syntax;
460 if (m == MATCH_ERROR)
461 return MATCH_ERROR;
463 new_val = gfc_get_data_value ();
464 mpz_init (new_val->repeat);
466 if (tail == NULL)
467 data->value = new_val;
468 else
469 tail->next = new_val;
471 tail = new_val;
473 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
475 tail->expr = expr;
476 mpz_set_ui (tail->repeat, 1);
478 else
480 mpz_set (tail->repeat, expr->value.integer);
481 gfc_free_expr (expr);
483 m = match_data_constant (&tail->expr);
484 if (m == MATCH_NO)
485 goto syntax;
486 if (m == MATCH_ERROR)
487 return MATCH_ERROR;
490 if (gfc_match_char ('/') == MATCH_YES)
491 break;
492 if (gfc_match_char (',') == MATCH_NO)
493 goto syntax;
496 return MATCH_YES;
498 syntax:
499 gfc_syntax_error (ST_DATA);
500 gfc_free_data_all (gfc_current_ns);
501 return MATCH_ERROR;
505 /* Matches an old style initialization. */
507 static match
508 match_old_style_init (const char *name)
510 match m;
511 gfc_symtree *st;
512 gfc_symbol *sym;
513 gfc_data *newdata;
515 /* Set up data structure to hold initializers. */
516 gfc_find_sym_tree (name, NULL, 0, &st);
517 sym = st->n.sym;
519 newdata = gfc_get_data ();
520 newdata->var = gfc_get_data_variable ();
521 newdata->var->expr = gfc_get_variable_expr (st);
522 newdata->where = gfc_current_locus;
524 /* Match initial value list. This also eats the terminal '/'. */
525 m = top_val_list (newdata);
526 if (m != MATCH_YES)
528 free (newdata);
529 return m;
532 if (gfc_pure (NULL))
534 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
535 free (newdata);
536 return MATCH_ERROR;
538 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
540 /* Mark the variable as having appeared in a data statement. */
541 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
543 free (newdata);
544 return MATCH_ERROR;
547 /* Chain in namespace list of DATA initializers. */
548 newdata->next = gfc_current_ns->data;
549 gfc_current_ns->data = newdata;
551 return m;
555 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
556 we are matching a DATA statement and are therefore issuing an error
557 if we encounter something unexpected, if not, we're trying to match
558 an old-style initialization expression of the form INTEGER I /2/. */
560 match
561 gfc_match_data (void)
563 gfc_data *new_data;
564 match m;
566 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
567 if ((gfc_current_state () == COMP_FUNCTION
568 || gfc_current_state () == COMP_SUBROUTINE)
569 && gfc_state_stack->previous->state == COMP_INTERFACE)
571 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
572 return MATCH_ERROR;
575 set_in_match_data (true);
577 for (;;)
579 new_data = gfc_get_data ();
580 new_data->where = gfc_current_locus;
582 m = top_var_list (new_data);
583 if (m != MATCH_YES)
584 goto cleanup;
586 m = top_val_list (new_data);
587 if (m != MATCH_YES)
588 goto cleanup;
590 new_data->next = gfc_current_ns->data;
591 gfc_current_ns->data = new_data;
593 if (gfc_match_eos () == MATCH_YES)
594 break;
596 gfc_match_char (','); /* Optional comma */
599 set_in_match_data (false);
601 if (gfc_pure (NULL))
603 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
604 return MATCH_ERROR;
606 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
608 return MATCH_YES;
610 cleanup:
611 set_in_match_data (false);
612 gfc_free_data (new_data);
613 return MATCH_ERROR;
617 /************************ Declaration statements *********************/
620 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
621 list). The difference here is the expression is a list of constants
622 and is surrounded by '/'.
623 The typespec ts must match the typespec of the variable which the
624 clist is initializing.
625 The arrayspec tells whether this should match a list of constants
626 corresponding to array elements or a scalar (as == NULL). */
628 static match
629 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
631 gfc_constructor_base array_head = NULL;
632 gfc_expr *expr = NULL;
633 match m;
634 locus where;
635 mpz_t repeat, size;
636 bool scalar;
637 int cmp;
639 gcc_assert (ts);
641 mpz_init_set_ui (repeat, 0);
642 mpz_init (size);
643 scalar = !as || !as->rank;
645 /* We have already matched '/' - now look for a constant list, as with
646 top_val_list from decl.c, but append the result to an array. */
647 if (gfc_match ("/") == MATCH_YES)
649 gfc_error ("Empty old style initializer list at %C");
650 goto cleanup;
653 where = gfc_current_locus;
654 for (;;)
656 m = match_data_constant (&expr);
657 if (m != MATCH_YES)
658 expr = NULL; /* match_data_constant may set expr to garbage */
659 if (m == MATCH_NO)
660 goto syntax;
661 if (m == MATCH_ERROR)
662 goto cleanup;
664 /* Found r in repeat spec r*c; look for the constant to repeat. */
665 if ( gfc_match_char ('*') == MATCH_YES)
667 if (scalar)
669 gfc_error ("Repeat spec invalid in scalar initializer at %C");
670 goto cleanup;
672 if (expr->ts.type != BT_INTEGER)
674 gfc_error ("Repeat spec must be an integer at %C");
675 goto cleanup;
677 mpz_set (repeat, expr->value.integer);
678 gfc_free_expr (expr);
679 expr = NULL;
681 m = match_data_constant (&expr);
682 if (m == MATCH_NO)
683 gfc_error ("Expected data constant after repeat spec at %C");
684 if (m != MATCH_YES)
685 goto cleanup;
687 /* No repeat spec, we matched the data constant itself. */
688 else
689 mpz_set_ui (repeat, 1);
691 if (!scalar)
693 /* Add the constant initializer as many times as repeated. */
694 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
696 /* Make sure types of elements match */
697 if(ts && !gfc_compare_types (&expr->ts, ts)
698 && !gfc_convert_type (expr, ts, 1))
699 goto cleanup;
701 gfc_constructor_append_expr (&array_head,
702 gfc_copy_expr (expr), &gfc_current_locus);
705 gfc_free_expr (expr);
706 expr = NULL;
709 /* For scalar initializers quit after one element. */
710 else
712 if(gfc_match_char ('/') != MATCH_YES)
714 gfc_error ("End of scalar initializer expected at %C");
715 goto cleanup;
717 break;
720 if (gfc_match_char ('/') == MATCH_YES)
721 break;
722 if (gfc_match_char (',') == MATCH_NO)
723 goto syntax;
726 /* Set up expr as an array constructor. */
727 if (!scalar)
729 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
730 expr->ts = *ts;
731 expr->value.constructor = array_head;
733 expr->rank = as->rank;
734 expr->shape = gfc_get_shape (expr->rank);
736 /* Validate sizes. */
737 gcc_assert (gfc_array_size (expr, &size));
738 gcc_assert (spec_size (as, &repeat));
739 cmp = mpz_cmp (size, repeat);
740 if (cmp < 0)
741 gfc_error ("Not enough elements in array initializer at %C");
742 else if (cmp > 0)
743 gfc_error ("Too many elements in array initializer at %C");
744 if (cmp)
745 goto cleanup;
748 /* Make sure scalar types match. */
749 else if (!gfc_compare_types (&expr->ts, ts)
750 && !gfc_convert_type (expr, ts, 1))
751 goto cleanup;
753 if (expr->ts.u.cl)
754 expr->ts.u.cl->length_from_typespec = 1;
756 *result = expr;
757 mpz_clear (size);
758 mpz_clear (repeat);
759 return MATCH_YES;
761 syntax:
762 gfc_error ("Syntax error in old style initializer list at %C");
764 cleanup:
765 if (expr)
766 expr->value.constructor = NULL;
767 gfc_free_expr (expr);
768 gfc_constructor_free (array_head);
769 mpz_clear (size);
770 mpz_clear (repeat);
771 return MATCH_ERROR;
775 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
777 static bool
778 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
780 int i;
782 if ((from->type == AS_ASSUMED_RANK && to->corank)
783 || (to->type == AS_ASSUMED_RANK && from->corank))
785 gfc_error ("The assumed-rank array at %C shall not have a codimension");
786 return false;
789 if (to->rank == 0 && from->rank > 0)
791 to->rank = from->rank;
792 to->type = from->type;
793 to->cray_pointee = from->cray_pointee;
794 to->cp_was_assumed = from->cp_was_assumed;
796 for (i = 0; i < to->corank; i++)
798 to->lower[from->rank + i] = to->lower[i];
799 to->upper[from->rank + i] = to->upper[i];
801 for (i = 0; i < from->rank; i++)
803 if (copy)
805 to->lower[i] = gfc_copy_expr (from->lower[i]);
806 to->upper[i] = gfc_copy_expr (from->upper[i]);
808 else
810 to->lower[i] = from->lower[i];
811 to->upper[i] = from->upper[i];
815 else if (to->corank == 0 && from->corank > 0)
817 to->corank = from->corank;
818 to->cotype = from->cotype;
820 for (i = 0; i < from->corank; i++)
822 if (copy)
824 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
825 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
827 else
829 to->lower[to->rank + i] = from->lower[i];
830 to->upper[to->rank + i] = from->upper[i];
835 return true;
839 /* Match an intent specification. Since this can only happen after an
840 INTENT word, a legal intent-spec must follow. */
842 static sym_intent
843 match_intent_spec (void)
846 if (gfc_match (" ( in out )") == MATCH_YES)
847 return INTENT_INOUT;
848 if (gfc_match (" ( in )") == MATCH_YES)
849 return INTENT_IN;
850 if (gfc_match (" ( out )") == MATCH_YES)
851 return INTENT_OUT;
853 gfc_error ("Bad INTENT specification at %C");
854 return INTENT_UNKNOWN;
858 /* Matches a character length specification, which is either a
859 specification expression, '*', or ':'. */
861 static match
862 char_len_param_value (gfc_expr **expr, bool *deferred)
864 match m;
866 *expr = NULL;
867 *deferred = false;
869 if (gfc_match_char ('*') == MATCH_YES)
870 return MATCH_YES;
872 if (gfc_match_char (':') == MATCH_YES)
874 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
875 return MATCH_ERROR;
877 *deferred = true;
879 return MATCH_YES;
882 m = gfc_match_expr (expr);
884 if (m == MATCH_NO || m == MATCH_ERROR)
885 return m;
887 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
888 return MATCH_ERROR;
890 if ((*expr)->expr_type == EXPR_FUNCTION)
892 if ((*expr)->ts.type == BT_INTEGER
893 || ((*expr)->ts.type == BT_UNKNOWN
894 && strcmp((*expr)->symtree->name, "null") != 0))
895 return MATCH_YES;
897 goto syntax;
899 else if ((*expr)->expr_type == EXPR_CONSTANT)
901 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
902 processor dependent and its value is greater than or equal to zero.
903 F2008, 4.4.3.2: If the character length parameter value evaluates
904 to a negative value, the length of character entities declared
905 is zero. */
907 if ((*expr)->ts.type == BT_INTEGER)
909 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
910 mpz_set_si ((*expr)->value.integer, 0);
912 else
913 goto syntax;
915 else if ((*expr)->expr_type == EXPR_ARRAY)
916 goto syntax;
917 else if ((*expr)->expr_type == EXPR_VARIABLE)
919 bool t;
920 gfc_expr *e;
922 e = gfc_copy_expr (*expr);
924 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
925 which causes an ICE if gfc_reduce_init_expr() is called. */
926 if (e->ref && e->ref->type == REF_ARRAY
927 && e->ref->u.ar.type == AR_UNKNOWN
928 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
929 goto syntax;
931 t = gfc_reduce_init_expr (e);
933 if (!t && e->ts.type == BT_UNKNOWN
934 && e->symtree->n.sym->attr.untyped == 1
935 && (flag_implicit_none
936 || e->symtree->n.sym->ns->seen_implicit_none == 1
937 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
939 gfc_free_expr (e);
940 goto syntax;
943 if ((e->ref && e->ref->type == REF_ARRAY
944 && e->ref->u.ar.type != AR_ELEMENT)
945 || (!e->ref && e->expr_type == EXPR_ARRAY))
947 gfc_free_expr (e);
948 goto syntax;
951 gfc_free_expr (e);
954 return m;
956 syntax:
957 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
958 return MATCH_ERROR;
962 /* A character length is a '*' followed by a literal integer or a
963 char_len_param_value in parenthesis. */
965 static match
966 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
968 int length;
969 match m;
971 *deferred = false;
972 m = gfc_match_char ('*');
973 if (m != MATCH_YES)
974 return m;
976 m = gfc_match_small_literal_int (&length, NULL);
977 if (m == MATCH_ERROR)
978 return m;
980 if (m == MATCH_YES)
982 if (obsolescent_check
983 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
984 return MATCH_ERROR;
985 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
986 return m;
989 if (gfc_match_char ('(') == MATCH_NO)
990 goto syntax;
992 m = char_len_param_value (expr, deferred);
993 if (m != MATCH_YES && gfc_matching_function)
995 gfc_undo_symbols ();
996 m = MATCH_YES;
999 if (m == MATCH_ERROR)
1000 return m;
1001 if (m == MATCH_NO)
1002 goto syntax;
1004 if (gfc_match_char (')') == MATCH_NO)
1006 gfc_free_expr (*expr);
1007 *expr = NULL;
1008 goto syntax;
1011 return MATCH_YES;
1013 syntax:
1014 gfc_error ("Syntax error in character length specification at %C");
1015 return MATCH_ERROR;
1019 /* Special subroutine for finding a symbol. Check if the name is found
1020 in the current name space. If not, and we're compiling a function or
1021 subroutine and the parent compilation unit is an interface, then check
1022 to see if the name we've been given is the name of the interface
1023 (located in another namespace). */
1025 static int
1026 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1028 gfc_state_data *s;
1029 gfc_symtree *st;
1030 int i;
1032 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1033 if (i == 0)
1035 *result = st ? st->n.sym : NULL;
1036 goto end;
1039 if (gfc_current_state () != COMP_SUBROUTINE
1040 && gfc_current_state () != COMP_FUNCTION)
1041 goto end;
1043 s = gfc_state_stack->previous;
1044 if (s == NULL)
1045 goto end;
1047 if (s->state != COMP_INTERFACE)
1048 goto end;
1049 if (s->sym == NULL)
1050 goto end; /* Nameless interface. */
1052 if (strcmp (name, s->sym->name) == 0)
1054 *result = s->sym;
1055 return 0;
1058 end:
1059 return i;
1063 /* Special subroutine for getting a symbol node associated with a
1064 procedure name, used in SUBROUTINE and FUNCTION statements. The
1065 symbol is created in the parent using with symtree node in the
1066 child unit pointing to the symbol. If the current namespace has no
1067 parent, then the symbol is just created in the current unit. */
1069 static int
1070 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1072 gfc_symtree *st;
1073 gfc_symbol *sym;
1074 int rc = 0;
1076 /* Module functions have to be left in their own namespace because
1077 they have potentially (almost certainly!) already been referenced.
1078 In this sense, they are rather like external functions. This is
1079 fixed up in resolve.c(resolve_entries), where the symbol name-
1080 space is set to point to the master function, so that the fake
1081 result mechanism can work. */
1082 if (module_fcn_entry)
1084 /* Present if entry is declared to be a module procedure. */
1085 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1087 if (*result == NULL)
1088 rc = gfc_get_symbol (name, NULL, result);
1089 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1090 && (*result)->ts.type == BT_UNKNOWN
1091 && sym->attr.flavor == FL_UNKNOWN)
1092 /* Pick up the typespec for the entry, if declared in the function
1093 body. Note that this symbol is FL_UNKNOWN because it will
1094 only have appeared in a type declaration. The local symtree
1095 is set to point to the module symbol and a unique symtree
1096 to the local version. This latter ensures a correct clearing
1097 of the symbols. */
1099 /* If the ENTRY proceeds its specification, we need to ensure
1100 that this does not raise a "has no IMPLICIT type" error. */
1101 if (sym->ts.type == BT_UNKNOWN)
1102 sym->attr.untyped = 1;
1104 (*result)->ts = sym->ts;
1106 /* Put the symbol in the procedure namespace so that, should
1107 the ENTRY precede its specification, the specification
1108 can be applied. */
1109 (*result)->ns = gfc_current_ns;
1111 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1112 st->n.sym = *result;
1113 st = gfc_get_unique_symtree (gfc_current_ns);
1114 sym->refs++;
1115 st->n.sym = sym;
1118 else
1119 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1121 if (rc)
1122 return rc;
1124 sym = *result;
1125 if (sym->attr.proc == PROC_ST_FUNCTION)
1126 return rc;
1128 if (sym->attr.module_procedure
1129 && sym->attr.if_source == IFSRC_IFBODY)
1131 /* Create a partially populated interface symbol to carry the
1132 characteristics of the procedure and the result. */
1133 sym->tlink = gfc_new_symbol (name, sym->ns);
1134 gfc_add_type (sym->tlink, &(sym->ts),
1135 &gfc_current_locus);
1136 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1137 if (sym->attr.dimension)
1138 sym->tlink->as = gfc_copy_array_spec (sym->as);
1140 /* Ideally, at this point, a copy would be made of the formal
1141 arguments and their namespace. However, this does not appear
1142 to be necessary, albeit at the expense of not being able to
1143 use gfc_compare_interfaces directly. */
1145 if (sym->result && sym->result != sym)
1147 sym->tlink->result = sym->result;
1148 sym->result = NULL;
1150 else if (sym->result)
1152 sym->tlink->result = sym->tlink;
1155 else if (sym && !sym->gfc_new
1156 && gfc_current_state () != COMP_INTERFACE)
1158 /* Trap another encompassed procedure with the same name. All
1159 these conditions are necessary to avoid picking up an entry
1160 whose name clashes with that of the encompassing procedure;
1161 this is handled using gsymbols to register unique, globally
1162 accessible names. */
1163 if (sym->attr.flavor != 0
1164 && sym->attr.proc != 0
1165 && (sym->attr.subroutine || sym->attr.function)
1166 && sym->attr.if_source != IFSRC_UNKNOWN)
1167 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1168 name, &sym->declared_at);
1170 /* Trap a procedure with a name the same as interface in the
1171 encompassing scope. */
1172 if (sym->attr.generic != 0
1173 && (sym->attr.subroutine || sym->attr.function)
1174 && !sym->attr.mod_proc)
1175 gfc_error_now ("Name %qs at %C is already defined"
1176 " as a generic interface at %L",
1177 name, &sym->declared_at);
1179 /* Trap declarations of attributes in encompassing scope. The
1180 signature for this is that ts.kind is set. Legitimate
1181 references only set ts.type. */
1182 if (sym->ts.kind != 0
1183 && !sym->attr.implicit_type
1184 && sym->attr.proc == 0
1185 && gfc_current_ns->parent != NULL
1186 && sym->attr.access == 0
1187 && !module_fcn_entry)
1188 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1189 "and must not have attributes declared at %L",
1190 name, &sym->declared_at);
1193 if (gfc_current_ns->parent == NULL || *result == NULL)
1194 return rc;
1196 /* Module function entries will already have a symtree in
1197 the current namespace but will need one at module level. */
1198 if (module_fcn_entry)
1200 /* Present if entry is declared to be a module procedure. */
1201 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1202 if (st == NULL)
1203 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1205 else
1206 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1208 st->n.sym = sym;
1209 sym->refs++;
1211 /* See if the procedure should be a module procedure. */
1213 if (((sym->ns->proc_name != NULL
1214 && sym->ns->proc_name->attr.flavor == FL_MODULE
1215 && sym->attr.proc != PROC_MODULE)
1216 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1217 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1218 rc = 2;
1220 return rc;
1224 /* Verify that the given symbol representing a parameter is C
1225 interoperable, by checking to see if it was marked as such after
1226 its declaration. If the given symbol is not interoperable, a
1227 warning is reported, thus removing the need to return the status to
1228 the calling function. The standard does not require the user use
1229 one of the iso_c_binding named constants to declare an
1230 interoperable parameter, but we can't be sure if the param is C
1231 interop or not if the user doesn't. For example, integer(4) may be
1232 legal Fortran, but doesn't have meaning in C. It may interop with
1233 a number of the C types, which causes a problem because the
1234 compiler can't know which one. This code is almost certainly not
1235 portable, and the user will get what they deserve if the C type
1236 across platforms isn't always interoperable with integer(4). If
1237 the user had used something like integer(c_int) or integer(c_long),
1238 the compiler could have automatically handled the varying sizes
1239 across platforms. */
1241 bool
1242 gfc_verify_c_interop_param (gfc_symbol *sym)
1244 int is_c_interop = 0;
1245 bool retval = true;
1247 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1248 Don't repeat the checks here. */
1249 if (sym->attr.implicit_type)
1250 return true;
1252 /* For subroutines or functions that are passed to a BIND(C) procedure,
1253 they're interoperable if they're BIND(C) and their params are all
1254 interoperable. */
1255 if (sym->attr.flavor == FL_PROCEDURE)
1257 if (sym->attr.is_bind_c == 0)
1259 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1260 "attribute to be C interoperable", sym->name,
1261 &(sym->declared_at));
1262 return false;
1264 else
1266 if (sym->attr.is_c_interop == 1)
1267 /* We've already checked this procedure; don't check it again. */
1268 return true;
1269 else
1270 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1271 sym->common_block);
1275 /* See if we've stored a reference to a procedure that owns sym. */
1276 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1278 if (sym->ns->proc_name->attr.is_bind_c == 1)
1280 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1282 if (is_c_interop != 1)
1284 /* Make personalized messages to give better feedback. */
1285 if (sym->ts.type == BT_DERIVED)
1286 gfc_error ("Variable %qs at %L is a dummy argument to the "
1287 "BIND(C) procedure %qs but is not C interoperable "
1288 "because derived type %qs is not C interoperable",
1289 sym->name, &(sym->declared_at),
1290 sym->ns->proc_name->name,
1291 sym->ts.u.derived->name);
1292 else if (sym->ts.type == BT_CLASS)
1293 gfc_error ("Variable %qs at %L is a dummy argument to the "
1294 "BIND(C) procedure %qs but is not C interoperable "
1295 "because it is polymorphic",
1296 sym->name, &(sym->declared_at),
1297 sym->ns->proc_name->name);
1298 else if (warn_c_binding_type)
1299 gfc_warning (OPT_Wc_binding_type,
1300 "Variable %qs at %L is a dummy argument of the "
1301 "BIND(C) procedure %qs but may not be C "
1302 "interoperable",
1303 sym->name, &(sym->declared_at),
1304 sym->ns->proc_name->name);
1307 /* Character strings are only C interoperable if they have a
1308 length of 1. */
1309 if (sym->ts.type == BT_CHARACTER)
1311 gfc_charlen *cl = sym->ts.u.cl;
1312 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1313 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1315 gfc_error ("Character argument %qs at %L "
1316 "must be length 1 because "
1317 "procedure %qs is BIND(C)",
1318 sym->name, &sym->declared_at,
1319 sym->ns->proc_name->name);
1320 retval = false;
1324 /* We have to make sure that any param to a bind(c) routine does
1325 not have the allocatable, pointer, or optional attributes,
1326 according to J3/04-007, section 5.1. */
1327 if (sym->attr.allocatable == 1
1328 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1329 "ALLOCATABLE attribute in procedure %qs "
1330 "with BIND(C)", sym->name,
1331 &(sym->declared_at),
1332 sym->ns->proc_name->name))
1333 retval = false;
1335 if (sym->attr.pointer == 1
1336 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1337 "POINTER attribute in procedure %qs "
1338 "with BIND(C)", sym->name,
1339 &(sym->declared_at),
1340 sym->ns->proc_name->name))
1341 retval = false;
1343 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1345 gfc_error ("Scalar variable %qs at %L with POINTER or "
1346 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1347 " supported", sym->name, &(sym->declared_at),
1348 sym->ns->proc_name->name);
1349 retval = false;
1352 if (sym->attr.optional == 1 && sym->attr.value)
1354 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1355 "and the VALUE attribute because procedure %qs "
1356 "is BIND(C)", sym->name, &(sym->declared_at),
1357 sym->ns->proc_name->name);
1358 retval = false;
1360 else if (sym->attr.optional == 1
1361 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1362 "at %L with OPTIONAL attribute in "
1363 "procedure %qs which is BIND(C)",
1364 sym->name, &(sym->declared_at),
1365 sym->ns->proc_name->name))
1366 retval = false;
1368 /* Make sure that if it has the dimension attribute, that it is
1369 either assumed size or explicit shape. Deferred shape is already
1370 covered by the pointer/allocatable attribute. */
1371 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1372 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1373 "at %L as dummy argument to the BIND(C) "
1374 "procedure %qs at %L", sym->name,
1375 &(sym->declared_at),
1376 sym->ns->proc_name->name,
1377 &(sym->ns->proc_name->declared_at)))
1378 retval = false;
1382 return retval;
1387 /* Function called by variable_decl() that adds a name to the symbol table. */
1389 static bool
1390 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1391 gfc_array_spec **as, locus *var_locus)
1393 symbol_attribute attr;
1394 gfc_symbol *sym;
1395 int upper;
1396 gfc_symtree *st;
1398 /* Symbols in a submodule are host associated from the parent module or
1399 submodules. Therefore, they can be overridden by declarations in the
1400 submodule scope. Deal with this by attaching the existing symbol to
1401 a new symtree and recycling the old symtree with a new symbol... */
1402 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1403 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1404 && st->n.sym != NULL
1405 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1407 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1408 s->n.sym = st->n.sym;
1409 sym = gfc_new_symbol (name, gfc_current_ns);
1412 st->n.sym = sym;
1413 sym->refs++;
1414 gfc_set_sym_referenced (sym);
1416 /* ...Otherwise generate a new symtree and new symbol. */
1417 else if (gfc_get_symbol (name, NULL, &sym))
1418 return false;
1420 /* Check if the name has already been defined as a type. The
1421 first letter of the symtree will be in upper case then. Of
1422 course, this is only necessary if the upper case letter is
1423 actually different. */
1425 upper = TOUPPER(name[0]);
1426 if (upper != name[0])
1428 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1429 gfc_symtree *st;
1430 int nlen;
1432 nlen = strlen(name);
1433 gcc_assert (nlen <= GFC_MAX_SYMBOL_LEN);
1434 strncpy (u_name, name, nlen + 1);
1435 u_name[0] = upper;
1437 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1439 /* STRUCTURE types can alias symbol names */
1440 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1442 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1443 &st->n.sym->declared_at);
1444 return false;
1448 /* Start updating the symbol table. Add basic type attribute if present. */
1449 if (current_ts.type != BT_UNKNOWN
1450 && (sym->attr.implicit_type == 0
1451 || !gfc_compare_types (&sym->ts, &current_ts))
1452 && !gfc_add_type (sym, &current_ts, var_locus))
1453 return false;
1455 if (sym->ts.type == BT_CHARACTER)
1457 sym->ts.u.cl = cl;
1458 sym->ts.deferred = cl_deferred;
1461 /* Add dimension attribute if present. */
1462 if (!gfc_set_array_spec (sym, *as, var_locus))
1463 return false;
1464 *as = NULL;
1466 /* Add attribute to symbol. The copy is so that we can reset the
1467 dimension attribute. */
1468 attr = current_attr;
1469 attr.dimension = 0;
1470 attr.codimension = 0;
1472 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1473 return false;
1475 /* Finish any work that may need to be done for the binding label,
1476 if it's a bind(c). The bind(c) attr is found before the symbol
1477 is made, and before the symbol name (for data decls), so the
1478 current_ts is holding the binding label, or nothing if the
1479 name= attr wasn't given. Therefore, test here if we're dealing
1480 with a bind(c) and make sure the binding label is set correctly. */
1481 if (sym->attr.is_bind_c == 1)
1483 if (!sym->binding_label)
1485 /* Set the binding label and verify that if a NAME= was specified
1486 then only one identifier was in the entity-decl-list. */
1487 if (!set_binding_label (&sym->binding_label, sym->name,
1488 num_idents_on_line))
1489 return false;
1493 /* See if we know we're in a common block, and if it's a bind(c)
1494 common then we need to make sure we're an interoperable type. */
1495 if (sym->attr.in_common == 1)
1497 /* Test the common block object. */
1498 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1499 && sym->ts.is_c_interop != 1)
1501 gfc_error_now ("Variable %qs in common block %qs at %C "
1502 "must be declared with a C interoperable "
1503 "kind since common block %qs is BIND(C)",
1504 sym->name, sym->common_block->name,
1505 sym->common_block->name);
1506 gfc_clear_error ();
1510 sym->attr.implied_index = 0;
1512 /* Use the parameter expressions for a parameterized derived type. */
1513 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1514 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1515 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1517 if (sym->ts.type == BT_CLASS)
1518 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1520 return true;
1524 /* Set character constant to the given length. The constant will be padded or
1525 truncated. If we're inside an array constructor without a typespec, we
1526 additionally check that all elements have the same length; check_len -1
1527 means no checking. */
1529 void
1530 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1532 gfc_char_t *s;
1533 int slen;
1535 if (expr->ts.type != BT_CHARACTER)
1536 return;
1538 if (expr->expr_type != EXPR_CONSTANT)
1540 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1541 return;
1544 slen = expr->value.character.length;
1545 if (len != slen)
1547 s = gfc_get_wide_string (len + 1);
1548 memcpy (s, expr->value.character.string,
1549 MIN (len, slen) * sizeof (gfc_char_t));
1550 if (len > slen)
1551 gfc_wide_memset (&s[slen], ' ', len - slen);
1553 if (warn_character_truncation && slen > len)
1554 gfc_warning_now (OPT_Wcharacter_truncation,
1555 "CHARACTER expression at %L is being truncated "
1556 "(%d/%d)", &expr->where, slen, len);
1558 /* Apply the standard by 'hand' otherwise it gets cleared for
1559 initializers. */
1560 if (check_len != -1 && slen != check_len
1561 && !(gfc_option.allow_std & GFC_STD_GNU))
1562 gfc_error_now ("The CHARACTER elements of the array constructor "
1563 "at %L must have the same length (%d/%d)",
1564 &expr->where, slen, check_len);
1566 s[len] = '\0';
1567 free (expr->value.character.string);
1568 expr->value.character.string = s;
1569 expr->value.character.length = len;
1574 /* Function to create and update the enumerator history
1575 using the information passed as arguments.
1576 Pointer "max_enum" is also updated, to point to
1577 enum history node containing largest initializer.
1579 SYM points to the symbol node of enumerator.
1580 INIT points to its enumerator value. */
1582 static void
1583 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1585 enumerator_history *new_enum_history;
1586 gcc_assert (sym != NULL && init != NULL);
1588 new_enum_history = XCNEW (enumerator_history);
1590 new_enum_history->sym = sym;
1591 new_enum_history->initializer = init;
1592 new_enum_history->next = NULL;
1594 if (enum_history == NULL)
1596 enum_history = new_enum_history;
1597 max_enum = enum_history;
1599 else
1601 new_enum_history->next = enum_history;
1602 enum_history = new_enum_history;
1604 if (mpz_cmp (max_enum->initializer->value.integer,
1605 new_enum_history->initializer->value.integer) < 0)
1606 max_enum = new_enum_history;
1611 /* Function to free enum kind history. */
1613 void
1614 gfc_free_enum_history (void)
1616 enumerator_history *current = enum_history;
1617 enumerator_history *next;
1619 while (current != NULL)
1621 next = current->next;
1622 free (current);
1623 current = next;
1625 max_enum = NULL;
1626 enum_history = NULL;
1630 /* Function called by variable_decl() that adds an initialization
1631 expression to a symbol. */
1633 static bool
1634 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1636 symbol_attribute attr;
1637 gfc_symbol *sym;
1638 gfc_expr *init;
1640 init = *initp;
1641 if (find_special (name, &sym, false))
1642 return false;
1644 attr = sym->attr;
1646 /* If this symbol is confirming an implicit parameter type,
1647 then an initialization expression is not allowed. */
1648 if (attr.flavor == FL_PARAMETER
1649 && sym->value != NULL
1650 && *initp != NULL)
1652 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1653 sym->name);
1654 return false;
1657 if (init == NULL)
1659 /* An initializer is required for PARAMETER declarations. */
1660 if (attr.flavor == FL_PARAMETER)
1662 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1663 return false;
1666 else
1668 /* If a variable appears in a DATA block, it cannot have an
1669 initializer. */
1670 if (sym->attr.data)
1672 gfc_error ("Variable %qs at %C with an initializer already "
1673 "appears in a DATA statement", sym->name);
1674 return false;
1677 /* Check if the assignment can happen. This has to be put off
1678 until later for derived type variables and procedure pointers. */
1679 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1680 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1681 && !sym->attr.proc_pointer
1682 && !gfc_check_assign_symbol (sym, NULL, init))
1683 return false;
1685 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1686 && init->ts.type == BT_CHARACTER)
1688 /* Update symbol character length according initializer. */
1689 if (!gfc_check_assign_symbol (sym, NULL, init))
1690 return false;
1692 if (sym->ts.u.cl->length == NULL)
1694 int clen;
1695 /* If there are multiple CHARACTER variables declared on the
1696 same line, we don't want them to share the same length. */
1697 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1699 if (sym->attr.flavor == FL_PARAMETER)
1701 if (init->expr_type == EXPR_CONSTANT)
1703 clen = init->value.character.length;
1704 sym->ts.u.cl->length
1705 = gfc_get_int_expr (gfc_default_integer_kind,
1706 NULL, clen);
1708 else if (init->expr_type == EXPR_ARRAY)
1710 if (init->ts.u.cl)
1712 const gfc_expr *length = init->ts.u.cl->length;
1713 if (length->expr_type != EXPR_CONSTANT)
1715 gfc_error ("Cannot initialize parameter array "
1716 "at %L "
1717 "with variable length elements",
1718 &sym->declared_at);
1719 return false;
1721 clen = mpz_get_si (length->value.integer);
1723 else if (init->value.constructor)
1725 gfc_constructor *c;
1726 c = gfc_constructor_first (init->value.constructor);
1727 clen = c->expr->value.character.length;
1729 else
1730 gcc_unreachable ();
1731 sym->ts.u.cl->length
1732 = gfc_get_int_expr (gfc_default_integer_kind,
1733 NULL, clen);
1735 else if (init->ts.u.cl && init->ts.u.cl->length)
1736 sym->ts.u.cl->length =
1737 gfc_copy_expr (sym->value->ts.u.cl->length);
1740 /* Update initializer character length according symbol. */
1741 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1743 int len;
1745 if (!gfc_specification_expr (sym->ts.u.cl->length))
1746 return false;
1748 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1750 if (init->expr_type == EXPR_CONSTANT)
1751 gfc_set_constant_character_len (len, init, -1);
1752 else if (init->expr_type == EXPR_ARRAY)
1754 gfc_constructor *c;
1756 /* Build a new charlen to prevent simplification from
1757 deleting the length before it is resolved. */
1758 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1759 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1761 for (c = gfc_constructor_first (init->value.constructor);
1762 c; c = gfc_constructor_next (c))
1763 gfc_set_constant_character_len (len, c->expr, -1);
1768 /* If sym is implied-shape, set its upper bounds from init. */
1769 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1770 && sym->as->type == AS_IMPLIED_SHAPE)
1772 int dim;
1774 if (init->rank == 0)
1776 gfc_error ("Can't initialize implied-shape array at %L"
1777 " with scalar", &sym->declared_at);
1778 return false;
1781 /* Shape should be present, we get an initialization expression. */
1782 gcc_assert (init->shape);
1784 for (dim = 0; dim < sym->as->rank; ++dim)
1786 int k;
1787 gfc_expr *e, *lower;
1789 lower = sym->as->lower[dim];
1791 /* If the lower bound is an array element from another
1792 parameterized array, then it is marked with EXPR_VARIABLE and
1793 is an initialization expression. Try to reduce it. */
1794 if (lower->expr_type == EXPR_VARIABLE)
1795 gfc_reduce_init_expr (lower);
1797 if (lower->expr_type == EXPR_CONSTANT)
1799 /* All dimensions must be without upper bound. */
1800 gcc_assert (!sym->as->upper[dim]);
1802 k = lower->ts.kind;
1803 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1804 mpz_add (e->value.integer, lower->value.integer,
1805 init->shape[dim]);
1806 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1807 sym->as->upper[dim] = e;
1809 else
1811 gfc_error ("Non-constant lower bound in implied-shape"
1812 " declaration at %L", &lower->where);
1813 return false;
1817 sym->as->type = AS_EXPLICIT;
1820 /* Need to check if the expression we initialized this
1821 to was one of the iso_c_binding named constants. If so,
1822 and we're a parameter (constant), let it be iso_c.
1823 For example:
1824 integer(c_int), parameter :: my_int = c_int
1825 integer(my_int) :: my_int_2
1826 If we mark my_int as iso_c (since we can see it's value
1827 is equal to one of the named constants), then my_int_2
1828 will be considered C interoperable. */
1829 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
1831 sym->ts.is_iso_c |= init->ts.is_iso_c;
1832 sym->ts.is_c_interop |= init->ts.is_c_interop;
1833 /* attr bits needed for module files. */
1834 sym->attr.is_iso_c |= init->ts.is_iso_c;
1835 sym->attr.is_c_interop |= init->ts.is_c_interop;
1836 if (init->ts.is_iso_c)
1837 sym->ts.f90_type = init->ts.f90_type;
1840 /* Add initializer. Make sure we keep the ranks sane. */
1841 if (sym->attr.dimension && init->rank == 0)
1843 mpz_t size;
1844 gfc_expr *array;
1845 int n;
1846 if (sym->attr.flavor == FL_PARAMETER
1847 && init->expr_type == EXPR_CONSTANT
1848 && spec_size (sym->as, &size)
1849 && mpz_cmp_si (size, 0) > 0)
1851 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1852 &init->where);
1853 for (n = 0; n < (int)mpz_get_si (size); n++)
1854 gfc_constructor_append_expr (&array->value.constructor,
1855 n == 0
1856 ? init
1857 : gfc_copy_expr (init),
1858 &init->where);
1860 array->shape = gfc_get_shape (sym->as->rank);
1861 for (n = 0; n < sym->as->rank; n++)
1862 spec_dimen_size (sym->as, n, &array->shape[n]);
1864 init = array;
1865 mpz_clear (size);
1867 init->rank = sym->as->rank;
1870 sym->value = init;
1871 if (sym->attr.save == SAVE_NONE)
1872 sym->attr.save = SAVE_IMPLICIT;
1873 *initp = NULL;
1876 return true;
1880 /* Function called by variable_decl() that adds a name to a structure
1881 being built. */
1883 static bool
1884 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1885 gfc_array_spec **as)
1887 gfc_state_data *s;
1888 gfc_component *c;
1890 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1891 constructing, it must have the pointer attribute. */
1892 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1893 && current_ts.u.derived == gfc_current_block ()
1894 && current_attr.pointer == 0)
1896 if (current_attr.allocatable
1897 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
1898 "must have the POINTER attribute"))
1900 return false;
1902 else if (current_attr.allocatable == 0)
1904 gfc_error ("Component at %C must have the POINTER attribute");
1905 return false;
1909 /* F03:C437. */
1910 if (current_ts.type == BT_CLASS
1911 && !(current_attr.pointer || current_attr.allocatable))
1913 gfc_error ("Component %qs with CLASS at %C must be allocatable "
1914 "or pointer", name);
1915 return false;
1918 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1920 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1922 gfc_error ("Array component of structure at %C must have explicit "
1923 "or deferred shape");
1924 return false;
1928 /* If we are in a nested union/map definition, gfc_add_component will not
1929 properly find repeated components because:
1930 (i) gfc_add_component does a flat search, where components of unions
1931 and maps are implicity chained so nested components may conflict.
1932 (ii) Unions and maps are not linked as components of their parent
1933 structures until after they are parsed.
1934 For (i) we use gfc_find_component which searches recursively, and for (ii)
1935 we search each block directly from the parse stack until we find the top
1936 level structure. */
1938 s = gfc_state_stack;
1939 if (s->state == COMP_UNION || s->state == COMP_MAP)
1941 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
1943 c = gfc_find_component (s->sym, name, true, true, NULL);
1944 if (c != NULL)
1946 gfc_error_now ("Component %qs at %C already declared at %L",
1947 name, &c->loc);
1948 return false;
1950 /* Break after we've searched the entire chain. */
1951 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
1952 break;
1953 s = s->previous;
1957 if (!gfc_add_component (gfc_current_block(), name, &c))
1958 return false;
1960 c->ts = current_ts;
1961 if (c->ts.type == BT_CHARACTER)
1962 c->ts.u.cl = cl;
1964 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
1965 && c->ts.kind == 0 && saved_kind_expr != NULL)
1966 c->kind_expr = gfc_copy_expr (saved_kind_expr);
1968 c->attr = current_attr;
1970 c->initializer = *init;
1971 *init = NULL;
1973 c->as = *as;
1974 if (c->as != NULL)
1976 if (c->as->corank)
1977 c->attr.codimension = 1;
1978 if (c->as->rank)
1979 c->attr.dimension = 1;
1981 *as = NULL;
1983 gfc_apply_init (&c->ts, &c->attr, c->initializer);
1985 /* Check array components. */
1986 if (!c->attr.dimension)
1987 goto scalar;
1989 if (c->attr.pointer)
1991 if (c->as->type != AS_DEFERRED)
1993 gfc_error ("Pointer array component of structure at %C must have a "
1994 "deferred shape");
1995 return false;
1998 else if (c->attr.allocatable)
2000 if (c->as->type != AS_DEFERRED)
2002 gfc_error ("Allocatable component of structure at %C must have a "
2003 "deferred shape");
2004 return false;
2007 else
2009 if (c->as->type != AS_EXPLICIT)
2011 gfc_error ("Array component of structure at %C must have an "
2012 "explicit shape");
2013 return false;
2017 scalar:
2018 if (c->ts.type == BT_CLASS)
2019 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2021 if (c->attr.pdt_kind || c->attr.pdt_len)
2023 gfc_symbol *sym;
2024 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2025 0, &sym);
2026 if (sym == NULL)
2028 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2029 "in the type parameter name list at %L",
2030 c->name, &gfc_current_block ()->declared_at);
2031 return false;
2033 sym->ts = c->ts;
2034 sym->attr.pdt_kind = c->attr.pdt_kind;
2035 sym->attr.pdt_len = c->attr.pdt_len;
2036 if (c->initializer)
2037 sym->value = gfc_copy_expr (c->initializer);
2038 sym->attr.flavor = FL_VARIABLE;
2041 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2042 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2043 && decl_type_param_list)
2044 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2046 return true;
2050 /* Match a 'NULL()', and possibly take care of some side effects. */
2052 match
2053 gfc_match_null (gfc_expr **result)
2055 gfc_symbol *sym;
2056 match m, m2 = MATCH_NO;
2058 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2059 return MATCH_ERROR;
2061 if (m == MATCH_NO)
2063 locus old_loc;
2064 char name[GFC_MAX_SYMBOL_LEN + 1];
2066 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2067 return m2;
2069 old_loc = gfc_current_locus;
2070 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2071 return MATCH_ERROR;
2072 if (m2 != MATCH_YES
2073 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2074 return MATCH_ERROR;
2075 if (m2 == MATCH_NO)
2077 gfc_current_locus = old_loc;
2078 return MATCH_NO;
2082 /* The NULL symbol now has to be/become an intrinsic function. */
2083 if (gfc_get_symbol ("null", NULL, &sym))
2085 gfc_error ("NULL() initialization at %C is ambiguous");
2086 return MATCH_ERROR;
2089 gfc_intrinsic_symbol (sym);
2091 if (sym->attr.proc != PROC_INTRINSIC
2092 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2093 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2094 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2095 return MATCH_ERROR;
2097 *result = gfc_get_null_expr (&gfc_current_locus);
2099 /* Invalid per F2008, C512. */
2100 if (m2 == MATCH_YES)
2102 gfc_error ("NULL() initialization at %C may not have MOLD");
2103 return MATCH_ERROR;
2106 return MATCH_YES;
2110 /* Match the initialization expr for a data pointer or procedure pointer. */
2112 static match
2113 match_pointer_init (gfc_expr **init, int procptr)
2115 match m;
2117 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2119 gfc_error ("Initialization of pointer at %C is not allowed in "
2120 "a PURE procedure");
2121 return MATCH_ERROR;
2123 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2125 /* Match NULL() initialization. */
2126 m = gfc_match_null (init);
2127 if (m != MATCH_NO)
2128 return m;
2130 /* Match non-NULL initialization. */
2131 gfc_matching_ptr_assignment = !procptr;
2132 gfc_matching_procptr_assignment = procptr;
2133 m = gfc_match_rvalue (init);
2134 gfc_matching_ptr_assignment = 0;
2135 gfc_matching_procptr_assignment = 0;
2136 if (m == MATCH_ERROR)
2137 return MATCH_ERROR;
2138 else if (m == MATCH_NO)
2140 gfc_error ("Error in pointer initialization at %C");
2141 return MATCH_ERROR;
2144 if (!procptr && !gfc_resolve_expr (*init))
2145 return MATCH_ERROR;
2147 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2148 "initialization at %C"))
2149 return MATCH_ERROR;
2151 return MATCH_YES;
2155 static bool
2156 check_function_name (char *name)
2158 /* In functions that have a RESULT variable defined, the function name always
2159 refers to function calls. Therefore, the name is not allowed to appear in
2160 specification statements. When checking this, be careful about
2161 'hidden' procedure pointer results ('ppr@'). */
2163 if (gfc_current_state () == COMP_FUNCTION)
2165 gfc_symbol *block = gfc_current_block ();
2166 if (block && block->result && block->result != block
2167 && strcmp (block->result->name, "ppr@") != 0
2168 && strcmp (block->name, name) == 0)
2170 gfc_error ("Function name %qs not allowed at %C", name);
2171 return false;
2175 return true;
2179 /* Match a variable name with an optional initializer. When this
2180 subroutine is called, a variable is expected to be parsed next.
2181 Depending on what is happening at the moment, updates either the
2182 symbol table or the current interface. */
2184 static match
2185 variable_decl (int elem)
2187 char name[GFC_MAX_SYMBOL_LEN + 1];
2188 static unsigned int fill_id = 0;
2189 gfc_expr *initializer, *char_len;
2190 gfc_array_spec *as;
2191 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2192 gfc_charlen *cl;
2193 bool cl_deferred;
2194 locus var_locus;
2195 match m;
2196 bool t;
2197 gfc_symbol *sym;
2199 initializer = NULL;
2200 as = NULL;
2201 cp_as = NULL;
2203 /* When we get here, we've just matched a list of attributes and
2204 maybe a type and a double colon. The next thing we expect to see
2205 is the name of the symbol. */
2207 /* If we are parsing a structure with legacy support, we allow the symbol
2208 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2209 m = MATCH_NO;
2210 gfc_gobble_whitespace ();
2211 if (gfc_peek_ascii_char () == '%')
2213 gfc_next_ascii_char ();
2214 m = gfc_match ("fill");
2217 if (m != MATCH_YES)
2219 m = gfc_match_name (name);
2220 if (m != MATCH_YES)
2221 goto cleanup;
2224 else
2226 m = MATCH_ERROR;
2227 if (gfc_current_state () != COMP_STRUCTURE)
2229 if (flag_dec_structure)
2230 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2231 else
2232 gfc_error ("%qs at %C is a DEC extension, enable with "
2233 "%<-fdec-structure%>", "%FILL");
2234 goto cleanup;
2237 if (attr_seen)
2239 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2240 goto cleanup;
2243 /* %FILL components are given invalid fortran names. */
2244 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2245 m = MATCH_YES;
2248 var_locus = gfc_current_locus;
2250 /* Now we could see the optional array spec. or character length. */
2251 m = gfc_match_array_spec (&as, true, true);
2252 if (m == MATCH_ERROR)
2253 goto cleanup;
2255 if (m == MATCH_NO)
2256 as = gfc_copy_array_spec (current_as);
2257 else if (current_as
2258 && !merge_array_spec (current_as, as, true))
2260 m = MATCH_ERROR;
2261 goto cleanup;
2264 if (flag_cray_pointer)
2265 cp_as = gfc_copy_array_spec (as);
2267 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2268 determine (and check) whether it can be implied-shape. If it
2269 was parsed as assumed-size, change it because PARAMETERs can not
2270 be assumed-size. */
2271 if (as)
2273 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2275 m = MATCH_ERROR;
2276 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2277 name, &var_locus);
2278 goto cleanup;
2281 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2282 && current_attr.flavor == FL_PARAMETER)
2283 as->type = AS_IMPLIED_SHAPE;
2285 if (as->type == AS_IMPLIED_SHAPE
2286 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2287 &var_locus))
2289 m = MATCH_ERROR;
2290 goto cleanup;
2294 char_len = NULL;
2295 cl = NULL;
2296 cl_deferred = false;
2298 if (current_ts.type == BT_CHARACTER)
2300 switch (match_char_length (&char_len, &cl_deferred, false))
2302 case MATCH_YES:
2303 cl = gfc_new_charlen (gfc_current_ns, NULL);
2305 cl->length = char_len;
2306 break;
2308 /* Non-constant lengths need to be copied after the first
2309 element. Also copy assumed lengths. */
2310 case MATCH_NO:
2311 if (elem > 1
2312 && (current_ts.u.cl->length == NULL
2313 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2315 cl = gfc_new_charlen (gfc_current_ns, NULL);
2316 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2318 else
2319 cl = current_ts.u.cl;
2321 cl_deferred = current_ts.deferred;
2323 break;
2325 case MATCH_ERROR:
2326 goto cleanup;
2330 /* The dummy arguments and result of the abreviated form of MODULE
2331 PROCEDUREs, used in SUBMODULES should not be redefined. */
2332 if (gfc_current_ns->proc_name
2333 && gfc_current_ns->proc_name->abr_modproc_decl)
2335 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2336 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2338 m = MATCH_ERROR;
2339 gfc_error ("%qs at %C is a redefinition of the declaration "
2340 "in the corresponding interface for MODULE "
2341 "PROCEDURE %qs", sym->name,
2342 gfc_current_ns->proc_name->name);
2343 goto cleanup;
2347 /* %FILL components may not have initializers. */
2348 if (strncmp (name, "%FILL", 5) == 0 && gfc_match_eos () != MATCH_YES)
2350 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2351 m = MATCH_ERROR;
2352 goto cleanup;
2355 /* If this symbol has already shown up in a Cray Pointer declaration,
2356 and this is not a component declaration,
2357 then we want to set the type & bail out. */
2358 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2360 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2361 if (sym != NULL && sym->attr.cray_pointee)
2363 sym->ts.type = current_ts.type;
2364 sym->ts.kind = current_ts.kind;
2365 sym->ts.u.cl = cl;
2366 sym->ts.u.derived = current_ts.u.derived;
2367 sym->ts.is_c_interop = current_ts.is_c_interop;
2368 sym->ts.is_iso_c = current_ts.is_iso_c;
2369 m = MATCH_YES;
2371 /* Check to see if we have an array specification. */
2372 if (cp_as != NULL)
2374 if (sym->as != NULL)
2376 gfc_error ("Duplicate array spec for Cray pointee at %C");
2377 gfc_free_array_spec (cp_as);
2378 m = MATCH_ERROR;
2379 goto cleanup;
2381 else
2383 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2384 gfc_internal_error ("Couldn't set pointee array spec.");
2386 /* Fix the array spec. */
2387 m = gfc_mod_pointee_as (sym->as);
2388 if (m == MATCH_ERROR)
2389 goto cleanup;
2392 goto cleanup;
2394 else
2396 gfc_free_array_spec (cp_as);
2400 /* Procedure pointer as function result. */
2401 if (gfc_current_state () == COMP_FUNCTION
2402 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2403 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2404 strcpy (name, "ppr@");
2406 if (gfc_current_state () == COMP_FUNCTION
2407 && strcmp (name, gfc_current_block ()->name) == 0
2408 && gfc_current_block ()->result
2409 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2410 strcpy (name, "ppr@");
2412 /* OK, we've successfully matched the declaration. Now put the
2413 symbol in the current namespace, because it might be used in the
2414 optional initialization expression for this symbol, e.g. this is
2415 perfectly legal:
2417 integer, parameter :: i = huge(i)
2419 This is only true for parameters or variables of a basic type.
2420 For components of derived types, it is not true, so we don't
2421 create a symbol for those yet. If we fail to create the symbol,
2422 bail out. */
2423 if (!gfc_comp_struct (gfc_current_state ())
2424 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2426 m = MATCH_ERROR;
2427 goto cleanup;
2430 if (!check_function_name (name))
2432 m = MATCH_ERROR;
2433 goto cleanup;
2436 /* We allow old-style initializations of the form
2437 integer i /2/, j(4) /3*3, 1/
2438 (if no colon has been seen). These are different from data
2439 statements in that initializers are only allowed to apply to the
2440 variable immediately preceding, i.e.
2441 integer i, j /1, 2/
2442 is not allowed. Therefore we have to do some work manually, that
2443 could otherwise be left to the matchers for DATA statements. */
2445 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2447 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2448 "initialization at %C"))
2449 return MATCH_ERROR;
2451 /* Allow old style initializations for components of STRUCTUREs and MAPs
2452 but not components of derived types. */
2453 else if (gfc_current_state () == COMP_DERIVED)
2455 gfc_error ("Invalid old style initialization for derived type "
2456 "component at %C");
2457 m = MATCH_ERROR;
2458 goto cleanup;
2461 /* For structure components, read the initializer as a special
2462 expression and let the rest of this function apply the initializer
2463 as usual. */
2464 else if (gfc_comp_struct (gfc_current_state ()))
2466 m = match_clist_expr (&initializer, &current_ts, as);
2467 if (m == MATCH_NO)
2468 gfc_error ("Syntax error in old style initialization of %s at %C",
2469 name);
2470 if (m != MATCH_YES)
2471 goto cleanup;
2474 /* Otherwise we treat the old style initialization just like a
2475 DATA declaration for the current variable. */
2476 else
2477 return match_old_style_init (name);
2480 /* The double colon must be present in order to have initializers.
2481 Otherwise the statement is ambiguous with an assignment statement. */
2482 if (colon_seen)
2484 if (gfc_match (" =>") == MATCH_YES)
2486 if (!current_attr.pointer)
2488 gfc_error ("Initialization at %C isn't for a pointer variable");
2489 m = MATCH_ERROR;
2490 goto cleanup;
2493 m = match_pointer_init (&initializer, 0);
2494 if (m != MATCH_YES)
2495 goto cleanup;
2497 else if (gfc_match_char ('=') == MATCH_YES)
2499 if (current_attr.pointer)
2501 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2502 "not %<=%>");
2503 m = MATCH_ERROR;
2504 goto cleanup;
2507 m = gfc_match_init_expr (&initializer);
2508 if (m == MATCH_NO)
2510 gfc_error ("Expected an initialization expression at %C");
2511 m = MATCH_ERROR;
2514 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2515 && !gfc_comp_struct (gfc_state_stack->state))
2517 gfc_error ("Initialization of variable at %C is not allowed in "
2518 "a PURE procedure");
2519 m = MATCH_ERROR;
2522 if (current_attr.flavor != FL_PARAMETER
2523 && !gfc_comp_struct (gfc_state_stack->state))
2524 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2526 if (m != MATCH_YES)
2527 goto cleanup;
2531 if (initializer != NULL && current_attr.allocatable
2532 && gfc_comp_struct (gfc_current_state ()))
2534 gfc_error ("Initialization of allocatable component at %C is not "
2535 "allowed");
2536 m = MATCH_ERROR;
2537 goto cleanup;
2540 if (gfc_current_state () == COMP_DERIVED
2541 && gfc_current_block ()->attr.pdt_template)
2543 gfc_symbol *param;
2544 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2545 0, &param);
2546 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2548 gfc_error ("The component with KIND or LEN attribute at %C does not "
2549 "not appear in the type parameter list at %L",
2550 &gfc_current_block ()->declared_at);
2551 m = MATCH_ERROR;
2552 goto cleanup;
2554 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2556 gfc_error ("The component at %C that appears in the type parameter "
2557 "list at %L has neither the KIND nor LEN attribute",
2558 &gfc_current_block ()->declared_at);
2559 m = MATCH_ERROR;
2560 goto cleanup;
2562 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2564 gfc_error ("The component at %C which is a type parameter must be "
2565 "a scalar");
2566 m = MATCH_ERROR;
2567 goto cleanup;
2569 else if (param && initializer)
2570 param->value = gfc_copy_expr (initializer);
2573 /* Add the initializer. Note that it is fine if initializer is
2574 NULL here, because we sometimes also need to check if a
2575 declaration *must* have an initialization expression. */
2576 if (!gfc_comp_struct (gfc_current_state ()))
2577 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2578 else
2580 if (current_ts.type == BT_DERIVED
2581 && !current_attr.pointer && !initializer)
2582 initializer = gfc_default_initializer (&current_ts);
2583 t = build_struct (name, cl, &initializer, &as);
2585 /* If we match a nested structure definition we expect to see the
2586 * body even if the variable declarations blow up, so we need to keep
2587 * the structure declaration around. */
2588 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2589 gfc_commit_symbol (gfc_new_block);
2592 m = (t) ? MATCH_YES : MATCH_ERROR;
2594 cleanup:
2595 /* Free stuff up and return. */
2596 gfc_free_expr (initializer);
2597 gfc_free_array_spec (as);
2599 return m;
2603 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2604 This assumes that the byte size is equal to the kind number for
2605 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2607 match
2608 gfc_match_old_kind_spec (gfc_typespec *ts)
2610 match m;
2611 int original_kind;
2613 if (gfc_match_char ('*') != MATCH_YES)
2614 return MATCH_NO;
2616 m = gfc_match_small_literal_int (&ts->kind, NULL);
2617 if (m != MATCH_YES)
2618 return MATCH_ERROR;
2620 original_kind = ts->kind;
2622 /* Massage the kind numbers for complex types. */
2623 if (ts->type == BT_COMPLEX)
2625 if (ts->kind % 2)
2627 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2628 gfc_basic_typename (ts->type), original_kind);
2629 return MATCH_ERROR;
2631 ts->kind /= 2;
2635 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2636 ts->kind = 8;
2638 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2640 if (ts->kind == 4)
2642 if (flag_real4_kind == 8)
2643 ts->kind = 8;
2644 if (flag_real4_kind == 10)
2645 ts->kind = 10;
2646 if (flag_real4_kind == 16)
2647 ts->kind = 16;
2650 if (ts->kind == 8)
2652 if (flag_real8_kind == 4)
2653 ts->kind = 4;
2654 if (flag_real8_kind == 10)
2655 ts->kind = 10;
2656 if (flag_real8_kind == 16)
2657 ts->kind = 16;
2661 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2663 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2664 gfc_basic_typename (ts->type), original_kind);
2665 return MATCH_ERROR;
2668 if (!gfc_notify_std (GFC_STD_GNU,
2669 "Nonstandard type declaration %s*%d at %C",
2670 gfc_basic_typename(ts->type), original_kind))
2671 return MATCH_ERROR;
2673 return MATCH_YES;
2677 /* Match a kind specification. Since kinds are generally optional, we
2678 usually return MATCH_NO if something goes wrong. If a "kind="
2679 string is found, then we know we have an error. */
2681 match
2682 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2684 locus where, loc;
2685 gfc_expr *e;
2686 match m, n;
2687 char c;
2689 m = MATCH_NO;
2690 n = MATCH_YES;
2691 e = NULL;
2692 saved_kind_expr = NULL;
2694 where = loc = gfc_current_locus;
2696 if (kind_expr_only)
2697 goto kind_expr;
2699 if (gfc_match_char ('(') == MATCH_NO)
2700 return MATCH_NO;
2702 /* Also gobbles optional text. */
2703 if (gfc_match (" kind = ") == MATCH_YES)
2704 m = MATCH_ERROR;
2706 loc = gfc_current_locus;
2708 kind_expr:
2710 n = gfc_match_init_expr (&e);
2712 if (gfc_derived_parameter_expr (e))
2714 ts->kind = 0;
2715 saved_kind_expr = gfc_copy_expr (e);
2716 goto close_brackets;
2719 if (n != MATCH_YES)
2721 if (gfc_matching_function)
2723 /* The function kind expression might include use associated or
2724 imported parameters and try again after the specification
2725 expressions..... */
2726 if (gfc_match_char (')') != MATCH_YES)
2728 gfc_error ("Missing right parenthesis at %C");
2729 m = MATCH_ERROR;
2730 goto no_match;
2733 gfc_free_expr (e);
2734 gfc_undo_symbols ();
2735 return MATCH_YES;
2737 else
2739 /* ....or else, the match is real. */
2740 if (n == MATCH_NO)
2741 gfc_error ("Expected initialization expression at %C");
2742 if (n != MATCH_YES)
2743 return MATCH_ERROR;
2747 if (e->rank != 0)
2749 gfc_error ("Expected scalar initialization expression at %C");
2750 m = MATCH_ERROR;
2751 goto no_match;
2754 if (gfc_extract_int (e, &ts->kind, 1))
2756 m = MATCH_ERROR;
2757 goto no_match;
2760 /* Before throwing away the expression, let's see if we had a
2761 C interoperable kind (and store the fact). */
2762 if (e->ts.is_c_interop == 1)
2764 /* Mark this as C interoperable if being declared with one
2765 of the named constants from iso_c_binding. */
2766 ts->is_c_interop = e->ts.is_iso_c;
2767 ts->f90_type = e->ts.f90_type;
2768 if (e->symtree)
2769 ts->interop_kind = e->symtree->n.sym;
2772 gfc_free_expr (e);
2773 e = NULL;
2775 /* Ignore errors to this point, if we've gotten here. This means
2776 we ignore the m=MATCH_ERROR from above. */
2777 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2779 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2780 gfc_basic_typename (ts->type));
2781 gfc_current_locus = where;
2782 return MATCH_ERROR;
2785 /* Warn if, e.g., c_int is used for a REAL variable, but not
2786 if, e.g., c_double is used for COMPLEX as the standard
2787 explicitly says that the kind type parameter for complex and real
2788 variable is the same, i.e. c_float == c_float_complex. */
2789 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2790 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2791 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2792 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2793 "is %s", gfc_basic_typename (ts->f90_type), &where,
2794 gfc_basic_typename (ts->type));
2796 close_brackets:
2798 gfc_gobble_whitespace ();
2799 if ((c = gfc_next_ascii_char ()) != ')'
2800 && (ts->type != BT_CHARACTER || c != ','))
2802 if (ts->type == BT_CHARACTER)
2803 gfc_error ("Missing right parenthesis or comma at %C");
2804 else
2805 gfc_error ("Missing right parenthesis at %C");
2806 m = MATCH_ERROR;
2808 else
2809 /* All tests passed. */
2810 m = MATCH_YES;
2812 if(m == MATCH_ERROR)
2813 gfc_current_locus = where;
2815 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2816 ts->kind = 8;
2818 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2820 if (ts->kind == 4)
2822 if (flag_real4_kind == 8)
2823 ts->kind = 8;
2824 if (flag_real4_kind == 10)
2825 ts->kind = 10;
2826 if (flag_real4_kind == 16)
2827 ts->kind = 16;
2830 if (ts->kind == 8)
2832 if (flag_real8_kind == 4)
2833 ts->kind = 4;
2834 if (flag_real8_kind == 10)
2835 ts->kind = 10;
2836 if (flag_real8_kind == 16)
2837 ts->kind = 16;
2841 /* Return what we know from the test(s). */
2842 return m;
2844 no_match:
2845 gfc_free_expr (e);
2846 gfc_current_locus = where;
2847 return m;
2851 static match
2852 match_char_kind (int * kind, int * is_iso_c)
2854 locus where;
2855 gfc_expr *e;
2856 match m, n;
2857 bool fail;
2859 m = MATCH_NO;
2860 e = NULL;
2861 where = gfc_current_locus;
2863 n = gfc_match_init_expr (&e);
2865 if (n != MATCH_YES && gfc_matching_function)
2867 /* The expression might include use-associated or imported
2868 parameters and try again after the specification
2869 expressions. */
2870 gfc_free_expr (e);
2871 gfc_undo_symbols ();
2872 return MATCH_YES;
2875 if (n == MATCH_NO)
2876 gfc_error ("Expected initialization expression at %C");
2877 if (n != MATCH_YES)
2878 return MATCH_ERROR;
2880 if (e->rank != 0)
2882 gfc_error ("Expected scalar initialization expression at %C");
2883 m = MATCH_ERROR;
2884 goto no_match;
2887 if (gfc_derived_parameter_expr (e))
2889 saved_kind_expr = e;
2890 *kind = 0;
2891 return MATCH_YES;
2894 fail = gfc_extract_int (e, kind, 1);
2895 *is_iso_c = e->ts.is_iso_c;
2896 if (fail)
2898 m = MATCH_ERROR;
2899 goto no_match;
2902 gfc_free_expr (e);
2904 /* Ignore errors to this point, if we've gotten here. This means
2905 we ignore the m=MATCH_ERROR from above. */
2906 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2908 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2909 m = MATCH_ERROR;
2911 else
2912 /* All tests passed. */
2913 m = MATCH_YES;
2915 if (m == MATCH_ERROR)
2916 gfc_current_locus = where;
2918 /* Return what we know from the test(s). */
2919 return m;
2921 no_match:
2922 gfc_free_expr (e);
2923 gfc_current_locus = where;
2924 return m;
2928 /* Match the various kind/length specifications in a CHARACTER
2929 declaration. We don't return MATCH_NO. */
2931 match
2932 gfc_match_char_spec (gfc_typespec *ts)
2934 int kind, seen_length, is_iso_c;
2935 gfc_charlen *cl;
2936 gfc_expr *len;
2937 match m;
2938 bool deferred;
2940 len = NULL;
2941 seen_length = 0;
2942 kind = 0;
2943 is_iso_c = 0;
2944 deferred = false;
2946 /* Try the old-style specification first. */
2947 old_char_selector = 0;
2949 m = match_char_length (&len, &deferred, true);
2950 if (m != MATCH_NO)
2952 if (m == MATCH_YES)
2953 old_char_selector = 1;
2954 seen_length = 1;
2955 goto done;
2958 m = gfc_match_char ('(');
2959 if (m != MATCH_YES)
2961 m = MATCH_YES; /* Character without length is a single char. */
2962 goto done;
2965 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2966 if (gfc_match (" kind =") == MATCH_YES)
2968 m = match_char_kind (&kind, &is_iso_c);
2970 if (m == MATCH_ERROR)
2971 goto done;
2972 if (m == MATCH_NO)
2973 goto syntax;
2975 if (gfc_match (" , len =") == MATCH_NO)
2976 goto rparen;
2978 m = char_len_param_value (&len, &deferred);
2979 if (m == MATCH_NO)
2980 goto syntax;
2981 if (m == MATCH_ERROR)
2982 goto done;
2983 seen_length = 1;
2985 goto rparen;
2988 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2989 if (gfc_match (" len =") == MATCH_YES)
2991 m = char_len_param_value (&len, &deferred);
2992 if (m == MATCH_NO)
2993 goto syntax;
2994 if (m == MATCH_ERROR)
2995 goto done;
2996 seen_length = 1;
2998 if (gfc_match_char (')') == MATCH_YES)
2999 goto done;
3001 if (gfc_match (" , kind =") != MATCH_YES)
3002 goto syntax;
3004 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3005 goto done;
3007 goto rparen;
3010 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3011 m = char_len_param_value (&len, &deferred);
3012 if (m == MATCH_NO)
3013 goto syntax;
3014 if (m == MATCH_ERROR)
3015 goto done;
3016 seen_length = 1;
3018 m = gfc_match_char (')');
3019 if (m == MATCH_YES)
3020 goto done;
3022 if (gfc_match_char (',') != MATCH_YES)
3023 goto syntax;
3025 gfc_match (" kind ="); /* Gobble optional text. */
3027 m = match_char_kind (&kind, &is_iso_c);
3028 if (m == MATCH_ERROR)
3029 goto done;
3030 if (m == MATCH_NO)
3031 goto syntax;
3033 rparen:
3034 /* Require a right-paren at this point. */
3035 m = gfc_match_char (')');
3036 if (m == MATCH_YES)
3037 goto done;
3039 syntax:
3040 gfc_error ("Syntax error in CHARACTER declaration at %C");
3041 m = MATCH_ERROR;
3042 gfc_free_expr (len);
3043 return m;
3045 done:
3046 /* Deal with character functions after USE and IMPORT statements. */
3047 if (gfc_matching_function)
3049 gfc_free_expr (len);
3050 gfc_undo_symbols ();
3051 return MATCH_YES;
3054 if (m != MATCH_YES)
3056 gfc_free_expr (len);
3057 return m;
3060 /* Do some final massaging of the length values. */
3061 cl = gfc_new_charlen (gfc_current_ns, NULL);
3063 if (seen_length == 0)
3064 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
3065 else
3066 cl->length = len;
3068 ts->u.cl = cl;
3069 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3070 ts->deferred = deferred;
3072 /* We have to know if it was a C interoperable kind so we can
3073 do accurate type checking of bind(c) procs, etc. */
3074 if (kind != 0)
3075 /* Mark this as C interoperable if being declared with one
3076 of the named constants from iso_c_binding. */
3077 ts->is_c_interop = is_iso_c;
3078 else if (len != NULL)
3079 /* Here, we might have parsed something such as: character(c_char)
3080 In this case, the parsing code above grabs the c_char when
3081 looking for the length (line 1690, roughly). it's the last
3082 testcase for parsing the kind params of a character variable.
3083 However, it's not actually the length. this seems like it
3084 could be an error.
3085 To see if the user used a C interop kind, test the expr
3086 of the so called length, and see if it's C interoperable. */
3087 ts->is_c_interop = len->ts.is_iso_c;
3089 return MATCH_YES;
3093 /* Matches a RECORD declaration. */
3095 static match
3096 match_record_decl (char *name)
3098 locus old_loc;
3099 old_loc = gfc_current_locus;
3100 match m;
3102 m = gfc_match (" record /");
3103 if (m == MATCH_YES)
3105 if (!flag_dec_structure)
3107 gfc_current_locus = old_loc;
3108 gfc_error ("RECORD at %C is an extension, enable it with "
3109 "-fdec-structure");
3110 return MATCH_ERROR;
3112 m = gfc_match (" %n/", name);
3113 if (m == MATCH_YES)
3114 return MATCH_YES;
3117 gfc_current_locus = old_loc;
3118 if (flag_dec_structure
3119 && (gfc_match (" record% ") == MATCH_YES
3120 || gfc_match (" record%t") == MATCH_YES))
3121 gfc_error ("Structure name expected after RECORD at %C");
3122 if (m == MATCH_NO)
3123 return MATCH_NO;
3125 return MATCH_ERROR;
3129 /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3130 of expressions to substitute into the possibly parameterized expression
3131 'e'. Using a list is inefficient but should not be too bad since the
3132 number of type parameters is not likely to be large. */
3133 static bool
3134 insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3135 int* f)
3137 gfc_actual_arglist *param;
3138 gfc_expr *copy;
3140 if (e->expr_type != EXPR_VARIABLE)
3141 return false;
3143 gcc_assert (e->symtree);
3144 if (e->symtree->n.sym->attr.pdt_kind
3145 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3147 for (param = type_param_spec_list; param; param = param->next)
3148 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3149 break;
3151 if (param)
3153 copy = gfc_copy_expr (param->expr);
3154 *e = *copy;
3155 free (copy);
3159 return false;
3163 bool
3164 gfc_insert_kind_parameter_exprs (gfc_expr *e)
3166 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3170 bool
3171 gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3173 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3174 type_param_spec_list = param_list;
3175 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3176 type_param_spec_list = NULL;
3177 type_param_spec_list = old_param_spec_list;
3180 /* Determines the instance of a parameterized derived type to be used by
3181 matching determining the values of the kind parameters and using them
3182 in the name of the instance. If the instance exists, it is used, otherwise
3183 a new derived type is created. */
3184 match
3185 gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3186 gfc_actual_arglist **ext_param_list)
3188 /* The PDT template symbol. */
3189 gfc_symbol *pdt = *sym;
3190 /* The symbol for the parameter in the template f2k_namespace. */
3191 gfc_symbol *param;
3192 /* The hoped for instance of the PDT. */
3193 gfc_symbol *instance;
3194 /* The list of parameters appearing in the PDT declaration. */
3195 gfc_formal_arglist *type_param_name_list;
3196 /* Used to store the parameter specification list during recursive calls. */
3197 gfc_actual_arglist *old_param_spec_list;
3198 /* Pointers to the parameter specification being used. */
3199 gfc_actual_arglist *actual_param;
3200 gfc_actual_arglist *tail = NULL;
3201 /* Used to build up the name of the PDT instance. The prefix uses 4
3202 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3203 char name[GFC_MAX_SYMBOL_LEN + 21];
3205 bool name_seen = (param_list == NULL);
3206 bool assumed_seen = false;
3207 bool deferred_seen = false;
3208 bool spec_error = false;
3209 int kind_value, i;
3210 gfc_expr *kind_expr;
3211 gfc_component *c1, *c2;
3212 match m;
3214 type_param_spec_list = NULL;
3216 type_param_name_list = pdt->formal;
3217 actual_param = param_list;
3218 sprintf (name, "Pdt%s", pdt->name);
3220 /* Run through the parameter name list and pick up the actual
3221 parameter values or use the default values in the PDT declaration. */
3222 for (; type_param_name_list;
3223 type_param_name_list = type_param_name_list->next)
3225 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3227 if (actual_param->spec_type == SPEC_ASSUMED)
3228 spec_error = deferred_seen;
3229 else
3230 spec_error = assumed_seen;
3232 if (spec_error)
3234 gfc_error ("The type parameter spec list at %C cannot contain "
3235 "both ASSUMED and DEFERRED parameters");
3236 goto error_return;
3240 if (actual_param && actual_param->name)
3241 name_seen = true;
3242 param = type_param_name_list->sym;
3244 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3245 /* An error should already have been thrown in resolve.c
3246 (resolve_fl_derived0). */
3247 if (!pdt->attr.use_assoc && !c1)
3248 goto error_return;
3250 kind_expr = NULL;
3251 if (!name_seen)
3253 if (!actual_param && !(c1 && c1->initializer))
3255 gfc_error ("The type parameter spec list at %C does not contain "
3256 "enough parameter expressions");
3257 goto error_return;
3259 else if (!actual_param && c1 && c1->initializer)
3260 kind_expr = gfc_copy_expr (c1->initializer);
3261 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3262 kind_expr = gfc_copy_expr (actual_param->expr);
3264 else
3266 actual_param = param_list;
3267 for (;actual_param; actual_param = actual_param->next)
3268 if (actual_param->name
3269 && strcmp (actual_param->name, param->name) == 0)
3270 break;
3271 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3272 kind_expr = gfc_copy_expr (actual_param->expr);
3273 else
3275 if (c1->initializer)
3276 kind_expr = gfc_copy_expr (c1->initializer);
3277 else if (!(actual_param && param->attr.pdt_len))
3279 gfc_error ("The derived parameter '%qs' at %C does not "
3280 "have a default value", param->name);
3281 goto error_return;
3286 /* Store the current parameter expressions in a temporary actual
3287 arglist 'list' so that they can be substituted in the corresponding
3288 expressions in the PDT instance. */
3289 if (type_param_spec_list == NULL)
3291 type_param_spec_list = gfc_get_actual_arglist ();
3292 tail = type_param_spec_list;
3294 else
3296 tail->next = gfc_get_actual_arglist ();
3297 tail = tail->next;
3299 tail->name = param->name;
3301 if (kind_expr)
3303 /* Try simplification even for LEN expressions. */
3304 gfc_resolve_expr (kind_expr);
3305 gfc_simplify_expr (kind_expr, 1);
3306 /* Variable expressions seem to default to BT_PROCEDURE.
3307 TODO find out why this is and fix it. */
3308 if (kind_expr->ts.type != BT_INTEGER
3309 && kind_expr->ts.type != BT_PROCEDURE)
3311 gfc_error ("The parameter expression at %C must be of "
3312 "INTEGER type and not %s type",
3313 gfc_basic_typename (kind_expr->ts.type));
3314 goto error_return;
3317 tail->expr = gfc_copy_expr (kind_expr);
3320 if (actual_param)
3321 tail->spec_type = actual_param->spec_type;
3323 if (!param->attr.pdt_kind)
3325 if (!name_seen && actual_param)
3326 actual_param = actual_param->next;
3327 if (kind_expr)
3329 gfc_free_expr (kind_expr);
3330 kind_expr = NULL;
3332 continue;
3335 if (actual_param
3336 && (actual_param->spec_type == SPEC_ASSUMED
3337 || actual_param->spec_type == SPEC_DEFERRED))
3339 gfc_error ("The KIND parameter '%qs' at %C cannot either be "
3340 "ASSUMED or DEFERRED", param->name);
3341 goto error_return;
3344 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3346 gfc_error ("The value for the KIND parameter '%qs' at %C does not "
3347 "reduce to a constant expression", param->name);
3348 goto error_return;
3351 gfc_extract_int (kind_expr, &kind_value);
3352 sprintf (name, "%s_%d", name, kind_value);
3354 if (!name_seen && actual_param)
3355 actual_param = actual_param->next;
3356 gfc_free_expr (kind_expr);
3359 if (!name_seen && actual_param)
3361 gfc_error ("The type parameter spec list at %C contains too many "
3362 "parameter expressions");
3363 goto error_return;
3366 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3367 build it, using 'pdt' as a template. */
3368 if (gfc_get_symbol (name, pdt->ns, &instance))
3370 gfc_error ("Parameterized derived type at %C is ambiguous");
3371 goto error_return;
3374 m = MATCH_YES;
3376 if (instance->attr.flavor == FL_DERIVED
3377 && instance->attr.pdt_type)
3379 instance->refs++;
3380 if (ext_param_list)
3381 *ext_param_list = type_param_spec_list;
3382 *sym = instance;
3383 gfc_commit_symbols ();
3384 return m;
3387 /* Start building the new instance of the parameterized type. */
3388 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3389 instance->attr.pdt_template = 0;
3390 instance->attr.pdt_type = 1;
3391 instance->declared_at = gfc_current_locus;
3393 /* Add the components, replacing the parameters in all expressions
3394 with the expressions for their values in 'type_param_spec_list'. */
3395 c1 = pdt->components;
3396 tail = type_param_spec_list;
3397 for (; c1; c1 = c1->next)
3399 gfc_add_component (instance, c1->name, &c2);
3400 c2->ts = c1->ts;
3401 c2->attr = c1->attr;
3403 /* Deal with type extension by recursively calling this function
3404 to obtain the instance of the extended type. */
3405 if (gfc_current_state () != COMP_DERIVED
3406 && c1 == pdt->components
3407 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3408 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3409 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3411 gfc_formal_arglist *f;
3413 old_param_spec_list = type_param_spec_list;
3415 /* Obtain a spec list appropriate to the extended type..*/
3416 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3417 type_param_spec_list = actual_param;
3418 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3419 actual_param = actual_param->next;
3420 if (actual_param)
3422 gfc_free_actual_arglist (actual_param->next);
3423 actual_param->next = NULL;
3426 /* Now obtain the PDT instance for the extended type. */
3427 c2->param_list = type_param_spec_list;
3428 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3429 NULL);
3430 type_param_spec_list = old_param_spec_list;
3432 c2->ts.u.derived->refs++;
3433 gfc_set_sym_referenced (c2->ts.u.derived);
3435 /* Set extension level. */
3436 if (c2->ts.u.derived->attr.extension == 255)
3438 /* Since the extension field is 8 bit wide, we can only have
3439 up to 255 extension levels. */
3440 gfc_error ("Maximum extension level reached with type %qs at %L",
3441 c2->ts.u.derived->name,
3442 &c2->ts.u.derived->declared_at);
3443 goto error_return;
3445 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3447 /* Advance the position in the spec list by the number of
3448 parameters in the extended type. */
3449 tail = type_param_spec_list;
3450 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3451 tail = tail->next;
3453 continue;
3456 /* Set the component kind using the parameterized expression. */
3457 if (c1->ts.kind == 0 && c1->kind_expr != NULL)
3459 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3460 gfc_insert_kind_parameter_exprs (e);
3461 gfc_simplify_expr (e, 1);
3462 gfc_extract_int (e, &c2->ts.kind);
3463 gfc_free_expr (e);
3464 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3466 gfc_error ("Kind %d not supported for type %s at %C",
3467 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3468 goto error_return;
3472 /* Similarly, set the string length if parameterized. */
3473 if (c1->ts.type == BT_CHARACTER
3474 && c1->ts.u.cl->length
3475 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3477 gfc_expr *e;
3478 e = gfc_copy_expr (c1->ts.u.cl->length);
3479 gfc_insert_kind_parameter_exprs (e);
3480 gfc_simplify_expr (e, 1);
3481 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3482 c2->ts.u.cl->length = e;
3483 c2->attr.pdt_string = 1;
3486 /* Set up either the KIND/LEN initializer, if constant,
3487 or the parameterized expression. Use the template
3488 initializer if one is not already set in this instance. */
3489 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3491 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3492 c2->initializer = gfc_copy_expr (tail->expr);
3493 else if (tail && tail->expr)
3495 c2->param_list = gfc_get_actual_arglist ();
3496 c2->param_list->name = tail->name;
3497 c2->param_list->expr = gfc_copy_expr (tail->expr);
3498 c2->param_list->next = NULL;
3501 if (!c2->initializer && c1->initializer)
3502 c2->initializer = gfc_copy_expr (c1->initializer);
3504 tail = tail->next;
3507 /* Copy the array spec. */
3508 c2->as = gfc_copy_array_spec (c1->as);
3509 if (c1->ts.type == BT_CLASS)
3510 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
3512 /* Determine if an array spec is parameterized. If so, substitute
3513 in the parameter expressions for the bounds and set the pdt_array
3514 attribute. Notice that this attribute must be unconditionally set
3515 if this is an array of parameterized character length. */
3516 if (c1->as && c1->as->type == AS_EXPLICIT)
3518 bool pdt_array = false;
3520 /* Are the bounds of the array parameterized? */
3521 for (i = 0; i < c1->as->rank; i++)
3523 if (gfc_derived_parameter_expr (c1->as->lower[i]))
3524 pdt_array = true;
3525 if (gfc_derived_parameter_expr (c1->as->upper[i]))
3526 pdt_array = true;
3529 /* If they are, free the expressions for the bounds and
3530 replace them with the template expressions with substitute
3531 values. */
3532 for (i = 0; pdt_array && i < c1->as->rank; i++)
3534 gfc_expr *e;
3535 e = gfc_copy_expr (c1->as->lower[i]);
3536 gfc_insert_kind_parameter_exprs (e);
3537 gfc_simplify_expr (e, 1);
3538 gfc_free_expr (c2->as->lower[i]);
3539 c2->as->lower[i] = e;
3540 e = gfc_copy_expr (c1->as->upper[i]);
3541 gfc_insert_kind_parameter_exprs (e);
3542 gfc_simplify_expr (e, 1);
3543 gfc_free_expr (c2->as->upper[i]);
3544 c2->as->upper[i] = e;
3546 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
3549 /* Recurse into this function for PDT components. */
3550 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3551 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
3553 gfc_actual_arglist *params;
3554 /* The component in the template has a list of specification
3555 expressions derived from its declaration. */
3556 params = gfc_copy_actual_arglist (c1->param_list);
3557 actual_param = params;
3558 /* Substitute the template parameters with the expressions
3559 from the specification list. */
3560 for (;actual_param; actual_param = actual_param->next)
3561 gfc_insert_parameter_exprs (actual_param->expr,
3562 type_param_spec_list);
3564 /* Now obtain the PDT instance for the component. */
3565 old_param_spec_list = type_param_spec_list;
3566 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
3567 type_param_spec_list = old_param_spec_list;
3569 c2->param_list = params;
3570 if (!(c2->attr.pointer || c2->attr.allocatable))
3571 c2->initializer = gfc_default_initializer (&c2->ts);
3573 if (c2->attr.allocatable)
3574 instance->attr.alloc_comp = 1;
3578 gfc_commit_symbol (instance);
3579 if (ext_param_list)
3580 *ext_param_list = type_param_spec_list;
3581 *sym = instance;
3582 return m;
3584 error_return:
3585 gfc_free_actual_arglist (type_param_spec_list);
3586 return MATCH_ERROR;
3590 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
3591 structure to the matched specification. This is necessary for FUNCTION and
3592 IMPLICIT statements.
3594 If implicit_flag is nonzero, then we don't check for the optional
3595 kind specification. Not doing so is needed for matching an IMPLICIT
3596 statement correctly. */
3598 match
3599 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
3601 char name[GFC_MAX_SYMBOL_LEN + 1];
3602 gfc_symbol *sym, *dt_sym;
3603 match m;
3604 char c;
3605 bool seen_deferred_kind, matched_type;
3606 const char *dt_name;
3608 decl_type_param_list = NULL;
3610 /* A belt and braces check that the typespec is correctly being treated
3611 as a deferred characteristic association. */
3612 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
3613 && (gfc_current_block ()->result->ts.kind == -1)
3614 && (ts->kind == -1);
3615 gfc_clear_ts (ts);
3616 if (seen_deferred_kind)
3617 ts->kind = -1;
3619 /* Clear the current binding label, in case one is given. */
3620 curr_binding_label = NULL;
3622 if (gfc_match (" byte") == MATCH_YES)
3624 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
3625 return MATCH_ERROR;
3627 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
3629 gfc_error ("BYTE type used at %C "
3630 "is not available on the target machine");
3631 return MATCH_ERROR;
3634 ts->type = BT_INTEGER;
3635 ts->kind = 1;
3636 return MATCH_YES;
3640 m = gfc_match (" type (");
3641 matched_type = (m == MATCH_YES);
3642 if (matched_type)
3644 gfc_gobble_whitespace ();
3645 if (gfc_peek_ascii_char () == '*')
3647 if ((m = gfc_match ("*)")) != MATCH_YES)
3648 return m;
3649 if (gfc_comp_struct (gfc_current_state ()))
3651 gfc_error ("Assumed type at %C is not allowed for components");
3652 return MATCH_ERROR;
3654 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
3655 "at %C"))
3656 return MATCH_ERROR;
3657 ts->type = BT_ASSUMED;
3658 return MATCH_YES;
3661 m = gfc_match ("%n", name);
3662 matched_type = (m == MATCH_YES);
3665 if ((matched_type && strcmp ("integer", name) == 0)
3666 || (!matched_type && gfc_match (" integer") == MATCH_YES))
3668 ts->type = BT_INTEGER;
3669 ts->kind = gfc_default_integer_kind;
3670 goto get_kind;
3673 if ((matched_type && strcmp ("character", name) == 0)
3674 || (!matched_type && gfc_match (" character") == MATCH_YES))
3676 if (matched_type
3677 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3678 "intrinsic-type-spec at %C"))
3679 return MATCH_ERROR;
3681 ts->type = BT_CHARACTER;
3682 if (implicit_flag == 0)
3683 m = gfc_match_char_spec (ts);
3684 else
3685 m = MATCH_YES;
3687 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
3688 m = MATCH_ERROR;
3690 return m;
3693 if ((matched_type && strcmp ("real", name) == 0)
3694 || (!matched_type && gfc_match (" real") == MATCH_YES))
3696 ts->type = BT_REAL;
3697 ts->kind = gfc_default_real_kind;
3698 goto get_kind;
3701 if ((matched_type
3702 && (strcmp ("doubleprecision", name) == 0
3703 || (strcmp ("double", name) == 0
3704 && gfc_match (" precision") == MATCH_YES)))
3705 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
3707 if (matched_type
3708 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3709 "intrinsic-type-spec at %C"))
3710 return MATCH_ERROR;
3711 if (matched_type && gfc_match_char (')') != MATCH_YES)
3712 return MATCH_ERROR;
3714 ts->type = BT_REAL;
3715 ts->kind = gfc_default_double_kind;
3716 return MATCH_YES;
3719 if ((matched_type && strcmp ("complex", name) == 0)
3720 || (!matched_type && gfc_match (" complex") == MATCH_YES))
3722 ts->type = BT_COMPLEX;
3723 ts->kind = gfc_default_complex_kind;
3724 goto get_kind;
3727 if ((matched_type
3728 && (strcmp ("doublecomplex", name) == 0
3729 || (strcmp ("double", name) == 0
3730 && gfc_match (" complex") == MATCH_YES)))
3731 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
3733 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
3734 return MATCH_ERROR;
3736 if (matched_type
3737 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3738 "intrinsic-type-spec at %C"))
3739 return MATCH_ERROR;
3741 if (matched_type && gfc_match_char (')') != MATCH_YES)
3742 return MATCH_ERROR;
3744 ts->type = BT_COMPLEX;
3745 ts->kind = gfc_default_double_kind;
3746 return MATCH_YES;
3749 if ((matched_type && strcmp ("logical", name) == 0)
3750 || (!matched_type && gfc_match (" logical") == MATCH_YES))
3752 ts->type = BT_LOGICAL;
3753 ts->kind = gfc_default_logical_kind;
3754 goto get_kind;
3757 if (matched_type)
3759 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
3760 if (m == MATCH_ERROR)
3761 return m;
3763 m = gfc_match_char (')');
3766 if (m != MATCH_YES)
3767 m = match_record_decl (name);
3769 if (matched_type || m == MATCH_YES)
3771 ts->type = BT_DERIVED;
3772 /* We accept record/s/ or type(s) where s is a structure, but we
3773 * don't need all the extra derived-type stuff for structures. */
3774 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
3776 gfc_error ("Type name %qs at %C is ambiguous", name);
3777 return MATCH_ERROR;
3780 if (sym && sym->attr.flavor == FL_DERIVED
3781 && sym->attr.pdt_template
3782 && gfc_current_state () != COMP_DERIVED)
3784 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
3785 if (m != MATCH_YES)
3786 return m;
3787 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
3788 ts->u.derived = sym;
3789 strcpy (name, gfc_dt_lower_string (sym->name));
3792 if (sym && sym->attr.flavor == FL_STRUCT)
3794 ts->u.derived = sym;
3795 return MATCH_YES;
3797 /* Actually a derived type. */
3800 else
3802 /* Match nested STRUCTURE declarations; only valid within another
3803 structure declaration. */
3804 if (flag_dec_structure
3805 && (gfc_current_state () == COMP_STRUCTURE
3806 || gfc_current_state () == COMP_MAP))
3808 m = gfc_match (" structure");
3809 if (m == MATCH_YES)
3811 m = gfc_match_structure_decl ();
3812 if (m == MATCH_YES)
3814 /* gfc_new_block is updated by match_structure_decl. */
3815 ts->type = BT_DERIVED;
3816 ts->u.derived = gfc_new_block;
3817 return MATCH_YES;
3820 if (m == MATCH_ERROR)
3821 return MATCH_ERROR;
3824 /* Match CLASS declarations. */
3825 m = gfc_match (" class ( * )");
3826 if (m == MATCH_ERROR)
3827 return MATCH_ERROR;
3828 else if (m == MATCH_YES)
3830 gfc_symbol *upe;
3831 gfc_symtree *st;
3832 ts->type = BT_CLASS;
3833 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
3834 if (upe == NULL)
3836 upe = gfc_new_symbol ("STAR", gfc_current_ns);
3837 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
3838 st->n.sym = upe;
3839 gfc_set_sym_referenced (upe);
3840 upe->refs++;
3841 upe->ts.type = BT_VOID;
3842 upe->attr.unlimited_polymorphic = 1;
3843 /* This is essential to force the construction of
3844 unlimited polymorphic component class containers. */
3845 upe->attr.zero_comp = 1;
3846 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
3847 &gfc_current_locus))
3848 return MATCH_ERROR;
3850 else
3852 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
3853 st->n.sym = upe;
3854 upe->refs++;
3856 ts->u.derived = upe;
3857 return m;
3860 m = gfc_match (" class (");
3862 if (m == MATCH_YES)
3863 m = gfc_match ("%n", name);
3864 else
3865 return m;
3867 if (m != MATCH_YES)
3868 return m;
3869 ts->type = BT_CLASS;
3871 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
3872 return MATCH_ERROR;
3874 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
3875 if (m == MATCH_ERROR)
3876 return m;
3878 m = gfc_match_char (')');
3879 if (m != MATCH_YES)
3880 return m;
3883 /* Defer association of the derived type until the end of the
3884 specification block. However, if the derived type can be
3885 found, add it to the typespec. */
3886 if (gfc_matching_function)
3888 ts->u.derived = NULL;
3889 if (gfc_current_state () != COMP_INTERFACE
3890 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
3892 sym = gfc_find_dt_in_generic (sym);
3893 ts->u.derived = sym;
3895 return MATCH_YES;
3898 /* Search for the name but allow the components to be defined later. If
3899 type = -1, this typespec has been seen in a function declaration but
3900 the type could not be accessed at that point. The actual derived type is
3901 stored in a symtree with the first letter of the name capitalized; the
3902 symtree with the all lower-case name contains the associated
3903 generic function. */
3904 dt_name = gfc_dt_upper_string (name);
3905 sym = NULL;
3906 dt_sym = NULL;
3907 if (ts->kind != -1)
3909 gfc_get_ha_symbol (name, &sym);
3910 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
3912 gfc_error ("Type name %qs at %C is ambiguous", name);
3913 return MATCH_ERROR;
3915 if (sym->generic && !dt_sym)
3916 dt_sym = gfc_find_dt_in_generic (sym);
3918 /* Host associated PDTs can get confused with their constructors
3919 because they ar instantiated in the template's namespace. */
3920 if (!dt_sym)
3922 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
3924 gfc_error ("Type name %qs at %C is ambiguous", name);
3925 return MATCH_ERROR;
3927 if (dt_sym && !dt_sym->attr.pdt_type)
3928 dt_sym = NULL;
3931 else if (ts->kind == -1)
3933 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
3934 || gfc_current_ns->has_import_set;
3935 gfc_find_symbol (name, NULL, iface, &sym);
3936 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
3938 gfc_error ("Type name %qs at %C is ambiguous", name);
3939 return MATCH_ERROR;
3941 if (sym && sym->generic && !dt_sym)
3942 dt_sym = gfc_find_dt_in_generic (sym);
3944 ts->kind = 0;
3945 if (sym == NULL)
3946 return MATCH_NO;
3949 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
3950 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
3951 || sym->attr.subroutine)
3953 gfc_error ("Type name %qs at %C conflicts with previously declared "
3954 "entity at %L, which has the same name", name,
3955 &sym->declared_at);
3956 return MATCH_ERROR;
3959 if (sym && sym->attr.flavor == FL_DERIVED
3960 && sym->attr.pdt_template
3961 && gfc_current_state () != COMP_DERIVED)
3963 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
3964 if (m != MATCH_YES)
3965 return m;
3966 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
3967 ts->u.derived = sym;
3968 strcpy (name, gfc_dt_lower_string (sym->name));
3971 gfc_save_symbol_data (sym);
3972 gfc_set_sym_referenced (sym);
3973 if (!sym->attr.generic
3974 && !gfc_add_generic (&sym->attr, sym->name, NULL))
3975 return MATCH_ERROR;
3977 if (!sym->attr.function
3978 && !gfc_add_function (&sym->attr, sym->name, NULL))
3979 return MATCH_ERROR;
3981 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
3982 && dt_sym->attr.pdt_template
3983 && gfc_current_state () != COMP_DERIVED)
3985 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
3986 if (m != MATCH_YES)
3987 return m;
3988 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
3991 if (!dt_sym)
3993 gfc_interface *intr, *head;
3995 /* Use upper case to save the actual derived-type symbol. */
3996 gfc_get_symbol (dt_name, NULL, &dt_sym);
3997 dt_sym->name = gfc_get_string ("%s", sym->name);
3998 head = sym->generic;
3999 intr = gfc_get_interface ();
4000 intr->sym = dt_sym;
4001 intr->where = gfc_current_locus;
4002 intr->next = head;
4003 sym->generic = intr;
4004 sym->attr.if_source = IFSRC_DECL;
4006 else
4007 gfc_save_symbol_data (dt_sym);
4009 gfc_set_sym_referenced (dt_sym);
4011 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4012 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4013 return MATCH_ERROR;
4015 ts->u.derived = dt_sym;
4017 return MATCH_YES;
4019 get_kind:
4020 if (matched_type
4021 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4022 "intrinsic-type-spec at %C"))
4023 return MATCH_ERROR;
4025 /* For all types except double, derived and character, look for an
4026 optional kind specifier. MATCH_NO is actually OK at this point. */
4027 if (implicit_flag == 1)
4029 if (matched_type && gfc_match_char (')') != MATCH_YES)
4030 return MATCH_ERROR;
4032 return MATCH_YES;
4035 if (gfc_current_form == FORM_FREE)
4037 c = gfc_peek_ascii_char ();
4038 if (!gfc_is_whitespace (c) && c != '*' && c != '('
4039 && c != ':' && c != ',')
4041 if (matched_type && c == ')')
4043 gfc_next_ascii_char ();
4044 return MATCH_YES;
4046 return MATCH_NO;
4050 m = gfc_match_kind_spec (ts, false);
4051 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4053 m = gfc_match_old_kind_spec (ts);
4054 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4055 return MATCH_ERROR;
4058 if (matched_type && gfc_match_char (')') != MATCH_YES)
4059 return MATCH_ERROR;
4061 /* Defer association of the KIND expression of function results
4062 until after USE and IMPORT statements. */
4063 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4064 || gfc_matching_function)
4065 return MATCH_YES;
4067 if (m == MATCH_NO)
4068 m = MATCH_YES; /* No kind specifier found. */
4070 return m;
4074 /* Match an IMPLICIT NONE statement. Actually, this statement is
4075 already matched in parse.c, or we would not end up here in the
4076 first place. So the only thing we need to check, is if there is
4077 trailing garbage. If not, the match is successful. */
4079 match
4080 gfc_match_implicit_none (void)
4082 char c;
4083 match m;
4084 char name[GFC_MAX_SYMBOL_LEN + 1];
4085 bool type = false;
4086 bool external = false;
4087 locus cur_loc = gfc_current_locus;
4089 if (gfc_current_ns->seen_implicit_none
4090 || gfc_current_ns->has_implicit_none_export)
4092 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4093 return MATCH_ERROR;
4096 gfc_gobble_whitespace ();
4097 c = gfc_peek_ascii_char ();
4098 if (c == '(')
4100 (void) gfc_next_ascii_char ();
4101 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
4102 return MATCH_ERROR;
4104 gfc_gobble_whitespace ();
4105 if (gfc_peek_ascii_char () == ')')
4107 (void) gfc_next_ascii_char ();
4108 type = true;
4110 else
4111 for(;;)
4113 m = gfc_match (" %n", name);
4114 if (m != MATCH_YES)
4115 return MATCH_ERROR;
4117 if (strcmp (name, "type") == 0)
4118 type = true;
4119 else if (strcmp (name, "external") == 0)
4120 external = true;
4121 else
4122 return MATCH_ERROR;
4124 gfc_gobble_whitespace ();
4125 c = gfc_next_ascii_char ();
4126 if (c == ',')
4127 continue;
4128 if (c == ')')
4129 break;
4130 return MATCH_ERROR;
4133 else
4134 type = true;
4136 if (gfc_match_eos () != MATCH_YES)
4137 return MATCH_ERROR;
4139 gfc_set_implicit_none (type, external, &cur_loc);
4141 return MATCH_YES;
4145 /* Match the letter range(s) of an IMPLICIT statement. */
4147 static match
4148 match_implicit_range (void)
4150 char c, c1, c2;
4151 int inner;
4152 locus cur_loc;
4154 cur_loc = gfc_current_locus;
4156 gfc_gobble_whitespace ();
4157 c = gfc_next_ascii_char ();
4158 if (c != '(')
4160 gfc_error ("Missing character range in IMPLICIT at %C");
4161 goto bad;
4164 inner = 1;
4165 while (inner)
4167 gfc_gobble_whitespace ();
4168 c1 = gfc_next_ascii_char ();
4169 if (!ISALPHA (c1))
4170 goto bad;
4172 gfc_gobble_whitespace ();
4173 c = gfc_next_ascii_char ();
4175 switch (c)
4177 case ')':
4178 inner = 0; /* Fall through. */
4180 case ',':
4181 c2 = c1;
4182 break;
4184 case '-':
4185 gfc_gobble_whitespace ();
4186 c2 = gfc_next_ascii_char ();
4187 if (!ISALPHA (c2))
4188 goto bad;
4190 gfc_gobble_whitespace ();
4191 c = gfc_next_ascii_char ();
4193 if ((c != ',') && (c != ')'))
4194 goto bad;
4195 if (c == ')')
4196 inner = 0;
4198 break;
4200 default:
4201 goto bad;
4204 if (c1 > c2)
4206 gfc_error ("Letters must be in alphabetic order in "
4207 "IMPLICIT statement at %C");
4208 goto bad;
4211 /* See if we can add the newly matched range to the pending
4212 implicits from this IMPLICIT statement. We do not check for
4213 conflicts with whatever earlier IMPLICIT statements may have
4214 set. This is done when we've successfully finished matching
4215 the current one. */
4216 if (!gfc_add_new_implicit_range (c1, c2))
4217 goto bad;
4220 return MATCH_YES;
4222 bad:
4223 gfc_syntax_error (ST_IMPLICIT);
4225 gfc_current_locus = cur_loc;
4226 return MATCH_ERROR;
4230 /* Match an IMPLICIT statement, storing the types for
4231 gfc_set_implicit() if the statement is accepted by the parser.
4232 There is a strange looking, but legal syntactic construction
4233 possible. It looks like:
4235 IMPLICIT INTEGER (a-b) (c-d)
4237 This is legal if "a-b" is a constant expression that happens to
4238 equal one of the legal kinds for integers. The real problem
4239 happens with an implicit specification that looks like:
4241 IMPLICIT INTEGER (a-b)
4243 In this case, a typespec matcher that is "greedy" (as most of the
4244 matchers are) gobbles the character range as a kindspec, leaving
4245 nothing left. We therefore have to go a bit more slowly in the
4246 matching process by inhibiting the kindspec checking during
4247 typespec matching and checking for a kind later. */
4249 match
4250 gfc_match_implicit (void)
4252 gfc_typespec ts;
4253 locus cur_loc;
4254 char c;
4255 match m;
4257 if (gfc_current_ns->seen_implicit_none)
4259 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4260 "statement");
4261 return MATCH_ERROR;
4264 gfc_clear_ts (&ts);
4266 /* We don't allow empty implicit statements. */
4267 if (gfc_match_eos () == MATCH_YES)
4269 gfc_error ("Empty IMPLICIT statement at %C");
4270 return MATCH_ERROR;
4275 /* First cleanup. */
4276 gfc_clear_new_implicit ();
4278 /* A basic type is mandatory here. */
4279 m = gfc_match_decl_type_spec (&ts, 1);
4280 if (m == MATCH_ERROR)
4281 goto error;
4282 if (m == MATCH_NO)
4283 goto syntax;
4285 cur_loc = gfc_current_locus;
4286 m = match_implicit_range ();
4288 if (m == MATCH_YES)
4290 /* We may have <TYPE> (<RANGE>). */
4291 gfc_gobble_whitespace ();
4292 c = gfc_peek_ascii_char ();
4293 if (c == ',' || c == '\n' || c == ';' || c == '!')
4295 /* Check for CHARACTER with no length parameter. */
4296 if (ts.type == BT_CHARACTER && !ts.u.cl)
4298 ts.kind = gfc_default_character_kind;
4299 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4300 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
4301 NULL, 1);
4304 /* Record the Successful match. */
4305 if (!gfc_merge_new_implicit (&ts))
4306 return MATCH_ERROR;
4307 if (c == ',')
4308 c = gfc_next_ascii_char ();
4309 else if (gfc_match_eos () == MATCH_ERROR)
4310 goto error;
4311 continue;
4314 gfc_current_locus = cur_loc;
4317 /* Discard the (incorrectly) matched range. */
4318 gfc_clear_new_implicit ();
4320 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4321 if (ts.type == BT_CHARACTER)
4322 m = gfc_match_char_spec (&ts);
4323 else
4325 m = gfc_match_kind_spec (&ts, false);
4326 if (m == MATCH_NO)
4328 m = gfc_match_old_kind_spec (&ts);
4329 if (m == MATCH_ERROR)
4330 goto error;
4331 if (m == MATCH_NO)
4332 goto syntax;
4335 if (m == MATCH_ERROR)
4336 goto error;
4338 m = match_implicit_range ();
4339 if (m == MATCH_ERROR)
4340 goto error;
4341 if (m == MATCH_NO)
4342 goto syntax;
4344 gfc_gobble_whitespace ();
4345 c = gfc_next_ascii_char ();
4346 if (c != ',' && gfc_match_eos () != MATCH_YES)
4347 goto syntax;
4349 if (!gfc_merge_new_implicit (&ts))
4350 return MATCH_ERROR;
4352 while (c == ',');
4354 return MATCH_YES;
4356 syntax:
4357 gfc_syntax_error (ST_IMPLICIT);
4359 error:
4360 return MATCH_ERROR;
4364 match
4365 gfc_match_import (void)
4367 char name[GFC_MAX_SYMBOL_LEN + 1];
4368 match m;
4369 gfc_symbol *sym;
4370 gfc_symtree *st;
4372 if (gfc_current_ns->proc_name == NULL
4373 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4375 gfc_error ("IMPORT statement at %C only permitted in "
4376 "an INTERFACE body");
4377 return MATCH_ERROR;
4380 if (gfc_current_ns->proc_name->attr.module_procedure)
4382 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4383 "in a module procedure interface body");
4384 return MATCH_ERROR;
4387 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4388 return MATCH_ERROR;
4390 if (gfc_match_eos () == MATCH_YES)
4392 /* All host variables should be imported. */
4393 gfc_current_ns->has_import_set = 1;
4394 return MATCH_YES;
4397 if (gfc_match (" ::") == MATCH_YES)
4399 if (gfc_match_eos () == MATCH_YES)
4401 gfc_error ("Expecting list of named entities at %C");
4402 return MATCH_ERROR;
4406 for(;;)
4408 sym = NULL;
4409 m = gfc_match (" %n", name);
4410 switch (m)
4412 case MATCH_YES:
4413 if (gfc_current_ns->parent != NULL
4414 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4416 gfc_error ("Type name %qs at %C is ambiguous", name);
4417 return MATCH_ERROR;
4419 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
4420 && gfc_find_symbol (name,
4421 gfc_current_ns->proc_name->ns->parent,
4422 1, &sym))
4424 gfc_error ("Type name %qs at %C is ambiguous", name);
4425 return MATCH_ERROR;
4428 if (sym == NULL)
4430 gfc_error ("Cannot IMPORT %qs from host scoping unit "
4431 "at %C - does not exist.", name);
4432 return MATCH_ERROR;
4435 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4437 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4438 "at %C", name);
4439 goto next_item;
4442 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4443 st->n.sym = sym;
4444 sym->refs++;
4445 sym->attr.imported = 1;
4447 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4449 /* The actual derived type is stored in a symtree with the first
4450 letter of the name capitalized; the symtree with the all
4451 lower-case name contains the associated generic function. */
4452 st = gfc_new_symtree (&gfc_current_ns->sym_root,
4453 gfc_dt_upper_string (name));
4454 st->n.sym = sym;
4455 sym->refs++;
4456 sym->attr.imported = 1;
4459 goto next_item;
4461 case MATCH_NO:
4462 break;
4464 case MATCH_ERROR:
4465 return MATCH_ERROR;
4468 next_item:
4469 if (gfc_match_eos () == MATCH_YES)
4470 break;
4471 if (gfc_match_char (',') != MATCH_YES)
4472 goto syntax;
4475 return MATCH_YES;
4477 syntax:
4478 gfc_error ("Syntax error in IMPORT statement at %C");
4479 return MATCH_ERROR;
4483 /* A minimal implementation of gfc_match without whitespace, escape
4484 characters or variable arguments. Returns true if the next
4485 characters match the TARGET template exactly. */
4487 static bool
4488 match_string_p (const char *target)
4490 const char *p;
4492 for (p = target; *p; p++)
4493 if ((char) gfc_next_ascii_char () != *p)
4494 return false;
4495 return true;
4498 /* Matches an attribute specification including array specs. If
4499 successful, leaves the variables current_attr and current_as
4500 holding the specification. Also sets the colon_seen variable for
4501 later use by matchers associated with initializations.
4503 This subroutine is a little tricky in the sense that we don't know
4504 if we really have an attr-spec until we hit the double colon.
4505 Until that time, we can only return MATCH_NO. This forces us to
4506 check for duplicate specification at this level. */
4508 static match
4509 match_attr_spec (void)
4511 /* Modifiers that can exist in a type statement. */
4512 enum
4513 { GFC_DECL_BEGIN = 0,
4514 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
4515 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
4516 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
4517 DECL_STATIC, DECL_AUTOMATIC,
4518 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
4519 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
4520 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
4523 /* GFC_DECL_END is the sentinel, index starts at 0. */
4524 #define NUM_DECL GFC_DECL_END
4526 locus start, seen_at[NUM_DECL];
4527 int seen[NUM_DECL];
4528 unsigned int d;
4529 const char *attr;
4530 match m;
4531 bool t;
4533 gfc_clear_attr (&current_attr);
4534 start = gfc_current_locus;
4536 current_as = NULL;
4537 colon_seen = 0;
4538 attr_seen = 0;
4540 /* See if we get all of the keywords up to the final double colon. */
4541 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4542 seen[d] = 0;
4544 for (;;)
4546 char ch;
4548 d = DECL_NONE;
4549 gfc_gobble_whitespace ();
4551 ch = gfc_next_ascii_char ();
4552 if (ch == ':')
4554 /* This is the successful exit condition for the loop. */
4555 if (gfc_next_ascii_char () == ':')
4556 break;
4558 else if (ch == ',')
4560 gfc_gobble_whitespace ();
4561 switch (gfc_peek_ascii_char ())
4563 case 'a':
4564 gfc_next_ascii_char ();
4565 switch (gfc_next_ascii_char ())
4567 case 'l':
4568 if (match_string_p ("locatable"))
4570 /* Matched "allocatable". */
4571 d = DECL_ALLOCATABLE;
4573 break;
4575 case 's':
4576 if (match_string_p ("ynchronous"))
4578 /* Matched "asynchronous". */
4579 d = DECL_ASYNCHRONOUS;
4581 break;
4583 case 'u':
4584 if (match_string_p ("tomatic"))
4586 /* Matched "automatic". */
4587 d = DECL_AUTOMATIC;
4589 break;
4591 break;
4593 case 'b':
4594 /* Try and match the bind(c). */
4595 m = gfc_match_bind_c (NULL, true);
4596 if (m == MATCH_YES)
4597 d = DECL_IS_BIND_C;
4598 else if (m == MATCH_ERROR)
4599 goto cleanup;
4600 break;
4602 case 'c':
4603 gfc_next_ascii_char ();
4604 if ('o' != gfc_next_ascii_char ())
4605 break;
4606 switch (gfc_next_ascii_char ())
4608 case 'd':
4609 if (match_string_p ("imension"))
4611 d = DECL_CODIMENSION;
4612 break;
4614 /* FALLTHRU */
4615 case 'n':
4616 if (match_string_p ("tiguous"))
4618 d = DECL_CONTIGUOUS;
4619 break;
4622 break;
4624 case 'd':
4625 if (match_string_p ("dimension"))
4626 d = DECL_DIMENSION;
4627 break;
4629 case 'e':
4630 if (match_string_p ("external"))
4631 d = DECL_EXTERNAL;
4632 break;
4634 case 'i':
4635 if (match_string_p ("int"))
4637 ch = gfc_next_ascii_char ();
4638 if (ch == 'e')
4640 if (match_string_p ("nt"))
4642 /* Matched "intent". */
4643 /* TODO: Call match_intent_spec from here. */
4644 if (gfc_match (" ( in out )") == MATCH_YES)
4645 d = DECL_INOUT;
4646 else if (gfc_match (" ( in )") == MATCH_YES)
4647 d = DECL_IN;
4648 else if (gfc_match (" ( out )") == MATCH_YES)
4649 d = DECL_OUT;
4652 else if (ch == 'r')
4654 if (match_string_p ("insic"))
4656 /* Matched "intrinsic". */
4657 d = DECL_INTRINSIC;
4661 break;
4663 case 'k':
4664 if (match_string_p ("kind"))
4665 d = DECL_KIND;
4666 break;
4668 case 'l':
4669 if (match_string_p ("len"))
4670 d = DECL_LEN;
4671 break;
4673 case 'o':
4674 if (match_string_p ("optional"))
4675 d = DECL_OPTIONAL;
4676 break;
4678 case 'p':
4679 gfc_next_ascii_char ();
4680 switch (gfc_next_ascii_char ())
4682 case 'a':
4683 if (match_string_p ("rameter"))
4685 /* Matched "parameter". */
4686 d = DECL_PARAMETER;
4688 break;
4690 case 'o':
4691 if (match_string_p ("inter"))
4693 /* Matched "pointer". */
4694 d = DECL_POINTER;
4696 break;
4698 case 'r':
4699 ch = gfc_next_ascii_char ();
4700 if (ch == 'i')
4702 if (match_string_p ("vate"))
4704 /* Matched "private". */
4705 d = DECL_PRIVATE;
4708 else if (ch == 'o')
4710 if (match_string_p ("tected"))
4712 /* Matched "protected". */
4713 d = DECL_PROTECTED;
4716 break;
4718 case 'u':
4719 if (match_string_p ("blic"))
4721 /* Matched "public". */
4722 d = DECL_PUBLIC;
4724 break;
4726 break;
4728 case 's':
4729 gfc_next_ascii_char ();
4730 switch (gfc_next_ascii_char ())
4732 case 'a':
4733 if (match_string_p ("ve"))
4735 /* Matched "save". */
4736 d = DECL_SAVE;
4738 break;
4740 case 't':
4741 if (match_string_p ("atic"))
4743 /* Matched "static". */
4744 d = DECL_STATIC;
4746 break;
4748 break;
4750 case 't':
4751 if (match_string_p ("target"))
4752 d = DECL_TARGET;
4753 break;
4755 case 'v':
4756 gfc_next_ascii_char ();
4757 ch = gfc_next_ascii_char ();
4758 if (ch == 'a')
4760 if (match_string_p ("lue"))
4762 /* Matched "value". */
4763 d = DECL_VALUE;
4766 else if (ch == 'o')
4768 if (match_string_p ("latile"))
4770 /* Matched "volatile". */
4771 d = DECL_VOLATILE;
4774 break;
4778 /* No double colon and no recognizable decl_type, so assume that
4779 we've been looking at something else the whole time. */
4780 if (d == DECL_NONE)
4782 m = MATCH_NO;
4783 goto cleanup;
4786 /* Check to make sure any parens are paired up correctly. */
4787 if (gfc_match_parens () == MATCH_ERROR)
4789 m = MATCH_ERROR;
4790 goto cleanup;
4793 seen[d]++;
4794 seen_at[d] = gfc_current_locus;
4796 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
4798 gfc_array_spec *as = NULL;
4800 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
4801 d == DECL_CODIMENSION);
4803 if (current_as == NULL)
4804 current_as = as;
4805 else if (m == MATCH_YES)
4807 if (!merge_array_spec (as, current_as, false))
4808 m = MATCH_ERROR;
4809 free (as);
4812 if (m == MATCH_NO)
4814 if (d == DECL_CODIMENSION)
4815 gfc_error ("Missing codimension specification at %C");
4816 else
4817 gfc_error ("Missing dimension specification at %C");
4818 m = MATCH_ERROR;
4821 if (m == MATCH_ERROR)
4822 goto cleanup;
4826 /* Since we've seen a double colon, we have to be looking at an
4827 attr-spec. This means that we can now issue errors. */
4828 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4829 if (seen[d] > 1)
4831 switch (d)
4833 case DECL_ALLOCATABLE:
4834 attr = "ALLOCATABLE";
4835 break;
4836 case DECL_ASYNCHRONOUS:
4837 attr = "ASYNCHRONOUS";
4838 break;
4839 case DECL_CODIMENSION:
4840 attr = "CODIMENSION";
4841 break;
4842 case DECL_CONTIGUOUS:
4843 attr = "CONTIGUOUS";
4844 break;
4845 case DECL_DIMENSION:
4846 attr = "DIMENSION";
4847 break;
4848 case DECL_EXTERNAL:
4849 attr = "EXTERNAL";
4850 break;
4851 case DECL_IN:
4852 attr = "INTENT (IN)";
4853 break;
4854 case DECL_OUT:
4855 attr = "INTENT (OUT)";
4856 break;
4857 case DECL_INOUT:
4858 attr = "INTENT (IN OUT)";
4859 break;
4860 case DECL_INTRINSIC:
4861 attr = "INTRINSIC";
4862 break;
4863 case DECL_OPTIONAL:
4864 attr = "OPTIONAL";
4865 break;
4866 case DECL_KIND:
4867 attr = "KIND";
4868 break;
4869 case DECL_LEN:
4870 attr = "LEN";
4871 break;
4872 case DECL_PARAMETER:
4873 attr = "PARAMETER";
4874 break;
4875 case DECL_POINTER:
4876 attr = "POINTER";
4877 break;
4878 case DECL_PROTECTED:
4879 attr = "PROTECTED";
4880 break;
4881 case DECL_PRIVATE:
4882 attr = "PRIVATE";
4883 break;
4884 case DECL_PUBLIC:
4885 attr = "PUBLIC";
4886 break;
4887 case DECL_SAVE:
4888 attr = "SAVE";
4889 break;
4890 case DECL_STATIC:
4891 attr = "STATIC";
4892 break;
4893 case DECL_AUTOMATIC:
4894 attr = "AUTOMATIC";
4895 break;
4896 case DECL_TARGET:
4897 attr = "TARGET";
4898 break;
4899 case DECL_IS_BIND_C:
4900 attr = "IS_BIND_C";
4901 break;
4902 case DECL_VALUE:
4903 attr = "VALUE";
4904 break;
4905 case DECL_VOLATILE:
4906 attr = "VOLATILE";
4907 break;
4908 default:
4909 attr = NULL; /* This shouldn't happen. */
4912 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
4913 m = MATCH_ERROR;
4914 goto cleanup;
4917 /* Now that we've dealt with duplicate attributes, add the attributes
4918 to the current attribute. */
4919 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4921 if (seen[d] == 0)
4922 continue;
4923 else
4924 attr_seen = 1;
4926 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
4927 && !flag_dec_static)
4929 gfc_error ("%s at %L is a DEC extension, enable with "
4930 "%<-fdec-static%>",
4931 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
4932 m = MATCH_ERROR;
4933 goto cleanup;
4935 /* Allow SAVE with STATIC, but don't complain. */
4936 if (d == DECL_STATIC && seen[DECL_SAVE])
4937 continue;
4939 if (gfc_current_state () == COMP_DERIVED
4940 && d != DECL_DIMENSION && d != DECL_CODIMENSION
4941 && d != DECL_POINTER && d != DECL_PRIVATE
4942 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
4944 if (d == DECL_ALLOCATABLE)
4946 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
4947 "attribute at %C in a TYPE definition"))
4949 m = MATCH_ERROR;
4950 goto cleanup;
4953 else if (d == DECL_KIND)
4955 if (!gfc_notify_std (GFC_STD_F2003, "KIND "
4956 "attribute at %C in a TYPE definition"))
4958 m = MATCH_ERROR;
4959 goto cleanup;
4961 if (current_ts.type != BT_INTEGER)
4963 gfc_error ("Component with KIND attribute at %C must be "
4964 "INTEGER");
4965 m = MATCH_ERROR;
4966 goto cleanup;
4968 if (current_ts.kind != gfc_default_integer_kind)
4970 gfc_error ("Component with KIND attribute at %C must be "
4971 "default integer kind (%d)",
4972 gfc_default_integer_kind);
4973 m = MATCH_ERROR;
4974 goto cleanup;
4977 else if (d == DECL_LEN)
4979 if (!gfc_notify_std (GFC_STD_F2003, "LEN "
4980 "attribute at %C in a TYPE definition"))
4982 m = MATCH_ERROR;
4983 goto cleanup;
4985 if (current_ts.type != BT_INTEGER)
4987 gfc_error ("Component with LEN attribute at %C must be "
4988 "INTEGER");
4989 m = MATCH_ERROR;
4990 goto cleanup;
4992 if (current_ts.kind != gfc_default_integer_kind)
4994 gfc_error ("Component with LEN attribute at %C must be "
4995 "default integer kind (%d)",
4996 gfc_default_integer_kind);
4997 m = MATCH_ERROR;
4998 goto cleanup;
5001 else
5003 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
5004 &seen_at[d]);
5005 m = MATCH_ERROR;
5006 goto cleanup;
5010 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5011 && gfc_current_state () != COMP_MODULE)
5013 if (d == DECL_PRIVATE)
5014 attr = "PRIVATE";
5015 else
5016 attr = "PUBLIC";
5017 if (gfc_current_state () == COMP_DERIVED
5018 && gfc_state_stack->previous
5019 && gfc_state_stack->previous->state == COMP_MODULE)
5021 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5022 "at %L in a TYPE definition", attr,
5023 &seen_at[d]))
5025 m = MATCH_ERROR;
5026 goto cleanup;
5029 else
5031 gfc_error ("%s attribute at %L is not allowed outside of the "
5032 "specification part of a module", attr, &seen_at[d]);
5033 m = MATCH_ERROR;
5034 goto cleanup;
5038 if (gfc_current_state () != COMP_DERIVED
5039 && (d == DECL_KIND || d == DECL_LEN))
5041 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5042 "definition", &seen_at[d]);
5043 m = MATCH_ERROR;
5044 goto cleanup;
5047 switch (d)
5049 case DECL_ALLOCATABLE:
5050 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5051 break;
5053 case DECL_ASYNCHRONOUS:
5054 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5055 t = false;
5056 else
5057 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5058 break;
5060 case DECL_CODIMENSION:
5061 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5062 break;
5064 case DECL_CONTIGUOUS:
5065 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5066 t = false;
5067 else
5068 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5069 break;
5071 case DECL_DIMENSION:
5072 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5073 break;
5075 case DECL_EXTERNAL:
5076 t = gfc_add_external (&current_attr, &seen_at[d]);
5077 break;
5079 case DECL_IN:
5080 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5081 break;
5083 case DECL_OUT:
5084 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5085 break;
5087 case DECL_INOUT:
5088 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5089 break;
5091 case DECL_INTRINSIC:
5092 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5093 break;
5095 case DECL_OPTIONAL:
5096 t = gfc_add_optional (&current_attr, &seen_at[d]);
5097 break;
5099 case DECL_KIND:
5100 t = gfc_add_kind (&current_attr, &seen_at[d]);
5101 break;
5103 case DECL_LEN:
5104 t = gfc_add_len (&current_attr, &seen_at[d]);
5105 break;
5107 case DECL_PARAMETER:
5108 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5109 break;
5111 case DECL_POINTER:
5112 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5113 break;
5115 case DECL_PROTECTED:
5116 if (gfc_current_state () != COMP_MODULE
5117 || (gfc_current_ns->proc_name
5118 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5120 gfc_error ("PROTECTED at %C only allowed in specification "
5121 "part of a module");
5122 t = false;
5123 break;
5126 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5127 t = false;
5128 else
5129 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5130 break;
5132 case DECL_PRIVATE:
5133 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5134 &seen_at[d]);
5135 break;
5137 case DECL_PUBLIC:
5138 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5139 &seen_at[d]);
5140 break;
5142 case DECL_STATIC:
5143 case DECL_SAVE:
5144 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5145 break;
5147 case DECL_AUTOMATIC:
5148 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5149 break;
5151 case DECL_TARGET:
5152 t = gfc_add_target (&current_attr, &seen_at[d]);
5153 break;
5155 case DECL_IS_BIND_C:
5156 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5157 break;
5159 case DECL_VALUE:
5160 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5161 t = false;
5162 else
5163 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5164 break;
5166 case DECL_VOLATILE:
5167 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5168 t = false;
5169 else
5170 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5171 break;
5173 default:
5174 gfc_internal_error ("match_attr_spec(): Bad attribute");
5177 if (!t)
5179 m = MATCH_ERROR;
5180 goto cleanup;
5184 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
5185 if ((gfc_current_state () == COMP_MODULE
5186 || gfc_current_state () == COMP_SUBMODULE)
5187 && !current_attr.save
5188 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5189 current_attr.save = SAVE_IMPLICIT;
5191 colon_seen = 1;
5192 return MATCH_YES;
5194 cleanup:
5195 gfc_current_locus = start;
5196 gfc_free_array_spec (current_as);
5197 current_as = NULL;
5198 attr_seen = 0;
5199 return m;
5203 /* Set the binding label, dest_label, either with the binding label
5204 stored in the given gfc_typespec, ts, or if none was provided, it
5205 will be the symbol name in all lower case, as required by the draft
5206 (J3/04-007, section 15.4.1). If a binding label was given and
5207 there is more than one argument (num_idents), it is an error. */
5209 static bool
5210 set_binding_label (const char **dest_label, const char *sym_name,
5211 int num_idents)
5213 if (num_idents > 1 && has_name_equals)
5215 gfc_error ("Multiple identifiers provided with "
5216 "single NAME= specifier at %C");
5217 return false;
5220 if (curr_binding_label)
5221 /* Binding label given; store in temp holder till have sym. */
5222 *dest_label = curr_binding_label;
5223 else
5225 /* No binding label given, and the NAME= specifier did not exist,
5226 which means there was no NAME="". */
5227 if (sym_name != NULL && has_name_equals == 0)
5228 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5231 return true;
5235 /* Set the status of the given common block as being BIND(C) or not,
5236 depending on the given parameter, is_bind_c. */
5238 void
5239 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5241 com_block->is_bind_c = is_bind_c;
5242 return;
5246 /* Verify that the given gfc_typespec is for a C interoperable type. */
5248 bool
5249 gfc_verify_c_interop (gfc_typespec *ts)
5251 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5252 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5253 ? true : false;
5254 else if (ts->type == BT_CLASS)
5255 return false;
5256 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5257 return false;
5259 return true;
5263 /* Verify that the variables of a given common block, which has been
5264 defined with the attribute specifier bind(c), to be of a C
5265 interoperable type. Errors will be reported here, if
5266 encountered. */
5268 bool
5269 verify_com_block_vars_c_interop (gfc_common_head *com_block)
5271 gfc_symbol *curr_sym = NULL;
5272 bool retval = true;
5274 curr_sym = com_block->head;
5276 /* Make sure we have at least one symbol. */
5277 if (curr_sym == NULL)
5278 return retval;
5280 /* Here we know we have a symbol, so we'll execute this loop
5281 at least once. */
5284 /* The second to last param, 1, says this is in a common block. */
5285 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5286 curr_sym = curr_sym->common_next;
5287 } while (curr_sym != NULL);
5289 return retval;
5293 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5294 an appropriate error message is reported. */
5296 bool
5297 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5298 int is_in_common, gfc_common_head *com_block)
5300 bool bind_c_function = false;
5301 bool retval = true;
5303 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5304 bind_c_function = true;
5306 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5308 tmp_sym = tmp_sym->result;
5309 /* Make sure it wasn't an implicitly typed result. */
5310 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5312 gfc_warning (OPT_Wc_binding_type,
5313 "Implicitly declared BIND(C) function %qs at "
5314 "%L may not be C interoperable", tmp_sym->name,
5315 &tmp_sym->declared_at);
5316 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5317 /* Mark it as C interoperable to prevent duplicate warnings. */
5318 tmp_sym->ts.is_c_interop = 1;
5319 tmp_sym->attr.is_c_interop = 1;
5323 /* Here, we know we have the bind(c) attribute, so if we have
5324 enough type info, then verify that it's a C interop kind.
5325 The info could be in the symbol already, or possibly still in
5326 the given ts (current_ts), so look in both. */
5327 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5329 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5331 /* See if we're dealing with a sym in a common block or not. */
5332 if (is_in_common == 1 && warn_c_binding_type)
5334 gfc_warning (OPT_Wc_binding_type,
5335 "Variable %qs in common block %qs at %L "
5336 "may not be a C interoperable "
5337 "kind though common block %qs is BIND(C)",
5338 tmp_sym->name, com_block->name,
5339 &(tmp_sym->declared_at), com_block->name);
5341 else
5343 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5344 gfc_error ("Type declaration %qs at %L is not C "
5345 "interoperable but it is BIND(C)",
5346 tmp_sym->name, &(tmp_sym->declared_at));
5347 else if (warn_c_binding_type)
5348 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5349 "may not be a C interoperable "
5350 "kind but it is BIND(C)",
5351 tmp_sym->name, &(tmp_sym->declared_at));
5355 /* Variables declared w/in a common block can't be bind(c)
5356 since there's no way for C to see these variables, so there's
5357 semantically no reason for the attribute. */
5358 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5360 gfc_error ("Variable %qs in common block %qs at "
5361 "%L cannot be declared with BIND(C) "
5362 "since it is not a global",
5363 tmp_sym->name, com_block->name,
5364 &(tmp_sym->declared_at));
5365 retval = false;
5368 /* Scalar variables that are bind(c) can not have the pointer
5369 or allocatable attributes. */
5370 if (tmp_sym->attr.is_bind_c == 1)
5372 if (tmp_sym->attr.pointer == 1)
5374 gfc_error ("Variable %qs at %L cannot have both the "
5375 "POINTER and BIND(C) attributes",
5376 tmp_sym->name, &(tmp_sym->declared_at));
5377 retval = false;
5380 if (tmp_sym->attr.allocatable == 1)
5382 gfc_error ("Variable %qs at %L cannot have both the "
5383 "ALLOCATABLE and BIND(C) attributes",
5384 tmp_sym->name, &(tmp_sym->declared_at));
5385 retval = false;
5390 /* If it is a BIND(C) function, make sure the return value is a
5391 scalar value. The previous tests in this function made sure
5392 the type is interoperable. */
5393 if (bind_c_function && tmp_sym->as != NULL)
5394 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5395 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5397 /* BIND(C) functions can not return a character string. */
5398 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5399 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5400 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5401 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5402 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5403 "be a character string", tmp_sym->name,
5404 &(tmp_sym->declared_at));
5407 /* See if the symbol has been marked as private. If it has, make sure
5408 there is no binding label and warn the user if there is one. */
5409 if (tmp_sym->attr.access == ACCESS_PRIVATE
5410 && tmp_sym->binding_label)
5411 /* Use gfc_warning_now because we won't say that the symbol fails
5412 just because of this. */
5413 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5414 "given the binding label %qs", tmp_sym->name,
5415 &(tmp_sym->declared_at), tmp_sym->binding_label);
5417 return retval;
5421 /* Set the appropriate fields for a symbol that's been declared as
5422 BIND(C) (the is_bind_c flag and the binding label), and verify that
5423 the type is C interoperable. Errors are reported by the functions
5424 used to set/test these fields. */
5426 bool
5427 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5429 bool retval = true;
5431 /* TODO: Do we need to make sure the vars aren't marked private? */
5433 /* Set the is_bind_c bit in symbol_attribute. */
5434 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5436 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5437 return false;
5439 return retval;
5443 /* Set the fields marking the given common block as BIND(C), including
5444 a binding label, and report any errors encountered. */
5446 bool
5447 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5449 bool retval = true;
5451 /* destLabel, common name, typespec (which may have binding label). */
5452 if (!set_binding_label (&com_block->binding_label, com_block->name,
5453 num_idents))
5454 return false;
5456 /* Set the given common block (com_block) to being bind(c) (1). */
5457 set_com_block_bind_c (com_block, 1);
5459 return retval;
5463 /* Retrieve the list of one or more identifiers that the given bind(c)
5464 attribute applies to. */
5466 bool
5467 get_bind_c_idents (void)
5469 char name[GFC_MAX_SYMBOL_LEN + 1];
5470 int num_idents = 0;
5471 gfc_symbol *tmp_sym = NULL;
5472 match found_id;
5473 gfc_common_head *com_block = NULL;
5475 if (gfc_match_name (name) == MATCH_YES)
5477 found_id = MATCH_YES;
5478 gfc_get_ha_symbol (name, &tmp_sym);
5480 else if (match_common_name (name) == MATCH_YES)
5482 found_id = MATCH_YES;
5483 com_block = gfc_get_common (name, 0);
5485 else
5487 gfc_error ("Need either entity or common block name for "
5488 "attribute specification statement at %C");
5489 return false;
5492 /* Save the current identifier and look for more. */
5495 /* Increment the number of identifiers found for this spec stmt. */
5496 num_idents++;
5498 /* Make sure we have a sym or com block, and verify that it can
5499 be bind(c). Set the appropriate field(s) and look for more
5500 identifiers. */
5501 if (tmp_sym != NULL || com_block != NULL)
5503 if (tmp_sym != NULL)
5505 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
5506 return false;
5508 else
5510 if (!set_verify_bind_c_com_block (com_block, num_idents))
5511 return false;
5514 /* Look to see if we have another identifier. */
5515 tmp_sym = NULL;
5516 if (gfc_match_eos () == MATCH_YES)
5517 found_id = MATCH_NO;
5518 else if (gfc_match_char (',') != MATCH_YES)
5519 found_id = MATCH_NO;
5520 else if (gfc_match_name (name) == MATCH_YES)
5522 found_id = MATCH_YES;
5523 gfc_get_ha_symbol (name, &tmp_sym);
5525 else if (match_common_name (name) == MATCH_YES)
5527 found_id = MATCH_YES;
5528 com_block = gfc_get_common (name, 0);
5530 else
5532 gfc_error ("Missing entity or common block name for "
5533 "attribute specification statement at %C");
5534 return false;
5537 else
5539 gfc_internal_error ("Missing symbol");
5541 } while (found_id == MATCH_YES);
5543 /* if we get here we were successful */
5544 return true;
5548 /* Try and match a BIND(C) attribute specification statement. */
5550 match
5551 gfc_match_bind_c_stmt (void)
5553 match found_match = MATCH_NO;
5554 gfc_typespec *ts;
5556 ts = &current_ts;
5558 /* This may not be necessary. */
5559 gfc_clear_ts (ts);
5560 /* Clear the temporary binding label holder. */
5561 curr_binding_label = NULL;
5563 /* Look for the bind(c). */
5564 found_match = gfc_match_bind_c (NULL, true);
5566 if (found_match == MATCH_YES)
5568 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
5569 return MATCH_ERROR;
5571 /* Look for the :: now, but it is not required. */
5572 gfc_match (" :: ");
5574 /* Get the identifier(s) that needs to be updated. This may need to
5575 change to hand the flag(s) for the attr specified so all identifiers
5576 found can have all appropriate parts updated (assuming that the same
5577 spec stmt can have multiple attrs, such as both bind(c) and
5578 allocatable...). */
5579 if (!get_bind_c_idents ())
5580 /* Error message should have printed already. */
5581 return MATCH_ERROR;
5584 return found_match;
5588 /* Match a data declaration statement. */
5590 match
5591 gfc_match_data_decl (void)
5593 gfc_symbol *sym;
5594 match m;
5595 int elem;
5597 type_param_spec_list = NULL;
5598 decl_type_param_list = NULL;
5600 num_idents_on_line = 0;
5602 m = gfc_match_decl_type_spec (&current_ts, 0);
5603 if (m != MATCH_YES)
5604 return m;
5606 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5607 && !gfc_comp_struct (gfc_current_state ()))
5609 sym = gfc_use_derived (current_ts.u.derived);
5611 if (sym == NULL)
5613 m = MATCH_ERROR;
5614 goto cleanup;
5617 current_ts.u.derived = sym;
5620 m = match_attr_spec ();
5621 if (m == MATCH_ERROR)
5623 m = MATCH_NO;
5624 goto cleanup;
5627 if (current_ts.type == BT_CLASS
5628 && current_ts.u.derived->attr.unlimited_polymorphic)
5629 goto ok;
5631 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5632 && current_ts.u.derived->components == NULL
5633 && !current_ts.u.derived->attr.zero_comp)
5636 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
5637 goto ok;
5639 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED
5640 && current_ts.u.derived == gfc_current_block ())
5641 goto ok;
5643 gfc_find_symbol (current_ts.u.derived->name,
5644 current_ts.u.derived->ns, 1, &sym);
5646 /* Any symbol that we find had better be a type definition
5647 which has its components defined, or be a structure definition
5648 actively being parsed. */
5649 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
5650 && (current_ts.u.derived->components != NULL
5651 || current_ts.u.derived->attr.zero_comp
5652 || current_ts.u.derived == gfc_new_block))
5653 goto ok;
5655 gfc_error ("Derived type at %C has not been previously defined "
5656 "and so cannot appear in a derived type definition");
5657 m = MATCH_ERROR;
5658 goto cleanup;
5662 /* If we have an old-style character declaration, and no new-style
5663 attribute specifications, then there a comma is optional between
5664 the type specification and the variable list. */
5665 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
5666 gfc_match_char (',');
5668 /* Give the types/attributes to symbols that follow. Give the element
5669 a number so that repeat character length expressions can be copied. */
5670 elem = 1;
5671 for (;;)
5673 num_idents_on_line++;
5674 m = variable_decl (elem++);
5675 if (m == MATCH_ERROR)
5676 goto cleanup;
5677 if (m == MATCH_NO)
5678 break;
5680 if (gfc_match_eos () == MATCH_YES)
5681 goto cleanup;
5682 if (gfc_match_char (',') != MATCH_YES)
5683 break;
5686 if (!gfc_error_flag_test ())
5688 /* An anonymous structure declaration is unambiguous; if we matched one
5689 according to gfc_match_structure_decl, we need to return MATCH_YES
5690 here to avoid confusing the remaining matchers, even if there was an
5691 error during variable_decl. We must flush any such errors. Note this
5692 causes the parser to gracefully continue parsing the remaining input
5693 as a structure body, which likely follows. */
5694 if (current_ts.type == BT_DERIVED && current_ts.u.derived
5695 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
5697 gfc_error_now ("Syntax error in anonymous structure declaration"
5698 " at %C");
5699 /* Skip the bad variable_decl and line up for the start of the
5700 structure body. */
5701 gfc_error_recovery ();
5702 m = MATCH_YES;
5703 goto cleanup;
5706 gfc_error ("Syntax error in data declaration at %C");
5709 m = MATCH_ERROR;
5711 gfc_free_data_all (gfc_current_ns);
5713 cleanup:
5714 if (saved_kind_expr)
5715 gfc_free_expr (saved_kind_expr);
5716 if (type_param_spec_list)
5717 gfc_free_actual_arglist (type_param_spec_list);
5718 if (decl_type_param_list)
5719 gfc_free_actual_arglist (decl_type_param_list);
5720 saved_kind_expr = NULL;
5721 gfc_free_array_spec (current_as);
5722 current_as = NULL;
5723 return m;
5727 /* Match a prefix associated with a function or subroutine
5728 declaration. If the typespec pointer is nonnull, then a typespec
5729 can be matched. Note that if nothing matches, MATCH_YES is
5730 returned (the null string was matched). */
5732 match
5733 gfc_match_prefix (gfc_typespec *ts)
5735 bool seen_type;
5736 bool seen_impure;
5737 bool found_prefix;
5739 gfc_clear_attr (&current_attr);
5740 seen_type = false;
5741 seen_impure = false;
5743 gcc_assert (!gfc_matching_prefix);
5744 gfc_matching_prefix = true;
5748 found_prefix = false;
5750 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
5751 corresponding attribute seems natural and distinguishes these
5752 procedures from procedure types of PROC_MODULE, which these are
5753 as well. */
5754 if (gfc_match ("module% ") == MATCH_YES)
5756 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
5757 goto error;
5759 current_attr.module_procedure = 1;
5760 found_prefix = true;
5763 if (!seen_type && ts != NULL
5764 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
5765 && gfc_match_space () == MATCH_YES)
5768 seen_type = true;
5769 found_prefix = true;
5772 if (gfc_match ("elemental% ") == MATCH_YES)
5774 if (!gfc_add_elemental (&current_attr, NULL))
5775 goto error;
5777 found_prefix = true;
5780 if (gfc_match ("pure% ") == MATCH_YES)
5782 if (!gfc_add_pure (&current_attr, NULL))
5783 goto error;
5785 found_prefix = true;
5788 if (gfc_match ("recursive% ") == MATCH_YES)
5790 if (!gfc_add_recursive (&current_attr, NULL))
5791 goto error;
5793 found_prefix = true;
5796 /* IMPURE is a somewhat special case, as it needs not set an actual
5797 attribute but rather only prevents ELEMENTAL routines from being
5798 automatically PURE. */
5799 if (gfc_match ("impure% ") == MATCH_YES)
5801 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
5802 goto error;
5804 seen_impure = true;
5805 found_prefix = true;
5808 while (found_prefix);
5810 /* IMPURE and PURE must not both appear, of course. */
5811 if (seen_impure && current_attr.pure)
5813 gfc_error ("PURE and IMPURE must not appear both at %C");
5814 goto error;
5817 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
5818 if (!seen_impure && current_attr.elemental && !current_attr.pure)
5820 if (!gfc_add_pure (&current_attr, NULL))
5821 goto error;
5824 /* At this point, the next item is not a prefix. */
5825 gcc_assert (gfc_matching_prefix);
5827 gfc_matching_prefix = false;
5828 return MATCH_YES;
5830 error:
5831 gcc_assert (gfc_matching_prefix);
5832 gfc_matching_prefix = false;
5833 return MATCH_ERROR;
5837 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
5839 static bool
5840 copy_prefix (symbol_attribute *dest, locus *where)
5842 if (dest->module_procedure)
5844 if (current_attr.elemental)
5845 dest->elemental = 1;
5847 if (current_attr.pure)
5848 dest->pure = 1;
5850 if (current_attr.recursive)
5851 dest->recursive = 1;
5853 /* Module procedures are unusual in that the 'dest' is copied from
5854 the interface declaration. However, this is an oportunity to
5855 check that the submodule declaration is compliant with the
5856 interface. */
5857 if (dest->elemental && !current_attr.elemental)
5859 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
5860 "missing at %L", where);
5861 return false;
5864 if (dest->pure && !current_attr.pure)
5866 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
5867 "missing at %L", where);
5868 return false;
5871 if (dest->recursive && !current_attr.recursive)
5873 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
5874 "missing at %L", where);
5875 return false;
5878 return true;
5881 if (current_attr.elemental && !gfc_add_elemental (dest, where))
5882 return false;
5884 if (current_attr.pure && !gfc_add_pure (dest, where))
5885 return false;
5887 if (current_attr.recursive && !gfc_add_recursive (dest, where))
5888 return false;
5890 return true;
5894 /* Match a formal argument list or, if typeparam is true, a
5895 type_param_name_list. */
5897 match
5898 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
5899 int null_flag, bool typeparam)
5901 gfc_formal_arglist *head, *tail, *p, *q;
5902 char name[GFC_MAX_SYMBOL_LEN + 1];
5903 gfc_symbol *sym;
5904 match m;
5905 gfc_formal_arglist *formal = NULL;
5907 head = tail = NULL;
5909 /* Keep the interface formal argument list and null it so that the
5910 matching for the new declaration can be done. The numbers and
5911 names of the arguments are checked here. The interface formal
5912 arguments are retained in formal_arglist and the characteristics
5913 are compared in resolve.c(resolve_fl_procedure). See the remark
5914 in get_proc_name about the eventual need to copy the formal_arglist
5915 and populate the formal namespace of the interface symbol. */
5916 if (progname->attr.module_procedure
5917 && progname->attr.host_assoc)
5919 formal = progname->formal;
5920 progname->formal = NULL;
5923 if (gfc_match_char ('(') != MATCH_YES)
5925 if (null_flag)
5926 goto ok;
5927 return MATCH_NO;
5930 if (gfc_match_char (')') == MATCH_YES)
5931 goto ok;
5933 for (;;)
5935 if (gfc_match_char ('*') == MATCH_YES)
5937 sym = NULL;
5938 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
5939 "at %C"))
5941 m = MATCH_ERROR;
5942 goto cleanup;
5945 else
5947 m = gfc_match_name (name);
5948 if (m != MATCH_YES)
5949 goto cleanup;
5951 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
5952 goto cleanup;
5953 else if (typeparam
5954 && gfc_get_symbol (name, progname->f2k_derived, &sym))
5955 goto cleanup;
5958 p = gfc_get_formal_arglist ();
5960 if (head == NULL)
5961 head = tail = p;
5962 else
5964 tail->next = p;
5965 tail = p;
5968 tail->sym = sym;
5970 /* We don't add the VARIABLE flavor because the name could be a
5971 dummy procedure. We don't apply these attributes to formal
5972 arguments of statement functions. */
5973 if (sym != NULL && !st_flag
5974 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
5975 || !gfc_missing_attr (&sym->attr, NULL)))
5977 m = MATCH_ERROR;
5978 goto cleanup;
5981 /* The name of a program unit can be in a different namespace,
5982 so check for it explicitly. After the statement is accepted,
5983 the name is checked for especially in gfc_get_symbol(). */
5984 if (gfc_new_block != NULL && sym != NULL && !typeparam
5985 && strcmp (sym->name, gfc_new_block->name) == 0)
5987 gfc_error ("Name %qs at %C is the name of the procedure",
5988 sym->name);
5989 m = MATCH_ERROR;
5990 goto cleanup;
5993 if (gfc_match_char (')') == MATCH_YES)
5994 goto ok;
5996 m = gfc_match_char (',');
5997 if (m != MATCH_YES)
5999 if (typeparam)
6000 gfc_error_now ("Expected parameter list in type declaration "
6001 "at %C");
6002 else
6003 gfc_error ("Unexpected junk in formal argument list at %C");
6004 goto cleanup;
6009 /* Check for duplicate symbols in the formal argument list. */
6010 if (head != NULL)
6012 for (p = head; p->next; p = p->next)
6014 if (p->sym == NULL)
6015 continue;
6017 for (q = p->next; q; q = q->next)
6018 if (p->sym == q->sym)
6020 if (typeparam)
6021 gfc_error_now ("Duplicate name %qs in parameter "
6022 "list at %C", p->sym->name);
6023 else
6024 gfc_error ("Duplicate symbol %qs in formal argument "
6025 "list at %C", p->sym->name);
6027 m = MATCH_ERROR;
6028 goto cleanup;
6033 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6035 m = MATCH_ERROR;
6036 goto cleanup;
6039 /* gfc_error_now used in following and return with MATCH_YES because
6040 doing otherwise results in a cascade of extraneous errors and in
6041 some cases an ICE in symbol.c(gfc_release_symbol). */
6042 if (progname->attr.module_procedure && progname->attr.host_assoc)
6044 bool arg_count_mismatch = false;
6046 if (!formal && head)
6047 arg_count_mismatch = true;
6049 /* Abbreviated module procedure declaration is not meant to have any
6050 formal arguments! */
6051 if (!progname->abr_modproc_decl && formal && !head)
6052 arg_count_mismatch = true;
6054 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6056 if ((p->next != NULL && q->next == NULL)
6057 || (p->next == NULL && q->next != NULL))
6058 arg_count_mismatch = true;
6059 else if ((p->sym == NULL && q->sym == NULL)
6060 || strcmp (p->sym->name, q->sym->name) == 0)
6061 continue;
6062 else
6063 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6064 "argument names (%s/%s) at %C",
6065 p->sym->name, q->sym->name);
6068 if (arg_count_mismatch)
6069 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6070 "formal arguments at %C");
6073 return MATCH_YES;
6075 cleanup:
6076 gfc_free_formal_arglist (head);
6077 return m;
6081 /* Match a RESULT specification following a function declaration or
6082 ENTRY statement. Also matches the end-of-statement. */
6084 static match
6085 match_result (gfc_symbol *function, gfc_symbol **result)
6087 char name[GFC_MAX_SYMBOL_LEN + 1];
6088 gfc_symbol *r;
6089 match m;
6091 if (gfc_match (" result (") != MATCH_YES)
6092 return MATCH_NO;
6094 m = gfc_match_name (name);
6095 if (m != MATCH_YES)
6096 return m;
6098 /* Get the right paren, and that's it because there could be the
6099 bind(c) attribute after the result clause. */
6100 if (gfc_match_char (')') != MATCH_YES)
6102 /* TODO: should report the missing right paren here. */
6103 return MATCH_ERROR;
6106 if (strcmp (function->name, name) == 0)
6108 gfc_error ("RESULT variable at %C must be different than function name");
6109 return MATCH_ERROR;
6112 if (gfc_get_symbol (name, NULL, &r))
6113 return MATCH_ERROR;
6115 if (!gfc_add_result (&r->attr, r->name, NULL))
6116 return MATCH_ERROR;
6118 *result = r;
6120 return MATCH_YES;
6124 /* Match a function suffix, which could be a combination of a result
6125 clause and BIND(C), either one, or neither. The draft does not
6126 require them to come in a specific order. */
6128 match
6129 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6131 match is_bind_c; /* Found bind(c). */
6132 match is_result; /* Found result clause. */
6133 match found_match; /* Status of whether we've found a good match. */
6134 char peek_char; /* Character we're going to peek at. */
6135 bool allow_binding_name;
6137 /* Initialize to having found nothing. */
6138 found_match = MATCH_NO;
6139 is_bind_c = MATCH_NO;
6140 is_result = MATCH_NO;
6142 /* Get the next char to narrow between result and bind(c). */
6143 gfc_gobble_whitespace ();
6144 peek_char = gfc_peek_ascii_char ();
6146 /* C binding names are not allowed for internal procedures. */
6147 if (gfc_current_state () == COMP_CONTAINS
6148 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6149 allow_binding_name = false;
6150 else
6151 allow_binding_name = true;
6153 switch (peek_char)
6155 case 'r':
6156 /* Look for result clause. */
6157 is_result = match_result (sym, result);
6158 if (is_result == MATCH_YES)
6160 /* Now see if there is a bind(c) after it. */
6161 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6162 /* We've found the result clause and possibly bind(c). */
6163 found_match = MATCH_YES;
6165 else
6166 /* This should only be MATCH_ERROR. */
6167 found_match = is_result;
6168 break;
6169 case 'b':
6170 /* Look for bind(c) first. */
6171 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6172 if (is_bind_c == MATCH_YES)
6174 /* Now see if a result clause followed it. */
6175 is_result = match_result (sym, result);
6176 found_match = MATCH_YES;
6178 else
6180 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6181 found_match = MATCH_ERROR;
6183 break;
6184 default:
6185 gfc_error ("Unexpected junk after function declaration at %C");
6186 found_match = MATCH_ERROR;
6187 break;
6190 if (is_bind_c == MATCH_YES)
6192 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
6193 if (gfc_current_state () == COMP_CONTAINS
6194 && sym->ns->proc_name->attr.flavor != FL_MODULE
6195 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6196 "at %L may not be specified for an internal "
6197 "procedure", &gfc_current_locus))
6198 return MATCH_ERROR;
6200 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6201 return MATCH_ERROR;
6204 return found_match;
6208 /* Procedure pointer return value without RESULT statement:
6209 Add "hidden" result variable named "ppr@". */
6211 static bool
6212 add_hidden_procptr_result (gfc_symbol *sym)
6214 bool case1,case2;
6216 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6217 return false;
6219 /* First usage case: PROCEDURE and EXTERNAL statements. */
6220 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6221 && strcmp (gfc_current_block ()->name, sym->name) == 0
6222 && sym->attr.external;
6223 /* Second usage case: INTERFACE statements. */
6224 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6225 && gfc_state_stack->previous->state == COMP_FUNCTION
6226 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6228 if (case1 || case2)
6230 gfc_symtree *stree;
6231 if (case1)
6232 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6233 else if (case2)
6235 gfc_symtree *st2;
6236 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6237 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6238 st2->n.sym = stree->n.sym;
6239 stree->n.sym->refs++;
6241 sym->result = stree->n.sym;
6243 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6244 sym->result->attr.pointer = sym->attr.pointer;
6245 sym->result->attr.external = sym->attr.external;
6246 sym->result->attr.referenced = sym->attr.referenced;
6247 sym->result->ts = sym->ts;
6248 sym->attr.proc_pointer = 0;
6249 sym->attr.pointer = 0;
6250 sym->attr.external = 0;
6251 if (sym->result->attr.external && sym->result->attr.pointer)
6253 sym->result->attr.pointer = 0;
6254 sym->result->attr.proc_pointer = 1;
6257 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6259 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6260 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6261 && sym->result && sym->result != sym && sym->result->attr.external
6262 && sym == gfc_current_ns->proc_name
6263 && sym == sym->result->ns->proc_name
6264 && strcmp ("ppr@", sym->result->name) == 0)
6266 sym->result->attr.proc_pointer = 1;
6267 sym->attr.pointer = 0;
6268 return true;
6270 else
6271 return false;
6275 /* Match the interface for a PROCEDURE declaration,
6276 including brackets (R1212). */
6278 static match
6279 match_procedure_interface (gfc_symbol **proc_if)
6281 match m;
6282 gfc_symtree *st;
6283 locus old_loc, entry_loc;
6284 gfc_namespace *old_ns = gfc_current_ns;
6285 char name[GFC_MAX_SYMBOL_LEN + 1];
6287 old_loc = entry_loc = gfc_current_locus;
6288 gfc_clear_ts (&current_ts);
6290 if (gfc_match (" (") != MATCH_YES)
6292 gfc_current_locus = entry_loc;
6293 return MATCH_NO;
6296 /* Get the type spec. for the procedure interface. */
6297 old_loc = gfc_current_locus;
6298 m = gfc_match_decl_type_spec (&current_ts, 0);
6299 gfc_gobble_whitespace ();
6300 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6301 goto got_ts;
6303 if (m == MATCH_ERROR)
6304 return m;
6306 /* Procedure interface is itself a procedure. */
6307 gfc_current_locus = old_loc;
6308 m = gfc_match_name (name);
6310 /* First look to see if it is already accessible in the current
6311 namespace because it is use associated or contained. */
6312 st = NULL;
6313 if (gfc_find_sym_tree (name, NULL, 0, &st))
6314 return MATCH_ERROR;
6316 /* If it is still not found, then try the parent namespace, if it
6317 exists and create the symbol there if it is still not found. */
6318 if (gfc_current_ns->parent)
6319 gfc_current_ns = gfc_current_ns->parent;
6320 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6321 return MATCH_ERROR;
6323 gfc_current_ns = old_ns;
6324 *proc_if = st->n.sym;
6326 if (*proc_if)
6328 (*proc_if)->refs++;
6329 /* Resolve interface if possible. That way, attr.procedure is only set
6330 if it is declared by a later procedure-declaration-stmt, which is
6331 invalid per F08:C1216 (cf. resolve_procedure_interface). */
6332 while ((*proc_if)->ts.interface
6333 && *proc_if != (*proc_if)->ts.interface)
6334 *proc_if = (*proc_if)->ts.interface;
6336 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6337 && (*proc_if)->ts.type == BT_UNKNOWN
6338 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6339 (*proc_if)->name, NULL))
6340 return MATCH_ERROR;
6343 got_ts:
6344 if (gfc_match (" )") != MATCH_YES)
6346 gfc_current_locus = entry_loc;
6347 return MATCH_NO;
6350 return MATCH_YES;
6354 /* Match a PROCEDURE declaration (R1211). */
6356 static match
6357 match_procedure_decl (void)
6359 match m;
6360 gfc_symbol *sym, *proc_if = NULL;
6361 int num;
6362 gfc_expr *initializer = NULL;
6364 /* Parse interface (with brackets). */
6365 m = match_procedure_interface (&proc_if);
6366 if (m != MATCH_YES)
6367 return m;
6369 /* Parse attributes (with colons). */
6370 m = match_attr_spec();
6371 if (m == MATCH_ERROR)
6372 return MATCH_ERROR;
6374 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6376 current_attr.is_bind_c = 1;
6377 has_name_equals = 0;
6378 curr_binding_label = NULL;
6381 /* Get procedure symbols. */
6382 for(num=1;;num++)
6384 m = gfc_match_symbol (&sym, 0);
6385 if (m == MATCH_NO)
6386 goto syntax;
6387 else if (m == MATCH_ERROR)
6388 return m;
6390 /* Add current_attr to the symbol attributes. */
6391 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
6392 return MATCH_ERROR;
6394 if (sym->attr.is_bind_c)
6396 /* Check for C1218. */
6397 if (!proc_if || !proc_if->attr.is_bind_c)
6399 gfc_error ("BIND(C) attribute at %C requires "
6400 "an interface with BIND(C)");
6401 return MATCH_ERROR;
6403 /* Check for C1217. */
6404 if (has_name_equals && sym->attr.pointer)
6406 gfc_error ("BIND(C) procedure with NAME may not have "
6407 "POINTER attribute at %C");
6408 return MATCH_ERROR;
6410 if (has_name_equals && sym->attr.dummy)
6412 gfc_error ("Dummy procedure at %C may not have "
6413 "BIND(C) attribute with NAME");
6414 return MATCH_ERROR;
6416 /* Set binding label for BIND(C). */
6417 if (!set_binding_label (&sym->binding_label, sym->name, num))
6418 return MATCH_ERROR;
6421 if (!gfc_add_external (&sym->attr, NULL))
6422 return MATCH_ERROR;
6424 if (add_hidden_procptr_result (sym))
6425 sym = sym->result;
6427 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
6428 return MATCH_ERROR;
6430 /* Set interface. */
6431 if (proc_if != NULL)
6433 if (sym->ts.type != BT_UNKNOWN)
6435 gfc_error ("Procedure %qs at %L already has basic type of %s",
6436 sym->name, &gfc_current_locus,
6437 gfc_basic_typename (sym->ts.type));
6438 return MATCH_ERROR;
6440 sym->ts.interface = proc_if;
6441 sym->attr.untyped = 1;
6442 sym->attr.if_source = IFSRC_IFBODY;
6444 else if (current_ts.type != BT_UNKNOWN)
6446 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
6447 return MATCH_ERROR;
6448 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6449 sym->ts.interface->ts = current_ts;
6450 sym->ts.interface->attr.flavor = FL_PROCEDURE;
6451 sym->ts.interface->attr.function = 1;
6452 sym->attr.function = 1;
6453 sym->attr.if_source = IFSRC_UNKNOWN;
6456 if (gfc_match (" =>") == MATCH_YES)
6458 if (!current_attr.pointer)
6460 gfc_error ("Initialization at %C isn't for a pointer variable");
6461 m = MATCH_ERROR;
6462 goto cleanup;
6465 m = match_pointer_init (&initializer, 1);
6466 if (m != MATCH_YES)
6467 goto cleanup;
6469 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
6470 goto cleanup;
6474 if (gfc_match_eos () == MATCH_YES)
6475 return MATCH_YES;
6476 if (gfc_match_char (',') != MATCH_YES)
6477 goto syntax;
6480 syntax:
6481 gfc_error ("Syntax error in PROCEDURE statement at %C");
6482 return MATCH_ERROR;
6484 cleanup:
6485 /* Free stuff up and return. */
6486 gfc_free_expr (initializer);
6487 return m;
6491 static match
6492 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
6495 /* Match a procedure pointer component declaration (R445). */
6497 static match
6498 match_ppc_decl (void)
6500 match m;
6501 gfc_symbol *proc_if = NULL;
6502 gfc_typespec ts;
6503 int num;
6504 gfc_component *c;
6505 gfc_expr *initializer = NULL;
6506 gfc_typebound_proc* tb;
6507 char name[GFC_MAX_SYMBOL_LEN + 1];
6509 /* Parse interface (with brackets). */
6510 m = match_procedure_interface (&proc_if);
6511 if (m != MATCH_YES)
6512 goto syntax;
6514 /* Parse attributes. */
6515 tb = XCNEW (gfc_typebound_proc);
6516 tb->where = gfc_current_locus;
6517 m = match_binding_attributes (tb, false, true);
6518 if (m == MATCH_ERROR)
6519 return m;
6521 gfc_clear_attr (&current_attr);
6522 current_attr.procedure = 1;
6523 current_attr.proc_pointer = 1;
6524 current_attr.access = tb->access;
6525 current_attr.flavor = FL_PROCEDURE;
6527 /* Match the colons (required). */
6528 if (gfc_match (" ::") != MATCH_YES)
6530 gfc_error ("Expected %<::%> after binding-attributes at %C");
6531 return MATCH_ERROR;
6534 /* Check for C450. */
6535 if (!tb->nopass && proc_if == NULL)
6537 gfc_error("NOPASS or explicit interface required at %C");
6538 return MATCH_ERROR;
6541 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
6542 return MATCH_ERROR;
6544 /* Match PPC names. */
6545 ts = current_ts;
6546 for(num=1;;num++)
6548 m = gfc_match_name (name);
6549 if (m == MATCH_NO)
6550 goto syntax;
6551 else if (m == MATCH_ERROR)
6552 return m;
6554 if (!gfc_add_component (gfc_current_block(), name, &c))
6555 return MATCH_ERROR;
6557 /* Add current_attr to the symbol attributes. */
6558 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
6559 return MATCH_ERROR;
6561 if (!gfc_add_external (&c->attr, NULL))
6562 return MATCH_ERROR;
6564 if (!gfc_add_proc (&c->attr, name, NULL))
6565 return MATCH_ERROR;
6567 if (num == 1)
6568 c->tb = tb;
6569 else
6571 c->tb = XCNEW (gfc_typebound_proc);
6572 c->tb->where = gfc_current_locus;
6573 *c->tb = *tb;
6576 /* Set interface. */
6577 if (proc_if != NULL)
6579 c->ts.interface = proc_if;
6580 c->attr.untyped = 1;
6581 c->attr.if_source = IFSRC_IFBODY;
6583 else if (ts.type != BT_UNKNOWN)
6585 c->ts = ts;
6586 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6587 c->ts.interface->result = c->ts.interface;
6588 c->ts.interface->ts = ts;
6589 c->ts.interface->attr.flavor = FL_PROCEDURE;
6590 c->ts.interface->attr.function = 1;
6591 c->attr.function = 1;
6592 c->attr.if_source = IFSRC_UNKNOWN;
6595 if (gfc_match (" =>") == MATCH_YES)
6597 m = match_pointer_init (&initializer, 1);
6598 if (m != MATCH_YES)
6600 gfc_free_expr (initializer);
6601 return m;
6603 c->initializer = initializer;
6606 if (gfc_match_eos () == MATCH_YES)
6607 return MATCH_YES;
6608 if (gfc_match_char (',') != MATCH_YES)
6609 goto syntax;
6612 syntax:
6613 gfc_error ("Syntax error in procedure pointer component at %C");
6614 return MATCH_ERROR;
6618 /* Match a PROCEDURE declaration inside an interface (R1206). */
6620 static match
6621 match_procedure_in_interface (void)
6623 match m;
6624 gfc_symbol *sym;
6625 char name[GFC_MAX_SYMBOL_LEN + 1];
6626 locus old_locus;
6628 if (current_interface.type == INTERFACE_NAMELESS
6629 || current_interface.type == INTERFACE_ABSTRACT)
6631 gfc_error ("PROCEDURE at %C must be in a generic interface");
6632 return MATCH_ERROR;
6635 /* Check if the F2008 optional double colon appears. */
6636 gfc_gobble_whitespace ();
6637 old_locus = gfc_current_locus;
6638 if (gfc_match ("::") == MATCH_YES)
6640 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
6641 "MODULE PROCEDURE statement at %L", &old_locus))
6642 return MATCH_ERROR;
6644 else
6645 gfc_current_locus = old_locus;
6647 for(;;)
6649 m = gfc_match_name (name);
6650 if (m == MATCH_NO)
6651 goto syntax;
6652 else if (m == MATCH_ERROR)
6653 return m;
6654 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
6655 return MATCH_ERROR;
6657 if (!gfc_add_interface (sym))
6658 return MATCH_ERROR;
6660 if (gfc_match_eos () == MATCH_YES)
6661 break;
6662 if (gfc_match_char (',') != MATCH_YES)
6663 goto syntax;
6666 return MATCH_YES;
6668 syntax:
6669 gfc_error ("Syntax error in PROCEDURE statement at %C");
6670 return MATCH_ERROR;
6674 /* General matcher for PROCEDURE declarations. */
6676 static match match_procedure_in_type (void);
6678 match
6679 gfc_match_procedure (void)
6681 match m;
6683 switch (gfc_current_state ())
6685 case COMP_NONE:
6686 case COMP_PROGRAM:
6687 case COMP_MODULE:
6688 case COMP_SUBMODULE:
6689 case COMP_SUBROUTINE:
6690 case COMP_FUNCTION:
6691 case COMP_BLOCK:
6692 m = match_procedure_decl ();
6693 break;
6694 case COMP_INTERFACE:
6695 m = match_procedure_in_interface ();
6696 break;
6697 case COMP_DERIVED:
6698 m = match_ppc_decl ();
6699 break;
6700 case COMP_DERIVED_CONTAINS:
6701 m = match_procedure_in_type ();
6702 break;
6703 default:
6704 return MATCH_NO;
6707 if (m != MATCH_YES)
6708 return m;
6710 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
6711 return MATCH_ERROR;
6713 return m;
6717 /* Warn if a matched procedure has the same name as an intrinsic; this is
6718 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
6719 parser-state-stack to find out whether we're in a module. */
6721 static void
6722 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
6724 bool in_module;
6726 in_module = (gfc_state_stack->previous
6727 && (gfc_state_stack->previous->state == COMP_MODULE
6728 || gfc_state_stack->previous->state == COMP_SUBMODULE));
6730 gfc_warn_intrinsic_shadow (sym, in_module, func);
6734 /* Match a function declaration. */
6736 match
6737 gfc_match_function_decl (void)
6739 char name[GFC_MAX_SYMBOL_LEN + 1];
6740 gfc_symbol *sym, *result;
6741 locus old_loc;
6742 match m;
6743 match suffix_match;
6744 match found_match; /* Status returned by match func. */
6746 if (gfc_current_state () != COMP_NONE
6747 && gfc_current_state () != COMP_INTERFACE
6748 && gfc_current_state () != COMP_CONTAINS)
6749 return MATCH_NO;
6751 gfc_clear_ts (&current_ts);
6753 old_loc = gfc_current_locus;
6755 m = gfc_match_prefix (&current_ts);
6756 if (m != MATCH_YES)
6758 gfc_current_locus = old_loc;
6759 return m;
6762 if (gfc_match ("function% %n", name) != MATCH_YES)
6764 gfc_current_locus = old_loc;
6765 return MATCH_NO;
6768 if (get_proc_name (name, &sym, false))
6769 return MATCH_ERROR;
6771 if (add_hidden_procptr_result (sym))
6772 sym = sym->result;
6774 if (current_attr.module_procedure)
6775 sym->attr.module_procedure = 1;
6777 gfc_new_block = sym;
6779 m = gfc_match_formal_arglist (sym, 0, 0);
6780 if (m == MATCH_NO)
6782 gfc_error ("Expected formal argument list in function "
6783 "definition at %C");
6784 m = MATCH_ERROR;
6785 goto cleanup;
6787 else if (m == MATCH_ERROR)
6788 goto cleanup;
6790 result = NULL;
6792 /* According to the draft, the bind(c) and result clause can
6793 come in either order after the formal_arg_list (i.e., either
6794 can be first, both can exist together or by themselves or neither
6795 one). Therefore, the match_result can't match the end of the
6796 string, and check for the bind(c) or result clause in either order. */
6797 found_match = gfc_match_eos ();
6799 /* Make sure that it isn't already declared as BIND(C). If it is, it
6800 must have been marked BIND(C) with a BIND(C) attribute and that is
6801 not allowed for procedures. */
6802 if (sym->attr.is_bind_c == 1)
6804 sym->attr.is_bind_c = 0;
6805 if (sym->old_symbol != NULL)
6806 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6807 "variables or common blocks",
6808 &(sym->old_symbol->declared_at));
6809 else
6810 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6811 "variables or common blocks", &gfc_current_locus);
6814 if (found_match != MATCH_YES)
6816 /* If we haven't found the end-of-statement, look for a suffix. */
6817 suffix_match = gfc_match_suffix (sym, &result);
6818 if (suffix_match == MATCH_YES)
6819 /* Need to get the eos now. */
6820 found_match = gfc_match_eos ();
6821 else
6822 found_match = suffix_match;
6825 if(found_match != MATCH_YES)
6826 m = MATCH_ERROR;
6827 else
6829 /* Make changes to the symbol. */
6830 m = MATCH_ERROR;
6832 if (!gfc_add_function (&sym->attr, sym->name, NULL))
6833 goto cleanup;
6835 if (!gfc_missing_attr (&sym->attr, NULL))
6836 goto cleanup;
6838 if (!copy_prefix (&sym->attr, &sym->declared_at))
6840 if(!sym->attr.module_procedure)
6841 goto cleanup;
6842 else
6843 gfc_error_check ();
6846 /* Delay matching the function characteristics until after the
6847 specification block by signalling kind=-1. */
6848 sym->declared_at = old_loc;
6849 if (current_ts.type != BT_UNKNOWN)
6850 current_ts.kind = -1;
6851 else
6852 current_ts.kind = 0;
6854 if (result == NULL)
6856 if (current_ts.type != BT_UNKNOWN
6857 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
6858 goto cleanup;
6859 sym->result = sym;
6861 else
6863 if (current_ts.type != BT_UNKNOWN
6864 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
6865 goto cleanup;
6866 sym->result = result;
6869 /* Warn if this procedure has the same name as an intrinsic. */
6870 do_warn_intrinsic_shadow (sym, true);
6872 return MATCH_YES;
6875 cleanup:
6876 gfc_current_locus = old_loc;
6877 return m;
6881 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
6882 pass the name of the entry, rather than the gfc_current_block name, and
6883 to return false upon finding an existing global entry. */
6885 static bool
6886 add_global_entry (const char *name, const char *binding_label, bool sub,
6887 locus *where)
6889 gfc_gsymbol *s;
6890 enum gfc_symbol_type type;
6892 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
6894 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
6895 name is a global identifier. */
6896 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
6898 s = gfc_get_gsymbol (name);
6900 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
6902 gfc_global_used (s, where);
6903 return false;
6905 else
6907 s->type = type;
6908 s->sym_name = name;
6909 s->where = *where;
6910 s->defined = 1;
6911 s->ns = gfc_current_ns;
6915 /* Don't add the symbol multiple times. */
6916 if (binding_label
6917 && (!gfc_notification_std (GFC_STD_F2008)
6918 || strcmp (name, binding_label) != 0))
6920 s = gfc_get_gsymbol (binding_label);
6922 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
6924 gfc_global_used (s, where);
6925 return false;
6927 else
6929 s->type = type;
6930 s->sym_name = name;
6931 s->binding_label = binding_label;
6932 s->where = *where;
6933 s->defined = 1;
6934 s->ns = gfc_current_ns;
6938 return true;
6942 /* Match an ENTRY statement. */
6944 match
6945 gfc_match_entry (void)
6947 gfc_symbol *proc;
6948 gfc_symbol *result;
6949 gfc_symbol *entry;
6950 char name[GFC_MAX_SYMBOL_LEN + 1];
6951 gfc_compile_state state;
6952 match m;
6953 gfc_entry_list *el;
6954 locus old_loc;
6955 bool module_procedure;
6956 char peek_char;
6957 match is_bind_c;
6959 m = gfc_match_name (name);
6960 if (m != MATCH_YES)
6961 return m;
6963 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
6964 return MATCH_ERROR;
6966 state = gfc_current_state ();
6967 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
6969 switch (state)
6971 case COMP_PROGRAM:
6972 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
6973 break;
6974 case COMP_MODULE:
6975 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
6976 break;
6977 case COMP_SUBMODULE:
6978 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
6979 break;
6980 case COMP_BLOCK_DATA:
6981 gfc_error ("ENTRY statement at %C cannot appear within "
6982 "a BLOCK DATA");
6983 break;
6984 case COMP_INTERFACE:
6985 gfc_error ("ENTRY statement at %C cannot appear within "
6986 "an INTERFACE");
6987 break;
6988 case COMP_STRUCTURE:
6989 gfc_error ("ENTRY statement at %C cannot appear within "
6990 "a STRUCTURE block");
6991 break;
6992 case COMP_DERIVED:
6993 gfc_error ("ENTRY statement at %C cannot appear within "
6994 "a DERIVED TYPE block");
6995 break;
6996 case COMP_IF:
6997 gfc_error ("ENTRY statement at %C cannot appear within "
6998 "an IF-THEN block");
6999 break;
7000 case COMP_DO:
7001 case COMP_DO_CONCURRENT:
7002 gfc_error ("ENTRY statement at %C cannot appear within "
7003 "a DO block");
7004 break;
7005 case COMP_SELECT:
7006 gfc_error ("ENTRY statement at %C cannot appear within "
7007 "a SELECT block");
7008 break;
7009 case COMP_FORALL:
7010 gfc_error ("ENTRY statement at %C cannot appear within "
7011 "a FORALL block");
7012 break;
7013 case COMP_WHERE:
7014 gfc_error ("ENTRY statement at %C cannot appear within "
7015 "a WHERE block");
7016 break;
7017 case COMP_CONTAINS:
7018 gfc_error ("ENTRY statement at %C cannot appear within "
7019 "a contained subprogram");
7020 break;
7021 default:
7022 gfc_error ("Unexpected ENTRY statement at %C");
7024 return MATCH_ERROR;
7027 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7028 && gfc_state_stack->previous->state == COMP_INTERFACE)
7030 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7031 return MATCH_ERROR;
7034 module_procedure = gfc_current_ns->parent != NULL
7035 && gfc_current_ns->parent->proc_name
7036 && gfc_current_ns->parent->proc_name->attr.flavor
7037 == FL_MODULE;
7039 if (gfc_current_ns->parent != NULL
7040 && gfc_current_ns->parent->proc_name
7041 && !module_procedure)
7043 gfc_error("ENTRY statement at %C cannot appear in a "
7044 "contained procedure");
7045 return MATCH_ERROR;
7048 /* Module function entries need special care in get_proc_name
7049 because previous references within the function will have
7050 created symbols attached to the current namespace. */
7051 if (get_proc_name (name, &entry,
7052 gfc_current_ns->parent != NULL
7053 && module_procedure))
7054 return MATCH_ERROR;
7056 proc = gfc_current_block ();
7058 /* Make sure that it isn't already declared as BIND(C). If it is, it
7059 must have been marked BIND(C) with a BIND(C) attribute and that is
7060 not allowed for procedures. */
7061 if (entry->attr.is_bind_c == 1)
7063 entry->attr.is_bind_c = 0;
7064 if (entry->old_symbol != NULL)
7065 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7066 "variables or common blocks",
7067 &(entry->old_symbol->declared_at));
7068 else
7069 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7070 "variables or common blocks", &gfc_current_locus);
7073 /* Check what next non-whitespace character is so we can tell if there
7074 is the required parens if we have a BIND(C). */
7075 old_loc = gfc_current_locus;
7076 gfc_gobble_whitespace ();
7077 peek_char = gfc_peek_ascii_char ();
7079 if (state == COMP_SUBROUTINE)
7081 m = gfc_match_formal_arglist (entry, 0, 1);
7082 if (m != MATCH_YES)
7083 return MATCH_ERROR;
7085 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7086 never be an internal procedure. */
7087 is_bind_c = gfc_match_bind_c (entry, true);
7088 if (is_bind_c == MATCH_ERROR)
7089 return MATCH_ERROR;
7090 if (is_bind_c == MATCH_YES)
7092 if (peek_char != '(')
7094 gfc_error ("Missing required parentheses before BIND(C) at %C");
7095 return MATCH_ERROR;
7097 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7098 &(entry->declared_at), 1))
7099 return MATCH_ERROR;
7102 if (!gfc_current_ns->parent
7103 && !add_global_entry (name, entry->binding_label, true,
7104 &old_loc))
7105 return MATCH_ERROR;
7107 /* An entry in a subroutine. */
7108 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7109 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7110 return MATCH_ERROR;
7112 else
7114 /* An entry in a function.
7115 We need to take special care because writing
7116 ENTRY f()
7118 ENTRY f
7119 is allowed, whereas
7120 ENTRY f() RESULT (r)
7121 can't be written as
7122 ENTRY f RESULT (r). */
7123 if (gfc_match_eos () == MATCH_YES)
7125 gfc_current_locus = old_loc;
7126 /* Match the empty argument list, and add the interface to
7127 the symbol. */
7128 m = gfc_match_formal_arglist (entry, 0, 1);
7130 else
7131 m = gfc_match_formal_arglist (entry, 0, 0);
7133 if (m != MATCH_YES)
7134 return MATCH_ERROR;
7136 result = NULL;
7138 if (gfc_match_eos () == MATCH_YES)
7140 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7141 || !gfc_add_function (&entry->attr, entry->name, NULL))
7142 return MATCH_ERROR;
7144 entry->result = entry;
7146 else
7148 m = gfc_match_suffix (entry, &result);
7149 if (m == MATCH_NO)
7150 gfc_syntax_error (ST_ENTRY);
7151 if (m != MATCH_YES)
7152 return MATCH_ERROR;
7154 if (result)
7156 if (!gfc_add_result (&result->attr, result->name, NULL)
7157 || !gfc_add_entry (&entry->attr, result->name, NULL)
7158 || !gfc_add_function (&entry->attr, result->name, NULL))
7159 return MATCH_ERROR;
7160 entry->result = result;
7162 else
7164 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7165 || !gfc_add_function (&entry->attr, entry->name, NULL))
7166 return MATCH_ERROR;
7167 entry->result = entry;
7171 if (!gfc_current_ns->parent
7172 && !add_global_entry (name, entry->binding_label, false,
7173 &old_loc))
7174 return MATCH_ERROR;
7177 if (gfc_match_eos () != MATCH_YES)
7179 gfc_syntax_error (ST_ENTRY);
7180 return MATCH_ERROR;
7183 entry->attr.recursive = proc->attr.recursive;
7184 entry->attr.elemental = proc->attr.elemental;
7185 entry->attr.pure = proc->attr.pure;
7187 el = gfc_get_entry_list ();
7188 el->sym = entry;
7189 el->next = gfc_current_ns->entries;
7190 gfc_current_ns->entries = el;
7191 if (el->next)
7192 el->id = el->next->id + 1;
7193 else
7194 el->id = 1;
7196 new_st.op = EXEC_ENTRY;
7197 new_st.ext.entry = el;
7199 return MATCH_YES;
7203 /* Match a subroutine statement, including optional prefixes. */
7205 match
7206 gfc_match_subroutine (void)
7208 char name[GFC_MAX_SYMBOL_LEN + 1];
7209 gfc_symbol *sym;
7210 match m;
7211 match is_bind_c;
7212 char peek_char;
7213 bool allow_binding_name;
7215 if (gfc_current_state () != COMP_NONE
7216 && gfc_current_state () != COMP_INTERFACE
7217 && gfc_current_state () != COMP_CONTAINS)
7218 return MATCH_NO;
7220 m = gfc_match_prefix (NULL);
7221 if (m != MATCH_YES)
7222 return m;
7224 m = gfc_match ("subroutine% %n", name);
7225 if (m != MATCH_YES)
7226 return m;
7228 if (get_proc_name (name, &sym, false))
7229 return MATCH_ERROR;
7231 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7232 the symbol existed before. */
7233 sym->declared_at = gfc_current_locus;
7235 if (current_attr.module_procedure)
7236 sym->attr.module_procedure = 1;
7238 if (add_hidden_procptr_result (sym))
7239 sym = sym->result;
7241 gfc_new_block = sym;
7243 /* Check what next non-whitespace character is so we can tell if there
7244 is the required parens if we have a BIND(C). */
7245 gfc_gobble_whitespace ();
7246 peek_char = gfc_peek_ascii_char ();
7248 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7249 return MATCH_ERROR;
7251 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7252 return MATCH_ERROR;
7254 /* Make sure that it isn't already declared as BIND(C). If it is, it
7255 must have been marked BIND(C) with a BIND(C) attribute and that is
7256 not allowed for procedures. */
7257 if (sym->attr.is_bind_c == 1)
7259 sym->attr.is_bind_c = 0;
7260 if (sym->old_symbol != NULL)
7261 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7262 "variables or common blocks",
7263 &(sym->old_symbol->declared_at));
7264 else
7265 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7266 "variables or common blocks", &gfc_current_locus);
7269 /* C binding names are not allowed for internal procedures. */
7270 if (gfc_current_state () == COMP_CONTAINS
7271 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7272 allow_binding_name = false;
7273 else
7274 allow_binding_name = true;
7276 /* Here, we are just checking if it has the bind(c) attribute, and if
7277 so, then we need to make sure it's all correct. If it doesn't,
7278 we still need to continue matching the rest of the subroutine line. */
7279 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7280 if (is_bind_c == MATCH_ERROR)
7282 /* There was an attempt at the bind(c), but it was wrong. An
7283 error message should have been printed w/in the gfc_match_bind_c
7284 so here we'll just return the MATCH_ERROR. */
7285 return MATCH_ERROR;
7288 if (is_bind_c == MATCH_YES)
7290 /* The following is allowed in the Fortran 2008 draft. */
7291 if (gfc_current_state () == COMP_CONTAINS
7292 && sym->ns->proc_name->attr.flavor != FL_MODULE
7293 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7294 "at %L may not be specified for an internal "
7295 "procedure", &gfc_current_locus))
7296 return MATCH_ERROR;
7298 if (peek_char != '(')
7300 gfc_error ("Missing required parentheses before BIND(C) at %C");
7301 return MATCH_ERROR;
7303 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
7304 &(sym->declared_at), 1))
7305 return MATCH_ERROR;
7308 if (gfc_match_eos () != MATCH_YES)
7310 gfc_syntax_error (ST_SUBROUTINE);
7311 return MATCH_ERROR;
7314 if (!copy_prefix (&sym->attr, &sym->declared_at))
7316 if(!sym->attr.module_procedure)
7317 return MATCH_ERROR;
7318 else
7319 gfc_error_check ();
7322 /* Warn if it has the same name as an intrinsic. */
7323 do_warn_intrinsic_shadow (sym, false);
7325 return MATCH_YES;
7329 /* Check that the NAME identifier in a BIND attribute or statement
7330 is conform to C identifier rules. */
7332 match
7333 check_bind_name_identifier (char **name)
7335 char *n = *name, *p;
7337 /* Remove leading spaces. */
7338 while (*n == ' ')
7339 n++;
7341 /* On an empty string, free memory and set name to NULL. */
7342 if (*n == '\0')
7344 free (*name);
7345 *name = NULL;
7346 return MATCH_YES;
7349 /* Remove trailing spaces. */
7350 p = n + strlen(n) - 1;
7351 while (*p == ' ')
7352 *(p--) = '\0';
7354 /* Insert the identifier into the symbol table. */
7355 p = xstrdup (n);
7356 free (*name);
7357 *name = p;
7359 /* Now check that identifier is valid under C rules. */
7360 if (ISDIGIT (*p))
7362 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7363 return MATCH_ERROR;
7366 for (; *p; p++)
7367 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
7369 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7370 return MATCH_ERROR;
7373 return MATCH_YES;
7377 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
7378 given, and set the binding label in either the given symbol (if not
7379 NULL), or in the current_ts. The symbol may be NULL because we may
7380 encounter the BIND(C) before the declaration itself. Return
7381 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
7382 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
7383 or MATCH_YES if the specifier was correct and the binding label and
7384 bind(c) fields were set correctly for the given symbol or the
7385 current_ts. If allow_binding_name is false, no binding name may be
7386 given. */
7388 match
7389 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
7391 char *binding_label = NULL;
7392 gfc_expr *e = NULL;
7394 /* Initialize the flag that specifies whether we encountered a NAME=
7395 specifier or not. */
7396 has_name_equals = 0;
7398 /* This much we have to be able to match, in this order, if
7399 there is a bind(c) label. */
7400 if (gfc_match (" bind ( c ") != MATCH_YES)
7401 return MATCH_NO;
7403 /* Now see if there is a binding label, or if we've reached the
7404 end of the bind(c) attribute without one. */
7405 if (gfc_match_char (',') == MATCH_YES)
7407 if (gfc_match (" name = ") != MATCH_YES)
7409 gfc_error ("Syntax error in NAME= specifier for binding label "
7410 "at %C");
7411 /* should give an error message here */
7412 return MATCH_ERROR;
7415 has_name_equals = 1;
7417 if (gfc_match_init_expr (&e) != MATCH_YES)
7419 gfc_free_expr (e);
7420 return MATCH_ERROR;
7423 if (!gfc_simplify_expr(e, 0))
7425 gfc_error ("NAME= specifier at %C should be a constant expression");
7426 gfc_free_expr (e);
7427 return MATCH_ERROR;
7430 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
7431 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
7433 gfc_error ("NAME= specifier at %C should be a scalar of "
7434 "default character kind");
7435 gfc_free_expr(e);
7436 return MATCH_ERROR;
7439 // Get a C string from the Fortran string constant
7440 binding_label = gfc_widechar_to_char (e->value.character.string,
7441 e->value.character.length);
7442 gfc_free_expr(e);
7444 // Check that it is valid (old gfc_match_name_C)
7445 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
7446 return MATCH_ERROR;
7449 /* Get the required right paren. */
7450 if (gfc_match_char (')') != MATCH_YES)
7452 gfc_error ("Missing closing paren for binding label at %C");
7453 return MATCH_ERROR;
7456 if (has_name_equals && !allow_binding_name)
7458 gfc_error ("No binding name is allowed in BIND(C) at %C");
7459 return MATCH_ERROR;
7462 if (has_name_equals && sym != NULL && sym->attr.dummy)
7464 gfc_error ("For dummy procedure %s, no binding name is "
7465 "allowed in BIND(C) at %C", sym->name);
7466 return MATCH_ERROR;
7470 /* Save the binding label to the symbol. If sym is null, we're
7471 probably matching the typespec attributes of a declaration and
7472 haven't gotten the name yet, and therefore, no symbol yet. */
7473 if (binding_label)
7475 if (sym != NULL)
7476 sym->binding_label = binding_label;
7477 else
7478 curr_binding_label = binding_label;
7480 else if (allow_binding_name)
7482 /* No binding label, but if symbol isn't null, we
7483 can set the label for it here.
7484 If name="" or allow_binding_name is false, no C binding name is
7485 created. */
7486 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
7487 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
7490 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
7491 && current_interface.type == INTERFACE_ABSTRACT)
7493 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
7494 return MATCH_ERROR;
7497 return MATCH_YES;
7501 /* Return nonzero if we're currently compiling a contained procedure. */
7503 static int
7504 contained_procedure (void)
7506 gfc_state_data *s = gfc_state_stack;
7508 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
7509 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
7510 return 1;
7512 return 0;
7515 /* Set the kind of each enumerator. The kind is selected such that it is
7516 interoperable with the corresponding C enumeration type, making
7517 sure that -fshort-enums is honored. */
7519 static void
7520 set_enum_kind(void)
7522 enumerator_history *current_history = NULL;
7523 int kind;
7524 int i;
7526 if (max_enum == NULL || enum_history == NULL)
7527 return;
7529 if (!flag_short_enums)
7530 return;
7532 i = 0;
7535 kind = gfc_integer_kinds[i++].kind;
7537 while (kind < gfc_c_int_kind
7538 && gfc_check_integer_range (max_enum->initializer->value.integer,
7539 kind) != ARITH_OK);
7541 current_history = enum_history;
7542 while (current_history != NULL)
7544 current_history->sym->ts.kind = kind;
7545 current_history = current_history->next;
7550 /* Match any of the various end-block statements. Returns the type of
7551 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
7552 and END BLOCK statements cannot be replaced by a single END statement. */
7554 match
7555 gfc_match_end (gfc_statement *st)
7557 char name[GFC_MAX_SYMBOL_LEN + 1];
7558 gfc_compile_state state;
7559 locus old_loc;
7560 const char *block_name;
7561 const char *target;
7562 int eos_ok;
7563 match m;
7564 gfc_namespace *parent_ns, *ns, *prev_ns;
7565 gfc_namespace **nsp;
7566 bool abreviated_modproc_decl = false;
7567 bool got_matching_end = false;
7569 old_loc = gfc_current_locus;
7570 if (gfc_match ("end") != MATCH_YES)
7571 return MATCH_NO;
7573 state = gfc_current_state ();
7574 block_name = gfc_current_block () == NULL
7575 ? NULL : gfc_current_block ()->name;
7577 switch (state)
7579 case COMP_ASSOCIATE:
7580 case COMP_BLOCK:
7581 if (!strncmp (block_name, "block@", strlen("block@")))
7582 block_name = NULL;
7583 break;
7585 case COMP_CONTAINS:
7586 case COMP_DERIVED_CONTAINS:
7587 state = gfc_state_stack->previous->state;
7588 block_name = gfc_state_stack->previous->sym == NULL
7589 ? NULL : gfc_state_stack->previous->sym->name;
7590 abreviated_modproc_decl = gfc_state_stack->previous->sym
7591 && gfc_state_stack->previous->sym->abr_modproc_decl;
7592 break;
7594 default:
7595 break;
7598 if (!abreviated_modproc_decl)
7599 abreviated_modproc_decl = gfc_current_block ()
7600 && gfc_current_block ()->abr_modproc_decl;
7602 switch (state)
7604 case COMP_NONE:
7605 case COMP_PROGRAM:
7606 *st = ST_END_PROGRAM;
7607 target = " program";
7608 eos_ok = 1;
7609 break;
7611 case COMP_SUBROUTINE:
7612 *st = ST_END_SUBROUTINE;
7613 if (!abreviated_modproc_decl)
7614 target = " subroutine";
7615 else
7616 target = " procedure";
7617 eos_ok = !contained_procedure ();
7618 break;
7620 case COMP_FUNCTION:
7621 *st = ST_END_FUNCTION;
7622 if (!abreviated_modproc_decl)
7623 target = " function";
7624 else
7625 target = " procedure";
7626 eos_ok = !contained_procedure ();
7627 break;
7629 case COMP_BLOCK_DATA:
7630 *st = ST_END_BLOCK_DATA;
7631 target = " block data";
7632 eos_ok = 1;
7633 break;
7635 case COMP_MODULE:
7636 *st = ST_END_MODULE;
7637 target = " module";
7638 eos_ok = 1;
7639 break;
7641 case COMP_SUBMODULE:
7642 *st = ST_END_SUBMODULE;
7643 target = " submodule";
7644 eos_ok = 1;
7645 break;
7647 case COMP_INTERFACE:
7648 *st = ST_END_INTERFACE;
7649 target = " interface";
7650 eos_ok = 0;
7651 break;
7653 case COMP_MAP:
7654 *st = ST_END_MAP;
7655 target = " map";
7656 eos_ok = 0;
7657 break;
7659 case COMP_UNION:
7660 *st = ST_END_UNION;
7661 target = " union";
7662 eos_ok = 0;
7663 break;
7665 case COMP_STRUCTURE:
7666 *st = ST_END_STRUCTURE;
7667 target = " structure";
7668 eos_ok = 0;
7669 break;
7671 case COMP_DERIVED:
7672 case COMP_DERIVED_CONTAINS:
7673 *st = ST_END_TYPE;
7674 target = " type";
7675 eos_ok = 0;
7676 break;
7678 case COMP_ASSOCIATE:
7679 *st = ST_END_ASSOCIATE;
7680 target = " associate";
7681 eos_ok = 0;
7682 break;
7684 case COMP_BLOCK:
7685 *st = ST_END_BLOCK;
7686 target = " block";
7687 eos_ok = 0;
7688 break;
7690 case COMP_IF:
7691 *st = ST_ENDIF;
7692 target = " if";
7693 eos_ok = 0;
7694 break;
7696 case COMP_DO:
7697 case COMP_DO_CONCURRENT:
7698 *st = ST_ENDDO;
7699 target = " do";
7700 eos_ok = 0;
7701 break;
7703 case COMP_CRITICAL:
7704 *st = ST_END_CRITICAL;
7705 target = " critical";
7706 eos_ok = 0;
7707 break;
7709 case COMP_SELECT:
7710 case COMP_SELECT_TYPE:
7711 *st = ST_END_SELECT;
7712 target = " select";
7713 eos_ok = 0;
7714 break;
7716 case COMP_FORALL:
7717 *st = ST_END_FORALL;
7718 target = " forall";
7719 eos_ok = 0;
7720 break;
7722 case COMP_WHERE:
7723 *st = ST_END_WHERE;
7724 target = " where";
7725 eos_ok = 0;
7726 break;
7728 case COMP_ENUM:
7729 *st = ST_END_ENUM;
7730 target = " enum";
7731 eos_ok = 0;
7732 last_initializer = NULL;
7733 set_enum_kind ();
7734 gfc_free_enum_history ();
7735 break;
7737 default:
7738 gfc_error ("Unexpected END statement at %C");
7739 goto cleanup;
7742 old_loc = gfc_current_locus;
7743 if (gfc_match_eos () == MATCH_YES)
7745 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
7747 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
7748 "instead of %s statement at %L",
7749 abreviated_modproc_decl ? "END PROCEDURE"
7750 : gfc_ascii_statement(*st), &old_loc))
7751 goto cleanup;
7753 else if (!eos_ok)
7755 /* We would have required END [something]. */
7756 gfc_error ("%s statement expected at %L",
7757 gfc_ascii_statement (*st), &old_loc);
7758 goto cleanup;
7761 return MATCH_YES;
7764 /* Verify that we've got the sort of end-block that we're expecting. */
7765 if (gfc_match (target) != MATCH_YES)
7767 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
7768 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
7769 goto cleanup;
7771 else
7772 got_matching_end = true;
7774 old_loc = gfc_current_locus;
7775 /* If we're at the end, make sure a block name wasn't required. */
7776 if (gfc_match_eos () == MATCH_YES)
7779 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
7780 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
7781 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
7782 return MATCH_YES;
7784 if (!block_name)
7785 return MATCH_YES;
7787 gfc_error ("Expected block name of %qs in %s statement at %L",
7788 block_name, gfc_ascii_statement (*st), &old_loc);
7790 return MATCH_ERROR;
7793 /* END INTERFACE has a special handler for its several possible endings. */
7794 if (*st == ST_END_INTERFACE)
7795 return gfc_match_end_interface ();
7797 /* We haven't hit the end of statement, so what is left must be an
7798 end-name. */
7799 m = gfc_match_space ();
7800 if (m == MATCH_YES)
7801 m = gfc_match_name (name);
7803 if (m == MATCH_NO)
7804 gfc_error ("Expected terminating name at %C");
7805 if (m != MATCH_YES)
7806 goto cleanup;
7808 if (block_name == NULL)
7809 goto syntax;
7811 /* We have to pick out the declared submodule name from the composite
7812 required by F2008:11.2.3 para 2, which ends in the declared name. */
7813 if (state == COMP_SUBMODULE)
7814 block_name = strchr (block_name, '.') + 1;
7816 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
7818 gfc_error ("Expected label %qs for %s statement at %C", block_name,
7819 gfc_ascii_statement (*st));
7820 goto cleanup;
7822 /* Procedure pointer as function result. */
7823 else if (strcmp (block_name, "ppr@") == 0
7824 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
7826 gfc_error ("Expected label %qs for %s statement at %C",
7827 gfc_current_block ()->ns->proc_name->name,
7828 gfc_ascii_statement (*st));
7829 goto cleanup;
7832 if (gfc_match_eos () == MATCH_YES)
7833 return MATCH_YES;
7835 syntax:
7836 gfc_syntax_error (*st);
7838 cleanup:
7839 gfc_current_locus = old_loc;
7841 /* If we are missing an END BLOCK, we created a half-ready namespace.
7842 Remove it from the parent namespace's sibling list. */
7844 while (state == COMP_BLOCK && !got_matching_end)
7846 parent_ns = gfc_current_ns->parent;
7848 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
7850 prev_ns = NULL;
7851 ns = *nsp;
7852 while (ns)
7854 if (ns == gfc_current_ns)
7856 if (prev_ns == NULL)
7857 *nsp = NULL;
7858 else
7859 prev_ns->sibling = ns->sibling;
7861 prev_ns = ns;
7862 ns = ns->sibling;
7865 gfc_free_namespace (gfc_current_ns);
7866 gfc_current_ns = parent_ns;
7867 gfc_state_stack = gfc_state_stack->previous;
7868 state = gfc_current_state ();
7871 return MATCH_ERROR;
7876 /***************** Attribute declaration statements ****************/
7878 /* Set the attribute of a single variable. */
7880 static match
7881 attr_decl1 (void)
7883 char name[GFC_MAX_SYMBOL_LEN + 1];
7884 gfc_array_spec *as;
7886 /* Workaround -Wmaybe-uninitialized false positive during
7887 profiledbootstrap by initializing them. */
7888 gfc_symbol *sym = NULL;
7889 locus var_locus;
7890 match m;
7892 as = NULL;
7894 m = gfc_match_name (name);
7895 if (m != MATCH_YES)
7896 goto cleanup;
7898 if (find_special (name, &sym, false))
7899 return MATCH_ERROR;
7901 if (!check_function_name (name))
7903 m = MATCH_ERROR;
7904 goto cleanup;
7907 var_locus = gfc_current_locus;
7909 /* Deal with possible array specification for certain attributes. */
7910 if (current_attr.dimension
7911 || current_attr.codimension
7912 || current_attr.allocatable
7913 || current_attr.pointer
7914 || current_attr.target)
7916 m = gfc_match_array_spec (&as, !current_attr.codimension,
7917 !current_attr.dimension
7918 && !current_attr.pointer
7919 && !current_attr.target);
7920 if (m == MATCH_ERROR)
7921 goto cleanup;
7923 if (current_attr.dimension && m == MATCH_NO)
7925 gfc_error ("Missing array specification at %L in DIMENSION "
7926 "statement", &var_locus);
7927 m = MATCH_ERROR;
7928 goto cleanup;
7931 if (current_attr.dimension && sym->value)
7933 gfc_error ("Dimensions specified for %s at %L after its "
7934 "initialization", sym->name, &var_locus);
7935 m = MATCH_ERROR;
7936 goto cleanup;
7939 if (current_attr.codimension && m == MATCH_NO)
7941 gfc_error ("Missing array specification at %L in CODIMENSION "
7942 "statement", &var_locus);
7943 m = MATCH_ERROR;
7944 goto cleanup;
7947 if ((current_attr.allocatable || current_attr.pointer)
7948 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
7950 gfc_error ("Array specification must be deferred at %L", &var_locus);
7951 m = MATCH_ERROR;
7952 goto cleanup;
7956 /* Update symbol table. DIMENSION attribute is set in
7957 gfc_set_array_spec(). For CLASS variables, this must be applied
7958 to the first component, or '_data' field. */
7959 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
7961 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
7963 m = MATCH_ERROR;
7964 goto cleanup;
7967 else
7969 if (current_attr.dimension == 0 && current_attr.codimension == 0
7970 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
7972 m = MATCH_ERROR;
7973 goto cleanup;
7977 if (sym->ts.type == BT_CLASS
7978 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
7980 m = MATCH_ERROR;
7981 goto cleanup;
7984 if (!gfc_set_array_spec (sym, as, &var_locus))
7986 m = MATCH_ERROR;
7987 goto cleanup;
7990 if (sym->attr.cray_pointee && sym->as != NULL)
7992 /* Fix the array spec. */
7993 m = gfc_mod_pointee_as (sym->as);
7994 if (m == MATCH_ERROR)
7995 goto cleanup;
7998 if (!gfc_add_attribute (&sym->attr, &var_locus))
8000 m = MATCH_ERROR;
8001 goto cleanup;
8004 if ((current_attr.external || current_attr.intrinsic)
8005 && sym->attr.flavor != FL_PROCEDURE
8006 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8008 m = MATCH_ERROR;
8009 goto cleanup;
8012 add_hidden_procptr_result (sym);
8014 return MATCH_YES;
8016 cleanup:
8017 gfc_free_array_spec (as);
8018 return m;
8022 /* Generic attribute declaration subroutine. Used for attributes that
8023 just have a list of names. */
8025 static match
8026 attr_decl (void)
8028 match m;
8030 /* Gobble the optional double colon, by simply ignoring the result
8031 of gfc_match(). */
8032 gfc_match (" ::");
8034 for (;;)
8036 m = attr_decl1 ();
8037 if (m != MATCH_YES)
8038 break;
8040 if (gfc_match_eos () == MATCH_YES)
8042 m = MATCH_YES;
8043 break;
8046 if (gfc_match_char (',') != MATCH_YES)
8048 gfc_error ("Unexpected character in variable list at %C");
8049 m = MATCH_ERROR;
8050 break;
8054 return m;
8058 /* This routine matches Cray Pointer declarations of the form:
8059 pointer ( <pointer>, <pointee> )
8061 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8062 The pointer, if already declared, should be an integer. Otherwise, we
8063 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8064 be either a scalar, or an array declaration. No space is allocated for
8065 the pointee. For the statement
8066 pointer (ipt, ar(10))
8067 any subsequent uses of ar will be translated (in C-notation) as
8068 ar(i) => ((<type> *) ipt)(i)
8069 After gimplification, pointee variable will disappear in the code. */
8071 static match
8072 cray_pointer_decl (void)
8074 match m;
8075 gfc_array_spec *as = NULL;
8076 gfc_symbol *cptr; /* Pointer symbol. */
8077 gfc_symbol *cpte; /* Pointee symbol. */
8078 locus var_locus;
8079 bool done = false;
8081 while (!done)
8083 if (gfc_match_char ('(') != MATCH_YES)
8085 gfc_error ("Expected %<(%> at %C");
8086 return MATCH_ERROR;
8089 /* Match pointer. */
8090 var_locus = gfc_current_locus;
8091 gfc_clear_attr (&current_attr);
8092 gfc_add_cray_pointer (&current_attr, &var_locus);
8093 current_ts.type = BT_INTEGER;
8094 current_ts.kind = gfc_index_integer_kind;
8096 m = gfc_match_symbol (&cptr, 0);
8097 if (m != MATCH_YES)
8099 gfc_error ("Expected variable name at %C");
8100 return m;
8103 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8104 return MATCH_ERROR;
8106 gfc_set_sym_referenced (cptr);
8108 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8110 cptr->ts.type = BT_INTEGER;
8111 cptr->ts.kind = gfc_index_integer_kind;
8113 else if (cptr->ts.type != BT_INTEGER)
8115 gfc_error ("Cray pointer at %C must be an integer");
8116 return MATCH_ERROR;
8118 else if (cptr->ts.kind < gfc_index_integer_kind)
8119 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8120 " memory addresses require %d bytes",
8121 cptr->ts.kind, gfc_index_integer_kind);
8123 if (gfc_match_char (',') != MATCH_YES)
8125 gfc_error ("Expected \",\" at %C");
8126 return MATCH_ERROR;
8129 /* Match Pointee. */
8130 var_locus = gfc_current_locus;
8131 gfc_clear_attr (&current_attr);
8132 gfc_add_cray_pointee (&current_attr, &var_locus);
8133 current_ts.type = BT_UNKNOWN;
8134 current_ts.kind = 0;
8136 m = gfc_match_symbol (&cpte, 0);
8137 if (m != MATCH_YES)
8139 gfc_error ("Expected variable name at %C");
8140 return m;
8143 /* Check for an optional array spec. */
8144 m = gfc_match_array_spec (&as, true, false);
8145 if (m == MATCH_ERROR)
8147 gfc_free_array_spec (as);
8148 return m;
8150 else if (m == MATCH_NO)
8152 gfc_free_array_spec (as);
8153 as = NULL;
8156 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8157 return MATCH_ERROR;
8159 gfc_set_sym_referenced (cpte);
8161 if (cpte->as == NULL)
8163 if (!gfc_set_array_spec (cpte, as, &var_locus))
8164 gfc_internal_error ("Couldn't set Cray pointee array spec.");
8166 else if (as != NULL)
8168 gfc_error ("Duplicate array spec for Cray pointee at %C");
8169 gfc_free_array_spec (as);
8170 return MATCH_ERROR;
8173 as = NULL;
8175 if (cpte->as != NULL)
8177 /* Fix array spec. */
8178 m = gfc_mod_pointee_as (cpte->as);
8179 if (m == MATCH_ERROR)
8180 return m;
8183 /* Point the Pointee at the Pointer. */
8184 cpte->cp_pointer = cptr;
8186 if (gfc_match_char (')') != MATCH_YES)
8188 gfc_error ("Expected \")\" at %C");
8189 return MATCH_ERROR;
8191 m = gfc_match_char (',');
8192 if (m != MATCH_YES)
8193 done = true; /* Stop searching for more declarations. */
8197 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8198 || gfc_match_eos () != MATCH_YES)
8200 gfc_error ("Expected %<,%> or end of statement at %C");
8201 return MATCH_ERROR;
8203 return MATCH_YES;
8207 match
8208 gfc_match_external (void)
8211 gfc_clear_attr (&current_attr);
8212 current_attr.external = 1;
8214 return attr_decl ();
8218 match
8219 gfc_match_intent (void)
8221 sym_intent intent;
8223 /* This is not allowed within a BLOCK construct! */
8224 if (gfc_current_state () == COMP_BLOCK)
8226 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8227 return MATCH_ERROR;
8230 intent = match_intent_spec ();
8231 if (intent == INTENT_UNKNOWN)
8232 return MATCH_ERROR;
8234 gfc_clear_attr (&current_attr);
8235 current_attr.intent = intent;
8237 return attr_decl ();
8241 match
8242 gfc_match_intrinsic (void)
8245 gfc_clear_attr (&current_attr);
8246 current_attr.intrinsic = 1;
8248 return attr_decl ();
8252 match
8253 gfc_match_optional (void)
8255 /* This is not allowed within a BLOCK construct! */
8256 if (gfc_current_state () == COMP_BLOCK)
8258 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8259 return MATCH_ERROR;
8262 gfc_clear_attr (&current_attr);
8263 current_attr.optional = 1;
8265 return attr_decl ();
8269 match
8270 gfc_match_pointer (void)
8272 gfc_gobble_whitespace ();
8273 if (gfc_peek_ascii_char () == '(')
8275 if (!flag_cray_pointer)
8277 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
8278 "flag");
8279 return MATCH_ERROR;
8281 return cray_pointer_decl ();
8283 else
8285 gfc_clear_attr (&current_attr);
8286 current_attr.pointer = 1;
8288 return attr_decl ();
8293 match
8294 gfc_match_allocatable (void)
8296 gfc_clear_attr (&current_attr);
8297 current_attr.allocatable = 1;
8299 return attr_decl ();
8303 match
8304 gfc_match_codimension (void)
8306 gfc_clear_attr (&current_attr);
8307 current_attr.codimension = 1;
8309 return attr_decl ();
8313 match
8314 gfc_match_contiguous (void)
8316 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
8317 return MATCH_ERROR;
8319 gfc_clear_attr (&current_attr);
8320 current_attr.contiguous = 1;
8322 return attr_decl ();
8326 match
8327 gfc_match_dimension (void)
8329 gfc_clear_attr (&current_attr);
8330 current_attr.dimension = 1;
8332 return attr_decl ();
8336 match
8337 gfc_match_target (void)
8339 gfc_clear_attr (&current_attr);
8340 current_attr.target = 1;
8342 return attr_decl ();
8346 /* Match the list of entities being specified in a PUBLIC or PRIVATE
8347 statement. */
8349 static match
8350 access_attr_decl (gfc_statement st)
8352 char name[GFC_MAX_SYMBOL_LEN + 1];
8353 interface_type type;
8354 gfc_user_op *uop;
8355 gfc_symbol *sym, *dt_sym;
8356 gfc_intrinsic_op op;
8357 match m;
8359 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8360 goto done;
8362 for (;;)
8364 m = gfc_match_generic_spec (&type, name, &op);
8365 if (m == MATCH_NO)
8366 goto syntax;
8367 if (m == MATCH_ERROR)
8368 return MATCH_ERROR;
8370 switch (type)
8372 case INTERFACE_NAMELESS:
8373 case INTERFACE_ABSTRACT:
8374 goto syntax;
8376 case INTERFACE_GENERIC:
8377 case INTERFACE_DTIO:
8379 if (gfc_get_symbol (name, NULL, &sym))
8380 goto done;
8382 if (type == INTERFACE_DTIO
8383 && gfc_current_ns->proc_name
8384 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
8385 && sym->attr.flavor == FL_UNKNOWN)
8386 sym->attr.flavor = FL_PROCEDURE;
8388 if (!gfc_add_access (&sym->attr,
8389 (st == ST_PUBLIC)
8390 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8391 sym->name, NULL))
8392 return MATCH_ERROR;
8394 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
8395 && !gfc_add_access (&dt_sym->attr,
8396 (st == ST_PUBLIC)
8397 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8398 sym->name, NULL))
8399 return MATCH_ERROR;
8401 break;
8403 case INTERFACE_INTRINSIC_OP:
8404 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
8406 gfc_intrinsic_op other_op;
8408 gfc_current_ns->operator_access[op] =
8409 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8411 /* Handle the case if there is another op with the same
8412 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
8413 other_op = gfc_equivalent_op (op);
8415 if (other_op != INTRINSIC_NONE)
8416 gfc_current_ns->operator_access[other_op] =
8417 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8420 else
8422 gfc_error ("Access specification of the %s operator at %C has "
8423 "already been specified", gfc_op2string (op));
8424 goto done;
8427 break;
8429 case INTERFACE_USER_OP:
8430 uop = gfc_get_uop (name);
8432 if (uop->access == ACCESS_UNKNOWN)
8434 uop->access = (st == ST_PUBLIC)
8435 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8437 else
8439 gfc_error ("Access specification of the .%s. operator at %C "
8440 "has already been specified", sym->name);
8441 goto done;
8444 break;
8447 if (gfc_match_char (',') == MATCH_NO)
8448 break;
8451 if (gfc_match_eos () != MATCH_YES)
8452 goto syntax;
8453 return MATCH_YES;
8455 syntax:
8456 gfc_syntax_error (st);
8458 done:
8459 return MATCH_ERROR;
8463 match
8464 gfc_match_protected (void)
8466 gfc_symbol *sym;
8467 match m;
8469 if (!gfc_current_ns->proc_name
8470 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
8472 gfc_error ("PROTECTED at %C only allowed in specification "
8473 "part of a module");
8474 return MATCH_ERROR;
8478 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
8479 return MATCH_ERROR;
8481 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8483 return MATCH_ERROR;
8486 if (gfc_match_eos () == MATCH_YES)
8487 goto syntax;
8489 for(;;)
8491 m = gfc_match_symbol (&sym, 0);
8492 switch (m)
8494 case MATCH_YES:
8495 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
8496 return MATCH_ERROR;
8497 goto next_item;
8499 case MATCH_NO:
8500 break;
8502 case MATCH_ERROR:
8503 return MATCH_ERROR;
8506 next_item:
8507 if (gfc_match_eos () == MATCH_YES)
8508 break;
8509 if (gfc_match_char (',') != MATCH_YES)
8510 goto syntax;
8513 return MATCH_YES;
8515 syntax:
8516 gfc_error ("Syntax error in PROTECTED statement at %C");
8517 return MATCH_ERROR;
8521 /* The PRIVATE statement is a bit weird in that it can be an attribute
8522 declaration, but also works as a standalone statement inside of a
8523 type declaration or a module. */
8525 match
8526 gfc_match_private (gfc_statement *st)
8529 if (gfc_match ("private") != MATCH_YES)
8530 return MATCH_NO;
8532 if (gfc_current_state () != COMP_MODULE
8533 && !(gfc_current_state () == COMP_DERIVED
8534 && gfc_state_stack->previous
8535 && gfc_state_stack->previous->state == COMP_MODULE)
8536 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
8537 && gfc_state_stack->previous && gfc_state_stack->previous->previous
8538 && gfc_state_stack->previous->previous->state == COMP_MODULE))
8540 gfc_error ("PRIVATE statement at %C is only allowed in the "
8541 "specification part of a module");
8542 return MATCH_ERROR;
8545 if (gfc_current_state () == COMP_DERIVED)
8547 if (gfc_match_eos () == MATCH_YES)
8549 *st = ST_PRIVATE;
8550 return MATCH_YES;
8553 gfc_syntax_error (ST_PRIVATE);
8554 return MATCH_ERROR;
8557 if (gfc_match_eos () == MATCH_YES)
8559 *st = ST_PRIVATE;
8560 return MATCH_YES;
8563 *st = ST_ATTR_DECL;
8564 return access_attr_decl (ST_PRIVATE);
8568 match
8569 gfc_match_public (gfc_statement *st)
8572 if (gfc_match ("public") != MATCH_YES)
8573 return MATCH_NO;
8575 if (gfc_current_state () != COMP_MODULE)
8577 gfc_error ("PUBLIC statement at %C is only allowed in the "
8578 "specification part of a module");
8579 return MATCH_ERROR;
8582 if (gfc_match_eos () == MATCH_YES)
8584 *st = ST_PUBLIC;
8585 return MATCH_YES;
8588 *st = ST_ATTR_DECL;
8589 return access_attr_decl (ST_PUBLIC);
8593 /* Workhorse for gfc_match_parameter. */
8595 static match
8596 do_parm (void)
8598 gfc_symbol *sym;
8599 gfc_expr *init;
8600 match m;
8601 bool t;
8603 m = gfc_match_symbol (&sym, 0);
8604 if (m == MATCH_NO)
8605 gfc_error ("Expected variable name at %C in PARAMETER statement");
8607 if (m != MATCH_YES)
8608 return m;
8610 if (gfc_match_char ('=') == MATCH_NO)
8612 gfc_error ("Expected = sign in PARAMETER statement at %C");
8613 return MATCH_ERROR;
8616 m = gfc_match_init_expr (&init);
8617 if (m == MATCH_NO)
8618 gfc_error ("Expected expression at %C in PARAMETER statement");
8619 if (m != MATCH_YES)
8620 return m;
8622 if (sym->ts.type == BT_UNKNOWN
8623 && !gfc_set_default_type (sym, 1, NULL))
8625 m = MATCH_ERROR;
8626 goto cleanup;
8629 if (!gfc_check_assign_symbol (sym, NULL, init)
8630 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
8632 m = MATCH_ERROR;
8633 goto cleanup;
8636 if (sym->value)
8638 gfc_error ("Initializing already initialized variable at %C");
8639 m = MATCH_ERROR;
8640 goto cleanup;
8643 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
8644 return (t) ? MATCH_YES : MATCH_ERROR;
8646 cleanup:
8647 gfc_free_expr (init);
8648 return m;
8652 /* Match a parameter statement, with the weird syntax that these have. */
8654 match
8655 gfc_match_parameter (void)
8657 const char *term = " )%t";
8658 match m;
8660 if (gfc_match_char ('(') == MATCH_NO)
8662 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
8663 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
8664 return MATCH_NO;
8665 term = " %t";
8668 for (;;)
8670 m = do_parm ();
8671 if (m != MATCH_YES)
8672 break;
8674 if (gfc_match (term) == MATCH_YES)
8675 break;
8677 if (gfc_match_char (',') != MATCH_YES)
8679 gfc_error ("Unexpected characters in PARAMETER statement at %C");
8680 m = MATCH_ERROR;
8681 break;
8685 return m;
8689 match
8690 gfc_match_automatic (void)
8692 gfc_symbol *sym;
8693 match m;
8694 bool seen_symbol = false;
8696 if (!flag_dec_static)
8698 gfc_error ("%s at %C is a DEC extension, enable with "
8699 "%<-fdec-static%>",
8700 "AUTOMATIC"
8702 return MATCH_ERROR;
8705 gfc_match (" ::");
8707 for (;;)
8709 m = gfc_match_symbol (&sym, 0);
8710 switch (m)
8712 case MATCH_NO:
8713 break;
8715 case MATCH_ERROR:
8716 return MATCH_ERROR;
8718 case MATCH_YES:
8719 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
8720 return MATCH_ERROR;
8721 seen_symbol = true;
8722 break;
8725 if (gfc_match_eos () == MATCH_YES)
8726 break;
8727 if (gfc_match_char (',') != MATCH_YES)
8728 goto syntax;
8731 if (!seen_symbol)
8733 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
8734 return MATCH_ERROR;
8737 return MATCH_YES;
8739 syntax:
8740 gfc_error ("Syntax error in AUTOMATIC statement at %C");
8741 return MATCH_ERROR;
8745 match
8746 gfc_match_static (void)
8748 gfc_symbol *sym;
8749 match m;
8750 bool seen_symbol = false;
8752 if (!flag_dec_static)
8754 gfc_error ("%s at %C is a DEC extension, enable with "
8755 "%<-fdec-static%>",
8756 "STATIC");
8757 return MATCH_ERROR;
8760 gfc_match (" ::");
8762 for (;;)
8764 m = gfc_match_symbol (&sym, 0);
8765 switch (m)
8767 case MATCH_NO:
8768 break;
8770 case MATCH_ERROR:
8771 return MATCH_ERROR;
8773 case MATCH_YES:
8774 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
8775 &gfc_current_locus))
8776 return MATCH_ERROR;
8777 seen_symbol = true;
8778 break;
8781 if (gfc_match_eos () == MATCH_YES)
8782 break;
8783 if (gfc_match_char (',') != MATCH_YES)
8784 goto syntax;
8787 if (!seen_symbol)
8789 gfc_error ("Expected entity-list in STATIC statement at %C");
8790 return MATCH_ERROR;
8793 return MATCH_YES;
8795 syntax:
8796 gfc_error ("Syntax error in STATIC statement at %C");
8797 return MATCH_ERROR;
8801 /* Save statements have a special syntax. */
8803 match
8804 gfc_match_save (void)
8806 char n[GFC_MAX_SYMBOL_LEN+1];
8807 gfc_common_head *c;
8808 gfc_symbol *sym;
8809 match m;
8811 if (gfc_match_eos () == MATCH_YES)
8813 if (gfc_current_ns->seen_save)
8815 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
8816 "follows previous SAVE statement"))
8817 return MATCH_ERROR;
8820 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
8821 return MATCH_YES;
8824 if (gfc_current_ns->save_all)
8826 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
8827 "blanket SAVE statement"))
8828 return MATCH_ERROR;
8831 gfc_match (" ::");
8833 for (;;)
8835 m = gfc_match_symbol (&sym, 0);
8836 switch (m)
8838 case MATCH_YES:
8839 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
8840 &gfc_current_locus))
8841 return MATCH_ERROR;
8842 goto next_item;
8844 case MATCH_NO:
8845 break;
8847 case MATCH_ERROR:
8848 return MATCH_ERROR;
8851 m = gfc_match (" / %n /", &n);
8852 if (m == MATCH_ERROR)
8853 return MATCH_ERROR;
8854 if (m == MATCH_NO)
8855 goto syntax;
8857 c = gfc_get_common (n, 0);
8858 c->saved = 1;
8860 gfc_current_ns->seen_save = 1;
8862 next_item:
8863 if (gfc_match_eos () == MATCH_YES)
8864 break;
8865 if (gfc_match_char (',') != MATCH_YES)
8866 goto syntax;
8869 return MATCH_YES;
8871 syntax:
8872 gfc_error ("Syntax error in SAVE statement at %C");
8873 return MATCH_ERROR;
8877 match
8878 gfc_match_value (void)
8880 gfc_symbol *sym;
8881 match m;
8883 /* This is not allowed within a BLOCK construct! */
8884 if (gfc_current_state () == COMP_BLOCK)
8886 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
8887 return MATCH_ERROR;
8890 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
8891 return MATCH_ERROR;
8893 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8895 return MATCH_ERROR;
8898 if (gfc_match_eos () == MATCH_YES)
8899 goto syntax;
8901 for(;;)
8903 m = gfc_match_symbol (&sym, 0);
8904 switch (m)
8906 case MATCH_YES:
8907 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
8908 return MATCH_ERROR;
8909 goto next_item;
8911 case MATCH_NO:
8912 break;
8914 case MATCH_ERROR:
8915 return MATCH_ERROR;
8918 next_item:
8919 if (gfc_match_eos () == MATCH_YES)
8920 break;
8921 if (gfc_match_char (',') != MATCH_YES)
8922 goto syntax;
8925 return MATCH_YES;
8927 syntax:
8928 gfc_error ("Syntax error in VALUE statement at %C");
8929 return MATCH_ERROR;
8933 match
8934 gfc_match_volatile (void)
8936 gfc_symbol *sym;
8937 match m;
8939 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
8940 return MATCH_ERROR;
8942 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8944 return MATCH_ERROR;
8947 if (gfc_match_eos () == MATCH_YES)
8948 goto syntax;
8950 for(;;)
8952 /* VOLATILE is special because it can be added to host-associated
8953 symbols locally. Except for coarrays. */
8954 m = gfc_match_symbol (&sym, 1);
8955 switch (m)
8957 case MATCH_YES:
8958 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
8959 for variable in a BLOCK which is defined outside of the BLOCK. */
8960 if (sym->ns != gfc_current_ns && sym->attr.codimension)
8962 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
8963 "%C, which is use-/host-associated", sym->name);
8964 return MATCH_ERROR;
8966 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
8967 return MATCH_ERROR;
8968 goto next_item;
8970 case MATCH_NO:
8971 break;
8973 case MATCH_ERROR:
8974 return MATCH_ERROR;
8977 next_item:
8978 if (gfc_match_eos () == MATCH_YES)
8979 break;
8980 if (gfc_match_char (',') != MATCH_YES)
8981 goto syntax;
8984 return MATCH_YES;
8986 syntax:
8987 gfc_error ("Syntax error in VOLATILE statement at %C");
8988 return MATCH_ERROR;
8992 match
8993 gfc_match_asynchronous (void)
8995 gfc_symbol *sym;
8996 match m;
8998 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
8999 return MATCH_ERROR;
9001 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9003 return MATCH_ERROR;
9006 if (gfc_match_eos () == MATCH_YES)
9007 goto syntax;
9009 for(;;)
9011 /* ASYNCHRONOUS is special because it can be added to host-associated
9012 symbols locally. */
9013 m = gfc_match_symbol (&sym, 1);
9014 switch (m)
9016 case MATCH_YES:
9017 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9018 return MATCH_ERROR;
9019 goto next_item;
9021 case MATCH_NO:
9022 break;
9024 case MATCH_ERROR:
9025 return MATCH_ERROR;
9028 next_item:
9029 if (gfc_match_eos () == MATCH_YES)
9030 break;
9031 if (gfc_match_char (',') != MATCH_YES)
9032 goto syntax;
9035 return MATCH_YES;
9037 syntax:
9038 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9039 return MATCH_ERROR;
9043 /* Match a module procedure statement in a submodule. */
9045 match
9046 gfc_match_submod_proc (void)
9048 char name[GFC_MAX_SYMBOL_LEN + 1];
9049 gfc_symbol *sym, *fsym;
9050 match m;
9051 gfc_formal_arglist *formal, *head, *tail;
9053 if (gfc_current_state () != COMP_CONTAINS
9054 || !(gfc_state_stack->previous
9055 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9056 || gfc_state_stack->previous->state == COMP_MODULE)))
9057 return MATCH_NO;
9059 m = gfc_match (" module% procedure% %n", name);
9060 if (m != MATCH_YES)
9061 return m;
9063 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9064 "at %C"))
9065 return MATCH_ERROR;
9067 if (get_proc_name (name, &sym, false))
9068 return MATCH_ERROR;
9070 /* Make sure that the result field is appropriately filled, even though
9071 the result symbol will be replaced later on. */
9072 if (sym->tlink && sym->tlink->attr.function)
9074 if (sym->tlink->result
9075 && sym->tlink->result != sym->tlink)
9076 sym->result= sym->tlink->result;
9077 else
9078 sym->result = sym;
9081 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9082 the symbol existed before. */
9083 sym->declared_at = gfc_current_locus;
9085 if (!sym->attr.module_procedure)
9086 return MATCH_ERROR;
9088 /* Signal match_end to expect "end procedure". */
9089 sym->abr_modproc_decl = 1;
9091 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9092 sym->attr.if_source = IFSRC_DECL;
9094 gfc_new_block = sym;
9096 /* Make a new formal arglist with the symbols in the procedure
9097 namespace. */
9098 head = tail = NULL;
9099 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9101 if (formal == sym->formal)
9102 head = tail = gfc_get_formal_arglist ();
9103 else
9105 tail->next = gfc_get_formal_arglist ();
9106 tail = tail->next;
9109 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9110 goto cleanup;
9112 tail->sym = fsym;
9113 gfc_set_sym_referenced (fsym);
9116 /* The dummy symbols get cleaned up, when the formal_namespace of the
9117 interface declaration is cleared. This allows us to add the
9118 explicit interface as is done for other type of procedure. */
9119 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9120 &gfc_current_locus))
9121 return MATCH_ERROR;
9123 if (gfc_match_eos () != MATCH_YES)
9125 gfc_syntax_error (ST_MODULE_PROC);
9126 return MATCH_ERROR;
9129 return MATCH_YES;
9131 cleanup:
9132 gfc_free_formal_arglist (head);
9133 return MATCH_ERROR;
9137 /* Match a module procedure statement. Note that we have to modify
9138 symbols in the parent's namespace because the current one was there
9139 to receive symbols that are in an interface's formal argument list. */
9141 match
9142 gfc_match_modproc (void)
9144 char name[GFC_MAX_SYMBOL_LEN + 1];
9145 gfc_symbol *sym;
9146 match m;
9147 locus old_locus;
9148 gfc_namespace *module_ns;
9149 gfc_interface *old_interface_head, *interface;
9151 if (gfc_state_stack->state != COMP_INTERFACE
9152 || gfc_state_stack->previous == NULL
9153 || current_interface.type == INTERFACE_NAMELESS
9154 || current_interface.type == INTERFACE_ABSTRACT)
9156 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9157 "interface");
9158 return MATCH_ERROR;
9161 module_ns = gfc_current_ns->parent;
9162 for (; module_ns; module_ns = module_ns->parent)
9163 if (module_ns->proc_name->attr.flavor == FL_MODULE
9164 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9165 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9166 && !module_ns->proc_name->attr.contained))
9167 break;
9169 if (module_ns == NULL)
9170 return MATCH_ERROR;
9172 /* Store the current state of the interface. We will need it if we
9173 end up with a syntax error and need to recover. */
9174 old_interface_head = gfc_current_interface_head ();
9176 /* Check if the F2008 optional double colon appears. */
9177 gfc_gobble_whitespace ();
9178 old_locus = gfc_current_locus;
9179 if (gfc_match ("::") == MATCH_YES)
9181 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9182 "MODULE PROCEDURE statement at %L", &old_locus))
9183 return MATCH_ERROR;
9185 else
9186 gfc_current_locus = old_locus;
9188 for (;;)
9190 bool last = false;
9191 old_locus = gfc_current_locus;
9193 m = gfc_match_name (name);
9194 if (m == MATCH_NO)
9195 goto syntax;
9196 if (m != MATCH_YES)
9197 return MATCH_ERROR;
9199 /* Check for syntax error before starting to add symbols to the
9200 current namespace. */
9201 if (gfc_match_eos () == MATCH_YES)
9202 last = true;
9204 if (!last && gfc_match_char (',') != MATCH_YES)
9205 goto syntax;
9207 /* Now we're sure the syntax is valid, we process this item
9208 further. */
9209 if (gfc_get_symbol (name, module_ns, &sym))
9210 return MATCH_ERROR;
9212 if (sym->attr.intrinsic)
9214 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9215 "PROCEDURE", &old_locus);
9216 return MATCH_ERROR;
9219 if (sym->attr.proc != PROC_MODULE
9220 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9221 return MATCH_ERROR;
9223 if (!gfc_add_interface (sym))
9224 return MATCH_ERROR;
9226 sym->attr.mod_proc = 1;
9227 sym->declared_at = old_locus;
9229 if (last)
9230 break;
9233 return MATCH_YES;
9235 syntax:
9236 /* Restore the previous state of the interface. */
9237 interface = gfc_current_interface_head ();
9238 gfc_set_current_interface_head (old_interface_head);
9240 /* Free the new interfaces. */
9241 while (interface != old_interface_head)
9243 gfc_interface *i = interface->next;
9244 free (interface);
9245 interface = i;
9248 /* And issue a syntax error. */
9249 gfc_syntax_error (ST_MODULE_PROC);
9250 return MATCH_ERROR;
9254 /* Check a derived type that is being extended. */
9256 static gfc_symbol*
9257 check_extended_derived_type (char *name)
9259 gfc_symbol *extended;
9261 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9263 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9264 return NULL;
9267 extended = gfc_find_dt_in_generic (extended);
9269 /* F08:C428. */
9270 if (!extended)
9272 gfc_error ("Symbol %qs at %C has not been previously defined", name);
9273 return NULL;
9276 if (extended->attr.flavor != FL_DERIVED)
9278 gfc_error ("%qs in EXTENDS expression at %C is not a "
9279 "derived type", name);
9280 return NULL;
9283 if (extended->attr.is_bind_c)
9285 gfc_error ("%qs cannot be extended at %C because it "
9286 "is BIND(C)", extended->name);
9287 return NULL;
9290 if (extended->attr.sequence)
9292 gfc_error ("%qs cannot be extended at %C because it "
9293 "is a SEQUENCE type", extended->name);
9294 return NULL;
9297 return extended;
9301 /* Match the optional attribute specifiers for a type declaration.
9302 Return MATCH_ERROR if an error is encountered in one of the handled
9303 attributes (public, private, bind(c)), MATCH_NO if what's found is
9304 not a handled attribute, and MATCH_YES otherwise. TODO: More error
9305 checking on attribute conflicts needs to be done. */
9307 match
9308 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
9310 /* See if the derived type is marked as private. */
9311 if (gfc_match (" , private") == MATCH_YES)
9313 if (gfc_current_state () != COMP_MODULE)
9315 gfc_error ("Derived type at %C can only be PRIVATE in the "
9316 "specification part of a module");
9317 return MATCH_ERROR;
9320 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
9321 return MATCH_ERROR;
9323 else if (gfc_match (" , public") == MATCH_YES)
9325 if (gfc_current_state () != COMP_MODULE)
9327 gfc_error ("Derived type at %C can only be PUBLIC in the "
9328 "specification part of a module");
9329 return MATCH_ERROR;
9332 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
9333 return MATCH_ERROR;
9335 else if (gfc_match (" , bind ( c )") == MATCH_YES)
9337 /* If the type is defined to be bind(c) it then needs to make
9338 sure that all fields are interoperable. This will
9339 need to be a semantic check on the finished derived type.
9340 See 15.2.3 (lines 9-12) of F2003 draft. */
9341 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
9342 return MATCH_ERROR;
9344 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
9346 else if (gfc_match (" , abstract") == MATCH_YES)
9348 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
9349 return MATCH_ERROR;
9351 if (!gfc_add_abstract (attr, &gfc_current_locus))
9352 return MATCH_ERROR;
9354 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
9356 if (!gfc_add_extension (attr, &gfc_current_locus))
9357 return MATCH_ERROR;
9359 else
9360 return MATCH_NO;
9362 /* If we get here, something matched. */
9363 return MATCH_YES;
9367 /* Common function for type declaration blocks similar to derived types, such
9368 as STRUCTURES and MAPs. Unlike derived types, a structure type
9369 does NOT have a generic symbol matching the name given by the user.
9370 STRUCTUREs can share names with variables and PARAMETERs so we must allow
9371 for the creation of an independent symbol.
9372 Other parameters are a message to prefix errors with, the name of the new
9373 type to be created, and the flavor to add to the resulting symbol. */
9375 static bool
9376 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
9377 gfc_symbol **result)
9379 gfc_symbol *sym;
9380 locus where;
9382 gcc_assert (name[0] == (char) TOUPPER (name[0]));
9384 if (decl)
9385 where = *decl;
9386 else
9387 where = gfc_current_locus;
9389 if (gfc_get_symbol (name, NULL, &sym))
9390 return false;
9392 if (!sym)
9394 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
9395 return false;
9398 if (sym->components != NULL || sym->attr.zero_comp)
9400 gfc_error ("Type definition of %qs at %C was already defined at %L",
9401 sym->name, &sym->declared_at);
9402 return false;
9405 sym->declared_at = where;
9407 if (sym->attr.flavor != fl
9408 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
9409 return false;
9411 if (!sym->hash_value)
9412 /* Set the hash for the compound name for this type. */
9413 sym->hash_value = gfc_hash_value (sym);
9415 /* Normally the type is expected to have been completely parsed by the time
9416 a field declaration with this type is seen. For unions, maps, and nested
9417 structure declarations, we need to indicate that it is okay that we
9418 haven't seen any components yet. This will be updated after the structure
9419 is fully parsed. */
9420 sym->attr.zero_comp = 0;
9422 /* Structures always act like derived-types with the SEQUENCE attribute */
9423 gfc_add_sequence (&sym->attr, sym->name, NULL);
9425 if (result) *result = sym;
9427 return true;
9431 /* Match the opening of a MAP block. Like a struct within a union in C;
9432 behaves identical to STRUCTURE blocks. */
9434 match
9435 gfc_match_map (void)
9437 /* Counter used to give unique internal names to map structures. */
9438 static unsigned int gfc_map_id = 0;
9439 char name[GFC_MAX_SYMBOL_LEN + 1];
9440 gfc_symbol *sym;
9441 locus old_loc;
9443 old_loc = gfc_current_locus;
9445 if (gfc_match_eos () != MATCH_YES)
9447 gfc_error ("Junk after MAP statement at %C");
9448 gfc_current_locus = old_loc;
9449 return MATCH_ERROR;
9452 /* Map blocks are anonymous so we make up unique names for the symbol table
9453 which are invalid Fortran identifiers. */
9454 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
9456 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
9457 return MATCH_ERROR;
9459 gfc_new_block = sym;
9461 return MATCH_YES;
9465 /* Match the opening of a UNION block. */
9467 match
9468 gfc_match_union (void)
9470 /* Counter used to give unique internal names to union types. */
9471 static unsigned int gfc_union_id = 0;
9472 char name[GFC_MAX_SYMBOL_LEN + 1];
9473 gfc_symbol *sym;
9474 locus old_loc;
9476 old_loc = gfc_current_locus;
9478 if (gfc_match_eos () != MATCH_YES)
9480 gfc_error ("Junk after UNION statement at %C");
9481 gfc_current_locus = old_loc;
9482 return MATCH_ERROR;
9485 /* Unions are anonymous so we make up unique names for the symbol table
9486 which are invalid Fortran identifiers. */
9487 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
9489 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
9490 return MATCH_ERROR;
9492 gfc_new_block = sym;
9494 return MATCH_YES;
9498 /* Match the beginning of a STRUCTURE declaration. This is similar to
9499 matching the beginning of a derived type declaration with a few
9500 twists. The resulting type symbol has no access control or other
9501 interesting attributes. */
9503 match
9504 gfc_match_structure_decl (void)
9506 /* Counter used to give unique internal names to anonymous structures. */
9507 static unsigned int gfc_structure_id = 0;
9508 char name[GFC_MAX_SYMBOL_LEN + 1];
9509 gfc_symbol *sym;
9510 match m;
9511 locus where;
9513 if (!flag_dec_structure)
9515 gfc_error ("%s at %C is a DEC extension, enable with "
9516 "%<-fdec-structure%>",
9517 "STRUCTURE");
9518 return MATCH_ERROR;
9521 name[0] = '\0';
9523 m = gfc_match (" /%n/", name);
9524 if (m != MATCH_YES)
9526 /* Non-nested structure declarations require a structure name. */
9527 if (!gfc_comp_struct (gfc_current_state ()))
9529 gfc_error ("Structure name expected in non-nested structure "
9530 "declaration at %C");
9531 return MATCH_ERROR;
9533 /* This is an anonymous structure; make up a unique name for it
9534 (upper-case letters never make it to symbol names from the source).
9535 The important thing is initializing the type variable
9536 and setting gfc_new_symbol, which is immediately used by
9537 parse_structure () and variable_decl () to add components of
9538 this type. */
9539 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
9542 where = gfc_current_locus;
9543 /* No field list allowed after non-nested structure declaration. */
9544 if (!gfc_comp_struct (gfc_current_state ())
9545 && gfc_match_eos () != MATCH_YES)
9547 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
9548 return MATCH_ERROR;
9551 /* Make sure the name is not the name of an intrinsic type. */
9552 if (gfc_is_intrinsic_typename (name))
9554 gfc_error ("Structure name %qs at %C cannot be the same as an"
9555 " intrinsic type", name);
9556 return MATCH_ERROR;
9559 /* Store the actual type symbol for the structure with an upper-case first
9560 letter (an invalid Fortran identifier). */
9562 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
9563 return MATCH_ERROR;
9565 gfc_new_block = sym;
9566 return MATCH_YES;
9570 /* This function does some work to determine which matcher should be used to
9571 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
9572 * as an alias for PRINT from derived type declarations, TYPE IS statements,
9573 * and derived type data declarations. */
9575 match
9576 gfc_match_type (gfc_statement *st)
9578 char name[GFC_MAX_SYMBOL_LEN + 1];
9579 match m;
9580 locus old_loc;
9582 /* Requires -fdec. */
9583 if (!flag_dec)
9584 return MATCH_NO;
9586 m = gfc_match ("type");
9587 if (m != MATCH_YES)
9588 return m;
9589 /* If we already have an error in the buffer, it is probably from failing to
9590 * match a derived type data declaration. Let it happen. */
9591 else if (gfc_error_flag_test ())
9592 return MATCH_NO;
9594 old_loc = gfc_current_locus;
9595 *st = ST_NONE;
9597 /* If we see an attribute list before anything else it's definitely a derived
9598 * type declaration. */
9599 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
9601 gfc_current_locus = old_loc;
9602 *st = ST_DERIVED_DECL;
9603 return gfc_match_derived_decl ();
9606 /* By now "TYPE" has already been matched. If we do not see a name, this may
9607 * be something like "TYPE *" or "TYPE <fmt>". */
9608 m = gfc_match_name (name);
9609 if (m != MATCH_YES)
9611 /* Let print match if it can, otherwise throw an error from
9612 * gfc_match_derived_decl. */
9613 gfc_current_locus = old_loc;
9614 if (gfc_match_print () == MATCH_YES)
9616 *st = ST_WRITE;
9617 return MATCH_YES;
9619 gfc_current_locus = old_loc;
9620 *st = ST_DERIVED_DECL;
9621 return gfc_match_derived_decl ();
9624 /* A derived type declaration requires an EOS. Without it, assume print. */
9625 m = gfc_match_eos ();
9626 if (m == MATCH_NO)
9628 /* Check manually for TYPE IS (... - this is invalid print syntax. */
9629 if (strncmp ("is", name, 3) == 0
9630 && gfc_match (" (", name) == MATCH_YES)
9632 gfc_current_locus = old_loc;
9633 gcc_assert (gfc_match (" is") == MATCH_YES);
9634 *st = ST_TYPE_IS;
9635 return gfc_match_type_is ();
9637 gfc_current_locus = old_loc;
9638 *st = ST_WRITE;
9639 return gfc_match_print ();
9641 else
9643 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
9644 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
9645 * Otherwise if gfc_match_derived_decl fails it's probably an existing
9646 * symbol which can be printed. */
9647 gfc_current_locus = old_loc;
9648 m = gfc_match_derived_decl ();
9649 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
9651 *st = ST_DERIVED_DECL;
9652 return m;
9654 gfc_current_locus = old_loc;
9655 *st = ST_WRITE;
9656 return gfc_match_print ();
9659 return MATCH_NO;
9663 /* Match the beginning of a derived type declaration. If a type name
9664 was the result of a function, then it is possible to have a symbol
9665 already to be known as a derived type yet have no components. */
9667 match
9668 gfc_match_derived_decl (void)
9670 char name[GFC_MAX_SYMBOL_LEN + 1];
9671 char parent[GFC_MAX_SYMBOL_LEN + 1];
9672 symbol_attribute attr;
9673 gfc_symbol *sym, *gensym;
9674 gfc_symbol *extended;
9675 match m;
9676 match is_type_attr_spec = MATCH_NO;
9677 bool seen_attr = false;
9678 gfc_interface *intr = NULL, *head;
9679 bool parameterized_type = false;
9680 bool seen_colons = false;
9682 if (gfc_comp_struct (gfc_current_state ()))
9683 return MATCH_NO;
9685 name[0] = '\0';
9686 parent[0] = '\0';
9687 gfc_clear_attr (&attr);
9688 extended = NULL;
9692 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
9693 if (is_type_attr_spec == MATCH_ERROR)
9694 return MATCH_ERROR;
9695 if (is_type_attr_spec == MATCH_YES)
9696 seen_attr = true;
9697 } while (is_type_attr_spec == MATCH_YES);
9699 /* Deal with derived type extensions. The extension attribute has
9700 been added to 'attr' but now the parent type must be found and
9701 checked. */
9702 if (parent[0])
9703 extended = check_extended_derived_type (parent);
9705 if (parent[0] && !extended)
9706 return MATCH_ERROR;
9708 m = gfc_match (" ::");
9709 if (m == MATCH_YES)
9711 seen_colons = true;
9713 else if (seen_attr)
9715 gfc_error ("Expected :: in TYPE definition at %C");
9716 return MATCH_ERROR;
9719 m = gfc_match (" %n ", name);
9720 if (m != MATCH_YES)
9721 return m;
9723 /* Make sure that we don't identify TYPE IS (...) as a parameterized
9724 derived type named 'is'.
9725 TODO Expand the check, when 'name' = "is" by matching " (tname) "
9726 and checking if this is a(n intrinsic) typename. his picks up
9727 misplaced TYPE IS statements such as in select_type_1.f03. */
9728 if (gfc_peek_ascii_char () == '(')
9730 if (gfc_current_state () == COMP_SELECT_TYPE
9731 || (!seen_colons && !strcmp (name, "is")))
9732 return MATCH_NO;
9733 parameterized_type = true;
9736 m = gfc_match_eos ();
9737 if (m != MATCH_YES && !parameterized_type)
9738 return m;
9740 /* Make sure the name is not the name of an intrinsic type. */
9741 if (gfc_is_intrinsic_typename (name))
9743 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
9744 "type", name);
9745 return MATCH_ERROR;
9748 if (gfc_get_symbol (name, NULL, &gensym))
9749 return MATCH_ERROR;
9751 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
9753 gfc_error ("Derived type name %qs at %C already has a basic type "
9754 "of %s", gensym->name, gfc_typename (&gensym->ts));
9755 return MATCH_ERROR;
9758 if (!gensym->attr.generic
9759 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
9760 return MATCH_ERROR;
9762 if (!gensym->attr.function
9763 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
9764 return MATCH_ERROR;
9766 sym = gfc_find_dt_in_generic (gensym);
9768 if (sym && (sym->components != NULL || sym->attr.zero_comp))
9770 gfc_error ("Derived type definition of %qs at %C has already been "
9771 "defined", sym->name);
9772 return MATCH_ERROR;
9775 if (!sym)
9777 /* Use upper case to save the actual derived-type symbol. */
9778 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
9779 sym->name = gfc_get_string ("%s", gensym->name);
9780 head = gensym->generic;
9781 intr = gfc_get_interface ();
9782 intr->sym = sym;
9783 intr->where = gfc_current_locus;
9784 intr->sym->declared_at = gfc_current_locus;
9785 intr->next = head;
9786 gensym->generic = intr;
9787 gensym->attr.if_source = IFSRC_DECL;
9790 /* The symbol may already have the derived attribute without the
9791 components. The ways this can happen is via a function
9792 definition, an INTRINSIC statement or a subtype in another
9793 derived type that is a pointer. The first part of the AND clause
9794 is true if the symbol is not the return value of a function. */
9795 if (sym->attr.flavor != FL_DERIVED
9796 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
9797 return MATCH_ERROR;
9799 if (attr.access != ACCESS_UNKNOWN
9800 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
9801 return MATCH_ERROR;
9802 else if (sym->attr.access == ACCESS_UNKNOWN
9803 && gensym->attr.access != ACCESS_UNKNOWN
9804 && !gfc_add_access (&sym->attr, gensym->attr.access,
9805 sym->name, NULL))
9806 return MATCH_ERROR;
9808 if (sym->attr.access != ACCESS_UNKNOWN
9809 && gensym->attr.access == ACCESS_UNKNOWN)
9810 gensym->attr.access = sym->attr.access;
9812 /* See if the derived type was labeled as bind(c). */
9813 if (attr.is_bind_c != 0)
9814 sym->attr.is_bind_c = attr.is_bind_c;
9816 /* Construct the f2k_derived namespace if it is not yet there. */
9817 if (!sym->f2k_derived)
9818 sym->f2k_derived = gfc_get_namespace (NULL, 0);
9820 if (parameterized_type)
9822 /* Ignore error or mismatches to avoid the component declarations
9823 causing problems later. */
9824 gfc_match_formal_arglist (sym, 0, 0, true);
9825 m = gfc_match_eos ();
9826 if (m != MATCH_YES)
9827 return m;
9828 sym->attr.pdt_template = 1;
9831 if (extended && !sym->components)
9833 gfc_component *p;
9834 gfc_formal_arglist *f, *g, *h;
9836 /* Add the extended derived type as the first component. */
9837 gfc_add_component (sym, parent, &p);
9838 extended->refs++;
9839 gfc_set_sym_referenced (extended);
9841 p->ts.type = BT_DERIVED;
9842 p->ts.u.derived = extended;
9843 p->initializer = gfc_default_initializer (&p->ts);
9845 /* Set extension level. */
9846 if (extended->attr.extension == 255)
9848 /* Since the extension field is 8 bit wide, we can only have
9849 up to 255 extension levels. */
9850 gfc_error ("Maximum extension level reached with type %qs at %L",
9851 extended->name, &extended->declared_at);
9852 return MATCH_ERROR;
9854 sym->attr.extension = extended->attr.extension + 1;
9856 /* Provide the links between the extended type and its extension. */
9857 if (!extended->f2k_derived)
9858 extended->f2k_derived = gfc_get_namespace (NULL, 0);
9860 /* Copy the extended type-param-name-list from the extended type,
9861 append those of the extension and add the whole lot to the
9862 extension. */
9863 if (extended->attr.pdt_template)
9865 g = h = NULL;
9866 sym->attr.pdt_template = 1;
9867 for (f = extended->formal; f; f = f->next)
9869 if (f == extended->formal)
9871 g = gfc_get_formal_arglist ();
9872 h = g;
9874 else
9876 g->next = gfc_get_formal_arglist ();
9877 g = g->next;
9879 g->sym = f->sym;
9881 g->next = sym->formal;
9882 sym->formal = h;
9886 if (!sym->hash_value)
9887 /* Set the hash for the compound name for this type. */
9888 sym->hash_value = gfc_hash_value (sym);
9890 /* Take over the ABSTRACT attribute. */
9891 sym->attr.abstract = attr.abstract;
9893 gfc_new_block = sym;
9895 return MATCH_YES;
9899 /* Cray Pointees can be declared as:
9900 pointer (ipt, a (n,m,...,*)) */
9902 match
9903 gfc_mod_pointee_as (gfc_array_spec *as)
9905 as->cray_pointee = true; /* This will be useful to know later. */
9906 if (as->type == AS_ASSUMED_SIZE)
9907 as->cp_was_assumed = true;
9908 else if (as->type == AS_ASSUMED_SHAPE)
9910 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
9911 return MATCH_ERROR;
9913 return MATCH_YES;
9917 /* Match the enum definition statement, here we are trying to match
9918 the first line of enum definition statement.
9919 Returns MATCH_YES if match is found. */
9921 match
9922 gfc_match_enum (void)
9924 match m;
9926 m = gfc_match_eos ();
9927 if (m != MATCH_YES)
9928 return m;
9930 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
9931 return MATCH_ERROR;
9933 return MATCH_YES;
9937 /* Returns an initializer whose value is one higher than the value of the
9938 LAST_INITIALIZER argument. If the argument is NULL, the
9939 initializers value will be set to zero. The initializer's kind
9940 will be set to gfc_c_int_kind.
9942 If -fshort-enums is given, the appropriate kind will be selected
9943 later after all enumerators have been parsed. A warning is issued
9944 here if an initializer exceeds gfc_c_int_kind. */
9946 static gfc_expr *
9947 enum_initializer (gfc_expr *last_initializer, locus where)
9949 gfc_expr *result;
9950 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
9952 mpz_init (result->value.integer);
9954 if (last_initializer != NULL)
9956 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
9957 result->where = last_initializer->where;
9959 if (gfc_check_integer_range (result->value.integer,
9960 gfc_c_int_kind) != ARITH_OK)
9962 gfc_error ("Enumerator exceeds the C integer type at %C");
9963 return NULL;
9966 else
9968 /* Control comes here, if it's the very first enumerator and no
9969 initializer has been given. It will be initialized to zero. */
9970 mpz_set_si (result->value.integer, 0);
9973 return result;
9977 /* Match a variable name with an optional initializer. When this
9978 subroutine is called, a variable is expected to be parsed next.
9979 Depending on what is happening at the moment, updates either the
9980 symbol table or the current interface. */
9982 static match
9983 enumerator_decl (void)
9985 char name[GFC_MAX_SYMBOL_LEN + 1];
9986 gfc_expr *initializer;
9987 gfc_array_spec *as = NULL;
9988 gfc_symbol *sym;
9989 locus var_locus;
9990 match m;
9991 bool t;
9992 locus old_locus;
9994 initializer = NULL;
9995 old_locus = gfc_current_locus;
9997 /* When we get here, we've just matched a list of attributes and
9998 maybe a type and a double colon. The next thing we expect to see
9999 is the name of the symbol. */
10000 m = gfc_match_name (name);
10001 if (m != MATCH_YES)
10002 goto cleanup;
10004 var_locus = gfc_current_locus;
10006 /* OK, we've successfully matched the declaration. Now put the
10007 symbol in the current namespace. If we fail to create the symbol,
10008 bail out. */
10009 if (!build_sym (name, NULL, false, &as, &var_locus))
10011 m = MATCH_ERROR;
10012 goto cleanup;
10015 /* The double colon must be present in order to have initializers.
10016 Otherwise the statement is ambiguous with an assignment statement. */
10017 if (colon_seen)
10019 if (gfc_match_char ('=') == MATCH_YES)
10021 m = gfc_match_init_expr (&initializer);
10022 if (m == MATCH_NO)
10024 gfc_error ("Expected an initialization expression at %C");
10025 m = MATCH_ERROR;
10028 if (m != MATCH_YES)
10029 goto cleanup;
10033 /* If we do not have an initializer, the initialization value of the
10034 previous enumerator (stored in last_initializer) is incremented
10035 by 1 and is used to initialize the current enumerator. */
10036 if (initializer == NULL)
10037 initializer = enum_initializer (last_initializer, old_locus);
10039 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10041 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10042 &var_locus);
10043 m = MATCH_ERROR;
10044 goto cleanup;
10047 /* Store this current initializer, for the next enumerator variable
10048 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10049 use last_initializer below. */
10050 last_initializer = initializer;
10051 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10053 /* Maintain enumerator history. */
10054 gfc_find_symbol (name, NULL, 0, &sym);
10055 create_enum_history (sym, last_initializer);
10057 return (t) ? MATCH_YES : MATCH_ERROR;
10059 cleanup:
10060 /* Free stuff up and return. */
10061 gfc_free_expr (initializer);
10063 return m;
10067 /* Match the enumerator definition statement. */
10069 match
10070 gfc_match_enumerator_def (void)
10072 match m;
10073 bool t;
10075 gfc_clear_ts (&current_ts);
10077 m = gfc_match (" enumerator");
10078 if (m != MATCH_YES)
10079 return m;
10081 m = gfc_match (" :: ");
10082 if (m == MATCH_ERROR)
10083 return m;
10085 colon_seen = (m == MATCH_YES);
10087 if (gfc_current_state () != COMP_ENUM)
10089 gfc_error ("ENUM definition statement expected before %C");
10090 gfc_free_enum_history ();
10091 return MATCH_ERROR;
10094 (&current_ts)->type = BT_INTEGER;
10095 (&current_ts)->kind = gfc_c_int_kind;
10097 gfc_clear_attr (&current_attr);
10098 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10099 if (!t)
10101 m = MATCH_ERROR;
10102 goto cleanup;
10105 for (;;)
10107 m = enumerator_decl ();
10108 if (m == MATCH_ERROR)
10110 gfc_free_enum_history ();
10111 goto cleanup;
10113 if (m == MATCH_NO)
10114 break;
10116 if (gfc_match_eos () == MATCH_YES)
10117 goto cleanup;
10118 if (gfc_match_char (',') != MATCH_YES)
10119 break;
10122 if (gfc_current_state () == COMP_ENUM)
10124 gfc_free_enum_history ();
10125 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10126 m = MATCH_ERROR;
10129 cleanup:
10130 gfc_free_array_spec (current_as);
10131 current_as = NULL;
10132 return m;
10137 /* Match binding attributes. */
10139 static match
10140 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10142 bool found_passing = false;
10143 bool seen_ptr = false;
10144 match m = MATCH_YES;
10146 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
10147 this case the defaults are in there. */
10148 ba->access = ACCESS_UNKNOWN;
10149 ba->pass_arg = NULL;
10150 ba->pass_arg_num = 0;
10151 ba->nopass = 0;
10152 ba->non_overridable = 0;
10153 ba->deferred = 0;
10154 ba->ppc = ppc;
10156 /* If we find a comma, we believe there are binding attributes. */
10157 m = gfc_match_char (',');
10158 if (m == MATCH_NO)
10159 goto done;
10163 /* Access specifier. */
10165 m = gfc_match (" public");
10166 if (m == MATCH_ERROR)
10167 goto error;
10168 if (m == MATCH_YES)
10170 if (ba->access != ACCESS_UNKNOWN)
10172 gfc_error ("Duplicate access-specifier at %C");
10173 goto error;
10176 ba->access = ACCESS_PUBLIC;
10177 continue;
10180 m = gfc_match (" private");
10181 if (m == MATCH_ERROR)
10182 goto error;
10183 if (m == MATCH_YES)
10185 if (ba->access != ACCESS_UNKNOWN)
10187 gfc_error ("Duplicate access-specifier at %C");
10188 goto error;
10191 ba->access = ACCESS_PRIVATE;
10192 continue;
10195 /* If inside GENERIC, the following is not allowed. */
10196 if (!generic)
10199 /* NOPASS flag. */
10200 m = gfc_match (" nopass");
10201 if (m == MATCH_ERROR)
10202 goto error;
10203 if (m == MATCH_YES)
10205 if (found_passing)
10207 gfc_error ("Binding attributes already specify passing,"
10208 " illegal NOPASS at %C");
10209 goto error;
10212 found_passing = true;
10213 ba->nopass = 1;
10214 continue;
10217 /* PASS possibly including argument. */
10218 m = gfc_match (" pass");
10219 if (m == MATCH_ERROR)
10220 goto error;
10221 if (m == MATCH_YES)
10223 char arg[GFC_MAX_SYMBOL_LEN + 1];
10225 if (found_passing)
10227 gfc_error ("Binding attributes already specify passing,"
10228 " illegal PASS at %C");
10229 goto error;
10232 m = gfc_match (" ( %n )", arg);
10233 if (m == MATCH_ERROR)
10234 goto error;
10235 if (m == MATCH_YES)
10236 ba->pass_arg = gfc_get_string ("%s", arg);
10237 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10239 found_passing = true;
10240 ba->nopass = 0;
10241 continue;
10244 if (ppc)
10246 /* POINTER flag. */
10247 m = gfc_match (" pointer");
10248 if (m == MATCH_ERROR)
10249 goto error;
10250 if (m == MATCH_YES)
10252 if (seen_ptr)
10254 gfc_error ("Duplicate POINTER attribute at %C");
10255 goto error;
10258 seen_ptr = true;
10259 continue;
10262 else
10264 /* NON_OVERRIDABLE flag. */
10265 m = gfc_match (" non_overridable");
10266 if (m == MATCH_ERROR)
10267 goto error;
10268 if (m == MATCH_YES)
10270 if (ba->non_overridable)
10272 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
10273 goto error;
10276 ba->non_overridable = 1;
10277 continue;
10280 /* DEFERRED flag. */
10281 m = gfc_match (" deferred");
10282 if (m == MATCH_ERROR)
10283 goto error;
10284 if (m == MATCH_YES)
10286 if (ba->deferred)
10288 gfc_error ("Duplicate DEFERRED at %C");
10289 goto error;
10292 ba->deferred = 1;
10293 continue;
10299 /* Nothing matching found. */
10300 if (generic)
10301 gfc_error ("Expected access-specifier at %C");
10302 else
10303 gfc_error ("Expected binding attribute at %C");
10304 goto error;
10306 while (gfc_match_char (',') == MATCH_YES);
10308 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
10309 if (ba->non_overridable && ba->deferred)
10311 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
10312 goto error;
10315 m = MATCH_YES;
10317 done:
10318 if (ba->access == ACCESS_UNKNOWN)
10319 ba->access = gfc_typebound_default_access;
10321 if (ppc && !seen_ptr)
10323 gfc_error ("POINTER attribute is required for procedure pointer component"
10324 " at %C");
10325 goto error;
10328 return m;
10330 error:
10331 return MATCH_ERROR;
10335 /* Match a PROCEDURE specific binding inside a derived type. */
10337 static match
10338 match_procedure_in_type (void)
10340 char name[GFC_MAX_SYMBOL_LEN + 1];
10341 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
10342 char* target = NULL, *ifc = NULL;
10343 gfc_typebound_proc tb;
10344 bool seen_colons;
10345 bool seen_attrs;
10346 match m;
10347 gfc_symtree* stree;
10348 gfc_namespace* ns;
10349 gfc_symbol* block;
10350 int num;
10352 /* Check current state. */
10353 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
10354 block = gfc_state_stack->previous->sym;
10355 gcc_assert (block);
10357 /* Try to match PROCEDURE(interface). */
10358 if (gfc_match (" (") == MATCH_YES)
10360 m = gfc_match_name (target_buf);
10361 if (m == MATCH_ERROR)
10362 return m;
10363 if (m != MATCH_YES)
10365 gfc_error ("Interface-name expected after %<(%> at %C");
10366 return MATCH_ERROR;
10369 if (gfc_match (" )") != MATCH_YES)
10371 gfc_error ("%<)%> expected at %C");
10372 return MATCH_ERROR;
10375 ifc = target_buf;
10378 /* Construct the data structure. */
10379 memset (&tb, 0, sizeof (tb));
10380 tb.where = gfc_current_locus;
10382 /* Match binding attributes. */
10383 m = match_binding_attributes (&tb, false, false);
10384 if (m == MATCH_ERROR)
10385 return m;
10386 seen_attrs = (m == MATCH_YES);
10388 /* Check that attribute DEFERRED is given if an interface is specified. */
10389 if (tb.deferred && !ifc)
10391 gfc_error ("Interface must be specified for DEFERRED binding at %C");
10392 return MATCH_ERROR;
10394 if (ifc && !tb.deferred)
10396 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
10397 return MATCH_ERROR;
10400 /* Match the colons. */
10401 m = gfc_match (" ::");
10402 if (m == MATCH_ERROR)
10403 return m;
10404 seen_colons = (m == MATCH_YES);
10405 if (seen_attrs && !seen_colons)
10407 gfc_error ("Expected %<::%> after binding-attributes at %C");
10408 return MATCH_ERROR;
10411 /* Match the binding names. */
10412 for(num=1;;num++)
10414 m = gfc_match_name (name);
10415 if (m == MATCH_ERROR)
10416 return m;
10417 if (m == MATCH_NO)
10419 gfc_error ("Expected binding name at %C");
10420 return MATCH_ERROR;
10423 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
10424 return MATCH_ERROR;
10426 /* Try to match the '=> target', if it's there. */
10427 target = ifc;
10428 m = gfc_match (" =>");
10429 if (m == MATCH_ERROR)
10430 return m;
10431 if (m == MATCH_YES)
10433 if (tb.deferred)
10435 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
10436 return MATCH_ERROR;
10439 if (!seen_colons)
10441 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
10442 " at %C");
10443 return MATCH_ERROR;
10446 m = gfc_match_name (target_buf);
10447 if (m == MATCH_ERROR)
10448 return m;
10449 if (m == MATCH_NO)
10451 gfc_error ("Expected binding target after %<=>%> at %C");
10452 return MATCH_ERROR;
10454 target = target_buf;
10457 /* If no target was found, it has the same name as the binding. */
10458 if (!target)
10459 target = name;
10461 /* Get the namespace to insert the symbols into. */
10462 ns = block->f2k_derived;
10463 gcc_assert (ns);
10465 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
10466 if (tb.deferred && !block->attr.abstract)
10468 gfc_error ("Type %qs containing DEFERRED binding at %C "
10469 "is not ABSTRACT", block->name);
10470 return MATCH_ERROR;
10473 /* See if we already have a binding with this name in the symtree which
10474 would be an error. If a GENERIC already targeted this binding, it may
10475 be already there but then typebound is still NULL. */
10476 stree = gfc_find_symtree (ns->tb_sym_root, name);
10477 if (stree && stree->n.tb)
10479 gfc_error ("There is already a procedure with binding name %qs for "
10480 "the derived type %qs at %C", name, block->name);
10481 return MATCH_ERROR;
10484 /* Insert it and set attributes. */
10486 if (!stree)
10488 stree = gfc_new_symtree (&ns->tb_sym_root, name);
10489 gcc_assert (stree);
10491 stree->n.tb = gfc_get_typebound_proc (&tb);
10493 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
10494 false))
10495 return MATCH_ERROR;
10496 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
10497 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
10498 target, &stree->n.tb->u.specific->n.sym->declared_at);
10500 if (gfc_match_eos () == MATCH_YES)
10501 return MATCH_YES;
10502 if (gfc_match_char (',') != MATCH_YES)
10503 goto syntax;
10506 syntax:
10507 gfc_error ("Syntax error in PROCEDURE statement at %C");
10508 return MATCH_ERROR;
10512 /* Match a GENERIC procedure binding inside a derived type. */
10514 match
10515 gfc_match_generic (void)
10517 char name[GFC_MAX_SYMBOL_LEN + 1];
10518 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
10519 gfc_symbol* block;
10520 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
10521 gfc_typebound_proc* tb;
10522 gfc_namespace* ns;
10523 interface_type op_type;
10524 gfc_intrinsic_op op;
10525 match m;
10527 /* Check current state. */
10528 if (gfc_current_state () == COMP_DERIVED)
10530 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
10531 return MATCH_ERROR;
10533 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
10534 return MATCH_NO;
10535 block = gfc_state_stack->previous->sym;
10536 ns = block->f2k_derived;
10537 gcc_assert (block && ns);
10539 memset (&tbattr, 0, sizeof (tbattr));
10540 tbattr.where = gfc_current_locus;
10542 /* See if we get an access-specifier. */
10543 m = match_binding_attributes (&tbattr, true, false);
10544 if (m == MATCH_ERROR)
10545 goto error;
10547 /* Now the colons, those are required. */
10548 if (gfc_match (" ::") != MATCH_YES)
10550 gfc_error ("Expected %<::%> at %C");
10551 goto error;
10554 /* Match the binding name; depending on type (operator / generic) format
10555 it for future error messages into bind_name. */
10557 m = gfc_match_generic_spec (&op_type, name, &op);
10558 if (m == MATCH_ERROR)
10559 return MATCH_ERROR;
10560 if (m == MATCH_NO)
10562 gfc_error ("Expected generic name or operator descriptor at %C");
10563 goto error;
10566 switch (op_type)
10568 case INTERFACE_GENERIC:
10569 case INTERFACE_DTIO:
10570 snprintf (bind_name, sizeof (bind_name), "%s", name);
10571 break;
10573 case INTERFACE_USER_OP:
10574 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
10575 break;
10577 case INTERFACE_INTRINSIC_OP:
10578 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
10579 gfc_op2string (op));
10580 break;
10582 case INTERFACE_NAMELESS:
10583 gfc_error ("Malformed GENERIC statement at %C");
10584 goto error;
10585 break;
10587 default:
10588 gcc_unreachable ();
10591 /* Match the required =>. */
10592 if (gfc_match (" =>") != MATCH_YES)
10594 gfc_error ("Expected %<=>%> at %C");
10595 goto error;
10598 /* Try to find existing GENERIC binding with this name / for this operator;
10599 if there is something, check that it is another GENERIC and then extend
10600 it rather than building a new node. Otherwise, create it and put it
10601 at the right position. */
10603 switch (op_type)
10605 case INTERFACE_DTIO:
10606 case INTERFACE_USER_OP:
10607 case INTERFACE_GENERIC:
10609 const bool is_op = (op_type == INTERFACE_USER_OP);
10610 gfc_symtree* st;
10612 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
10613 tb = st ? st->n.tb : NULL;
10614 break;
10617 case INTERFACE_INTRINSIC_OP:
10618 tb = ns->tb_op[op];
10619 break;
10621 default:
10622 gcc_unreachable ();
10625 if (tb)
10627 if (!tb->is_generic)
10629 gcc_assert (op_type == INTERFACE_GENERIC);
10630 gfc_error ("There's already a non-generic procedure with binding name"
10631 " %qs for the derived type %qs at %C",
10632 bind_name, block->name);
10633 goto error;
10636 if (tb->access != tbattr.access)
10638 gfc_error ("Binding at %C must have the same access as already"
10639 " defined binding %qs", bind_name);
10640 goto error;
10643 else
10645 tb = gfc_get_typebound_proc (NULL);
10646 tb->where = gfc_current_locus;
10647 tb->access = tbattr.access;
10648 tb->is_generic = 1;
10649 tb->u.generic = NULL;
10651 switch (op_type)
10653 case INTERFACE_DTIO:
10654 case INTERFACE_GENERIC:
10655 case INTERFACE_USER_OP:
10657 const bool is_op = (op_type == INTERFACE_USER_OP);
10658 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
10659 &ns->tb_sym_root, name);
10660 gcc_assert (st);
10661 st->n.tb = tb;
10663 break;
10666 case INTERFACE_INTRINSIC_OP:
10667 ns->tb_op[op] = tb;
10668 break;
10670 default:
10671 gcc_unreachable ();
10675 /* Now, match all following names as specific targets. */
10678 gfc_symtree* target_st;
10679 gfc_tbp_generic* target;
10681 m = gfc_match_name (name);
10682 if (m == MATCH_ERROR)
10683 goto error;
10684 if (m == MATCH_NO)
10686 gfc_error ("Expected specific binding name at %C");
10687 goto error;
10690 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
10692 /* See if this is a duplicate specification. */
10693 for (target = tb->u.generic; target; target = target->next)
10694 if (target_st == target->specific_st)
10696 gfc_error ("%qs already defined as specific binding for the"
10697 " generic %qs at %C", name, bind_name);
10698 goto error;
10701 target = gfc_get_tbp_generic ();
10702 target->specific_st = target_st;
10703 target->specific = NULL;
10704 target->next = tb->u.generic;
10705 target->is_operator = ((op_type == INTERFACE_USER_OP)
10706 || (op_type == INTERFACE_INTRINSIC_OP));
10707 tb->u.generic = target;
10709 while (gfc_match (" ,") == MATCH_YES);
10711 /* Here should be the end. */
10712 if (gfc_match_eos () != MATCH_YES)
10714 gfc_error ("Junk after GENERIC binding at %C");
10715 goto error;
10718 return MATCH_YES;
10720 error:
10721 return MATCH_ERROR;
10725 /* Match a FINAL declaration inside a derived type. */
10727 match
10728 gfc_match_final_decl (void)
10730 char name[GFC_MAX_SYMBOL_LEN + 1];
10731 gfc_symbol* sym;
10732 match m;
10733 gfc_namespace* module_ns;
10734 bool first, last;
10735 gfc_symbol* block;
10737 if (gfc_current_form == FORM_FREE)
10739 char c = gfc_peek_ascii_char ();
10740 if (!gfc_is_whitespace (c) && c != ':')
10741 return MATCH_NO;
10744 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
10746 if (gfc_current_form == FORM_FIXED)
10747 return MATCH_NO;
10749 gfc_error ("FINAL declaration at %C must be inside a derived type "
10750 "CONTAINS section");
10751 return MATCH_ERROR;
10754 block = gfc_state_stack->previous->sym;
10755 gcc_assert (block);
10757 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
10758 || gfc_state_stack->previous->previous->state != COMP_MODULE)
10760 gfc_error ("Derived type declaration with FINAL at %C must be in the"
10761 " specification part of a MODULE");
10762 return MATCH_ERROR;
10765 module_ns = gfc_current_ns;
10766 gcc_assert (module_ns);
10767 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
10769 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
10770 if (gfc_match (" ::") == MATCH_ERROR)
10771 return MATCH_ERROR;
10773 /* Match the sequence of procedure names. */
10774 first = true;
10775 last = false;
10778 gfc_finalizer* f;
10780 if (first && gfc_match_eos () == MATCH_YES)
10782 gfc_error ("Empty FINAL at %C");
10783 return MATCH_ERROR;
10786 m = gfc_match_name (name);
10787 if (m == MATCH_NO)
10789 gfc_error ("Expected module procedure name at %C");
10790 return MATCH_ERROR;
10792 else if (m != MATCH_YES)
10793 return MATCH_ERROR;
10795 if (gfc_match_eos () == MATCH_YES)
10796 last = true;
10797 if (!last && gfc_match_char (',') != MATCH_YES)
10799 gfc_error ("Expected %<,%> at %C");
10800 return MATCH_ERROR;
10803 if (gfc_get_symbol (name, module_ns, &sym))
10805 gfc_error ("Unknown procedure name %qs at %C", name);
10806 return MATCH_ERROR;
10809 /* Mark the symbol as module procedure. */
10810 if (sym->attr.proc != PROC_MODULE
10811 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
10812 return MATCH_ERROR;
10814 /* Check if we already have this symbol in the list, this is an error. */
10815 for (f = block->f2k_derived->finalizers; f; f = f->next)
10816 if (f->proc_sym == sym)
10818 gfc_error ("%qs at %C is already defined as FINAL procedure",
10819 name);
10820 return MATCH_ERROR;
10823 /* Add this symbol to the list of finalizers. */
10824 gcc_assert (block->f2k_derived);
10825 sym->refs++;
10826 f = XCNEW (gfc_finalizer);
10827 f->proc_sym = sym;
10828 f->proc_tree = NULL;
10829 f->where = gfc_current_locus;
10830 f->next = block->f2k_derived->finalizers;
10831 block->f2k_derived->finalizers = f;
10833 first = false;
10835 while (!last);
10837 return MATCH_YES;
10841 const ext_attr_t ext_attr_list[] = {
10842 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
10843 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
10844 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
10845 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
10846 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
10847 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
10848 { NULL, EXT_ATTR_LAST, NULL }
10851 /* Match a !GCC$ ATTRIBUTES statement of the form:
10852 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
10853 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
10855 TODO: We should support all GCC attributes using the same syntax for
10856 the attribute list, i.e. the list in C
10857 __attributes(( attribute-list ))
10858 matches then
10859 !GCC$ ATTRIBUTES attribute-list ::
10860 Cf. c-parser.c's c_parser_attributes; the data can then directly be
10861 saved into a TREE.
10863 As there is absolutely no risk of confusion, we should never return
10864 MATCH_NO. */
10865 match
10866 gfc_match_gcc_attributes (void)
10868 symbol_attribute attr;
10869 char name[GFC_MAX_SYMBOL_LEN + 1];
10870 unsigned id;
10871 gfc_symbol *sym;
10872 match m;
10874 gfc_clear_attr (&attr);
10875 for(;;)
10877 char ch;
10879 if (gfc_match_name (name) != MATCH_YES)
10880 return MATCH_ERROR;
10882 for (id = 0; id < EXT_ATTR_LAST; id++)
10883 if (strcmp (name, ext_attr_list[id].name) == 0)
10884 break;
10886 if (id == EXT_ATTR_LAST)
10888 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
10889 return MATCH_ERROR;
10892 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
10893 return MATCH_ERROR;
10895 gfc_gobble_whitespace ();
10896 ch = gfc_next_ascii_char ();
10897 if (ch == ':')
10899 /* This is the successful exit condition for the loop. */
10900 if (gfc_next_ascii_char () == ':')
10901 break;
10904 if (ch == ',')
10905 continue;
10907 goto syntax;
10910 if (gfc_match_eos () == MATCH_YES)
10911 goto syntax;
10913 for(;;)
10915 m = gfc_match_name (name);
10916 if (m != MATCH_YES)
10917 return m;
10919 if (find_special (name, &sym, true))
10920 return MATCH_ERROR;
10922 sym->attr.ext_attr |= attr.ext_attr;
10924 if (gfc_match_eos () == MATCH_YES)
10925 break;
10927 if (gfc_match_char (',') != MATCH_YES)
10928 goto syntax;
10931 return MATCH_YES;
10933 syntax:
10934 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
10935 return MATCH_ERROR;