2016-07-30 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / decl.c
blob7ff2f0df29767f8d0044230408480e7e5dd145c8
1 /* Declaration statement matcher
2 Copyright (C) 2002-2016 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 && e->symtree->n.sym->ns->seen_implicit_none == 1))
927 gfc_free_expr (e);
928 goto syntax;
931 if ((e->ref && e->ref->type == REF_ARRAY
932 && e->ref->u.ar.type != AR_ELEMENT)
933 || (!e->ref && e->expr_type == EXPR_ARRAY))
935 gfc_free_expr (e);
936 goto syntax;
939 gfc_free_expr (e);
942 return m;
944 syntax:
945 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
946 return MATCH_ERROR;
950 /* A character length is a '*' followed by a literal integer or a
951 char_len_param_value in parenthesis. */
953 static match
954 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
956 int length;
957 match m;
959 *deferred = false;
960 m = gfc_match_char ('*');
961 if (m != MATCH_YES)
962 return m;
964 m = gfc_match_small_literal_int (&length, NULL);
965 if (m == MATCH_ERROR)
966 return m;
968 if (m == MATCH_YES)
970 if (obsolescent_check
971 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
972 return MATCH_ERROR;
973 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
974 return m;
977 if (gfc_match_char ('(') == MATCH_NO)
978 goto syntax;
980 m = char_len_param_value (expr, deferred);
981 if (m != MATCH_YES && gfc_matching_function)
983 gfc_undo_symbols ();
984 m = MATCH_YES;
987 if (m == MATCH_ERROR)
988 return m;
989 if (m == MATCH_NO)
990 goto syntax;
992 if (gfc_match_char (')') == MATCH_NO)
994 gfc_free_expr (*expr);
995 *expr = NULL;
996 goto syntax;
999 return MATCH_YES;
1001 syntax:
1002 gfc_error ("Syntax error in character length specification at %C");
1003 return MATCH_ERROR;
1007 /* Special subroutine for finding a symbol. Check if the name is found
1008 in the current name space. If not, and we're compiling a function or
1009 subroutine and the parent compilation unit is an interface, then check
1010 to see if the name we've been given is the name of the interface
1011 (located in another namespace). */
1013 static int
1014 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1016 gfc_state_data *s;
1017 gfc_symtree *st;
1018 int i;
1020 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1021 if (i == 0)
1023 *result = st ? st->n.sym : NULL;
1024 goto end;
1027 if (gfc_current_state () != COMP_SUBROUTINE
1028 && gfc_current_state () != COMP_FUNCTION)
1029 goto end;
1031 s = gfc_state_stack->previous;
1032 if (s == NULL)
1033 goto end;
1035 if (s->state != COMP_INTERFACE)
1036 goto end;
1037 if (s->sym == NULL)
1038 goto end; /* Nameless interface. */
1040 if (strcmp (name, s->sym->name) == 0)
1042 *result = s->sym;
1043 return 0;
1046 end:
1047 return i;
1051 /* Special subroutine for getting a symbol node associated with a
1052 procedure name, used in SUBROUTINE and FUNCTION statements. The
1053 symbol is created in the parent using with symtree node in the
1054 child unit pointing to the symbol. If the current namespace has no
1055 parent, then the symbol is just created in the current unit. */
1057 static int
1058 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1060 gfc_symtree *st;
1061 gfc_symbol *sym;
1062 int rc = 0;
1064 /* Module functions have to be left in their own namespace because
1065 they have potentially (almost certainly!) already been referenced.
1066 In this sense, they are rather like external functions. This is
1067 fixed up in resolve.c(resolve_entries), where the symbol name-
1068 space is set to point to the master function, so that the fake
1069 result mechanism can work. */
1070 if (module_fcn_entry)
1072 /* Present if entry is declared to be a module procedure. */
1073 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1075 if (*result == NULL)
1076 rc = gfc_get_symbol (name, NULL, result);
1077 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1078 && (*result)->ts.type == BT_UNKNOWN
1079 && sym->attr.flavor == FL_UNKNOWN)
1080 /* Pick up the typespec for the entry, if declared in the function
1081 body. Note that this symbol is FL_UNKNOWN because it will
1082 only have appeared in a type declaration. The local symtree
1083 is set to point to the module symbol and a unique symtree
1084 to the local version. This latter ensures a correct clearing
1085 of the symbols. */
1087 /* If the ENTRY proceeds its specification, we need to ensure
1088 that this does not raise a "has no IMPLICIT type" error. */
1089 if (sym->ts.type == BT_UNKNOWN)
1090 sym->attr.untyped = 1;
1092 (*result)->ts = sym->ts;
1094 /* Put the symbol in the procedure namespace so that, should
1095 the ENTRY precede its specification, the specification
1096 can be applied. */
1097 (*result)->ns = gfc_current_ns;
1099 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1100 st->n.sym = *result;
1101 st = gfc_get_unique_symtree (gfc_current_ns);
1102 sym->refs++;
1103 st->n.sym = sym;
1106 else
1107 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1109 if (rc)
1110 return rc;
1112 sym = *result;
1113 if (sym->attr.proc == PROC_ST_FUNCTION)
1114 return rc;
1116 if (sym->attr.module_procedure
1117 && sym->attr.if_source == IFSRC_IFBODY)
1119 /* Create a partially populated interface symbol to carry the
1120 characteristics of the procedure and the result. */
1121 sym->ts.interface = gfc_new_symbol (name, sym->ns);
1122 gfc_add_type (sym->ts.interface, &(sym->ts),
1123 &gfc_current_locus);
1124 gfc_copy_attr (&sym->ts.interface->attr, &sym->attr, NULL);
1125 if (sym->attr.dimension)
1126 sym->ts.interface->as = gfc_copy_array_spec (sym->as);
1128 /* Ideally, at this point, a copy would be made of the formal
1129 arguments and their namespace. However, this does not appear
1130 to be necessary, albeit at the expense of not being able to
1131 use gfc_compare_interfaces directly. */
1133 if (sym->result && sym->result != sym)
1135 sym->ts.interface->result = sym->result;
1136 sym->result = NULL;
1138 else if (sym->result)
1140 sym->ts.interface->result = sym->ts.interface;
1143 else if (sym && !sym->gfc_new
1144 && gfc_current_state () != COMP_INTERFACE)
1146 /* Trap another encompassed procedure with the same name. All
1147 these conditions are necessary to avoid picking up an entry
1148 whose name clashes with that of the encompassing procedure;
1149 this is handled using gsymbols to register unique, globally
1150 accessible names. */
1151 if (sym->attr.flavor != 0
1152 && sym->attr.proc != 0
1153 && (sym->attr.subroutine || sym->attr.function)
1154 && sym->attr.if_source != IFSRC_UNKNOWN)
1155 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1156 name, &sym->declared_at);
1158 /* Trap a procedure with a name the same as interface in the
1159 encompassing scope. */
1160 if (sym->attr.generic != 0
1161 && (sym->attr.subroutine || sym->attr.function)
1162 && !sym->attr.mod_proc)
1163 gfc_error_now ("Name %qs at %C is already defined"
1164 " as a generic interface at %L",
1165 name, &sym->declared_at);
1167 /* Trap declarations of attributes in encompassing scope. The
1168 signature for this is that ts.kind is set. Legitimate
1169 references only set ts.type. */
1170 if (sym->ts.kind != 0
1171 && !sym->attr.implicit_type
1172 && sym->attr.proc == 0
1173 && gfc_current_ns->parent != NULL
1174 && sym->attr.access == 0
1175 && !module_fcn_entry)
1176 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1177 "and must not have attributes declared at %L",
1178 name, &sym->declared_at);
1181 if (gfc_current_ns->parent == NULL || *result == NULL)
1182 return rc;
1184 /* Module function entries will already have a symtree in
1185 the current namespace but will need one at module level. */
1186 if (module_fcn_entry)
1188 /* Present if entry is declared to be a module procedure. */
1189 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1190 if (st == NULL)
1191 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1193 else
1194 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1196 st->n.sym = sym;
1197 sym->refs++;
1199 /* See if the procedure should be a module procedure. */
1201 if (((sym->ns->proc_name != NULL
1202 && sym->ns->proc_name->attr.flavor == FL_MODULE
1203 && sym->attr.proc != PROC_MODULE)
1204 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1205 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1206 rc = 2;
1208 return rc;
1212 /* Verify that the given symbol representing a parameter is C
1213 interoperable, by checking to see if it was marked as such after
1214 its declaration. If the given symbol is not interoperable, a
1215 warning is reported, thus removing the need to return the status to
1216 the calling function. The standard does not require the user use
1217 one of the iso_c_binding named constants to declare an
1218 interoperable parameter, but we can't be sure if the param is C
1219 interop or not if the user doesn't. For example, integer(4) may be
1220 legal Fortran, but doesn't have meaning in C. It may interop with
1221 a number of the C types, which causes a problem because the
1222 compiler can't know which one. This code is almost certainly not
1223 portable, and the user will get what they deserve if the C type
1224 across platforms isn't always interoperable with integer(4). If
1225 the user had used something like integer(c_int) or integer(c_long),
1226 the compiler could have automatically handled the varying sizes
1227 across platforms. */
1229 bool
1230 gfc_verify_c_interop_param (gfc_symbol *sym)
1232 int is_c_interop = 0;
1233 bool retval = true;
1235 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1236 Don't repeat the checks here. */
1237 if (sym->attr.implicit_type)
1238 return true;
1240 /* For subroutines or functions that are passed to a BIND(C) procedure,
1241 they're interoperable if they're BIND(C) and their params are all
1242 interoperable. */
1243 if (sym->attr.flavor == FL_PROCEDURE)
1245 if (sym->attr.is_bind_c == 0)
1247 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1248 "attribute to be C interoperable", sym->name,
1249 &(sym->declared_at));
1250 return false;
1252 else
1254 if (sym->attr.is_c_interop == 1)
1255 /* We've already checked this procedure; don't check it again. */
1256 return true;
1257 else
1258 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1259 sym->common_block);
1263 /* See if we've stored a reference to a procedure that owns sym. */
1264 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1266 if (sym->ns->proc_name->attr.is_bind_c == 1)
1268 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1270 if (is_c_interop != 1)
1272 /* Make personalized messages to give better feedback. */
1273 if (sym->ts.type == BT_DERIVED)
1274 gfc_error ("Variable %qs at %L is a dummy argument to the "
1275 "BIND(C) procedure %qs but is not C interoperable "
1276 "because derived type %qs is not C interoperable",
1277 sym->name, &(sym->declared_at),
1278 sym->ns->proc_name->name,
1279 sym->ts.u.derived->name);
1280 else if (sym->ts.type == BT_CLASS)
1281 gfc_error ("Variable %qs at %L is a dummy argument to the "
1282 "BIND(C) procedure %qs but is not C interoperable "
1283 "because it is polymorphic",
1284 sym->name, &(sym->declared_at),
1285 sym->ns->proc_name->name);
1286 else if (warn_c_binding_type)
1287 gfc_warning (OPT_Wc_binding_type,
1288 "Variable %qs at %L is a dummy argument of the "
1289 "BIND(C) procedure %qs but may not be C "
1290 "interoperable",
1291 sym->name, &(sym->declared_at),
1292 sym->ns->proc_name->name);
1295 /* Character strings are only C interoperable if they have a
1296 length of 1. */
1297 if (sym->ts.type == BT_CHARACTER)
1299 gfc_charlen *cl = sym->ts.u.cl;
1300 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1301 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1303 gfc_error ("Character argument %qs at %L "
1304 "must be length 1 because "
1305 "procedure %qs is BIND(C)",
1306 sym->name, &sym->declared_at,
1307 sym->ns->proc_name->name);
1308 retval = false;
1312 /* We have to make sure that any param to a bind(c) routine does
1313 not have the allocatable, pointer, or optional attributes,
1314 according to J3/04-007, section 5.1. */
1315 if (sym->attr.allocatable == 1
1316 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1317 "ALLOCATABLE attribute in procedure %qs "
1318 "with BIND(C)", sym->name,
1319 &(sym->declared_at),
1320 sym->ns->proc_name->name))
1321 retval = false;
1323 if (sym->attr.pointer == 1
1324 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with "
1325 "POINTER attribute in procedure %qs "
1326 "with BIND(C)", sym->name,
1327 &(sym->declared_at),
1328 sym->ns->proc_name->name))
1329 retval = false;
1331 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1333 gfc_error ("Scalar variable %qs at %L with POINTER or "
1334 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1335 " supported", sym->name, &(sym->declared_at),
1336 sym->ns->proc_name->name);
1337 retval = false;
1340 if (sym->attr.optional == 1 && sym->attr.value)
1342 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1343 "and the VALUE attribute because procedure %qs "
1344 "is BIND(C)", sym->name, &(sym->declared_at),
1345 sym->ns->proc_name->name);
1346 retval = false;
1348 else if (sym->attr.optional == 1
1349 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs "
1350 "at %L with OPTIONAL attribute in "
1351 "procedure %qs which is BIND(C)",
1352 sym->name, &(sym->declared_at),
1353 sym->ns->proc_name->name))
1354 retval = false;
1356 /* Make sure that if it has the dimension attribute, that it is
1357 either assumed size or explicit shape. Deferred shape is already
1358 covered by the pointer/allocatable attribute. */
1359 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1360 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array %qs "
1361 "at %L as dummy argument to the BIND(C) "
1362 "procedure %qs at %L", sym->name,
1363 &(sym->declared_at),
1364 sym->ns->proc_name->name,
1365 &(sym->ns->proc_name->declared_at)))
1366 retval = false;
1370 return retval;
1375 /* Function called by variable_decl() that adds a name to the symbol table. */
1377 static bool
1378 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1379 gfc_array_spec **as, locus *var_locus)
1381 symbol_attribute attr;
1382 gfc_symbol *sym;
1383 int upper;
1385 if (gfc_get_symbol (name, NULL, &sym))
1386 return false;
1388 /* Check if the name has already been defined as a type. The
1389 first letter of the symtree will be in upper case then. Of
1390 course, this is only necessary if the upper case letter is
1391 actually different. */
1393 upper = TOUPPER(name[0]);
1394 if (upper != name[0])
1396 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1397 gfc_symtree *st;
1398 int nlen;
1400 nlen = strlen(name);
1401 gcc_assert (nlen <= GFC_MAX_SYMBOL_LEN);
1402 strncpy (u_name, name, nlen + 1);
1403 u_name[0] = upper;
1405 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1407 /* STRUCTURE types can alias symbol names */
1408 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1410 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1411 &st->n.sym->declared_at);
1412 return false;
1416 /* Start updating the symbol table. Add basic type attribute if present. */
1417 if (current_ts.type != BT_UNKNOWN
1418 && (sym->attr.implicit_type == 0
1419 || !gfc_compare_types (&sym->ts, &current_ts))
1420 && !gfc_add_type (sym, &current_ts, var_locus))
1421 return false;
1423 if (sym->ts.type == BT_CHARACTER)
1425 sym->ts.u.cl = cl;
1426 sym->ts.deferred = cl_deferred;
1429 /* Add dimension attribute if present. */
1430 if (!gfc_set_array_spec (sym, *as, var_locus))
1431 return false;
1432 *as = NULL;
1434 /* Add attribute to symbol. The copy is so that we can reset the
1435 dimension attribute. */
1436 attr = current_attr;
1437 attr.dimension = 0;
1438 attr.codimension = 0;
1440 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1441 return false;
1443 /* Finish any work that may need to be done for the binding label,
1444 if it's a bind(c). The bind(c) attr is found before the symbol
1445 is made, and before the symbol name (for data decls), so the
1446 current_ts is holding the binding label, or nothing if the
1447 name= attr wasn't given. Therefore, test here if we're dealing
1448 with a bind(c) and make sure the binding label is set correctly. */
1449 if (sym->attr.is_bind_c == 1)
1451 if (!sym->binding_label)
1453 /* Set the binding label and verify that if a NAME= was specified
1454 then only one identifier was in the entity-decl-list. */
1455 if (!set_binding_label (&sym->binding_label, sym->name,
1456 num_idents_on_line))
1457 return false;
1461 /* See if we know we're in a common block, and if it's a bind(c)
1462 common then we need to make sure we're an interoperable type. */
1463 if (sym->attr.in_common == 1)
1465 /* Test the common block object. */
1466 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1467 && sym->ts.is_c_interop != 1)
1469 gfc_error_now ("Variable %qs in common block %qs at %C "
1470 "must be declared with a C interoperable "
1471 "kind since common block %qs is BIND(C)",
1472 sym->name, sym->common_block->name,
1473 sym->common_block->name);
1474 gfc_clear_error ();
1478 sym->attr.implied_index = 0;
1480 if (sym->ts.type == BT_CLASS)
1481 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1483 return true;
1487 /* Set character constant to the given length. The constant will be padded or
1488 truncated. If we're inside an array constructor without a typespec, we
1489 additionally check that all elements have the same length; check_len -1
1490 means no checking. */
1492 void
1493 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1495 gfc_char_t *s;
1496 int slen;
1498 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1500 if (expr->ts.type != BT_CHARACTER)
1501 return;
1503 slen = expr->value.character.length;
1504 if (len != slen)
1506 s = gfc_get_wide_string (len + 1);
1507 memcpy (s, expr->value.character.string,
1508 MIN (len, slen) * sizeof (gfc_char_t));
1509 if (len > slen)
1510 gfc_wide_memset (&s[slen], ' ', len - slen);
1512 if (warn_character_truncation && slen > len)
1513 gfc_warning_now (OPT_Wcharacter_truncation,
1514 "CHARACTER expression at %L is being truncated "
1515 "(%d/%d)", &expr->where, slen, len);
1517 /* Apply the standard by 'hand' otherwise it gets cleared for
1518 initializers. */
1519 if (check_len != -1 && slen != check_len
1520 && !(gfc_option.allow_std & GFC_STD_GNU))
1521 gfc_error_now ("The CHARACTER elements of the array constructor "
1522 "at %L must have the same length (%d/%d)",
1523 &expr->where, slen, check_len);
1525 s[len] = '\0';
1526 free (expr->value.character.string);
1527 expr->value.character.string = s;
1528 expr->value.character.length = len;
1533 /* Function to create and update the enumerator history
1534 using the information passed as arguments.
1535 Pointer "max_enum" is also updated, to point to
1536 enum history node containing largest initializer.
1538 SYM points to the symbol node of enumerator.
1539 INIT points to its enumerator value. */
1541 static void
1542 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1544 enumerator_history *new_enum_history;
1545 gcc_assert (sym != NULL && init != NULL);
1547 new_enum_history = XCNEW (enumerator_history);
1549 new_enum_history->sym = sym;
1550 new_enum_history->initializer = init;
1551 new_enum_history->next = NULL;
1553 if (enum_history == NULL)
1555 enum_history = new_enum_history;
1556 max_enum = enum_history;
1558 else
1560 new_enum_history->next = enum_history;
1561 enum_history = new_enum_history;
1563 if (mpz_cmp (max_enum->initializer->value.integer,
1564 new_enum_history->initializer->value.integer) < 0)
1565 max_enum = new_enum_history;
1570 /* Function to free enum kind history. */
1572 void
1573 gfc_free_enum_history (void)
1575 enumerator_history *current = enum_history;
1576 enumerator_history *next;
1578 while (current != NULL)
1580 next = current->next;
1581 free (current);
1582 current = next;
1584 max_enum = NULL;
1585 enum_history = NULL;
1589 /* Function called by variable_decl() that adds an initialization
1590 expression to a symbol. */
1592 static bool
1593 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1595 symbol_attribute attr;
1596 gfc_symbol *sym;
1597 gfc_expr *init;
1599 init = *initp;
1600 if (find_special (name, &sym, false))
1601 return false;
1603 attr = sym->attr;
1605 /* If this symbol is confirming an implicit parameter type,
1606 then an initialization expression is not allowed. */
1607 if (attr.flavor == FL_PARAMETER
1608 && sym->value != NULL
1609 && *initp != NULL)
1611 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1612 sym->name);
1613 return false;
1616 if (init == NULL)
1618 /* An initializer is required for PARAMETER declarations. */
1619 if (attr.flavor == FL_PARAMETER)
1621 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1622 return false;
1625 else
1627 /* If a variable appears in a DATA block, it cannot have an
1628 initializer. */
1629 if (sym->attr.data)
1631 gfc_error ("Variable %qs at %C with an initializer already "
1632 "appears in a DATA statement", sym->name);
1633 return false;
1636 /* Check if the assignment can happen. This has to be put off
1637 until later for derived type variables and procedure pointers. */
1638 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1639 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1640 && !sym->attr.proc_pointer
1641 && !gfc_check_assign_symbol (sym, NULL, init))
1642 return false;
1644 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1645 && init->ts.type == BT_CHARACTER)
1647 /* Update symbol character length according initializer. */
1648 if (!gfc_check_assign_symbol (sym, NULL, init))
1649 return false;
1651 if (sym->ts.u.cl->length == NULL)
1653 int clen;
1654 /* If there are multiple CHARACTER variables declared on the
1655 same line, we don't want them to share the same length. */
1656 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1658 if (sym->attr.flavor == FL_PARAMETER)
1660 if (init->expr_type == EXPR_CONSTANT)
1662 clen = init->value.character.length;
1663 sym->ts.u.cl->length
1664 = gfc_get_int_expr (gfc_default_integer_kind,
1665 NULL, clen);
1667 else if (init->expr_type == EXPR_ARRAY)
1669 if (init->ts.u.cl)
1670 clen = mpz_get_si (init->ts.u.cl->length->value.integer);
1671 else if (init->value.constructor)
1673 gfc_constructor *c;
1674 c = gfc_constructor_first (init->value.constructor);
1675 clen = c->expr->value.character.length;
1677 else
1678 gcc_unreachable ();
1679 sym->ts.u.cl->length
1680 = gfc_get_int_expr (gfc_default_integer_kind,
1681 NULL, clen);
1683 else if (init->ts.u.cl && init->ts.u.cl->length)
1684 sym->ts.u.cl->length =
1685 gfc_copy_expr (sym->value->ts.u.cl->length);
1688 /* Update initializer character length according symbol. */
1689 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1691 int len;
1693 if (!gfc_specification_expr (sym->ts.u.cl->length))
1694 return false;
1696 len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1698 if (init->expr_type == EXPR_CONSTANT)
1699 gfc_set_constant_character_len (len, init, -1);
1700 else if (init->expr_type == EXPR_ARRAY)
1702 gfc_constructor *c;
1704 /* Build a new charlen to prevent simplification from
1705 deleting the length before it is resolved. */
1706 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1707 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1709 for (c = gfc_constructor_first (init->value.constructor);
1710 c; c = gfc_constructor_next (c))
1711 gfc_set_constant_character_len (len, c->expr, -1);
1716 /* If sym is implied-shape, set its upper bounds from init. */
1717 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1718 && sym->as->type == AS_IMPLIED_SHAPE)
1720 int dim;
1722 if (init->rank == 0)
1724 gfc_error ("Can't initialize implied-shape array at %L"
1725 " with scalar", &sym->declared_at);
1726 return false;
1729 /* Shape should be present, we get an initialization expression. */
1730 gcc_assert (init->shape);
1732 for (dim = 0; dim < sym->as->rank; ++dim)
1734 int k;
1735 gfc_expr *e, *lower;
1737 lower = sym->as->lower[dim];
1739 /* If the lower bound is an array element from another
1740 parameterized array, then it is marked with EXPR_VARIABLE and
1741 is an initialization expression. Try to reduce it. */
1742 if (lower->expr_type == EXPR_VARIABLE)
1743 gfc_reduce_init_expr (lower);
1745 if (lower->expr_type == EXPR_CONSTANT)
1747 /* All dimensions must be without upper bound. */
1748 gcc_assert (!sym->as->upper[dim]);
1750 k = lower->ts.kind;
1751 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1752 mpz_add (e->value.integer, lower->value.integer,
1753 init->shape[dim]);
1754 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1755 sym->as->upper[dim] = e;
1757 else
1759 gfc_error ("Non-constant lower bound in implied-shape"
1760 " declaration at %L", &lower->where);
1761 return false;
1765 sym->as->type = AS_EXPLICIT;
1768 /* Need to check if the expression we initialized this
1769 to was one of the iso_c_binding named constants. If so,
1770 and we're a parameter (constant), let it be iso_c.
1771 For example:
1772 integer(c_int), parameter :: my_int = c_int
1773 integer(my_int) :: my_int_2
1774 If we mark my_int as iso_c (since we can see it's value
1775 is equal to one of the named constants), then my_int_2
1776 will be considered C interoperable. */
1777 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
1779 sym->ts.is_iso_c |= init->ts.is_iso_c;
1780 sym->ts.is_c_interop |= init->ts.is_c_interop;
1781 /* attr bits needed for module files. */
1782 sym->attr.is_iso_c |= init->ts.is_iso_c;
1783 sym->attr.is_c_interop |= init->ts.is_c_interop;
1784 if (init->ts.is_iso_c)
1785 sym->ts.f90_type = init->ts.f90_type;
1788 /* Add initializer. Make sure we keep the ranks sane. */
1789 if (sym->attr.dimension && init->rank == 0)
1791 mpz_t size;
1792 gfc_expr *array;
1793 int n;
1794 if (sym->attr.flavor == FL_PARAMETER
1795 && init->expr_type == EXPR_CONSTANT
1796 && spec_size (sym->as, &size)
1797 && mpz_cmp_si (size, 0) > 0)
1799 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1800 &init->where);
1801 for (n = 0; n < (int)mpz_get_si (size); n++)
1802 gfc_constructor_append_expr (&array->value.constructor,
1803 n == 0
1804 ? init
1805 : gfc_copy_expr (init),
1806 &init->where);
1808 array->shape = gfc_get_shape (sym->as->rank);
1809 for (n = 0; n < sym->as->rank; n++)
1810 spec_dimen_size (sym->as, n, &array->shape[n]);
1812 init = array;
1813 mpz_clear (size);
1815 init->rank = sym->as->rank;
1818 sym->value = init;
1819 if (sym->attr.save == SAVE_NONE)
1820 sym->attr.save = SAVE_IMPLICIT;
1821 *initp = NULL;
1824 return true;
1828 /* Function called by variable_decl() that adds a name to a structure
1829 being built. */
1831 static bool
1832 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1833 gfc_array_spec **as)
1835 gfc_state_data *s;
1836 gfc_component *c;
1837 bool t = true;
1839 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1840 constructing, it must have the pointer attribute. */
1841 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1842 && current_ts.u.derived == gfc_current_block ()
1843 && current_attr.pointer == 0)
1845 gfc_error ("Component at %C must have the POINTER attribute");
1846 return false;
1849 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1851 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1853 gfc_error ("Array component of structure at %C must have explicit "
1854 "or deferred shape");
1855 return false;
1859 /* If we are in a nested union/map definition, gfc_add_component will not
1860 properly find repeated components because:
1861 (i) gfc_add_component does a flat search, where components of unions
1862 and maps are implicity chained so nested components may conflict.
1863 (ii) Unions and maps are not linked as components of their parent
1864 structures until after they are parsed.
1865 For (i) we use gfc_find_component which searches recursively, and for (ii)
1866 we search each block directly from the parse stack until we find the top
1867 level structure. */
1869 s = gfc_state_stack;
1870 if (s->state == COMP_UNION || s->state == COMP_MAP)
1872 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
1874 c = gfc_find_component (s->sym, name, true, true, NULL);
1875 if (c != NULL)
1877 gfc_error_now ("Component '%s' at %C already declared at %L",
1878 name, &c->loc);
1879 return false;
1881 /* Break after we've searched the entire chain. */
1882 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
1883 break;
1884 s = s->previous;
1888 if (!gfc_add_component (gfc_current_block(), name, &c))
1889 return false;
1891 c->ts = current_ts;
1892 if (c->ts.type == BT_CHARACTER)
1893 c->ts.u.cl = cl;
1894 c->attr = current_attr;
1896 c->initializer = *init;
1897 *init = NULL;
1899 c->as = *as;
1900 if (c->as != NULL)
1902 if (c->as->corank)
1903 c->attr.codimension = 1;
1904 if (c->as->rank)
1905 c->attr.dimension = 1;
1907 *as = NULL;
1909 /* Should this ever get more complicated, combine with similar section
1910 in add_init_expr_to_sym into a separate function. */
1911 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1912 && c->ts.u.cl
1913 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1915 int len;
1917 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1918 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1919 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1921 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1923 if (c->initializer->expr_type == EXPR_CONSTANT)
1924 gfc_set_constant_character_len (len, c->initializer, -1);
1925 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1926 c->initializer->ts.u.cl->length->value.integer))
1928 gfc_constructor *ctor;
1929 ctor = gfc_constructor_first (c->initializer->value.constructor);
1931 if (ctor)
1933 int first_len;
1934 bool has_ts = (c->initializer->ts.u.cl
1935 && c->initializer->ts.u.cl->length_from_typespec);
1937 /* Remember the length of the first element for checking
1938 that all elements *in the constructor* have the same
1939 length. This need not be the length of the LHS! */
1940 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1941 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1942 first_len = ctor->expr->value.character.length;
1944 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1945 if (ctor->expr->expr_type == EXPR_CONSTANT)
1947 gfc_set_constant_character_len (len, ctor->expr,
1948 has_ts ? -1 : first_len);
1949 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1955 /* Check array components. */
1956 if (!c->attr.dimension)
1957 goto scalar;
1959 if (c->attr.pointer)
1961 if (c->as->type != AS_DEFERRED)
1963 gfc_error ("Pointer array component of structure at %C must have a "
1964 "deferred shape");
1965 t = false;
1968 else if (c->attr.allocatable)
1970 if (c->as->type != AS_DEFERRED)
1972 gfc_error ("Allocatable component of structure at %C must have a "
1973 "deferred shape");
1974 t = false;
1977 else
1979 if (c->as->type != AS_EXPLICIT)
1981 gfc_error ("Array component of structure at %C must have an "
1982 "explicit shape");
1983 t = false;
1987 scalar:
1988 if (c->ts.type == BT_CLASS)
1990 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1992 if (t)
1993 t = t2;
1996 return t;
2000 /* Match a 'NULL()', and possibly take care of some side effects. */
2002 match
2003 gfc_match_null (gfc_expr **result)
2005 gfc_symbol *sym;
2006 match m, m2 = MATCH_NO;
2008 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2009 return MATCH_ERROR;
2011 if (m == MATCH_NO)
2013 locus old_loc;
2014 char name[GFC_MAX_SYMBOL_LEN + 1];
2016 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2017 return m2;
2019 old_loc = gfc_current_locus;
2020 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2021 return MATCH_ERROR;
2022 if (m2 != MATCH_YES
2023 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2024 return MATCH_ERROR;
2025 if (m2 == MATCH_NO)
2027 gfc_current_locus = old_loc;
2028 return MATCH_NO;
2032 /* The NULL symbol now has to be/become an intrinsic function. */
2033 if (gfc_get_symbol ("null", NULL, &sym))
2035 gfc_error ("NULL() initialization at %C is ambiguous");
2036 return MATCH_ERROR;
2039 gfc_intrinsic_symbol (sym);
2041 if (sym->attr.proc != PROC_INTRINSIC
2042 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2043 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2044 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2045 return MATCH_ERROR;
2047 *result = gfc_get_null_expr (&gfc_current_locus);
2049 /* Invalid per F2008, C512. */
2050 if (m2 == MATCH_YES)
2052 gfc_error ("NULL() initialization at %C may not have MOLD");
2053 return MATCH_ERROR;
2056 return MATCH_YES;
2060 /* Match the initialization expr for a data pointer or procedure pointer. */
2062 static match
2063 match_pointer_init (gfc_expr **init, int procptr)
2065 match m;
2067 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2069 gfc_error ("Initialization of pointer at %C is not allowed in "
2070 "a PURE procedure");
2071 return MATCH_ERROR;
2073 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2075 /* Match NULL() initialization. */
2076 m = gfc_match_null (init);
2077 if (m != MATCH_NO)
2078 return m;
2080 /* Match non-NULL initialization. */
2081 gfc_matching_ptr_assignment = !procptr;
2082 gfc_matching_procptr_assignment = procptr;
2083 m = gfc_match_rvalue (init);
2084 gfc_matching_ptr_assignment = 0;
2085 gfc_matching_procptr_assignment = 0;
2086 if (m == MATCH_ERROR)
2087 return MATCH_ERROR;
2088 else if (m == MATCH_NO)
2090 gfc_error ("Error in pointer initialization at %C");
2091 return MATCH_ERROR;
2094 if (!procptr && !gfc_resolve_expr (*init))
2095 return MATCH_ERROR;
2097 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2098 "initialization at %C"))
2099 return MATCH_ERROR;
2101 return MATCH_YES;
2105 static bool
2106 check_function_name (char *name)
2108 /* In functions that have a RESULT variable defined, the function name always
2109 refers to function calls. Therefore, the name is not allowed to appear in
2110 specification statements. When checking this, be careful about
2111 'hidden' procedure pointer results ('ppr@'). */
2113 if (gfc_current_state () == COMP_FUNCTION)
2115 gfc_symbol *block = gfc_current_block ();
2116 if (block && block->result && block->result != block
2117 && strcmp (block->result->name, "ppr@") != 0
2118 && strcmp (block->name, name) == 0)
2120 gfc_error ("Function name %qs not allowed at %C", name);
2121 return false;
2125 return true;
2129 /* Match a variable name with an optional initializer. When this
2130 subroutine is called, a variable is expected to be parsed next.
2131 Depending on what is happening at the moment, updates either the
2132 symbol table or the current interface. */
2134 static match
2135 variable_decl (int elem)
2137 char name[GFC_MAX_SYMBOL_LEN + 1];
2138 gfc_expr *initializer, *char_len;
2139 gfc_array_spec *as;
2140 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2141 gfc_charlen *cl;
2142 bool cl_deferred;
2143 locus var_locus;
2144 match m;
2145 bool t;
2146 gfc_symbol *sym;
2148 initializer = NULL;
2149 as = NULL;
2150 cp_as = NULL;
2152 /* When we get here, we've just matched a list of attributes and
2153 maybe a type and a double colon. The next thing we expect to see
2154 is the name of the symbol. */
2155 m = gfc_match_name (name);
2156 if (m != MATCH_YES)
2157 goto cleanup;
2159 var_locus = gfc_current_locus;
2161 /* Now we could see the optional array spec. or character length. */
2162 m = gfc_match_array_spec (&as, true, true);
2163 if (m == MATCH_ERROR)
2164 goto cleanup;
2166 if (m == MATCH_NO)
2167 as = gfc_copy_array_spec (current_as);
2168 else if (current_as
2169 && !merge_array_spec (current_as, as, true))
2171 m = MATCH_ERROR;
2172 goto cleanup;
2175 if (flag_cray_pointer)
2176 cp_as = gfc_copy_array_spec (as);
2178 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2179 determine (and check) whether it can be implied-shape. If it
2180 was parsed as assumed-size, change it because PARAMETERs can not
2181 be assumed-size. */
2182 if (as)
2184 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2186 m = MATCH_ERROR;
2187 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2188 name, &var_locus);
2189 goto cleanup;
2192 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2193 && current_attr.flavor == FL_PARAMETER)
2194 as->type = AS_IMPLIED_SHAPE;
2196 if (as->type == AS_IMPLIED_SHAPE
2197 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2198 &var_locus))
2200 m = MATCH_ERROR;
2201 goto cleanup;
2205 char_len = NULL;
2206 cl = NULL;
2207 cl_deferred = false;
2209 if (current_ts.type == BT_CHARACTER)
2211 switch (match_char_length (&char_len, &cl_deferred, false))
2213 case MATCH_YES:
2214 cl = gfc_new_charlen (gfc_current_ns, NULL);
2216 cl->length = char_len;
2217 break;
2219 /* Non-constant lengths need to be copied after the first
2220 element. Also copy assumed lengths. */
2221 case MATCH_NO:
2222 if (elem > 1
2223 && (current_ts.u.cl->length == NULL
2224 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2226 cl = gfc_new_charlen (gfc_current_ns, NULL);
2227 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2229 else
2230 cl = current_ts.u.cl;
2232 cl_deferred = current_ts.deferred;
2234 break;
2236 case MATCH_ERROR:
2237 goto cleanup;
2241 /* The dummy arguments and result of the abreviated form of MODULE
2242 PROCEDUREs, used in SUBMODULES should not be redefined. */
2243 if (gfc_current_ns->proc_name
2244 && gfc_current_ns->proc_name->abr_modproc_decl)
2246 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2247 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2249 m = MATCH_ERROR;
2250 gfc_error ("%qs at %C is a redefinition of the declaration "
2251 "in the corresponding interface for MODULE "
2252 "PROCEDURE %qs", sym->name,
2253 gfc_current_ns->proc_name->name);
2254 goto cleanup;
2258 /* If this symbol has already shown up in a Cray Pointer declaration,
2259 and this is not a component declaration,
2260 then we want to set the type & bail out. */
2261 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2263 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2264 if (sym != NULL && sym->attr.cray_pointee)
2266 sym->ts.type = current_ts.type;
2267 sym->ts.kind = current_ts.kind;
2268 sym->ts.u.cl = cl;
2269 sym->ts.u.derived = current_ts.u.derived;
2270 sym->ts.is_c_interop = current_ts.is_c_interop;
2271 sym->ts.is_iso_c = current_ts.is_iso_c;
2272 m = MATCH_YES;
2274 /* Check to see if we have an array specification. */
2275 if (cp_as != NULL)
2277 if (sym->as != NULL)
2279 gfc_error ("Duplicate array spec for Cray pointee at %C");
2280 gfc_free_array_spec (cp_as);
2281 m = MATCH_ERROR;
2282 goto cleanup;
2284 else
2286 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2287 gfc_internal_error ("Couldn't set pointee array spec.");
2289 /* Fix the array spec. */
2290 m = gfc_mod_pointee_as (sym->as);
2291 if (m == MATCH_ERROR)
2292 goto cleanup;
2295 goto cleanup;
2297 else
2299 gfc_free_array_spec (cp_as);
2303 /* Procedure pointer as function result. */
2304 if (gfc_current_state () == COMP_FUNCTION
2305 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2306 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2307 strcpy (name, "ppr@");
2309 if (gfc_current_state () == COMP_FUNCTION
2310 && strcmp (name, gfc_current_block ()->name) == 0
2311 && gfc_current_block ()->result
2312 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2313 strcpy (name, "ppr@");
2315 /* OK, we've successfully matched the declaration. Now put the
2316 symbol in the current namespace, because it might be used in the
2317 optional initialization expression for this symbol, e.g. this is
2318 perfectly legal:
2320 integer, parameter :: i = huge(i)
2322 This is only true for parameters or variables of a basic type.
2323 For components of derived types, it is not true, so we don't
2324 create a symbol for those yet. If we fail to create the symbol,
2325 bail out. */
2326 if (!gfc_comp_struct (gfc_current_state ())
2327 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2329 m = MATCH_ERROR;
2330 goto cleanup;
2333 if (!check_function_name (name))
2335 m = MATCH_ERROR;
2336 goto cleanup;
2339 /* We allow old-style initializations of the form
2340 integer i /2/, j(4) /3*3, 1/
2341 (if no colon has been seen). These are different from data
2342 statements in that initializers are only allowed to apply to the
2343 variable immediately preceding, i.e.
2344 integer i, j /1, 2/
2345 is not allowed. Therefore we have to do some work manually, that
2346 could otherwise be left to the matchers for DATA statements. */
2348 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2350 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2351 "initialization at %C"))
2352 return MATCH_ERROR;
2354 /* Allow old style initializations for components of STRUCTUREs and MAPs
2355 but not components of derived types. */
2356 else if (gfc_current_state () == COMP_DERIVED)
2358 gfc_error ("Invalid old style initialization for derived type "
2359 "component at %C");
2360 m = MATCH_ERROR;
2361 goto cleanup;
2364 /* For structure components, read the initializer as a special
2365 expression and let the rest of this function apply the initializer
2366 as usual. */
2367 else if (gfc_comp_struct (gfc_current_state ()))
2369 m = match_clist_expr (&initializer, &current_ts, as);
2370 if (m == MATCH_NO)
2371 gfc_error ("Syntax error in old style initialization of %s at %C",
2372 name);
2373 if (m != MATCH_YES)
2374 goto cleanup;
2377 /* Otherwise we treat the old style initialization just like a
2378 DATA declaration for the current variable. */
2379 else
2380 return match_old_style_init (name);
2383 /* The double colon must be present in order to have initializers.
2384 Otherwise the statement is ambiguous with an assignment statement. */
2385 if (colon_seen)
2387 if (gfc_match (" =>") == MATCH_YES)
2389 if (!current_attr.pointer)
2391 gfc_error ("Initialization at %C isn't for a pointer variable");
2392 m = MATCH_ERROR;
2393 goto cleanup;
2396 m = match_pointer_init (&initializer, 0);
2397 if (m != MATCH_YES)
2398 goto cleanup;
2400 else if (gfc_match_char ('=') == MATCH_YES)
2402 if (current_attr.pointer)
2404 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2405 "not %<=%>");
2406 m = MATCH_ERROR;
2407 goto cleanup;
2410 m = gfc_match_init_expr (&initializer);
2411 if (m == MATCH_NO)
2413 gfc_error ("Expected an initialization expression at %C");
2414 m = MATCH_ERROR;
2417 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2418 && !gfc_comp_struct (gfc_state_stack->state))
2420 gfc_error ("Initialization of variable at %C is not allowed in "
2421 "a PURE procedure");
2422 m = MATCH_ERROR;
2425 if (current_attr.flavor != FL_PARAMETER
2426 && !gfc_comp_struct (gfc_state_stack->state))
2427 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2429 if (m != MATCH_YES)
2430 goto cleanup;
2434 if (initializer != NULL && current_attr.allocatable
2435 && gfc_comp_struct (gfc_current_state ()))
2437 gfc_error ("Initialization of allocatable component at %C is not "
2438 "allowed");
2439 m = MATCH_ERROR;
2440 goto cleanup;
2443 /* Add the initializer. Note that it is fine if initializer is
2444 NULL here, because we sometimes also need to check if a
2445 declaration *must* have an initialization expression. */
2446 if (!gfc_comp_struct (gfc_current_state ()))
2447 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2448 else
2450 if (current_ts.type == BT_DERIVED
2451 && !current_attr.pointer && !initializer)
2452 initializer = gfc_default_initializer (&current_ts);
2453 t = build_struct (name, cl, &initializer, &as);
2455 /* If we match a nested structure definition we expect to see the
2456 * body even if the variable declarations blow up, so we need to keep
2457 * the structure declaration around. */
2458 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2459 gfc_commit_symbol (gfc_new_block);
2462 m = (t) ? MATCH_YES : MATCH_ERROR;
2464 cleanup:
2465 /* Free stuff up and return. */
2466 gfc_free_expr (initializer);
2467 gfc_free_array_spec (as);
2469 return m;
2473 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2474 This assumes that the byte size is equal to the kind number for
2475 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2477 match
2478 gfc_match_old_kind_spec (gfc_typespec *ts)
2480 match m;
2481 int original_kind;
2483 if (gfc_match_char ('*') != MATCH_YES)
2484 return MATCH_NO;
2486 m = gfc_match_small_literal_int (&ts->kind, NULL);
2487 if (m != MATCH_YES)
2488 return MATCH_ERROR;
2490 original_kind = ts->kind;
2492 /* Massage the kind numbers for complex types. */
2493 if (ts->type == BT_COMPLEX)
2495 if (ts->kind % 2)
2497 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2498 gfc_basic_typename (ts->type), original_kind);
2499 return MATCH_ERROR;
2501 ts->kind /= 2;
2505 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2506 ts->kind = 8;
2508 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2510 if (ts->kind == 4)
2512 if (flag_real4_kind == 8)
2513 ts->kind = 8;
2514 if (flag_real4_kind == 10)
2515 ts->kind = 10;
2516 if (flag_real4_kind == 16)
2517 ts->kind = 16;
2520 if (ts->kind == 8)
2522 if (flag_real8_kind == 4)
2523 ts->kind = 4;
2524 if (flag_real8_kind == 10)
2525 ts->kind = 10;
2526 if (flag_real8_kind == 16)
2527 ts->kind = 16;
2531 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2533 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2534 gfc_basic_typename (ts->type), original_kind);
2535 return MATCH_ERROR;
2538 if (!gfc_notify_std (GFC_STD_GNU,
2539 "Nonstandard type declaration %s*%d at %C",
2540 gfc_basic_typename(ts->type), original_kind))
2541 return MATCH_ERROR;
2543 return MATCH_YES;
2547 /* Match a kind specification. Since kinds are generally optional, we
2548 usually return MATCH_NO if something goes wrong. If a "kind="
2549 string is found, then we know we have an error. */
2551 match
2552 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2554 locus where, loc;
2555 gfc_expr *e;
2556 match m, n;
2557 char c;
2558 const char *msg;
2560 m = MATCH_NO;
2561 n = MATCH_YES;
2562 e = NULL;
2564 where = loc = gfc_current_locus;
2566 if (kind_expr_only)
2567 goto kind_expr;
2569 if (gfc_match_char ('(') == MATCH_NO)
2570 return MATCH_NO;
2572 /* Also gobbles optional text. */
2573 if (gfc_match (" kind = ") == MATCH_YES)
2574 m = MATCH_ERROR;
2576 loc = gfc_current_locus;
2578 kind_expr:
2579 n = gfc_match_init_expr (&e);
2581 if (n != MATCH_YES)
2583 if (gfc_matching_function)
2585 /* The function kind expression might include use associated or
2586 imported parameters and try again after the specification
2587 expressions..... */
2588 if (gfc_match_char (')') != MATCH_YES)
2590 gfc_error ("Missing right parenthesis at %C");
2591 m = MATCH_ERROR;
2592 goto no_match;
2595 gfc_free_expr (e);
2596 gfc_undo_symbols ();
2597 return MATCH_YES;
2599 else
2601 /* ....or else, the match is real. */
2602 if (n == MATCH_NO)
2603 gfc_error ("Expected initialization expression at %C");
2604 if (n != MATCH_YES)
2605 return MATCH_ERROR;
2609 if (e->rank != 0)
2611 gfc_error ("Expected scalar initialization expression at %C");
2612 m = MATCH_ERROR;
2613 goto no_match;
2616 msg = gfc_extract_int (e, &ts->kind);
2618 if (msg != NULL)
2620 gfc_error (msg);
2621 m = MATCH_ERROR;
2622 goto no_match;
2625 /* Before throwing away the expression, let's see if we had a
2626 C interoperable kind (and store the fact). */
2627 if (e->ts.is_c_interop == 1)
2629 /* Mark this as C interoperable if being declared with one
2630 of the named constants from iso_c_binding. */
2631 ts->is_c_interop = e->ts.is_iso_c;
2632 ts->f90_type = e->ts.f90_type;
2635 gfc_free_expr (e);
2636 e = NULL;
2638 /* Ignore errors to this point, if we've gotten here. This means
2639 we ignore the m=MATCH_ERROR from above. */
2640 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2642 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2643 gfc_basic_typename (ts->type));
2644 gfc_current_locus = where;
2645 return MATCH_ERROR;
2648 /* Warn if, e.g., c_int is used for a REAL variable, but not
2649 if, e.g., c_double is used for COMPLEX as the standard
2650 explicitly says that the kind type parameter for complex and real
2651 variable is the same, i.e. c_float == c_float_complex. */
2652 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2653 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2654 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2655 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2656 "is %s", gfc_basic_typename (ts->f90_type), &where,
2657 gfc_basic_typename (ts->type));
2659 gfc_gobble_whitespace ();
2660 if ((c = gfc_next_ascii_char ()) != ')'
2661 && (ts->type != BT_CHARACTER || c != ','))
2663 if (ts->type == BT_CHARACTER)
2664 gfc_error ("Missing right parenthesis or comma at %C");
2665 else
2666 gfc_error ("Missing right parenthesis at %C");
2667 m = MATCH_ERROR;
2669 else
2670 /* All tests passed. */
2671 m = MATCH_YES;
2673 if(m == MATCH_ERROR)
2674 gfc_current_locus = where;
2676 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2677 ts->kind = 8;
2679 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2681 if (ts->kind == 4)
2683 if (flag_real4_kind == 8)
2684 ts->kind = 8;
2685 if (flag_real4_kind == 10)
2686 ts->kind = 10;
2687 if (flag_real4_kind == 16)
2688 ts->kind = 16;
2691 if (ts->kind == 8)
2693 if (flag_real8_kind == 4)
2694 ts->kind = 4;
2695 if (flag_real8_kind == 10)
2696 ts->kind = 10;
2697 if (flag_real8_kind == 16)
2698 ts->kind = 16;
2702 /* Return what we know from the test(s). */
2703 return m;
2705 no_match:
2706 gfc_free_expr (e);
2707 gfc_current_locus = where;
2708 return m;
2712 static match
2713 match_char_kind (int * kind, int * is_iso_c)
2715 locus where;
2716 gfc_expr *e;
2717 match m, n;
2718 const char *msg;
2720 m = MATCH_NO;
2721 e = NULL;
2722 where = gfc_current_locus;
2724 n = gfc_match_init_expr (&e);
2726 if (n != MATCH_YES && gfc_matching_function)
2728 /* The expression might include use-associated or imported
2729 parameters and try again after the specification
2730 expressions. */
2731 gfc_free_expr (e);
2732 gfc_undo_symbols ();
2733 return MATCH_YES;
2736 if (n == MATCH_NO)
2737 gfc_error ("Expected initialization expression at %C");
2738 if (n != MATCH_YES)
2739 return MATCH_ERROR;
2741 if (e->rank != 0)
2743 gfc_error ("Expected scalar initialization expression at %C");
2744 m = MATCH_ERROR;
2745 goto no_match;
2748 msg = gfc_extract_int (e, kind);
2749 *is_iso_c = e->ts.is_iso_c;
2750 if (msg != NULL)
2752 gfc_error (msg);
2753 m = MATCH_ERROR;
2754 goto no_match;
2757 gfc_free_expr (e);
2759 /* Ignore errors to this point, if we've gotten here. This means
2760 we ignore the m=MATCH_ERROR from above. */
2761 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2763 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2764 m = MATCH_ERROR;
2766 else
2767 /* All tests passed. */
2768 m = MATCH_YES;
2770 if (m == MATCH_ERROR)
2771 gfc_current_locus = where;
2773 /* Return what we know from the test(s). */
2774 return m;
2776 no_match:
2777 gfc_free_expr (e);
2778 gfc_current_locus = where;
2779 return m;
2783 /* Match the various kind/length specifications in a CHARACTER
2784 declaration. We don't return MATCH_NO. */
2786 match
2787 gfc_match_char_spec (gfc_typespec *ts)
2789 int kind, seen_length, is_iso_c;
2790 gfc_charlen *cl;
2791 gfc_expr *len;
2792 match m;
2793 bool deferred;
2795 len = NULL;
2796 seen_length = 0;
2797 kind = 0;
2798 is_iso_c = 0;
2799 deferred = false;
2801 /* Try the old-style specification first. */
2802 old_char_selector = 0;
2804 m = match_char_length (&len, &deferred, true);
2805 if (m != MATCH_NO)
2807 if (m == MATCH_YES)
2808 old_char_selector = 1;
2809 seen_length = 1;
2810 goto done;
2813 m = gfc_match_char ('(');
2814 if (m != MATCH_YES)
2816 m = MATCH_YES; /* Character without length is a single char. */
2817 goto done;
2820 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2821 if (gfc_match (" kind =") == MATCH_YES)
2823 m = match_char_kind (&kind, &is_iso_c);
2825 if (m == MATCH_ERROR)
2826 goto done;
2827 if (m == MATCH_NO)
2828 goto syntax;
2830 if (gfc_match (" , len =") == MATCH_NO)
2831 goto rparen;
2833 m = char_len_param_value (&len, &deferred);
2834 if (m == MATCH_NO)
2835 goto syntax;
2836 if (m == MATCH_ERROR)
2837 goto done;
2838 seen_length = 1;
2840 goto rparen;
2843 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2844 if (gfc_match (" len =") == MATCH_YES)
2846 m = char_len_param_value (&len, &deferred);
2847 if (m == MATCH_NO)
2848 goto syntax;
2849 if (m == MATCH_ERROR)
2850 goto done;
2851 seen_length = 1;
2853 if (gfc_match_char (')') == MATCH_YES)
2854 goto done;
2856 if (gfc_match (" , kind =") != MATCH_YES)
2857 goto syntax;
2859 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2860 goto done;
2862 goto rparen;
2865 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2866 m = char_len_param_value (&len, &deferred);
2867 if (m == MATCH_NO)
2868 goto syntax;
2869 if (m == MATCH_ERROR)
2870 goto done;
2871 seen_length = 1;
2873 m = gfc_match_char (')');
2874 if (m == MATCH_YES)
2875 goto done;
2877 if (gfc_match_char (',') != MATCH_YES)
2878 goto syntax;
2880 gfc_match (" kind ="); /* Gobble optional text. */
2882 m = match_char_kind (&kind, &is_iso_c);
2883 if (m == MATCH_ERROR)
2884 goto done;
2885 if (m == MATCH_NO)
2886 goto syntax;
2888 rparen:
2889 /* Require a right-paren at this point. */
2890 m = gfc_match_char (')');
2891 if (m == MATCH_YES)
2892 goto done;
2894 syntax:
2895 gfc_error ("Syntax error in CHARACTER declaration at %C");
2896 m = MATCH_ERROR;
2897 gfc_free_expr (len);
2898 return m;
2900 done:
2901 /* Deal with character functions after USE and IMPORT statements. */
2902 if (gfc_matching_function)
2904 gfc_free_expr (len);
2905 gfc_undo_symbols ();
2906 return MATCH_YES;
2909 if (m != MATCH_YES)
2911 gfc_free_expr (len);
2912 return m;
2915 /* Do some final massaging of the length values. */
2916 cl = gfc_new_charlen (gfc_current_ns, NULL);
2918 if (seen_length == 0)
2919 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2920 else
2921 cl->length = len;
2923 ts->u.cl = cl;
2924 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2925 ts->deferred = deferred;
2927 /* We have to know if it was a C interoperable kind so we can
2928 do accurate type checking of bind(c) procs, etc. */
2929 if (kind != 0)
2930 /* Mark this as C interoperable if being declared with one
2931 of the named constants from iso_c_binding. */
2932 ts->is_c_interop = is_iso_c;
2933 else if (len != NULL)
2934 /* Here, we might have parsed something such as: character(c_char)
2935 In this case, the parsing code above grabs the c_char when
2936 looking for the length (line 1690, roughly). it's the last
2937 testcase for parsing the kind params of a character variable.
2938 However, it's not actually the length. this seems like it
2939 could be an error.
2940 To see if the user used a C interop kind, test the expr
2941 of the so called length, and see if it's C interoperable. */
2942 ts->is_c_interop = len->ts.is_iso_c;
2944 return MATCH_YES;
2948 /* Matches a RECORD declaration. */
2950 static match
2951 match_record_decl (const char *name)
2953 locus old_loc;
2954 old_loc = gfc_current_locus;
2956 if (gfc_match (" record") == MATCH_YES)
2958 if (!gfc_option.flag_dec_structure)
2960 gfc_current_locus = old_loc;
2961 gfc_error ("RECORD at %C is an extension, enable it with "
2962 "-fdec-structure");
2963 return MATCH_ERROR;
2965 if (gfc_match (" /%n/", name) != MATCH_YES)
2967 gfc_error ("Structure name expected after RECORD at %C");
2968 gfc_current_locus = old_loc;
2969 return MATCH_ERROR;
2971 return MATCH_YES;
2974 gfc_current_locus = old_loc;
2975 return MATCH_NO;
2978 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2979 structure to the matched specification. This is necessary for FUNCTION and
2980 IMPLICIT statements.
2982 If implicit_flag is nonzero, then we don't check for the optional
2983 kind specification. Not doing so is needed for matching an IMPLICIT
2984 statement correctly. */
2986 match
2987 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2989 char name[GFC_MAX_SYMBOL_LEN + 1];
2990 gfc_symbol *sym, *dt_sym;
2991 match m;
2992 char c;
2993 bool seen_deferred_kind, matched_type;
2994 const char *dt_name;
2996 /* A belt and braces check that the typespec is correctly being treated
2997 as a deferred characteristic association. */
2998 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2999 && (gfc_current_block ()->result->ts.kind == -1)
3000 && (ts->kind == -1);
3001 gfc_clear_ts (ts);
3002 if (seen_deferred_kind)
3003 ts->kind = -1;
3005 /* Clear the current binding label, in case one is given. */
3006 curr_binding_label = NULL;
3008 if (gfc_match (" byte") == MATCH_YES)
3010 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
3011 return MATCH_ERROR;
3013 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
3015 gfc_error ("BYTE type used at %C "
3016 "is not available on the target machine");
3017 return MATCH_ERROR;
3020 ts->type = BT_INTEGER;
3021 ts->kind = 1;
3022 return MATCH_YES;
3026 m = gfc_match (" type (");
3027 matched_type = (m == MATCH_YES);
3028 if (matched_type)
3030 gfc_gobble_whitespace ();
3031 if (gfc_peek_ascii_char () == '*')
3033 if ((m = gfc_match ("*)")) != MATCH_YES)
3034 return m;
3035 if (gfc_comp_struct (gfc_current_state ()))
3037 gfc_error ("Assumed type at %C is not allowed for components");
3038 return MATCH_ERROR;
3040 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
3041 "at %C"))
3042 return MATCH_ERROR;
3043 ts->type = BT_ASSUMED;
3044 return MATCH_YES;
3047 m = gfc_match ("%n", name);
3048 matched_type = (m == MATCH_YES);
3051 if ((matched_type && strcmp ("integer", name) == 0)
3052 || (!matched_type && gfc_match (" integer") == MATCH_YES))
3054 ts->type = BT_INTEGER;
3055 ts->kind = gfc_default_integer_kind;
3056 goto get_kind;
3059 if ((matched_type && strcmp ("character", name) == 0)
3060 || (!matched_type && gfc_match (" character") == MATCH_YES))
3062 if (matched_type
3063 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3064 "intrinsic-type-spec at %C"))
3065 return MATCH_ERROR;
3067 ts->type = BT_CHARACTER;
3068 if (implicit_flag == 0)
3069 m = gfc_match_char_spec (ts);
3070 else
3071 m = MATCH_YES;
3073 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
3074 m = MATCH_ERROR;
3076 return m;
3079 if ((matched_type && strcmp ("real", name) == 0)
3080 || (!matched_type && gfc_match (" real") == MATCH_YES))
3082 ts->type = BT_REAL;
3083 ts->kind = gfc_default_real_kind;
3084 goto get_kind;
3087 if ((matched_type
3088 && (strcmp ("doubleprecision", name) == 0
3089 || (strcmp ("double", name) == 0
3090 && gfc_match (" precision") == MATCH_YES)))
3091 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
3093 if (matched_type
3094 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3095 "intrinsic-type-spec at %C"))
3096 return MATCH_ERROR;
3097 if (matched_type && gfc_match_char (')') != MATCH_YES)
3098 return MATCH_ERROR;
3100 ts->type = BT_REAL;
3101 ts->kind = gfc_default_double_kind;
3102 return MATCH_YES;
3105 if ((matched_type && strcmp ("complex", name) == 0)
3106 || (!matched_type && gfc_match (" complex") == MATCH_YES))
3108 ts->type = BT_COMPLEX;
3109 ts->kind = gfc_default_complex_kind;
3110 goto get_kind;
3113 if ((matched_type
3114 && (strcmp ("doublecomplex", name) == 0
3115 || (strcmp ("double", name) == 0
3116 && gfc_match (" complex") == MATCH_YES)))
3117 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
3119 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
3120 return MATCH_ERROR;
3122 if (matched_type
3123 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3124 "intrinsic-type-spec at %C"))
3125 return MATCH_ERROR;
3127 if (matched_type && gfc_match_char (')') != MATCH_YES)
3128 return MATCH_ERROR;
3130 ts->type = BT_COMPLEX;
3131 ts->kind = gfc_default_double_kind;
3132 return MATCH_YES;
3135 if ((matched_type && strcmp ("logical", name) == 0)
3136 || (!matched_type && gfc_match (" logical") == MATCH_YES))
3138 ts->type = BT_LOGICAL;
3139 ts->kind = gfc_default_logical_kind;
3140 goto get_kind;
3143 if (matched_type)
3144 m = gfc_match_char (')');
3146 if (m != MATCH_YES)
3147 m = match_record_decl (name);
3149 if (matched_type || m == MATCH_YES)
3151 ts->type = BT_DERIVED;
3152 /* We accept record/s/ or type(s) where s is a structure, but we
3153 * don't need all the extra derived-type stuff for structures. */
3154 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
3156 gfc_error ("Type name '%s' at %C is ambiguous", name);
3157 return MATCH_ERROR;
3159 if (sym && sym->attr.flavor == FL_STRUCT)
3161 ts->u.derived = sym;
3162 return MATCH_YES;
3164 /* Actually a derived type. */
3167 else
3169 /* Match nested STRUCTURE declarations; only valid within another
3170 structure declaration. */
3171 m = gfc_match (" structure");
3172 if (m == MATCH_ERROR)
3173 return MATCH_ERROR;
3174 else if (m == MATCH_YES)
3176 if ( gfc_current_state () != COMP_STRUCTURE
3177 && gfc_current_state () != COMP_MAP)
3178 return MATCH_ERROR;
3180 m = gfc_match_structure_decl ();
3181 if (m == MATCH_YES)
3183 /* gfc_new_block is updated by match_structure_decl. */
3184 ts->type = BT_DERIVED;
3185 ts->u.derived = gfc_new_block;
3186 return MATCH_YES;
3188 return MATCH_ERROR;
3191 /* Match CLASS declarations. */
3192 m = gfc_match (" class ( * )");
3193 if (m == MATCH_ERROR)
3194 return MATCH_ERROR;
3195 else if (m == MATCH_YES)
3197 gfc_symbol *upe;
3198 gfc_symtree *st;
3199 ts->type = BT_CLASS;
3200 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
3201 if (upe == NULL)
3203 upe = gfc_new_symbol ("STAR", gfc_current_ns);
3204 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
3205 st->n.sym = upe;
3206 gfc_set_sym_referenced (upe);
3207 upe->refs++;
3208 upe->ts.type = BT_VOID;
3209 upe->attr.unlimited_polymorphic = 1;
3210 /* This is essential to force the construction of
3211 unlimited polymorphic component class containers. */
3212 upe->attr.zero_comp = 1;
3213 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
3214 &gfc_current_locus))
3215 return MATCH_ERROR;
3217 else
3219 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
3220 if (st == NULL)
3221 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
3222 st->n.sym = upe;
3223 upe->refs++;
3225 ts->u.derived = upe;
3226 return m;
3229 m = gfc_match (" class ( %n )", name);
3230 if (m != MATCH_YES)
3231 return m;
3232 ts->type = BT_CLASS;
3234 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
3235 return MATCH_ERROR;
3238 /* Defer association of the derived type until the end of the
3239 specification block. However, if the derived type can be
3240 found, add it to the typespec. */
3241 if (gfc_matching_function)
3243 ts->u.derived = NULL;
3244 if (gfc_current_state () != COMP_INTERFACE
3245 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
3247 sym = gfc_find_dt_in_generic (sym);
3248 ts->u.derived = sym;
3250 return MATCH_YES;
3253 /* Search for the name but allow the components to be defined later. If
3254 type = -1, this typespec has been seen in a function declaration but
3255 the type could not be accessed at that point. The actual derived type is
3256 stored in a symtree with the first letter of the name capitalized; the
3257 symtree with the all lower-case name contains the associated
3258 generic function. */
3259 dt_name = gfc_dt_upper_string (name);
3260 sym = NULL;
3261 dt_sym = NULL;
3262 if (ts->kind != -1)
3264 gfc_get_ha_symbol (name, &sym);
3265 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
3267 gfc_error ("Type name %qs at %C is ambiguous", name);
3268 return MATCH_ERROR;
3270 if (sym->generic && !dt_sym)
3271 dt_sym = gfc_find_dt_in_generic (sym);
3273 else if (ts->kind == -1)
3275 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
3276 || gfc_current_ns->has_import_set;
3277 gfc_find_symbol (name, NULL, iface, &sym);
3278 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
3280 gfc_error ("Type name %qs at %C is ambiguous", name);
3281 return MATCH_ERROR;
3283 if (sym && sym->generic && !dt_sym)
3284 dt_sym = gfc_find_dt_in_generic (sym);
3286 ts->kind = 0;
3287 if (sym == NULL)
3288 return MATCH_NO;
3291 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
3292 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
3293 || sym->attr.subroutine)
3295 gfc_error ("Type name %qs at %C conflicts with previously declared "
3296 "entity at %L, which has the same name", name,
3297 &sym->declared_at);
3298 return MATCH_ERROR;
3301 gfc_save_symbol_data (sym);
3302 gfc_set_sym_referenced (sym);
3303 if (!sym->attr.generic
3304 && !gfc_add_generic (&sym->attr, sym->name, NULL))
3305 return MATCH_ERROR;
3307 if (!sym->attr.function
3308 && !gfc_add_function (&sym->attr, sym->name, NULL))
3309 return MATCH_ERROR;
3311 if (!dt_sym)
3313 gfc_interface *intr, *head;
3315 /* Use upper case to save the actual derived-type symbol. */
3316 gfc_get_symbol (dt_name, NULL, &dt_sym);
3317 dt_sym->name = gfc_get_string (sym->name);
3318 head = sym->generic;
3319 intr = gfc_get_interface ();
3320 intr->sym = dt_sym;
3321 intr->where = gfc_current_locus;
3322 intr->next = head;
3323 sym->generic = intr;
3324 sym->attr.if_source = IFSRC_DECL;
3326 else
3327 gfc_save_symbol_data (dt_sym);
3329 gfc_set_sym_referenced (dt_sym);
3331 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
3332 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
3333 return MATCH_ERROR;
3335 ts->u.derived = dt_sym;
3337 return MATCH_YES;
3339 get_kind:
3340 if (matched_type
3341 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
3342 "intrinsic-type-spec at %C"))
3343 return MATCH_ERROR;
3345 /* For all types except double, derived and character, look for an
3346 optional kind specifier. MATCH_NO is actually OK at this point. */
3347 if (implicit_flag == 1)
3349 if (matched_type && gfc_match_char (')') != MATCH_YES)
3350 return MATCH_ERROR;
3352 return MATCH_YES;
3355 if (gfc_current_form == FORM_FREE)
3357 c = gfc_peek_ascii_char ();
3358 if (!gfc_is_whitespace (c) && c != '*' && c != '('
3359 && c != ':' && c != ',')
3361 if (matched_type && c == ')')
3363 gfc_next_ascii_char ();
3364 return MATCH_YES;
3366 return MATCH_NO;
3370 m = gfc_match_kind_spec (ts, false);
3371 if (m == MATCH_NO && ts->type != BT_CHARACTER)
3373 m = gfc_match_old_kind_spec (ts);
3374 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
3375 return MATCH_ERROR;
3378 if (matched_type && gfc_match_char (')') != MATCH_YES)
3379 return MATCH_ERROR;
3381 /* Defer association of the KIND expression of function results
3382 until after USE and IMPORT statements. */
3383 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
3384 || gfc_matching_function)
3385 return MATCH_YES;
3387 if (m == MATCH_NO)
3388 m = MATCH_YES; /* No kind specifier found. */
3390 return m;
3394 /* Match an IMPLICIT NONE statement. Actually, this statement is
3395 already matched in parse.c, or we would not end up here in the
3396 first place. So the only thing we need to check, is if there is
3397 trailing garbage. If not, the match is successful. */
3399 match
3400 gfc_match_implicit_none (void)
3402 char c;
3403 match m;
3404 char name[GFC_MAX_SYMBOL_LEN + 1];
3405 bool type = false;
3406 bool external = false;
3407 locus cur_loc = gfc_current_locus;
3409 if (gfc_current_ns->seen_implicit_none
3410 || gfc_current_ns->has_implicit_none_export)
3412 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3413 return MATCH_ERROR;
3416 gfc_gobble_whitespace ();
3417 c = gfc_peek_ascii_char ();
3418 if (c == '(')
3420 (void) gfc_next_ascii_char ();
3421 if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C"))
3422 return MATCH_ERROR;
3424 gfc_gobble_whitespace ();
3425 if (gfc_peek_ascii_char () == ')')
3427 (void) gfc_next_ascii_char ();
3428 type = true;
3430 else
3431 for(;;)
3433 m = gfc_match (" %n", name);
3434 if (m != MATCH_YES)
3435 return MATCH_ERROR;
3437 if (strcmp (name, "type") == 0)
3438 type = true;
3439 else if (strcmp (name, "external") == 0)
3440 external = true;
3441 else
3442 return MATCH_ERROR;
3444 gfc_gobble_whitespace ();
3445 c = gfc_next_ascii_char ();
3446 if (c == ',')
3447 continue;
3448 if (c == ')')
3449 break;
3450 return MATCH_ERROR;
3453 else
3454 type = true;
3456 if (gfc_match_eos () != MATCH_YES)
3457 return MATCH_ERROR;
3459 gfc_set_implicit_none (type, external, &cur_loc);
3461 return MATCH_YES;
3465 /* Match the letter range(s) of an IMPLICIT statement. */
3467 static match
3468 match_implicit_range (void)
3470 char c, c1, c2;
3471 int inner;
3472 locus cur_loc;
3474 cur_loc = gfc_current_locus;
3476 gfc_gobble_whitespace ();
3477 c = gfc_next_ascii_char ();
3478 if (c != '(')
3480 gfc_error ("Missing character range in IMPLICIT at %C");
3481 goto bad;
3484 inner = 1;
3485 while (inner)
3487 gfc_gobble_whitespace ();
3488 c1 = gfc_next_ascii_char ();
3489 if (!ISALPHA (c1))
3490 goto bad;
3492 gfc_gobble_whitespace ();
3493 c = gfc_next_ascii_char ();
3495 switch (c)
3497 case ')':
3498 inner = 0; /* Fall through. */
3500 case ',':
3501 c2 = c1;
3502 break;
3504 case '-':
3505 gfc_gobble_whitespace ();
3506 c2 = gfc_next_ascii_char ();
3507 if (!ISALPHA (c2))
3508 goto bad;
3510 gfc_gobble_whitespace ();
3511 c = gfc_next_ascii_char ();
3513 if ((c != ',') && (c != ')'))
3514 goto bad;
3515 if (c == ')')
3516 inner = 0;
3518 break;
3520 default:
3521 goto bad;
3524 if (c1 > c2)
3526 gfc_error ("Letters must be in alphabetic order in "
3527 "IMPLICIT statement at %C");
3528 goto bad;
3531 /* See if we can add the newly matched range to the pending
3532 implicits from this IMPLICIT statement. We do not check for
3533 conflicts with whatever earlier IMPLICIT statements may have
3534 set. This is done when we've successfully finished matching
3535 the current one. */
3536 if (!gfc_add_new_implicit_range (c1, c2))
3537 goto bad;
3540 return MATCH_YES;
3542 bad:
3543 gfc_syntax_error (ST_IMPLICIT);
3545 gfc_current_locus = cur_loc;
3546 return MATCH_ERROR;
3550 /* Match an IMPLICIT statement, storing the types for
3551 gfc_set_implicit() if the statement is accepted by the parser.
3552 There is a strange looking, but legal syntactic construction
3553 possible. It looks like:
3555 IMPLICIT INTEGER (a-b) (c-d)
3557 This is legal if "a-b" is a constant expression that happens to
3558 equal one of the legal kinds for integers. The real problem
3559 happens with an implicit specification that looks like:
3561 IMPLICIT INTEGER (a-b)
3563 In this case, a typespec matcher that is "greedy" (as most of the
3564 matchers are) gobbles the character range as a kindspec, leaving
3565 nothing left. We therefore have to go a bit more slowly in the
3566 matching process by inhibiting the kindspec checking during
3567 typespec matching and checking for a kind later. */
3569 match
3570 gfc_match_implicit (void)
3572 gfc_typespec ts;
3573 locus cur_loc;
3574 char c;
3575 match m;
3577 if (gfc_current_ns->seen_implicit_none)
3579 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3580 "statement");
3581 return MATCH_ERROR;
3584 gfc_clear_ts (&ts);
3586 /* We don't allow empty implicit statements. */
3587 if (gfc_match_eos () == MATCH_YES)
3589 gfc_error ("Empty IMPLICIT statement at %C");
3590 return MATCH_ERROR;
3595 /* First cleanup. */
3596 gfc_clear_new_implicit ();
3598 /* A basic type is mandatory here. */
3599 m = gfc_match_decl_type_spec (&ts, 1);
3600 if (m == MATCH_ERROR)
3601 goto error;
3602 if (m == MATCH_NO)
3603 goto syntax;
3605 cur_loc = gfc_current_locus;
3606 m = match_implicit_range ();
3608 if (m == MATCH_YES)
3610 /* We may have <TYPE> (<RANGE>). */
3611 gfc_gobble_whitespace ();
3612 c = gfc_peek_ascii_char ();
3613 if (c == ',' || c == '\n' || c == ';' || c == '!')
3615 /* Check for CHARACTER with no length parameter. */
3616 if (ts.type == BT_CHARACTER && !ts.u.cl)
3618 ts.kind = gfc_default_character_kind;
3619 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3620 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3621 NULL, 1);
3624 /* Record the Successful match. */
3625 if (!gfc_merge_new_implicit (&ts))
3626 return MATCH_ERROR;
3627 if (c == ',')
3628 c = gfc_next_ascii_char ();
3629 else if (gfc_match_eos () == MATCH_ERROR)
3630 goto error;
3631 continue;
3634 gfc_current_locus = cur_loc;
3637 /* Discard the (incorrectly) matched range. */
3638 gfc_clear_new_implicit ();
3640 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3641 if (ts.type == BT_CHARACTER)
3642 m = gfc_match_char_spec (&ts);
3643 else
3645 m = gfc_match_kind_spec (&ts, false);
3646 if (m == MATCH_NO)
3648 m = gfc_match_old_kind_spec (&ts);
3649 if (m == MATCH_ERROR)
3650 goto error;
3651 if (m == MATCH_NO)
3652 goto syntax;
3655 if (m == MATCH_ERROR)
3656 goto error;
3658 m = match_implicit_range ();
3659 if (m == MATCH_ERROR)
3660 goto error;
3661 if (m == MATCH_NO)
3662 goto syntax;
3664 gfc_gobble_whitespace ();
3665 c = gfc_next_ascii_char ();
3666 if (c != ',' && gfc_match_eos () != MATCH_YES)
3667 goto syntax;
3669 if (!gfc_merge_new_implicit (&ts))
3670 return MATCH_ERROR;
3672 while (c == ',');
3674 return MATCH_YES;
3676 syntax:
3677 gfc_syntax_error (ST_IMPLICIT);
3679 error:
3680 return MATCH_ERROR;
3684 match
3685 gfc_match_import (void)
3687 char name[GFC_MAX_SYMBOL_LEN + 1];
3688 match m;
3689 gfc_symbol *sym;
3690 gfc_symtree *st;
3692 if (gfc_current_ns->proc_name == NULL
3693 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3695 gfc_error ("IMPORT statement at %C only permitted in "
3696 "an INTERFACE body");
3697 return MATCH_ERROR;
3700 if (gfc_current_ns->proc_name->attr.module_procedure)
3702 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3703 "in a module procedure interface body");
3704 return MATCH_ERROR;
3707 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3708 return MATCH_ERROR;
3710 if (gfc_match_eos () == MATCH_YES)
3712 /* All host variables should be imported. */
3713 gfc_current_ns->has_import_set = 1;
3714 return MATCH_YES;
3717 if (gfc_match (" ::") == MATCH_YES)
3719 if (gfc_match_eos () == MATCH_YES)
3721 gfc_error ("Expecting list of named entities at %C");
3722 return MATCH_ERROR;
3726 for(;;)
3728 sym = NULL;
3729 m = gfc_match (" %n", name);
3730 switch (m)
3732 case MATCH_YES:
3733 if (gfc_current_ns->parent != NULL
3734 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3736 gfc_error ("Type name %qs at %C is ambiguous", name);
3737 return MATCH_ERROR;
3739 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3740 && gfc_find_symbol (name,
3741 gfc_current_ns->proc_name->ns->parent,
3742 1, &sym))
3744 gfc_error ("Type name %qs at %C is ambiguous", name);
3745 return MATCH_ERROR;
3748 if (sym == NULL)
3750 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3751 "at %C - does not exist.", name);
3752 return MATCH_ERROR;
3755 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3757 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3758 "at %C", name);
3759 goto next_item;
3762 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3763 st->n.sym = sym;
3764 sym->refs++;
3765 sym->attr.imported = 1;
3767 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3769 /* The actual derived type is stored in a symtree with the first
3770 letter of the name capitalized; the symtree with the all
3771 lower-case name contains the associated generic function. */
3772 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3773 gfc_dt_upper_string (name));
3774 st->n.sym = sym;
3775 sym->refs++;
3776 sym->attr.imported = 1;
3779 goto next_item;
3781 case MATCH_NO:
3782 break;
3784 case MATCH_ERROR:
3785 return MATCH_ERROR;
3788 next_item:
3789 if (gfc_match_eos () == MATCH_YES)
3790 break;
3791 if (gfc_match_char (',') != MATCH_YES)
3792 goto syntax;
3795 return MATCH_YES;
3797 syntax:
3798 gfc_error ("Syntax error in IMPORT statement at %C");
3799 return MATCH_ERROR;
3803 /* A minimal implementation of gfc_match without whitespace, escape
3804 characters or variable arguments. Returns true if the next
3805 characters match the TARGET template exactly. */
3807 static bool
3808 match_string_p (const char *target)
3810 const char *p;
3812 for (p = target; *p; p++)
3813 if ((char) gfc_next_ascii_char () != *p)
3814 return false;
3815 return true;
3818 /* Matches an attribute specification including array specs. If
3819 successful, leaves the variables current_attr and current_as
3820 holding the specification. Also sets the colon_seen variable for
3821 later use by matchers associated with initializations.
3823 This subroutine is a little tricky in the sense that we don't know
3824 if we really have an attr-spec until we hit the double colon.
3825 Until that time, we can only return MATCH_NO. This forces us to
3826 check for duplicate specification at this level. */
3828 static match
3829 match_attr_spec (void)
3831 /* Modifiers that can exist in a type statement. */
3832 enum
3833 { GFC_DECL_BEGIN = 0,
3834 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3835 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3836 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3837 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3838 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3839 DECL_NONE, GFC_DECL_END /* Sentinel */
3842 /* GFC_DECL_END is the sentinel, index starts at 0. */
3843 #define NUM_DECL GFC_DECL_END
3845 locus start, seen_at[NUM_DECL];
3846 int seen[NUM_DECL];
3847 unsigned int d;
3848 const char *attr;
3849 match m;
3850 bool t;
3852 gfc_clear_attr (&current_attr);
3853 start = gfc_current_locus;
3855 current_as = NULL;
3856 colon_seen = 0;
3858 /* See if we get all of the keywords up to the final double colon. */
3859 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3860 seen[d] = 0;
3862 for (;;)
3864 char ch;
3866 d = DECL_NONE;
3867 gfc_gobble_whitespace ();
3869 ch = gfc_next_ascii_char ();
3870 if (ch == ':')
3872 /* This is the successful exit condition for the loop. */
3873 if (gfc_next_ascii_char () == ':')
3874 break;
3876 else if (ch == ',')
3878 gfc_gobble_whitespace ();
3879 switch (gfc_peek_ascii_char ())
3881 case 'a':
3882 gfc_next_ascii_char ();
3883 switch (gfc_next_ascii_char ())
3885 case 'l':
3886 if (match_string_p ("locatable"))
3888 /* Matched "allocatable". */
3889 d = DECL_ALLOCATABLE;
3891 break;
3893 case 's':
3894 if (match_string_p ("ynchronous"))
3896 /* Matched "asynchronous". */
3897 d = DECL_ASYNCHRONOUS;
3899 break;
3901 break;
3903 case 'b':
3904 /* Try and match the bind(c). */
3905 m = gfc_match_bind_c (NULL, true);
3906 if (m == MATCH_YES)
3907 d = DECL_IS_BIND_C;
3908 else if (m == MATCH_ERROR)
3909 goto cleanup;
3910 break;
3912 case 'c':
3913 gfc_next_ascii_char ();
3914 if ('o' != gfc_next_ascii_char ())
3915 break;
3916 switch (gfc_next_ascii_char ())
3918 case 'd':
3919 if (match_string_p ("imension"))
3921 d = DECL_CODIMENSION;
3922 break;
3924 case 'n':
3925 if (match_string_p ("tiguous"))
3927 d = DECL_CONTIGUOUS;
3928 break;
3931 break;
3933 case 'd':
3934 if (match_string_p ("dimension"))
3935 d = DECL_DIMENSION;
3936 break;
3938 case 'e':
3939 if (match_string_p ("external"))
3940 d = DECL_EXTERNAL;
3941 break;
3943 case 'i':
3944 if (match_string_p ("int"))
3946 ch = gfc_next_ascii_char ();
3947 if (ch == 'e')
3949 if (match_string_p ("nt"))
3951 /* Matched "intent". */
3952 /* TODO: Call match_intent_spec from here. */
3953 if (gfc_match (" ( in out )") == MATCH_YES)
3954 d = DECL_INOUT;
3955 else if (gfc_match (" ( in )") == MATCH_YES)
3956 d = DECL_IN;
3957 else if (gfc_match (" ( out )") == MATCH_YES)
3958 d = DECL_OUT;
3961 else if (ch == 'r')
3963 if (match_string_p ("insic"))
3965 /* Matched "intrinsic". */
3966 d = DECL_INTRINSIC;
3970 break;
3972 case 'o':
3973 if (match_string_p ("optional"))
3974 d = DECL_OPTIONAL;
3975 break;
3977 case 'p':
3978 gfc_next_ascii_char ();
3979 switch (gfc_next_ascii_char ())
3981 case 'a':
3982 if (match_string_p ("rameter"))
3984 /* Matched "parameter". */
3985 d = DECL_PARAMETER;
3987 break;
3989 case 'o':
3990 if (match_string_p ("inter"))
3992 /* Matched "pointer". */
3993 d = DECL_POINTER;
3995 break;
3997 case 'r':
3998 ch = gfc_next_ascii_char ();
3999 if (ch == 'i')
4001 if (match_string_p ("vate"))
4003 /* Matched "private". */
4004 d = DECL_PRIVATE;
4007 else if (ch == 'o')
4009 if (match_string_p ("tected"))
4011 /* Matched "protected". */
4012 d = DECL_PROTECTED;
4015 break;
4017 case 'u':
4018 if (match_string_p ("blic"))
4020 /* Matched "public". */
4021 d = DECL_PUBLIC;
4023 break;
4025 break;
4027 case 's':
4028 if (match_string_p ("save"))
4029 d = DECL_SAVE;
4030 break;
4032 case 't':
4033 if (match_string_p ("target"))
4034 d = DECL_TARGET;
4035 break;
4037 case 'v':
4038 gfc_next_ascii_char ();
4039 ch = gfc_next_ascii_char ();
4040 if (ch == 'a')
4042 if (match_string_p ("lue"))
4044 /* Matched "value". */
4045 d = DECL_VALUE;
4048 else if (ch == 'o')
4050 if (match_string_p ("latile"))
4052 /* Matched "volatile". */
4053 d = DECL_VOLATILE;
4056 break;
4060 /* No double colon and no recognizable decl_type, so assume that
4061 we've been looking at something else the whole time. */
4062 if (d == DECL_NONE)
4064 m = MATCH_NO;
4065 goto cleanup;
4068 /* Check to make sure any parens are paired up correctly. */
4069 if (gfc_match_parens () == MATCH_ERROR)
4071 m = MATCH_ERROR;
4072 goto cleanup;
4075 seen[d]++;
4076 seen_at[d] = gfc_current_locus;
4078 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
4080 gfc_array_spec *as = NULL;
4082 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
4083 d == DECL_CODIMENSION);
4085 if (current_as == NULL)
4086 current_as = as;
4087 else if (m == MATCH_YES)
4089 if (!merge_array_spec (as, current_as, false))
4090 m = MATCH_ERROR;
4091 free (as);
4094 if (m == MATCH_NO)
4096 if (d == DECL_CODIMENSION)
4097 gfc_error ("Missing codimension specification at %C");
4098 else
4099 gfc_error ("Missing dimension specification at %C");
4100 m = MATCH_ERROR;
4103 if (m == MATCH_ERROR)
4104 goto cleanup;
4108 /* Since we've seen a double colon, we have to be looking at an
4109 attr-spec. This means that we can now issue errors. */
4110 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4111 if (seen[d] > 1)
4113 switch (d)
4115 case DECL_ALLOCATABLE:
4116 attr = "ALLOCATABLE";
4117 break;
4118 case DECL_ASYNCHRONOUS:
4119 attr = "ASYNCHRONOUS";
4120 break;
4121 case DECL_CODIMENSION:
4122 attr = "CODIMENSION";
4123 break;
4124 case DECL_CONTIGUOUS:
4125 attr = "CONTIGUOUS";
4126 break;
4127 case DECL_DIMENSION:
4128 attr = "DIMENSION";
4129 break;
4130 case DECL_EXTERNAL:
4131 attr = "EXTERNAL";
4132 break;
4133 case DECL_IN:
4134 attr = "INTENT (IN)";
4135 break;
4136 case DECL_OUT:
4137 attr = "INTENT (OUT)";
4138 break;
4139 case DECL_INOUT:
4140 attr = "INTENT (IN OUT)";
4141 break;
4142 case DECL_INTRINSIC:
4143 attr = "INTRINSIC";
4144 break;
4145 case DECL_OPTIONAL:
4146 attr = "OPTIONAL";
4147 break;
4148 case DECL_PARAMETER:
4149 attr = "PARAMETER";
4150 break;
4151 case DECL_POINTER:
4152 attr = "POINTER";
4153 break;
4154 case DECL_PROTECTED:
4155 attr = "PROTECTED";
4156 break;
4157 case DECL_PRIVATE:
4158 attr = "PRIVATE";
4159 break;
4160 case DECL_PUBLIC:
4161 attr = "PUBLIC";
4162 break;
4163 case DECL_SAVE:
4164 attr = "SAVE";
4165 break;
4166 case DECL_TARGET:
4167 attr = "TARGET";
4168 break;
4169 case DECL_IS_BIND_C:
4170 attr = "IS_BIND_C";
4171 break;
4172 case DECL_VALUE:
4173 attr = "VALUE";
4174 break;
4175 case DECL_VOLATILE:
4176 attr = "VOLATILE";
4177 break;
4178 default:
4179 attr = NULL; /* This shouldn't happen. */
4182 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
4183 m = MATCH_ERROR;
4184 goto cleanup;
4187 /* Now that we've dealt with duplicate attributes, add the attributes
4188 to the current attribute. */
4189 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4191 if (seen[d] == 0)
4192 continue;
4194 if (gfc_current_state () == COMP_DERIVED
4195 && d != DECL_DIMENSION && d != DECL_CODIMENSION
4196 && d != DECL_POINTER && d != DECL_PRIVATE
4197 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
4199 if (d == DECL_ALLOCATABLE)
4201 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
4202 "attribute at %C in a TYPE definition"))
4204 m = MATCH_ERROR;
4205 goto cleanup;
4208 else
4210 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
4211 &seen_at[d]);
4212 m = MATCH_ERROR;
4213 goto cleanup;
4217 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
4218 && gfc_current_state () != COMP_MODULE)
4220 if (d == DECL_PRIVATE)
4221 attr = "PRIVATE";
4222 else
4223 attr = "PUBLIC";
4224 if (gfc_current_state () == COMP_DERIVED
4225 && gfc_state_stack->previous
4226 && gfc_state_stack->previous->state == COMP_MODULE)
4228 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
4229 "at %L in a TYPE definition", attr,
4230 &seen_at[d]))
4232 m = MATCH_ERROR;
4233 goto cleanup;
4236 else
4238 gfc_error ("%s attribute at %L is not allowed outside of the "
4239 "specification part of a module", attr, &seen_at[d]);
4240 m = MATCH_ERROR;
4241 goto cleanup;
4245 switch (d)
4247 case DECL_ALLOCATABLE:
4248 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
4249 break;
4251 case DECL_ASYNCHRONOUS:
4252 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
4253 t = false;
4254 else
4255 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
4256 break;
4258 case DECL_CODIMENSION:
4259 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
4260 break;
4262 case DECL_CONTIGUOUS:
4263 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
4264 t = false;
4265 else
4266 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
4267 break;
4269 case DECL_DIMENSION:
4270 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
4271 break;
4273 case DECL_EXTERNAL:
4274 t = gfc_add_external (&current_attr, &seen_at[d]);
4275 break;
4277 case DECL_IN:
4278 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
4279 break;
4281 case DECL_OUT:
4282 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
4283 break;
4285 case DECL_INOUT:
4286 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
4287 break;
4289 case DECL_INTRINSIC:
4290 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
4291 break;
4293 case DECL_OPTIONAL:
4294 t = gfc_add_optional (&current_attr, &seen_at[d]);
4295 break;
4297 case DECL_PARAMETER:
4298 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
4299 break;
4301 case DECL_POINTER:
4302 t = gfc_add_pointer (&current_attr, &seen_at[d]);
4303 break;
4305 case DECL_PROTECTED:
4306 if (gfc_current_state () != COMP_MODULE
4307 || (gfc_current_ns->proc_name
4308 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
4310 gfc_error ("PROTECTED at %C only allowed in specification "
4311 "part of a module");
4312 t = false;
4313 break;
4316 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
4317 t = false;
4318 else
4319 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
4320 break;
4322 case DECL_PRIVATE:
4323 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
4324 &seen_at[d]);
4325 break;
4327 case DECL_PUBLIC:
4328 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
4329 &seen_at[d]);
4330 break;
4332 case DECL_SAVE:
4333 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
4334 break;
4336 case DECL_TARGET:
4337 t = gfc_add_target (&current_attr, &seen_at[d]);
4338 break;
4340 case DECL_IS_BIND_C:
4341 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
4342 break;
4344 case DECL_VALUE:
4345 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
4346 t = false;
4347 else
4348 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
4349 break;
4351 case DECL_VOLATILE:
4352 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
4353 t = false;
4354 else
4355 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
4356 break;
4358 default:
4359 gfc_internal_error ("match_attr_spec(): Bad attribute");
4362 if (!t)
4364 m = MATCH_ERROR;
4365 goto cleanup;
4369 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4370 if ((gfc_current_state () == COMP_MODULE
4371 || gfc_current_state () == COMP_SUBMODULE)
4372 && !current_attr.save
4373 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
4374 current_attr.save = SAVE_IMPLICIT;
4376 colon_seen = 1;
4377 return MATCH_YES;
4379 cleanup:
4380 gfc_current_locus = start;
4381 gfc_free_array_spec (current_as);
4382 current_as = NULL;
4383 return m;
4387 /* Set the binding label, dest_label, either with the binding label
4388 stored in the given gfc_typespec, ts, or if none was provided, it
4389 will be the symbol name in all lower case, as required by the draft
4390 (J3/04-007, section 15.4.1). If a binding label was given and
4391 there is more than one argument (num_idents), it is an error. */
4393 static bool
4394 set_binding_label (const char **dest_label, const char *sym_name,
4395 int num_idents)
4397 if (num_idents > 1 && has_name_equals)
4399 gfc_error ("Multiple identifiers provided with "
4400 "single NAME= specifier at %C");
4401 return false;
4404 if (curr_binding_label)
4405 /* Binding label given; store in temp holder till have sym. */
4406 *dest_label = curr_binding_label;
4407 else
4409 /* No binding label given, and the NAME= specifier did not exist,
4410 which means there was no NAME="". */
4411 if (sym_name != NULL && has_name_equals == 0)
4412 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
4415 return true;
4419 /* Set the status of the given common block as being BIND(C) or not,
4420 depending on the given parameter, is_bind_c. */
4422 void
4423 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
4425 com_block->is_bind_c = is_bind_c;
4426 return;
4430 /* Verify that the given gfc_typespec is for a C interoperable type. */
4432 bool
4433 gfc_verify_c_interop (gfc_typespec *ts)
4435 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
4436 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
4437 ? true : false;
4438 else if (ts->type == BT_CLASS)
4439 return false;
4440 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
4441 return false;
4443 return true;
4447 /* Verify that the variables of a given common block, which has been
4448 defined with the attribute specifier bind(c), to be of a C
4449 interoperable type. Errors will be reported here, if
4450 encountered. */
4452 bool
4453 verify_com_block_vars_c_interop (gfc_common_head *com_block)
4455 gfc_symbol *curr_sym = NULL;
4456 bool retval = true;
4458 curr_sym = com_block->head;
4460 /* Make sure we have at least one symbol. */
4461 if (curr_sym == NULL)
4462 return retval;
4464 /* Here we know we have a symbol, so we'll execute this loop
4465 at least once. */
4468 /* The second to last param, 1, says this is in a common block. */
4469 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
4470 curr_sym = curr_sym->common_next;
4471 } while (curr_sym != NULL);
4473 return retval;
4477 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4478 an appropriate error message is reported. */
4480 bool
4481 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
4482 int is_in_common, gfc_common_head *com_block)
4484 bool bind_c_function = false;
4485 bool retval = true;
4487 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
4488 bind_c_function = true;
4490 if (tmp_sym->attr.function && tmp_sym->result != NULL)
4492 tmp_sym = tmp_sym->result;
4493 /* Make sure it wasn't an implicitly typed result. */
4494 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
4496 gfc_warning (OPT_Wc_binding_type,
4497 "Implicitly declared BIND(C) function %qs at "
4498 "%L may not be C interoperable", tmp_sym->name,
4499 &tmp_sym->declared_at);
4500 tmp_sym->ts.f90_type = tmp_sym->ts.type;
4501 /* Mark it as C interoperable to prevent duplicate warnings. */
4502 tmp_sym->ts.is_c_interop = 1;
4503 tmp_sym->attr.is_c_interop = 1;
4507 /* Here, we know we have the bind(c) attribute, so if we have
4508 enough type info, then verify that it's a C interop kind.
4509 The info could be in the symbol already, or possibly still in
4510 the given ts (current_ts), so look in both. */
4511 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
4513 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
4515 /* See if we're dealing with a sym in a common block or not. */
4516 if (is_in_common == 1 && warn_c_binding_type)
4518 gfc_warning (OPT_Wc_binding_type,
4519 "Variable %qs in common block %qs at %L "
4520 "may not be a C interoperable "
4521 "kind though common block %qs is BIND(C)",
4522 tmp_sym->name, com_block->name,
4523 &(tmp_sym->declared_at), com_block->name);
4525 else
4527 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4528 gfc_error ("Type declaration %qs at %L is not C "
4529 "interoperable but it is BIND(C)",
4530 tmp_sym->name, &(tmp_sym->declared_at));
4531 else if (warn_c_binding_type)
4532 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
4533 "may not be a C interoperable "
4534 "kind but it is BIND(C)",
4535 tmp_sym->name, &(tmp_sym->declared_at));
4539 /* Variables declared w/in a common block can't be bind(c)
4540 since there's no way for C to see these variables, so there's
4541 semantically no reason for the attribute. */
4542 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4544 gfc_error ("Variable %qs in common block %qs at "
4545 "%L cannot be declared with BIND(C) "
4546 "since it is not a global",
4547 tmp_sym->name, com_block->name,
4548 &(tmp_sym->declared_at));
4549 retval = false;
4552 /* Scalar variables that are bind(c) can not have the pointer
4553 or allocatable attributes. */
4554 if (tmp_sym->attr.is_bind_c == 1)
4556 if (tmp_sym->attr.pointer == 1)
4558 gfc_error ("Variable %qs at %L cannot have both the "
4559 "POINTER and BIND(C) attributes",
4560 tmp_sym->name, &(tmp_sym->declared_at));
4561 retval = false;
4564 if (tmp_sym->attr.allocatable == 1)
4566 gfc_error ("Variable %qs at %L cannot have both the "
4567 "ALLOCATABLE and BIND(C) attributes",
4568 tmp_sym->name, &(tmp_sym->declared_at));
4569 retval = false;
4574 /* If it is a BIND(C) function, make sure the return value is a
4575 scalar value. The previous tests in this function made sure
4576 the type is interoperable. */
4577 if (bind_c_function && tmp_sym->as != NULL)
4578 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4579 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4581 /* BIND(C) functions can not return a character string. */
4582 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4583 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4584 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4585 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4586 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4587 "be a character string", tmp_sym->name,
4588 &(tmp_sym->declared_at));
4591 /* See if the symbol has been marked as private. If it has, make sure
4592 there is no binding label and warn the user if there is one. */
4593 if (tmp_sym->attr.access == ACCESS_PRIVATE
4594 && tmp_sym->binding_label)
4595 /* Use gfc_warning_now because we won't say that the symbol fails
4596 just because of this. */
4597 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4598 "given the binding label %qs", tmp_sym->name,
4599 &(tmp_sym->declared_at), tmp_sym->binding_label);
4601 return retval;
4605 /* Set the appropriate fields for a symbol that's been declared as
4606 BIND(C) (the is_bind_c flag and the binding label), and verify that
4607 the type is C interoperable. Errors are reported by the functions
4608 used to set/test these fields. */
4610 bool
4611 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4613 bool retval = true;
4615 /* TODO: Do we need to make sure the vars aren't marked private? */
4617 /* Set the is_bind_c bit in symbol_attribute. */
4618 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4620 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4621 return false;
4623 return retval;
4627 /* Set the fields marking the given common block as BIND(C), including
4628 a binding label, and report any errors encountered. */
4630 bool
4631 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4633 bool retval = true;
4635 /* destLabel, common name, typespec (which may have binding label). */
4636 if (!set_binding_label (&com_block->binding_label, com_block->name,
4637 num_idents))
4638 return false;
4640 /* Set the given common block (com_block) to being bind(c) (1). */
4641 set_com_block_bind_c (com_block, 1);
4643 return retval;
4647 /* Retrieve the list of one or more identifiers that the given bind(c)
4648 attribute applies to. */
4650 bool
4651 get_bind_c_idents (void)
4653 char name[GFC_MAX_SYMBOL_LEN + 1];
4654 int num_idents = 0;
4655 gfc_symbol *tmp_sym = NULL;
4656 match found_id;
4657 gfc_common_head *com_block = NULL;
4659 if (gfc_match_name (name) == MATCH_YES)
4661 found_id = MATCH_YES;
4662 gfc_get_ha_symbol (name, &tmp_sym);
4664 else if (match_common_name (name) == MATCH_YES)
4666 found_id = MATCH_YES;
4667 com_block = gfc_get_common (name, 0);
4669 else
4671 gfc_error ("Need either entity or common block name for "
4672 "attribute specification statement at %C");
4673 return false;
4676 /* Save the current identifier and look for more. */
4679 /* Increment the number of identifiers found for this spec stmt. */
4680 num_idents++;
4682 /* Make sure we have a sym or com block, and verify that it can
4683 be bind(c). Set the appropriate field(s) and look for more
4684 identifiers. */
4685 if (tmp_sym != NULL || com_block != NULL)
4687 if (tmp_sym != NULL)
4689 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4690 return false;
4692 else
4694 if (!set_verify_bind_c_com_block (com_block, num_idents))
4695 return false;
4698 /* Look to see if we have another identifier. */
4699 tmp_sym = NULL;
4700 if (gfc_match_eos () == MATCH_YES)
4701 found_id = MATCH_NO;
4702 else if (gfc_match_char (',') != MATCH_YES)
4703 found_id = MATCH_NO;
4704 else if (gfc_match_name (name) == MATCH_YES)
4706 found_id = MATCH_YES;
4707 gfc_get_ha_symbol (name, &tmp_sym);
4709 else if (match_common_name (name) == MATCH_YES)
4711 found_id = MATCH_YES;
4712 com_block = gfc_get_common (name, 0);
4714 else
4716 gfc_error ("Missing entity or common block name for "
4717 "attribute specification statement at %C");
4718 return false;
4721 else
4723 gfc_internal_error ("Missing symbol");
4725 } while (found_id == MATCH_YES);
4727 /* if we get here we were successful */
4728 return true;
4732 /* Try and match a BIND(C) attribute specification statement. */
4734 match
4735 gfc_match_bind_c_stmt (void)
4737 match found_match = MATCH_NO;
4738 gfc_typespec *ts;
4740 ts = &current_ts;
4742 /* This may not be necessary. */
4743 gfc_clear_ts (ts);
4744 /* Clear the temporary binding label holder. */
4745 curr_binding_label = NULL;
4747 /* Look for the bind(c). */
4748 found_match = gfc_match_bind_c (NULL, true);
4750 if (found_match == MATCH_YES)
4752 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4753 return MATCH_ERROR;
4755 /* Look for the :: now, but it is not required. */
4756 gfc_match (" :: ");
4758 /* Get the identifier(s) that needs to be updated. This may need to
4759 change to hand the flag(s) for the attr specified so all identifiers
4760 found can have all appropriate parts updated (assuming that the same
4761 spec stmt can have multiple attrs, such as both bind(c) and
4762 allocatable...). */
4763 if (!get_bind_c_idents ())
4764 /* Error message should have printed already. */
4765 return MATCH_ERROR;
4768 return found_match;
4772 /* Match a data declaration statement. */
4774 match
4775 gfc_match_data_decl (void)
4777 gfc_symbol *sym;
4778 match m;
4779 int elem;
4781 num_idents_on_line = 0;
4783 m = gfc_match_decl_type_spec (&current_ts, 0);
4784 if (m != MATCH_YES)
4785 return m;
4787 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4788 && !gfc_comp_struct (gfc_current_state ()))
4790 sym = gfc_use_derived (current_ts.u.derived);
4792 if (sym == NULL)
4794 m = MATCH_ERROR;
4795 goto cleanup;
4798 current_ts.u.derived = sym;
4801 m = match_attr_spec ();
4802 if (m == MATCH_ERROR)
4804 m = MATCH_NO;
4805 goto cleanup;
4808 if (current_ts.type == BT_CLASS
4809 && current_ts.u.derived->attr.unlimited_polymorphic)
4810 goto ok;
4812 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4813 && current_ts.u.derived->components == NULL
4814 && !current_ts.u.derived->attr.zero_comp)
4817 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
4818 goto ok;
4820 gfc_find_symbol (current_ts.u.derived->name,
4821 current_ts.u.derived->ns, 1, &sym);
4823 /* Any symbol that we find had better be a type definition
4824 which has its components defined, or be a structure definition
4825 actively being parsed. */
4826 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
4827 && (current_ts.u.derived->components != NULL
4828 || current_ts.u.derived->attr.zero_comp
4829 || current_ts.u.derived == gfc_new_block))
4830 goto ok;
4832 gfc_error ("Derived type at %C has not been previously defined "
4833 "and so cannot appear in a derived type definition");
4834 m = MATCH_ERROR;
4835 goto cleanup;
4839 /* If we have an old-style character declaration, and no new-style
4840 attribute specifications, then there a comma is optional between
4841 the type specification and the variable list. */
4842 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4843 gfc_match_char (',');
4845 /* Give the types/attributes to symbols that follow. Give the element
4846 a number so that repeat character length expressions can be copied. */
4847 elem = 1;
4848 for (;;)
4850 num_idents_on_line++;
4851 m = variable_decl (elem++);
4852 if (m == MATCH_ERROR)
4853 goto cleanup;
4854 if (m == MATCH_NO)
4855 break;
4857 if (gfc_match_eos () == MATCH_YES)
4858 goto cleanup;
4859 if (gfc_match_char (',') != MATCH_YES)
4860 break;
4863 if (!gfc_error_flag_test ())
4864 gfc_error ("Syntax error in data declaration at %C");
4865 m = MATCH_ERROR;
4867 gfc_free_data_all (gfc_current_ns);
4869 cleanup:
4870 gfc_free_array_spec (current_as);
4871 current_as = NULL;
4872 return m;
4876 /* Match a prefix associated with a function or subroutine
4877 declaration. If the typespec pointer is nonnull, then a typespec
4878 can be matched. Note that if nothing matches, MATCH_YES is
4879 returned (the null string was matched). */
4881 match
4882 gfc_match_prefix (gfc_typespec *ts)
4884 bool seen_type;
4885 bool seen_impure;
4886 bool found_prefix;
4888 gfc_clear_attr (&current_attr);
4889 seen_type = false;
4890 seen_impure = false;
4892 gcc_assert (!gfc_matching_prefix);
4893 gfc_matching_prefix = true;
4897 found_prefix = false;
4899 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
4900 corresponding attribute seems natural and distinguishes these
4901 procedures from procedure types of PROC_MODULE, which these are
4902 as well. */
4903 if (gfc_match ("module% ") == MATCH_YES)
4905 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
4906 goto error;
4908 current_attr.module_procedure = 1;
4909 found_prefix = true;
4912 if (!seen_type && ts != NULL
4913 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4914 && gfc_match_space () == MATCH_YES)
4917 seen_type = true;
4918 found_prefix = true;
4921 if (gfc_match ("elemental% ") == MATCH_YES)
4923 if (!gfc_add_elemental (&current_attr, NULL))
4924 goto error;
4926 found_prefix = true;
4929 if (gfc_match ("pure% ") == MATCH_YES)
4931 if (!gfc_add_pure (&current_attr, NULL))
4932 goto error;
4934 found_prefix = true;
4937 if (gfc_match ("recursive% ") == MATCH_YES)
4939 if (!gfc_add_recursive (&current_attr, NULL))
4940 goto error;
4942 found_prefix = true;
4945 /* IMPURE is a somewhat special case, as it needs not set an actual
4946 attribute but rather only prevents ELEMENTAL routines from being
4947 automatically PURE. */
4948 if (gfc_match ("impure% ") == MATCH_YES)
4950 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4951 goto error;
4953 seen_impure = true;
4954 found_prefix = true;
4957 while (found_prefix);
4959 /* IMPURE and PURE must not both appear, of course. */
4960 if (seen_impure && current_attr.pure)
4962 gfc_error ("PURE and IMPURE must not appear both at %C");
4963 goto error;
4966 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4967 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4969 if (!gfc_add_pure (&current_attr, NULL))
4970 goto error;
4973 /* At this point, the next item is not a prefix. */
4974 gcc_assert (gfc_matching_prefix);
4976 gfc_matching_prefix = false;
4977 return MATCH_YES;
4979 error:
4980 gcc_assert (gfc_matching_prefix);
4981 gfc_matching_prefix = false;
4982 return MATCH_ERROR;
4986 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4988 static bool
4989 copy_prefix (symbol_attribute *dest, locus *where)
4991 if (dest->module_procedure)
4993 if (current_attr.elemental)
4994 dest->elemental = 1;
4996 if (current_attr.pure)
4997 dest->pure = 1;
4999 if (current_attr.recursive)
5000 dest->recursive = 1;
5002 /* Module procedures are unusual in that the 'dest' is copied from
5003 the interface declaration. However, this is an oportunity to
5004 check that the submodule declaration is compliant with the
5005 interface. */
5006 if (dest->elemental && !current_attr.elemental)
5008 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
5009 "missing at %L", where);
5010 return false;
5013 if (dest->pure && !current_attr.pure)
5015 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
5016 "missing at %L", where);
5017 return false;
5020 if (dest->recursive && !current_attr.recursive)
5022 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
5023 "missing at %L", where);
5024 return false;
5027 return true;
5030 if (current_attr.elemental && !gfc_add_elemental (dest, where))
5031 return false;
5033 if (current_attr.pure && !gfc_add_pure (dest, where))
5034 return false;
5036 if (current_attr.recursive && !gfc_add_recursive (dest, where))
5037 return false;
5039 return true;
5043 /* Match a formal argument list. */
5045 match
5046 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
5048 gfc_formal_arglist *head, *tail, *p, *q;
5049 char name[GFC_MAX_SYMBOL_LEN + 1];
5050 gfc_symbol *sym;
5051 match m;
5052 gfc_formal_arglist *formal = NULL;
5054 head = tail = NULL;
5056 /* Keep the interface formal argument list and null it so that the
5057 matching for the new declaration can be done. The numbers and
5058 names of the arguments are checked here. The interface formal
5059 arguments are retained in formal_arglist and the characteristics
5060 are compared in resolve.c(resolve_fl_procedure). See the remark
5061 in get_proc_name about the eventual need to copy the formal_arglist
5062 and populate the formal namespace of the interface symbol. */
5063 if (progname->attr.module_procedure
5064 && progname->attr.host_assoc)
5066 formal = progname->formal;
5067 progname->formal = NULL;
5070 if (gfc_match_char ('(') != MATCH_YES)
5072 if (null_flag)
5073 goto ok;
5074 return MATCH_NO;
5077 if (gfc_match_char (')') == MATCH_YES)
5078 goto ok;
5080 for (;;)
5082 if (gfc_match_char ('*') == MATCH_YES)
5084 sym = NULL;
5085 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
5086 "at %C"))
5088 m = MATCH_ERROR;
5089 goto cleanup;
5092 else
5094 m = gfc_match_name (name);
5095 if (m != MATCH_YES)
5096 goto cleanup;
5098 if (gfc_get_symbol (name, NULL, &sym))
5099 goto cleanup;
5102 p = gfc_get_formal_arglist ();
5104 if (head == NULL)
5105 head = tail = p;
5106 else
5108 tail->next = p;
5109 tail = p;
5112 tail->sym = sym;
5114 /* We don't add the VARIABLE flavor because the name could be a
5115 dummy procedure. We don't apply these attributes to formal
5116 arguments of statement functions. */
5117 if (sym != NULL && !st_flag
5118 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
5119 || !gfc_missing_attr (&sym->attr, NULL)))
5121 m = MATCH_ERROR;
5122 goto cleanup;
5125 /* The name of a program unit can be in a different namespace,
5126 so check for it explicitly. After the statement is accepted,
5127 the name is checked for especially in gfc_get_symbol(). */
5128 if (gfc_new_block != NULL && sym != NULL
5129 && strcmp (sym->name, gfc_new_block->name) == 0)
5131 gfc_error ("Name %qs at %C is the name of the procedure",
5132 sym->name);
5133 m = MATCH_ERROR;
5134 goto cleanup;
5137 if (gfc_match_char (')') == MATCH_YES)
5138 goto ok;
5140 m = gfc_match_char (',');
5141 if (m != MATCH_YES)
5143 gfc_error ("Unexpected junk in formal argument list at %C");
5144 goto cleanup;
5149 /* Check for duplicate symbols in the formal argument list. */
5150 if (head != NULL)
5152 for (p = head; p->next; p = p->next)
5154 if (p->sym == NULL)
5155 continue;
5157 for (q = p->next; q; q = q->next)
5158 if (p->sym == q->sym)
5160 gfc_error ("Duplicate symbol %qs in formal argument list "
5161 "at %C", p->sym->name);
5163 m = MATCH_ERROR;
5164 goto cleanup;
5169 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
5171 m = MATCH_ERROR;
5172 goto cleanup;
5175 /* gfc_error_now used in following and return with MATCH_YES because
5176 doing otherwise results in a cascade of extraneous errors and in
5177 some cases an ICE in symbol.c(gfc_release_symbol). */
5178 if (progname->attr.module_procedure && progname->attr.host_assoc)
5180 bool arg_count_mismatch = false;
5182 if (!formal && head)
5183 arg_count_mismatch = true;
5185 /* Abbreviated module procedure declaration is not meant to have any
5186 formal arguments! */
5187 if (!progname->abr_modproc_decl && formal && !head)
5188 arg_count_mismatch = true;
5190 for (p = formal, q = head; p && q; p = p->next, q = q->next)
5192 if ((p->next != NULL && q->next == NULL)
5193 || (p->next == NULL && q->next != NULL))
5194 arg_count_mismatch = true;
5195 else if ((p->sym == NULL && q->sym == NULL)
5196 || strcmp (p->sym->name, q->sym->name) == 0)
5197 continue;
5198 else
5199 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
5200 "argument names (%s/%s) at %C",
5201 p->sym->name, q->sym->name);
5204 if (arg_count_mismatch)
5205 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
5206 "formal arguments at %C");
5209 return MATCH_YES;
5211 cleanup:
5212 gfc_free_formal_arglist (head);
5213 return m;
5217 /* Match a RESULT specification following a function declaration or
5218 ENTRY statement. Also matches the end-of-statement. */
5220 static match
5221 match_result (gfc_symbol *function, gfc_symbol **result)
5223 char name[GFC_MAX_SYMBOL_LEN + 1];
5224 gfc_symbol *r;
5225 match m;
5227 if (gfc_match (" result (") != MATCH_YES)
5228 return MATCH_NO;
5230 m = gfc_match_name (name);
5231 if (m != MATCH_YES)
5232 return m;
5234 /* Get the right paren, and that's it because there could be the
5235 bind(c) attribute after the result clause. */
5236 if (gfc_match_char (')') != MATCH_YES)
5238 /* TODO: should report the missing right paren here. */
5239 return MATCH_ERROR;
5242 if (strcmp (function->name, name) == 0)
5244 gfc_error ("RESULT variable at %C must be different than function name");
5245 return MATCH_ERROR;
5248 if (gfc_get_symbol (name, NULL, &r))
5249 return MATCH_ERROR;
5251 if (!gfc_add_result (&r->attr, r->name, NULL))
5252 return MATCH_ERROR;
5254 *result = r;
5256 return MATCH_YES;
5260 /* Match a function suffix, which could be a combination of a result
5261 clause and BIND(C), either one, or neither. The draft does not
5262 require them to come in a specific order. */
5264 match
5265 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
5267 match is_bind_c; /* Found bind(c). */
5268 match is_result; /* Found result clause. */
5269 match found_match; /* Status of whether we've found a good match. */
5270 char peek_char; /* Character we're going to peek at. */
5271 bool allow_binding_name;
5273 /* Initialize to having found nothing. */
5274 found_match = MATCH_NO;
5275 is_bind_c = MATCH_NO;
5276 is_result = MATCH_NO;
5278 /* Get the next char to narrow between result and bind(c). */
5279 gfc_gobble_whitespace ();
5280 peek_char = gfc_peek_ascii_char ();
5282 /* C binding names are not allowed for internal procedures. */
5283 if (gfc_current_state () == COMP_CONTAINS
5284 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5285 allow_binding_name = false;
5286 else
5287 allow_binding_name = true;
5289 switch (peek_char)
5291 case 'r':
5292 /* Look for result clause. */
5293 is_result = match_result (sym, result);
5294 if (is_result == MATCH_YES)
5296 /* Now see if there is a bind(c) after it. */
5297 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5298 /* We've found the result clause and possibly bind(c). */
5299 found_match = MATCH_YES;
5301 else
5302 /* This should only be MATCH_ERROR. */
5303 found_match = is_result;
5304 break;
5305 case 'b':
5306 /* Look for bind(c) first. */
5307 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5308 if (is_bind_c == MATCH_YES)
5310 /* Now see if a result clause followed it. */
5311 is_result = match_result (sym, result);
5312 found_match = MATCH_YES;
5314 else
5316 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
5317 found_match = MATCH_ERROR;
5319 break;
5320 default:
5321 gfc_error ("Unexpected junk after function declaration at %C");
5322 found_match = MATCH_ERROR;
5323 break;
5326 if (is_bind_c == MATCH_YES)
5328 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
5329 if (gfc_current_state () == COMP_CONTAINS
5330 && sym->ns->proc_name->attr.flavor != FL_MODULE
5331 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5332 "at %L may not be specified for an internal "
5333 "procedure", &gfc_current_locus))
5334 return MATCH_ERROR;
5336 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
5337 return MATCH_ERROR;
5340 return found_match;
5344 /* Procedure pointer return value without RESULT statement:
5345 Add "hidden" result variable named "ppr@". */
5347 static bool
5348 add_hidden_procptr_result (gfc_symbol *sym)
5350 bool case1,case2;
5352 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
5353 return false;
5355 /* First usage case: PROCEDURE and EXTERNAL statements. */
5356 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
5357 && strcmp (gfc_current_block ()->name, sym->name) == 0
5358 && sym->attr.external;
5359 /* Second usage case: INTERFACE statements. */
5360 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
5361 && gfc_state_stack->previous->state == COMP_FUNCTION
5362 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
5364 if (case1 || case2)
5366 gfc_symtree *stree;
5367 if (case1)
5368 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
5369 else if (case2)
5371 gfc_symtree *st2;
5372 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
5373 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
5374 st2->n.sym = stree->n.sym;
5376 sym->result = stree->n.sym;
5378 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
5379 sym->result->attr.pointer = sym->attr.pointer;
5380 sym->result->attr.external = sym->attr.external;
5381 sym->result->attr.referenced = sym->attr.referenced;
5382 sym->result->ts = sym->ts;
5383 sym->attr.proc_pointer = 0;
5384 sym->attr.pointer = 0;
5385 sym->attr.external = 0;
5386 if (sym->result->attr.external && sym->result->attr.pointer)
5388 sym->result->attr.pointer = 0;
5389 sym->result->attr.proc_pointer = 1;
5392 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
5394 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5395 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
5396 && sym->result && sym->result != sym && sym->result->attr.external
5397 && sym == gfc_current_ns->proc_name
5398 && sym == sym->result->ns->proc_name
5399 && strcmp ("ppr@", sym->result->name) == 0)
5401 sym->result->attr.proc_pointer = 1;
5402 sym->attr.pointer = 0;
5403 return true;
5405 else
5406 return false;
5410 /* Match the interface for a PROCEDURE declaration,
5411 including brackets (R1212). */
5413 static match
5414 match_procedure_interface (gfc_symbol **proc_if)
5416 match m;
5417 gfc_symtree *st;
5418 locus old_loc, entry_loc;
5419 gfc_namespace *old_ns = gfc_current_ns;
5420 char name[GFC_MAX_SYMBOL_LEN + 1];
5422 old_loc = entry_loc = gfc_current_locus;
5423 gfc_clear_ts (&current_ts);
5425 if (gfc_match (" (") != MATCH_YES)
5427 gfc_current_locus = entry_loc;
5428 return MATCH_NO;
5431 /* Get the type spec. for the procedure interface. */
5432 old_loc = gfc_current_locus;
5433 m = gfc_match_decl_type_spec (&current_ts, 0);
5434 gfc_gobble_whitespace ();
5435 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
5436 goto got_ts;
5438 if (m == MATCH_ERROR)
5439 return m;
5441 /* Procedure interface is itself a procedure. */
5442 gfc_current_locus = old_loc;
5443 m = gfc_match_name (name);
5445 /* First look to see if it is already accessible in the current
5446 namespace because it is use associated or contained. */
5447 st = NULL;
5448 if (gfc_find_sym_tree (name, NULL, 0, &st))
5449 return MATCH_ERROR;
5451 /* If it is still not found, then try the parent namespace, if it
5452 exists and create the symbol there if it is still not found. */
5453 if (gfc_current_ns->parent)
5454 gfc_current_ns = gfc_current_ns->parent;
5455 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5456 return MATCH_ERROR;
5458 gfc_current_ns = old_ns;
5459 *proc_if = st->n.sym;
5461 if (*proc_if)
5463 (*proc_if)->refs++;
5464 /* Resolve interface if possible. That way, attr.procedure is only set
5465 if it is declared by a later procedure-declaration-stmt, which is
5466 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5467 while ((*proc_if)->ts.interface
5468 && *proc_if != (*proc_if)->ts.interface)
5469 *proc_if = (*proc_if)->ts.interface;
5471 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5472 && (*proc_if)->ts.type == BT_UNKNOWN
5473 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5474 (*proc_if)->name, NULL))
5475 return MATCH_ERROR;
5478 got_ts:
5479 if (gfc_match (" )") != MATCH_YES)
5481 gfc_current_locus = entry_loc;
5482 return MATCH_NO;
5485 return MATCH_YES;
5489 /* Match a PROCEDURE declaration (R1211). */
5491 static match
5492 match_procedure_decl (void)
5494 match m;
5495 gfc_symbol *sym, *proc_if = NULL;
5496 int num;
5497 gfc_expr *initializer = NULL;
5499 /* Parse interface (with brackets). */
5500 m = match_procedure_interface (&proc_if);
5501 if (m != MATCH_YES)
5502 return m;
5504 /* Parse attributes (with colons). */
5505 m = match_attr_spec();
5506 if (m == MATCH_ERROR)
5507 return MATCH_ERROR;
5509 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5511 current_attr.is_bind_c = 1;
5512 has_name_equals = 0;
5513 curr_binding_label = NULL;
5516 /* Get procedure symbols. */
5517 for(num=1;;num++)
5519 m = gfc_match_symbol (&sym, 0);
5520 if (m == MATCH_NO)
5521 goto syntax;
5522 else if (m == MATCH_ERROR)
5523 return m;
5525 /* Add current_attr to the symbol attributes. */
5526 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
5527 return MATCH_ERROR;
5529 if (sym->attr.is_bind_c)
5531 /* Check for C1218. */
5532 if (!proc_if || !proc_if->attr.is_bind_c)
5534 gfc_error ("BIND(C) attribute at %C requires "
5535 "an interface with BIND(C)");
5536 return MATCH_ERROR;
5538 /* Check for C1217. */
5539 if (has_name_equals && sym->attr.pointer)
5541 gfc_error ("BIND(C) procedure with NAME may not have "
5542 "POINTER attribute at %C");
5543 return MATCH_ERROR;
5545 if (has_name_equals && sym->attr.dummy)
5547 gfc_error ("Dummy procedure at %C may not have "
5548 "BIND(C) attribute with NAME");
5549 return MATCH_ERROR;
5551 /* Set binding label for BIND(C). */
5552 if (!set_binding_label (&sym->binding_label, sym->name, num))
5553 return MATCH_ERROR;
5556 if (!gfc_add_external (&sym->attr, NULL))
5557 return MATCH_ERROR;
5559 if (add_hidden_procptr_result (sym))
5560 sym = sym->result;
5562 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
5563 return MATCH_ERROR;
5565 /* Set interface. */
5566 if (proc_if != NULL)
5568 if (sym->ts.type != BT_UNKNOWN)
5570 gfc_error ("Procedure %qs at %L already has basic type of %s",
5571 sym->name, &gfc_current_locus,
5572 gfc_basic_typename (sym->ts.type));
5573 return MATCH_ERROR;
5575 sym->ts.interface = proc_if;
5576 sym->attr.untyped = 1;
5577 sym->attr.if_source = IFSRC_IFBODY;
5579 else if (current_ts.type != BT_UNKNOWN)
5581 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
5582 return MATCH_ERROR;
5583 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5584 sym->ts.interface->ts = current_ts;
5585 sym->ts.interface->attr.flavor = FL_PROCEDURE;
5586 sym->ts.interface->attr.function = 1;
5587 sym->attr.function = 1;
5588 sym->attr.if_source = IFSRC_UNKNOWN;
5591 if (gfc_match (" =>") == MATCH_YES)
5593 if (!current_attr.pointer)
5595 gfc_error ("Initialization at %C isn't for a pointer variable");
5596 m = MATCH_ERROR;
5597 goto cleanup;
5600 m = match_pointer_init (&initializer, 1);
5601 if (m != MATCH_YES)
5602 goto cleanup;
5604 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
5605 goto cleanup;
5609 if (gfc_match_eos () == MATCH_YES)
5610 return MATCH_YES;
5611 if (gfc_match_char (',') != MATCH_YES)
5612 goto syntax;
5615 syntax:
5616 gfc_error ("Syntax error in PROCEDURE statement at %C");
5617 return MATCH_ERROR;
5619 cleanup:
5620 /* Free stuff up and return. */
5621 gfc_free_expr (initializer);
5622 return m;
5626 static match
5627 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5630 /* Match a procedure pointer component declaration (R445). */
5632 static match
5633 match_ppc_decl (void)
5635 match m;
5636 gfc_symbol *proc_if = NULL;
5637 gfc_typespec ts;
5638 int num;
5639 gfc_component *c;
5640 gfc_expr *initializer = NULL;
5641 gfc_typebound_proc* tb;
5642 char name[GFC_MAX_SYMBOL_LEN + 1];
5644 /* Parse interface (with brackets). */
5645 m = match_procedure_interface (&proc_if);
5646 if (m != MATCH_YES)
5647 goto syntax;
5649 /* Parse attributes. */
5650 tb = XCNEW (gfc_typebound_proc);
5651 tb->where = gfc_current_locus;
5652 m = match_binding_attributes (tb, false, true);
5653 if (m == MATCH_ERROR)
5654 return m;
5656 gfc_clear_attr (&current_attr);
5657 current_attr.procedure = 1;
5658 current_attr.proc_pointer = 1;
5659 current_attr.access = tb->access;
5660 current_attr.flavor = FL_PROCEDURE;
5662 /* Match the colons (required). */
5663 if (gfc_match (" ::") != MATCH_YES)
5665 gfc_error ("Expected %<::%> after binding-attributes at %C");
5666 return MATCH_ERROR;
5669 /* Check for C450. */
5670 if (!tb->nopass && proc_if == NULL)
5672 gfc_error("NOPASS or explicit interface required at %C");
5673 return MATCH_ERROR;
5676 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5677 return MATCH_ERROR;
5679 /* Match PPC names. */
5680 ts = current_ts;
5681 for(num=1;;num++)
5683 m = gfc_match_name (name);
5684 if (m == MATCH_NO)
5685 goto syntax;
5686 else if (m == MATCH_ERROR)
5687 return m;
5689 if (!gfc_add_component (gfc_current_block(), name, &c))
5690 return MATCH_ERROR;
5692 /* Add current_attr to the symbol attributes. */
5693 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5694 return MATCH_ERROR;
5696 if (!gfc_add_external (&c->attr, NULL))
5697 return MATCH_ERROR;
5699 if (!gfc_add_proc (&c->attr, name, NULL))
5700 return MATCH_ERROR;
5702 if (num == 1)
5703 c->tb = tb;
5704 else
5706 c->tb = XCNEW (gfc_typebound_proc);
5707 c->tb->where = gfc_current_locus;
5708 *c->tb = *tb;
5711 /* Set interface. */
5712 if (proc_if != NULL)
5714 c->ts.interface = proc_if;
5715 c->attr.untyped = 1;
5716 c->attr.if_source = IFSRC_IFBODY;
5718 else if (ts.type != BT_UNKNOWN)
5720 c->ts = ts;
5721 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5722 c->ts.interface->result = c->ts.interface;
5723 c->ts.interface->ts = ts;
5724 c->ts.interface->attr.flavor = FL_PROCEDURE;
5725 c->ts.interface->attr.function = 1;
5726 c->attr.function = 1;
5727 c->attr.if_source = IFSRC_UNKNOWN;
5730 if (gfc_match (" =>") == MATCH_YES)
5732 m = match_pointer_init (&initializer, 1);
5733 if (m != MATCH_YES)
5735 gfc_free_expr (initializer);
5736 return m;
5738 c->initializer = initializer;
5741 if (gfc_match_eos () == MATCH_YES)
5742 return MATCH_YES;
5743 if (gfc_match_char (',') != MATCH_YES)
5744 goto syntax;
5747 syntax:
5748 gfc_error ("Syntax error in procedure pointer component at %C");
5749 return MATCH_ERROR;
5753 /* Match a PROCEDURE declaration inside an interface (R1206). */
5755 static match
5756 match_procedure_in_interface (void)
5758 match m;
5759 gfc_symbol *sym;
5760 char name[GFC_MAX_SYMBOL_LEN + 1];
5761 locus old_locus;
5763 if (current_interface.type == INTERFACE_NAMELESS
5764 || current_interface.type == INTERFACE_ABSTRACT)
5766 gfc_error ("PROCEDURE at %C must be in a generic interface");
5767 return MATCH_ERROR;
5770 /* Check if the F2008 optional double colon appears. */
5771 gfc_gobble_whitespace ();
5772 old_locus = gfc_current_locus;
5773 if (gfc_match ("::") == MATCH_YES)
5775 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5776 "MODULE PROCEDURE statement at %L", &old_locus))
5777 return MATCH_ERROR;
5779 else
5780 gfc_current_locus = old_locus;
5782 for(;;)
5784 m = gfc_match_name (name);
5785 if (m == MATCH_NO)
5786 goto syntax;
5787 else if (m == MATCH_ERROR)
5788 return m;
5789 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5790 return MATCH_ERROR;
5792 if (!gfc_add_interface (sym))
5793 return MATCH_ERROR;
5795 if (gfc_match_eos () == MATCH_YES)
5796 break;
5797 if (gfc_match_char (',') != MATCH_YES)
5798 goto syntax;
5801 return MATCH_YES;
5803 syntax:
5804 gfc_error ("Syntax error in PROCEDURE statement at %C");
5805 return MATCH_ERROR;
5809 /* General matcher for PROCEDURE declarations. */
5811 static match match_procedure_in_type (void);
5813 match
5814 gfc_match_procedure (void)
5816 match m;
5818 switch (gfc_current_state ())
5820 case COMP_NONE:
5821 case COMP_PROGRAM:
5822 case COMP_MODULE:
5823 case COMP_SUBMODULE:
5824 case COMP_SUBROUTINE:
5825 case COMP_FUNCTION:
5826 case COMP_BLOCK:
5827 m = match_procedure_decl ();
5828 break;
5829 case COMP_INTERFACE:
5830 m = match_procedure_in_interface ();
5831 break;
5832 case COMP_DERIVED:
5833 m = match_ppc_decl ();
5834 break;
5835 case COMP_DERIVED_CONTAINS:
5836 m = match_procedure_in_type ();
5837 break;
5838 default:
5839 return MATCH_NO;
5842 if (m != MATCH_YES)
5843 return m;
5845 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5846 return MATCH_ERROR;
5848 return m;
5852 /* Warn if a matched procedure has the same name as an intrinsic; this is
5853 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5854 parser-state-stack to find out whether we're in a module. */
5856 static void
5857 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5859 bool in_module;
5861 in_module = (gfc_state_stack->previous
5862 && (gfc_state_stack->previous->state == COMP_MODULE
5863 || gfc_state_stack->previous->state == COMP_SUBMODULE));
5865 gfc_warn_intrinsic_shadow (sym, in_module, func);
5869 /* Match a function declaration. */
5871 match
5872 gfc_match_function_decl (void)
5874 char name[GFC_MAX_SYMBOL_LEN + 1];
5875 gfc_symbol *sym, *result;
5876 locus old_loc;
5877 match m;
5878 match suffix_match;
5879 match found_match; /* Status returned by match func. */
5881 if (gfc_current_state () != COMP_NONE
5882 && gfc_current_state () != COMP_INTERFACE
5883 && gfc_current_state () != COMP_CONTAINS)
5884 return MATCH_NO;
5886 gfc_clear_ts (&current_ts);
5888 old_loc = gfc_current_locus;
5890 m = gfc_match_prefix (&current_ts);
5891 if (m != MATCH_YES)
5893 gfc_current_locus = old_loc;
5894 return m;
5897 if (gfc_match ("function% %n", name) != MATCH_YES)
5899 gfc_current_locus = old_loc;
5900 return MATCH_NO;
5903 if (get_proc_name (name, &sym, false))
5904 return MATCH_ERROR;
5906 if (add_hidden_procptr_result (sym))
5907 sym = sym->result;
5909 if (current_attr.module_procedure)
5910 sym->attr.module_procedure = 1;
5912 gfc_new_block = sym;
5914 m = gfc_match_formal_arglist (sym, 0, 0);
5915 if (m == MATCH_NO)
5917 gfc_error ("Expected formal argument list in function "
5918 "definition at %C");
5919 m = MATCH_ERROR;
5920 goto cleanup;
5922 else if (m == MATCH_ERROR)
5923 goto cleanup;
5925 result = NULL;
5927 /* According to the draft, the bind(c) and result clause can
5928 come in either order after the formal_arg_list (i.e., either
5929 can be first, both can exist together or by themselves or neither
5930 one). Therefore, the match_result can't match the end of the
5931 string, and check for the bind(c) or result clause in either order. */
5932 found_match = gfc_match_eos ();
5934 /* Make sure that it isn't already declared as BIND(C). If it is, it
5935 must have been marked BIND(C) with a BIND(C) attribute and that is
5936 not allowed for procedures. */
5937 if (sym->attr.is_bind_c == 1)
5939 sym->attr.is_bind_c = 0;
5940 if (sym->old_symbol != NULL)
5941 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5942 "variables or common blocks",
5943 &(sym->old_symbol->declared_at));
5944 else
5945 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5946 "variables or common blocks", &gfc_current_locus);
5949 if (found_match != MATCH_YES)
5951 /* If we haven't found the end-of-statement, look for a suffix. */
5952 suffix_match = gfc_match_suffix (sym, &result);
5953 if (suffix_match == MATCH_YES)
5954 /* Need to get the eos now. */
5955 found_match = gfc_match_eos ();
5956 else
5957 found_match = suffix_match;
5960 if(found_match != MATCH_YES)
5961 m = MATCH_ERROR;
5962 else
5964 /* Make changes to the symbol. */
5965 m = MATCH_ERROR;
5967 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5968 goto cleanup;
5970 if (!gfc_missing_attr (&sym->attr, NULL))
5971 goto cleanup;
5973 if (!copy_prefix (&sym->attr, &sym->declared_at))
5975 if(!sym->attr.module_procedure)
5976 goto cleanup;
5977 else
5978 gfc_error_check ();
5981 /* Delay matching the function characteristics until after the
5982 specification block by signalling kind=-1. */
5983 sym->declared_at = old_loc;
5984 if (current_ts.type != BT_UNKNOWN)
5985 current_ts.kind = -1;
5986 else
5987 current_ts.kind = 0;
5989 if (result == NULL)
5991 if (current_ts.type != BT_UNKNOWN
5992 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5993 goto cleanup;
5994 sym->result = sym;
5996 else
5998 if (current_ts.type != BT_UNKNOWN
5999 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
6000 goto cleanup;
6001 sym->result = result;
6005 /* Warn if this procedure has the same name as an intrinsic. */
6006 do_warn_intrinsic_shadow (sym, true);
6008 return MATCH_YES;
6011 cleanup:
6012 gfc_current_locus = old_loc;
6013 return m;
6017 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
6018 pass the name of the entry, rather than the gfc_current_block name, and
6019 to return false upon finding an existing global entry. */
6021 static bool
6022 add_global_entry (const char *name, const char *binding_label, bool sub,
6023 locus *where)
6025 gfc_gsymbol *s;
6026 enum gfc_symbol_type type;
6028 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
6030 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
6031 name is a global identifier. */
6032 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
6034 s = gfc_get_gsymbol (name);
6036 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
6038 gfc_global_used (s, where);
6039 return false;
6041 else
6043 s->type = type;
6044 s->sym_name = name;
6045 s->where = *where;
6046 s->defined = 1;
6047 s->ns = gfc_current_ns;
6051 /* Don't add the symbol multiple times. */
6052 if (binding_label
6053 && (!gfc_notification_std (GFC_STD_F2008)
6054 || strcmp (name, binding_label) != 0))
6056 s = gfc_get_gsymbol (binding_label);
6058 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
6060 gfc_global_used (s, where);
6061 return false;
6063 else
6065 s->type = type;
6066 s->sym_name = name;
6067 s->binding_label = binding_label;
6068 s->where = *where;
6069 s->defined = 1;
6070 s->ns = gfc_current_ns;
6074 return true;
6078 /* Match an ENTRY statement. */
6080 match
6081 gfc_match_entry (void)
6083 gfc_symbol *proc;
6084 gfc_symbol *result;
6085 gfc_symbol *entry;
6086 char name[GFC_MAX_SYMBOL_LEN + 1];
6087 gfc_compile_state state;
6088 match m;
6089 gfc_entry_list *el;
6090 locus old_loc;
6091 bool module_procedure;
6092 char peek_char;
6093 match is_bind_c;
6095 m = gfc_match_name (name);
6096 if (m != MATCH_YES)
6097 return m;
6099 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
6100 return MATCH_ERROR;
6102 state = gfc_current_state ();
6103 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
6105 switch (state)
6107 case COMP_PROGRAM:
6108 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
6109 break;
6110 case COMP_MODULE:
6111 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
6112 break;
6113 case COMP_SUBMODULE:
6114 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
6115 break;
6116 case COMP_BLOCK_DATA:
6117 gfc_error ("ENTRY statement at %C cannot appear within "
6118 "a BLOCK DATA");
6119 break;
6120 case COMP_INTERFACE:
6121 gfc_error ("ENTRY statement at %C cannot appear within "
6122 "an INTERFACE");
6123 break;
6124 case COMP_STRUCTURE:
6125 gfc_error ("ENTRY statement at %C cannot appear within "
6126 "a STRUCTURE block");
6127 break;
6128 case COMP_DERIVED:
6129 gfc_error ("ENTRY statement at %C cannot appear within "
6130 "a DERIVED TYPE block");
6131 break;
6132 case COMP_IF:
6133 gfc_error ("ENTRY statement at %C cannot appear within "
6134 "an IF-THEN block");
6135 break;
6136 case COMP_DO:
6137 case COMP_DO_CONCURRENT:
6138 gfc_error ("ENTRY statement at %C cannot appear within "
6139 "a DO block");
6140 break;
6141 case COMP_SELECT:
6142 gfc_error ("ENTRY statement at %C cannot appear within "
6143 "a SELECT block");
6144 break;
6145 case COMP_FORALL:
6146 gfc_error ("ENTRY statement at %C cannot appear within "
6147 "a FORALL block");
6148 break;
6149 case COMP_WHERE:
6150 gfc_error ("ENTRY statement at %C cannot appear within "
6151 "a WHERE block");
6152 break;
6153 case COMP_CONTAINS:
6154 gfc_error ("ENTRY statement at %C cannot appear within "
6155 "a contained subprogram");
6156 break;
6157 default:
6158 gfc_error ("Unexpected ENTRY statement at %C");
6160 return MATCH_ERROR;
6163 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
6164 && gfc_state_stack->previous->state == COMP_INTERFACE)
6166 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
6167 return MATCH_ERROR;
6170 module_procedure = gfc_current_ns->parent != NULL
6171 && gfc_current_ns->parent->proc_name
6172 && gfc_current_ns->parent->proc_name->attr.flavor
6173 == FL_MODULE;
6175 if (gfc_current_ns->parent != NULL
6176 && gfc_current_ns->parent->proc_name
6177 && !module_procedure)
6179 gfc_error("ENTRY statement at %C cannot appear in a "
6180 "contained procedure");
6181 return MATCH_ERROR;
6184 /* Module function entries need special care in get_proc_name
6185 because previous references within the function will have
6186 created symbols attached to the current namespace. */
6187 if (get_proc_name (name, &entry,
6188 gfc_current_ns->parent != NULL
6189 && module_procedure))
6190 return MATCH_ERROR;
6192 proc = gfc_current_block ();
6194 /* Make sure that it isn't already declared as BIND(C). If it is, it
6195 must have been marked BIND(C) with a BIND(C) attribute and that is
6196 not allowed for procedures. */
6197 if (entry->attr.is_bind_c == 1)
6199 entry->attr.is_bind_c = 0;
6200 if (entry->old_symbol != NULL)
6201 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6202 "variables or common blocks",
6203 &(entry->old_symbol->declared_at));
6204 else
6205 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6206 "variables or common blocks", &gfc_current_locus);
6209 /* Check what next non-whitespace character is so we can tell if there
6210 is the required parens if we have a BIND(C). */
6211 old_loc = gfc_current_locus;
6212 gfc_gobble_whitespace ();
6213 peek_char = gfc_peek_ascii_char ();
6215 if (state == COMP_SUBROUTINE)
6217 m = gfc_match_formal_arglist (entry, 0, 1);
6218 if (m != MATCH_YES)
6219 return MATCH_ERROR;
6221 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
6222 never be an internal procedure. */
6223 is_bind_c = gfc_match_bind_c (entry, true);
6224 if (is_bind_c == MATCH_ERROR)
6225 return MATCH_ERROR;
6226 if (is_bind_c == MATCH_YES)
6228 if (peek_char != '(')
6230 gfc_error ("Missing required parentheses before BIND(C) at %C");
6231 return MATCH_ERROR;
6233 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
6234 &(entry->declared_at), 1))
6235 return MATCH_ERROR;
6238 if (!gfc_current_ns->parent
6239 && !add_global_entry (name, entry->binding_label, true,
6240 &old_loc))
6241 return MATCH_ERROR;
6243 /* An entry in a subroutine. */
6244 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
6245 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
6246 return MATCH_ERROR;
6248 else
6250 /* An entry in a function.
6251 We need to take special care because writing
6252 ENTRY f()
6254 ENTRY f
6255 is allowed, whereas
6256 ENTRY f() RESULT (r)
6257 can't be written as
6258 ENTRY f RESULT (r). */
6259 if (gfc_match_eos () == MATCH_YES)
6261 gfc_current_locus = old_loc;
6262 /* Match the empty argument list, and add the interface to
6263 the symbol. */
6264 m = gfc_match_formal_arglist (entry, 0, 1);
6266 else
6267 m = gfc_match_formal_arglist (entry, 0, 0);
6269 if (m != MATCH_YES)
6270 return MATCH_ERROR;
6272 result = NULL;
6274 if (gfc_match_eos () == MATCH_YES)
6276 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
6277 || !gfc_add_function (&entry->attr, entry->name, NULL))
6278 return MATCH_ERROR;
6280 entry->result = entry;
6282 else
6284 m = gfc_match_suffix (entry, &result);
6285 if (m == MATCH_NO)
6286 gfc_syntax_error (ST_ENTRY);
6287 if (m != MATCH_YES)
6288 return MATCH_ERROR;
6290 if (result)
6292 if (!gfc_add_result (&result->attr, result->name, NULL)
6293 || !gfc_add_entry (&entry->attr, result->name, NULL)
6294 || !gfc_add_function (&entry->attr, result->name, NULL))
6295 return MATCH_ERROR;
6296 entry->result = result;
6298 else
6300 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
6301 || !gfc_add_function (&entry->attr, entry->name, NULL))
6302 return MATCH_ERROR;
6303 entry->result = entry;
6307 if (!gfc_current_ns->parent
6308 && !add_global_entry (name, entry->binding_label, false,
6309 &old_loc))
6310 return MATCH_ERROR;
6313 if (gfc_match_eos () != MATCH_YES)
6315 gfc_syntax_error (ST_ENTRY);
6316 return MATCH_ERROR;
6319 entry->attr.recursive = proc->attr.recursive;
6320 entry->attr.elemental = proc->attr.elemental;
6321 entry->attr.pure = proc->attr.pure;
6323 el = gfc_get_entry_list ();
6324 el->sym = entry;
6325 el->next = gfc_current_ns->entries;
6326 gfc_current_ns->entries = el;
6327 if (el->next)
6328 el->id = el->next->id + 1;
6329 else
6330 el->id = 1;
6332 new_st.op = EXEC_ENTRY;
6333 new_st.ext.entry = el;
6335 return MATCH_YES;
6339 /* Match a subroutine statement, including optional prefixes. */
6341 match
6342 gfc_match_subroutine (void)
6344 char name[GFC_MAX_SYMBOL_LEN + 1];
6345 gfc_symbol *sym;
6346 match m;
6347 match is_bind_c;
6348 char peek_char;
6349 bool allow_binding_name;
6351 if (gfc_current_state () != COMP_NONE
6352 && gfc_current_state () != COMP_INTERFACE
6353 && gfc_current_state () != COMP_CONTAINS)
6354 return MATCH_NO;
6356 m = gfc_match_prefix (NULL);
6357 if (m != MATCH_YES)
6358 return m;
6360 m = gfc_match ("subroutine% %n", name);
6361 if (m != MATCH_YES)
6362 return m;
6364 if (get_proc_name (name, &sym, false))
6365 return MATCH_ERROR;
6367 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
6368 the symbol existed before. */
6369 sym->declared_at = gfc_current_locus;
6371 if (current_attr.module_procedure)
6372 sym->attr.module_procedure = 1;
6374 if (add_hidden_procptr_result (sym))
6375 sym = sym->result;
6377 gfc_new_block = sym;
6379 /* Check what next non-whitespace character is so we can tell if there
6380 is the required parens if we have a BIND(C). */
6381 gfc_gobble_whitespace ();
6382 peek_char = gfc_peek_ascii_char ();
6384 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
6385 return MATCH_ERROR;
6387 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
6388 return MATCH_ERROR;
6390 /* Make sure that it isn't already declared as BIND(C). If it is, it
6391 must have been marked BIND(C) with a BIND(C) attribute and that is
6392 not allowed for procedures. */
6393 if (sym->attr.is_bind_c == 1)
6395 sym->attr.is_bind_c = 0;
6396 if (sym->old_symbol != NULL)
6397 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6398 "variables or common blocks",
6399 &(sym->old_symbol->declared_at));
6400 else
6401 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6402 "variables or common blocks", &gfc_current_locus);
6405 /* C binding names are not allowed for internal procedures. */
6406 if (gfc_current_state () == COMP_CONTAINS
6407 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6408 allow_binding_name = false;
6409 else
6410 allow_binding_name = true;
6412 /* Here, we are just checking if it has the bind(c) attribute, and if
6413 so, then we need to make sure it's all correct. If it doesn't,
6414 we still need to continue matching the rest of the subroutine line. */
6415 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6416 if (is_bind_c == MATCH_ERROR)
6418 /* There was an attempt at the bind(c), but it was wrong. An
6419 error message should have been printed w/in the gfc_match_bind_c
6420 so here we'll just return the MATCH_ERROR. */
6421 return MATCH_ERROR;
6424 if (is_bind_c == MATCH_YES)
6426 /* The following is allowed in the Fortran 2008 draft. */
6427 if (gfc_current_state () == COMP_CONTAINS
6428 && sym->ns->proc_name->attr.flavor != FL_MODULE
6429 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6430 "at %L may not be specified for an internal "
6431 "procedure", &gfc_current_locus))
6432 return MATCH_ERROR;
6434 if (peek_char != '(')
6436 gfc_error ("Missing required parentheses before BIND(C) at %C");
6437 return MATCH_ERROR;
6439 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
6440 &(sym->declared_at), 1))
6441 return MATCH_ERROR;
6444 if (gfc_match_eos () != MATCH_YES)
6446 gfc_syntax_error (ST_SUBROUTINE);
6447 return MATCH_ERROR;
6450 if (!copy_prefix (&sym->attr, &sym->declared_at))
6452 if(!sym->attr.module_procedure)
6453 return MATCH_ERROR;
6454 else
6455 gfc_error_check ();
6458 /* Warn if it has the same name as an intrinsic. */
6459 do_warn_intrinsic_shadow (sym, false);
6461 return MATCH_YES;
6465 /* Check that the NAME identifier in a BIND attribute or statement
6466 is conform to C identifier rules. */
6468 match
6469 check_bind_name_identifier (char **name)
6471 char *n = *name, *p;
6473 /* Remove leading spaces. */
6474 while (*n == ' ')
6475 n++;
6477 /* On an empty string, free memory and set name to NULL. */
6478 if (*n == '\0')
6480 free (*name);
6481 *name = NULL;
6482 return MATCH_YES;
6485 /* Remove trailing spaces. */
6486 p = n + strlen(n) - 1;
6487 while (*p == ' ')
6488 *(p--) = '\0';
6490 /* Insert the identifier into the symbol table. */
6491 p = xstrdup (n);
6492 free (*name);
6493 *name = p;
6495 /* Now check that identifier is valid under C rules. */
6496 if (ISDIGIT (*p))
6498 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6499 return MATCH_ERROR;
6502 for (; *p; p++)
6503 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6505 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6506 return MATCH_ERROR;
6509 return MATCH_YES;
6513 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6514 given, and set the binding label in either the given symbol (if not
6515 NULL), or in the current_ts. The symbol may be NULL because we may
6516 encounter the BIND(C) before the declaration itself. Return
6517 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6518 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6519 or MATCH_YES if the specifier was correct and the binding label and
6520 bind(c) fields were set correctly for the given symbol or the
6521 current_ts. If allow_binding_name is false, no binding name may be
6522 given. */
6524 match
6525 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
6527 char *binding_label = NULL;
6528 gfc_expr *e = NULL;
6530 /* Initialize the flag that specifies whether we encountered a NAME=
6531 specifier or not. */
6532 has_name_equals = 0;
6534 /* This much we have to be able to match, in this order, if
6535 there is a bind(c) label. */
6536 if (gfc_match (" bind ( c ") != MATCH_YES)
6537 return MATCH_NO;
6539 /* Now see if there is a binding label, or if we've reached the
6540 end of the bind(c) attribute without one. */
6541 if (gfc_match_char (',') == MATCH_YES)
6543 if (gfc_match (" name = ") != MATCH_YES)
6545 gfc_error ("Syntax error in NAME= specifier for binding label "
6546 "at %C");
6547 /* should give an error message here */
6548 return MATCH_ERROR;
6551 has_name_equals = 1;
6553 if (gfc_match_init_expr (&e) != MATCH_YES)
6555 gfc_free_expr (e);
6556 return MATCH_ERROR;
6559 if (!gfc_simplify_expr(e, 0))
6561 gfc_error ("NAME= specifier at %C should be a constant expression");
6562 gfc_free_expr (e);
6563 return MATCH_ERROR;
6566 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6567 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
6569 gfc_error ("NAME= specifier at %C should be a scalar of "
6570 "default character kind");
6571 gfc_free_expr(e);
6572 return MATCH_ERROR;
6575 // Get a C string from the Fortran string constant
6576 binding_label = gfc_widechar_to_char (e->value.character.string,
6577 e->value.character.length);
6578 gfc_free_expr(e);
6580 // Check that it is valid (old gfc_match_name_C)
6581 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6582 return MATCH_ERROR;
6585 /* Get the required right paren. */
6586 if (gfc_match_char (')') != MATCH_YES)
6588 gfc_error ("Missing closing paren for binding label at %C");
6589 return MATCH_ERROR;
6592 if (has_name_equals && !allow_binding_name)
6594 gfc_error ("No binding name is allowed in BIND(C) at %C");
6595 return MATCH_ERROR;
6598 if (has_name_equals && sym != NULL && sym->attr.dummy)
6600 gfc_error ("For dummy procedure %s, no binding name is "
6601 "allowed in BIND(C) at %C", sym->name);
6602 return MATCH_ERROR;
6606 /* Save the binding label to the symbol. If sym is null, we're
6607 probably matching the typespec attributes of a declaration and
6608 haven't gotten the name yet, and therefore, no symbol yet. */
6609 if (binding_label)
6611 if (sym != NULL)
6612 sym->binding_label = binding_label;
6613 else
6614 curr_binding_label = binding_label;
6616 else if (allow_binding_name)
6618 /* No binding label, but if symbol isn't null, we
6619 can set the label for it here.
6620 If name="" or allow_binding_name is false, no C binding name is
6621 created. */
6622 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
6623 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
6626 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6627 && current_interface.type == INTERFACE_ABSTRACT)
6629 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6630 return MATCH_ERROR;
6633 return MATCH_YES;
6637 /* Return nonzero if we're currently compiling a contained procedure. */
6639 static int
6640 contained_procedure (void)
6642 gfc_state_data *s = gfc_state_stack;
6644 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6645 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6646 return 1;
6648 return 0;
6651 /* Set the kind of each enumerator. The kind is selected such that it is
6652 interoperable with the corresponding C enumeration type, making
6653 sure that -fshort-enums is honored. */
6655 static void
6656 set_enum_kind(void)
6658 enumerator_history *current_history = NULL;
6659 int kind;
6660 int i;
6662 if (max_enum == NULL || enum_history == NULL)
6663 return;
6665 if (!flag_short_enums)
6666 return;
6668 i = 0;
6671 kind = gfc_integer_kinds[i++].kind;
6673 while (kind < gfc_c_int_kind
6674 && gfc_check_integer_range (max_enum->initializer->value.integer,
6675 kind) != ARITH_OK);
6677 current_history = enum_history;
6678 while (current_history != NULL)
6680 current_history->sym->ts.kind = kind;
6681 current_history = current_history->next;
6686 /* Match any of the various end-block statements. Returns the type of
6687 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6688 and END BLOCK statements cannot be replaced by a single END statement. */
6690 match
6691 gfc_match_end (gfc_statement *st)
6693 char name[GFC_MAX_SYMBOL_LEN + 1];
6694 gfc_compile_state state;
6695 locus old_loc;
6696 const char *block_name;
6697 const char *target;
6698 int eos_ok;
6699 match m;
6700 gfc_namespace *parent_ns, *ns, *prev_ns;
6701 gfc_namespace **nsp;
6702 bool abreviated_modproc_decl;
6703 bool got_matching_end = false;
6705 old_loc = gfc_current_locus;
6706 if (gfc_match ("end") != MATCH_YES)
6707 return MATCH_NO;
6709 state = gfc_current_state ();
6710 block_name = gfc_current_block () == NULL
6711 ? NULL : gfc_current_block ()->name;
6713 switch (state)
6715 case COMP_ASSOCIATE:
6716 case COMP_BLOCK:
6717 if (!strncmp (block_name, "block@", strlen("block@")))
6718 block_name = NULL;
6719 break;
6721 case COMP_CONTAINS:
6722 case COMP_DERIVED_CONTAINS:
6723 state = gfc_state_stack->previous->state;
6724 block_name = gfc_state_stack->previous->sym == NULL
6725 ? NULL : gfc_state_stack->previous->sym->name;
6726 break;
6728 default:
6729 break;
6732 abreviated_modproc_decl
6733 = gfc_current_block ()
6734 && gfc_current_block ()->abr_modproc_decl;
6736 switch (state)
6738 case COMP_NONE:
6739 case COMP_PROGRAM:
6740 *st = ST_END_PROGRAM;
6741 target = " program";
6742 eos_ok = 1;
6743 break;
6745 case COMP_SUBROUTINE:
6746 *st = ST_END_SUBROUTINE;
6747 if (!abreviated_modproc_decl)
6748 target = " subroutine";
6749 else
6750 target = " procedure";
6751 eos_ok = !contained_procedure ();
6752 break;
6754 case COMP_FUNCTION:
6755 *st = ST_END_FUNCTION;
6756 if (!abreviated_modproc_decl)
6757 target = " function";
6758 else
6759 target = " procedure";
6760 eos_ok = !contained_procedure ();
6761 break;
6763 case COMP_BLOCK_DATA:
6764 *st = ST_END_BLOCK_DATA;
6765 target = " block data";
6766 eos_ok = 1;
6767 break;
6769 case COMP_MODULE:
6770 *st = ST_END_MODULE;
6771 target = " module";
6772 eos_ok = 1;
6773 break;
6775 case COMP_SUBMODULE:
6776 *st = ST_END_SUBMODULE;
6777 target = " submodule";
6778 eos_ok = 1;
6779 break;
6781 case COMP_INTERFACE:
6782 *st = ST_END_INTERFACE;
6783 target = " interface";
6784 eos_ok = 0;
6785 break;
6787 case COMP_MAP:
6788 *st = ST_END_MAP;
6789 target = " map";
6790 eos_ok = 0;
6791 break;
6793 case COMP_UNION:
6794 *st = ST_END_UNION;
6795 target = " union";
6796 eos_ok = 0;
6797 break;
6799 case COMP_STRUCTURE:
6800 *st = ST_END_STRUCTURE;
6801 target = " structure";
6802 eos_ok = 0;
6803 break;
6805 case COMP_DERIVED:
6806 case COMP_DERIVED_CONTAINS:
6807 *st = ST_END_TYPE;
6808 target = " type";
6809 eos_ok = 0;
6810 break;
6812 case COMP_ASSOCIATE:
6813 *st = ST_END_ASSOCIATE;
6814 target = " associate";
6815 eos_ok = 0;
6816 break;
6818 case COMP_BLOCK:
6819 *st = ST_END_BLOCK;
6820 target = " block";
6821 eos_ok = 0;
6822 break;
6824 case COMP_IF:
6825 *st = ST_ENDIF;
6826 target = " if";
6827 eos_ok = 0;
6828 break;
6830 case COMP_DO:
6831 case COMP_DO_CONCURRENT:
6832 *st = ST_ENDDO;
6833 target = " do";
6834 eos_ok = 0;
6835 break;
6837 case COMP_CRITICAL:
6838 *st = ST_END_CRITICAL;
6839 target = " critical";
6840 eos_ok = 0;
6841 break;
6843 case COMP_SELECT:
6844 case COMP_SELECT_TYPE:
6845 *st = ST_END_SELECT;
6846 target = " select";
6847 eos_ok = 0;
6848 break;
6850 case COMP_FORALL:
6851 *st = ST_END_FORALL;
6852 target = " forall";
6853 eos_ok = 0;
6854 break;
6856 case COMP_WHERE:
6857 *st = ST_END_WHERE;
6858 target = " where";
6859 eos_ok = 0;
6860 break;
6862 case COMP_ENUM:
6863 *st = ST_END_ENUM;
6864 target = " enum";
6865 eos_ok = 0;
6866 last_initializer = NULL;
6867 set_enum_kind ();
6868 gfc_free_enum_history ();
6869 break;
6871 default:
6872 gfc_error ("Unexpected END statement at %C");
6873 goto cleanup;
6876 old_loc = gfc_current_locus;
6877 if (gfc_match_eos () == MATCH_YES)
6879 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6881 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6882 "instead of %s statement at %L",
6883 abreviated_modproc_decl ? "END PROCEDURE"
6884 : gfc_ascii_statement(*st), &old_loc))
6885 goto cleanup;
6887 else if (!eos_ok)
6889 /* We would have required END [something]. */
6890 gfc_error ("%s statement expected at %L",
6891 gfc_ascii_statement (*st), &old_loc);
6892 goto cleanup;
6895 return MATCH_YES;
6898 /* Verify that we've got the sort of end-block that we're expecting. */
6899 if (gfc_match (target) != MATCH_YES)
6901 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6902 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6903 goto cleanup;
6905 else
6906 got_matching_end = true;
6908 old_loc = gfc_current_locus;
6909 /* If we're at the end, make sure a block name wasn't required. */
6910 if (gfc_match_eos () == MATCH_YES)
6913 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6914 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6915 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6916 return MATCH_YES;
6918 if (!block_name)
6919 return MATCH_YES;
6921 gfc_error ("Expected block name of %qs in %s statement at %L",
6922 block_name, gfc_ascii_statement (*st), &old_loc);
6924 return MATCH_ERROR;
6927 /* END INTERFACE has a special handler for its several possible endings. */
6928 if (*st == ST_END_INTERFACE)
6929 return gfc_match_end_interface ();
6931 /* We haven't hit the end of statement, so what is left must be an
6932 end-name. */
6933 m = gfc_match_space ();
6934 if (m == MATCH_YES)
6935 m = gfc_match_name (name);
6937 if (m == MATCH_NO)
6938 gfc_error ("Expected terminating name at %C");
6939 if (m != MATCH_YES)
6940 goto cleanup;
6942 if (block_name == NULL)
6943 goto syntax;
6945 /* We have to pick out the declared submodule name from the composite
6946 required by F2008:11.2.3 para 2, which ends in the declared name. */
6947 if (state == COMP_SUBMODULE)
6948 block_name = strchr (block_name, '.') + 1;
6950 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6952 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6953 gfc_ascii_statement (*st));
6954 goto cleanup;
6956 /* Procedure pointer as function result. */
6957 else if (strcmp (block_name, "ppr@") == 0
6958 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6960 gfc_error ("Expected label %qs for %s statement at %C",
6961 gfc_current_block ()->ns->proc_name->name,
6962 gfc_ascii_statement (*st));
6963 goto cleanup;
6966 if (gfc_match_eos () == MATCH_YES)
6967 return MATCH_YES;
6969 syntax:
6970 gfc_syntax_error (*st);
6972 cleanup:
6973 gfc_current_locus = old_loc;
6975 /* If we are missing an END BLOCK, we created a half-ready namespace.
6976 Remove it from the parent namespace's sibling list. */
6978 while (state == COMP_BLOCK && !got_matching_end)
6980 parent_ns = gfc_current_ns->parent;
6982 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6984 prev_ns = NULL;
6985 ns = *nsp;
6986 while (ns)
6988 if (ns == gfc_current_ns)
6990 if (prev_ns == NULL)
6991 *nsp = NULL;
6992 else
6993 prev_ns->sibling = ns->sibling;
6995 prev_ns = ns;
6996 ns = ns->sibling;
6999 gfc_free_namespace (gfc_current_ns);
7000 gfc_current_ns = parent_ns;
7001 gfc_state_stack = gfc_state_stack->previous;
7002 state = gfc_current_state ();
7005 return MATCH_ERROR;
7010 /***************** Attribute declaration statements ****************/
7012 /* Set the attribute of a single variable. */
7014 static match
7015 attr_decl1 (void)
7017 char name[GFC_MAX_SYMBOL_LEN + 1];
7018 gfc_array_spec *as;
7020 /* Workaround -Wmaybe-uninitialized false positive during
7021 profiledbootstrap by initializing them. */
7022 gfc_symbol *sym = NULL;
7023 locus var_locus;
7024 match m;
7026 as = NULL;
7028 m = gfc_match_name (name);
7029 if (m != MATCH_YES)
7030 goto cleanup;
7032 if (find_special (name, &sym, false))
7033 return MATCH_ERROR;
7035 if (!check_function_name (name))
7037 m = MATCH_ERROR;
7038 goto cleanup;
7041 var_locus = gfc_current_locus;
7043 /* Deal with possible array specification for certain attributes. */
7044 if (current_attr.dimension
7045 || current_attr.codimension
7046 || current_attr.allocatable
7047 || current_attr.pointer
7048 || current_attr.target)
7050 m = gfc_match_array_spec (&as, !current_attr.codimension,
7051 !current_attr.dimension
7052 && !current_attr.pointer
7053 && !current_attr.target);
7054 if (m == MATCH_ERROR)
7055 goto cleanup;
7057 if (current_attr.dimension && m == MATCH_NO)
7059 gfc_error ("Missing array specification at %L in DIMENSION "
7060 "statement", &var_locus);
7061 m = MATCH_ERROR;
7062 goto cleanup;
7065 if (current_attr.dimension && sym->value)
7067 gfc_error ("Dimensions specified for %s at %L after its "
7068 "initialisation", sym->name, &var_locus);
7069 m = MATCH_ERROR;
7070 goto cleanup;
7073 if (current_attr.codimension && m == MATCH_NO)
7075 gfc_error ("Missing array specification at %L in CODIMENSION "
7076 "statement", &var_locus);
7077 m = MATCH_ERROR;
7078 goto cleanup;
7081 if ((current_attr.allocatable || current_attr.pointer)
7082 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
7084 gfc_error ("Array specification must be deferred at %L", &var_locus);
7085 m = MATCH_ERROR;
7086 goto cleanup;
7090 /* Update symbol table. DIMENSION attribute is set in
7091 gfc_set_array_spec(). For CLASS variables, this must be applied
7092 to the first component, or '_data' field. */
7093 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
7095 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
7097 m = MATCH_ERROR;
7098 goto cleanup;
7101 else
7103 if (current_attr.dimension == 0 && current_attr.codimension == 0
7104 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
7106 m = MATCH_ERROR;
7107 goto cleanup;
7111 if (sym->ts.type == BT_CLASS
7112 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
7114 m = MATCH_ERROR;
7115 goto cleanup;
7118 if (!gfc_set_array_spec (sym, as, &var_locus))
7120 m = MATCH_ERROR;
7121 goto cleanup;
7124 if (sym->attr.cray_pointee && sym->as != NULL)
7126 /* Fix the array spec. */
7127 m = gfc_mod_pointee_as (sym->as);
7128 if (m == MATCH_ERROR)
7129 goto cleanup;
7132 if (!gfc_add_attribute (&sym->attr, &var_locus))
7134 m = MATCH_ERROR;
7135 goto cleanup;
7138 if ((current_attr.external || current_attr.intrinsic)
7139 && sym->attr.flavor != FL_PROCEDURE
7140 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
7142 m = MATCH_ERROR;
7143 goto cleanup;
7146 add_hidden_procptr_result (sym);
7148 return MATCH_YES;
7150 cleanup:
7151 gfc_free_array_spec (as);
7152 return m;
7156 /* Generic attribute declaration subroutine. Used for attributes that
7157 just have a list of names. */
7159 static match
7160 attr_decl (void)
7162 match m;
7164 /* Gobble the optional double colon, by simply ignoring the result
7165 of gfc_match(). */
7166 gfc_match (" ::");
7168 for (;;)
7170 m = attr_decl1 ();
7171 if (m != MATCH_YES)
7172 break;
7174 if (gfc_match_eos () == MATCH_YES)
7176 m = MATCH_YES;
7177 break;
7180 if (gfc_match_char (',') != MATCH_YES)
7182 gfc_error ("Unexpected character in variable list at %C");
7183 m = MATCH_ERROR;
7184 break;
7188 return m;
7192 /* This routine matches Cray Pointer declarations of the form:
7193 pointer ( <pointer>, <pointee> )
7195 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
7196 The pointer, if already declared, should be an integer. Otherwise, we
7197 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
7198 be either a scalar, or an array declaration. No space is allocated for
7199 the pointee. For the statement
7200 pointer (ipt, ar(10))
7201 any subsequent uses of ar will be translated (in C-notation) as
7202 ar(i) => ((<type> *) ipt)(i)
7203 After gimplification, pointee variable will disappear in the code. */
7205 static match
7206 cray_pointer_decl (void)
7208 match m;
7209 gfc_array_spec *as = NULL;
7210 gfc_symbol *cptr; /* Pointer symbol. */
7211 gfc_symbol *cpte; /* Pointee symbol. */
7212 locus var_locus;
7213 bool done = false;
7215 while (!done)
7217 if (gfc_match_char ('(') != MATCH_YES)
7219 gfc_error ("Expected %<(%> at %C");
7220 return MATCH_ERROR;
7223 /* Match pointer. */
7224 var_locus = gfc_current_locus;
7225 gfc_clear_attr (&current_attr);
7226 gfc_add_cray_pointer (&current_attr, &var_locus);
7227 current_ts.type = BT_INTEGER;
7228 current_ts.kind = gfc_index_integer_kind;
7230 m = gfc_match_symbol (&cptr, 0);
7231 if (m != MATCH_YES)
7233 gfc_error ("Expected variable name at %C");
7234 return m;
7237 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
7238 return MATCH_ERROR;
7240 gfc_set_sym_referenced (cptr);
7242 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
7244 cptr->ts.type = BT_INTEGER;
7245 cptr->ts.kind = gfc_index_integer_kind;
7247 else if (cptr->ts.type != BT_INTEGER)
7249 gfc_error ("Cray pointer at %C must be an integer");
7250 return MATCH_ERROR;
7252 else if (cptr->ts.kind < gfc_index_integer_kind)
7253 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
7254 " memory addresses require %d bytes",
7255 cptr->ts.kind, gfc_index_integer_kind);
7257 if (gfc_match_char (',') != MATCH_YES)
7259 gfc_error ("Expected \",\" at %C");
7260 return MATCH_ERROR;
7263 /* Match Pointee. */
7264 var_locus = gfc_current_locus;
7265 gfc_clear_attr (&current_attr);
7266 gfc_add_cray_pointee (&current_attr, &var_locus);
7267 current_ts.type = BT_UNKNOWN;
7268 current_ts.kind = 0;
7270 m = gfc_match_symbol (&cpte, 0);
7271 if (m != MATCH_YES)
7273 gfc_error ("Expected variable name at %C");
7274 return m;
7277 /* Check for an optional array spec. */
7278 m = gfc_match_array_spec (&as, true, false);
7279 if (m == MATCH_ERROR)
7281 gfc_free_array_spec (as);
7282 return m;
7284 else if (m == MATCH_NO)
7286 gfc_free_array_spec (as);
7287 as = NULL;
7290 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
7291 return MATCH_ERROR;
7293 gfc_set_sym_referenced (cpte);
7295 if (cpte->as == NULL)
7297 if (!gfc_set_array_spec (cpte, as, &var_locus))
7298 gfc_internal_error ("Couldn't set Cray pointee array spec.");
7300 else if (as != NULL)
7302 gfc_error ("Duplicate array spec for Cray pointee at %C");
7303 gfc_free_array_spec (as);
7304 return MATCH_ERROR;
7307 as = NULL;
7309 if (cpte->as != NULL)
7311 /* Fix array spec. */
7312 m = gfc_mod_pointee_as (cpte->as);
7313 if (m == MATCH_ERROR)
7314 return m;
7317 /* Point the Pointee at the Pointer. */
7318 cpte->cp_pointer = cptr;
7320 if (gfc_match_char (')') != MATCH_YES)
7322 gfc_error ("Expected \")\" at %C");
7323 return MATCH_ERROR;
7325 m = gfc_match_char (',');
7326 if (m != MATCH_YES)
7327 done = true; /* Stop searching for more declarations. */
7331 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
7332 || gfc_match_eos () != MATCH_YES)
7334 gfc_error ("Expected %<,%> or end of statement at %C");
7335 return MATCH_ERROR;
7337 return MATCH_YES;
7341 match
7342 gfc_match_external (void)
7345 gfc_clear_attr (&current_attr);
7346 current_attr.external = 1;
7348 return attr_decl ();
7352 match
7353 gfc_match_intent (void)
7355 sym_intent intent;
7357 /* This is not allowed within a BLOCK construct! */
7358 if (gfc_current_state () == COMP_BLOCK)
7360 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
7361 return MATCH_ERROR;
7364 intent = match_intent_spec ();
7365 if (intent == INTENT_UNKNOWN)
7366 return MATCH_ERROR;
7368 gfc_clear_attr (&current_attr);
7369 current_attr.intent = intent;
7371 return attr_decl ();
7375 match
7376 gfc_match_intrinsic (void)
7379 gfc_clear_attr (&current_attr);
7380 current_attr.intrinsic = 1;
7382 return attr_decl ();
7386 match
7387 gfc_match_optional (void)
7389 /* This is not allowed within a BLOCK construct! */
7390 if (gfc_current_state () == COMP_BLOCK)
7392 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
7393 return MATCH_ERROR;
7396 gfc_clear_attr (&current_attr);
7397 current_attr.optional = 1;
7399 return attr_decl ();
7403 match
7404 gfc_match_pointer (void)
7406 gfc_gobble_whitespace ();
7407 if (gfc_peek_ascii_char () == '(')
7409 if (!flag_cray_pointer)
7411 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7412 "flag");
7413 return MATCH_ERROR;
7415 return cray_pointer_decl ();
7417 else
7419 gfc_clear_attr (&current_attr);
7420 current_attr.pointer = 1;
7422 return attr_decl ();
7427 match
7428 gfc_match_allocatable (void)
7430 gfc_clear_attr (&current_attr);
7431 current_attr.allocatable = 1;
7433 return attr_decl ();
7437 match
7438 gfc_match_codimension (void)
7440 gfc_clear_attr (&current_attr);
7441 current_attr.codimension = 1;
7443 return attr_decl ();
7447 match
7448 gfc_match_contiguous (void)
7450 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
7451 return MATCH_ERROR;
7453 gfc_clear_attr (&current_attr);
7454 current_attr.contiguous = 1;
7456 return attr_decl ();
7460 match
7461 gfc_match_dimension (void)
7463 gfc_clear_attr (&current_attr);
7464 current_attr.dimension = 1;
7466 return attr_decl ();
7470 match
7471 gfc_match_target (void)
7473 gfc_clear_attr (&current_attr);
7474 current_attr.target = 1;
7476 return attr_decl ();
7480 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7481 statement. */
7483 static match
7484 access_attr_decl (gfc_statement st)
7486 char name[GFC_MAX_SYMBOL_LEN + 1];
7487 interface_type type;
7488 gfc_user_op *uop;
7489 gfc_symbol *sym, *dt_sym;
7490 gfc_intrinsic_op op;
7491 match m;
7493 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7494 goto done;
7496 for (;;)
7498 m = gfc_match_generic_spec (&type, name, &op);
7499 if (m == MATCH_NO)
7500 goto syntax;
7501 if (m == MATCH_ERROR)
7502 return MATCH_ERROR;
7504 switch (type)
7506 case INTERFACE_NAMELESS:
7507 case INTERFACE_ABSTRACT:
7508 goto syntax;
7510 case INTERFACE_GENERIC:
7511 if (gfc_get_symbol (name, NULL, &sym))
7512 goto done;
7514 if (!gfc_add_access (&sym->attr,
7515 (st == ST_PUBLIC)
7516 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7517 sym->name, NULL))
7518 return MATCH_ERROR;
7520 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
7521 && !gfc_add_access (&dt_sym->attr,
7522 (st == ST_PUBLIC)
7523 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7524 sym->name, NULL))
7525 return MATCH_ERROR;
7527 break;
7529 case INTERFACE_INTRINSIC_OP:
7530 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
7532 gfc_intrinsic_op other_op;
7534 gfc_current_ns->operator_access[op] =
7535 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7537 /* Handle the case if there is another op with the same
7538 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7539 other_op = gfc_equivalent_op (op);
7541 if (other_op != INTRINSIC_NONE)
7542 gfc_current_ns->operator_access[other_op] =
7543 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7546 else
7548 gfc_error ("Access specification of the %s operator at %C has "
7549 "already been specified", gfc_op2string (op));
7550 goto done;
7553 break;
7555 case INTERFACE_USER_OP:
7556 uop = gfc_get_uop (name);
7558 if (uop->access == ACCESS_UNKNOWN)
7560 uop->access = (st == ST_PUBLIC)
7561 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7563 else
7565 gfc_error ("Access specification of the .%s. operator at %C "
7566 "has already been specified", sym->name);
7567 goto done;
7570 break;
7573 if (gfc_match_char (',') == MATCH_NO)
7574 break;
7577 if (gfc_match_eos () != MATCH_YES)
7578 goto syntax;
7579 return MATCH_YES;
7581 syntax:
7582 gfc_syntax_error (st);
7584 done:
7585 return MATCH_ERROR;
7589 match
7590 gfc_match_protected (void)
7592 gfc_symbol *sym;
7593 match m;
7595 if (!gfc_current_ns->proc_name
7596 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7598 gfc_error ("PROTECTED at %C only allowed in specification "
7599 "part of a module");
7600 return MATCH_ERROR;
7604 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
7605 return MATCH_ERROR;
7607 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7609 return MATCH_ERROR;
7612 if (gfc_match_eos () == MATCH_YES)
7613 goto syntax;
7615 for(;;)
7617 m = gfc_match_symbol (&sym, 0);
7618 switch (m)
7620 case MATCH_YES:
7621 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
7622 return MATCH_ERROR;
7623 goto next_item;
7625 case MATCH_NO:
7626 break;
7628 case MATCH_ERROR:
7629 return MATCH_ERROR;
7632 next_item:
7633 if (gfc_match_eos () == MATCH_YES)
7634 break;
7635 if (gfc_match_char (',') != MATCH_YES)
7636 goto syntax;
7639 return MATCH_YES;
7641 syntax:
7642 gfc_error ("Syntax error in PROTECTED statement at %C");
7643 return MATCH_ERROR;
7647 /* The PRIVATE statement is a bit weird in that it can be an attribute
7648 declaration, but also works as a standalone statement inside of a
7649 type declaration or a module. */
7651 match
7652 gfc_match_private (gfc_statement *st)
7655 if (gfc_match ("private") != MATCH_YES)
7656 return MATCH_NO;
7658 if (gfc_current_state () != COMP_MODULE
7659 && !(gfc_current_state () == COMP_DERIVED
7660 && gfc_state_stack->previous
7661 && gfc_state_stack->previous->state == COMP_MODULE)
7662 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7663 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7664 && gfc_state_stack->previous->previous->state == COMP_MODULE))
7666 gfc_error ("PRIVATE statement at %C is only allowed in the "
7667 "specification part of a module");
7668 return MATCH_ERROR;
7671 if (gfc_current_state () == COMP_DERIVED)
7673 if (gfc_match_eos () == MATCH_YES)
7675 *st = ST_PRIVATE;
7676 return MATCH_YES;
7679 gfc_syntax_error (ST_PRIVATE);
7680 return MATCH_ERROR;
7683 if (gfc_match_eos () == MATCH_YES)
7685 *st = ST_PRIVATE;
7686 return MATCH_YES;
7689 *st = ST_ATTR_DECL;
7690 return access_attr_decl (ST_PRIVATE);
7694 match
7695 gfc_match_public (gfc_statement *st)
7698 if (gfc_match ("public") != MATCH_YES)
7699 return MATCH_NO;
7701 if (gfc_current_state () != COMP_MODULE)
7703 gfc_error ("PUBLIC statement at %C is only allowed in the "
7704 "specification part of a module");
7705 return MATCH_ERROR;
7708 if (gfc_match_eos () == MATCH_YES)
7710 *st = ST_PUBLIC;
7711 return MATCH_YES;
7714 *st = ST_ATTR_DECL;
7715 return access_attr_decl (ST_PUBLIC);
7719 /* Workhorse for gfc_match_parameter. */
7721 static match
7722 do_parm (void)
7724 gfc_symbol *sym;
7725 gfc_expr *init;
7726 match m;
7727 bool t;
7729 m = gfc_match_symbol (&sym, 0);
7730 if (m == MATCH_NO)
7731 gfc_error ("Expected variable name at %C in PARAMETER statement");
7733 if (m != MATCH_YES)
7734 return m;
7736 if (gfc_match_char ('=') == MATCH_NO)
7738 gfc_error ("Expected = sign in PARAMETER statement at %C");
7739 return MATCH_ERROR;
7742 m = gfc_match_init_expr (&init);
7743 if (m == MATCH_NO)
7744 gfc_error ("Expected expression at %C in PARAMETER statement");
7745 if (m != MATCH_YES)
7746 return m;
7748 if (sym->ts.type == BT_UNKNOWN
7749 && !gfc_set_default_type (sym, 1, NULL))
7751 m = MATCH_ERROR;
7752 goto cleanup;
7755 if (!gfc_check_assign_symbol (sym, NULL, init)
7756 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7758 m = MATCH_ERROR;
7759 goto cleanup;
7762 if (sym->value)
7764 gfc_error ("Initializing already initialized variable at %C");
7765 m = MATCH_ERROR;
7766 goto cleanup;
7769 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7770 return (t) ? MATCH_YES : MATCH_ERROR;
7772 cleanup:
7773 gfc_free_expr (init);
7774 return m;
7778 /* Match a parameter statement, with the weird syntax that these have. */
7780 match
7781 gfc_match_parameter (void)
7783 match m;
7785 if (gfc_match_char ('(') == MATCH_NO)
7786 return MATCH_NO;
7788 for (;;)
7790 m = do_parm ();
7791 if (m != MATCH_YES)
7792 break;
7794 if (gfc_match (" )%t") == MATCH_YES)
7795 break;
7797 if (gfc_match_char (',') != MATCH_YES)
7799 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7800 m = MATCH_ERROR;
7801 break;
7805 return m;
7809 /* Save statements have a special syntax. */
7811 match
7812 gfc_match_save (void)
7814 char n[GFC_MAX_SYMBOL_LEN+1];
7815 gfc_common_head *c;
7816 gfc_symbol *sym;
7817 match m;
7819 if (gfc_match_eos () == MATCH_YES)
7821 if (gfc_current_ns->seen_save)
7823 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7824 "follows previous SAVE statement"))
7825 return MATCH_ERROR;
7828 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7829 return MATCH_YES;
7832 if (gfc_current_ns->save_all)
7834 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7835 "blanket SAVE statement"))
7836 return MATCH_ERROR;
7839 gfc_match (" ::");
7841 for (;;)
7843 m = gfc_match_symbol (&sym, 0);
7844 switch (m)
7846 case MATCH_YES:
7847 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7848 &gfc_current_locus))
7849 return MATCH_ERROR;
7850 goto next_item;
7852 case MATCH_NO:
7853 break;
7855 case MATCH_ERROR:
7856 return MATCH_ERROR;
7859 m = gfc_match (" / %n /", &n);
7860 if (m == MATCH_ERROR)
7861 return MATCH_ERROR;
7862 if (m == MATCH_NO)
7863 goto syntax;
7865 c = gfc_get_common (n, 0);
7866 c->saved = 1;
7868 gfc_current_ns->seen_save = 1;
7870 next_item:
7871 if (gfc_match_eos () == MATCH_YES)
7872 break;
7873 if (gfc_match_char (',') != MATCH_YES)
7874 goto syntax;
7877 return MATCH_YES;
7879 syntax:
7880 gfc_error ("Syntax error in SAVE statement at %C");
7881 return MATCH_ERROR;
7885 match
7886 gfc_match_value (void)
7888 gfc_symbol *sym;
7889 match m;
7891 /* This is not allowed within a BLOCK construct! */
7892 if (gfc_current_state () == COMP_BLOCK)
7894 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7895 return MATCH_ERROR;
7898 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7899 return MATCH_ERROR;
7901 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7903 return MATCH_ERROR;
7906 if (gfc_match_eos () == MATCH_YES)
7907 goto syntax;
7909 for(;;)
7911 m = gfc_match_symbol (&sym, 0);
7912 switch (m)
7914 case MATCH_YES:
7915 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7916 return MATCH_ERROR;
7917 goto next_item;
7919 case MATCH_NO:
7920 break;
7922 case MATCH_ERROR:
7923 return MATCH_ERROR;
7926 next_item:
7927 if (gfc_match_eos () == MATCH_YES)
7928 break;
7929 if (gfc_match_char (',') != MATCH_YES)
7930 goto syntax;
7933 return MATCH_YES;
7935 syntax:
7936 gfc_error ("Syntax error in VALUE statement at %C");
7937 return MATCH_ERROR;
7941 match
7942 gfc_match_volatile (void)
7944 gfc_symbol *sym;
7945 match m;
7947 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7948 return MATCH_ERROR;
7950 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7952 return MATCH_ERROR;
7955 if (gfc_match_eos () == MATCH_YES)
7956 goto syntax;
7958 for(;;)
7960 /* VOLATILE is special because it can be added to host-associated
7961 symbols locally. Except for coarrays. */
7962 m = gfc_match_symbol (&sym, 1);
7963 switch (m)
7965 case MATCH_YES:
7966 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7967 for variable in a BLOCK which is defined outside of the BLOCK. */
7968 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7970 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7971 "%C, which is use-/host-associated", sym->name);
7972 return MATCH_ERROR;
7974 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7975 return MATCH_ERROR;
7976 goto next_item;
7978 case MATCH_NO:
7979 break;
7981 case MATCH_ERROR:
7982 return MATCH_ERROR;
7985 next_item:
7986 if (gfc_match_eos () == MATCH_YES)
7987 break;
7988 if (gfc_match_char (',') != MATCH_YES)
7989 goto syntax;
7992 return MATCH_YES;
7994 syntax:
7995 gfc_error ("Syntax error in VOLATILE statement at %C");
7996 return MATCH_ERROR;
8000 match
8001 gfc_match_asynchronous (void)
8003 gfc_symbol *sym;
8004 match m;
8006 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
8007 return MATCH_ERROR;
8009 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8011 return MATCH_ERROR;
8014 if (gfc_match_eos () == MATCH_YES)
8015 goto syntax;
8017 for(;;)
8019 /* ASYNCHRONOUS is special because it can be added to host-associated
8020 symbols locally. */
8021 m = gfc_match_symbol (&sym, 1);
8022 switch (m)
8024 case MATCH_YES:
8025 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
8026 return MATCH_ERROR;
8027 goto next_item;
8029 case MATCH_NO:
8030 break;
8032 case MATCH_ERROR:
8033 return MATCH_ERROR;
8036 next_item:
8037 if (gfc_match_eos () == MATCH_YES)
8038 break;
8039 if (gfc_match_char (',') != MATCH_YES)
8040 goto syntax;
8043 return MATCH_YES;
8045 syntax:
8046 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
8047 return MATCH_ERROR;
8051 /* Match a module procedure statement in a submodule. */
8053 match
8054 gfc_match_submod_proc (void)
8056 char name[GFC_MAX_SYMBOL_LEN + 1];
8057 gfc_symbol *sym, *fsym;
8058 match m;
8059 gfc_formal_arglist *formal, *head, *tail;
8061 if (gfc_current_state () != COMP_CONTAINS
8062 || !(gfc_state_stack->previous
8063 && (gfc_state_stack->previous->state == COMP_SUBMODULE
8064 || gfc_state_stack->previous->state == COMP_MODULE)))
8065 return MATCH_NO;
8067 m = gfc_match (" module% procedure% %n", name);
8068 if (m != MATCH_YES)
8069 return m;
8071 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
8072 "at %C"))
8073 return MATCH_ERROR;
8075 if (get_proc_name (name, &sym, false))
8076 return MATCH_ERROR;
8078 /* Make sure that the result field is appropriately filled, even though
8079 the result symbol will be replaced later on. */
8080 if (sym->ts.interface && sym->ts.interface->attr.function)
8082 if (sym->ts.interface->result
8083 && sym->ts.interface->result != sym->ts.interface)
8084 sym->result= sym->ts.interface->result;
8085 else
8086 sym->result = sym;
8089 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
8090 the symbol existed before. */
8091 sym->declared_at = gfc_current_locus;
8093 if (!sym->attr.module_procedure)
8094 return MATCH_ERROR;
8096 /* Signal match_end to expect "end procedure". */
8097 sym->abr_modproc_decl = 1;
8099 /* Change from IFSRC_IFBODY coming from the interface declaration. */
8100 sym->attr.if_source = IFSRC_DECL;
8102 gfc_new_block = sym;
8104 /* Make a new formal arglist with the symbols in the procedure
8105 namespace. */
8106 head = tail = NULL;
8107 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
8109 if (formal == sym->formal)
8110 head = tail = gfc_get_formal_arglist ();
8111 else
8113 tail->next = gfc_get_formal_arglist ();
8114 tail = tail->next;
8117 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
8118 goto cleanup;
8120 tail->sym = fsym;
8121 gfc_set_sym_referenced (fsym);
8124 /* The dummy symbols get cleaned up, when the formal_namespace of the
8125 interface declaration is cleared. This allows us to add the
8126 explicit interface as is done for other type of procedure. */
8127 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
8128 &gfc_current_locus))
8129 return MATCH_ERROR;
8131 if (gfc_match_eos () != MATCH_YES)
8133 gfc_syntax_error (ST_MODULE_PROC);
8134 return MATCH_ERROR;
8137 return MATCH_YES;
8139 cleanup:
8140 gfc_free_formal_arglist (head);
8141 return MATCH_ERROR;
8145 /* Match a module procedure statement. Note that we have to modify
8146 symbols in the parent's namespace because the current one was there
8147 to receive symbols that are in an interface's formal argument list. */
8149 match
8150 gfc_match_modproc (void)
8152 char name[GFC_MAX_SYMBOL_LEN + 1];
8153 gfc_symbol *sym;
8154 match m;
8155 locus old_locus;
8156 gfc_namespace *module_ns;
8157 gfc_interface *old_interface_head, *interface;
8159 if (gfc_state_stack->state != COMP_INTERFACE
8160 || gfc_state_stack->previous == NULL
8161 || current_interface.type == INTERFACE_NAMELESS
8162 || current_interface.type == INTERFACE_ABSTRACT)
8164 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
8165 "interface");
8166 return MATCH_ERROR;
8169 module_ns = gfc_current_ns->parent;
8170 for (; module_ns; module_ns = module_ns->parent)
8171 if (module_ns->proc_name->attr.flavor == FL_MODULE
8172 || module_ns->proc_name->attr.flavor == FL_PROGRAM
8173 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
8174 && !module_ns->proc_name->attr.contained))
8175 break;
8177 if (module_ns == NULL)
8178 return MATCH_ERROR;
8180 /* Store the current state of the interface. We will need it if we
8181 end up with a syntax error and need to recover. */
8182 old_interface_head = gfc_current_interface_head ();
8184 /* Check if the F2008 optional double colon appears. */
8185 gfc_gobble_whitespace ();
8186 old_locus = gfc_current_locus;
8187 if (gfc_match ("::") == MATCH_YES)
8189 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
8190 "MODULE PROCEDURE statement at %L", &old_locus))
8191 return MATCH_ERROR;
8193 else
8194 gfc_current_locus = old_locus;
8196 for (;;)
8198 bool last = false;
8199 old_locus = gfc_current_locus;
8201 m = gfc_match_name (name);
8202 if (m == MATCH_NO)
8203 goto syntax;
8204 if (m != MATCH_YES)
8205 return MATCH_ERROR;
8207 /* Check for syntax error before starting to add symbols to the
8208 current namespace. */
8209 if (gfc_match_eos () == MATCH_YES)
8210 last = true;
8212 if (!last && gfc_match_char (',') != MATCH_YES)
8213 goto syntax;
8215 /* Now we're sure the syntax is valid, we process this item
8216 further. */
8217 if (gfc_get_symbol (name, module_ns, &sym))
8218 return MATCH_ERROR;
8220 if (sym->attr.intrinsic)
8222 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
8223 "PROCEDURE", &old_locus);
8224 return MATCH_ERROR;
8227 if (sym->attr.proc != PROC_MODULE
8228 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8229 return MATCH_ERROR;
8231 if (!gfc_add_interface (sym))
8232 return MATCH_ERROR;
8234 sym->attr.mod_proc = 1;
8235 sym->declared_at = old_locus;
8237 if (last)
8238 break;
8241 return MATCH_YES;
8243 syntax:
8244 /* Restore the previous state of the interface. */
8245 interface = gfc_current_interface_head ();
8246 gfc_set_current_interface_head (old_interface_head);
8248 /* Free the new interfaces. */
8249 while (interface != old_interface_head)
8251 gfc_interface *i = interface->next;
8252 free (interface);
8253 interface = i;
8256 /* And issue a syntax error. */
8257 gfc_syntax_error (ST_MODULE_PROC);
8258 return MATCH_ERROR;
8262 /* Check a derived type that is being extended. */
8264 static gfc_symbol*
8265 check_extended_derived_type (char *name)
8267 gfc_symbol *extended;
8269 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
8271 gfc_error ("Ambiguous symbol in TYPE definition at %C");
8272 return NULL;
8275 extended = gfc_find_dt_in_generic (extended);
8277 /* F08:C428. */
8278 if (!extended)
8280 gfc_error ("Symbol %qs at %C has not been previously defined", name);
8281 return NULL;
8284 if (extended->attr.flavor != FL_DERIVED)
8286 gfc_error ("%qs in EXTENDS expression at %C is not a "
8287 "derived type", name);
8288 return NULL;
8291 if (extended->attr.is_bind_c)
8293 gfc_error ("%qs cannot be extended at %C because it "
8294 "is BIND(C)", extended->name);
8295 return NULL;
8298 if (extended->attr.sequence)
8300 gfc_error ("%qs cannot be extended at %C because it "
8301 "is a SEQUENCE type", extended->name);
8302 return NULL;
8305 return extended;
8309 /* Match the optional attribute specifiers for a type declaration.
8310 Return MATCH_ERROR if an error is encountered in one of the handled
8311 attributes (public, private, bind(c)), MATCH_NO if what's found is
8312 not a handled attribute, and MATCH_YES otherwise. TODO: More error
8313 checking on attribute conflicts needs to be done. */
8315 match
8316 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
8318 /* See if the derived type is marked as private. */
8319 if (gfc_match (" , private") == MATCH_YES)
8321 if (gfc_current_state () != COMP_MODULE)
8323 gfc_error ("Derived type at %C can only be PRIVATE in the "
8324 "specification part of a module");
8325 return MATCH_ERROR;
8328 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
8329 return MATCH_ERROR;
8331 else if (gfc_match (" , public") == MATCH_YES)
8333 if (gfc_current_state () != COMP_MODULE)
8335 gfc_error ("Derived type at %C can only be PUBLIC in the "
8336 "specification part of a module");
8337 return MATCH_ERROR;
8340 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
8341 return MATCH_ERROR;
8343 else if (gfc_match (" , bind ( c )") == MATCH_YES)
8345 /* If the type is defined to be bind(c) it then needs to make
8346 sure that all fields are interoperable. This will
8347 need to be a semantic check on the finished derived type.
8348 See 15.2.3 (lines 9-12) of F2003 draft. */
8349 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
8350 return MATCH_ERROR;
8352 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
8354 else if (gfc_match (" , abstract") == MATCH_YES)
8356 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
8357 return MATCH_ERROR;
8359 if (!gfc_add_abstract (attr, &gfc_current_locus))
8360 return MATCH_ERROR;
8362 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
8364 if (!gfc_add_extension (attr, &gfc_current_locus))
8365 return MATCH_ERROR;
8367 else
8368 return MATCH_NO;
8370 /* If we get here, something matched. */
8371 return MATCH_YES;
8375 /* Common function for type declaration blocks similar to derived types, such
8376 as STRUCTURES and MAPs. Unlike derived types, a structure type
8377 does NOT have a generic symbol matching the name given by the user.
8378 STRUCTUREs can share names with variables and PARAMETERs so we must allow
8379 for the creation of an independent symbol.
8380 Other parameters are a message to prefix errors with, the name of the new
8381 type to be created, and the flavor to add to the resulting symbol. */
8383 static bool
8384 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
8385 gfc_symbol **result)
8387 gfc_symbol *sym;
8388 locus where;
8390 gcc_assert (name[0] == (char) TOUPPER (name[0]));
8392 if (decl)
8393 where = *decl;
8394 else
8395 where = gfc_current_locus;
8397 if (gfc_get_symbol (name, NULL, &sym))
8398 return false;
8400 if (!sym)
8402 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
8403 return false;
8406 if (sym->components != NULL || sym->attr.zero_comp)
8408 gfc_error ("Type definition of '%s' at %C was already defined at %L",
8409 sym->name, &sym->declared_at);
8410 return false;
8413 sym->declared_at = where;
8415 if (sym->attr.flavor != fl
8416 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
8417 return false;
8419 if (!sym->hash_value)
8420 /* Set the hash for the compound name for this type. */
8421 sym->hash_value = gfc_hash_value (sym);
8423 /* Normally the type is expected to have been completely parsed by the time
8424 a field declaration with this type is seen. For unions, maps, and nested
8425 structure declarations, we need to indicate that it is okay that we
8426 haven't seen any components yet. This will be updated after the structure
8427 is fully parsed. */
8428 sym->attr.zero_comp = 0;
8430 /* Structures always act like derived-types with the SEQUENCE attribute */
8431 gfc_add_sequence (&sym->attr, sym->name, NULL);
8433 if (result) *result = sym;
8435 return true;
8439 /* Match the opening of a MAP block. Like a struct within a union in C;
8440 behaves identical to STRUCTURE blocks. */
8442 match
8443 gfc_match_map (void)
8445 /* Counter used to give unique internal names to map structures. */
8446 static unsigned int gfc_map_id = 0;
8447 char name[GFC_MAX_SYMBOL_LEN + 1];
8448 gfc_symbol *sym;
8449 locus old_loc;
8451 old_loc = gfc_current_locus;
8453 if (gfc_match_eos () != MATCH_YES)
8455 gfc_error ("Junk after MAP statement at %C");
8456 gfc_current_locus = old_loc;
8457 return MATCH_ERROR;
8460 /* Map blocks are anonymous so we make up unique names for the symbol table
8461 which are invalid Fortran identifiers. */
8462 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
8464 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
8465 return MATCH_ERROR;
8467 gfc_new_block = sym;
8469 return MATCH_YES;
8473 /* Match the opening of a UNION block. */
8475 match
8476 gfc_match_union (void)
8478 /* Counter used to give unique internal names to union types. */
8479 static unsigned int gfc_union_id = 0;
8480 char name[GFC_MAX_SYMBOL_LEN + 1];
8481 gfc_symbol *sym;
8482 locus old_loc;
8484 old_loc = gfc_current_locus;
8486 if (gfc_match_eos () != MATCH_YES)
8488 gfc_error ("Junk after UNION statement at %C");
8489 gfc_current_locus = old_loc;
8490 return MATCH_ERROR;
8493 /* Unions are anonymous so we make up unique names for the symbol table
8494 which are invalid Fortran identifiers. */
8495 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
8497 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
8498 return MATCH_ERROR;
8500 gfc_new_block = sym;
8502 return MATCH_YES;
8506 /* Match the beginning of a STRUCTURE declaration. This is similar to
8507 matching the beginning of a derived type declaration with a few
8508 twists. The resulting type symbol has no access control or other
8509 interesting attributes. */
8511 match
8512 gfc_match_structure_decl (void)
8514 /* Counter used to give unique internal names to anonymous structures. */
8515 int gfc_structure_id = 0;
8516 char name[GFC_MAX_SYMBOL_LEN + 1];
8517 gfc_symbol *sym;
8518 match m;
8519 locus where;
8521 if(!gfc_option.flag_dec_structure)
8523 gfc_error ("STRUCTURE at %C is a DEC extension, enable with "
8524 "-fdec-structure");
8525 return MATCH_ERROR;
8528 name[0] = '\0';
8530 m = gfc_match (" /%n/", name);
8531 if (m != MATCH_YES)
8533 /* Non-nested structure declarations require a structure name. */
8534 if (!gfc_comp_struct (gfc_current_state ()))
8536 gfc_error ("Structure name expected in non-nested structure "
8537 "declaration at %C");
8538 return MATCH_ERROR;
8540 /* This is an anonymous structure; make up a unique name for it
8541 (upper-case letters never make it to symbol names from the source).
8542 The important thing is initializing the type variable
8543 and setting gfc_new_symbol, which is immediately used by
8544 parse_structure () and variable_decl () to add components of
8545 this type. */
8546 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
8549 where = gfc_current_locus;
8550 /* No field list allowed after non-nested structure declaration. */
8551 if (!gfc_comp_struct (gfc_current_state ())
8552 && gfc_match_eos () != MATCH_YES)
8554 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
8555 return MATCH_ERROR;
8558 /* Make sure the name is not the name of an intrinsic type. */
8559 if (gfc_is_intrinsic_typename (name))
8561 gfc_error ("Structure name '%s' at %C cannot be the same as an"
8562 " intrinsic type", name);
8563 return MATCH_ERROR;
8566 /* Store the actual type symbol for the structure with an upper-case first
8567 letter (an invalid Fortran identifier). */
8569 sprintf (name, gfc_dt_upper_string (name));
8570 if (!get_struct_decl (name, FL_STRUCT, &where, &sym))
8571 return MATCH_ERROR;
8573 gfc_new_block = sym;
8574 return MATCH_YES;
8577 /* Match the beginning of a derived type declaration. If a type name
8578 was the result of a function, then it is possible to have a symbol
8579 already to be known as a derived type yet have no components. */
8581 match
8582 gfc_match_derived_decl (void)
8584 char name[GFC_MAX_SYMBOL_LEN + 1];
8585 char parent[GFC_MAX_SYMBOL_LEN + 1];
8586 symbol_attribute attr;
8587 gfc_symbol *sym, *gensym;
8588 gfc_symbol *extended;
8589 match m;
8590 match is_type_attr_spec = MATCH_NO;
8591 bool seen_attr = false;
8592 gfc_interface *intr = NULL, *head;
8594 if (gfc_comp_struct (gfc_current_state ()))
8595 return MATCH_NO;
8597 name[0] = '\0';
8598 parent[0] = '\0';
8599 gfc_clear_attr (&attr);
8600 extended = NULL;
8604 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
8605 if (is_type_attr_spec == MATCH_ERROR)
8606 return MATCH_ERROR;
8607 if (is_type_attr_spec == MATCH_YES)
8608 seen_attr = true;
8609 } while (is_type_attr_spec == MATCH_YES);
8611 /* Deal with derived type extensions. The extension attribute has
8612 been added to 'attr' but now the parent type must be found and
8613 checked. */
8614 if (parent[0])
8615 extended = check_extended_derived_type (parent);
8617 if (parent[0] && !extended)
8618 return MATCH_ERROR;
8620 if (gfc_match (" ::") != MATCH_YES && seen_attr)
8622 gfc_error ("Expected :: in TYPE definition at %C");
8623 return MATCH_ERROR;
8626 m = gfc_match (" %n%t", name);
8627 if (m != MATCH_YES)
8628 return m;
8630 /* Make sure the name is not the name of an intrinsic type. */
8631 if (gfc_is_intrinsic_typename (name))
8633 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
8634 "type", name);
8635 return MATCH_ERROR;
8638 if (gfc_get_symbol (name, NULL, &gensym))
8639 return MATCH_ERROR;
8641 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
8643 gfc_error ("Derived type name %qs at %C already has a basic type "
8644 "of %s", gensym->name, gfc_typename (&gensym->ts));
8645 return MATCH_ERROR;
8648 if (!gensym->attr.generic
8649 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
8650 return MATCH_ERROR;
8652 if (!gensym->attr.function
8653 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
8654 return MATCH_ERROR;
8656 sym = gfc_find_dt_in_generic (gensym);
8658 if (sym && (sym->components != NULL || sym->attr.zero_comp))
8660 gfc_error ("Derived type definition of %qs at %C has already been "
8661 "defined", sym->name);
8662 return MATCH_ERROR;
8665 if (!sym)
8667 /* Use upper case to save the actual derived-type symbol. */
8668 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
8669 sym->name = gfc_get_string (gensym->name);
8670 head = gensym->generic;
8671 intr = gfc_get_interface ();
8672 intr->sym = sym;
8673 intr->where = gfc_current_locus;
8674 intr->sym->declared_at = gfc_current_locus;
8675 intr->next = head;
8676 gensym->generic = intr;
8677 gensym->attr.if_source = IFSRC_DECL;
8680 /* The symbol may already have the derived attribute without the
8681 components. The ways this can happen is via a function
8682 definition, an INTRINSIC statement or a subtype in another
8683 derived type that is a pointer. The first part of the AND clause
8684 is true if the symbol is not the return value of a function. */
8685 if (sym->attr.flavor != FL_DERIVED
8686 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
8687 return MATCH_ERROR;
8689 if (attr.access != ACCESS_UNKNOWN
8690 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
8691 return MATCH_ERROR;
8692 else if (sym->attr.access == ACCESS_UNKNOWN
8693 && gensym->attr.access != ACCESS_UNKNOWN
8694 && !gfc_add_access (&sym->attr, gensym->attr.access,
8695 sym->name, NULL))
8696 return MATCH_ERROR;
8698 if (sym->attr.access != ACCESS_UNKNOWN
8699 && gensym->attr.access == ACCESS_UNKNOWN)
8700 gensym->attr.access = sym->attr.access;
8702 /* See if the derived type was labeled as bind(c). */
8703 if (attr.is_bind_c != 0)
8704 sym->attr.is_bind_c = attr.is_bind_c;
8706 /* Construct the f2k_derived namespace if it is not yet there. */
8707 if (!sym->f2k_derived)
8708 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8710 if (extended && !sym->components)
8712 gfc_component *p;
8714 /* Add the extended derived type as the first component. */
8715 gfc_add_component (sym, parent, &p);
8716 extended->refs++;
8717 gfc_set_sym_referenced (extended);
8719 p->ts.type = BT_DERIVED;
8720 p->ts.u.derived = extended;
8721 p->initializer = gfc_default_initializer (&p->ts);
8723 /* Set extension level. */
8724 if (extended->attr.extension == 255)
8726 /* Since the extension field is 8 bit wide, we can only have
8727 up to 255 extension levels. */
8728 gfc_error ("Maximum extension level reached with type %qs at %L",
8729 extended->name, &extended->declared_at);
8730 return MATCH_ERROR;
8732 sym->attr.extension = extended->attr.extension + 1;
8734 /* Provide the links between the extended type and its extension. */
8735 if (!extended->f2k_derived)
8736 extended->f2k_derived = gfc_get_namespace (NULL, 0);
8739 if (!sym->hash_value)
8740 /* Set the hash for the compound name for this type. */
8741 sym->hash_value = gfc_hash_value (sym);
8743 /* Take over the ABSTRACT attribute. */
8744 sym->attr.abstract = attr.abstract;
8746 gfc_new_block = sym;
8748 return MATCH_YES;
8752 /* Cray Pointees can be declared as:
8753 pointer (ipt, a (n,m,...,*)) */
8755 match
8756 gfc_mod_pointee_as (gfc_array_spec *as)
8758 as->cray_pointee = true; /* This will be useful to know later. */
8759 if (as->type == AS_ASSUMED_SIZE)
8760 as->cp_was_assumed = true;
8761 else if (as->type == AS_ASSUMED_SHAPE)
8763 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8764 return MATCH_ERROR;
8766 return MATCH_YES;
8770 /* Match the enum definition statement, here we are trying to match
8771 the first line of enum definition statement.
8772 Returns MATCH_YES if match is found. */
8774 match
8775 gfc_match_enum (void)
8777 match m;
8779 m = gfc_match_eos ();
8780 if (m != MATCH_YES)
8781 return m;
8783 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
8784 return MATCH_ERROR;
8786 return MATCH_YES;
8790 /* Returns an initializer whose value is one higher than the value of the
8791 LAST_INITIALIZER argument. If the argument is NULL, the
8792 initializers value will be set to zero. The initializer's kind
8793 will be set to gfc_c_int_kind.
8795 If -fshort-enums is given, the appropriate kind will be selected
8796 later after all enumerators have been parsed. A warning is issued
8797 here if an initializer exceeds gfc_c_int_kind. */
8799 static gfc_expr *
8800 enum_initializer (gfc_expr *last_initializer, locus where)
8802 gfc_expr *result;
8803 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
8805 mpz_init (result->value.integer);
8807 if (last_initializer != NULL)
8809 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8810 result->where = last_initializer->where;
8812 if (gfc_check_integer_range (result->value.integer,
8813 gfc_c_int_kind) != ARITH_OK)
8815 gfc_error ("Enumerator exceeds the C integer type at %C");
8816 return NULL;
8819 else
8821 /* Control comes here, if it's the very first enumerator and no
8822 initializer has been given. It will be initialized to zero. */
8823 mpz_set_si (result->value.integer, 0);
8826 return result;
8830 /* Match a variable name with an optional initializer. When this
8831 subroutine is called, a variable is expected to be parsed next.
8832 Depending on what is happening at the moment, updates either the
8833 symbol table or the current interface. */
8835 static match
8836 enumerator_decl (void)
8838 char name[GFC_MAX_SYMBOL_LEN + 1];
8839 gfc_expr *initializer;
8840 gfc_array_spec *as = NULL;
8841 gfc_symbol *sym;
8842 locus var_locus;
8843 match m;
8844 bool t;
8845 locus old_locus;
8847 initializer = NULL;
8848 old_locus = gfc_current_locus;
8850 /* When we get here, we've just matched a list of attributes and
8851 maybe a type and a double colon. The next thing we expect to see
8852 is the name of the symbol. */
8853 m = gfc_match_name (name);
8854 if (m != MATCH_YES)
8855 goto cleanup;
8857 var_locus = gfc_current_locus;
8859 /* OK, we've successfully matched the declaration. Now put the
8860 symbol in the current namespace. If we fail to create the symbol,
8861 bail out. */
8862 if (!build_sym (name, NULL, false, &as, &var_locus))
8864 m = MATCH_ERROR;
8865 goto cleanup;
8868 /* The double colon must be present in order to have initializers.
8869 Otherwise the statement is ambiguous with an assignment statement. */
8870 if (colon_seen)
8872 if (gfc_match_char ('=') == MATCH_YES)
8874 m = gfc_match_init_expr (&initializer);
8875 if (m == MATCH_NO)
8877 gfc_error ("Expected an initialization expression at %C");
8878 m = MATCH_ERROR;
8881 if (m != MATCH_YES)
8882 goto cleanup;
8886 /* If we do not have an initializer, the initialization value of the
8887 previous enumerator (stored in last_initializer) is incremented
8888 by 1 and is used to initialize the current enumerator. */
8889 if (initializer == NULL)
8890 initializer = enum_initializer (last_initializer, old_locus);
8892 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8894 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8895 &var_locus);
8896 m = MATCH_ERROR;
8897 goto cleanup;
8900 /* Store this current initializer, for the next enumerator variable
8901 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8902 use last_initializer below. */
8903 last_initializer = initializer;
8904 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8906 /* Maintain enumerator history. */
8907 gfc_find_symbol (name, NULL, 0, &sym);
8908 create_enum_history (sym, last_initializer);
8910 return (t) ? MATCH_YES : MATCH_ERROR;
8912 cleanup:
8913 /* Free stuff up and return. */
8914 gfc_free_expr (initializer);
8916 return m;
8920 /* Match the enumerator definition statement. */
8922 match
8923 gfc_match_enumerator_def (void)
8925 match m;
8926 bool t;
8928 gfc_clear_ts (&current_ts);
8930 m = gfc_match (" enumerator");
8931 if (m != MATCH_YES)
8932 return m;
8934 m = gfc_match (" :: ");
8935 if (m == MATCH_ERROR)
8936 return m;
8938 colon_seen = (m == MATCH_YES);
8940 if (gfc_current_state () != COMP_ENUM)
8942 gfc_error ("ENUM definition statement expected before %C");
8943 gfc_free_enum_history ();
8944 return MATCH_ERROR;
8947 (&current_ts)->type = BT_INTEGER;
8948 (&current_ts)->kind = gfc_c_int_kind;
8950 gfc_clear_attr (&current_attr);
8951 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
8952 if (!t)
8954 m = MATCH_ERROR;
8955 goto cleanup;
8958 for (;;)
8960 m = enumerator_decl ();
8961 if (m == MATCH_ERROR)
8963 gfc_free_enum_history ();
8964 goto cleanup;
8966 if (m == MATCH_NO)
8967 break;
8969 if (gfc_match_eos () == MATCH_YES)
8970 goto cleanup;
8971 if (gfc_match_char (',') != MATCH_YES)
8972 break;
8975 if (gfc_current_state () == COMP_ENUM)
8977 gfc_free_enum_history ();
8978 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8979 m = MATCH_ERROR;
8982 cleanup:
8983 gfc_free_array_spec (current_as);
8984 current_as = NULL;
8985 return m;
8990 /* Match binding attributes. */
8992 static match
8993 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
8995 bool found_passing = false;
8996 bool seen_ptr = false;
8997 match m = MATCH_YES;
8999 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
9000 this case the defaults are in there. */
9001 ba->access = ACCESS_UNKNOWN;
9002 ba->pass_arg = NULL;
9003 ba->pass_arg_num = 0;
9004 ba->nopass = 0;
9005 ba->non_overridable = 0;
9006 ba->deferred = 0;
9007 ba->ppc = ppc;
9009 /* If we find a comma, we believe there are binding attributes. */
9010 m = gfc_match_char (',');
9011 if (m == MATCH_NO)
9012 goto done;
9016 /* Access specifier. */
9018 m = gfc_match (" public");
9019 if (m == MATCH_ERROR)
9020 goto error;
9021 if (m == MATCH_YES)
9023 if (ba->access != ACCESS_UNKNOWN)
9025 gfc_error ("Duplicate access-specifier at %C");
9026 goto error;
9029 ba->access = ACCESS_PUBLIC;
9030 continue;
9033 m = gfc_match (" private");
9034 if (m == MATCH_ERROR)
9035 goto error;
9036 if (m == MATCH_YES)
9038 if (ba->access != ACCESS_UNKNOWN)
9040 gfc_error ("Duplicate access-specifier at %C");
9041 goto error;
9044 ba->access = ACCESS_PRIVATE;
9045 continue;
9048 /* If inside GENERIC, the following is not allowed. */
9049 if (!generic)
9052 /* NOPASS flag. */
9053 m = gfc_match (" nopass");
9054 if (m == MATCH_ERROR)
9055 goto error;
9056 if (m == MATCH_YES)
9058 if (found_passing)
9060 gfc_error ("Binding attributes already specify passing,"
9061 " illegal NOPASS at %C");
9062 goto error;
9065 found_passing = true;
9066 ba->nopass = 1;
9067 continue;
9070 /* PASS possibly including argument. */
9071 m = gfc_match (" pass");
9072 if (m == MATCH_ERROR)
9073 goto error;
9074 if (m == MATCH_YES)
9076 char arg[GFC_MAX_SYMBOL_LEN + 1];
9078 if (found_passing)
9080 gfc_error ("Binding attributes already specify passing,"
9081 " illegal PASS at %C");
9082 goto error;
9085 m = gfc_match (" ( %n )", arg);
9086 if (m == MATCH_ERROR)
9087 goto error;
9088 if (m == MATCH_YES)
9089 ba->pass_arg = gfc_get_string (arg);
9090 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
9092 found_passing = true;
9093 ba->nopass = 0;
9094 continue;
9097 if (ppc)
9099 /* POINTER flag. */
9100 m = gfc_match (" pointer");
9101 if (m == MATCH_ERROR)
9102 goto error;
9103 if (m == MATCH_YES)
9105 if (seen_ptr)
9107 gfc_error ("Duplicate POINTER attribute at %C");
9108 goto error;
9111 seen_ptr = true;
9112 continue;
9115 else
9117 /* NON_OVERRIDABLE flag. */
9118 m = gfc_match (" non_overridable");
9119 if (m == MATCH_ERROR)
9120 goto error;
9121 if (m == MATCH_YES)
9123 if (ba->non_overridable)
9125 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
9126 goto error;
9129 ba->non_overridable = 1;
9130 continue;
9133 /* DEFERRED flag. */
9134 m = gfc_match (" deferred");
9135 if (m == MATCH_ERROR)
9136 goto error;
9137 if (m == MATCH_YES)
9139 if (ba->deferred)
9141 gfc_error ("Duplicate DEFERRED at %C");
9142 goto error;
9145 ba->deferred = 1;
9146 continue;
9152 /* Nothing matching found. */
9153 if (generic)
9154 gfc_error ("Expected access-specifier at %C");
9155 else
9156 gfc_error ("Expected binding attribute at %C");
9157 goto error;
9159 while (gfc_match_char (',') == MATCH_YES);
9161 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
9162 if (ba->non_overridable && ba->deferred)
9164 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
9165 goto error;
9168 m = MATCH_YES;
9170 done:
9171 if (ba->access == ACCESS_UNKNOWN)
9172 ba->access = gfc_typebound_default_access;
9174 if (ppc && !seen_ptr)
9176 gfc_error ("POINTER attribute is required for procedure pointer component"
9177 " at %C");
9178 goto error;
9181 return m;
9183 error:
9184 return MATCH_ERROR;
9188 /* Match a PROCEDURE specific binding inside a derived type. */
9190 static match
9191 match_procedure_in_type (void)
9193 char name[GFC_MAX_SYMBOL_LEN + 1];
9194 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
9195 char* target = NULL, *ifc = NULL;
9196 gfc_typebound_proc tb;
9197 bool seen_colons;
9198 bool seen_attrs;
9199 match m;
9200 gfc_symtree* stree;
9201 gfc_namespace* ns;
9202 gfc_symbol* block;
9203 int num;
9205 /* Check current state. */
9206 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
9207 block = gfc_state_stack->previous->sym;
9208 gcc_assert (block);
9210 /* Try to match PROCEDURE(interface). */
9211 if (gfc_match (" (") == MATCH_YES)
9213 m = gfc_match_name (target_buf);
9214 if (m == MATCH_ERROR)
9215 return m;
9216 if (m != MATCH_YES)
9218 gfc_error ("Interface-name expected after %<(%> at %C");
9219 return MATCH_ERROR;
9222 if (gfc_match (" )") != MATCH_YES)
9224 gfc_error ("%<)%> expected at %C");
9225 return MATCH_ERROR;
9228 ifc = target_buf;
9231 /* Construct the data structure. */
9232 memset (&tb, 0, sizeof (tb));
9233 tb.where = gfc_current_locus;
9235 /* Match binding attributes. */
9236 m = match_binding_attributes (&tb, false, false);
9237 if (m == MATCH_ERROR)
9238 return m;
9239 seen_attrs = (m == MATCH_YES);
9241 /* Check that attribute DEFERRED is given if an interface is specified. */
9242 if (tb.deferred && !ifc)
9244 gfc_error ("Interface must be specified for DEFERRED binding at %C");
9245 return MATCH_ERROR;
9247 if (ifc && !tb.deferred)
9249 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
9250 return MATCH_ERROR;
9253 /* Match the colons. */
9254 m = gfc_match (" ::");
9255 if (m == MATCH_ERROR)
9256 return m;
9257 seen_colons = (m == MATCH_YES);
9258 if (seen_attrs && !seen_colons)
9260 gfc_error ("Expected %<::%> after binding-attributes at %C");
9261 return MATCH_ERROR;
9264 /* Match the binding names. */
9265 for(num=1;;num++)
9267 m = gfc_match_name (name);
9268 if (m == MATCH_ERROR)
9269 return m;
9270 if (m == MATCH_NO)
9272 gfc_error ("Expected binding name at %C");
9273 return MATCH_ERROR;
9276 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
9277 return MATCH_ERROR;
9279 /* Try to match the '=> target', if it's there. */
9280 target = ifc;
9281 m = gfc_match (" =>");
9282 if (m == MATCH_ERROR)
9283 return m;
9284 if (m == MATCH_YES)
9286 if (tb.deferred)
9288 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
9289 return MATCH_ERROR;
9292 if (!seen_colons)
9294 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
9295 " at %C");
9296 return MATCH_ERROR;
9299 m = gfc_match_name (target_buf);
9300 if (m == MATCH_ERROR)
9301 return m;
9302 if (m == MATCH_NO)
9304 gfc_error ("Expected binding target after %<=>%> at %C");
9305 return MATCH_ERROR;
9307 target = target_buf;
9310 /* If no target was found, it has the same name as the binding. */
9311 if (!target)
9312 target = name;
9314 /* Get the namespace to insert the symbols into. */
9315 ns = block->f2k_derived;
9316 gcc_assert (ns);
9318 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
9319 if (tb.deferred && !block->attr.abstract)
9321 gfc_error ("Type %qs containing DEFERRED binding at %C "
9322 "is not ABSTRACT", block->name);
9323 return MATCH_ERROR;
9326 /* See if we already have a binding with this name in the symtree which
9327 would be an error. If a GENERIC already targeted this binding, it may
9328 be already there but then typebound is still NULL. */
9329 stree = gfc_find_symtree (ns->tb_sym_root, name);
9330 if (stree && stree->n.tb)
9332 gfc_error ("There is already a procedure with binding name %qs for "
9333 "the derived type %qs at %C", name, block->name);
9334 return MATCH_ERROR;
9337 /* Insert it and set attributes. */
9339 if (!stree)
9341 stree = gfc_new_symtree (&ns->tb_sym_root, name);
9342 gcc_assert (stree);
9344 stree->n.tb = gfc_get_typebound_proc (&tb);
9346 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
9347 false))
9348 return MATCH_ERROR;
9349 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
9351 if (gfc_match_eos () == MATCH_YES)
9352 return MATCH_YES;
9353 if (gfc_match_char (',') != MATCH_YES)
9354 goto syntax;
9357 syntax:
9358 gfc_error ("Syntax error in PROCEDURE statement at %C");
9359 return MATCH_ERROR;
9363 /* Match a GENERIC procedure binding inside a derived type. */
9365 match
9366 gfc_match_generic (void)
9368 char name[GFC_MAX_SYMBOL_LEN + 1];
9369 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
9370 gfc_symbol* block;
9371 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
9372 gfc_typebound_proc* tb;
9373 gfc_namespace* ns;
9374 interface_type op_type;
9375 gfc_intrinsic_op op;
9376 match m;
9378 /* Check current state. */
9379 if (gfc_current_state () == COMP_DERIVED)
9381 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
9382 return MATCH_ERROR;
9384 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
9385 return MATCH_NO;
9386 block = gfc_state_stack->previous->sym;
9387 ns = block->f2k_derived;
9388 gcc_assert (block && ns);
9390 memset (&tbattr, 0, sizeof (tbattr));
9391 tbattr.where = gfc_current_locus;
9393 /* See if we get an access-specifier. */
9394 m = match_binding_attributes (&tbattr, true, false);
9395 if (m == MATCH_ERROR)
9396 goto error;
9398 /* Now the colons, those are required. */
9399 if (gfc_match (" ::") != MATCH_YES)
9401 gfc_error ("Expected %<::%> at %C");
9402 goto error;
9405 /* Match the binding name; depending on type (operator / generic) format
9406 it for future error messages into bind_name. */
9408 m = gfc_match_generic_spec (&op_type, name, &op);
9409 if (m == MATCH_ERROR)
9410 return MATCH_ERROR;
9411 if (m == MATCH_NO)
9413 gfc_error ("Expected generic name or operator descriptor at %C");
9414 goto error;
9417 switch (op_type)
9419 case INTERFACE_GENERIC:
9420 snprintf (bind_name, sizeof (bind_name), "%s", name);
9421 break;
9423 case INTERFACE_USER_OP:
9424 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
9425 break;
9427 case INTERFACE_INTRINSIC_OP:
9428 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
9429 gfc_op2string (op));
9430 break;
9432 case INTERFACE_NAMELESS:
9433 gfc_error ("Malformed GENERIC statement at %C");
9434 goto error;
9435 break;
9437 default:
9438 gcc_unreachable ();
9441 /* Match the required =>. */
9442 if (gfc_match (" =>") != MATCH_YES)
9444 gfc_error ("Expected %<=>%> at %C");
9445 goto error;
9448 /* Try to find existing GENERIC binding with this name / for this operator;
9449 if there is something, check that it is another GENERIC and then extend
9450 it rather than building a new node. Otherwise, create it and put it
9451 at the right position. */
9453 switch (op_type)
9455 case INTERFACE_USER_OP:
9456 case INTERFACE_GENERIC:
9458 const bool is_op = (op_type == INTERFACE_USER_OP);
9459 gfc_symtree* st;
9461 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
9462 if (st)
9464 tb = st->n.tb;
9465 gcc_assert (tb);
9467 else
9468 tb = NULL;
9470 break;
9473 case INTERFACE_INTRINSIC_OP:
9474 tb = ns->tb_op[op];
9475 break;
9477 default:
9478 gcc_unreachable ();
9481 if (tb)
9483 if (!tb->is_generic)
9485 gcc_assert (op_type == INTERFACE_GENERIC);
9486 gfc_error ("There's already a non-generic procedure with binding name"
9487 " %qs for the derived type %qs at %C",
9488 bind_name, block->name);
9489 goto error;
9492 if (tb->access != tbattr.access)
9494 gfc_error ("Binding at %C must have the same access as already"
9495 " defined binding %qs", bind_name);
9496 goto error;
9499 else
9501 tb = gfc_get_typebound_proc (NULL);
9502 tb->where = gfc_current_locus;
9503 tb->access = tbattr.access;
9504 tb->is_generic = 1;
9505 tb->u.generic = NULL;
9507 switch (op_type)
9509 case INTERFACE_GENERIC:
9510 case INTERFACE_USER_OP:
9512 const bool is_op = (op_type == INTERFACE_USER_OP);
9513 gfc_symtree* st;
9515 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
9516 name);
9517 gcc_assert (st);
9518 st->n.tb = tb;
9520 break;
9523 case INTERFACE_INTRINSIC_OP:
9524 ns->tb_op[op] = tb;
9525 break;
9527 default:
9528 gcc_unreachable ();
9532 /* Now, match all following names as specific targets. */
9535 gfc_symtree* target_st;
9536 gfc_tbp_generic* target;
9538 m = gfc_match_name (name);
9539 if (m == MATCH_ERROR)
9540 goto error;
9541 if (m == MATCH_NO)
9543 gfc_error ("Expected specific binding name at %C");
9544 goto error;
9547 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
9549 /* See if this is a duplicate specification. */
9550 for (target = tb->u.generic; target; target = target->next)
9551 if (target_st == target->specific_st)
9553 gfc_error ("%qs already defined as specific binding for the"
9554 " generic %qs at %C", name, bind_name);
9555 goto error;
9558 target = gfc_get_tbp_generic ();
9559 target->specific_st = target_st;
9560 target->specific = NULL;
9561 target->next = tb->u.generic;
9562 target->is_operator = ((op_type == INTERFACE_USER_OP)
9563 || (op_type == INTERFACE_INTRINSIC_OP));
9564 tb->u.generic = target;
9566 while (gfc_match (" ,") == MATCH_YES);
9568 /* Here should be the end. */
9569 if (gfc_match_eos () != MATCH_YES)
9571 gfc_error ("Junk after GENERIC binding at %C");
9572 goto error;
9575 return MATCH_YES;
9577 error:
9578 return MATCH_ERROR;
9582 /* Match a FINAL declaration inside a derived type. */
9584 match
9585 gfc_match_final_decl (void)
9587 char name[GFC_MAX_SYMBOL_LEN + 1];
9588 gfc_symbol* sym;
9589 match m;
9590 gfc_namespace* module_ns;
9591 bool first, last;
9592 gfc_symbol* block;
9594 if (gfc_current_form == FORM_FREE)
9596 char c = gfc_peek_ascii_char ();
9597 if (!gfc_is_whitespace (c) && c != ':')
9598 return MATCH_NO;
9601 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
9603 if (gfc_current_form == FORM_FIXED)
9604 return MATCH_NO;
9606 gfc_error ("FINAL declaration at %C must be inside a derived type "
9607 "CONTAINS section");
9608 return MATCH_ERROR;
9611 block = gfc_state_stack->previous->sym;
9612 gcc_assert (block);
9614 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
9615 || gfc_state_stack->previous->previous->state != COMP_MODULE)
9617 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9618 " specification part of a MODULE");
9619 return MATCH_ERROR;
9622 module_ns = gfc_current_ns;
9623 gcc_assert (module_ns);
9624 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
9626 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9627 if (gfc_match (" ::") == MATCH_ERROR)
9628 return MATCH_ERROR;
9630 /* Match the sequence of procedure names. */
9631 first = true;
9632 last = false;
9635 gfc_finalizer* f;
9637 if (first && gfc_match_eos () == MATCH_YES)
9639 gfc_error ("Empty FINAL at %C");
9640 return MATCH_ERROR;
9643 m = gfc_match_name (name);
9644 if (m == MATCH_NO)
9646 gfc_error ("Expected module procedure name at %C");
9647 return MATCH_ERROR;
9649 else if (m != MATCH_YES)
9650 return MATCH_ERROR;
9652 if (gfc_match_eos () == MATCH_YES)
9653 last = true;
9654 if (!last && gfc_match_char (',') != MATCH_YES)
9656 gfc_error ("Expected %<,%> at %C");
9657 return MATCH_ERROR;
9660 if (gfc_get_symbol (name, module_ns, &sym))
9662 gfc_error ("Unknown procedure name %qs at %C", name);
9663 return MATCH_ERROR;
9666 /* Mark the symbol as module procedure. */
9667 if (sym->attr.proc != PROC_MODULE
9668 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9669 return MATCH_ERROR;
9671 /* Check if we already have this symbol in the list, this is an error. */
9672 for (f = block->f2k_derived->finalizers; f; f = f->next)
9673 if (f->proc_sym == sym)
9675 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9676 name);
9677 return MATCH_ERROR;
9680 /* Add this symbol to the list of finalizers. */
9681 gcc_assert (block->f2k_derived);
9682 sym->refs++;
9683 f = XCNEW (gfc_finalizer);
9684 f->proc_sym = sym;
9685 f->proc_tree = NULL;
9686 f->where = gfc_current_locus;
9687 f->next = block->f2k_derived->finalizers;
9688 block->f2k_derived->finalizers = f;
9690 first = false;
9692 while (!last);
9694 return MATCH_YES;
9698 const ext_attr_t ext_attr_list[] = {
9699 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9700 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9701 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9702 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9703 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9704 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9705 { NULL, EXT_ATTR_LAST, NULL }
9708 /* Match a !GCC$ ATTRIBUTES statement of the form:
9709 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9710 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9712 TODO: We should support all GCC attributes using the same syntax for
9713 the attribute list, i.e. the list in C
9714 __attributes(( attribute-list ))
9715 matches then
9716 !GCC$ ATTRIBUTES attribute-list ::
9717 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9718 saved into a TREE.
9720 As there is absolutely no risk of confusion, we should never return
9721 MATCH_NO. */
9722 match
9723 gfc_match_gcc_attributes (void)
9725 symbol_attribute attr;
9726 char name[GFC_MAX_SYMBOL_LEN + 1];
9727 unsigned id;
9728 gfc_symbol *sym;
9729 match m;
9731 gfc_clear_attr (&attr);
9732 for(;;)
9734 char ch;
9736 if (gfc_match_name (name) != MATCH_YES)
9737 return MATCH_ERROR;
9739 for (id = 0; id < EXT_ATTR_LAST; id++)
9740 if (strcmp (name, ext_attr_list[id].name) == 0)
9741 break;
9743 if (id == EXT_ATTR_LAST)
9745 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9746 return MATCH_ERROR;
9749 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
9750 return MATCH_ERROR;
9752 gfc_gobble_whitespace ();
9753 ch = gfc_next_ascii_char ();
9754 if (ch == ':')
9756 /* This is the successful exit condition for the loop. */
9757 if (gfc_next_ascii_char () == ':')
9758 break;
9761 if (ch == ',')
9762 continue;
9764 goto syntax;
9767 if (gfc_match_eos () == MATCH_YES)
9768 goto syntax;
9770 for(;;)
9772 m = gfc_match_name (name);
9773 if (m != MATCH_YES)
9774 return m;
9776 if (find_special (name, &sym, true))
9777 return MATCH_ERROR;
9779 sym->attr.ext_attr |= attr.ext_attr;
9781 if (gfc_match_eos () == MATCH_YES)
9782 break;
9784 if (gfc_match_char (',') != MATCH_YES)
9785 goto syntax;
9788 return MATCH_YES;
9790 syntax:
9791 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9792 return MATCH_ERROR;