target-supports.exp (check_effective_target_weak_undefined): Return 0 on hppa*-*...
[official-gcc.git] / gcc / fortran / decl.c
blob31c7fb6325ce43c60f3e68fe258ad729f85ec91e
1 /* Declaration statement matcher
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "stringpool.h"
28 #include "match.h"
29 #include "parse.h"
30 #include "constructor.h"
31 #include "target.h"
33 /* Macros to access allocate memory for gfc_data_variable,
34 gfc_data_value and gfc_data. */
35 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
36 #define gfc_get_data_value() XCNEW (gfc_data_value)
37 #define gfc_get_data() XCNEW (gfc_data)
40 static bool set_binding_label (const char **, const char *, int);
43 /* This flag is set if an old-style length selector is matched
44 during a type-declaration statement. */
46 static int old_char_selector;
48 /* When variables acquire types and attributes from a declaration
49 statement, they get them from the following static variables. The
50 first part of a declaration sets these variables and the second
51 part copies these into symbol structures. */
53 static gfc_typespec current_ts;
55 static symbol_attribute current_attr;
56 static gfc_array_spec *current_as;
57 static int colon_seen;
58 static int attr_seen;
60 /* The current binding label (if any). */
61 static const char* curr_binding_label;
62 /* Need to know how many identifiers are on the current data declaration
63 line in case we're given the BIND(C) attribute with a NAME= specifier. */
64 static int num_idents_on_line;
65 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
66 can supply a name if the curr_binding_label is nil and NAME= was not. */
67 static int has_name_equals = 0;
69 /* Initializer of the previous enumerator. */
71 static gfc_expr *last_initializer;
73 /* History of all the enumerators is maintained, so that
74 kind values of all the enumerators could be updated depending
75 upon the maximum initialized value. */
77 typedef struct enumerator_history
79 gfc_symbol *sym;
80 gfc_expr *initializer;
81 struct enumerator_history *next;
83 enumerator_history;
85 /* Header of enum history chain. */
87 static enumerator_history *enum_history = NULL;
89 /* Pointer of enum history node containing largest initializer. */
91 static enumerator_history *max_enum = NULL;
93 /* gfc_new_block points to the symbol of a newly matched block. */
95 gfc_symbol *gfc_new_block;
97 bool gfc_matching_function;
99 /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
100 int directive_unroll = -1;
102 /* Map of middle-end built-ins that should be vectorized. */
103 hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
105 /* If a kind expression of a component of a parameterized derived type is
106 parameterized, temporarily store the expression here. */
107 static gfc_expr *saved_kind_expr = NULL;
109 /* Used to store the parameter list arising in a PDT declaration and
110 in the typespec of a PDT variable or component. */
111 static gfc_actual_arglist *decl_type_param_list;
112 static gfc_actual_arglist *type_param_spec_list;
114 /********************* DATA statement subroutines *********************/
116 static bool in_match_data = false;
118 bool
119 gfc_in_match_data (void)
121 return in_match_data;
124 static void
125 set_in_match_data (bool set_value)
127 in_match_data = set_value;
130 /* Free a gfc_data_variable structure and everything beneath it. */
132 static void
133 free_variable (gfc_data_variable *p)
135 gfc_data_variable *q;
137 for (; p; p = q)
139 q = p->next;
140 gfc_free_expr (p->expr);
141 gfc_free_iterator (&p->iter, 0);
142 free_variable (p->list);
143 free (p);
148 /* Free a gfc_data_value structure and everything beneath it. */
150 static void
151 free_value (gfc_data_value *p)
153 gfc_data_value *q;
155 for (; p; p = q)
157 q = p->next;
158 mpz_clear (p->repeat);
159 gfc_free_expr (p->expr);
160 free (p);
165 /* Free a list of gfc_data structures. */
167 void
168 gfc_free_data (gfc_data *p)
170 gfc_data *q;
172 for (; p; p = q)
174 q = p->next;
175 free_variable (p->var);
176 free_value (p->value);
177 free (p);
182 /* Free all data in a namespace. */
184 static void
185 gfc_free_data_all (gfc_namespace *ns)
187 gfc_data *d;
189 for (;ns->data;)
191 d = ns->data->next;
192 free (ns->data);
193 ns->data = d;
197 /* Reject data parsed since the last restore point was marked. */
199 void
200 gfc_reject_data (gfc_namespace *ns)
202 gfc_data *d;
204 while (ns->data && ns->data != ns->old_data)
206 d = ns->data->next;
207 free (ns->data);
208 ns->data = d;
212 static match var_element (gfc_data_variable *);
214 /* Match a list of variables terminated by an iterator and a right
215 parenthesis. */
217 static match
218 var_list (gfc_data_variable *parent)
220 gfc_data_variable *tail, var;
221 match m;
223 m = var_element (&var);
224 if (m == MATCH_ERROR)
225 return MATCH_ERROR;
226 if (m == MATCH_NO)
227 goto syntax;
229 tail = gfc_get_data_variable ();
230 *tail = var;
232 parent->list = tail;
234 for (;;)
236 if (gfc_match_char (',') != MATCH_YES)
237 goto syntax;
239 m = gfc_match_iterator (&parent->iter, 1);
240 if (m == MATCH_YES)
241 break;
242 if (m == MATCH_ERROR)
243 return MATCH_ERROR;
245 m = var_element (&var);
246 if (m == MATCH_ERROR)
247 return MATCH_ERROR;
248 if (m == MATCH_NO)
249 goto syntax;
251 tail->next = gfc_get_data_variable ();
252 tail = tail->next;
254 *tail = var;
257 if (gfc_match_char (')') != MATCH_YES)
258 goto syntax;
259 return MATCH_YES;
261 syntax:
262 gfc_syntax_error (ST_DATA);
263 return MATCH_ERROR;
267 /* Match a single element in a data variable list, which can be a
268 variable-iterator list. */
270 static match
271 var_element (gfc_data_variable *new_var)
273 match m;
274 gfc_symbol *sym;
276 memset (new_var, 0, sizeof (gfc_data_variable));
278 if (gfc_match_char ('(') == MATCH_YES)
279 return var_list (new_var);
281 m = gfc_match_variable (&new_var->expr, 0);
282 if (m != MATCH_YES)
283 return m;
285 if (new_var->expr->expr_type == EXPR_CONSTANT
286 && new_var->expr->symtree == NULL)
288 gfc_error ("Inquiry parameter cannot appear in a "
289 "data-stmt-object-list at %C");
290 return MATCH_ERROR;
293 sym = new_var->expr->symtree->n.sym;
295 /* Symbol should already have an associated type. */
296 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
297 return MATCH_ERROR;
299 if (!sym->attr.function && gfc_current_ns->parent
300 && gfc_current_ns->parent == sym->ns)
302 gfc_error ("Host associated variable %qs may not be in the DATA "
303 "statement at %C", sym->name);
304 return MATCH_ERROR;
307 if (gfc_current_state () != COMP_BLOCK_DATA
308 && sym->attr.in_common
309 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
310 "common block variable %qs in DATA statement at %C",
311 sym->name))
312 return MATCH_ERROR;
314 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
315 return MATCH_ERROR;
317 return MATCH_YES;
321 /* Match the top-level list of data variables. */
323 static match
324 top_var_list (gfc_data *d)
326 gfc_data_variable var, *tail, *new_var;
327 match m;
329 tail = NULL;
331 for (;;)
333 m = var_element (&var);
334 if (m == MATCH_NO)
335 goto syntax;
336 if (m == MATCH_ERROR)
337 return MATCH_ERROR;
339 new_var = gfc_get_data_variable ();
340 *new_var = var;
341 if (new_var->expr)
342 new_var->expr->where = gfc_current_locus;
344 if (tail == NULL)
345 d->var = new_var;
346 else
347 tail->next = new_var;
349 tail = new_var;
351 if (gfc_match_char ('/') == MATCH_YES)
352 break;
353 if (gfc_match_char (',') != MATCH_YES)
354 goto syntax;
357 return MATCH_YES;
359 syntax:
360 gfc_syntax_error (ST_DATA);
361 gfc_free_data_all (gfc_current_ns);
362 return MATCH_ERROR;
366 static match
367 match_data_constant (gfc_expr **result)
369 char name[GFC_MAX_SYMBOL_LEN + 1];
370 gfc_symbol *sym, *dt_sym = NULL;
371 gfc_expr *expr;
372 match m;
373 locus old_loc;
375 m = gfc_match_literal_constant (&expr, 1);
376 if (m == MATCH_YES)
378 *result = expr;
379 return MATCH_YES;
382 if (m == MATCH_ERROR)
383 return MATCH_ERROR;
385 m = gfc_match_null (result);
386 if (m != MATCH_NO)
387 return m;
389 old_loc = gfc_current_locus;
391 /* Should this be a structure component, try to match it
392 before matching a name. */
393 m = gfc_match_rvalue (result);
394 if (m == MATCH_ERROR)
395 return m;
397 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
399 if (!gfc_simplify_expr (*result, 0))
400 m = MATCH_ERROR;
401 return m;
403 else if (m == MATCH_YES)
405 /* If a parameter inquiry ends up here, symtree is NULL but **result
406 contains the right constant expression. Check here. */
407 if ((*result)->symtree == NULL
408 && (*result)->expr_type == EXPR_CONSTANT
409 && ((*result)->ts.type == BT_INTEGER
410 || (*result)->ts.type == BT_REAL))
411 return m;
413 /* F2018:R845 data-stmt-constant is initial-data-target.
414 A data-stmt-constant shall be ... initial-data-target if and
415 only if the corresponding data-stmt-object has the POINTER
416 attribute. ... If data-stmt-constant is initial-data-target
417 the corresponding data statement object shall be
418 data-pointer-initialization compatible (7.5.4.6) with the initial
419 data target; the data statement object is initially associated
420 with the target. */
421 if ((*result)->symtree->n.sym->attr.save
422 && (*result)->symtree->n.sym->attr.target)
423 return m;
424 gfc_free_expr (*result);
427 gfc_current_locus = old_loc;
429 m = gfc_match_name (name);
430 if (m != MATCH_YES)
431 return m;
433 if (gfc_find_symbol (name, NULL, 1, &sym))
434 return MATCH_ERROR;
436 if (sym && sym->attr.generic)
437 dt_sym = gfc_find_dt_in_generic (sym);
439 if (sym == NULL
440 || (sym->attr.flavor != FL_PARAMETER
441 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
443 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
444 name);
445 *result = NULL;
446 return MATCH_ERROR;
448 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
449 return gfc_match_structure_constructor (dt_sym, result);
451 /* Check to see if the value is an initialization array expression. */
452 if (sym->value->expr_type == EXPR_ARRAY)
454 gfc_current_locus = old_loc;
456 m = gfc_match_init_expr (result);
457 if (m == MATCH_ERROR)
458 return m;
460 if (m == MATCH_YES)
462 if (!gfc_simplify_expr (*result, 0))
463 m = MATCH_ERROR;
465 if ((*result)->expr_type == EXPR_CONSTANT)
466 return m;
467 else
469 gfc_error ("Invalid initializer %s in Data statement at %C", name);
470 return MATCH_ERROR;
475 *result = gfc_copy_expr (sym->value);
476 return MATCH_YES;
480 /* Match a list of values in a DATA statement. The leading '/' has
481 already been seen at this point. */
483 static match
484 top_val_list (gfc_data *data)
486 gfc_data_value *new_val, *tail;
487 gfc_expr *expr;
488 match m;
490 tail = NULL;
492 for (;;)
494 m = match_data_constant (&expr);
495 if (m == MATCH_NO)
496 goto syntax;
497 if (m == MATCH_ERROR)
498 return MATCH_ERROR;
500 new_val = gfc_get_data_value ();
501 mpz_init (new_val->repeat);
503 if (tail == NULL)
504 data->value = new_val;
505 else
506 tail->next = new_val;
508 tail = new_val;
510 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
512 tail->expr = expr;
513 mpz_set_ui (tail->repeat, 1);
515 else
517 mpz_set (tail->repeat, expr->value.integer);
518 gfc_free_expr (expr);
520 m = match_data_constant (&tail->expr);
521 if (m == MATCH_NO)
522 goto syntax;
523 if (m == MATCH_ERROR)
524 return MATCH_ERROR;
527 if (gfc_match_char ('/') == MATCH_YES)
528 break;
529 if (gfc_match_char (',') == MATCH_NO)
530 goto syntax;
533 return MATCH_YES;
535 syntax:
536 gfc_syntax_error (ST_DATA);
537 gfc_free_data_all (gfc_current_ns);
538 return MATCH_ERROR;
542 /* Matches an old style initialization. */
544 static match
545 match_old_style_init (const char *name)
547 match m;
548 gfc_symtree *st;
549 gfc_symbol *sym;
550 gfc_data *newdata;
552 /* Set up data structure to hold initializers. */
553 gfc_find_sym_tree (name, NULL, 0, &st);
554 sym = st->n.sym;
556 newdata = gfc_get_data ();
557 newdata->var = gfc_get_data_variable ();
558 newdata->var->expr = gfc_get_variable_expr (st);
559 newdata->var->expr->where = sym->declared_at;
560 newdata->where = gfc_current_locus;
562 /* Match initial value list. This also eats the terminal '/'. */
563 m = top_val_list (newdata);
564 if (m != MATCH_YES)
566 free (newdata);
567 return m;
570 if (gfc_pure (NULL))
572 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
573 free (newdata);
574 return MATCH_ERROR;
576 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
578 /* Mark the variable as having appeared in a data statement. */
579 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
581 free (newdata);
582 return MATCH_ERROR;
585 /* Chain in namespace list of DATA initializers. */
586 newdata->next = gfc_current_ns->data;
587 gfc_current_ns->data = newdata;
589 return m;
593 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
594 we are matching a DATA statement and are therefore issuing an error
595 if we encounter something unexpected, if not, we're trying to match
596 an old-style initialization expression of the form INTEGER I /2/. */
598 match
599 gfc_match_data (void)
601 gfc_data *new_data;
602 gfc_expr *e;
603 gfc_ref *ref;
604 match m;
606 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
607 if ((gfc_current_state () == COMP_FUNCTION
608 || gfc_current_state () == COMP_SUBROUTINE)
609 && gfc_state_stack->previous->state == COMP_INTERFACE)
611 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
612 return MATCH_ERROR;
615 set_in_match_data (true);
617 for (;;)
619 new_data = gfc_get_data ();
620 new_data->where = gfc_current_locus;
622 m = top_var_list (new_data);
623 if (m != MATCH_YES)
624 goto cleanup;
626 if (new_data->var->iter.var
627 && new_data->var->iter.var->ts.type == BT_INTEGER
628 && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
629 && new_data->var->list
630 && new_data->var->list->expr
631 && new_data->var->list->expr->ts.type == BT_CHARACTER
632 && new_data->var->list->expr->ref
633 && new_data->var->list->expr->ref->type == REF_SUBSTRING)
635 gfc_error ("Invalid substring in data-implied-do at %L in DATA "
636 "statement", &new_data->var->list->expr->where);
637 goto cleanup;
640 /* Check for an entity with an allocatable component, which is not
641 allowed. */
642 e = new_data->var->expr;
643 if (e)
645 bool invalid;
647 invalid = false;
648 for (ref = e->ref; ref; ref = ref->next)
649 if ((ref->type == REF_COMPONENT
650 && ref->u.c.component->attr.allocatable)
651 || (ref->type == REF_ARRAY
652 && e->symtree->n.sym->attr.pointer != 1
653 && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
654 invalid = true;
656 if (invalid)
658 gfc_error ("Allocatable component or deferred-shaped array "
659 "near %C in DATA statement");
660 goto cleanup;
663 /* F2008:C567 (R536) A data-i-do-object or a variable that appears
664 as a data-stmt-object shall not be an object designator in which
665 a pointer appears other than as the entire rightmost part-ref. */
666 ref = e->ref;
667 if (e->symtree->n.sym->ts.type == BT_DERIVED
668 && e->symtree->n.sym->attr.pointer
669 && ref->type == REF_COMPONENT)
670 goto partref;
672 for (; ref; ref = ref->next)
673 if (ref->type == REF_COMPONENT
674 && ref->u.c.component->attr.pointer
675 && ref->next)
676 goto partref;
679 m = top_val_list (new_data);
680 if (m != MATCH_YES)
681 goto cleanup;
683 new_data->next = gfc_current_ns->data;
684 gfc_current_ns->data = new_data;
686 if (gfc_match_eos () == MATCH_YES)
687 break;
689 gfc_match_char (','); /* Optional comma */
692 set_in_match_data (false);
694 if (gfc_pure (NULL))
696 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
697 return MATCH_ERROR;
699 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
701 return MATCH_YES;
703 partref:
705 gfc_error ("part-ref with pointer attribute near %L is not "
706 "rightmost part-ref of data-stmt-object",
707 &e->where);
709 cleanup:
710 set_in_match_data (false);
711 gfc_free_data (new_data);
712 return MATCH_ERROR;
716 /************************ Declaration statements *********************/
719 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
720 list). The difference here is the expression is a list of constants
721 and is surrounded by '/'.
722 The typespec ts must match the typespec of the variable which the
723 clist is initializing.
724 The arrayspec tells whether this should match a list of constants
725 corresponding to array elements or a scalar (as == NULL). */
727 static match
728 match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
730 gfc_constructor_base array_head = NULL;
731 gfc_expr *expr = NULL;
732 match m = MATCH_ERROR;
733 locus where;
734 mpz_t repeat, cons_size, as_size;
735 bool scalar;
736 int cmp;
738 gcc_assert (ts);
740 /* We have already matched '/' - now look for a constant list, as with
741 top_val_list from decl.c, but append the result to an array. */
742 if (gfc_match ("/") == MATCH_YES)
744 gfc_error ("Empty old style initializer list at %C");
745 return MATCH_ERROR;
748 where = gfc_current_locus;
749 scalar = !as || !as->rank;
751 if (!scalar && !spec_size (as, &as_size))
753 gfc_error ("Array in initializer list at %L must have an explicit shape",
754 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
755 /* Nothing to cleanup yet. */
756 return MATCH_ERROR;
759 mpz_init_set_ui (repeat, 0);
761 for (;;)
763 m = match_data_constant (&expr);
764 if (m != MATCH_YES)
765 expr = NULL; /* match_data_constant may set expr to garbage */
766 if (m == MATCH_NO)
767 goto syntax;
768 if (m == MATCH_ERROR)
769 goto cleanup;
771 /* Found r in repeat spec r*c; look for the constant to repeat. */
772 if ( gfc_match_char ('*') == MATCH_YES)
774 if (scalar)
776 gfc_error ("Repeat spec invalid in scalar initializer at %C");
777 goto cleanup;
779 if (expr->ts.type != BT_INTEGER)
781 gfc_error ("Repeat spec must be an integer at %C");
782 goto cleanup;
784 mpz_set (repeat, expr->value.integer);
785 gfc_free_expr (expr);
786 expr = NULL;
788 m = match_data_constant (&expr);
789 if (m == MATCH_NO)
791 m = MATCH_ERROR;
792 gfc_error ("Expected data constant after repeat spec at %C");
794 if (m != MATCH_YES)
795 goto cleanup;
797 /* No repeat spec, we matched the data constant itself. */
798 else
799 mpz_set_ui (repeat, 1);
801 if (!scalar)
803 /* Add the constant initializer as many times as repeated. */
804 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
806 /* Make sure types of elements match */
807 if(ts && !gfc_compare_types (&expr->ts, ts)
808 && !gfc_convert_type (expr, ts, 1))
809 goto cleanup;
811 gfc_constructor_append_expr (&array_head,
812 gfc_copy_expr (expr), &gfc_current_locus);
815 gfc_free_expr (expr);
816 expr = NULL;
819 /* For scalar initializers quit after one element. */
820 else
822 if(gfc_match_char ('/') != MATCH_YES)
824 gfc_error ("End of scalar initializer expected at %C");
825 goto cleanup;
827 break;
830 if (gfc_match_char ('/') == MATCH_YES)
831 break;
832 if (gfc_match_char (',') == MATCH_NO)
833 goto syntax;
836 /* If we break early from here out, we encountered an error. */
837 m = MATCH_ERROR;
839 /* Set up expr as an array constructor. */
840 if (!scalar)
842 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
843 expr->ts = *ts;
844 expr->value.constructor = array_head;
846 expr->rank = as->rank;
847 expr->shape = gfc_get_shape (expr->rank);
849 /* Validate sizes. We built expr ourselves, so cons_size will be
850 constant (we fail above for non-constant expressions).
851 We still need to verify that the sizes match. */
852 gcc_assert (gfc_array_size (expr, &cons_size));
853 cmp = mpz_cmp (cons_size, as_size);
854 if (cmp < 0)
855 gfc_error ("Not enough elements in array initializer at %C");
856 else if (cmp > 0)
857 gfc_error ("Too many elements in array initializer at %C");
858 mpz_clear (cons_size);
859 if (cmp)
860 goto cleanup;
863 /* Make sure scalar types match. */
864 else if (!gfc_compare_types (&expr->ts, ts)
865 && !gfc_convert_type (expr, ts, 1))
866 goto cleanup;
868 if (expr->ts.u.cl)
869 expr->ts.u.cl->length_from_typespec = 1;
871 *result = expr;
872 m = MATCH_YES;
873 goto done;
875 syntax:
876 m = MATCH_ERROR;
877 gfc_error ("Syntax error in old style initializer list at %C");
879 cleanup:
880 if (expr)
881 expr->value.constructor = NULL;
882 gfc_free_expr (expr);
883 gfc_constructor_free (array_head);
885 done:
886 mpz_clear (repeat);
887 if (!scalar)
888 mpz_clear (as_size);
889 return m;
893 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
895 static bool
896 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
898 int i, j;
900 if ((from->type == AS_ASSUMED_RANK && to->corank)
901 || (to->type == AS_ASSUMED_RANK && from->corank))
903 gfc_error ("The assumed-rank array at %C shall not have a codimension");
904 return false;
907 if (to->rank == 0 && from->rank > 0)
909 to->rank = from->rank;
910 to->type = from->type;
911 to->cray_pointee = from->cray_pointee;
912 to->cp_was_assumed = from->cp_was_assumed;
914 for (i = 0; i < to->corank; i++)
916 /* Do not exceed the limits on lower[] and upper[]. gfortran
917 cleans up elsewhere. */
918 j = from->rank + i;
919 if (j >= GFC_MAX_DIMENSIONS)
920 break;
922 to->lower[j] = to->lower[i];
923 to->upper[j] = to->upper[i];
925 for (i = 0; i < from->rank; i++)
927 if (copy)
929 to->lower[i] = gfc_copy_expr (from->lower[i]);
930 to->upper[i] = gfc_copy_expr (from->upper[i]);
932 else
934 to->lower[i] = from->lower[i];
935 to->upper[i] = from->upper[i];
939 else if (to->corank == 0 && from->corank > 0)
941 to->corank = from->corank;
942 to->cotype = from->cotype;
944 for (i = 0; i < from->corank; i++)
946 /* Do not exceed the limits on lower[] and upper[]. gfortran
947 cleans up elsewhere. */
948 j = to->rank + i;
949 if (j >= GFC_MAX_DIMENSIONS)
950 break;
952 if (copy)
954 to->lower[j] = gfc_copy_expr (from->lower[i]);
955 to->upper[j] = gfc_copy_expr (from->upper[i]);
957 else
959 to->lower[j] = from->lower[i];
960 to->upper[j] = from->upper[i];
965 if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
967 gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
968 "allowed dimensions of %d",
969 to->rank, to->corank, GFC_MAX_DIMENSIONS);
970 to->corank = GFC_MAX_DIMENSIONS - to->rank;
971 return false;
973 return true;
977 /* Match an intent specification. Since this can only happen after an
978 INTENT word, a legal intent-spec must follow. */
980 static sym_intent
981 match_intent_spec (void)
984 if (gfc_match (" ( in out )") == MATCH_YES)
985 return INTENT_INOUT;
986 if (gfc_match (" ( in )") == MATCH_YES)
987 return INTENT_IN;
988 if (gfc_match (" ( out )") == MATCH_YES)
989 return INTENT_OUT;
991 gfc_error ("Bad INTENT specification at %C");
992 return INTENT_UNKNOWN;
996 /* Matches a character length specification, which is either a
997 specification expression, '*', or ':'. */
999 static match
1000 char_len_param_value (gfc_expr **expr, bool *deferred)
1002 match m;
1004 *expr = NULL;
1005 *deferred = false;
1007 if (gfc_match_char ('*') == MATCH_YES)
1008 return MATCH_YES;
1010 if (gfc_match_char (':') == MATCH_YES)
1012 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1013 return MATCH_ERROR;
1015 *deferred = true;
1017 return MATCH_YES;
1020 m = gfc_match_expr (expr);
1022 if (m == MATCH_NO || m == MATCH_ERROR)
1023 return m;
1025 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1026 return MATCH_ERROR;
1028 if ((*expr)->expr_type == EXPR_FUNCTION)
1030 if ((*expr)->ts.type == BT_INTEGER
1031 || ((*expr)->ts.type == BT_UNKNOWN
1032 && strcmp((*expr)->symtree->name, "null") != 0))
1033 return MATCH_YES;
1035 goto syntax;
1037 else if ((*expr)->expr_type == EXPR_CONSTANT)
1039 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1040 processor dependent and its value is greater than or equal to zero.
1041 F2008, 4.4.3.2: If the character length parameter value evaluates
1042 to a negative value, the length of character entities declared
1043 is zero. */
1045 if ((*expr)->ts.type == BT_INTEGER)
1047 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1048 mpz_set_si ((*expr)->value.integer, 0);
1050 else
1051 goto syntax;
1053 else if ((*expr)->expr_type == EXPR_ARRAY)
1054 goto syntax;
1055 else if ((*expr)->expr_type == EXPR_VARIABLE)
1057 bool t;
1058 gfc_expr *e;
1060 e = gfc_copy_expr (*expr);
1062 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1063 which causes an ICE if gfc_reduce_init_expr() is called. */
1064 if (e->ref && e->ref->type == REF_ARRAY
1065 && e->ref->u.ar.type == AR_UNKNOWN
1066 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1067 goto syntax;
1069 t = gfc_reduce_init_expr (e);
1071 if (!t && e->ts.type == BT_UNKNOWN
1072 && e->symtree->n.sym->attr.untyped == 1
1073 && (flag_implicit_none
1074 || e->symtree->n.sym->ns->seen_implicit_none == 1
1075 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1077 gfc_free_expr (e);
1078 goto syntax;
1081 if ((e->ref && e->ref->type == REF_ARRAY
1082 && e->ref->u.ar.type != AR_ELEMENT)
1083 || (!e->ref && e->expr_type == EXPR_ARRAY))
1085 gfc_free_expr (e);
1086 goto syntax;
1089 gfc_free_expr (e);
1092 return m;
1094 syntax:
1095 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1096 return MATCH_ERROR;
1100 /* A character length is a '*' followed by a literal integer or a
1101 char_len_param_value in parenthesis. */
1103 static match
1104 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1106 int length;
1107 match m;
1109 *deferred = false;
1110 m = gfc_match_char ('*');
1111 if (m != MATCH_YES)
1112 return m;
1114 m = gfc_match_small_literal_int (&length, NULL);
1115 if (m == MATCH_ERROR)
1116 return m;
1118 if (m == MATCH_YES)
1120 if (obsolescent_check
1121 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1122 return MATCH_ERROR;
1123 *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1124 return m;
1127 if (gfc_match_char ('(') == MATCH_NO)
1128 goto syntax;
1130 m = char_len_param_value (expr, deferred);
1131 if (m != MATCH_YES && gfc_matching_function)
1133 gfc_undo_symbols ();
1134 m = MATCH_YES;
1137 if (m == MATCH_ERROR)
1138 return m;
1139 if (m == MATCH_NO)
1140 goto syntax;
1142 if (gfc_match_char (')') == MATCH_NO)
1144 gfc_free_expr (*expr);
1145 *expr = NULL;
1146 goto syntax;
1149 return MATCH_YES;
1151 syntax:
1152 gfc_error ("Syntax error in character length specification at %C");
1153 return MATCH_ERROR;
1157 /* Special subroutine for finding a symbol. Check if the name is found
1158 in the current name space. If not, and we're compiling a function or
1159 subroutine and the parent compilation unit is an interface, then check
1160 to see if the name we've been given is the name of the interface
1161 (located in another namespace). */
1163 static int
1164 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1166 gfc_state_data *s;
1167 gfc_symtree *st;
1168 int i;
1170 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1171 if (i == 0)
1173 *result = st ? st->n.sym : NULL;
1174 goto end;
1177 if (gfc_current_state () != COMP_SUBROUTINE
1178 && gfc_current_state () != COMP_FUNCTION)
1179 goto end;
1181 s = gfc_state_stack->previous;
1182 if (s == NULL)
1183 goto end;
1185 if (s->state != COMP_INTERFACE)
1186 goto end;
1187 if (s->sym == NULL)
1188 goto end; /* Nameless interface. */
1190 if (strcmp (name, s->sym->name) == 0)
1192 *result = s->sym;
1193 return 0;
1196 end:
1197 return i;
1201 /* Special subroutine for getting a symbol node associated with a
1202 procedure name, used in SUBROUTINE and FUNCTION statements. The
1203 symbol is created in the parent using with symtree node in the
1204 child unit pointing to the symbol. If the current namespace has no
1205 parent, then the symbol is just created in the current unit. */
1207 static int
1208 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1210 gfc_symtree *st;
1211 gfc_symbol *sym;
1212 int rc = 0;
1214 /* Module functions have to be left in their own namespace because
1215 they have potentially (almost certainly!) already been referenced.
1216 In this sense, they are rather like external functions. This is
1217 fixed up in resolve.c(resolve_entries), where the symbol name-
1218 space is set to point to the master function, so that the fake
1219 result mechanism can work. */
1220 if (module_fcn_entry)
1222 /* Present if entry is declared to be a module procedure. */
1223 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1225 if (*result == NULL)
1226 rc = gfc_get_symbol (name, NULL, result);
1227 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1228 && (*result)->ts.type == BT_UNKNOWN
1229 && sym->attr.flavor == FL_UNKNOWN)
1230 /* Pick up the typespec for the entry, if declared in the function
1231 body. Note that this symbol is FL_UNKNOWN because it will
1232 only have appeared in a type declaration. The local symtree
1233 is set to point to the module symbol and a unique symtree
1234 to the local version. This latter ensures a correct clearing
1235 of the symbols. */
1237 /* If the ENTRY proceeds its specification, we need to ensure
1238 that this does not raise a "has no IMPLICIT type" error. */
1239 if (sym->ts.type == BT_UNKNOWN)
1240 sym->attr.untyped = 1;
1242 (*result)->ts = sym->ts;
1244 /* Put the symbol in the procedure namespace so that, should
1245 the ENTRY precede its specification, the specification
1246 can be applied. */
1247 (*result)->ns = gfc_current_ns;
1249 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1250 st->n.sym = *result;
1251 st = gfc_get_unique_symtree (gfc_current_ns);
1252 sym->refs++;
1253 st->n.sym = sym;
1256 else
1257 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1259 if (rc)
1260 return rc;
1262 sym = *result;
1263 if (sym->attr.proc == PROC_ST_FUNCTION)
1264 return rc;
1266 if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1268 /* Create a partially populated interface symbol to carry the
1269 characteristics of the procedure and the result. */
1270 sym->tlink = gfc_new_symbol (name, sym->ns);
1271 gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1272 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1273 if (sym->attr.dimension)
1274 sym->tlink->as = gfc_copy_array_spec (sym->as);
1276 /* Ideally, at this point, a copy would be made of the formal
1277 arguments and their namespace. However, this does not appear
1278 to be necessary, albeit at the expense of not being able to
1279 use gfc_compare_interfaces directly. */
1281 if (sym->result && sym->result != sym)
1283 sym->tlink->result = sym->result;
1284 sym->result = NULL;
1286 else if (sym->result)
1288 sym->tlink->result = sym->tlink;
1291 else if (sym && !sym->gfc_new
1292 && gfc_current_state () != COMP_INTERFACE)
1294 /* Trap another encompassed procedure with the same name. All
1295 these conditions are necessary to avoid picking up an entry
1296 whose name clashes with that of the encompassing procedure;
1297 this is handled using gsymbols to register unique, globally
1298 accessible names. */
1299 if (sym->attr.flavor != 0
1300 && sym->attr.proc != 0
1301 && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1302 && sym->attr.if_source != IFSRC_UNKNOWN)
1304 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1305 name, &sym->declared_at);
1306 return true;
1308 if (sym->attr.flavor != 0
1309 && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1311 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1312 name, &sym->declared_at);
1313 return true;
1316 if (sym->attr.external && sym->attr.procedure
1317 && gfc_current_state () == COMP_CONTAINS)
1319 gfc_error_now ("Contained procedure %qs at %C clashes with "
1320 "procedure defined at %L",
1321 name, &sym->declared_at);
1322 return true;
1325 /* Trap a procedure with a name the same as interface in the
1326 encompassing scope. */
1327 if (sym->attr.generic != 0
1328 && (sym->attr.subroutine || sym->attr.function)
1329 && !sym->attr.mod_proc)
1331 gfc_error_now ("Name %qs at %C is already defined"
1332 " as a generic interface at %L",
1333 name, &sym->declared_at);
1334 return true;
1337 /* Trap declarations of attributes in encompassing scope. The
1338 signature for this is that ts.kind is set. Legitimate
1339 references only set ts.type. */
1340 if (sym->ts.kind != 0
1341 && !sym->attr.implicit_type
1342 && sym->attr.proc == 0
1343 && gfc_current_ns->parent != NULL
1344 && sym->attr.access == 0
1345 && !module_fcn_entry)
1347 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1348 "from a previous declaration", name);
1349 return true;
1353 /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1354 subroutine-stmt of a module subprogram or of a nonabstract interface
1355 body that is declared in the scoping unit of a module or submodule. */
1356 if (sym->attr.external
1357 && (sym->attr.subroutine || sym->attr.function)
1358 && sym->attr.if_source == IFSRC_IFBODY
1359 && !current_attr.module_procedure
1360 && sym->attr.proc == PROC_MODULE
1361 && gfc_state_stack->state == COMP_CONTAINS)
1363 gfc_error_now ("Procedure %qs defined in interface body at %L "
1364 "clashes with internal procedure defined at %C",
1365 name, &sym->declared_at);
1366 return true;
1369 if (sym && !sym->gfc_new
1370 && sym->attr.flavor != FL_UNKNOWN
1371 && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1372 && gfc_state_stack->state == COMP_CONTAINS
1373 && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1375 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1376 name, &sym->declared_at);
1377 return true;
1380 if (gfc_current_ns->parent == NULL || *result == NULL)
1381 return rc;
1383 /* Module function entries will already have a symtree in
1384 the current namespace but will need one at module level. */
1385 if (module_fcn_entry)
1387 /* Present if entry is declared to be a module procedure. */
1388 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1389 if (st == NULL)
1390 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1392 else
1393 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1395 st->n.sym = sym;
1396 sym->refs++;
1398 /* See if the procedure should be a module procedure. */
1400 if (((sym->ns->proc_name != NULL
1401 && sym->ns->proc_name->attr.flavor == FL_MODULE
1402 && sym->attr.proc != PROC_MODULE)
1403 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1404 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1405 rc = 2;
1407 return rc;
1411 /* Verify that the given symbol representing a parameter is C
1412 interoperable, by checking to see if it was marked as such after
1413 its declaration. If the given symbol is not interoperable, a
1414 warning is reported, thus removing the need to return the status to
1415 the calling function. The standard does not require the user use
1416 one of the iso_c_binding named constants to declare an
1417 interoperable parameter, but we can't be sure if the param is C
1418 interop or not if the user doesn't. For example, integer(4) may be
1419 legal Fortran, but doesn't have meaning in C. It may interop with
1420 a number of the C types, which causes a problem because the
1421 compiler can't know which one. This code is almost certainly not
1422 portable, and the user will get what they deserve if the C type
1423 across platforms isn't always interoperable with integer(4). If
1424 the user had used something like integer(c_int) or integer(c_long),
1425 the compiler could have automatically handled the varying sizes
1426 across platforms. */
1428 bool
1429 gfc_verify_c_interop_param (gfc_symbol *sym)
1431 int is_c_interop = 0;
1432 bool retval = true;
1434 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1435 Don't repeat the checks here. */
1436 if (sym->attr.implicit_type)
1437 return true;
1439 /* For subroutines or functions that are passed to a BIND(C) procedure,
1440 they're interoperable if they're BIND(C) and their params are all
1441 interoperable. */
1442 if (sym->attr.flavor == FL_PROCEDURE)
1444 if (sym->attr.is_bind_c == 0)
1446 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1447 "attribute to be C interoperable", sym->name,
1448 &(sym->declared_at));
1449 return false;
1451 else
1453 if (sym->attr.is_c_interop == 1)
1454 /* We've already checked this procedure; don't check it again. */
1455 return true;
1456 else
1457 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1458 sym->common_block);
1462 /* See if we've stored a reference to a procedure that owns sym. */
1463 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1465 if (sym->ns->proc_name->attr.is_bind_c == 1)
1467 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1469 if (is_c_interop != 1)
1471 /* Make personalized messages to give better feedback. */
1472 if (sym->ts.type == BT_DERIVED)
1473 gfc_error ("Variable %qs at %L is a dummy argument to the "
1474 "BIND(C) procedure %qs but is not C interoperable "
1475 "because derived type %qs is not C interoperable",
1476 sym->name, &(sym->declared_at),
1477 sym->ns->proc_name->name,
1478 sym->ts.u.derived->name);
1479 else if (sym->ts.type == BT_CLASS)
1480 gfc_error ("Variable %qs at %L is a dummy argument to the "
1481 "BIND(C) procedure %qs but is not C interoperable "
1482 "because it is polymorphic",
1483 sym->name, &(sym->declared_at),
1484 sym->ns->proc_name->name);
1485 else if (warn_c_binding_type)
1486 gfc_warning (OPT_Wc_binding_type,
1487 "Variable %qs at %L is a dummy argument of the "
1488 "BIND(C) procedure %qs but may not be C "
1489 "interoperable",
1490 sym->name, &(sym->declared_at),
1491 sym->ns->proc_name->name);
1494 /* Character strings are only C interoperable if they have a
1495 length of 1. */
1496 if (sym->ts.type == BT_CHARACTER)
1498 gfc_charlen *cl = sym->ts.u.cl;
1499 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1500 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1502 if (!gfc_notify_std (GFC_STD_F2018,
1503 "Character argument %qs at %L "
1504 "must be length 1 because "
1505 "procedure %qs is BIND(C)",
1506 sym->name, &sym->declared_at,
1507 sym->ns->proc_name->name))
1508 retval = false;
1512 /* We have to make sure that any param to a bind(c) routine does
1513 not have the allocatable, pointer, or optional attributes,
1514 according to J3/04-007, section 5.1. */
1515 if (sym->attr.allocatable == 1
1516 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1517 "ALLOCATABLE attribute in procedure %qs "
1518 "with BIND(C)", sym->name,
1519 &(sym->declared_at),
1520 sym->ns->proc_name->name))
1521 retval = false;
1523 if (sym->attr.pointer == 1
1524 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1525 "POINTER attribute in procedure %qs "
1526 "with BIND(C)", sym->name,
1527 &(sym->declared_at),
1528 sym->ns->proc_name->name))
1529 retval = false;
1531 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1533 gfc_error ("Scalar variable %qs at %L with POINTER or "
1534 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1535 " supported", sym->name, &(sym->declared_at),
1536 sym->ns->proc_name->name);
1537 retval = false;
1540 if (sym->attr.optional == 1 && sym->attr.value)
1542 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1543 "and the VALUE attribute because procedure %qs "
1544 "is BIND(C)", sym->name, &(sym->declared_at),
1545 sym->ns->proc_name->name);
1546 retval = false;
1548 else if (sym->attr.optional == 1
1549 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1550 "at %L with OPTIONAL attribute in "
1551 "procedure %qs which is BIND(C)",
1552 sym->name, &(sym->declared_at),
1553 sym->ns->proc_name->name))
1554 retval = false;
1556 /* Make sure that if it has the dimension attribute, that it is
1557 either assumed size or explicit shape. Deferred shape is already
1558 covered by the pointer/allocatable attribute. */
1559 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1560 && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1561 "at %L as dummy argument to the BIND(C) "
1562 "procedure %qs at %L", sym->name,
1563 &(sym->declared_at),
1564 sym->ns->proc_name->name,
1565 &(sym->ns->proc_name->declared_at)))
1566 retval = false;
1570 return retval;
1575 /* Function called by variable_decl() that adds a name to the symbol table. */
1577 static bool
1578 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1579 gfc_array_spec **as, locus *var_locus)
1581 symbol_attribute attr;
1582 gfc_symbol *sym;
1583 int upper;
1584 gfc_symtree *st;
1586 /* Symbols in a submodule are host associated from the parent module or
1587 submodules. Therefore, they can be overridden by declarations in the
1588 submodule scope. Deal with this by attaching the existing symbol to
1589 a new symtree and recycling the old symtree with a new symbol... */
1590 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1591 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1592 && st->n.sym != NULL
1593 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1595 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1596 s->n.sym = st->n.sym;
1597 sym = gfc_new_symbol (name, gfc_current_ns);
1600 st->n.sym = sym;
1601 sym->refs++;
1602 gfc_set_sym_referenced (sym);
1604 /* ...Otherwise generate a new symtree and new symbol. */
1605 else if (gfc_get_symbol (name, NULL, &sym))
1606 return false;
1608 /* Check if the name has already been defined as a type. The
1609 first letter of the symtree will be in upper case then. Of
1610 course, this is only necessary if the upper case letter is
1611 actually different. */
1613 upper = TOUPPER(name[0]);
1614 if (upper != name[0])
1616 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1617 gfc_symtree *st;
1619 gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1620 strcpy (u_name, name);
1621 u_name[0] = upper;
1623 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1625 /* STRUCTURE types can alias symbol names */
1626 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1628 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1629 &st->n.sym->declared_at);
1630 return false;
1634 /* Start updating the symbol table. Add basic type attribute if present. */
1635 if (current_ts.type != BT_UNKNOWN
1636 && (sym->attr.implicit_type == 0
1637 || !gfc_compare_types (&sym->ts, &current_ts))
1638 && !gfc_add_type (sym, &current_ts, var_locus))
1639 return false;
1641 if (sym->ts.type == BT_CHARACTER)
1643 sym->ts.u.cl = cl;
1644 sym->ts.deferred = cl_deferred;
1647 /* Add dimension attribute if present. */
1648 if (!gfc_set_array_spec (sym, *as, var_locus))
1649 return false;
1650 *as = NULL;
1652 /* Add attribute to symbol. The copy is so that we can reset the
1653 dimension attribute. */
1654 attr = current_attr;
1655 attr.dimension = 0;
1656 attr.codimension = 0;
1658 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1659 return false;
1661 /* Finish any work that may need to be done for the binding label,
1662 if it's a bind(c). The bind(c) attr is found before the symbol
1663 is made, and before the symbol name (for data decls), so the
1664 current_ts is holding the binding label, or nothing if the
1665 name= attr wasn't given. Therefore, test here if we're dealing
1666 with a bind(c) and make sure the binding label is set correctly. */
1667 if (sym->attr.is_bind_c == 1)
1669 if (!sym->binding_label)
1671 /* Set the binding label and verify that if a NAME= was specified
1672 then only one identifier was in the entity-decl-list. */
1673 if (!set_binding_label (&sym->binding_label, sym->name,
1674 num_idents_on_line))
1675 return false;
1679 /* See if we know we're in a common block, and if it's a bind(c)
1680 common then we need to make sure we're an interoperable type. */
1681 if (sym->attr.in_common == 1)
1683 /* Test the common block object. */
1684 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1685 && sym->ts.is_c_interop != 1)
1687 gfc_error_now ("Variable %qs in common block %qs at %C "
1688 "must be declared with a C interoperable "
1689 "kind since common block %qs is BIND(C)",
1690 sym->name, sym->common_block->name,
1691 sym->common_block->name);
1692 gfc_clear_error ();
1696 sym->attr.implied_index = 0;
1698 /* Use the parameter expressions for a parameterized derived type. */
1699 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1700 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1701 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1703 if (sym->ts.type == BT_CLASS)
1704 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1706 return true;
1710 /* Set character constant to the given length. The constant will be padded or
1711 truncated. If we're inside an array constructor without a typespec, we
1712 additionally check that all elements have the same length; check_len -1
1713 means no checking. */
1715 void
1716 gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1717 gfc_charlen_t check_len)
1719 gfc_char_t *s;
1720 gfc_charlen_t slen;
1722 if (expr->ts.type != BT_CHARACTER)
1723 return;
1725 if (expr->expr_type != EXPR_CONSTANT)
1727 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1728 return;
1731 slen = expr->value.character.length;
1732 if (len != slen)
1734 s = gfc_get_wide_string (len + 1);
1735 memcpy (s, expr->value.character.string,
1736 MIN (len, slen) * sizeof (gfc_char_t));
1737 if (len > slen)
1738 gfc_wide_memset (&s[slen], ' ', len - slen);
1740 if (warn_character_truncation && slen > len)
1741 gfc_warning_now (OPT_Wcharacter_truncation,
1742 "CHARACTER expression at %L is being truncated "
1743 "(%ld/%ld)", &expr->where,
1744 (long) slen, (long) len);
1746 /* Apply the standard by 'hand' otherwise it gets cleared for
1747 initializers. */
1748 if (check_len != -1 && slen != check_len
1749 && !(gfc_option.allow_std & GFC_STD_GNU))
1750 gfc_error_now ("The CHARACTER elements of the array constructor "
1751 "at %L must have the same length (%ld/%ld)",
1752 &expr->where, (long) slen,
1753 (long) check_len);
1755 s[len] = '\0';
1756 free (expr->value.character.string);
1757 expr->value.character.string = s;
1758 expr->value.character.length = len;
1759 /* If explicit representation was given, clear it
1760 as it is no longer needed after padding. */
1761 if (expr->representation.length)
1763 expr->representation.length = 0;
1764 free (expr->representation.string);
1765 expr->representation.string = NULL;
1771 /* Function to create and update the enumerator history
1772 using the information passed as arguments.
1773 Pointer "max_enum" is also updated, to point to
1774 enum history node containing largest initializer.
1776 SYM points to the symbol node of enumerator.
1777 INIT points to its enumerator value. */
1779 static void
1780 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1782 enumerator_history *new_enum_history;
1783 gcc_assert (sym != NULL && init != NULL);
1785 new_enum_history = XCNEW (enumerator_history);
1787 new_enum_history->sym = sym;
1788 new_enum_history->initializer = init;
1789 new_enum_history->next = NULL;
1791 if (enum_history == NULL)
1793 enum_history = new_enum_history;
1794 max_enum = enum_history;
1796 else
1798 new_enum_history->next = enum_history;
1799 enum_history = new_enum_history;
1801 if (mpz_cmp (max_enum->initializer->value.integer,
1802 new_enum_history->initializer->value.integer) < 0)
1803 max_enum = new_enum_history;
1808 /* Function to free enum kind history. */
1810 void
1811 gfc_free_enum_history (void)
1813 enumerator_history *current = enum_history;
1814 enumerator_history *next;
1816 while (current != NULL)
1818 next = current->next;
1819 free (current);
1820 current = next;
1822 max_enum = NULL;
1823 enum_history = NULL;
1827 /* Function called by variable_decl() that adds an initialization
1828 expression to a symbol. */
1830 static bool
1831 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1833 symbol_attribute attr;
1834 gfc_symbol *sym;
1835 gfc_expr *init;
1837 init = *initp;
1838 if (find_special (name, &sym, false))
1839 return false;
1841 attr = sym->attr;
1843 /* If this symbol is confirming an implicit parameter type,
1844 then an initialization expression is not allowed. */
1845 if (attr.flavor == FL_PARAMETER
1846 && sym->value != NULL
1847 && *initp != NULL)
1849 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1850 sym->name);
1851 return false;
1854 if (init == NULL)
1856 /* An initializer is required for PARAMETER declarations. */
1857 if (attr.flavor == FL_PARAMETER)
1859 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1860 return false;
1863 else
1865 /* If a variable appears in a DATA block, it cannot have an
1866 initializer. */
1867 if (sym->attr.data)
1869 gfc_error ("Variable %qs at %C with an initializer already "
1870 "appears in a DATA statement", sym->name);
1871 return false;
1874 /* Check if the assignment can happen. This has to be put off
1875 until later for derived type variables and procedure pointers. */
1876 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1877 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1878 && !sym->attr.proc_pointer
1879 && !gfc_check_assign_symbol (sym, NULL, init))
1880 return false;
1882 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1883 && init->ts.type == BT_CHARACTER)
1885 /* Update symbol character length according initializer. */
1886 if (!gfc_check_assign_symbol (sym, NULL, init))
1887 return false;
1889 if (sym->ts.u.cl->length == NULL)
1891 gfc_charlen_t clen;
1892 /* If there are multiple CHARACTER variables declared on the
1893 same line, we don't want them to share the same length. */
1894 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1896 if (sym->attr.flavor == FL_PARAMETER)
1898 if (init->expr_type == EXPR_CONSTANT)
1900 clen = init->value.character.length;
1901 sym->ts.u.cl->length
1902 = gfc_get_int_expr (gfc_charlen_int_kind,
1903 NULL, clen);
1905 else if (init->expr_type == EXPR_ARRAY)
1907 if (init->ts.u.cl && init->ts.u.cl->length)
1909 const gfc_expr *length = init->ts.u.cl->length;
1910 if (length->expr_type != EXPR_CONSTANT)
1912 gfc_error ("Cannot initialize parameter array "
1913 "at %L "
1914 "with variable length elements",
1915 &sym->declared_at);
1916 return false;
1918 clen = mpz_get_si (length->value.integer);
1920 else if (init->value.constructor)
1922 gfc_constructor *c;
1923 c = gfc_constructor_first (init->value.constructor);
1924 clen = c->expr->value.character.length;
1926 else
1927 gcc_unreachable ();
1928 sym->ts.u.cl->length
1929 = gfc_get_int_expr (gfc_charlen_int_kind,
1930 NULL, clen);
1932 else if (init->ts.u.cl && init->ts.u.cl->length)
1933 sym->ts.u.cl->length =
1934 gfc_copy_expr (init->ts.u.cl->length);
1937 /* Update initializer character length according symbol. */
1938 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1940 if (!gfc_specification_expr (sym->ts.u.cl->length))
1941 return false;
1943 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
1944 false);
1945 /* resolve_charlen will complain later on if the length
1946 is too large. Just skeep the initialization in that case. */
1947 if (mpz_cmp (sym->ts.u.cl->length->value.integer,
1948 gfc_integer_kinds[k].huge) <= 0)
1950 HOST_WIDE_INT len
1951 = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
1953 if (init->expr_type == EXPR_CONSTANT)
1954 gfc_set_constant_character_len (len, init, -1);
1955 else if (init->expr_type == EXPR_ARRAY)
1957 gfc_constructor *c;
1959 /* Build a new charlen to prevent simplification from
1960 deleting the length before it is resolved. */
1961 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1962 init->ts.u.cl->length
1963 = gfc_copy_expr (sym->ts.u.cl->length);
1965 for (c = gfc_constructor_first (init->value.constructor);
1966 c; c = gfc_constructor_next (c))
1967 gfc_set_constant_character_len (len, c->expr, -1);
1973 /* If sym is implied-shape, set its upper bounds from init. */
1974 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1975 && sym->as->type == AS_IMPLIED_SHAPE)
1977 int dim;
1979 if (init->rank == 0)
1981 gfc_error ("Can't initialize implied-shape array at %L"
1982 " with scalar", &sym->declared_at);
1983 return false;
1986 /* The shape may be NULL for EXPR_ARRAY, set it. */
1987 if (init->shape == NULL)
1989 gcc_assert (init->expr_type == EXPR_ARRAY);
1990 init->shape = gfc_get_shape (1);
1991 if (!gfc_array_size (init, &init->shape[0]))
1992 gfc_internal_error ("gfc_array_size failed");
1995 for (dim = 0; dim < sym->as->rank; ++dim)
1997 int k;
1998 gfc_expr *e, *lower;
2000 lower = sym->as->lower[dim];
2002 /* If the lower bound is an array element from another
2003 parameterized array, then it is marked with EXPR_VARIABLE and
2004 is an initialization expression. Try to reduce it. */
2005 if (lower->expr_type == EXPR_VARIABLE)
2006 gfc_reduce_init_expr (lower);
2008 if (lower->expr_type == EXPR_CONSTANT)
2010 /* All dimensions must be without upper bound. */
2011 gcc_assert (!sym->as->upper[dim]);
2013 k = lower->ts.kind;
2014 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2015 mpz_add (e->value.integer, lower->value.integer,
2016 init->shape[dim]);
2017 mpz_sub_ui (e->value.integer, e->value.integer, 1);
2018 sym->as->upper[dim] = e;
2020 else
2022 gfc_error ("Non-constant lower bound in implied-shape"
2023 " declaration at %L", &lower->where);
2024 return false;
2028 sym->as->type = AS_EXPLICIT;
2031 /* Need to check if the expression we initialized this
2032 to was one of the iso_c_binding named constants. If so,
2033 and we're a parameter (constant), let it be iso_c.
2034 For example:
2035 integer(c_int), parameter :: my_int = c_int
2036 integer(my_int) :: my_int_2
2037 If we mark my_int as iso_c (since we can see it's value
2038 is equal to one of the named constants), then my_int_2
2039 will be considered C interoperable. */
2040 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2042 sym->ts.is_iso_c |= init->ts.is_iso_c;
2043 sym->ts.is_c_interop |= init->ts.is_c_interop;
2044 /* attr bits needed for module files. */
2045 sym->attr.is_iso_c |= init->ts.is_iso_c;
2046 sym->attr.is_c_interop |= init->ts.is_c_interop;
2047 if (init->ts.is_iso_c)
2048 sym->ts.f90_type = init->ts.f90_type;
2051 /* Add initializer. Make sure we keep the ranks sane. */
2052 if (sym->attr.dimension && init->rank == 0)
2054 mpz_t size;
2055 gfc_expr *array;
2056 int n;
2057 if (sym->attr.flavor == FL_PARAMETER
2058 && init->expr_type == EXPR_CONSTANT
2059 && spec_size (sym->as, &size)
2060 && mpz_cmp_si (size, 0) > 0)
2062 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2063 &init->where);
2064 for (n = 0; n < (int)mpz_get_si (size); n++)
2065 gfc_constructor_append_expr (&array->value.constructor,
2066 n == 0
2067 ? init
2068 : gfc_copy_expr (init),
2069 &init->where);
2071 array->shape = gfc_get_shape (sym->as->rank);
2072 for (n = 0; n < sym->as->rank; n++)
2073 spec_dimen_size (sym->as, n, &array->shape[n]);
2075 init = array;
2076 mpz_clear (size);
2078 init->rank = sym->as->rank;
2081 sym->value = init;
2082 if (sym->attr.save == SAVE_NONE)
2083 sym->attr.save = SAVE_IMPLICIT;
2084 *initp = NULL;
2087 return true;
2091 /* Function called by variable_decl() that adds a name to a structure
2092 being built. */
2094 static bool
2095 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2096 gfc_array_spec **as)
2098 gfc_state_data *s;
2099 gfc_component *c;
2101 /* F03:C438/C439. If the current symbol is of the same derived type that we're
2102 constructing, it must have the pointer attribute. */
2103 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2104 && current_ts.u.derived == gfc_current_block ()
2105 && current_attr.pointer == 0)
2107 if (current_attr.allocatable
2108 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2109 "must have the POINTER attribute"))
2111 return false;
2113 else if (current_attr.allocatable == 0)
2115 gfc_error ("Component at %C must have the POINTER attribute");
2116 return false;
2120 /* F03:C437. */
2121 if (current_ts.type == BT_CLASS
2122 && !(current_attr.pointer || current_attr.allocatable))
2124 gfc_error ("Component %qs with CLASS at %C must be allocatable "
2125 "or pointer", name);
2126 return false;
2129 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2131 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2133 gfc_error ("Array component of structure at %C must have explicit "
2134 "or deferred shape");
2135 return false;
2139 /* If we are in a nested union/map definition, gfc_add_component will not
2140 properly find repeated components because:
2141 (i) gfc_add_component does a flat search, where components of unions
2142 and maps are implicity chained so nested components may conflict.
2143 (ii) Unions and maps are not linked as components of their parent
2144 structures until after they are parsed.
2145 For (i) we use gfc_find_component which searches recursively, and for (ii)
2146 we search each block directly from the parse stack until we find the top
2147 level structure. */
2149 s = gfc_state_stack;
2150 if (s->state == COMP_UNION || s->state == COMP_MAP)
2152 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2154 c = gfc_find_component (s->sym, name, true, true, NULL);
2155 if (c != NULL)
2157 gfc_error_now ("Component %qs at %C already declared at %L",
2158 name, &c->loc);
2159 return false;
2161 /* Break after we've searched the entire chain. */
2162 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2163 break;
2164 s = s->previous;
2168 if (!gfc_add_component (gfc_current_block(), name, &c))
2169 return false;
2171 c->ts = current_ts;
2172 if (c->ts.type == BT_CHARACTER)
2173 c->ts.u.cl = cl;
2175 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2176 && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2177 && saved_kind_expr != NULL)
2178 c->kind_expr = gfc_copy_expr (saved_kind_expr);
2180 c->attr = current_attr;
2182 c->initializer = *init;
2183 *init = NULL;
2185 c->as = *as;
2186 if (c->as != NULL)
2188 if (c->as->corank)
2189 c->attr.codimension = 1;
2190 if (c->as->rank)
2191 c->attr.dimension = 1;
2193 *as = NULL;
2195 gfc_apply_init (&c->ts, &c->attr, c->initializer);
2197 /* Check array components. */
2198 if (!c->attr.dimension)
2199 goto scalar;
2201 if (c->attr.pointer)
2203 if (c->as->type != AS_DEFERRED)
2205 gfc_error ("Pointer array component of structure at %C must have a "
2206 "deferred shape");
2207 return false;
2210 else if (c->attr.allocatable)
2212 if (c->as->type != AS_DEFERRED)
2214 gfc_error ("Allocatable component of structure at %C must have a "
2215 "deferred shape");
2216 return false;
2219 else
2221 if (c->as->type != AS_EXPLICIT)
2223 gfc_error ("Array component of structure at %C must have an "
2224 "explicit shape");
2225 return false;
2229 scalar:
2230 if (c->ts.type == BT_CLASS)
2231 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2233 if (c->attr.pdt_kind || c->attr.pdt_len)
2235 gfc_symbol *sym;
2236 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2237 0, &sym);
2238 if (sym == NULL)
2240 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2241 "in the type parameter name list at %L",
2242 c->name, &gfc_current_block ()->declared_at);
2243 return false;
2245 sym->ts = c->ts;
2246 sym->attr.pdt_kind = c->attr.pdt_kind;
2247 sym->attr.pdt_len = c->attr.pdt_len;
2248 if (c->initializer)
2249 sym->value = gfc_copy_expr (c->initializer);
2250 sym->attr.flavor = FL_VARIABLE;
2253 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2254 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2255 && decl_type_param_list)
2256 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2258 return true;
2262 /* Match a 'NULL()', and possibly take care of some side effects. */
2264 match
2265 gfc_match_null (gfc_expr **result)
2267 gfc_symbol *sym;
2268 match m, m2 = MATCH_NO;
2270 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2271 return MATCH_ERROR;
2273 if (m == MATCH_NO)
2275 locus old_loc;
2276 char name[GFC_MAX_SYMBOL_LEN + 1];
2278 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2279 return m2;
2281 old_loc = gfc_current_locus;
2282 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2283 return MATCH_ERROR;
2284 if (m2 != MATCH_YES
2285 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2286 return MATCH_ERROR;
2287 if (m2 == MATCH_NO)
2289 gfc_current_locus = old_loc;
2290 return MATCH_NO;
2294 /* The NULL symbol now has to be/become an intrinsic function. */
2295 if (gfc_get_symbol ("null", NULL, &sym))
2297 gfc_error ("NULL() initialization at %C is ambiguous");
2298 return MATCH_ERROR;
2301 gfc_intrinsic_symbol (sym);
2303 if (sym->attr.proc != PROC_INTRINSIC
2304 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2305 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2306 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2307 return MATCH_ERROR;
2309 *result = gfc_get_null_expr (&gfc_current_locus);
2311 /* Invalid per F2008, C512. */
2312 if (m2 == MATCH_YES)
2314 gfc_error ("NULL() initialization at %C may not have MOLD");
2315 return MATCH_ERROR;
2318 return MATCH_YES;
2322 /* Match the initialization expr for a data pointer or procedure pointer. */
2324 static match
2325 match_pointer_init (gfc_expr **init, int procptr)
2327 match m;
2329 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2331 gfc_error ("Initialization of pointer at %C is not allowed in "
2332 "a PURE procedure");
2333 return MATCH_ERROR;
2335 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2337 /* Match NULL() initialization. */
2338 m = gfc_match_null (init);
2339 if (m != MATCH_NO)
2340 return m;
2342 /* Match non-NULL initialization. */
2343 gfc_matching_ptr_assignment = !procptr;
2344 gfc_matching_procptr_assignment = procptr;
2345 m = gfc_match_rvalue (init);
2346 gfc_matching_ptr_assignment = 0;
2347 gfc_matching_procptr_assignment = 0;
2348 if (m == MATCH_ERROR)
2349 return MATCH_ERROR;
2350 else if (m == MATCH_NO)
2352 gfc_error ("Error in pointer initialization at %C");
2353 return MATCH_ERROR;
2356 if (!procptr && !gfc_resolve_expr (*init))
2357 return MATCH_ERROR;
2359 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2360 "initialization at %C"))
2361 return MATCH_ERROR;
2363 return MATCH_YES;
2367 static bool
2368 check_function_name (char *name)
2370 /* In functions that have a RESULT variable defined, the function name always
2371 refers to function calls. Therefore, the name is not allowed to appear in
2372 specification statements. When checking this, be careful about
2373 'hidden' procedure pointer results ('ppr@'). */
2375 if (gfc_current_state () == COMP_FUNCTION)
2377 gfc_symbol *block = gfc_current_block ();
2378 if (block && block->result && block->result != block
2379 && strcmp (block->result->name, "ppr@") != 0
2380 && strcmp (block->name, name) == 0)
2382 gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2383 "from appearing in a specification statement",
2384 block->result->name, &block->result->declared_at, name);
2385 return false;
2389 return true;
2393 /* Match a variable name with an optional initializer. When this
2394 subroutine is called, a variable is expected to be parsed next.
2395 Depending on what is happening at the moment, updates either the
2396 symbol table or the current interface. */
2398 static match
2399 variable_decl (int elem)
2401 char name[GFC_MAX_SYMBOL_LEN + 1];
2402 static unsigned int fill_id = 0;
2403 gfc_expr *initializer, *char_len;
2404 gfc_array_spec *as;
2405 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2406 gfc_charlen *cl;
2407 bool cl_deferred;
2408 locus var_locus;
2409 match m;
2410 bool t;
2411 gfc_symbol *sym;
2413 initializer = NULL;
2414 as = NULL;
2415 cp_as = NULL;
2417 /* When we get here, we've just matched a list of attributes and
2418 maybe a type and a double colon. The next thing we expect to see
2419 is the name of the symbol. */
2421 /* If we are parsing a structure with legacy support, we allow the symbol
2422 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2423 m = MATCH_NO;
2424 gfc_gobble_whitespace ();
2425 if (gfc_peek_ascii_char () == '%')
2427 gfc_next_ascii_char ();
2428 m = gfc_match ("fill");
2431 if (m != MATCH_YES)
2433 m = gfc_match_name (name);
2434 if (m != MATCH_YES)
2435 goto cleanup;
2438 else
2440 m = MATCH_ERROR;
2441 if (gfc_current_state () != COMP_STRUCTURE)
2443 if (flag_dec_structure)
2444 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2445 else
2446 gfc_error ("%qs at %C is a DEC extension, enable with "
2447 "%<-fdec-structure%>", "%FILL");
2448 goto cleanup;
2451 if (attr_seen)
2453 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2454 goto cleanup;
2457 /* %FILL components are given invalid fortran names. */
2458 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2459 m = MATCH_YES;
2462 var_locus = gfc_current_locus;
2464 /* Now we could see the optional array spec. or character length. */
2465 m = gfc_match_array_spec (&as, true, true);
2466 if (m == MATCH_ERROR)
2467 goto cleanup;
2469 if (m == MATCH_NO)
2470 as = gfc_copy_array_spec (current_as);
2471 else if (current_as
2472 && !merge_array_spec (current_as, as, true))
2474 m = MATCH_ERROR;
2475 goto cleanup;
2478 if (flag_cray_pointer)
2479 cp_as = gfc_copy_array_spec (as);
2481 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2482 determine (and check) whether it can be implied-shape. If it
2483 was parsed as assumed-size, change it because PARAMETERs cannot
2484 be assumed-size.
2486 An explicit-shape-array cannot appear under several conditions.
2487 That check is done here as well. */
2488 if (as)
2490 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2492 m = MATCH_ERROR;
2493 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2494 name, &var_locus);
2495 goto cleanup;
2498 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2499 && current_attr.flavor == FL_PARAMETER)
2500 as->type = AS_IMPLIED_SHAPE;
2502 if (as->type == AS_IMPLIED_SHAPE
2503 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2504 &var_locus))
2506 m = MATCH_ERROR;
2507 goto cleanup;
2510 /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2511 constant expressions shall appear only in a subprogram, derived
2512 type definition, BLOCK construct, or interface body. */
2513 if (as->type == AS_EXPLICIT
2514 && gfc_current_state () != COMP_BLOCK
2515 && gfc_current_state () != COMP_DERIVED
2516 && gfc_current_state () != COMP_FUNCTION
2517 && gfc_current_state () != COMP_INTERFACE
2518 && gfc_current_state () != COMP_SUBROUTINE)
2520 gfc_expr *e;
2521 bool not_constant = false;
2523 for (int i = 0; i < as->rank; i++)
2525 e = gfc_copy_expr (as->lower[i]);
2526 gfc_resolve_expr (e);
2527 gfc_simplify_expr (e, 0);
2528 if (e && (e->expr_type != EXPR_CONSTANT))
2530 not_constant = true;
2531 break;
2533 gfc_free_expr (e);
2535 e = gfc_copy_expr (as->upper[i]);
2536 gfc_resolve_expr (e);
2537 gfc_simplify_expr (e, 0);
2538 if (e && (e->expr_type != EXPR_CONSTANT))
2540 not_constant = true;
2541 break;
2543 gfc_free_expr (e);
2546 if (not_constant)
2548 gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2549 m = MATCH_ERROR;
2550 goto cleanup;
2553 if (as->type == AS_EXPLICIT)
2555 for (int i = 0; i < as->rank; i++)
2557 gfc_expr *e, *n;
2558 e = as->lower[i];
2559 if (e->expr_type != EXPR_CONSTANT)
2561 n = gfc_copy_expr (e);
2562 gfc_simplify_expr (n, 1);
2563 if (n->expr_type == EXPR_CONSTANT)
2564 gfc_replace_expr (e, n);
2565 else
2566 gfc_free_expr (n);
2568 e = as->upper[i];
2569 if (e->expr_type != EXPR_CONSTANT)
2571 n = gfc_copy_expr (e);
2572 gfc_simplify_expr (n, 1);
2573 if (n->expr_type == EXPR_CONSTANT)
2574 gfc_replace_expr (e, n);
2575 else
2576 gfc_free_expr (n);
2582 char_len = NULL;
2583 cl = NULL;
2584 cl_deferred = false;
2586 if (current_ts.type == BT_CHARACTER)
2588 switch (match_char_length (&char_len, &cl_deferred, false))
2590 case MATCH_YES:
2591 cl = gfc_new_charlen (gfc_current_ns, NULL);
2593 cl->length = char_len;
2594 break;
2596 /* Non-constant lengths need to be copied after the first
2597 element. Also copy assumed lengths. */
2598 case MATCH_NO:
2599 if (elem > 1
2600 && (current_ts.u.cl->length == NULL
2601 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2603 cl = gfc_new_charlen (gfc_current_ns, NULL);
2604 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2606 else
2607 cl = current_ts.u.cl;
2609 cl_deferred = current_ts.deferred;
2611 break;
2613 case MATCH_ERROR:
2614 goto cleanup;
2618 /* The dummy arguments and result of the abreviated form of MODULE
2619 PROCEDUREs, used in SUBMODULES should not be redefined. */
2620 if (gfc_current_ns->proc_name
2621 && gfc_current_ns->proc_name->abr_modproc_decl)
2623 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2624 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2626 m = MATCH_ERROR;
2627 gfc_error ("%qs at %C is a redefinition of the declaration "
2628 "in the corresponding interface for MODULE "
2629 "PROCEDURE %qs", sym->name,
2630 gfc_current_ns->proc_name->name);
2631 goto cleanup;
2635 /* %FILL components may not have initializers. */
2636 if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
2638 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2639 m = MATCH_ERROR;
2640 goto cleanup;
2643 /* If this symbol has already shown up in a Cray Pointer declaration,
2644 and this is not a component declaration,
2645 then we want to set the type & bail out. */
2646 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2648 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2649 if (sym != NULL && sym->attr.cray_pointee)
2651 m = MATCH_YES;
2652 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
2654 m = MATCH_ERROR;
2655 goto cleanup;
2658 /* Check to see if we have an array specification. */
2659 if (cp_as != NULL)
2661 if (sym->as != NULL)
2663 gfc_error ("Duplicate array spec for Cray pointee at %C");
2664 gfc_free_array_spec (cp_as);
2665 m = MATCH_ERROR;
2666 goto cleanup;
2668 else
2670 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2671 gfc_internal_error ("Couldn't set pointee array spec.");
2673 /* Fix the array spec. */
2674 m = gfc_mod_pointee_as (sym->as);
2675 if (m == MATCH_ERROR)
2676 goto cleanup;
2679 goto cleanup;
2681 else
2683 gfc_free_array_spec (cp_as);
2687 /* Procedure pointer as function result. */
2688 if (gfc_current_state () == COMP_FUNCTION
2689 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2690 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2691 strcpy (name, "ppr@");
2693 if (gfc_current_state () == COMP_FUNCTION
2694 && strcmp (name, gfc_current_block ()->name) == 0
2695 && gfc_current_block ()->result
2696 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2697 strcpy (name, "ppr@");
2699 /* OK, we've successfully matched the declaration. Now put the
2700 symbol in the current namespace, because it might be used in the
2701 optional initialization expression for this symbol, e.g. this is
2702 perfectly legal:
2704 integer, parameter :: i = huge(i)
2706 This is only true for parameters or variables of a basic type.
2707 For components of derived types, it is not true, so we don't
2708 create a symbol for those yet. If we fail to create the symbol,
2709 bail out. */
2710 if (!gfc_comp_struct (gfc_current_state ())
2711 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2713 m = MATCH_ERROR;
2714 goto cleanup;
2717 if (!check_function_name (name))
2719 m = MATCH_ERROR;
2720 goto cleanup;
2723 /* We allow old-style initializations of the form
2724 integer i /2/, j(4) /3*3, 1/
2725 (if no colon has been seen). These are different from data
2726 statements in that initializers are only allowed to apply to the
2727 variable immediately preceding, i.e.
2728 integer i, j /1, 2/
2729 is not allowed. Therefore we have to do some work manually, that
2730 could otherwise be left to the matchers for DATA statements. */
2732 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2734 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2735 "initialization at %C"))
2736 return MATCH_ERROR;
2738 /* Allow old style initializations for components of STRUCTUREs and MAPs
2739 but not components of derived types. */
2740 else if (gfc_current_state () == COMP_DERIVED)
2742 gfc_error ("Invalid old style initialization for derived type "
2743 "component at %C");
2744 m = MATCH_ERROR;
2745 goto cleanup;
2748 /* For structure components, read the initializer as a special
2749 expression and let the rest of this function apply the initializer
2750 as usual. */
2751 else if (gfc_comp_struct (gfc_current_state ()))
2753 m = match_clist_expr (&initializer, &current_ts, as);
2754 if (m == MATCH_NO)
2755 gfc_error ("Syntax error in old style initialization of %s at %C",
2756 name);
2757 if (m != MATCH_YES)
2758 goto cleanup;
2761 /* Otherwise we treat the old style initialization just like a
2762 DATA declaration for the current variable. */
2763 else
2764 return match_old_style_init (name);
2767 /* The double colon must be present in order to have initializers.
2768 Otherwise the statement is ambiguous with an assignment statement. */
2769 if (colon_seen)
2771 if (gfc_match (" =>") == MATCH_YES)
2773 if (!current_attr.pointer)
2775 gfc_error ("Initialization at %C isn't for a pointer variable");
2776 m = MATCH_ERROR;
2777 goto cleanup;
2780 m = match_pointer_init (&initializer, 0);
2781 if (m != MATCH_YES)
2782 goto cleanup;
2784 else if (gfc_match_char ('=') == MATCH_YES)
2786 if (current_attr.pointer)
2788 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2789 "not %<=%>");
2790 m = MATCH_ERROR;
2791 goto cleanup;
2794 m = gfc_match_init_expr (&initializer);
2795 if (m == MATCH_NO)
2797 gfc_error ("Expected an initialization expression at %C");
2798 m = MATCH_ERROR;
2801 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2802 && !gfc_comp_struct (gfc_state_stack->state))
2804 gfc_error ("Initialization of variable at %C is not allowed in "
2805 "a PURE procedure");
2806 m = MATCH_ERROR;
2809 if (current_attr.flavor != FL_PARAMETER
2810 && !gfc_comp_struct (gfc_state_stack->state))
2811 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2813 if (m != MATCH_YES)
2814 goto cleanup;
2818 if (initializer != NULL && current_attr.allocatable
2819 && gfc_comp_struct (gfc_current_state ()))
2821 gfc_error ("Initialization of allocatable component at %C is not "
2822 "allowed");
2823 m = MATCH_ERROR;
2824 goto cleanup;
2827 if (gfc_current_state () == COMP_DERIVED
2828 && gfc_current_block ()->attr.pdt_template)
2830 gfc_symbol *param;
2831 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2832 0, &param);
2833 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2835 gfc_error ("The component with KIND or LEN attribute at %C does not "
2836 "not appear in the type parameter list at %L",
2837 &gfc_current_block ()->declared_at);
2838 m = MATCH_ERROR;
2839 goto cleanup;
2841 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2843 gfc_error ("The component at %C that appears in the type parameter "
2844 "list at %L has neither the KIND nor LEN attribute",
2845 &gfc_current_block ()->declared_at);
2846 m = MATCH_ERROR;
2847 goto cleanup;
2849 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2851 gfc_error ("The component at %C which is a type parameter must be "
2852 "a scalar");
2853 m = MATCH_ERROR;
2854 goto cleanup;
2856 else if (param && initializer)
2857 param->value = gfc_copy_expr (initializer);
2860 /* Before adding a possible initilizer, do a simple check for compatibility
2861 of lhs and rhs types. Assigning a REAL value to a derived type is not a
2862 good thing. */
2863 if (current_ts.type == BT_DERIVED && initializer
2864 && (gfc_numeric_ts (&initializer->ts)
2865 || initializer->ts.type == BT_LOGICAL
2866 || initializer->ts.type == BT_CHARACTER))
2868 gfc_error ("Incompatible initialization between a derived type "
2869 "entity and an entity with %qs type at %C",
2870 gfc_typename (&initializer->ts));
2871 m = MATCH_ERROR;
2872 goto cleanup;
2876 /* Add the initializer. Note that it is fine if initializer is
2877 NULL here, because we sometimes also need to check if a
2878 declaration *must* have an initialization expression. */
2879 if (!gfc_comp_struct (gfc_current_state ()))
2880 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2881 else
2883 if (current_ts.type == BT_DERIVED
2884 && !current_attr.pointer && !initializer)
2885 initializer = gfc_default_initializer (&current_ts);
2886 t = build_struct (name, cl, &initializer, &as);
2888 /* If we match a nested structure definition we expect to see the
2889 * body even if the variable declarations blow up, so we need to keep
2890 * the structure declaration around. */
2891 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2892 gfc_commit_symbol (gfc_new_block);
2895 m = (t) ? MATCH_YES : MATCH_ERROR;
2897 cleanup:
2898 /* Free stuff up and return. */
2899 gfc_free_expr (initializer);
2900 gfc_free_array_spec (as);
2902 return m;
2906 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2907 This assumes that the byte size is equal to the kind number for
2908 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2910 match
2911 gfc_match_old_kind_spec (gfc_typespec *ts)
2913 match m;
2914 int original_kind;
2916 if (gfc_match_char ('*') != MATCH_YES)
2917 return MATCH_NO;
2919 m = gfc_match_small_literal_int (&ts->kind, NULL);
2920 if (m != MATCH_YES)
2921 return MATCH_ERROR;
2923 original_kind = ts->kind;
2925 /* Massage the kind numbers for complex types. */
2926 if (ts->type == BT_COMPLEX)
2928 if (ts->kind % 2)
2930 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2931 gfc_basic_typename (ts->type), original_kind);
2932 return MATCH_ERROR;
2934 ts->kind /= 2;
2938 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
2939 ts->kind = 8;
2941 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2943 if (ts->kind == 4)
2945 if (flag_real4_kind == 8)
2946 ts->kind = 8;
2947 if (flag_real4_kind == 10)
2948 ts->kind = 10;
2949 if (flag_real4_kind == 16)
2950 ts->kind = 16;
2953 if (ts->kind == 8)
2955 if (flag_real8_kind == 4)
2956 ts->kind = 4;
2957 if (flag_real8_kind == 10)
2958 ts->kind = 10;
2959 if (flag_real8_kind == 16)
2960 ts->kind = 16;
2964 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2966 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2967 gfc_basic_typename (ts->type), original_kind);
2968 return MATCH_ERROR;
2971 if (!gfc_notify_std (GFC_STD_GNU,
2972 "Nonstandard type declaration %s*%d at %C",
2973 gfc_basic_typename(ts->type), original_kind))
2974 return MATCH_ERROR;
2976 return MATCH_YES;
2980 /* Match a kind specification. Since kinds are generally optional, we
2981 usually return MATCH_NO if something goes wrong. If a "kind="
2982 string is found, then we know we have an error. */
2984 match
2985 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2987 locus where, loc;
2988 gfc_expr *e;
2989 match m, n;
2990 char c;
2992 m = MATCH_NO;
2993 n = MATCH_YES;
2994 e = NULL;
2995 saved_kind_expr = NULL;
2997 where = loc = gfc_current_locus;
2999 if (kind_expr_only)
3000 goto kind_expr;
3002 if (gfc_match_char ('(') == MATCH_NO)
3003 return MATCH_NO;
3005 /* Also gobbles optional text. */
3006 if (gfc_match (" kind = ") == MATCH_YES)
3007 m = MATCH_ERROR;
3009 loc = gfc_current_locus;
3011 kind_expr:
3013 n = gfc_match_init_expr (&e);
3015 if (gfc_derived_parameter_expr (e))
3017 ts->kind = 0;
3018 saved_kind_expr = gfc_copy_expr (e);
3019 goto close_brackets;
3022 if (n != MATCH_YES)
3024 if (gfc_matching_function)
3026 /* The function kind expression might include use associated or
3027 imported parameters and try again after the specification
3028 expressions..... */
3029 if (gfc_match_char (')') != MATCH_YES)
3031 gfc_error ("Missing right parenthesis at %C");
3032 m = MATCH_ERROR;
3033 goto no_match;
3036 gfc_free_expr (e);
3037 gfc_undo_symbols ();
3038 return MATCH_YES;
3040 else
3042 /* ....or else, the match is real. */
3043 if (n == MATCH_NO)
3044 gfc_error ("Expected initialization expression at %C");
3045 if (n != MATCH_YES)
3046 return MATCH_ERROR;
3050 if (e->rank != 0)
3052 gfc_error ("Expected scalar initialization expression at %C");
3053 m = MATCH_ERROR;
3054 goto no_match;
3057 if (gfc_extract_int (e, &ts->kind, 1))
3059 m = MATCH_ERROR;
3060 goto no_match;
3063 /* Before throwing away the expression, let's see if we had a
3064 C interoperable kind (and store the fact). */
3065 if (e->ts.is_c_interop == 1)
3067 /* Mark this as C interoperable if being declared with one
3068 of the named constants from iso_c_binding. */
3069 ts->is_c_interop = e->ts.is_iso_c;
3070 ts->f90_type = e->ts.f90_type;
3071 if (e->symtree)
3072 ts->interop_kind = e->symtree->n.sym;
3075 gfc_free_expr (e);
3076 e = NULL;
3078 /* Ignore errors to this point, if we've gotten here. This means
3079 we ignore the m=MATCH_ERROR from above. */
3080 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3082 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3083 gfc_basic_typename (ts->type));
3084 gfc_current_locus = where;
3085 return MATCH_ERROR;
3088 /* Warn if, e.g., c_int is used for a REAL variable, but not
3089 if, e.g., c_double is used for COMPLEX as the standard
3090 explicitly says that the kind type parameter for complex and real
3091 variable is the same, i.e. c_float == c_float_complex. */
3092 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3093 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3094 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3095 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3096 "is %s", gfc_basic_typename (ts->f90_type), &where,
3097 gfc_basic_typename (ts->type));
3099 close_brackets:
3101 gfc_gobble_whitespace ();
3102 if ((c = gfc_next_ascii_char ()) != ')'
3103 && (ts->type != BT_CHARACTER || c != ','))
3105 if (ts->type == BT_CHARACTER)
3106 gfc_error ("Missing right parenthesis or comma at %C");
3107 else
3108 gfc_error ("Missing right parenthesis at %C");
3109 m = MATCH_ERROR;
3111 else
3112 /* All tests passed. */
3113 m = MATCH_YES;
3115 if(m == MATCH_ERROR)
3116 gfc_current_locus = where;
3118 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3119 ts->kind = 8;
3121 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3123 if (ts->kind == 4)
3125 if (flag_real4_kind == 8)
3126 ts->kind = 8;
3127 if (flag_real4_kind == 10)
3128 ts->kind = 10;
3129 if (flag_real4_kind == 16)
3130 ts->kind = 16;
3133 if (ts->kind == 8)
3135 if (flag_real8_kind == 4)
3136 ts->kind = 4;
3137 if (flag_real8_kind == 10)
3138 ts->kind = 10;
3139 if (flag_real8_kind == 16)
3140 ts->kind = 16;
3144 /* Return what we know from the test(s). */
3145 return m;
3147 no_match:
3148 gfc_free_expr (e);
3149 gfc_current_locus = where;
3150 return m;
3154 static match
3155 match_char_kind (int * kind, int * is_iso_c)
3157 locus where;
3158 gfc_expr *e;
3159 match m, n;
3160 bool fail;
3162 m = MATCH_NO;
3163 e = NULL;
3164 where = gfc_current_locus;
3166 n = gfc_match_init_expr (&e);
3168 if (n != MATCH_YES && gfc_matching_function)
3170 /* The expression might include use-associated or imported
3171 parameters and try again after the specification
3172 expressions. */
3173 gfc_free_expr (e);
3174 gfc_undo_symbols ();
3175 return MATCH_YES;
3178 if (n == MATCH_NO)
3179 gfc_error ("Expected initialization expression at %C");
3180 if (n != MATCH_YES)
3181 return MATCH_ERROR;
3183 if (e->rank != 0)
3185 gfc_error ("Expected scalar initialization expression at %C");
3186 m = MATCH_ERROR;
3187 goto no_match;
3190 if (gfc_derived_parameter_expr (e))
3192 saved_kind_expr = e;
3193 *kind = 0;
3194 return MATCH_YES;
3197 fail = gfc_extract_int (e, kind, 1);
3198 *is_iso_c = e->ts.is_iso_c;
3199 if (fail)
3201 m = MATCH_ERROR;
3202 goto no_match;
3205 gfc_free_expr (e);
3207 /* Ignore errors to this point, if we've gotten here. This means
3208 we ignore the m=MATCH_ERROR from above. */
3209 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3211 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3212 m = MATCH_ERROR;
3214 else
3215 /* All tests passed. */
3216 m = MATCH_YES;
3218 if (m == MATCH_ERROR)
3219 gfc_current_locus = where;
3221 /* Return what we know from the test(s). */
3222 return m;
3224 no_match:
3225 gfc_free_expr (e);
3226 gfc_current_locus = where;
3227 return m;
3231 /* Match the various kind/length specifications in a CHARACTER
3232 declaration. We don't return MATCH_NO. */
3234 match
3235 gfc_match_char_spec (gfc_typespec *ts)
3237 int kind, seen_length, is_iso_c;
3238 gfc_charlen *cl;
3239 gfc_expr *len;
3240 match m;
3241 bool deferred;
3243 len = NULL;
3244 seen_length = 0;
3245 kind = 0;
3246 is_iso_c = 0;
3247 deferred = false;
3249 /* Try the old-style specification first. */
3250 old_char_selector = 0;
3252 m = match_char_length (&len, &deferred, true);
3253 if (m != MATCH_NO)
3255 if (m == MATCH_YES)
3256 old_char_selector = 1;
3257 seen_length = 1;
3258 goto done;
3261 m = gfc_match_char ('(');
3262 if (m != MATCH_YES)
3264 m = MATCH_YES; /* Character without length is a single char. */
3265 goto done;
3268 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3269 if (gfc_match (" kind =") == MATCH_YES)
3271 m = match_char_kind (&kind, &is_iso_c);
3273 if (m == MATCH_ERROR)
3274 goto done;
3275 if (m == MATCH_NO)
3276 goto syntax;
3278 if (gfc_match (" , len =") == MATCH_NO)
3279 goto rparen;
3281 m = char_len_param_value (&len, &deferred);
3282 if (m == MATCH_NO)
3283 goto syntax;
3284 if (m == MATCH_ERROR)
3285 goto done;
3286 seen_length = 1;
3288 goto rparen;
3291 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3292 if (gfc_match (" len =") == MATCH_YES)
3294 m = char_len_param_value (&len, &deferred);
3295 if (m == MATCH_NO)
3296 goto syntax;
3297 if (m == MATCH_ERROR)
3298 goto done;
3299 seen_length = 1;
3301 if (gfc_match_char (')') == MATCH_YES)
3302 goto done;
3304 if (gfc_match (" , kind =") != MATCH_YES)
3305 goto syntax;
3307 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3308 goto done;
3310 goto rparen;
3313 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3314 m = char_len_param_value (&len, &deferred);
3315 if (m == MATCH_NO)
3316 goto syntax;
3317 if (m == MATCH_ERROR)
3318 goto done;
3319 seen_length = 1;
3321 m = gfc_match_char (')');
3322 if (m == MATCH_YES)
3323 goto done;
3325 if (gfc_match_char (',') != MATCH_YES)
3326 goto syntax;
3328 gfc_match (" kind ="); /* Gobble optional text. */
3330 m = match_char_kind (&kind, &is_iso_c);
3331 if (m == MATCH_ERROR)
3332 goto done;
3333 if (m == MATCH_NO)
3334 goto syntax;
3336 rparen:
3337 /* Require a right-paren at this point. */
3338 m = gfc_match_char (')');
3339 if (m == MATCH_YES)
3340 goto done;
3342 syntax:
3343 gfc_error ("Syntax error in CHARACTER declaration at %C");
3344 m = MATCH_ERROR;
3345 gfc_free_expr (len);
3346 return m;
3348 done:
3349 /* Deal with character functions after USE and IMPORT statements. */
3350 if (gfc_matching_function)
3352 gfc_free_expr (len);
3353 gfc_undo_symbols ();
3354 return MATCH_YES;
3357 if (m != MATCH_YES)
3359 gfc_free_expr (len);
3360 return m;
3363 /* Do some final massaging of the length values. */
3364 cl = gfc_new_charlen (gfc_current_ns, NULL);
3366 if (seen_length == 0)
3367 cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3368 else
3370 /* If gfortran ends up here, then len may be reducible to a constant.
3371 Try to do that here. If it does not reduce, simply assign len to
3372 charlen. A complication occurs with user-defined generic functions,
3373 which are not resolved. Use a private namespace to deal with
3374 generic functions. */
3376 if (len && len->expr_type != EXPR_CONSTANT)
3378 gfc_namespace *old_ns;
3379 gfc_expr *e;
3381 old_ns = gfc_current_ns;
3382 gfc_current_ns = gfc_get_namespace (NULL, 0);
3384 e = gfc_copy_expr (len);
3385 gfc_reduce_init_expr (e);
3386 if (e->expr_type == EXPR_CONSTANT)
3388 gfc_replace_expr (len, e);
3389 if (mpz_cmp_si (len->value.integer, 0) < 0)
3390 mpz_set_ui (len->value.integer, 0);
3392 else
3393 gfc_free_expr (e);
3395 gfc_free_namespace (gfc_current_ns);
3396 gfc_current_ns = old_ns;
3399 cl->length = len;
3402 ts->u.cl = cl;
3403 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3404 ts->deferred = deferred;
3406 /* We have to know if it was a C interoperable kind so we can
3407 do accurate type checking of bind(c) procs, etc. */
3408 if (kind != 0)
3409 /* Mark this as C interoperable if being declared with one
3410 of the named constants from iso_c_binding. */
3411 ts->is_c_interop = is_iso_c;
3412 else if (len != NULL)
3413 /* Here, we might have parsed something such as: character(c_char)
3414 In this case, the parsing code above grabs the c_char when
3415 looking for the length (line 1690, roughly). it's the last
3416 testcase for parsing the kind params of a character variable.
3417 However, it's not actually the length. this seems like it
3418 could be an error.
3419 To see if the user used a C interop kind, test the expr
3420 of the so called length, and see if it's C interoperable. */
3421 ts->is_c_interop = len->ts.is_iso_c;
3423 return MATCH_YES;
3427 /* Matches a RECORD declaration. */
3429 static match
3430 match_record_decl (char *name)
3432 locus old_loc;
3433 old_loc = gfc_current_locus;
3434 match m;
3436 m = gfc_match (" record /");
3437 if (m == MATCH_YES)
3439 if (!flag_dec_structure)
3441 gfc_current_locus = old_loc;
3442 gfc_error ("RECORD at %C is an extension, enable it with "
3443 "-fdec-structure");
3444 return MATCH_ERROR;
3446 m = gfc_match (" %n/", name);
3447 if (m == MATCH_YES)
3448 return MATCH_YES;
3451 gfc_current_locus = old_loc;
3452 if (flag_dec_structure
3453 && (gfc_match (" record% ") == MATCH_YES
3454 || gfc_match (" record%t") == MATCH_YES))
3455 gfc_error ("Structure name expected after RECORD at %C");
3456 if (m == MATCH_NO)
3457 return MATCH_NO;
3459 return MATCH_ERROR;
3463 /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3464 of expressions to substitute into the possibly parameterized expression
3465 'e'. Using a list is inefficient but should not be too bad since the
3466 number of type parameters is not likely to be large. */
3467 static bool
3468 insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3469 int* f)
3471 gfc_actual_arglist *param;
3472 gfc_expr *copy;
3474 if (e->expr_type != EXPR_VARIABLE)
3475 return false;
3477 gcc_assert (e->symtree);
3478 if (e->symtree->n.sym->attr.pdt_kind
3479 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3481 for (param = type_param_spec_list; param; param = param->next)
3482 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3483 break;
3485 if (param)
3487 copy = gfc_copy_expr (param->expr);
3488 *e = *copy;
3489 free (copy);
3493 return false;
3497 bool
3498 gfc_insert_kind_parameter_exprs (gfc_expr *e)
3500 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3504 bool
3505 gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3507 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3508 type_param_spec_list = param_list;
3509 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3510 type_param_spec_list = NULL;
3511 type_param_spec_list = old_param_spec_list;
3514 /* Determines the instance of a parameterized derived type to be used by
3515 matching determining the values of the kind parameters and using them
3516 in the name of the instance. If the instance exists, it is used, otherwise
3517 a new derived type is created. */
3518 match
3519 gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3520 gfc_actual_arglist **ext_param_list)
3522 /* The PDT template symbol. */
3523 gfc_symbol *pdt = *sym;
3524 /* The symbol for the parameter in the template f2k_namespace. */
3525 gfc_symbol *param;
3526 /* The hoped for instance of the PDT. */
3527 gfc_symbol *instance;
3528 /* The list of parameters appearing in the PDT declaration. */
3529 gfc_formal_arglist *type_param_name_list;
3530 /* Used to store the parameter specification list during recursive calls. */
3531 gfc_actual_arglist *old_param_spec_list;
3532 /* Pointers to the parameter specification being used. */
3533 gfc_actual_arglist *actual_param;
3534 gfc_actual_arglist *tail = NULL;
3535 /* Used to build up the name of the PDT instance. The prefix uses 4
3536 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3537 char name[GFC_MAX_SYMBOL_LEN + 21];
3539 bool name_seen = (param_list == NULL);
3540 bool assumed_seen = false;
3541 bool deferred_seen = false;
3542 bool spec_error = false;
3543 int kind_value, i;
3544 gfc_expr *kind_expr;
3545 gfc_component *c1, *c2;
3546 match m;
3548 type_param_spec_list = NULL;
3550 type_param_name_list = pdt->formal;
3551 actual_param = param_list;
3552 sprintf (name, "Pdt%s", pdt->name);
3554 /* Run through the parameter name list and pick up the actual
3555 parameter values or use the default values in the PDT declaration. */
3556 for (; type_param_name_list;
3557 type_param_name_list = type_param_name_list->next)
3559 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3561 if (actual_param->spec_type == SPEC_ASSUMED)
3562 spec_error = deferred_seen;
3563 else
3564 spec_error = assumed_seen;
3566 if (spec_error)
3568 gfc_error ("The type parameter spec list at %C cannot contain "
3569 "both ASSUMED and DEFERRED parameters");
3570 goto error_return;
3574 if (actual_param && actual_param->name)
3575 name_seen = true;
3576 param = type_param_name_list->sym;
3578 if (!param || !param->name)
3579 continue;
3581 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3582 /* An error should already have been thrown in resolve.c
3583 (resolve_fl_derived0). */
3584 if (!pdt->attr.use_assoc && !c1)
3585 goto error_return;
3587 kind_expr = NULL;
3588 if (!name_seen)
3590 if (!actual_param && !(c1 && c1->initializer))
3592 gfc_error ("The type parameter spec list at %C does not contain "
3593 "enough parameter expressions");
3594 goto error_return;
3596 else if (!actual_param && c1 && c1->initializer)
3597 kind_expr = gfc_copy_expr (c1->initializer);
3598 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3599 kind_expr = gfc_copy_expr (actual_param->expr);
3601 else
3603 actual_param = param_list;
3604 for (;actual_param; actual_param = actual_param->next)
3605 if (actual_param->name
3606 && strcmp (actual_param->name, param->name) == 0)
3607 break;
3608 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3609 kind_expr = gfc_copy_expr (actual_param->expr);
3610 else
3612 if (c1->initializer)
3613 kind_expr = gfc_copy_expr (c1->initializer);
3614 else if (!(actual_param && param->attr.pdt_len))
3616 gfc_error ("The derived parameter %qs at %C does not "
3617 "have a default value", param->name);
3618 goto error_return;
3623 /* Store the current parameter expressions in a temporary actual
3624 arglist 'list' so that they can be substituted in the corresponding
3625 expressions in the PDT instance. */
3626 if (type_param_spec_list == NULL)
3628 type_param_spec_list = gfc_get_actual_arglist ();
3629 tail = type_param_spec_list;
3631 else
3633 tail->next = gfc_get_actual_arglist ();
3634 tail = tail->next;
3636 tail->name = param->name;
3638 if (kind_expr)
3640 /* Try simplification even for LEN expressions. */
3641 gfc_resolve_expr (kind_expr);
3642 gfc_simplify_expr (kind_expr, 1);
3643 /* Variable expressions seem to default to BT_PROCEDURE.
3644 TODO find out why this is and fix it. */
3645 if (kind_expr->ts.type != BT_INTEGER
3646 && kind_expr->ts.type != BT_PROCEDURE)
3648 gfc_error ("The parameter expression at %C must be of "
3649 "INTEGER type and not %s type",
3650 gfc_basic_typename (kind_expr->ts.type));
3651 goto error_return;
3654 tail->expr = gfc_copy_expr (kind_expr);
3657 if (actual_param)
3658 tail->spec_type = actual_param->spec_type;
3660 if (!param->attr.pdt_kind)
3662 if (!name_seen && actual_param)
3663 actual_param = actual_param->next;
3664 if (kind_expr)
3666 gfc_free_expr (kind_expr);
3667 kind_expr = NULL;
3669 continue;
3672 if (actual_param
3673 && (actual_param->spec_type == SPEC_ASSUMED
3674 || actual_param->spec_type == SPEC_DEFERRED))
3676 gfc_error ("The KIND parameter %qs at %C cannot either be "
3677 "ASSUMED or DEFERRED", param->name);
3678 goto error_return;
3681 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3683 gfc_error ("The value for the KIND parameter %qs at %C does not "
3684 "reduce to a constant expression", param->name);
3685 goto error_return;
3688 gfc_extract_int (kind_expr, &kind_value);
3689 sprintf (name + strlen (name), "_%d", kind_value);
3691 if (!name_seen && actual_param)
3692 actual_param = actual_param->next;
3693 gfc_free_expr (kind_expr);
3696 if (!name_seen && actual_param)
3698 gfc_error ("The type parameter spec list at %C contains too many "
3699 "parameter expressions");
3700 goto error_return;
3703 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3704 build it, using 'pdt' as a template. */
3705 if (gfc_get_symbol (name, pdt->ns, &instance))
3707 gfc_error ("Parameterized derived type at %C is ambiguous");
3708 goto error_return;
3711 m = MATCH_YES;
3713 if (instance->attr.flavor == FL_DERIVED
3714 && instance->attr.pdt_type)
3716 instance->refs++;
3717 if (ext_param_list)
3718 *ext_param_list = type_param_spec_list;
3719 *sym = instance;
3720 gfc_commit_symbols ();
3721 return m;
3724 /* Start building the new instance of the parameterized type. */
3725 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3726 instance->attr.pdt_template = 0;
3727 instance->attr.pdt_type = 1;
3728 instance->declared_at = gfc_current_locus;
3730 /* Add the components, replacing the parameters in all expressions
3731 with the expressions for their values in 'type_param_spec_list'. */
3732 c1 = pdt->components;
3733 tail = type_param_spec_list;
3734 for (; c1; c1 = c1->next)
3736 gfc_add_component (instance, c1->name, &c2);
3738 c2->ts = c1->ts;
3739 c2->attr = c1->attr;
3741 /* The order of declaration of the type_specs might not be the
3742 same as that of the components. */
3743 if (c1->attr.pdt_kind || c1->attr.pdt_len)
3745 for (tail = type_param_spec_list; tail; tail = tail->next)
3746 if (strcmp (c1->name, tail->name) == 0)
3747 break;
3750 /* Deal with type extension by recursively calling this function
3751 to obtain the instance of the extended type. */
3752 if (gfc_current_state () != COMP_DERIVED
3753 && c1 == pdt->components
3754 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3755 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3756 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3758 gfc_formal_arglist *f;
3760 old_param_spec_list = type_param_spec_list;
3762 /* Obtain a spec list appropriate to the extended type..*/
3763 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3764 type_param_spec_list = actual_param;
3765 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3766 actual_param = actual_param->next;
3767 if (actual_param)
3769 gfc_free_actual_arglist (actual_param->next);
3770 actual_param->next = NULL;
3773 /* Now obtain the PDT instance for the extended type. */
3774 c2->param_list = type_param_spec_list;
3775 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3776 NULL);
3777 type_param_spec_list = old_param_spec_list;
3779 c2->ts.u.derived->refs++;
3780 gfc_set_sym_referenced (c2->ts.u.derived);
3782 /* Set extension level. */
3783 if (c2->ts.u.derived->attr.extension == 255)
3785 /* Since the extension field is 8 bit wide, we can only have
3786 up to 255 extension levels. */
3787 gfc_error ("Maximum extension level reached with type %qs at %L",
3788 c2->ts.u.derived->name,
3789 &c2->ts.u.derived->declared_at);
3790 goto error_return;
3792 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3794 continue;
3797 /* Set the component kind using the parameterized expression. */
3798 if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3799 && c1->kind_expr != NULL)
3801 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3802 gfc_insert_kind_parameter_exprs (e);
3803 gfc_simplify_expr (e, 1);
3804 gfc_extract_int (e, &c2->ts.kind);
3805 gfc_free_expr (e);
3806 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3808 gfc_error ("Kind %d not supported for type %s at %C",
3809 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3810 goto error_return;
3814 /* Similarly, set the string length if parameterized. */
3815 if (c1->ts.type == BT_CHARACTER
3816 && c1->ts.u.cl->length
3817 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3819 gfc_expr *e;
3820 e = gfc_copy_expr (c1->ts.u.cl->length);
3821 gfc_insert_kind_parameter_exprs (e);
3822 gfc_simplify_expr (e, 1);
3823 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3824 c2->ts.u.cl->length = e;
3825 c2->attr.pdt_string = 1;
3828 /* Set up either the KIND/LEN initializer, if constant,
3829 or the parameterized expression. Use the template
3830 initializer if one is not already set in this instance. */
3831 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3833 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3834 c2->initializer = gfc_copy_expr (tail->expr);
3835 else if (tail && tail->expr)
3837 c2->param_list = gfc_get_actual_arglist ();
3838 c2->param_list->name = tail->name;
3839 c2->param_list->expr = gfc_copy_expr (tail->expr);
3840 c2->param_list->next = NULL;
3843 if (!c2->initializer && c1->initializer)
3844 c2->initializer = gfc_copy_expr (c1->initializer);
3847 /* Copy the array spec. */
3848 c2->as = gfc_copy_array_spec (c1->as);
3849 if (c1->ts.type == BT_CLASS)
3850 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
3852 /* Determine if an array spec is parameterized. If so, substitute
3853 in the parameter expressions for the bounds and set the pdt_array
3854 attribute. Notice that this attribute must be unconditionally set
3855 if this is an array of parameterized character length. */
3856 if (c1->as && c1->as->type == AS_EXPLICIT)
3858 bool pdt_array = false;
3860 /* Are the bounds of the array parameterized? */
3861 for (i = 0; i < c1->as->rank; i++)
3863 if (gfc_derived_parameter_expr (c1->as->lower[i]))
3864 pdt_array = true;
3865 if (gfc_derived_parameter_expr (c1->as->upper[i]))
3866 pdt_array = true;
3869 /* If they are, free the expressions for the bounds and
3870 replace them with the template expressions with substitute
3871 values. */
3872 for (i = 0; pdt_array && i < c1->as->rank; i++)
3874 gfc_expr *e;
3875 e = gfc_copy_expr (c1->as->lower[i]);
3876 gfc_insert_kind_parameter_exprs (e);
3877 gfc_simplify_expr (e, 1);
3878 gfc_free_expr (c2->as->lower[i]);
3879 c2->as->lower[i] = e;
3880 e = gfc_copy_expr (c1->as->upper[i]);
3881 gfc_insert_kind_parameter_exprs (e);
3882 gfc_simplify_expr (e, 1);
3883 gfc_free_expr (c2->as->upper[i]);
3884 c2->as->upper[i] = e;
3886 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
3887 if (c1->initializer)
3889 c2->initializer = gfc_copy_expr (c1->initializer);
3890 gfc_insert_kind_parameter_exprs (c2->initializer);
3891 gfc_simplify_expr (c2->initializer, 1);
3895 /* Recurse into this function for PDT components. */
3896 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3897 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
3899 gfc_actual_arglist *params;
3900 /* The component in the template has a list of specification
3901 expressions derived from its declaration. */
3902 params = gfc_copy_actual_arglist (c1->param_list);
3903 actual_param = params;
3904 /* Substitute the template parameters with the expressions
3905 from the specification list. */
3906 for (;actual_param; actual_param = actual_param->next)
3907 gfc_insert_parameter_exprs (actual_param->expr,
3908 type_param_spec_list);
3910 /* Now obtain the PDT instance for the component. */
3911 old_param_spec_list = type_param_spec_list;
3912 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
3913 type_param_spec_list = old_param_spec_list;
3915 c2->param_list = params;
3916 if (!(c2->attr.pointer || c2->attr.allocatable))
3917 c2->initializer = gfc_default_initializer (&c2->ts);
3919 if (c2->attr.allocatable)
3920 instance->attr.alloc_comp = 1;
3924 gfc_commit_symbol (instance);
3925 if (ext_param_list)
3926 *ext_param_list = type_param_spec_list;
3927 *sym = instance;
3928 return m;
3930 error_return:
3931 gfc_free_actual_arglist (type_param_spec_list);
3932 return MATCH_ERROR;
3936 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
3937 structure to the matched specification. This is necessary for FUNCTION and
3938 IMPLICIT statements.
3940 If implicit_flag is nonzero, then we don't check for the optional
3941 kind specification. Not doing so is needed for matching an IMPLICIT
3942 statement correctly. */
3944 match
3945 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
3947 char name[GFC_MAX_SYMBOL_LEN + 1];
3948 gfc_symbol *sym, *dt_sym;
3949 match m;
3950 char c;
3951 bool seen_deferred_kind, matched_type;
3952 const char *dt_name;
3954 decl_type_param_list = NULL;
3956 /* A belt and braces check that the typespec is correctly being treated
3957 as a deferred characteristic association. */
3958 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
3959 && (gfc_current_block ()->result->ts.kind == -1)
3960 && (ts->kind == -1);
3961 gfc_clear_ts (ts);
3962 if (seen_deferred_kind)
3963 ts->kind = -1;
3965 /* Clear the current binding label, in case one is given. */
3966 curr_binding_label = NULL;
3968 if (gfc_match (" byte") == MATCH_YES)
3970 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
3971 return MATCH_ERROR;
3973 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
3975 gfc_error ("BYTE type used at %C "
3976 "is not available on the target machine");
3977 return MATCH_ERROR;
3980 ts->type = BT_INTEGER;
3981 ts->kind = 1;
3982 return MATCH_YES;
3986 m = gfc_match (" type (");
3987 matched_type = (m == MATCH_YES);
3988 if (matched_type)
3990 gfc_gobble_whitespace ();
3991 if (gfc_peek_ascii_char () == '*')
3993 if ((m = gfc_match ("*)")) != MATCH_YES)
3994 return m;
3995 if (gfc_comp_struct (gfc_current_state ()))
3997 gfc_error ("Assumed type at %C is not allowed for components");
3998 return MATCH_ERROR;
4000 if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4001 return MATCH_ERROR;
4002 ts->type = BT_ASSUMED;
4003 return MATCH_YES;
4006 m = gfc_match ("%n", name);
4007 matched_type = (m == MATCH_YES);
4010 if ((matched_type && strcmp ("integer", name) == 0)
4011 || (!matched_type && gfc_match (" integer") == MATCH_YES))
4013 ts->type = BT_INTEGER;
4014 ts->kind = gfc_default_integer_kind;
4015 goto get_kind;
4018 if ((matched_type && strcmp ("character", name) == 0)
4019 || (!matched_type && gfc_match (" character") == MATCH_YES))
4021 if (matched_type
4022 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4023 "intrinsic-type-spec at %C"))
4024 return MATCH_ERROR;
4026 ts->type = BT_CHARACTER;
4027 if (implicit_flag == 0)
4028 m = gfc_match_char_spec (ts);
4029 else
4030 m = MATCH_YES;
4032 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4033 m = MATCH_ERROR;
4035 return m;
4038 if ((matched_type && strcmp ("real", name) == 0)
4039 || (!matched_type && gfc_match (" real") == MATCH_YES))
4041 ts->type = BT_REAL;
4042 ts->kind = gfc_default_real_kind;
4043 goto get_kind;
4046 if ((matched_type
4047 && (strcmp ("doubleprecision", name) == 0
4048 || (strcmp ("double", name) == 0
4049 && gfc_match (" precision") == MATCH_YES)))
4050 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4052 if (matched_type
4053 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4054 "intrinsic-type-spec at %C"))
4055 return MATCH_ERROR;
4056 if (matched_type && gfc_match_char (')') != MATCH_YES)
4057 return MATCH_ERROR;
4059 ts->type = BT_REAL;
4060 ts->kind = gfc_default_double_kind;
4061 return MATCH_YES;
4064 if ((matched_type && strcmp ("complex", name) == 0)
4065 || (!matched_type && gfc_match (" complex") == MATCH_YES))
4067 ts->type = BT_COMPLEX;
4068 ts->kind = gfc_default_complex_kind;
4069 goto get_kind;
4072 if ((matched_type
4073 && (strcmp ("doublecomplex", name) == 0
4074 || (strcmp ("double", name) == 0
4075 && gfc_match (" complex") == MATCH_YES)))
4076 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4078 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4079 return MATCH_ERROR;
4081 if (matched_type
4082 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4083 "intrinsic-type-spec at %C"))
4084 return MATCH_ERROR;
4086 if (matched_type && gfc_match_char (')') != MATCH_YES)
4087 return MATCH_ERROR;
4089 ts->type = BT_COMPLEX;
4090 ts->kind = gfc_default_double_kind;
4091 return MATCH_YES;
4094 if ((matched_type && strcmp ("logical", name) == 0)
4095 || (!matched_type && gfc_match (" logical") == MATCH_YES))
4097 ts->type = BT_LOGICAL;
4098 ts->kind = gfc_default_logical_kind;
4099 goto get_kind;
4102 if (matched_type)
4104 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4105 if (m == MATCH_ERROR)
4106 return m;
4108 m = gfc_match_char (')');
4111 if (m != MATCH_YES)
4112 m = match_record_decl (name);
4114 if (matched_type || m == MATCH_YES)
4116 ts->type = BT_DERIVED;
4117 /* We accept record/s/ or type(s) where s is a structure, but we
4118 * don't need all the extra derived-type stuff for structures. */
4119 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4121 gfc_error ("Type name %qs at %C is ambiguous", name);
4122 return MATCH_ERROR;
4125 if (sym && sym->attr.flavor == FL_DERIVED
4126 && sym->attr.pdt_template
4127 && gfc_current_state () != COMP_DERIVED)
4129 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4130 if (m != MATCH_YES)
4131 return m;
4132 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4133 ts->u.derived = sym;
4134 strcpy (name, gfc_dt_lower_string (sym->name));
4137 if (sym && sym->attr.flavor == FL_STRUCT)
4139 ts->u.derived = sym;
4140 return MATCH_YES;
4142 /* Actually a derived type. */
4145 else
4147 /* Match nested STRUCTURE declarations; only valid within another
4148 structure declaration. */
4149 if (flag_dec_structure
4150 && (gfc_current_state () == COMP_STRUCTURE
4151 || gfc_current_state () == COMP_MAP))
4153 m = gfc_match (" structure");
4154 if (m == MATCH_YES)
4156 m = gfc_match_structure_decl ();
4157 if (m == MATCH_YES)
4159 /* gfc_new_block is updated by match_structure_decl. */
4160 ts->type = BT_DERIVED;
4161 ts->u.derived = gfc_new_block;
4162 return MATCH_YES;
4165 if (m == MATCH_ERROR)
4166 return MATCH_ERROR;
4169 /* Match CLASS declarations. */
4170 m = gfc_match (" class ( * )");
4171 if (m == MATCH_ERROR)
4172 return MATCH_ERROR;
4173 else if (m == MATCH_YES)
4175 gfc_symbol *upe;
4176 gfc_symtree *st;
4177 ts->type = BT_CLASS;
4178 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4179 if (upe == NULL)
4181 upe = gfc_new_symbol ("STAR", gfc_current_ns);
4182 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4183 st->n.sym = upe;
4184 gfc_set_sym_referenced (upe);
4185 upe->refs++;
4186 upe->ts.type = BT_VOID;
4187 upe->attr.unlimited_polymorphic = 1;
4188 /* This is essential to force the construction of
4189 unlimited polymorphic component class containers. */
4190 upe->attr.zero_comp = 1;
4191 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4192 &gfc_current_locus))
4193 return MATCH_ERROR;
4195 else
4197 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4198 st->n.sym = upe;
4199 upe->refs++;
4201 ts->u.derived = upe;
4202 return m;
4205 m = gfc_match (" class (");
4207 if (m == MATCH_YES)
4208 m = gfc_match ("%n", name);
4209 else
4210 return m;
4212 if (m != MATCH_YES)
4213 return m;
4214 ts->type = BT_CLASS;
4216 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4217 return MATCH_ERROR;
4219 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4220 if (m == MATCH_ERROR)
4221 return m;
4223 m = gfc_match_char (')');
4224 if (m != MATCH_YES)
4225 return m;
4228 /* Defer association of the derived type until the end of the
4229 specification block. However, if the derived type can be
4230 found, add it to the typespec. */
4231 if (gfc_matching_function)
4233 ts->u.derived = NULL;
4234 if (gfc_current_state () != COMP_INTERFACE
4235 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4237 sym = gfc_find_dt_in_generic (sym);
4238 ts->u.derived = sym;
4240 return MATCH_YES;
4243 /* Search for the name but allow the components to be defined later. If
4244 type = -1, this typespec has been seen in a function declaration but
4245 the type could not be accessed at that point. The actual derived type is
4246 stored in a symtree with the first letter of the name capitalized; the
4247 symtree with the all lower-case name contains the associated
4248 generic function. */
4249 dt_name = gfc_dt_upper_string (name);
4250 sym = NULL;
4251 dt_sym = NULL;
4252 if (ts->kind != -1)
4254 gfc_get_ha_symbol (name, &sym);
4255 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4257 gfc_error ("Type name %qs at %C is ambiguous", name);
4258 return MATCH_ERROR;
4260 if (sym->generic && !dt_sym)
4261 dt_sym = gfc_find_dt_in_generic (sym);
4263 /* Host associated PDTs can get confused with their constructors
4264 because they ar instantiated in the template's namespace. */
4265 if (!dt_sym)
4267 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4269 gfc_error ("Type name %qs at %C is ambiguous", name);
4270 return MATCH_ERROR;
4272 if (dt_sym && !dt_sym->attr.pdt_type)
4273 dt_sym = NULL;
4276 else if (ts->kind == -1)
4278 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4279 || gfc_current_ns->has_import_set;
4280 gfc_find_symbol (name, NULL, iface, &sym);
4281 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4283 gfc_error ("Type name %qs at %C is ambiguous", name);
4284 return MATCH_ERROR;
4286 if (sym && sym->generic && !dt_sym)
4287 dt_sym = gfc_find_dt_in_generic (sym);
4289 ts->kind = 0;
4290 if (sym == NULL)
4291 return MATCH_NO;
4294 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4295 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4296 || sym->attr.subroutine)
4298 gfc_error ("Type name %qs at %C conflicts with previously declared "
4299 "entity at %L, which has the same name", name,
4300 &sym->declared_at);
4301 return MATCH_ERROR;
4304 if (sym && sym->attr.flavor == FL_DERIVED
4305 && sym->attr.pdt_template
4306 && gfc_current_state () != COMP_DERIVED)
4308 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4309 if (m != MATCH_YES)
4310 return m;
4311 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4312 ts->u.derived = sym;
4313 strcpy (name, gfc_dt_lower_string (sym->name));
4316 gfc_save_symbol_data (sym);
4317 gfc_set_sym_referenced (sym);
4318 if (!sym->attr.generic
4319 && !gfc_add_generic (&sym->attr, sym->name, NULL))
4320 return MATCH_ERROR;
4322 if (!sym->attr.function
4323 && !gfc_add_function (&sym->attr, sym->name, NULL))
4324 return MATCH_ERROR;
4326 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4327 && dt_sym->attr.pdt_template
4328 && gfc_current_state () != COMP_DERIVED)
4330 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4331 if (m != MATCH_YES)
4332 return m;
4333 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4336 if (!dt_sym)
4338 gfc_interface *intr, *head;
4340 /* Use upper case to save the actual derived-type symbol. */
4341 gfc_get_symbol (dt_name, NULL, &dt_sym);
4342 dt_sym->name = gfc_get_string ("%s", sym->name);
4343 head = sym->generic;
4344 intr = gfc_get_interface ();
4345 intr->sym = dt_sym;
4346 intr->where = gfc_current_locus;
4347 intr->next = head;
4348 sym->generic = intr;
4349 sym->attr.if_source = IFSRC_DECL;
4351 else
4352 gfc_save_symbol_data (dt_sym);
4354 gfc_set_sym_referenced (dt_sym);
4356 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4357 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4358 return MATCH_ERROR;
4360 ts->u.derived = dt_sym;
4362 return MATCH_YES;
4364 get_kind:
4365 if (matched_type
4366 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4367 "intrinsic-type-spec at %C"))
4368 return MATCH_ERROR;
4370 /* For all types except double, derived and character, look for an
4371 optional kind specifier. MATCH_NO is actually OK at this point. */
4372 if (implicit_flag == 1)
4374 if (matched_type && gfc_match_char (')') != MATCH_YES)
4375 return MATCH_ERROR;
4377 return MATCH_YES;
4380 if (gfc_current_form == FORM_FREE)
4382 c = gfc_peek_ascii_char ();
4383 if (!gfc_is_whitespace (c) && c != '*' && c != '('
4384 && c != ':' && c != ',')
4386 if (matched_type && c == ')')
4388 gfc_next_ascii_char ();
4389 return MATCH_YES;
4391 return MATCH_NO;
4395 m = gfc_match_kind_spec (ts, false);
4396 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4398 m = gfc_match_old_kind_spec (ts);
4399 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4400 return MATCH_ERROR;
4403 if (matched_type && gfc_match_char (')') != MATCH_YES)
4404 return MATCH_ERROR;
4406 /* Defer association of the KIND expression of function results
4407 until after USE and IMPORT statements. */
4408 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4409 || gfc_matching_function)
4410 return MATCH_YES;
4412 if (m == MATCH_NO)
4413 m = MATCH_YES; /* No kind specifier found. */
4415 return m;
4419 /* Match an IMPLICIT NONE statement. Actually, this statement is
4420 already matched in parse.c, or we would not end up here in the
4421 first place. So the only thing we need to check, is if there is
4422 trailing garbage. If not, the match is successful. */
4424 match
4425 gfc_match_implicit_none (void)
4427 char c;
4428 match m;
4429 char name[GFC_MAX_SYMBOL_LEN + 1];
4430 bool type = false;
4431 bool external = false;
4432 locus cur_loc = gfc_current_locus;
4434 if (gfc_current_ns->seen_implicit_none
4435 || gfc_current_ns->has_implicit_none_export)
4437 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4438 return MATCH_ERROR;
4441 gfc_gobble_whitespace ();
4442 c = gfc_peek_ascii_char ();
4443 if (c == '(')
4445 (void) gfc_next_ascii_char ();
4446 if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
4447 return MATCH_ERROR;
4449 gfc_gobble_whitespace ();
4450 if (gfc_peek_ascii_char () == ')')
4452 (void) gfc_next_ascii_char ();
4453 type = true;
4455 else
4456 for(;;)
4458 m = gfc_match (" %n", name);
4459 if (m != MATCH_YES)
4460 return MATCH_ERROR;
4462 if (strcmp (name, "type") == 0)
4463 type = true;
4464 else if (strcmp (name, "external") == 0)
4465 external = true;
4466 else
4467 return MATCH_ERROR;
4469 gfc_gobble_whitespace ();
4470 c = gfc_next_ascii_char ();
4471 if (c == ',')
4472 continue;
4473 if (c == ')')
4474 break;
4475 return MATCH_ERROR;
4478 else
4479 type = true;
4481 if (gfc_match_eos () != MATCH_YES)
4482 return MATCH_ERROR;
4484 gfc_set_implicit_none (type, external, &cur_loc);
4486 return MATCH_YES;
4490 /* Match the letter range(s) of an IMPLICIT statement. */
4492 static match
4493 match_implicit_range (void)
4495 char c, c1, c2;
4496 int inner;
4497 locus cur_loc;
4499 cur_loc = gfc_current_locus;
4501 gfc_gobble_whitespace ();
4502 c = gfc_next_ascii_char ();
4503 if (c != '(')
4505 gfc_error ("Missing character range in IMPLICIT at %C");
4506 goto bad;
4509 inner = 1;
4510 while (inner)
4512 gfc_gobble_whitespace ();
4513 c1 = gfc_next_ascii_char ();
4514 if (!ISALPHA (c1))
4515 goto bad;
4517 gfc_gobble_whitespace ();
4518 c = gfc_next_ascii_char ();
4520 switch (c)
4522 case ')':
4523 inner = 0; /* Fall through. */
4525 case ',':
4526 c2 = c1;
4527 break;
4529 case '-':
4530 gfc_gobble_whitespace ();
4531 c2 = gfc_next_ascii_char ();
4532 if (!ISALPHA (c2))
4533 goto bad;
4535 gfc_gobble_whitespace ();
4536 c = gfc_next_ascii_char ();
4538 if ((c != ',') && (c != ')'))
4539 goto bad;
4540 if (c == ')')
4541 inner = 0;
4543 break;
4545 default:
4546 goto bad;
4549 if (c1 > c2)
4551 gfc_error ("Letters must be in alphabetic order in "
4552 "IMPLICIT statement at %C");
4553 goto bad;
4556 /* See if we can add the newly matched range to the pending
4557 implicits from this IMPLICIT statement. We do not check for
4558 conflicts with whatever earlier IMPLICIT statements may have
4559 set. This is done when we've successfully finished matching
4560 the current one. */
4561 if (!gfc_add_new_implicit_range (c1, c2))
4562 goto bad;
4565 return MATCH_YES;
4567 bad:
4568 gfc_syntax_error (ST_IMPLICIT);
4570 gfc_current_locus = cur_loc;
4571 return MATCH_ERROR;
4575 /* Match an IMPLICIT statement, storing the types for
4576 gfc_set_implicit() if the statement is accepted by the parser.
4577 There is a strange looking, but legal syntactic construction
4578 possible. It looks like:
4580 IMPLICIT INTEGER (a-b) (c-d)
4582 This is legal if "a-b" is a constant expression that happens to
4583 equal one of the legal kinds for integers. The real problem
4584 happens with an implicit specification that looks like:
4586 IMPLICIT INTEGER (a-b)
4588 In this case, a typespec matcher that is "greedy" (as most of the
4589 matchers are) gobbles the character range as a kindspec, leaving
4590 nothing left. We therefore have to go a bit more slowly in the
4591 matching process by inhibiting the kindspec checking during
4592 typespec matching and checking for a kind later. */
4594 match
4595 gfc_match_implicit (void)
4597 gfc_typespec ts;
4598 locus cur_loc;
4599 char c;
4600 match m;
4602 if (gfc_current_ns->seen_implicit_none)
4604 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4605 "statement");
4606 return MATCH_ERROR;
4609 gfc_clear_ts (&ts);
4611 /* We don't allow empty implicit statements. */
4612 if (gfc_match_eos () == MATCH_YES)
4614 gfc_error ("Empty IMPLICIT statement at %C");
4615 return MATCH_ERROR;
4620 /* First cleanup. */
4621 gfc_clear_new_implicit ();
4623 /* A basic type is mandatory here. */
4624 m = gfc_match_decl_type_spec (&ts, 1);
4625 if (m == MATCH_ERROR)
4626 goto error;
4627 if (m == MATCH_NO)
4628 goto syntax;
4630 cur_loc = gfc_current_locus;
4631 m = match_implicit_range ();
4633 if (m == MATCH_YES)
4635 /* We may have <TYPE> (<RANGE>). */
4636 gfc_gobble_whitespace ();
4637 c = gfc_peek_ascii_char ();
4638 if (c == ',' || c == '\n' || c == ';' || c == '!')
4640 /* Check for CHARACTER with no length parameter. */
4641 if (ts.type == BT_CHARACTER && !ts.u.cl)
4643 ts.kind = gfc_default_character_kind;
4644 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4645 ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4646 NULL, 1);
4649 /* Record the Successful match. */
4650 if (!gfc_merge_new_implicit (&ts))
4651 return MATCH_ERROR;
4652 if (c == ',')
4653 c = gfc_next_ascii_char ();
4654 else if (gfc_match_eos () == MATCH_ERROR)
4655 goto error;
4656 continue;
4659 gfc_current_locus = cur_loc;
4662 /* Discard the (incorrectly) matched range. */
4663 gfc_clear_new_implicit ();
4665 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4666 if (ts.type == BT_CHARACTER)
4667 m = gfc_match_char_spec (&ts);
4668 else
4670 m = gfc_match_kind_spec (&ts, false);
4671 if (m == MATCH_NO)
4673 m = gfc_match_old_kind_spec (&ts);
4674 if (m == MATCH_ERROR)
4675 goto error;
4676 if (m == MATCH_NO)
4677 goto syntax;
4680 if (m == MATCH_ERROR)
4681 goto error;
4683 m = match_implicit_range ();
4684 if (m == MATCH_ERROR)
4685 goto error;
4686 if (m == MATCH_NO)
4687 goto syntax;
4689 gfc_gobble_whitespace ();
4690 c = gfc_next_ascii_char ();
4691 if (c != ',' && gfc_match_eos () != MATCH_YES)
4692 goto syntax;
4694 if (!gfc_merge_new_implicit (&ts))
4695 return MATCH_ERROR;
4697 while (c == ',');
4699 return MATCH_YES;
4701 syntax:
4702 gfc_syntax_error (ST_IMPLICIT);
4704 error:
4705 return MATCH_ERROR;
4709 match
4710 gfc_match_import (void)
4712 char name[GFC_MAX_SYMBOL_LEN + 1];
4713 match m;
4714 gfc_symbol *sym;
4715 gfc_symtree *st;
4717 if (gfc_current_ns->proc_name == NULL
4718 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4720 gfc_error ("IMPORT statement at %C only permitted in "
4721 "an INTERFACE body");
4722 return MATCH_ERROR;
4725 if (gfc_current_ns->proc_name->attr.module_procedure)
4727 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4728 "in a module procedure interface body");
4729 return MATCH_ERROR;
4732 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4733 return MATCH_ERROR;
4735 if (gfc_match_eos () == MATCH_YES)
4737 /* All host variables should be imported. */
4738 gfc_current_ns->has_import_set = 1;
4739 return MATCH_YES;
4742 if (gfc_match (" ::") == MATCH_YES)
4744 if (gfc_match_eos () == MATCH_YES)
4746 gfc_error ("Expecting list of named entities at %C");
4747 return MATCH_ERROR;
4751 for(;;)
4753 sym = NULL;
4754 m = gfc_match (" %n", name);
4755 switch (m)
4757 case MATCH_YES:
4758 if (gfc_current_ns->parent != NULL
4759 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4761 gfc_error ("Type name %qs at %C is ambiguous", name);
4762 return MATCH_ERROR;
4764 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
4765 && gfc_find_symbol (name,
4766 gfc_current_ns->proc_name->ns->parent,
4767 1, &sym))
4769 gfc_error ("Type name %qs at %C is ambiguous", name);
4770 return MATCH_ERROR;
4773 if (sym == NULL)
4775 gfc_error ("Cannot IMPORT %qs from host scoping unit "
4776 "at %C - does not exist.", name);
4777 return MATCH_ERROR;
4780 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4782 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4783 "at %C", name);
4784 goto next_item;
4787 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4788 st->n.sym = sym;
4789 sym->refs++;
4790 sym->attr.imported = 1;
4792 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4794 /* The actual derived type is stored in a symtree with the first
4795 letter of the name capitalized; the symtree with the all
4796 lower-case name contains the associated generic function. */
4797 st = gfc_new_symtree (&gfc_current_ns->sym_root,
4798 gfc_dt_upper_string (name));
4799 st->n.sym = sym;
4800 sym->refs++;
4801 sym->attr.imported = 1;
4804 goto next_item;
4806 case MATCH_NO:
4807 break;
4809 case MATCH_ERROR:
4810 return MATCH_ERROR;
4813 next_item:
4814 if (gfc_match_eos () == MATCH_YES)
4815 break;
4816 if (gfc_match_char (',') != MATCH_YES)
4817 goto syntax;
4820 return MATCH_YES;
4822 syntax:
4823 gfc_error ("Syntax error in IMPORT statement at %C");
4824 return MATCH_ERROR;
4828 /* A minimal implementation of gfc_match without whitespace, escape
4829 characters or variable arguments. Returns true if the next
4830 characters match the TARGET template exactly. */
4832 static bool
4833 match_string_p (const char *target)
4835 const char *p;
4837 for (p = target; *p; p++)
4838 if ((char) gfc_next_ascii_char () != *p)
4839 return false;
4840 return true;
4843 /* Matches an attribute specification including array specs. If
4844 successful, leaves the variables current_attr and current_as
4845 holding the specification. Also sets the colon_seen variable for
4846 later use by matchers associated with initializations.
4848 This subroutine is a little tricky in the sense that we don't know
4849 if we really have an attr-spec until we hit the double colon.
4850 Until that time, we can only return MATCH_NO. This forces us to
4851 check for duplicate specification at this level. */
4853 static match
4854 match_attr_spec (void)
4856 /* Modifiers that can exist in a type statement. */
4857 enum
4858 { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
4859 DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
4860 DECL_DIMENSION, DECL_EXTERNAL,
4861 DECL_INTRINSIC, DECL_OPTIONAL,
4862 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
4863 DECL_STATIC, DECL_AUTOMATIC,
4864 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
4865 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
4866 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
4869 /* GFC_DECL_END is the sentinel, index starts at 0. */
4870 #define NUM_DECL GFC_DECL_END
4872 /* Make sure that values from sym_intent are safe to be used here. */
4873 gcc_assert (INTENT_IN > 0);
4875 locus start, seen_at[NUM_DECL];
4876 int seen[NUM_DECL];
4877 unsigned int d;
4878 const char *attr;
4879 match m;
4880 bool t;
4882 gfc_clear_attr (&current_attr);
4883 start = gfc_current_locus;
4885 current_as = NULL;
4886 colon_seen = 0;
4887 attr_seen = 0;
4889 /* See if we get all of the keywords up to the final double colon. */
4890 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4891 seen[d] = 0;
4893 for (;;)
4895 char ch;
4897 d = DECL_NONE;
4898 gfc_gobble_whitespace ();
4900 ch = gfc_next_ascii_char ();
4901 if (ch == ':')
4903 /* This is the successful exit condition for the loop. */
4904 if (gfc_next_ascii_char () == ':')
4905 break;
4907 else if (ch == ',')
4909 gfc_gobble_whitespace ();
4910 switch (gfc_peek_ascii_char ())
4912 case 'a':
4913 gfc_next_ascii_char ();
4914 switch (gfc_next_ascii_char ())
4916 case 'l':
4917 if (match_string_p ("locatable"))
4919 /* Matched "allocatable". */
4920 d = DECL_ALLOCATABLE;
4922 break;
4924 case 's':
4925 if (match_string_p ("ynchronous"))
4927 /* Matched "asynchronous". */
4928 d = DECL_ASYNCHRONOUS;
4930 break;
4932 case 'u':
4933 if (match_string_p ("tomatic"))
4935 /* Matched "automatic". */
4936 d = DECL_AUTOMATIC;
4938 break;
4940 break;
4942 case 'b':
4943 /* Try and match the bind(c). */
4944 m = gfc_match_bind_c (NULL, true);
4945 if (m == MATCH_YES)
4946 d = DECL_IS_BIND_C;
4947 else if (m == MATCH_ERROR)
4948 goto cleanup;
4949 break;
4951 case 'c':
4952 gfc_next_ascii_char ();
4953 if ('o' != gfc_next_ascii_char ())
4954 break;
4955 switch (gfc_next_ascii_char ())
4957 case 'd':
4958 if (match_string_p ("imension"))
4960 d = DECL_CODIMENSION;
4961 break;
4963 /* FALLTHRU */
4964 case 'n':
4965 if (match_string_p ("tiguous"))
4967 d = DECL_CONTIGUOUS;
4968 break;
4971 break;
4973 case 'd':
4974 if (match_string_p ("dimension"))
4975 d = DECL_DIMENSION;
4976 break;
4978 case 'e':
4979 if (match_string_p ("external"))
4980 d = DECL_EXTERNAL;
4981 break;
4983 case 'i':
4984 if (match_string_p ("int"))
4986 ch = gfc_next_ascii_char ();
4987 if (ch == 'e')
4989 if (match_string_p ("nt"))
4991 /* Matched "intent". */
4992 d = match_intent_spec ();
4993 if (d == INTENT_UNKNOWN)
4995 m = MATCH_ERROR;
4996 goto cleanup;
5000 else if (ch == 'r')
5002 if (match_string_p ("insic"))
5004 /* Matched "intrinsic". */
5005 d = DECL_INTRINSIC;
5009 break;
5011 case 'k':
5012 if (match_string_p ("kind"))
5013 d = DECL_KIND;
5014 break;
5016 case 'l':
5017 if (match_string_p ("len"))
5018 d = DECL_LEN;
5019 break;
5021 case 'o':
5022 if (match_string_p ("optional"))
5023 d = DECL_OPTIONAL;
5024 break;
5026 case 'p':
5027 gfc_next_ascii_char ();
5028 switch (gfc_next_ascii_char ())
5030 case 'a':
5031 if (match_string_p ("rameter"))
5033 /* Matched "parameter". */
5034 d = DECL_PARAMETER;
5036 break;
5038 case 'o':
5039 if (match_string_p ("inter"))
5041 /* Matched "pointer". */
5042 d = DECL_POINTER;
5044 break;
5046 case 'r':
5047 ch = gfc_next_ascii_char ();
5048 if (ch == 'i')
5050 if (match_string_p ("vate"))
5052 /* Matched "private". */
5053 d = DECL_PRIVATE;
5056 else if (ch == 'o')
5058 if (match_string_p ("tected"))
5060 /* Matched "protected". */
5061 d = DECL_PROTECTED;
5064 break;
5066 case 'u':
5067 if (match_string_p ("blic"))
5069 /* Matched "public". */
5070 d = DECL_PUBLIC;
5072 break;
5074 break;
5076 case 's':
5077 gfc_next_ascii_char ();
5078 switch (gfc_next_ascii_char ())
5080 case 'a':
5081 if (match_string_p ("ve"))
5083 /* Matched "save". */
5084 d = DECL_SAVE;
5086 break;
5088 case 't':
5089 if (match_string_p ("atic"))
5091 /* Matched "static". */
5092 d = DECL_STATIC;
5094 break;
5096 break;
5098 case 't':
5099 if (match_string_p ("target"))
5100 d = DECL_TARGET;
5101 break;
5103 case 'v':
5104 gfc_next_ascii_char ();
5105 ch = gfc_next_ascii_char ();
5106 if (ch == 'a')
5108 if (match_string_p ("lue"))
5110 /* Matched "value". */
5111 d = DECL_VALUE;
5114 else if (ch == 'o')
5116 if (match_string_p ("latile"))
5118 /* Matched "volatile". */
5119 d = DECL_VOLATILE;
5122 break;
5126 /* No double colon and no recognizable decl_type, so assume that
5127 we've been looking at something else the whole time. */
5128 if (d == DECL_NONE)
5130 m = MATCH_NO;
5131 goto cleanup;
5134 /* Check to make sure any parens are paired up correctly. */
5135 if (gfc_match_parens () == MATCH_ERROR)
5137 m = MATCH_ERROR;
5138 goto cleanup;
5141 seen[d]++;
5142 seen_at[d] = gfc_current_locus;
5144 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
5146 gfc_array_spec *as = NULL;
5148 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5149 d == DECL_CODIMENSION);
5151 if (current_as == NULL)
5152 current_as = as;
5153 else if (m == MATCH_YES)
5155 if (!merge_array_spec (as, current_as, false))
5156 m = MATCH_ERROR;
5157 free (as);
5160 if (m == MATCH_NO)
5162 if (d == DECL_CODIMENSION)
5163 gfc_error ("Missing codimension specification at %C");
5164 else
5165 gfc_error ("Missing dimension specification at %C");
5166 m = MATCH_ERROR;
5169 if (m == MATCH_ERROR)
5170 goto cleanup;
5174 /* Since we've seen a double colon, we have to be looking at an
5175 attr-spec. This means that we can now issue errors. */
5176 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5177 if (seen[d] > 1)
5179 switch (d)
5181 case DECL_ALLOCATABLE:
5182 attr = "ALLOCATABLE";
5183 break;
5184 case DECL_ASYNCHRONOUS:
5185 attr = "ASYNCHRONOUS";
5186 break;
5187 case DECL_CODIMENSION:
5188 attr = "CODIMENSION";
5189 break;
5190 case DECL_CONTIGUOUS:
5191 attr = "CONTIGUOUS";
5192 break;
5193 case DECL_DIMENSION:
5194 attr = "DIMENSION";
5195 break;
5196 case DECL_EXTERNAL:
5197 attr = "EXTERNAL";
5198 break;
5199 case DECL_IN:
5200 attr = "INTENT (IN)";
5201 break;
5202 case DECL_OUT:
5203 attr = "INTENT (OUT)";
5204 break;
5205 case DECL_INOUT:
5206 attr = "INTENT (IN OUT)";
5207 break;
5208 case DECL_INTRINSIC:
5209 attr = "INTRINSIC";
5210 break;
5211 case DECL_OPTIONAL:
5212 attr = "OPTIONAL";
5213 break;
5214 case DECL_KIND:
5215 attr = "KIND";
5216 break;
5217 case DECL_LEN:
5218 attr = "LEN";
5219 break;
5220 case DECL_PARAMETER:
5221 attr = "PARAMETER";
5222 break;
5223 case DECL_POINTER:
5224 attr = "POINTER";
5225 break;
5226 case DECL_PROTECTED:
5227 attr = "PROTECTED";
5228 break;
5229 case DECL_PRIVATE:
5230 attr = "PRIVATE";
5231 break;
5232 case DECL_PUBLIC:
5233 attr = "PUBLIC";
5234 break;
5235 case DECL_SAVE:
5236 attr = "SAVE";
5237 break;
5238 case DECL_STATIC:
5239 attr = "STATIC";
5240 break;
5241 case DECL_AUTOMATIC:
5242 attr = "AUTOMATIC";
5243 break;
5244 case DECL_TARGET:
5245 attr = "TARGET";
5246 break;
5247 case DECL_IS_BIND_C:
5248 attr = "IS_BIND_C";
5249 break;
5250 case DECL_VALUE:
5251 attr = "VALUE";
5252 break;
5253 case DECL_VOLATILE:
5254 attr = "VOLATILE";
5255 break;
5256 default:
5257 attr = NULL; /* This shouldn't happen. */
5260 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5261 m = MATCH_ERROR;
5262 goto cleanup;
5265 /* Now that we've dealt with duplicate attributes, add the attributes
5266 to the current attribute. */
5267 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5269 if (seen[d] == 0)
5270 continue;
5271 else
5272 attr_seen = 1;
5274 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5275 && !flag_dec_static)
5277 gfc_error ("%s at %L is a DEC extension, enable with "
5278 "%<-fdec-static%>",
5279 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5280 m = MATCH_ERROR;
5281 goto cleanup;
5283 /* Allow SAVE with STATIC, but don't complain. */
5284 if (d == DECL_STATIC && seen[DECL_SAVE])
5285 continue;
5287 if (gfc_current_state () == COMP_DERIVED
5288 && d != DECL_DIMENSION && d != DECL_CODIMENSION
5289 && d != DECL_POINTER && d != DECL_PRIVATE
5290 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
5292 if (d == DECL_ALLOCATABLE)
5294 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
5295 "attribute at %C in a TYPE definition"))
5297 m = MATCH_ERROR;
5298 goto cleanup;
5301 else if (d == DECL_KIND)
5303 if (!gfc_notify_std (GFC_STD_F2003, "KIND "
5304 "attribute at %C in a TYPE definition"))
5306 m = MATCH_ERROR;
5307 goto cleanup;
5309 if (current_ts.type != BT_INTEGER)
5311 gfc_error ("Component with KIND attribute at %C must be "
5312 "INTEGER");
5313 m = MATCH_ERROR;
5314 goto cleanup;
5316 if (current_ts.kind != gfc_default_integer_kind)
5318 gfc_error ("Component with KIND attribute at %C must be "
5319 "default integer kind (%d)",
5320 gfc_default_integer_kind);
5321 m = MATCH_ERROR;
5322 goto cleanup;
5325 else if (d == DECL_LEN)
5327 if (!gfc_notify_std (GFC_STD_F2003, "LEN "
5328 "attribute at %C in a TYPE definition"))
5330 m = MATCH_ERROR;
5331 goto cleanup;
5333 if (current_ts.type != BT_INTEGER)
5335 gfc_error ("Component with LEN attribute at %C must be "
5336 "INTEGER");
5337 m = MATCH_ERROR;
5338 goto cleanup;
5340 if (current_ts.kind != gfc_default_integer_kind)
5342 gfc_error ("Component with LEN attribute at %C must be "
5343 "default integer kind (%d)",
5344 gfc_default_integer_kind);
5345 m = MATCH_ERROR;
5346 goto cleanup;
5349 else
5351 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
5352 &seen_at[d]);
5353 m = MATCH_ERROR;
5354 goto cleanup;
5358 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5359 && gfc_current_state () != COMP_MODULE)
5361 if (d == DECL_PRIVATE)
5362 attr = "PRIVATE";
5363 else
5364 attr = "PUBLIC";
5365 if (gfc_current_state () == COMP_DERIVED
5366 && gfc_state_stack->previous
5367 && gfc_state_stack->previous->state == COMP_MODULE)
5369 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5370 "at %L in a TYPE definition", attr,
5371 &seen_at[d]))
5373 m = MATCH_ERROR;
5374 goto cleanup;
5377 else
5379 gfc_error ("%s attribute at %L is not allowed outside of the "
5380 "specification part of a module", attr, &seen_at[d]);
5381 m = MATCH_ERROR;
5382 goto cleanup;
5386 if (gfc_current_state () != COMP_DERIVED
5387 && (d == DECL_KIND || d == DECL_LEN))
5389 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5390 "definition", &seen_at[d]);
5391 m = MATCH_ERROR;
5392 goto cleanup;
5395 switch (d)
5397 case DECL_ALLOCATABLE:
5398 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5399 break;
5401 case DECL_ASYNCHRONOUS:
5402 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5403 t = false;
5404 else
5405 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5406 break;
5408 case DECL_CODIMENSION:
5409 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5410 break;
5412 case DECL_CONTIGUOUS:
5413 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5414 t = false;
5415 else
5416 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5417 break;
5419 case DECL_DIMENSION:
5420 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5421 break;
5423 case DECL_EXTERNAL:
5424 t = gfc_add_external (&current_attr, &seen_at[d]);
5425 break;
5427 case DECL_IN:
5428 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5429 break;
5431 case DECL_OUT:
5432 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5433 break;
5435 case DECL_INOUT:
5436 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5437 break;
5439 case DECL_INTRINSIC:
5440 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5441 break;
5443 case DECL_OPTIONAL:
5444 t = gfc_add_optional (&current_attr, &seen_at[d]);
5445 break;
5447 case DECL_KIND:
5448 t = gfc_add_kind (&current_attr, &seen_at[d]);
5449 break;
5451 case DECL_LEN:
5452 t = gfc_add_len (&current_attr, &seen_at[d]);
5453 break;
5455 case DECL_PARAMETER:
5456 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5457 break;
5459 case DECL_POINTER:
5460 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5461 break;
5463 case DECL_PROTECTED:
5464 if (gfc_current_state () != COMP_MODULE
5465 || (gfc_current_ns->proc_name
5466 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5468 gfc_error ("PROTECTED at %C only allowed in specification "
5469 "part of a module");
5470 t = false;
5471 break;
5474 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5475 t = false;
5476 else
5477 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5478 break;
5480 case DECL_PRIVATE:
5481 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5482 &seen_at[d]);
5483 break;
5485 case DECL_PUBLIC:
5486 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5487 &seen_at[d]);
5488 break;
5490 case DECL_STATIC:
5491 case DECL_SAVE:
5492 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5493 break;
5495 case DECL_AUTOMATIC:
5496 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5497 break;
5499 case DECL_TARGET:
5500 t = gfc_add_target (&current_attr, &seen_at[d]);
5501 break;
5503 case DECL_IS_BIND_C:
5504 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5505 break;
5507 case DECL_VALUE:
5508 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5509 t = false;
5510 else
5511 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5512 break;
5514 case DECL_VOLATILE:
5515 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5516 t = false;
5517 else
5518 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5519 break;
5521 default:
5522 gfc_internal_error ("match_attr_spec(): Bad attribute");
5525 if (!t)
5527 m = MATCH_ERROR;
5528 goto cleanup;
5532 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
5533 if ((gfc_current_state () == COMP_MODULE
5534 || gfc_current_state () == COMP_SUBMODULE)
5535 && !current_attr.save
5536 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5537 current_attr.save = SAVE_IMPLICIT;
5539 colon_seen = 1;
5540 return MATCH_YES;
5542 cleanup:
5543 gfc_current_locus = start;
5544 gfc_free_array_spec (current_as);
5545 current_as = NULL;
5546 attr_seen = 0;
5547 return m;
5551 /* Set the binding label, dest_label, either with the binding label
5552 stored in the given gfc_typespec, ts, or if none was provided, it
5553 will be the symbol name in all lower case, as required by the draft
5554 (J3/04-007, section 15.4.1). If a binding label was given and
5555 there is more than one argument (num_idents), it is an error. */
5557 static bool
5558 set_binding_label (const char **dest_label, const char *sym_name,
5559 int num_idents)
5561 if (num_idents > 1 && has_name_equals)
5563 gfc_error ("Multiple identifiers provided with "
5564 "single NAME= specifier at %C");
5565 return false;
5568 if (curr_binding_label)
5569 /* Binding label given; store in temp holder till have sym. */
5570 *dest_label = curr_binding_label;
5571 else
5573 /* No binding label given, and the NAME= specifier did not exist,
5574 which means there was no NAME="". */
5575 if (sym_name != NULL && has_name_equals == 0)
5576 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5579 return true;
5583 /* Set the status of the given common block as being BIND(C) or not,
5584 depending on the given parameter, is_bind_c. */
5586 void
5587 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5589 com_block->is_bind_c = is_bind_c;
5590 return;
5594 /* Verify that the given gfc_typespec is for a C interoperable type. */
5596 bool
5597 gfc_verify_c_interop (gfc_typespec *ts)
5599 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5600 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5601 ? true : false;
5602 else if (ts->type == BT_CLASS)
5603 return false;
5604 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5605 return false;
5607 return true;
5611 /* Verify that the variables of a given common block, which has been
5612 defined with the attribute specifier bind(c), to be of a C
5613 interoperable type. Errors will be reported here, if
5614 encountered. */
5616 bool
5617 verify_com_block_vars_c_interop (gfc_common_head *com_block)
5619 gfc_symbol *curr_sym = NULL;
5620 bool retval = true;
5622 curr_sym = com_block->head;
5624 /* Make sure we have at least one symbol. */
5625 if (curr_sym == NULL)
5626 return retval;
5628 /* Here we know we have a symbol, so we'll execute this loop
5629 at least once. */
5632 /* The second to last param, 1, says this is in a common block. */
5633 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5634 curr_sym = curr_sym->common_next;
5635 } while (curr_sym != NULL);
5637 return retval;
5641 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5642 an appropriate error message is reported. */
5644 bool
5645 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5646 int is_in_common, gfc_common_head *com_block)
5648 bool bind_c_function = false;
5649 bool retval = true;
5651 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5652 bind_c_function = true;
5654 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5656 tmp_sym = tmp_sym->result;
5657 /* Make sure it wasn't an implicitly typed result. */
5658 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5660 gfc_warning (OPT_Wc_binding_type,
5661 "Implicitly declared BIND(C) function %qs at "
5662 "%L may not be C interoperable", tmp_sym->name,
5663 &tmp_sym->declared_at);
5664 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5665 /* Mark it as C interoperable to prevent duplicate warnings. */
5666 tmp_sym->ts.is_c_interop = 1;
5667 tmp_sym->attr.is_c_interop = 1;
5671 /* Here, we know we have the bind(c) attribute, so if we have
5672 enough type info, then verify that it's a C interop kind.
5673 The info could be in the symbol already, or possibly still in
5674 the given ts (current_ts), so look in both. */
5675 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5677 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5679 /* See if we're dealing with a sym in a common block or not. */
5680 if (is_in_common == 1 && warn_c_binding_type)
5682 gfc_warning (OPT_Wc_binding_type,
5683 "Variable %qs in common block %qs at %L "
5684 "may not be a C interoperable "
5685 "kind though common block %qs is BIND(C)",
5686 tmp_sym->name, com_block->name,
5687 &(tmp_sym->declared_at), com_block->name);
5689 else
5691 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5692 gfc_error ("Type declaration %qs at %L is not C "
5693 "interoperable but it is BIND(C)",
5694 tmp_sym->name, &(tmp_sym->declared_at));
5695 else if (warn_c_binding_type)
5696 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5697 "may not be a C interoperable "
5698 "kind but it is BIND(C)",
5699 tmp_sym->name, &(tmp_sym->declared_at));
5703 /* Variables declared w/in a common block can't be bind(c)
5704 since there's no way for C to see these variables, so there's
5705 semantically no reason for the attribute. */
5706 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5708 gfc_error ("Variable %qs in common block %qs at "
5709 "%L cannot be declared with BIND(C) "
5710 "since it is not a global",
5711 tmp_sym->name, com_block->name,
5712 &(tmp_sym->declared_at));
5713 retval = false;
5716 /* Scalar variables that are bind(c) cannot have the pointer
5717 or allocatable attributes. */
5718 if (tmp_sym->attr.is_bind_c == 1)
5720 if (tmp_sym->attr.pointer == 1)
5722 gfc_error ("Variable %qs at %L cannot have both the "
5723 "POINTER and BIND(C) attributes",
5724 tmp_sym->name, &(tmp_sym->declared_at));
5725 retval = false;
5728 if (tmp_sym->attr.allocatable == 1)
5730 gfc_error ("Variable %qs at %L cannot have both the "
5731 "ALLOCATABLE and BIND(C) attributes",
5732 tmp_sym->name, &(tmp_sym->declared_at));
5733 retval = false;
5738 /* If it is a BIND(C) function, make sure the return value is a
5739 scalar value. The previous tests in this function made sure
5740 the type is interoperable. */
5741 if (bind_c_function && tmp_sym->as != NULL)
5742 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5743 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5745 /* BIND(C) functions cannot return a character string. */
5746 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5747 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5748 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5749 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5750 gfc_error ("Return type of BIND(C) function %qs of character "
5751 "type at %L must have length 1", tmp_sym->name,
5752 &(tmp_sym->declared_at));
5755 /* See if the symbol has been marked as private. If it has, make sure
5756 there is no binding label and warn the user if there is one. */
5757 if (tmp_sym->attr.access == ACCESS_PRIVATE
5758 && tmp_sym->binding_label)
5759 /* Use gfc_warning_now because we won't say that the symbol fails
5760 just because of this. */
5761 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5762 "given the binding label %qs", tmp_sym->name,
5763 &(tmp_sym->declared_at), tmp_sym->binding_label);
5765 return retval;
5769 /* Set the appropriate fields for a symbol that's been declared as
5770 BIND(C) (the is_bind_c flag and the binding label), and verify that
5771 the type is C interoperable. Errors are reported by the functions
5772 used to set/test these fields. */
5774 bool
5775 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5777 bool retval = true;
5779 /* TODO: Do we need to make sure the vars aren't marked private? */
5781 /* Set the is_bind_c bit in symbol_attribute. */
5782 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5784 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5785 return false;
5787 return retval;
5791 /* Set the fields marking the given common block as BIND(C), including
5792 a binding label, and report any errors encountered. */
5794 bool
5795 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5797 bool retval = true;
5799 /* destLabel, common name, typespec (which may have binding label). */
5800 if (!set_binding_label (&com_block->binding_label, com_block->name,
5801 num_idents))
5802 return false;
5804 /* Set the given common block (com_block) to being bind(c) (1). */
5805 set_com_block_bind_c (com_block, 1);
5807 return retval;
5811 /* Retrieve the list of one or more identifiers that the given bind(c)
5812 attribute applies to. */
5814 bool
5815 get_bind_c_idents (void)
5817 char name[GFC_MAX_SYMBOL_LEN + 1];
5818 int num_idents = 0;
5819 gfc_symbol *tmp_sym = NULL;
5820 match found_id;
5821 gfc_common_head *com_block = NULL;
5823 if (gfc_match_name (name) == MATCH_YES)
5825 found_id = MATCH_YES;
5826 gfc_get_ha_symbol (name, &tmp_sym);
5828 else if (match_common_name (name) == MATCH_YES)
5830 found_id = MATCH_YES;
5831 com_block = gfc_get_common (name, 0);
5833 else
5835 gfc_error ("Need either entity or common block name for "
5836 "attribute specification statement at %C");
5837 return false;
5840 /* Save the current identifier and look for more. */
5843 /* Increment the number of identifiers found for this spec stmt. */
5844 num_idents++;
5846 /* Make sure we have a sym or com block, and verify that it can
5847 be bind(c). Set the appropriate field(s) and look for more
5848 identifiers. */
5849 if (tmp_sym != NULL || com_block != NULL)
5851 if (tmp_sym != NULL)
5853 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
5854 return false;
5856 else
5858 if (!set_verify_bind_c_com_block (com_block, num_idents))
5859 return false;
5862 /* Look to see if we have another identifier. */
5863 tmp_sym = NULL;
5864 if (gfc_match_eos () == MATCH_YES)
5865 found_id = MATCH_NO;
5866 else if (gfc_match_char (',') != MATCH_YES)
5867 found_id = MATCH_NO;
5868 else if (gfc_match_name (name) == MATCH_YES)
5870 found_id = MATCH_YES;
5871 gfc_get_ha_symbol (name, &tmp_sym);
5873 else if (match_common_name (name) == MATCH_YES)
5875 found_id = MATCH_YES;
5876 com_block = gfc_get_common (name, 0);
5878 else
5880 gfc_error ("Missing entity or common block name for "
5881 "attribute specification statement at %C");
5882 return false;
5885 else
5887 gfc_internal_error ("Missing symbol");
5889 } while (found_id == MATCH_YES);
5891 /* if we get here we were successful */
5892 return true;
5896 /* Try and match a BIND(C) attribute specification statement. */
5898 match
5899 gfc_match_bind_c_stmt (void)
5901 match found_match = MATCH_NO;
5902 gfc_typespec *ts;
5904 ts = &current_ts;
5906 /* This may not be necessary. */
5907 gfc_clear_ts (ts);
5908 /* Clear the temporary binding label holder. */
5909 curr_binding_label = NULL;
5911 /* Look for the bind(c). */
5912 found_match = gfc_match_bind_c (NULL, true);
5914 if (found_match == MATCH_YES)
5916 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
5917 return MATCH_ERROR;
5919 /* Look for the :: now, but it is not required. */
5920 gfc_match (" :: ");
5922 /* Get the identifier(s) that needs to be updated. This may need to
5923 change to hand the flag(s) for the attr specified so all identifiers
5924 found can have all appropriate parts updated (assuming that the same
5925 spec stmt can have multiple attrs, such as both bind(c) and
5926 allocatable...). */
5927 if (!get_bind_c_idents ())
5928 /* Error message should have printed already. */
5929 return MATCH_ERROR;
5932 return found_match;
5936 /* Match a data declaration statement. */
5938 match
5939 gfc_match_data_decl (void)
5941 gfc_symbol *sym;
5942 match m;
5943 int elem;
5945 type_param_spec_list = NULL;
5946 decl_type_param_list = NULL;
5948 num_idents_on_line = 0;
5950 m = gfc_match_decl_type_spec (&current_ts, 0);
5951 if (m != MATCH_YES)
5952 return m;
5954 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5955 && !gfc_comp_struct (gfc_current_state ()))
5957 sym = gfc_use_derived (current_ts.u.derived);
5959 if (sym == NULL)
5961 m = MATCH_ERROR;
5962 goto cleanup;
5965 current_ts.u.derived = sym;
5968 m = match_attr_spec ();
5969 if (m == MATCH_ERROR)
5971 m = MATCH_NO;
5972 goto cleanup;
5975 if (current_ts.type == BT_CLASS
5976 && current_ts.u.derived->attr.unlimited_polymorphic)
5977 goto ok;
5979 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
5980 && current_ts.u.derived->components == NULL
5981 && !current_ts.u.derived->attr.zero_comp)
5984 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
5985 goto ok;
5987 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
5988 goto ok;
5990 gfc_find_symbol (current_ts.u.derived->name,
5991 current_ts.u.derived->ns, 1, &sym);
5993 /* Any symbol that we find had better be a type definition
5994 which has its components defined, or be a structure definition
5995 actively being parsed. */
5996 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
5997 && (current_ts.u.derived->components != NULL
5998 || current_ts.u.derived->attr.zero_comp
5999 || current_ts.u.derived == gfc_new_block))
6000 goto ok;
6002 gfc_error ("Derived type at %C has not been previously defined "
6003 "and so cannot appear in a derived type definition");
6004 m = MATCH_ERROR;
6005 goto cleanup;
6009 /* If we have an old-style character declaration, and no new-style
6010 attribute specifications, then there a comma is optional between
6011 the type specification and the variable list. */
6012 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
6013 gfc_match_char (',');
6015 /* Give the types/attributes to symbols that follow. Give the element
6016 a number so that repeat character length expressions can be copied. */
6017 elem = 1;
6018 for (;;)
6020 num_idents_on_line++;
6021 m = variable_decl (elem++);
6022 if (m == MATCH_ERROR)
6023 goto cleanup;
6024 if (m == MATCH_NO)
6025 break;
6027 if (gfc_match_eos () == MATCH_YES)
6028 goto cleanup;
6029 if (gfc_match_char (',') != MATCH_YES)
6030 break;
6033 if (!gfc_error_flag_test ())
6035 /* An anonymous structure declaration is unambiguous; if we matched one
6036 according to gfc_match_structure_decl, we need to return MATCH_YES
6037 here to avoid confusing the remaining matchers, even if there was an
6038 error during variable_decl. We must flush any such errors. Note this
6039 causes the parser to gracefully continue parsing the remaining input
6040 as a structure body, which likely follows. */
6041 if (current_ts.type == BT_DERIVED && current_ts.u.derived
6042 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6044 gfc_error_now ("Syntax error in anonymous structure declaration"
6045 " at %C");
6046 /* Skip the bad variable_decl and line up for the start of the
6047 structure body. */
6048 gfc_error_recovery ();
6049 m = MATCH_YES;
6050 goto cleanup;
6053 gfc_error ("Syntax error in data declaration at %C");
6056 m = MATCH_ERROR;
6058 gfc_free_data_all (gfc_current_ns);
6060 cleanup:
6061 if (saved_kind_expr)
6062 gfc_free_expr (saved_kind_expr);
6063 if (type_param_spec_list)
6064 gfc_free_actual_arglist (type_param_spec_list);
6065 if (decl_type_param_list)
6066 gfc_free_actual_arglist (decl_type_param_list);
6067 saved_kind_expr = NULL;
6068 gfc_free_array_spec (current_as);
6069 current_as = NULL;
6070 return m;
6074 /* Match a prefix associated with a function or subroutine
6075 declaration. If the typespec pointer is nonnull, then a typespec
6076 can be matched. Note that if nothing matches, MATCH_YES is
6077 returned (the null string was matched). */
6079 match
6080 gfc_match_prefix (gfc_typespec *ts)
6082 bool seen_type;
6083 bool seen_impure;
6084 bool found_prefix;
6086 gfc_clear_attr (&current_attr);
6087 seen_type = false;
6088 seen_impure = false;
6090 gcc_assert (!gfc_matching_prefix);
6091 gfc_matching_prefix = true;
6095 found_prefix = false;
6097 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6098 corresponding attribute seems natural and distinguishes these
6099 procedures from procedure types of PROC_MODULE, which these are
6100 as well. */
6101 if (gfc_match ("module% ") == MATCH_YES)
6103 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6104 goto error;
6106 current_attr.module_procedure = 1;
6107 found_prefix = true;
6110 if (!seen_type && ts != NULL
6111 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
6112 && gfc_match_space () == MATCH_YES)
6115 seen_type = true;
6116 found_prefix = true;
6119 if (gfc_match ("elemental% ") == MATCH_YES)
6121 if (!gfc_add_elemental (&current_attr, NULL))
6122 goto error;
6124 found_prefix = true;
6127 if (gfc_match ("pure% ") == MATCH_YES)
6129 if (!gfc_add_pure (&current_attr, NULL))
6130 goto error;
6132 found_prefix = true;
6135 if (gfc_match ("recursive% ") == MATCH_YES)
6137 if (!gfc_add_recursive (&current_attr, NULL))
6138 goto error;
6140 found_prefix = true;
6143 /* IMPURE is a somewhat special case, as it needs not set an actual
6144 attribute but rather only prevents ELEMENTAL routines from being
6145 automatically PURE. */
6146 if (gfc_match ("impure% ") == MATCH_YES)
6148 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
6149 goto error;
6151 seen_impure = true;
6152 found_prefix = true;
6155 while (found_prefix);
6157 /* IMPURE and PURE must not both appear, of course. */
6158 if (seen_impure && current_attr.pure)
6160 gfc_error ("PURE and IMPURE must not appear both at %C");
6161 goto error;
6164 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6165 if (!seen_impure && current_attr.elemental && !current_attr.pure)
6167 if (!gfc_add_pure (&current_attr, NULL))
6168 goto error;
6171 /* At this point, the next item is not a prefix. */
6172 gcc_assert (gfc_matching_prefix);
6174 gfc_matching_prefix = false;
6175 return MATCH_YES;
6177 error:
6178 gcc_assert (gfc_matching_prefix);
6179 gfc_matching_prefix = false;
6180 return MATCH_ERROR;
6184 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6186 static bool
6187 copy_prefix (symbol_attribute *dest, locus *where)
6189 if (dest->module_procedure)
6191 if (current_attr.elemental)
6192 dest->elemental = 1;
6194 if (current_attr.pure)
6195 dest->pure = 1;
6197 if (current_attr.recursive)
6198 dest->recursive = 1;
6200 /* Module procedures are unusual in that the 'dest' is copied from
6201 the interface declaration. However, this is an oportunity to
6202 check that the submodule declaration is compliant with the
6203 interface. */
6204 if (dest->elemental && !current_attr.elemental)
6206 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6207 "missing at %L", where);
6208 return false;
6211 if (dest->pure && !current_attr.pure)
6213 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6214 "missing at %L", where);
6215 return false;
6218 if (dest->recursive && !current_attr.recursive)
6220 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6221 "missing at %L", where);
6222 return false;
6225 return true;
6228 if (current_attr.elemental && !gfc_add_elemental (dest, where))
6229 return false;
6231 if (current_attr.pure && !gfc_add_pure (dest, where))
6232 return false;
6234 if (current_attr.recursive && !gfc_add_recursive (dest, where))
6235 return false;
6237 return true;
6241 /* Match a formal argument list or, if typeparam is true, a
6242 type_param_name_list. */
6244 match
6245 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6246 int null_flag, bool typeparam)
6248 gfc_formal_arglist *head, *tail, *p, *q;
6249 char name[GFC_MAX_SYMBOL_LEN + 1];
6250 gfc_symbol *sym;
6251 match m;
6252 gfc_formal_arglist *formal = NULL;
6254 head = tail = NULL;
6256 /* Keep the interface formal argument list and null it so that the
6257 matching for the new declaration can be done. The numbers and
6258 names of the arguments are checked here. The interface formal
6259 arguments are retained in formal_arglist and the characteristics
6260 are compared in resolve.c(resolve_fl_procedure). See the remark
6261 in get_proc_name about the eventual need to copy the formal_arglist
6262 and populate the formal namespace of the interface symbol. */
6263 if (progname->attr.module_procedure
6264 && progname->attr.host_assoc)
6266 formal = progname->formal;
6267 progname->formal = NULL;
6270 if (gfc_match_char ('(') != MATCH_YES)
6272 if (null_flag)
6273 goto ok;
6274 return MATCH_NO;
6277 if (gfc_match_char (')') == MATCH_YES)
6278 goto ok;
6280 for (;;)
6282 if (gfc_match_char ('*') == MATCH_YES)
6284 sym = NULL;
6285 if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6286 "Alternate-return argument at %C"))
6288 m = MATCH_ERROR;
6289 goto cleanup;
6291 else if (typeparam)
6292 gfc_error_now ("A parameter name is required at %C");
6294 else
6296 m = gfc_match_name (name);
6297 if (m != MATCH_YES)
6299 if(typeparam)
6300 gfc_error_now ("A parameter name is required at %C");
6301 goto cleanup;
6304 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6305 goto cleanup;
6306 else if (typeparam
6307 && gfc_get_symbol (name, progname->f2k_derived, &sym))
6308 goto cleanup;
6311 p = gfc_get_formal_arglist ();
6313 if (head == NULL)
6314 head = tail = p;
6315 else
6317 tail->next = p;
6318 tail = p;
6321 tail->sym = sym;
6323 /* We don't add the VARIABLE flavor because the name could be a
6324 dummy procedure. We don't apply these attributes to formal
6325 arguments of statement functions. */
6326 if (sym != NULL && !st_flag
6327 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6328 || !gfc_missing_attr (&sym->attr, NULL)))
6330 m = MATCH_ERROR;
6331 goto cleanup;
6334 /* The name of a program unit can be in a different namespace,
6335 so check for it explicitly. After the statement is accepted,
6336 the name is checked for especially in gfc_get_symbol(). */
6337 if (gfc_new_block != NULL && sym != NULL && !typeparam
6338 && strcmp (sym->name, gfc_new_block->name) == 0)
6340 gfc_error ("Name %qs at %C is the name of the procedure",
6341 sym->name);
6342 m = MATCH_ERROR;
6343 goto cleanup;
6346 if (gfc_match_char (')') == MATCH_YES)
6347 goto ok;
6349 m = gfc_match_char (',');
6350 if (m != MATCH_YES)
6352 if (typeparam)
6353 gfc_error_now ("Expected parameter list in type declaration "
6354 "at %C");
6355 else
6356 gfc_error ("Unexpected junk in formal argument list at %C");
6357 goto cleanup;
6362 /* Check for duplicate symbols in the formal argument list. */
6363 if (head != NULL)
6365 for (p = head; p->next; p = p->next)
6367 if (p->sym == NULL)
6368 continue;
6370 for (q = p->next; q; q = q->next)
6371 if (p->sym == q->sym)
6373 if (typeparam)
6374 gfc_error_now ("Duplicate name %qs in parameter "
6375 "list at %C", p->sym->name);
6376 else
6377 gfc_error ("Duplicate symbol %qs in formal argument "
6378 "list at %C", p->sym->name);
6380 m = MATCH_ERROR;
6381 goto cleanup;
6386 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6388 m = MATCH_ERROR;
6389 goto cleanup;
6392 /* gfc_error_now used in following and return with MATCH_YES because
6393 doing otherwise results in a cascade of extraneous errors and in
6394 some cases an ICE in symbol.c(gfc_release_symbol). */
6395 if (progname->attr.module_procedure && progname->attr.host_assoc)
6397 bool arg_count_mismatch = false;
6399 if (!formal && head)
6400 arg_count_mismatch = true;
6402 /* Abbreviated module procedure declaration is not meant to have any
6403 formal arguments! */
6404 if (!progname->abr_modproc_decl && formal && !head)
6405 arg_count_mismatch = true;
6407 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6409 if ((p->next != NULL && q->next == NULL)
6410 || (p->next == NULL && q->next != NULL))
6411 arg_count_mismatch = true;
6412 else if ((p->sym == NULL && q->sym == NULL)
6413 || strcmp (p->sym->name, q->sym->name) == 0)
6414 continue;
6415 else
6416 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6417 "argument names (%s/%s) at %C",
6418 p->sym->name, q->sym->name);
6421 if (arg_count_mismatch)
6422 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6423 "formal arguments at %C");
6426 return MATCH_YES;
6428 cleanup:
6429 gfc_free_formal_arglist (head);
6430 return m;
6434 /* Match a RESULT specification following a function declaration or
6435 ENTRY statement. Also matches the end-of-statement. */
6437 static match
6438 match_result (gfc_symbol *function, gfc_symbol **result)
6440 char name[GFC_MAX_SYMBOL_LEN + 1];
6441 gfc_symbol *r;
6442 match m;
6444 if (gfc_match (" result (") != MATCH_YES)
6445 return MATCH_NO;
6447 m = gfc_match_name (name);
6448 if (m != MATCH_YES)
6449 return m;
6451 /* Get the right paren, and that's it because there could be the
6452 bind(c) attribute after the result clause. */
6453 if (gfc_match_char (')') != MATCH_YES)
6455 /* TODO: should report the missing right paren here. */
6456 return MATCH_ERROR;
6459 if (strcmp (function->name, name) == 0)
6461 gfc_error ("RESULT variable at %C must be different than function name");
6462 return MATCH_ERROR;
6465 if (gfc_get_symbol (name, NULL, &r))
6466 return MATCH_ERROR;
6468 if (!gfc_add_result (&r->attr, r->name, NULL))
6469 return MATCH_ERROR;
6471 *result = r;
6473 return MATCH_YES;
6477 /* Match a function suffix, which could be a combination of a result
6478 clause and BIND(C), either one, or neither. The draft does not
6479 require them to come in a specific order. */
6481 match
6482 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6484 match is_bind_c; /* Found bind(c). */
6485 match is_result; /* Found result clause. */
6486 match found_match; /* Status of whether we've found a good match. */
6487 char peek_char; /* Character we're going to peek at. */
6488 bool allow_binding_name;
6490 /* Initialize to having found nothing. */
6491 found_match = MATCH_NO;
6492 is_bind_c = MATCH_NO;
6493 is_result = MATCH_NO;
6495 /* Get the next char to narrow between result and bind(c). */
6496 gfc_gobble_whitespace ();
6497 peek_char = gfc_peek_ascii_char ();
6499 /* C binding names are not allowed for internal procedures. */
6500 if (gfc_current_state () == COMP_CONTAINS
6501 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6502 allow_binding_name = false;
6503 else
6504 allow_binding_name = true;
6506 switch (peek_char)
6508 case 'r':
6509 /* Look for result clause. */
6510 is_result = match_result (sym, result);
6511 if (is_result == MATCH_YES)
6513 /* Now see if there is a bind(c) after it. */
6514 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6515 /* We've found the result clause and possibly bind(c). */
6516 found_match = MATCH_YES;
6518 else
6519 /* This should only be MATCH_ERROR. */
6520 found_match = is_result;
6521 break;
6522 case 'b':
6523 /* Look for bind(c) first. */
6524 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6525 if (is_bind_c == MATCH_YES)
6527 /* Now see if a result clause followed it. */
6528 is_result = match_result (sym, result);
6529 found_match = MATCH_YES;
6531 else
6533 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6534 found_match = MATCH_ERROR;
6536 break;
6537 default:
6538 gfc_error ("Unexpected junk after function declaration at %C");
6539 found_match = MATCH_ERROR;
6540 break;
6543 if (is_bind_c == MATCH_YES)
6545 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
6546 if (gfc_current_state () == COMP_CONTAINS
6547 && sym->ns->proc_name->attr.flavor != FL_MODULE
6548 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6549 "at %L may not be specified for an internal "
6550 "procedure", &gfc_current_locus))
6551 return MATCH_ERROR;
6553 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6554 return MATCH_ERROR;
6557 return found_match;
6561 /* Procedure pointer return value without RESULT statement:
6562 Add "hidden" result variable named "ppr@". */
6564 static bool
6565 add_hidden_procptr_result (gfc_symbol *sym)
6567 bool case1,case2;
6569 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6570 return false;
6572 /* First usage case: PROCEDURE and EXTERNAL statements. */
6573 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6574 && strcmp (gfc_current_block ()->name, sym->name) == 0
6575 && sym->attr.external;
6576 /* Second usage case: INTERFACE statements. */
6577 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6578 && gfc_state_stack->previous->state == COMP_FUNCTION
6579 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6581 if (case1 || case2)
6583 gfc_symtree *stree;
6584 if (case1)
6585 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6586 else
6588 gfc_symtree *st2;
6589 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6590 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6591 st2->n.sym = stree->n.sym;
6592 stree->n.sym->refs++;
6594 sym->result = stree->n.sym;
6596 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6597 sym->result->attr.pointer = sym->attr.pointer;
6598 sym->result->attr.external = sym->attr.external;
6599 sym->result->attr.referenced = sym->attr.referenced;
6600 sym->result->ts = sym->ts;
6601 sym->attr.proc_pointer = 0;
6602 sym->attr.pointer = 0;
6603 sym->attr.external = 0;
6604 if (sym->result->attr.external && sym->result->attr.pointer)
6606 sym->result->attr.pointer = 0;
6607 sym->result->attr.proc_pointer = 1;
6610 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6612 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6613 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6614 && sym->result && sym->result != sym && sym->result->attr.external
6615 && sym == gfc_current_ns->proc_name
6616 && sym == sym->result->ns->proc_name
6617 && strcmp ("ppr@", sym->result->name) == 0)
6619 sym->result->attr.proc_pointer = 1;
6620 sym->attr.pointer = 0;
6621 return true;
6623 else
6624 return false;
6628 /* Match the interface for a PROCEDURE declaration,
6629 including brackets (R1212). */
6631 static match
6632 match_procedure_interface (gfc_symbol **proc_if)
6634 match m;
6635 gfc_symtree *st;
6636 locus old_loc, entry_loc;
6637 gfc_namespace *old_ns = gfc_current_ns;
6638 char name[GFC_MAX_SYMBOL_LEN + 1];
6640 old_loc = entry_loc = gfc_current_locus;
6641 gfc_clear_ts (&current_ts);
6643 if (gfc_match (" (") != MATCH_YES)
6645 gfc_current_locus = entry_loc;
6646 return MATCH_NO;
6649 /* Get the type spec. for the procedure interface. */
6650 old_loc = gfc_current_locus;
6651 m = gfc_match_decl_type_spec (&current_ts, 0);
6652 gfc_gobble_whitespace ();
6653 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6654 goto got_ts;
6656 if (m == MATCH_ERROR)
6657 return m;
6659 /* Procedure interface is itself a procedure. */
6660 gfc_current_locus = old_loc;
6661 m = gfc_match_name (name);
6663 /* First look to see if it is already accessible in the current
6664 namespace because it is use associated or contained. */
6665 st = NULL;
6666 if (gfc_find_sym_tree (name, NULL, 0, &st))
6667 return MATCH_ERROR;
6669 /* If it is still not found, then try the parent namespace, if it
6670 exists and create the symbol there if it is still not found. */
6671 if (gfc_current_ns->parent)
6672 gfc_current_ns = gfc_current_ns->parent;
6673 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6674 return MATCH_ERROR;
6676 gfc_current_ns = old_ns;
6677 *proc_if = st->n.sym;
6679 if (*proc_if)
6681 (*proc_if)->refs++;
6682 /* Resolve interface if possible. That way, attr.procedure is only set
6683 if it is declared by a later procedure-declaration-stmt, which is
6684 invalid per F08:C1216 (cf. resolve_procedure_interface). */
6685 while ((*proc_if)->ts.interface
6686 && *proc_if != (*proc_if)->ts.interface)
6687 *proc_if = (*proc_if)->ts.interface;
6689 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6690 && (*proc_if)->ts.type == BT_UNKNOWN
6691 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6692 (*proc_if)->name, NULL))
6693 return MATCH_ERROR;
6696 got_ts:
6697 if (gfc_match (" )") != MATCH_YES)
6699 gfc_current_locus = entry_loc;
6700 return MATCH_NO;
6703 return MATCH_YES;
6707 /* Match a PROCEDURE declaration (R1211). */
6709 static match
6710 match_procedure_decl (void)
6712 match m;
6713 gfc_symbol *sym, *proc_if = NULL;
6714 int num;
6715 gfc_expr *initializer = NULL;
6717 /* Parse interface (with brackets). */
6718 m = match_procedure_interface (&proc_if);
6719 if (m != MATCH_YES)
6720 return m;
6722 /* Parse attributes (with colons). */
6723 m = match_attr_spec();
6724 if (m == MATCH_ERROR)
6725 return MATCH_ERROR;
6727 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6729 current_attr.is_bind_c = 1;
6730 has_name_equals = 0;
6731 curr_binding_label = NULL;
6734 /* Get procedure symbols. */
6735 for(num=1;;num++)
6737 m = gfc_match_symbol (&sym, 0);
6738 if (m == MATCH_NO)
6739 goto syntax;
6740 else if (m == MATCH_ERROR)
6741 return m;
6743 /* Add current_attr to the symbol attributes. */
6744 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
6745 return MATCH_ERROR;
6747 if (sym->attr.is_bind_c)
6749 /* Check for C1218. */
6750 if (!proc_if || !proc_if->attr.is_bind_c)
6752 gfc_error ("BIND(C) attribute at %C requires "
6753 "an interface with BIND(C)");
6754 return MATCH_ERROR;
6756 /* Check for C1217. */
6757 if (has_name_equals && sym->attr.pointer)
6759 gfc_error ("BIND(C) procedure with NAME may not have "
6760 "POINTER attribute at %C");
6761 return MATCH_ERROR;
6763 if (has_name_equals && sym->attr.dummy)
6765 gfc_error ("Dummy procedure at %C may not have "
6766 "BIND(C) attribute with NAME");
6767 return MATCH_ERROR;
6769 /* Set binding label for BIND(C). */
6770 if (!set_binding_label (&sym->binding_label, sym->name, num))
6771 return MATCH_ERROR;
6774 if (!gfc_add_external (&sym->attr, NULL))
6775 return MATCH_ERROR;
6777 if (add_hidden_procptr_result (sym))
6778 sym = sym->result;
6780 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
6781 return MATCH_ERROR;
6783 /* Set interface. */
6784 if (proc_if != NULL)
6786 if (sym->ts.type != BT_UNKNOWN)
6788 gfc_error ("Procedure %qs at %L already has basic type of %s",
6789 sym->name, &gfc_current_locus,
6790 gfc_basic_typename (sym->ts.type));
6791 return MATCH_ERROR;
6793 sym->ts.interface = proc_if;
6794 sym->attr.untyped = 1;
6795 sym->attr.if_source = IFSRC_IFBODY;
6797 else if (current_ts.type != BT_UNKNOWN)
6799 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
6800 return MATCH_ERROR;
6801 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6802 sym->ts.interface->ts = current_ts;
6803 sym->ts.interface->attr.flavor = FL_PROCEDURE;
6804 sym->ts.interface->attr.function = 1;
6805 sym->attr.function = 1;
6806 sym->attr.if_source = IFSRC_UNKNOWN;
6809 if (gfc_match (" =>") == MATCH_YES)
6811 if (!current_attr.pointer)
6813 gfc_error ("Initialization at %C isn't for a pointer variable");
6814 m = MATCH_ERROR;
6815 goto cleanup;
6818 m = match_pointer_init (&initializer, 1);
6819 if (m != MATCH_YES)
6820 goto cleanup;
6822 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
6823 goto cleanup;
6827 if (gfc_match_eos () == MATCH_YES)
6828 return MATCH_YES;
6829 if (gfc_match_char (',') != MATCH_YES)
6830 goto syntax;
6833 syntax:
6834 gfc_error ("Syntax error in PROCEDURE statement at %C");
6835 return MATCH_ERROR;
6837 cleanup:
6838 /* Free stuff up and return. */
6839 gfc_free_expr (initializer);
6840 return m;
6844 static match
6845 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
6848 /* Match a procedure pointer component declaration (R445). */
6850 static match
6851 match_ppc_decl (void)
6853 match m;
6854 gfc_symbol *proc_if = NULL;
6855 gfc_typespec ts;
6856 int num;
6857 gfc_component *c;
6858 gfc_expr *initializer = NULL;
6859 gfc_typebound_proc* tb;
6860 char name[GFC_MAX_SYMBOL_LEN + 1];
6862 /* Parse interface (with brackets). */
6863 m = match_procedure_interface (&proc_if);
6864 if (m != MATCH_YES)
6865 goto syntax;
6867 /* Parse attributes. */
6868 tb = XCNEW (gfc_typebound_proc);
6869 tb->where = gfc_current_locus;
6870 m = match_binding_attributes (tb, false, true);
6871 if (m == MATCH_ERROR)
6872 return m;
6874 gfc_clear_attr (&current_attr);
6875 current_attr.procedure = 1;
6876 current_attr.proc_pointer = 1;
6877 current_attr.access = tb->access;
6878 current_attr.flavor = FL_PROCEDURE;
6880 /* Match the colons (required). */
6881 if (gfc_match (" ::") != MATCH_YES)
6883 gfc_error ("Expected %<::%> after binding-attributes at %C");
6884 return MATCH_ERROR;
6887 /* Check for C450. */
6888 if (!tb->nopass && proc_if == NULL)
6890 gfc_error("NOPASS or explicit interface required at %C");
6891 return MATCH_ERROR;
6894 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
6895 return MATCH_ERROR;
6897 /* Match PPC names. */
6898 ts = current_ts;
6899 for(num=1;;num++)
6901 m = gfc_match_name (name);
6902 if (m == MATCH_NO)
6903 goto syntax;
6904 else if (m == MATCH_ERROR)
6905 return m;
6907 if (!gfc_add_component (gfc_current_block(), name, &c))
6908 return MATCH_ERROR;
6910 /* Add current_attr to the symbol attributes. */
6911 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
6912 return MATCH_ERROR;
6914 if (!gfc_add_external (&c->attr, NULL))
6915 return MATCH_ERROR;
6917 if (!gfc_add_proc (&c->attr, name, NULL))
6918 return MATCH_ERROR;
6920 if (num == 1)
6921 c->tb = tb;
6922 else
6924 c->tb = XCNEW (gfc_typebound_proc);
6925 c->tb->where = gfc_current_locus;
6926 *c->tb = *tb;
6929 /* Set interface. */
6930 if (proc_if != NULL)
6932 c->ts.interface = proc_if;
6933 c->attr.untyped = 1;
6934 c->attr.if_source = IFSRC_IFBODY;
6936 else if (ts.type != BT_UNKNOWN)
6938 c->ts = ts;
6939 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6940 c->ts.interface->result = c->ts.interface;
6941 c->ts.interface->ts = ts;
6942 c->ts.interface->attr.flavor = FL_PROCEDURE;
6943 c->ts.interface->attr.function = 1;
6944 c->attr.function = 1;
6945 c->attr.if_source = IFSRC_UNKNOWN;
6948 if (gfc_match (" =>") == MATCH_YES)
6950 m = match_pointer_init (&initializer, 1);
6951 if (m != MATCH_YES)
6953 gfc_free_expr (initializer);
6954 return m;
6956 c->initializer = initializer;
6959 if (gfc_match_eos () == MATCH_YES)
6960 return MATCH_YES;
6961 if (gfc_match_char (',') != MATCH_YES)
6962 goto syntax;
6965 syntax:
6966 gfc_error ("Syntax error in procedure pointer component at %C");
6967 return MATCH_ERROR;
6971 /* Match a PROCEDURE declaration inside an interface (R1206). */
6973 static match
6974 match_procedure_in_interface (void)
6976 match m;
6977 gfc_symbol *sym;
6978 char name[GFC_MAX_SYMBOL_LEN + 1];
6979 locus old_locus;
6981 if (current_interface.type == INTERFACE_NAMELESS
6982 || current_interface.type == INTERFACE_ABSTRACT)
6984 gfc_error ("PROCEDURE at %C must be in a generic interface");
6985 return MATCH_ERROR;
6988 /* Check if the F2008 optional double colon appears. */
6989 gfc_gobble_whitespace ();
6990 old_locus = gfc_current_locus;
6991 if (gfc_match ("::") == MATCH_YES)
6993 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
6994 "MODULE PROCEDURE statement at %L", &old_locus))
6995 return MATCH_ERROR;
6997 else
6998 gfc_current_locus = old_locus;
7000 for(;;)
7002 m = gfc_match_name (name);
7003 if (m == MATCH_NO)
7004 goto syntax;
7005 else if (m == MATCH_ERROR)
7006 return m;
7007 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
7008 return MATCH_ERROR;
7010 if (!gfc_add_interface (sym))
7011 return MATCH_ERROR;
7013 if (gfc_match_eos () == MATCH_YES)
7014 break;
7015 if (gfc_match_char (',') != MATCH_YES)
7016 goto syntax;
7019 return MATCH_YES;
7021 syntax:
7022 gfc_error ("Syntax error in PROCEDURE statement at %C");
7023 return MATCH_ERROR;
7027 /* General matcher for PROCEDURE declarations. */
7029 static match match_procedure_in_type (void);
7031 match
7032 gfc_match_procedure (void)
7034 match m;
7036 switch (gfc_current_state ())
7038 case COMP_NONE:
7039 case COMP_PROGRAM:
7040 case COMP_MODULE:
7041 case COMP_SUBMODULE:
7042 case COMP_SUBROUTINE:
7043 case COMP_FUNCTION:
7044 case COMP_BLOCK:
7045 m = match_procedure_decl ();
7046 break;
7047 case COMP_INTERFACE:
7048 m = match_procedure_in_interface ();
7049 break;
7050 case COMP_DERIVED:
7051 m = match_ppc_decl ();
7052 break;
7053 case COMP_DERIVED_CONTAINS:
7054 m = match_procedure_in_type ();
7055 break;
7056 default:
7057 return MATCH_NO;
7060 if (m != MATCH_YES)
7061 return m;
7063 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
7064 return MATCH_ERROR;
7066 return m;
7070 /* Warn if a matched procedure has the same name as an intrinsic; this is
7071 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7072 parser-state-stack to find out whether we're in a module. */
7074 static void
7075 do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
7077 bool in_module;
7079 in_module = (gfc_state_stack->previous
7080 && (gfc_state_stack->previous->state == COMP_MODULE
7081 || gfc_state_stack->previous->state == COMP_SUBMODULE));
7083 gfc_warn_intrinsic_shadow (sym, in_module, func);
7087 /* Match a function declaration. */
7089 match
7090 gfc_match_function_decl (void)
7092 char name[GFC_MAX_SYMBOL_LEN + 1];
7093 gfc_symbol *sym, *result;
7094 locus old_loc;
7095 match m;
7096 match suffix_match;
7097 match found_match; /* Status returned by match func. */
7099 if (gfc_current_state () != COMP_NONE
7100 && gfc_current_state () != COMP_INTERFACE
7101 && gfc_current_state () != COMP_CONTAINS)
7102 return MATCH_NO;
7104 gfc_clear_ts (&current_ts);
7106 old_loc = gfc_current_locus;
7108 m = gfc_match_prefix (&current_ts);
7109 if (m != MATCH_YES)
7111 gfc_current_locus = old_loc;
7112 return m;
7115 if (gfc_match ("function% %n", name) != MATCH_YES)
7117 gfc_current_locus = old_loc;
7118 return MATCH_NO;
7121 if (get_proc_name (name, &sym, false))
7122 return MATCH_ERROR;
7124 if (add_hidden_procptr_result (sym))
7125 sym = sym->result;
7127 if (current_attr.module_procedure)
7128 sym->attr.module_procedure = 1;
7130 gfc_new_block = sym;
7132 m = gfc_match_formal_arglist (sym, 0, 0);
7133 if (m == MATCH_NO)
7135 gfc_error ("Expected formal argument list in function "
7136 "definition at %C");
7137 m = MATCH_ERROR;
7138 goto cleanup;
7140 else if (m == MATCH_ERROR)
7141 goto cleanup;
7143 result = NULL;
7145 /* According to the draft, the bind(c) and result clause can
7146 come in either order after the formal_arg_list (i.e., either
7147 can be first, both can exist together or by themselves or neither
7148 one). Therefore, the match_result can't match the end of the
7149 string, and check for the bind(c) or result clause in either order. */
7150 found_match = gfc_match_eos ();
7152 /* Make sure that it isn't already declared as BIND(C). If it is, it
7153 must have been marked BIND(C) with a BIND(C) attribute and that is
7154 not allowed for procedures. */
7155 if (sym->attr.is_bind_c == 1)
7157 sym->attr.is_bind_c = 0;
7158 if (sym->old_symbol != NULL)
7159 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7160 "variables or common blocks",
7161 &(sym->old_symbol->declared_at));
7162 else
7163 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7164 "variables or common blocks", &gfc_current_locus);
7167 if (found_match != MATCH_YES)
7169 /* If we haven't found the end-of-statement, look for a suffix. */
7170 suffix_match = gfc_match_suffix (sym, &result);
7171 if (suffix_match == MATCH_YES)
7172 /* Need to get the eos now. */
7173 found_match = gfc_match_eos ();
7174 else
7175 found_match = suffix_match;
7178 if(found_match != MATCH_YES)
7179 m = MATCH_ERROR;
7180 else
7182 /* Make changes to the symbol. */
7183 m = MATCH_ERROR;
7185 if (!gfc_add_function (&sym->attr, sym->name, NULL))
7186 goto cleanup;
7188 if (!gfc_missing_attr (&sym->attr, NULL))
7189 goto cleanup;
7191 if (!copy_prefix (&sym->attr, &sym->declared_at))
7193 if(!sym->attr.module_procedure)
7194 goto cleanup;
7195 else
7196 gfc_error_check ();
7199 /* Delay matching the function characteristics until after the
7200 specification block by signalling kind=-1. */
7201 sym->declared_at = old_loc;
7202 if (current_ts.type != BT_UNKNOWN)
7203 current_ts.kind = -1;
7204 else
7205 current_ts.kind = 0;
7207 if (result == NULL)
7209 if (current_ts.type != BT_UNKNOWN
7210 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
7211 goto cleanup;
7212 sym->result = sym;
7214 else
7216 if (current_ts.type != BT_UNKNOWN
7217 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
7218 goto cleanup;
7219 sym->result = result;
7222 /* Warn if this procedure has the same name as an intrinsic. */
7223 do_warn_intrinsic_shadow (sym, true);
7225 return MATCH_YES;
7228 cleanup:
7229 gfc_current_locus = old_loc;
7230 return m;
7234 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
7235 pass the name of the entry, rather than the gfc_current_block name, and
7236 to return false upon finding an existing global entry. */
7238 static bool
7239 add_global_entry (const char *name, const char *binding_label, bool sub,
7240 locus *where)
7242 gfc_gsymbol *s;
7243 enum gfc_symbol_type type;
7245 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7247 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7248 name is a global identifier. */
7249 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
7251 s = gfc_get_gsymbol (name);
7253 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7255 gfc_global_used (s, where);
7256 return false;
7258 else
7260 s->type = type;
7261 s->sym_name = name;
7262 s->where = *where;
7263 s->defined = 1;
7264 s->ns = gfc_current_ns;
7268 /* Don't add the symbol multiple times. */
7269 if (binding_label
7270 && (!gfc_notification_std (GFC_STD_F2008)
7271 || strcmp (name, binding_label) != 0))
7273 s = gfc_get_gsymbol (binding_label);
7275 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7277 gfc_global_used (s, where);
7278 return false;
7280 else
7282 s->type = type;
7283 s->sym_name = name;
7284 s->binding_label = binding_label;
7285 s->where = *where;
7286 s->defined = 1;
7287 s->ns = gfc_current_ns;
7291 return true;
7295 /* Match an ENTRY statement. */
7297 match
7298 gfc_match_entry (void)
7300 gfc_symbol *proc;
7301 gfc_symbol *result;
7302 gfc_symbol *entry;
7303 char name[GFC_MAX_SYMBOL_LEN + 1];
7304 gfc_compile_state state;
7305 match m;
7306 gfc_entry_list *el;
7307 locus old_loc;
7308 bool module_procedure;
7309 char peek_char;
7310 match is_bind_c;
7312 m = gfc_match_name (name);
7313 if (m != MATCH_YES)
7314 return m;
7316 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
7317 return MATCH_ERROR;
7319 state = gfc_current_state ();
7320 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
7322 switch (state)
7324 case COMP_PROGRAM:
7325 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7326 break;
7327 case COMP_MODULE:
7328 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7329 break;
7330 case COMP_SUBMODULE:
7331 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7332 break;
7333 case COMP_BLOCK_DATA:
7334 gfc_error ("ENTRY statement at %C cannot appear within "
7335 "a BLOCK DATA");
7336 break;
7337 case COMP_INTERFACE:
7338 gfc_error ("ENTRY statement at %C cannot appear within "
7339 "an INTERFACE");
7340 break;
7341 case COMP_STRUCTURE:
7342 gfc_error ("ENTRY statement at %C cannot appear within "
7343 "a STRUCTURE block");
7344 break;
7345 case COMP_DERIVED:
7346 gfc_error ("ENTRY statement at %C cannot appear within "
7347 "a DERIVED TYPE block");
7348 break;
7349 case COMP_IF:
7350 gfc_error ("ENTRY statement at %C cannot appear within "
7351 "an IF-THEN block");
7352 break;
7353 case COMP_DO:
7354 case COMP_DO_CONCURRENT:
7355 gfc_error ("ENTRY statement at %C cannot appear within "
7356 "a DO block");
7357 break;
7358 case COMP_SELECT:
7359 gfc_error ("ENTRY statement at %C cannot appear within "
7360 "a SELECT block");
7361 break;
7362 case COMP_FORALL:
7363 gfc_error ("ENTRY statement at %C cannot appear within "
7364 "a FORALL block");
7365 break;
7366 case COMP_WHERE:
7367 gfc_error ("ENTRY statement at %C cannot appear within "
7368 "a WHERE block");
7369 break;
7370 case COMP_CONTAINS:
7371 gfc_error ("ENTRY statement at %C cannot appear within "
7372 "a contained subprogram");
7373 break;
7374 default:
7375 gfc_error ("Unexpected ENTRY statement at %C");
7377 return MATCH_ERROR;
7380 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7381 && gfc_state_stack->previous->state == COMP_INTERFACE)
7383 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7384 return MATCH_ERROR;
7387 module_procedure = gfc_current_ns->parent != NULL
7388 && gfc_current_ns->parent->proc_name
7389 && gfc_current_ns->parent->proc_name->attr.flavor
7390 == FL_MODULE;
7392 if (gfc_current_ns->parent != NULL
7393 && gfc_current_ns->parent->proc_name
7394 && !module_procedure)
7396 gfc_error("ENTRY statement at %C cannot appear in a "
7397 "contained procedure");
7398 return MATCH_ERROR;
7401 /* Module function entries need special care in get_proc_name
7402 because previous references within the function will have
7403 created symbols attached to the current namespace. */
7404 if (get_proc_name (name, &entry,
7405 gfc_current_ns->parent != NULL
7406 && module_procedure))
7407 return MATCH_ERROR;
7409 proc = gfc_current_block ();
7411 /* Make sure that it isn't already declared as BIND(C). If it is, it
7412 must have been marked BIND(C) with a BIND(C) attribute and that is
7413 not allowed for procedures. */
7414 if (entry->attr.is_bind_c == 1)
7416 entry->attr.is_bind_c = 0;
7417 if (entry->old_symbol != NULL)
7418 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7419 "variables or common blocks",
7420 &(entry->old_symbol->declared_at));
7421 else
7422 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7423 "variables or common blocks", &gfc_current_locus);
7426 /* Check what next non-whitespace character is so we can tell if there
7427 is the required parens if we have a BIND(C). */
7428 old_loc = gfc_current_locus;
7429 gfc_gobble_whitespace ();
7430 peek_char = gfc_peek_ascii_char ();
7432 if (state == COMP_SUBROUTINE)
7434 m = gfc_match_formal_arglist (entry, 0, 1);
7435 if (m != MATCH_YES)
7436 return MATCH_ERROR;
7438 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7439 never be an internal procedure. */
7440 is_bind_c = gfc_match_bind_c (entry, true);
7441 if (is_bind_c == MATCH_ERROR)
7442 return MATCH_ERROR;
7443 if (is_bind_c == MATCH_YES)
7445 if (peek_char != '(')
7447 gfc_error ("Missing required parentheses before BIND(C) at %C");
7448 return MATCH_ERROR;
7451 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7452 &(entry->declared_at), 1))
7453 return MATCH_ERROR;
7457 if (!gfc_current_ns->parent
7458 && !add_global_entry (name, entry->binding_label, true,
7459 &old_loc))
7460 return MATCH_ERROR;
7462 /* An entry in a subroutine. */
7463 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7464 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7465 return MATCH_ERROR;
7467 else
7469 /* An entry in a function.
7470 We need to take special care because writing
7471 ENTRY f()
7473 ENTRY f
7474 is allowed, whereas
7475 ENTRY f() RESULT (r)
7476 can't be written as
7477 ENTRY f RESULT (r). */
7478 if (gfc_match_eos () == MATCH_YES)
7480 gfc_current_locus = old_loc;
7481 /* Match the empty argument list, and add the interface to
7482 the symbol. */
7483 m = gfc_match_formal_arglist (entry, 0, 1);
7485 else
7486 m = gfc_match_formal_arglist (entry, 0, 0);
7488 if (m != MATCH_YES)
7489 return MATCH_ERROR;
7491 result = NULL;
7493 if (gfc_match_eos () == MATCH_YES)
7495 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7496 || !gfc_add_function (&entry->attr, entry->name, NULL))
7497 return MATCH_ERROR;
7499 entry->result = entry;
7501 else
7503 m = gfc_match_suffix (entry, &result);
7504 if (m == MATCH_NO)
7505 gfc_syntax_error (ST_ENTRY);
7506 if (m != MATCH_YES)
7507 return MATCH_ERROR;
7509 if (result)
7511 if (!gfc_add_result (&result->attr, result->name, NULL)
7512 || !gfc_add_entry (&entry->attr, result->name, NULL)
7513 || !gfc_add_function (&entry->attr, result->name, NULL))
7514 return MATCH_ERROR;
7515 entry->result = result;
7517 else
7519 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7520 || !gfc_add_function (&entry->attr, entry->name, NULL))
7521 return MATCH_ERROR;
7522 entry->result = entry;
7526 if (!gfc_current_ns->parent
7527 && !add_global_entry (name, entry->binding_label, false,
7528 &old_loc))
7529 return MATCH_ERROR;
7532 if (gfc_match_eos () != MATCH_YES)
7534 gfc_syntax_error (ST_ENTRY);
7535 return MATCH_ERROR;
7538 /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
7539 if (proc->attr.elemental && entry->attr.is_bind_c)
7541 gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
7542 "elemental procedure", &entry->declared_at);
7543 return MATCH_ERROR;
7546 entry->attr.recursive = proc->attr.recursive;
7547 entry->attr.elemental = proc->attr.elemental;
7548 entry->attr.pure = proc->attr.pure;
7550 el = gfc_get_entry_list ();
7551 el->sym = entry;
7552 el->next = gfc_current_ns->entries;
7553 gfc_current_ns->entries = el;
7554 if (el->next)
7555 el->id = el->next->id + 1;
7556 else
7557 el->id = 1;
7559 new_st.op = EXEC_ENTRY;
7560 new_st.ext.entry = el;
7562 return MATCH_YES;
7566 /* Match a subroutine statement, including optional prefixes. */
7568 match
7569 gfc_match_subroutine (void)
7571 char name[GFC_MAX_SYMBOL_LEN + 1];
7572 gfc_symbol *sym;
7573 match m;
7574 match is_bind_c;
7575 char peek_char;
7576 bool allow_binding_name;
7577 locus loc;
7579 if (gfc_current_state () != COMP_NONE
7580 && gfc_current_state () != COMP_INTERFACE
7581 && gfc_current_state () != COMP_CONTAINS)
7582 return MATCH_NO;
7584 m = gfc_match_prefix (NULL);
7585 if (m != MATCH_YES)
7586 return m;
7588 m = gfc_match ("subroutine% %n", name);
7589 if (m != MATCH_YES)
7590 return m;
7592 if (get_proc_name (name, &sym, false))
7593 return MATCH_ERROR;
7595 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7596 the symbol existed before. */
7597 sym->declared_at = gfc_current_locus;
7599 if (current_attr.module_procedure)
7600 sym->attr.module_procedure = 1;
7602 if (add_hidden_procptr_result (sym))
7603 sym = sym->result;
7605 gfc_new_block = sym;
7607 /* Check what next non-whitespace character is so we can tell if there
7608 is the required parens if we have a BIND(C). */
7609 gfc_gobble_whitespace ();
7610 peek_char = gfc_peek_ascii_char ();
7612 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7613 return MATCH_ERROR;
7615 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7616 return MATCH_ERROR;
7618 /* Make sure that it isn't already declared as BIND(C). If it is, it
7619 must have been marked BIND(C) with a BIND(C) attribute and that is
7620 not allowed for procedures. */
7621 if (sym->attr.is_bind_c == 1)
7623 sym->attr.is_bind_c = 0;
7624 if (sym->old_symbol != NULL)
7625 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7626 "variables or common blocks",
7627 &(sym->old_symbol->declared_at));
7628 else
7629 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7630 "variables or common blocks", &gfc_current_locus);
7633 /* C binding names are not allowed for internal procedures. */
7634 if (gfc_current_state () == COMP_CONTAINS
7635 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7636 allow_binding_name = false;
7637 else
7638 allow_binding_name = true;
7640 /* Here, we are just checking if it has the bind(c) attribute, and if
7641 so, then we need to make sure it's all correct. If it doesn't,
7642 we still need to continue matching the rest of the subroutine line. */
7643 gfc_gobble_whitespace ();
7644 loc = gfc_current_locus;
7645 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7646 if (is_bind_c == MATCH_ERROR)
7648 /* There was an attempt at the bind(c), but it was wrong. An
7649 error message should have been printed w/in the gfc_match_bind_c
7650 so here we'll just return the MATCH_ERROR. */
7651 return MATCH_ERROR;
7654 if (is_bind_c == MATCH_YES)
7656 gfc_formal_arglist *arg;
7658 /* The following is allowed in the Fortran 2008 draft. */
7659 if (gfc_current_state () == COMP_CONTAINS
7660 && sym->ns->proc_name->attr.flavor != FL_MODULE
7661 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7662 "at %L may not be specified for an internal "
7663 "procedure", &gfc_current_locus))
7664 return MATCH_ERROR;
7666 if (peek_char != '(')
7668 gfc_error ("Missing required parentheses before BIND(C) at %C");
7669 return MATCH_ERROR;
7672 /* Scan the dummy arguments for an alternate return. */
7673 for (arg = sym->formal; arg; arg = arg->next)
7674 if (!arg->sym)
7676 gfc_error ("Alternate return dummy argument cannot appear in a "
7677 "SUBROUTINE with the BIND(C) attribute at %L", &loc);
7678 return MATCH_ERROR;
7681 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
7682 return MATCH_ERROR;
7685 if (gfc_match_eos () != MATCH_YES)
7687 gfc_syntax_error (ST_SUBROUTINE);
7688 return MATCH_ERROR;
7691 if (!copy_prefix (&sym->attr, &sym->declared_at))
7693 if(!sym->attr.module_procedure)
7694 return MATCH_ERROR;
7695 else
7696 gfc_error_check ();
7699 /* Warn if it has the same name as an intrinsic. */
7700 do_warn_intrinsic_shadow (sym, false);
7702 return MATCH_YES;
7706 /* Check that the NAME identifier in a BIND attribute or statement
7707 is conform to C identifier rules. */
7709 match
7710 check_bind_name_identifier (char **name)
7712 char *n = *name, *p;
7714 /* Remove leading spaces. */
7715 while (*n == ' ')
7716 n++;
7718 /* On an empty string, free memory and set name to NULL. */
7719 if (*n == '\0')
7721 free (*name);
7722 *name = NULL;
7723 return MATCH_YES;
7726 /* Remove trailing spaces. */
7727 p = n + strlen(n) - 1;
7728 while (*p == ' ')
7729 *(p--) = '\0';
7731 /* Insert the identifier into the symbol table. */
7732 p = xstrdup (n);
7733 free (*name);
7734 *name = p;
7736 /* Now check that identifier is valid under C rules. */
7737 if (ISDIGIT (*p))
7739 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7740 return MATCH_ERROR;
7743 for (; *p; p++)
7744 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
7746 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7747 return MATCH_ERROR;
7750 return MATCH_YES;
7754 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
7755 given, and set the binding label in either the given symbol (if not
7756 NULL), or in the current_ts. The symbol may be NULL because we may
7757 encounter the BIND(C) before the declaration itself. Return
7758 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
7759 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
7760 or MATCH_YES if the specifier was correct and the binding label and
7761 bind(c) fields were set correctly for the given symbol or the
7762 current_ts. If allow_binding_name is false, no binding name may be
7763 given. */
7765 match
7766 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
7768 char *binding_label = NULL;
7769 gfc_expr *e = NULL;
7771 /* Initialize the flag that specifies whether we encountered a NAME=
7772 specifier or not. */
7773 has_name_equals = 0;
7775 /* This much we have to be able to match, in this order, if
7776 there is a bind(c) label. */
7777 if (gfc_match (" bind ( c ") != MATCH_YES)
7778 return MATCH_NO;
7780 /* Now see if there is a binding label, or if we've reached the
7781 end of the bind(c) attribute without one. */
7782 if (gfc_match_char (',') == MATCH_YES)
7784 if (gfc_match (" name = ") != MATCH_YES)
7786 gfc_error ("Syntax error in NAME= specifier for binding label "
7787 "at %C");
7788 /* should give an error message here */
7789 return MATCH_ERROR;
7792 has_name_equals = 1;
7794 if (gfc_match_init_expr (&e) != MATCH_YES)
7796 gfc_free_expr (e);
7797 return MATCH_ERROR;
7800 if (!gfc_simplify_expr(e, 0))
7802 gfc_error ("NAME= specifier at %C should be a constant expression");
7803 gfc_free_expr (e);
7804 return MATCH_ERROR;
7807 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
7808 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
7810 gfc_error ("NAME= specifier at %C should be a scalar of "
7811 "default character kind");
7812 gfc_free_expr(e);
7813 return MATCH_ERROR;
7816 // Get a C string from the Fortran string constant
7817 binding_label = gfc_widechar_to_char (e->value.character.string,
7818 e->value.character.length);
7819 gfc_free_expr(e);
7821 // Check that it is valid (old gfc_match_name_C)
7822 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
7823 return MATCH_ERROR;
7826 /* Get the required right paren. */
7827 if (gfc_match_char (')') != MATCH_YES)
7829 gfc_error ("Missing closing paren for binding label at %C");
7830 return MATCH_ERROR;
7833 if (has_name_equals && !allow_binding_name)
7835 gfc_error ("No binding name is allowed in BIND(C) at %C");
7836 return MATCH_ERROR;
7839 if (has_name_equals && sym != NULL && sym->attr.dummy)
7841 gfc_error ("For dummy procedure %s, no binding name is "
7842 "allowed in BIND(C) at %C", sym->name);
7843 return MATCH_ERROR;
7847 /* Save the binding label to the symbol. If sym is null, we're
7848 probably matching the typespec attributes of a declaration and
7849 haven't gotten the name yet, and therefore, no symbol yet. */
7850 if (binding_label)
7852 if (sym != NULL)
7853 sym->binding_label = binding_label;
7854 else
7855 curr_binding_label = binding_label;
7857 else if (allow_binding_name)
7859 /* No binding label, but if symbol isn't null, we
7860 can set the label for it here.
7861 If name="" or allow_binding_name is false, no C binding name is
7862 created. */
7863 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
7864 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
7867 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
7868 && current_interface.type == INTERFACE_ABSTRACT)
7870 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
7871 return MATCH_ERROR;
7874 return MATCH_YES;
7878 /* Return nonzero if we're currently compiling a contained procedure. */
7880 static int
7881 contained_procedure (void)
7883 gfc_state_data *s = gfc_state_stack;
7885 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
7886 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
7887 return 1;
7889 return 0;
7892 /* Set the kind of each enumerator. The kind is selected such that it is
7893 interoperable with the corresponding C enumeration type, making
7894 sure that -fshort-enums is honored. */
7896 static void
7897 set_enum_kind(void)
7899 enumerator_history *current_history = NULL;
7900 int kind;
7901 int i;
7903 if (max_enum == NULL || enum_history == NULL)
7904 return;
7906 if (!flag_short_enums)
7907 return;
7909 i = 0;
7912 kind = gfc_integer_kinds[i++].kind;
7914 while (kind < gfc_c_int_kind
7915 && gfc_check_integer_range (max_enum->initializer->value.integer,
7916 kind) != ARITH_OK);
7918 current_history = enum_history;
7919 while (current_history != NULL)
7921 current_history->sym->ts.kind = kind;
7922 current_history = current_history->next;
7927 /* Match any of the various end-block statements. Returns the type of
7928 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
7929 and END BLOCK statements cannot be replaced by a single END statement. */
7931 match
7932 gfc_match_end (gfc_statement *st)
7934 char name[GFC_MAX_SYMBOL_LEN + 1];
7935 gfc_compile_state state;
7936 locus old_loc;
7937 const char *block_name;
7938 const char *target;
7939 int eos_ok;
7940 match m;
7941 gfc_namespace *parent_ns, *ns, *prev_ns;
7942 gfc_namespace **nsp;
7943 bool abreviated_modproc_decl = false;
7944 bool got_matching_end = false;
7946 old_loc = gfc_current_locus;
7947 if (gfc_match ("end") != MATCH_YES)
7948 return MATCH_NO;
7950 state = gfc_current_state ();
7951 block_name = gfc_current_block () == NULL
7952 ? NULL : gfc_current_block ()->name;
7954 switch (state)
7956 case COMP_ASSOCIATE:
7957 case COMP_BLOCK:
7958 if (gfc_str_startswith (block_name, "block@"))
7959 block_name = NULL;
7960 break;
7962 case COMP_CONTAINS:
7963 case COMP_DERIVED_CONTAINS:
7964 state = gfc_state_stack->previous->state;
7965 block_name = gfc_state_stack->previous->sym == NULL
7966 ? NULL : gfc_state_stack->previous->sym->name;
7967 abreviated_modproc_decl = gfc_state_stack->previous->sym
7968 && gfc_state_stack->previous->sym->abr_modproc_decl;
7969 break;
7971 default:
7972 break;
7975 if (!abreviated_modproc_decl)
7976 abreviated_modproc_decl = gfc_current_block ()
7977 && gfc_current_block ()->abr_modproc_decl;
7979 switch (state)
7981 case COMP_NONE:
7982 case COMP_PROGRAM:
7983 *st = ST_END_PROGRAM;
7984 target = " program";
7985 eos_ok = 1;
7986 break;
7988 case COMP_SUBROUTINE:
7989 *st = ST_END_SUBROUTINE;
7990 if (!abreviated_modproc_decl)
7991 target = " subroutine";
7992 else
7993 target = " procedure";
7994 eos_ok = !contained_procedure ();
7995 break;
7997 case COMP_FUNCTION:
7998 *st = ST_END_FUNCTION;
7999 if (!abreviated_modproc_decl)
8000 target = " function";
8001 else
8002 target = " procedure";
8003 eos_ok = !contained_procedure ();
8004 break;
8006 case COMP_BLOCK_DATA:
8007 *st = ST_END_BLOCK_DATA;
8008 target = " block data";
8009 eos_ok = 1;
8010 break;
8012 case COMP_MODULE:
8013 *st = ST_END_MODULE;
8014 target = " module";
8015 eos_ok = 1;
8016 break;
8018 case COMP_SUBMODULE:
8019 *st = ST_END_SUBMODULE;
8020 target = " submodule";
8021 eos_ok = 1;
8022 break;
8024 case COMP_INTERFACE:
8025 *st = ST_END_INTERFACE;
8026 target = " interface";
8027 eos_ok = 0;
8028 break;
8030 case COMP_MAP:
8031 *st = ST_END_MAP;
8032 target = " map";
8033 eos_ok = 0;
8034 break;
8036 case COMP_UNION:
8037 *st = ST_END_UNION;
8038 target = " union";
8039 eos_ok = 0;
8040 break;
8042 case COMP_STRUCTURE:
8043 *st = ST_END_STRUCTURE;
8044 target = " structure";
8045 eos_ok = 0;
8046 break;
8048 case COMP_DERIVED:
8049 case COMP_DERIVED_CONTAINS:
8050 *st = ST_END_TYPE;
8051 target = " type";
8052 eos_ok = 0;
8053 break;
8055 case COMP_ASSOCIATE:
8056 *st = ST_END_ASSOCIATE;
8057 target = " associate";
8058 eos_ok = 0;
8059 break;
8061 case COMP_BLOCK:
8062 *st = ST_END_BLOCK;
8063 target = " block";
8064 eos_ok = 0;
8065 break;
8067 case COMP_IF:
8068 *st = ST_ENDIF;
8069 target = " if";
8070 eos_ok = 0;
8071 break;
8073 case COMP_DO:
8074 case COMP_DO_CONCURRENT:
8075 *st = ST_ENDDO;
8076 target = " do";
8077 eos_ok = 0;
8078 break;
8080 case COMP_CRITICAL:
8081 *st = ST_END_CRITICAL;
8082 target = " critical";
8083 eos_ok = 0;
8084 break;
8086 case COMP_SELECT:
8087 case COMP_SELECT_TYPE:
8088 *st = ST_END_SELECT;
8089 target = " select";
8090 eos_ok = 0;
8091 break;
8093 case COMP_FORALL:
8094 *st = ST_END_FORALL;
8095 target = " forall";
8096 eos_ok = 0;
8097 break;
8099 case COMP_WHERE:
8100 *st = ST_END_WHERE;
8101 target = " where";
8102 eos_ok = 0;
8103 break;
8105 case COMP_ENUM:
8106 *st = ST_END_ENUM;
8107 target = " enum";
8108 eos_ok = 0;
8109 last_initializer = NULL;
8110 set_enum_kind ();
8111 gfc_free_enum_history ();
8112 break;
8114 default:
8115 gfc_error ("Unexpected END statement at %C");
8116 goto cleanup;
8119 old_loc = gfc_current_locus;
8120 if (gfc_match_eos () == MATCH_YES)
8122 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
8124 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
8125 "instead of %s statement at %L",
8126 abreviated_modproc_decl ? "END PROCEDURE"
8127 : gfc_ascii_statement(*st), &old_loc))
8128 goto cleanup;
8130 else if (!eos_ok)
8132 /* We would have required END [something]. */
8133 gfc_error ("%s statement expected at %L",
8134 gfc_ascii_statement (*st), &old_loc);
8135 goto cleanup;
8138 return MATCH_YES;
8141 /* Verify that we've got the sort of end-block that we're expecting. */
8142 if (gfc_match (target) != MATCH_YES)
8144 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
8145 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
8146 goto cleanup;
8148 else
8149 got_matching_end = true;
8151 old_loc = gfc_current_locus;
8152 /* If we're at the end, make sure a block name wasn't required. */
8153 if (gfc_match_eos () == MATCH_YES)
8156 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
8157 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
8158 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
8159 return MATCH_YES;
8161 if (!block_name)
8162 return MATCH_YES;
8164 gfc_error ("Expected block name of %qs in %s statement at %L",
8165 block_name, gfc_ascii_statement (*st), &old_loc);
8167 return MATCH_ERROR;
8170 /* END INTERFACE has a special handler for its several possible endings. */
8171 if (*st == ST_END_INTERFACE)
8172 return gfc_match_end_interface ();
8174 /* We haven't hit the end of statement, so what is left must be an
8175 end-name. */
8176 m = gfc_match_space ();
8177 if (m == MATCH_YES)
8178 m = gfc_match_name (name);
8180 if (m == MATCH_NO)
8181 gfc_error ("Expected terminating name at %C");
8182 if (m != MATCH_YES)
8183 goto cleanup;
8185 if (block_name == NULL)
8186 goto syntax;
8188 /* We have to pick out the declared submodule name from the composite
8189 required by F2008:11.2.3 para 2, which ends in the declared name. */
8190 if (state == COMP_SUBMODULE)
8191 block_name = strchr (block_name, '.') + 1;
8193 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
8195 gfc_error ("Expected label %qs for %s statement at %C", block_name,
8196 gfc_ascii_statement (*st));
8197 goto cleanup;
8199 /* Procedure pointer as function result. */
8200 else if (strcmp (block_name, "ppr@") == 0
8201 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8203 gfc_error ("Expected label %qs for %s statement at %C",
8204 gfc_current_block ()->ns->proc_name->name,
8205 gfc_ascii_statement (*st));
8206 goto cleanup;
8209 if (gfc_match_eos () == MATCH_YES)
8210 return MATCH_YES;
8212 syntax:
8213 gfc_syntax_error (*st);
8215 cleanup:
8216 gfc_current_locus = old_loc;
8218 /* If we are missing an END BLOCK, we created a half-ready namespace.
8219 Remove it from the parent namespace's sibling list. */
8221 while (state == COMP_BLOCK && !got_matching_end)
8223 parent_ns = gfc_current_ns->parent;
8225 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8227 prev_ns = NULL;
8228 ns = *nsp;
8229 while (ns)
8231 if (ns == gfc_current_ns)
8233 if (prev_ns == NULL)
8234 *nsp = NULL;
8235 else
8236 prev_ns->sibling = ns->sibling;
8238 prev_ns = ns;
8239 ns = ns->sibling;
8242 gfc_free_namespace (gfc_current_ns);
8243 gfc_current_ns = parent_ns;
8244 gfc_state_stack = gfc_state_stack->previous;
8245 state = gfc_current_state ();
8248 return MATCH_ERROR;
8253 /***************** Attribute declaration statements ****************/
8255 /* Set the attribute of a single variable. */
8257 static match
8258 attr_decl1 (void)
8260 char name[GFC_MAX_SYMBOL_LEN + 1];
8261 gfc_array_spec *as;
8263 /* Workaround -Wmaybe-uninitialized false positive during
8264 profiledbootstrap by initializing them. */
8265 gfc_symbol *sym = NULL;
8266 locus var_locus;
8267 match m;
8269 as = NULL;
8271 m = gfc_match_name (name);
8272 if (m != MATCH_YES)
8273 goto cleanup;
8275 if (find_special (name, &sym, false))
8276 return MATCH_ERROR;
8278 if (!check_function_name (name))
8280 m = MATCH_ERROR;
8281 goto cleanup;
8284 var_locus = gfc_current_locus;
8286 /* Deal with possible array specification for certain attributes. */
8287 if (current_attr.dimension
8288 || current_attr.codimension
8289 || current_attr.allocatable
8290 || current_attr.pointer
8291 || current_attr.target)
8293 m = gfc_match_array_spec (&as, !current_attr.codimension,
8294 !current_attr.dimension
8295 && !current_attr.pointer
8296 && !current_attr.target);
8297 if (m == MATCH_ERROR)
8298 goto cleanup;
8300 if (current_attr.dimension && m == MATCH_NO)
8302 gfc_error ("Missing array specification at %L in DIMENSION "
8303 "statement", &var_locus);
8304 m = MATCH_ERROR;
8305 goto cleanup;
8308 if (current_attr.dimension && sym->value)
8310 gfc_error ("Dimensions specified for %s at %L after its "
8311 "initialization", sym->name, &var_locus);
8312 m = MATCH_ERROR;
8313 goto cleanup;
8316 if (current_attr.codimension && m == MATCH_NO)
8318 gfc_error ("Missing array specification at %L in CODIMENSION "
8319 "statement", &var_locus);
8320 m = MATCH_ERROR;
8321 goto cleanup;
8324 if ((current_attr.allocatable || current_attr.pointer)
8325 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8327 gfc_error ("Array specification must be deferred at %L", &var_locus);
8328 m = MATCH_ERROR;
8329 goto cleanup;
8333 /* Update symbol table. DIMENSION attribute is set in
8334 gfc_set_array_spec(). For CLASS variables, this must be applied
8335 to the first component, or '_data' field. */
8336 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
8338 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
8340 m = MATCH_ERROR;
8341 goto cleanup;
8344 else
8346 if (current_attr.dimension == 0 && current_attr.codimension == 0
8347 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
8349 m = MATCH_ERROR;
8350 goto cleanup;
8354 if (sym->ts.type == BT_CLASS
8355 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
8357 m = MATCH_ERROR;
8358 goto cleanup;
8361 if (!gfc_set_array_spec (sym, as, &var_locus))
8363 m = MATCH_ERROR;
8364 goto cleanup;
8367 if (sym->attr.cray_pointee && sym->as != NULL)
8369 /* Fix the array spec. */
8370 m = gfc_mod_pointee_as (sym->as);
8371 if (m == MATCH_ERROR)
8372 goto cleanup;
8375 if (!gfc_add_attribute (&sym->attr, &var_locus))
8377 m = MATCH_ERROR;
8378 goto cleanup;
8381 if ((current_attr.external || current_attr.intrinsic)
8382 && sym->attr.flavor != FL_PROCEDURE
8383 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8385 m = MATCH_ERROR;
8386 goto cleanup;
8389 add_hidden_procptr_result (sym);
8391 return MATCH_YES;
8393 cleanup:
8394 gfc_free_array_spec (as);
8395 return m;
8399 /* Generic attribute declaration subroutine. Used for attributes that
8400 just have a list of names. */
8402 static match
8403 attr_decl (void)
8405 match m;
8407 /* Gobble the optional double colon, by simply ignoring the result
8408 of gfc_match(). */
8409 gfc_match (" ::");
8411 for (;;)
8413 m = attr_decl1 ();
8414 if (m != MATCH_YES)
8415 break;
8417 if (gfc_match_eos () == MATCH_YES)
8419 m = MATCH_YES;
8420 break;
8423 if (gfc_match_char (',') != MATCH_YES)
8425 gfc_error ("Unexpected character in variable list at %C");
8426 m = MATCH_ERROR;
8427 break;
8431 return m;
8435 /* This routine matches Cray Pointer declarations of the form:
8436 pointer ( <pointer>, <pointee> )
8438 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8439 The pointer, if already declared, should be an integer. Otherwise, we
8440 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8441 be either a scalar, or an array declaration. No space is allocated for
8442 the pointee. For the statement
8443 pointer (ipt, ar(10))
8444 any subsequent uses of ar will be translated (in C-notation) as
8445 ar(i) => ((<type> *) ipt)(i)
8446 After gimplification, pointee variable will disappear in the code. */
8448 static match
8449 cray_pointer_decl (void)
8451 match m;
8452 gfc_array_spec *as = NULL;
8453 gfc_symbol *cptr; /* Pointer symbol. */
8454 gfc_symbol *cpte; /* Pointee symbol. */
8455 locus var_locus;
8456 bool done = false;
8458 while (!done)
8460 if (gfc_match_char ('(') != MATCH_YES)
8462 gfc_error ("Expected %<(%> at %C");
8463 return MATCH_ERROR;
8466 /* Match pointer. */
8467 var_locus = gfc_current_locus;
8468 gfc_clear_attr (&current_attr);
8469 gfc_add_cray_pointer (&current_attr, &var_locus);
8470 current_ts.type = BT_INTEGER;
8471 current_ts.kind = gfc_index_integer_kind;
8473 m = gfc_match_symbol (&cptr, 0);
8474 if (m != MATCH_YES)
8476 gfc_error ("Expected variable name at %C");
8477 return m;
8480 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8481 return MATCH_ERROR;
8483 gfc_set_sym_referenced (cptr);
8485 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8487 cptr->ts.type = BT_INTEGER;
8488 cptr->ts.kind = gfc_index_integer_kind;
8490 else if (cptr->ts.type != BT_INTEGER)
8492 gfc_error ("Cray pointer at %C must be an integer");
8493 return MATCH_ERROR;
8495 else if (cptr->ts.kind < gfc_index_integer_kind)
8496 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8497 " memory addresses require %d bytes",
8498 cptr->ts.kind, gfc_index_integer_kind);
8500 if (gfc_match_char (',') != MATCH_YES)
8502 gfc_error ("Expected \",\" at %C");
8503 return MATCH_ERROR;
8506 /* Match Pointee. */
8507 var_locus = gfc_current_locus;
8508 gfc_clear_attr (&current_attr);
8509 gfc_add_cray_pointee (&current_attr, &var_locus);
8510 current_ts.type = BT_UNKNOWN;
8511 current_ts.kind = 0;
8513 m = gfc_match_symbol (&cpte, 0);
8514 if (m != MATCH_YES)
8516 gfc_error ("Expected variable name at %C");
8517 return m;
8520 /* Check for an optional array spec. */
8521 m = gfc_match_array_spec (&as, true, false);
8522 if (m == MATCH_ERROR)
8524 gfc_free_array_spec (as);
8525 return m;
8527 else if (m == MATCH_NO)
8529 gfc_free_array_spec (as);
8530 as = NULL;
8533 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8534 return MATCH_ERROR;
8536 gfc_set_sym_referenced (cpte);
8538 if (cpte->as == NULL)
8540 if (!gfc_set_array_spec (cpte, as, &var_locus))
8541 gfc_internal_error ("Couldn't set Cray pointee array spec.");
8543 else if (as != NULL)
8545 gfc_error ("Duplicate array spec for Cray pointee at %C");
8546 gfc_free_array_spec (as);
8547 return MATCH_ERROR;
8550 as = NULL;
8552 if (cpte->as != NULL)
8554 /* Fix array spec. */
8555 m = gfc_mod_pointee_as (cpte->as);
8556 if (m == MATCH_ERROR)
8557 return m;
8560 /* Point the Pointee at the Pointer. */
8561 cpte->cp_pointer = cptr;
8563 if (gfc_match_char (')') != MATCH_YES)
8565 gfc_error ("Expected \")\" at %C");
8566 return MATCH_ERROR;
8568 m = gfc_match_char (',');
8569 if (m != MATCH_YES)
8570 done = true; /* Stop searching for more declarations. */
8574 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8575 || gfc_match_eos () != MATCH_YES)
8577 gfc_error ("Expected %<,%> or end of statement at %C");
8578 return MATCH_ERROR;
8580 return MATCH_YES;
8584 match
8585 gfc_match_external (void)
8588 gfc_clear_attr (&current_attr);
8589 current_attr.external = 1;
8591 return attr_decl ();
8595 match
8596 gfc_match_intent (void)
8598 sym_intent intent;
8600 /* This is not allowed within a BLOCK construct! */
8601 if (gfc_current_state () == COMP_BLOCK)
8603 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8604 return MATCH_ERROR;
8607 intent = match_intent_spec ();
8608 if (intent == INTENT_UNKNOWN)
8609 return MATCH_ERROR;
8611 gfc_clear_attr (&current_attr);
8612 current_attr.intent = intent;
8614 return attr_decl ();
8618 match
8619 gfc_match_intrinsic (void)
8622 gfc_clear_attr (&current_attr);
8623 current_attr.intrinsic = 1;
8625 return attr_decl ();
8629 match
8630 gfc_match_optional (void)
8632 /* This is not allowed within a BLOCK construct! */
8633 if (gfc_current_state () == COMP_BLOCK)
8635 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8636 return MATCH_ERROR;
8639 gfc_clear_attr (&current_attr);
8640 current_attr.optional = 1;
8642 return attr_decl ();
8646 match
8647 gfc_match_pointer (void)
8649 gfc_gobble_whitespace ();
8650 if (gfc_peek_ascii_char () == '(')
8652 if (!flag_cray_pointer)
8654 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
8655 "flag");
8656 return MATCH_ERROR;
8658 return cray_pointer_decl ();
8660 else
8662 gfc_clear_attr (&current_attr);
8663 current_attr.pointer = 1;
8665 return attr_decl ();
8670 match
8671 gfc_match_allocatable (void)
8673 gfc_clear_attr (&current_attr);
8674 current_attr.allocatable = 1;
8676 return attr_decl ();
8680 match
8681 gfc_match_codimension (void)
8683 gfc_clear_attr (&current_attr);
8684 current_attr.codimension = 1;
8686 return attr_decl ();
8690 match
8691 gfc_match_contiguous (void)
8693 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
8694 return MATCH_ERROR;
8696 gfc_clear_attr (&current_attr);
8697 current_attr.contiguous = 1;
8699 return attr_decl ();
8703 match
8704 gfc_match_dimension (void)
8706 gfc_clear_attr (&current_attr);
8707 current_attr.dimension = 1;
8709 return attr_decl ();
8713 match
8714 gfc_match_target (void)
8716 gfc_clear_attr (&current_attr);
8717 current_attr.target = 1;
8719 return attr_decl ();
8723 /* Match the list of entities being specified in a PUBLIC or PRIVATE
8724 statement. */
8726 static match
8727 access_attr_decl (gfc_statement st)
8729 char name[GFC_MAX_SYMBOL_LEN + 1];
8730 interface_type type;
8731 gfc_user_op *uop;
8732 gfc_symbol *sym, *dt_sym;
8733 gfc_intrinsic_op op;
8734 match m;
8736 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8737 goto done;
8739 for (;;)
8741 m = gfc_match_generic_spec (&type, name, &op);
8742 if (m == MATCH_NO)
8743 goto syntax;
8744 if (m == MATCH_ERROR)
8745 return MATCH_ERROR;
8747 switch (type)
8749 case INTERFACE_NAMELESS:
8750 case INTERFACE_ABSTRACT:
8751 goto syntax;
8753 case INTERFACE_GENERIC:
8754 case INTERFACE_DTIO:
8756 if (gfc_get_symbol (name, NULL, &sym))
8757 goto done;
8759 if (type == INTERFACE_DTIO
8760 && gfc_current_ns->proc_name
8761 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
8762 && sym->attr.flavor == FL_UNKNOWN)
8763 sym->attr.flavor = FL_PROCEDURE;
8765 if (!gfc_add_access (&sym->attr,
8766 (st == ST_PUBLIC)
8767 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8768 sym->name, NULL))
8769 return MATCH_ERROR;
8771 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
8772 && !gfc_add_access (&dt_sym->attr,
8773 (st == ST_PUBLIC)
8774 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
8775 sym->name, NULL))
8776 return MATCH_ERROR;
8778 break;
8780 case INTERFACE_INTRINSIC_OP:
8781 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
8783 gfc_intrinsic_op other_op;
8785 gfc_current_ns->operator_access[op] =
8786 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8788 /* Handle the case if there is another op with the same
8789 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
8790 other_op = gfc_equivalent_op (op);
8792 if (other_op != INTRINSIC_NONE)
8793 gfc_current_ns->operator_access[other_op] =
8794 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8797 else
8799 gfc_error ("Access specification of the %s operator at %C has "
8800 "already been specified", gfc_op2string (op));
8801 goto done;
8804 break;
8806 case INTERFACE_USER_OP:
8807 uop = gfc_get_uop (name);
8809 if (uop->access == ACCESS_UNKNOWN)
8811 uop->access = (st == ST_PUBLIC)
8812 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
8814 else
8816 gfc_error ("Access specification of the .%s. operator at %C "
8817 "has already been specified", sym->name);
8818 goto done;
8821 break;
8824 if (gfc_match_char (',') == MATCH_NO)
8825 break;
8828 if (gfc_match_eos () != MATCH_YES)
8829 goto syntax;
8830 return MATCH_YES;
8832 syntax:
8833 gfc_syntax_error (st);
8835 done:
8836 return MATCH_ERROR;
8840 match
8841 gfc_match_protected (void)
8843 gfc_symbol *sym;
8844 match m;
8846 if (!gfc_current_ns->proc_name
8847 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
8849 gfc_error ("PROTECTED at %C only allowed in specification "
8850 "part of a module");
8851 return MATCH_ERROR;
8855 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
8856 return MATCH_ERROR;
8858 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8860 return MATCH_ERROR;
8863 if (gfc_match_eos () == MATCH_YES)
8864 goto syntax;
8866 for(;;)
8868 m = gfc_match_symbol (&sym, 0);
8869 switch (m)
8871 case MATCH_YES:
8872 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
8873 return MATCH_ERROR;
8874 goto next_item;
8876 case MATCH_NO:
8877 break;
8879 case MATCH_ERROR:
8880 return MATCH_ERROR;
8883 next_item:
8884 if (gfc_match_eos () == MATCH_YES)
8885 break;
8886 if (gfc_match_char (',') != MATCH_YES)
8887 goto syntax;
8890 return MATCH_YES;
8892 syntax:
8893 gfc_error ("Syntax error in PROTECTED statement at %C");
8894 return MATCH_ERROR;
8898 /* The PRIVATE statement is a bit weird in that it can be an attribute
8899 declaration, but also works as a standalone statement inside of a
8900 type declaration or a module. */
8902 match
8903 gfc_match_private (gfc_statement *st)
8906 if (gfc_match ("private") != MATCH_YES)
8907 return MATCH_NO;
8909 if (gfc_current_state () != COMP_MODULE
8910 && !(gfc_current_state () == COMP_DERIVED
8911 && gfc_state_stack->previous
8912 && gfc_state_stack->previous->state == COMP_MODULE)
8913 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
8914 && gfc_state_stack->previous && gfc_state_stack->previous->previous
8915 && gfc_state_stack->previous->previous->state == COMP_MODULE))
8917 gfc_error ("PRIVATE statement at %C is only allowed in the "
8918 "specification part of a module");
8919 return MATCH_ERROR;
8922 if (gfc_current_state () == COMP_DERIVED)
8924 if (gfc_match_eos () == MATCH_YES)
8926 *st = ST_PRIVATE;
8927 return MATCH_YES;
8930 gfc_syntax_error (ST_PRIVATE);
8931 return MATCH_ERROR;
8934 if (gfc_match_eos () == MATCH_YES)
8936 *st = ST_PRIVATE;
8937 return MATCH_YES;
8940 *st = ST_ATTR_DECL;
8941 return access_attr_decl (ST_PRIVATE);
8945 match
8946 gfc_match_public (gfc_statement *st)
8949 if (gfc_match ("public") != MATCH_YES)
8950 return MATCH_NO;
8952 if (gfc_current_state () != COMP_MODULE)
8954 gfc_error ("PUBLIC statement at %C is only allowed in the "
8955 "specification part of a module");
8956 return MATCH_ERROR;
8959 if (gfc_match_eos () == MATCH_YES)
8961 *st = ST_PUBLIC;
8962 return MATCH_YES;
8965 *st = ST_ATTR_DECL;
8966 return access_attr_decl (ST_PUBLIC);
8970 /* Workhorse for gfc_match_parameter. */
8972 static match
8973 do_parm (void)
8975 gfc_symbol *sym;
8976 gfc_expr *init;
8977 match m;
8978 bool t;
8980 m = gfc_match_symbol (&sym, 0);
8981 if (m == MATCH_NO)
8982 gfc_error ("Expected variable name at %C in PARAMETER statement");
8984 if (m != MATCH_YES)
8985 return m;
8987 if (gfc_match_char ('=') == MATCH_NO)
8989 gfc_error ("Expected = sign in PARAMETER statement at %C");
8990 return MATCH_ERROR;
8993 m = gfc_match_init_expr (&init);
8994 if (m == MATCH_NO)
8995 gfc_error ("Expected expression at %C in PARAMETER statement");
8996 if (m != MATCH_YES)
8997 return m;
8999 if (sym->ts.type == BT_UNKNOWN
9000 && !gfc_set_default_type (sym, 1, NULL))
9002 m = MATCH_ERROR;
9003 goto cleanup;
9006 if (!gfc_check_assign_symbol (sym, NULL, init)
9007 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
9009 m = MATCH_ERROR;
9010 goto cleanup;
9013 if (sym->value)
9015 gfc_error ("Initializing already initialized variable at %C");
9016 m = MATCH_ERROR;
9017 goto cleanup;
9020 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
9021 return (t) ? MATCH_YES : MATCH_ERROR;
9023 cleanup:
9024 gfc_free_expr (init);
9025 return m;
9029 /* Match a parameter statement, with the weird syntax that these have. */
9031 match
9032 gfc_match_parameter (void)
9034 const char *term = " )%t";
9035 match m;
9037 if (gfc_match_char ('(') == MATCH_NO)
9039 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
9040 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
9041 return MATCH_NO;
9042 term = " %t";
9045 for (;;)
9047 m = do_parm ();
9048 if (m != MATCH_YES)
9049 break;
9051 if (gfc_match (term) == MATCH_YES)
9052 break;
9054 if (gfc_match_char (',') != MATCH_YES)
9056 gfc_error ("Unexpected characters in PARAMETER statement at %C");
9057 m = MATCH_ERROR;
9058 break;
9062 return m;
9066 match
9067 gfc_match_automatic (void)
9069 gfc_symbol *sym;
9070 match m;
9071 bool seen_symbol = false;
9073 if (!flag_dec_static)
9075 gfc_error ("%s at %C is a DEC extension, enable with "
9076 "%<-fdec-static%>",
9077 "AUTOMATIC"
9079 return MATCH_ERROR;
9082 gfc_match (" ::");
9084 for (;;)
9086 m = gfc_match_symbol (&sym, 0);
9087 switch (m)
9089 case MATCH_NO:
9090 break;
9092 case MATCH_ERROR:
9093 return MATCH_ERROR;
9095 case MATCH_YES:
9096 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
9097 return MATCH_ERROR;
9098 seen_symbol = true;
9099 break;
9102 if (gfc_match_eos () == MATCH_YES)
9103 break;
9104 if (gfc_match_char (',') != MATCH_YES)
9105 goto syntax;
9108 if (!seen_symbol)
9110 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
9111 return MATCH_ERROR;
9114 return MATCH_YES;
9116 syntax:
9117 gfc_error ("Syntax error in AUTOMATIC statement at %C");
9118 return MATCH_ERROR;
9122 match
9123 gfc_match_static (void)
9125 gfc_symbol *sym;
9126 match m;
9127 bool seen_symbol = false;
9129 if (!flag_dec_static)
9131 gfc_error ("%s at %C is a DEC extension, enable with "
9132 "%<-fdec-static%>",
9133 "STATIC");
9134 return MATCH_ERROR;
9137 gfc_match (" ::");
9139 for (;;)
9141 m = gfc_match_symbol (&sym, 0);
9142 switch (m)
9144 case MATCH_NO:
9145 break;
9147 case MATCH_ERROR:
9148 return MATCH_ERROR;
9150 case MATCH_YES:
9151 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9152 &gfc_current_locus))
9153 return MATCH_ERROR;
9154 seen_symbol = true;
9155 break;
9158 if (gfc_match_eos () == MATCH_YES)
9159 break;
9160 if (gfc_match_char (',') != MATCH_YES)
9161 goto syntax;
9164 if (!seen_symbol)
9166 gfc_error ("Expected entity-list in STATIC statement at %C");
9167 return MATCH_ERROR;
9170 return MATCH_YES;
9172 syntax:
9173 gfc_error ("Syntax error in STATIC statement at %C");
9174 return MATCH_ERROR;
9178 /* Save statements have a special syntax. */
9180 match
9181 gfc_match_save (void)
9183 char n[GFC_MAX_SYMBOL_LEN+1];
9184 gfc_common_head *c;
9185 gfc_symbol *sym;
9186 match m;
9188 if (gfc_match_eos () == MATCH_YES)
9190 if (gfc_current_ns->seen_save)
9192 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9193 "follows previous SAVE statement"))
9194 return MATCH_ERROR;
9197 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9198 return MATCH_YES;
9201 if (gfc_current_ns->save_all)
9203 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9204 "blanket SAVE statement"))
9205 return MATCH_ERROR;
9208 gfc_match (" ::");
9210 for (;;)
9212 m = gfc_match_symbol (&sym, 0);
9213 switch (m)
9215 case MATCH_YES:
9216 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9217 &gfc_current_locus))
9218 return MATCH_ERROR;
9219 goto next_item;
9221 case MATCH_NO:
9222 break;
9224 case MATCH_ERROR:
9225 return MATCH_ERROR;
9228 m = gfc_match (" / %n /", &n);
9229 if (m == MATCH_ERROR)
9230 return MATCH_ERROR;
9231 if (m == MATCH_NO)
9232 goto syntax;
9234 c = gfc_get_common (n, 0);
9235 c->saved = 1;
9237 gfc_current_ns->seen_save = 1;
9239 next_item:
9240 if (gfc_match_eos () == MATCH_YES)
9241 break;
9242 if (gfc_match_char (',') != MATCH_YES)
9243 goto syntax;
9246 return MATCH_YES;
9248 syntax:
9249 gfc_error ("Syntax error in SAVE statement at %C");
9250 return MATCH_ERROR;
9254 match
9255 gfc_match_value (void)
9257 gfc_symbol *sym;
9258 match m;
9260 /* This is not allowed within a BLOCK construct! */
9261 if (gfc_current_state () == COMP_BLOCK)
9263 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9264 return MATCH_ERROR;
9267 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
9268 return MATCH_ERROR;
9270 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9272 return MATCH_ERROR;
9275 if (gfc_match_eos () == MATCH_YES)
9276 goto syntax;
9278 for(;;)
9280 m = gfc_match_symbol (&sym, 0);
9281 switch (m)
9283 case MATCH_YES:
9284 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
9285 return MATCH_ERROR;
9286 goto next_item;
9288 case MATCH_NO:
9289 break;
9291 case MATCH_ERROR:
9292 return MATCH_ERROR;
9295 next_item:
9296 if (gfc_match_eos () == MATCH_YES)
9297 break;
9298 if (gfc_match_char (',') != MATCH_YES)
9299 goto syntax;
9302 return MATCH_YES;
9304 syntax:
9305 gfc_error ("Syntax error in VALUE statement at %C");
9306 return MATCH_ERROR;
9310 match
9311 gfc_match_volatile (void)
9313 gfc_symbol *sym;
9314 char *name;
9315 match m;
9317 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
9318 return MATCH_ERROR;
9320 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9322 return MATCH_ERROR;
9325 if (gfc_match_eos () == MATCH_YES)
9326 goto syntax;
9328 for(;;)
9330 /* VOLATILE is special because it can be added to host-associated
9331 symbols locally. Except for coarrays. */
9332 m = gfc_match_symbol (&sym, 1);
9333 switch (m)
9335 case MATCH_YES:
9336 name = XCNEWVAR (char, strlen (sym->name) + 1);
9337 strcpy (name, sym->name);
9338 if (!check_function_name (name))
9339 return MATCH_ERROR;
9340 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9341 for variable in a BLOCK which is defined outside of the BLOCK. */
9342 if (sym->ns != gfc_current_ns && sym->attr.codimension)
9344 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
9345 "%C, which is use-/host-associated", sym->name);
9346 return MATCH_ERROR;
9348 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
9349 return MATCH_ERROR;
9350 goto next_item;
9352 case MATCH_NO:
9353 break;
9355 case MATCH_ERROR:
9356 return MATCH_ERROR;
9359 next_item:
9360 if (gfc_match_eos () == MATCH_YES)
9361 break;
9362 if (gfc_match_char (',') != MATCH_YES)
9363 goto syntax;
9366 return MATCH_YES;
9368 syntax:
9369 gfc_error ("Syntax error in VOLATILE statement at %C");
9370 return MATCH_ERROR;
9374 match
9375 gfc_match_asynchronous (void)
9377 gfc_symbol *sym;
9378 char *name;
9379 match m;
9381 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
9382 return MATCH_ERROR;
9384 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9386 return MATCH_ERROR;
9389 if (gfc_match_eos () == MATCH_YES)
9390 goto syntax;
9392 for(;;)
9394 /* ASYNCHRONOUS is special because it can be added to host-associated
9395 symbols locally. */
9396 m = gfc_match_symbol (&sym, 1);
9397 switch (m)
9399 case MATCH_YES:
9400 name = XCNEWVAR (char, strlen (sym->name) + 1);
9401 strcpy (name, sym->name);
9402 if (!check_function_name (name))
9403 return MATCH_ERROR;
9404 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9405 return MATCH_ERROR;
9406 goto next_item;
9408 case MATCH_NO:
9409 break;
9411 case MATCH_ERROR:
9412 return MATCH_ERROR;
9415 next_item:
9416 if (gfc_match_eos () == MATCH_YES)
9417 break;
9418 if (gfc_match_char (',') != MATCH_YES)
9419 goto syntax;
9422 return MATCH_YES;
9424 syntax:
9425 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9426 return MATCH_ERROR;
9430 /* Match a module procedure statement in a submodule. */
9432 match
9433 gfc_match_submod_proc (void)
9435 char name[GFC_MAX_SYMBOL_LEN + 1];
9436 gfc_symbol *sym, *fsym;
9437 match m;
9438 gfc_formal_arglist *formal, *head, *tail;
9440 if (gfc_current_state () != COMP_CONTAINS
9441 || !(gfc_state_stack->previous
9442 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9443 || gfc_state_stack->previous->state == COMP_MODULE)))
9444 return MATCH_NO;
9446 m = gfc_match (" module% procedure% %n", name);
9447 if (m != MATCH_YES)
9448 return m;
9450 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9451 "at %C"))
9452 return MATCH_ERROR;
9454 if (get_proc_name (name, &sym, false))
9455 return MATCH_ERROR;
9457 /* Make sure that the result field is appropriately filled, even though
9458 the result symbol will be replaced later on. */
9459 if (sym->tlink && sym->tlink->attr.function)
9461 if (sym->tlink->result
9462 && sym->tlink->result != sym->tlink)
9463 sym->result= sym->tlink->result;
9464 else
9465 sym->result = sym;
9468 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9469 the symbol existed before. */
9470 sym->declared_at = gfc_current_locus;
9472 if (!sym->attr.module_procedure)
9473 return MATCH_ERROR;
9475 /* Signal match_end to expect "end procedure". */
9476 sym->abr_modproc_decl = 1;
9478 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9479 sym->attr.if_source = IFSRC_DECL;
9481 gfc_new_block = sym;
9483 /* Make a new formal arglist with the symbols in the procedure
9484 namespace. */
9485 head = tail = NULL;
9486 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9488 if (formal == sym->formal)
9489 head = tail = gfc_get_formal_arglist ();
9490 else
9492 tail->next = gfc_get_formal_arglist ();
9493 tail = tail->next;
9496 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9497 goto cleanup;
9499 tail->sym = fsym;
9500 gfc_set_sym_referenced (fsym);
9503 /* The dummy symbols get cleaned up, when the formal_namespace of the
9504 interface declaration is cleared. This allows us to add the
9505 explicit interface as is done for other type of procedure. */
9506 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9507 &gfc_current_locus))
9508 return MATCH_ERROR;
9510 if (gfc_match_eos () != MATCH_YES)
9512 gfc_syntax_error (ST_MODULE_PROC);
9513 return MATCH_ERROR;
9516 return MATCH_YES;
9518 cleanup:
9519 gfc_free_formal_arglist (head);
9520 return MATCH_ERROR;
9524 /* Match a module procedure statement. Note that we have to modify
9525 symbols in the parent's namespace because the current one was there
9526 to receive symbols that are in an interface's formal argument list. */
9528 match
9529 gfc_match_modproc (void)
9531 char name[GFC_MAX_SYMBOL_LEN + 1];
9532 gfc_symbol *sym;
9533 match m;
9534 locus old_locus;
9535 gfc_namespace *module_ns;
9536 gfc_interface *old_interface_head, *interface;
9538 if (gfc_state_stack->state != COMP_INTERFACE
9539 || gfc_state_stack->previous == NULL
9540 || current_interface.type == INTERFACE_NAMELESS
9541 || current_interface.type == INTERFACE_ABSTRACT)
9543 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9544 "interface");
9545 return MATCH_ERROR;
9548 module_ns = gfc_current_ns->parent;
9549 for (; module_ns; module_ns = module_ns->parent)
9550 if (module_ns->proc_name->attr.flavor == FL_MODULE
9551 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9552 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9553 && !module_ns->proc_name->attr.contained))
9554 break;
9556 if (module_ns == NULL)
9557 return MATCH_ERROR;
9559 /* Store the current state of the interface. We will need it if we
9560 end up with a syntax error and need to recover. */
9561 old_interface_head = gfc_current_interface_head ();
9563 /* Check if the F2008 optional double colon appears. */
9564 gfc_gobble_whitespace ();
9565 old_locus = gfc_current_locus;
9566 if (gfc_match ("::") == MATCH_YES)
9568 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9569 "MODULE PROCEDURE statement at %L", &old_locus))
9570 return MATCH_ERROR;
9572 else
9573 gfc_current_locus = old_locus;
9575 for (;;)
9577 bool last = false;
9578 old_locus = gfc_current_locus;
9580 m = gfc_match_name (name);
9581 if (m == MATCH_NO)
9582 goto syntax;
9583 if (m != MATCH_YES)
9584 return MATCH_ERROR;
9586 /* Check for syntax error before starting to add symbols to the
9587 current namespace. */
9588 if (gfc_match_eos () == MATCH_YES)
9589 last = true;
9591 if (!last && gfc_match_char (',') != MATCH_YES)
9592 goto syntax;
9594 /* Now we're sure the syntax is valid, we process this item
9595 further. */
9596 if (gfc_get_symbol (name, module_ns, &sym))
9597 return MATCH_ERROR;
9599 if (sym->attr.intrinsic)
9601 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9602 "PROCEDURE", &old_locus);
9603 return MATCH_ERROR;
9606 if (sym->attr.proc != PROC_MODULE
9607 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9608 return MATCH_ERROR;
9610 if (!gfc_add_interface (sym))
9611 return MATCH_ERROR;
9613 sym->attr.mod_proc = 1;
9614 sym->declared_at = old_locus;
9616 if (last)
9617 break;
9620 return MATCH_YES;
9622 syntax:
9623 /* Restore the previous state of the interface. */
9624 interface = gfc_current_interface_head ();
9625 gfc_set_current_interface_head (old_interface_head);
9627 /* Free the new interfaces. */
9628 while (interface != old_interface_head)
9630 gfc_interface *i = interface->next;
9631 free (interface);
9632 interface = i;
9635 /* And issue a syntax error. */
9636 gfc_syntax_error (ST_MODULE_PROC);
9637 return MATCH_ERROR;
9641 /* Check a derived type that is being extended. */
9643 static gfc_symbol*
9644 check_extended_derived_type (char *name)
9646 gfc_symbol *extended;
9648 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9650 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9651 return NULL;
9654 extended = gfc_find_dt_in_generic (extended);
9656 /* F08:C428. */
9657 if (!extended)
9659 gfc_error ("Symbol %qs at %C has not been previously defined", name);
9660 return NULL;
9663 if (extended->attr.flavor != FL_DERIVED)
9665 gfc_error ("%qs in EXTENDS expression at %C is not a "
9666 "derived type", name);
9667 return NULL;
9670 if (extended->attr.is_bind_c)
9672 gfc_error ("%qs cannot be extended at %C because it "
9673 "is BIND(C)", extended->name);
9674 return NULL;
9677 if (extended->attr.sequence)
9679 gfc_error ("%qs cannot be extended at %C because it "
9680 "is a SEQUENCE type", extended->name);
9681 return NULL;
9684 return extended;
9688 /* Match the optional attribute specifiers for a type declaration.
9689 Return MATCH_ERROR if an error is encountered in one of the handled
9690 attributes (public, private, bind(c)), MATCH_NO if what's found is
9691 not a handled attribute, and MATCH_YES otherwise. TODO: More error
9692 checking on attribute conflicts needs to be done. */
9694 match
9695 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
9697 /* See if the derived type is marked as private. */
9698 if (gfc_match (" , private") == MATCH_YES)
9700 if (gfc_current_state () != COMP_MODULE)
9702 gfc_error ("Derived type at %C can only be PRIVATE in the "
9703 "specification part of a module");
9704 return MATCH_ERROR;
9707 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
9708 return MATCH_ERROR;
9710 else if (gfc_match (" , public") == MATCH_YES)
9712 if (gfc_current_state () != COMP_MODULE)
9714 gfc_error ("Derived type at %C can only be PUBLIC in the "
9715 "specification part of a module");
9716 return MATCH_ERROR;
9719 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
9720 return MATCH_ERROR;
9722 else if (gfc_match (" , bind ( c )") == MATCH_YES)
9724 /* If the type is defined to be bind(c) it then needs to make
9725 sure that all fields are interoperable. This will
9726 need to be a semantic check on the finished derived type.
9727 See 15.2.3 (lines 9-12) of F2003 draft. */
9728 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
9729 return MATCH_ERROR;
9731 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
9733 else if (gfc_match (" , abstract") == MATCH_YES)
9735 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
9736 return MATCH_ERROR;
9738 if (!gfc_add_abstract (attr, &gfc_current_locus))
9739 return MATCH_ERROR;
9741 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
9743 if (!gfc_add_extension (attr, &gfc_current_locus))
9744 return MATCH_ERROR;
9746 else
9747 return MATCH_NO;
9749 /* If we get here, something matched. */
9750 return MATCH_YES;
9754 /* Common function for type declaration blocks similar to derived types, such
9755 as STRUCTURES and MAPs. Unlike derived types, a structure type
9756 does NOT have a generic symbol matching the name given by the user.
9757 STRUCTUREs can share names with variables and PARAMETERs so we must allow
9758 for the creation of an independent symbol.
9759 Other parameters are a message to prefix errors with, the name of the new
9760 type to be created, and the flavor to add to the resulting symbol. */
9762 static bool
9763 get_struct_decl (const char *name, sym_flavor fl, locus *decl,
9764 gfc_symbol **result)
9766 gfc_symbol *sym;
9767 locus where;
9769 gcc_assert (name[0] == (char) TOUPPER (name[0]));
9771 if (decl)
9772 where = *decl;
9773 else
9774 where = gfc_current_locus;
9776 if (gfc_get_symbol (name, NULL, &sym))
9777 return false;
9779 if (!sym)
9781 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
9782 return false;
9785 if (sym->components != NULL || sym->attr.zero_comp)
9787 gfc_error ("Type definition of %qs at %C was already defined at %L",
9788 sym->name, &sym->declared_at);
9789 return false;
9792 sym->declared_at = where;
9794 if (sym->attr.flavor != fl
9795 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
9796 return false;
9798 if (!sym->hash_value)
9799 /* Set the hash for the compound name for this type. */
9800 sym->hash_value = gfc_hash_value (sym);
9802 /* Normally the type is expected to have been completely parsed by the time
9803 a field declaration with this type is seen. For unions, maps, and nested
9804 structure declarations, we need to indicate that it is okay that we
9805 haven't seen any components yet. This will be updated after the structure
9806 is fully parsed. */
9807 sym->attr.zero_comp = 0;
9809 /* Structures always act like derived-types with the SEQUENCE attribute */
9810 gfc_add_sequence (&sym->attr, sym->name, NULL);
9812 if (result) *result = sym;
9814 return true;
9818 /* Match the opening of a MAP block. Like a struct within a union in C;
9819 behaves identical to STRUCTURE blocks. */
9821 match
9822 gfc_match_map (void)
9824 /* Counter used to give unique internal names to map structures. */
9825 static unsigned int gfc_map_id = 0;
9826 char name[GFC_MAX_SYMBOL_LEN + 1];
9827 gfc_symbol *sym;
9828 locus old_loc;
9830 old_loc = gfc_current_locus;
9832 if (gfc_match_eos () != MATCH_YES)
9834 gfc_error ("Junk after MAP statement at %C");
9835 gfc_current_locus = old_loc;
9836 return MATCH_ERROR;
9839 /* Map blocks are anonymous so we make up unique names for the symbol table
9840 which are invalid Fortran identifiers. */
9841 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
9843 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
9844 return MATCH_ERROR;
9846 gfc_new_block = sym;
9848 return MATCH_YES;
9852 /* Match the opening of a UNION block. */
9854 match
9855 gfc_match_union (void)
9857 /* Counter used to give unique internal names to union types. */
9858 static unsigned int gfc_union_id = 0;
9859 char name[GFC_MAX_SYMBOL_LEN + 1];
9860 gfc_symbol *sym;
9861 locus old_loc;
9863 old_loc = gfc_current_locus;
9865 if (gfc_match_eos () != MATCH_YES)
9867 gfc_error ("Junk after UNION statement at %C");
9868 gfc_current_locus = old_loc;
9869 return MATCH_ERROR;
9872 /* Unions are anonymous so we make up unique names for the symbol table
9873 which are invalid Fortran identifiers. */
9874 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
9876 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
9877 return MATCH_ERROR;
9879 gfc_new_block = sym;
9881 return MATCH_YES;
9885 /* Match the beginning of a STRUCTURE declaration. This is similar to
9886 matching the beginning of a derived type declaration with a few
9887 twists. The resulting type symbol has no access control or other
9888 interesting attributes. */
9890 match
9891 gfc_match_structure_decl (void)
9893 /* Counter used to give unique internal names to anonymous structures. */
9894 static unsigned int gfc_structure_id = 0;
9895 char name[GFC_MAX_SYMBOL_LEN + 1];
9896 gfc_symbol *sym;
9897 match m;
9898 locus where;
9900 if (!flag_dec_structure)
9902 gfc_error ("%s at %C is a DEC extension, enable with "
9903 "%<-fdec-structure%>",
9904 "STRUCTURE");
9905 return MATCH_ERROR;
9908 name[0] = '\0';
9910 m = gfc_match (" /%n/", name);
9911 if (m != MATCH_YES)
9913 /* Non-nested structure declarations require a structure name. */
9914 if (!gfc_comp_struct (gfc_current_state ()))
9916 gfc_error ("Structure name expected in non-nested structure "
9917 "declaration at %C");
9918 return MATCH_ERROR;
9920 /* This is an anonymous structure; make up a unique name for it
9921 (upper-case letters never make it to symbol names from the source).
9922 The important thing is initializing the type variable
9923 and setting gfc_new_symbol, which is immediately used by
9924 parse_structure () and variable_decl () to add components of
9925 this type. */
9926 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
9929 where = gfc_current_locus;
9930 /* No field list allowed after non-nested structure declaration. */
9931 if (!gfc_comp_struct (gfc_current_state ())
9932 && gfc_match_eos () != MATCH_YES)
9934 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
9935 return MATCH_ERROR;
9938 /* Make sure the name is not the name of an intrinsic type. */
9939 if (gfc_is_intrinsic_typename (name))
9941 gfc_error ("Structure name %qs at %C cannot be the same as an"
9942 " intrinsic type", name);
9943 return MATCH_ERROR;
9946 /* Store the actual type symbol for the structure with an upper-case first
9947 letter (an invalid Fortran identifier). */
9949 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
9950 return MATCH_ERROR;
9952 gfc_new_block = sym;
9953 return MATCH_YES;
9957 /* This function does some work to determine which matcher should be used to
9958 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
9959 * as an alias for PRINT from derived type declarations, TYPE IS statements,
9960 * and [parameterized] derived type declarations. */
9962 match
9963 gfc_match_type (gfc_statement *st)
9965 char name[GFC_MAX_SYMBOL_LEN + 1];
9966 match m;
9967 locus old_loc;
9969 /* Requires -fdec. */
9970 if (!flag_dec)
9971 return MATCH_NO;
9973 m = gfc_match ("type");
9974 if (m != MATCH_YES)
9975 return m;
9976 /* If we already have an error in the buffer, it is probably from failing to
9977 * match a derived type data declaration. Let it happen. */
9978 else if (gfc_error_flag_test ())
9979 return MATCH_NO;
9981 old_loc = gfc_current_locus;
9982 *st = ST_NONE;
9984 /* If we see an attribute list before anything else it's definitely a derived
9985 * type declaration. */
9986 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
9987 goto derived;
9989 /* By now "TYPE" has already been matched. If we do not see a name, this may
9990 * be something like "TYPE *" or "TYPE <fmt>". */
9991 m = gfc_match_name (name);
9992 if (m != MATCH_YES)
9994 /* Let print match if it can, otherwise throw an error from
9995 * gfc_match_derived_decl. */
9996 gfc_current_locus = old_loc;
9997 if (gfc_match_print () == MATCH_YES)
9999 *st = ST_WRITE;
10000 return MATCH_YES;
10002 goto derived;
10005 /* Check for EOS. */
10006 if (gfc_match_eos () == MATCH_YES)
10008 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
10009 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
10010 * Otherwise if gfc_match_derived_decl fails it's probably an existing
10011 * symbol which can be printed. */
10012 gfc_current_locus = old_loc;
10013 m = gfc_match_derived_decl ();
10014 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
10016 *st = ST_DERIVED_DECL;
10017 return m;
10020 else
10022 /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
10023 like <type name(parameter)>. */
10024 gfc_gobble_whitespace ();
10025 bool paren = gfc_peek_ascii_char () == '(';
10026 if (paren)
10028 if (strcmp ("is", name) == 0)
10029 goto typeis;
10030 else
10031 goto derived;
10035 /* Treat TYPE... like PRINT... */
10036 gfc_current_locus = old_loc;
10037 *st = ST_WRITE;
10038 return gfc_match_print ();
10040 derived:
10041 gfc_current_locus = old_loc;
10042 *st = ST_DERIVED_DECL;
10043 return gfc_match_derived_decl ();
10045 typeis:
10046 gfc_current_locus = old_loc;
10047 *st = ST_TYPE_IS;
10048 return gfc_match_type_is ();
10052 /* Match the beginning of a derived type declaration. If a type name
10053 was the result of a function, then it is possible to have a symbol
10054 already to be known as a derived type yet have no components. */
10056 match
10057 gfc_match_derived_decl (void)
10059 char name[GFC_MAX_SYMBOL_LEN + 1];
10060 char parent[GFC_MAX_SYMBOL_LEN + 1];
10061 symbol_attribute attr;
10062 gfc_symbol *sym, *gensym;
10063 gfc_symbol *extended;
10064 match m;
10065 match is_type_attr_spec = MATCH_NO;
10066 bool seen_attr = false;
10067 gfc_interface *intr = NULL, *head;
10068 bool parameterized_type = false;
10069 bool seen_colons = false;
10071 if (gfc_comp_struct (gfc_current_state ()))
10072 return MATCH_NO;
10074 name[0] = '\0';
10075 parent[0] = '\0';
10076 gfc_clear_attr (&attr);
10077 extended = NULL;
10081 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
10082 if (is_type_attr_spec == MATCH_ERROR)
10083 return MATCH_ERROR;
10084 if (is_type_attr_spec == MATCH_YES)
10085 seen_attr = true;
10086 } while (is_type_attr_spec == MATCH_YES);
10088 /* Deal with derived type extensions. The extension attribute has
10089 been added to 'attr' but now the parent type must be found and
10090 checked. */
10091 if (parent[0])
10092 extended = check_extended_derived_type (parent);
10094 if (parent[0] && !extended)
10095 return MATCH_ERROR;
10097 m = gfc_match (" ::");
10098 if (m == MATCH_YES)
10100 seen_colons = true;
10102 else if (seen_attr)
10104 gfc_error ("Expected :: in TYPE definition at %C");
10105 return MATCH_ERROR;
10108 m = gfc_match (" %n ", name);
10109 if (m != MATCH_YES)
10110 return m;
10112 /* Make sure that we don't identify TYPE IS (...) as a parameterized
10113 derived type named 'is'.
10114 TODO Expand the check, when 'name' = "is" by matching " (tname) "
10115 and checking if this is a(n intrinsic) typename. his picks up
10116 misplaced TYPE IS statements such as in select_type_1.f03. */
10117 if (gfc_peek_ascii_char () == '(')
10119 if (gfc_current_state () == COMP_SELECT_TYPE
10120 || (!seen_colons && !strcmp (name, "is")))
10121 return MATCH_NO;
10122 parameterized_type = true;
10125 m = gfc_match_eos ();
10126 if (m != MATCH_YES && !parameterized_type)
10127 return m;
10129 /* Make sure the name is not the name of an intrinsic type. */
10130 if (gfc_is_intrinsic_typename (name))
10132 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
10133 "type", name);
10134 return MATCH_ERROR;
10137 if (gfc_get_symbol (name, NULL, &gensym))
10138 return MATCH_ERROR;
10140 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
10142 if (gensym->ts.u.derived)
10143 gfc_error ("Derived type name %qs at %C already has a basic type "
10144 "of %s", gensym->name, gfc_typename (&gensym->ts));
10145 else
10146 gfc_error ("Derived type name %qs at %C already has a basic type",
10147 gensym->name);
10148 return MATCH_ERROR;
10151 if (!gensym->attr.generic
10152 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
10153 return MATCH_ERROR;
10155 if (!gensym->attr.function
10156 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
10157 return MATCH_ERROR;
10159 sym = gfc_find_dt_in_generic (gensym);
10161 if (sym && (sym->components != NULL || sym->attr.zero_comp))
10163 gfc_error ("Derived type definition of %qs at %C has already been "
10164 "defined", sym->name);
10165 return MATCH_ERROR;
10168 if (!sym)
10170 /* Use upper case to save the actual derived-type symbol. */
10171 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
10172 sym->name = gfc_get_string ("%s", gensym->name);
10173 head = gensym->generic;
10174 intr = gfc_get_interface ();
10175 intr->sym = sym;
10176 intr->where = gfc_current_locus;
10177 intr->sym->declared_at = gfc_current_locus;
10178 intr->next = head;
10179 gensym->generic = intr;
10180 gensym->attr.if_source = IFSRC_DECL;
10183 /* The symbol may already have the derived attribute without the
10184 components. The ways this can happen is via a function
10185 definition, an INTRINSIC statement or a subtype in another
10186 derived type that is a pointer. The first part of the AND clause
10187 is true if the symbol is not the return value of a function. */
10188 if (sym->attr.flavor != FL_DERIVED
10189 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
10190 return MATCH_ERROR;
10192 if (attr.access != ACCESS_UNKNOWN
10193 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
10194 return MATCH_ERROR;
10195 else if (sym->attr.access == ACCESS_UNKNOWN
10196 && gensym->attr.access != ACCESS_UNKNOWN
10197 && !gfc_add_access (&sym->attr, gensym->attr.access,
10198 sym->name, NULL))
10199 return MATCH_ERROR;
10201 if (sym->attr.access != ACCESS_UNKNOWN
10202 && gensym->attr.access == ACCESS_UNKNOWN)
10203 gensym->attr.access = sym->attr.access;
10205 /* See if the derived type was labeled as bind(c). */
10206 if (attr.is_bind_c != 0)
10207 sym->attr.is_bind_c = attr.is_bind_c;
10209 /* Construct the f2k_derived namespace if it is not yet there. */
10210 if (!sym->f2k_derived)
10211 sym->f2k_derived = gfc_get_namespace (NULL, 0);
10213 if (parameterized_type)
10215 /* Ignore error or mismatches by going to the end of the statement
10216 in order to avoid the component declarations causing problems. */
10217 m = gfc_match_formal_arglist (sym, 0, 0, true);
10218 if (m != MATCH_YES)
10219 gfc_error_recovery ();
10220 m = gfc_match_eos ();
10221 if (m != MATCH_YES)
10223 gfc_error_recovery ();
10224 gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10226 sym->attr.pdt_template = 1;
10229 if (extended && !sym->components)
10231 gfc_component *p;
10232 gfc_formal_arglist *f, *g, *h;
10234 /* Add the extended derived type as the first component. */
10235 gfc_add_component (sym, parent, &p);
10236 extended->refs++;
10237 gfc_set_sym_referenced (extended);
10239 p->ts.type = BT_DERIVED;
10240 p->ts.u.derived = extended;
10241 p->initializer = gfc_default_initializer (&p->ts);
10243 /* Set extension level. */
10244 if (extended->attr.extension == 255)
10246 /* Since the extension field is 8 bit wide, we can only have
10247 up to 255 extension levels. */
10248 gfc_error ("Maximum extension level reached with type %qs at %L",
10249 extended->name, &extended->declared_at);
10250 return MATCH_ERROR;
10252 sym->attr.extension = extended->attr.extension + 1;
10254 /* Provide the links between the extended type and its extension. */
10255 if (!extended->f2k_derived)
10256 extended->f2k_derived = gfc_get_namespace (NULL, 0);
10258 /* Copy the extended type-param-name-list from the extended type,
10259 append those of the extension and add the whole lot to the
10260 extension. */
10261 if (extended->attr.pdt_template)
10263 g = h = NULL;
10264 sym->attr.pdt_template = 1;
10265 for (f = extended->formal; f; f = f->next)
10267 if (f == extended->formal)
10269 g = gfc_get_formal_arglist ();
10270 h = g;
10272 else
10274 g->next = gfc_get_formal_arglist ();
10275 g = g->next;
10277 g->sym = f->sym;
10279 g->next = sym->formal;
10280 sym->formal = h;
10284 if (!sym->hash_value)
10285 /* Set the hash for the compound name for this type. */
10286 sym->hash_value = gfc_hash_value (sym);
10288 /* Take over the ABSTRACT attribute. */
10289 sym->attr.abstract = attr.abstract;
10291 gfc_new_block = sym;
10293 return MATCH_YES;
10297 /* Cray Pointees can be declared as:
10298 pointer (ipt, a (n,m,...,*)) */
10300 match
10301 gfc_mod_pointee_as (gfc_array_spec *as)
10303 as->cray_pointee = true; /* This will be useful to know later. */
10304 if (as->type == AS_ASSUMED_SIZE)
10305 as->cp_was_assumed = true;
10306 else if (as->type == AS_ASSUMED_SHAPE)
10308 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10309 return MATCH_ERROR;
10311 return MATCH_YES;
10315 /* Match the enum definition statement, here we are trying to match
10316 the first line of enum definition statement.
10317 Returns MATCH_YES if match is found. */
10319 match
10320 gfc_match_enum (void)
10322 match m;
10324 m = gfc_match_eos ();
10325 if (m != MATCH_YES)
10326 return m;
10328 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
10329 return MATCH_ERROR;
10331 return MATCH_YES;
10335 /* Returns an initializer whose value is one higher than the value of the
10336 LAST_INITIALIZER argument. If the argument is NULL, the
10337 initializers value will be set to zero. The initializer's kind
10338 will be set to gfc_c_int_kind.
10340 If -fshort-enums is given, the appropriate kind will be selected
10341 later after all enumerators have been parsed. A warning is issued
10342 here if an initializer exceeds gfc_c_int_kind. */
10344 static gfc_expr *
10345 enum_initializer (gfc_expr *last_initializer, locus where)
10347 gfc_expr *result;
10348 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
10350 mpz_init (result->value.integer);
10352 if (last_initializer != NULL)
10354 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10355 result->where = last_initializer->where;
10357 if (gfc_check_integer_range (result->value.integer,
10358 gfc_c_int_kind) != ARITH_OK)
10360 gfc_error ("Enumerator exceeds the C integer type at %C");
10361 return NULL;
10364 else
10366 /* Control comes here, if it's the very first enumerator and no
10367 initializer has been given. It will be initialized to zero. */
10368 mpz_set_si (result->value.integer, 0);
10371 return result;
10375 /* Match a variable name with an optional initializer. When this
10376 subroutine is called, a variable is expected to be parsed next.
10377 Depending on what is happening at the moment, updates either the
10378 symbol table or the current interface. */
10380 static match
10381 enumerator_decl (void)
10383 char name[GFC_MAX_SYMBOL_LEN + 1];
10384 gfc_expr *initializer;
10385 gfc_array_spec *as = NULL;
10386 gfc_symbol *sym;
10387 locus var_locus;
10388 match m;
10389 bool t;
10390 locus old_locus;
10392 initializer = NULL;
10393 old_locus = gfc_current_locus;
10395 /* When we get here, we've just matched a list of attributes and
10396 maybe a type and a double colon. The next thing we expect to see
10397 is the name of the symbol. */
10398 m = gfc_match_name (name);
10399 if (m != MATCH_YES)
10400 goto cleanup;
10402 var_locus = gfc_current_locus;
10404 /* OK, we've successfully matched the declaration. Now put the
10405 symbol in the current namespace. If we fail to create the symbol,
10406 bail out. */
10407 if (!build_sym (name, NULL, false, &as, &var_locus))
10409 m = MATCH_ERROR;
10410 goto cleanup;
10413 /* The double colon must be present in order to have initializers.
10414 Otherwise the statement is ambiguous with an assignment statement. */
10415 if (colon_seen)
10417 if (gfc_match_char ('=') == MATCH_YES)
10419 m = gfc_match_init_expr (&initializer);
10420 if (m == MATCH_NO)
10422 gfc_error ("Expected an initialization expression at %C");
10423 m = MATCH_ERROR;
10426 if (m != MATCH_YES)
10427 goto cleanup;
10431 /* If we do not have an initializer, the initialization value of the
10432 previous enumerator (stored in last_initializer) is incremented
10433 by 1 and is used to initialize the current enumerator. */
10434 if (initializer == NULL)
10435 initializer = enum_initializer (last_initializer, old_locus);
10437 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10439 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10440 &var_locus);
10441 m = MATCH_ERROR;
10442 goto cleanup;
10445 /* Store this current initializer, for the next enumerator variable
10446 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10447 use last_initializer below. */
10448 last_initializer = initializer;
10449 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10451 /* Maintain enumerator history. */
10452 gfc_find_symbol (name, NULL, 0, &sym);
10453 create_enum_history (sym, last_initializer);
10455 return (t) ? MATCH_YES : MATCH_ERROR;
10457 cleanup:
10458 /* Free stuff up and return. */
10459 gfc_free_expr (initializer);
10461 return m;
10465 /* Match the enumerator definition statement. */
10467 match
10468 gfc_match_enumerator_def (void)
10470 match m;
10471 bool t;
10473 gfc_clear_ts (&current_ts);
10475 m = gfc_match (" enumerator");
10476 if (m != MATCH_YES)
10477 return m;
10479 m = gfc_match (" :: ");
10480 if (m == MATCH_ERROR)
10481 return m;
10483 colon_seen = (m == MATCH_YES);
10485 if (gfc_current_state () != COMP_ENUM)
10487 gfc_error ("ENUM definition statement expected before %C");
10488 gfc_free_enum_history ();
10489 return MATCH_ERROR;
10492 (&current_ts)->type = BT_INTEGER;
10493 (&current_ts)->kind = gfc_c_int_kind;
10495 gfc_clear_attr (&current_attr);
10496 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10497 if (!t)
10499 m = MATCH_ERROR;
10500 goto cleanup;
10503 for (;;)
10505 m = enumerator_decl ();
10506 if (m == MATCH_ERROR)
10508 gfc_free_enum_history ();
10509 goto cleanup;
10511 if (m == MATCH_NO)
10512 break;
10514 if (gfc_match_eos () == MATCH_YES)
10515 goto cleanup;
10516 if (gfc_match_char (',') != MATCH_YES)
10517 break;
10520 if (gfc_current_state () == COMP_ENUM)
10522 gfc_free_enum_history ();
10523 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10524 m = MATCH_ERROR;
10527 cleanup:
10528 gfc_free_array_spec (current_as);
10529 current_as = NULL;
10530 return m;
10535 /* Match binding attributes. */
10537 static match
10538 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10540 bool found_passing = false;
10541 bool seen_ptr = false;
10542 match m = MATCH_YES;
10544 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
10545 this case the defaults are in there. */
10546 ba->access = ACCESS_UNKNOWN;
10547 ba->pass_arg = NULL;
10548 ba->pass_arg_num = 0;
10549 ba->nopass = 0;
10550 ba->non_overridable = 0;
10551 ba->deferred = 0;
10552 ba->ppc = ppc;
10554 /* If we find a comma, we believe there are binding attributes. */
10555 m = gfc_match_char (',');
10556 if (m == MATCH_NO)
10557 goto done;
10561 /* Access specifier. */
10563 m = gfc_match (" public");
10564 if (m == MATCH_ERROR)
10565 goto error;
10566 if (m == MATCH_YES)
10568 if (ba->access != ACCESS_UNKNOWN)
10570 gfc_error ("Duplicate access-specifier at %C");
10571 goto error;
10574 ba->access = ACCESS_PUBLIC;
10575 continue;
10578 m = gfc_match (" private");
10579 if (m == MATCH_ERROR)
10580 goto error;
10581 if (m == MATCH_YES)
10583 if (ba->access != ACCESS_UNKNOWN)
10585 gfc_error ("Duplicate access-specifier at %C");
10586 goto error;
10589 ba->access = ACCESS_PRIVATE;
10590 continue;
10593 /* If inside GENERIC, the following is not allowed. */
10594 if (!generic)
10597 /* NOPASS flag. */
10598 m = gfc_match (" nopass");
10599 if (m == MATCH_ERROR)
10600 goto error;
10601 if (m == MATCH_YES)
10603 if (found_passing)
10605 gfc_error ("Binding attributes already specify passing,"
10606 " illegal NOPASS at %C");
10607 goto error;
10610 found_passing = true;
10611 ba->nopass = 1;
10612 continue;
10615 /* PASS possibly including argument. */
10616 m = gfc_match (" pass");
10617 if (m == MATCH_ERROR)
10618 goto error;
10619 if (m == MATCH_YES)
10621 char arg[GFC_MAX_SYMBOL_LEN + 1];
10623 if (found_passing)
10625 gfc_error ("Binding attributes already specify passing,"
10626 " illegal PASS at %C");
10627 goto error;
10630 m = gfc_match (" ( %n )", arg);
10631 if (m == MATCH_ERROR)
10632 goto error;
10633 if (m == MATCH_YES)
10634 ba->pass_arg = gfc_get_string ("%s", arg);
10635 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10637 found_passing = true;
10638 ba->nopass = 0;
10639 continue;
10642 if (ppc)
10644 /* POINTER flag. */
10645 m = gfc_match (" pointer");
10646 if (m == MATCH_ERROR)
10647 goto error;
10648 if (m == MATCH_YES)
10650 if (seen_ptr)
10652 gfc_error ("Duplicate POINTER attribute at %C");
10653 goto error;
10656 seen_ptr = true;
10657 continue;
10660 else
10662 /* NON_OVERRIDABLE flag. */
10663 m = gfc_match (" non_overridable");
10664 if (m == MATCH_ERROR)
10665 goto error;
10666 if (m == MATCH_YES)
10668 if (ba->non_overridable)
10670 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
10671 goto error;
10674 ba->non_overridable = 1;
10675 continue;
10678 /* DEFERRED flag. */
10679 m = gfc_match (" deferred");
10680 if (m == MATCH_ERROR)
10681 goto error;
10682 if (m == MATCH_YES)
10684 if (ba->deferred)
10686 gfc_error ("Duplicate DEFERRED at %C");
10687 goto error;
10690 ba->deferred = 1;
10691 continue;
10697 /* Nothing matching found. */
10698 if (generic)
10699 gfc_error ("Expected access-specifier at %C");
10700 else
10701 gfc_error ("Expected binding attribute at %C");
10702 goto error;
10704 while (gfc_match_char (',') == MATCH_YES);
10706 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
10707 if (ba->non_overridable && ba->deferred)
10709 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
10710 goto error;
10713 m = MATCH_YES;
10715 done:
10716 if (ba->access == ACCESS_UNKNOWN)
10717 ba->access = ppc ? gfc_current_block()->component_access
10718 : gfc_typebound_default_access;
10720 if (ppc && !seen_ptr)
10722 gfc_error ("POINTER attribute is required for procedure pointer component"
10723 " at %C");
10724 goto error;
10727 return m;
10729 error:
10730 return MATCH_ERROR;
10734 /* Match a PROCEDURE specific binding inside a derived type. */
10736 static match
10737 match_procedure_in_type (void)
10739 char name[GFC_MAX_SYMBOL_LEN + 1];
10740 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
10741 char* target = NULL, *ifc = NULL;
10742 gfc_typebound_proc tb;
10743 bool seen_colons;
10744 bool seen_attrs;
10745 match m;
10746 gfc_symtree* stree;
10747 gfc_namespace* ns;
10748 gfc_symbol* block;
10749 int num;
10751 /* Check current state. */
10752 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
10753 block = gfc_state_stack->previous->sym;
10754 gcc_assert (block);
10756 /* Try to match PROCEDURE(interface). */
10757 if (gfc_match (" (") == MATCH_YES)
10759 m = gfc_match_name (target_buf);
10760 if (m == MATCH_ERROR)
10761 return m;
10762 if (m != MATCH_YES)
10764 gfc_error ("Interface-name expected after %<(%> at %C");
10765 return MATCH_ERROR;
10768 if (gfc_match (" )") != MATCH_YES)
10770 gfc_error ("%<)%> expected at %C");
10771 return MATCH_ERROR;
10774 ifc = target_buf;
10777 /* Construct the data structure. */
10778 memset (&tb, 0, sizeof (tb));
10779 tb.where = gfc_current_locus;
10781 /* Match binding attributes. */
10782 m = match_binding_attributes (&tb, false, false);
10783 if (m == MATCH_ERROR)
10784 return m;
10785 seen_attrs = (m == MATCH_YES);
10787 /* Check that attribute DEFERRED is given if an interface is specified. */
10788 if (tb.deferred && !ifc)
10790 gfc_error ("Interface must be specified for DEFERRED binding at %C");
10791 return MATCH_ERROR;
10793 if (ifc && !tb.deferred)
10795 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
10796 return MATCH_ERROR;
10799 /* Match the colons. */
10800 m = gfc_match (" ::");
10801 if (m == MATCH_ERROR)
10802 return m;
10803 seen_colons = (m == MATCH_YES);
10804 if (seen_attrs && !seen_colons)
10806 gfc_error ("Expected %<::%> after binding-attributes at %C");
10807 return MATCH_ERROR;
10810 /* Match the binding names. */
10811 for(num=1;;num++)
10813 m = gfc_match_name (name);
10814 if (m == MATCH_ERROR)
10815 return m;
10816 if (m == MATCH_NO)
10818 gfc_error ("Expected binding name at %C");
10819 return MATCH_ERROR;
10822 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
10823 return MATCH_ERROR;
10825 /* Try to match the '=> target', if it's there. */
10826 target = ifc;
10827 m = gfc_match (" =>");
10828 if (m == MATCH_ERROR)
10829 return m;
10830 if (m == MATCH_YES)
10832 if (tb.deferred)
10834 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
10835 return MATCH_ERROR;
10838 if (!seen_colons)
10840 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
10841 " at %C");
10842 return MATCH_ERROR;
10845 m = gfc_match_name (target_buf);
10846 if (m == MATCH_ERROR)
10847 return m;
10848 if (m == MATCH_NO)
10850 gfc_error ("Expected binding target after %<=>%> at %C");
10851 return MATCH_ERROR;
10853 target = target_buf;
10856 /* If no target was found, it has the same name as the binding. */
10857 if (!target)
10858 target = name;
10860 /* Get the namespace to insert the symbols into. */
10861 ns = block->f2k_derived;
10862 gcc_assert (ns);
10864 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
10865 if (tb.deferred && !block->attr.abstract)
10867 gfc_error ("Type %qs containing DEFERRED binding at %C "
10868 "is not ABSTRACT", block->name);
10869 return MATCH_ERROR;
10872 /* See if we already have a binding with this name in the symtree which
10873 would be an error. If a GENERIC already targeted this binding, it may
10874 be already there but then typebound is still NULL. */
10875 stree = gfc_find_symtree (ns->tb_sym_root, name);
10876 if (stree && stree->n.tb)
10878 gfc_error ("There is already a procedure with binding name %qs for "
10879 "the derived type %qs at %C", name, block->name);
10880 return MATCH_ERROR;
10883 /* Insert it and set attributes. */
10885 if (!stree)
10887 stree = gfc_new_symtree (&ns->tb_sym_root, name);
10888 gcc_assert (stree);
10890 stree->n.tb = gfc_get_typebound_proc (&tb);
10892 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
10893 false))
10894 return MATCH_ERROR;
10895 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
10896 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
10897 target, &stree->n.tb->u.specific->n.sym->declared_at);
10899 if (gfc_match_eos () == MATCH_YES)
10900 return MATCH_YES;
10901 if (gfc_match_char (',') != MATCH_YES)
10902 goto syntax;
10905 syntax:
10906 gfc_error ("Syntax error in PROCEDURE statement at %C");
10907 return MATCH_ERROR;
10911 /* Match a GENERIC procedure binding inside a derived type. */
10913 match
10914 gfc_match_generic (void)
10916 char name[GFC_MAX_SYMBOL_LEN + 1];
10917 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
10918 gfc_symbol* block;
10919 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
10920 gfc_typebound_proc* tb;
10921 gfc_namespace* ns;
10922 interface_type op_type;
10923 gfc_intrinsic_op op;
10924 match m;
10926 /* Check current state. */
10927 if (gfc_current_state () == COMP_DERIVED)
10929 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
10930 return MATCH_ERROR;
10932 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
10933 return MATCH_NO;
10934 block = gfc_state_stack->previous->sym;
10935 ns = block->f2k_derived;
10936 gcc_assert (block && ns);
10938 memset (&tbattr, 0, sizeof (tbattr));
10939 tbattr.where = gfc_current_locus;
10941 /* See if we get an access-specifier. */
10942 m = match_binding_attributes (&tbattr, true, false);
10943 if (m == MATCH_ERROR)
10944 goto error;
10946 /* Now the colons, those are required. */
10947 if (gfc_match (" ::") != MATCH_YES)
10949 gfc_error ("Expected %<::%> at %C");
10950 goto error;
10953 /* Match the binding name; depending on type (operator / generic) format
10954 it for future error messages into bind_name. */
10956 m = gfc_match_generic_spec (&op_type, name, &op);
10957 if (m == MATCH_ERROR)
10958 return MATCH_ERROR;
10959 if (m == MATCH_NO)
10961 gfc_error ("Expected generic name or operator descriptor at %C");
10962 goto error;
10965 switch (op_type)
10967 case INTERFACE_GENERIC:
10968 case INTERFACE_DTIO:
10969 snprintf (bind_name, sizeof (bind_name), "%s", name);
10970 break;
10972 case INTERFACE_USER_OP:
10973 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
10974 break;
10976 case INTERFACE_INTRINSIC_OP:
10977 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
10978 gfc_op2string (op));
10979 break;
10981 case INTERFACE_NAMELESS:
10982 gfc_error ("Malformed GENERIC statement at %C");
10983 goto error;
10984 break;
10986 default:
10987 gcc_unreachable ();
10990 /* Match the required =>. */
10991 if (gfc_match (" =>") != MATCH_YES)
10993 gfc_error ("Expected %<=>%> at %C");
10994 goto error;
10997 /* Try to find existing GENERIC binding with this name / for this operator;
10998 if there is something, check that it is another GENERIC and then extend
10999 it rather than building a new node. Otherwise, create it and put it
11000 at the right position. */
11002 switch (op_type)
11004 case INTERFACE_DTIO:
11005 case INTERFACE_USER_OP:
11006 case INTERFACE_GENERIC:
11008 const bool is_op = (op_type == INTERFACE_USER_OP);
11009 gfc_symtree* st;
11011 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
11012 tb = st ? st->n.tb : NULL;
11013 break;
11016 case INTERFACE_INTRINSIC_OP:
11017 tb = ns->tb_op[op];
11018 break;
11020 default:
11021 gcc_unreachable ();
11024 if (tb)
11026 if (!tb->is_generic)
11028 gcc_assert (op_type == INTERFACE_GENERIC);
11029 gfc_error ("There's already a non-generic procedure with binding name"
11030 " %qs for the derived type %qs at %C",
11031 bind_name, block->name);
11032 goto error;
11035 if (tb->access != tbattr.access)
11037 gfc_error ("Binding at %C must have the same access as already"
11038 " defined binding %qs", bind_name);
11039 goto error;
11042 else
11044 tb = gfc_get_typebound_proc (NULL);
11045 tb->where = gfc_current_locus;
11046 tb->access = tbattr.access;
11047 tb->is_generic = 1;
11048 tb->u.generic = NULL;
11050 switch (op_type)
11052 case INTERFACE_DTIO:
11053 case INTERFACE_GENERIC:
11054 case INTERFACE_USER_OP:
11056 const bool is_op = (op_type == INTERFACE_USER_OP);
11057 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
11058 &ns->tb_sym_root, name);
11059 gcc_assert (st);
11060 st->n.tb = tb;
11062 break;
11065 case INTERFACE_INTRINSIC_OP:
11066 ns->tb_op[op] = tb;
11067 break;
11069 default:
11070 gcc_unreachable ();
11074 /* Now, match all following names as specific targets. */
11077 gfc_symtree* target_st;
11078 gfc_tbp_generic* target;
11080 m = gfc_match_name (name);
11081 if (m == MATCH_ERROR)
11082 goto error;
11083 if (m == MATCH_NO)
11085 gfc_error ("Expected specific binding name at %C");
11086 goto error;
11089 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
11091 /* See if this is a duplicate specification. */
11092 for (target = tb->u.generic; target; target = target->next)
11093 if (target_st == target->specific_st)
11095 gfc_error ("%qs already defined as specific binding for the"
11096 " generic %qs at %C", name, bind_name);
11097 goto error;
11100 target = gfc_get_tbp_generic ();
11101 target->specific_st = target_st;
11102 target->specific = NULL;
11103 target->next = tb->u.generic;
11104 target->is_operator = ((op_type == INTERFACE_USER_OP)
11105 || (op_type == INTERFACE_INTRINSIC_OP));
11106 tb->u.generic = target;
11108 while (gfc_match (" ,") == MATCH_YES);
11110 /* Here should be the end. */
11111 if (gfc_match_eos () != MATCH_YES)
11113 gfc_error ("Junk after GENERIC binding at %C");
11114 goto error;
11117 return MATCH_YES;
11119 error:
11120 return MATCH_ERROR;
11124 /* Match a FINAL declaration inside a derived type. */
11126 match
11127 gfc_match_final_decl (void)
11129 char name[GFC_MAX_SYMBOL_LEN + 1];
11130 gfc_symbol* sym;
11131 match m;
11132 gfc_namespace* module_ns;
11133 bool first, last;
11134 gfc_symbol* block;
11136 if (gfc_current_form == FORM_FREE)
11138 char c = gfc_peek_ascii_char ();
11139 if (!gfc_is_whitespace (c) && c != ':')
11140 return MATCH_NO;
11143 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
11145 if (gfc_current_form == FORM_FIXED)
11146 return MATCH_NO;
11148 gfc_error ("FINAL declaration at %C must be inside a derived type "
11149 "CONTAINS section");
11150 return MATCH_ERROR;
11153 block = gfc_state_stack->previous->sym;
11154 gcc_assert (block);
11156 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
11157 || gfc_state_stack->previous->previous->state != COMP_MODULE)
11159 gfc_error ("Derived type declaration with FINAL at %C must be in the"
11160 " specification part of a MODULE");
11161 return MATCH_ERROR;
11164 module_ns = gfc_current_ns;
11165 gcc_assert (module_ns);
11166 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
11168 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
11169 if (gfc_match (" ::") == MATCH_ERROR)
11170 return MATCH_ERROR;
11172 /* Match the sequence of procedure names. */
11173 first = true;
11174 last = false;
11177 gfc_finalizer* f;
11179 if (first && gfc_match_eos () == MATCH_YES)
11181 gfc_error ("Empty FINAL at %C");
11182 return MATCH_ERROR;
11185 m = gfc_match_name (name);
11186 if (m == MATCH_NO)
11188 gfc_error ("Expected module procedure name at %C");
11189 return MATCH_ERROR;
11191 else if (m != MATCH_YES)
11192 return MATCH_ERROR;
11194 if (gfc_match_eos () == MATCH_YES)
11195 last = true;
11196 if (!last && gfc_match_char (',') != MATCH_YES)
11198 gfc_error ("Expected %<,%> at %C");
11199 return MATCH_ERROR;
11202 if (gfc_get_symbol (name, module_ns, &sym))
11204 gfc_error ("Unknown procedure name %qs at %C", name);
11205 return MATCH_ERROR;
11208 /* Mark the symbol as module procedure. */
11209 if (sym->attr.proc != PROC_MODULE
11210 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
11211 return MATCH_ERROR;
11213 /* Check if we already have this symbol in the list, this is an error. */
11214 for (f = block->f2k_derived->finalizers; f; f = f->next)
11215 if (f->proc_sym == sym)
11217 gfc_error ("%qs at %C is already defined as FINAL procedure",
11218 name);
11219 return MATCH_ERROR;
11222 /* Add this symbol to the list of finalizers. */
11223 gcc_assert (block->f2k_derived);
11224 sym->refs++;
11225 f = XCNEW (gfc_finalizer);
11226 f->proc_sym = sym;
11227 f->proc_tree = NULL;
11228 f->where = gfc_current_locus;
11229 f->next = block->f2k_derived->finalizers;
11230 block->f2k_derived->finalizers = f;
11232 first = false;
11234 while (!last);
11236 return MATCH_YES;
11240 const ext_attr_t ext_attr_list[] = {
11241 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
11242 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
11243 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
11244 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
11245 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
11246 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
11247 { NULL, EXT_ATTR_LAST, NULL }
11250 /* Match a !GCC$ ATTRIBUTES statement of the form:
11251 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11252 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11254 TODO: We should support all GCC attributes using the same syntax for
11255 the attribute list, i.e. the list in C
11256 __attributes(( attribute-list ))
11257 matches then
11258 !GCC$ ATTRIBUTES attribute-list ::
11259 Cf. c-parser.c's c_parser_attributes; the data can then directly be
11260 saved into a TREE.
11262 As there is absolutely no risk of confusion, we should never return
11263 MATCH_NO. */
11264 match
11265 gfc_match_gcc_attributes (void)
11267 symbol_attribute attr;
11268 char name[GFC_MAX_SYMBOL_LEN + 1];
11269 unsigned id;
11270 gfc_symbol *sym;
11271 match m;
11273 gfc_clear_attr (&attr);
11274 for(;;)
11276 char ch;
11278 if (gfc_match_name (name) != MATCH_YES)
11279 return MATCH_ERROR;
11281 for (id = 0; id < EXT_ATTR_LAST; id++)
11282 if (strcmp (name, ext_attr_list[id].name) == 0)
11283 break;
11285 if (id == EXT_ATTR_LAST)
11287 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11288 return MATCH_ERROR;
11291 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
11292 return MATCH_ERROR;
11294 gfc_gobble_whitespace ();
11295 ch = gfc_next_ascii_char ();
11296 if (ch == ':')
11298 /* This is the successful exit condition for the loop. */
11299 if (gfc_next_ascii_char () == ':')
11300 break;
11303 if (ch == ',')
11304 continue;
11306 goto syntax;
11309 if (gfc_match_eos () == MATCH_YES)
11310 goto syntax;
11312 for(;;)
11314 m = gfc_match_name (name);
11315 if (m != MATCH_YES)
11316 return m;
11318 if (find_special (name, &sym, true))
11319 return MATCH_ERROR;
11321 sym->attr.ext_attr |= attr.ext_attr;
11323 if (gfc_match_eos () == MATCH_YES)
11324 break;
11326 if (gfc_match_char (',') != MATCH_YES)
11327 goto syntax;
11330 return MATCH_YES;
11332 syntax:
11333 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11334 return MATCH_ERROR;
11338 /* Match a !GCC$ UNROLL statement of the form:
11339 !GCC$ UNROLL n
11341 The parameter n is the number of times we are supposed to unroll.
11343 When we come here, we have already matched the !GCC$ UNROLL string. */
11344 match
11345 gfc_match_gcc_unroll (void)
11347 int value;
11349 if (gfc_match_small_int (&value) == MATCH_YES)
11351 if (value < 0 || value > USHRT_MAX)
11353 gfc_error ("%<GCC unroll%> directive requires a"
11354 " non-negative integral constant"
11355 " less than or equal to %u at %C",
11356 USHRT_MAX
11358 return MATCH_ERROR;
11360 if (gfc_match_eos () == MATCH_YES)
11362 directive_unroll = value == 0 ? 1 : value;
11363 return MATCH_YES;
11367 gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11368 return MATCH_ERROR;
11371 /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
11373 The parameter b is name of a middle-end built-in.
11374 FLAGS is optional and must be one of:
11375 - (inbranch)
11376 - (notinbranch)
11378 IF('target') is optional and TARGET is a name of a multilib ABI.
11380 When we come here, we have already matched the !GCC$ builtin string. */
11382 match
11383 gfc_match_gcc_builtin (void)
11385 char builtin[GFC_MAX_SYMBOL_LEN + 1];
11386 char target[GFC_MAX_SYMBOL_LEN + 1];
11388 if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
11389 return MATCH_ERROR;
11391 gfc_simd_clause clause = SIMD_NONE;
11392 if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
11393 clause = SIMD_NOTINBRANCH;
11394 else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
11395 clause = SIMD_INBRANCH;
11397 if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
11399 const char *abi = targetm.get_multilib_abi_name ();
11400 if (abi == NULL || strcmp (abi, target) != 0)
11401 return MATCH_YES;
11404 if (gfc_vectorized_builtins == NULL)
11405 gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
11407 char *r = XNEWVEC (char, strlen (builtin) + 32);
11408 sprintf (r, "__builtin_%s", builtin);
11410 bool existed;
11411 int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
11412 value |= clause;
11413 if (existed)
11414 free (r);
11416 return MATCH_YES;