2017-08-06 Andrew Pinski <apinski@cavium.com>
[official-gcc.git] / gcc / fortran / decl.c
blob54ee5d3d2a62cd54033372ce581c75a5319ace6c
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;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals = 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr *last_initializer;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
77 gfc_symbol *sym;
78 gfc_expr *initializer;
79 struct enumerator_history *next;
81 enumerator_history;
83 /* Header of enum history chain. */
85 static enumerator_history *enum_history = NULL;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history *max_enum = NULL;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol *gfc_new_block;
95 bool gfc_matching_function;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data = false;
102 bool
103 gfc_in_match_data (void)
105 return in_match_data;
108 static void
109 set_in_match_data (bool set_value)
111 in_match_data = set_value;
114 /* Free a gfc_data_variable structure and everything beneath it. */
116 static void
117 free_variable (gfc_data_variable *p)
119 gfc_data_variable *q;
121 for (; p; p = q)
123 q = p->next;
124 gfc_free_expr (p->expr);
125 gfc_free_iterator (&p->iter, 0);
126 free_variable (p->list);
127 free (p);
132 /* Free a gfc_data_value structure and everything beneath it. */
134 static void
135 free_value (gfc_data_value *p)
137 gfc_data_value *q;
139 for (; p; p = q)
141 q = p->next;
142 mpz_clear (p->repeat);
143 gfc_free_expr (p->expr);
144 free (p);
149 /* Free a list of gfc_data structures. */
151 void
152 gfc_free_data (gfc_data *p)
154 gfc_data *q;
156 for (; p; p = q)
158 q = p->next;
159 free_variable (p->var);
160 free_value (p->value);
161 free (p);
166 /* Free all data in a namespace. */
168 static void
169 gfc_free_data_all (gfc_namespace *ns)
171 gfc_data *d;
173 for (;ns->data;)
175 d = ns->data->next;
176 free (ns->data);
177 ns->data = d;
181 /* Reject data parsed since the last restore point was marked. */
183 void
184 gfc_reject_data (gfc_namespace *ns)
186 gfc_data *d;
188 while (ns->data && ns->data != ns->old_data)
190 d = ns->data->next;
191 free (ns->data);
192 ns->data = d;
196 static match var_element (gfc_data_variable *);
198 /* Match a list of variables terminated by an iterator and a right
199 parenthesis. */
201 static match
202 var_list (gfc_data_variable *parent)
204 gfc_data_variable *tail, var;
205 match m;
207 m = var_element (&var);
208 if (m == MATCH_ERROR)
209 return MATCH_ERROR;
210 if (m == MATCH_NO)
211 goto syntax;
213 tail = gfc_get_data_variable ();
214 *tail = var;
216 parent->list = tail;
218 for (;;)
220 if (gfc_match_char (',') != MATCH_YES)
221 goto syntax;
223 m = gfc_match_iterator (&parent->iter, 1);
224 if (m == MATCH_YES)
225 break;
226 if (m == MATCH_ERROR)
227 return MATCH_ERROR;
229 m = var_element (&var);
230 if (m == MATCH_ERROR)
231 return MATCH_ERROR;
232 if (m == MATCH_NO)
233 goto syntax;
235 tail->next = gfc_get_data_variable ();
236 tail = tail->next;
238 *tail = var;
241 if (gfc_match_char (')') != MATCH_YES)
242 goto syntax;
243 return MATCH_YES;
245 syntax:
246 gfc_syntax_error (ST_DATA);
247 return MATCH_ERROR;
251 /* Match a single element in a data variable list, which can be a
252 variable-iterator list. */
254 static match
255 var_element (gfc_data_variable *new_var)
257 match m;
258 gfc_symbol *sym;
260 memset (new_var, 0, sizeof (gfc_data_variable));
262 if (gfc_match_char ('(') == MATCH_YES)
263 return var_list (new_var);
265 m = gfc_match_variable (&new_var->expr, 0);
266 if (m != MATCH_YES)
267 return m;
269 sym = new_var->expr->symtree->n.sym;
271 /* Symbol should already have an associated type. */
272 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
273 return MATCH_ERROR;
275 if (!sym->attr.function && gfc_current_ns->parent
276 && gfc_current_ns->parent == sym->ns)
278 gfc_error ("Host associated variable %qs may not be in the DATA "
279 "statement at %C", sym->name);
280 return MATCH_ERROR;
283 if (gfc_current_state () != COMP_BLOCK_DATA
284 && sym->attr.in_common
285 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
286 "common block variable %qs in DATA statement at %C",
287 sym->name))
288 return MATCH_ERROR;
290 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
291 return MATCH_ERROR;
293 return MATCH_YES;
297 /* Match the top-level list of data variables. */
299 static match
300 top_var_list (gfc_data *d)
302 gfc_data_variable var, *tail, *new_var;
303 match m;
305 tail = NULL;
307 for (;;)
309 m = var_element (&var);
310 if (m == MATCH_NO)
311 goto syntax;
312 if (m == MATCH_ERROR)
313 return MATCH_ERROR;
315 new_var = gfc_get_data_variable ();
316 *new_var = var;
318 if (tail == NULL)
319 d->var = new_var;
320 else
321 tail->next = new_var;
323 tail = new_var;
325 if (gfc_match_char ('/') == MATCH_YES)
326 break;
327 if (gfc_match_char (',') != MATCH_YES)
328 goto syntax;
331 return MATCH_YES;
333 syntax:
334 gfc_syntax_error (ST_DATA);
335 gfc_free_data_all (gfc_current_ns);
336 return MATCH_ERROR;
340 static match
341 match_data_constant (gfc_expr **result)
343 char name[GFC_MAX_SYMBOL_LEN + 1];
344 gfc_symbol *sym, *dt_sym = NULL;
345 gfc_expr *expr;
346 match m;
347 locus old_loc;
349 m = gfc_match_literal_constant (&expr, 1);
350 if (m == MATCH_YES)
352 *result = expr;
353 return MATCH_YES;
356 if (m == MATCH_ERROR)
357 return MATCH_ERROR;
359 m = gfc_match_null (result);
360 if (m != MATCH_NO)
361 return m;
363 old_loc = gfc_current_locus;
365 /* Should this be a structure component, try to match it
366 before matching a name. */
367 m = gfc_match_rvalue (result);
368 if (m == MATCH_ERROR)
369 return m;
371 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
373 if (!gfc_simplify_expr (*result, 0))
374 m = MATCH_ERROR;
375 return m;
377 else if (m == MATCH_YES)
378 gfc_free_expr (*result);
380 gfc_current_locus = old_loc;
382 m = gfc_match_name (name);
383 if (m != MATCH_YES)
384 return m;
386 if (gfc_find_symbol (name, NULL, 1, &sym))
387 return MATCH_ERROR;
389 if (sym && sym->attr.generic)
390 dt_sym = gfc_find_dt_in_generic (sym);
392 if (sym == NULL
393 || (sym->attr.flavor != FL_PARAMETER
394 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
396 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
397 name);
398 *result = NULL;
399 return MATCH_ERROR;
401 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
402 return gfc_match_structure_constructor (dt_sym, result);
404 /* Check to see if the value is an initialization array expression. */
405 if (sym->value->expr_type == EXPR_ARRAY)
407 gfc_current_locus = old_loc;
409 m = gfc_match_init_expr (result);
410 if (m == MATCH_ERROR)
411 return m;
413 if (m == MATCH_YES)
415 if (!gfc_simplify_expr (*result, 0))
416 m = MATCH_ERROR;
418 if ((*result)->expr_type == EXPR_CONSTANT)
419 return m;
420 else
422 gfc_error ("Invalid initializer %s in Data statement at %C", name);
423 return MATCH_ERROR;
428 *result = gfc_copy_expr (sym->value);
429 return MATCH_YES;
433 /* Match a list of values in a DATA statement. The leading '/' has
434 already been seen at this point. */
436 static match
437 top_val_list (gfc_data *data)
439 gfc_data_value *new_val, *tail;
440 gfc_expr *expr;
441 match m;
443 tail = NULL;
445 for (;;)
447 m = match_data_constant (&expr);
448 if (m == MATCH_NO)
449 goto syntax;
450 if (m == MATCH_ERROR)
451 return MATCH_ERROR;
453 new_val = gfc_get_data_value ();
454 mpz_init (new_val->repeat);
456 if (tail == NULL)
457 data->value = new_val;
458 else
459 tail->next = new_val;
461 tail = new_val;
463 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
465 tail->expr = expr;
466 mpz_set_ui (tail->repeat, 1);
468 else
470 mpz_set (tail->repeat, expr->value.integer);
471 gfc_free_expr (expr);
473 m = match_data_constant (&tail->expr);
474 if (m == MATCH_NO)
475 goto syntax;
476 if (m == MATCH_ERROR)
477 return MATCH_ERROR;
480 if (gfc_match_char ('/') == MATCH_YES)
481 break;
482 if (gfc_match_char (',') == MATCH_NO)
483 goto syntax;
486 return MATCH_YES;
488 syntax:
489 gfc_syntax_error (ST_DATA);
490 gfc_free_data_all (gfc_current_ns);
491 return MATCH_ERROR;
495 /* Matches an old style initialization. */
497 static match
498 match_old_style_init (const char *name)
500 match m;
501 gfc_symtree *st;
502 gfc_symbol *sym;
503 gfc_data *newdata;
505 /* Set up data structure to hold initializers. */
506 gfc_find_sym_tree (name, NULL, 0, &st);
507 sym = st->n.sym;
509 newdata = gfc_get_data ();
510 newdata->var = gfc_get_data_variable ();
511 newdata->var->expr = gfc_get_variable_expr (st);
512 newdata->where = gfc_current_locus;
514 /* Match initial value list. This also eats the terminal '/'. */
515 m = top_val_list (newdata);
516 if (m != MATCH_YES)
518 free (newdata);
519 return m;
522 if (gfc_pure (NULL))
524 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
525 free (newdata);
526 return MATCH_ERROR;
528 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
530 /* Mark the variable as having appeared in a data statement. */
531 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
533 free (newdata);
534 return MATCH_ERROR;
537 /* Chain in namespace list of DATA initializers. */
538 newdata->next = gfc_current_ns->data;
539 gfc_current_ns->data = newdata;
541 return m;
545 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
546 we are matching a DATA statement and are therefore issuing an error
547 if we encounter something unexpected, if not, we're trying to match
548 an old-style initialization expression of the form INTEGER I /2/. */
550 match
551 gfc_match_data (void)
553 gfc_data *new_data;
554 match m;
556 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
557 if ((gfc_current_state () == COMP_FUNCTION
558 || gfc_current_state () == COMP_SUBROUTINE)
559 && gfc_state_stack->previous->state == COMP_INTERFACE)
561 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
562 return MATCH_ERROR;
565 set_in_match_data (true);
567 for (;;)
569 new_data = gfc_get_data ();
570 new_data->where = gfc_current_locus;
572 m = top_var_list (new_data);
573 if (m != MATCH_YES)
574 goto cleanup;
576 m = top_val_list (new_data);
577 if (m != MATCH_YES)
578 goto cleanup;
580 new_data->next = gfc_current_ns->data;
581 gfc_current_ns->data = new_data;
583 if (gfc_match_eos () == MATCH_YES)
584 break;
586 gfc_match_char (','); /* Optional comma */
589 set_in_match_data (false);
591 if (gfc_pure (NULL))
593 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
594 return MATCH_ERROR;
596 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
598 return MATCH_YES;
600 cleanup:
601 set_in_match_data (false);
602 gfc_free_data (new_data);
603 return MATCH_ERROR;
607 /************************ Declaration statements *********************/
610 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
611 list). The difference here is the expression is a list of constants
612 and is surrounded by '/'.
613 The typespec ts must match the typespec of the variable which the
614 clist is initializing.
615 The arrayspec tells whether this should match a list of constants
616 corresponding to array elements or a scalar (as == NULL). */
618 static match
619 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
621 gfc_constructor_base array_head = NULL;
622 gfc_expr *expr = NULL;
623 match m;
624 locus where;
625 mpz_t repeat, size;
626 bool scalar;
627 int cmp;
629 gcc_assert (ts);
631 mpz_init_set_ui (repeat, 0);
632 mpz_init (size);
633 scalar = !as || !as->rank;
635 /* We have already matched '/' - now look for a constant list, as with
636 top_val_list from decl.c, but append the result to an array. */
637 if (gfc_match ("/") == MATCH_YES)
639 gfc_error ("Empty old style initializer list at %C");
640 goto cleanup;
643 where = gfc_current_locus;
644 for (;;)
646 m = match_data_constant (&expr);
647 if (m != MATCH_YES)
648 expr = NULL; /* match_data_constant may set expr to garbage */
649 if (m == MATCH_NO)
650 goto syntax;
651 if (m == MATCH_ERROR)
652 goto cleanup;
654 /* Found r in repeat spec r*c; look for the constant to repeat. */
655 if ( gfc_match_char ('*') == MATCH_YES)
657 if (scalar)
659 gfc_error ("Repeat spec invalid in scalar initializer at %C");
660 goto cleanup;
662 if (expr->ts.type != BT_INTEGER)
664 gfc_error ("Repeat spec must be an integer at %C");
665 goto cleanup;
667 mpz_set (repeat, expr->value.integer);
668 gfc_free_expr (expr);
669 expr = NULL;
671 m = match_data_constant (&expr);
672 if (m == MATCH_NO)
673 gfc_error ("Expected data constant after repeat spec at %C");
674 if (m != MATCH_YES)
675 goto cleanup;
677 /* No repeat spec, we matched the data constant itself. */
678 else
679 mpz_set_ui (repeat, 1);
681 if (!scalar)
683 /* Add the constant initializer as many times as repeated. */
684 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
686 /* Make sure types of elements match */
687 if(ts && !gfc_compare_types (&expr->ts, ts)
688 && !gfc_convert_type (expr, ts, 1))
689 goto cleanup;
691 gfc_constructor_append_expr (&array_head,
692 gfc_copy_expr (expr), &gfc_current_locus);
695 gfc_free_expr (expr);
696 expr = NULL;
699 /* For scalar initializers quit after one element. */
700 else
702 if(gfc_match_char ('/') != MATCH_YES)
704 gfc_error ("End of scalar initializer expected at %C");
705 goto cleanup;
707 break;
710 if (gfc_match_char ('/') == MATCH_YES)
711 break;
712 if (gfc_match_char (',') == MATCH_NO)
713 goto syntax;
716 /* Set up expr as an array constructor. */
717 if (!scalar)
719 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
720 expr->ts = *ts;
721 expr->value.constructor = array_head;
723 expr->rank = as->rank;
724 expr->shape = gfc_get_shape (expr->rank);
726 /* Validate sizes. */
727 gcc_assert (gfc_array_size (expr, &size));
728 gcc_assert (spec_size (as, &repeat));
729 cmp = mpz_cmp (size, repeat);
730 if (cmp < 0)
731 gfc_error ("Not enough elements in array initializer at %C");
732 else if (cmp > 0)
733 gfc_error ("Too many elements in array initializer at %C");
734 if (cmp)
735 goto cleanup;
738 /* Make sure scalar types match. */
739 else if (!gfc_compare_types (&expr->ts, ts)
740 && !gfc_convert_type (expr, ts, 1))
741 goto cleanup;
743 if (expr->ts.u.cl)
744 expr->ts.u.cl->length_from_typespec = 1;
746 *result = expr;
747 mpz_clear (size);
748 mpz_clear (repeat);
749 return MATCH_YES;
751 syntax:
752 gfc_error ("Syntax error in old style initializer list at %C");
754 cleanup:
755 if (expr)
756 expr->value.constructor = NULL;
757 gfc_free_expr (expr);
758 gfc_constructor_free (array_head);
759 mpz_clear (size);
760 mpz_clear (repeat);
761 return MATCH_ERROR;
765 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
767 static bool
768 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
770 int i;
772 if ((from->type == AS_ASSUMED_RANK && to->corank)
773 || (to->type == AS_ASSUMED_RANK && from->corank))
775 gfc_error ("The assumed-rank array at %C shall not have a codimension");
776 return false;
779 if (to->rank == 0 && from->rank > 0)
781 to->rank = from->rank;
782 to->type = from->type;
783 to->cray_pointee = from->cray_pointee;
784 to->cp_was_assumed = from->cp_was_assumed;
786 for (i = 0; i < to->corank; i++)
788 to->lower[from->rank + i] = to->lower[i];
789 to->upper[from->rank + i] = to->upper[i];
791 for (i = 0; i < from->rank; i++)
793 if (copy)
795 to->lower[i] = gfc_copy_expr (from->lower[i]);
796 to->upper[i] = gfc_copy_expr (from->upper[i]);
798 else
800 to->lower[i] = from->lower[i];
801 to->upper[i] = from->upper[i];
805 else if (to->corank == 0 && from->corank > 0)
807 to->corank = from->corank;
808 to->cotype = from->cotype;
810 for (i = 0; i < from->corank; i++)
812 if (copy)
814 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
815 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
817 else
819 to->lower[to->rank + i] = from->lower[i];
820 to->upper[to->rank + i] = from->upper[i];
825 return true;
829 /* Match an intent specification. Since this can only happen after an
830 INTENT word, a legal intent-spec must follow. */
832 static sym_intent
833 match_intent_spec (void)
836 if (gfc_match (" ( in out )") == MATCH_YES)
837 return INTENT_INOUT;
838 if (gfc_match (" ( in )") == MATCH_YES)
839 return INTENT_IN;
840 if (gfc_match (" ( out )") == MATCH_YES)
841 return INTENT_OUT;
843 gfc_error ("Bad INTENT specification at %C");
844 return INTENT_UNKNOWN;
848 /* Matches a character length specification, which is either a
849 specification expression, '*', or ':'. */
851 static match
852 char_len_param_value (gfc_expr **expr, bool *deferred)
854 match m;
856 *expr = NULL;
857 *deferred = false;
859 if (gfc_match_char ('*') == MATCH_YES)
860 return MATCH_YES;
862 if (gfc_match_char (':') == MATCH_YES)
864 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
865 return MATCH_ERROR;
867 *deferred = true;
869 return MATCH_YES;
872 m = gfc_match_expr (expr);
874 if (m == MATCH_NO || m == MATCH_ERROR)
875 return m;
877 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
878 return MATCH_ERROR;
880 if ((*expr)->expr_type == EXPR_FUNCTION)
882 if ((*expr)->ts.type == BT_INTEGER
883 || ((*expr)->ts.type == BT_UNKNOWN
884 && strcmp((*expr)->symtree->name, "null") != 0))
885 return MATCH_YES;
887 goto syntax;
889 else if ((*expr)->expr_type == EXPR_CONSTANT)
891 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
892 processor dependent and its value is greater than or equal to zero.
893 F2008, 4.4.3.2: If the character length parameter value evaluates
894 to a negative value, the length of character entities declared
895 is zero. */
897 if ((*expr)->ts.type == BT_INTEGER)
899 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
900 mpz_set_si ((*expr)->value.integer, 0);
902 else
903 goto syntax;
905 else if ((*expr)->expr_type == EXPR_ARRAY)
906 goto syntax;
907 else if ((*expr)->expr_type == EXPR_VARIABLE)
909 bool t;
910 gfc_expr *e;
912 e = gfc_copy_expr (*expr);
914 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
915 which causes an ICE if gfc_reduce_init_expr() is called. */
916 if (e->ref && e->ref->type == REF_ARRAY
917 && e->ref->u.ar.type == AR_UNKNOWN
918 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
919 goto syntax;
921 t = gfc_reduce_init_expr (e);
923 if (!t && e->ts.type == BT_UNKNOWN
924 && e->symtree->n.sym->attr.untyped == 1
925 && (flag_implicit_none
926 || e->symtree->n.sym->ns->seen_implicit_none == 1
927 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
929 gfc_free_expr (e);
930 goto syntax;
933 if ((e->ref && e->ref->type == REF_ARRAY
934 && e->ref->u.ar.type != AR_ELEMENT)
935 || (!e->ref && e->expr_type == EXPR_ARRAY))
937 gfc_free_expr (e);
938 goto syntax;
941 gfc_free_expr (e);
944 return m;
946 syntax:
947 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
948 return MATCH_ERROR;
952 /* A character length is a '*' followed by a literal integer or a
953 char_len_param_value in parenthesis. */
955 static match
956 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
958 int length;
959 match m;
961 *deferred = false;
962 m = gfc_match_char ('*');
963 if (m != MATCH_YES)
964 return m;
966 m = gfc_match_small_literal_int (&length, NULL);
967 if (m == MATCH_ERROR)
968 return m;
970 if (m == MATCH_YES)
972 if (obsolescent_check
973 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
974 return MATCH_ERROR;
975 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
976 return m;
979 if (gfc_match_char ('(') == MATCH_NO)
980 goto syntax;
982 m = char_len_param_value (expr, deferred);
983 if (m != MATCH_YES && gfc_matching_function)
985 gfc_undo_symbols ();
986 m = MATCH_YES;
989 if (m == MATCH_ERROR)
990 return m;
991 if (m == MATCH_NO)
992 goto syntax;
994 if (gfc_match_char (')') == MATCH_NO)
996 gfc_free_expr (*expr);
997 *expr = NULL;
998 goto syntax;
1001 return MATCH_YES;
1003 syntax:
1004 gfc_error ("Syntax error in character length specification at %C");
1005 return MATCH_ERROR;
1009 /* Special subroutine for finding a symbol. Check if the name is found
1010 in the current name space. If not, and we're compiling a function or
1011 subroutine and the parent compilation unit is an interface, then check
1012 to see if the name we've been given is the name of the interface
1013 (located in another namespace). */
1015 static int
1016 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1018 gfc_state_data *s;
1019 gfc_symtree *st;
1020 int i;
1022 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1023 if (i == 0)
1025 *result = st ? st->n.sym : NULL;
1026 goto end;
1029 if (gfc_current_state () != COMP_SUBROUTINE
1030 && gfc_current_state () != COMP_FUNCTION)
1031 goto end;
1033 s = gfc_state_stack->previous;
1034 if (s == NULL)
1035 goto end;
1037 if (s->state != COMP_INTERFACE)
1038 goto end;
1039 if (s->sym == NULL)
1040 goto end; /* Nameless interface. */
1042 if (strcmp (name, s->sym->name) == 0)
1044 *result = s->sym;
1045 return 0;
1048 end:
1049 return i;
1053 /* Special subroutine for getting a symbol node associated with a
1054 procedure name, used in SUBROUTINE and FUNCTION statements. The
1055 symbol is created in the parent using with symtree node in the
1056 child unit pointing to the symbol. If the current namespace has no
1057 parent, then the symbol is just created in the current unit. */
1059 static int
1060 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1062 gfc_symtree *st;
1063 gfc_symbol *sym;
1064 int rc = 0;
1066 /* Module functions have to be left in their own namespace because
1067 they have potentially (almost certainly!) already been referenced.
1068 In this sense, they are rather like external functions. This is
1069 fixed up in resolve.c(resolve_entries), where the symbol name-
1070 space is set to point to the master function, so that the fake
1071 result mechanism can work. */
1072 if (module_fcn_entry)
1074 /* Present if entry is declared to be a module procedure. */
1075 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1077 if (*result == NULL)
1078 rc = gfc_get_symbol (name, NULL, result);
1079 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1080 && (*result)->ts.type == BT_UNKNOWN
1081 && sym->attr.flavor == FL_UNKNOWN)
1082 /* Pick up the typespec for the entry, if declared in the function
1083 body. Note that this symbol is FL_UNKNOWN because it will
1084 only have appeared in a type declaration. The local symtree
1085 is set to point to the module symbol and a unique symtree
1086 to the local version. This latter ensures a correct clearing
1087 of the symbols. */
1089 /* If the ENTRY proceeds its specification, we need to ensure
1090 that this does not raise a "has no IMPLICIT type" error. */
1091 if (sym->ts.type == BT_UNKNOWN)
1092 sym->attr.untyped = 1;
1094 (*result)->ts = sym->ts;
1096 /* Put the symbol in the procedure namespace so that, should
1097 the ENTRY precede its specification, the specification
1098 can be applied. */
1099 (*result)->ns = gfc_current_ns;
1101 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1102 st->n.sym = *result;
1103 st = gfc_get_unique_symtree (gfc_current_ns);
1104 sym->refs++;
1105 st->n.sym = sym;
1108 else
1109 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1111 if (rc)
1112 return rc;
1114 sym = *result;
1115 if (sym->attr.proc == PROC_ST_FUNCTION)
1116 return rc;
1118 if (sym->attr.module_procedure
1119 && sym->attr.if_source == IFSRC_IFBODY)
1121 /* Create a partially populated interface symbol to carry the
1122 characteristics of the procedure and the result. */
1123 sym->tlink = gfc_new_symbol (name, sym->ns);
1124 gfc_add_type (sym->tlink, &(sym->ts),
1125 &gfc_current_locus);
1126 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1127 if (sym->attr.dimension)
1128 sym->tlink->as = gfc_copy_array_spec (sym->as);
1130 /* Ideally, at this point, a copy would be made of the formal
1131 arguments and their namespace. However, this does not appear
1132 to be necessary, albeit at the expense of not being able to
1133 use gfc_compare_interfaces directly. */
1135 if (sym->result && sym->result != sym)
1137 sym->tlink->result = sym->result;
1138 sym->result = NULL;
1140 else if (sym->result)
1142 sym->tlink->result = sym->tlink;
1145 else if (sym && !sym->gfc_new
1146 && gfc_current_state () != COMP_INTERFACE)
1148 /* Trap another encompassed procedure with the same name. All
1149 these conditions are necessary to avoid picking up an entry
1150 whose name clashes with that of the encompassing procedure;
1151 this is handled using gsymbols to register unique, globally
1152 accessible names. */
1153 if (sym->attr.flavor != 0
1154 && sym->attr.proc != 0
1155 && (sym->attr.subroutine || sym->attr.function)
1156 && sym->attr.if_source != IFSRC_UNKNOWN)
1157 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1158 name, &sym->declared_at);
1160 /* Trap a procedure with a name the same as interface in the
1161 encompassing scope. */
1162 if (sym->attr.generic != 0
1163 && (sym->attr.subroutine || sym->attr.function)
1164 && !sym->attr.mod_proc)
1165 gfc_error_now ("Name %qs at %C is already defined"
1166 " as a generic interface at %L",
1167 name, &sym->declared_at);
1169 /* Trap declarations of attributes in encompassing scope. The
1170 signature for this is that ts.kind is set. Legitimate
1171 references only set ts.type. */
1172 if (sym->ts.kind != 0
1173 && !sym->attr.implicit_type
1174 && sym->attr.proc == 0
1175 && gfc_current_ns->parent != NULL
1176 && sym->attr.access == 0
1177 && !module_fcn_entry)
1178 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1179 "and must not have attributes declared at %L",
1180 name, &sym->declared_at);
1183 if (gfc_current_ns->parent == NULL || *result == NULL)
1184 return rc;
1186 /* Module function entries will already have a symtree in
1187 the current namespace but will need one at module level. */
1188 if (module_fcn_entry)
1190 /* Present if entry is declared to be a module procedure. */
1191 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1192 if (st == NULL)
1193 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1195 else
1196 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1198 st->n.sym = sym;
1199 sym->refs++;
1201 /* See if the procedure should be a module procedure. */
1203 if (((sym->ns->proc_name != NULL
1204 && sym->ns->proc_name->attr.flavor == FL_MODULE
1205 && sym->attr.proc != PROC_MODULE)
1206 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1207 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1208 rc = 2;
1210 return rc;
1214 /* Verify that the given symbol representing a parameter is C
1215 interoperable, by checking to see if it was marked as such after
1216 its declaration. If the given symbol is not interoperable, a
1217 warning is reported, thus removing the need to return the status to
1218 the calling function. The standard does not require the user use
1219 one of the iso_c_binding named constants to declare an
1220 interoperable parameter, but we can't be sure if the param is C
1221 interop or not if the user doesn't. For example, integer(4) may be
1222 legal Fortran, but doesn't have meaning in C. It may interop with
1223 a number of the C types, which causes a problem because the
1224 compiler can't know which one. This code is almost certainly not
1225 portable, and the user will get what they deserve if the C type
1226 across platforms isn't always interoperable with integer(4). If
1227 the user had used something like integer(c_int) or integer(c_long),
1228 the compiler could have automatically handled the varying sizes
1229 across platforms. */
1231 bool
1232 gfc_verify_c_interop_param (gfc_symbol *sym)
1234 int is_c_interop = 0;
1235 bool retval = true;
1237 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1238 Don't repeat the checks here. */
1239 if (sym->attr.implicit_type)
1240 return true;
1242 /* For subroutines or functions that are passed to a BIND(C) procedure,
1243 they're interoperable if they're BIND(C) and their params are all
1244 interoperable. */
1245 if (sym->attr.flavor == FL_PROCEDURE)
1247 if (sym->attr.is_bind_c == 0)
1249 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1250 "attribute to be C interoperable", sym->name,
1251 &(sym->declared_at));
1252 return false;
1254 else
1256 if (sym->attr.is_c_interop == 1)
1257 /* We've already checked this procedure; don't check it again. */
1258 return true;
1259 else
1260 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1261 sym->common_block);
1265 /* See if we've stored a reference to a procedure that owns sym. */
1266 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1268 if (sym->ns->proc_name->attr.is_bind_c == 1)
1270 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1272 if (is_c_interop != 1)
1274 /* Make personalized messages to give better feedback. */
1275 if (sym->ts.type == BT_DERIVED)
1276 gfc_error ("Variable %qs at %L is a dummy argument to the "
1277 "BIND(C) procedure %qs but is not C interoperable "
1278 "because derived type %qs is not C interoperable",
1279 sym->name, &(sym->declared_at),
1280 sym->ns->proc_name->name,
1281 sym->ts.u.derived->name);
1282 else if (sym->ts.type == BT_CLASS)
1283 gfc_error ("Variable %qs at %L is a dummy argument to the "
1284 "BIND(C) procedure %qs but is not C interoperable "
1285 "because it is polymorphic",
1286 sym->name, &(sym->declared_at),
1287 sym->ns->proc_name->name);
1288 else if (warn_c_binding_type)
1289 gfc_warning (OPT_Wc_binding_type,
1290 "Variable %qs at %L is a dummy argument of the "
1291 "BIND(C) procedure %qs but may not be C "
1292 "interoperable",
1293 sym->name, &(sym->declared_at),
1294 sym->ns->proc_name->name);
1297 /* Character strings are only C interoperable if they have a
1298 length of 1. */
1299 if (sym->ts.type == BT_CHARACTER)
1301 gfc_charlen *cl = sym->ts.u.cl;
1302 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1303 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1305 gfc_error ("Character argument %qs at %L "
1306 "must be length 1 because "
1307 "procedure %qs is BIND(C)",
1308 sym->name, &sym->declared_at,
1309 sym->ns->proc_name->name);
1310 retval = false;
1314 /* We have to make sure that any param to a bind(c) routine does
1315 not have the allocatable, pointer, or optional attributes,
1316 according to J3/04-007, section 5.1. */
1317 if (sym->attr.allocatable == 1
1318 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1319 "ALLOCATABLE attribute in procedure %qs "
1320 "with BIND(C)", sym->name,
1321 &(sym->declared_at),
1322 sym->ns->proc_name->name))
1323 retval = false;
1325 if (sym->attr.pointer == 1
1326 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1327 "POINTER attribute in procedure %qs "
1328 "with BIND(C)", sym->name,
1329 &(sym->declared_at),
1330 sym->ns->proc_name->name))
1331 retval = false;
1333 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1335 gfc_error ("Scalar variable %qs at %L with POINTER or "
1336 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1337 " supported", sym->name, &(sym->declared_at),
1338 sym->ns->proc_name->name);
1339 retval = false;
1342 if (sym->attr.optional == 1 && sym->attr.value)
1344 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1345 "and the VALUE attribute because procedure %qs "
1346 "is BIND(C)", sym->name, &(sym->declared_at),
1347 sym->ns->proc_name->name);
1348 retval = false;
1350 else if (sym->attr.optional == 1
1351 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1352 "at %L with OPTIONAL attribute in "
1353 "procedure %qs which is BIND(C)",
1354 sym->name, &(sym->declared_at),
1355 sym->ns->proc_name->name))
1356 retval = false;
1358 /* Make sure that if it has the dimension attribute, that it is
1359 either assumed size or explicit shape. Deferred shape is already
1360 covered by the pointer/allocatable attribute. */
1361 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1362 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1363 "at %L as dummy argument to the BIND(C) "
1364 "procedure %qs at %L", sym->name,
1365 &(sym->declared_at),
1366 sym->ns->proc_name->name,
1367 &(sym->ns->proc_name->declared_at)))
1368 retval = false;
1372 return retval;
1377 /* Function called by variable_decl() that adds a name to the symbol table. */
1379 static bool
1380 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1381 gfc_array_spec **as, locus *var_locus)
1383 symbol_attribute attr;
1384 gfc_symbol *sym;
1385 int upper;
1386 gfc_symtree *st;
1388 /* Symbols in a submodule are host associated from the parent module or
1389 submodules. Therefore, they can be overridden by declarations in the
1390 submodule scope. Deal with this by attaching the existing symbol to
1391 a new symtree and recycling the old symtree with a new symbol... */
1392 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1393 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1394 && st->n.sym != NULL
1395 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1397 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1398 s->n.sym = st->n.sym;
1399 sym = gfc_new_symbol (name, gfc_current_ns);
1402 st->n.sym = sym;
1403 sym->refs++;
1404 gfc_set_sym_referenced (sym);
1406 /* ...Otherwise generate a new symtree and new symbol. */
1407 else if (gfc_get_symbol (name, NULL, &sym))
1408 return false;
1410 /* Check if the name has already been defined as a type. The
1411 first letter of the symtree will be in upper case then. Of
1412 course, this is only necessary if the upper case letter is
1413 actually different. */
1415 upper = TOUPPER(name[0]);
1416 if (upper != name[0])
1418 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1419 gfc_symtree *st;
1420 int nlen;
1422 nlen = strlen(name);
1423 gcc_assert (nlen <= GFC_MAX_SYMBOL_LEN);
1424 strncpy (u_name, name, nlen + 1);
1425 u_name[0] = upper;
1427 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1429 /* STRUCTURE types can alias symbol names */
1430 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1432 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1433 &st->n.sym->declared_at);
1434 return false;
1438 /* Start updating the symbol table. Add basic type attribute if present. */
1439 if (current_ts.type != BT_UNKNOWN
1440 && (sym->attr.implicit_type == 0
1441 || !gfc_compare_types (&sym->ts, &current_ts))
1442 && !gfc_add_type (sym, &current_ts, var_locus))
1443 return false;
1445 if (sym->ts.type == BT_CHARACTER)
1447 sym->ts.u.cl = cl;
1448 sym->ts.deferred = cl_deferred;
1451 /* Add dimension attribute if present. */
1452 if (!gfc_set_array_spec (sym, *as, var_locus))
1453 return false;
1454 *as = NULL;
1456 /* Add attribute to symbol. The copy is so that we can reset the
1457 dimension attribute. */
1458 attr = current_attr;
1459 attr.dimension = 0;
1460 attr.codimension = 0;
1462 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1463 return false;
1465 /* Finish any work that may need to be done for the binding label,
1466 if it's a bind(c). The bind(c) attr is found before the symbol
1467 is made, and before the symbol name (for data decls), so the
1468 current_ts is holding the binding label, or nothing if the
1469 name= attr wasn't given. Therefore, test here if we're dealing
1470 with a bind(c) and make sure the binding label is set correctly. */
1471 if (sym->attr.is_bind_c == 1)
1473 if (!sym->binding_label)
1475 /* Set the binding label and verify that if a NAME= was specified
1476 then only one identifier was in the entity-decl-list. */
1477 if (!set_binding_label (&sym->binding_label, sym->name,
1478 num_idents_on_line))
1479 return false;
1483 /* See if we know we're in a common block, and if it's a bind(c)
1484 common then we need to make sure we're an interoperable type. */
1485 if (sym->attr.in_common == 1)
1487 /* Test the common block object. */
1488 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1489 && sym->ts.is_c_interop != 1)
1491 gfc_error_now ("Variable %qs in common block %qs at %C "
1492 "must be declared with a C interoperable "
1493 "kind since common block %qs is BIND(C)",
1494 sym->name, sym->common_block->name,
1495 sym->common_block->name);
1496 gfc_clear_error ();
1500 sym->attr.implied_index = 0;
1502 if (sym->ts.type == BT_CLASS)
1503 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1505 return true;
1509 /* Set character constant to the given length. The constant will be padded or
1510 truncated. If we're inside an array constructor without a typespec, we
1511 additionally check that all elements have the same length; check_len -1
1512 means no checking. */
1514 void
1515 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1517 gfc_char_t *s;
1518 int slen;
1520 if (expr->ts.type != BT_CHARACTER)
1521 return;
1523 if (expr->expr_type != EXPR_CONSTANT)
1525 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1526 return;
1529 slen = expr->value.character.length;
1530 if (len != slen)
1532 s = gfc_get_wide_string (len + 1);
1533 memcpy (s, expr->value.character.string,
1534 MIN (len, slen) * sizeof (gfc_char_t));
1535 if (len > slen)
1536 gfc_wide_memset (&s[slen], ' ', len - slen);
1538 if (warn_character_truncation && slen > len)
1539 gfc_warning_now (OPT_Wcharacter_truncation,
1540 "CHARACTER expression at %L is being truncated "
1541 "(%d/%d)", &expr->where, slen, len);
1543 /* Apply the standard by 'hand' otherwise it gets cleared for
1544 initializers. */
1545 if (check_len != -1 && slen != check_len
1546 && !(gfc_option.allow_std & GFC_STD_GNU))
1547 gfc_error_now ("The CHARACTER elements of the array constructor "
1548 "at %L must have the same length (%d/%d)",
1549 &expr->where, slen, check_len);
1551 s[len] = '\0';
1552 free (expr->value.character.string);
1553 expr->value.character.string = s;
1554 expr->value.character.length = len;
1559 /* Function to create and update the enumerator history
1560 using the information passed as arguments.
1561 Pointer "max_enum" is also updated, to point to
1562 enum history node containing largest initializer.
1564 SYM points to the symbol node of enumerator.
1565 INIT points to its enumerator value. */
1567 static void
1568 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1570 enumerator_history *new_enum_history;
1571 gcc_assert (sym != NULL && init != NULL);
1573 new_enum_history = XCNEW (enumerator_history);
1575 new_enum_history->sym = sym;
1576 new_enum_history->initializer = init;
1577 new_enum_history->next = NULL;
1579 if (enum_history == NULL)
1581 enum_history = new_enum_history;
1582 max_enum = enum_history;
1584 else
1586 new_enum_history->next = enum_history;
1587 enum_history = new_enum_history;
1589 if (mpz_cmp (max_enum->initializer->value.integer,
1590 new_enum_history->initializer->value.integer) < 0)
1591 max_enum = new_enum_history;
1596 /* Function to free enum kind history. */
1598 void
1599 gfc_free_enum_history (void)
1601 enumerator_history *current = enum_history;
1602 enumerator_history *next;
1604 while (current != NULL)
1606 next = current->next;
1607 free (current);
1608 current = next;
1610 max_enum = NULL;
1611 enum_history = NULL;
1615 /* Function called by variable_decl() that adds an initialization
1616 expression to a symbol. */
1618 static bool
1619 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1621 symbol_attribute attr;
1622 gfc_symbol *sym;
1623 gfc_expr *init;
1625 init = *initp;
1626 if (find_special (name, &sym, false))
1627 return false;
1629 attr = sym->attr;
1631 /* If this symbol is confirming an implicit parameter type,
1632 then an initialization expression is not allowed. */
1633 if (attr.flavor == FL_PARAMETER
1634 && sym->value != NULL
1635 && *initp != NULL)
1637 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1638 sym->name);
1639 return false;
1642 if (init == NULL)
1644 /* An initializer is required for PARAMETER declarations. */
1645 if (attr.flavor == FL_PARAMETER)
1647 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1648 return false;
1651 else
1653 /* If a variable appears in a DATA block, it cannot have an
1654 initializer. */
1655 if (sym->attr.data)
1657 gfc_error ("Variable %qs at %C with an initializer already "
1658 "appears in a DATA statement", sym->name);
1659 return false;
1662 /* Check if the assignment can happen. This has to be put off
1663 until later for derived type variables and procedure pointers. */
1664 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1665 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1666 && !sym->attr.proc_pointer
1667 && !gfc_check_assign_symbol (sym, NULL, init))
1668 return false;
1670 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1671 && init->ts.type == BT_CHARACTER)
1673 /* Update symbol character length according initializer. */
1674 if (!gfc_check_assign_symbol (sym, NULL, init))
1675 return false;
1677 if (sym->ts.u.cl->length == NULL)
1679 int clen;
1680 /* If there are multiple CHARACTER variables declared on the
1681 same line, we don't want them to share the same length. */
1682 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1684 if (sym->attr.flavor == FL_PARAMETER)
1686 if (init->expr_type == EXPR_CONSTANT)
1688 clen = init->value.character.length;
1689 sym->ts.u.cl->length
1690 = gfc_get_int_expr (gfc_default_integer_kind,
1691 NULL, clen);
1693 else if (init->expr_type == EXPR_ARRAY)
1695 if (init->ts.u.cl)
1697 const gfc_expr *length = init->ts.u.cl->length;
1698 if (length->expr_type != EXPR_CONSTANT)
1700 gfc_error ("Cannot initialize parameter array "
1701 "at %L "
1702 "with variable length elements",
1703 &sym->declared_at);
1704 return false;
1706 clen = mpz_get_si (length->value.integer);
1708 else if (init->value.constructor)
1710 gfc_constructor *c;
1711 c = gfc_constructor_first (init->value.constructor);
1712 clen = c->expr->value.character.length;
1714 else
1715 gcc_unreachable ();
1716 sym->ts.u.cl->length
1717 = gfc_get_int_expr (gfc_default_integer_kind,
1718 NULL, clen);
1720 else if (init->ts.u.cl && init->ts.u.cl->length)
1721 sym->ts.u.cl->length =
1722 gfc_copy_expr (sym->value->ts.u.cl->length);
1725 /* Update initializer character length according symbol. */
1726 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1728 int len;
1730 if (!gfc_specification_expr (sym->ts.u.cl->length))
1731 return false;
1733 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1735 if (init->expr_type == EXPR_CONSTANT)
1736 gfc_set_constant_character_len (len, init, -1);
1737 else if (init->expr_type == EXPR_ARRAY)
1739 gfc_constructor *c;
1741 /* Build a new charlen to prevent simplification from
1742 deleting the length before it is resolved. */
1743 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1744 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1746 for (c = gfc_constructor_first (init->value.constructor);
1747 c; c = gfc_constructor_next (c))
1748 gfc_set_constant_character_len (len, c->expr, -1);
1753 /* If sym is implied-shape, set its upper bounds from init. */
1754 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1755 && sym->as->type == AS_IMPLIED_SHAPE)
1757 int dim;
1759 if (init->rank == 0)
1761 gfc_error ("Can't initialize implied-shape array at %L"
1762 " with scalar", &sym->declared_at);
1763 return false;
1766 /* Shape should be present, we get an initialization expression. */
1767 gcc_assert (init->shape);
1769 for (dim = 0; dim < sym->as->rank; ++dim)
1771 int k;
1772 gfc_expr *e, *lower;
1774 lower = sym->as->lower[dim];
1776 /* If the lower bound is an array element from another
1777 parameterized array, then it is marked with EXPR_VARIABLE and
1778 is an initialization expression. Try to reduce it. */
1779 if (lower->expr_type == EXPR_VARIABLE)
1780 gfc_reduce_init_expr (lower);
1782 if (lower->expr_type == EXPR_CONSTANT)
1784 /* All dimensions must be without upper bound. */
1785 gcc_assert (!sym->as->upper[dim]);
1787 k = lower->ts.kind;
1788 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1789 mpz_add (e->value.integer, lower->value.integer,
1790 init->shape[dim]);
1791 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1792 sym->as->upper[dim] = e;
1794 else
1796 gfc_error ("Non-constant lower bound in implied-shape"
1797 " declaration at %L", &lower->where);
1798 return false;
1802 sym->as->type = AS_EXPLICIT;
1805 /* Need to check if the expression we initialized this
1806 to was one of the iso_c_binding named constants. If so,
1807 and we're a parameter (constant), let it be iso_c.
1808 For example:
1809 integer(c_int), parameter :: my_int = c_int
1810 integer(my_int) :: my_int_2
1811 If we mark my_int as iso_c (since we can see it's value
1812 is equal to one of the named constants), then my_int_2
1813 will be considered C interoperable. */
1814 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
1816 sym->ts.is_iso_c |= init->ts.is_iso_c;
1817 sym->ts.is_c_interop |= init->ts.is_c_interop;
1818 /* attr bits needed for module files. */
1819 sym->attr.is_iso_c |= init->ts.is_iso_c;
1820 sym->attr.is_c_interop |= init->ts.is_c_interop;
1821 if (init->ts.is_iso_c)
1822 sym->ts.f90_type = init->ts.f90_type;
1825 /* Add initializer. Make sure we keep the ranks sane. */
1826 if (sym->attr.dimension && init->rank == 0)
1828 mpz_t size;
1829 gfc_expr *array;
1830 int n;
1831 if (sym->attr.flavor == FL_PARAMETER
1832 && init->expr_type == EXPR_CONSTANT
1833 && spec_size (sym->as, &size)
1834 && mpz_cmp_si (size, 0) > 0)
1836 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1837 &init->where);
1838 for (n = 0; n < (int)mpz_get_si (size); n++)
1839 gfc_constructor_append_expr (&array->value.constructor,
1840 n == 0
1841 ? init
1842 : gfc_copy_expr (init),
1843 &init->where);
1845 array->shape = gfc_get_shape (sym->as->rank);
1846 for (n = 0; n < sym->as->rank; n++)
1847 spec_dimen_size (sym->as, n, &array->shape[n]);
1849 init = array;
1850 mpz_clear (size);
1852 init->rank = sym->as->rank;
1855 sym->value = init;
1856 if (sym->attr.save == SAVE_NONE)
1857 sym->attr.save = SAVE_IMPLICIT;
1858 *initp = NULL;
1861 return true;
1865 /* Function called by variable_decl() that adds a name to a structure
1866 being built. */
1868 static bool
1869 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1870 gfc_array_spec **as)
1872 gfc_state_data *s;
1873 gfc_component *c;
1875 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1876 constructing, it must have the pointer attribute. */
1877 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1878 && current_ts.u.derived == gfc_current_block ()
1879 && current_attr.pointer == 0)
1881 if (current_attr.allocatable
1882 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
1883 "must have the POINTER attribute"))
1885 return false;
1887 else if (current_attr.allocatable == 0)
1889 gfc_error ("Component at %C must have the POINTER attribute");
1890 return false;
1894 /* F03:C437. */
1895 if (current_ts.type == BT_CLASS
1896 && !(current_attr.pointer || current_attr.allocatable))
1898 gfc_error ("Component %qs with CLASS at %C must be allocatable "
1899 "or pointer", name);
1900 return false;
1903 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1905 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1907 gfc_error ("Array component of structure at %C must have explicit "
1908 "or deferred shape");
1909 return false;
1913 /* If we are in a nested union/map definition, gfc_add_component will not
1914 properly find repeated components because:
1915 (i) gfc_add_component does a flat search, where components of unions
1916 and maps are implicity chained so nested components may conflict.
1917 (ii) Unions and maps are not linked as components of their parent
1918 structures until after they are parsed.
1919 For (i) we use gfc_find_component which searches recursively, and for (ii)
1920 we search each block directly from the parse stack until we find the top
1921 level structure. */
1923 s = gfc_state_stack;
1924 if (s->state == COMP_UNION || s->state == COMP_MAP)
1926 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
1928 c = gfc_find_component (s->sym, name, true, true, NULL);
1929 if (c != NULL)
1931 gfc_error_now ("Component %qs at %C already declared at %L",
1932 name, &c->loc);
1933 return false;
1935 /* Break after we've searched the entire chain. */
1936 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
1937 break;
1938 s = s->previous;
1942 if (!gfc_add_component (gfc_current_block(), name, &c))
1943 return false;
1945 c->ts = current_ts;
1946 if (c->ts.type == BT_CHARACTER)
1947 c->ts.u.cl = cl;
1948 c->attr = current_attr;
1950 c->initializer = *init;
1951 *init = NULL;
1953 c->as = *as;
1954 if (c->as != NULL)
1956 if (c->as->corank)
1957 c->attr.codimension = 1;
1958 if (c->as->rank)
1959 c->attr.dimension = 1;
1961 *as = NULL;
1963 gfc_apply_init (&c->ts, &c->attr, c->initializer);
1965 /* Check array components. */
1966 if (!c->attr.dimension)
1967 goto scalar;
1969 if (c->attr.pointer)
1971 if (c->as->type != AS_DEFERRED)
1973 gfc_error ("Pointer array component of structure at %C must have a "
1974 "deferred shape");
1975 return false;
1978 else if (c->attr.allocatable)
1980 if (c->as->type != AS_DEFERRED)
1982 gfc_error ("Allocatable component of structure at %C must have a "
1983 "deferred shape");
1984 return false;
1987 else
1989 if (c->as->type != AS_EXPLICIT)
1991 gfc_error ("Array component of structure at %C must have an "
1992 "explicit shape");
1993 return false;
1997 scalar:
1998 if (c->ts.type == BT_CLASS)
1999 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2001 return true;
2005 /* Match a 'NULL()', and possibly take care of some side effects. */
2007 match
2008 gfc_match_null (gfc_expr **result)
2010 gfc_symbol *sym;
2011 match m, m2 = MATCH_NO;
2013 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2014 return MATCH_ERROR;
2016 if (m == MATCH_NO)
2018 locus old_loc;
2019 char name[GFC_MAX_SYMBOL_LEN + 1];
2021 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2022 return m2;
2024 old_loc = gfc_current_locus;
2025 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2026 return MATCH_ERROR;
2027 if (m2 != MATCH_YES
2028 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2029 return MATCH_ERROR;
2030 if (m2 == MATCH_NO)
2032 gfc_current_locus = old_loc;
2033 return MATCH_NO;
2037 /* The NULL symbol now has to be/become an intrinsic function. */
2038 if (gfc_get_symbol ("null", NULL, &sym))
2040 gfc_error ("NULL() initialization at %C is ambiguous");
2041 return MATCH_ERROR;
2044 gfc_intrinsic_symbol (sym);
2046 if (sym->attr.proc != PROC_INTRINSIC
2047 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2048 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2049 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2050 return MATCH_ERROR;
2052 *result = gfc_get_null_expr (&gfc_current_locus);
2054 /* Invalid per F2008, C512. */
2055 if (m2 == MATCH_YES)
2057 gfc_error ("NULL() initialization at %C may not have MOLD");
2058 return MATCH_ERROR;
2061 return MATCH_YES;
2065 /* Match the initialization expr for a data pointer or procedure pointer. */
2067 static match
2068 match_pointer_init (gfc_expr **init, int procptr)
2070 match m;
2072 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2074 gfc_error ("Initialization of pointer at %C is not allowed in "
2075 "a PURE procedure");
2076 return MATCH_ERROR;
2078 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2080 /* Match NULL() initialization. */
2081 m = gfc_match_null (init);
2082 if (m != MATCH_NO)
2083 return m;
2085 /* Match non-NULL initialization. */
2086 gfc_matching_ptr_assignment = !procptr;
2087 gfc_matching_procptr_assignment = procptr;
2088 m = gfc_match_rvalue (init);
2089 gfc_matching_ptr_assignment = 0;
2090 gfc_matching_procptr_assignment = 0;
2091 if (m == MATCH_ERROR)
2092 return MATCH_ERROR;
2093 else if (m == MATCH_NO)
2095 gfc_error ("Error in pointer initialization at %C");
2096 return MATCH_ERROR;
2099 if (!procptr && !gfc_resolve_expr (*init))
2100 return MATCH_ERROR;
2102 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2103 "initialization at %C"))
2104 return MATCH_ERROR;
2106 return MATCH_YES;
2110 static bool
2111 check_function_name (char *name)
2113 /* In functions that have a RESULT variable defined, the function name always
2114 refers to function calls. Therefore, the name is not allowed to appear in
2115 specification statements. When checking this, be careful about
2116 'hidden' procedure pointer results ('ppr@'). */
2118 if (gfc_current_state () == COMP_FUNCTION)
2120 gfc_symbol *block = gfc_current_block ();
2121 if (block && block->result && block->result != block
2122 && strcmp (block->result->name, "ppr@") != 0
2123 && strcmp (block->name, name) == 0)
2125 gfc_error ("Function name %qs not allowed at %C", name);
2126 return false;
2130 return true;
2134 /* Match a variable name with an optional initializer. When this
2135 subroutine is called, a variable is expected to be parsed next.
2136 Depending on what is happening at the moment, updates either the
2137 symbol table or the current interface. */
2139 static match
2140 variable_decl (int elem)
2142 char name[GFC_MAX_SYMBOL_LEN + 1];
2143 gfc_expr *initializer, *char_len;
2144 gfc_array_spec *as;
2145 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2146 gfc_charlen *cl;
2147 bool cl_deferred;
2148 locus var_locus;
2149 match m;
2150 bool t;
2151 gfc_symbol *sym;
2153 initializer = NULL;
2154 as = NULL;
2155 cp_as = NULL;
2157 /* When we get here, we've just matched a list of attributes and
2158 maybe a type and a double colon. The next thing we expect to see
2159 is the name of the symbol. */
2160 m = gfc_match_name (name);
2161 if (m != MATCH_YES)
2162 goto cleanup;
2164 var_locus = gfc_current_locus;
2166 /* Now we could see the optional array spec. or character length. */
2167 m = gfc_match_array_spec (&as, true, true);
2168 if (m == MATCH_ERROR)
2169 goto cleanup;
2171 if (m == MATCH_NO)
2172 as = gfc_copy_array_spec (current_as);
2173 else if (current_as
2174 && !merge_array_spec (current_as, as, true))
2176 m = MATCH_ERROR;
2177 goto cleanup;
2180 if (flag_cray_pointer)
2181 cp_as = gfc_copy_array_spec (as);
2183 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2184 determine (and check) whether it can be implied-shape. If it
2185 was parsed as assumed-size, change it because PARAMETERs can not
2186 be assumed-size. */
2187 if (as)
2189 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2191 m = MATCH_ERROR;
2192 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2193 name, &var_locus);
2194 goto cleanup;
2197 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2198 && current_attr.flavor == FL_PARAMETER)
2199 as->type = AS_IMPLIED_SHAPE;
2201 if (as->type == AS_IMPLIED_SHAPE
2202 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2203 &var_locus))
2205 m = MATCH_ERROR;
2206 goto cleanup;
2210 char_len = NULL;
2211 cl = NULL;
2212 cl_deferred = false;
2214 if (current_ts.type == BT_CHARACTER)
2216 switch (match_char_length (&char_len, &cl_deferred, false))
2218 case MATCH_YES:
2219 cl = gfc_new_charlen (gfc_current_ns, NULL);
2221 cl->length = char_len;
2222 break;
2224 /* Non-constant lengths need to be copied after the first
2225 element. Also copy assumed lengths. */
2226 case MATCH_NO:
2227 if (elem > 1
2228 && (current_ts.u.cl->length == NULL
2229 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2231 cl = gfc_new_charlen (gfc_current_ns, NULL);
2232 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2234 else
2235 cl = current_ts.u.cl;
2237 cl_deferred = current_ts.deferred;
2239 break;
2241 case MATCH_ERROR:
2242 goto cleanup;
2246 /* The dummy arguments and result of the abreviated form of MODULE
2247 PROCEDUREs, used in SUBMODULES should not be redefined. */
2248 if (gfc_current_ns->proc_name
2249 && gfc_current_ns->proc_name->abr_modproc_decl)
2251 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2252 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2254 m = MATCH_ERROR;
2255 gfc_error ("%qs at %C is a redefinition of the declaration "
2256 "in the corresponding interface for MODULE "
2257 "PROCEDURE %qs", sym->name,
2258 gfc_current_ns->proc_name->name);
2259 goto cleanup;
2263 /* If this symbol has already shown up in a Cray Pointer declaration,
2264 and this is not a component declaration,
2265 then we want to set the type & bail out. */
2266 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2268 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2269 if (sym != NULL && sym->attr.cray_pointee)
2271 sym->ts.type = current_ts.type;
2272 sym->ts.kind = current_ts.kind;
2273 sym->ts.u.cl = cl;
2274 sym->ts.u.derived = current_ts.u.derived;
2275 sym->ts.is_c_interop = current_ts.is_c_interop;
2276 sym->ts.is_iso_c = current_ts.is_iso_c;
2277 m = MATCH_YES;
2279 /* Check to see if we have an array specification. */
2280 if (cp_as != NULL)
2282 if (sym->as != NULL)
2284 gfc_error ("Duplicate array spec for Cray pointee at %C");
2285 gfc_free_array_spec (cp_as);
2286 m = MATCH_ERROR;
2287 goto cleanup;
2289 else
2291 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2292 gfc_internal_error ("Couldn't set pointee array spec.");
2294 /* Fix the array spec. */
2295 m = gfc_mod_pointee_as (sym->as);
2296 if (m == MATCH_ERROR)
2297 goto cleanup;
2300 goto cleanup;
2302 else
2304 gfc_free_array_spec (cp_as);
2308 /* Procedure pointer as function result. */
2309 if (gfc_current_state () == COMP_FUNCTION
2310 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2311 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2312 strcpy (name, "ppr@");
2314 if (gfc_current_state () == COMP_FUNCTION
2315 && strcmp (name, gfc_current_block ()->name) == 0
2316 && gfc_current_block ()->result
2317 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2318 strcpy (name, "ppr@");
2320 /* OK, we've successfully matched the declaration. Now put the
2321 symbol in the current namespace, because it might be used in the
2322 optional initialization expression for this symbol, e.g. this is
2323 perfectly legal:
2325 integer, parameter :: i = huge(i)
2327 This is only true for parameters or variables of a basic type.
2328 For components of derived types, it is not true, so we don't
2329 create a symbol for those yet. If we fail to create the symbol,
2330 bail out. */
2331 if (!gfc_comp_struct (gfc_current_state ())
2332 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2334 m = MATCH_ERROR;
2335 goto cleanup;
2338 if (!check_function_name (name))
2340 m = MATCH_ERROR;
2341 goto cleanup;
2344 /* We allow old-style initializations of the form
2345 integer i /2/, j(4) /3*3, 1/
2346 (if no colon has been seen). These are different from data
2347 statements in that initializers are only allowed to apply to the
2348 variable immediately preceding, i.e.
2349 integer i, j /1, 2/
2350 is not allowed. Therefore we have to do some work manually, that
2351 could otherwise be left to the matchers for DATA statements. */
2353 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2355 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2356 "initialization at %C"))
2357 return MATCH_ERROR;
2359 /* Allow old style initializations for components of STRUCTUREs and MAPs
2360 but not components of derived types. */
2361 else if (gfc_current_state () == COMP_DERIVED)
2363 gfc_error ("Invalid old style initialization for derived type "
2364 "component at %C");
2365 m = MATCH_ERROR;
2366 goto cleanup;
2369 /* For structure components, read the initializer as a special
2370 expression and let the rest of this function apply the initializer
2371 as usual. */
2372 else if (gfc_comp_struct (gfc_current_state ()))
2374 m = match_clist_expr (&initializer, &current_ts, as);
2375 if (m == MATCH_NO)
2376 gfc_error ("Syntax error in old style initialization of %s at %C",
2377 name);
2378 if (m != MATCH_YES)
2379 goto cleanup;
2382 /* Otherwise we treat the old style initialization just like a
2383 DATA declaration for the current variable. */
2384 else
2385 return match_old_style_init (name);
2388 /* The double colon must be present in order to have initializers.
2389 Otherwise the statement is ambiguous with an assignment statement. */
2390 if (colon_seen)
2392 if (gfc_match (" =>") == MATCH_YES)
2394 if (!current_attr.pointer)
2396 gfc_error ("Initialization at %C isn't for a pointer variable");
2397 m = MATCH_ERROR;
2398 goto cleanup;
2401 m = match_pointer_init (&initializer, 0);
2402 if (m != MATCH_YES)
2403 goto cleanup;
2405 else if (gfc_match_char ('=') == MATCH_YES)
2407 if (current_attr.pointer)
2409 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2410 "not %<=%>");
2411 m = MATCH_ERROR;
2412 goto cleanup;
2415 m = gfc_match_init_expr (&initializer);
2416 if (m == MATCH_NO)
2418 gfc_error ("Expected an initialization expression at %C");
2419 m = MATCH_ERROR;
2422 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2423 && !gfc_comp_struct (gfc_state_stack->state))
2425 gfc_error ("Initialization of variable at %C is not allowed in "
2426 "a PURE procedure");
2427 m = MATCH_ERROR;
2430 if (current_attr.flavor != FL_PARAMETER
2431 && !gfc_comp_struct (gfc_state_stack->state))
2432 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2434 if (m != MATCH_YES)
2435 goto cleanup;
2439 if (initializer != NULL && current_attr.allocatable
2440 && gfc_comp_struct (gfc_current_state ()))
2442 gfc_error ("Initialization of allocatable component at %C is not "
2443 "allowed");
2444 m = MATCH_ERROR;
2445 goto cleanup;
2448 /* Add the initializer. Note that it is fine if initializer is
2449 NULL here, because we sometimes also need to check if a
2450 declaration *must* have an initialization expression. */
2451 if (!gfc_comp_struct (gfc_current_state ()))
2452 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2453 else
2455 if (current_ts.type == BT_DERIVED
2456 && !current_attr.pointer && !initializer)
2457 initializer = gfc_default_initializer (&current_ts);
2458 t = build_struct (name, cl, &initializer, &as);
2460 /* If we match a nested structure definition we expect to see the
2461 * body even if the variable declarations blow up, so we need to keep
2462 * the structure declaration around. */
2463 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2464 gfc_commit_symbol (gfc_new_block);
2467 m = (t) ? MATCH_YES : MATCH_ERROR;
2469 cleanup:
2470 /* Free stuff up and return. */
2471 gfc_free_expr (initializer);
2472 gfc_free_array_spec (as);
2474 return m;
2478 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2479 This assumes that the byte size is equal to the kind number for
2480 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2482 match
2483 gfc_match_old_kind_spec (gfc_typespec *ts)
2485 match m;
2486 int original_kind;
2488 if (gfc_match_char ('*') != MATCH_YES)
2489 return MATCH_NO;
2491 m = gfc_match_small_literal_int (&ts->kind, NULL);
2492 if (m != MATCH_YES)
2493 return MATCH_ERROR;
2495 original_kind = ts->kind;
2497 /* Massage the kind numbers for complex types. */
2498 if (ts->type == BT_COMPLEX)
2500 if (ts->kind % 2)
2502 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2503 gfc_basic_typename (ts->type), original_kind);
2504 return MATCH_ERROR;
2506 ts->kind /= 2;
2510 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2511 ts->kind = 8;
2513 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2515 if (ts->kind == 4)
2517 if (flag_real4_kind == 8)
2518 ts->kind = 8;
2519 if (flag_real4_kind == 10)
2520 ts->kind = 10;
2521 if (flag_real4_kind == 16)
2522 ts->kind = 16;
2525 if (ts->kind == 8)
2527 if (flag_real8_kind == 4)
2528 ts->kind = 4;
2529 if (flag_real8_kind == 10)
2530 ts->kind = 10;
2531 if (flag_real8_kind == 16)
2532 ts->kind = 16;
2536 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2538 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2539 gfc_basic_typename (ts->type), original_kind);
2540 return MATCH_ERROR;
2543 if (!gfc_notify_std (GFC_STD_GNU,
2544 "Nonstandard type declaration %s*%d at %C",
2545 gfc_basic_typename(ts->type), original_kind))
2546 return MATCH_ERROR;
2548 return MATCH_YES;
2552 /* Match a kind specification. Since kinds are generally optional, we
2553 usually return MATCH_NO if something goes wrong. If a "kind="
2554 string is found, then we know we have an error. */
2556 match
2557 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2559 locus where, loc;
2560 gfc_expr *e;
2561 match m, n;
2562 char c;
2564 m = MATCH_NO;
2565 n = MATCH_YES;
2566 e = NULL;
2568 where = loc = gfc_current_locus;
2570 if (kind_expr_only)
2571 goto kind_expr;
2573 if (gfc_match_char ('(') == MATCH_NO)
2574 return MATCH_NO;
2576 /* Also gobbles optional text. */
2577 if (gfc_match (" kind = ") == MATCH_YES)
2578 m = MATCH_ERROR;
2580 loc = gfc_current_locus;
2582 kind_expr:
2583 n = gfc_match_init_expr (&e);
2585 if (n != MATCH_YES)
2587 if (gfc_matching_function)
2589 /* The function kind expression might include use associated or
2590 imported parameters and try again after the specification
2591 expressions..... */
2592 if (gfc_match_char (')') != MATCH_YES)
2594 gfc_error ("Missing right parenthesis at %C");
2595 m = MATCH_ERROR;
2596 goto no_match;
2599 gfc_free_expr (e);
2600 gfc_undo_symbols ();
2601 return MATCH_YES;
2603 else
2605 /* ....or else, the match is real. */
2606 if (n == MATCH_NO)
2607 gfc_error ("Expected initialization expression at %C");
2608 if (n != MATCH_YES)
2609 return MATCH_ERROR;
2613 if (e->rank != 0)
2615 gfc_error ("Expected scalar initialization expression at %C");
2616 m = MATCH_ERROR;
2617 goto no_match;
2620 if (gfc_extract_int (e, &ts->kind, 1))
2622 m = MATCH_ERROR;
2623 goto no_match;
2626 /* Before throwing away the expression, let's see if we had a
2627 C interoperable kind (and store the fact). */
2628 if (e->ts.is_c_interop == 1)
2630 /* Mark this as C interoperable if being declared with one
2631 of the named constants from iso_c_binding. */
2632 ts->is_c_interop = e->ts.is_iso_c;
2633 ts->f90_type = e->ts.f90_type;
2634 if (e->symtree)
2635 ts->interop_kind = e->symtree->n.sym;
2638 gfc_free_expr (e);
2639 e = NULL;
2641 /* Ignore errors to this point, if we've gotten here. This means
2642 we ignore the m=MATCH_ERROR from above. */
2643 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2645 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2646 gfc_basic_typename (ts->type));
2647 gfc_current_locus = where;
2648 return MATCH_ERROR;
2651 /* Warn if, e.g., c_int is used for a REAL variable, but not
2652 if, e.g., c_double is used for COMPLEX as the standard
2653 explicitly says that the kind type parameter for complex and real
2654 variable is the same, i.e. c_float == c_float_complex. */
2655 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2656 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2657 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2658 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2659 "is %s", gfc_basic_typename (ts->f90_type), &where,
2660 gfc_basic_typename (ts->type));
2662 gfc_gobble_whitespace ();
2663 if ((c = gfc_next_ascii_char ()) != ')'
2664 && (ts->type != BT_CHARACTER || c != ','))
2666 if (ts->type == BT_CHARACTER)
2667 gfc_error ("Missing right parenthesis or comma at %C");
2668 else
2669 gfc_error ("Missing right parenthesis at %C");
2670 m = MATCH_ERROR;
2672 else
2673 /* All tests passed. */
2674 m = MATCH_YES;
2676 if(m == MATCH_ERROR)
2677 gfc_current_locus = where;
2679 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2680 ts->kind = 8;
2682 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2684 if (ts->kind == 4)
2686 if (flag_real4_kind == 8)
2687 ts->kind = 8;
2688 if (flag_real4_kind == 10)
2689 ts->kind = 10;
2690 if (flag_real4_kind == 16)
2691 ts->kind = 16;
2694 if (ts->kind == 8)
2696 if (flag_real8_kind == 4)
2697 ts->kind = 4;
2698 if (flag_real8_kind == 10)
2699 ts->kind = 10;
2700 if (flag_real8_kind == 16)
2701 ts->kind = 16;
2705 /* Return what we know from the test(s). */
2706 return m;
2708 no_match:
2709 gfc_free_expr (e);
2710 gfc_current_locus = where;
2711 return m;
2715 static match
2716 match_char_kind (int * kind, int * is_iso_c)
2718 locus where;
2719 gfc_expr *e;
2720 match m, n;
2721 bool fail;
2723 m = MATCH_NO;
2724 e = NULL;
2725 where = gfc_current_locus;
2727 n = gfc_match_init_expr (&e);
2729 if (n != MATCH_YES && gfc_matching_function)
2731 /* The expression might include use-associated or imported
2732 parameters and try again after the specification
2733 expressions. */
2734 gfc_free_expr (e);
2735 gfc_undo_symbols ();
2736 return MATCH_YES;
2739 if (n == MATCH_NO)
2740 gfc_error ("Expected initialization expression at %C");
2741 if (n != MATCH_YES)
2742 return MATCH_ERROR;
2744 if (e->rank != 0)
2746 gfc_error ("Expected scalar initialization expression at %C");
2747 m = MATCH_ERROR;
2748 goto no_match;
2751 fail = gfc_extract_int (e, kind, 1);
2752 *is_iso_c = e->ts.is_iso_c;
2753 if (fail)
2755 m = MATCH_ERROR;
2756 goto no_match;
2759 gfc_free_expr (e);
2761 /* Ignore errors to this point, if we've gotten here. This means
2762 we ignore the m=MATCH_ERROR from above. */
2763 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2765 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2766 m = MATCH_ERROR;
2768 else
2769 /* All tests passed. */
2770 m = MATCH_YES;
2772 if (m == MATCH_ERROR)
2773 gfc_current_locus = where;
2775 /* Return what we know from the test(s). */
2776 return m;
2778 no_match:
2779 gfc_free_expr (e);
2780 gfc_current_locus = where;
2781 return m;
2785 /* Match the various kind/length specifications in a CHARACTER
2786 declaration. We don't return MATCH_NO. */
2788 match
2789 gfc_match_char_spec (gfc_typespec *ts)
2791 int kind, seen_length, is_iso_c;
2792 gfc_charlen *cl;
2793 gfc_expr *len;
2794 match m;
2795 bool deferred;
2797 len = NULL;
2798 seen_length = 0;
2799 kind = 0;
2800 is_iso_c = 0;
2801 deferred = false;
2803 /* Try the old-style specification first. */
2804 old_char_selector = 0;
2806 m = match_char_length (&len, &deferred, true);
2807 if (m != MATCH_NO)
2809 if (m == MATCH_YES)
2810 old_char_selector = 1;
2811 seen_length = 1;
2812 goto done;
2815 m = gfc_match_char ('(');
2816 if (m != MATCH_YES)
2818 m = MATCH_YES; /* Character without length is a single char. */
2819 goto done;
2822 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2823 if (gfc_match (" kind =") == MATCH_YES)
2825 m = match_char_kind (&kind, &is_iso_c);
2827 if (m == MATCH_ERROR)
2828 goto done;
2829 if (m == MATCH_NO)
2830 goto syntax;
2832 if (gfc_match (" , len =") == MATCH_NO)
2833 goto rparen;
2835 m = char_len_param_value (&len, &deferred);
2836 if (m == MATCH_NO)
2837 goto syntax;
2838 if (m == MATCH_ERROR)
2839 goto done;
2840 seen_length = 1;
2842 goto rparen;
2845 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2846 if (gfc_match (" len =") == MATCH_YES)
2848 m = char_len_param_value (&len, &deferred);
2849 if (m == MATCH_NO)
2850 goto syntax;
2851 if (m == MATCH_ERROR)
2852 goto done;
2853 seen_length = 1;
2855 if (gfc_match_char (')') == MATCH_YES)
2856 goto done;
2858 if (gfc_match (" , kind =") != MATCH_YES)
2859 goto syntax;
2861 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2862 goto done;
2864 goto rparen;
2867 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2868 m = char_len_param_value (&len, &deferred);
2869 if (m == MATCH_NO)
2870 goto syntax;
2871 if (m == MATCH_ERROR)
2872 goto done;
2873 seen_length = 1;
2875 m = gfc_match_char (')');
2876 if (m == MATCH_YES)
2877 goto done;
2879 if (gfc_match_char (',') != MATCH_YES)
2880 goto syntax;
2882 gfc_match (" kind ="); /* Gobble optional text. */
2884 m = match_char_kind (&kind, &is_iso_c);
2885 if (m == MATCH_ERROR)
2886 goto done;
2887 if (m == MATCH_NO)
2888 goto syntax;
2890 rparen:
2891 /* Require a right-paren at this point. */
2892 m = gfc_match_char (')');
2893 if (m == MATCH_YES)
2894 goto done;
2896 syntax:
2897 gfc_error ("Syntax error in CHARACTER declaration at %C");
2898 m = MATCH_ERROR;
2899 gfc_free_expr (len);
2900 return m;
2902 done:
2903 /* Deal with character functions after USE and IMPORT statements. */
2904 if (gfc_matching_function)
2906 gfc_free_expr (len);
2907 gfc_undo_symbols ();
2908 return MATCH_YES;
2911 if (m != MATCH_YES)
2913 gfc_free_expr (len);
2914 return m;
2917 /* Do some final massaging of the length values. */
2918 cl = gfc_new_charlen (gfc_current_ns, NULL);
2920 if (seen_length == 0)
2921 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2922 else
2923 cl->length = len;
2925 ts->u.cl = cl;
2926 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2927 ts->deferred = deferred;
2929 /* We have to know if it was a C interoperable kind so we can
2930 do accurate type checking of bind(c) procs, etc. */
2931 if (kind != 0)
2932 /* Mark this as C interoperable if being declared with one
2933 of the named constants from iso_c_binding. */
2934 ts->is_c_interop = is_iso_c;
2935 else if (len != NULL)
2936 /* Here, we might have parsed something such as: character(c_char)
2937 In this case, the parsing code above grabs the c_char when
2938 looking for the length (line 1690, roughly). it's the last
2939 testcase for parsing the kind params of a character variable.
2940 However, it's not actually the length. this seems like it
2941 could be an error.
2942 To see if the user used a C interop kind, test the expr
2943 of the so called length, and see if it's C interoperable. */
2944 ts->is_c_interop = len->ts.is_iso_c;
2946 return MATCH_YES;
2950 /* Matches a RECORD declaration. */
2952 static match
2953 match_record_decl (char *name)
2955 locus old_loc;
2956 old_loc = gfc_current_locus;
2957 match m;
2959 m = gfc_match (" record /");
2960 if (m == MATCH_YES)
2962 if (!flag_dec_structure)
2964 gfc_current_locus = old_loc;
2965 gfc_error ("RECORD at %C is an extension, enable it with "
2966 "-fdec-structure");
2967 return MATCH_ERROR;
2969 m = gfc_match (" %n/", name);
2970 if (m == MATCH_YES)
2971 return MATCH_YES;
2974 gfc_current_locus = old_loc;
2975 if (flag_dec_structure
2976 && (gfc_match (" record% ") == MATCH_YES
2977 || gfc_match (" record%t") == MATCH_YES))
2978 gfc_error ("Structure name expected after RECORD at %C");
2979 if (m == MATCH_NO)
2980 return MATCH_NO;
2982 return MATCH_ERROR;
2985 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2986 structure to the matched specification. This is necessary for FUNCTION and
2987 IMPLICIT statements.
2989 If implicit_flag is nonzero, then we don't check for the optional
2990 kind specification. Not doing so is needed for matching an IMPLICIT
2991 statement correctly. */
2993 match
2994 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2996 char name[GFC_MAX_SYMBOL_LEN + 1];
2997 gfc_symbol *sym, *dt_sym;
2998 match m;
2999 char c;
3000 bool seen_deferred_kind, matched_type;
3001 const char *dt_name;
3003 /* A belt and braces check that the typespec is correctly being treated
3004 as a deferred characteristic association. */
3005 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
3006 && (gfc_current_block ()->result->ts.kind == -1)
3007 && (ts->kind == -1);
3008 gfc_clear_ts (ts);
3009 if (seen_deferred_kind)
3010 ts->kind = -1;
3012 /* Clear the current binding label, in case one is given. */
3013 curr_binding_label = NULL;
3015 if (gfc_match (" byte") == MATCH_YES)
3017 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
3018 return MATCH_ERROR;
3020 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
3022 gfc_error ("BYTE type used at %C "
3023 "is not available on the target machine");
3024 return MATCH_ERROR;
3027 ts->type = BT_INTEGER;
3028 ts->kind = 1;
3029 return MATCH_YES;
3033 m = gfc_match (" type (");
3034 matched_type = (m == MATCH_YES);
3035 if (matched_type)
3037 gfc_gobble_whitespace ();
3038 if (gfc_peek_ascii_char () == '*')
3040 if ((m = gfc_match ("*)")) != MATCH_YES)
3041 return m;
3042 if (gfc_comp_struct (gfc_current_state ()))
3044 gfc_error ("Assumed type at %C is not allowed for components");
3045 return MATCH_ERROR;
3047 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
3048 "at %C"))
3049 return MATCH_ERROR;
3050 ts->type = BT_ASSUMED;
3051 return MATCH_YES;
3054 m = gfc_match ("%n", name);
3055 matched_type = (m == MATCH_YES);
3058 if ((matched_type && strcmp ("integer", name) == 0)
3059 || (!matched_type && gfc_match (" integer") == MATCH_YES))
3061 ts->type = BT_INTEGER;
3062 ts->kind = gfc_default_integer_kind;
3063 goto get_kind;
3066 if ((matched_type && strcmp ("character", name) == 0)
3067 || (!matched_type && gfc_match (" character") == MATCH_YES))
3069 if (matched_type
3070 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3071 "intrinsic-type-spec at %C"))
3072 return MATCH_ERROR;
3074 ts->type = BT_CHARACTER;
3075 if (implicit_flag == 0)
3076 m = gfc_match_char_spec (ts);
3077 else
3078 m = MATCH_YES;
3080 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
3081 m = MATCH_ERROR;
3083 return m;
3086 if ((matched_type && strcmp ("real", name) == 0)
3087 || (!matched_type && gfc_match (" real") == MATCH_YES))
3089 ts->type = BT_REAL;
3090 ts->kind = gfc_default_real_kind;
3091 goto get_kind;
3094 if ((matched_type
3095 && (strcmp ("doubleprecision", name) == 0
3096 || (strcmp ("double", name) == 0
3097 && gfc_match (" precision") == MATCH_YES)))
3098 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
3100 if (matched_type
3101 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3102 "intrinsic-type-spec at %C"))
3103 return MATCH_ERROR;
3104 if (matched_type && gfc_match_char (')') != MATCH_YES)
3105 return MATCH_ERROR;
3107 ts->type = BT_REAL;
3108 ts->kind = gfc_default_double_kind;
3109 return MATCH_YES;
3112 if ((matched_type && strcmp ("complex", name) == 0)
3113 || (!matched_type && gfc_match (" complex") == MATCH_YES))
3115 ts->type = BT_COMPLEX;
3116 ts->kind = gfc_default_complex_kind;
3117 goto get_kind;
3120 if ((matched_type
3121 && (strcmp ("doublecomplex", name) == 0
3122 || (strcmp ("double", name) == 0
3123 && gfc_match (" complex") == MATCH_YES)))
3124 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
3126 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
3127 return MATCH_ERROR;
3129 if (matched_type
3130 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3131 "intrinsic-type-spec at %C"))
3132 return MATCH_ERROR;
3134 if (matched_type && gfc_match_char (')') != MATCH_YES)
3135 return MATCH_ERROR;
3137 ts->type = BT_COMPLEX;
3138 ts->kind = gfc_default_double_kind;
3139 return MATCH_YES;
3142 if ((matched_type && strcmp ("logical", name) == 0)
3143 || (!matched_type && gfc_match (" logical") == MATCH_YES))
3145 ts->type = BT_LOGICAL;
3146 ts->kind = gfc_default_logical_kind;
3147 goto get_kind;
3150 if (matched_type)
3151 m = gfc_match_char (')');
3153 if (m != MATCH_YES)
3154 m = match_record_decl (name);
3156 if (matched_type || m == MATCH_YES)
3158 ts->type = BT_DERIVED;
3159 /* We accept record/s/ or type(s) where s is a structure, but we
3160 * don't need all the extra derived-type stuff for structures. */
3161 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
3163 gfc_error ("Type name %qs at %C is ambiguous", name);
3164 return MATCH_ERROR;
3166 if (sym && sym->attr.flavor == FL_STRUCT)
3168 ts->u.derived = sym;
3169 return MATCH_YES;
3171 /* Actually a derived type. */
3174 else
3176 /* Match nested STRUCTURE declarations; only valid within another
3177 structure declaration. */
3178 if (flag_dec_structure
3179 && (gfc_current_state () == COMP_STRUCTURE
3180 || gfc_current_state () == COMP_MAP))
3182 m = gfc_match (" structure");
3183 if (m == MATCH_YES)
3185 m = gfc_match_structure_decl ();
3186 if (m == MATCH_YES)
3188 /* gfc_new_block is updated by match_structure_decl. */
3189 ts->type = BT_DERIVED;
3190 ts->u.derived = gfc_new_block;
3191 return MATCH_YES;
3194 if (m == MATCH_ERROR)
3195 return MATCH_ERROR;
3198 /* Match CLASS declarations. */
3199 m = gfc_match (" class ( * )");
3200 if (m == MATCH_ERROR)
3201 return MATCH_ERROR;
3202 else if (m == MATCH_YES)
3204 gfc_symbol *upe;
3205 gfc_symtree *st;
3206 ts->type = BT_CLASS;
3207 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
3208 if (upe == NULL)
3210 upe = gfc_new_symbol ("STAR", gfc_current_ns);
3211 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
3212 st->n.sym = upe;
3213 gfc_set_sym_referenced (upe);
3214 upe->refs++;
3215 upe->ts.type = BT_VOID;
3216 upe->attr.unlimited_polymorphic = 1;
3217 /* This is essential to force the construction of
3218 unlimited polymorphic component class containers. */
3219 upe->attr.zero_comp = 1;
3220 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
3221 &gfc_current_locus))
3222 return MATCH_ERROR;
3224 else
3226 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
3227 st->n.sym = upe;
3228 upe->refs++;
3230 ts->u.derived = upe;
3231 return m;
3234 m = gfc_match (" class ( %n )", name);
3235 if (m != MATCH_YES)
3236 return m;
3237 ts->type = BT_CLASS;
3239 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
3240 return MATCH_ERROR;
3243 /* Defer association of the derived type until the end of the
3244 specification block. However, if the derived type can be
3245 found, add it to the typespec. */
3246 if (gfc_matching_function)
3248 ts->u.derived = NULL;
3249 if (gfc_current_state () != COMP_INTERFACE
3250 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
3252 sym = gfc_find_dt_in_generic (sym);
3253 ts->u.derived = sym;
3255 return MATCH_YES;
3258 /* Search for the name but allow the components to be defined later. If
3259 type = -1, this typespec has been seen in a function declaration but
3260 the type could not be accessed at that point. The actual derived type is
3261 stored in a symtree with the first letter of the name capitalized; the
3262 symtree with the all lower-case name contains the associated
3263 generic function. */
3264 dt_name = gfc_dt_upper_string (name);
3265 sym = NULL;
3266 dt_sym = NULL;
3267 if (ts->kind != -1)
3269 gfc_get_ha_symbol (name, &sym);
3270 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
3272 gfc_error ("Type name %qs at %C is ambiguous", name);
3273 return MATCH_ERROR;
3275 if (sym->generic && !dt_sym)
3276 dt_sym = gfc_find_dt_in_generic (sym);
3278 else if (ts->kind == -1)
3280 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
3281 || gfc_current_ns->has_import_set;
3282 gfc_find_symbol (name, NULL, iface, &sym);
3283 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
3285 gfc_error ("Type name %qs at %C is ambiguous", name);
3286 return MATCH_ERROR;
3288 if (sym && sym->generic && !dt_sym)
3289 dt_sym = gfc_find_dt_in_generic (sym);
3291 ts->kind = 0;
3292 if (sym == NULL)
3293 return MATCH_NO;
3296 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
3297 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
3298 || sym->attr.subroutine)
3300 gfc_error ("Type name %qs at %C conflicts with previously declared "
3301 "entity at %L, which has the same name", name,
3302 &sym->declared_at);
3303 return MATCH_ERROR;
3306 gfc_save_symbol_data (sym);
3307 gfc_set_sym_referenced (sym);
3308 if (!sym->attr.generic
3309 && !gfc_add_generic (&sym->attr, sym->name, NULL))
3310 return MATCH_ERROR;
3312 if (!sym->attr.function
3313 && !gfc_add_function (&sym->attr, sym->name, NULL))
3314 return MATCH_ERROR;
3316 if (!dt_sym)
3318 gfc_interface *intr, *head;
3320 /* Use upper case to save the actual derived-type symbol. */
3321 gfc_get_symbol (dt_name, NULL, &dt_sym);
3322 dt_sym->name = gfc_get_string ("%s", sym->name);
3323 head = sym->generic;
3324 intr = gfc_get_interface ();
3325 intr->sym = dt_sym;
3326 intr->where = gfc_current_locus;
3327 intr->next = head;
3328 sym->generic = intr;
3329 sym->attr.if_source = IFSRC_DECL;
3331 else
3332 gfc_save_symbol_data (dt_sym);
3334 gfc_set_sym_referenced (dt_sym);
3336 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
3337 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
3338 return MATCH_ERROR;
3340 ts->u.derived = dt_sym;
3342 return MATCH_YES;
3344 get_kind:
3345 if (matched_type
3346 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3347 "intrinsic-type-spec at %C"))
3348 return MATCH_ERROR;
3350 /* For all types except double, derived and character, look for an
3351 optional kind specifier. MATCH_NO is actually OK at this point. */
3352 if (implicit_flag == 1)
3354 if (matched_type && gfc_match_char (')') != MATCH_YES)
3355 return MATCH_ERROR;
3357 return MATCH_YES;
3360 if (gfc_current_form == FORM_FREE)
3362 c = gfc_peek_ascii_char ();
3363 if (!gfc_is_whitespace (c) && c != '*' && c != '('
3364 && c != ':' && c != ',')
3366 if (matched_type && c == ')')
3368 gfc_next_ascii_char ();
3369 return MATCH_YES;
3371 return MATCH_NO;
3375 m = gfc_match_kind_spec (ts, false);
3376 if (m == MATCH_NO && ts->type != BT_CHARACTER)
3378 m = gfc_match_old_kind_spec (ts);
3379 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
3380 return MATCH_ERROR;
3383 if (matched_type && gfc_match_char (')') != MATCH_YES)
3384 return MATCH_ERROR;
3386 /* Defer association of the KIND expression of function results
3387 until after USE and IMPORT statements. */
3388 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
3389 || gfc_matching_function)
3390 return MATCH_YES;
3392 if (m == MATCH_NO)
3393 m = MATCH_YES; /* No kind specifier found. */
3395 return m;
3399 /* Match an IMPLICIT NONE statement. Actually, this statement is
3400 already matched in parse.c, or we would not end up here in the
3401 first place. So the only thing we need to check, is if there is
3402 trailing garbage. If not, the match is successful. */
3404 match
3405 gfc_match_implicit_none (void)
3407 char c;
3408 match m;
3409 char name[GFC_MAX_SYMBOL_LEN + 1];
3410 bool type = false;
3411 bool external = false;
3412 locus cur_loc = gfc_current_locus;
3414 if (gfc_current_ns->seen_implicit_none
3415 || gfc_current_ns->has_implicit_none_export)
3417 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3418 return MATCH_ERROR;
3421 gfc_gobble_whitespace ();
3422 c = gfc_peek_ascii_char ();
3423 if (c == '(')
3425 (void) gfc_next_ascii_char ();
3426 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3427 return MATCH_ERROR;
3429 gfc_gobble_whitespace ();
3430 if (gfc_peek_ascii_char () == ')')
3432 (void) gfc_next_ascii_char ();
3433 type = true;
3435 else
3436 for(;;)
3438 m = gfc_match (" %n", name);
3439 if (m != MATCH_YES)
3440 return MATCH_ERROR;
3442 if (strcmp (name, "type") == 0)
3443 type = true;
3444 else if (strcmp (name, "external") == 0)
3445 external = true;
3446 else
3447 return MATCH_ERROR;
3449 gfc_gobble_whitespace ();
3450 c = gfc_next_ascii_char ();
3451 if (c == ',')
3452 continue;
3453 if (c == ')')
3454 break;
3455 return MATCH_ERROR;
3458 else
3459 type = true;
3461 if (gfc_match_eos () != MATCH_YES)
3462 return MATCH_ERROR;
3464 gfc_set_implicit_none (type, external, &cur_loc);
3466 return MATCH_YES;
3470 /* Match the letter range(s) of an IMPLICIT statement. */
3472 static match
3473 match_implicit_range (void)
3475 char c, c1, c2;
3476 int inner;
3477 locus cur_loc;
3479 cur_loc = gfc_current_locus;
3481 gfc_gobble_whitespace ();
3482 c = gfc_next_ascii_char ();
3483 if (c != '(')
3485 gfc_error ("Missing character range in IMPLICIT at %C");
3486 goto bad;
3489 inner = 1;
3490 while (inner)
3492 gfc_gobble_whitespace ();
3493 c1 = gfc_next_ascii_char ();
3494 if (!ISALPHA (c1))
3495 goto bad;
3497 gfc_gobble_whitespace ();
3498 c = gfc_next_ascii_char ();
3500 switch (c)
3502 case ')':
3503 inner = 0; /* Fall through. */
3505 case ',':
3506 c2 = c1;
3507 break;
3509 case '-':
3510 gfc_gobble_whitespace ();
3511 c2 = gfc_next_ascii_char ();
3512 if (!ISALPHA (c2))
3513 goto bad;
3515 gfc_gobble_whitespace ();
3516 c = gfc_next_ascii_char ();
3518 if ((c != ',') && (c != ')'))
3519 goto bad;
3520 if (c == ')')
3521 inner = 0;
3523 break;
3525 default:
3526 goto bad;
3529 if (c1 > c2)
3531 gfc_error ("Letters must be in alphabetic order in "
3532 "IMPLICIT statement at %C");
3533 goto bad;
3536 /* See if we can add the newly matched range to the pending
3537 implicits from this IMPLICIT statement. We do not check for
3538 conflicts with whatever earlier IMPLICIT statements may have
3539 set. This is done when we've successfully finished matching
3540 the current one. */
3541 if (!gfc_add_new_implicit_range (c1, c2))
3542 goto bad;
3545 return MATCH_YES;
3547 bad:
3548 gfc_syntax_error (ST_IMPLICIT);
3550 gfc_current_locus = cur_loc;
3551 return MATCH_ERROR;
3555 /* Match an IMPLICIT statement, storing the types for
3556 gfc_set_implicit() if the statement is accepted by the parser.
3557 There is a strange looking, but legal syntactic construction
3558 possible. It looks like:
3560 IMPLICIT INTEGER (a-b) (c-d)
3562 This is legal if "a-b" is a constant expression that happens to
3563 equal one of the legal kinds for integers. The real problem
3564 happens with an implicit specification that looks like:
3566 IMPLICIT INTEGER (a-b)
3568 In this case, a typespec matcher that is "greedy" (as most of the
3569 matchers are) gobbles the character range as a kindspec, leaving
3570 nothing left. We therefore have to go a bit more slowly in the
3571 matching process by inhibiting the kindspec checking during
3572 typespec matching and checking for a kind later. */
3574 match
3575 gfc_match_implicit (void)
3577 gfc_typespec ts;
3578 locus cur_loc;
3579 char c;
3580 match m;
3582 if (gfc_current_ns->seen_implicit_none)
3584 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3585 "statement");
3586 return MATCH_ERROR;
3589 gfc_clear_ts (&ts);
3591 /* We don't allow empty implicit statements. */
3592 if (gfc_match_eos () == MATCH_YES)
3594 gfc_error ("Empty IMPLICIT statement at %C");
3595 return MATCH_ERROR;
3600 /* First cleanup. */
3601 gfc_clear_new_implicit ();
3603 /* A basic type is mandatory here. */
3604 m = gfc_match_decl_type_spec (&ts, 1);
3605 if (m == MATCH_ERROR)
3606 goto error;
3607 if (m == MATCH_NO)
3608 goto syntax;
3610 cur_loc = gfc_current_locus;
3611 m = match_implicit_range ();
3613 if (m == MATCH_YES)
3615 /* We may have <TYPE> (<RANGE>). */
3616 gfc_gobble_whitespace ();
3617 c = gfc_peek_ascii_char ();
3618 if (c == ',' || c == '\n' || c == ';' || c == '!')
3620 /* Check for CHARACTER with no length parameter. */
3621 if (ts.type == BT_CHARACTER && !ts.u.cl)
3623 ts.kind = gfc_default_character_kind;
3624 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3625 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3626 NULL, 1);
3629 /* Record the Successful match. */
3630 if (!gfc_merge_new_implicit (&ts))
3631 return MATCH_ERROR;
3632 if (c == ',')
3633 c = gfc_next_ascii_char ();
3634 else if (gfc_match_eos () == MATCH_ERROR)
3635 goto error;
3636 continue;
3639 gfc_current_locus = cur_loc;
3642 /* Discard the (incorrectly) matched range. */
3643 gfc_clear_new_implicit ();
3645 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3646 if (ts.type == BT_CHARACTER)
3647 m = gfc_match_char_spec (&ts);
3648 else
3650 m = gfc_match_kind_spec (&ts, false);
3651 if (m == MATCH_NO)
3653 m = gfc_match_old_kind_spec (&ts);
3654 if (m == MATCH_ERROR)
3655 goto error;
3656 if (m == MATCH_NO)
3657 goto syntax;
3660 if (m == MATCH_ERROR)
3661 goto error;
3663 m = match_implicit_range ();
3664 if (m == MATCH_ERROR)
3665 goto error;
3666 if (m == MATCH_NO)
3667 goto syntax;
3669 gfc_gobble_whitespace ();
3670 c = gfc_next_ascii_char ();
3671 if (c != ',' && gfc_match_eos () != MATCH_YES)
3672 goto syntax;
3674 if (!gfc_merge_new_implicit (&ts))
3675 return MATCH_ERROR;
3677 while (c == ',');
3679 return MATCH_YES;
3681 syntax:
3682 gfc_syntax_error (ST_IMPLICIT);
3684 error:
3685 return MATCH_ERROR;
3689 match
3690 gfc_match_import (void)
3692 char name[GFC_MAX_SYMBOL_LEN + 1];
3693 match m;
3694 gfc_symbol *sym;
3695 gfc_symtree *st;
3697 if (gfc_current_ns->proc_name == NULL
3698 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3700 gfc_error ("IMPORT statement at %C only permitted in "
3701 "an INTERFACE body");
3702 return MATCH_ERROR;
3705 if (gfc_current_ns->proc_name->attr.module_procedure)
3707 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3708 "in a module procedure interface body");
3709 return MATCH_ERROR;
3712 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3713 return MATCH_ERROR;
3715 if (gfc_match_eos () == MATCH_YES)
3717 /* All host variables should be imported. */
3718 gfc_current_ns->has_import_set = 1;
3719 return MATCH_YES;
3722 if (gfc_match (" ::") == MATCH_YES)
3724 if (gfc_match_eos () == MATCH_YES)
3726 gfc_error ("Expecting list of named entities at %C");
3727 return MATCH_ERROR;
3731 for(;;)
3733 sym = NULL;
3734 m = gfc_match (" %n", name);
3735 switch (m)
3737 case MATCH_YES:
3738 if (gfc_current_ns->parent != NULL
3739 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3741 gfc_error ("Type name %qs at %C is ambiguous", name);
3742 return MATCH_ERROR;
3744 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3745 && gfc_find_symbol (name,
3746 gfc_current_ns->proc_name->ns->parent,
3747 1, &sym))
3749 gfc_error ("Type name %qs at %C is ambiguous", name);
3750 return MATCH_ERROR;
3753 if (sym == NULL)
3755 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3756 "at %C - does not exist.", name);
3757 return MATCH_ERROR;
3760 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3762 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3763 "at %C", name);
3764 goto next_item;
3767 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3768 st->n.sym = sym;
3769 sym->refs++;
3770 sym->attr.imported = 1;
3772 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3774 /* The actual derived type is stored in a symtree with the first
3775 letter of the name capitalized; the symtree with the all
3776 lower-case name contains the associated generic function. */
3777 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3778 gfc_dt_upper_string (name));
3779 st->n.sym = sym;
3780 sym->refs++;
3781 sym->attr.imported = 1;
3784 goto next_item;
3786 case MATCH_NO:
3787 break;
3789 case MATCH_ERROR:
3790 return MATCH_ERROR;
3793 next_item:
3794 if (gfc_match_eos () == MATCH_YES)
3795 break;
3796 if (gfc_match_char (',') != MATCH_YES)
3797 goto syntax;
3800 return MATCH_YES;
3802 syntax:
3803 gfc_error ("Syntax error in IMPORT statement at %C");
3804 return MATCH_ERROR;
3808 /* A minimal implementation of gfc_match without whitespace, escape
3809 characters or variable arguments. Returns true if the next
3810 characters match the TARGET template exactly. */
3812 static bool
3813 match_string_p (const char *target)
3815 const char *p;
3817 for (p = target; *p; p++)
3818 if ((char) gfc_next_ascii_char () != *p)
3819 return false;
3820 return true;
3823 /* Matches an attribute specification including array specs. If
3824 successful, leaves the variables current_attr and current_as
3825 holding the specification. Also sets the colon_seen variable for
3826 later use by matchers associated with initializations.
3828 This subroutine is a little tricky in the sense that we don't know
3829 if we really have an attr-spec until we hit the double colon.
3830 Until that time, we can only return MATCH_NO. This forces us to
3831 check for duplicate specification at this level. */
3833 static match
3834 match_attr_spec (void)
3836 /* Modifiers that can exist in a type statement. */
3837 enum
3838 { GFC_DECL_BEGIN = 0,
3839 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3840 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3841 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3842 DECL_STATIC, DECL_AUTOMATIC,
3843 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3844 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3845 DECL_NONE, GFC_DECL_END /* Sentinel */
3848 /* GFC_DECL_END is the sentinel, index starts at 0. */
3849 #define NUM_DECL GFC_DECL_END
3851 locus start, seen_at[NUM_DECL];
3852 int seen[NUM_DECL];
3853 unsigned int d;
3854 const char *attr;
3855 match m;
3856 bool t;
3858 gfc_clear_attr (&current_attr);
3859 start = gfc_current_locus;
3861 current_as = NULL;
3862 colon_seen = 0;
3864 /* See if we get all of the keywords up to the final double colon. */
3865 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3866 seen[d] = 0;
3868 for (;;)
3870 char ch;
3872 d = DECL_NONE;
3873 gfc_gobble_whitespace ();
3875 ch = gfc_next_ascii_char ();
3876 if (ch == ':')
3878 /* This is the successful exit condition for the loop. */
3879 if (gfc_next_ascii_char () == ':')
3880 break;
3882 else if (ch == ',')
3884 gfc_gobble_whitespace ();
3885 switch (gfc_peek_ascii_char ())
3887 case 'a':
3888 gfc_next_ascii_char ();
3889 switch (gfc_next_ascii_char ())
3891 case 'l':
3892 if (match_string_p ("locatable"))
3894 /* Matched "allocatable". */
3895 d = DECL_ALLOCATABLE;
3897 break;
3899 case 's':
3900 if (match_string_p ("ynchronous"))
3902 /* Matched "asynchronous". */
3903 d = DECL_ASYNCHRONOUS;
3905 break;
3907 case 'u':
3908 if (match_string_p ("tomatic"))
3910 /* Matched "automatic". */
3911 d = DECL_AUTOMATIC;
3913 break;
3915 break;
3917 case 'b':
3918 /* Try and match the bind(c). */
3919 m = gfc_match_bind_c (NULL, true);
3920 if (m == MATCH_YES)
3921 d = DECL_IS_BIND_C;
3922 else if (m == MATCH_ERROR)
3923 goto cleanup;
3924 break;
3926 case 'c':
3927 gfc_next_ascii_char ();
3928 if ('o' != gfc_next_ascii_char ())
3929 break;
3930 switch (gfc_next_ascii_char ())
3932 case 'd':
3933 if (match_string_p ("imension"))
3935 d = DECL_CODIMENSION;
3936 break;
3938 /* FALLTHRU */
3939 case 'n':
3940 if (match_string_p ("tiguous"))
3942 d = DECL_CONTIGUOUS;
3943 break;
3946 break;
3948 case 'd':
3949 if (match_string_p ("dimension"))
3950 d = DECL_DIMENSION;
3951 break;
3953 case 'e':
3954 if (match_string_p ("external"))
3955 d = DECL_EXTERNAL;
3956 break;
3958 case 'i':
3959 if (match_string_p ("int"))
3961 ch = gfc_next_ascii_char ();
3962 if (ch == 'e')
3964 if (match_string_p ("nt"))
3966 /* Matched "intent". */
3967 /* TODO: Call match_intent_spec from here. */
3968 if (gfc_match (" ( in out )") == MATCH_YES)
3969 d = DECL_INOUT;
3970 else if (gfc_match (" ( in )") == MATCH_YES)
3971 d = DECL_IN;
3972 else if (gfc_match (" ( out )") == MATCH_YES)
3973 d = DECL_OUT;
3976 else if (ch == 'r')
3978 if (match_string_p ("insic"))
3980 /* Matched "intrinsic". */
3981 d = DECL_INTRINSIC;
3985 break;
3987 case 'o':
3988 if (match_string_p ("optional"))
3989 d = DECL_OPTIONAL;
3990 break;
3992 case 'p':
3993 gfc_next_ascii_char ();
3994 switch (gfc_next_ascii_char ())
3996 case 'a':
3997 if (match_string_p ("rameter"))
3999 /* Matched "parameter". */
4000 d = DECL_PARAMETER;
4002 break;
4004 case 'o':
4005 if (match_string_p ("inter"))
4007 /* Matched "pointer". */
4008 d = DECL_POINTER;
4010 break;
4012 case 'r':
4013 ch = gfc_next_ascii_char ();
4014 if (ch == 'i')
4016 if (match_string_p ("vate"))
4018 /* Matched "private". */
4019 d = DECL_PRIVATE;
4022 else if (ch == 'o')
4024 if (match_string_p ("tected"))
4026 /* Matched "protected". */
4027 d = DECL_PROTECTED;
4030 break;
4032 case 'u':
4033 if (match_string_p ("blic"))
4035 /* Matched "public". */
4036 d = DECL_PUBLIC;
4038 break;
4040 break;
4042 case 's':
4043 gfc_next_ascii_char ();
4044 switch (gfc_next_ascii_char ())
4046 case 'a':
4047 if (match_string_p ("ve"))
4049 /* Matched "save". */
4050 d = DECL_SAVE;
4052 break;
4054 case 't':
4055 if (match_string_p ("atic"))
4057 /* Matched "static". */
4058 d = DECL_STATIC;
4060 break;
4062 break;
4064 case 't':
4065 if (match_string_p ("target"))
4066 d = DECL_TARGET;
4067 break;
4069 case 'v':
4070 gfc_next_ascii_char ();
4071 ch = gfc_next_ascii_char ();
4072 if (ch == 'a')
4074 if (match_string_p ("lue"))
4076 /* Matched "value". */
4077 d = DECL_VALUE;
4080 else if (ch == 'o')
4082 if (match_string_p ("latile"))
4084 /* Matched "volatile". */
4085 d = DECL_VOLATILE;
4088 break;
4092 /* No double colon and no recognizable decl_type, so assume that
4093 we've been looking at something else the whole time. */
4094 if (d == DECL_NONE)
4096 m = MATCH_NO;
4097 goto cleanup;
4100 /* Check to make sure any parens are paired up correctly. */
4101 if (gfc_match_parens () == MATCH_ERROR)
4103 m = MATCH_ERROR;
4104 goto cleanup;
4107 seen[d]++;
4108 seen_at[d] = gfc_current_locus;
4110 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
4112 gfc_array_spec *as = NULL;
4114 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
4115 d == DECL_CODIMENSION);
4117 if (current_as == NULL)
4118 current_as = as;
4119 else if (m == MATCH_YES)
4121 if (!merge_array_spec (as, current_as, false))
4122 m = MATCH_ERROR;
4123 free (as);
4126 if (m == MATCH_NO)
4128 if (d == DECL_CODIMENSION)
4129 gfc_error ("Missing codimension specification at %C");
4130 else
4131 gfc_error ("Missing dimension specification at %C");
4132 m = MATCH_ERROR;
4135 if (m == MATCH_ERROR)
4136 goto cleanup;
4140 /* Since we've seen a double colon, we have to be looking at an
4141 attr-spec. This means that we can now issue errors. */
4142 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4143 if (seen[d] > 1)
4145 switch (d)
4147 case DECL_ALLOCATABLE:
4148 attr = "ALLOCATABLE";
4149 break;
4150 case DECL_ASYNCHRONOUS:
4151 attr = "ASYNCHRONOUS";
4152 break;
4153 case DECL_CODIMENSION:
4154 attr = "CODIMENSION";
4155 break;
4156 case DECL_CONTIGUOUS:
4157 attr = "CONTIGUOUS";
4158 break;
4159 case DECL_DIMENSION:
4160 attr = "DIMENSION";
4161 break;
4162 case DECL_EXTERNAL:
4163 attr = "EXTERNAL";
4164 break;
4165 case DECL_IN:
4166 attr = "INTENT (IN)";
4167 break;
4168 case DECL_OUT:
4169 attr = "INTENT (OUT)";
4170 break;
4171 case DECL_INOUT:
4172 attr = "INTENT (IN OUT)";
4173 break;
4174 case DECL_INTRINSIC:
4175 attr = "INTRINSIC";
4176 break;
4177 case DECL_OPTIONAL:
4178 attr = "OPTIONAL";
4179 break;
4180 case DECL_PARAMETER:
4181 attr = "PARAMETER";
4182 break;
4183 case DECL_POINTER:
4184 attr = "POINTER";
4185 break;
4186 case DECL_PROTECTED:
4187 attr = "PROTECTED";
4188 break;
4189 case DECL_PRIVATE:
4190 attr = "PRIVATE";
4191 break;
4192 case DECL_PUBLIC:
4193 attr = "PUBLIC";
4194 break;
4195 case DECL_SAVE:
4196 attr = "SAVE";
4197 break;
4198 case DECL_STATIC:
4199 attr = "STATIC";
4200 break;
4201 case DECL_AUTOMATIC:
4202 attr = "AUTOMATIC";
4203 break;
4204 case DECL_TARGET:
4205 attr = "TARGET";
4206 break;
4207 case DECL_IS_BIND_C:
4208 attr = "IS_BIND_C";
4209 break;
4210 case DECL_VALUE:
4211 attr = "VALUE";
4212 break;
4213 case DECL_VOLATILE:
4214 attr = "VOLATILE";
4215 break;
4216 default:
4217 attr = NULL; /* This shouldn't happen. */
4220 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
4221 m = MATCH_ERROR;
4222 goto cleanup;
4225 /* Now that we've dealt with duplicate attributes, add the attributes
4226 to the current attribute. */
4227 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4229 if (seen[d] == 0)
4230 continue;
4232 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
4233 && !flag_dec_static)
4235 gfc_error ("%s at %L is a DEC extension, enable with "
4236 "%<-fdec-static%>",
4237 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
4238 m = MATCH_ERROR;
4239 goto cleanup;
4241 /* Allow SAVE with STATIC, but don't complain. */
4242 if (d == DECL_STATIC && seen[DECL_SAVE])
4243 continue;
4245 if (gfc_current_state () == COMP_DERIVED
4246 && d != DECL_DIMENSION && d != DECL_CODIMENSION
4247 && d != DECL_POINTER && d != DECL_PRIVATE
4248 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
4250 if (d == DECL_ALLOCATABLE)
4252 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
4253 "attribute at %C in a TYPE definition"))
4255 m = MATCH_ERROR;
4256 goto cleanup;
4259 else
4261 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
4262 &seen_at[d]);
4263 m = MATCH_ERROR;
4264 goto cleanup;
4268 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
4269 && gfc_current_state () != COMP_MODULE)
4271 if (d == DECL_PRIVATE)
4272 attr = "PRIVATE";
4273 else
4274 attr = "PUBLIC";
4275 if (gfc_current_state () == COMP_DERIVED
4276 && gfc_state_stack->previous
4277 && gfc_state_stack->previous->state == COMP_MODULE)
4279 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
4280 "at %L in a TYPE definition", attr,
4281 &seen_at[d]))
4283 m = MATCH_ERROR;
4284 goto cleanup;
4287 else
4289 gfc_error ("%s attribute at %L is not allowed outside of the "
4290 "specification part of a module", attr, &seen_at[d]);
4291 m = MATCH_ERROR;
4292 goto cleanup;
4296 switch (d)
4298 case DECL_ALLOCATABLE:
4299 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
4300 break;
4302 case DECL_ASYNCHRONOUS:
4303 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
4304 t = false;
4305 else
4306 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
4307 break;
4309 case DECL_CODIMENSION:
4310 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
4311 break;
4313 case DECL_CONTIGUOUS:
4314 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
4315 t = false;
4316 else
4317 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
4318 break;
4320 case DECL_DIMENSION:
4321 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
4322 break;
4324 case DECL_EXTERNAL:
4325 t = gfc_add_external (&current_attr, &seen_at[d]);
4326 break;
4328 case DECL_IN:
4329 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
4330 break;
4332 case DECL_OUT:
4333 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
4334 break;
4336 case DECL_INOUT:
4337 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
4338 break;
4340 case DECL_INTRINSIC:
4341 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
4342 break;
4344 case DECL_OPTIONAL:
4345 t = gfc_add_optional (&current_attr, &seen_at[d]);
4346 break;
4348 case DECL_PARAMETER:
4349 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
4350 break;
4352 case DECL_POINTER:
4353 t = gfc_add_pointer (&current_attr, &seen_at[d]);
4354 break;
4356 case DECL_PROTECTED:
4357 if (gfc_current_state () != COMP_MODULE
4358 || (gfc_current_ns->proc_name
4359 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
4361 gfc_error ("PROTECTED at %C only allowed in specification "
4362 "part of a module");
4363 t = false;
4364 break;
4367 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
4368 t = false;
4369 else
4370 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
4371 break;
4373 case DECL_PRIVATE:
4374 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
4375 &seen_at[d]);
4376 break;
4378 case DECL_PUBLIC:
4379 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
4380 &seen_at[d]);
4381 break;
4383 case DECL_STATIC:
4384 case DECL_SAVE:
4385 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
4386 break;
4388 case DECL_AUTOMATIC:
4389 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
4390 break;
4392 case DECL_TARGET:
4393 t = gfc_add_target (&current_attr, &seen_at[d]);
4394 break;
4396 case DECL_IS_BIND_C:
4397 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
4398 break;
4400 case DECL_VALUE:
4401 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
4402 t = false;
4403 else
4404 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
4405 break;
4407 case DECL_VOLATILE:
4408 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
4409 t = false;
4410 else
4411 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
4412 break;
4414 default:
4415 gfc_internal_error ("match_attr_spec(): Bad attribute");
4418 if (!t)
4420 m = MATCH_ERROR;
4421 goto cleanup;
4425 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4426 if ((gfc_current_state () == COMP_MODULE
4427 || gfc_current_state () == COMP_SUBMODULE)
4428 && !current_attr.save
4429 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4430 current_attr.save = SAVE_IMPLICIT;
4432 colon_seen = 1;
4433 return MATCH_YES;
4435 cleanup:
4436 gfc_current_locus = start;
4437 gfc_free_array_spec (current_as);
4438 current_as = NULL;
4439 return m;
4443 /* Set the binding label, dest_label, either with the binding label
4444 stored in the given gfc_typespec, ts, or if none was provided, it
4445 will be the symbol name in all lower case, as required by the draft
4446 (J3/04-007, section 15.4.1). If a binding label was given and
4447 there is more than one argument (num_idents), it is an error. */
4449 static bool
4450 set_binding_label (const char **dest_label, const char *sym_name,
4451 int num_idents)
4453 if (num_idents > 1 && has_name_equals)
4455 gfc_error ("Multiple identifiers provided with "
4456 "single NAME= specifier at %C");
4457 return false;
4460 if (curr_binding_label)
4461 /* Binding label given; store in temp holder till have sym. */
4462 *dest_label = curr_binding_label;
4463 else
4465 /* No binding label given, and the NAME= specifier did not exist,
4466 which means there was no NAME="". */
4467 if (sym_name != NULL && has_name_equals == 0)
4468 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4471 return true;
4475 /* Set the status of the given common block as being BIND(C) or not,
4476 depending on the given parameter, is_bind_c. */
4478 void
4479 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4481 com_block->is_bind_c = is_bind_c;
4482 return;
4486 /* Verify that the given gfc_typespec is for a C interoperable type. */
4488 bool
4489 gfc_verify_c_interop (gfc_typespec *ts)
4491 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4492 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4493 ? true : false;
4494 else if (ts->type == BT_CLASS)
4495 return false;
4496 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4497 return false;
4499 return true;
4503 /* Verify that the variables of a given common block, which has been
4504 defined with the attribute specifier bind(c), to be of a C
4505 interoperable type. Errors will be reported here, if
4506 encountered. */
4508 bool
4509 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4511 gfc_symbol *curr_sym = NULL;
4512 bool retval = true;
4514 curr_sym = com_block->head;
4516 /* Make sure we have at least one symbol. */
4517 if (curr_sym == NULL)
4518 return retval;
4520 /* Here we know we have a symbol, so we'll execute this loop
4521 at least once. */
4524 /* The second to last param, 1, says this is in a common block. */
4525 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4526 curr_sym = curr_sym->common_next;
4527 } while (curr_sym != NULL);
4529 return retval;
4533 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4534 an appropriate error message is reported. */
4536 bool
4537 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4538 int is_in_common, gfc_common_head *com_block)
4540 bool bind_c_function = false;
4541 bool retval = true;
4543 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4544 bind_c_function = true;
4546 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4548 tmp_sym = tmp_sym->result;
4549 /* Make sure it wasn't an implicitly typed result. */
4550 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4552 gfc_warning (OPT_Wc_binding_type,
4553 "Implicitly declared BIND(C) function %qs at "
4554 "%L may not be C interoperable", tmp_sym->name,
4555 &tmp_sym->declared_at);
4556 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4557 /* Mark it as C interoperable to prevent duplicate warnings. */
4558 tmp_sym->ts.is_c_interop = 1;
4559 tmp_sym->attr.is_c_interop = 1;
4563 /* Here, we know we have the bind(c) attribute, so if we have
4564 enough type info, then verify that it's a C interop kind.
4565 The info could be in the symbol already, or possibly still in
4566 the given ts (current_ts), so look in both. */
4567 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4569 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4571 /* See if we're dealing with a sym in a common block or not. */
4572 if (is_in_common == 1 && warn_c_binding_type)
4574 gfc_warning (OPT_Wc_binding_type,
4575 "Variable %qs in common block %qs at %L "
4576 "may not be a C interoperable "
4577 "kind though common block %qs is BIND(C)",
4578 tmp_sym->name, com_block->name,
4579 &(tmp_sym->declared_at), com_block->name);
4581 else
4583 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4584 gfc_error ("Type declaration %qs at %L is not C "
4585 "interoperable but it is BIND(C)",
4586 tmp_sym->name, &(tmp_sym->declared_at));
4587 else if (warn_c_binding_type)
4588 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4589 "may not be a C interoperable "
4590 "kind but it is BIND(C)",
4591 tmp_sym->name, &(tmp_sym->declared_at));
4595 /* Variables declared w/in a common block can't be bind(c)
4596 since there's no way for C to see these variables, so there's
4597 semantically no reason for the attribute. */
4598 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4600 gfc_error ("Variable %qs in common block %qs at "
4601 "%L cannot be declared with BIND(C) "
4602 "since it is not a global",
4603 tmp_sym->name, com_block->name,
4604 &(tmp_sym->declared_at));
4605 retval = false;
4608 /* Scalar variables that are bind(c) can not have the pointer
4609 or allocatable attributes. */
4610 if (tmp_sym->attr.is_bind_c == 1)
4612 if (tmp_sym->attr.pointer == 1)
4614 gfc_error ("Variable %qs at %L cannot have both the "
4615 "POINTER and BIND(C) attributes",
4616 tmp_sym->name, &(tmp_sym->declared_at));
4617 retval = false;
4620 if (tmp_sym->attr.allocatable == 1)
4622 gfc_error ("Variable %qs at %L cannot have both the "
4623 "ALLOCATABLE and BIND(C) attributes",
4624 tmp_sym->name, &(tmp_sym->declared_at));
4625 retval = false;
4630 /* If it is a BIND(C) function, make sure the return value is a
4631 scalar value. The previous tests in this function made sure
4632 the type is interoperable. */
4633 if (bind_c_function && tmp_sym->as != NULL)
4634 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4635 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4637 /* BIND(C) functions can not return a character string. */
4638 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4639 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4640 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4641 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4642 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4643 "be a character string", tmp_sym->name,
4644 &(tmp_sym->declared_at));
4647 /* See if the symbol has been marked as private. If it has, make sure
4648 there is no binding label and warn the user if there is one. */
4649 if (tmp_sym->attr.access == ACCESS_PRIVATE
4650 && tmp_sym->binding_label)
4651 /* Use gfc_warning_now because we won't say that the symbol fails
4652 just because of this. */
4653 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4654 "given the binding label %qs", tmp_sym->name,
4655 &(tmp_sym->declared_at), tmp_sym->binding_label);
4657 return retval;
4661 /* Set the appropriate fields for a symbol that's been declared as
4662 BIND(C) (the is_bind_c flag and the binding label), and verify that
4663 the type is C interoperable. Errors are reported by the functions
4664 used to set/test these fields. */
4666 bool
4667 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4669 bool retval = true;
4671 /* TODO: Do we need to make sure the vars aren't marked private? */
4673 /* Set the is_bind_c bit in symbol_attribute. */
4674 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4676 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4677 return false;
4679 return retval;
4683 /* Set the fields marking the given common block as BIND(C), including
4684 a binding label, and report any errors encountered. */
4686 bool
4687 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4689 bool retval = true;
4691 /* destLabel, common name, typespec (which may have binding label). */
4692 if (!set_binding_label (&com_block->binding_label, com_block->name,
4693 num_idents))
4694 return false;
4696 /* Set the given common block (com_block) to being bind(c) (1). */
4697 set_com_block_bind_c (com_block, 1);
4699 return retval;
4703 /* Retrieve the list of one or more identifiers that the given bind(c)
4704 attribute applies to. */
4706 bool
4707 get_bind_c_idents (void)
4709 char name[GFC_MAX_SYMBOL_LEN + 1];
4710 int num_idents = 0;
4711 gfc_symbol *tmp_sym = NULL;
4712 match found_id;
4713 gfc_common_head *com_block = NULL;
4715 if (gfc_match_name (name) == MATCH_YES)
4717 found_id = MATCH_YES;
4718 gfc_get_ha_symbol (name, &tmp_sym);
4720 else if (match_common_name (name) == MATCH_YES)
4722 found_id = MATCH_YES;
4723 com_block = gfc_get_common (name, 0);
4725 else
4727 gfc_error ("Need either entity or common block name for "
4728 "attribute specification statement at %C");
4729 return false;
4732 /* Save the current identifier and look for more. */
4735 /* Increment the number of identifiers found for this spec stmt. */
4736 num_idents++;
4738 /* Make sure we have a sym or com block, and verify that it can
4739 be bind(c). Set the appropriate field(s) and look for more
4740 identifiers. */
4741 if (tmp_sym != NULL || com_block != NULL)
4743 if (tmp_sym != NULL)
4745 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4746 return false;
4748 else
4750 if (!set_verify_bind_c_com_block (com_block, num_idents))
4751 return false;
4754 /* Look to see if we have another identifier. */
4755 tmp_sym = NULL;
4756 if (gfc_match_eos () == MATCH_YES)
4757 found_id = MATCH_NO;
4758 else if (gfc_match_char (',') != MATCH_YES)
4759 found_id = MATCH_NO;
4760 else if (gfc_match_name (name) == MATCH_YES)
4762 found_id = MATCH_YES;
4763 gfc_get_ha_symbol (name, &tmp_sym);
4765 else if (match_common_name (name) == MATCH_YES)
4767 found_id = MATCH_YES;
4768 com_block = gfc_get_common (name, 0);
4770 else
4772 gfc_error ("Missing entity or common block name for "
4773 "attribute specification statement at %C");
4774 return false;
4777 else
4779 gfc_internal_error ("Missing symbol");
4781 } while (found_id == MATCH_YES);
4783 /* if we get here we were successful */
4784 return true;
4788 /* Try and match a BIND(C) attribute specification statement. */
4790 match
4791 gfc_match_bind_c_stmt (void)
4793 match found_match = MATCH_NO;
4794 gfc_typespec *ts;
4796 ts = &current_ts;
4798 /* This may not be necessary. */
4799 gfc_clear_ts (ts);
4800 /* Clear the temporary binding label holder. */
4801 curr_binding_label = NULL;
4803 /* Look for the bind(c). */
4804 found_match = gfc_match_bind_c (NULL, true);
4806 if (found_match == MATCH_YES)
4808 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4809 return MATCH_ERROR;
4811 /* Look for the :: now, but it is not required. */
4812 gfc_match (" :: ");
4814 /* Get the identifier(s) that needs to be updated. This may need to
4815 change to hand the flag(s) for the attr specified so all identifiers
4816 found can have all appropriate parts updated (assuming that the same
4817 spec stmt can have multiple attrs, such as both bind(c) and
4818 allocatable...). */
4819 if (!get_bind_c_idents ())
4820 /* Error message should have printed already. */
4821 return MATCH_ERROR;
4824 return found_match;
4828 /* Match a data declaration statement. */
4830 match
4831 gfc_match_data_decl (void)
4833 gfc_symbol *sym;
4834 match m;
4835 int elem;
4837 num_idents_on_line = 0;
4839 m = gfc_match_decl_type_spec (&current_ts, 0);
4840 if (m != MATCH_YES)
4841 return m;
4843 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4844 && !gfc_comp_struct (gfc_current_state ()))
4846 sym = gfc_use_derived (current_ts.u.derived);
4848 if (sym == NULL)
4850 m = MATCH_ERROR;
4851 goto cleanup;
4854 current_ts.u.derived = sym;
4857 m = match_attr_spec ();
4858 if (m == MATCH_ERROR)
4860 m = MATCH_NO;
4861 goto cleanup;
4864 if (current_ts.type == BT_CLASS
4865 && current_ts.u.derived->attr.unlimited_polymorphic)
4866 goto ok;
4868 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4869 && current_ts.u.derived->components == NULL
4870 && !current_ts.u.derived->attr.zero_comp)
4873 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
4874 goto ok;
4876 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED
4877 && current_ts.u.derived == gfc_current_block ())
4878 goto ok;
4880 gfc_find_symbol (current_ts.u.derived->name,
4881 current_ts.u.derived->ns, 1, &sym);
4883 /* Any symbol that we find had better be a type definition
4884 which has its components defined, or be a structure definition
4885 actively being parsed. */
4886 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
4887 && (current_ts.u.derived->components != NULL
4888 || current_ts.u.derived->attr.zero_comp
4889 || current_ts.u.derived == gfc_new_block))
4890 goto ok;
4892 gfc_error ("Derived type at %C has not been previously defined "
4893 "and so cannot appear in a derived type definition");
4894 m = MATCH_ERROR;
4895 goto cleanup;
4899 /* If we have an old-style character declaration, and no new-style
4900 attribute specifications, then there a comma is optional between
4901 the type specification and the variable list. */
4902 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4903 gfc_match_char (',');
4905 /* Give the types/attributes to symbols that follow. Give the element
4906 a number so that repeat character length expressions can be copied. */
4907 elem = 1;
4908 for (;;)
4910 num_idents_on_line++;
4911 m = variable_decl (elem++);
4912 if (m == MATCH_ERROR)
4913 goto cleanup;
4914 if (m == MATCH_NO)
4915 break;
4917 if (gfc_match_eos () == MATCH_YES)
4918 goto cleanup;
4919 if (gfc_match_char (',') != MATCH_YES)
4920 break;
4923 if (!gfc_error_flag_test ())
4925 /* An anonymous structure declaration is unambiguous; if we matched one
4926 according to gfc_match_structure_decl, we need to return MATCH_YES
4927 here to avoid confusing the remaining matchers, even if there was an
4928 error during variable_decl. We must flush any such errors. Note this
4929 causes the parser to gracefully continue parsing the remaining input
4930 as a structure body, which likely follows. */
4931 if (current_ts.type == BT_DERIVED && current_ts.u.derived
4932 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
4934 gfc_error_now ("Syntax error in anonymous structure declaration"
4935 " at %C");
4936 /* Skip the bad variable_decl and line up for the start of the
4937 structure body. */
4938 gfc_error_recovery ();
4939 m = MATCH_YES;
4940 goto cleanup;
4943 gfc_error ("Syntax error in data declaration at %C");
4946 m = MATCH_ERROR;
4948 gfc_free_data_all (gfc_current_ns);
4950 cleanup:
4951 gfc_free_array_spec (current_as);
4952 current_as = NULL;
4953 return m;
4957 /* Match a prefix associated with a function or subroutine
4958 declaration. If the typespec pointer is nonnull, then a typespec
4959 can be matched. Note that if nothing matches, MATCH_YES is
4960 returned (the null string was matched). */
4962 match
4963 gfc_match_prefix (gfc_typespec *ts)
4965 bool seen_type;
4966 bool seen_impure;
4967 bool found_prefix;
4969 gfc_clear_attr (&current_attr);
4970 seen_type = false;
4971 seen_impure = false;
4973 gcc_assert (!gfc_matching_prefix);
4974 gfc_matching_prefix = true;
4978 found_prefix = false;
4980 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
4981 corresponding attribute seems natural and distinguishes these
4982 procedures from procedure types of PROC_MODULE, which these are
4983 as well. */
4984 if (gfc_match ("module% ") == MATCH_YES)
4986 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4987 goto error;
4989 current_attr.module_procedure = 1;
4990 found_prefix = true;
4993 if (!seen_type && ts != NULL
4994 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4995 && gfc_match_space () == MATCH_YES)
4998 seen_type = true;
4999 found_prefix = true;
5002 if (gfc_match ("elemental% ") == MATCH_YES)
5004 if (!gfc_add_elemental (&current_attr, NULL))
5005 goto error;
5007 found_prefix = true;
5010 if (gfc_match ("pure% ") == MATCH_YES)
5012 if (!gfc_add_pure (&current_attr, NULL))
5013 goto error;
5015 found_prefix = true;
5018 if (gfc_match ("recursive% ") == MATCH_YES)
5020 if (!gfc_add_recursive (&current_attr, NULL))
5021 goto error;
5023 found_prefix = true;
5026 /* IMPURE is a somewhat special case, as it needs not set an actual
5027 attribute but rather only prevents ELEMENTAL routines from being
5028 automatically PURE. */
5029 if (gfc_match ("impure% ") == MATCH_YES)
5031 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
5032 goto error;
5034 seen_impure = true;
5035 found_prefix = true;
5038 while (found_prefix);
5040 /* IMPURE and PURE must not both appear, of course. */
5041 if (seen_impure && current_attr.pure)
5043 gfc_error ("PURE and IMPURE must not appear both at %C");
5044 goto error;
5047 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
5048 if (!seen_impure && current_attr.elemental && !current_attr.pure)
5050 if (!gfc_add_pure (&current_attr, NULL))
5051 goto error;
5054 /* At this point, the next item is not a prefix. */
5055 gcc_assert (gfc_matching_prefix);
5057 gfc_matching_prefix = false;
5058 return MATCH_YES;
5060 error:
5061 gcc_assert (gfc_matching_prefix);
5062 gfc_matching_prefix = false;
5063 return MATCH_ERROR;
5067 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
5069 static bool
5070 copy_prefix (symbol_attribute *dest, locus *where)
5072 if (dest->module_procedure)
5074 if (current_attr.elemental)
5075 dest->elemental = 1;
5077 if (current_attr.pure)
5078 dest->pure = 1;
5080 if (current_attr.recursive)
5081 dest->recursive = 1;
5083 /* Module procedures are unusual in that the 'dest' is copied from
5084 the interface declaration. However, this is an oportunity to
5085 check that the submodule declaration is compliant with the
5086 interface. */
5087 if (dest->elemental && !current_attr.elemental)
5089 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
5090 "missing at %L", where);
5091 return false;
5094 if (dest->pure && !current_attr.pure)
5096 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
5097 "missing at %L", where);
5098 return false;
5101 if (dest->recursive && !current_attr.recursive)
5103 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
5104 "missing at %L", where);
5105 return false;
5108 return true;
5111 if (current_attr.elemental && !gfc_add_elemental (dest, where))
5112 return false;
5114 if (current_attr.pure && !gfc_add_pure (dest, where))
5115 return false;
5117 if (current_attr.recursive && !gfc_add_recursive (dest, where))
5118 return false;
5120 return true;
5124 /* Match a formal argument list. */
5126 match
5127 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
5129 gfc_formal_arglist *head, *tail, *p, *q;
5130 char name[GFC_MAX_SYMBOL_LEN + 1];
5131 gfc_symbol *sym;
5132 match m;
5133 gfc_formal_arglist *formal = NULL;
5135 head = tail = NULL;
5137 /* Keep the interface formal argument list and null it so that the
5138 matching for the new declaration can be done. The numbers and
5139 names of the arguments are checked here. The interface formal
5140 arguments are retained in formal_arglist and the characteristics
5141 are compared in resolve.c(resolve_fl_procedure). See the remark
5142 in get_proc_name about the eventual need to copy the formal_arglist
5143 and populate the formal namespace of the interface symbol. */
5144 if (progname->attr.module_procedure
5145 && progname->attr.host_assoc)
5147 formal = progname->formal;
5148 progname->formal = NULL;
5151 if (gfc_match_char ('(') != MATCH_YES)
5153 if (null_flag)
5154 goto ok;
5155 return MATCH_NO;
5158 if (gfc_match_char (')') == MATCH_YES)
5159 goto ok;
5161 for (;;)
5163 if (gfc_match_char ('*') == MATCH_YES)
5165 sym = NULL;
5166 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
5167 "at %C"))
5169 m = MATCH_ERROR;
5170 goto cleanup;
5173 else
5175 m = gfc_match_name (name);
5176 if (m != MATCH_YES)
5177 goto cleanup;
5179 if (gfc_get_symbol (name, NULL, &sym))
5180 goto cleanup;
5183 p = gfc_get_formal_arglist ();
5185 if (head == NULL)
5186 head = tail = p;
5187 else
5189 tail->next = p;
5190 tail = p;
5193 tail->sym = sym;
5195 /* We don't add the VARIABLE flavor because the name could be a
5196 dummy procedure. We don't apply these attributes to formal
5197 arguments of statement functions. */
5198 if (sym != NULL && !st_flag
5199 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
5200 || !gfc_missing_attr (&sym->attr, NULL)))
5202 m = MATCH_ERROR;
5203 goto cleanup;
5206 /* The name of a program unit can be in a different namespace,
5207 so check for it explicitly. After the statement is accepted,
5208 the name is checked for especially in gfc_get_symbol(). */
5209 if (gfc_new_block != NULL && sym != NULL
5210 && strcmp (sym->name, gfc_new_block->name) == 0)
5212 gfc_error ("Name %qs at %C is the name of the procedure",
5213 sym->name);
5214 m = MATCH_ERROR;
5215 goto cleanup;
5218 if (gfc_match_char (')') == MATCH_YES)
5219 goto ok;
5221 m = gfc_match_char (',');
5222 if (m != MATCH_YES)
5224 gfc_error ("Unexpected junk in formal argument list at %C");
5225 goto cleanup;
5230 /* Check for duplicate symbols in the formal argument list. */
5231 if (head != NULL)
5233 for (p = head; p->next; p = p->next)
5235 if (p->sym == NULL)
5236 continue;
5238 for (q = p->next; q; q = q->next)
5239 if (p->sym == q->sym)
5241 gfc_error ("Duplicate symbol %qs in formal argument list "
5242 "at %C", p->sym->name);
5244 m = MATCH_ERROR;
5245 goto cleanup;
5250 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
5252 m = MATCH_ERROR;
5253 goto cleanup;
5256 /* gfc_error_now used in following and return with MATCH_YES because
5257 doing otherwise results in a cascade of extraneous errors and in
5258 some cases an ICE in symbol.c(gfc_release_symbol). */
5259 if (progname->attr.module_procedure && progname->attr.host_assoc)
5261 bool arg_count_mismatch = false;
5263 if (!formal && head)
5264 arg_count_mismatch = true;
5266 /* Abbreviated module procedure declaration is not meant to have any
5267 formal arguments! */
5268 if (!progname->abr_modproc_decl && formal && !head)
5269 arg_count_mismatch = true;
5271 for (p = formal, q = head; p && q; p = p->next, q = q->next)
5273 if ((p->next != NULL && q->next == NULL)
5274 || (p->next == NULL && q->next != NULL))
5275 arg_count_mismatch = true;
5276 else if ((p->sym == NULL && q->sym == NULL)
5277 || strcmp (p->sym->name, q->sym->name) == 0)
5278 continue;
5279 else
5280 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
5281 "argument names (%s/%s) at %C",
5282 p->sym->name, q->sym->name);
5285 if (arg_count_mismatch)
5286 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
5287 "formal arguments at %C");
5290 return MATCH_YES;
5292 cleanup:
5293 gfc_free_formal_arglist (head);
5294 return m;
5298 /* Match a RESULT specification following a function declaration or
5299 ENTRY statement. Also matches the end-of-statement. */
5301 static match
5302 match_result (gfc_symbol *function, gfc_symbol **result)
5304 char name[GFC_MAX_SYMBOL_LEN + 1];
5305 gfc_symbol *r;
5306 match m;
5308 if (gfc_match (" result (") != MATCH_YES)
5309 return MATCH_NO;
5311 m = gfc_match_name (name);
5312 if (m != MATCH_YES)
5313 return m;
5315 /* Get the right paren, and that's it because there could be the
5316 bind(c) attribute after the result clause. */
5317 if (gfc_match_char (')') != MATCH_YES)
5319 /* TODO: should report the missing right paren here. */
5320 return MATCH_ERROR;
5323 if (strcmp (function->name, name) == 0)
5325 gfc_error ("RESULT variable at %C must be different than function name");
5326 return MATCH_ERROR;
5329 if (gfc_get_symbol (name, NULL, &r))
5330 return MATCH_ERROR;
5332 if (!gfc_add_result (&r->attr, r->name, NULL))
5333 return MATCH_ERROR;
5335 *result = r;
5337 return MATCH_YES;
5341 /* Match a function suffix, which could be a combination of a result
5342 clause and BIND(C), either one, or neither. The draft does not
5343 require them to come in a specific order. */
5345 match
5346 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
5348 match is_bind_c; /* Found bind(c). */
5349 match is_result; /* Found result clause. */
5350 match found_match; /* Status of whether we've found a good match. */
5351 char peek_char; /* Character we're going to peek at. */
5352 bool allow_binding_name;
5354 /* Initialize to having found nothing. */
5355 found_match = MATCH_NO;
5356 is_bind_c = MATCH_NO;
5357 is_result = MATCH_NO;
5359 /* Get the next char to narrow between result and bind(c). */
5360 gfc_gobble_whitespace ();
5361 peek_char = gfc_peek_ascii_char ();
5363 /* C binding names are not allowed for internal procedures. */
5364 if (gfc_current_state () == COMP_CONTAINS
5365 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5366 allow_binding_name = false;
5367 else
5368 allow_binding_name = true;
5370 switch (peek_char)
5372 case 'r':
5373 /* Look for result clause. */
5374 is_result = match_result (sym, result);
5375 if (is_result == MATCH_YES)
5377 /* Now see if there is a bind(c) after it. */
5378 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5379 /* We've found the result clause and possibly bind(c). */
5380 found_match = MATCH_YES;
5382 else
5383 /* This should only be MATCH_ERROR. */
5384 found_match = is_result;
5385 break;
5386 case 'b':
5387 /* Look for bind(c) first. */
5388 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5389 if (is_bind_c == MATCH_YES)
5391 /* Now see if a result clause followed it. */
5392 is_result = match_result (sym, result);
5393 found_match = MATCH_YES;
5395 else
5397 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
5398 found_match = MATCH_ERROR;
5400 break;
5401 default:
5402 gfc_error ("Unexpected junk after function declaration at %C");
5403 found_match = MATCH_ERROR;
5404 break;
5407 if (is_bind_c == MATCH_YES)
5409 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
5410 if (gfc_current_state () == COMP_CONTAINS
5411 && sym->ns->proc_name->attr.flavor != FL_MODULE
5412 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5413 "at %L may not be specified for an internal "
5414 "procedure", &gfc_current_locus))
5415 return MATCH_ERROR;
5417 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
5418 return MATCH_ERROR;
5421 return found_match;
5425 /* Procedure pointer return value without RESULT statement:
5426 Add "hidden" result variable named "ppr@". */
5428 static bool
5429 add_hidden_procptr_result (gfc_symbol *sym)
5431 bool case1,case2;
5433 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
5434 return false;
5436 /* First usage case: PROCEDURE and EXTERNAL statements. */
5437 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
5438 && strcmp (gfc_current_block ()->name, sym->name) == 0
5439 && sym->attr.external;
5440 /* Second usage case: INTERFACE statements. */
5441 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
5442 && gfc_state_stack->previous->state == COMP_FUNCTION
5443 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
5445 if (case1 || case2)
5447 gfc_symtree *stree;
5448 if (case1)
5449 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
5450 else if (case2)
5452 gfc_symtree *st2;
5453 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
5454 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
5455 st2->n.sym = stree->n.sym;
5456 stree->n.sym->refs++;
5458 sym->result = stree->n.sym;
5460 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
5461 sym->result->attr.pointer = sym->attr.pointer;
5462 sym->result->attr.external = sym->attr.external;
5463 sym->result->attr.referenced = sym->attr.referenced;
5464 sym->result->ts = sym->ts;
5465 sym->attr.proc_pointer = 0;
5466 sym->attr.pointer = 0;
5467 sym->attr.external = 0;
5468 if (sym->result->attr.external && sym->result->attr.pointer)
5470 sym->result->attr.pointer = 0;
5471 sym->result->attr.proc_pointer = 1;
5474 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
5476 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5477 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
5478 && sym->result && sym->result != sym && sym->result->attr.external
5479 && sym == gfc_current_ns->proc_name
5480 && sym == sym->result->ns->proc_name
5481 && strcmp ("ppr@", sym->result->name) == 0)
5483 sym->result->attr.proc_pointer = 1;
5484 sym->attr.pointer = 0;
5485 return true;
5487 else
5488 return false;
5492 /* Match the interface for a PROCEDURE declaration,
5493 including brackets (R1212). */
5495 static match
5496 match_procedure_interface (gfc_symbol **proc_if)
5498 match m;
5499 gfc_symtree *st;
5500 locus old_loc, entry_loc;
5501 gfc_namespace *old_ns = gfc_current_ns;
5502 char name[GFC_MAX_SYMBOL_LEN + 1];
5504 old_loc = entry_loc = gfc_current_locus;
5505 gfc_clear_ts (&current_ts);
5507 if (gfc_match (" (") != MATCH_YES)
5509 gfc_current_locus = entry_loc;
5510 return MATCH_NO;
5513 /* Get the type spec. for the procedure interface. */
5514 old_loc = gfc_current_locus;
5515 m = gfc_match_decl_type_spec (&current_ts, 0);
5516 gfc_gobble_whitespace ();
5517 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
5518 goto got_ts;
5520 if (m == MATCH_ERROR)
5521 return m;
5523 /* Procedure interface is itself a procedure. */
5524 gfc_current_locus = old_loc;
5525 m = gfc_match_name (name);
5527 /* First look to see if it is already accessible in the current
5528 namespace because it is use associated or contained. */
5529 st = NULL;
5530 if (gfc_find_sym_tree (name, NULL, 0, &st))
5531 return MATCH_ERROR;
5533 /* If it is still not found, then try the parent namespace, if it
5534 exists and create the symbol there if it is still not found. */
5535 if (gfc_current_ns->parent)
5536 gfc_current_ns = gfc_current_ns->parent;
5537 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5538 return MATCH_ERROR;
5540 gfc_current_ns = old_ns;
5541 *proc_if = st->n.sym;
5543 if (*proc_if)
5545 (*proc_if)->refs++;
5546 /* Resolve interface if possible. That way, attr.procedure is only set
5547 if it is declared by a later procedure-declaration-stmt, which is
5548 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5549 while ((*proc_if)->ts.interface
5550 && *proc_if != (*proc_if)->ts.interface)
5551 *proc_if = (*proc_if)->ts.interface;
5553 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5554 && (*proc_if)->ts.type == BT_UNKNOWN
5555 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5556 (*proc_if)->name, NULL))
5557 return MATCH_ERROR;
5560 got_ts:
5561 if (gfc_match (" )") != MATCH_YES)
5563 gfc_current_locus = entry_loc;
5564 return MATCH_NO;
5567 return MATCH_YES;
5571 /* Match a PROCEDURE declaration (R1211). */
5573 static match
5574 match_procedure_decl (void)
5576 match m;
5577 gfc_symbol *sym, *proc_if = NULL;
5578 int num;
5579 gfc_expr *initializer = NULL;
5581 /* Parse interface (with brackets). */
5582 m = match_procedure_interface (&proc_if);
5583 if (m != MATCH_YES)
5584 return m;
5586 /* Parse attributes (with colons). */
5587 m = match_attr_spec();
5588 if (m == MATCH_ERROR)
5589 return MATCH_ERROR;
5591 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5593 current_attr.is_bind_c = 1;
5594 has_name_equals = 0;
5595 curr_binding_label = NULL;
5598 /* Get procedure symbols. */
5599 for(num=1;;num++)
5601 m = gfc_match_symbol (&sym, 0);
5602 if (m == MATCH_NO)
5603 goto syntax;
5604 else if (m == MATCH_ERROR)
5605 return m;
5607 /* Add current_attr to the symbol attributes. */
5608 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5609 return MATCH_ERROR;
5611 if (sym->attr.is_bind_c)
5613 /* Check for C1218. */
5614 if (!proc_if || !proc_if->attr.is_bind_c)
5616 gfc_error ("BIND(C) attribute at %C requires "
5617 "an interface with BIND(C)");
5618 return MATCH_ERROR;
5620 /* Check for C1217. */
5621 if (has_name_equals && sym->attr.pointer)
5623 gfc_error ("BIND(C) procedure with NAME may not have "
5624 "POINTER attribute at %C");
5625 return MATCH_ERROR;
5627 if (has_name_equals && sym->attr.dummy)
5629 gfc_error ("Dummy procedure at %C may not have "
5630 "BIND(C) attribute with NAME");
5631 return MATCH_ERROR;
5633 /* Set binding label for BIND(C). */
5634 if (!set_binding_label (&sym->binding_label, sym->name, num))
5635 return MATCH_ERROR;
5638 if (!gfc_add_external (&sym->attr, NULL))
5639 return MATCH_ERROR;
5641 if (add_hidden_procptr_result (sym))
5642 sym = sym->result;
5644 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5645 return MATCH_ERROR;
5647 /* Set interface. */
5648 if (proc_if != NULL)
5650 if (sym->ts.type != BT_UNKNOWN)
5652 gfc_error ("Procedure %qs at %L already has basic type of %s",
5653 sym->name, &gfc_current_locus,
5654 gfc_basic_typename (sym->ts.type));
5655 return MATCH_ERROR;
5657 sym->ts.interface = proc_if;
5658 sym->attr.untyped = 1;
5659 sym->attr.if_source = IFSRC_IFBODY;
5661 else if (current_ts.type != BT_UNKNOWN)
5663 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5664 return MATCH_ERROR;
5665 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5666 sym->ts.interface->ts = current_ts;
5667 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5668 sym->ts.interface->attr.function = 1;
5669 sym->attr.function = 1;
5670 sym->attr.if_source = IFSRC_UNKNOWN;
5673 if (gfc_match (" =>") == MATCH_YES)
5675 if (!current_attr.pointer)
5677 gfc_error ("Initialization at %C isn't for a pointer variable");
5678 m = MATCH_ERROR;
5679 goto cleanup;
5682 m = match_pointer_init (&initializer, 1);
5683 if (m != MATCH_YES)
5684 goto cleanup;
5686 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5687 goto cleanup;
5691 if (gfc_match_eos () == MATCH_YES)
5692 return MATCH_YES;
5693 if (gfc_match_char (',') != MATCH_YES)
5694 goto syntax;
5697 syntax:
5698 gfc_error ("Syntax error in PROCEDURE statement at %C");
5699 return MATCH_ERROR;
5701 cleanup:
5702 /* Free stuff up and return. */
5703 gfc_free_expr (initializer);
5704 return m;
5708 static match
5709 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5712 /* Match a procedure pointer component declaration (R445). */
5714 static match
5715 match_ppc_decl (void)
5717 match m;
5718 gfc_symbol *proc_if = NULL;
5719 gfc_typespec ts;
5720 int num;
5721 gfc_component *c;
5722 gfc_expr *initializer = NULL;
5723 gfc_typebound_proc* tb;
5724 char name[GFC_MAX_SYMBOL_LEN + 1];
5726 /* Parse interface (with brackets). */
5727 m = match_procedure_interface (&proc_if);
5728 if (m != MATCH_YES)
5729 goto syntax;
5731 /* Parse attributes. */
5732 tb = XCNEW (gfc_typebound_proc);
5733 tb->where = gfc_current_locus;
5734 m = match_binding_attributes (tb, false, true);
5735 if (m == MATCH_ERROR)
5736 return m;
5738 gfc_clear_attr (&current_attr);
5739 current_attr.procedure = 1;
5740 current_attr.proc_pointer = 1;
5741 current_attr.access = tb->access;
5742 current_attr.flavor = FL_PROCEDURE;
5744 /* Match the colons (required). */
5745 if (gfc_match (" ::") != MATCH_YES)
5747 gfc_error ("Expected %<::%> after binding-attributes at %C");
5748 return MATCH_ERROR;
5751 /* Check for C450. */
5752 if (!tb->nopass && proc_if == NULL)
5754 gfc_error("NOPASS or explicit interface required at %C");
5755 return MATCH_ERROR;
5758 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5759 return MATCH_ERROR;
5761 /* Match PPC names. */
5762 ts = current_ts;
5763 for(num=1;;num++)
5765 m = gfc_match_name (name);
5766 if (m == MATCH_NO)
5767 goto syntax;
5768 else if (m == MATCH_ERROR)
5769 return m;
5771 if (!gfc_add_component (gfc_current_block(), name, &c))
5772 return MATCH_ERROR;
5774 /* Add current_attr to the symbol attributes. */
5775 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5776 return MATCH_ERROR;
5778 if (!gfc_add_external (&c->attr, NULL))
5779 return MATCH_ERROR;
5781 if (!gfc_add_proc (&c->attr, name, NULL))
5782 return MATCH_ERROR;
5784 if (num == 1)
5785 c->tb = tb;
5786 else
5788 c->tb = XCNEW (gfc_typebound_proc);
5789 c->tb->where = gfc_current_locus;
5790 *c->tb = *tb;
5793 /* Set interface. */
5794 if (proc_if != NULL)
5796 c->ts.interface = proc_if;
5797 c->attr.untyped = 1;
5798 c->attr.if_source = IFSRC_IFBODY;
5800 else if (ts.type != BT_UNKNOWN)
5802 c->ts = ts;
5803 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5804 c->ts.interface->result = c->ts.interface;
5805 c->ts.interface->ts = ts;
5806 c->ts.interface->attr.flavor = FL_PROCEDURE;
5807 c->ts.interface->attr.function = 1;
5808 c->attr.function = 1;
5809 c->attr.if_source = IFSRC_UNKNOWN;
5812 if (gfc_match (" =>") == MATCH_YES)
5814 m = match_pointer_init (&initializer, 1);
5815 if (m != MATCH_YES)
5817 gfc_free_expr (initializer);
5818 return m;
5820 c->initializer = initializer;
5823 if (gfc_match_eos () == MATCH_YES)
5824 return MATCH_YES;
5825 if (gfc_match_char (',') != MATCH_YES)
5826 goto syntax;
5829 syntax:
5830 gfc_error ("Syntax error in procedure pointer component at %C");
5831 return MATCH_ERROR;
5835 /* Match a PROCEDURE declaration inside an interface (R1206). */
5837 static match
5838 match_procedure_in_interface (void)
5840 match m;
5841 gfc_symbol *sym;
5842 char name[GFC_MAX_SYMBOL_LEN + 1];
5843 locus old_locus;
5845 if (current_interface.type == INTERFACE_NAMELESS
5846 || current_interface.type == INTERFACE_ABSTRACT)
5848 gfc_error ("PROCEDURE at %C must be in a generic interface");
5849 return MATCH_ERROR;
5852 /* Check if the F2008 optional double colon appears. */
5853 gfc_gobble_whitespace ();
5854 old_locus = gfc_current_locus;
5855 if (gfc_match ("::") == MATCH_YES)
5857 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5858 "MODULE PROCEDURE statement at %L", &old_locus))
5859 return MATCH_ERROR;
5861 else
5862 gfc_current_locus = old_locus;
5864 for(;;)
5866 m = gfc_match_name (name);
5867 if (m == MATCH_NO)
5868 goto syntax;
5869 else if (m == MATCH_ERROR)
5870 return m;
5871 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5872 return MATCH_ERROR;
5874 if (!gfc_add_interface (sym))
5875 return MATCH_ERROR;
5877 if (gfc_match_eos () == MATCH_YES)
5878 break;
5879 if (gfc_match_char (',') != MATCH_YES)
5880 goto syntax;
5883 return MATCH_YES;
5885 syntax:
5886 gfc_error ("Syntax error in PROCEDURE statement at %C");
5887 return MATCH_ERROR;
5891 /* General matcher for PROCEDURE declarations. */
5893 static match match_procedure_in_type (void);
5895 match
5896 gfc_match_procedure (void)
5898 match m;
5900 switch (gfc_current_state ())
5902 case COMP_NONE:
5903 case COMP_PROGRAM:
5904 case COMP_MODULE:
5905 case COMP_SUBMODULE:
5906 case COMP_SUBROUTINE:
5907 case COMP_FUNCTION:
5908 case COMP_BLOCK:
5909 m = match_procedure_decl ();
5910 break;
5911 case COMP_INTERFACE:
5912 m = match_procedure_in_interface ();
5913 break;
5914 case COMP_DERIVED:
5915 m = match_ppc_decl ();
5916 break;
5917 case COMP_DERIVED_CONTAINS:
5918 m = match_procedure_in_type ();
5919 break;
5920 default:
5921 return MATCH_NO;
5924 if (m != MATCH_YES)
5925 return m;
5927 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5928 return MATCH_ERROR;
5930 return m;
5934 /* Warn if a matched procedure has the same name as an intrinsic; this is
5935 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5936 parser-state-stack to find out whether we're in a module. */
5938 static void
5939 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5941 bool in_module;
5943 in_module = (gfc_state_stack->previous
5944 && (gfc_state_stack->previous->state == COMP_MODULE
5945 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5947 gfc_warn_intrinsic_shadow (sym, in_module, func);
5951 /* Match a function declaration. */
5953 match
5954 gfc_match_function_decl (void)
5956 char name[GFC_MAX_SYMBOL_LEN + 1];
5957 gfc_symbol *sym, *result;
5958 locus old_loc;
5959 match m;
5960 match suffix_match;
5961 match found_match; /* Status returned by match func. */
5963 if (gfc_current_state () != COMP_NONE
5964 && gfc_current_state () != COMP_INTERFACE
5965 && gfc_current_state () != COMP_CONTAINS)
5966 return MATCH_NO;
5968 gfc_clear_ts (&current_ts);
5970 old_loc = gfc_current_locus;
5972 m = gfc_match_prefix (&current_ts);
5973 if (m != MATCH_YES)
5975 gfc_current_locus = old_loc;
5976 return m;
5979 if (gfc_match ("function% %n", name) != MATCH_YES)
5981 gfc_current_locus = old_loc;
5982 return MATCH_NO;
5985 if (get_proc_name (name, &sym, false))
5986 return MATCH_ERROR;
5988 if (add_hidden_procptr_result (sym))
5989 sym = sym->result;
5991 if (current_attr.module_procedure)
5992 sym->attr.module_procedure = 1;
5994 gfc_new_block = sym;
5996 m = gfc_match_formal_arglist (sym, 0, 0);
5997 if (m == MATCH_NO)
5999 gfc_error ("Expected formal argument list in function "
6000 "definition at %C");
6001 m = MATCH_ERROR;
6002 goto cleanup;
6004 else if (m == MATCH_ERROR)
6005 goto cleanup;
6007 result = NULL;
6009 /* According to the draft, the bind(c) and result clause can
6010 come in either order after the formal_arg_list (i.e., either
6011 can be first, both can exist together or by themselves or neither
6012 one). Therefore, the match_result can't match the end of the
6013 string, and check for the bind(c) or result clause in either order. */
6014 found_match = gfc_match_eos ();
6016 /* Make sure that it isn't already declared as BIND(C). If it is, it
6017 must have been marked BIND(C) with a BIND(C) attribute and that is
6018 not allowed for procedures. */
6019 if (sym->attr.is_bind_c == 1)
6021 sym->attr.is_bind_c = 0;
6022 if (sym->old_symbol != NULL)
6023 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6024 "variables or common blocks",
6025 &(sym->old_symbol->declared_at));
6026 else
6027 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6028 "variables or common blocks", &gfc_current_locus);
6031 if (found_match != MATCH_YES)
6033 /* If we haven't found the end-of-statement, look for a suffix. */
6034 suffix_match = gfc_match_suffix (sym, &result);
6035 if (suffix_match == MATCH_YES)
6036 /* Need to get the eos now. */
6037 found_match = gfc_match_eos ();
6038 else
6039 found_match = suffix_match;
6042 if(found_match != MATCH_YES)
6043 m = MATCH_ERROR;
6044 else
6046 /* Make changes to the symbol. */
6047 m = MATCH_ERROR;
6049 if (!gfc_add_function (&sym->attr, sym->name, NULL))
6050 goto cleanup;
6052 if (!gfc_missing_attr (&sym->attr, NULL))
6053 goto cleanup;
6055 if (!copy_prefix (&sym->attr, &sym->declared_at))
6057 if(!sym->attr.module_procedure)
6058 goto cleanup;
6059 else
6060 gfc_error_check ();
6063 /* Delay matching the function characteristics until after the
6064 specification block by signalling kind=-1. */
6065 sym->declared_at = old_loc;
6066 if (current_ts.type != BT_UNKNOWN)
6067 current_ts.kind = -1;
6068 else
6069 current_ts.kind = 0;
6071 if (result == NULL)
6073 if (current_ts.type != BT_UNKNOWN
6074 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
6075 goto cleanup;
6076 sym->result = sym;
6078 else
6080 if (current_ts.type != BT_UNKNOWN
6081 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
6082 goto cleanup;
6083 sym->result = result;
6086 /* Warn if this procedure has the same name as an intrinsic. */
6087 do_warn_intrinsic_shadow (sym, true);
6089 return MATCH_YES;
6092 cleanup:
6093 gfc_current_locus = old_loc;
6094 return m;
6098 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
6099 pass the name of the entry, rather than the gfc_current_block name, and
6100 to return false upon finding an existing global entry. */
6102 static bool
6103 add_global_entry (const char *name, const char *binding_label, bool sub,
6104 locus *where)
6106 gfc_gsymbol *s;
6107 enum gfc_symbol_type type;
6109 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
6111 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
6112 name is a global identifier. */
6113 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
6115 s = gfc_get_gsymbol (name);
6117 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
6119 gfc_global_used (s, where);
6120 return false;
6122 else
6124 s->type = type;
6125 s->sym_name = name;
6126 s->where = *where;
6127 s->defined = 1;
6128 s->ns = gfc_current_ns;
6132 /* Don't add the symbol multiple times. */
6133 if (binding_label
6134 && (!gfc_notification_std (GFC_STD_F2008)
6135 || strcmp (name, binding_label) != 0))
6137 s = gfc_get_gsymbol (binding_label);
6139 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
6141 gfc_global_used (s, where);
6142 return false;
6144 else
6146 s->type = type;
6147 s->sym_name = name;
6148 s->binding_label = binding_label;
6149 s->where = *where;
6150 s->defined = 1;
6151 s->ns = gfc_current_ns;
6155 return true;
6159 /* Match an ENTRY statement. */
6161 match
6162 gfc_match_entry (void)
6164 gfc_symbol *proc;
6165 gfc_symbol *result;
6166 gfc_symbol *entry;
6167 char name[GFC_MAX_SYMBOL_LEN + 1];
6168 gfc_compile_state state;
6169 match m;
6170 gfc_entry_list *el;
6171 locus old_loc;
6172 bool module_procedure;
6173 char peek_char;
6174 match is_bind_c;
6176 m = gfc_match_name (name);
6177 if (m != MATCH_YES)
6178 return m;
6180 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
6181 return MATCH_ERROR;
6183 state = gfc_current_state ();
6184 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
6186 switch (state)
6188 case COMP_PROGRAM:
6189 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
6190 break;
6191 case COMP_MODULE:
6192 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
6193 break;
6194 case COMP_SUBMODULE:
6195 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
6196 break;
6197 case COMP_BLOCK_DATA:
6198 gfc_error ("ENTRY statement at %C cannot appear within "
6199 "a BLOCK DATA");
6200 break;
6201 case COMP_INTERFACE:
6202 gfc_error ("ENTRY statement at %C cannot appear within "
6203 "an INTERFACE");
6204 break;
6205 case COMP_STRUCTURE:
6206 gfc_error ("ENTRY statement at %C cannot appear within "
6207 "a STRUCTURE block");
6208 break;
6209 case COMP_DERIVED:
6210 gfc_error ("ENTRY statement at %C cannot appear within "
6211 "a DERIVED TYPE block");
6212 break;
6213 case COMP_IF:
6214 gfc_error ("ENTRY statement at %C cannot appear within "
6215 "an IF-THEN block");
6216 break;
6217 case COMP_DO:
6218 case COMP_DO_CONCURRENT:
6219 gfc_error ("ENTRY statement at %C cannot appear within "
6220 "a DO block");
6221 break;
6222 case COMP_SELECT:
6223 gfc_error ("ENTRY statement at %C cannot appear within "
6224 "a SELECT block");
6225 break;
6226 case COMP_FORALL:
6227 gfc_error ("ENTRY statement at %C cannot appear within "
6228 "a FORALL block");
6229 break;
6230 case COMP_WHERE:
6231 gfc_error ("ENTRY statement at %C cannot appear within "
6232 "a WHERE block");
6233 break;
6234 case COMP_CONTAINS:
6235 gfc_error ("ENTRY statement at %C cannot appear within "
6236 "a contained subprogram");
6237 break;
6238 default:
6239 gfc_error ("Unexpected ENTRY statement at %C");
6241 return MATCH_ERROR;
6244 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
6245 && gfc_state_stack->previous->state == COMP_INTERFACE)
6247 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
6248 return MATCH_ERROR;
6251 module_procedure = gfc_current_ns->parent != NULL
6252 && gfc_current_ns->parent->proc_name
6253 && gfc_current_ns->parent->proc_name->attr.flavor
6254 == FL_MODULE;
6256 if (gfc_current_ns->parent != NULL
6257 && gfc_current_ns->parent->proc_name
6258 && !module_procedure)
6260 gfc_error("ENTRY statement at %C cannot appear in a "
6261 "contained procedure");
6262 return MATCH_ERROR;
6265 /* Module function entries need special care in get_proc_name
6266 because previous references within the function will have
6267 created symbols attached to the current namespace. */
6268 if (get_proc_name (name, &entry,
6269 gfc_current_ns->parent != NULL
6270 && module_procedure))
6271 return MATCH_ERROR;
6273 proc = gfc_current_block ();
6275 /* Make sure that it isn't already declared as BIND(C). If it is, it
6276 must have been marked BIND(C) with a BIND(C) attribute and that is
6277 not allowed for procedures. */
6278 if (entry->attr.is_bind_c == 1)
6280 entry->attr.is_bind_c = 0;
6281 if (entry->old_symbol != NULL)
6282 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6283 "variables or common blocks",
6284 &(entry->old_symbol->declared_at));
6285 else
6286 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6287 "variables or common blocks", &gfc_current_locus);
6290 /* Check what next non-whitespace character is so we can tell if there
6291 is the required parens if we have a BIND(C). */
6292 old_loc = gfc_current_locus;
6293 gfc_gobble_whitespace ();
6294 peek_char = gfc_peek_ascii_char ();
6296 if (state == COMP_SUBROUTINE)
6298 m = gfc_match_formal_arglist (entry, 0, 1);
6299 if (m != MATCH_YES)
6300 return MATCH_ERROR;
6302 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
6303 never be an internal procedure. */
6304 is_bind_c = gfc_match_bind_c (entry, true);
6305 if (is_bind_c == MATCH_ERROR)
6306 return MATCH_ERROR;
6307 if (is_bind_c == MATCH_YES)
6309 if (peek_char != '(')
6311 gfc_error ("Missing required parentheses before BIND(C) at %C");
6312 return MATCH_ERROR;
6314 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
6315 &(entry->declared_at), 1))
6316 return MATCH_ERROR;
6319 if (!gfc_current_ns->parent
6320 && !add_global_entry (name, entry->binding_label, true,
6321 &old_loc))
6322 return MATCH_ERROR;
6324 /* An entry in a subroutine. */
6325 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
6326 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
6327 return MATCH_ERROR;
6329 else
6331 /* An entry in a function.
6332 We need to take special care because writing
6333 ENTRY f()
6335 ENTRY f
6336 is allowed, whereas
6337 ENTRY f() RESULT (r)
6338 can't be written as
6339 ENTRY f RESULT (r). */
6340 if (gfc_match_eos () == MATCH_YES)
6342 gfc_current_locus = old_loc;
6343 /* Match the empty argument list, and add the interface to
6344 the symbol. */
6345 m = gfc_match_formal_arglist (entry, 0, 1);
6347 else
6348 m = gfc_match_formal_arglist (entry, 0, 0);
6350 if (m != MATCH_YES)
6351 return MATCH_ERROR;
6353 result = NULL;
6355 if (gfc_match_eos () == MATCH_YES)
6357 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
6358 || !gfc_add_function (&entry->attr, entry->name, NULL))
6359 return MATCH_ERROR;
6361 entry->result = entry;
6363 else
6365 m = gfc_match_suffix (entry, &result);
6366 if (m == MATCH_NO)
6367 gfc_syntax_error (ST_ENTRY);
6368 if (m != MATCH_YES)
6369 return MATCH_ERROR;
6371 if (result)
6373 if (!gfc_add_result (&result->attr, result->name, NULL)
6374 || !gfc_add_entry (&entry->attr, result->name, NULL)
6375 || !gfc_add_function (&entry->attr, result->name, NULL))
6376 return MATCH_ERROR;
6377 entry->result = result;
6379 else
6381 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
6382 || !gfc_add_function (&entry->attr, entry->name, NULL))
6383 return MATCH_ERROR;
6384 entry->result = entry;
6388 if (!gfc_current_ns->parent
6389 && !add_global_entry (name, entry->binding_label, false,
6390 &old_loc))
6391 return MATCH_ERROR;
6394 if (gfc_match_eos () != MATCH_YES)
6396 gfc_syntax_error (ST_ENTRY);
6397 return MATCH_ERROR;
6400 entry->attr.recursive = proc->attr.recursive;
6401 entry->attr.elemental = proc->attr.elemental;
6402 entry->attr.pure = proc->attr.pure;
6404 el = gfc_get_entry_list ();
6405 el->sym = entry;
6406 el->next = gfc_current_ns->entries;
6407 gfc_current_ns->entries = el;
6408 if (el->next)
6409 el->id = el->next->id + 1;
6410 else
6411 el->id = 1;
6413 new_st.op = EXEC_ENTRY;
6414 new_st.ext.entry = el;
6416 return MATCH_YES;
6420 /* Match a subroutine statement, including optional prefixes. */
6422 match
6423 gfc_match_subroutine (void)
6425 char name[GFC_MAX_SYMBOL_LEN + 1];
6426 gfc_symbol *sym;
6427 match m;
6428 match is_bind_c;
6429 char peek_char;
6430 bool allow_binding_name;
6432 if (gfc_current_state () != COMP_NONE
6433 && gfc_current_state () != COMP_INTERFACE
6434 && gfc_current_state () != COMP_CONTAINS)
6435 return MATCH_NO;
6437 m = gfc_match_prefix (NULL);
6438 if (m != MATCH_YES)
6439 return m;
6441 m = gfc_match ("subroutine% %n", name);
6442 if (m != MATCH_YES)
6443 return m;
6445 if (get_proc_name (name, &sym, false))
6446 return MATCH_ERROR;
6448 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
6449 the symbol existed before. */
6450 sym->declared_at = gfc_current_locus;
6452 if (current_attr.module_procedure)
6453 sym->attr.module_procedure = 1;
6455 if (add_hidden_procptr_result (sym))
6456 sym = sym->result;
6458 gfc_new_block = sym;
6460 /* Check what next non-whitespace character is so we can tell if there
6461 is the required parens if we have a BIND(C). */
6462 gfc_gobble_whitespace ();
6463 peek_char = gfc_peek_ascii_char ();
6465 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
6466 return MATCH_ERROR;
6468 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
6469 return MATCH_ERROR;
6471 /* Make sure that it isn't already declared as BIND(C). If it is, it
6472 must have been marked BIND(C) with a BIND(C) attribute and that is
6473 not allowed for procedures. */
6474 if (sym->attr.is_bind_c == 1)
6476 sym->attr.is_bind_c = 0;
6477 if (sym->old_symbol != NULL)
6478 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6479 "variables or common blocks",
6480 &(sym->old_symbol->declared_at));
6481 else
6482 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6483 "variables or common blocks", &gfc_current_locus);
6486 /* C binding names are not allowed for internal procedures. */
6487 if (gfc_current_state () == COMP_CONTAINS
6488 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6489 allow_binding_name = false;
6490 else
6491 allow_binding_name = true;
6493 /* Here, we are just checking if it has the bind(c) attribute, and if
6494 so, then we need to make sure it's all correct. If it doesn't,
6495 we still need to continue matching the rest of the subroutine line. */
6496 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6497 if (is_bind_c == MATCH_ERROR)
6499 /* There was an attempt at the bind(c), but it was wrong. An
6500 error message should have been printed w/in the gfc_match_bind_c
6501 so here we'll just return the MATCH_ERROR. */
6502 return MATCH_ERROR;
6505 if (is_bind_c == MATCH_YES)
6507 /* The following is allowed in the Fortran 2008 draft. */
6508 if (gfc_current_state () == COMP_CONTAINS
6509 && sym->ns->proc_name->attr.flavor != FL_MODULE
6510 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6511 "at %L may not be specified for an internal "
6512 "procedure", &gfc_current_locus))
6513 return MATCH_ERROR;
6515 if (peek_char != '(')
6517 gfc_error ("Missing required parentheses before BIND(C) at %C");
6518 return MATCH_ERROR;
6520 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
6521 &(sym->declared_at), 1))
6522 return MATCH_ERROR;
6525 if (gfc_match_eos () != MATCH_YES)
6527 gfc_syntax_error (ST_SUBROUTINE);
6528 return MATCH_ERROR;
6531 if (!copy_prefix (&sym->attr, &sym->declared_at))
6533 if(!sym->attr.module_procedure)
6534 return MATCH_ERROR;
6535 else
6536 gfc_error_check ();
6539 /* Warn if it has the same name as an intrinsic. */
6540 do_warn_intrinsic_shadow (sym, false);
6542 return MATCH_YES;
6546 /* Check that the NAME identifier in a BIND attribute or statement
6547 is conform to C identifier rules. */
6549 match
6550 check_bind_name_identifier (char **name)
6552 char *n = *name, *p;
6554 /* Remove leading spaces. */
6555 while (*n == ' ')
6556 n++;
6558 /* On an empty string, free memory and set name to NULL. */
6559 if (*n == '\0')
6561 free (*name);
6562 *name = NULL;
6563 return MATCH_YES;
6566 /* Remove trailing spaces. */
6567 p = n + strlen(n) - 1;
6568 while (*p == ' ')
6569 *(p--) = '\0';
6571 /* Insert the identifier into the symbol table. */
6572 p = xstrdup (n);
6573 free (*name);
6574 *name = p;
6576 /* Now check that identifier is valid under C rules. */
6577 if (ISDIGIT (*p))
6579 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6580 return MATCH_ERROR;
6583 for (; *p; p++)
6584 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6586 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6587 return MATCH_ERROR;
6590 return MATCH_YES;
6594 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6595 given, and set the binding label in either the given symbol (if not
6596 NULL), or in the current_ts. The symbol may be NULL because we may
6597 encounter the BIND(C) before the declaration itself. Return
6598 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6599 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6600 or MATCH_YES if the specifier was correct and the binding label and
6601 bind(c) fields were set correctly for the given symbol or the
6602 current_ts. If allow_binding_name is false, no binding name may be
6603 given. */
6605 match
6606 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6608 char *binding_label = NULL;
6609 gfc_expr *e = NULL;
6611 /* Initialize the flag that specifies whether we encountered a NAME=
6612 specifier or not. */
6613 has_name_equals = 0;
6615 /* This much we have to be able to match, in this order, if
6616 there is a bind(c) label. */
6617 if (gfc_match (" bind ( c ") != MATCH_YES)
6618 return MATCH_NO;
6620 /* Now see if there is a binding label, or if we've reached the
6621 end of the bind(c) attribute without one. */
6622 if (gfc_match_char (',') == MATCH_YES)
6624 if (gfc_match (" name = ") != MATCH_YES)
6626 gfc_error ("Syntax error in NAME= specifier for binding label "
6627 "at %C");
6628 /* should give an error message here */
6629 return MATCH_ERROR;
6632 has_name_equals = 1;
6634 if (gfc_match_init_expr (&e) != MATCH_YES)
6636 gfc_free_expr (e);
6637 return MATCH_ERROR;
6640 if (!gfc_simplify_expr(e, 0))
6642 gfc_error ("NAME= specifier at %C should be a constant expression");
6643 gfc_free_expr (e);
6644 return MATCH_ERROR;
6647 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6648 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6650 gfc_error ("NAME= specifier at %C should be a scalar of "
6651 "default character kind");
6652 gfc_free_expr(e);
6653 return MATCH_ERROR;
6656 // Get a C string from the Fortran string constant
6657 binding_label = gfc_widechar_to_char (e->value.character.string,
6658 e->value.character.length);
6659 gfc_free_expr(e);
6661 // Check that it is valid (old gfc_match_name_C)
6662 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6663 return MATCH_ERROR;
6666 /* Get the required right paren. */
6667 if (gfc_match_char (')') != MATCH_YES)
6669 gfc_error ("Missing closing paren for binding label at %C");
6670 return MATCH_ERROR;
6673 if (has_name_equals && !allow_binding_name)
6675 gfc_error ("No binding name is allowed in BIND(C) at %C");
6676 return MATCH_ERROR;
6679 if (has_name_equals && sym != NULL && sym->attr.dummy)
6681 gfc_error ("For dummy procedure %s, no binding name is "
6682 "allowed in BIND(C) at %C", sym->name);
6683 return MATCH_ERROR;
6687 /* Save the binding label to the symbol. If sym is null, we're
6688 probably matching the typespec attributes of a declaration and
6689 haven't gotten the name yet, and therefore, no symbol yet. */
6690 if (binding_label)
6692 if (sym != NULL)
6693 sym->binding_label = binding_label;
6694 else
6695 curr_binding_label = binding_label;
6697 else if (allow_binding_name)
6699 /* No binding label, but if symbol isn't null, we
6700 can set the label for it here.
6701 If name="" or allow_binding_name is false, no C binding name is
6702 created. */
6703 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6704 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6707 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6708 && current_interface.type == INTERFACE_ABSTRACT)
6710 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6711 return MATCH_ERROR;
6714 return MATCH_YES;
6718 /* Return nonzero if we're currently compiling a contained procedure. */
6720 static int
6721 contained_procedure (void)
6723 gfc_state_data *s = gfc_state_stack;
6725 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6726 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6727 return 1;
6729 return 0;
6732 /* Set the kind of each enumerator. The kind is selected such that it is
6733 interoperable with the corresponding C enumeration type, making
6734 sure that -fshort-enums is honored. */
6736 static void
6737 set_enum_kind(void)
6739 enumerator_history *current_history = NULL;
6740 int kind;
6741 int i;
6743 if (max_enum == NULL || enum_history == NULL)
6744 return;
6746 if (!flag_short_enums)
6747 return;
6749 i = 0;
6752 kind = gfc_integer_kinds[i++].kind;
6754 while (kind < gfc_c_int_kind
6755 && gfc_check_integer_range (max_enum->initializer->value.integer,
6756 kind) != ARITH_OK);
6758 current_history = enum_history;
6759 while (current_history != NULL)
6761 current_history->sym->ts.kind = kind;
6762 current_history = current_history->next;
6767 /* Match any of the various end-block statements. Returns the type of
6768 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6769 and END BLOCK statements cannot be replaced by a single END statement. */
6771 match
6772 gfc_match_end (gfc_statement *st)
6774 char name[GFC_MAX_SYMBOL_LEN + 1];
6775 gfc_compile_state state;
6776 locus old_loc;
6777 const char *block_name;
6778 const char *target;
6779 int eos_ok;
6780 match m;
6781 gfc_namespace *parent_ns, *ns, *prev_ns;
6782 gfc_namespace **nsp;
6783 bool abreviated_modproc_decl = false;
6784 bool got_matching_end = false;
6786 old_loc = gfc_current_locus;
6787 if (gfc_match ("end") != MATCH_YES)
6788 return MATCH_NO;
6790 state = gfc_current_state ();
6791 block_name = gfc_current_block () == NULL
6792 ? NULL : gfc_current_block ()->name;
6794 switch (state)
6796 case COMP_ASSOCIATE:
6797 case COMP_BLOCK:
6798 if (!strncmp (block_name, "block@", strlen("block@")))
6799 block_name = NULL;
6800 break;
6802 case COMP_CONTAINS:
6803 case COMP_DERIVED_CONTAINS:
6804 state = gfc_state_stack->previous->state;
6805 block_name = gfc_state_stack->previous->sym == NULL
6806 ? NULL : gfc_state_stack->previous->sym->name;
6807 abreviated_modproc_decl = gfc_state_stack->previous->sym
6808 && gfc_state_stack->previous->sym->abr_modproc_decl;
6809 break;
6811 default:
6812 break;
6815 if (!abreviated_modproc_decl)
6816 abreviated_modproc_decl = gfc_current_block ()
6817 && gfc_current_block ()->abr_modproc_decl;
6819 switch (state)
6821 case COMP_NONE:
6822 case COMP_PROGRAM:
6823 *st = ST_END_PROGRAM;
6824 target = " program";
6825 eos_ok = 1;
6826 break;
6828 case COMP_SUBROUTINE:
6829 *st = ST_END_SUBROUTINE;
6830 if (!abreviated_modproc_decl)
6831 target = " subroutine";
6832 else
6833 target = " procedure";
6834 eos_ok = !contained_procedure ();
6835 break;
6837 case COMP_FUNCTION:
6838 *st = ST_END_FUNCTION;
6839 if (!abreviated_modproc_decl)
6840 target = " function";
6841 else
6842 target = " procedure";
6843 eos_ok = !contained_procedure ();
6844 break;
6846 case COMP_BLOCK_DATA:
6847 *st = ST_END_BLOCK_DATA;
6848 target = " block data";
6849 eos_ok = 1;
6850 break;
6852 case COMP_MODULE:
6853 *st = ST_END_MODULE;
6854 target = " module";
6855 eos_ok = 1;
6856 break;
6858 case COMP_SUBMODULE:
6859 *st = ST_END_SUBMODULE;
6860 target = " submodule";
6861 eos_ok = 1;
6862 break;
6864 case COMP_INTERFACE:
6865 *st = ST_END_INTERFACE;
6866 target = " interface";
6867 eos_ok = 0;
6868 break;
6870 case COMP_MAP:
6871 *st = ST_END_MAP;
6872 target = " map";
6873 eos_ok = 0;
6874 break;
6876 case COMP_UNION:
6877 *st = ST_END_UNION;
6878 target = " union";
6879 eos_ok = 0;
6880 break;
6882 case COMP_STRUCTURE:
6883 *st = ST_END_STRUCTURE;
6884 target = " structure";
6885 eos_ok = 0;
6886 break;
6888 case COMP_DERIVED:
6889 case COMP_DERIVED_CONTAINS:
6890 *st = ST_END_TYPE;
6891 target = " type";
6892 eos_ok = 0;
6893 break;
6895 case COMP_ASSOCIATE:
6896 *st = ST_END_ASSOCIATE;
6897 target = " associate";
6898 eos_ok = 0;
6899 break;
6901 case COMP_BLOCK:
6902 *st = ST_END_BLOCK;
6903 target = " block";
6904 eos_ok = 0;
6905 break;
6907 case COMP_IF:
6908 *st = ST_ENDIF;
6909 target = " if";
6910 eos_ok = 0;
6911 break;
6913 case COMP_DO:
6914 case COMP_DO_CONCURRENT:
6915 *st = ST_ENDDO;
6916 target = " do";
6917 eos_ok = 0;
6918 break;
6920 case COMP_CRITICAL:
6921 *st = ST_END_CRITICAL;
6922 target = " critical";
6923 eos_ok = 0;
6924 break;
6926 case COMP_SELECT:
6927 case COMP_SELECT_TYPE:
6928 *st = ST_END_SELECT;
6929 target = " select";
6930 eos_ok = 0;
6931 break;
6933 case COMP_FORALL:
6934 *st = ST_END_FORALL;
6935 target = " forall";
6936 eos_ok = 0;
6937 break;
6939 case COMP_WHERE:
6940 *st = ST_END_WHERE;
6941 target = " where";
6942 eos_ok = 0;
6943 break;
6945 case COMP_ENUM:
6946 *st = ST_END_ENUM;
6947 target = " enum";
6948 eos_ok = 0;
6949 last_initializer = NULL;
6950 set_enum_kind ();
6951 gfc_free_enum_history ();
6952 break;
6954 default:
6955 gfc_error ("Unexpected END statement at %C");
6956 goto cleanup;
6959 old_loc = gfc_current_locus;
6960 if (gfc_match_eos () == MATCH_YES)
6962 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6964 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6965 "instead of %s statement at %L",
6966 abreviated_modproc_decl ? "END PROCEDURE"
6967 : gfc_ascii_statement(*st), &old_loc))
6968 goto cleanup;
6970 else if (!eos_ok)
6972 /* We would have required END [something]. */
6973 gfc_error ("%s statement expected at %L",
6974 gfc_ascii_statement (*st), &old_loc);
6975 goto cleanup;
6978 return MATCH_YES;
6981 /* Verify that we've got the sort of end-block that we're expecting. */
6982 if (gfc_match (target) != MATCH_YES)
6984 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6985 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6986 goto cleanup;
6988 else
6989 got_matching_end = true;
6991 old_loc = gfc_current_locus;
6992 /* If we're at the end, make sure a block name wasn't required. */
6993 if (gfc_match_eos () == MATCH_YES)
6996 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6997 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6998 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6999 return MATCH_YES;
7001 if (!block_name)
7002 return MATCH_YES;
7004 gfc_error ("Expected block name of %qs in %s statement at %L",
7005 block_name, gfc_ascii_statement (*st), &old_loc);
7007 return MATCH_ERROR;
7010 /* END INTERFACE has a special handler for its several possible endings. */
7011 if (*st == ST_END_INTERFACE)
7012 return gfc_match_end_interface ();
7014 /* We haven't hit the end of statement, so what is left must be an
7015 end-name. */
7016 m = gfc_match_space ();
7017 if (m == MATCH_YES)
7018 m = gfc_match_name (name);
7020 if (m == MATCH_NO)
7021 gfc_error ("Expected terminating name at %C");
7022 if (m != MATCH_YES)
7023 goto cleanup;
7025 if (block_name == NULL)
7026 goto syntax;
7028 /* We have to pick out the declared submodule name from the composite
7029 required by F2008:11.2.3 para 2, which ends in the declared name. */
7030 if (state == COMP_SUBMODULE)
7031 block_name = strchr (block_name, '.') + 1;
7033 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
7035 gfc_error ("Expected label %qs for %s statement at %C", block_name,
7036 gfc_ascii_statement (*st));
7037 goto cleanup;
7039 /* Procedure pointer as function result. */
7040 else if (strcmp (block_name, "ppr@") == 0
7041 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
7043 gfc_error ("Expected label %qs for %s statement at %C",
7044 gfc_current_block ()->ns->proc_name->name,
7045 gfc_ascii_statement (*st));
7046 goto cleanup;
7049 if (gfc_match_eos () == MATCH_YES)
7050 return MATCH_YES;
7052 syntax:
7053 gfc_syntax_error (*st);
7055 cleanup:
7056 gfc_current_locus = old_loc;
7058 /* If we are missing an END BLOCK, we created a half-ready namespace.
7059 Remove it from the parent namespace's sibling list. */
7061 while (state == COMP_BLOCK && !got_matching_end)
7063 parent_ns = gfc_current_ns->parent;
7065 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
7067 prev_ns = NULL;
7068 ns = *nsp;
7069 while (ns)
7071 if (ns == gfc_current_ns)
7073 if (prev_ns == NULL)
7074 *nsp = NULL;
7075 else
7076 prev_ns->sibling = ns->sibling;
7078 prev_ns = ns;
7079 ns = ns->sibling;
7082 gfc_free_namespace (gfc_current_ns);
7083 gfc_current_ns = parent_ns;
7084 gfc_state_stack = gfc_state_stack->previous;
7085 state = gfc_current_state ();
7088 return MATCH_ERROR;
7093 /***************** Attribute declaration statements ****************/
7095 /* Set the attribute of a single variable. */
7097 static match
7098 attr_decl1 (void)
7100 char name[GFC_MAX_SYMBOL_LEN + 1];
7101 gfc_array_spec *as;
7103 /* Workaround -Wmaybe-uninitialized false positive during
7104 profiledbootstrap by initializing them. */
7105 gfc_symbol *sym = NULL;
7106 locus var_locus;
7107 match m;
7109 as = NULL;
7111 m = gfc_match_name (name);
7112 if (m != MATCH_YES)
7113 goto cleanup;
7115 if (find_special (name, &sym, false))
7116 return MATCH_ERROR;
7118 if (!check_function_name (name))
7120 m = MATCH_ERROR;
7121 goto cleanup;
7124 var_locus = gfc_current_locus;
7126 /* Deal with possible array specification for certain attributes. */
7127 if (current_attr.dimension
7128 || current_attr.codimension
7129 || current_attr.allocatable
7130 || current_attr.pointer
7131 || current_attr.target)
7133 m = gfc_match_array_spec (&as, !current_attr.codimension,
7134 !current_attr.dimension
7135 && !current_attr.pointer
7136 && !current_attr.target);
7137 if (m == MATCH_ERROR)
7138 goto cleanup;
7140 if (current_attr.dimension && m == MATCH_NO)
7142 gfc_error ("Missing array specification at %L in DIMENSION "
7143 "statement", &var_locus);
7144 m = MATCH_ERROR;
7145 goto cleanup;
7148 if (current_attr.dimension && sym->value)
7150 gfc_error ("Dimensions specified for %s at %L after its "
7151 "initialization", sym->name, &var_locus);
7152 m = MATCH_ERROR;
7153 goto cleanup;
7156 if (current_attr.codimension && m == MATCH_NO)
7158 gfc_error ("Missing array specification at %L in CODIMENSION "
7159 "statement", &var_locus);
7160 m = MATCH_ERROR;
7161 goto cleanup;
7164 if ((current_attr.allocatable || current_attr.pointer)
7165 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
7167 gfc_error ("Array specification must be deferred at %L", &var_locus);
7168 m = MATCH_ERROR;
7169 goto cleanup;
7173 /* Update symbol table. DIMENSION attribute is set in
7174 gfc_set_array_spec(). For CLASS variables, this must be applied
7175 to the first component, or '_data' field. */
7176 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
7178 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
7180 m = MATCH_ERROR;
7181 goto cleanup;
7184 else
7186 if (current_attr.dimension == 0 && current_attr.codimension == 0
7187 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
7189 m = MATCH_ERROR;
7190 goto cleanup;
7194 if (sym->ts.type == BT_CLASS
7195 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
7197 m = MATCH_ERROR;
7198 goto cleanup;
7201 if (!gfc_set_array_spec (sym, as, &var_locus))
7203 m = MATCH_ERROR;
7204 goto cleanup;
7207 if (sym->attr.cray_pointee && sym->as != NULL)
7209 /* Fix the array spec. */
7210 m = gfc_mod_pointee_as (sym->as);
7211 if (m == MATCH_ERROR)
7212 goto cleanup;
7215 if (!gfc_add_attribute (&sym->attr, &var_locus))
7217 m = MATCH_ERROR;
7218 goto cleanup;
7221 if ((current_attr.external || current_attr.intrinsic)
7222 && sym->attr.flavor != FL_PROCEDURE
7223 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
7225 m = MATCH_ERROR;
7226 goto cleanup;
7229 add_hidden_procptr_result (sym);
7231 return MATCH_YES;
7233 cleanup:
7234 gfc_free_array_spec (as);
7235 return m;
7239 /* Generic attribute declaration subroutine. Used for attributes that
7240 just have a list of names. */
7242 static match
7243 attr_decl (void)
7245 match m;
7247 /* Gobble the optional double colon, by simply ignoring the result
7248 of gfc_match(). */
7249 gfc_match (" ::");
7251 for (;;)
7253 m = attr_decl1 ();
7254 if (m != MATCH_YES)
7255 break;
7257 if (gfc_match_eos () == MATCH_YES)
7259 m = MATCH_YES;
7260 break;
7263 if (gfc_match_char (',') != MATCH_YES)
7265 gfc_error ("Unexpected character in variable list at %C");
7266 m = MATCH_ERROR;
7267 break;
7271 return m;
7275 /* This routine matches Cray Pointer declarations of the form:
7276 pointer ( <pointer>, <pointee> )
7278 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
7279 The pointer, if already declared, should be an integer. Otherwise, we
7280 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
7281 be either a scalar, or an array declaration. No space is allocated for
7282 the pointee. For the statement
7283 pointer (ipt, ar(10))
7284 any subsequent uses of ar will be translated (in C-notation) as
7285 ar(i) => ((<type> *) ipt)(i)
7286 After gimplification, pointee variable will disappear in the code. */
7288 static match
7289 cray_pointer_decl (void)
7291 match m;
7292 gfc_array_spec *as = NULL;
7293 gfc_symbol *cptr; /* Pointer symbol. */
7294 gfc_symbol *cpte; /* Pointee symbol. */
7295 locus var_locus;
7296 bool done = false;
7298 while (!done)
7300 if (gfc_match_char ('(') != MATCH_YES)
7302 gfc_error ("Expected %<(%> at %C");
7303 return MATCH_ERROR;
7306 /* Match pointer. */
7307 var_locus = gfc_current_locus;
7308 gfc_clear_attr (&current_attr);
7309 gfc_add_cray_pointer (&current_attr, &var_locus);
7310 current_ts.type = BT_INTEGER;
7311 current_ts.kind = gfc_index_integer_kind;
7313 m = gfc_match_symbol (&cptr, 0);
7314 if (m != MATCH_YES)
7316 gfc_error ("Expected variable name at %C");
7317 return m;
7320 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
7321 return MATCH_ERROR;
7323 gfc_set_sym_referenced (cptr);
7325 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
7327 cptr->ts.type = BT_INTEGER;
7328 cptr->ts.kind = gfc_index_integer_kind;
7330 else if (cptr->ts.type != BT_INTEGER)
7332 gfc_error ("Cray pointer at %C must be an integer");
7333 return MATCH_ERROR;
7335 else if (cptr->ts.kind < gfc_index_integer_kind)
7336 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
7337 " memory addresses require %d bytes",
7338 cptr->ts.kind, gfc_index_integer_kind);
7340 if (gfc_match_char (',') != MATCH_YES)
7342 gfc_error ("Expected \",\" at %C");
7343 return MATCH_ERROR;
7346 /* Match Pointee. */
7347 var_locus = gfc_current_locus;
7348 gfc_clear_attr (&current_attr);
7349 gfc_add_cray_pointee (&current_attr, &var_locus);
7350 current_ts.type = BT_UNKNOWN;
7351 current_ts.kind = 0;
7353 m = gfc_match_symbol (&cpte, 0);
7354 if (m != MATCH_YES)
7356 gfc_error ("Expected variable name at %C");
7357 return m;
7360 /* Check for an optional array spec. */
7361 m = gfc_match_array_spec (&as, true, false);
7362 if (m == MATCH_ERROR)
7364 gfc_free_array_spec (as);
7365 return m;
7367 else if (m == MATCH_NO)
7369 gfc_free_array_spec (as);
7370 as = NULL;
7373 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
7374 return MATCH_ERROR;
7376 gfc_set_sym_referenced (cpte);
7378 if (cpte->as == NULL)
7380 if (!gfc_set_array_spec (cpte, as, &var_locus))
7381 gfc_internal_error ("Couldn't set Cray pointee array spec.");
7383 else if (as != NULL)
7385 gfc_error ("Duplicate array spec for Cray pointee at %C");
7386 gfc_free_array_spec (as);
7387 return MATCH_ERROR;
7390 as = NULL;
7392 if (cpte->as != NULL)
7394 /* Fix array spec. */
7395 m = gfc_mod_pointee_as (cpte->as);
7396 if (m == MATCH_ERROR)
7397 return m;
7400 /* Point the Pointee at the Pointer. */
7401 cpte->cp_pointer = cptr;
7403 if (gfc_match_char (')') != MATCH_YES)
7405 gfc_error ("Expected \")\" at %C");
7406 return MATCH_ERROR;
7408 m = gfc_match_char (',');
7409 if (m != MATCH_YES)
7410 done = true; /* Stop searching for more declarations. */
7414 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
7415 || gfc_match_eos () != MATCH_YES)
7417 gfc_error ("Expected %<,%> or end of statement at %C");
7418 return MATCH_ERROR;
7420 return MATCH_YES;
7424 match
7425 gfc_match_external (void)
7428 gfc_clear_attr (&current_attr);
7429 current_attr.external = 1;
7431 return attr_decl ();
7435 match
7436 gfc_match_intent (void)
7438 sym_intent intent;
7440 /* This is not allowed within a BLOCK construct! */
7441 if (gfc_current_state () == COMP_BLOCK)
7443 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
7444 return MATCH_ERROR;
7447 intent = match_intent_spec ();
7448 if (intent == INTENT_UNKNOWN)
7449 return MATCH_ERROR;
7451 gfc_clear_attr (&current_attr);
7452 current_attr.intent = intent;
7454 return attr_decl ();
7458 match
7459 gfc_match_intrinsic (void)
7462 gfc_clear_attr (&current_attr);
7463 current_attr.intrinsic = 1;
7465 return attr_decl ();
7469 match
7470 gfc_match_optional (void)
7472 /* This is not allowed within a BLOCK construct! */
7473 if (gfc_current_state () == COMP_BLOCK)
7475 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
7476 return MATCH_ERROR;
7479 gfc_clear_attr (&current_attr);
7480 current_attr.optional = 1;
7482 return attr_decl ();
7486 match
7487 gfc_match_pointer (void)
7489 gfc_gobble_whitespace ();
7490 if (gfc_peek_ascii_char () == '(')
7492 if (!flag_cray_pointer)
7494 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7495 "flag");
7496 return MATCH_ERROR;
7498 return cray_pointer_decl ();
7500 else
7502 gfc_clear_attr (&current_attr);
7503 current_attr.pointer = 1;
7505 return attr_decl ();
7510 match
7511 gfc_match_allocatable (void)
7513 gfc_clear_attr (&current_attr);
7514 current_attr.allocatable = 1;
7516 return attr_decl ();
7520 match
7521 gfc_match_codimension (void)
7523 gfc_clear_attr (&current_attr);
7524 current_attr.codimension = 1;
7526 return attr_decl ();
7530 match
7531 gfc_match_contiguous (void)
7533 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
7534 return MATCH_ERROR;
7536 gfc_clear_attr (&current_attr);
7537 current_attr.contiguous = 1;
7539 return attr_decl ();
7543 match
7544 gfc_match_dimension (void)
7546 gfc_clear_attr (&current_attr);
7547 current_attr.dimension = 1;
7549 return attr_decl ();
7553 match
7554 gfc_match_target (void)
7556 gfc_clear_attr (&current_attr);
7557 current_attr.target = 1;
7559 return attr_decl ();
7563 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7564 statement. */
7566 static match
7567 access_attr_decl (gfc_statement st)
7569 char name[GFC_MAX_SYMBOL_LEN + 1];
7570 interface_type type;
7571 gfc_user_op *uop;
7572 gfc_symbol *sym, *dt_sym;
7573 gfc_intrinsic_op op;
7574 match m;
7576 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7577 goto done;
7579 for (;;)
7581 m = gfc_match_generic_spec (&type, name, &op);
7582 if (m == MATCH_NO)
7583 goto syntax;
7584 if (m == MATCH_ERROR)
7585 return MATCH_ERROR;
7587 switch (type)
7589 case INTERFACE_NAMELESS:
7590 case INTERFACE_ABSTRACT:
7591 goto syntax;
7593 case INTERFACE_GENERIC:
7594 case INTERFACE_DTIO:
7596 if (gfc_get_symbol (name, NULL, &sym))
7597 goto done;
7599 if (type == INTERFACE_DTIO
7600 && gfc_current_ns->proc_name
7601 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
7602 && sym->attr.flavor == FL_UNKNOWN)
7603 sym->attr.flavor = FL_PROCEDURE;
7605 if (!gfc_add_access (&sym->attr,
7606 (st == ST_PUBLIC)
7607 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7608 sym->name, NULL))
7609 return MATCH_ERROR;
7611 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7612 && !gfc_add_access (&dt_sym->attr,
7613 (st == ST_PUBLIC)
7614 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7615 sym->name, NULL))
7616 return MATCH_ERROR;
7618 break;
7620 case INTERFACE_INTRINSIC_OP:
7621 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7623 gfc_intrinsic_op other_op;
7625 gfc_current_ns->operator_access[op] =
7626 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7628 /* Handle the case if there is another op with the same
7629 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7630 other_op = gfc_equivalent_op (op);
7632 if (other_op != INTRINSIC_NONE)
7633 gfc_current_ns->operator_access[other_op] =
7634 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7637 else
7639 gfc_error ("Access specification of the %s operator at %C has "
7640 "already been specified", gfc_op2string (op));
7641 goto done;
7644 break;
7646 case INTERFACE_USER_OP:
7647 uop = gfc_get_uop (name);
7649 if (uop->access == ACCESS_UNKNOWN)
7651 uop->access = (st == ST_PUBLIC)
7652 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7654 else
7656 gfc_error ("Access specification of the .%s. operator at %C "
7657 "has already been specified", sym->name);
7658 goto done;
7661 break;
7664 if (gfc_match_char (',') == MATCH_NO)
7665 break;
7668 if (gfc_match_eos () != MATCH_YES)
7669 goto syntax;
7670 return MATCH_YES;
7672 syntax:
7673 gfc_syntax_error (st);
7675 done:
7676 return MATCH_ERROR;
7680 match
7681 gfc_match_protected (void)
7683 gfc_symbol *sym;
7684 match m;
7686 if (!gfc_current_ns->proc_name
7687 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7689 gfc_error ("PROTECTED at %C only allowed in specification "
7690 "part of a module");
7691 return MATCH_ERROR;
7695 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7696 return MATCH_ERROR;
7698 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7700 return MATCH_ERROR;
7703 if (gfc_match_eos () == MATCH_YES)
7704 goto syntax;
7706 for(;;)
7708 m = gfc_match_symbol (&sym, 0);
7709 switch (m)
7711 case MATCH_YES:
7712 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7713 return MATCH_ERROR;
7714 goto next_item;
7716 case MATCH_NO:
7717 break;
7719 case MATCH_ERROR:
7720 return MATCH_ERROR;
7723 next_item:
7724 if (gfc_match_eos () == MATCH_YES)
7725 break;
7726 if (gfc_match_char (',') != MATCH_YES)
7727 goto syntax;
7730 return MATCH_YES;
7732 syntax:
7733 gfc_error ("Syntax error in PROTECTED statement at %C");
7734 return MATCH_ERROR;
7738 /* The PRIVATE statement is a bit weird in that it can be an attribute
7739 declaration, but also works as a standalone statement inside of a
7740 type declaration or a module. */
7742 match
7743 gfc_match_private (gfc_statement *st)
7746 if (gfc_match ("private") != MATCH_YES)
7747 return MATCH_NO;
7749 if (gfc_current_state () != COMP_MODULE
7750 && !(gfc_current_state () == COMP_DERIVED
7751 && gfc_state_stack->previous
7752 && gfc_state_stack->previous->state == COMP_MODULE)
7753 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7754 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7755 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7757 gfc_error ("PRIVATE statement at %C is only allowed in the "
7758 "specification part of a module");
7759 return MATCH_ERROR;
7762 if (gfc_current_state () == COMP_DERIVED)
7764 if (gfc_match_eos () == MATCH_YES)
7766 *st = ST_PRIVATE;
7767 return MATCH_YES;
7770 gfc_syntax_error (ST_PRIVATE);
7771 return MATCH_ERROR;
7774 if (gfc_match_eos () == MATCH_YES)
7776 *st = ST_PRIVATE;
7777 return MATCH_YES;
7780 *st = ST_ATTR_DECL;
7781 return access_attr_decl (ST_PRIVATE);
7785 match
7786 gfc_match_public (gfc_statement *st)
7789 if (gfc_match ("public") != MATCH_YES)
7790 return MATCH_NO;
7792 if (gfc_current_state () != COMP_MODULE)
7794 gfc_error ("PUBLIC statement at %C is only allowed in the "
7795 "specification part of a module");
7796 return MATCH_ERROR;
7799 if (gfc_match_eos () == MATCH_YES)
7801 *st = ST_PUBLIC;
7802 return MATCH_YES;
7805 *st = ST_ATTR_DECL;
7806 return access_attr_decl (ST_PUBLIC);
7810 /* Workhorse for gfc_match_parameter. */
7812 static match
7813 do_parm (void)
7815 gfc_symbol *sym;
7816 gfc_expr *init;
7817 match m;
7818 bool t;
7820 m = gfc_match_symbol (&sym, 0);
7821 if (m == MATCH_NO)
7822 gfc_error ("Expected variable name at %C in PARAMETER statement");
7824 if (m != MATCH_YES)
7825 return m;
7827 if (gfc_match_char ('=') == MATCH_NO)
7829 gfc_error ("Expected = sign in PARAMETER statement at %C");
7830 return MATCH_ERROR;
7833 m = gfc_match_init_expr (&init);
7834 if (m == MATCH_NO)
7835 gfc_error ("Expected expression at %C in PARAMETER statement");
7836 if (m != MATCH_YES)
7837 return m;
7839 if (sym->ts.type == BT_UNKNOWN
7840 && !gfc_set_default_type (sym, 1, NULL))
7842 m = MATCH_ERROR;
7843 goto cleanup;
7846 if (!gfc_check_assign_symbol (sym, NULL, init)
7847 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7849 m = MATCH_ERROR;
7850 goto cleanup;
7853 if (sym->value)
7855 gfc_error ("Initializing already initialized variable at %C");
7856 m = MATCH_ERROR;
7857 goto cleanup;
7860 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7861 return (t) ? MATCH_YES : MATCH_ERROR;
7863 cleanup:
7864 gfc_free_expr (init);
7865 return m;
7869 /* Match a parameter statement, with the weird syntax that these have. */
7871 match
7872 gfc_match_parameter (void)
7874 const char *term = " )%t";
7875 match m;
7877 if (gfc_match_char ('(') == MATCH_NO)
7879 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
7880 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
7881 return MATCH_NO;
7882 term = " %t";
7885 for (;;)
7887 m = do_parm ();
7888 if (m != MATCH_YES)
7889 break;
7891 if (gfc_match (term) == MATCH_YES)
7892 break;
7894 if (gfc_match_char (',') != MATCH_YES)
7896 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7897 m = MATCH_ERROR;
7898 break;
7902 return m;
7906 match
7907 gfc_match_automatic (void)
7909 gfc_symbol *sym;
7910 match m;
7911 bool seen_symbol = false;
7913 if (!flag_dec_static)
7915 gfc_error ("%s at %C is a DEC extension, enable with "
7916 "%<-fdec-static%>",
7917 "AUTOMATIC"
7919 return MATCH_ERROR;
7922 gfc_match (" ::");
7924 for (;;)
7926 m = gfc_match_symbol (&sym, 0);
7927 switch (m)
7929 case MATCH_NO:
7930 break;
7932 case MATCH_ERROR:
7933 return MATCH_ERROR;
7935 case MATCH_YES:
7936 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
7937 return MATCH_ERROR;
7938 seen_symbol = true;
7939 break;
7942 if (gfc_match_eos () == MATCH_YES)
7943 break;
7944 if (gfc_match_char (',') != MATCH_YES)
7945 goto syntax;
7948 if (!seen_symbol)
7950 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
7951 return MATCH_ERROR;
7954 return MATCH_YES;
7956 syntax:
7957 gfc_error ("Syntax error in AUTOMATIC statement at %C");
7958 return MATCH_ERROR;
7962 match
7963 gfc_match_static (void)
7965 gfc_symbol *sym;
7966 match m;
7967 bool seen_symbol = false;
7969 if (!flag_dec_static)
7971 gfc_error ("%s at %C is a DEC extension, enable with "
7972 "%<-fdec-static%>",
7973 "STATIC");
7974 return MATCH_ERROR;
7977 gfc_match (" ::");
7979 for (;;)
7981 m = gfc_match_symbol (&sym, 0);
7982 switch (m)
7984 case MATCH_NO:
7985 break;
7987 case MATCH_ERROR:
7988 return MATCH_ERROR;
7990 case MATCH_YES:
7991 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7992 &gfc_current_locus))
7993 return MATCH_ERROR;
7994 seen_symbol = true;
7995 break;
7998 if (gfc_match_eos () == MATCH_YES)
7999 break;
8000 if (gfc_match_char (',') != MATCH_YES)
8001 goto syntax;
8004 if (!seen_symbol)
8006 gfc_error ("Expected entity-list in STATIC statement at %C");
8007 return MATCH_ERROR;
8010 return MATCH_YES;
8012 syntax:
8013 gfc_error ("Syntax error in STATIC statement at %C");
8014 return MATCH_ERROR;
8018 /* Save statements have a special syntax. */
8020 match
8021 gfc_match_save (void)
8023 char n[GFC_MAX_SYMBOL_LEN+1];
8024 gfc_common_head *c;
8025 gfc_symbol *sym;
8026 match m;
8028 if (gfc_match_eos () == MATCH_YES)
8030 if (gfc_current_ns->seen_save)
8032 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
8033 "follows previous SAVE statement"))
8034 return MATCH_ERROR;
8037 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
8038 return MATCH_YES;
8041 if (gfc_current_ns->save_all)
8043 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
8044 "blanket SAVE statement"))
8045 return MATCH_ERROR;
8048 gfc_match (" ::");
8050 for (;;)
8052 m = gfc_match_symbol (&sym, 0);
8053 switch (m)
8055 case MATCH_YES:
8056 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
8057 &gfc_current_locus))
8058 return MATCH_ERROR;
8059 goto next_item;
8061 case MATCH_NO:
8062 break;
8064 case MATCH_ERROR:
8065 return MATCH_ERROR;
8068 m = gfc_match (" / %n /", &n);
8069 if (m == MATCH_ERROR)
8070 return MATCH_ERROR;
8071 if (m == MATCH_NO)
8072 goto syntax;
8074 c = gfc_get_common (n, 0);
8075 c->saved = 1;
8077 gfc_current_ns->seen_save = 1;
8079 next_item:
8080 if (gfc_match_eos () == MATCH_YES)
8081 break;
8082 if (gfc_match_char (',') != MATCH_YES)
8083 goto syntax;
8086 return MATCH_YES;
8088 syntax:
8089 gfc_error ("Syntax error in SAVE statement at %C");
8090 return MATCH_ERROR;
8094 match
8095 gfc_match_value (void)
8097 gfc_symbol *sym;
8098 match m;
8100 /* This is not allowed within a BLOCK construct! */
8101 if (gfc_current_state () == COMP_BLOCK)
8103 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
8104 return MATCH_ERROR;
8107 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
8108 return MATCH_ERROR;
8110 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8112 return MATCH_ERROR;
8115 if (gfc_match_eos () == MATCH_YES)
8116 goto syntax;
8118 for(;;)
8120 m = gfc_match_symbol (&sym, 0);
8121 switch (m)
8123 case MATCH_YES:
8124 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
8125 return MATCH_ERROR;
8126 goto next_item;
8128 case MATCH_NO:
8129 break;
8131 case MATCH_ERROR:
8132 return MATCH_ERROR;
8135 next_item:
8136 if (gfc_match_eos () == MATCH_YES)
8137 break;
8138 if (gfc_match_char (',') != MATCH_YES)
8139 goto syntax;
8142 return MATCH_YES;
8144 syntax:
8145 gfc_error ("Syntax error in VALUE statement at %C");
8146 return MATCH_ERROR;
8150 match
8151 gfc_match_volatile (void)
8153 gfc_symbol *sym;
8154 match m;
8156 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
8157 return MATCH_ERROR;
8159 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8161 return MATCH_ERROR;
8164 if (gfc_match_eos () == MATCH_YES)
8165 goto syntax;
8167 for(;;)
8169 /* VOLATILE is special because it can be added to host-associated
8170 symbols locally. Except for coarrays. */
8171 m = gfc_match_symbol (&sym, 1);
8172 switch (m)
8174 case MATCH_YES:
8175 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
8176 for variable in a BLOCK which is defined outside of the BLOCK. */
8177 if (sym->ns != gfc_current_ns && sym->attr.codimension)
8179 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
8180 "%C, which is use-/host-associated", sym->name);
8181 return MATCH_ERROR;
8183 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
8184 return MATCH_ERROR;
8185 goto next_item;
8187 case MATCH_NO:
8188 break;
8190 case MATCH_ERROR:
8191 return MATCH_ERROR;
8194 next_item:
8195 if (gfc_match_eos () == MATCH_YES)
8196 break;
8197 if (gfc_match_char (',') != MATCH_YES)
8198 goto syntax;
8201 return MATCH_YES;
8203 syntax:
8204 gfc_error ("Syntax error in VOLATILE statement at %C");
8205 return MATCH_ERROR;
8209 match
8210 gfc_match_asynchronous (void)
8212 gfc_symbol *sym;
8213 match m;
8215 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
8216 return MATCH_ERROR;
8218 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8220 return MATCH_ERROR;
8223 if (gfc_match_eos () == MATCH_YES)
8224 goto syntax;
8226 for(;;)
8228 /* ASYNCHRONOUS is special because it can be added to host-associated
8229 symbols locally. */
8230 m = gfc_match_symbol (&sym, 1);
8231 switch (m)
8233 case MATCH_YES:
8234 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
8235 return MATCH_ERROR;
8236 goto next_item;
8238 case MATCH_NO:
8239 break;
8241 case MATCH_ERROR:
8242 return MATCH_ERROR;
8245 next_item:
8246 if (gfc_match_eos () == MATCH_YES)
8247 break;
8248 if (gfc_match_char (',') != MATCH_YES)
8249 goto syntax;
8252 return MATCH_YES;
8254 syntax:
8255 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
8256 return MATCH_ERROR;
8260 /* Match a module procedure statement in a submodule. */
8262 match
8263 gfc_match_submod_proc (void)
8265 char name[GFC_MAX_SYMBOL_LEN + 1];
8266 gfc_symbol *sym, *fsym;
8267 match m;
8268 gfc_formal_arglist *formal, *head, *tail;
8270 if (gfc_current_state () != COMP_CONTAINS
8271 || !(gfc_state_stack->previous
8272 && (gfc_state_stack->previous->state == COMP_SUBMODULE
8273 || gfc_state_stack->previous->state == COMP_MODULE)))
8274 return MATCH_NO;
8276 m = gfc_match (" module% procedure% %n", name);
8277 if (m != MATCH_YES)
8278 return m;
8280 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
8281 "at %C"))
8282 return MATCH_ERROR;
8284 if (get_proc_name (name, &sym, false))
8285 return MATCH_ERROR;
8287 /* Make sure that the result field is appropriately filled, even though
8288 the result symbol will be replaced later on. */
8289 if (sym->tlink && sym->tlink->attr.function)
8291 if (sym->tlink->result
8292 && sym->tlink->result != sym->tlink)
8293 sym->result= sym->tlink->result;
8294 else
8295 sym->result = sym;
8298 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
8299 the symbol existed before. */
8300 sym->declared_at = gfc_current_locus;
8302 if (!sym->attr.module_procedure)
8303 return MATCH_ERROR;
8305 /* Signal match_end to expect "end procedure". */
8306 sym->abr_modproc_decl = 1;
8308 /* Change from IFSRC_IFBODY coming from the interface declaration. */
8309 sym->attr.if_source = IFSRC_DECL;
8311 gfc_new_block = sym;
8313 /* Make a new formal arglist with the symbols in the procedure
8314 namespace. */
8315 head = tail = NULL;
8316 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
8318 if (formal == sym->formal)
8319 head = tail = gfc_get_formal_arglist ();
8320 else
8322 tail->next = gfc_get_formal_arglist ();
8323 tail = tail->next;
8326 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
8327 goto cleanup;
8329 tail->sym = fsym;
8330 gfc_set_sym_referenced (fsym);
8333 /* The dummy symbols get cleaned up, when the formal_namespace of the
8334 interface declaration is cleared. This allows us to add the
8335 explicit interface as is done for other type of procedure. */
8336 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
8337 &gfc_current_locus))
8338 return MATCH_ERROR;
8340 if (gfc_match_eos () != MATCH_YES)
8342 gfc_syntax_error (ST_MODULE_PROC);
8343 return MATCH_ERROR;
8346 return MATCH_YES;
8348 cleanup:
8349 gfc_free_formal_arglist (head);
8350 return MATCH_ERROR;
8354 /* Match a module procedure statement. Note that we have to modify
8355 symbols in the parent's namespace because the current one was there
8356 to receive symbols that are in an interface's formal argument list. */
8358 match
8359 gfc_match_modproc (void)
8361 char name[GFC_MAX_SYMBOL_LEN + 1];
8362 gfc_symbol *sym;
8363 match m;
8364 locus old_locus;
8365 gfc_namespace *module_ns;
8366 gfc_interface *old_interface_head, *interface;
8368 if (gfc_state_stack->state != COMP_INTERFACE
8369 || gfc_state_stack->previous == NULL
8370 || current_interface.type == INTERFACE_NAMELESS
8371 || current_interface.type == INTERFACE_ABSTRACT)
8373 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
8374 "interface");
8375 return MATCH_ERROR;
8378 module_ns = gfc_current_ns->parent;
8379 for (; module_ns; module_ns = module_ns->parent)
8380 if (module_ns->proc_name->attr.flavor == FL_MODULE
8381 || module_ns->proc_name->attr.flavor == FL_PROGRAM
8382 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
8383 && !module_ns->proc_name->attr.contained))
8384 break;
8386 if (module_ns == NULL)
8387 return MATCH_ERROR;
8389 /* Store the current state of the interface. We will need it if we
8390 end up with a syntax error and need to recover. */
8391 old_interface_head = gfc_current_interface_head ();
8393 /* Check if the F2008 optional double colon appears. */
8394 gfc_gobble_whitespace ();
8395 old_locus = gfc_current_locus;
8396 if (gfc_match ("::") == MATCH_YES)
8398 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
8399 "MODULE PROCEDURE statement at %L", &old_locus))
8400 return MATCH_ERROR;
8402 else
8403 gfc_current_locus = old_locus;
8405 for (;;)
8407 bool last = false;
8408 old_locus = gfc_current_locus;
8410 m = gfc_match_name (name);
8411 if (m == MATCH_NO)
8412 goto syntax;
8413 if (m != MATCH_YES)
8414 return MATCH_ERROR;
8416 /* Check for syntax error before starting to add symbols to the
8417 current namespace. */
8418 if (gfc_match_eos () == MATCH_YES)
8419 last = true;
8421 if (!last && gfc_match_char (',') != MATCH_YES)
8422 goto syntax;
8424 /* Now we're sure the syntax is valid, we process this item
8425 further. */
8426 if (gfc_get_symbol (name, module_ns, &sym))
8427 return MATCH_ERROR;
8429 if (sym->attr.intrinsic)
8431 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
8432 "PROCEDURE", &old_locus);
8433 return MATCH_ERROR;
8436 if (sym->attr.proc != PROC_MODULE
8437 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8438 return MATCH_ERROR;
8440 if (!gfc_add_interface (sym))
8441 return MATCH_ERROR;
8443 sym->attr.mod_proc = 1;
8444 sym->declared_at = old_locus;
8446 if (last)
8447 break;
8450 return MATCH_YES;
8452 syntax:
8453 /* Restore the previous state of the interface. */
8454 interface = gfc_current_interface_head ();
8455 gfc_set_current_interface_head (old_interface_head);
8457 /* Free the new interfaces. */
8458 while (interface != old_interface_head)
8460 gfc_interface *i = interface->next;
8461 free (interface);
8462 interface = i;
8465 /* And issue a syntax error. */
8466 gfc_syntax_error (ST_MODULE_PROC);
8467 return MATCH_ERROR;
8471 /* Check a derived type that is being extended. */
8473 static gfc_symbol*
8474 check_extended_derived_type (char *name)
8476 gfc_symbol *extended;
8478 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
8480 gfc_error ("Ambiguous symbol in TYPE definition at %C");
8481 return NULL;
8484 extended = gfc_find_dt_in_generic (extended);
8486 /* F08:C428. */
8487 if (!extended)
8489 gfc_error ("Symbol %qs at %C has not been previously defined", name);
8490 return NULL;
8493 if (extended->attr.flavor != FL_DERIVED)
8495 gfc_error ("%qs in EXTENDS expression at %C is not a "
8496 "derived type", name);
8497 return NULL;
8500 if (extended->attr.is_bind_c)
8502 gfc_error ("%qs cannot be extended at %C because it "
8503 "is BIND(C)", extended->name);
8504 return NULL;
8507 if (extended->attr.sequence)
8509 gfc_error ("%qs cannot be extended at %C because it "
8510 "is a SEQUENCE type", extended->name);
8511 return NULL;
8514 return extended;
8518 /* Match the optional attribute specifiers for a type declaration.
8519 Return MATCH_ERROR if an error is encountered in one of the handled
8520 attributes (public, private, bind(c)), MATCH_NO if what's found is
8521 not a handled attribute, and MATCH_YES otherwise. TODO: More error
8522 checking on attribute conflicts needs to be done. */
8524 match
8525 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
8527 /* See if the derived type is marked as private. */
8528 if (gfc_match (" , private") == MATCH_YES)
8530 if (gfc_current_state () != COMP_MODULE)
8532 gfc_error ("Derived type at %C can only be PRIVATE in the "
8533 "specification part of a module");
8534 return MATCH_ERROR;
8537 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
8538 return MATCH_ERROR;
8540 else if (gfc_match (" , public") == MATCH_YES)
8542 if (gfc_current_state () != COMP_MODULE)
8544 gfc_error ("Derived type at %C can only be PUBLIC in the "
8545 "specification part of a module");
8546 return MATCH_ERROR;
8549 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
8550 return MATCH_ERROR;
8552 else if (gfc_match (" , bind ( c )") == MATCH_YES)
8554 /* If the type is defined to be bind(c) it then needs to make
8555 sure that all fields are interoperable. This will
8556 need to be a semantic check on the finished derived type.
8557 See 15.2.3 (lines 9-12) of F2003 draft. */
8558 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
8559 return MATCH_ERROR;
8561 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
8563 else if (gfc_match (" , abstract") == MATCH_YES)
8565 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
8566 return MATCH_ERROR;
8568 if (!gfc_add_abstract (attr, &gfc_current_locus))
8569 return MATCH_ERROR;
8571 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
8573 if (!gfc_add_extension (attr, &gfc_current_locus))
8574 return MATCH_ERROR;
8576 else
8577 return MATCH_NO;
8579 /* If we get here, something matched. */
8580 return MATCH_YES;
8584 /* Common function for type declaration blocks similar to derived types, such
8585 as STRUCTURES and MAPs. Unlike derived types, a structure type
8586 does NOT have a generic symbol matching the name given by the user.
8587 STRUCTUREs can share names with variables and PARAMETERs so we must allow
8588 for the creation of an independent symbol.
8589 Other parameters are a message to prefix errors with, the name of the new
8590 type to be created, and the flavor to add to the resulting symbol. */
8592 static bool
8593 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
8594 gfc_symbol **result)
8596 gfc_symbol *sym;
8597 locus where;
8599 gcc_assert (name[0] == (char) TOUPPER (name[0]));
8601 if (decl)
8602 where = *decl;
8603 else
8604 where = gfc_current_locus;
8606 if (gfc_get_symbol (name, NULL, &sym))
8607 return false;
8609 if (!sym)
8611 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
8612 return false;
8615 if (sym->components != NULL || sym->attr.zero_comp)
8617 gfc_error ("Type definition of %qs at %C was already defined at %L",
8618 sym->name, &sym->declared_at);
8619 return false;
8622 sym->declared_at = where;
8624 if (sym->attr.flavor != fl
8625 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
8626 return false;
8628 if (!sym->hash_value)
8629 /* Set the hash for the compound name for this type. */
8630 sym->hash_value = gfc_hash_value (sym);
8632 /* Normally the type is expected to have been completely parsed by the time
8633 a field declaration with this type is seen. For unions, maps, and nested
8634 structure declarations, we need to indicate that it is okay that we
8635 haven't seen any components yet. This will be updated after the structure
8636 is fully parsed. */
8637 sym->attr.zero_comp = 0;
8639 /* Structures always act like derived-types with the SEQUENCE attribute */
8640 gfc_add_sequence (&sym->attr, sym->name, NULL);
8642 if (result) *result = sym;
8644 return true;
8648 /* Match the opening of a MAP block. Like a struct within a union in C;
8649 behaves identical to STRUCTURE blocks. */
8651 match
8652 gfc_match_map (void)
8654 /* Counter used to give unique internal names to map structures. */
8655 static unsigned int gfc_map_id = 0;
8656 char name[GFC_MAX_SYMBOL_LEN + 1];
8657 gfc_symbol *sym;
8658 locus old_loc;
8660 old_loc = gfc_current_locus;
8662 if (gfc_match_eos () != MATCH_YES)
8664 gfc_error ("Junk after MAP statement at %C");
8665 gfc_current_locus = old_loc;
8666 return MATCH_ERROR;
8669 /* Map blocks are anonymous so we make up unique names for the symbol table
8670 which are invalid Fortran identifiers. */
8671 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
8673 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
8674 return MATCH_ERROR;
8676 gfc_new_block = sym;
8678 return MATCH_YES;
8682 /* Match the opening of a UNION block. */
8684 match
8685 gfc_match_union (void)
8687 /* Counter used to give unique internal names to union types. */
8688 static unsigned int gfc_union_id = 0;
8689 char name[GFC_MAX_SYMBOL_LEN + 1];
8690 gfc_symbol *sym;
8691 locus old_loc;
8693 old_loc = gfc_current_locus;
8695 if (gfc_match_eos () != MATCH_YES)
8697 gfc_error ("Junk after UNION statement at %C");
8698 gfc_current_locus = old_loc;
8699 return MATCH_ERROR;
8702 /* Unions are anonymous so we make up unique names for the symbol table
8703 which are invalid Fortran identifiers. */
8704 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
8706 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
8707 return MATCH_ERROR;
8709 gfc_new_block = sym;
8711 return MATCH_YES;
8715 /* Match the beginning of a STRUCTURE declaration. This is similar to
8716 matching the beginning of a derived type declaration with a few
8717 twists. The resulting type symbol has no access control or other
8718 interesting attributes. */
8720 match
8721 gfc_match_structure_decl (void)
8723 /* Counter used to give unique internal names to anonymous structures. */
8724 static unsigned int gfc_structure_id = 0;
8725 char name[GFC_MAX_SYMBOL_LEN + 1];
8726 gfc_symbol *sym;
8727 match m;
8728 locus where;
8730 if (!flag_dec_structure)
8732 gfc_error ("%s at %C is a DEC extension, enable with "
8733 "%<-fdec-structure%>",
8734 "STRUCTURE");
8735 return MATCH_ERROR;
8738 name[0] = '\0';
8740 m = gfc_match (" /%n/", name);
8741 if (m != MATCH_YES)
8743 /* Non-nested structure declarations require a structure name. */
8744 if (!gfc_comp_struct (gfc_current_state ()))
8746 gfc_error ("Structure name expected in non-nested structure "
8747 "declaration at %C");
8748 return MATCH_ERROR;
8750 /* This is an anonymous structure; make up a unique name for it
8751 (upper-case letters never make it to symbol names from the source).
8752 The important thing is initializing the type variable
8753 and setting gfc_new_symbol, which is immediately used by
8754 parse_structure () and variable_decl () to add components of
8755 this type. */
8756 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
8759 where = gfc_current_locus;
8760 /* No field list allowed after non-nested structure declaration. */
8761 if (!gfc_comp_struct (gfc_current_state ())
8762 && gfc_match_eos () != MATCH_YES)
8764 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
8765 return MATCH_ERROR;
8768 /* Make sure the name is not the name of an intrinsic type. */
8769 if (gfc_is_intrinsic_typename (name))
8771 gfc_error ("Structure name %qs at %C cannot be the same as an"
8772 " intrinsic type", name);
8773 return MATCH_ERROR;
8776 /* Store the actual type symbol for the structure with an upper-case first
8777 letter (an invalid Fortran identifier). */
8779 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
8780 return MATCH_ERROR;
8782 gfc_new_block = sym;
8783 return MATCH_YES;
8787 /* This function does some work to determine which matcher should be used to
8788 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
8789 * as an alias for PRINT from derived type declarations, TYPE IS statements,
8790 * and derived type data declarations. */
8792 match
8793 gfc_match_type (gfc_statement *st)
8795 char name[GFC_MAX_SYMBOL_LEN + 1];
8796 match m;
8797 locus old_loc;
8799 /* Requires -fdec. */
8800 if (!flag_dec)
8801 return MATCH_NO;
8803 m = gfc_match ("type");
8804 if (m != MATCH_YES)
8805 return m;
8806 /* If we already have an error in the buffer, it is probably from failing to
8807 * match a derived type data declaration. Let it happen. */
8808 else if (gfc_error_flag_test ())
8809 return MATCH_NO;
8811 old_loc = gfc_current_locus;
8812 *st = ST_NONE;
8814 /* If we see an attribute list before anything else it's definitely a derived
8815 * type declaration. */
8816 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
8818 gfc_current_locus = old_loc;
8819 *st = ST_DERIVED_DECL;
8820 return gfc_match_derived_decl ();
8823 /* By now "TYPE" has already been matched. If we do not see a name, this may
8824 * be something like "TYPE *" or "TYPE <fmt>". */
8825 m = gfc_match_name (name);
8826 if (m != MATCH_YES)
8828 /* Let print match if it can, otherwise throw an error from
8829 * gfc_match_derived_decl. */
8830 gfc_current_locus = old_loc;
8831 if (gfc_match_print () == MATCH_YES)
8833 *st = ST_WRITE;
8834 return MATCH_YES;
8836 gfc_current_locus = old_loc;
8837 *st = ST_DERIVED_DECL;
8838 return gfc_match_derived_decl ();
8841 /* A derived type declaration requires an EOS. Without it, assume print. */
8842 m = gfc_match_eos ();
8843 if (m == MATCH_NO)
8845 /* Check manually for TYPE IS (... - this is invalid print syntax. */
8846 if (strncmp ("is", name, 3) == 0
8847 && gfc_match (" (", name) == MATCH_YES)
8849 gfc_current_locus = old_loc;
8850 gcc_assert (gfc_match (" is") == MATCH_YES);
8851 *st = ST_TYPE_IS;
8852 return gfc_match_type_is ();
8854 gfc_current_locus = old_loc;
8855 *st = ST_WRITE;
8856 return gfc_match_print ();
8858 else
8860 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
8861 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
8862 * Otherwise if gfc_match_derived_decl fails it's probably an existing
8863 * symbol which can be printed. */
8864 gfc_current_locus = old_loc;
8865 m = gfc_match_derived_decl ();
8866 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
8868 *st = ST_DERIVED_DECL;
8869 return m;
8871 gfc_current_locus = old_loc;
8872 *st = ST_WRITE;
8873 return gfc_match_print ();
8876 return MATCH_NO;
8880 /* Match the beginning of a derived type declaration. If a type name
8881 was the result of a function, then it is possible to have a symbol
8882 already to be known as a derived type yet have no components. */
8884 match
8885 gfc_match_derived_decl (void)
8887 char name[GFC_MAX_SYMBOL_LEN + 1];
8888 char parent[GFC_MAX_SYMBOL_LEN + 1];
8889 symbol_attribute attr;
8890 gfc_symbol *sym, *gensym;
8891 gfc_symbol *extended;
8892 match m;
8893 match is_type_attr_spec = MATCH_NO;
8894 bool seen_attr = false;
8895 gfc_interface *intr = NULL, *head;
8897 if (gfc_comp_struct (gfc_current_state ()))
8898 return MATCH_NO;
8900 name[0] = '\0';
8901 parent[0] = '\0';
8902 gfc_clear_attr (&attr);
8903 extended = NULL;
8907 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
8908 if (is_type_attr_spec == MATCH_ERROR)
8909 return MATCH_ERROR;
8910 if (is_type_attr_spec == MATCH_YES)
8911 seen_attr = true;
8912 } while (is_type_attr_spec == MATCH_YES);
8914 /* Deal with derived type extensions. The extension attribute has
8915 been added to 'attr' but now the parent type must be found and
8916 checked. */
8917 if (parent[0])
8918 extended = check_extended_derived_type (parent);
8920 if (parent[0] && !extended)
8921 return MATCH_ERROR;
8923 if (gfc_match (" ::") != MATCH_YES && seen_attr)
8925 gfc_error ("Expected :: in TYPE definition at %C");
8926 return MATCH_ERROR;
8929 m = gfc_match (" %n%t", name);
8930 if (m != MATCH_YES)
8931 return m;
8933 /* Make sure the name is not the name of an intrinsic type. */
8934 if (gfc_is_intrinsic_typename (name))
8936 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
8937 "type", name);
8938 return MATCH_ERROR;
8941 if (gfc_get_symbol (name, NULL, &gensym))
8942 return MATCH_ERROR;
8944 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
8946 gfc_error ("Derived type name %qs at %C already has a basic type "
8947 "of %s", gensym->name, gfc_typename (&gensym->ts));
8948 return MATCH_ERROR;
8951 if (!gensym->attr.generic
8952 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
8953 return MATCH_ERROR;
8955 if (!gensym->attr.function
8956 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
8957 return MATCH_ERROR;
8959 sym = gfc_find_dt_in_generic (gensym);
8961 if (sym && (sym->components != NULL || sym->attr.zero_comp))
8963 gfc_error ("Derived type definition of %qs at %C has already been "
8964 "defined", sym->name);
8965 return MATCH_ERROR;
8968 if (!sym)
8970 /* Use upper case to save the actual derived-type symbol. */
8971 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
8972 sym->name = gfc_get_string ("%s", gensym->name);
8973 head = gensym->generic;
8974 intr = gfc_get_interface ();
8975 intr->sym = sym;
8976 intr->where = gfc_current_locus;
8977 intr->sym->declared_at = gfc_current_locus;
8978 intr->next = head;
8979 gensym->generic = intr;
8980 gensym->attr.if_source = IFSRC_DECL;
8983 /* The symbol may already have the derived attribute without the
8984 components. The ways this can happen is via a function
8985 definition, an INTRINSIC statement or a subtype in another
8986 derived type that is a pointer. The first part of the AND clause
8987 is true if the symbol is not the return value of a function. */
8988 if (sym->attr.flavor != FL_DERIVED
8989 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
8990 return MATCH_ERROR;
8992 if (attr.access != ACCESS_UNKNOWN
8993 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
8994 return MATCH_ERROR;
8995 else if (sym->attr.access == ACCESS_UNKNOWN
8996 && gensym->attr.access != ACCESS_UNKNOWN
8997 && !gfc_add_access (&sym->attr, gensym->attr.access,
8998 sym->name, NULL))
8999 return MATCH_ERROR;
9001 if (sym->attr.access != ACCESS_UNKNOWN
9002 && gensym->attr.access == ACCESS_UNKNOWN)
9003 gensym->attr.access = sym->attr.access;
9005 /* See if the derived type was labeled as bind(c). */
9006 if (attr.is_bind_c != 0)
9007 sym->attr.is_bind_c = attr.is_bind_c;
9009 /* Construct the f2k_derived namespace if it is not yet there. */
9010 if (!sym->f2k_derived)
9011 sym->f2k_derived = gfc_get_namespace (NULL, 0);
9013 if (extended && !sym->components)
9015 gfc_component *p;
9017 /* Add the extended derived type as the first component. */
9018 gfc_add_component (sym, parent, &p);
9019 extended->refs++;
9020 gfc_set_sym_referenced (extended);
9022 p->ts.type = BT_DERIVED;
9023 p->ts.u.derived = extended;
9024 p->initializer = gfc_default_initializer (&p->ts);
9026 /* Set extension level. */
9027 if (extended->attr.extension == 255)
9029 /* Since the extension field is 8 bit wide, we can only have
9030 up to 255 extension levels. */
9031 gfc_error ("Maximum extension level reached with type %qs at %L",
9032 extended->name, &extended->declared_at);
9033 return MATCH_ERROR;
9035 sym->attr.extension = extended->attr.extension + 1;
9037 /* Provide the links between the extended type and its extension. */
9038 if (!extended->f2k_derived)
9039 extended->f2k_derived = gfc_get_namespace (NULL, 0);
9042 if (!sym->hash_value)
9043 /* Set the hash for the compound name for this type. */
9044 sym->hash_value = gfc_hash_value (sym);
9046 /* Take over the ABSTRACT attribute. */
9047 sym->attr.abstract = attr.abstract;
9049 gfc_new_block = sym;
9051 return MATCH_YES;
9055 /* Cray Pointees can be declared as:
9056 pointer (ipt, a (n,m,...,*)) */
9058 match
9059 gfc_mod_pointee_as (gfc_array_spec *as)
9061 as->cray_pointee = true; /* This will be useful to know later. */
9062 if (as->type == AS_ASSUMED_SIZE)
9063 as->cp_was_assumed = true;
9064 else if (as->type == AS_ASSUMED_SHAPE)
9066 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
9067 return MATCH_ERROR;
9069 return MATCH_YES;
9073 /* Match the enum definition statement, here we are trying to match
9074 the first line of enum definition statement.
9075 Returns MATCH_YES if match is found. */
9077 match
9078 gfc_match_enum (void)
9080 match m;
9082 m = gfc_match_eos ();
9083 if (m != MATCH_YES)
9084 return m;
9086 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
9087 return MATCH_ERROR;
9089 return MATCH_YES;
9093 /* Returns an initializer whose value is one higher than the value of the
9094 LAST_INITIALIZER argument. If the argument is NULL, the
9095 initializers value will be set to zero. The initializer's kind
9096 will be set to gfc_c_int_kind.
9098 If -fshort-enums is given, the appropriate kind will be selected
9099 later after all enumerators have been parsed. A warning is issued
9100 here if an initializer exceeds gfc_c_int_kind. */
9102 static gfc_expr *
9103 enum_initializer (gfc_expr *last_initializer, locus where)
9105 gfc_expr *result;
9106 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
9108 mpz_init (result->value.integer);
9110 if (last_initializer != NULL)
9112 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
9113 result->where = last_initializer->where;
9115 if (gfc_check_integer_range (result->value.integer,
9116 gfc_c_int_kind) != ARITH_OK)
9118 gfc_error ("Enumerator exceeds the C integer type at %C");
9119 return NULL;
9122 else
9124 /* Control comes here, if it's the very first enumerator and no
9125 initializer has been given. It will be initialized to zero. */
9126 mpz_set_si (result->value.integer, 0);
9129 return result;
9133 /* Match a variable name with an optional initializer. When this
9134 subroutine is called, a variable is expected to be parsed next.
9135 Depending on what is happening at the moment, updates either the
9136 symbol table or the current interface. */
9138 static match
9139 enumerator_decl (void)
9141 char name[GFC_MAX_SYMBOL_LEN + 1];
9142 gfc_expr *initializer;
9143 gfc_array_spec *as = NULL;
9144 gfc_symbol *sym;
9145 locus var_locus;
9146 match m;
9147 bool t;
9148 locus old_locus;
9150 initializer = NULL;
9151 old_locus = gfc_current_locus;
9153 /* When we get here, we've just matched a list of attributes and
9154 maybe a type and a double colon. The next thing we expect to see
9155 is the name of the symbol. */
9156 m = gfc_match_name (name);
9157 if (m != MATCH_YES)
9158 goto cleanup;
9160 var_locus = gfc_current_locus;
9162 /* OK, we've successfully matched the declaration. Now put the
9163 symbol in the current namespace. If we fail to create the symbol,
9164 bail out. */
9165 if (!build_sym (name, NULL, false, &as, &var_locus))
9167 m = MATCH_ERROR;
9168 goto cleanup;
9171 /* The double colon must be present in order to have initializers.
9172 Otherwise the statement is ambiguous with an assignment statement. */
9173 if (colon_seen)
9175 if (gfc_match_char ('=') == MATCH_YES)
9177 m = gfc_match_init_expr (&initializer);
9178 if (m == MATCH_NO)
9180 gfc_error ("Expected an initialization expression at %C");
9181 m = MATCH_ERROR;
9184 if (m != MATCH_YES)
9185 goto cleanup;
9189 /* If we do not have an initializer, the initialization value of the
9190 previous enumerator (stored in last_initializer) is incremented
9191 by 1 and is used to initialize the current enumerator. */
9192 if (initializer == NULL)
9193 initializer = enum_initializer (last_initializer, old_locus);
9195 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
9197 gfc_error ("ENUMERATOR %L not initialized with integer expression",
9198 &var_locus);
9199 m = MATCH_ERROR;
9200 goto cleanup;
9203 /* Store this current initializer, for the next enumerator variable
9204 to be parsed. add_init_expr_to_sym() zeros initializer, so we
9205 use last_initializer below. */
9206 last_initializer = initializer;
9207 t = add_init_expr_to_sym (name, &initializer, &var_locus);
9209 /* Maintain enumerator history. */
9210 gfc_find_symbol (name, NULL, 0, &sym);
9211 create_enum_history (sym, last_initializer);
9213 return (t) ? MATCH_YES : MATCH_ERROR;
9215 cleanup:
9216 /* Free stuff up and return. */
9217 gfc_free_expr (initializer);
9219 return m;
9223 /* Match the enumerator definition statement. */
9225 match
9226 gfc_match_enumerator_def (void)
9228 match m;
9229 bool t;
9231 gfc_clear_ts (&current_ts);
9233 m = gfc_match (" enumerator");
9234 if (m != MATCH_YES)
9235 return m;
9237 m = gfc_match (" :: ");
9238 if (m == MATCH_ERROR)
9239 return m;
9241 colon_seen = (m == MATCH_YES);
9243 if (gfc_current_state () != COMP_ENUM)
9245 gfc_error ("ENUM definition statement expected before %C");
9246 gfc_free_enum_history ();
9247 return MATCH_ERROR;
9250 (&current_ts)->type = BT_INTEGER;
9251 (&current_ts)->kind = gfc_c_int_kind;
9253 gfc_clear_attr (&current_attr);
9254 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
9255 if (!t)
9257 m = MATCH_ERROR;
9258 goto cleanup;
9261 for (;;)
9263 m = enumerator_decl ();
9264 if (m == MATCH_ERROR)
9266 gfc_free_enum_history ();
9267 goto cleanup;
9269 if (m == MATCH_NO)
9270 break;
9272 if (gfc_match_eos () == MATCH_YES)
9273 goto cleanup;
9274 if (gfc_match_char (',') != MATCH_YES)
9275 break;
9278 if (gfc_current_state () == COMP_ENUM)
9280 gfc_free_enum_history ();
9281 gfc_error ("Syntax error in ENUMERATOR definition at %C");
9282 m = MATCH_ERROR;
9285 cleanup:
9286 gfc_free_array_spec (current_as);
9287 current_as = NULL;
9288 return m;
9293 /* Match binding attributes. */
9295 static match
9296 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
9298 bool found_passing = false;
9299 bool seen_ptr = false;
9300 match m = MATCH_YES;
9302 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
9303 this case the defaults are in there. */
9304 ba->access = ACCESS_UNKNOWN;
9305 ba->pass_arg = NULL;
9306 ba->pass_arg_num = 0;
9307 ba->nopass = 0;
9308 ba->non_overridable = 0;
9309 ba->deferred = 0;
9310 ba->ppc = ppc;
9312 /* If we find a comma, we believe there are binding attributes. */
9313 m = gfc_match_char (',');
9314 if (m == MATCH_NO)
9315 goto done;
9319 /* Access specifier. */
9321 m = gfc_match (" public");
9322 if (m == MATCH_ERROR)
9323 goto error;
9324 if (m == MATCH_YES)
9326 if (ba->access != ACCESS_UNKNOWN)
9328 gfc_error ("Duplicate access-specifier at %C");
9329 goto error;
9332 ba->access = ACCESS_PUBLIC;
9333 continue;
9336 m = gfc_match (" private");
9337 if (m == MATCH_ERROR)
9338 goto error;
9339 if (m == MATCH_YES)
9341 if (ba->access != ACCESS_UNKNOWN)
9343 gfc_error ("Duplicate access-specifier at %C");
9344 goto error;
9347 ba->access = ACCESS_PRIVATE;
9348 continue;
9351 /* If inside GENERIC, the following is not allowed. */
9352 if (!generic)
9355 /* NOPASS flag. */
9356 m = gfc_match (" nopass");
9357 if (m == MATCH_ERROR)
9358 goto error;
9359 if (m == MATCH_YES)
9361 if (found_passing)
9363 gfc_error ("Binding attributes already specify passing,"
9364 " illegal NOPASS at %C");
9365 goto error;
9368 found_passing = true;
9369 ba->nopass = 1;
9370 continue;
9373 /* PASS possibly including argument. */
9374 m = gfc_match (" pass");
9375 if (m == MATCH_ERROR)
9376 goto error;
9377 if (m == MATCH_YES)
9379 char arg[GFC_MAX_SYMBOL_LEN + 1];
9381 if (found_passing)
9383 gfc_error ("Binding attributes already specify passing,"
9384 " illegal PASS at %C");
9385 goto error;
9388 m = gfc_match (" ( %n )", arg);
9389 if (m == MATCH_ERROR)
9390 goto error;
9391 if (m == MATCH_YES)
9392 ba->pass_arg = gfc_get_string ("%s", arg);
9393 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
9395 found_passing = true;
9396 ba->nopass = 0;
9397 continue;
9400 if (ppc)
9402 /* POINTER flag. */
9403 m = gfc_match (" pointer");
9404 if (m == MATCH_ERROR)
9405 goto error;
9406 if (m == MATCH_YES)
9408 if (seen_ptr)
9410 gfc_error ("Duplicate POINTER attribute at %C");
9411 goto error;
9414 seen_ptr = true;
9415 continue;
9418 else
9420 /* NON_OVERRIDABLE flag. */
9421 m = gfc_match (" non_overridable");
9422 if (m == MATCH_ERROR)
9423 goto error;
9424 if (m == MATCH_YES)
9426 if (ba->non_overridable)
9428 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
9429 goto error;
9432 ba->non_overridable = 1;
9433 continue;
9436 /* DEFERRED flag. */
9437 m = gfc_match (" deferred");
9438 if (m == MATCH_ERROR)
9439 goto error;
9440 if (m == MATCH_YES)
9442 if (ba->deferred)
9444 gfc_error ("Duplicate DEFERRED at %C");
9445 goto error;
9448 ba->deferred = 1;
9449 continue;
9455 /* Nothing matching found. */
9456 if (generic)
9457 gfc_error ("Expected access-specifier at %C");
9458 else
9459 gfc_error ("Expected binding attribute at %C");
9460 goto error;
9462 while (gfc_match_char (',') == MATCH_YES);
9464 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
9465 if (ba->non_overridable && ba->deferred)
9467 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
9468 goto error;
9471 m = MATCH_YES;
9473 done:
9474 if (ba->access == ACCESS_UNKNOWN)
9475 ba->access = gfc_typebound_default_access;
9477 if (ppc && !seen_ptr)
9479 gfc_error ("POINTER attribute is required for procedure pointer component"
9480 " at %C");
9481 goto error;
9484 return m;
9486 error:
9487 return MATCH_ERROR;
9491 /* Match a PROCEDURE specific binding inside a derived type. */
9493 static match
9494 match_procedure_in_type (void)
9496 char name[GFC_MAX_SYMBOL_LEN + 1];
9497 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
9498 char* target = NULL, *ifc = NULL;
9499 gfc_typebound_proc tb;
9500 bool seen_colons;
9501 bool seen_attrs;
9502 match m;
9503 gfc_symtree* stree;
9504 gfc_namespace* ns;
9505 gfc_symbol* block;
9506 int num;
9508 /* Check current state. */
9509 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
9510 block = gfc_state_stack->previous->sym;
9511 gcc_assert (block);
9513 /* Try to match PROCEDURE(interface). */
9514 if (gfc_match (" (") == MATCH_YES)
9516 m = gfc_match_name (target_buf);
9517 if (m == MATCH_ERROR)
9518 return m;
9519 if (m != MATCH_YES)
9521 gfc_error ("Interface-name expected after %<(%> at %C");
9522 return MATCH_ERROR;
9525 if (gfc_match (" )") != MATCH_YES)
9527 gfc_error ("%<)%> expected at %C");
9528 return MATCH_ERROR;
9531 ifc = target_buf;
9534 /* Construct the data structure. */
9535 memset (&tb, 0, sizeof (tb));
9536 tb.where = gfc_current_locus;
9538 /* Match binding attributes. */
9539 m = match_binding_attributes (&tb, false, false);
9540 if (m == MATCH_ERROR)
9541 return m;
9542 seen_attrs = (m == MATCH_YES);
9544 /* Check that attribute DEFERRED is given if an interface is specified. */
9545 if (tb.deferred && !ifc)
9547 gfc_error ("Interface must be specified for DEFERRED binding at %C");
9548 return MATCH_ERROR;
9550 if (ifc && !tb.deferred)
9552 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
9553 return MATCH_ERROR;
9556 /* Match the colons. */
9557 m = gfc_match (" ::");
9558 if (m == MATCH_ERROR)
9559 return m;
9560 seen_colons = (m == MATCH_YES);
9561 if (seen_attrs && !seen_colons)
9563 gfc_error ("Expected %<::%> after binding-attributes at %C");
9564 return MATCH_ERROR;
9567 /* Match the binding names. */
9568 for(num=1;;num++)
9570 m = gfc_match_name (name);
9571 if (m == MATCH_ERROR)
9572 return m;
9573 if (m == MATCH_NO)
9575 gfc_error ("Expected binding name at %C");
9576 return MATCH_ERROR;
9579 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
9580 return MATCH_ERROR;
9582 /* Try to match the '=> target', if it's there. */
9583 target = ifc;
9584 m = gfc_match (" =>");
9585 if (m == MATCH_ERROR)
9586 return m;
9587 if (m == MATCH_YES)
9589 if (tb.deferred)
9591 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
9592 return MATCH_ERROR;
9595 if (!seen_colons)
9597 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
9598 " at %C");
9599 return MATCH_ERROR;
9602 m = gfc_match_name (target_buf);
9603 if (m == MATCH_ERROR)
9604 return m;
9605 if (m == MATCH_NO)
9607 gfc_error ("Expected binding target after %<=>%> at %C");
9608 return MATCH_ERROR;
9610 target = target_buf;
9613 /* If no target was found, it has the same name as the binding. */
9614 if (!target)
9615 target = name;
9617 /* Get the namespace to insert the symbols into. */
9618 ns = block->f2k_derived;
9619 gcc_assert (ns);
9621 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
9622 if (tb.deferred && !block->attr.abstract)
9624 gfc_error ("Type %qs containing DEFERRED binding at %C "
9625 "is not ABSTRACT", block->name);
9626 return MATCH_ERROR;
9629 /* See if we already have a binding with this name in the symtree which
9630 would be an error. If a GENERIC already targeted this binding, it may
9631 be already there but then typebound is still NULL. */
9632 stree = gfc_find_symtree (ns->tb_sym_root, name);
9633 if (stree && stree->n.tb)
9635 gfc_error ("There is already a procedure with binding name %qs for "
9636 "the derived type %qs at %C", name, block->name);
9637 return MATCH_ERROR;
9640 /* Insert it and set attributes. */
9642 if (!stree)
9644 stree = gfc_new_symtree (&ns->tb_sym_root, name);
9645 gcc_assert (stree);
9647 stree->n.tb = gfc_get_typebound_proc (&tb);
9649 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
9650 false))
9651 return MATCH_ERROR;
9652 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
9653 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
9654 target, &stree->n.tb->u.specific->n.sym->declared_at);
9656 if (gfc_match_eos () == MATCH_YES)
9657 return MATCH_YES;
9658 if (gfc_match_char (',') != MATCH_YES)
9659 goto syntax;
9662 syntax:
9663 gfc_error ("Syntax error in PROCEDURE statement at %C");
9664 return MATCH_ERROR;
9668 /* Match a GENERIC procedure binding inside a derived type. */
9670 match
9671 gfc_match_generic (void)
9673 char name[GFC_MAX_SYMBOL_LEN + 1];
9674 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
9675 gfc_symbol* block;
9676 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
9677 gfc_typebound_proc* tb;
9678 gfc_namespace* ns;
9679 interface_type op_type;
9680 gfc_intrinsic_op op;
9681 match m;
9683 /* Check current state. */
9684 if (gfc_current_state () == COMP_DERIVED)
9686 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
9687 return MATCH_ERROR;
9689 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
9690 return MATCH_NO;
9691 block = gfc_state_stack->previous->sym;
9692 ns = block->f2k_derived;
9693 gcc_assert (block && ns);
9695 memset (&tbattr, 0, sizeof (tbattr));
9696 tbattr.where = gfc_current_locus;
9698 /* See if we get an access-specifier. */
9699 m = match_binding_attributes (&tbattr, true, false);
9700 if (m == MATCH_ERROR)
9701 goto error;
9703 /* Now the colons, those are required. */
9704 if (gfc_match (" ::") != MATCH_YES)
9706 gfc_error ("Expected %<::%> at %C");
9707 goto error;
9710 /* Match the binding name; depending on type (operator / generic) format
9711 it for future error messages into bind_name. */
9713 m = gfc_match_generic_spec (&op_type, name, &op);
9714 if (m == MATCH_ERROR)
9715 return MATCH_ERROR;
9716 if (m == MATCH_NO)
9718 gfc_error ("Expected generic name or operator descriptor at %C");
9719 goto error;
9722 switch (op_type)
9724 case INTERFACE_GENERIC:
9725 case INTERFACE_DTIO:
9726 snprintf (bind_name, sizeof (bind_name), "%s", name);
9727 break;
9729 case INTERFACE_USER_OP:
9730 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
9731 break;
9733 case INTERFACE_INTRINSIC_OP:
9734 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
9735 gfc_op2string (op));
9736 break;
9738 case INTERFACE_NAMELESS:
9739 gfc_error ("Malformed GENERIC statement at %C");
9740 goto error;
9741 break;
9743 default:
9744 gcc_unreachable ();
9747 /* Match the required =>. */
9748 if (gfc_match (" =>") != MATCH_YES)
9750 gfc_error ("Expected %<=>%> at %C");
9751 goto error;
9754 /* Try to find existing GENERIC binding with this name / for this operator;
9755 if there is something, check that it is another GENERIC and then extend
9756 it rather than building a new node. Otherwise, create it and put it
9757 at the right position. */
9759 switch (op_type)
9761 case INTERFACE_DTIO:
9762 case INTERFACE_USER_OP:
9763 case INTERFACE_GENERIC:
9765 const bool is_op = (op_type == INTERFACE_USER_OP);
9766 gfc_symtree* st;
9768 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
9769 tb = st ? st->n.tb : NULL;
9770 break;
9773 case INTERFACE_INTRINSIC_OP:
9774 tb = ns->tb_op[op];
9775 break;
9777 default:
9778 gcc_unreachable ();
9781 if (tb)
9783 if (!tb->is_generic)
9785 gcc_assert (op_type == INTERFACE_GENERIC);
9786 gfc_error ("There's already a non-generic procedure with binding name"
9787 " %qs for the derived type %qs at %C",
9788 bind_name, block->name);
9789 goto error;
9792 if (tb->access != tbattr.access)
9794 gfc_error ("Binding at %C must have the same access as already"
9795 " defined binding %qs", bind_name);
9796 goto error;
9799 else
9801 tb = gfc_get_typebound_proc (NULL);
9802 tb->where = gfc_current_locus;
9803 tb->access = tbattr.access;
9804 tb->is_generic = 1;
9805 tb->u.generic = NULL;
9807 switch (op_type)
9809 case INTERFACE_DTIO:
9810 case INTERFACE_GENERIC:
9811 case INTERFACE_USER_OP:
9813 const bool is_op = (op_type == INTERFACE_USER_OP);
9814 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
9815 &ns->tb_sym_root, name);
9816 gcc_assert (st);
9817 st->n.tb = tb;
9819 break;
9822 case INTERFACE_INTRINSIC_OP:
9823 ns->tb_op[op] = tb;
9824 break;
9826 default:
9827 gcc_unreachable ();
9831 /* Now, match all following names as specific targets. */
9834 gfc_symtree* target_st;
9835 gfc_tbp_generic* target;
9837 m = gfc_match_name (name);
9838 if (m == MATCH_ERROR)
9839 goto error;
9840 if (m == MATCH_NO)
9842 gfc_error ("Expected specific binding name at %C");
9843 goto error;
9846 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
9848 /* See if this is a duplicate specification. */
9849 for (target = tb->u.generic; target; target = target->next)
9850 if (target_st == target->specific_st)
9852 gfc_error ("%qs already defined as specific binding for the"
9853 " generic %qs at %C", name, bind_name);
9854 goto error;
9857 target = gfc_get_tbp_generic ();
9858 target->specific_st = target_st;
9859 target->specific = NULL;
9860 target->next = tb->u.generic;
9861 target->is_operator = ((op_type == INTERFACE_USER_OP)
9862 || (op_type == INTERFACE_INTRINSIC_OP));
9863 tb->u.generic = target;
9865 while (gfc_match (" ,") == MATCH_YES);
9867 /* Here should be the end. */
9868 if (gfc_match_eos () != MATCH_YES)
9870 gfc_error ("Junk after GENERIC binding at %C");
9871 goto error;
9874 return MATCH_YES;
9876 error:
9877 return MATCH_ERROR;
9881 /* Match a FINAL declaration inside a derived type. */
9883 match
9884 gfc_match_final_decl (void)
9886 char name[GFC_MAX_SYMBOL_LEN + 1];
9887 gfc_symbol* sym;
9888 match m;
9889 gfc_namespace* module_ns;
9890 bool first, last;
9891 gfc_symbol* block;
9893 if (gfc_current_form == FORM_FREE)
9895 char c = gfc_peek_ascii_char ();
9896 if (!gfc_is_whitespace (c) && c != ':')
9897 return MATCH_NO;
9900 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
9902 if (gfc_current_form == FORM_FIXED)
9903 return MATCH_NO;
9905 gfc_error ("FINAL declaration at %C must be inside a derived type "
9906 "CONTAINS section");
9907 return MATCH_ERROR;
9910 block = gfc_state_stack->previous->sym;
9911 gcc_assert (block);
9913 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
9914 || gfc_state_stack->previous->previous->state != COMP_MODULE)
9916 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9917 " specification part of a MODULE");
9918 return MATCH_ERROR;
9921 module_ns = gfc_current_ns;
9922 gcc_assert (module_ns);
9923 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
9925 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9926 if (gfc_match (" ::") == MATCH_ERROR)
9927 return MATCH_ERROR;
9929 /* Match the sequence of procedure names. */
9930 first = true;
9931 last = false;
9934 gfc_finalizer* f;
9936 if (first && gfc_match_eos () == MATCH_YES)
9938 gfc_error ("Empty FINAL at %C");
9939 return MATCH_ERROR;
9942 m = gfc_match_name (name);
9943 if (m == MATCH_NO)
9945 gfc_error ("Expected module procedure name at %C");
9946 return MATCH_ERROR;
9948 else if (m != MATCH_YES)
9949 return MATCH_ERROR;
9951 if (gfc_match_eos () == MATCH_YES)
9952 last = true;
9953 if (!last && gfc_match_char (',') != MATCH_YES)
9955 gfc_error ("Expected %<,%> at %C");
9956 return MATCH_ERROR;
9959 if (gfc_get_symbol (name, module_ns, &sym))
9961 gfc_error ("Unknown procedure name %qs at %C", name);
9962 return MATCH_ERROR;
9965 /* Mark the symbol as module procedure. */
9966 if (sym->attr.proc != PROC_MODULE
9967 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9968 return MATCH_ERROR;
9970 /* Check if we already have this symbol in the list, this is an error. */
9971 for (f = block->f2k_derived->finalizers; f; f = f->next)
9972 if (f->proc_sym == sym)
9974 gfc_error ("%qs at %C is already defined as FINAL procedure",
9975 name);
9976 return MATCH_ERROR;
9979 /* Add this symbol to the list of finalizers. */
9980 gcc_assert (block->f2k_derived);
9981 sym->refs++;
9982 f = XCNEW (gfc_finalizer);
9983 f->proc_sym = sym;
9984 f->proc_tree = NULL;
9985 f->where = gfc_current_locus;
9986 f->next = block->f2k_derived->finalizers;
9987 block->f2k_derived->finalizers = f;
9989 first = false;
9991 while (!last);
9993 return MATCH_YES;
9997 const ext_attr_t ext_attr_list[] = {
9998 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9999 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
10000 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
10001 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
10002 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
10003 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
10004 { NULL, EXT_ATTR_LAST, NULL }
10007 /* Match a !GCC$ ATTRIBUTES statement of the form:
10008 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
10009 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
10011 TODO: We should support all GCC attributes using the same syntax for
10012 the attribute list, i.e. the list in C
10013 __attributes(( attribute-list ))
10014 matches then
10015 !GCC$ ATTRIBUTES attribute-list ::
10016 Cf. c-parser.c's c_parser_attributes; the data can then directly be
10017 saved into a TREE.
10019 As there is absolutely no risk of confusion, we should never return
10020 MATCH_NO. */
10021 match
10022 gfc_match_gcc_attributes (void)
10024 symbol_attribute attr;
10025 char name[GFC_MAX_SYMBOL_LEN + 1];
10026 unsigned id;
10027 gfc_symbol *sym;
10028 match m;
10030 gfc_clear_attr (&attr);
10031 for(;;)
10033 char ch;
10035 if (gfc_match_name (name) != MATCH_YES)
10036 return MATCH_ERROR;
10038 for (id = 0; id < EXT_ATTR_LAST; id++)
10039 if (strcmp (name, ext_attr_list[id].name) == 0)
10040 break;
10042 if (id == EXT_ATTR_LAST)
10044 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
10045 return MATCH_ERROR;
10048 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
10049 return MATCH_ERROR;
10051 gfc_gobble_whitespace ();
10052 ch = gfc_next_ascii_char ();
10053 if (ch == ':')
10055 /* This is the successful exit condition for the loop. */
10056 if (gfc_next_ascii_char () == ':')
10057 break;
10060 if (ch == ',')
10061 continue;
10063 goto syntax;
10066 if (gfc_match_eos () == MATCH_YES)
10067 goto syntax;
10069 for(;;)
10071 m = gfc_match_name (name);
10072 if (m != MATCH_YES)
10073 return m;
10075 if (find_special (name, &sym, true))
10076 return MATCH_ERROR;
10078 sym->attr.ext_attr |= attr.ext_attr;
10080 if (gfc_match_eos () == MATCH_YES)
10081 break;
10083 if (gfc_match_char (',') != MATCH_YES)
10084 goto syntax;
10087 return MATCH_YES;
10089 syntax:
10090 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
10091 return MATCH_ERROR;