Daily bump.
[official-gcc.git] / gcc / fortran / decl.c
blob0e0364cb54eb9e00eb4d6bb43afaedcbff1165d5
1 /* Declaration statement matcher
2 Copyright (C) 2002-2014 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 "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "flags.h"
28 #include "constructor.h"
29 #include "tree.h"
30 #include "stringpool.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts;
54 static symbol_attribute current_attr;
55 static gfc_array_spec *current_as;
56 static int colon_seen;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals = 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr *last_initializer;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
77 gfc_symbol *sym;
78 gfc_expr *initializer;
79 struct enumerator_history *next;
81 enumerator_history;
83 /* Header of enum history chain. */
85 static enumerator_history *enum_history = NULL;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history *max_enum = NULL;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol *gfc_new_block;
95 bool gfc_matching_function;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data = false;
102 bool
103 gfc_in_match_data (void)
105 return in_match_data;
108 static void
109 set_in_match_data (bool set_value)
111 in_match_data = set_value;
114 /* Free a gfc_data_variable structure and everything beneath it. */
116 static void
117 free_variable (gfc_data_variable *p)
119 gfc_data_variable *q;
121 for (; p; p = q)
123 q = p->next;
124 gfc_free_expr (p->expr);
125 gfc_free_iterator (&p->iter, 0);
126 free_variable (p->list);
127 free (p);
132 /* Free a gfc_data_value structure and everything beneath it. */
134 static void
135 free_value (gfc_data_value *p)
137 gfc_data_value *q;
139 for (; p; p = q)
141 q = p->next;
142 mpz_clear (p->repeat);
143 gfc_free_expr (p->expr);
144 free (p);
149 /* Free a list of gfc_data structures. */
151 void
152 gfc_free_data (gfc_data *p)
154 gfc_data *q;
156 for (; p; p = q)
158 q = p->next;
159 free_variable (p->var);
160 free_value (p->value);
161 free (p);
166 /* Free all data in a namespace. */
168 static void
169 gfc_free_data_all (gfc_namespace *ns)
171 gfc_data *d;
173 for (;ns->data;)
175 d = ns->data->next;
176 free (ns->data);
177 ns->data = d;
182 static match var_element (gfc_data_variable *);
184 /* Match a list of variables terminated by an iterator and a right
185 parenthesis. */
187 static match
188 var_list (gfc_data_variable *parent)
190 gfc_data_variable *tail, var;
191 match m;
193 m = var_element (&var);
194 if (m == MATCH_ERROR)
195 return MATCH_ERROR;
196 if (m == MATCH_NO)
197 goto syntax;
199 tail = gfc_get_data_variable ();
200 *tail = var;
202 parent->list = tail;
204 for (;;)
206 if (gfc_match_char (',') != MATCH_YES)
207 goto syntax;
209 m = gfc_match_iterator (&parent->iter, 1);
210 if (m == MATCH_YES)
211 break;
212 if (m == MATCH_ERROR)
213 return MATCH_ERROR;
215 m = var_element (&var);
216 if (m == MATCH_ERROR)
217 return MATCH_ERROR;
218 if (m == MATCH_NO)
219 goto syntax;
221 tail->next = gfc_get_data_variable ();
222 tail = tail->next;
224 *tail = var;
227 if (gfc_match_char (')') != MATCH_YES)
228 goto syntax;
229 return MATCH_YES;
231 syntax:
232 gfc_syntax_error (ST_DATA);
233 return MATCH_ERROR;
237 /* Match a single element in a data variable list, which can be a
238 variable-iterator list. */
240 static match
241 var_element (gfc_data_variable *new_var)
243 match m;
244 gfc_symbol *sym;
246 memset (new_var, 0, sizeof (gfc_data_variable));
248 if (gfc_match_char ('(') == MATCH_YES)
249 return var_list (new_var);
251 m = gfc_match_variable (&new_var->expr, 0);
252 if (m != MATCH_YES)
253 return m;
255 sym = new_var->expr->symtree->n.sym;
257 /* Symbol should already have an associated type. */
258 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
259 return MATCH_ERROR;
261 if (!sym->attr.function && gfc_current_ns->parent
262 && gfc_current_ns->parent == sym->ns)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym->name);
266 return MATCH_ERROR;
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym->attr.in_common
271 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym->name))
274 return MATCH_ERROR;
276 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
277 return MATCH_ERROR;
279 return MATCH_YES;
283 /* Match the top-level list of data variables. */
285 static match
286 top_var_list (gfc_data *d)
288 gfc_data_variable var, *tail, *new_var;
289 match m;
291 tail = NULL;
293 for (;;)
295 m = var_element (&var);
296 if (m == MATCH_NO)
297 goto syntax;
298 if (m == MATCH_ERROR)
299 return MATCH_ERROR;
301 new_var = gfc_get_data_variable ();
302 *new_var = var;
304 if (tail == NULL)
305 d->var = new_var;
306 else
307 tail->next = new_var;
309 tail = new_var;
311 if (gfc_match_char ('/') == MATCH_YES)
312 break;
313 if (gfc_match_char (',') != MATCH_YES)
314 goto syntax;
317 return MATCH_YES;
319 syntax:
320 gfc_syntax_error (ST_DATA);
321 gfc_free_data_all (gfc_current_ns);
322 return MATCH_ERROR;
326 static match
327 match_data_constant (gfc_expr **result)
329 char name[GFC_MAX_SYMBOL_LEN + 1];
330 gfc_symbol *sym, *dt_sym = NULL;
331 gfc_expr *expr;
332 match m;
333 locus old_loc;
335 m = gfc_match_literal_constant (&expr, 1);
336 if (m == MATCH_YES)
338 *result = expr;
339 return MATCH_YES;
342 if (m == MATCH_ERROR)
343 return MATCH_ERROR;
345 m = gfc_match_null (result);
346 if (m != MATCH_NO)
347 return m;
349 old_loc = gfc_current_locus;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m = gfc_match_rvalue (result);
354 if (m == MATCH_ERROR)
355 return m;
357 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
359 if (!gfc_simplify_expr (*result, 0))
360 m = MATCH_ERROR;
361 return m;
363 else if (m == MATCH_YES)
364 gfc_free_expr (*result);
366 gfc_current_locus = old_loc;
368 m = gfc_match_name (name);
369 if (m != MATCH_YES)
370 return m;
372 if (gfc_find_symbol (name, NULL, 1, &sym))
373 return MATCH_ERROR;
375 if (sym && sym->attr.generic)
376 dt_sym = gfc_find_dt_in_generic (sym);
378 if (sym == NULL
379 || (sym->attr.flavor != FL_PARAMETER
380 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
382 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
383 name);
384 return MATCH_ERROR;
386 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
387 return gfc_match_structure_constructor (dt_sym, result);
389 /* Check to see if the value is an initialization array expression. */
390 if (sym->value->expr_type == EXPR_ARRAY)
392 gfc_current_locus = old_loc;
394 m = gfc_match_init_expr (result);
395 if (m == MATCH_ERROR)
396 return m;
398 if (m == MATCH_YES)
400 if (!gfc_simplify_expr (*result, 0))
401 m = MATCH_ERROR;
403 if ((*result)->expr_type == EXPR_CONSTANT)
404 return m;
405 else
407 gfc_error ("Invalid initializer %s in Data statement at %C", name);
408 return MATCH_ERROR;
413 *result = gfc_copy_expr (sym->value);
414 return MATCH_YES;
418 /* Match a list of values in a DATA statement. The leading '/' has
419 already been seen at this point. */
421 static match
422 top_val_list (gfc_data *data)
424 gfc_data_value *new_val, *tail;
425 gfc_expr *expr;
426 match m;
428 tail = NULL;
430 for (;;)
432 m = match_data_constant (&expr);
433 if (m == MATCH_NO)
434 goto syntax;
435 if (m == MATCH_ERROR)
436 return MATCH_ERROR;
438 new_val = gfc_get_data_value ();
439 mpz_init (new_val->repeat);
441 if (tail == NULL)
442 data->value = new_val;
443 else
444 tail->next = new_val;
446 tail = new_val;
448 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
450 tail->expr = expr;
451 mpz_set_ui (tail->repeat, 1);
453 else
455 mpz_set (tail->repeat, expr->value.integer);
456 gfc_free_expr (expr);
458 m = match_data_constant (&tail->expr);
459 if (m == MATCH_NO)
460 goto syntax;
461 if (m == MATCH_ERROR)
462 return MATCH_ERROR;
465 if (gfc_match_char ('/') == MATCH_YES)
466 break;
467 if (gfc_match_char (',') == MATCH_NO)
468 goto syntax;
471 return MATCH_YES;
473 syntax:
474 gfc_syntax_error (ST_DATA);
475 gfc_free_data_all (gfc_current_ns);
476 return MATCH_ERROR;
480 /* Matches an old style initialization. */
482 static match
483 match_old_style_init (const char *name)
485 match m;
486 gfc_symtree *st;
487 gfc_symbol *sym;
488 gfc_data *newdata;
490 /* Set up data structure to hold initializers. */
491 gfc_find_sym_tree (name, NULL, 0, &st);
492 sym = st->n.sym;
494 newdata = gfc_get_data ();
495 newdata->var = gfc_get_data_variable ();
496 newdata->var->expr = gfc_get_variable_expr (st);
497 newdata->where = gfc_current_locus;
499 /* Match initial value list. This also eats the terminal '/'. */
500 m = top_val_list (newdata);
501 if (m != MATCH_YES)
503 free (newdata);
504 return m;
507 if (gfc_pure (NULL))
509 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
510 free (newdata);
511 return MATCH_ERROR;
513 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
515 /* Mark the variable as having appeared in a data statement. */
516 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
518 free (newdata);
519 return MATCH_ERROR;
522 /* Chain in namespace list of DATA initializers. */
523 newdata->next = gfc_current_ns->data;
524 gfc_current_ns->data = newdata;
526 return m;
530 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
531 we are matching a DATA statement and are therefore issuing an error
532 if we encounter something unexpected, if not, we're trying to match
533 an old-style initialization expression of the form INTEGER I /2/. */
535 match
536 gfc_match_data (void)
538 gfc_data *new_data;
539 match m;
541 set_in_match_data (true);
543 for (;;)
545 new_data = gfc_get_data ();
546 new_data->where = gfc_current_locus;
548 m = top_var_list (new_data);
549 if (m != MATCH_YES)
550 goto cleanup;
552 m = top_val_list (new_data);
553 if (m != MATCH_YES)
554 goto cleanup;
556 new_data->next = gfc_current_ns->data;
557 gfc_current_ns->data = new_data;
559 if (gfc_match_eos () == MATCH_YES)
560 break;
562 gfc_match_char (','); /* Optional comma */
565 set_in_match_data (false);
567 if (gfc_pure (NULL))
569 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
570 return MATCH_ERROR;
572 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
574 return MATCH_YES;
576 cleanup:
577 set_in_match_data (false);
578 gfc_free_data (new_data);
579 return MATCH_ERROR;
583 /************************ Declaration statements *********************/
586 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
588 static bool
589 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
591 int i;
593 if ((from->type == AS_ASSUMED_RANK && to->corank)
594 || (to->type == AS_ASSUMED_RANK && from->corank))
596 gfc_error ("The assumed-rank array at %C shall not have a codimension");
597 return false;
600 if (to->rank == 0 && from->rank > 0)
602 to->rank = from->rank;
603 to->type = from->type;
604 to->cray_pointee = from->cray_pointee;
605 to->cp_was_assumed = from->cp_was_assumed;
607 for (i = 0; i < to->corank; i++)
609 to->lower[from->rank + i] = to->lower[i];
610 to->upper[from->rank + i] = to->upper[i];
612 for (i = 0; i < from->rank; i++)
614 if (copy)
616 to->lower[i] = gfc_copy_expr (from->lower[i]);
617 to->upper[i] = gfc_copy_expr (from->upper[i]);
619 else
621 to->lower[i] = from->lower[i];
622 to->upper[i] = from->upper[i];
626 else if (to->corank == 0 && from->corank > 0)
628 to->corank = from->corank;
629 to->cotype = from->cotype;
631 for (i = 0; i < from->corank; i++)
633 if (copy)
635 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
636 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
638 else
640 to->lower[to->rank + i] = from->lower[i];
641 to->upper[to->rank + i] = from->upper[i];
646 return true;
650 /* Match an intent specification. Since this can only happen after an
651 INTENT word, a legal intent-spec must follow. */
653 static sym_intent
654 match_intent_spec (void)
657 if (gfc_match (" ( in out )") == MATCH_YES)
658 return INTENT_INOUT;
659 if (gfc_match (" ( in )") == MATCH_YES)
660 return INTENT_IN;
661 if (gfc_match (" ( out )") == MATCH_YES)
662 return INTENT_OUT;
664 gfc_error ("Bad INTENT specification at %C");
665 return INTENT_UNKNOWN;
669 /* Matches a character length specification, which is either a
670 specification expression, '*', or ':'. */
672 static match
673 char_len_param_value (gfc_expr **expr, bool *deferred)
675 match m;
677 *expr = NULL;
678 *deferred = false;
680 if (gfc_match_char ('*') == MATCH_YES)
681 return MATCH_YES;
683 if (gfc_match_char (':') == MATCH_YES)
685 if (!gfc_notify_std (GFC_STD_F2003, "deferred type "
686 "parameter at %C"))
687 return MATCH_ERROR;
689 *deferred = true;
691 return MATCH_YES;
694 m = gfc_match_expr (expr);
696 if (m == MATCH_YES
697 && !gfc_expr_check_typed (*expr, gfc_current_ns, false))
698 return MATCH_ERROR;
700 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
702 if ((*expr)->value.function.actual
703 && (*expr)->value.function.actual->expr->symtree)
705 gfc_expr *e;
706 e = (*expr)->value.function.actual->expr;
707 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
708 && e->expr_type == EXPR_VARIABLE)
710 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
711 goto syntax;
712 if (e->symtree->n.sym->ts.type == BT_CHARACTER
713 && e->symtree->n.sym->ts.u.cl
714 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
715 goto syntax;
719 return m;
721 syntax:
722 gfc_error ("Conflict in attributes of function argument at %C");
723 return MATCH_ERROR;
727 /* A character length is a '*' followed by a literal integer or a
728 char_len_param_value in parenthesis. */
730 static match
731 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
733 int length;
734 match m;
736 *deferred = false;
737 m = gfc_match_char ('*');
738 if (m != MATCH_YES)
739 return m;
741 m = gfc_match_small_literal_int (&length, NULL);
742 if (m == MATCH_ERROR)
743 return m;
745 if (m == MATCH_YES)
747 if (obsolescent_check
748 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
749 return MATCH_ERROR;
750 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
751 return m;
754 if (gfc_match_char ('(') == MATCH_NO)
755 goto syntax;
757 m = char_len_param_value (expr, deferred);
758 if (m != MATCH_YES && gfc_matching_function)
760 gfc_undo_symbols ();
761 m = MATCH_YES;
764 if (m == MATCH_ERROR)
765 return m;
766 if (m == MATCH_NO)
767 goto syntax;
769 if (gfc_match_char (')') == MATCH_NO)
771 gfc_free_expr (*expr);
772 *expr = NULL;
773 goto syntax;
776 return MATCH_YES;
778 syntax:
779 gfc_error ("Syntax error in character length specification at %C");
780 return MATCH_ERROR;
784 /* Special subroutine for finding a symbol. Check if the name is found
785 in the current name space. If not, and we're compiling a function or
786 subroutine and the parent compilation unit is an interface, then check
787 to see if the name we've been given is the name of the interface
788 (located in another namespace). */
790 static int
791 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
793 gfc_state_data *s;
794 gfc_symtree *st;
795 int i;
797 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
798 if (i == 0)
800 *result = st ? st->n.sym : NULL;
801 goto end;
804 if (gfc_current_state () != COMP_SUBROUTINE
805 && gfc_current_state () != COMP_FUNCTION)
806 goto end;
808 s = gfc_state_stack->previous;
809 if (s == NULL)
810 goto end;
812 if (s->state != COMP_INTERFACE)
813 goto end;
814 if (s->sym == NULL)
815 goto end; /* Nameless interface. */
817 if (strcmp (name, s->sym->name) == 0)
819 *result = s->sym;
820 return 0;
823 end:
824 return i;
828 /* Special subroutine for getting a symbol node associated with a
829 procedure name, used in SUBROUTINE and FUNCTION statements. The
830 symbol is created in the parent using with symtree node in the
831 child unit pointing to the symbol. If the current namespace has no
832 parent, then the symbol is just created in the current unit. */
834 static int
835 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
837 gfc_symtree *st;
838 gfc_symbol *sym;
839 int rc = 0;
841 /* Module functions have to be left in their own namespace because
842 they have potentially (almost certainly!) already been referenced.
843 In this sense, they are rather like external functions. This is
844 fixed up in resolve.c(resolve_entries), where the symbol name-
845 space is set to point to the master function, so that the fake
846 result mechanism can work. */
847 if (module_fcn_entry)
849 /* Present if entry is declared to be a module procedure. */
850 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
852 if (*result == NULL)
853 rc = gfc_get_symbol (name, NULL, result);
854 else if (!gfc_get_symbol (name, NULL, &sym) && sym
855 && (*result)->ts.type == BT_UNKNOWN
856 && sym->attr.flavor == FL_UNKNOWN)
857 /* Pick up the typespec for the entry, if declared in the function
858 body. Note that this symbol is FL_UNKNOWN because it will
859 only have appeared in a type declaration. The local symtree
860 is set to point to the module symbol and a unique symtree
861 to the local version. This latter ensures a correct clearing
862 of the symbols. */
864 /* If the ENTRY proceeds its specification, we need to ensure
865 that this does not raise a "has no IMPLICIT type" error. */
866 if (sym->ts.type == BT_UNKNOWN)
867 sym->attr.untyped = 1;
869 (*result)->ts = sym->ts;
871 /* Put the symbol in the procedure namespace so that, should
872 the ENTRY precede its specification, the specification
873 can be applied. */
874 (*result)->ns = gfc_current_ns;
876 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
877 st->n.sym = *result;
878 st = gfc_get_unique_symtree (gfc_current_ns);
879 st->n.sym = sym;
882 else
883 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
885 if (rc)
886 return rc;
888 sym = *result;
890 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
892 /* Trap another encompassed procedure with the same name. All
893 these conditions are necessary to avoid picking up an entry
894 whose name clashes with that of the encompassing procedure;
895 this is handled using gsymbols to register unique,globally
896 accessible names. */
897 if (sym->attr.flavor != 0
898 && sym->attr.proc != 0
899 && (sym->attr.subroutine || sym->attr.function)
900 && sym->attr.if_source != IFSRC_UNKNOWN)
901 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
902 name, &sym->declared_at);
904 /* Trap a procedure with a name the same as interface in the
905 encompassing scope. */
906 if (sym->attr.generic != 0
907 && (sym->attr.subroutine || sym->attr.function)
908 && !sym->attr.mod_proc)
909 gfc_error_now ("Name '%s' at %C is already defined"
910 " as a generic interface at %L",
911 name, &sym->declared_at);
913 /* Trap declarations of attributes in encompassing scope. The
914 signature for this is that ts.kind is set. Legitimate
915 references only set ts.type. */
916 if (sym->ts.kind != 0
917 && !sym->attr.implicit_type
918 && sym->attr.proc == 0
919 && gfc_current_ns->parent != NULL
920 && sym->attr.access == 0
921 && !module_fcn_entry)
922 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
923 "and must not have attributes declared at %L",
924 name, &sym->declared_at);
927 if (gfc_current_ns->parent == NULL || *result == NULL)
928 return rc;
930 /* Module function entries will already have a symtree in
931 the current namespace but will need one at module level. */
932 if (module_fcn_entry)
934 /* Present if entry is declared to be a module procedure. */
935 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
936 if (st == NULL)
937 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
939 else
940 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
942 st->n.sym = sym;
943 sym->refs++;
945 /* See if the procedure should be a module procedure. */
947 if (((sym->ns->proc_name != NULL
948 && sym->ns->proc_name->attr.flavor == FL_MODULE
949 && sym->attr.proc != PROC_MODULE)
950 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
951 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
952 rc = 2;
954 return rc;
958 /* Verify that the given symbol representing a parameter is C
959 interoperable, by checking to see if it was marked as such after
960 its declaration. If the given symbol is not interoperable, a
961 warning is reported, thus removing the need to return the status to
962 the calling function. The standard does not require the user use
963 one of the iso_c_binding named constants to declare an
964 interoperable parameter, but we can't be sure if the param is C
965 interop or not if the user doesn't. For example, integer(4) may be
966 legal Fortran, but doesn't have meaning in C. It may interop with
967 a number of the C types, which causes a problem because the
968 compiler can't know which one. This code is almost certainly not
969 portable, and the user will get what they deserve if the C type
970 across platforms isn't always interoperable with integer(4). If
971 the user had used something like integer(c_int) or integer(c_long),
972 the compiler could have automatically handled the varying sizes
973 across platforms. */
975 bool
976 gfc_verify_c_interop_param (gfc_symbol *sym)
978 int is_c_interop = 0;
979 bool retval = true;
981 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
982 Don't repeat the checks here. */
983 if (sym->attr.implicit_type)
984 return true;
986 /* For subroutines or functions that are passed to a BIND(C) procedure,
987 they're interoperable if they're BIND(C) and their params are all
988 interoperable. */
989 if (sym->attr.flavor == FL_PROCEDURE)
991 if (sym->attr.is_bind_c == 0)
993 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
994 "attribute to be C interoperable", sym->name,
995 &(sym->declared_at));
997 return false;
999 else
1001 if (sym->attr.is_c_interop == 1)
1002 /* We've already checked this procedure; don't check it again. */
1003 return true;
1004 else
1005 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1006 sym->common_block);
1010 /* See if we've stored a reference to a procedure that owns sym. */
1011 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1013 if (sym->ns->proc_name->attr.is_bind_c == 1)
1015 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1017 if (is_c_interop != 1)
1019 /* Make personalized messages to give better feedback. */
1020 if (sym->ts.type == BT_DERIVED)
1021 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1022 "BIND(C) procedure '%s' but is not C interoperable "
1023 "because derived type '%s' is not C interoperable",
1024 sym->name, &(sym->declared_at),
1025 sym->ns->proc_name->name,
1026 sym->ts.u.derived->name);
1027 else if (sym->ts.type == BT_CLASS)
1028 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1029 "BIND(C) procedure '%s' but is not C interoperable "
1030 "because it is polymorphic",
1031 sym->name, &(sym->declared_at),
1032 sym->ns->proc_name->name);
1033 else if (gfc_option.warn_c_binding_type)
1034 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1035 "BIND(C) procedure '%s' but may not be C "
1036 "interoperable",
1037 sym->name, &(sym->declared_at),
1038 sym->ns->proc_name->name);
1041 /* Character strings are only C interoperable if they have a
1042 length of 1. */
1043 if (sym->ts.type == BT_CHARACTER)
1045 gfc_charlen *cl = sym->ts.u.cl;
1046 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1047 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1049 gfc_error ("Character argument '%s' at %L "
1050 "must be length 1 because "
1051 "procedure '%s' is BIND(C)",
1052 sym->name, &sym->declared_at,
1053 sym->ns->proc_name->name);
1054 retval = false;
1058 /* We have to make sure that any param to a bind(c) routine does
1059 not have the allocatable, pointer, or optional attributes,
1060 according to J3/04-007, section 5.1. */
1061 if (sym->attr.allocatable == 1
1062 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' at %L with "
1063 "ALLOCATABLE attribute in procedure '%s' "
1064 "with BIND(C)", sym->name,
1065 &(sym->declared_at),
1066 sym->ns->proc_name->name))
1067 retval = false;
1069 if (sym->attr.pointer == 1
1070 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' at %L with "
1071 "POINTER attribute in procedure '%s' "
1072 "with BIND(C)", sym->name,
1073 &(sym->declared_at),
1074 sym->ns->proc_name->name))
1075 retval = false;
1077 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1079 gfc_error ("Scalar variable '%s' at %L with POINTER or "
1080 "ALLOCATABLE in procedure '%s' with BIND(C) is not yet"
1081 " supported", sym->name, &(sym->declared_at),
1082 sym->ns->proc_name->name);
1083 retval = false;
1086 if (sym->attr.optional == 1 && sym->attr.value)
1088 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1089 "and the VALUE attribute because procedure '%s' "
1090 "is BIND(C)", sym->name, &(sym->declared_at),
1091 sym->ns->proc_name->name);
1092 retval = false;
1094 else if (sym->attr.optional == 1
1095 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' "
1096 "at %L with OPTIONAL attribute in "
1097 "procedure '%s' which is BIND(C)",
1098 sym->name, &(sym->declared_at),
1099 sym->ns->proc_name->name))
1100 retval = false;
1102 /* Make sure that if it has the dimension attribute, that it is
1103 either assumed size or explicit shape. Deferred shape is already
1104 covered by the pointer/allocatable attribute. */
1105 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1106 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array '%s' "
1107 "at %L as dummy argument to the BIND(C) "
1108 "procedure '%s' at %L", sym->name,
1109 &(sym->declared_at),
1110 sym->ns->proc_name->name,
1111 &(sym->ns->proc_name->declared_at)))
1112 retval = false;
1116 return retval;
1121 /* Function called by variable_decl() that adds a name to the symbol table. */
1123 static bool
1124 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1125 gfc_array_spec **as, locus *var_locus)
1127 symbol_attribute attr;
1128 gfc_symbol *sym;
1130 if (gfc_get_symbol (name, NULL, &sym))
1131 return false;
1133 /* Start updating the symbol table. Add basic type attribute if present. */
1134 if (current_ts.type != BT_UNKNOWN
1135 && (sym->attr.implicit_type == 0
1136 || !gfc_compare_types (&sym->ts, &current_ts))
1137 && !gfc_add_type (sym, &current_ts, var_locus))
1138 return false;
1140 if (sym->ts.type == BT_CHARACTER)
1142 sym->ts.u.cl = cl;
1143 sym->ts.deferred = cl_deferred;
1146 /* Add dimension attribute if present. */
1147 if (!gfc_set_array_spec (sym, *as, var_locus))
1148 return false;
1149 *as = NULL;
1151 /* Add attribute to symbol. The copy is so that we can reset the
1152 dimension attribute. */
1153 attr = current_attr;
1154 attr.dimension = 0;
1155 attr.codimension = 0;
1157 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1158 return false;
1160 /* Finish any work that may need to be done for the binding label,
1161 if it's a bind(c). The bind(c) attr is found before the symbol
1162 is made, and before the symbol name (for data decls), so the
1163 current_ts is holding the binding label, or nothing if the
1164 name= attr wasn't given. Therefore, test here if we're dealing
1165 with a bind(c) and make sure the binding label is set correctly. */
1166 if (sym->attr.is_bind_c == 1)
1168 if (!sym->binding_label)
1170 /* Set the binding label and verify that if a NAME= was specified
1171 then only one identifier was in the entity-decl-list. */
1172 if (!set_binding_label (&sym->binding_label, sym->name,
1173 num_idents_on_line))
1174 return false;
1178 /* See if we know we're in a common block, and if it's a bind(c)
1179 common then we need to make sure we're an interoperable type. */
1180 if (sym->attr.in_common == 1)
1182 /* Test the common block object. */
1183 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1184 && sym->ts.is_c_interop != 1)
1186 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1187 "must be declared with a C interoperable "
1188 "kind since common block '%s' is BIND(C)",
1189 sym->name, sym->common_block->name,
1190 sym->common_block->name);
1191 gfc_clear_error ();
1195 sym->attr.implied_index = 0;
1197 if (sym->ts.type == BT_CLASS)
1198 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1200 return true;
1204 /* Set character constant to the given length. The constant will be padded or
1205 truncated. If we're inside an array constructor without a typespec, we
1206 additionally check that all elements have the same length; check_len -1
1207 means no checking. */
1209 void
1210 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1212 gfc_char_t *s;
1213 int slen;
1215 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1216 gcc_assert (expr->ts.type == BT_CHARACTER);
1218 slen = expr->value.character.length;
1219 if (len != slen)
1221 s = gfc_get_wide_string (len + 1);
1222 memcpy (s, expr->value.character.string,
1223 MIN (len, slen) * sizeof (gfc_char_t));
1224 if (len > slen)
1225 gfc_wide_memset (&s[slen], ' ', len - slen);
1227 if (gfc_option.warn_character_truncation && slen > len)
1228 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1229 "(%d/%d)", &expr->where, slen, len);
1231 /* Apply the standard by 'hand' otherwise it gets cleared for
1232 initializers. */
1233 if (check_len != -1 && slen != check_len
1234 && !(gfc_option.allow_std & GFC_STD_GNU))
1235 gfc_error_now ("The CHARACTER elements of the array constructor "
1236 "at %L must have the same length (%d/%d)",
1237 &expr->where, slen, check_len);
1239 s[len] = '\0';
1240 free (expr->value.character.string);
1241 expr->value.character.string = s;
1242 expr->value.character.length = len;
1247 /* Function to create and update the enumerator history
1248 using the information passed as arguments.
1249 Pointer "max_enum" is also updated, to point to
1250 enum history node containing largest initializer.
1252 SYM points to the symbol node of enumerator.
1253 INIT points to its enumerator value. */
1255 static void
1256 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1258 enumerator_history *new_enum_history;
1259 gcc_assert (sym != NULL && init != NULL);
1261 new_enum_history = XCNEW (enumerator_history);
1263 new_enum_history->sym = sym;
1264 new_enum_history->initializer = init;
1265 new_enum_history->next = NULL;
1267 if (enum_history == NULL)
1269 enum_history = new_enum_history;
1270 max_enum = enum_history;
1272 else
1274 new_enum_history->next = enum_history;
1275 enum_history = new_enum_history;
1277 if (mpz_cmp (max_enum->initializer->value.integer,
1278 new_enum_history->initializer->value.integer) < 0)
1279 max_enum = new_enum_history;
1284 /* Function to free enum kind history. */
1286 void
1287 gfc_free_enum_history (void)
1289 enumerator_history *current = enum_history;
1290 enumerator_history *next;
1292 while (current != NULL)
1294 next = current->next;
1295 free (current);
1296 current = next;
1298 max_enum = NULL;
1299 enum_history = NULL;
1303 /* Function called by variable_decl() that adds an initialization
1304 expression to a symbol. */
1306 static bool
1307 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1309 symbol_attribute attr;
1310 gfc_symbol *sym;
1311 gfc_expr *init;
1313 init = *initp;
1314 if (find_special (name, &sym, false))
1315 return false;
1317 attr = sym->attr;
1319 /* If this symbol is confirming an implicit parameter type,
1320 then an initialization expression is not allowed. */
1321 if (attr.flavor == FL_PARAMETER
1322 && sym->value != NULL
1323 && *initp != NULL)
1325 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1326 sym->name);
1327 return false;
1330 if (init == NULL)
1332 /* An initializer is required for PARAMETER declarations. */
1333 if (attr.flavor == FL_PARAMETER)
1335 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1336 return false;
1339 else
1341 /* If a variable appears in a DATA block, it cannot have an
1342 initializer. */
1343 if (sym->attr.data)
1345 gfc_error ("Variable '%s' at %C with an initializer already "
1346 "appears in a DATA statement", sym->name);
1347 return false;
1350 /* Check if the assignment can happen. This has to be put off
1351 until later for derived type variables and procedure pointers. */
1352 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1353 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1354 && !sym->attr.proc_pointer
1355 && !gfc_check_assign_symbol (sym, NULL, init))
1356 return false;
1358 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1359 && init->ts.type == BT_CHARACTER)
1361 /* Update symbol character length according initializer. */
1362 if (!gfc_check_assign_symbol (sym, NULL, init))
1363 return false;
1365 if (sym->ts.u.cl->length == NULL)
1367 int clen;
1368 /* If there are multiple CHARACTER variables declared on the
1369 same line, we don't want them to share the same length. */
1370 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1372 if (sym->attr.flavor == FL_PARAMETER)
1374 if (init->expr_type == EXPR_CONSTANT)
1376 clen = init->value.character.length;
1377 sym->ts.u.cl->length
1378 = gfc_get_int_expr (gfc_default_integer_kind,
1379 NULL, clen);
1381 else if (init->expr_type == EXPR_ARRAY)
1383 gfc_constructor *c;
1384 c = gfc_constructor_first (init->value.constructor);
1385 clen = c->expr->value.character.length;
1386 sym->ts.u.cl->length
1387 = gfc_get_int_expr (gfc_default_integer_kind,
1388 NULL, clen);
1390 else if (init->ts.u.cl && init->ts.u.cl->length)
1391 sym->ts.u.cl->length =
1392 gfc_copy_expr (sym->value->ts.u.cl->length);
1395 /* Update initializer character length according symbol. */
1396 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1398 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1400 if (init->expr_type == EXPR_CONSTANT)
1401 gfc_set_constant_character_len (len, init, -1);
1402 else if (init->expr_type == EXPR_ARRAY)
1404 gfc_constructor *c;
1406 /* Build a new charlen to prevent simplification from
1407 deleting the length before it is resolved. */
1408 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1409 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1411 for (c = gfc_constructor_first (init->value.constructor);
1412 c; c = gfc_constructor_next (c))
1413 gfc_set_constant_character_len (len, c->expr, -1);
1418 /* If sym is implied-shape, set its upper bounds from init. */
1419 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1420 && sym->as->type == AS_IMPLIED_SHAPE)
1422 int dim;
1424 if (init->rank == 0)
1426 gfc_error ("Can't initialize implied-shape array at %L"
1427 " with scalar", &sym->declared_at);
1428 return false;
1430 gcc_assert (sym->as->rank == init->rank);
1432 /* Shape should be present, we get an initialization expression. */
1433 gcc_assert (init->shape);
1435 for (dim = 0; dim < sym->as->rank; ++dim)
1437 int k;
1438 gfc_expr* lower;
1439 gfc_expr* e;
1441 lower = sym->as->lower[dim];
1442 if (lower->expr_type != EXPR_CONSTANT)
1444 gfc_error ("Non-constant lower bound in implied-shape"
1445 " declaration at %L", &lower->where);
1446 return false;
1449 /* All dimensions must be without upper bound. */
1450 gcc_assert (!sym->as->upper[dim]);
1452 k = lower->ts.kind;
1453 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1454 mpz_add (e->value.integer,
1455 lower->value.integer, init->shape[dim]);
1456 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1457 sym->as->upper[dim] = e;
1460 sym->as->type = AS_EXPLICIT;
1463 /* Need to check if the expression we initialized this
1464 to was one of the iso_c_binding named constants. If so,
1465 and we're a parameter (constant), let it be iso_c.
1466 For example:
1467 integer(c_int), parameter :: my_int = c_int
1468 integer(my_int) :: my_int_2
1469 If we mark my_int as iso_c (since we can see it's value
1470 is equal to one of the named constants), then my_int_2
1471 will be considered C interoperable. */
1472 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1474 sym->ts.is_iso_c |= init->ts.is_iso_c;
1475 sym->ts.is_c_interop |= init->ts.is_c_interop;
1476 /* attr bits needed for module files. */
1477 sym->attr.is_iso_c |= init->ts.is_iso_c;
1478 sym->attr.is_c_interop |= init->ts.is_c_interop;
1479 if (init->ts.is_iso_c)
1480 sym->ts.f90_type = init->ts.f90_type;
1483 /* Add initializer. Make sure we keep the ranks sane. */
1484 if (sym->attr.dimension && init->rank == 0)
1486 mpz_t size;
1487 gfc_expr *array;
1488 int n;
1489 if (sym->attr.flavor == FL_PARAMETER
1490 && init->expr_type == EXPR_CONSTANT
1491 && spec_size (sym->as, &size)
1492 && mpz_cmp_si (size, 0) > 0)
1494 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1495 &init->where);
1496 for (n = 0; n < (int)mpz_get_si (size); n++)
1497 gfc_constructor_append_expr (&array->value.constructor,
1498 n == 0
1499 ? init
1500 : gfc_copy_expr (init),
1501 &init->where);
1503 array->shape = gfc_get_shape (sym->as->rank);
1504 for (n = 0; n < sym->as->rank; n++)
1505 spec_dimen_size (sym->as, n, &array->shape[n]);
1507 init = array;
1508 mpz_clear (size);
1510 init->rank = sym->as->rank;
1513 sym->value = init;
1514 if (sym->attr.save == SAVE_NONE)
1515 sym->attr.save = SAVE_IMPLICIT;
1516 *initp = NULL;
1519 return true;
1523 /* Function called by variable_decl() that adds a name to a structure
1524 being built. */
1526 static bool
1527 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1528 gfc_array_spec **as)
1530 gfc_component *c;
1531 bool t = true;
1533 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1534 constructing, it must have the pointer attribute. */
1535 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1536 && current_ts.u.derived == gfc_current_block ()
1537 && current_attr.pointer == 0)
1539 gfc_error ("Component at %C must have the POINTER attribute");
1540 return false;
1543 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1545 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1547 gfc_error ("Array component of structure at %C must have explicit "
1548 "or deferred shape");
1549 return false;
1553 if (!gfc_add_component (gfc_current_block(), name, &c))
1554 return false;
1556 c->ts = current_ts;
1557 if (c->ts.type == BT_CHARACTER)
1558 c->ts.u.cl = cl;
1559 c->attr = current_attr;
1561 c->initializer = *init;
1562 *init = NULL;
1564 c->as = *as;
1565 if (c->as != NULL)
1567 if (c->as->corank)
1568 c->attr.codimension = 1;
1569 if (c->as->rank)
1570 c->attr.dimension = 1;
1572 *as = NULL;
1574 /* Should this ever get more complicated, combine with similar section
1575 in add_init_expr_to_sym into a separate function. */
1576 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1577 && c->ts.u.cl
1578 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1580 int len;
1582 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1583 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1584 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1586 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1588 if (c->initializer->expr_type == EXPR_CONSTANT)
1589 gfc_set_constant_character_len (len, c->initializer, -1);
1590 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1591 c->initializer->ts.u.cl->length->value.integer))
1593 gfc_constructor *ctor;
1594 ctor = gfc_constructor_first (c->initializer->value.constructor);
1596 if (ctor)
1598 int first_len;
1599 bool has_ts = (c->initializer->ts.u.cl
1600 && c->initializer->ts.u.cl->length_from_typespec);
1602 /* Remember the length of the first element for checking
1603 that all elements *in the constructor* have the same
1604 length. This need not be the length of the LHS! */
1605 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1606 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1607 first_len = ctor->expr->value.character.length;
1609 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1610 if (ctor->expr->expr_type == EXPR_CONSTANT)
1612 gfc_set_constant_character_len (len, ctor->expr,
1613 has_ts ? -1 : first_len);
1614 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1620 /* Check array components. */
1621 if (!c->attr.dimension)
1622 goto scalar;
1624 if (c->attr.pointer)
1626 if (c->as->type != AS_DEFERRED)
1628 gfc_error ("Pointer array component of structure at %C must have a "
1629 "deferred shape");
1630 t = false;
1633 else if (c->attr.allocatable)
1635 if (c->as->type != AS_DEFERRED)
1637 gfc_error ("Allocatable component of structure at %C must have a "
1638 "deferred shape");
1639 t = false;
1642 else
1644 if (c->as->type != AS_EXPLICIT)
1646 gfc_error ("Array component of structure at %C must have an "
1647 "explicit shape");
1648 t = false;
1652 scalar:
1653 if (c->ts.type == BT_CLASS)
1655 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1657 if (t)
1658 t = t2;
1661 return t;
1665 /* Match a 'NULL()', and possibly take care of some side effects. */
1667 match
1668 gfc_match_null (gfc_expr **result)
1670 gfc_symbol *sym;
1671 match m, m2 = MATCH_NO;
1673 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1674 return MATCH_ERROR;
1676 if (m == MATCH_NO)
1678 locus old_loc;
1679 char name[GFC_MAX_SYMBOL_LEN + 1];
1681 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1682 return m2;
1684 old_loc = gfc_current_locus;
1685 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1686 return MATCH_ERROR;
1687 if (m2 != MATCH_YES
1688 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1689 return MATCH_ERROR;
1690 if (m2 == MATCH_NO)
1692 gfc_current_locus = old_loc;
1693 return MATCH_NO;
1697 /* The NULL symbol now has to be/become an intrinsic function. */
1698 if (gfc_get_symbol ("null", NULL, &sym))
1700 gfc_error ("NULL() initialization at %C is ambiguous");
1701 return MATCH_ERROR;
1704 gfc_intrinsic_symbol (sym);
1706 if (sym->attr.proc != PROC_INTRINSIC
1707 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1708 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1709 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1710 return MATCH_ERROR;
1712 *result = gfc_get_null_expr (&gfc_current_locus);
1714 /* Invalid per F2008, C512. */
1715 if (m2 == MATCH_YES)
1717 gfc_error ("NULL() initialization at %C may not have MOLD");
1718 return MATCH_ERROR;
1721 return MATCH_YES;
1725 /* Match the initialization expr for a data pointer or procedure pointer. */
1727 static match
1728 match_pointer_init (gfc_expr **init, int procptr)
1730 match m;
1732 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1734 gfc_error ("Initialization of pointer at %C is not allowed in "
1735 "a PURE procedure");
1736 return MATCH_ERROR;
1738 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1740 /* Match NULL() initialization. */
1741 m = gfc_match_null (init);
1742 if (m != MATCH_NO)
1743 return m;
1745 /* Match non-NULL initialization. */
1746 gfc_matching_ptr_assignment = !procptr;
1747 gfc_matching_procptr_assignment = procptr;
1748 m = gfc_match_rvalue (init);
1749 gfc_matching_ptr_assignment = 0;
1750 gfc_matching_procptr_assignment = 0;
1751 if (m == MATCH_ERROR)
1752 return MATCH_ERROR;
1753 else if (m == MATCH_NO)
1755 gfc_error ("Error in pointer initialization at %C");
1756 return MATCH_ERROR;
1759 if (!procptr)
1760 gfc_resolve_expr (*init);
1762 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1763 "initialization at %C"))
1764 return MATCH_ERROR;
1766 return MATCH_YES;
1770 static bool
1771 check_function_name (char *name)
1773 /* In functions that have a RESULT variable defined, the function name always
1774 refers to function calls. Therefore, the name is not allowed to appear in
1775 specification statements. When checking this, be careful about
1776 'hidden' procedure pointer results ('ppr@'). */
1778 if (gfc_current_state () == COMP_FUNCTION)
1780 gfc_symbol *block = gfc_current_block ();
1781 if (block && block->result && block->result != block
1782 && strcmp (block->result->name, "ppr@") != 0
1783 && strcmp (block->name, name) == 0)
1785 gfc_error ("Function name '%s' not allowed at %C", name);
1786 return false;
1790 return true;
1794 /* Match a variable name with an optional initializer. When this
1795 subroutine is called, a variable is expected to be parsed next.
1796 Depending on what is happening at the moment, updates either the
1797 symbol table or the current interface. */
1799 static match
1800 variable_decl (int elem)
1802 char name[GFC_MAX_SYMBOL_LEN + 1];
1803 gfc_expr *initializer, *char_len;
1804 gfc_array_spec *as;
1805 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1806 gfc_charlen *cl;
1807 bool cl_deferred;
1808 locus var_locus;
1809 match m;
1810 bool t;
1811 gfc_symbol *sym;
1813 initializer = NULL;
1814 as = NULL;
1815 cp_as = NULL;
1817 /* When we get here, we've just matched a list of attributes and
1818 maybe a type and a double colon. The next thing we expect to see
1819 is the name of the symbol. */
1820 m = gfc_match_name (name);
1821 if (m != MATCH_YES)
1822 goto cleanup;
1824 var_locus = gfc_current_locus;
1826 /* Now we could see the optional array spec. or character length. */
1827 m = gfc_match_array_spec (&as, true, true);
1828 if (m == MATCH_ERROR)
1829 goto cleanup;
1831 if (m == MATCH_NO)
1832 as = gfc_copy_array_spec (current_as);
1833 else if (current_as
1834 && !merge_array_spec (current_as, as, true))
1836 m = MATCH_ERROR;
1837 goto cleanup;
1840 if (gfc_option.flag_cray_pointer)
1841 cp_as = gfc_copy_array_spec (as);
1843 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1844 determine (and check) whether it can be implied-shape. If it
1845 was parsed as assumed-size, change it because PARAMETERs can not
1846 be assumed-size. */
1847 if (as)
1849 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1851 m = MATCH_ERROR;
1852 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1853 name, &var_locus);
1854 goto cleanup;
1857 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1858 && current_attr.flavor == FL_PARAMETER)
1859 as->type = AS_IMPLIED_SHAPE;
1861 if (as->type == AS_IMPLIED_SHAPE
1862 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1863 &var_locus))
1865 m = MATCH_ERROR;
1866 goto cleanup;
1870 char_len = NULL;
1871 cl = NULL;
1872 cl_deferred = false;
1874 if (current_ts.type == BT_CHARACTER)
1876 switch (match_char_length (&char_len, &cl_deferred, false))
1878 case MATCH_YES:
1879 cl = gfc_new_charlen (gfc_current_ns, NULL);
1881 cl->length = char_len;
1882 break;
1884 /* Non-constant lengths need to be copied after the first
1885 element. Also copy assumed lengths. */
1886 case MATCH_NO:
1887 if (elem > 1
1888 && (current_ts.u.cl->length == NULL
1889 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1891 cl = gfc_new_charlen (gfc_current_ns, NULL);
1892 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1894 else
1895 cl = current_ts.u.cl;
1897 cl_deferred = current_ts.deferred;
1899 break;
1901 case MATCH_ERROR:
1902 goto cleanup;
1906 /* If this symbol has already shown up in a Cray Pointer declaration,
1907 and this is not a component declaration,
1908 then we want to set the type & bail out. */
1909 if (gfc_option.flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
1911 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1912 if (sym != NULL && sym->attr.cray_pointee)
1914 sym->ts.type = current_ts.type;
1915 sym->ts.kind = current_ts.kind;
1916 sym->ts.u.cl = cl;
1917 sym->ts.u.derived = current_ts.u.derived;
1918 sym->ts.is_c_interop = current_ts.is_c_interop;
1919 sym->ts.is_iso_c = current_ts.is_iso_c;
1920 m = MATCH_YES;
1922 /* Check to see if we have an array specification. */
1923 if (cp_as != NULL)
1925 if (sym->as != NULL)
1927 gfc_error ("Duplicate array spec for Cray pointee at %C");
1928 gfc_free_array_spec (cp_as);
1929 m = MATCH_ERROR;
1930 goto cleanup;
1932 else
1934 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
1935 gfc_internal_error ("Couldn't set pointee array spec.");
1937 /* Fix the array spec. */
1938 m = gfc_mod_pointee_as (sym->as);
1939 if (m == MATCH_ERROR)
1940 goto cleanup;
1943 goto cleanup;
1945 else
1947 gfc_free_array_spec (cp_as);
1951 /* Procedure pointer as function result. */
1952 if (gfc_current_state () == COMP_FUNCTION
1953 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1954 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1955 strcpy (name, "ppr@");
1957 if (gfc_current_state () == COMP_FUNCTION
1958 && strcmp (name, gfc_current_block ()->name) == 0
1959 && gfc_current_block ()->result
1960 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1961 strcpy (name, "ppr@");
1963 /* OK, we've successfully matched the declaration. Now put the
1964 symbol in the current namespace, because it might be used in the
1965 optional initialization expression for this symbol, e.g. this is
1966 perfectly legal:
1968 integer, parameter :: i = huge(i)
1970 This is only true for parameters or variables of a basic type.
1971 For components of derived types, it is not true, so we don't
1972 create a symbol for those yet. If we fail to create the symbol,
1973 bail out. */
1974 if (gfc_current_state () != COMP_DERIVED
1975 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
1977 m = MATCH_ERROR;
1978 goto cleanup;
1981 if (!check_function_name (name))
1983 m = MATCH_ERROR;
1984 goto cleanup;
1987 /* We allow old-style initializations of the form
1988 integer i /2/, j(4) /3*3, 1/
1989 (if no colon has been seen). These are different from data
1990 statements in that initializers are only allowed to apply to the
1991 variable immediately preceding, i.e.
1992 integer i, j /1, 2/
1993 is not allowed. Therefore we have to do some work manually, that
1994 could otherwise be left to the matchers for DATA statements. */
1996 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1998 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
1999 "initialization at %C"))
2000 return MATCH_ERROR;
2001 else if (gfc_current_state () == COMP_DERIVED)
2003 gfc_error ("Invalid old style initialization for derived type "
2004 "component at %C");
2005 m = MATCH_ERROR;
2006 goto cleanup;
2009 return match_old_style_init (name);
2012 /* The double colon must be present in order to have initializers.
2013 Otherwise the statement is ambiguous with an assignment statement. */
2014 if (colon_seen)
2016 if (gfc_match (" =>") == MATCH_YES)
2018 if (!current_attr.pointer)
2020 gfc_error ("Initialization at %C isn't for a pointer variable");
2021 m = MATCH_ERROR;
2022 goto cleanup;
2025 m = match_pointer_init (&initializer, 0);
2026 if (m != MATCH_YES)
2027 goto cleanup;
2029 else if (gfc_match_char ('=') == MATCH_YES)
2031 if (current_attr.pointer)
2033 gfc_error ("Pointer initialization at %C requires '=>', "
2034 "not '='");
2035 m = MATCH_ERROR;
2036 goto cleanup;
2039 m = gfc_match_init_expr (&initializer);
2040 if (m == MATCH_NO)
2042 gfc_error ("Expected an initialization expression at %C");
2043 m = MATCH_ERROR;
2046 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2047 && gfc_state_stack->state != COMP_DERIVED)
2049 gfc_error ("Initialization of variable at %C is not allowed in "
2050 "a PURE procedure");
2051 m = MATCH_ERROR;
2054 if (current_attr.flavor != FL_PARAMETER
2055 && gfc_state_stack->state != COMP_DERIVED)
2056 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2058 if (m != MATCH_YES)
2059 goto cleanup;
2063 if (initializer != NULL && current_attr.allocatable
2064 && gfc_current_state () == COMP_DERIVED)
2066 gfc_error ("Initialization of allocatable component at %C is not "
2067 "allowed");
2068 m = MATCH_ERROR;
2069 goto cleanup;
2072 /* Add the initializer. Note that it is fine if initializer is
2073 NULL here, because we sometimes also need to check if a
2074 declaration *must* have an initialization expression. */
2075 if (gfc_current_state () != COMP_DERIVED)
2076 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2077 else
2079 if (current_ts.type == BT_DERIVED
2080 && !current_attr.pointer && !initializer)
2081 initializer = gfc_default_initializer (&current_ts);
2082 t = build_struct (name, cl, &initializer, &as);
2085 m = (t) ? MATCH_YES : MATCH_ERROR;
2087 cleanup:
2088 /* Free stuff up and return. */
2089 gfc_free_expr (initializer);
2090 gfc_free_array_spec (as);
2092 return m;
2096 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2097 This assumes that the byte size is equal to the kind number for
2098 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2100 match
2101 gfc_match_old_kind_spec (gfc_typespec *ts)
2103 match m;
2104 int original_kind;
2106 if (gfc_match_char ('*') != MATCH_YES)
2107 return MATCH_NO;
2109 m = gfc_match_small_literal_int (&ts->kind, NULL);
2110 if (m != MATCH_YES)
2111 return MATCH_ERROR;
2113 original_kind = ts->kind;
2115 /* Massage the kind numbers for complex types. */
2116 if (ts->type == BT_COMPLEX)
2118 if (ts->kind % 2)
2120 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2121 gfc_basic_typename (ts->type), original_kind);
2122 return MATCH_ERROR;
2124 ts->kind /= 2;
2128 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2129 ts->kind = 8;
2131 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2133 if (ts->kind == 4)
2135 if (gfc_option.flag_real4_kind == 8)
2136 ts->kind = 8;
2137 if (gfc_option.flag_real4_kind == 10)
2138 ts->kind = 10;
2139 if (gfc_option.flag_real4_kind == 16)
2140 ts->kind = 16;
2143 if (ts->kind == 8)
2145 if (gfc_option.flag_real8_kind == 4)
2146 ts->kind = 4;
2147 if (gfc_option.flag_real8_kind == 10)
2148 ts->kind = 10;
2149 if (gfc_option.flag_real8_kind == 16)
2150 ts->kind = 16;
2154 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2156 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2157 gfc_basic_typename (ts->type), original_kind);
2158 return MATCH_ERROR;
2161 if (!gfc_notify_std (GFC_STD_GNU,
2162 "Nonstandard type declaration %s*%d at %C",
2163 gfc_basic_typename(ts->type), original_kind))
2164 return MATCH_ERROR;
2166 return MATCH_YES;
2170 /* Match a kind specification. Since kinds are generally optional, we
2171 usually return MATCH_NO if something goes wrong. If a "kind="
2172 string is found, then we know we have an error. */
2174 match
2175 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2177 locus where, loc;
2178 gfc_expr *e;
2179 match m, n;
2180 char c;
2181 const char *msg;
2183 m = MATCH_NO;
2184 n = MATCH_YES;
2185 e = NULL;
2187 where = loc = gfc_current_locus;
2189 if (kind_expr_only)
2190 goto kind_expr;
2192 if (gfc_match_char ('(') == MATCH_NO)
2193 return MATCH_NO;
2195 /* Also gobbles optional text. */
2196 if (gfc_match (" kind = ") == MATCH_YES)
2197 m = MATCH_ERROR;
2199 loc = gfc_current_locus;
2201 kind_expr:
2202 n = gfc_match_init_expr (&e);
2204 if (n != MATCH_YES)
2206 if (gfc_matching_function)
2208 /* The function kind expression might include use associated or
2209 imported parameters and try again after the specification
2210 expressions..... */
2211 if (gfc_match_char (')') != MATCH_YES)
2213 gfc_error ("Missing right parenthesis at %C");
2214 m = MATCH_ERROR;
2215 goto no_match;
2218 gfc_free_expr (e);
2219 gfc_undo_symbols ();
2220 return MATCH_YES;
2222 else
2224 /* ....or else, the match is real. */
2225 if (n == MATCH_NO)
2226 gfc_error ("Expected initialization expression at %C");
2227 if (n != MATCH_YES)
2228 return MATCH_ERROR;
2232 if (e->rank != 0)
2234 gfc_error ("Expected scalar initialization expression at %C");
2235 m = MATCH_ERROR;
2236 goto no_match;
2239 msg = gfc_extract_int (e, &ts->kind);
2241 if (msg != NULL)
2243 gfc_error (msg);
2244 m = MATCH_ERROR;
2245 goto no_match;
2248 /* Before throwing away the expression, let's see if we had a
2249 C interoperable kind (and store the fact). */
2250 if (e->ts.is_c_interop == 1)
2252 /* Mark this as C interoperable if being declared with one
2253 of the named constants from iso_c_binding. */
2254 ts->is_c_interop = e->ts.is_iso_c;
2255 ts->f90_type = e->ts.f90_type;
2258 gfc_free_expr (e);
2259 e = NULL;
2261 /* Ignore errors to this point, if we've gotten here. This means
2262 we ignore the m=MATCH_ERROR from above. */
2263 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2265 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2266 gfc_basic_typename (ts->type));
2267 gfc_current_locus = where;
2268 return MATCH_ERROR;
2271 /* Warn if, e.g., c_int is used for a REAL variable, but not
2272 if, e.g., c_double is used for COMPLEX as the standard
2273 explicitly says that the kind type parameter for complex and real
2274 variable is the same, i.e. c_float == c_float_complex. */
2275 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2276 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2277 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2278 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2279 "is %s", gfc_basic_typename (ts->f90_type), &where,
2280 gfc_basic_typename (ts->type));
2282 gfc_gobble_whitespace ();
2283 if ((c = gfc_next_ascii_char ()) != ')'
2284 && (ts->type != BT_CHARACTER || c != ','))
2286 if (ts->type == BT_CHARACTER)
2287 gfc_error ("Missing right parenthesis or comma at %C");
2288 else
2289 gfc_error ("Missing right parenthesis at %C");
2290 m = MATCH_ERROR;
2292 else
2293 /* All tests passed. */
2294 m = MATCH_YES;
2296 if(m == MATCH_ERROR)
2297 gfc_current_locus = where;
2299 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2300 ts->kind = 8;
2302 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2304 if (ts->kind == 4)
2306 if (gfc_option.flag_real4_kind == 8)
2307 ts->kind = 8;
2308 if (gfc_option.flag_real4_kind == 10)
2309 ts->kind = 10;
2310 if (gfc_option.flag_real4_kind == 16)
2311 ts->kind = 16;
2314 if (ts->kind == 8)
2316 if (gfc_option.flag_real8_kind == 4)
2317 ts->kind = 4;
2318 if (gfc_option.flag_real8_kind == 10)
2319 ts->kind = 10;
2320 if (gfc_option.flag_real8_kind == 16)
2321 ts->kind = 16;
2325 /* Return what we know from the test(s). */
2326 return m;
2328 no_match:
2329 gfc_free_expr (e);
2330 gfc_current_locus = where;
2331 return m;
2335 static match
2336 match_char_kind (int * kind, int * is_iso_c)
2338 locus where;
2339 gfc_expr *e;
2340 match m, n;
2341 const char *msg;
2343 m = MATCH_NO;
2344 e = NULL;
2345 where = gfc_current_locus;
2347 n = gfc_match_init_expr (&e);
2349 if (n != MATCH_YES && gfc_matching_function)
2351 /* The expression might include use-associated or imported
2352 parameters and try again after the specification
2353 expressions. */
2354 gfc_free_expr (e);
2355 gfc_undo_symbols ();
2356 return MATCH_YES;
2359 if (n == MATCH_NO)
2360 gfc_error ("Expected initialization expression at %C");
2361 if (n != MATCH_YES)
2362 return MATCH_ERROR;
2364 if (e->rank != 0)
2366 gfc_error ("Expected scalar initialization expression at %C");
2367 m = MATCH_ERROR;
2368 goto no_match;
2371 msg = gfc_extract_int (e, kind);
2372 *is_iso_c = e->ts.is_iso_c;
2373 if (msg != NULL)
2375 gfc_error (msg);
2376 m = MATCH_ERROR;
2377 goto no_match;
2380 gfc_free_expr (e);
2382 /* Ignore errors to this point, if we've gotten here. This means
2383 we ignore the m=MATCH_ERROR from above. */
2384 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2386 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2387 m = MATCH_ERROR;
2389 else
2390 /* All tests passed. */
2391 m = MATCH_YES;
2393 if (m == MATCH_ERROR)
2394 gfc_current_locus = where;
2396 /* Return what we know from the test(s). */
2397 return m;
2399 no_match:
2400 gfc_free_expr (e);
2401 gfc_current_locus = where;
2402 return m;
2406 /* Match the various kind/length specifications in a CHARACTER
2407 declaration. We don't return MATCH_NO. */
2409 match
2410 gfc_match_char_spec (gfc_typespec *ts)
2412 int kind, seen_length, is_iso_c;
2413 gfc_charlen *cl;
2414 gfc_expr *len;
2415 match m;
2416 bool deferred;
2418 len = NULL;
2419 seen_length = 0;
2420 kind = 0;
2421 is_iso_c = 0;
2422 deferred = false;
2424 /* Try the old-style specification first. */
2425 old_char_selector = 0;
2427 m = match_char_length (&len, &deferred, true);
2428 if (m != MATCH_NO)
2430 if (m == MATCH_YES)
2431 old_char_selector = 1;
2432 seen_length = 1;
2433 goto done;
2436 m = gfc_match_char ('(');
2437 if (m != MATCH_YES)
2439 m = MATCH_YES; /* Character without length is a single char. */
2440 goto done;
2443 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2444 if (gfc_match (" kind =") == MATCH_YES)
2446 m = match_char_kind (&kind, &is_iso_c);
2448 if (m == MATCH_ERROR)
2449 goto done;
2450 if (m == MATCH_NO)
2451 goto syntax;
2453 if (gfc_match (" , len =") == MATCH_NO)
2454 goto rparen;
2456 m = char_len_param_value (&len, &deferred);
2457 if (m == MATCH_NO)
2458 goto syntax;
2459 if (m == MATCH_ERROR)
2460 goto done;
2461 seen_length = 1;
2463 goto rparen;
2466 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2467 if (gfc_match (" len =") == MATCH_YES)
2469 m = char_len_param_value (&len, &deferred);
2470 if (m == MATCH_NO)
2471 goto syntax;
2472 if (m == MATCH_ERROR)
2473 goto done;
2474 seen_length = 1;
2476 if (gfc_match_char (')') == MATCH_YES)
2477 goto done;
2479 if (gfc_match (" , kind =") != MATCH_YES)
2480 goto syntax;
2482 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2483 goto done;
2485 goto rparen;
2488 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2489 m = char_len_param_value (&len, &deferred);
2490 if (m == MATCH_NO)
2491 goto syntax;
2492 if (m == MATCH_ERROR)
2493 goto done;
2494 seen_length = 1;
2496 m = gfc_match_char (')');
2497 if (m == MATCH_YES)
2498 goto done;
2500 if (gfc_match_char (',') != MATCH_YES)
2501 goto syntax;
2503 gfc_match (" kind ="); /* Gobble optional text. */
2505 m = match_char_kind (&kind, &is_iso_c);
2506 if (m == MATCH_ERROR)
2507 goto done;
2508 if (m == MATCH_NO)
2509 goto syntax;
2511 rparen:
2512 /* Require a right-paren at this point. */
2513 m = gfc_match_char (')');
2514 if (m == MATCH_YES)
2515 goto done;
2517 syntax:
2518 gfc_error ("Syntax error in CHARACTER declaration at %C");
2519 m = MATCH_ERROR;
2520 gfc_free_expr (len);
2521 return m;
2523 done:
2524 /* Deal with character functions after USE and IMPORT statements. */
2525 if (gfc_matching_function)
2527 gfc_free_expr (len);
2528 gfc_undo_symbols ();
2529 return MATCH_YES;
2532 if (m != MATCH_YES)
2534 gfc_free_expr (len);
2535 return m;
2538 /* Do some final massaging of the length values. */
2539 cl = gfc_new_charlen (gfc_current_ns, NULL);
2541 if (seen_length == 0)
2542 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2543 else
2544 cl->length = len;
2546 ts->u.cl = cl;
2547 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2548 ts->deferred = deferred;
2550 /* We have to know if it was a C interoperable kind so we can
2551 do accurate type checking of bind(c) procs, etc. */
2552 if (kind != 0)
2553 /* Mark this as C interoperable if being declared with one
2554 of the named constants from iso_c_binding. */
2555 ts->is_c_interop = is_iso_c;
2556 else if (len != NULL)
2557 /* Here, we might have parsed something such as: character(c_char)
2558 In this case, the parsing code above grabs the c_char when
2559 looking for the length (line 1690, roughly). it's the last
2560 testcase for parsing the kind params of a character variable.
2561 However, it's not actually the length. this seems like it
2562 could be an error.
2563 To see if the user used a C interop kind, test the expr
2564 of the so called length, and see if it's C interoperable. */
2565 ts->is_c_interop = len->ts.is_iso_c;
2567 return MATCH_YES;
2571 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2572 structure to the matched specification. This is necessary for FUNCTION and
2573 IMPLICIT statements.
2575 If implicit_flag is nonzero, then we don't check for the optional
2576 kind specification. Not doing so is needed for matching an IMPLICIT
2577 statement correctly. */
2579 match
2580 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2582 char name[GFC_MAX_SYMBOL_LEN + 1];
2583 gfc_symbol *sym, *dt_sym;
2584 match m;
2585 char c;
2586 bool seen_deferred_kind, matched_type;
2587 const char *dt_name;
2589 /* A belt and braces check that the typespec is correctly being treated
2590 as a deferred characteristic association. */
2591 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2592 && (gfc_current_block ()->result->ts.kind == -1)
2593 && (ts->kind == -1);
2594 gfc_clear_ts (ts);
2595 if (seen_deferred_kind)
2596 ts->kind = -1;
2598 /* Clear the current binding label, in case one is given. */
2599 curr_binding_label = NULL;
2601 if (gfc_match (" byte") == MATCH_YES)
2603 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2604 return MATCH_ERROR;
2606 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2608 gfc_error ("BYTE type used at %C "
2609 "is not available on the target machine");
2610 return MATCH_ERROR;
2613 ts->type = BT_INTEGER;
2614 ts->kind = 1;
2615 return MATCH_YES;
2619 m = gfc_match (" type (");
2620 matched_type = (m == MATCH_YES);
2621 if (matched_type)
2623 gfc_gobble_whitespace ();
2624 if (gfc_peek_ascii_char () == '*')
2626 if ((m = gfc_match ("*)")) != MATCH_YES)
2627 return m;
2628 if (gfc_current_state () == COMP_DERIVED)
2630 gfc_error ("Assumed type at %C is not allowed for components");
2631 return MATCH_ERROR;
2633 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2634 "at %C"))
2635 return MATCH_ERROR;
2636 ts->type = BT_ASSUMED;
2637 return MATCH_YES;
2640 m = gfc_match ("%n", name);
2641 matched_type = (m == MATCH_YES);
2644 if ((matched_type && strcmp ("integer", name) == 0)
2645 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2647 ts->type = BT_INTEGER;
2648 ts->kind = gfc_default_integer_kind;
2649 goto get_kind;
2652 if ((matched_type && strcmp ("character", name) == 0)
2653 || (!matched_type && gfc_match (" character") == MATCH_YES))
2655 if (matched_type
2656 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2657 "intrinsic-type-spec at %C"))
2658 return MATCH_ERROR;
2660 ts->type = BT_CHARACTER;
2661 if (implicit_flag == 0)
2662 m = gfc_match_char_spec (ts);
2663 else
2664 m = MATCH_YES;
2666 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2667 m = MATCH_ERROR;
2669 return m;
2672 if ((matched_type && strcmp ("real", name) == 0)
2673 || (!matched_type && gfc_match (" real") == MATCH_YES))
2675 ts->type = BT_REAL;
2676 ts->kind = gfc_default_real_kind;
2677 goto get_kind;
2680 if ((matched_type
2681 && (strcmp ("doubleprecision", name) == 0
2682 || (strcmp ("double", name) == 0
2683 && gfc_match (" precision") == MATCH_YES)))
2684 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2686 if (matched_type
2687 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2688 "intrinsic-type-spec at %C"))
2689 return MATCH_ERROR;
2690 if (matched_type && gfc_match_char (')') != MATCH_YES)
2691 return MATCH_ERROR;
2693 ts->type = BT_REAL;
2694 ts->kind = gfc_default_double_kind;
2695 return MATCH_YES;
2698 if ((matched_type && strcmp ("complex", name) == 0)
2699 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2701 ts->type = BT_COMPLEX;
2702 ts->kind = gfc_default_complex_kind;
2703 goto get_kind;
2706 if ((matched_type
2707 && (strcmp ("doublecomplex", name) == 0
2708 || (strcmp ("double", name) == 0
2709 && gfc_match (" complex") == MATCH_YES)))
2710 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2712 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2713 return MATCH_ERROR;
2715 if (matched_type
2716 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2717 "intrinsic-type-spec at %C"))
2718 return MATCH_ERROR;
2720 if (matched_type && gfc_match_char (')') != MATCH_YES)
2721 return MATCH_ERROR;
2723 ts->type = BT_COMPLEX;
2724 ts->kind = gfc_default_double_kind;
2725 return MATCH_YES;
2728 if ((matched_type && strcmp ("logical", name) == 0)
2729 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2731 ts->type = BT_LOGICAL;
2732 ts->kind = gfc_default_logical_kind;
2733 goto get_kind;
2736 if (matched_type)
2737 m = gfc_match_char (')');
2739 if (m == MATCH_YES)
2740 ts->type = BT_DERIVED;
2741 else
2743 /* Match CLASS declarations. */
2744 m = gfc_match (" class ( * )");
2745 if (m == MATCH_ERROR)
2746 return MATCH_ERROR;
2747 else if (m == MATCH_YES)
2749 gfc_symbol *upe;
2750 gfc_symtree *st;
2751 ts->type = BT_CLASS;
2752 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2753 if (upe == NULL)
2755 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2756 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2757 st->n.sym = upe;
2758 gfc_set_sym_referenced (upe);
2759 upe->refs++;
2760 upe->ts.type = BT_VOID;
2761 upe->attr.unlimited_polymorphic = 1;
2762 /* This is essential to force the construction of
2763 unlimited polymorphic component class containers. */
2764 upe->attr.zero_comp = 1;
2765 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2766 &gfc_current_locus))
2767 return MATCH_ERROR;
2769 else
2771 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2772 if (st == NULL)
2773 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2774 st->n.sym = upe;
2775 upe->refs++;
2777 ts->u.derived = upe;
2778 return m;
2781 m = gfc_match (" class ( %n )", name);
2782 if (m != MATCH_YES)
2783 return m;
2784 ts->type = BT_CLASS;
2786 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2787 return MATCH_ERROR;
2790 /* Defer association of the derived type until the end of the
2791 specification block. However, if the derived type can be
2792 found, add it to the typespec. */
2793 if (gfc_matching_function)
2795 ts->u.derived = NULL;
2796 if (gfc_current_state () != COMP_INTERFACE
2797 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2799 sym = gfc_find_dt_in_generic (sym);
2800 ts->u.derived = sym;
2802 return MATCH_YES;
2805 /* Search for the name but allow the components to be defined later. If
2806 type = -1, this typespec has been seen in a function declaration but
2807 the type could not be accessed at that point. The actual derived type is
2808 stored in a symtree with the first letter of the name capitalized; the
2809 symtree with the all lower-case name contains the associated
2810 generic function. */
2811 dt_name = gfc_get_string ("%c%s",
2812 (char) TOUPPER ((unsigned char) name[0]),
2813 (const char*)&name[1]);
2814 sym = NULL;
2815 dt_sym = NULL;
2816 if (ts->kind != -1)
2818 gfc_get_ha_symbol (name, &sym);
2819 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2821 gfc_error ("Type name '%s' at %C is ambiguous", name);
2822 return MATCH_ERROR;
2824 if (sym->generic && !dt_sym)
2825 dt_sym = gfc_find_dt_in_generic (sym);
2827 else if (ts->kind == -1)
2829 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2830 || gfc_current_ns->has_import_set;
2831 gfc_find_symbol (name, NULL, iface, &sym);
2832 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2834 gfc_error ("Type name '%s' at %C is ambiguous", name);
2835 return MATCH_ERROR;
2837 if (sym && sym->generic && !dt_sym)
2838 dt_sym = gfc_find_dt_in_generic (sym);
2840 ts->kind = 0;
2841 if (sym == NULL)
2842 return MATCH_NO;
2845 if ((sym->attr.flavor != FL_UNKNOWN
2846 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2847 || sym->attr.subroutine)
2849 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2850 "entity at %L, which has the same name", name,
2851 &sym->declared_at);
2852 return MATCH_ERROR;
2855 gfc_set_sym_referenced (sym);
2856 if (!sym->attr.generic
2857 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2858 return MATCH_ERROR;
2860 if (!sym->attr.function
2861 && !gfc_add_function (&sym->attr, sym->name, NULL))
2862 return MATCH_ERROR;
2864 if (!dt_sym)
2866 gfc_interface *intr, *head;
2868 /* Use upper case to save the actual derived-type symbol. */
2869 gfc_get_symbol (dt_name, NULL, &dt_sym);
2870 dt_sym->name = gfc_get_string (sym->name);
2871 head = sym->generic;
2872 intr = gfc_get_interface ();
2873 intr->sym = dt_sym;
2874 intr->where = gfc_current_locus;
2875 intr->next = head;
2876 sym->generic = intr;
2877 sym->attr.if_source = IFSRC_DECL;
2880 gfc_set_sym_referenced (dt_sym);
2882 if (dt_sym->attr.flavor != FL_DERIVED
2883 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2884 return MATCH_ERROR;
2886 ts->u.derived = dt_sym;
2888 return MATCH_YES;
2890 get_kind:
2891 if (matched_type
2892 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2893 "intrinsic-type-spec at %C"))
2894 return MATCH_ERROR;
2896 /* For all types except double, derived and character, look for an
2897 optional kind specifier. MATCH_NO is actually OK at this point. */
2898 if (implicit_flag == 1)
2900 if (matched_type && gfc_match_char (')') != MATCH_YES)
2901 return MATCH_ERROR;
2903 return MATCH_YES;
2906 if (gfc_current_form == FORM_FREE)
2908 c = gfc_peek_ascii_char ();
2909 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2910 && c != ':' && c != ',')
2912 if (matched_type && c == ')')
2914 gfc_next_ascii_char ();
2915 return MATCH_YES;
2917 return MATCH_NO;
2921 m = gfc_match_kind_spec (ts, false);
2922 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2923 m = gfc_match_old_kind_spec (ts);
2925 if (matched_type && gfc_match_char (')') != MATCH_YES)
2926 return MATCH_ERROR;
2928 /* Defer association of the KIND expression of function results
2929 until after USE and IMPORT statements. */
2930 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2931 || gfc_matching_function)
2932 return MATCH_YES;
2934 if (m == MATCH_NO)
2935 m = MATCH_YES; /* No kind specifier found. */
2937 return m;
2941 /* Match an IMPLICIT NONE statement. Actually, this statement is
2942 already matched in parse.c, or we would not end up here in the
2943 first place. So the only thing we need to check, is if there is
2944 trailing garbage. If not, the match is successful. */
2946 match
2947 gfc_match_implicit_none (void)
2949 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2953 /* Match the letter range(s) of an IMPLICIT statement. */
2955 static match
2956 match_implicit_range (void)
2958 char c, c1, c2;
2959 int inner;
2960 locus cur_loc;
2962 cur_loc = gfc_current_locus;
2964 gfc_gobble_whitespace ();
2965 c = gfc_next_ascii_char ();
2966 if (c != '(')
2968 gfc_error ("Missing character range in IMPLICIT at %C");
2969 goto bad;
2972 inner = 1;
2973 while (inner)
2975 gfc_gobble_whitespace ();
2976 c1 = gfc_next_ascii_char ();
2977 if (!ISALPHA (c1))
2978 goto bad;
2980 gfc_gobble_whitespace ();
2981 c = gfc_next_ascii_char ();
2983 switch (c)
2985 case ')':
2986 inner = 0; /* Fall through. */
2988 case ',':
2989 c2 = c1;
2990 break;
2992 case '-':
2993 gfc_gobble_whitespace ();
2994 c2 = gfc_next_ascii_char ();
2995 if (!ISALPHA (c2))
2996 goto bad;
2998 gfc_gobble_whitespace ();
2999 c = gfc_next_ascii_char ();
3001 if ((c != ',') && (c != ')'))
3002 goto bad;
3003 if (c == ')')
3004 inner = 0;
3006 break;
3008 default:
3009 goto bad;
3012 if (c1 > c2)
3014 gfc_error ("Letters must be in alphabetic order in "
3015 "IMPLICIT statement at %C");
3016 goto bad;
3019 /* See if we can add the newly matched range to the pending
3020 implicits from this IMPLICIT statement. We do not check for
3021 conflicts with whatever earlier IMPLICIT statements may have
3022 set. This is done when we've successfully finished matching
3023 the current one. */
3024 if (!gfc_add_new_implicit_range (c1, c2))
3025 goto bad;
3028 return MATCH_YES;
3030 bad:
3031 gfc_syntax_error (ST_IMPLICIT);
3033 gfc_current_locus = cur_loc;
3034 return MATCH_ERROR;
3038 /* Match an IMPLICIT statement, storing the types for
3039 gfc_set_implicit() if the statement is accepted by the parser.
3040 There is a strange looking, but legal syntactic construction
3041 possible. It looks like:
3043 IMPLICIT INTEGER (a-b) (c-d)
3045 This is legal if "a-b" is a constant expression that happens to
3046 equal one of the legal kinds for integers. The real problem
3047 happens with an implicit specification that looks like:
3049 IMPLICIT INTEGER (a-b)
3051 In this case, a typespec matcher that is "greedy" (as most of the
3052 matchers are) gobbles the character range as a kindspec, leaving
3053 nothing left. We therefore have to go a bit more slowly in the
3054 matching process by inhibiting the kindspec checking during
3055 typespec matching and checking for a kind later. */
3057 match
3058 gfc_match_implicit (void)
3060 gfc_typespec ts;
3061 locus cur_loc;
3062 char c;
3063 match m;
3065 gfc_clear_ts (&ts);
3067 /* We don't allow empty implicit statements. */
3068 if (gfc_match_eos () == MATCH_YES)
3070 gfc_error ("Empty IMPLICIT statement at %C");
3071 return MATCH_ERROR;
3076 /* First cleanup. */
3077 gfc_clear_new_implicit ();
3079 /* A basic type is mandatory here. */
3080 m = gfc_match_decl_type_spec (&ts, 1);
3081 if (m == MATCH_ERROR)
3082 goto error;
3083 if (m == MATCH_NO)
3084 goto syntax;
3086 cur_loc = gfc_current_locus;
3087 m = match_implicit_range ();
3089 if (m == MATCH_YES)
3091 /* We may have <TYPE> (<RANGE>). */
3092 gfc_gobble_whitespace ();
3093 c = gfc_next_ascii_char ();
3094 if ((c == '\n') || (c == ','))
3096 /* Check for CHARACTER with no length parameter. */
3097 if (ts.type == BT_CHARACTER && !ts.u.cl)
3099 ts.kind = gfc_default_character_kind;
3100 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3101 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3102 NULL, 1);
3105 /* Record the Successful match. */
3106 if (!gfc_merge_new_implicit (&ts))
3107 return MATCH_ERROR;
3108 continue;
3111 gfc_current_locus = cur_loc;
3114 /* Discard the (incorrectly) matched range. */
3115 gfc_clear_new_implicit ();
3117 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3118 if (ts.type == BT_CHARACTER)
3119 m = gfc_match_char_spec (&ts);
3120 else
3122 m = gfc_match_kind_spec (&ts, false);
3123 if (m == MATCH_NO)
3125 m = gfc_match_old_kind_spec (&ts);
3126 if (m == MATCH_ERROR)
3127 goto error;
3128 if (m == MATCH_NO)
3129 goto syntax;
3132 if (m == MATCH_ERROR)
3133 goto error;
3135 m = match_implicit_range ();
3136 if (m == MATCH_ERROR)
3137 goto error;
3138 if (m == MATCH_NO)
3139 goto syntax;
3141 gfc_gobble_whitespace ();
3142 c = gfc_next_ascii_char ();
3143 if ((c != '\n') && (c != ','))
3144 goto syntax;
3146 if (!gfc_merge_new_implicit (&ts))
3147 return MATCH_ERROR;
3149 while (c == ',');
3151 return MATCH_YES;
3153 syntax:
3154 gfc_syntax_error (ST_IMPLICIT);
3156 error:
3157 return MATCH_ERROR;
3161 match
3162 gfc_match_import (void)
3164 char name[GFC_MAX_SYMBOL_LEN + 1];
3165 match m;
3166 gfc_symbol *sym;
3167 gfc_symtree *st;
3169 if (gfc_current_ns->proc_name == NULL
3170 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3172 gfc_error ("IMPORT statement at %C only permitted in "
3173 "an INTERFACE body");
3174 return MATCH_ERROR;
3177 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3178 return MATCH_ERROR;
3180 if (gfc_match_eos () == MATCH_YES)
3182 /* All host variables should be imported. */
3183 gfc_current_ns->has_import_set = 1;
3184 return MATCH_YES;
3187 if (gfc_match (" ::") == MATCH_YES)
3189 if (gfc_match_eos () == MATCH_YES)
3191 gfc_error ("Expecting list of named entities at %C");
3192 return MATCH_ERROR;
3196 for(;;)
3198 sym = NULL;
3199 m = gfc_match (" %n", name);
3200 switch (m)
3202 case MATCH_YES:
3203 if (gfc_current_ns->parent != NULL
3204 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3206 gfc_error ("Type name '%s' at %C is ambiguous", name);
3207 return MATCH_ERROR;
3209 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3210 && gfc_find_symbol (name,
3211 gfc_current_ns->proc_name->ns->parent,
3212 1, &sym))
3214 gfc_error ("Type name '%s' at %C is ambiguous", name);
3215 return MATCH_ERROR;
3218 if (sym == NULL)
3220 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3221 "at %C - does not exist.", name);
3222 return MATCH_ERROR;
3225 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3227 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3228 "at %C.", name);
3229 goto next_item;
3232 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3233 st->n.sym = sym;
3234 sym->refs++;
3235 sym->attr.imported = 1;
3237 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3239 /* The actual derived type is stored in a symtree with the first
3240 letter of the name capitalized; the symtree with the all
3241 lower-case name contains the associated generic function. */
3242 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3243 gfc_get_string ("%c%s",
3244 (char) TOUPPER ((unsigned char) name[0]),
3245 &name[1]));
3246 st->n.sym = sym;
3247 sym->refs++;
3248 sym->attr.imported = 1;
3251 goto next_item;
3253 case MATCH_NO:
3254 break;
3256 case MATCH_ERROR:
3257 return MATCH_ERROR;
3260 next_item:
3261 if (gfc_match_eos () == MATCH_YES)
3262 break;
3263 if (gfc_match_char (',') != MATCH_YES)
3264 goto syntax;
3267 return MATCH_YES;
3269 syntax:
3270 gfc_error ("Syntax error in IMPORT statement at %C");
3271 return MATCH_ERROR;
3275 /* A minimal implementation of gfc_match without whitespace, escape
3276 characters or variable arguments. Returns true if the next
3277 characters match the TARGET template exactly. */
3279 static bool
3280 match_string_p (const char *target)
3282 const char *p;
3284 for (p = target; *p; p++)
3285 if ((char) gfc_next_ascii_char () != *p)
3286 return false;
3287 return true;
3290 /* Matches an attribute specification including array specs. If
3291 successful, leaves the variables current_attr and current_as
3292 holding the specification. Also sets the colon_seen variable for
3293 later use by matchers associated with initializations.
3295 This subroutine is a little tricky in the sense that we don't know
3296 if we really have an attr-spec until we hit the double colon.
3297 Until that time, we can only return MATCH_NO. This forces us to
3298 check for duplicate specification at this level. */
3300 static match
3301 match_attr_spec (void)
3303 /* Modifiers that can exist in a type statement. */
3304 enum
3305 { GFC_DECL_BEGIN = 0,
3306 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3307 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3308 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3309 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3310 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3311 DECL_NONE, GFC_DECL_END /* Sentinel */
3314 /* GFC_DECL_END is the sentinel, index starts at 0. */
3315 #define NUM_DECL GFC_DECL_END
3317 locus start, seen_at[NUM_DECL];
3318 int seen[NUM_DECL];
3319 unsigned int d;
3320 const char *attr;
3321 match m;
3322 bool t;
3324 gfc_clear_attr (&current_attr);
3325 start = gfc_current_locus;
3327 current_as = NULL;
3328 colon_seen = 0;
3330 /* See if we get all of the keywords up to the final double colon. */
3331 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3332 seen[d] = 0;
3334 for (;;)
3336 char ch;
3338 d = DECL_NONE;
3339 gfc_gobble_whitespace ();
3341 ch = gfc_next_ascii_char ();
3342 if (ch == ':')
3344 /* This is the successful exit condition for the loop. */
3345 if (gfc_next_ascii_char () == ':')
3346 break;
3348 else if (ch == ',')
3350 gfc_gobble_whitespace ();
3351 switch (gfc_peek_ascii_char ())
3353 case 'a':
3354 gfc_next_ascii_char ();
3355 switch (gfc_next_ascii_char ())
3357 case 'l':
3358 if (match_string_p ("locatable"))
3360 /* Matched "allocatable". */
3361 d = DECL_ALLOCATABLE;
3363 break;
3365 case 's':
3366 if (match_string_p ("ynchronous"))
3368 /* Matched "asynchronous". */
3369 d = DECL_ASYNCHRONOUS;
3371 break;
3373 break;
3375 case 'b':
3376 /* Try and match the bind(c). */
3377 m = gfc_match_bind_c (NULL, true);
3378 if (m == MATCH_YES)
3379 d = DECL_IS_BIND_C;
3380 else if (m == MATCH_ERROR)
3381 goto cleanup;
3382 break;
3384 case 'c':
3385 gfc_next_ascii_char ();
3386 if ('o' != gfc_next_ascii_char ())
3387 break;
3388 switch (gfc_next_ascii_char ())
3390 case 'd':
3391 if (match_string_p ("imension"))
3393 d = DECL_CODIMENSION;
3394 break;
3396 case 'n':
3397 if (match_string_p ("tiguous"))
3399 d = DECL_CONTIGUOUS;
3400 break;
3403 break;
3405 case 'd':
3406 if (match_string_p ("dimension"))
3407 d = DECL_DIMENSION;
3408 break;
3410 case 'e':
3411 if (match_string_p ("external"))
3412 d = DECL_EXTERNAL;
3413 break;
3415 case 'i':
3416 if (match_string_p ("int"))
3418 ch = gfc_next_ascii_char ();
3419 if (ch == 'e')
3421 if (match_string_p ("nt"))
3423 /* Matched "intent". */
3424 /* TODO: Call match_intent_spec from here. */
3425 if (gfc_match (" ( in out )") == MATCH_YES)
3426 d = DECL_INOUT;
3427 else if (gfc_match (" ( in )") == MATCH_YES)
3428 d = DECL_IN;
3429 else if (gfc_match (" ( out )") == MATCH_YES)
3430 d = DECL_OUT;
3433 else if (ch == 'r')
3435 if (match_string_p ("insic"))
3437 /* Matched "intrinsic". */
3438 d = DECL_INTRINSIC;
3442 break;
3444 case 'o':
3445 if (match_string_p ("optional"))
3446 d = DECL_OPTIONAL;
3447 break;
3449 case 'p':
3450 gfc_next_ascii_char ();
3451 switch (gfc_next_ascii_char ())
3453 case 'a':
3454 if (match_string_p ("rameter"))
3456 /* Matched "parameter". */
3457 d = DECL_PARAMETER;
3459 break;
3461 case 'o':
3462 if (match_string_p ("inter"))
3464 /* Matched "pointer". */
3465 d = DECL_POINTER;
3467 break;
3469 case 'r':
3470 ch = gfc_next_ascii_char ();
3471 if (ch == 'i')
3473 if (match_string_p ("vate"))
3475 /* Matched "private". */
3476 d = DECL_PRIVATE;
3479 else if (ch == 'o')
3481 if (match_string_p ("tected"))
3483 /* Matched "protected". */
3484 d = DECL_PROTECTED;
3487 break;
3489 case 'u':
3490 if (match_string_p ("blic"))
3492 /* Matched "public". */
3493 d = DECL_PUBLIC;
3495 break;
3497 break;
3499 case 's':
3500 if (match_string_p ("save"))
3501 d = DECL_SAVE;
3502 break;
3504 case 't':
3505 if (match_string_p ("target"))
3506 d = DECL_TARGET;
3507 break;
3509 case 'v':
3510 gfc_next_ascii_char ();
3511 ch = gfc_next_ascii_char ();
3512 if (ch == 'a')
3514 if (match_string_p ("lue"))
3516 /* Matched "value". */
3517 d = DECL_VALUE;
3520 else if (ch == 'o')
3522 if (match_string_p ("latile"))
3524 /* Matched "volatile". */
3525 d = DECL_VOLATILE;
3528 break;
3532 /* No double colon and no recognizable decl_type, so assume that
3533 we've been looking at something else the whole time. */
3534 if (d == DECL_NONE)
3536 m = MATCH_NO;
3537 goto cleanup;
3540 /* Check to make sure any parens are paired up correctly. */
3541 if (gfc_match_parens () == MATCH_ERROR)
3543 m = MATCH_ERROR;
3544 goto cleanup;
3547 seen[d]++;
3548 seen_at[d] = gfc_current_locus;
3550 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3552 gfc_array_spec *as = NULL;
3554 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3555 d == DECL_CODIMENSION);
3557 if (current_as == NULL)
3558 current_as = as;
3559 else if (m == MATCH_YES)
3561 if (!merge_array_spec (as, current_as, false))
3562 m = MATCH_ERROR;
3563 free (as);
3566 if (m == MATCH_NO)
3568 if (d == DECL_CODIMENSION)
3569 gfc_error ("Missing codimension specification at %C");
3570 else
3571 gfc_error ("Missing dimension specification at %C");
3572 m = MATCH_ERROR;
3575 if (m == MATCH_ERROR)
3576 goto cleanup;
3580 /* Since we've seen a double colon, we have to be looking at an
3581 attr-spec. This means that we can now issue errors. */
3582 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3583 if (seen[d] > 1)
3585 switch (d)
3587 case DECL_ALLOCATABLE:
3588 attr = "ALLOCATABLE";
3589 break;
3590 case DECL_ASYNCHRONOUS:
3591 attr = "ASYNCHRONOUS";
3592 break;
3593 case DECL_CODIMENSION:
3594 attr = "CODIMENSION";
3595 break;
3596 case DECL_CONTIGUOUS:
3597 attr = "CONTIGUOUS";
3598 break;
3599 case DECL_DIMENSION:
3600 attr = "DIMENSION";
3601 break;
3602 case DECL_EXTERNAL:
3603 attr = "EXTERNAL";
3604 break;
3605 case DECL_IN:
3606 attr = "INTENT (IN)";
3607 break;
3608 case DECL_OUT:
3609 attr = "INTENT (OUT)";
3610 break;
3611 case DECL_INOUT:
3612 attr = "INTENT (IN OUT)";
3613 break;
3614 case DECL_INTRINSIC:
3615 attr = "INTRINSIC";
3616 break;
3617 case DECL_OPTIONAL:
3618 attr = "OPTIONAL";
3619 break;
3620 case DECL_PARAMETER:
3621 attr = "PARAMETER";
3622 break;
3623 case DECL_POINTER:
3624 attr = "POINTER";
3625 break;
3626 case DECL_PROTECTED:
3627 attr = "PROTECTED";
3628 break;
3629 case DECL_PRIVATE:
3630 attr = "PRIVATE";
3631 break;
3632 case DECL_PUBLIC:
3633 attr = "PUBLIC";
3634 break;
3635 case DECL_SAVE:
3636 attr = "SAVE";
3637 break;
3638 case DECL_TARGET:
3639 attr = "TARGET";
3640 break;
3641 case DECL_IS_BIND_C:
3642 attr = "IS_BIND_C";
3643 break;
3644 case DECL_VALUE:
3645 attr = "VALUE";
3646 break;
3647 case DECL_VOLATILE:
3648 attr = "VOLATILE";
3649 break;
3650 default:
3651 attr = NULL; /* This shouldn't happen. */
3654 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3655 m = MATCH_ERROR;
3656 goto cleanup;
3659 /* Now that we've dealt with duplicate attributes, add the attributes
3660 to the current attribute. */
3661 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3663 if (seen[d] == 0)
3664 continue;
3666 if (gfc_current_state () == COMP_DERIVED
3667 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3668 && d != DECL_POINTER && d != DECL_PRIVATE
3669 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3671 if (d == DECL_ALLOCATABLE)
3673 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3674 "attribute at %C in a TYPE definition"))
3676 m = MATCH_ERROR;
3677 goto cleanup;
3680 else
3682 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3683 &seen_at[d]);
3684 m = MATCH_ERROR;
3685 goto cleanup;
3689 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3690 && gfc_current_state () != COMP_MODULE)
3692 if (d == DECL_PRIVATE)
3693 attr = "PRIVATE";
3694 else
3695 attr = "PUBLIC";
3696 if (gfc_current_state () == COMP_DERIVED
3697 && gfc_state_stack->previous
3698 && gfc_state_stack->previous->state == COMP_MODULE)
3700 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3701 "at %L in a TYPE definition", attr,
3702 &seen_at[d]))
3704 m = MATCH_ERROR;
3705 goto cleanup;
3708 else
3710 gfc_error ("%s attribute at %L is not allowed outside of the "
3711 "specification part of a module", attr, &seen_at[d]);
3712 m = MATCH_ERROR;
3713 goto cleanup;
3717 switch (d)
3719 case DECL_ALLOCATABLE:
3720 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3721 break;
3723 case DECL_ASYNCHRONOUS:
3724 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3725 t = false;
3726 else
3727 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3728 break;
3730 case DECL_CODIMENSION:
3731 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3732 break;
3734 case DECL_CONTIGUOUS:
3735 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3736 t = false;
3737 else
3738 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3739 break;
3741 case DECL_DIMENSION:
3742 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3743 break;
3745 case DECL_EXTERNAL:
3746 t = gfc_add_external (&current_attr, &seen_at[d]);
3747 break;
3749 case DECL_IN:
3750 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3751 break;
3753 case DECL_OUT:
3754 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3755 break;
3757 case DECL_INOUT:
3758 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3759 break;
3761 case DECL_INTRINSIC:
3762 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3763 break;
3765 case DECL_OPTIONAL:
3766 t = gfc_add_optional (&current_attr, &seen_at[d]);
3767 break;
3769 case DECL_PARAMETER:
3770 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3771 break;
3773 case DECL_POINTER:
3774 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3775 break;
3777 case DECL_PROTECTED:
3778 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3780 gfc_error ("PROTECTED at %C only allowed in specification "
3781 "part of a module");
3782 t = false;
3783 break;
3786 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3787 t = false;
3788 else
3789 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3790 break;
3792 case DECL_PRIVATE:
3793 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3794 &seen_at[d]);
3795 break;
3797 case DECL_PUBLIC:
3798 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3799 &seen_at[d]);
3800 break;
3802 case DECL_SAVE:
3803 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3804 break;
3806 case DECL_TARGET:
3807 t = gfc_add_target (&current_attr, &seen_at[d]);
3808 break;
3810 case DECL_IS_BIND_C:
3811 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3812 break;
3814 case DECL_VALUE:
3815 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
3816 t = false;
3817 else
3818 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3819 break;
3821 case DECL_VOLATILE:
3822 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
3823 t = false;
3824 else
3825 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3826 break;
3828 default:
3829 gfc_internal_error ("match_attr_spec(): Bad attribute");
3832 if (!t)
3834 m = MATCH_ERROR;
3835 goto cleanup;
3839 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3840 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3841 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3842 current_attr.save = SAVE_IMPLICIT;
3844 colon_seen = 1;
3845 return MATCH_YES;
3847 cleanup:
3848 gfc_current_locus = start;
3849 gfc_free_array_spec (current_as);
3850 current_as = NULL;
3851 return m;
3855 /* Set the binding label, dest_label, either with the binding label
3856 stored in the given gfc_typespec, ts, or if none was provided, it
3857 will be the symbol name in all lower case, as required by the draft
3858 (J3/04-007, section 15.4.1). If a binding label was given and
3859 there is more than one argument (num_idents), it is an error. */
3861 static bool
3862 set_binding_label (const char **dest_label, const char *sym_name,
3863 int num_idents)
3865 if (num_idents > 1 && has_name_equals)
3867 gfc_error ("Multiple identifiers provided with "
3868 "single NAME= specifier at %C");
3869 return false;
3872 if (curr_binding_label)
3873 /* Binding label given; store in temp holder till have sym. */
3874 *dest_label = curr_binding_label;
3875 else
3877 /* No binding label given, and the NAME= specifier did not exist,
3878 which means there was no NAME="". */
3879 if (sym_name != NULL && has_name_equals == 0)
3880 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3883 return true;
3887 /* Set the status of the given common block as being BIND(C) or not,
3888 depending on the given parameter, is_bind_c. */
3890 void
3891 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3893 com_block->is_bind_c = is_bind_c;
3894 return;
3898 /* Verify that the given gfc_typespec is for a C interoperable type. */
3900 bool
3901 gfc_verify_c_interop (gfc_typespec *ts)
3903 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3904 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3905 ? true : false;
3906 else if (ts->type == BT_CLASS)
3907 return false;
3908 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
3909 return false;
3911 return true;
3915 /* Verify that the variables of a given common block, which has been
3916 defined with the attribute specifier bind(c), to be of a C
3917 interoperable type. Errors will be reported here, if
3918 encountered. */
3920 bool
3921 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3923 gfc_symbol *curr_sym = NULL;
3924 bool retval = true;
3926 curr_sym = com_block->head;
3928 /* Make sure we have at least one symbol. */
3929 if (curr_sym == NULL)
3930 return retval;
3932 /* Here we know we have a symbol, so we'll execute this loop
3933 at least once. */
3936 /* The second to last param, 1, says this is in a common block. */
3937 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3938 curr_sym = curr_sym->common_next;
3939 } while (curr_sym != NULL);
3941 return retval;
3945 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3946 an appropriate error message is reported. */
3948 bool
3949 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3950 int is_in_common, gfc_common_head *com_block)
3952 bool bind_c_function = false;
3953 bool retval = true;
3955 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3956 bind_c_function = true;
3958 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3960 tmp_sym = tmp_sym->result;
3961 /* Make sure it wasn't an implicitly typed result. */
3962 if (tmp_sym->attr.implicit_type && gfc_option.warn_c_binding_type)
3964 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3965 "%L may not be C interoperable", tmp_sym->name,
3966 &tmp_sym->declared_at);
3967 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3968 /* Mark it as C interoperable to prevent duplicate warnings. */
3969 tmp_sym->ts.is_c_interop = 1;
3970 tmp_sym->attr.is_c_interop = 1;
3974 /* Here, we know we have the bind(c) attribute, so if we have
3975 enough type info, then verify that it's a C interop kind.
3976 The info could be in the symbol already, or possibly still in
3977 the given ts (current_ts), so look in both. */
3978 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3980 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
3982 /* See if we're dealing with a sym in a common block or not. */
3983 if (is_in_common == 1 && gfc_option.warn_c_binding_type)
3985 gfc_warning ("Variable '%s' in common block '%s' at %L "
3986 "may not be a C interoperable "
3987 "kind though common block '%s' is BIND(C)",
3988 tmp_sym->name, com_block->name,
3989 &(tmp_sym->declared_at), com_block->name);
3991 else
3993 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3994 gfc_error ("Type declaration '%s' at %L is not C "
3995 "interoperable but it is BIND(C)",
3996 tmp_sym->name, &(tmp_sym->declared_at));
3997 else if (gfc_option.warn_c_binding_type)
3998 gfc_warning ("Variable '%s' at %L "
3999 "may not be a C interoperable "
4000 "kind but it is bind(c)",
4001 tmp_sym->name, &(tmp_sym->declared_at));
4005 /* Variables declared w/in a common block can't be bind(c)
4006 since there's no way for C to see these variables, so there's
4007 semantically no reason for the attribute. */
4008 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4010 gfc_error ("Variable '%s' in common block '%s' at "
4011 "%L cannot be declared with BIND(C) "
4012 "since it is not a global",
4013 tmp_sym->name, com_block->name,
4014 &(tmp_sym->declared_at));
4015 retval = false;
4018 /* Scalar variables that are bind(c) can not have the pointer
4019 or allocatable attributes. */
4020 if (tmp_sym->attr.is_bind_c == 1)
4022 if (tmp_sym->attr.pointer == 1)
4024 gfc_error ("Variable '%s' at %L cannot have both the "
4025 "POINTER and BIND(C) attributes",
4026 tmp_sym->name, &(tmp_sym->declared_at));
4027 retval = false;
4030 if (tmp_sym->attr.allocatable == 1)
4032 gfc_error ("Variable '%s' at %L cannot have both the "
4033 "ALLOCATABLE and BIND(C) attributes",
4034 tmp_sym->name, &(tmp_sym->declared_at));
4035 retval = false;
4040 /* If it is a BIND(C) function, make sure the return value is a
4041 scalar value. The previous tests in this function made sure
4042 the type is interoperable. */
4043 if (bind_c_function && tmp_sym->as != NULL)
4044 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4045 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4047 /* BIND(C) functions can not return a character string. */
4048 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4049 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4050 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4051 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4052 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4053 "be a character string", tmp_sym->name,
4054 &(tmp_sym->declared_at));
4057 /* See if the symbol has been marked as private. If it has, make sure
4058 there is no binding label and warn the user if there is one. */
4059 if (tmp_sym->attr.access == ACCESS_PRIVATE
4060 && tmp_sym->binding_label)
4061 /* Use gfc_warning_now because we won't say that the symbol fails
4062 just because of this. */
4063 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4064 "given the binding label '%s'", tmp_sym->name,
4065 &(tmp_sym->declared_at), tmp_sym->binding_label);
4067 return retval;
4071 /* Set the appropriate fields for a symbol that's been declared as
4072 BIND(C) (the is_bind_c flag and the binding label), and verify that
4073 the type is C interoperable. Errors are reported by the functions
4074 used to set/test these fields. */
4076 bool
4077 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4079 bool retval = true;
4081 /* TODO: Do we need to make sure the vars aren't marked private? */
4083 /* Set the is_bind_c bit in symbol_attribute. */
4084 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4086 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4087 return false;
4089 return retval;
4093 /* Set the fields marking the given common block as BIND(C), including
4094 a binding label, and report any errors encountered. */
4096 bool
4097 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4099 bool retval = true;
4101 /* destLabel, common name, typespec (which may have binding label). */
4102 if (!set_binding_label (&com_block->binding_label, com_block->name,
4103 num_idents))
4104 return false;
4106 /* Set the given common block (com_block) to being bind(c) (1). */
4107 set_com_block_bind_c (com_block, 1);
4109 return retval;
4113 /* Retrieve the list of one or more identifiers that the given bind(c)
4114 attribute applies to. */
4116 bool
4117 get_bind_c_idents (void)
4119 char name[GFC_MAX_SYMBOL_LEN + 1];
4120 int num_idents = 0;
4121 gfc_symbol *tmp_sym = NULL;
4122 match found_id;
4123 gfc_common_head *com_block = NULL;
4125 if (gfc_match_name (name) == MATCH_YES)
4127 found_id = MATCH_YES;
4128 gfc_get_ha_symbol (name, &tmp_sym);
4130 else if (match_common_name (name) == MATCH_YES)
4132 found_id = MATCH_YES;
4133 com_block = gfc_get_common (name, 0);
4135 else
4137 gfc_error ("Need either entity or common block name for "
4138 "attribute specification statement at %C");
4139 return false;
4142 /* Save the current identifier and look for more. */
4145 /* Increment the number of identifiers found for this spec stmt. */
4146 num_idents++;
4148 /* Make sure we have a sym or com block, and verify that it can
4149 be bind(c). Set the appropriate field(s) and look for more
4150 identifiers. */
4151 if (tmp_sym != NULL || com_block != NULL)
4153 if (tmp_sym != NULL)
4155 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4156 return false;
4158 else
4160 if (!set_verify_bind_c_com_block (com_block, num_idents))
4161 return false;
4164 /* Look to see if we have another identifier. */
4165 tmp_sym = NULL;
4166 if (gfc_match_eos () == MATCH_YES)
4167 found_id = MATCH_NO;
4168 else if (gfc_match_char (',') != MATCH_YES)
4169 found_id = MATCH_NO;
4170 else if (gfc_match_name (name) == MATCH_YES)
4172 found_id = MATCH_YES;
4173 gfc_get_ha_symbol (name, &tmp_sym);
4175 else if (match_common_name (name) == MATCH_YES)
4177 found_id = MATCH_YES;
4178 com_block = gfc_get_common (name, 0);
4180 else
4182 gfc_error ("Missing entity or common block name for "
4183 "attribute specification statement at %C");
4184 return false;
4187 else
4189 gfc_internal_error ("Missing symbol");
4191 } while (found_id == MATCH_YES);
4193 /* if we get here we were successful */
4194 return true;
4198 /* Try and match a BIND(C) attribute specification statement. */
4200 match
4201 gfc_match_bind_c_stmt (void)
4203 match found_match = MATCH_NO;
4204 gfc_typespec *ts;
4206 ts = &current_ts;
4208 /* This may not be necessary. */
4209 gfc_clear_ts (ts);
4210 /* Clear the temporary binding label holder. */
4211 curr_binding_label = NULL;
4213 /* Look for the bind(c). */
4214 found_match = gfc_match_bind_c (NULL, true);
4216 if (found_match == MATCH_YES)
4218 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4219 return MATCH_ERROR;
4221 /* Look for the :: now, but it is not required. */
4222 gfc_match (" :: ");
4224 /* Get the identifier(s) that needs to be updated. This may need to
4225 change to hand the flag(s) for the attr specified so all identifiers
4226 found can have all appropriate parts updated (assuming that the same
4227 spec stmt can have multiple attrs, such as both bind(c) and
4228 allocatable...). */
4229 if (!get_bind_c_idents ())
4230 /* Error message should have printed already. */
4231 return MATCH_ERROR;
4234 return found_match;
4238 /* Match a data declaration statement. */
4240 match
4241 gfc_match_data_decl (void)
4243 gfc_symbol *sym;
4244 match m;
4245 int elem;
4247 num_idents_on_line = 0;
4249 m = gfc_match_decl_type_spec (&current_ts, 0);
4250 if (m != MATCH_YES)
4251 return m;
4253 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4254 && gfc_current_state () != COMP_DERIVED)
4256 sym = gfc_use_derived (current_ts.u.derived);
4258 if (sym == NULL)
4260 m = MATCH_ERROR;
4261 goto cleanup;
4264 current_ts.u.derived = sym;
4267 m = match_attr_spec ();
4268 if (m == MATCH_ERROR)
4270 m = MATCH_NO;
4271 goto cleanup;
4274 if (current_ts.type == BT_CLASS
4275 && current_ts.u.derived->attr.unlimited_polymorphic)
4276 goto ok;
4278 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4279 && current_ts.u.derived->components == NULL
4280 && !current_ts.u.derived->attr.zero_comp)
4283 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4284 goto ok;
4286 gfc_find_symbol (current_ts.u.derived->name,
4287 current_ts.u.derived->ns, 1, &sym);
4289 /* Any symbol that we find had better be a type definition
4290 which has its components defined. */
4291 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4292 && (current_ts.u.derived->components != NULL
4293 || current_ts.u.derived->attr.zero_comp))
4294 goto ok;
4296 gfc_error ("Derived type at %C has not been previously defined "
4297 "and so cannot appear in a derived type definition");
4298 m = MATCH_ERROR;
4299 goto cleanup;
4303 /* If we have an old-style character declaration, and no new-style
4304 attribute specifications, then there a comma is optional between
4305 the type specification and the variable list. */
4306 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4307 gfc_match_char (',');
4309 /* Give the types/attributes to symbols that follow. Give the element
4310 a number so that repeat character length expressions can be copied. */
4311 elem = 1;
4312 for (;;)
4314 num_idents_on_line++;
4315 m = variable_decl (elem++);
4316 if (m == MATCH_ERROR)
4317 goto cleanup;
4318 if (m == MATCH_NO)
4319 break;
4321 if (gfc_match_eos () == MATCH_YES)
4322 goto cleanup;
4323 if (gfc_match_char (',') != MATCH_YES)
4324 break;
4327 if (gfc_error_flag_test () == 0)
4328 gfc_error ("Syntax error in data declaration at %C");
4329 m = MATCH_ERROR;
4331 gfc_free_data_all (gfc_current_ns);
4333 cleanup:
4334 gfc_free_array_spec (current_as);
4335 current_as = NULL;
4336 return m;
4340 /* Match a prefix associated with a function or subroutine
4341 declaration. If the typespec pointer is nonnull, then a typespec
4342 can be matched. Note that if nothing matches, MATCH_YES is
4343 returned (the null string was matched). */
4345 match
4346 gfc_match_prefix (gfc_typespec *ts)
4348 bool seen_type;
4349 bool seen_impure;
4350 bool found_prefix;
4352 gfc_clear_attr (&current_attr);
4353 seen_type = false;
4354 seen_impure = false;
4356 gcc_assert (!gfc_matching_prefix);
4357 gfc_matching_prefix = true;
4361 found_prefix = false;
4363 if (!seen_type && ts != NULL
4364 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4365 && gfc_match_space () == MATCH_YES)
4368 seen_type = true;
4369 found_prefix = true;
4372 if (gfc_match ("elemental% ") == MATCH_YES)
4374 if (!gfc_add_elemental (&current_attr, NULL))
4375 goto error;
4377 found_prefix = true;
4380 if (gfc_match ("pure% ") == MATCH_YES)
4382 if (!gfc_add_pure (&current_attr, NULL))
4383 goto error;
4385 found_prefix = true;
4388 if (gfc_match ("recursive% ") == MATCH_YES)
4390 if (!gfc_add_recursive (&current_attr, NULL))
4391 goto error;
4393 found_prefix = true;
4396 /* IMPURE is a somewhat special case, as it needs not set an actual
4397 attribute but rather only prevents ELEMENTAL routines from being
4398 automatically PURE. */
4399 if (gfc_match ("impure% ") == MATCH_YES)
4401 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4402 goto error;
4404 seen_impure = true;
4405 found_prefix = true;
4408 while (found_prefix);
4410 /* IMPURE and PURE must not both appear, of course. */
4411 if (seen_impure && current_attr.pure)
4413 gfc_error ("PURE and IMPURE must not appear both at %C");
4414 goto error;
4417 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4418 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4420 if (!gfc_add_pure (&current_attr, NULL))
4421 goto error;
4424 /* At this point, the next item is not a prefix. */
4425 gcc_assert (gfc_matching_prefix);
4426 gfc_matching_prefix = false;
4427 return MATCH_YES;
4429 error:
4430 gcc_assert (gfc_matching_prefix);
4431 gfc_matching_prefix = false;
4432 return MATCH_ERROR;
4436 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4438 static bool
4439 copy_prefix (symbol_attribute *dest, locus *where)
4441 if (current_attr.pure && !gfc_add_pure (dest, where))
4442 return false;
4444 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4445 return false;
4447 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4448 return false;
4450 return true;
4454 /* Match a formal argument list. */
4456 match
4457 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4459 gfc_formal_arglist *head, *tail, *p, *q;
4460 char name[GFC_MAX_SYMBOL_LEN + 1];
4461 gfc_symbol *sym;
4462 match m;
4464 head = tail = NULL;
4466 if (gfc_match_char ('(') != MATCH_YES)
4468 if (null_flag)
4469 goto ok;
4470 return MATCH_NO;
4473 if (gfc_match_char (')') == MATCH_YES)
4474 goto ok;
4476 for (;;)
4478 if (gfc_match_char ('*') == MATCH_YES)
4480 sym = NULL;
4481 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4482 "at %C"))
4484 m = MATCH_ERROR;
4485 goto cleanup;
4488 else
4490 m = gfc_match_name (name);
4491 if (m != MATCH_YES)
4492 goto cleanup;
4494 if (gfc_get_symbol (name, NULL, &sym))
4495 goto cleanup;
4498 p = gfc_get_formal_arglist ();
4500 if (head == NULL)
4501 head = tail = p;
4502 else
4504 tail->next = p;
4505 tail = p;
4508 tail->sym = sym;
4510 /* We don't add the VARIABLE flavor because the name could be a
4511 dummy procedure. We don't apply these attributes to formal
4512 arguments of statement functions. */
4513 if (sym != NULL && !st_flag
4514 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4515 || !gfc_missing_attr (&sym->attr, NULL)))
4517 m = MATCH_ERROR;
4518 goto cleanup;
4521 /* The name of a program unit can be in a different namespace,
4522 so check for it explicitly. After the statement is accepted,
4523 the name is checked for especially in gfc_get_symbol(). */
4524 if (gfc_new_block != NULL && sym != NULL
4525 && strcmp (sym->name, gfc_new_block->name) == 0)
4527 gfc_error ("Name '%s' at %C is the name of the procedure",
4528 sym->name);
4529 m = MATCH_ERROR;
4530 goto cleanup;
4533 if (gfc_match_char (')') == MATCH_YES)
4534 goto ok;
4536 m = gfc_match_char (',');
4537 if (m != MATCH_YES)
4539 gfc_error ("Unexpected junk in formal argument list at %C");
4540 goto cleanup;
4545 /* Check for duplicate symbols in the formal argument list. */
4546 if (head != NULL)
4548 for (p = head; p->next; p = p->next)
4550 if (p->sym == NULL)
4551 continue;
4553 for (q = p->next; q; q = q->next)
4554 if (p->sym == q->sym)
4556 gfc_error ("Duplicate symbol '%s' in formal argument list "
4557 "at %C", p->sym->name);
4559 m = MATCH_ERROR;
4560 goto cleanup;
4565 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4567 m = MATCH_ERROR;
4568 goto cleanup;
4571 return MATCH_YES;
4573 cleanup:
4574 gfc_free_formal_arglist (head);
4575 return m;
4579 /* Match a RESULT specification following a function declaration or
4580 ENTRY statement. Also matches the end-of-statement. */
4582 static match
4583 match_result (gfc_symbol *function, gfc_symbol **result)
4585 char name[GFC_MAX_SYMBOL_LEN + 1];
4586 gfc_symbol *r;
4587 match m;
4589 if (gfc_match (" result (") != MATCH_YES)
4590 return MATCH_NO;
4592 m = gfc_match_name (name);
4593 if (m != MATCH_YES)
4594 return m;
4596 /* Get the right paren, and that's it because there could be the
4597 bind(c) attribute after the result clause. */
4598 if (gfc_match_char (')') != MATCH_YES)
4600 /* TODO: should report the missing right paren here. */
4601 return MATCH_ERROR;
4604 if (strcmp (function->name, name) == 0)
4606 gfc_error ("RESULT variable at %C must be different than function name");
4607 return MATCH_ERROR;
4610 if (gfc_get_symbol (name, NULL, &r))
4611 return MATCH_ERROR;
4613 if (!gfc_add_result (&r->attr, r->name, NULL))
4614 return MATCH_ERROR;
4616 *result = r;
4618 return MATCH_YES;
4622 /* Match a function suffix, which could be a combination of a result
4623 clause and BIND(C), either one, or neither. The draft does not
4624 require them to come in a specific order. */
4626 match
4627 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4629 match is_bind_c; /* Found bind(c). */
4630 match is_result; /* Found result clause. */
4631 match found_match; /* Status of whether we've found a good match. */
4632 char peek_char; /* Character we're going to peek at. */
4633 bool allow_binding_name;
4635 /* Initialize to having found nothing. */
4636 found_match = MATCH_NO;
4637 is_bind_c = MATCH_NO;
4638 is_result = MATCH_NO;
4640 /* Get the next char to narrow between result and bind(c). */
4641 gfc_gobble_whitespace ();
4642 peek_char = gfc_peek_ascii_char ();
4644 /* C binding names are not allowed for internal procedures. */
4645 if (gfc_current_state () == COMP_CONTAINS
4646 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4647 allow_binding_name = false;
4648 else
4649 allow_binding_name = true;
4651 switch (peek_char)
4653 case 'r':
4654 /* Look for result clause. */
4655 is_result = match_result (sym, result);
4656 if (is_result == MATCH_YES)
4658 /* Now see if there is a bind(c) after it. */
4659 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4660 /* We've found the result clause and possibly bind(c). */
4661 found_match = MATCH_YES;
4663 else
4664 /* This should only be MATCH_ERROR. */
4665 found_match = is_result;
4666 break;
4667 case 'b':
4668 /* Look for bind(c) first. */
4669 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4670 if (is_bind_c == MATCH_YES)
4672 /* Now see if a result clause followed it. */
4673 is_result = match_result (sym, result);
4674 found_match = MATCH_YES;
4676 else
4678 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4679 found_match = MATCH_ERROR;
4681 break;
4682 default:
4683 gfc_error ("Unexpected junk after function declaration at %C");
4684 found_match = MATCH_ERROR;
4685 break;
4688 if (is_bind_c == MATCH_YES)
4690 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4691 if (gfc_current_state () == COMP_CONTAINS
4692 && sym->ns->proc_name->attr.flavor != FL_MODULE
4693 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4694 "at %L may not be specified for an internal "
4695 "procedure", &gfc_current_locus))
4696 return MATCH_ERROR;
4698 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4699 return MATCH_ERROR;
4702 return found_match;
4706 /* Procedure pointer return value without RESULT statement:
4707 Add "hidden" result variable named "ppr@". */
4709 static bool
4710 add_hidden_procptr_result (gfc_symbol *sym)
4712 bool case1,case2;
4714 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4715 return false;
4717 /* First usage case: PROCEDURE and EXTERNAL statements. */
4718 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4719 && strcmp (gfc_current_block ()->name, sym->name) == 0
4720 && sym->attr.external;
4721 /* Second usage case: INTERFACE statements. */
4722 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4723 && gfc_state_stack->previous->state == COMP_FUNCTION
4724 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4726 if (case1 || case2)
4728 gfc_symtree *stree;
4729 if (case1)
4730 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4731 else if (case2)
4733 gfc_symtree *st2;
4734 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4735 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4736 st2->n.sym = stree->n.sym;
4738 sym->result = stree->n.sym;
4740 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4741 sym->result->attr.pointer = sym->attr.pointer;
4742 sym->result->attr.external = sym->attr.external;
4743 sym->result->attr.referenced = sym->attr.referenced;
4744 sym->result->ts = sym->ts;
4745 sym->attr.proc_pointer = 0;
4746 sym->attr.pointer = 0;
4747 sym->attr.external = 0;
4748 if (sym->result->attr.external && sym->result->attr.pointer)
4750 sym->result->attr.pointer = 0;
4751 sym->result->attr.proc_pointer = 1;
4754 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4756 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4757 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4758 && sym->result && sym->result != sym && sym->result->attr.external
4759 && sym == gfc_current_ns->proc_name
4760 && sym == sym->result->ns->proc_name
4761 && strcmp ("ppr@", sym->result->name) == 0)
4763 sym->result->attr.proc_pointer = 1;
4764 sym->attr.pointer = 0;
4765 return true;
4767 else
4768 return false;
4772 /* Match the interface for a PROCEDURE declaration,
4773 including brackets (R1212). */
4775 static match
4776 match_procedure_interface (gfc_symbol **proc_if)
4778 match m;
4779 gfc_symtree *st;
4780 locus old_loc, entry_loc;
4781 gfc_namespace *old_ns = gfc_current_ns;
4782 char name[GFC_MAX_SYMBOL_LEN + 1];
4784 old_loc = entry_loc = gfc_current_locus;
4785 gfc_clear_ts (&current_ts);
4787 if (gfc_match (" (") != MATCH_YES)
4789 gfc_current_locus = entry_loc;
4790 return MATCH_NO;
4793 /* Get the type spec. for the procedure interface. */
4794 old_loc = gfc_current_locus;
4795 m = gfc_match_decl_type_spec (&current_ts, 0);
4796 gfc_gobble_whitespace ();
4797 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4798 goto got_ts;
4800 if (m == MATCH_ERROR)
4801 return m;
4803 /* Procedure interface is itself a procedure. */
4804 gfc_current_locus = old_loc;
4805 m = gfc_match_name (name);
4807 /* First look to see if it is already accessible in the current
4808 namespace because it is use associated or contained. */
4809 st = NULL;
4810 if (gfc_find_sym_tree (name, NULL, 0, &st))
4811 return MATCH_ERROR;
4813 /* If it is still not found, then try the parent namespace, if it
4814 exists and create the symbol there if it is still not found. */
4815 if (gfc_current_ns->parent)
4816 gfc_current_ns = gfc_current_ns->parent;
4817 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4818 return MATCH_ERROR;
4820 gfc_current_ns = old_ns;
4821 *proc_if = st->n.sym;
4823 if (*proc_if)
4825 (*proc_if)->refs++;
4826 /* Resolve interface if possible. That way, attr.procedure is only set
4827 if it is declared by a later procedure-declaration-stmt, which is
4828 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4829 while ((*proc_if)->ts.interface)
4830 *proc_if = (*proc_if)->ts.interface;
4832 if ((*proc_if)->attr.flavor == FL_UNKNOWN
4833 && (*proc_if)->ts.type == BT_UNKNOWN
4834 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
4835 (*proc_if)->name, NULL))
4836 return MATCH_ERROR;
4839 got_ts:
4840 if (gfc_match (" )") != MATCH_YES)
4842 gfc_current_locus = entry_loc;
4843 return MATCH_NO;
4846 return MATCH_YES;
4850 /* Match a PROCEDURE declaration (R1211). */
4852 static match
4853 match_procedure_decl (void)
4855 match m;
4856 gfc_symbol *sym, *proc_if = NULL;
4857 int num;
4858 gfc_expr *initializer = NULL;
4860 /* Parse interface (with brackets). */
4861 m = match_procedure_interface (&proc_if);
4862 if (m != MATCH_YES)
4863 return m;
4865 /* Parse attributes (with colons). */
4866 m = match_attr_spec();
4867 if (m == MATCH_ERROR)
4868 return MATCH_ERROR;
4870 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
4872 current_attr.is_bind_c = 1;
4873 has_name_equals = 0;
4874 curr_binding_label = NULL;
4877 /* Get procedure symbols. */
4878 for(num=1;;num++)
4880 m = gfc_match_symbol (&sym, 0);
4881 if (m == MATCH_NO)
4882 goto syntax;
4883 else if (m == MATCH_ERROR)
4884 return m;
4886 /* Add current_attr to the symbol attributes. */
4887 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
4888 return MATCH_ERROR;
4890 if (sym->attr.is_bind_c)
4892 /* Check for C1218. */
4893 if (!proc_if || !proc_if->attr.is_bind_c)
4895 gfc_error ("BIND(C) attribute at %C requires "
4896 "an interface with BIND(C)");
4897 return MATCH_ERROR;
4899 /* Check for C1217. */
4900 if (has_name_equals && sym->attr.pointer)
4902 gfc_error ("BIND(C) procedure with NAME may not have "
4903 "POINTER attribute at %C");
4904 return MATCH_ERROR;
4906 if (has_name_equals && sym->attr.dummy)
4908 gfc_error ("Dummy procedure at %C may not have "
4909 "BIND(C) attribute with NAME");
4910 return MATCH_ERROR;
4912 /* Set binding label for BIND(C). */
4913 if (!set_binding_label (&sym->binding_label, sym->name, num))
4914 return MATCH_ERROR;
4917 if (!gfc_add_external (&sym->attr, NULL))
4918 return MATCH_ERROR;
4920 if (add_hidden_procptr_result (sym))
4921 sym = sym->result;
4923 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
4924 return MATCH_ERROR;
4926 /* Set interface. */
4927 if (proc_if != NULL)
4929 if (sym->ts.type != BT_UNKNOWN)
4931 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4932 sym->name, &gfc_current_locus,
4933 gfc_basic_typename (sym->ts.type));
4934 return MATCH_ERROR;
4936 sym->ts.interface = proc_if;
4937 sym->attr.untyped = 1;
4938 sym->attr.if_source = IFSRC_IFBODY;
4940 else if (current_ts.type != BT_UNKNOWN)
4942 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
4943 return MATCH_ERROR;
4944 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4945 sym->ts.interface->ts = current_ts;
4946 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4947 sym->ts.interface->attr.function = 1;
4948 sym->attr.function = 1;
4949 sym->attr.if_source = IFSRC_UNKNOWN;
4952 if (gfc_match (" =>") == MATCH_YES)
4954 if (!current_attr.pointer)
4956 gfc_error ("Initialization at %C isn't for a pointer variable");
4957 m = MATCH_ERROR;
4958 goto cleanup;
4961 m = match_pointer_init (&initializer, 1);
4962 if (m != MATCH_YES)
4963 goto cleanup;
4965 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
4966 goto cleanup;
4970 if (gfc_match_eos () == MATCH_YES)
4971 return MATCH_YES;
4972 if (gfc_match_char (',') != MATCH_YES)
4973 goto syntax;
4976 syntax:
4977 gfc_error ("Syntax error in PROCEDURE statement at %C");
4978 return MATCH_ERROR;
4980 cleanup:
4981 /* Free stuff up and return. */
4982 gfc_free_expr (initializer);
4983 return m;
4987 static match
4988 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4991 /* Match a procedure pointer component declaration (R445). */
4993 static match
4994 match_ppc_decl (void)
4996 match m;
4997 gfc_symbol *proc_if = NULL;
4998 gfc_typespec ts;
4999 int num;
5000 gfc_component *c;
5001 gfc_expr *initializer = NULL;
5002 gfc_typebound_proc* tb;
5003 char name[GFC_MAX_SYMBOL_LEN + 1];
5005 /* Parse interface (with brackets). */
5006 m = match_procedure_interface (&proc_if);
5007 if (m != MATCH_YES)
5008 goto syntax;
5010 /* Parse attributes. */
5011 tb = XCNEW (gfc_typebound_proc);
5012 tb->where = gfc_current_locus;
5013 m = match_binding_attributes (tb, false, true);
5014 if (m == MATCH_ERROR)
5015 return m;
5017 gfc_clear_attr (&current_attr);
5018 current_attr.procedure = 1;
5019 current_attr.proc_pointer = 1;
5020 current_attr.access = tb->access;
5021 current_attr.flavor = FL_PROCEDURE;
5023 /* Match the colons (required). */
5024 if (gfc_match (" ::") != MATCH_YES)
5026 gfc_error ("Expected '::' after binding-attributes at %C");
5027 return MATCH_ERROR;
5030 /* Check for C450. */
5031 if (!tb->nopass && proc_if == NULL)
5033 gfc_error("NOPASS or explicit interface required at %C");
5034 return MATCH_ERROR;
5037 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5038 return MATCH_ERROR;
5040 /* Match PPC names. */
5041 ts = current_ts;
5042 for(num=1;;num++)
5044 m = gfc_match_name (name);
5045 if (m == MATCH_NO)
5046 goto syntax;
5047 else if (m == MATCH_ERROR)
5048 return m;
5050 if (!gfc_add_component (gfc_current_block(), name, &c))
5051 return MATCH_ERROR;
5053 /* Add current_attr to the symbol attributes. */
5054 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5055 return MATCH_ERROR;
5057 if (!gfc_add_external (&c->attr, NULL))
5058 return MATCH_ERROR;
5060 if (!gfc_add_proc (&c->attr, name, NULL))
5061 return MATCH_ERROR;
5063 if (num == 1)
5064 c->tb = tb;
5065 else
5067 c->tb = XCNEW (gfc_typebound_proc);
5068 c->tb->where = gfc_current_locus;
5069 *c->tb = *tb;
5072 /* Set interface. */
5073 if (proc_if != NULL)
5075 c->ts.interface = proc_if;
5076 c->attr.untyped = 1;
5077 c->attr.if_source = IFSRC_IFBODY;
5079 else if (ts.type != BT_UNKNOWN)
5081 c->ts = ts;
5082 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5083 c->ts.interface->result = c->ts.interface;
5084 c->ts.interface->ts = ts;
5085 c->ts.interface->attr.flavor = FL_PROCEDURE;
5086 c->ts.interface->attr.function = 1;
5087 c->attr.function = 1;
5088 c->attr.if_source = IFSRC_UNKNOWN;
5091 if (gfc_match (" =>") == MATCH_YES)
5093 m = match_pointer_init (&initializer, 1);
5094 if (m != MATCH_YES)
5096 gfc_free_expr (initializer);
5097 return m;
5099 c->initializer = initializer;
5102 if (gfc_match_eos () == MATCH_YES)
5103 return MATCH_YES;
5104 if (gfc_match_char (',') != MATCH_YES)
5105 goto syntax;
5108 syntax:
5109 gfc_error ("Syntax error in procedure pointer component at %C");
5110 return MATCH_ERROR;
5114 /* Match a PROCEDURE declaration inside an interface (R1206). */
5116 static match
5117 match_procedure_in_interface (void)
5119 match m;
5120 gfc_symbol *sym;
5121 char name[GFC_MAX_SYMBOL_LEN + 1];
5122 locus old_locus;
5124 if (current_interface.type == INTERFACE_NAMELESS
5125 || current_interface.type == INTERFACE_ABSTRACT)
5127 gfc_error ("PROCEDURE at %C must be in a generic interface");
5128 return MATCH_ERROR;
5131 /* Check if the F2008 optional double colon appears. */
5132 gfc_gobble_whitespace ();
5133 old_locus = gfc_current_locus;
5134 if (gfc_match ("::") == MATCH_YES)
5136 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5137 "MODULE PROCEDURE statement at %L", &old_locus))
5138 return MATCH_ERROR;
5140 else
5141 gfc_current_locus = old_locus;
5143 for(;;)
5145 m = gfc_match_name (name);
5146 if (m == MATCH_NO)
5147 goto syntax;
5148 else if (m == MATCH_ERROR)
5149 return m;
5150 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5151 return MATCH_ERROR;
5153 if (!gfc_add_interface (sym))
5154 return MATCH_ERROR;
5156 if (gfc_match_eos () == MATCH_YES)
5157 break;
5158 if (gfc_match_char (',') != MATCH_YES)
5159 goto syntax;
5162 return MATCH_YES;
5164 syntax:
5165 gfc_error ("Syntax error in PROCEDURE statement at %C");
5166 return MATCH_ERROR;
5170 /* General matcher for PROCEDURE declarations. */
5172 static match match_procedure_in_type (void);
5174 match
5175 gfc_match_procedure (void)
5177 match m;
5179 switch (gfc_current_state ())
5181 case COMP_NONE:
5182 case COMP_PROGRAM:
5183 case COMP_MODULE:
5184 case COMP_SUBROUTINE:
5185 case COMP_FUNCTION:
5186 case COMP_BLOCK:
5187 m = match_procedure_decl ();
5188 break;
5189 case COMP_INTERFACE:
5190 m = match_procedure_in_interface ();
5191 break;
5192 case COMP_DERIVED:
5193 m = match_ppc_decl ();
5194 break;
5195 case COMP_DERIVED_CONTAINS:
5196 m = match_procedure_in_type ();
5197 break;
5198 default:
5199 return MATCH_NO;
5202 if (m != MATCH_YES)
5203 return m;
5205 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5206 return MATCH_ERROR;
5208 return m;
5212 /* Warn if a matched procedure has the same name as an intrinsic; this is
5213 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5214 parser-state-stack to find out whether we're in a module. */
5216 static void
5217 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5219 bool in_module;
5221 in_module = (gfc_state_stack->previous
5222 && gfc_state_stack->previous->state == COMP_MODULE);
5224 gfc_warn_intrinsic_shadow (sym, in_module, func);
5228 /* Match a function declaration. */
5230 match
5231 gfc_match_function_decl (void)
5233 char name[GFC_MAX_SYMBOL_LEN + 1];
5234 gfc_symbol *sym, *result;
5235 locus old_loc;
5236 match m;
5237 match suffix_match;
5238 match found_match; /* Status returned by match func. */
5240 if (gfc_current_state () != COMP_NONE
5241 && gfc_current_state () != COMP_INTERFACE
5242 && gfc_current_state () != COMP_CONTAINS)
5243 return MATCH_NO;
5245 gfc_clear_ts (&current_ts);
5247 old_loc = gfc_current_locus;
5249 m = gfc_match_prefix (&current_ts);
5250 if (m != MATCH_YES)
5252 gfc_current_locus = old_loc;
5253 return m;
5256 if (gfc_match ("function% %n", name) != MATCH_YES)
5258 gfc_current_locus = old_loc;
5259 return MATCH_NO;
5261 if (get_proc_name (name, &sym, false))
5262 return MATCH_ERROR;
5264 if (add_hidden_procptr_result (sym))
5265 sym = sym->result;
5267 gfc_new_block = sym;
5269 m = gfc_match_formal_arglist (sym, 0, 0);
5270 if (m == MATCH_NO)
5272 gfc_error ("Expected formal argument list in function "
5273 "definition at %C");
5274 m = MATCH_ERROR;
5275 goto cleanup;
5277 else if (m == MATCH_ERROR)
5278 goto cleanup;
5280 result = NULL;
5282 /* According to the draft, the bind(c) and result clause can
5283 come in either order after the formal_arg_list (i.e., either
5284 can be first, both can exist together or by themselves or neither
5285 one). Therefore, the match_result can't match the end of the
5286 string, and check for the bind(c) or result clause in either order. */
5287 found_match = gfc_match_eos ();
5289 /* Make sure that it isn't already declared as BIND(C). If it is, it
5290 must have been marked BIND(C) with a BIND(C) attribute and that is
5291 not allowed for procedures. */
5292 if (sym->attr.is_bind_c == 1)
5294 sym->attr.is_bind_c = 0;
5295 if (sym->old_symbol != NULL)
5296 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5297 "variables or common blocks",
5298 &(sym->old_symbol->declared_at));
5299 else
5300 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5301 "variables or common blocks", &gfc_current_locus);
5304 if (found_match != MATCH_YES)
5306 /* If we haven't found the end-of-statement, look for a suffix. */
5307 suffix_match = gfc_match_suffix (sym, &result);
5308 if (suffix_match == MATCH_YES)
5309 /* Need to get the eos now. */
5310 found_match = gfc_match_eos ();
5311 else
5312 found_match = suffix_match;
5315 if(found_match != MATCH_YES)
5316 m = MATCH_ERROR;
5317 else
5319 /* Make changes to the symbol. */
5320 m = MATCH_ERROR;
5322 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5323 goto cleanup;
5325 if (!gfc_missing_attr (&sym->attr, NULL)
5326 || !copy_prefix (&sym->attr, &sym->declared_at))
5327 goto cleanup;
5329 /* Delay matching the function characteristics until after the
5330 specification block by signalling kind=-1. */
5331 sym->declared_at = old_loc;
5332 if (current_ts.type != BT_UNKNOWN)
5333 current_ts.kind = -1;
5334 else
5335 current_ts.kind = 0;
5337 if (result == NULL)
5339 if (current_ts.type != BT_UNKNOWN
5340 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5341 goto cleanup;
5342 sym->result = sym;
5344 else
5346 if (current_ts.type != BT_UNKNOWN
5347 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5348 goto cleanup;
5349 sym->result = result;
5352 /* Warn if this procedure has the same name as an intrinsic. */
5353 warn_intrinsic_shadow (sym, true);
5355 return MATCH_YES;
5358 cleanup:
5359 gfc_current_locus = old_loc;
5360 return m;
5364 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5365 pass the name of the entry, rather than the gfc_current_block name, and
5366 to return false upon finding an existing global entry. */
5368 static bool
5369 add_global_entry (const char *name, const char *binding_label, bool sub,
5370 locus *where)
5372 gfc_gsymbol *s;
5373 enum gfc_symbol_type type;
5375 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5377 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5378 name is a global identifier. */
5379 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5381 s = gfc_get_gsymbol (name);
5383 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5385 gfc_global_used (s, where);
5386 return false;
5388 else
5390 s->type = type;
5391 s->sym_name = name;
5392 s->where = *where;
5393 s->defined = 1;
5394 s->ns = gfc_current_ns;
5398 /* Don't add the symbol multiple times. */
5399 if (binding_label
5400 && (!gfc_notification_std (GFC_STD_F2008)
5401 || strcmp (name, binding_label) != 0))
5403 s = gfc_get_gsymbol (binding_label);
5405 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5407 gfc_global_used (s, where);
5408 return false;
5410 else
5412 s->type = type;
5413 s->sym_name = name;
5414 s->binding_label = binding_label;
5415 s->where = *where;
5416 s->defined = 1;
5417 s->ns = gfc_current_ns;
5421 return true;
5425 /* Match an ENTRY statement. */
5427 match
5428 gfc_match_entry (void)
5430 gfc_symbol *proc;
5431 gfc_symbol *result;
5432 gfc_symbol *entry;
5433 char name[GFC_MAX_SYMBOL_LEN + 1];
5434 gfc_compile_state state;
5435 match m;
5436 gfc_entry_list *el;
5437 locus old_loc;
5438 bool module_procedure;
5439 char peek_char;
5440 match is_bind_c;
5442 m = gfc_match_name (name);
5443 if (m != MATCH_YES)
5444 return m;
5446 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5447 return MATCH_ERROR;
5449 state = gfc_current_state ();
5450 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5452 switch (state)
5454 case COMP_PROGRAM:
5455 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5456 break;
5457 case COMP_MODULE:
5458 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5459 break;
5460 case COMP_BLOCK_DATA:
5461 gfc_error ("ENTRY statement at %C cannot appear within "
5462 "a BLOCK DATA");
5463 break;
5464 case COMP_INTERFACE:
5465 gfc_error ("ENTRY statement at %C cannot appear within "
5466 "an INTERFACE");
5467 break;
5468 case COMP_DERIVED:
5469 gfc_error ("ENTRY statement at %C cannot appear within "
5470 "a DERIVED TYPE block");
5471 break;
5472 case COMP_IF:
5473 gfc_error ("ENTRY statement at %C cannot appear within "
5474 "an IF-THEN block");
5475 break;
5476 case COMP_DO:
5477 case COMP_DO_CONCURRENT:
5478 gfc_error ("ENTRY statement at %C cannot appear within "
5479 "a DO block");
5480 break;
5481 case COMP_SELECT:
5482 gfc_error ("ENTRY statement at %C cannot appear within "
5483 "a SELECT block");
5484 break;
5485 case COMP_FORALL:
5486 gfc_error ("ENTRY statement at %C cannot appear within "
5487 "a FORALL block");
5488 break;
5489 case COMP_WHERE:
5490 gfc_error ("ENTRY statement at %C cannot appear within "
5491 "a WHERE block");
5492 break;
5493 case COMP_CONTAINS:
5494 gfc_error ("ENTRY statement at %C cannot appear within "
5495 "a contained subprogram");
5496 break;
5497 default:
5498 gfc_internal_error ("gfc_match_entry(): Bad state");
5500 return MATCH_ERROR;
5503 module_procedure = gfc_current_ns->parent != NULL
5504 && gfc_current_ns->parent->proc_name
5505 && gfc_current_ns->parent->proc_name->attr.flavor
5506 == FL_MODULE;
5508 if (gfc_current_ns->parent != NULL
5509 && gfc_current_ns->parent->proc_name
5510 && !module_procedure)
5512 gfc_error("ENTRY statement at %C cannot appear in a "
5513 "contained procedure");
5514 return MATCH_ERROR;
5517 /* Module function entries need special care in get_proc_name
5518 because previous references within the function will have
5519 created symbols attached to the current namespace. */
5520 if (get_proc_name (name, &entry,
5521 gfc_current_ns->parent != NULL
5522 && module_procedure))
5523 return MATCH_ERROR;
5525 proc = gfc_current_block ();
5527 /* Make sure that it isn't already declared as BIND(C). If it is, it
5528 must have been marked BIND(C) with a BIND(C) attribute and that is
5529 not allowed for procedures. */
5530 if (entry->attr.is_bind_c == 1)
5532 entry->attr.is_bind_c = 0;
5533 if (entry->old_symbol != NULL)
5534 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5535 "variables or common blocks",
5536 &(entry->old_symbol->declared_at));
5537 else
5538 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5539 "variables or common blocks", &gfc_current_locus);
5542 /* Check what next non-whitespace character is so we can tell if there
5543 is the required parens if we have a BIND(C). */
5544 old_loc = gfc_current_locus;
5545 gfc_gobble_whitespace ();
5546 peek_char = gfc_peek_ascii_char ();
5548 if (state == COMP_SUBROUTINE)
5550 m = gfc_match_formal_arglist (entry, 0, 1);
5551 if (m != MATCH_YES)
5552 return MATCH_ERROR;
5554 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5555 never be an internal procedure. */
5556 is_bind_c = gfc_match_bind_c (entry, true);
5557 if (is_bind_c == MATCH_ERROR)
5558 return MATCH_ERROR;
5559 if (is_bind_c == MATCH_YES)
5561 if (peek_char != '(')
5563 gfc_error ("Missing required parentheses before BIND(C) at %C");
5564 return MATCH_ERROR;
5566 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5567 &(entry->declared_at), 1))
5568 return MATCH_ERROR;
5571 if (!gfc_current_ns->parent
5572 && !add_global_entry (name, entry->binding_label, true,
5573 &old_loc))
5574 return MATCH_ERROR;
5576 /* An entry in a subroutine. */
5577 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5578 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5579 return MATCH_ERROR;
5581 else
5583 /* An entry in a function.
5584 We need to take special care because writing
5585 ENTRY f()
5587 ENTRY f
5588 is allowed, whereas
5589 ENTRY f() RESULT (r)
5590 can't be written as
5591 ENTRY f RESULT (r). */
5592 if (gfc_match_eos () == MATCH_YES)
5594 gfc_current_locus = old_loc;
5595 /* Match the empty argument list, and add the interface to
5596 the symbol. */
5597 m = gfc_match_formal_arglist (entry, 0, 1);
5599 else
5600 m = gfc_match_formal_arglist (entry, 0, 0);
5602 if (m != MATCH_YES)
5603 return MATCH_ERROR;
5605 result = NULL;
5607 if (gfc_match_eos () == MATCH_YES)
5609 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5610 || !gfc_add_function (&entry->attr, entry->name, NULL))
5611 return MATCH_ERROR;
5613 entry->result = entry;
5615 else
5617 m = gfc_match_suffix (entry, &result);
5618 if (m == MATCH_NO)
5619 gfc_syntax_error (ST_ENTRY);
5620 if (m != MATCH_YES)
5621 return MATCH_ERROR;
5623 if (result)
5625 if (!gfc_add_result (&result->attr, result->name, NULL)
5626 || !gfc_add_entry (&entry->attr, result->name, NULL)
5627 || !gfc_add_function (&entry->attr, result->name, NULL))
5628 return MATCH_ERROR;
5629 entry->result = result;
5631 else
5633 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5634 || !gfc_add_function (&entry->attr, entry->name, NULL))
5635 return MATCH_ERROR;
5636 entry->result = entry;
5640 if (!gfc_current_ns->parent
5641 && !add_global_entry (name, entry->binding_label, false,
5642 &old_loc))
5643 return MATCH_ERROR;
5646 if (gfc_match_eos () != MATCH_YES)
5648 gfc_syntax_error (ST_ENTRY);
5649 return MATCH_ERROR;
5652 entry->attr.recursive = proc->attr.recursive;
5653 entry->attr.elemental = proc->attr.elemental;
5654 entry->attr.pure = proc->attr.pure;
5656 el = gfc_get_entry_list ();
5657 el->sym = entry;
5658 el->next = gfc_current_ns->entries;
5659 gfc_current_ns->entries = el;
5660 if (el->next)
5661 el->id = el->next->id + 1;
5662 else
5663 el->id = 1;
5665 new_st.op = EXEC_ENTRY;
5666 new_st.ext.entry = el;
5668 return MATCH_YES;
5672 /* Match a subroutine statement, including optional prefixes. */
5674 match
5675 gfc_match_subroutine (void)
5677 char name[GFC_MAX_SYMBOL_LEN + 1];
5678 gfc_symbol *sym;
5679 match m;
5680 match is_bind_c;
5681 char peek_char;
5682 bool allow_binding_name;
5684 if (gfc_current_state () != COMP_NONE
5685 && gfc_current_state () != COMP_INTERFACE
5686 && gfc_current_state () != COMP_CONTAINS)
5687 return MATCH_NO;
5689 m = gfc_match_prefix (NULL);
5690 if (m != MATCH_YES)
5691 return m;
5693 m = gfc_match ("subroutine% %n", name);
5694 if (m != MATCH_YES)
5695 return m;
5697 if (get_proc_name (name, &sym, false))
5698 return MATCH_ERROR;
5700 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5701 the symbol existed before. */
5702 sym->declared_at = gfc_current_locus;
5704 if (add_hidden_procptr_result (sym))
5705 sym = sym->result;
5707 gfc_new_block = sym;
5709 /* Check what next non-whitespace character is so we can tell if there
5710 is the required parens if we have a BIND(C). */
5711 gfc_gobble_whitespace ();
5712 peek_char = gfc_peek_ascii_char ();
5714 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5715 return MATCH_ERROR;
5717 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5718 return MATCH_ERROR;
5720 /* Make sure that it isn't already declared as BIND(C). If it is, it
5721 must have been marked BIND(C) with a BIND(C) attribute and that is
5722 not allowed for procedures. */
5723 if (sym->attr.is_bind_c == 1)
5725 sym->attr.is_bind_c = 0;
5726 if (sym->old_symbol != NULL)
5727 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5728 "variables or common blocks",
5729 &(sym->old_symbol->declared_at));
5730 else
5731 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5732 "variables or common blocks", &gfc_current_locus);
5735 /* C binding names are not allowed for internal procedures. */
5736 if (gfc_current_state () == COMP_CONTAINS
5737 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5738 allow_binding_name = false;
5739 else
5740 allow_binding_name = true;
5742 /* Here, we are just checking if it has the bind(c) attribute, and if
5743 so, then we need to make sure it's all correct. If it doesn't,
5744 we still need to continue matching the rest of the subroutine line. */
5745 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5746 if (is_bind_c == MATCH_ERROR)
5748 /* There was an attempt at the bind(c), but it was wrong. An
5749 error message should have been printed w/in the gfc_match_bind_c
5750 so here we'll just return the MATCH_ERROR. */
5751 return MATCH_ERROR;
5754 if (is_bind_c == MATCH_YES)
5756 /* The following is allowed in the Fortran 2008 draft. */
5757 if (gfc_current_state () == COMP_CONTAINS
5758 && sym->ns->proc_name->attr.flavor != FL_MODULE
5759 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5760 "at %L may not be specified for an internal "
5761 "procedure", &gfc_current_locus))
5762 return MATCH_ERROR;
5764 if (peek_char != '(')
5766 gfc_error ("Missing required parentheses before BIND(C) at %C");
5767 return MATCH_ERROR;
5769 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5770 &(sym->declared_at), 1))
5771 return MATCH_ERROR;
5774 if (gfc_match_eos () != MATCH_YES)
5776 gfc_syntax_error (ST_SUBROUTINE);
5777 return MATCH_ERROR;
5780 if (!copy_prefix (&sym->attr, &sym->declared_at))
5781 return MATCH_ERROR;
5783 /* Warn if it has the same name as an intrinsic. */
5784 warn_intrinsic_shadow (sym, false);
5786 return MATCH_YES;
5790 /* Check that the NAME identifier in a BIND attribute or statement
5791 is conform to C identifier rules. */
5793 match
5794 check_bind_name_identifier (char **name)
5796 char *n = *name, *p;
5798 /* Remove leading spaces. */
5799 while (*n == ' ')
5800 n++;
5802 /* On an empty string, free memory and set name to NULL. */
5803 if (*n == '\0')
5805 free (*name);
5806 *name = NULL;
5807 return MATCH_YES;
5810 /* Remove trailing spaces. */
5811 p = n + strlen(n) - 1;
5812 while (*p == ' ')
5813 *(p--) = '\0';
5815 /* Insert the identifier into the symbol table. */
5816 p = xstrdup (n);
5817 free (*name);
5818 *name = p;
5820 /* Now check that identifier is valid under C rules. */
5821 if (ISDIGIT (*p))
5823 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5824 return MATCH_ERROR;
5827 for (; *p; p++)
5828 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
5830 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5831 return MATCH_ERROR;
5834 return MATCH_YES;
5838 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5839 given, and set the binding label in either the given symbol (if not
5840 NULL), or in the current_ts. The symbol may be NULL because we may
5841 encounter the BIND(C) before the declaration itself. Return
5842 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5843 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5844 or MATCH_YES if the specifier was correct and the binding label and
5845 bind(c) fields were set correctly for the given symbol or the
5846 current_ts. If allow_binding_name is false, no binding name may be
5847 given. */
5849 match
5850 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5852 char *binding_label = NULL;
5853 gfc_expr *e = NULL;
5855 /* Initialize the flag that specifies whether we encountered a NAME=
5856 specifier or not. */
5857 has_name_equals = 0;
5859 /* This much we have to be able to match, in this order, if
5860 there is a bind(c) label. */
5861 if (gfc_match (" bind ( c ") != MATCH_YES)
5862 return MATCH_NO;
5864 /* Now see if there is a binding label, or if we've reached the
5865 end of the bind(c) attribute without one. */
5866 if (gfc_match_char (',') == MATCH_YES)
5868 if (gfc_match (" name = ") != MATCH_YES)
5870 gfc_error ("Syntax error in NAME= specifier for binding label "
5871 "at %C");
5872 /* should give an error message here */
5873 return MATCH_ERROR;
5876 has_name_equals = 1;
5878 if (gfc_match_init_expr (&e) != MATCH_YES)
5880 gfc_free_expr (e);
5881 return MATCH_ERROR;
5884 if (!gfc_simplify_expr(e, 0))
5886 gfc_error ("NAME= specifier at %C should be a constant expression");
5887 gfc_free_expr (e);
5888 return MATCH_ERROR;
5891 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
5892 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
5894 gfc_error ("NAME= specifier at %C should be a scalar of "
5895 "default character kind");
5896 gfc_free_expr(e);
5897 return MATCH_ERROR;
5900 // Get a C string from the Fortran string constant
5901 binding_label = gfc_widechar_to_char (e->value.character.string,
5902 e->value.character.length);
5903 gfc_free_expr(e);
5905 // Check that it is valid (old gfc_match_name_C)
5906 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
5907 return MATCH_ERROR;
5910 /* Get the required right paren. */
5911 if (gfc_match_char (')') != MATCH_YES)
5913 gfc_error ("Missing closing paren for binding label at %C");
5914 return MATCH_ERROR;
5917 if (has_name_equals && !allow_binding_name)
5919 gfc_error ("No binding name is allowed in BIND(C) at %C");
5920 return MATCH_ERROR;
5923 if (has_name_equals && sym != NULL && sym->attr.dummy)
5925 gfc_error ("For dummy procedure %s, no binding name is "
5926 "allowed in BIND(C) at %C", sym->name);
5927 return MATCH_ERROR;
5931 /* Save the binding label to the symbol. If sym is null, we're
5932 probably matching the typespec attributes of a declaration and
5933 haven't gotten the name yet, and therefore, no symbol yet. */
5934 if (binding_label)
5936 if (sym != NULL)
5937 sym->binding_label = binding_label;
5938 else
5939 curr_binding_label = binding_label;
5941 else if (allow_binding_name)
5943 /* No binding label, but if symbol isn't null, we
5944 can set the label for it here.
5945 If name="" or allow_binding_name is false, no C binding name is
5946 created. */
5947 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5948 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5951 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5952 && current_interface.type == INTERFACE_ABSTRACT)
5954 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5955 return MATCH_ERROR;
5958 return MATCH_YES;
5962 /* Return nonzero if we're currently compiling a contained procedure. */
5964 static int
5965 contained_procedure (void)
5967 gfc_state_data *s = gfc_state_stack;
5969 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5970 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5971 return 1;
5973 return 0;
5976 /* Set the kind of each enumerator. The kind is selected such that it is
5977 interoperable with the corresponding C enumeration type, making
5978 sure that -fshort-enums is honored. */
5980 static void
5981 set_enum_kind(void)
5983 enumerator_history *current_history = NULL;
5984 int kind;
5985 int i;
5987 if (max_enum == NULL || enum_history == NULL)
5988 return;
5990 if (!flag_short_enums)
5991 return;
5993 i = 0;
5996 kind = gfc_integer_kinds[i++].kind;
5998 while (kind < gfc_c_int_kind
5999 && gfc_check_integer_range (max_enum->initializer->value.integer,
6000 kind) != ARITH_OK);
6002 current_history = enum_history;
6003 while (current_history != NULL)
6005 current_history->sym->ts.kind = kind;
6006 current_history = current_history->next;
6011 /* Match any of the various end-block statements. Returns the type of
6012 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6013 and END BLOCK statements cannot be replaced by a single END statement. */
6015 match
6016 gfc_match_end (gfc_statement *st)
6018 char name[GFC_MAX_SYMBOL_LEN + 1];
6019 gfc_compile_state state;
6020 locus old_loc;
6021 const char *block_name;
6022 const char *target;
6023 int eos_ok;
6024 match m;
6025 gfc_namespace *parent_ns, *ns, *prev_ns;
6026 gfc_namespace **nsp;
6028 old_loc = gfc_current_locus;
6029 if (gfc_match ("end") != MATCH_YES)
6030 return MATCH_NO;
6032 state = gfc_current_state ();
6033 block_name = gfc_current_block () == NULL
6034 ? NULL : gfc_current_block ()->name;
6036 switch (state)
6038 case COMP_ASSOCIATE:
6039 case COMP_BLOCK:
6040 if (!strncmp (block_name, "block@", strlen("block@")))
6041 block_name = NULL;
6042 break;
6044 case COMP_CONTAINS:
6045 case COMP_DERIVED_CONTAINS:
6046 state = gfc_state_stack->previous->state;
6047 block_name = gfc_state_stack->previous->sym == NULL
6048 ? NULL : gfc_state_stack->previous->sym->name;
6049 break;
6051 default:
6052 break;
6055 switch (state)
6057 case COMP_NONE:
6058 case COMP_PROGRAM:
6059 *st = ST_END_PROGRAM;
6060 target = " program";
6061 eos_ok = 1;
6062 break;
6064 case COMP_SUBROUTINE:
6065 *st = ST_END_SUBROUTINE;
6066 target = " subroutine";
6067 eos_ok = !contained_procedure ();
6068 break;
6070 case COMP_FUNCTION:
6071 *st = ST_END_FUNCTION;
6072 target = " function";
6073 eos_ok = !contained_procedure ();
6074 break;
6076 case COMP_BLOCK_DATA:
6077 *st = ST_END_BLOCK_DATA;
6078 target = " block data";
6079 eos_ok = 1;
6080 break;
6082 case COMP_MODULE:
6083 *st = ST_END_MODULE;
6084 target = " module";
6085 eos_ok = 1;
6086 break;
6088 case COMP_INTERFACE:
6089 *st = ST_END_INTERFACE;
6090 target = " interface";
6091 eos_ok = 0;
6092 break;
6094 case COMP_DERIVED:
6095 case COMP_DERIVED_CONTAINS:
6096 *st = ST_END_TYPE;
6097 target = " type";
6098 eos_ok = 0;
6099 break;
6101 case COMP_ASSOCIATE:
6102 *st = ST_END_ASSOCIATE;
6103 target = " associate";
6104 eos_ok = 0;
6105 break;
6107 case COMP_BLOCK:
6108 *st = ST_END_BLOCK;
6109 target = " block";
6110 eos_ok = 0;
6111 break;
6113 case COMP_IF:
6114 *st = ST_ENDIF;
6115 target = " if";
6116 eos_ok = 0;
6117 break;
6119 case COMP_DO:
6120 case COMP_DO_CONCURRENT:
6121 *st = ST_ENDDO;
6122 target = " do";
6123 eos_ok = 0;
6124 break;
6126 case COMP_CRITICAL:
6127 *st = ST_END_CRITICAL;
6128 target = " critical";
6129 eos_ok = 0;
6130 break;
6132 case COMP_SELECT:
6133 case COMP_SELECT_TYPE:
6134 *st = ST_END_SELECT;
6135 target = " select";
6136 eos_ok = 0;
6137 break;
6139 case COMP_FORALL:
6140 *st = ST_END_FORALL;
6141 target = " forall";
6142 eos_ok = 0;
6143 break;
6145 case COMP_WHERE:
6146 *st = ST_END_WHERE;
6147 target = " where";
6148 eos_ok = 0;
6149 break;
6151 case COMP_ENUM:
6152 *st = ST_END_ENUM;
6153 target = " enum";
6154 eos_ok = 0;
6155 last_initializer = NULL;
6156 set_enum_kind ();
6157 gfc_free_enum_history ();
6158 break;
6160 default:
6161 gfc_error ("Unexpected END statement at %C");
6162 goto cleanup;
6165 old_loc = gfc_current_locus;
6166 if (gfc_match_eos () == MATCH_YES)
6168 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6170 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6171 "instead of %s statement at %L",
6172 gfc_ascii_statement(*st), &old_loc))
6173 goto cleanup;
6175 else if (!eos_ok)
6177 /* We would have required END [something]. */
6178 gfc_error ("%s statement expected at %L",
6179 gfc_ascii_statement (*st), &old_loc);
6180 goto cleanup;
6183 return MATCH_YES;
6186 /* Verify that we've got the sort of end-block that we're expecting. */
6187 if (gfc_match (target) != MATCH_YES)
6189 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st),
6190 &old_loc);
6191 goto cleanup;
6194 old_loc = gfc_current_locus;
6195 /* If we're at the end, make sure a block name wasn't required. */
6196 if (gfc_match_eos () == MATCH_YES)
6199 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6200 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6201 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6202 return MATCH_YES;
6204 if (!block_name)
6205 return MATCH_YES;
6207 gfc_error ("Expected block name of '%s' in %s statement at %L",
6208 block_name, gfc_ascii_statement (*st), &old_loc);
6210 return MATCH_ERROR;
6213 /* END INTERFACE has a special handler for its several possible endings. */
6214 if (*st == ST_END_INTERFACE)
6215 return gfc_match_end_interface ();
6217 /* We haven't hit the end of statement, so what is left must be an
6218 end-name. */
6219 m = gfc_match_space ();
6220 if (m == MATCH_YES)
6221 m = gfc_match_name (name);
6223 if (m == MATCH_NO)
6224 gfc_error ("Expected terminating name at %C");
6225 if (m != MATCH_YES)
6226 goto cleanup;
6228 if (block_name == NULL)
6229 goto syntax;
6231 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6233 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6234 gfc_ascii_statement (*st));
6235 goto cleanup;
6237 /* Procedure pointer as function result. */
6238 else if (strcmp (block_name, "ppr@") == 0
6239 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6241 gfc_error ("Expected label '%s' for %s statement at %C",
6242 gfc_current_block ()->ns->proc_name->name,
6243 gfc_ascii_statement (*st));
6244 goto cleanup;
6247 if (gfc_match_eos () == MATCH_YES)
6248 return MATCH_YES;
6250 syntax:
6251 gfc_syntax_error (*st);
6253 cleanup:
6254 gfc_current_locus = old_loc;
6256 /* If we are missing an END BLOCK, we created a half-ready namespace.
6257 Remove it from the parent namespace's sibling list. */
6259 if (state == COMP_BLOCK)
6261 parent_ns = gfc_current_ns->parent;
6263 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6265 prev_ns = NULL;
6266 ns = *nsp;
6267 while (ns)
6269 if (ns == gfc_current_ns)
6271 if (prev_ns == NULL)
6272 *nsp = NULL;
6273 else
6274 prev_ns->sibling = ns->sibling;
6276 prev_ns = ns;
6277 ns = ns->sibling;
6280 gfc_free_namespace (gfc_current_ns);
6281 gfc_current_ns = parent_ns;
6284 return MATCH_ERROR;
6289 /***************** Attribute declaration statements ****************/
6291 /* Set the attribute of a single variable. */
6293 static match
6294 attr_decl1 (void)
6296 char name[GFC_MAX_SYMBOL_LEN + 1];
6297 gfc_array_spec *as;
6298 gfc_symbol *sym;
6299 locus var_locus;
6300 match m;
6302 as = NULL;
6304 m = gfc_match_name (name);
6305 if (m != MATCH_YES)
6306 goto cleanup;
6308 if (find_special (name, &sym, false))
6309 return MATCH_ERROR;
6311 if (!check_function_name (name))
6313 m = MATCH_ERROR;
6314 goto cleanup;
6317 var_locus = gfc_current_locus;
6319 /* Deal with possible array specification for certain attributes. */
6320 if (current_attr.dimension
6321 || current_attr.codimension
6322 || current_attr.allocatable
6323 || current_attr.pointer
6324 || current_attr.target)
6326 m = gfc_match_array_spec (&as, !current_attr.codimension,
6327 !current_attr.dimension
6328 && !current_attr.pointer
6329 && !current_attr.target);
6330 if (m == MATCH_ERROR)
6331 goto cleanup;
6333 if (current_attr.dimension && m == MATCH_NO)
6335 gfc_error ("Missing array specification at %L in DIMENSION "
6336 "statement", &var_locus);
6337 m = MATCH_ERROR;
6338 goto cleanup;
6341 if (current_attr.dimension && sym->value)
6343 gfc_error ("Dimensions specified for %s at %L after its "
6344 "initialisation", sym->name, &var_locus);
6345 m = MATCH_ERROR;
6346 goto cleanup;
6349 if (current_attr.codimension && m == MATCH_NO)
6351 gfc_error ("Missing array specification at %L in CODIMENSION "
6352 "statement", &var_locus);
6353 m = MATCH_ERROR;
6354 goto cleanup;
6357 if ((current_attr.allocatable || current_attr.pointer)
6358 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6360 gfc_error ("Array specification must be deferred at %L", &var_locus);
6361 m = MATCH_ERROR;
6362 goto cleanup;
6366 /* Update symbol table. DIMENSION attribute is set in
6367 gfc_set_array_spec(). For CLASS variables, this must be applied
6368 to the first component, or '_data' field. */
6369 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6371 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6373 m = MATCH_ERROR;
6374 goto cleanup;
6377 else
6379 if (current_attr.dimension == 0 && current_attr.codimension == 0
6380 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6382 m = MATCH_ERROR;
6383 goto cleanup;
6387 if (sym->ts.type == BT_CLASS
6388 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6390 m = MATCH_ERROR;
6391 goto cleanup;
6394 if (!gfc_set_array_spec (sym, as, &var_locus))
6396 m = MATCH_ERROR;
6397 goto cleanup;
6400 if (sym->attr.cray_pointee && sym->as != NULL)
6402 /* Fix the array spec. */
6403 m = gfc_mod_pointee_as (sym->as);
6404 if (m == MATCH_ERROR)
6405 goto cleanup;
6408 if (!gfc_add_attribute (&sym->attr, &var_locus))
6410 m = MATCH_ERROR;
6411 goto cleanup;
6414 if ((current_attr.external || current_attr.intrinsic)
6415 && sym->attr.flavor != FL_PROCEDURE
6416 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6418 m = MATCH_ERROR;
6419 goto cleanup;
6422 add_hidden_procptr_result (sym);
6424 return MATCH_YES;
6426 cleanup:
6427 gfc_free_array_spec (as);
6428 return m;
6432 /* Generic attribute declaration subroutine. Used for attributes that
6433 just have a list of names. */
6435 static match
6436 attr_decl (void)
6438 match m;
6440 /* Gobble the optional double colon, by simply ignoring the result
6441 of gfc_match(). */
6442 gfc_match (" ::");
6444 for (;;)
6446 m = attr_decl1 ();
6447 if (m != MATCH_YES)
6448 break;
6450 if (gfc_match_eos () == MATCH_YES)
6452 m = MATCH_YES;
6453 break;
6456 if (gfc_match_char (',') != MATCH_YES)
6458 gfc_error ("Unexpected character in variable list at %C");
6459 m = MATCH_ERROR;
6460 break;
6464 return m;
6468 /* This routine matches Cray Pointer declarations of the form:
6469 pointer ( <pointer>, <pointee> )
6471 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6472 The pointer, if already declared, should be an integer. Otherwise, we
6473 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6474 be either a scalar, or an array declaration. No space is allocated for
6475 the pointee. For the statement
6476 pointer (ipt, ar(10))
6477 any subsequent uses of ar will be translated (in C-notation) as
6478 ar(i) => ((<type> *) ipt)(i)
6479 After gimplification, pointee variable will disappear in the code. */
6481 static match
6482 cray_pointer_decl (void)
6484 match m;
6485 gfc_array_spec *as = NULL;
6486 gfc_symbol *cptr; /* Pointer symbol. */
6487 gfc_symbol *cpte; /* Pointee symbol. */
6488 locus var_locus;
6489 bool done = false;
6491 while (!done)
6493 if (gfc_match_char ('(') != MATCH_YES)
6495 gfc_error ("Expected '(' at %C");
6496 return MATCH_ERROR;
6499 /* Match pointer. */
6500 var_locus = gfc_current_locus;
6501 gfc_clear_attr (&current_attr);
6502 gfc_add_cray_pointer (&current_attr, &var_locus);
6503 current_ts.type = BT_INTEGER;
6504 current_ts.kind = gfc_index_integer_kind;
6506 m = gfc_match_symbol (&cptr, 0);
6507 if (m != MATCH_YES)
6509 gfc_error ("Expected variable name at %C");
6510 return m;
6513 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6514 return MATCH_ERROR;
6516 gfc_set_sym_referenced (cptr);
6518 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6520 cptr->ts.type = BT_INTEGER;
6521 cptr->ts.kind = gfc_index_integer_kind;
6523 else if (cptr->ts.type != BT_INTEGER)
6525 gfc_error ("Cray pointer at %C must be an integer");
6526 return MATCH_ERROR;
6528 else if (cptr->ts.kind < gfc_index_integer_kind)
6529 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6530 " memory addresses require %d bytes",
6531 cptr->ts.kind, gfc_index_integer_kind);
6533 if (gfc_match_char (',') != MATCH_YES)
6535 gfc_error ("Expected \",\" at %C");
6536 return MATCH_ERROR;
6539 /* Match Pointee. */
6540 var_locus = gfc_current_locus;
6541 gfc_clear_attr (&current_attr);
6542 gfc_add_cray_pointee (&current_attr, &var_locus);
6543 current_ts.type = BT_UNKNOWN;
6544 current_ts.kind = 0;
6546 m = gfc_match_symbol (&cpte, 0);
6547 if (m != MATCH_YES)
6549 gfc_error ("Expected variable name at %C");
6550 return m;
6553 /* Check for an optional array spec. */
6554 m = gfc_match_array_spec (&as, true, false);
6555 if (m == MATCH_ERROR)
6557 gfc_free_array_spec (as);
6558 return m;
6560 else if (m == MATCH_NO)
6562 gfc_free_array_spec (as);
6563 as = NULL;
6566 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6567 return MATCH_ERROR;
6569 gfc_set_sym_referenced (cpte);
6571 if (cpte->as == NULL)
6573 if (!gfc_set_array_spec (cpte, as, &var_locus))
6574 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6576 else if (as != NULL)
6578 gfc_error ("Duplicate array spec for Cray pointee at %C");
6579 gfc_free_array_spec (as);
6580 return MATCH_ERROR;
6583 as = NULL;
6585 if (cpte->as != NULL)
6587 /* Fix array spec. */
6588 m = gfc_mod_pointee_as (cpte->as);
6589 if (m == MATCH_ERROR)
6590 return m;
6593 /* Point the Pointee at the Pointer. */
6594 cpte->cp_pointer = cptr;
6596 if (gfc_match_char (')') != MATCH_YES)
6598 gfc_error ("Expected \")\" at %C");
6599 return MATCH_ERROR;
6601 m = gfc_match_char (',');
6602 if (m != MATCH_YES)
6603 done = true; /* Stop searching for more declarations. */
6607 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6608 || gfc_match_eos () != MATCH_YES)
6610 gfc_error ("Expected \",\" or end of statement at %C");
6611 return MATCH_ERROR;
6613 return MATCH_YES;
6617 match
6618 gfc_match_external (void)
6621 gfc_clear_attr (&current_attr);
6622 current_attr.external = 1;
6624 return attr_decl ();
6628 match
6629 gfc_match_intent (void)
6631 sym_intent intent;
6633 /* This is not allowed within a BLOCK construct! */
6634 if (gfc_current_state () == COMP_BLOCK)
6636 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6637 return MATCH_ERROR;
6640 intent = match_intent_spec ();
6641 if (intent == INTENT_UNKNOWN)
6642 return MATCH_ERROR;
6644 gfc_clear_attr (&current_attr);
6645 current_attr.intent = intent;
6647 return attr_decl ();
6651 match
6652 gfc_match_intrinsic (void)
6655 gfc_clear_attr (&current_attr);
6656 current_attr.intrinsic = 1;
6658 return attr_decl ();
6662 match
6663 gfc_match_optional (void)
6665 /* This is not allowed within a BLOCK construct! */
6666 if (gfc_current_state () == COMP_BLOCK)
6668 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6669 return MATCH_ERROR;
6672 gfc_clear_attr (&current_attr);
6673 current_attr.optional = 1;
6675 return attr_decl ();
6679 match
6680 gfc_match_pointer (void)
6682 gfc_gobble_whitespace ();
6683 if (gfc_peek_ascii_char () == '(')
6685 if (!gfc_option.flag_cray_pointer)
6687 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6688 "flag");
6689 return MATCH_ERROR;
6691 return cray_pointer_decl ();
6693 else
6695 gfc_clear_attr (&current_attr);
6696 current_attr.pointer = 1;
6698 return attr_decl ();
6703 match
6704 gfc_match_allocatable (void)
6706 gfc_clear_attr (&current_attr);
6707 current_attr.allocatable = 1;
6709 return attr_decl ();
6713 match
6714 gfc_match_codimension (void)
6716 gfc_clear_attr (&current_attr);
6717 current_attr.codimension = 1;
6719 return attr_decl ();
6723 match
6724 gfc_match_contiguous (void)
6726 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
6727 return MATCH_ERROR;
6729 gfc_clear_attr (&current_attr);
6730 current_attr.contiguous = 1;
6732 return attr_decl ();
6736 match
6737 gfc_match_dimension (void)
6739 gfc_clear_attr (&current_attr);
6740 current_attr.dimension = 1;
6742 return attr_decl ();
6746 match
6747 gfc_match_target (void)
6749 gfc_clear_attr (&current_attr);
6750 current_attr.target = 1;
6752 return attr_decl ();
6756 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6757 statement. */
6759 static match
6760 access_attr_decl (gfc_statement st)
6762 char name[GFC_MAX_SYMBOL_LEN + 1];
6763 interface_type type;
6764 gfc_user_op *uop;
6765 gfc_symbol *sym, *dt_sym;
6766 gfc_intrinsic_op op;
6767 match m;
6769 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6770 goto done;
6772 for (;;)
6774 m = gfc_match_generic_spec (&type, name, &op);
6775 if (m == MATCH_NO)
6776 goto syntax;
6777 if (m == MATCH_ERROR)
6778 return MATCH_ERROR;
6780 switch (type)
6782 case INTERFACE_NAMELESS:
6783 case INTERFACE_ABSTRACT:
6784 goto syntax;
6786 case INTERFACE_GENERIC:
6787 if (gfc_get_symbol (name, NULL, &sym))
6788 goto done;
6790 if (!gfc_add_access (&sym->attr,
6791 (st == ST_PUBLIC)
6792 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6793 sym->name, NULL))
6794 return MATCH_ERROR;
6796 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6797 && !gfc_add_access (&dt_sym->attr,
6798 (st == ST_PUBLIC)
6799 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6800 sym->name, NULL))
6801 return MATCH_ERROR;
6803 break;
6805 case INTERFACE_INTRINSIC_OP:
6806 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6808 gfc_intrinsic_op other_op;
6810 gfc_current_ns->operator_access[op] =
6811 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6813 /* Handle the case if there is another op with the same
6814 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6815 other_op = gfc_equivalent_op (op);
6817 if (other_op != INTRINSIC_NONE)
6818 gfc_current_ns->operator_access[other_op] =
6819 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6822 else
6824 gfc_error ("Access specification of the %s operator at %C has "
6825 "already been specified", gfc_op2string (op));
6826 goto done;
6829 break;
6831 case INTERFACE_USER_OP:
6832 uop = gfc_get_uop (name);
6834 if (uop->access == ACCESS_UNKNOWN)
6836 uop->access = (st == ST_PUBLIC)
6837 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6839 else
6841 gfc_error ("Access specification of the .%s. operator at %C "
6842 "has already been specified", sym->name);
6843 goto done;
6846 break;
6849 if (gfc_match_char (',') == MATCH_NO)
6850 break;
6853 if (gfc_match_eos () != MATCH_YES)
6854 goto syntax;
6855 return MATCH_YES;
6857 syntax:
6858 gfc_syntax_error (st);
6860 done:
6861 return MATCH_ERROR;
6865 match
6866 gfc_match_protected (void)
6868 gfc_symbol *sym;
6869 match m;
6871 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6873 gfc_error ("PROTECTED at %C only allowed in specification "
6874 "part of a module");
6875 return MATCH_ERROR;
6879 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
6880 return MATCH_ERROR;
6882 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6884 return MATCH_ERROR;
6887 if (gfc_match_eos () == MATCH_YES)
6888 goto syntax;
6890 for(;;)
6892 m = gfc_match_symbol (&sym, 0);
6893 switch (m)
6895 case MATCH_YES:
6896 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
6897 return MATCH_ERROR;
6898 goto next_item;
6900 case MATCH_NO:
6901 break;
6903 case MATCH_ERROR:
6904 return MATCH_ERROR;
6907 next_item:
6908 if (gfc_match_eos () == MATCH_YES)
6909 break;
6910 if (gfc_match_char (',') != MATCH_YES)
6911 goto syntax;
6914 return MATCH_YES;
6916 syntax:
6917 gfc_error ("Syntax error in PROTECTED statement at %C");
6918 return MATCH_ERROR;
6922 /* The PRIVATE statement is a bit weird in that it can be an attribute
6923 declaration, but also works as a standalone statement inside of a
6924 type declaration or a module. */
6926 match
6927 gfc_match_private (gfc_statement *st)
6930 if (gfc_match ("private") != MATCH_YES)
6931 return MATCH_NO;
6933 if (gfc_current_state () != COMP_MODULE
6934 && !(gfc_current_state () == COMP_DERIVED
6935 && gfc_state_stack->previous
6936 && gfc_state_stack->previous->state == COMP_MODULE)
6937 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6938 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6939 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6941 gfc_error ("PRIVATE statement at %C is only allowed in the "
6942 "specification part of a module");
6943 return MATCH_ERROR;
6946 if (gfc_current_state () == COMP_DERIVED)
6948 if (gfc_match_eos () == MATCH_YES)
6950 *st = ST_PRIVATE;
6951 return MATCH_YES;
6954 gfc_syntax_error (ST_PRIVATE);
6955 return MATCH_ERROR;
6958 if (gfc_match_eos () == MATCH_YES)
6960 *st = ST_PRIVATE;
6961 return MATCH_YES;
6964 *st = ST_ATTR_DECL;
6965 return access_attr_decl (ST_PRIVATE);
6969 match
6970 gfc_match_public (gfc_statement *st)
6973 if (gfc_match ("public") != MATCH_YES)
6974 return MATCH_NO;
6976 if (gfc_current_state () != COMP_MODULE)
6978 gfc_error ("PUBLIC statement at %C is only allowed in the "
6979 "specification part of a module");
6980 return MATCH_ERROR;
6983 if (gfc_match_eos () == MATCH_YES)
6985 *st = ST_PUBLIC;
6986 return MATCH_YES;
6989 *st = ST_ATTR_DECL;
6990 return access_attr_decl (ST_PUBLIC);
6994 /* Workhorse for gfc_match_parameter. */
6996 static match
6997 do_parm (void)
6999 gfc_symbol *sym;
7000 gfc_expr *init;
7001 match m;
7002 bool t;
7004 m = gfc_match_symbol (&sym, 0);
7005 if (m == MATCH_NO)
7006 gfc_error ("Expected variable name at %C in PARAMETER statement");
7008 if (m != MATCH_YES)
7009 return m;
7011 if (gfc_match_char ('=') == MATCH_NO)
7013 gfc_error ("Expected = sign in PARAMETER statement at %C");
7014 return MATCH_ERROR;
7017 m = gfc_match_init_expr (&init);
7018 if (m == MATCH_NO)
7019 gfc_error ("Expected expression at %C in PARAMETER statement");
7020 if (m != MATCH_YES)
7021 return m;
7023 if (sym->ts.type == BT_UNKNOWN
7024 && !gfc_set_default_type (sym, 1, NULL))
7026 m = MATCH_ERROR;
7027 goto cleanup;
7030 if (!gfc_check_assign_symbol (sym, NULL, init)
7031 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7033 m = MATCH_ERROR;
7034 goto cleanup;
7037 if (sym->value)
7039 gfc_error ("Initializing already initialized variable at %C");
7040 m = MATCH_ERROR;
7041 goto cleanup;
7044 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7045 return (t) ? MATCH_YES : MATCH_ERROR;
7047 cleanup:
7048 gfc_free_expr (init);
7049 return m;
7053 /* Match a parameter statement, with the weird syntax that these have. */
7055 match
7056 gfc_match_parameter (void)
7058 match m;
7060 if (gfc_match_char ('(') == MATCH_NO)
7061 return MATCH_NO;
7063 for (;;)
7065 m = do_parm ();
7066 if (m != MATCH_YES)
7067 break;
7069 if (gfc_match (" )%t") == MATCH_YES)
7070 break;
7072 if (gfc_match_char (',') != MATCH_YES)
7074 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7075 m = MATCH_ERROR;
7076 break;
7080 return m;
7084 /* Save statements have a special syntax. */
7086 match
7087 gfc_match_save (void)
7089 char n[GFC_MAX_SYMBOL_LEN+1];
7090 gfc_common_head *c;
7091 gfc_symbol *sym;
7092 match m;
7094 if (gfc_match_eos () == MATCH_YES)
7096 if (gfc_current_ns->seen_save)
7098 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7099 "follows previous SAVE statement"))
7100 return MATCH_ERROR;
7103 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7104 return MATCH_YES;
7107 if (gfc_current_ns->save_all)
7109 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7110 "blanket SAVE statement"))
7111 return MATCH_ERROR;
7114 gfc_match (" ::");
7116 for (;;)
7118 m = gfc_match_symbol (&sym, 0);
7119 switch (m)
7121 case MATCH_YES:
7122 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7123 &gfc_current_locus))
7124 return MATCH_ERROR;
7125 goto next_item;
7127 case MATCH_NO:
7128 break;
7130 case MATCH_ERROR:
7131 return MATCH_ERROR;
7134 m = gfc_match (" / %n /", &n);
7135 if (m == MATCH_ERROR)
7136 return MATCH_ERROR;
7137 if (m == MATCH_NO)
7138 goto syntax;
7140 c = gfc_get_common (n, 0);
7141 c->saved = 1;
7143 gfc_current_ns->seen_save = 1;
7145 next_item:
7146 if (gfc_match_eos () == MATCH_YES)
7147 break;
7148 if (gfc_match_char (',') != MATCH_YES)
7149 goto syntax;
7152 return MATCH_YES;
7154 syntax:
7155 gfc_error ("Syntax error in SAVE statement at %C");
7156 return MATCH_ERROR;
7160 match
7161 gfc_match_value (void)
7163 gfc_symbol *sym;
7164 match m;
7166 /* This is not allowed within a BLOCK construct! */
7167 if (gfc_current_state () == COMP_BLOCK)
7169 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7170 return MATCH_ERROR;
7173 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7174 return MATCH_ERROR;
7176 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7178 return MATCH_ERROR;
7181 if (gfc_match_eos () == MATCH_YES)
7182 goto syntax;
7184 for(;;)
7186 m = gfc_match_symbol (&sym, 0);
7187 switch (m)
7189 case MATCH_YES:
7190 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7191 return MATCH_ERROR;
7192 goto next_item;
7194 case MATCH_NO:
7195 break;
7197 case MATCH_ERROR:
7198 return MATCH_ERROR;
7201 next_item:
7202 if (gfc_match_eos () == MATCH_YES)
7203 break;
7204 if (gfc_match_char (',') != MATCH_YES)
7205 goto syntax;
7208 return MATCH_YES;
7210 syntax:
7211 gfc_error ("Syntax error in VALUE statement at %C");
7212 return MATCH_ERROR;
7216 match
7217 gfc_match_volatile (void)
7219 gfc_symbol *sym;
7220 match m;
7222 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7223 return MATCH_ERROR;
7225 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7227 return MATCH_ERROR;
7230 if (gfc_match_eos () == MATCH_YES)
7231 goto syntax;
7233 for(;;)
7235 /* VOLATILE is special because it can be added to host-associated
7236 symbols locally. Except for coarrays. */
7237 m = gfc_match_symbol (&sym, 1);
7238 switch (m)
7240 case MATCH_YES:
7241 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7242 for variable in a BLOCK which is defined outside of the BLOCK. */
7243 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7245 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7246 "%C, which is use-/host-associated", sym->name);
7247 return MATCH_ERROR;
7249 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7250 return MATCH_ERROR;
7251 goto next_item;
7253 case MATCH_NO:
7254 break;
7256 case MATCH_ERROR:
7257 return MATCH_ERROR;
7260 next_item:
7261 if (gfc_match_eos () == MATCH_YES)
7262 break;
7263 if (gfc_match_char (',') != MATCH_YES)
7264 goto syntax;
7267 return MATCH_YES;
7269 syntax:
7270 gfc_error ("Syntax error in VOLATILE statement at %C");
7271 return MATCH_ERROR;
7275 match
7276 gfc_match_asynchronous (void)
7278 gfc_symbol *sym;
7279 match m;
7281 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7282 return MATCH_ERROR;
7284 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7286 return MATCH_ERROR;
7289 if (gfc_match_eos () == MATCH_YES)
7290 goto syntax;
7292 for(;;)
7294 /* ASYNCHRONOUS is special because it can be added to host-associated
7295 symbols locally. */
7296 m = gfc_match_symbol (&sym, 1);
7297 switch (m)
7299 case MATCH_YES:
7300 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7301 return MATCH_ERROR;
7302 goto next_item;
7304 case MATCH_NO:
7305 break;
7307 case MATCH_ERROR:
7308 return MATCH_ERROR;
7311 next_item:
7312 if (gfc_match_eos () == MATCH_YES)
7313 break;
7314 if (gfc_match_char (',') != MATCH_YES)
7315 goto syntax;
7318 return MATCH_YES;
7320 syntax:
7321 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7322 return MATCH_ERROR;
7326 /* Match a module procedure statement. Note that we have to modify
7327 symbols in the parent's namespace because the current one was there
7328 to receive symbols that are in an interface's formal argument list. */
7330 match
7331 gfc_match_modproc (void)
7333 char name[GFC_MAX_SYMBOL_LEN + 1];
7334 gfc_symbol *sym;
7335 match m;
7336 locus old_locus;
7337 gfc_namespace *module_ns;
7338 gfc_interface *old_interface_head, *interface;
7340 if (gfc_state_stack->state != COMP_INTERFACE
7341 || gfc_state_stack->previous == NULL
7342 || current_interface.type == INTERFACE_NAMELESS
7343 || current_interface.type == INTERFACE_ABSTRACT)
7345 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7346 "interface");
7347 return MATCH_ERROR;
7350 module_ns = gfc_current_ns->parent;
7351 for (; module_ns; module_ns = module_ns->parent)
7352 if (module_ns->proc_name->attr.flavor == FL_MODULE
7353 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7354 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7355 && !module_ns->proc_name->attr.contained))
7356 break;
7358 if (module_ns == NULL)
7359 return MATCH_ERROR;
7361 /* Store the current state of the interface. We will need it if we
7362 end up with a syntax error and need to recover. */
7363 old_interface_head = gfc_current_interface_head ();
7365 /* Check if the F2008 optional double colon appears. */
7366 gfc_gobble_whitespace ();
7367 old_locus = gfc_current_locus;
7368 if (gfc_match ("::") == MATCH_YES)
7370 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7371 "MODULE PROCEDURE statement at %L", &old_locus))
7372 return MATCH_ERROR;
7374 else
7375 gfc_current_locus = old_locus;
7377 for (;;)
7379 bool last = false;
7380 old_locus = gfc_current_locus;
7382 m = gfc_match_name (name);
7383 if (m == MATCH_NO)
7384 goto syntax;
7385 if (m != MATCH_YES)
7386 return MATCH_ERROR;
7388 /* Check for syntax error before starting to add symbols to the
7389 current namespace. */
7390 if (gfc_match_eos () == MATCH_YES)
7391 last = true;
7393 if (!last && gfc_match_char (',') != MATCH_YES)
7394 goto syntax;
7396 /* Now we're sure the syntax is valid, we process this item
7397 further. */
7398 if (gfc_get_symbol (name, module_ns, &sym))
7399 return MATCH_ERROR;
7401 if (sym->attr.intrinsic)
7403 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7404 "PROCEDURE", &old_locus);
7405 return MATCH_ERROR;
7408 if (sym->attr.proc != PROC_MODULE
7409 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7410 return MATCH_ERROR;
7412 if (!gfc_add_interface (sym))
7413 return MATCH_ERROR;
7415 sym->attr.mod_proc = 1;
7416 sym->declared_at = old_locus;
7418 if (last)
7419 break;
7422 return MATCH_YES;
7424 syntax:
7425 /* Restore the previous state of the interface. */
7426 interface = gfc_current_interface_head ();
7427 gfc_set_current_interface_head (old_interface_head);
7429 /* Free the new interfaces. */
7430 while (interface != old_interface_head)
7432 gfc_interface *i = interface->next;
7433 free (interface);
7434 interface = i;
7437 /* And issue a syntax error. */
7438 gfc_syntax_error (ST_MODULE_PROC);
7439 return MATCH_ERROR;
7443 /* Check a derived type that is being extended. */
7445 static gfc_symbol*
7446 check_extended_derived_type (char *name)
7448 gfc_symbol *extended;
7450 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7452 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7453 return NULL;
7456 extended = gfc_find_dt_in_generic (extended);
7458 /* F08:C428. */
7459 if (!extended)
7461 gfc_error ("Symbol '%s' at %C has not been previously defined", name);
7462 return NULL;
7465 if (extended->attr.flavor != FL_DERIVED)
7467 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7468 "derived type", name);
7469 return NULL;
7472 if (extended->attr.is_bind_c)
7474 gfc_error ("'%s' cannot be extended at %C because it "
7475 "is BIND(C)", extended->name);
7476 return NULL;
7479 if (extended->attr.sequence)
7481 gfc_error ("'%s' cannot be extended at %C because it "
7482 "is a SEQUENCE type", extended->name);
7483 return NULL;
7486 return extended;
7490 /* Match the optional attribute specifiers for a type declaration.
7491 Return MATCH_ERROR if an error is encountered in one of the handled
7492 attributes (public, private, bind(c)), MATCH_NO if what's found is
7493 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7494 checking on attribute conflicts needs to be done. */
7496 match
7497 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7499 /* See if the derived type is marked as private. */
7500 if (gfc_match (" , private") == MATCH_YES)
7502 if (gfc_current_state () != COMP_MODULE)
7504 gfc_error ("Derived type at %C can only be PRIVATE in the "
7505 "specification part of a module");
7506 return MATCH_ERROR;
7509 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7510 return MATCH_ERROR;
7512 else if (gfc_match (" , public") == MATCH_YES)
7514 if (gfc_current_state () != COMP_MODULE)
7516 gfc_error ("Derived type at %C can only be PUBLIC in the "
7517 "specification part of a module");
7518 return MATCH_ERROR;
7521 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7522 return MATCH_ERROR;
7524 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7526 /* If the type is defined to be bind(c) it then needs to make
7527 sure that all fields are interoperable. This will
7528 need to be a semantic check on the finished derived type.
7529 See 15.2.3 (lines 9-12) of F2003 draft. */
7530 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7531 return MATCH_ERROR;
7533 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7535 else if (gfc_match (" , abstract") == MATCH_YES)
7537 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7538 return MATCH_ERROR;
7540 if (!gfc_add_abstract (attr, &gfc_current_locus))
7541 return MATCH_ERROR;
7543 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7545 if (!gfc_add_extension (attr, &gfc_current_locus))
7546 return MATCH_ERROR;
7548 else
7549 return MATCH_NO;
7551 /* If we get here, something matched. */
7552 return MATCH_YES;
7556 /* Match the beginning of a derived type declaration. If a type name
7557 was the result of a function, then it is possible to have a symbol
7558 already to be known as a derived type yet have no components. */
7560 match
7561 gfc_match_derived_decl (void)
7563 char name[GFC_MAX_SYMBOL_LEN + 1];
7564 char parent[GFC_MAX_SYMBOL_LEN + 1];
7565 symbol_attribute attr;
7566 gfc_symbol *sym, *gensym;
7567 gfc_symbol *extended;
7568 match m;
7569 match is_type_attr_spec = MATCH_NO;
7570 bool seen_attr = false;
7571 gfc_interface *intr = NULL, *head;
7573 if (gfc_current_state () == COMP_DERIVED)
7574 return MATCH_NO;
7576 name[0] = '\0';
7577 parent[0] = '\0';
7578 gfc_clear_attr (&attr);
7579 extended = NULL;
7583 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7584 if (is_type_attr_spec == MATCH_ERROR)
7585 return MATCH_ERROR;
7586 if (is_type_attr_spec == MATCH_YES)
7587 seen_attr = true;
7588 } while (is_type_attr_spec == MATCH_YES);
7590 /* Deal with derived type extensions. The extension attribute has
7591 been added to 'attr' but now the parent type must be found and
7592 checked. */
7593 if (parent[0])
7594 extended = check_extended_derived_type (parent);
7596 if (parent[0] && !extended)
7597 return MATCH_ERROR;
7599 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7601 gfc_error ("Expected :: in TYPE definition at %C");
7602 return MATCH_ERROR;
7605 m = gfc_match (" %n%t", name);
7606 if (m != MATCH_YES)
7607 return m;
7609 /* Make sure the name is not the name of an intrinsic type. */
7610 if (gfc_is_intrinsic_typename (name))
7612 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7613 "type", name);
7614 return MATCH_ERROR;
7617 if (gfc_get_symbol (name, NULL, &gensym))
7618 return MATCH_ERROR;
7620 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7622 gfc_error ("Derived type name '%s' at %C already has a basic type "
7623 "of %s", gensym->name, gfc_typename (&gensym->ts));
7624 return MATCH_ERROR;
7627 if (!gensym->attr.generic
7628 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
7629 return MATCH_ERROR;
7631 if (!gensym->attr.function
7632 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
7633 return MATCH_ERROR;
7635 sym = gfc_find_dt_in_generic (gensym);
7637 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7639 gfc_error ("Derived type definition of '%s' at %C has already been "
7640 "defined", sym->name);
7641 return MATCH_ERROR;
7644 if (!sym)
7646 /* Use upper case to save the actual derived-type symbol. */
7647 gfc_get_symbol (gfc_get_string ("%c%s",
7648 (char) TOUPPER ((unsigned char) gensym->name[0]),
7649 &gensym->name[1]), NULL, &sym);
7650 sym->name = gfc_get_string (gensym->name);
7651 head = gensym->generic;
7652 intr = gfc_get_interface ();
7653 intr->sym = sym;
7654 intr->where = gfc_current_locus;
7655 intr->sym->declared_at = gfc_current_locus;
7656 intr->next = head;
7657 gensym->generic = intr;
7658 gensym->attr.if_source = IFSRC_DECL;
7661 /* The symbol may already have the derived attribute without the
7662 components. The ways this can happen is via a function
7663 definition, an INTRINSIC statement or a subtype in another
7664 derived type that is a pointer. The first part of the AND clause
7665 is true if the symbol is not the return value of a function. */
7666 if (sym->attr.flavor != FL_DERIVED
7667 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
7668 return MATCH_ERROR;
7670 if (attr.access != ACCESS_UNKNOWN
7671 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
7672 return MATCH_ERROR;
7673 else if (sym->attr.access == ACCESS_UNKNOWN
7674 && gensym->attr.access != ACCESS_UNKNOWN
7675 && !gfc_add_access (&sym->attr, gensym->attr.access,
7676 sym->name, NULL))
7677 return MATCH_ERROR;
7679 if (sym->attr.access != ACCESS_UNKNOWN
7680 && gensym->attr.access == ACCESS_UNKNOWN)
7681 gensym->attr.access = sym->attr.access;
7683 /* See if the derived type was labeled as bind(c). */
7684 if (attr.is_bind_c != 0)
7685 sym->attr.is_bind_c = attr.is_bind_c;
7687 /* Construct the f2k_derived namespace if it is not yet there. */
7688 if (!sym->f2k_derived)
7689 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7691 if (extended && !sym->components)
7693 gfc_component *p;
7694 gfc_symtree *st;
7696 /* Add the extended derived type as the first component. */
7697 gfc_add_component (sym, parent, &p);
7698 extended->refs++;
7699 gfc_set_sym_referenced (extended);
7701 p->ts.type = BT_DERIVED;
7702 p->ts.u.derived = extended;
7703 p->initializer = gfc_default_initializer (&p->ts);
7705 /* Set extension level. */
7706 if (extended->attr.extension == 255)
7708 /* Since the extension field is 8 bit wide, we can only have
7709 up to 255 extension levels. */
7710 gfc_error ("Maximum extension level reached with type '%s' at %L",
7711 extended->name, &extended->declared_at);
7712 return MATCH_ERROR;
7714 sym->attr.extension = extended->attr.extension + 1;
7716 /* Provide the links between the extended type and its extension. */
7717 if (!extended->f2k_derived)
7718 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7719 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7720 st->n.sym = sym;
7723 if (!sym->hash_value)
7724 /* Set the hash for the compound name for this type. */
7725 sym->hash_value = gfc_hash_value (sym);
7727 /* Take over the ABSTRACT attribute. */
7728 sym->attr.abstract = attr.abstract;
7730 gfc_new_block = sym;
7732 return MATCH_YES;
7736 /* Cray Pointees can be declared as:
7737 pointer (ipt, a (n,m,...,*)) */
7739 match
7740 gfc_mod_pointee_as (gfc_array_spec *as)
7742 as->cray_pointee = true; /* This will be useful to know later. */
7743 if (as->type == AS_ASSUMED_SIZE)
7744 as->cp_was_assumed = true;
7745 else if (as->type == AS_ASSUMED_SHAPE)
7747 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7748 return MATCH_ERROR;
7750 return MATCH_YES;
7754 /* Match the enum definition statement, here we are trying to match
7755 the first line of enum definition statement.
7756 Returns MATCH_YES if match is found. */
7758 match
7759 gfc_match_enum (void)
7761 match m;
7763 m = gfc_match_eos ();
7764 if (m != MATCH_YES)
7765 return m;
7767 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
7768 return MATCH_ERROR;
7770 return MATCH_YES;
7774 /* Returns an initializer whose value is one higher than the value of the
7775 LAST_INITIALIZER argument. If the argument is NULL, the
7776 initializers value will be set to zero. The initializer's kind
7777 will be set to gfc_c_int_kind.
7779 If -fshort-enums is given, the appropriate kind will be selected
7780 later after all enumerators have been parsed. A warning is issued
7781 here if an initializer exceeds gfc_c_int_kind. */
7783 static gfc_expr *
7784 enum_initializer (gfc_expr *last_initializer, locus where)
7786 gfc_expr *result;
7787 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7789 mpz_init (result->value.integer);
7791 if (last_initializer != NULL)
7793 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7794 result->where = last_initializer->where;
7796 if (gfc_check_integer_range (result->value.integer,
7797 gfc_c_int_kind) != ARITH_OK)
7799 gfc_error ("Enumerator exceeds the C integer type at %C");
7800 return NULL;
7803 else
7805 /* Control comes here, if it's the very first enumerator and no
7806 initializer has been given. It will be initialized to zero. */
7807 mpz_set_si (result->value.integer, 0);
7810 return result;
7814 /* Match a variable name with an optional initializer. When this
7815 subroutine is called, a variable is expected to be parsed next.
7816 Depending on what is happening at the moment, updates either the
7817 symbol table or the current interface. */
7819 static match
7820 enumerator_decl (void)
7822 char name[GFC_MAX_SYMBOL_LEN + 1];
7823 gfc_expr *initializer;
7824 gfc_array_spec *as = NULL;
7825 gfc_symbol *sym;
7826 locus var_locus;
7827 match m;
7828 bool t;
7829 locus old_locus;
7831 initializer = NULL;
7832 old_locus = gfc_current_locus;
7834 /* When we get here, we've just matched a list of attributes and
7835 maybe a type and a double colon. The next thing we expect to see
7836 is the name of the symbol. */
7837 m = gfc_match_name (name);
7838 if (m != MATCH_YES)
7839 goto cleanup;
7841 var_locus = gfc_current_locus;
7843 /* OK, we've successfully matched the declaration. Now put the
7844 symbol in the current namespace. If we fail to create the symbol,
7845 bail out. */
7846 if (!build_sym (name, NULL, false, &as, &var_locus))
7848 m = MATCH_ERROR;
7849 goto cleanup;
7852 /* The double colon must be present in order to have initializers.
7853 Otherwise the statement is ambiguous with an assignment statement. */
7854 if (colon_seen)
7856 if (gfc_match_char ('=') == MATCH_YES)
7858 m = gfc_match_init_expr (&initializer);
7859 if (m == MATCH_NO)
7861 gfc_error ("Expected an initialization expression at %C");
7862 m = MATCH_ERROR;
7865 if (m != MATCH_YES)
7866 goto cleanup;
7870 /* If we do not have an initializer, the initialization value of the
7871 previous enumerator (stored in last_initializer) is incremented
7872 by 1 and is used to initialize the current enumerator. */
7873 if (initializer == NULL)
7874 initializer = enum_initializer (last_initializer, old_locus);
7876 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7878 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7879 &var_locus);
7880 m = MATCH_ERROR;
7881 goto cleanup;
7884 /* Store this current initializer, for the next enumerator variable
7885 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7886 use last_initializer below. */
7887 last_initializer = initializer;
7888 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7890 /* Maintain enumerator history. */
7891 gfc_find_symbol (name, NULL, 0, &sym);
7892 create_enum_history (sym, last_initializer);
7894 return (t) ? MATCH_YES : MATCH_ERROR;
7896 cleanup:
7897 /* Free stuff up and return. */
7898 gfc_free_expr (initializer);
7900 return m;
7904 /* Match the enumerator definition statement. */
7906 match
7907 gfc_match_enumerator_def (void)
7909 match m;
7910 bool t;
7912 gfc_clear_ts (&current_ts);
7914 m = gfc_match (" enumerator");
7915 if (m != MATCH_YES)
7916 return m;
7918 m = gfc_match (" :: ");
7919 if (m == MATCH_ERROR)
7920 return m;
7922 colon_seen = (m == MATCH_YES);
7924 if (gfc_current_state () != COMP_ENUM)
7926 gfc_error ("ENUM definition statement expected before %C");
7927 gfc_free_enum_history ();
7928 return MATCH_ERROR;
7931 (&current_ts)->type = BT_INTEGER;
7932 (&current_ts)->kind = gfc_c_int_kind;
7934 gfc_clear_attr (&current_attr);
7935 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7936 if (!t)
7938 m = MATCH_ERROR;
7939 goto cleanup;
7942 for (;;)
7944 m = enumerator_decl ();
7945 if (m == MATCH_ERROR)
7947 gfc_free_enum_history ();
7948 goto cleanup;
7950 if (m == MATCH_NO)
7951 break;
7953 if (gfc_match_eos () == MATCH_YES)
7954 goto cleanup;
7955 if (gfc_match_char (',') != MATCH_YES)
7956 break;
7959 if (gfc_current_state () == COMP_ENUM)
7961 gfc_free_enum_history ();
7962 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7963 m = MATCH_ERROR;
7966 cleanup:
7967 gfc_free_array_spec (current_as);
7968 current_as = NULL;
7969 return m;
7974 /* Match binding attributes. */
7976 static match
7977 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7979 bool found_passing = false;
7980 bool seen_ptr = false;
7981 match m = MATCH_YES;
7983 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7984 this case the defaults are in there. */
7985 ba->access = ACCESS_UNKNOWN;
7986 ba->pass_arg = NULL;
7987 ba->pass_arg_num = 0;
7988 ba->nopass = 0;
7989 ba->non_overridable = 0;
7990 ba->deferred = 0;
7991 ba->ppc = ppc;
7993 /* If we find a comma, we believe there are binding attributes. */
7994 m = gfc_match_char (',');
7995 if (m == MATCH_NO)
7996 goto done;
8000 /* Access specifier. */
8002 m = gfc_match (" public");
8003 if (m == MATCH_ERROR)
8004 goto error;
8005 if (m == MATCH_YES)
8007 if (ba->access != ACCESS_UNKNOWN)
8009 gfc_error ("Duplicate access-specifier at %C");
8010 goto error;
8013 ba->access = ACCESS_PUBLIC;
8014 continue;
8017 m = gfc_match (" private");
8018 if (m == MATCH_ERROR)
8019 goto error;
8020 if (m == MATCH_YES)
8022 if (ba->access != ACCESS_UNKNOWN)
8024 gfc_error ("Duplicate access-specifier at %C");
8025 goto error;
8028 ba->access = ACCESS_PRIVATE;
8029 continue;
8032 /* If inside GENERIC, the following is not allowed. */
8033 if (!generic)
8036 /* NOPASS flag. */
8037 m = gfc_match (" nopass");
8038 if (m == MATCH_ERROR)
8039 goto error;
8040 if (m == MATCH_YES)
8042 if (found_passing)
8044 gfc_error ("Binding attributes already specify passing,"
8045 " illegal NOPASS at %C");
8046 goto error;
8049 found_passing = true;
8050 ba->nopass = 1;
8051 continue;
8054 /* PASS possibly including argument. */
8055 m = gfc_match (" pass");
8056 if (m == MATCH_ERROR)
8057 goto error;
8058 if (m == MATCH_YES)
8060 char arg[GFC_MAX_SYMBOL_LEN + 1];
8062 if (found_passing)
8064 gfc_error ("Binding attributes already specify passing,"
8065 " illegal PASS at %C");
8066 goto error;
8069 m = gfc_match (" ( %n )", arg);
8070 if (m == MATCH_ERROR)
8071 goto error;
8072 if (m == MATCH_YES)
8073 ba->pass_arg = gfc_get_string (arg);
8074 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8076 found_passing = true;
8077 ba->nopass = 0;
8078 continue;
8081 if (ppc)
8083 /* POINTER flag. */
8084 m = gfc_match (" pointer");
8085 if (m == MATCH_ERROR)
8086 goto error;
8087 if (m == MATCH_YES)
8089 if (seen_ptr)
8091 gfc_error ("Duplicate POINTER attribute at %C");
8092 goto error;
8095 seen_ptr = true;
8096 continue;
8099 else
8101 /* NON_OVERRIDABLE flag. */
8102 m = gfc_match (" non_overridable");
8103 if (m == MATCH_ERROR)
8104 goto error;
8105 if (m == MATCH_YES)
8107 if (ba->non_overridable)
8109 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8110 goto error;
8113 ba->non_overridable = 1;
8114 continue;
8117 /* DEFERRED flag. */
8118 m = gfc_match (" deferred");
8119 if (m == MATCH_ERROR)
8120 goto error;
8121 if (m == MATCH_YES)
8123 if (ba->deferred)
8125 gfc_error ("Duplicate DEFERRED at %C");
8126 goto error;
8129 ba->deferred = 1;
8130 continue;
8136 /* Nothing matching found. */
8137 if (generic)
8138 gfc_error ("Expected access-specifier at %C");
8139 else
8140 gfc_error ("Expected binding attribute at %C");
8141 goto error;
8143 while (gfc_match_char (',') == MATCH_YES);
8145 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8146 if (ba->non_overridable && ba->deferred)
8148 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8149 goto error;
8152 m = MATCH_YES;
8154 done:
8155 if (ba->access == ACCESS_UNKNOWN)
8156 ba->access = gfc_typebound_default_access;
8158 if (ppc && !seen_ptr)
8160 gfc_error ("POINTER attribute is required for procedure pointer component"
8161 " at %C");
8162 goto error;
8165 return m;
8167 error:
8168 return MATCH_ERROR;
8172 /* Match a PROCEDURE specific binding inside a derived type. */
8174 static match
8175 match_procedure_in_type (void)
8177 char name[GFC_MAX_SYMBOL_LEN + 1];
8178 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8179 char* target = NULL, *ifc = NULL;
8180 gfc_typebound_proc tb;
8181 bool seen_colons;
8182 bool seen_attrs;
8183 match m;
8184 gfc_symtree* stree;
8185 gfc_namespace* ns;
8186 gfc_symbol* block;
8187 int num;
8189 /* Check current state. */
8190 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8191 block = gfc_state_stack->previous->sym;
8192 gcc_assert (block);
8194 /* Try to match PROCEDURE(interface). */
8195 if (gfc_match (" (") == MATCH_YES)
8197 m = gfc_match_name (target_buf);
8198 if (m == MATCH_ERROR)
8199 return m;
8200 if (m != MATCH_YES)
8202 gfc_error ("Interface-name expected after '(' at %C");
8203 return MATCH_ERROR;
8206 if (gfc_match (" )") != MATCH_YES)
8208 gfc_error ("')' expected at %C");
8209 return MATCH_ERROR;
8212 ifc = target_buf;
8215 /* Construct the data structure. */
8216 memset (&tb, 0, sizeof (tb));
8217 tb.where = gfc_current_locus;
8219 /* Match binding attributes. */
8220 m = match_binding_attributes (&tb, false, false);
8221 if (m == MATCH_ERROR)
8222 return m;
8223 seen_attrs = (m == MATCH_YES);
8225 /* Check that attribute DEFERRED is given if an interface is specified. */
8226 if (tb.deferred && !ifc)
8228 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8229 return MATCH_ERROR;
8231 if (ifc && !tb.deferred)
8233 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8234 return MATCH_ERROR;
8237 /* Match the colons. */
8238 m = gfc_match (" ::");
8239 if (m == MATCH_ERROR)
8240 return m;
8241 seen_colons = (m == MATCH_YES);
8242 if (seen_attrs && !seen_colons)
8244 gfc_error ("Expected '::' after binding-attributes at %C");
8245 return MATCH_ERROR;
8248 /* Match the binding names. */
8249 for(num=1;;num++)
8251 m = gfc_match_name (name);
8252 if (m == MATCH_ERROR)
8253 return m;
8254 if (m == MATCH_NO)
8256 gfc_error ("Expected binding name at %C");
8257 return MATCH_ERROR;
8260 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8261 return MATCH_ERROR;
8263 /* Try to match the '=> target', if it's there. */
8264 target = ifc;
8265 m = gfc_match (" =>");
8266 if (m == MATCH_ERROR)
8267 return m;
8268 if (m == MATCH_YES)
8270 if (tb.deferred)
8272 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8273 return MATCH_ERROR;
8276 if (!seen_colons)
8278 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8279 " at %C");
8280 return MATCH_ERROR;
8283 m = gfc_match_name (target_buf);
8284 if (m == MATCH_ERROR)
8285 return m;
8286 if (m == MATCH_NO)
8288 gfc_error ("Expected binding target after '=>' at %C");
8289 return MATCH_ERROR;
8291 target = target_buf;
8294 /* If no target was found, it has the same name as the binding. */
8295 if (!target)
8296 target = name;
8298 /* Get the namespace to insert the symbols into. */
8299 ns = block->f2k_derived;
8300 gcc_assert (ns);
8302 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8303 if (tb.deferred && !block->attr.abstract)
8305 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8306 "is not ABSTRACT", block->name);
8307 return MATCH_ERROR;
8310 /* See if we already have a binding with this name in the symtree which
8311 would be an error. If a GENERIC already targeted this binding, it may
8312 be already there but then typebound is still NULL. */
8313 stree = gfc_find_symtree (ns->tb_sym_root, name);
8314 if (stree && stree->n.tb)
8316 gfc_error ("There is already a procedure with binding name '%s' for "
8317 "the derived type '%s' at %C", name, block->name);
8318 return MATCH_ERROR;
8321 /* Insert it and set attributes. */
8323 if (!stree)
8325 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8326 gcc_assert (stree);
8328 stree->n.tb = gfc_get_typebound_proc (&tb);
8330 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8331 false))
8332 return MATCH_ERROR;
8333 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8335 if (gfc_match_eos () == MATCH_YES)
8336 return MATCH_YES;
8337 if (gfc_match_char (',') != MATCH_YES)
8338 goto syntax;
8341 syntax:
8342 gfc_error ("Syntax error in PROCEDURE statement at %C");
8343 return MATCH_ERROR;
8347 /* Match a GENERIC procedure binding inside a derived type. */
8349 match
8350 gfc_match_generic (void)
8352 char name[GFC_MAX_SYMBOL_LEN + 1];
8353 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8354 gfc_symbol* block;
8355 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8356 gfc_typebound_proc* tb;
8357 gfc_namespace* ns;
8358 interface_type op_type;
8359 gfc_intrinsic_op op;
8360 match m;
8362 /* Check current state. */
8363 if (gfc_current_state () == COMP_DERIVED)
8365 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8366 return MATCH_ERROR;
8368 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8369 return MATCH_NO;
8370 block = gfc_state_stack->previous->sym;
8371 ns = block->f2k_derived;
8372 gcc_assert (block && ns);
8374 memset (&tbattr, 0, sizeof (tbattr));
8375 tbattr.where = gfc_current_locus;
8377 /* See if we get an access-specifier. */
8378 m = match_binding_attributes (&tbattr, true, false);
8379 if (m == MATCH_ERROR)
8380 goto error;
8382 /* Now the colons, those are required. */
8383 if (gfc_match (" ::") != MATCH_YES)
8385 gfc_error ("Expected '::' at %C");
8386 goto error;
8389 /* Match the binding name; depending on type (operator / generic) format
8390 it for future error messages into bind_name. */
8392 m = gfc_match_generic_spec (&op_type, name, &op);
8393 if (m == MATCH_ERROR)
8394 return MATCH_ERROR;
8395 if (m == MATCH_NO)
8397 gfc_error ("Expected generic name or operator descriptor at %C");
8398 goto error;
8401 switch (op_type)
8403 case INTERFACE_GENERIC:
8404 snprintf (bind_name, sizeof (bind_name), "%s", name);
8405 break;
8407 case INTERFACE_USER_OP:
8408 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8409 break;
8411 case INTERFACE_INTRINSIC_OP:
8412 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8413 gfc_op2string (op));
8414 break;
8416 default:
8417 gcc_unreachable ();
8420 /* Match the required =>. */
8421 if (gfc_match (" =>") != MATCH_YES)
8423 gfc_error ("Expected '=>' at %C");
8424 goto error;
8427 /* Try to find existing GENERIC binding with this name / for this operator;
8428 if there is something, check that it is another GENERIC and then extend
8429 it rather than building a new node. Otherwise, create it and put it
8430 at the right position. */
8432 switch (op_type)
8434 case INTERFACE_USER_OP:
8435 case INTERFACE_GENERIC:
8437 const bool is_op = (op_type == INTERFACE_USER_OP);
8438 gfc_symtree* st;
8440 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8441 if (st)
8443 tb = st->n.tb;
8444 gcc_assert (tb);
8446 else
8447 tb = NULL;
8449 break;
8452 case INTERFACE_INTRINSIC_OP:
8453 tb = ns->tb_op[op];
8454 break;
8456 default:
8457 gcc_unreachable ();
8460 if (tb)
8462 if (!tb->is_generic)
8464 gcc_assert (op_type == INTERFACE_GENERIC);
8465 gfc_error ("There's already a non-generic procedure with binding name"
8466 " '%s' for the derived type '%s' at %C",
8467 bind_name, block->name);
8468 goto error;
8471 if (tb->access != tbattr.access)
8473 gfc_error ("Binding at %C must have the same access as already"
8474 " defined binding '%s'", bind_name);
8475 goto error;
8478 else
8480 tb = gfc_get_typebound_proc (NULL);
8481 tb->where = gfc_current_locus;
8482 tb->access = tbattr.access;
8483 tb->is_generic = 1;
8484 tb->u.generic = NULL;
8486 switch (op_type)
8488 case INTERFACE_GENERIC:
8489 case INTERFACE_USER_OP:
8491 const bool is_op = (op_type == INTERFACE_USER_OP);
8492 gfc_symtree* st;
8494 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8495 name);
8496 gcc_assert (st);
8497 st->n.tb = tb;
8499 break;
8502 case INTERFACE_INTRINSIC_OP:
8503 ns->tb_op[op] = tb;
8504 break;
8506 default:
8507 gcc_unreachable ();
8511 /* Now, match all following names as specific targets. */
8514 gfc_symtree* target_st;
8515 gfc_tbp_generic* target;
8517 m = gfc_match_name (name);
8518 if (m == MATCH_ERROR)
8519 goto error;
8520 if (m == MATCH_NO)
8522 gfc_error ("Expected specific binding name at %C");
8523 goto error;
8526 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8528 /* See if this is a duplicate specification. */
8529 for (target = tb->u.generic; target; target = target->next)
8530 if (target_st == target->specific_st)
8532 gfc_error ("'%s' already defined as specific binding for the"
8533 " generic '%s' at %C", name, bind_name);
8534 goto error;
8537 target = gfc_get_tbp_generic ();
8538 target->specific_st = target_st;
8539 target->specific = NULL;
8540 target->next = tb->u.generic;
8541 target->is_operator = ((op_type == INTERFACE_USER_OP)
8542 || (op_type == INTERFACE_INTRINSIC_OP));
8543 tb->u.generic = target;
8545 while (gfc_match (" ,") == MATCH_YES);
8547 /* Here should be the end. */
8548 if (gfc_match_eos () != MATCH_YES)
8550 gfc_error ("Junk after GENERIC binding at %C");
8551 goto error;
8554 return MATCH_YES;
8556 error:
8557 return MATCH_ERROR;
8561 /* Match a FINAL declaration inside a derived type. */
8563 match
8564 gfc_match_final_decl (void)
8566 char name[GFC_MAX_SYMBOL_LEN + 1];
8567 gfc_symbol* sym;
8568 match m;
8569 gfc_namespace* module_ns;
8570 bool first, last;
8571 gfc_symbol* block;
8573 if (gfc_current_form == FORM_FREE)
8575 char c = gfc_peek_ascii_char ();
8576 if (!gfc_is_whitespace (c) && c != ':')
8577 return MATCH_NO;
8580 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8582 if (gfc_current_form == FORM_FIXED)
8583 return MATCH_NO;
8585 gfc_error ("FINAL declaration at %C must be inside a derived type "
8586 "CONTAINS section");
8587 return MATCH_ERROR;
8590 block = gfc_state_stack->previous->sym;
8591 gcc_assert (block);
8593 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8594 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8596 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8597 " specification part of a MODULE");
8598 return MATCH_ERROR;
8601 module_ns = gfc_current_ns;
8602 gcc_assert (module_ns);
8603 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8605 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8606 if (gfc_match (" ::") == MATCH_ERROR)
8607 return MATCH_ERROR;
8609 /* Match the sequence of procedure names. */
8610 first = true;
8611 last = false;
8614 gfc_finalizer* f;
8616 if (first && gfc_match_eos () == MATCH_YES)
8618 gfc_error ("Empty FINAL at %C");
8619 return MATCH_ERROR;
8622 m = gfc_match_name (name);
8623 if (m == MATCH_NO)
8625 gfc_error ("Expected module procedure name at %C");
8626 return MATCH_ERROR;
8628 else if (m != MATCH_YES)
8629 return MATCH_ERROR;
8631 if (gfc_match_eos () == MATCH_YES)
8632 last = true;
8633 if (!last && gfc_match_char (',') != MATCH_YES)
8635 gfc_error ("Expected ',' at %C");
8636 return MATCH_ERROR;
8639 if (gfc_get_symbol (name, module_ns, &sym))
8641 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8642 return MATCH_ERROR;
8645 /* Mark the symbol as module procedure. */
8646 if (sym->attr.proc != PROC_MODULE
8647 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8648 return MATCH_ERROR;
8650 /* Check if we already have this symbol in the list, this is an error. */
8651 for (f = block->f2k_derived->finalizers; f; f = f->next)
8652 if (f->proc_sym == sym)
8654 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8655 name);
8656 return MATCH_ERROR;
8659 /* Add this symbol to the list of finalizers. */
8660 gcc_assert (block->f2k_derived);
8661 ++sym->refs;
8662 f = XCNEW (gfc_finalizer);
8663 f->proc_sym = sym;
8664 f->proc_tree = NULL;
8665 f->where = gfc_current_locus;
8666 f->next = block->f2k_derived->finalizers;
8667 block->f2k_derived->finalizers = f;
8669 first = false;
8671 while (!last);
8673 return MATCH_YES;
8677 const ext_attr_t ext_attr_list[] = {
8678 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8679 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8680 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8681 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8682 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8683 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
8684 { NULL, EXT_ATTR_LAST, NULL }
8687 /* Match a !GCC$ ATTRIBUTES statement of the form:
8688 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8689 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8691 TODO: We should support all GCC attributes using the same syntax for
8692 the attribute list, i.e. the list in C
8693 __attributes(( attribute-list ))
8694 matches then
8695 !GCC$ ATTRIBUTES attribute-list ::
8696 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8697 saved into a TREE.
8699 As there is absolutely no risk of confusion, we should never return
8700 MATCH_NO. */
8701 match
8702 gfc_match_gcc_attributes (void)
8704 symbol_attribute attr;
8705 char name[GFC_MAX_SYMBOL_LEN + 1];
8706 unsigned id;
8707 gfc_symbol *sym;
8708 match m;
8710 gfc_clear_attr (&attr);
8711 for(;;)
8713 char ch;
8715 if (gfc_match_name (name) != MATCH_YES)
8716 return MATCH_ERROR;
8718 for (id = 0; id < EXT_ATTR_LAST; id++)
8719 if (strcmp (name, ext_attr_list[id].name) == 0)
8720 break;
8722 if (id == EXT_ATTR_LAST)
8724 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8725 return MATCH_ERROR;
8728 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
8729 return MATCH_ERROR;
8731 gfc_gobble_whitespace ();
8732 ch = gfc_next_ascii_char ();
8733 if (ch == ':')
8735 /* This is the successful exit condition for the loop. */
8736 if (gfc_next_ascii_char () == ':')
8737 break;
8740 if (ch == ',')
8741 continue;
8743 goto syntax;
8746 if (gfc_match_eos () == MATCH_YES)
8747 goto syntax;
8749 for(;;)
8751 m = gfc_match_name (name);
8752 if (m != MATCH_YES)
8753 return m;
8755 if (find_special (name, &sym, true))
8756 return MATCH_ERROR;
8758 sym->attr.ext_attr |= attr.ext_attr;
8760 if (gfc_match_eos () == MATCH_YES)
8761 break;
8763 if (gfc_match_char (',') != MATCH_YES)
8764 goto syntax;
8767 return MATCH_YES;
8769 syntax:
8770 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8771 return MATCH_ERROR;